* config/tc-mips.c (mips_set_options): Add sym32 field.
[binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too. */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about. */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT 1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP 28
99 #define SP 29
100 #define FP 30
101 #define RA 31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format. */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
122 /* The ABI to use. */
123 enum mips_abi_level
124 {
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131 };
132
133 /* MIPS ABI we are using for this output file. */
134 static enum mips_abi_level mips_abi = NO_ABI;
135
136 /* Whether or not we have code that can call pic code. */
137 int mips_abicalls = FALSE;
138
139 /* Whether or not we have code which can be put into a shared
140 library. */
141 static bfd_boolean mips_in_shared = TRUE;
142
143 /* This is the set of options which may be modified by the .set
144 pseudo-op. We use a struct so that .set push and .set pop are more
145 reliable. */
146
147 struct mips_set_options
148 {
149 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
150 if it has not been initialized. Changed by `.set mipsN', and the
151 -mipsN command line option, and the default CPU. */
152 int isa;
153 /* Enabled Application Specific Extensions (ASEs). These are set to -1
154 if they have not been initialized. Changed by `.set <asename>', by
155 command line options, and based on the default architecture. */
156 int ase_mips3d;
157 int ase_mdmx;
158 /* Whether we are assembling for the mips16 processor. 0 if we are
159 not, 1 if we are, and -1 if the value has not been initialized.
160 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
161 -nomips16 command line options, and the default CPU. */
162 int mips16;
163 /* Non-zero if we should not reorder instructions. Changed by `.set
164 reorder' and `.set noreorder'. */
165 int noreorder;
166 /* Non-zero if we should not permit the $at ($1) register to be used
167 in instructions. Changed by `.set at' and `.set noat'. */
168 int noat;
169 /* Non-zero if we should warn when a macro instruction expands into
170 more than one machine instruction. Changed by `.set nomacro' and
171 `.set macro'. */
172 int warn_about_macros;
173 /* Non-zero if we should not move instructions. Changed by `.set
174 move', `.set volatile', `.set nomove', and `.set novolatile'. */
175 int nomove;
176 /* Non-zero if we should not optimize branches by moving the target
177 of the branch into the delay slot. Actually, we don't perform
178 this optimization anyhow. Changed by `.set bopt' and `.set
179 nobopt'. */
180 int nobopt;
181 /* Non-zero if we should not autoextend mips16 instructions.
182 Changed by `.set autoextend' and `.set noautoextend'. */
183 int noautoextend;
184 /* Restrict general purpose registers and floating point registers
185 to 32 bit. This is initially determined when -mgp32 or -mfp32
186 is passed but can changed if the assembler code uses .set mipsN. */
187 int gp32;
188 int fp32;
189 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
190 command line option, and the default CPU. */
191 int arch;
192 /* True if ".set sym32" is in effect. */
193 bfd_boolean sym32;
194 };
195
196 /* True if -mgp32 was passed. */
197 static int file_mips_gp32 = -1;
198
199 /* True if -mfp32 was passed. */
200 static int file_mips_fp32 = -1;
201
202 /* This is the struct we use to hold the current set of options. Note
203 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
204 -1 to indicate that they have not been initialized. */
205
206 static struct mips_set_options mips_opts =
207 {
208 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
209 };
210
211 /* These variables are filled in with the masks of registers used.
212 The object format code reads them and puts them in the appropriate
213 place. */
214 unsigned long mips_gprmask;
215 unsigned long mips_cprmask[4];
216
217 /* MIPS ISA we are using for this output file. */
218 static int file_mips_isa = ISA_UNKNOWN;
219
220 /* True if -mips16 was passed or implied by arguments passed on the
221 command line (e.g., by -march). */
222 static int file_ase_mips16;
223
224 /* True if -mips3d was passed or implied by arguments passed on the
225 command line (e.g., by -march). */
226 static int file_ase_mips3d;
227
228 /* True if -mdmx was passed or implied by arguments passed on the
229 command line (e.g., by -march). */
230 static int file_ase_mdmx;
231
232 /* The argument of the -march= flag. The architecture we are assembling. */
233 static int file_mips_arch = CPU_UNKNOWN;
234 static const char *mips_arch_string;
235
236 /* The argument of the -mtune= flag. The architecture for which we
237 are optimizing. */
238 static int mips_tune = CPU_UNKNOWN;
239 static const char *mips_tune_string;
240
241 /* True when generating 32-bit code for a 64-bit processor. */
242 static int mips_32bitmode = 0;
243
244 /* True if the given ABI requires 32-bit registers. */
245 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
246
247 /* Likewise 64-bit registers. */
248 #define ABI_NEEDS_64BIT_REGS(ABI) \
249 ((ABI) == N32_ABI \
250 || (ABI) == N64_ABI \
251 || (ABI) == O64_ABI)
252
253 /* Return true if ISA supports 64 bit gp register instructions. */
254 #define ISA_HAS_64BIT_REGS(ISA) ( \
255 (ISA) == ISA_MIPS3 \
256 || (ISA) == ISA_MIPS4 \
257 || (ISA) == ISA_MIPS5 \
258 || (ISA) == ISA_MIPS64 \
259 || (ISA) == ISA_MIPS64R2 \
260 )
261
262 /* Return true if ISA supports 64-bit right rotate (dror et al.)
263 instructions. */
264 #define ISA_HAS_DROR(ISA) ( \
265 (ISA) == ISA_MIPS64R2 \
266 )
267
268 /* Return true if ISA supports 32-bit right rotate (ror et al.)
269 instructions. */
270 #define ISA_HAS_ROR(ISA) ( \
271 (ISA) == ISA_MIPS32R2 \
272 || (ISA) == ISA_MIPS64R2 \
273 )
274
275 #define HAVE_32BIT_GPRS \
276 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
277
278 #define HAVE_32BIT_FPRS \
279 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
284 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
285
286 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
287
288 /* True if relocations are stored in-place. */
289 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
290
291 /* The ABI-derived address size. */
292 #define HAVE_64BIT_ADDRESSES \
293 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
294 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
295
296 /* The size of symbolic constants (i.e., expressions of the form
297 "SYMBOL" or "SYMBOL + OFFSET"). */
298 #define HAVE_32BIT_SYMBOLS \
299 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
300 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
301
302 /* Addresses are loaded in different ways, depending on the address size
303 in use. The n32 ABI Documentation also mandates the use of additions
304 with overflow checking, but existing implementations don't follow it. */
305 #define ADDRESS_ADD_INSN \
306 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
307
308 #define ADDRESS_ADDI_INSN \
309 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
310
311 #define ADDRESS_LOAD_INSN \
312 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
313
314 #define ADDRESS_STORE_INSN \
315 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
316
317 /* Return true if the given CPU supports the MIPS16 ASE. */
318 #define CPU_HAS_MIPS16(cpu) \
319 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
320 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
321
322 /* Return true if the given CPU supports the MIPS3D ASE. */
323 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
324 )
325
326 /* Return true if the given CPU supports the MDMX ASE. */
327 #define CPU_HAS_MDMX(cpu) (FALSE \
328 )
329
330 /* True if CPU has a dror instruction. */
331 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
332
333 /* True if CPU has a ror instruction. */
334 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
335
336 /* True if mflo and mfhi can be immediately followed by instructions
337 which write to the HI and LO registers.
338
339 According to MIPS specifications, MIPS ISAs I, II, and III need
340 (at least) two instructions between the reads of HI/LO and
341 instructions which write them, and later ISAs do not. Contradicting
342 the MIPS specifications, some MIPS IV processor user manuals (e.g.
343 the UM for the NEC Vr5000) document needing the instructions between
344 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
345 MIPS64 and later ISAs to have the interlocks, plus any specific
346 earlier-ISA CPUs for which CPU documentation declares that the
347 instructions are really interlocked. */
348 #define hilo_interlocks \
349 (mips_opts.isa == ISA_MIPS32 \
350 || mips_opts.isa == ISA_MIPS32R2 \
351 || mips_opts.isa == ISA_MIPS64 \
352 || mips_opts.isa == ISA_MIPS64R2 \
353 || mips_opts.arch == CPU_R4010 \
354 || mips_opts.arch == CPU_R10000 \
355 || mips_opts.arch == CPU_R12000 \
356 || mips_opts.arch == CPU_RM7000 \
357 || mips_opts.arch == CPU_VR5500 \
358 )
359
360 /* Whether the processor uses hardware interlocks to protect reads
361 from the GPRs after they are loaded from memory, and thus does not
362 require nops to be inserted. This applies to instructions marked
363 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
364 level I. */
365 #define gpr_interlocks \
366 (mips_opts.isa != ISA_MIPS1 \
367 || mips_opts.arch == CPU_R3900)
368
369 /* Whether the processor uses hardware interlocks to avoid delays
370 required by coprocessor instructions, and thus does not require
371 nops to be inserted. This applies to instructions marked
372 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
373 between instructions marked INSN_WRITE_COND_CODE and ones marked
374 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
375 levels I, II, and III. */
376 /* Itbl support may require additional care here. */
377 #define cop_interlocks \
378 ((mips_opts.isa != ISA_MIPS1 \
379 && mips_opts.isa != ISA_MIPS2 \
380 && mips_opts.isa != ISA_MIPS3) \
381 || mips_opts.arch == CPU_R4300 \
382 )
383
384 /* Whether the processor uses hardware interlocks to protect reads
385 from coprocessor registers after they are loaded from memory, and
386 thus does not require nops to be inserted. This applies to
387 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
388 requires at MIPS ISA level I. */
389 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
390
391 /* Is this a mfhi or mflo instruction? */
392 #define MF_HILO_INSN(PINFO) \
393 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
394
395 /* MIPS PIC level. */
396
397 enum mips_pic_level mips_pic;
398
399 /* 1 if we should generate 32 bit offsets from the $gp register in
400 SVR4_PIC mode. Currently has no meaning in other modes. */
401 static int mips_big_got = 0;
402
403 /* 1 if trap instructions should used for overflow rather than break
404 instructions. */
405 static int mips_trap = 0;
406
407 /* 1 if double width floating point constants should not be constructed
408 by assembling two single width halves into two single width floating
409 point registers which just happen to alias the double width destination
410 register. On some architectures this aliasing can be disabled by a bit
411 in the status register, and the setting of this bit cannot be determined
412 automatically at assemble time. */
413 static int mips_disable_float_construction;
414
415 /* Non-zero if any .set noreorder directives were used. */
416
417 static int mips_any_noreorder;
418
419 /* Non-zero if nops should be inserted when the register referenced in
420 an mfhi/mflo instruction is read in the next two instructions. */
421 static int mips_7000_hilo_fix;
422
423 /* The size of the small data section. */
424 static unsigned int g_switch_value = 8;
425 /* Whether the -G option was used. */
426 static int g_switch_seen = 0;
427
428 #define N_RMASK 0xc4
429 #define N_VFP 0xd4
430
431 /* If we can determine in advance that GP optimization won't be
432 possible, we can skip the relaxation stuff that tries to produce
433 GP-relative references. This makes delay slot optimization work
434 better.
435
436 This function can only provide a guess, but it seems to work for
437 gcc output. It needs to guess right for gcc, otherwise gcc
438 will put what it thinks is a GP-relative instruction in a branch
439 delay slot.
440
441 I don't know if a fix is needed for the SVR4_PIC mode. I've only
442 fixed it for the non-PIC mode. KR 95/04/07 */
443 static int nopic_need_relax (symbolS *, int);
444
445 /* handle of the OPCODE hash table */
446 static struct hash_control *op_hash = NULL;
447
448 /* The opcode hash table we use for the mips16. */
449 static struct hash_control *mips16_op_hash = NULL;
450
451 /* This array holds the chars that always start a comment. If the
452 pre-processor is disabled, these aren't very useful */
453 const char comment_chars[] = "#";
454
455 /* This array holds the chars that only start a comment at the beginning of
456 a line. If the line seems to have the form '# 123 filename'
457 .line and .file directives will appear in the pre-processed output */
458 /* Note that input_file.c hand checks for '#' at the beginning of the
459 first line of the input file. This is because the compiler outputs
460 #NO_APP at the beginning of its output. */
461 /* Also note that C style comments are always supported. */
462 const char line_comment_chars[] = "#";
463
464 /* This array holds machine specific line separator characters. */
465 const char line_separator_chars[] = ";";
466
467 /* Chars that can be used to separate mant from exp in floating point nums */
468 const char EXP_CHARS[] = "eE";
469
470 /* Chars that mean this number is a floating point constant */
471 /* As in 0f12.456 */
472 /* or 0d1.2345e12 */
473 const char FLT_CHARS[] = "rRsSfFdDxXpP";
474
475 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
476 changed in read.c . Ideally it shouldn't have to know about it at all,
477 but nothing is ideal around here.
478 */
479
480 static char *insn_error;
481
482 static int auto_align = 1;
483
484 /* When outputting SVR4 PIC code, the assembler needs to know the
485 offset in the stack frame from which to restore the $gp register.
486 This is set by the .cprestore pseudo-op, and saved in this
487 variable. */
488 static offsetT mips_cprestore_offset = -1;
489
490 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
491 more optimizations, it can use a register value instead of a memory-saved
492 offset and even an other register than $gp as global pointer. */
493 static offsetT mips_cpreturn_offset = -1;
494 static int mips_cpreturn_register = -1;
495 static int mips_gp_register = GP;
496 static int mips_gprel_offset = 0;
497
498 /* Whether mips_cprestore_offset has been set in the current function
499 (or whether it has already been warned about, if not). */
500 static int mips_cprestore_valid = 0;
501
502 /* This is the register which holds the stack frame, as set by the
503 .frame pseudo-op. This is needed to implement .cprestore. */
504 static int mips_frame_reg = SP;
505
506 /* Whether mips_frame_reg has been set in the current function
507 (or whether it has already been warned about, if not). */
508 static int mips_frame_reg_valid = 0;
509
510 /* To output NOP instructions correctly, we need to keep information
511 about the previous two instructions. */
512
513 /* Whether we are optimizing. The default value of 2 means to remove
514 unneeded NOPs and swap branch instructions when possible. A value
515 of 1 means to not swap branches. A value of 0 means to always
516 insert NOPs. */
517 static int mips_optimize = 2;
518
519 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
520 equivalent to seeing no -g option at all. */
521 static int mips_debug = 0;
522
523 /* The previous instruction. */
524 static struct mips_cl_insn prev_insn;
525
526 /* The instruction before prev_insn. */
527 static struct mips_cl_insn prev_prev_insn;
528
529 /* If we don't want information for prev_insn or prev_prev_insn, we
530 point the insn_mo field at this dummy integer. */
531 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0, 0 };
532
533 /* Non-zero if prev_insn is valid. */
534 static int prev_insn_valid;
535
536 /* The frag for the previous instruction. */
537 static struct frag *prev_insn_frag;
538
539 /* The offset into prev_insn_frag for the previous instruction. */
540 static long prev_insn_where;
541
542 /* The reloc type for the previous instruction, if any. */
543 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
544
545 /* The reloc for the previous instruction, if any. */
546 static fixS *prev_insn_fixp[3];
547
548 /* Non-zero if the previous instruction was in a delay slot. */
549 static int prev_insn_is_delay_slot;
550
551 /* Non-zero if the previous instruction was in a .set noreorder. */
552 static int prev_insn_unreordered;
553
554 /* Non-zero if the previous instruction uses an extend opcode (if
555 mips16). */
556 static int prev_insn_extended;
557
558 /* Non-zero if the previous previous instruction was in a .set
559 noreorder. */
560 static int prev_prev_insn_unreordered;
561
562 /* If this is set, it points to a frag holding nop instructions which
563 were inserted before the start of a noreorder section. If those
564 nops turn out to be unnecessary, the size of the frag can be
565 decreased. */
566 static fragS *prev_nop_frag;
567
568 /* The number of nop instructions we created in prev_nop_frag. */
569 static int prev_nop_frag_holds;
570
571 /* The number of nop instructions that we know we need in
572 prev_nop_frag. */
573 static int prev_nop_frag_required;
574
575 /* The number of instructions we've seen since prev_nop_frag. */
576 static int prev_nop_frag_since;
577
578 /* For ECOFF and ELF, relocations against symbols are done in two
579 parts, with a HI relocation and a LO relocation. Each relocation
580 has only 16 bits of space to store an addend. This means that in
581 order for the linker to handle carries correctly, it must be able
582 to locate both the HI and the LO relocation. This means that the
583 relocations must appear in order in the relocation table.
584
585 In order to implement this, we keep track of each unmatched HI
586 relocation. We then sort them so that they immediately precede the
587 corresponding LO relocation. */
588
589 struct mips_hi_fixup
590 {
591 /* Next HI fixup. */
592 struct mips_hi_fixup *next;
593 /* This fixup. */
594 fixS *fixp;
595 /* The section this fixup is in. */
596 segT seg;
597 };
598
599 /* The list of unmatched HI relocs. */
600
601 static struct mips_hi_fixup *mips_hi_fixup_list;
602
603 /* The frag containing the last explicit relocation operator.
604 Null if explicit relocations have not been used. */
605
606 static fragS *prev_reloc_op_frag;
607
608 /* Map normal MIPS register numbers to mips16 register numbers. */
609
610 #define X ILLEGAL_REG
611 static const int mips32_to_16_reg_map[] =
612 {
613 X, X, 2, 3, 4, 5, 6, 7,
614 X, X, X, X, X, X, X, X,
615 0, 1, X, X, X, X, X, X,
616 X, X, X, X, X, X, X, X
617 };
618 #undef X
619
620 /* Map mips16 register numbers to normal MIPS register numbers. */
621
622 static const unsigned int mips16_to_32_reg_map[] =
623 {
624 16, 17, 2, 3, 4, 5, 6, 7
625 };
626
627 static int mips_fix_vr4120;
628
629 /* We don't relax branches by default, since this causes us to expand
630 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
631 fail to compute the offset before expanding the macro to the most
632 efficient expansion. */
633
634 static int mips_relax_branch;
635 \f
636 /* The expansion of many macros depends on the type of symbol that
637 they refer to. For example, when generating position-dependent code,
638 a macro that refers to a symbol may have two different expansions,
639 one which uses GP-relative addresses and one which uses absolute
640 addresses. When generating SVR4-style PIC, a macro may have
641 different expansions for local and global symbols.
642
643 We handle these situations by generating both sequences and putting
644 them in variant frags. In position-dependent code, the first sequence
645 will be the GP-relative one and the second sequence will be the
646 absolute one. In SVR4 PIC, the first sequence will be for global
647 symbols and the second will be for local symbols.
648
649 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
650 SECOND are the lengths of the two sequences in bytes. These fields
651 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
652 the subtype has the following flags:
653
654 RELAX_USE_SECOND
655 Set if it has been decided that we should use the second
656 sequence instead of the first.
657
658 RELAX_SECOND_LONGER
659 Set in the first variant frag if the macro's second implementation
660 is longer than its first. This refers to the macro as a whole,
661 not an individual relaxation.
662
663 RELAX_NOMACRO
664 Set in the first variant frag if the macro appeared in a .set nomacro
665 block and if one alternative requires a warning but the other does not.
666
667 RELAX_DELAY_SLOT
668 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
669 delay slot.
670
671 The frag's "opcode" points to the first fixup for relaxable code.
672
673 Relaxable macros are generated using a sequence such as:
674
675 relax_start (SYMBOL);
676 ... generate first expansion ...
677 relax_switch ();
678 ... generate second expansion ...
679 relax_end ();
680
681 The code and fixups for the unwanted alternative are discarded
682 by md_convert_frag. */
683 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
684
685 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
686 #define RELAX_SECOND(X) ((X) & 0xff)
687 #define RELAX_USE_SECOND 0x10000
688 #define RELAX_SECOND_LONGER 0x20000
689 #define RELAX_NOMACRO 0x40000
690 #define RELAX_DELAY_SLOT 0x80000
691
692 /* Branch without likely bit. If label is out of range, we turn:
693
694 beq reg1, reg2, label
695 delay slot
696
697 into
698
699 bne reg1, reg2, 0f
700 nop
701 j label
702 0: delay slot
703
704 with the following opcode replacements:
705
706 beq <-> bne
707 blez <-> bgtz
708 bltz <-> bgez
709 bc1f <-> bc1t
710
711 bltzal <-> bgezal (with jal label instead of j label)
712
713 Even though keeping the delay slot instruction in the delay slot of
714 the branch would be more efficient, it would be very tricky to do
715 correctly, because we'd have to introduce a variable frag *after*
716 the delay slot instruction, and expand that instead. Let's do it
717 the easy way for now, even if the branch-not-taken case now costs
718 one additional instruction. Out-of-range branches are not supposed
719 to be common, anyway.
720
721 Branch likely. If label is out of range, we turn:
722
723 beql reg1, reg2, label
724 delay slot (annulled if branch not taken)
725
726 into
727
728 beql reg1, reg2, 1f
729 nop
730 beql $0, $0, 2f
731 nop
732 1: j[al] label
733 delay slot (executed only if branch taken)
734 2:
735
736 It would be possible to generate a shorter sequence by losing the
737 likely bit, generating something like:
738
739 bne reg1, reg2, 0f
740 nop
741 j[al] label
742 delay slot (executed only if branch taken)
743 0:
744
745 beql -> bne
746 bnel -> beq
747 blezl -> bgtz
748 bgtzl -> blez
749 bltzl -> bgez
750 bgezl -> bltz
751 bc1fl -> bc1t
752 bc1tl -> bc1f
753
754 bltzall -> bgezal (with jal label instead of j label)
755 bgezall -> bltzal (ditto)
756
757
758 but it's not clear that it would actually improve performance. */
759 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
760 ((relax_substateT) \
761 (0xc0000000 \
762 | ((toofar) ? 1 : 0) \
763 | ((link) ? 2 : 0) \
764 | ((likely) ? 4 : 0) \
765 | ((uncond) ? 8 : 0)))
766 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
767 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
768 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
769 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
770 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
771
772 /* For mips16 code, we use an entirely different form of relaxation.
773 mips16 supports two versions of most instructions which take
774 immediate values: a small one which takes some small value, and a
775 larger one which takes a 16 bit value. Since branches also follow
776 this pattern, relaxing these values is required.
777
778 We can assemble both mips16 and normal MIPS code in a single
779 object. Therefore, we need to support this type of relaxation at
780 the same time that we support the relaxation described above. We
781 use the high bit of the subtype field to distinguish these cases.
782
783 The information we store for this type of relaxation is the
784 argument code found in the opcode file for this relocation, whether
785 the user explicitly requested a small or extended form, and whether
786 the relocation is in a jump or jal delay slot. That tells us the
787 size of the value, and how it should be stored. We also store
788 whether the fragment is considered to be extended or not. We also
789 store whether this is known to be a branch to a different section,
790 whether we have tried to relax this frag yet, and whether we have
791 ever extended a PC relative fragment because of a shift count. */
792 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
793 (0x80000000 \
794 | ((type) & 0xff) \
795 | ((small) ? 0x100 : 0) \
796 | ((ext) ? 0x200 : 0) \
797 | ((dslot) ? 0x400 : 0) \
798 | ((jal_dslot) ? 0x800 : 0))
799 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
800 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
801 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
802 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
803 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
804 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
805 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
806 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
807 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
808 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
809 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
810 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
811
812 /* Is the given value a sign-extended 32-bit value? */
813 #define IS_SEXT_32BIT_NUM(x) \
814 (((x) &~ (offsetT) 0x7fffffff) == 0 \
815 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
816
817 /* Is the given value a sign-extended 16-bit value? */
818 #define IS_SEXT_16BIT_NUM(x) \
819 (((x) &~ (offsetT) 0x7fff) == 0 \
820 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
821
822 \f
823 /* Global variables used when generating relaxable macros. See the
824 comment above RELAX_ENCODE for more details about how relaxation
825 is used. */
826 static struct {
827 /* 0 if we're not emitting a relaxable macro.
828 1 if we're emitting the first of the two relaxation alternatives.
829 2 if we're emitting the second alternative. */
830 int sequence;
831
832 /* The first relaxable fixup in the current frag. (In other words,
833 the first fixup that refers to relaxable code.) */
834 fixS *first_fixup;
835
836 /* sizes[0] says how many bytes of the first alternative are stored in
837 the current frag. Likewise sizes[1] for the second alternative. */
838 unsigned int sizes[2];
839
840 /* The symbol on which the choice of sequence depends. */
841 symbolS *symbol;
842 } mips_relax;
843 \f
844 /* Global variables used to decide whether a macro needs a warning. */
845 static struct {
846 /* True if the macro is in a branch delay slot. */
847 bfd_boolean delay_slot_p;
848
849 /* For relaxable macros, sizes[0] is the length of the first alternative
850 in bytes and sizes[1] is the length of the second alternative.
851 For non-relaxable macros, both elements give the length of the
852 macro in bytes. */
853 unsigned int sizes[2];
854
855 /* The first variant frag for this macro. */
856 fragS *first_frag;
857 } mips_macro_warning;
858 \f
859 /* Prototypes for static functions. */
860
861 #define internalError() \
862 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
863
864 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
865
866 static void append_insn
867 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
868 static void mips_no_prev_insn (int);
869 static void mips16_macro_build
870 (expressionS *, const char *, const char *, va_list);
871 static void load_register (int, expressionS *, int);
872 static void macro_start (void);
873 static void macro_end (void);
874 static void macro (struct mips_cl_insn * ip);
875 static void mips16_macro (struct mips_cl_insn * ip);
876 #ifdef LOSING_COMPILER
877 static void macro2 (struct mips_cl_insn * ip);
878 #endif
879 static void mips_ip (char *str, struct mips_cl_insn * ip);
880 static void mips16_ip (char *str, struct mips_cl_insn * ip);
881 static void mips16_immed
882 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
883 unsigned long *, bfd_boolean *, unsigned short *);
884 static size_t my_getSmallExpression
885 (expressionS *, bfd_reloc_code_real_type *, char *);
886 static void my_getExpression (expressionS *, char *);
887 static void s_align (int);
888 static void s_change_sec (int);
889 static void s_change_section (int);
890 static void s_cons (int);
891 static void s_float_cons (int);
892 static void s_mips_globl (int);
893 static void s_option (int);
894 static void s_mipsset (int);
895 static void s_abicalls (int);
896 static void s_cpload (int);
897 static void s_cpsetup (int);
898 static void s_cplocal (int);
899 static void s_cprestore (int);
900 static void s_cpreturn (int);
901 static void s_gpvalue (int);
902 static void s_gpword (int);
903 static void s_gpdword (int);
904 static void s_cpadd (int);
905 static void s_insn (int);
906 static void md_obj_begin (void);
907 static void md_obj_end (void);
908 static void s_mips_ent (int);
909 static void s_mips_end (int);
910 static void s_mips_frame (int);
911 static void s_mips_mask (int reg_type);
912 static void s_mips_stab (int);
913 static void s_mips_weakext (int);
914 static void s_mips_file (int);
915 static void s_mips_loc (int);
916 static bfd_boolean pic_need_relax (symbolS *, asection *);
917 static int relaxed_branch_length (fragS *, asection *, int);
918 static int validate_mips_insn (const struct mips_opcode *);
919
920 /* Table and functions used to map between CPU/ISA names, and
921 ISA levels, and CPU numbers. */
922
923 struct mips_cpu_info
924 {
925 const char *name; /* CPU or ISA name. */
926 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
927 int isa; /* ISA level. */
928 int cpu; /* CPU number (default CPU if ISA). */
929 };
930
931 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
932 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
933 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
934 \f
935 /* Pseudo-op table.
936
937 The following pseudo-ops from the Kane and Heinrich MIPS book
938 should be defined here, but are currently unsupported: .alias,
939 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
940
941 The following pseudo-ops from the Kane and Heinrich MIPS book are
942 specific to the type of debugging information being generated, and
943 should be defined by the object format: .aent, .begin, .bend,
944 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
945 .vreg.
946
947 The following pseudo-ops from the Kane and Heinrich MIPS book are
948 not MIPS CPU specific, but are also not specific to the object file
949 format. This file is probably the best place to define them, but
950 they are not currently supported: .asm0, .endr, .lab, .repeat,
951 .struct. */
952
953 static const pseudo_typeS mips_pseudo_table[] =
954 {
955 /* MIPS specific pseudo-ops. */
956 {"option", s_option, 0},
957 {"set", s_mipsset, 0},
958 {"rdata", s_change_sec, 'r'},
959 {"sdata", s_change_sec, 's'},
960 {"livereg", s_ignore, 0},
961 {"abicalls", s_abicalls, 0},
962 {"cpload", s_cpload, 0},
963 {"cpsetup", s_cpsetup, 0},
964 {"cplocal", s_cplocal, 0},
965 {"cprestore", s_cprestore, 0},
966 {"cpreturn", s_cpreturn, 0},
967 {"gpvalue", s_gpvalue, 0},
968 {"gpword", s_gpword, 0},
969 {"gpdword", s_gpdword, 0},
970 {"cpadd", s_cpadd, 0},
971 {"insn", s_insn, 0},
972
973 /* Relatively generic pseudo-ops that happen to be used on MIPS
974 chips. */
975 {"asciiz", stringer, 1},
976 {"bss", s_change_sec, 'b'},
977 {"err", s_err, 0},
978 {"half", s_cons, 1},
979 {"dword", s_cons, 3},
980 {"weakext", s_mips_weakext, 0},
981
982 /* These pseudo-ops are defined in read.c, but must be overridden
983 here for one reason or another. */
984 {"align", s_align, 0},
985 {"byte", s_cons, 0},
986 {"data", s_change_sec, 'd'},
987 {"double", s_float_cons, 'd'},
988 {"float", s_float_cons, 'f'},
989 {"globl", s_mips_globl, 0},
990 {"global", s_mips_globl, 0},
991 {"hword", s_cons, 1},
992 {"int", s_cons, 2},
993 {"long", s_cons, 2},
994 {"octa", s_cons, 4},
995 {"quad", s_cons, 3},
996 {"section", s_change_section, 0},
997 {"short", s_cons, 1},
998 {"single", s_float_cons, 'f'},
999 {"stabn", s_mips_stab, 'n'},
1000 {"text", s_change_sec, 't'},
1001 {"word", s_cons, 2},
1002
1003 { "extern", ecoff_directive_extern, 0},
1004
1005 { NULL, NULL, 0 },
1006 };
1007
1008 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1009 {
1010 /* These pseudo-ops should be defined by the object file format.
1011 However, a.out doesn't support them, so we have versions here. */
1012 {"aent", s_mips_ent, 1},
1013 {"bgnb", s_ignore, 0},
1014 {"end", s_mips_end, 0},
1015 {"endb", s_ignore, 0},
1016 {"ent", s_mips_ent, 0},
1017 {"file", s_mips_file, 0},
1018 {"fmask", s_mips_mask, 'F'},
1019 {"frame", s_mips_frame, 0},
1020 {"loc", s_mips_loc, 0},
1021 {"mask", s_mips_mask, 'R'},
1022 {"verstamp", s_ignore, 0},
1023 { NULL, NULL, 0 },
1024 };
1025
1026 extern void pop_insert (const pseudo_typeS *);
1027
1028 void
1029 mips_pop_insert (void)
1030 {
1031 pop_insert (mips_pseudo_table);
1032 if (! ECOFF_DEBUGGING)
1033 pop_insert (mips_nonecoff_pseudo_table);
1034 }
1035 \f
1036 /* Symbols labelling the current insn. */
1037
1038 struct insn_label_list
1039 {
1040 struct insn_label_list *next;
1041 symbolS *label;
1042 };
1043
1044 static struct insn_label_list *insn_labels;
1045 static struct insn_label_list *free_insn_labels;
1046
1047 static void mips_clear_insn_labels (void);
1048
1049 static inline void
1050 mips_clear_insn_labels (void)
1051 {
1052 register struct insn_label_list **pl;
1053
1054 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1055 ;
1056 *pl = insn_labels;
1057 insn_labels = NULL;
1058 }
1059 \f
1060 static char *expr_end;
1061
1062 /* Expressions which appear in instructions. These are set by
1063 mips_ip. */
1064
1065 static expressionS imm_expr;
1066 static expressionS imm2_expr;
1067 static expressionS offset_expr;
1068
1069 /* Relocs associated with imm_expr and offset_expr. */
1070
1071 static bfd_reloc_code_real_type imm_reloc[3]
1072 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1073 static bfd_reloc_code_real_type offset_reloc[3]
1074 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1075
1076 /* These are set by mips16_ip if an explicit extension is used. */
1077
1078 static bfd_boolean mips16_small, mips16_ext;
1079
1080 #ifdef OBJ_ELF
1081 /* The pdr segment for per procedure frame/regmask info. Not used for
1082 ECOFF debugging. */
1083
1084 static segT pdr_seg;
1085 #endif
1086
1087 /* The default target format to use. */
1088
1089 const char *
1090 mips_target_format (void)
1091 {
1092 switch (OUTPUT_FLAVOR)
1093 {
1094 case bfd_target_ecoff_flavour:
1095 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1096 case bfd_target_coff_flavour:
1097 return "pe-mips";
1098 case bfd_target_elf_flavour:
1099 #ifdef TE_TMIPS
1100 /* This is traditional mips. */
1101 return (target_big_endian
1102 ? (HAVE_64BIT_OBJECTS
1103 ? "elf64-tradbigmips"
1104 : (HAVE_NEWABI
1105 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1106 : (HAVE_64BIT_OBJECTS
1107 ? "elf64-tradlittlemips"
1108 : (HAVE_NEWABI
1109 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1110 #else
1111 return (target_big_endian
1112 ? (HAVE_64BIT_OBJECTS
1113 ? "elf64-bigmips"
1114 : (HAVE_NEWABI
1115 ? "elf32-nbigmips" : "elf32-bigmips"))
1116 : (HAVE_64BIT_OBJECTS
1117 ? "elf64-littlemips"
1118 : (HAVE_NEWABI
1119 ? "elf32-nlittlemips" : "elf32-littlemips")));
1120 #endif
1121 default:
1122 abort ();
1123 return NULL;
1124 }
1125 }
1126
1127 /* This function is called once, at assembler startup time. It should
1128 set up all the tables, etc. that the MD part of the assembler will need. */
1129
1130 void
1131 md_begin (void)
1132 {
1133 register const char *retval = NULL;
1134 int i = 0;
1135 int broken = 0;
1136
1137 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1138 as_warn (_("Could not set architecture and machine"));
1139
1140 op_hash = hash_new ();
1141
1142 for (i = 0; i < NUMOPCODES;)
1143 {
1144 const char *name = mips_opcodes[i].name;
1145
1146 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1147 if (retval != NULL)
1148 {
1149 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1150 mips_opcodes[i].name, retval);
1151 /* Probably a memory allocation problem? Give up now. */
1152 as_fatal (_("Broken assembler. No assembly attempted."));
1153 }
1154 do
1155 {
1156 if (mips_opcodes[i].pinfo != INSN_MACRO)
1157 {
1158 if (!validate_mips_insn (&mips_opcodes[i]))
1159 broken = 1;
1160 }
1161 ++i;
1162 }
1163 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1164 }
1165
1166 mips16_op_hash = hash_new ();
1167
1168 i = 0;
1169 while (i < bfd_mips16_num_opcodes)
1170 {
1171 const char *name = mips16_opcodes[i].name;
1172
1173 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1174 if (retval != NULL)
1175 as_fatal (_("internal: can't hash `%s': %s"),
1176 mips16_opcodes[i].name, retval);
1177 do
1178 {
1179 if (mips16_opcodes[i].pinfo != INSN_MACRO
1180 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1181 != mips16_opcodes[i].match))
1182 {
1183 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1184 mips16_opcodes[i].name, mips16_opcodes[i].args);
1185 broken = 1;
1186 }
1187 ++i;
1188 }
1189 while (i < bfd_mips16_num_opcodes
1190 && strcmp (mips16_opcodes[i].name, name) == 0);
1191 }
1192
1193 if (broken)
1194 as_fatal (_("Broken assembler. No assembly attempted."));
1195
1196 /* We add all the general register names to the symbol table. This
1197 helps us detect invalid uses of them. */
1198 for (i = 0; i < 32; i++)
1199 {
1200 char buf[5];
1201
1202 sprintf (buf, "$%d", i);
1203 symbol_table_insert (symbol_new (buf, reg_section, i,
1204 &zero_address_frag));
1205 }
1206 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1207 &zero_address_frag));
1208 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1209 &zero_address_frag));
1210 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1211 &zero_address_frag));
1212 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1213 &zero_address_frag));
1214 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1215 &zero_address_frag));
1216 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1217 &zero_address_frag));
1218 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1219 &zero_address_frag));
1220 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1221 &zero_address_frag));
1222 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1223 &zero_address_frag));
1224
1225 /* If we don't add these register names to the symbol table, they
1226 may end up being added as regular symbols by operand(), and then
1227 make it to the object file as undefined in case they're not
1228 regarded as local symbols. They're local in o32, since `$' is a
1229 local symbol prefix, but not in n32 or n64. */
1230 for (i = 0; i < 8; i++)
1231 {
1232 char buf[6];
1233
1234 sprintf (buf, "$fcc%i", i);
1235 symbol_table_insert (symbol_new (buf, reg_section, -1,
1236 &zero_address_frag));
1237 }
1238
1239 mips_no_prev_insn (FALSE);
1240
1241 mips_gprmask = 0;
1242 mips_cprmask[0] = 0;
1243 mips_cprmask[1] = 0;
1244 mips_cprmask[2] = 0;
1245 mips_cprmask[3] = 0;
1246
1247 /* set the default alignment for the text section (2**2) */
1248 record_alignment (text_section, 2);
1249
1250 bfd_set_gp_size (stdoutput, g_switch_value);
1251
1252 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1253 {
1254 /* On a native system, sections must be aligned to 16 byte
1255 boundaries. When configured for an embedded ELF target, we
1256 don't bother. */
1257 if (strcmp (TARGET_OS, "elf") != 0)
1258 {
1259 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1260 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1261 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1262 }
1263
1264 /* Create a .reginfo section for register masks and a .mdebug
1265 section for debugging information. */
1266 {
1267 segT seg;
1268 subsegT subseg;
1269 flagword flags;
1270 segT sec;
1271
1272 seg = now_seg;
1273 subseg = now_subseg;
1274
1275 /* The ABI says this section should be loaded so that the
1276 running program can access it. However, we don't load it
1277 if we are configured for an embedded target */
1278 flags = SEC_READONLY | SEC_DATA;
1279 if (strcmp (TARGET_OS, "elf") != 0)
1280 flags |= SEC_ALLOC | SEC_LOAD;
1281
1282 if (mips_abi != N64_ABI)
1283 {
1284 sec = subseg_new (".reginfo", (subsegT) 0);
1285
1286 bfd_set_section_flags (stdoutput, sec, flags);
1287 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1288
1289 #ifdef OBJ_ELF
1290 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1291 #endif
1292 }
1293 else
1294 {
1295 /* The 64-bit ABI uses a .MIPS.options section rather than
1296 .reginfo section. */
1297 sec = subseg_new (".MIPS.options", (subsegT) 0);
1298 bfd_set_section_flags (stdoutput, sec, flags);
1299 bfd_set_section_alignment (stdoutput, sec, 3);
1300
1301 #ifdef OBJ_ELF
1302 /* Set up the option header. */
1303 {
1304 Elf_Internal_Options opthdr;
1305 char *f;
1306
1307 opthdr.kind = ODK_REGINFO;
1308 opthdr.size = (sizeof (Elf_External_Options)
1309 + sizeof (Elf64_External_RegInfo));
1310 opthdr.section = 0;
1311 opthdr.info = 0;
1312 f = frag_more (sizeof (Elf_External_Options));
1313 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1314 (Elf_External_Options *) f);
1315
1316 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1317 }
1318 #endif
1319 }
1320
1321 if (ECOFF_DEBUGGING)
1322 {
1323 sec = subseg_new (".mdebug", (subsegT) 0);
1324 (void) bfd_set_section_flags (stdoutput, sec,
1325 SEC_HAS_CONTENTS | SEC_READONLY);
1326 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1327 }
1328 #ifdef OBJ_ELF
1329 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1330 {
1331 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1332 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1333 SEC_READONLY | SEC_RELOC
1334 | SEC_DEBUGGING);
1335 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1336 }
1337 #endif
1338
1339 subseg_set (seg, subseg);
1340 }
1341 }
1342
1343 if (! ECOFF_DEBUGGING)
1344 md_obj_begin ();
1345 }
1346
1347 void
1348 md_mips_end (void)
1349 {
1350 if (! ECOFF_DEBUGGING)
1351 md_obj_end ();
1352 }
1353
1354 void
1355 md_assemble (char *str)
1356 {
1357 struct mips_cl_insn insn;
1358 bfd_reloc_code_real_type unused_reloc[3]
1359 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1360
1361 imm_expr.X_op = O_absent;
1362 imm2_expr.X_op = O_absent;
1363 offset_expr.X_op = O_absent;
1364 imm_reloc[0] = BFD_RELOC_UNUSED;
1365 imm_reloc[1] = BFD_RELOC_UNUSED;
1366 imm_reloc[2] = BFD_RELOC_UNUSED;
1367 offset_reloc[0] = BFD_RELOC_UNUSED;
1368 offset_reloc[1] = BFD_RELOC_UNUSED;
1369 offset_reloc[2] = BFD_RELOC_UNUSED;
1370
1371 if (mips_opts.mips16)
1372 mips16_ip (str, &insn);
1373 else
1374 {
1375 mips_ip (str, &insn);
1376 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1377 str, insn.insn_opcode));
1378 }
1379
1380 if (insn_error)
1381 {
1382 as_bad ("%s `%s'", insn_error, str);
1383 return;
1384 }
1385
1386 if (insn.insn_mo->pinfo == INSN_MACRO)
1387 {
1388 macro_start ();
1389 if (mips_opts.mips16)
1390 mips16_macro (&insn);
1391 else
1392 macro (&insn);
1393 macro_end ();
1394 }
1395 else
1396 {
1397 if (imm_expr.X_op != O_absent)
1398 append_insn (&insn, &imm_expr, imm_reloc);
1399 else if (offset_expr.X_op != O_absent)
1400 append_insn (&insn, &offset_expr, offset_reloc);
1401 else
1402 append_insn (&insn, NULL, unused_reloc);
1403 }
1404 }
1405
1406 /* Return true if the given relocation might need a matching %lo().
1407 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1408 applied to local symbols. */
1409
1410 static inline bfd_boolean
1411 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1412 {
1413 return (HAVE_IN_PLACE_ADDENDS
1414 && (reloc == BFD_RELOC_HI16_S
1415 || reloc == BFD_RELOC_MIPS_GOT16
1416 || reloc == BFD_RELOC_MIPS16_HI16_S));
1417 }
1418
1419 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1420 relocation. */
1421
1422 static inline bfd_boolean
1423 fixup_has_matching_lo_p (fixS *fixp)
1424 {
1425 return (fixp->fx_next != NULL
1426 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1427 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1428 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1429 && fixp->fx_offset == fixp->fx_next->fx_offset);
1430 }
1431
1432 /* See whether instruction IP reads register REG. CLASS is the type
1433 of register. */
1434
1435 static int
1436 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1437 enum mips_regclass class)
1438 {
1439 if (class == MIPS16_REG)
1440 {
1441 assert (mips_opts.mips16);
1442 reg = mips16_to_32_reg_map[reg];
1443 class = MIPS_GR_REG;
1444 }
1445
1446 /* Don't report on general register ZERO, since it never changes. */
1447 if (class == MIPS_GR_REG && reg == ZERO)
1448 return 0;
1449
1450 if (class == MIPS_FP_REG)
1451 {
1452 assert (! mips_opts.mips16);
1453 /* If we are called with either $f0 or $f1, we must check $f0.
1454 This is not optimal, because it will introduce an unnecessary
1455 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1456 need to distinguish reading both $f0 and $f1 or just one of
1457 them. Note that we don't have to check the other way,
1458 because there is no instruction that sets both $f0 and $f1
1459 and requires a delay. */
1460 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1461 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1462 == (reg &~ (unsigned) 1)))
1463 return 1;
1464 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1465 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1466 == (reg &~ (unsigned) 1)))
1467 return 1;
1468 }
1469 else if (! mips_opts.mips16)
1470 {
1471 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1472 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1473 return 1;
1474 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1475 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1476 return 1;
1477 }
1478 else
1479 {
1480 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1481 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1482 & MIPS16OP_MASK_RX)]
1483 == reg))
1484 return 1;
1485 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1486 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1487 & MIPS16OP_MASK_RY)]
1488 == reg))
1489 return 1;
1490 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1491 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1492 & MIPS16OP_MASK_MOVE32Z)]
1493 == reg))
1494 return 1;
1495 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1496 return 1;
1497 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1498 return 1;
1499 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1500 return 1;
1501 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1502 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1503 & MIPS16OP_MASK_REGR32) == reg)
1504 return 1;
1505 }
1506
1507 return 0;
1508 }
1509
1510 /* This function returns true if modifying a register requires a
1511 delay. */
1512
1513 static int
1514 reg_needs_delay (unsigned int reg)
1515 {
1516 unsigned long prev_pinfo;
1517
1518 prev_pinfo = prev_insn.insn_mo->pinfo;
1519 if (! mips_opts.noreorder
1520 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1521 && ! gpr_interlocks)
1522 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1523 && ! cop_interlocks)))
1524 {
1525 /* A load from a coprocessor or from memory. All load delays
1526 delay the use of general register rt for one instruction. */
1527 /* Itbl support may require additional care here. */
1528 know (prev_pinfo & INSN_WRITE_GPR_T);
1529 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1530 return 1;
1531 }
1532
1533 return 0;
1534 }
1535
1536 /* Mark instruction labels in mips16 mode. This permits the linker to
1537 handle them specially, such as generating jalx instructions when
1538 needed. We also make them odd for the duration of the assembly, in
1539 order to generate the right sort of code. We will make them even
1540 in the adjust_symtab routine, while leaving them marked. This is
1541 convenient for the debugger and the disassembler. The linker knows
1542 to make them odd again. */
1543
1544 static void
1545 mips16_mark_labels (void)
1546 {
1547 if (mips_opts.mips16)
1548 {
1549 struct insn_label_list *l;
1550 valueT val;
1551
1552 for (l = insn_labels; l != NULL; l = l->next)
1553 {
1554 #ifdef OBJ_ELF
1555 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1556 S_SET_OTHER (l->label, STO_MIPS16);
1557 #endif
1558 val = S_GET_VALUE (l->label);
1559 if ((val & 1) == 0)
1560 S_SET_VALUE (l->label, val + 1);
1561 }
1562 }
1563 }
1564
1565 /* End the current frag. Make it a variant frag and record the
1566 relaxation info. */
1567
1568 static void
1569 relax_close_frag (void)
1570 {
1571 mips_macro_warning.first_frag = frag_now;
1572 frag_var (rs_machine_dependent, 0, 0,
1573 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1574 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1575
1576 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1577 mips_relax.first_fixup = 0;
1578 }
1579
1580 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1581 See the comment above RELAX_ENCODE for more details. */
1582
1583 static void
1584 relax_start (symbolS *symbol)
1585 {
1586 assert (mips_relax.sequence == 0);
1587 mips_relax.sequence = 1;
1588 mips_relax.symbol = symbol;
1589 }
1590
1591 /* Start generating the second version of a relaxable sequence.
1592 See the comment above RELAX_ENCODE for more details. */
1593
1594 static void
1595 relax_switch (void)
1596 {
1597 assert (mips_relax.sequence == 1);
1598 mips_relax.sequence = 2;
1599 }
1600
1601 /* End the current relaxable sequence. */
1602
1603 static void
1604 relax_end (void)
1605 {
1606 assert (mips_relax.sequence == 2);
1607 relax_close_frag ();
1608 mips_relax.sequence = 0;
1609 }
1610
1611 /* Output an instruction. IP is the instruction information.
1612 ADDRESS_EXPR is an operand of the instruction to be used with
1613 RELOC_TYPE. */
1614
1615 static void
1616 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
1617 bfd_reloc_code_real_type *reloc_type)
1618 {
1619 register unsigned long prev_pinfo, pinfo;
1620 char *f;
1621 fixS *fixp[3];
1622 int nops = 0;
1623 relax_stateT prev_insn_frag_type = 0;
1624 bfd_boolean relaxed_branch = FALSE;
1625 bfd_boolean force_new_frag = FALSE;
1626
1627 /* Mark instruction labels in mips16 mode. */
1628 mips16_mark_labels ();
1629
1630 prev_pinfo = prev_insn.insn_mo->pinfo;
1631 pinfo = ip->insn_mo->pinfo;
1632
1633 if (mips_relax.sequence != 2
1634 && (!mips_opts.noreorder || prev_nop_frag != NULL))
1635 {
1636 int prev_prev_nop;
1637
1638 /* If the previous insn required any delay slots, see if we need
1639 to insert a NOP or two. There are eight kinds of possible
1640 hazards, of which an instruction can have at most one type.
1641 (1) a load from memory delay
1642 (2) a load from a coprocessor delay
1643 (3) an unconditional branch delay
1644 (4) a conditional branch delay
1645 (5) a move to coprocessor register delay
1646 (6) a load coprocessor register from memory delay
1647 (7) a coprocessor condition code delay
1648 (8) a HI/LO special register delay
1649
1650 There are a lot of optimizations we could do that we don't.
1651 In particular, we do not, in general, reorder instructions.
1652 If you use gcc with optimization, it will reorder
1653 instructions and generally do much more optimization then we
1654 do here; repeating all that work in the assembler would only
1655 benefit hand written assembly code, and does not seem worth
1656 it. */
1657
1658 /* This is how a NOP is emitted. */
1659 #define emit_nop() \
1660 (mips_opts.mips16 \
1661 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1662 : md_number_to_chars (frag_more (4), 0, 4))
1663
1664 /* The previous insn might require a delay slot, depending upon
1665 the contents of the current insn. */
1666 if (! mips_opts.mips16
1667 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1668 && ! gpr_interlocks)
1669 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1670 && ! cop_interlocks)))
1671 {
1672 /* A load from a coprocessor or from memory. All load
1673 delays delay the use of general register rt for one
1674 instruction. */
1675 /* Itbl support may require additional care here. */
1676 know (prev_pinfo & INSN_WRITE_GPR_T);
1677 if (mips_optimize == 0
1678 || insn_uses_reg (ip,
1679 ((prev_insn.insn_opcode >> OP_SH_RT)
1680 & OP_MASK_RT),
1681 MIPS_GR_REG))
1682 ++nops;
1683 }
1684 else if (! mips_opts.mips16
1685 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1686 && ! cop_interlocks)
1687 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1688 && ! cop_mem_interlocks)))
1689 {
1690 /* A generic coprocessor delay. The previous instruction
1691 modified a coprocessor general or control register. If
1692 it modified a control register, we need to avoid any
1693 coprocessor instruction (this is probably not always
1694 required, but it sometimes is). If it modified a general
1695 register, we avoid using that register.
1696
1697 This case is not handled very well. There is no special
1698 knowledge of CP0 handling, and the coprocessors other
1699 than the floating point unit are not distinguished at
1700 all. */
1701 /* Itbl support may require additional care here. FIXME!
1702 Need to modify this to include knowledge about
1703 user specified delays! */
1704 if (prev_pinfo & INSN_WRITE_FPR_T)
1705 {
1706 if (mips_optimize == 0
1707 || insn_uses_reg (ip,
1708 ((prev_insn.insn_opcode >> OP_SH_FT)
1709 & OP_MASK_FT),
1710 MIPS_FP_REG))
1711 ++nops;
1712 }
1713 else if (prev_pinfo & INSN_WRITE_FPR_S)
1714 {
1715 if (mips_optimize == 0
1716 || insn_uses_reg (ip,
1717 ((prev_insn.insn_opcode >> OP_SH_FS)
1718 & OP_MASK_FS),
1719 MIPS_FP_REG))
1720 ++nops;
1721 }
1722 else
1723 {
1724 /* We don't know exactly what the previous instruction
1725 does. If the current instruction uses a coprocessor
1726 register, we must insert a NOP. If previous
1727 instruction may set the condition codes, and the
1728 current instruction uses them, we must insert two
1729 NOPS. */
1730 /* Itbl support may require additional care here. */
1731 if (mips_optimize == 0
1732 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1733 && (pinfo & INSN_READ_COND_CODE)))
1734 nops += 2;
1735 else if (pinfo & INSN_COP)
1736 ++nops;
1737 }
1738 }
1739 else if (! mips_opts.mips16
1740 && (prev_pinfo & INSN_WRITE_COND_CODE)
1741 && ! cop_interlocks)
1742 {
1743 /* The previous instruction sets the coprocessor condition
1744 codes, but does not require a general coprocessor delay
1745 (this means it is a floating point comparison
1746 instruction). If this instruction uses the condition
1747 codes, we need to insert a single NOP. */
1748 /* Itbl support may require additional care here. */
1749 if (mips_optimize == 0
1750 || (pinfo & INSN_READ_COND_CODE))
1751 ++nops;
1752 }
1753
1754 /* If we're fixing up mfhi/mflo for the r7000 and the
1755 previous insn was an mfhi/mflo and the current insn
1756 reads the register that the mfhi/mflo wrote to, then
1757 insert two nops. */
1758
1759 else if (mips_7000_hilo_fix
1760 && MF_HILO_INSN (prev_pinfo)
1761 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1762 & OP_MASK_RD),
1763 MIPS_GR_REG))
1764 {
1765 nops += 2;
1766 }
1767
1768 /* If we're fixing up mfhi/mflo for the r7000 and the
1769 2nd previous insn was an mfhi/mflo and the current insn
1770 reads the register that the mfhi/mflo wrote to, then
1771 insert one nop. */
1772
1773 else if (mips_7000_hilo_fix
1774 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1775 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1776 & OP_MASK_RD),
1777 MIPS_GR_REG))
1778
1779 {
1780 ++nops;
1781 }
1782
1783 else if (prev_pinfo & INSN_READ_LO)
1784 {
1785 /* The previous instruction reads the LO register; if the
1786 current instruction writes to the LO register, we must
1787 insert two NOPS. Some newer processors have interlocks.
1788 Also the tx39's multiply instructions can be executed
1789 immediately after a read from HI/LO (without the delay),
1790 though the tx39's divide insns still do require the
1791 delay. */
1792 if (! (hilo_interlocks
1793 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1794 && (mips_optimize == 0
1795 || (pinfo & INSN_WRITE_LO)))
1796 nops += 2;
1797 /* Most mips16 branch insns don't have a delay slot.
1798 If a read from LO is immediately followed by a branch
1799 to a write to LO we have a read followed by a write
1800 less than 2 insns away. We assume the target of
1801 a branch might be a write to LO, and insert a nop
1802 between a read and an immediately following branch. */
1803 else if (mips_opts.mips16
1804 && (mips_optimize == 0
1805 || (pinfo & MIPS16_INSN_BRANCH)))
1806 ++nops;
1807 }
1808 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1809 {
1810 /* The previous instruction reads the HI register; if the
1811 current instruction writes to the HI register, we must
1812 insert a NOP. Some newer processors have interlocks.
1813 Also the note tx39's multiply above. */
1814 if (! (hilo_interlocks
1815 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1816 && (mips_optimize == 0
1817 || (pinfo & INSN_WRITE_HI)))
1818 nops += 2;
1819 /* Most mips16 branch insns don't have a delay slot.
1820 If a read from HI is immediately followed by a branch
1821 to a write to HI we have a read followed by a write
1822 less than 2 insns away. We assume the target of
1823 a branch might be a write to HI, and insert a nop
1824 between a read and an immediately following branch. */
1825 else if (mips_opts.mips16
1826 && (mips_optimize == 0
1827 || (pinfo & MIPS16_INSN_BRANCH)))
1828 ++nops;
1829 }
1830
1831 /* If the previous instruction was in a noreorder section, then
1832 we don't want to insert the nop after all. */
1833 /* Itbl support may require additional care here. */
1834 if (prev_insn_unreordered)
1835 nops = 0;
1836
1837 /* There are two cases which require two intervening
1838 instructions: 1) setting the condition codes using a move to
1839 coprocessor instruction which requires a general coprocessor
1840 delay and then reading the condition codes 2) reading the HI
1841 or LO register and then writing to it (except on processors
1842 which have interlocks). If we are not already emitting a NOP
1843 instruction, we must check for these cases compared to the
1844 instruction previous to the previous instruction. */
1845 if ((! mips_opts.mips16
1846 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1847 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1848 && (pinfo & INSN_READ_COND_CODE)
1849 && ! cop_interlocks)
1850 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1851 && (pinfo & INSN_WRITE_LO)
1852 && ! (hilo_interlocks
1853 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
1854 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1855 && (pinfo & INSN_WRITE_HI)
1856 && ! (hilo_interlocks
1857 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
1858 prev_prev_nop = 1;
1859 else
1860 prev_prev_nop = 0;
1861
1862 if (prev_prev_insn_unreordered)
1863 prev_prev_nop = 0;
1864
1865 if (prev_prev_nop && nops == 0)
1866 ++nops;
1867
1868 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
1869 {
1870 /* We're out of bits in pinfo, so we must resort to string
1871 ops here. Shortcuts are selected based on opcodes being
1872 limited to the VR4120 instruction set. */
1873 int min_nops = 0;
1874 const char *pn = prev_insn.insn_mo->name;
1875 const char *tn = ip->insn_mo->name;
1876 if (strncmp (pn, "macc", 4) == 0
1877 || strncmp (pn, "dmacc", 5) == 0)
1878 {
1879 /* Errata 21 - [D]DIV[U] after [D]MACC */
1880 if (strstr (tn, "div"))
1881 min_nops = 1;
1882
1883 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1884 instruction is executed immediately after a MACC or
1885 DMACC instruction, the result of [either instruction]
1886 is incorrect." */
1887 if (strncmp (tn, "mult", 4) == 0
1888 || strncmp (tn, "dmult", 5) == 0)
1889 min_nops = 1;
1890
1891 /* Errata 23 - Continuous DMULT[U]/DMACC instructions.
1892 Applies on top of VR4181A MD(1) errata. */
1893 if (pn[0] == 'd' && strncmp (tn, "dmacc", 5) == 0)
1894 min_nops = 1;
1895
1896 /* Errata 24 - MT{LO,HI} after [D]MACC */
1897 if (strcmp (tn, "mtlo") == 0
1898 || strcmp (tn, "mthi") == 0)
1899 min_nops = 1;
1900 }
1901 else if (strncmp (pn, "dmult", 5) == 0
1902 && (strncmp (tn, "dmult", 5) == 0
1903 || strncmp (tn, "dmacc", 5) == 0))
1904 {
1905 /* Here is the rest of errata 23. */
1906 min_nops = 1;
1907 }
1908 else if ((strncmp (pn, "dmult", 5) == 0 || strstr (pn, "div"))
1909 && (strncmp (tn, "macc", 4) == 0
1910 || strncmp (tn, "dmacc", 5) == 0))
1911 {
1912 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1913 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1914 DDIV or DDIVU instruction, the result of the MACC or
1915 DMACC instruction is incorrect.". This partly overlaps
1916 the workaround for errata 23. */
1917 min_nops = 1;
1918 }
1919 if (nops < min_nops)
1920 nops = min_nops;
1921 }
1922
1923 /* If we are being given a nop instruction, don't bother with
1924 one of the nops we would otherwise output. This will only
1925 happen when a nop instruction is used with mips_optimize set
1926 to 0. */
1927 if (nops > 0
1928 && ! mips_opts.noreorder
1929 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1930 --nops;
1931
1932 /* Now emit the right number of NOP instructions. */
1933 if (nops > 0 && ! mips_opts.noreorder)
1934 {
1935 fragS *old_frag;
1936 unsigned long old_frag_offset;
1937 int i;
1938 struct insn_label_list *l;
1939
1940 old_frag = frag_now;
1941 old_frag_offset = frag_now_fix ();
1942
1943 for (i = 0; i < nops; i++)
1944 emit_nop ();
1945
1946 if (listing)
1947 {
1948 listing_prev_line ();
1949 /* We may be at the start of a variant frag. In case we
1950 are, make sure there is enough space for the frag
1951 after the frags created by listing_prev_line. The
1952 argument to frag_grow here must be at least as large
1953 as the argument to all other calls to frag_grow in
1954 this file. We don't have to worry about being in the
1955 middle of a variant frag, because the variants insert
1956 all needed nop instructions themselves. */
1957 frag_grow (40);
1958 }
1959
1960 for (l = insn_labels; l != NULL; l = l->next)
1961 {
1962 valueT val;
1963
1964 assert (S_GET_SEGMENT (l->label) == now_seg);
1965 symbol_set_frag (l->label, frag_now);
1966 val = (valueT) frag_now_fix ();
1967 /* mips16 text labels are stored as odd. */
1968 if (mips_opts.mips16)
1969 ++val;
1970 S_SET_VALUE (l->label, val);
1971 }
1972
1973 #ifndef NO_ECOFF_DEBUGGING
1974 if (ECOFF_DEBUGGING)
1975 ecoff_fix_loc (old_frag, old_frag_offset);
1976 #endif
1977 }
1978 else if (prev_nop_frag != NULL)
1979 {
1980 /* We have a frag holding nops we may be able to remove. If
1981 we don't need any nops, we can decrease the size of
1982 prev_nop_frag by the size of one instruction. If we do
1983 need some nops, we count them in prev_nops_required. */
1984 if (prev_nop_frag_since == 0)
1985 {
1986 if (nops == 0)
1987 {
1988 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1989 --prev_nop_frag_holds;
1990 }
1991 else
1992 prev_nop_frag_required += nops;
1993 }
1994 else
1995 {
1996 if (prev_prev_nop == 0)
1997 {
1998 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1999 --prev_nop_frag_holds;
2000 }
2001 else
2002 ++prev_nop_frag_required;
2003 }
2004
2005 if (prev_nop_frag_holds <= prev_nop_frag_required)
2006 prev_nop_frag = NULL;
2007
2008 ++prev_nop_frag_since;
2009
2010 /* Sanity check: by the time we reach the second instruction
2011 after prev_nop_frag, we should have used up all the nops
2012 one way or another. */
2013 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2014 }
2015 }
2016
2017 #ifdef OBJ_ELF
2018 /* The value passed to dwarf2_emit_insn is the distance between
2019 the beginning of the current instruction and the address that
2020 should be recorded in the debug tables. For MIPS16 debug info
2021 we want to use ISA-encoded addresses, so we pass -1 for an
2022 address higher by one than the current. */
2023 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2024 #endif
2025
2026 /* Record the frag type before frag_var. */
2027 if (prev_insn_frag)
2028 prev_insn_frag_type = prev_insn_frag->fr_type;
2029
2030 if (address_expr
2031 && *reloc_type == BFD_RELOC_16_PCREL_S2
2032 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2033 || pinfo & INSN_COND_BRANCH_LIKELY)
2034 && mips_relax_branch
2035 /* Don't try branch relaxation within .set nomacro, or within
2036 .set noat if we use $at for PIC computations. If it turns
2037 out that the branch was out-of-range, we'll get an error. */
2038 && !mips_opts.warn_about_macros
2039 && !(mips_opts.noat && mips_pic != NO_PIC)
2040 && !mips_opts.mips16)
2041 {
2042 relaxed_branch = TRUE;
2043 f = frag_var (rs_machine_dependent,
2044 relaxed_branch_length
2045 (NULL, NULL,
2046 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2047 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2048 RELAX_BRANCH_ENCODE
2049 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2050 pinfo & INSN_COND_BRANCH_LIKELY,
2051 pinfo & INSN_WRITE_GPR_31,
2052 0),
2053 address_expr->X_add_symbol,
2054 address_expr->X_add_number,
2055 0);
2056 *reloc_type = BFD_RELOC_UNUSED;
2057 }
2058 else if (*reloc_type > BFD_RELOC_UNUSED)
2059 {
2060 /* We need to set up a variant frag. */
2061 assert (mips_opts.mips16 && address_expr != NULL);
2062 f = frag_var (rs_machine_dependent, 4, 0,
2063 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2064 mips16_small, mips16_ext,
2065 (prev_pinfo
2066 & INSN_UNCOND_BRANCH_DELAY),
2067 (*prev_insn_reloc_type
2068 == BFD_RELOC_MIPS16_JMP)),
2069 make_expr_symbol (address_expr), 0, NULL);
2070 }
2071 else if (mips_opts.mips16
2072 && ! ip->use_extend
2073 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2074 {
2075 /* Make sure there is enough room to swap this instruction with
2076 a following jump instruction. */
2077 frag_grow (6);
2078 f = frag_more (2);
2079 }
2080 else
2081 {
2082 if (mips_opts.mips16
2083 && mips_opts.noreorder
2084 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2085 as_warn (_("extended instruction in delay slot"));
2086
2087 if (mips_relax.sequence)
2088 {
2089 /* If we've reached the end of this frag, turn it into a variant
2090 frag and record the information for the instructions we've
2091 written so far. */
2092 if (frag_room () < 4)
2093 relax_close_frag ();
2094 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2095 }
2096
2097 if (mips_relax.sequence != 2)
2098 mips_macro_warning.sizes[0] += 4;
2099 if (mips_relax.sequence != 1)
2100 mips_macro_warning.sizes[1] += 4;
2101
2102 f = frag_more (4);
2103 }
2104
2105 fixp[0] = fixp[1] = fixp[2] = NULL;
2106 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2107 {
2108 if (address_expr->X_op == O_constant)
2109 {
2110 unsigned int tmp;
2111
2112 switch (*reloc_type)
2113 {
2114 case BFD_RELOC_32:
2115 ip->insn_opcode |= address_expr->X_add_number;
2116 break;
2117
2118 case BFD_RELOC_MIPS_HIGHEST:
2119 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2120 ip->insn_opcode |= tmp & 0xffff;
2121 break;
2122
2123 case BFD_RELOC_MIPS_HIGHER:
2124 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2125 ip->insn_opcode |= tmp & 0xffff;
2126 break;
2127
2128 case BFD_RELOC_HI16_S:
2129 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2130 ip->insn_opcode |= tmp & 0xffff;
2131 break;
2132
2133 case BFD_RELOC_HI16:
2134 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2135 break;
2136
2137 case BFD_RELOC_UNUSED:
2138 case BFD_RELOC_LO16:
2139 case BFD_RELOC_MIPS_GOT_DISP:
2140 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2141 break;
2142
2143 case BFD_RELOC_MIPS_JMP:
2144 if ((address_expr->X_add_number & 3) != 0)
2145 as_bad (_("jump to misaligned address (0x%lx)"),
2146 (unsigned long) address_expr->X_add_number);
2147 if (address_expr->X_add_number & ~0xfffffff)
2148 as_bad (_("jump address range overflow (0x%lx)"),
2149 (unsigned long) address_expr->X_add_number);
2150 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2151 break;
2152
2153 case BFD_RELOC_MIPS16_JMP:
2154 if ((address_expr->X_add_number & 3) != 0)
2155 as_bad (_("jump to misaligned address (0x%lx)"),
2156 (unsigned long) address_expr->X_add_number);
2157 if (address_expr->X_add_number & ~0xfffffff)
2158 as_bad (_("jump address range overflow (0x%lx)"),
2159 (unsigned long) address_expr->X_add_number);
2160 ip->insn_opcode |=
2161 (((address_expr->X_add_number & 0x7c0000) << 3)
2162 | ((address_expr->X_add_number & 0xf800000) >> 7)
2163 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2164 break;
2165
2166 case BFD_RELOC_16_PCREL_S2:
2167 goto need_reloc;
2168
2169 default:
2170 internalError ();
2171 }
2172 }
2173 else if (*reloc_type < BFD_RELOC_UNUSED)
2174 need_reloc:
2175 {
2176 reloc_howto_type *howto;
2177 int i;
2178
2179 /* In a compound relocation, it is the final (outermost)
2180 operator that determines the relocated field. */
2181 for (i = 1; i < 3; i++)
2182 if (reloc_type[i] == BFD_RELOC_UNUSED)
2183 break;
2184
2185 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2186 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2187 bfd_get_reloc_size(howto),
2188 address_expr,
2189 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2190 reloc_type[0]);
2191
2192 /* These relocations can have an addend that won't fit in
2193 4 octets for 64bit assembly. */
2194 if (HAVE_64BIT_GPRS
2195 && ! howto->partial_inplace
2196 && (reloc_type[0] == BFD_RELOC_16
2197 || reloc_type[0] == BFD_RELOC_32
2198 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2199 || reloc_type[0] == BFD_RELOC_HI16_S
2200 || reloc_type[0] == BFD_RELOC_LO16
2201 || reloc_type[0] == BFD_RELOC_GPREL16
2202 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2203 || reloc_type[0] == BFD_RELOC_GPREL32
2204 || reloc_type[0] == BFD_RELOC_64
2205 || reloc_type[0] == BFD_RELOC_CTOR
2206 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2207 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2208 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2209 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2210 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2211 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2212 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2213 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2214 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2215 fixp[0]->fx_no_overflow = 1;
2216
2217 if (mips_relax.sequence)
2218 {
2219 if (mips_relax.first_fixup == 0)
2220 mips_relax.first_fixup = fixp[0];
2221 }
2222 else if (reloc_needs_lo_p (*reloc_type))
2223 {
2224 struct mips_hi_fixup *hi_fixup;
2225
2226 /* Reuse the last entry if it already has a matching %lo. */
2227 hi_fixup = mips_hi_fixup_list;
2228 if (hi_fixup == 0
2229 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2230 {
2231 hi_fixup = ((struct mips_hi_fixup *)
2232 xmalloc (sizeof (struct mips_hi_fixup)));
2233 hi_fixup->next = mips_hi_fixup_list;
2234 mips_hi_fixup_list = hi_fixup;
2235 }
2236 hi_fixup->fixp = fixp[0];
2237 hi_fixup->seg = now_seg;
2238 }
2239
2240 /* Add fixups for the second and third relocations, if given.
2241 Note that the ABI allows the second relocation to be
2242 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2243 moment we only use RSS_UNDEF, but we could add support
2244 for the others if it ever becomes necessary. */
2245 for (i = 1; i < 3; i++)
2246 if (reloc_type[i] != BFD_RELOC_UNUSED)
2247 {
2248 fixp[i] = fix_new (frag_now, fixp[0]->fx_where,
2249 fixp[0]->fx_size, NULL, 0,
2250 FALSE, reloc_type[i]);
2251
2252 /* Use fx_tcbit to mark compound relocs. */
2253 fixp[0]->fx_tcbit = 1;
2254 fixp[i]->fx_tcbit = 1;
2255 }
2256 }
2257 }
2258
2259 if (! mips_opts.mips16)
2260 md_number_to_chars (f, ip->insn_opcode, 4);
2261 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2262 {
2263 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2264 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2265 }
2266 else
2267 {
2268 if (ip->use_extend)
2269 {
2270 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2271 f += 2;
2272 }
2273 md_number_to_chars (f, ip->insn_opcode, 2);
2274 }
2275
2276 /* Update the register mask information. */
2277 if (! mips_opts.mips16)
2278 {
2279 if (pinfo & INSN_WRITE_GPR_D)
2280 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2281 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2282 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2283 if (pinfo & INSN_READ_GPR_S)
2284 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2285 if (pinfo & INSN_WRITE_GPR_31)
2286 mips_gprmask |= 1 << RA;
2287 if (pinfo & INSN_WRITE_FPR_D)
2288 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2289 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2290 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2291 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2292 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2293 if ((pinfo & INSN_READ_FPR_R) != 0)
2294 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2295 if (pinfo & INSN_COP)
2296 {
2297 /* We don't keep enough information to sort these cases out.
2298 The itbl support does keep this information however, although
2299 we currently don't support itbl fprmats as part of the cop
2300 instruction. May want to add this support in the future. */
2301 }
2302 /* Never set the bit for $0, which is always zero. */
2303 mips_gprmask &= ~1 << 0;
2304 }
2305 else
2306 {
2307 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2308 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2309 & MIPS16OP_MASK_RX);
2310 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2311 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2312 & MIPS16OP_MASK_RY);
2313 if (pinfo & MIPS16_INSN_WRITE_Z)
2314 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2315 & MIPS16OP_MASK_RZ);
2316 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2317 mips_gprmask |= 1 << TREG;
2318 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2319 mips_gprmask |= 1 << SP;
2320 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2321 mips_gprmask |= 1 << RA;
2322 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2323 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2324 if (pinfo & MIPS16_INSN_READ_Z)
2325 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2326 & MIPS16OP_MASK_MOVE32Z);
2327 if (pinfo & MIPS16_INSN_READ_GPR_X)
2328 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2329 & MIPS16OP_MASK_REGR32);
2330 }
2331
2332 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2333 {
2334 /* Filling the branch delay slot is more complex. We try to
2335 switch the branch with the previous instruction, which we can
2336 do if the previous instruction does not set up a condition
2337 that the branch tests and if the branch is not itself the
2338 target of any branch. */
2339 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2340 || (pinfo & INSN_COND_BRANCH_DELAY))
2341 {
2342 if (mips_optimize < 2
2343 /* If we have seen .set volatile or .set nomove, don't
2344 optimize. */
2345 || mips_opts.nomove != 0
2346 /* If we had to emit any NOP instructions, then we
2347 already know we can not swap. */
2348 || nops != 0
2349 /* If we don't even know the previous insn, we can not
2350 swap. */
2351 || ! prev_insn_valid
2352 /* If the previous insn is already in a branch delay
2353 slot, then we can not swap. */
2354 || prev_insn_is_delay_slot
2355 /* If the previous previous insn was in a .set
2356 noreorder, we can't swap. Actually, the MIPS
2357 assembler will swap in this situation. However, gcc
2358 configured -with-gnu-as will generate code like
2359 .set noreorder
2360 lw $4,XXX
2361 .set reorder
2362 INSN
2363 bne $4,$0,foo
2364 in which we can not swap the bne and INSN. If gcc is
2365 not configured -with-gnu-as, it does not output the
2366 .set pseudo-ops. We don't have to check
2367 prev_insn_unreordered, because prev_insn_valid will
2368 be 0 in that case. We don't want to use
2369 prev_prev_insn_valid, because we do want to be able
2370 to swap at the start of a function. */
2371 || prev_prev_insn_unreordered
2372 /* If the branch is itself the target of a branch, we
2373 can not swap. We cheat on this; all we check for is
2374 whether there is a label on this instruction. If
2375 there are any branches to anything other than a
2376 label, users must use .set noreorder. */
2377 || insn_labels != NULL
2378 /* If the previous instruction is in a variant frag
2379 other than this branch's one, we cannot do the swap.
2380 This does not apply to the mips16, which uses variant
2381 frags for different purposes. */
2382 || (! mips_opts.mips16
2383 && prev_insn_frag_type == rs_machine_dependent)
2384 /* If the branch reads the condition codes, we don't
2385 even try to swap, because in the sequence
2386 ctc1 $X,$31
2387 INSN
2388 INSN
2389 bc1t LABEL
2390 we can not swap, and I don't feel like handling that
2391 case. */
2392 || (! mips_opts.mips16
2393 && (pinfo & INSN_READ_COND_CODE)
2394 && ! cop_interlocks)
2395 /* We can not swap with an instruction that requires a
2396 delay slot, because the target of the branch might
2397 interfere with that instruction. */
2398 || (! mips_opts.mips16
2399 && (prev_pinfo
2400 /* Itbl support may require additional care here. */
2401 & (INSN_LOAD_COPROC_DELAY
2402 | INSN_COPROC_MOVE_DELAY
2403 | INSN_WRITE_COND_CODE))
2404 && ! cop_interlocks)
2405 || (! (hilo_interlocks
2406 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
2407 && (prev_pinfo
2408 & (INSN_READ_LO
2409 | INSN_READ_HI)))
2410 || (! mips_opts.mips16
2411 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2412 && ! gpr_interlocks)
2413 || (! mips_opts.mips16
2414 /* Itbl support may require additional care here. */
2415 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2416 && ! cop_mem_interlocks)
2417 /* We can not swap with a branch instruction. */
2418 || (prev_pinfo
2419 & (INSN_UNCOND_BRANCH_DELAY
2420 | INSN_COND_BRANCH_DELAY
2421 | INSN_COND_BRANCH_LIKELY))
2422 /* We do not swap with a trap instruction, since it
2423 complicates trap handlers to have the trap
2424 instruction be in a delay slot. */
2425 || (prev_pinfo & INSN_TRAP)
2426 /* If the branch reads a register that the previous
2427 instruction sets, we can not swap. */
2428 || (! mips_opts.mips16
2429 && (prev_pinfo & INSN_WRITE_GPR_T)
2430 && insn_uses_reg (ip,
2431 ((prev_insn.insn_opcode >> OP_SH_RT)
2432 & OP_MASK_RT),
2433 MIPS_GR_REG))
2434 || (! mips_opts.mips16
2435 && (prev_pinfo & INSN_WRITE_GPR_D)
2436 && insn_uses_reg (ip,
2437 ((prev_insn.insn_opcode >> OP_SH_RD)
2438 & OP_MASK_RD),
2439 MIPS_GR_REG))
2440 || (mips_opts.mips16
2441 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2442 && insn_uses_reg (ip,
2443 ((prev_insn.insn_opcode
2444 >> MIPS16OP_SH_RX)
2445 & MIPS16OP_MASK_RX),
2446 MIPS16_REG))
2447 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2448 && insn_uses_reg (ip,
2449 ((prev_insn.insn_opcode
2450 >> MIPS16OP_SH_RY)
2451 & MIPS16OP_MASK_RY),
2452 MIPS16_REG))
2453 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2454 && insn_uses_reg (ip,
2455 ((prev_insn.insn_opcode
2456 >> MIPS16OP_SH_RZ)
2457 & MIPS16OP_MASK_RZ),
2458 MIPS16_REG))
2459 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2460 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2461 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2462 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2463 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2464 && insn_uses_reg (ip,
2465 MIPS16OP_EXTRACT_REG32R (prev_insn.
2466 insn_opcode),
2467 MIPS_GR_REG))))
2468 /* If the branch writes a register that the previous
2469 instruction sets, we can not swap (we know that
2470 branches write only to RD or to $31). */
2471 || (! mips_opts.mips16
2472 && (prev_pinfo & INSN_WRITE_GPR_T)
2473 && (((pinfo & INSN_WRITE_GPR_D)
2474 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2475 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2476 || ((pinfo & INSN_WRITE_GPR_31)
2477 && (((prev_insn.insn_opcode >> OP_SH_RT)
2478 & OP_MASK_RT)
2479 == RA))))
2480 || (! mips_opts.mips16
2481 && (prev_pinfo & INSN_WRITE_GPR_D)
2482 && (((pinfo & INSN_WRITE_GPR_D)
2483 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2484 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2485 || ((pinfo & INSN_WRITE_GPR_31)
2486 && (((prev_insn.insn_opcode >> OP_SH_RD)
2487 & OP_MASK_RD)
2488 == RA))))
2489 || (mips_opts.mips16
2490 && (pinfo & MIPS16_INSN_WRITE_31)
2491 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2492 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2493 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2494 == RA))))
2495 /* If the branch writes a register that the previous
2496 instruction reads, we can not swap (we know that
2497 branches only write to RD or to $31). */
2498 || (! mips_opts.mips16
2499 && (pinfo & INSN_WRITE_GPR_D)
2500 && insn_uses_reg (&prev_insn,
2501 ((ip->insn_opcode >> OP_SH_RD)
2502 & OP_MASK_RD),
2503 MIPS_GR_REG))
2504 || (! mips_opts.mips16
2505 && (pinfo & INSN_WRITE_GPR_31)
2506 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2507 || (mips_opts.mips16
2508 && (pinfo & MIPS16_INSN_WRITE_31)
2509 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2510 /* If the previous previous instruction has a load
2511 delay, and sets a register that the branch reads, we
2512 can not swap. */
2513 || (! mips_opts.mips16
2514 /* Itbl support may require additional care here. */
2515 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2516 && ! cop_interlocks)
2517 || ((prev_prev_insn.insn_mo->pinfo
2518 & INSN_LOAD_MEMORY_DELAY)
2519 && ! gpr_interlocks))
2520 && insn_uses_reg (ip,
2521 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2522 & OP_MASK_RT),
2523 MIPS_GR_REG))
2524 /* If one instruction sets a condition code and the
2525 other one uses a condition code, we can not swap. */
2526 || ((pinfo & INSN_READ_COND_CODE)
2527 && (prev_pinfo & INSN_WRITE_COND_CODE))
2528 || ((pinfo & INSN_WRITE_COND_CODE)
2529 && (prev_pinfo & INSN_READ_COND_CODE))
2530 /* If the previous instruction uses the PC, we can not
2531 swap. */
2532 || (mips_opts.mips16
2533 && (prev_pinfo & MIPS16_INSN_READ_PC))
2534 /* If the previous instruction was extended, we can not
2535 swap. */
2536 || (mips_opts.mips16 && prev_insn_extended)
2537 /* If the previous instruction had a fixup in mips16
2538 mode, we can not swap. This normally means that the
2539 previous instruction was a 4 byte branch anyhow. */
2540 || (mips_opts.mips16 && prev_insn_fixp[0])
2541 /* If the previous instruction is a sync, sync.l, or
2542 sync.p, we can not swap. */
2543 || (prev_pinfo & INSN_SYNC))
2544 {
2545 /* We could do even better for unconditional branches to
2546 portions of this object file; we could pick up the
2547 instruction at the destination, put it in the delay
2548 slot, and bump the destination address. */
2549 emit_nop ();
2550 if (mips_relax.sequence)
2551 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2552 /* Update the previous insn information. */
2553 prev_prev_insn = *ip;
2554 prev_insn.insn_mo = &dummy_opcode;
2555 }
2556 else
2557 {
2558 /* It looks like we can actually do the swap. */
2559 if (! mips_opts.mips16)
2560 {
2561 char *prev_f;
2562 char temp[4];
2563
2564 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2565 if (!relaxed_branch)
2566 {
2567 /* If this is not a relaxed branch, then just
2568 swap the instructions. */
2569 memcpy (temp, prev_f, 4);
2570 memcpy (prev_f, f, 4);
2571 memcpy (f, temp, 4);
2572 }
2573 else
2574 {
2575 /* If this is a relaxed branch, then we move the
2576 instruction to be placed in the delay slot to
2577 the current frag, shrinking the fixed part of
2578 the originating frag. If the branch occupies
2579 the tail of the latter, we move it backwards,
2580 into the space freed by the moved instruction. */
2581 f = frag_more (4);
2582 memcpy (f, prev_f, 4);
2583 prev_insn_frag->fr_fix -= 4;
2584 if (prev_insn_frag->fr_type == rs_machine_dependent)
2585 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2586 }
2587
2588 if (prev_insn_fixp[0])
2589 {
2590 prev_insn_fixp[0]->fx_frag = frag_now;
2591 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2592 }
2593 if (prev_insn_fixp[1])
2594 {
2595 prev_insn_fixp[1]->fx_frag = frag_now;
2596 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2597 }
2598 if (prev_insn_fixp[2])
2599 {
2600 prev_insn_fixp[2]->fx_frag = frag_now;
2601 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2602 }
2603 if (prev_insn_fixp[0] && HAVE_NEWABI
2604 && prev_insn_frag != frag_now
2605 && (prev_insn_fixp[0]->fx_r_type
2606 == BFD_RELOC_MIPS_GOT_DISP
2607 || (prev_insn_fixp[0]->fx_r_type
2608 == BFD_RELOC_MIPS_CALL16)))
2609 {
2610 /* To avoid confusion in tc_gen_reloc, we must
2611 ensure that this does not become a variant
2612 frag. */
2613 force_new_frag = TRUE;
2614 }
2615
2616 if (!relaxed_branch)
2617 {
2618 if (fixp[0])
2619 {
2620 fixp[0]->fx_frag = prev_insn_frag;
2621 fixp[0]->fx_where = prev_insn_where;
2622 }
2623 if (fixp[1])
2624 {
2625 fixp[1]->fx_frag = prev_insn_frag;
2626 fixp[1]->fx_where = prev_insn_where;
2627 }
2628 if (fixp[2])
2629 {
2630 fixp[2]->fx_frag = prev_insn_frag;
2631 fixp[2]->fx_where = prev_insn_where;
2632 }
2633 }
2634 else if (prev_insn_frag->fr_type == rs_machine_dependent)
2635 {
2636 if (fixp[0])
2637 fixp[0]->fx_where -= 4;
2638 if (fixp[1])
2639 fixp[1]->fx_where -= 4;
2640 if (fixp[2])
2641 fixp[2]->fx_where -= 4;
2642 }
2643 }
2644 else
2645 {
2646 char *prev_f;
2647 char temp[2];
2648
2649 assert (prev_insn_fixp[0] == NULL);
2650 assert (prev_insn_fixp[1] == NULL);
2651 assert (prev_insn_fixp[2] == NULL);
2652 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2653 memcpy (temp, prev_f, 2);
2654 memcpy (prev_f, f, 2);
2655 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2656 {
2657 assert (*reloc_type == BFD_RELOC_UNUSED);
2658 memcpy (f, temp, 2);
2659 }
2660 else
2661 {
2662 memcpy (f, f + 2, 2);
2663 memcpy (f + 2, temp, 2);
2664 }
2665 if (fixp[0])
2666 {
2667 fixp[0]->fx_frag = prev_insn_frag;
2668 fixp[0]->fx_where = prev_insn_where;
2669 }
2670 if (fixp[1])
2671 {
2672 fixp[1]->fx_frag = prev_insn_frag;
2673 fixp[1]->fx_where = prev_insn_where;
2674 }
2675 if (fixp[2])
2676 {
2677 fixp[2]->fx_frag = prev_insn_frag;
2678 fixp[2]->fx_where = prev_insn_where;
2679 }
2680 }
2681
2682 /* Update the previous insn information; leave prev_insn
2683 unchanged. */
2684 prev_prev_insn = *ip;
2685 }
2686 prev_insn_is_delay_slot = 1;
2687
2688 /* If that was an unconditional branch, forget the previous
2689 insn information. */
2690 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2691 {
2692 prev_prev_insn.insn_mo = &dummy_opcode;
2693 prev_insn.insn_mo = &dummy_opcode;
2694 }
2695
2696 prev_insn_fixp[0] = NULL;
2697 prev_insn_fixp[1] = NULL;
2698 prev_insn_fixp[2] = NULL;
2699 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2700 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2701 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2702 prev_insn_extended = 0;
2703 }
2704 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2705 {
2706 /* We don't yet optimize a branch likely. What we should do
2707 is look at the target, copy the instruction found there
2708 into the delay slot, and increment the branch to jump to
2709 the next instruction. */
2710 emit_nop ();
2711 /* Update the previous insn information. */
2712 prev_prev_insn = *ip;
2713 prev_insn.insn_mo = &dummy_opcode;
2714 prev_insn_fixp[0] = NULL;
2715 prev_insn_fixp[1] = NULL;
2716 prev_insn_fixp[2] = NULL;
2717 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2718 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2719 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2720 prev_insn_extended = 0;
2721 prev_insn_is_delay_slot = 1;
2722 }
2723 else
2724 {
2725 /* Update the previous insn information. */
2726 if (nops > 0)
2727 prev_prev_insn.insn_mo = &dummy_opcode;
2728 else
2729 prev_prev_insn = prev_insn;
2730 prev_insn = *ip;
2731
2732 /* Any time we see a branch, we always fill the delay slot
2733 immediately; since this insn is not a branch, we know it
2734 is not in a delay slot. */
2735 prev_insn_is_delay_slot = 0;
2736
2737 prev_insn_fixp[0] = fixp[0];
2738 prev_insn_fixp[1] = fixp[1];
2739 prev_insn_fixp[2] = fixp[2];
2740 prev_insn_reloc_type[0] = reloc_type[0];
2741 prev_insn_reloc_type[1] = reloc_type[1];
2742 prev_insn_reloc_type[2] = reloc_type[2];
2743 if (mips_opts.mips16)
2744 prev_insn_extended = (ip->use_extend
2745 || *reloc_type > BFD_RELOC_UNUSED);
2746 }
2747
2748 prev_prev_insn_unreordered = prev_insn_unreordered;
2749 prev_insn_unreordered = 0;
2750 prev_insn_frag = frag_now;
2751 prev_insn_where = f - frag_now->fr_literal;
2752 prev_insn_valid = 1;
2753 }
2754 else if (mips_relax.sequence != 2)
2755 {
2756 /* We need to record a bit of information even when we are not
2757 reordering, in order to determine the base address for mips16
2758 PC relative relocs. */
2759 prev_prev_insn = prev_insn;
2760 prev_insn = *ip;
2761 prev_insn_reloc_type[0] = reloc_type[0];
2762 prev_insn_reloc_type[1] = reloc_type[1];
2763 prev_insn_reloc_type[2] = reloc_type[2];
2764 prev_prev_insn_unreordered = prev_insn_unreordered;
2765 prev_insn_unreordered = 1;
2766 }
2767
2768 /* We just output an insn, so the next one doesn't have a label. */
2769 mips_clear_insn_labels ();
2770 }
2771
2772 /* This function forgets that there was any previous instruction or
2773 label. If PRESERVE is non-zero, it remembers enough information to
2774 know whether nops are needed before a noreorder section. */
2775
2776 static void
2777 mips_no_prev_insn (int preserve)
2778 {
2779 if (! preserve)
2780 {
2781 prev_insn.insn_mo = &dummy_opcode;
2782 prev_prev_insn.insn_mo = &dummy_opcode;
2783 prev_nop_frag = NULL;
2784 prev_nop_frag_holds = 0;
2785 prev_nop_frag_required = 0;
2786 prev_nop_frag_since = 0;
2787 }
2788 prev_insn_valid = 0;
2789 prev_insn_is_delay_slot = 0;
2790 prev_insn_unreordered = 0;
2791 prev_insn_extended = 0;
2792 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2793 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2794 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2795 prev_prev_insn_unreordered = 0;
2796 mips_clear_insn_labels ();
2797 }
2798
2799 /* This function must be called whenever we turn on noreorder or emit
2800 something other than instructions. It inserts any NOPS which might
2801 be needed by the previous instruction, and clears the information
2802 kept for the previous instructions. The INSNS parameter is true if
2803 instructions are to follow. */
2804
2805 static void
2806 mips_emit_delays (bfd_boolean insns)
2807 {
2808 if (! mips_opts.noreorder)
2809 {
2810 int nops;
2811
2812 nops = 0;
2813 if ((! mips_opts.mips16
2814 && ((prev_insn.insn_mo->pinfo
2815 & (INSN_LOAD_COPROC_DELAY
2816 | INSN_COPROC_MOVE_DELAY
2817 | INSN_WRITE_COND_CODE))
2818 && ! cop_interlocks))
2819 || (! hilo_interlocks
2820 && (prev_insn.insn_mo->pinfo
2821 & (INSN_READ_LO
2822 | INSN_READ_HI)))
2823 || (! mips_opts.mips16
2824 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2825 && ! gpr_interlocks)
2826 || (! mips_opts.mips16
2827 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2828 && ! cop_mem_interlocks))
2829 {
2830 /* Itbl support may require additional care here. */
2831 ++nops;
2832 if ((! mips_opts.mips16
2833 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2834 && ! cop_interlocks))
2835 || (! hilo_interlocks
2836 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2837 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2838 ++nops;
2839
2840 if (prev_insn_unreordered)
2841 nops = 0;
2842 }
2843 else if ((! mips_opts.mips16
2844 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2845 && ! cop_interlocks))
2846 || (! hilo_interlocks
2847 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2848 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2849 {
2850 /* Itbl support may require additional care here. */
2851 if (! prev_prev_insn_unreordered)
2852 ++nops;
2853 }
2854
2855 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
2856 {
2857 int min_nops = 0;
2858 const char *pn = prev_insn.insn_mo->name;
2859 if (strncmp (pn, "macc", 4) == 0
2860 || strncmp (pn, "dmacc", 5) == 0
2861 || strncmp (pn, "dmult", 5) == 0
2862 || strstr (pn, "div"))
2863 min_nops = 1;
2864 if (nops < min_nops)
2865 nops = min_nops;
2866 }
2867
2868 if (nops > 0)
2869 {
2870 struct insn_label_list *l;
2871
2872 if (insns)
2873 {
2874 /* Record the frag which holds the nop instructions, so
2875 that we can remove them if we don't need them. */
2876 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2877 prev_nop_frag = frag_now;
2878 prev_nop_frag_holds = nops;
2879 prev_nop_frag_required = 0;
2880 prev_nop_frag_since = 0;
2881 }
2882
2883 for (; nops > 0; --nops)
2884 emit_nop ();
2885
2886 if (insns)
2887 {
2888 /* Move on to a new frag, so that it is safe to simply
2889 decrease the size of prev_nop_frag. */
2890 frag_wane (frag_now);
2891 frag_new (0);
2892 }
2893
2894 for (l = insn_labels; l != NULL; l = l->next)
2895 {
2896 valueT val;
2897
2898 assert (S_GET_SEGMENT (l->label) == now_seg);
2899 symbol_set_frag (l->label, frag_now);
2900 val = (valueT) frag_now_fix ();
2901 /* mips16 text labels are stored as odd. */
2902 if (mips_opts.mips16)
2903 ++val;
2904 S_SET_VALUE (l->label, val);
2905 }
2906 }
2907 }
2908
2909 /* Mark instruction labels in mips16 mode. */
2910 if (insns)
2911 mips16_mark_labels ();
2912
2913 mips_no_prev_insn (insns);
2914 }
2915
2916 /* Set up global variables for the start of a new macro. */
2917
2918 static void
2919 macro_start (void)
2920 {
2921 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2922 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2923 && (prev_insn.insn_mo->pinfo
2924 & (INSN_UNCOND_BRANCH_DELAY
2925 | INSN_COND_BRANCH_DELAY
2926 | INSN_COND_BRANCH_LIKELY)) != 0);
2927 }
2928
2929 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2930 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2931 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2932
2933 static const char *
2934 macro_warning (relax_substateT subtype)
2935 {
2936 if (subtype & RELAX_DELAY_SLOT)
2937 return _("Macro instruction expanded into multiple instructions"
2938 " in a branch delay slot");
2939 else if (subtype & RELAX_NOMACRO)
2940 return _("Macro instruction expanded into multiple instructions");
2941 else
2942 return 0;
2943 }
2944
2945 /* Finish up a macro. Emit warnings as appropriate. */
2946
2947 static void
2948 macro_end (void)
2949 {
2950 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2951 {
2952 relax_substateT subtype;
2953
2954 /* Set up the relaxation warning flags. */
2955 subtype = 0;
2956 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2957 subtype |= RELAX_SECOND_LONGER;
2958 if (mips_opts.warn_about_macros)
2959 subtype |= RELAX_NOMACRO;
2960 if (mips_macro_warning.delay_slot_p)
2961 subtype |= RELAX_DELAY_SLOT;
2962
2963 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2964 {
2965 /* Either the macro has a single implementation or both
2966 implementations are longer than 4 bytes. Emit the
2967 warning now. */
2968 const char *msg = macro_warning (subtype);
2969 if (msg != 0)
2970 as_warn (msg);
2971 }
2972 else
2973 {
2974 /* One implementation might need a warning but the other
2975 definitely doesn't. */
2976 mips_macro_warning.first_frag->fr_subtype |= subtype;
2977 }
2978 }
2979 }
2980
2981 /* Read a macro's relocation codes from *ARGS and store them in *R.
2982 The first argument in *ARGS will be either the code for a single
2983 relocation or -1 followed by the three codes that make up a
2984 composite relocation. */
2985
2986 static void
2987 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2988 {
2989 int i, next;
2990
2991 next = va_arg (*args, int);
2992 if (next >= 0)
2993 r[0] = (bfd_reloc_code_real_type) next;
2994 else
2995 for (i = 0; i < 3; i++)
2996 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2997 }
2998
2999 /* Build an instruction created by a macro expansion. This is passed
3000 a pointer to the count of instructions created so far, an
3001 expression, the name of the instruction to build, an operand format
3002 string, and corresponding arguments. */
3003
3004 static void
3005 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3006 {
3007 struct mips_cl_insn insn;
3008 bfd_reloc_code_real_type r[3];
3009 va_list args;
3010
3011 va_start (args, fmt);
3012
3013 if (mips_opts.mips16)
3014 {
3015 mips16_macro_build (ep, name, fmt, args);
3016 va_end (args);
3017 return;
3018 }
3019
3020 r[0] = BFD_RELOC_UNUSED;
3021 r[1] = BFD_RELOC_UNUSED;
3022 r[2] = BFD_RELOC_UNUSED;
3023 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3024 assert (insn.insn_mo);
3025 assert (strcmp (name, insn.insn_mo->name) == 0);
3026
3027 /* Search until we get a match for NAME. */
3028 while (1)
3029 {
3030 /* It is assumed here that macros will never generate
3031 MDMX or MIPS-3D instructions. */
3032 if (strcmp (fmt, insn.insn_mo->args) == 0
3033 && insn.insn_mo->pinfo != INSN_MACRO
3034 && OPCODE_IS_MEMBER (insn.insn_mo,
3035 (mips_opts.isa
3036 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3037 mips_opts.arch)
3038 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3039 break;
3040
3041 ++insn.insn_mo;
3042 assert (insn.insn_mo->name);
3043 assert (strcmp (name, insn.insn_mo->name) == 0);
3044 }
3045
3046 insn.insn_opcode = insn.insn_mo->match;
3047 for (;;)
3048 {
3049 switch (*fmt++)
3050 {
3051 case '\0':
3052 break;
3053
3054 case ',':
3055 case '(':
3056 case ')':
3057 continue;
3058
3059 case '+':
3060 switch (*fmt++)
3061 {
3062 case 'A':
3063 case 'E':
3064 insn.insn_opcode |= (va_arg (args, int)
3065 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3066 continue;
3067
3068 case 'B':
3069 case 'F':
3070 /* Note that in the macro case, these arguments are already
3071 in MSB form. (When handling the instruction in the
3072 non-macro case, these arguments are sizes from which
3073 MSB values must be calculated.) */
3074 insn.insn_opcode |= (va_arg (args, int)
3075 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3076 continue;
3077
3078 case 'C':
3079 case 'G':
3080 case 'H':
3081 /* Note that in the macro case, these arguments are already
3082 in MSBD form. (When handling the instruction in the
3083 non-macro case, these arguments are sizes from which
3084 MSBD values must be calculated.) */
3085 insn.insn_opcode |= (va_arg (args, int)
3086 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3087 continue;
3088
3089 default:
3090 internalError ();
3091 }
3092 continue;
3093
3094 case 't':
3095 case 'w':
3096 case 'E':
3097 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3098 continue;
3099
3100 case 'c':
3101 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3102 continue;
3103
3104 case 'T':
3105 case 'W':
3106 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3107 continue;
3108
3109 case 'd':
3110 case 'G':
3111 case 'K':
3112 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3113 continue;
3114
3115 case 'U':
3116 {
3117 int tmp = va_arg (args, int);
3118
3119 insn.insn_opcode |= tmp << OP_SH_RT;
3120 insn.insn_opcode |= tmp << OP_SH_RD;
3121 continue;
3122 }
3123
3124 case 'V':
3125 case 'S':
3126 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3127 continue;
3128
3129 case 'z':
3130 continue;
3131
3132 case '<':
3133 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3134 continue;
3135
3136 case 'D':
3137 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3138 continue;
3139
3140 case 'B':
3141 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3142 continue;
3143
3144 case 'J':
3145 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3146 continue;
3147
3148 case 'q':
3149 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3150 continue;
3151
3152 case 'b':
3153 case 's':
3154 case 'r':
3155 case 'v':
3156 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3157 continue;
3158
3159 case 'i':
3160 case 'j':
3161 case 'o':
3162 macro_read_relocs (&args, r);
3163 assert (*r == BFD_RELOC_GPREL16
3164 || *r == BFD_RELOC_MIPS_LITERAL
3165 || *r == BFD_RELOC_MIPS_HIGHER
3166 || *r == BFD_RELOC_HI16_S
3167 || *r == BFD_RELOC_LO16
3168 || *r == BFD_RELOC_MIPS_GOT16
3169 || *r == BFD_RELOC_MIPS_CALL16
3170 || *r == BFD_RELOC_MIPS_GOT_DISP
3171 || *r == BFD_RELOC_MIPS_GOT_PAGE
3172 || *r == BFD_RELOC_MIPS_GOT_OFST
3173 || *r == BFD_RELOC_MIPS_GOT_LO16
3174 || *r == BFD_RELOC_MIPS_CALL_LO16);
3175 continue;
3176
3177 case 'u':
3178 macro_read_relocs (&args, r);
3179 assert (ep != NULL
3180 && (ep->X_op == O_constant
3181 || (ep->X_op == O_symbol
3182 && (*r == BFD_RELOC_MIPS_HIGHEST
3183 || *r == BFD_RELOC_HI16_S
3184 || *r == BFD_RELOC_HI16
3185 || *r == BFD_RELOC_GPREL16
3186 || *r == BFD_RELOC_MIPS_GOT_HI16
3187 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3188 continue;
3189
3190 case 'p':
3191 assert (ep != NULL);
3192 /*
3193 * This allows macro() to pass an immediate expression for
3194 * creating short branches without creating a symbol.
3195 * Note that the expression still might come from the assembly
3196 * input, in which case the value is not checked for range nor
3197 * is a relocation entry generated (yuck).
3198 */
3199 if (ep->X_op == O_constant)
3200 {
3201 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3202 ep = NULL;
3203 }
3204 else
3205 *r = BFD_RELOC_16_PCREL_S2;
3206 continue;
3207
3208 case 'a':
3209 assert (ep != NULL);
3210 *r = BFD_RELOC_MIPS_JMP;
3211 continue;
3212
3213 case 'C':
3214 insn.insn_opcode |= va_arg (args, unsigned long);
3215 continue;
3216
3217 default:
3218 internalError ();
3219 }
3220 break;
3221 }
3222 va_end (args);
3223 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3224
3225 append_insn (&insn, ep, r);
3226 }
3227
3228 static void
3229 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3230 va_list args)
3231 {
3232 struct mips_cl_insn insn;
3233 bfd_reloc_code_real_type r[3]
3234 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3235
3236 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3237 assert (insn.insn_mo);
3238 assert (strcmp (name, insn.insn_mo->name) == 0);
3239
3240 while (strcmp (fmt, insn.insn_mo->args) != 0
3241 || insn.insn_mo->pinfo == INSN_MACRO)
3242 {
3243 ++insn.insn_mo;
3244 assert (insn.insn_mo->name);
3245 assert (strcmp (name, insn.insn_mo->name) == 0);
3246 }
3247
3248 insn.insn_opcode = insn.insn_mo->match;
3249 insn.use_extend = FALSE;
3250
3251 for (;;)
3252 {
3253 int c;
3254
3255 c = *fmt++;
3256 switch (c)
3257 {
3258 case '\0':
3259 break;
3260
3261 case ',':
3262 case '(':
3263 case ')':
3264 continue;
3265
3266 case 'y':
3267 case 'w':
3268 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3269 continue;
3270
3271 case 'x':
3272 case 'v':
3273 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3274 continue;
3275
3276 case 'z':
3277 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3278 continue;
3279
3280 case 'Z':
3281 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3282 continue;
3283
3284 case '0':
3285 case 'S':
3286 case 'P':
3287 case 'R':
3288 continue;
3289
3290 case 'X':
3291 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3292 continue;
3293
3294 case 'Y':
3295 {
3296 int regno;
3297
3298 regno = va_arg (args, int);
3299 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3300 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3301 }
3302 continue;
3303
3304 case '<':
3305 case '>':
3306 case '4':
3307 case '5':
3308 case 'H':
3309 case 'W':
3310 case 'D':
3311 case 'j':
3312 case '8':
3313 case 'V':
3314 case 'C':
3315 case 'U':
3316 case 'k':
3317 case 'K':
3318 case 'p':
3319 case 'q':
3320 {
3321 assert (ep != NULL);
3322
3323 if (ep->X_op != O_constant)
3324 *r = (int) BFD_RELOC_UNUSED + c;
3325 else
3326 {
3327 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3328 FALSE, &insn.insn_opcode, &insn.use_extend,
3329 &insn.extend);
3330 ep = NULL;
3331 *r = BFD_RELOC_UNUSED;
3332 }
3333 }
3334 continue;
3335
3336 case '6':
3337 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3338 continue;
3339 }
3340
3341 break;
3342 }
3343
3344 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3345
3346 append_insn (&insn, ep, r);
3347 }
3348
3349 /*
3350 * Generate a "jalr" instruction with a relocation hint to the called
3351 * function. This occurs in NewABI PIC code.
3352 */
3353 static void
3354 macro_build_jalr (expressionS *ep)
3355 {
3356 char *f = NULL;
3357
3358 if (HAVE_NEWABI)
3359 {
3360 frag_grow (8);
3361 f = frag_more (0);
3362 }
3363 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3364 if (HAVE_NEWABI)
3365 fix_new_exp (frag_now, f - frag_now->fr_literal,
3366 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3367 }
3368
3369 /*
3370 * Generate a "lui" instruction.
3371 */
3372 static void
3373 macro_build_lui (expressionS *ep, int regnum)
3374 {
3375 expressionS high_expr;
3376 struct mips_cl_insn insn;
3377 bfd_reloc_code_real_type r[3]
3378 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3379 const char *name = "lui";
3380 const char *fmt = "t,u";
3381
3382 assert (! mips_opts.mips16);
3383
3384 high_expr = *ep;
3385
3386 if (high_expr.X_op == O_constant)
3387 {
3388 /* we can compute the instruction now without a relocation entry */
3389 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3390 >> 16) & 0xffff;
3391 *r = BFD_RELOC_UNUSED;
3392 }
3393 else
3394 {
3395 assert (ep->X_op == O_symbol);
3396 /* _gp_disp is a special case, used from s_cpload. _gp is used
3397 if mips_no_shared. */
3398 assert (mips_pic == NO_PIC
3399 || (! HAVE_NEWABI
3400 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3401 || (! mips_in_shared
3402 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp") == 0));
3403 *r = BFD_RELOC_HI16_S;
3404 }
3405
3406 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3407 assert (insn.insn_mo);
3408 assert (strcmp (name, insn.insn_mo->name) == 0);
3409 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3410
3411 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3412 if (*r == BFD_RELOC_UNUSED)
3413 {
3414 insn.insn_opcode |= high_expr.X_add_number;
3415 append_insn (&insn, NULL, r);
3416 }
3417 else
3418 append_insn (&insn, &high_expr, r);
3419 }
3420
3421 /* Generate a sequence of instructions to do a load or store from a constant
3422 offset off of a base register (breg) into/from a target register (treg),
3423 using AT if necessary. */
3424 static void
3425 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3426 int treg, int breg, int dbl)
3427 {
3428 assert (ep->X_op == O_constant);
3429
3430 /* Sign-extending 32-bit constants makes their handling easier. */
3431 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3432 == ~((bfd_vma) 0x7fffffff)))
3433 {
3434 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3435 as_bad (_("constant too large"));
3436
3437 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3438 - 0x80000000);
3439 }
3440
3441 /* Right now, this routine can only handle signed 32-bit constants. */
3442 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3443 as_warn (_("operand overflow"));
3444
3445 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3446 {
3447 /* Signed 16-bit offset will fit in the op. Easy! */
3448 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3449 }
3450 else
3451 {
3452 /* 32-bit offset, need multiple instructions and AT, like:
3453 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3454 addu $tempreg,$tempreg,$breg
3455 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3456 to handle the complete offset. */
3457 macro_build_lui (ep, AT);
3458 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3459 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3460
3461 if (mips_opts.noat)
3462 as_bad (_("Macro used $at after \".set noat\""));
3463 }
3464 }
3465
3466 /* set_at()
3467 * Generates code to set the $at register to true (one)
3468 * if reg is less than the immediate expression.
3469 */
3470 static void
3471 set_at (int reg, int unsignedp)
3472 {
3473 if (imm_expr.X_op == O_constant
3474 && imm_expr.X_add_number >= -0x8000
3475 && imm_expr.X_add_number < 0x8000)
3476 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3477 AT, reg, BFD_RELOC_LO16);
3478 else
3479 {
3480 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3481 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3482 }
3483 }
3484
3485 static void
3486 normalize_constant_expr (expressionS *ex)
3487 {
3488 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3489 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3490 - 0x80000000);
3491 }
3492
3493 /* Warn if an expression is not a constant. */
3494
3495 static void
3496 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3497 {
3498 if (ex->X_op == O_big)
3499 as_bad (_("unsupported large constant"));
3500 else if (ex->X_op != O_constant)
3501 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3502
3503 normalize_constant_expr (ex);
3504 }
3505
3506 /* Count the leading zeroes by performing a binary chop. This is a
3507 bulky bit of source, but performance is a LOT better for the
3508 majority of values than a simple loop to count the bits:
3509 for (lcnt = 0; (lcnt < 32); lcnt++)
3510 if ((v) & (1 << (31 - lcnt)))
3511 break;
3512 However it is not code size friendly, and the gain will drop a bit
3513 on certain cached systems.
3514 */
3515 #define COUNT_TOP_ZEROES(v) \
3516 (((v) & ~0xffff) == 0 \
3517 ? ((v) & ~0xff) == 0 \
3518 ? ((v) & ~0xf) == 0 \
3519 ? ((v) & ~0x3) == 0 \
3520 ? ((v) & ~0x1) == 0 \
3521 ? !(v) \
3522 ? 32 \
3523 : 31 \
3524 : 30 \
3525 : ((v) & ~0x7) == 0 \
3526 ? 29 \
3527 : 28 \
3528 : ((v) & ~0x3f) == 0 \
3529 ? ((v) & ~0x1f) == 0 \
3530 ? 27 \
3531 : 26 \
3532 : ((v) & ~0x7f) == 0 \
3533 ? 25 \
3534 : 24 \
3535 : ((v) & ~0xfff) == 0 \
3536 ? ((v) & ~0x3ff) == 0 \
3537 ? ((v) & ~0x1ff) == 0 \
3538 ? 23 \
3539 : 22 \
3540 : ((v) & ~0x7ff) == 0 \
3541 ? 21 \
3542 : 20 \
3543 : ((v) & ~0x3fff) == 0 \
3544 ? ((v) & ~0x1fff) == 0 \
3545 ? 19 \
3546 : 18 \
3547 : ((v) & ~0x7fff) == 0 \
3548 ? 17 \
3549 : 16 \
3550 : ((v) & ~0xffffff) == 0 \
3551 ? ((v) & ~0xfffff) == 0 \
3552 ? ((v) & ~0x3ffff) == 0 \
3553 ? ((v) & ~0x1ffff) == 0 \
3554 ? 15 \
3555 : 14 \
3556 : ((v) & ~0x7ffff) == 0 \
3557 ? 13 \
3558 : 12 \
3559 : ((v) & ~0x3fffff) == 0 \
3560 ? ((v) & ~0x1fffff) == 0 \
3561 ? 11 \
3562 : 10 \
3563 : ((v) & ~0x7fffff) == 0 \
3564 ? 9 \
3565 : 8 \
3566 : ((v) & ~0xfffffff) == 0 \
3567 ? ((v) & ~0x3ffffff) == 0 \
3568 ? ((v) & ~0x1ffffff) == 0 \
3569 ? 7 \
3570 : 6 \
3571 : ((v) & ~0x7ffffff) == 0 \
3572 ? 5 \
3573 : 4 \
3574 : ((v) & ~0x3fffffff) == 0 \
3575 ? ((v) & ~0x1fffffff) == 0 \
3576 ? 3 \
3577 : 2 \
3578 : ((v) & ~0x7fffffff) == 0 \
3579 ? 1 \
3580 : 0)
3581
3582 /* load_register()
3583 * This routine generates the least number of instructions necessary to load
3584 * an absolute expression value into a register.
3585 */
3586 static void
3587 load_register (int reg, expressionS *ep, int dbl)
3588 {
3589 int freg;
3590 expressionS hi32, lo32;
3591
3592 if (ep->X_op != O_big)
3593 {
3594 assert (ep->X_op == O_constant);
3595
3596 /* Sign-extending 32-bit constants makes their handling easier. */
3597 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3598 == ~((bfd_vma) 0x7fffffff)))
3599 {
3600 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3601 as_bad (_("constant too large"));
3602
3603 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3604 - 0x80000000);
3605 }
3606
3607 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3608 {
3609 /* We can handle 16 bit signed values with an addiu to
3610 $zero. No need to ever use daddiu here, since $zero and
3611 the result are always correct in 32 bit mode. */
3612 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3613 return;
3614 }
3615 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3616 {
3617 /* We can handle 16 bit unsigned values with an ori to
3618 $zero. */
3619 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3620 return;
3621 }
3622 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3623 {
3624 /* 32 bit values require an lui. */
3625 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3626 if ((ep->X_add_number & 0xffff) != 0)
3627 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3628 return;
3629 }
3630 }
3631
3632 /* The value is larger than 32 bits. */
3633
3634 if (HAVE_32BIT_GPRS)
3635 {
3636 as_bad (_("Number (0x%lx) larger than 32 bits"),
3637 (unsigned long) ep->X_add_number);
3638 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3639 return;
3640 }
3641
3642 if (ep->X_op != O_big)
3643 {
3644 hi32 = *ep;
3645 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3646 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3647 hi32.X_add_number &= 0xffffffff;
3648 lo32 = *ep;
3649 lo32.X_add_number &= 0xffffffff;
3650 }
3651 else
3652 {
3653 assert (ep->X_add_number > 2);
3654 if (ep->X_add_number == 3)
3655 generic_bignum[3] = 0;
3656 else if (ep->X_add_number > 4)
3657 as_bad (_("Number larger than 64 bits"));
3658 lo32.X_op = O_constant;
3659 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3660 hi32.X_op = O_constant;
3661 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3662 }
3663
3664 if (hi32.X_add_number == 0)
3665 freg = 0;
3666 else
3667 {
3668 int shift, bit;
3669 unsigned long hi, lo;
3670
3671 if (hi32.X_add_number == (offsetT) 0xffffffff)
3672 {
3673 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3674 {
3675 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3676 return;
3677 }
3678 if (lo32.X_add_number & 0x80000000)
3679 {
3680 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3681 if (lo32.X_add_number & 0xffff)
3682 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3683 return;
3684 }
3685 }
3686
3687 /* Check for 16bit shifted constant. We know that hi32 is
3688 non-zero, so start the mask on the first bit of the hi32
3689 value. */
3690 shift = 17;
3691 do
3692 {
3693 unsigned long himask, lomask;
3694
3695 if (shift < 32)
3696 {
3697 himask = 0xffff >> (32 - shift);
3698 lomask = (0xffff << shift) & 0xffffffff;
3699 }
3700 else
3701 {
3702 himask = 0xffff << (shift - 32);
3703 lomask = 0;
3704 }
3705 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3706 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3707 {
3708 expressionS tmp;
3709
3710 tmp.X_op = O_constant;
3711 if (shift < 32)
3712 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3713 | (lo32.X_add_number >> shift));
3714 else
3715 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3716 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3717 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3718 reg, reg, (shift >= 32) ? shift - 32 : shift);
3719 return;
3720 }
3721 ++shift;
3722 }
3723 while (shift <= (64 - 16));
3724
3725 /* Find the bit number of the lowest one bit, and store the
3726 shifted value in hi/lo. */
3727 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3728 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3729 if (lo != 0)
3730 {
3731 bit = 0;
3732 while ((lo & 1) == 0)
3733 {
3734 lo >>= 1;
3735 ++bit;
3736 }
3737 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3738 hi >>= bit;
3739 }
3740 else
3741 {
3742 bit = 32;
3743 while ((hi & 1) == 0)
3744 {
3745 hi >>= 1;
3746 ++bit;
3747 }
3748 lo = hi;
3749 hi = 0;
3750 }
3751
3752 /* Optimize if the shifted value is a (power of 2) - 1. */
3753 if ((hi == 0 && ((lo + 1) & lo) == 0)
3754 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3755 {
3756 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3757 if (shift != 0)
3758 {
3759 expressionS tmp;
3760
3761 /* This instruction will set the register to be all
3762 ones. */
3763 tmp.X_op = O_constant;
3764 tmp.X_add_number = (offsetT) -1;
3765 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3766 if (bit != 0)
3767 {
3768 bit += shift;
3769 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3770 reg, reg, (bit >= 32) ? bit - 32 : bit);
3771 }
3772 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3773 reg, reg, (shift >= 32) ? shift - 32 : shift);
3774 return;
3775 }
3776 }
3777
3778 /* Sign extend hi32 before calling load_register, because we can
3779 generally get better code when we load a sign extended value. */
3780 if ((hi32.X_add_number & 0x80000000) != 0)
3781 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3782 load_register (reg, &hi32, 0);
3783 freg = reg;
3784 }
3785 if ((lo32.X_add_number & 0xffff0000) == 0)
3786 {
3787 if (freg != 0)
3788 {
3789 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3790 freg = reg;
3791 }
3792 }
3793 else
3794 {
3795 expressionS mid16;
3796
3797 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3798 {
3799 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3800 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3801 return;
3802 }
3803
3804 if (freg != 0)
3805 {
3806 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3807 freg = reg;
3808 }
3809 mid16 = lo32;
3810 mid16.X_add_number >>= 16;
3811 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3812 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3813 freg = reg;
3814 }
3815 if ((lo32.X_add_number & 0xffff) != 0)
3816 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3817 }
3818
3819 static inline void
3820 load_delay_nop (void)
3821 {
3822 if (!gpr_interlocks)
3823 macro_build (NULL, "nop", "");
3824 }
3825
3826 /* Load an address into a register. */
3827
3828 static void
3829 load_address (int reg, expressionS *ep, int *used_at)
3830 {
3831 if (ep->X_op != O_constant
3832 && ep->X_op != O_symbol)
3833 {
3834 as_bad (_("expression too complex"));
3835 ep->X_op = O_constant;
3836 }
3837
3838 if (ep->X_op == O_constant)
3839 {
3840 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3841 return;
3842 }
3843
3844 if (mips_pic == NO_PIC)
3845 {
3846 /* If this is a reference to a GP relative symbol, we want
3847 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3848 Otherwise we want
3849 lui $reg,<sym> (BFD_RELOC_HI16_S)
3850 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3851 If we have an addend, we always use the latter form.
3852
3853 With 64bit address space and a usable $at we want
3854 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3855 lui $at,<sym> (BFD_RELOC_HI16_S)
3856 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3857 daddiu $at,<sym> (BFD_RELOC_LO16)
3858 dsll32 $reg,0
3859 daddu $reg,$reg,$at
3860
3861 If $at is already in use, we use a path which is suboptimal
3862 on superscalar processors.
3863 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3864 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3865 dsll $reg,16
3866 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3867 dsll $reg,16
3868 daddiu $reg,<sym> (BFD_RELOC_LO16)
3869
3870 For GP relative symbols in 64bit address space we can use
3871 the same sequence as in 32bit address space. */
3872 if (HAVE_64BIT_SYMBOLS)
3873 {
3874 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3875 && !nopic_need_relax (ep->X_add_symbol, 1))
3876 {
3877 relax_start (ep->X_add_symbol);
3878 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3879 mips_gp_register, BFD_RELOC_GPREL16);
3880 relax_switch ();
3881 }
3882
3883 if (*used_at == 0 && !mips_opts.noat)
3884 {
3885 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3886 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3887 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3888 BFD_RELOC_MIPS_HIGHER);
3889 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3890 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3891 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3892 *used_at = 1;
3893 }
3894 else
3895 {
3896 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3897 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3898 BFD_RELOC_MIPS_HIGHER);
3899 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3900 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3901 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3902 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3903 }
3904
3905 if (mips_relax.sequence)
3906 relax_end ();
3907 }
3908 else
3909 {
3910 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3911 && !nopic_need_relax (ep->X_add_symbol, 1))
3912 {
3913 relax_start (ep->X_add_symbol);
3914 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3915 mips_gp_register, BFD_RELOC_GPREL16);
3916 relax_switch ();
3917 }
3918 macro_build_lui (ep, reg);
3919 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3920 reg, reg, BFD_RELOC_LO16);
3921 if (mips_relax.sequence)
3922 relax_end ();
3923 }
3924 }
3925 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3926 {
3927 expressionS ex;
3928
3929 /* If this is a reference to an external symbol, we want
3930 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3931 Otherwise we want
3932 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3933 nop
3934 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3935 If there is a constant, it must be added in after.
3936
3937 If we have NewABI, we want
3938 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3939 unless we're referencing a global symbol with a non-zero
3940 offset, in which case cst must be added separately. */
3941 if (HAVE_NEWABI)
3942 {
3943 if (ep->X_add_number)
3944 {
3945 ex.X_add_number = ep->X_add_number;
3946 ep->X_add_number = 0;
3947 relax_start (ep->X_add_symbol);
3948 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3949 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3950 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3951 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3952 ex.X_op = O_constant;
3953 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3954 reg, reg, BFD_RELOC_LO16);
3955 ep->X_add_number = ex.X_add_number;
3956 relax_switch ();
3957 }
3958 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3959 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3960 if (mips_relax.sequence)
3961 relax_end ();
3962 }
3963 else
3964 {
3965 ex.X_add_number = ep->X_add_number;
3966 ep->X_add_number = 0;
3967 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3968 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3969 load_delay_nop ();
3970 relax_start (ep->X_add_symbol);
3971 relax_switch ();
3972 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3973 BFD_RELOC_LO16);
3974 relax_end ();
3975
3976 if (ex.X_add_number != 0)
3977 {
3978 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3979 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3980 ex.X_op = O_constant;
3981 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3982 reg, reg, BFD_RELOC_LO16);
3983 }
3984 }
3985 }
3986 else if (mips_pic == SVR4_PIC)
3987 {
3988 expressionS ex;
3989
3990 /* This is the large GOT case. If this is a reference to an
3991 external symbol, we want
3992 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3993 addu $reg,$reg,$gp
3994 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3995
3996 Otherwise, for a reference to a local symbol in old ABI, we want
3997 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3998 nop
3999 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4000 If there is a constant, it must be added in after.
4001
4002 In the NewABI, for local symbols, with or without offsets, we want:
4003 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4004 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4005 */
4006 if (HAVE_NEWABI)
4007 {
4008 ex.X_add_number = ep->X_add_number;
4009 ep->X_add_number = 0;
4010 relax_start (ep->X_add_symbol);
4011 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4012 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4013 reg, reg, mips_gp_register);
4014 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4015 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4016 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4017 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4018 else if (ex.X_add_number)
4019 {
4020 ex.X_op = O_constant;
4021 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4022 BFD_RELOC_LO16);
4023 }
4024
4025 ep->X_add_number = ex.X_add_number;
4026 relax_switch ();
4027 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4028 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4029 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4030 BFD_RELOC_MIPS_GOT_OFST);
4031 relax_end ();
4032 }
4033 else
4034 {
4035 ex.X_add_number = ep->X_add_number;
4036 ep->X_add_number = 0;
4037 relax_start (ep->X_add_symbol);
4038 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4040 reg, reg, mips_gp_register);
4041 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4042 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4043 relax_switch ();
4044 if (reg_needs_delay (mips_gp_register))
4045 {
4046 /* We need a nop before loading from $gp. This special
4047 check is required because the lui which starts the main
4048 instruction stream does not refer to $gp, and so will not
4049 insert the nop which may be required. */
4050 macro_build (NULL, "nop", "");
4051 }
4052 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4053 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4054 load_delay_nop ();
4055 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4056 BFD_RELOC_LO16);
4057 relax_end ();
4058
4059 if (ex.X_add_number != 0)
4060 {
4061 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4062 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4063 ex.X_op = O_constant;
4064 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4065 BFD_RELOC_LO16);
4066 }
4067 }
4068 }
4069 else
4070 abort ();
4071
4072 if (mips_opts.noat && *used_at == 1)
4073 as_bad (_("Macro used $at after \".set noat\""));
4074 }
4075
4076 /* Move the contents of register SOURCE into register DEST. */
4077
4078 static void
4079 move_register (int dest, int source)
4080 {
4081 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4082 dest, source, 0);
4083 }
4084
4085 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4086 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4087 The two alternatives are:
4088
4089 Global symbol Local sybmol
4090 ------------- ------------
4091 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4092 ... ...
4093 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4094
4095 load_got_offset emits the first instruction and add_got_offset
4096 emits the second for a 16-bit offset or add_got_offset_hilo emits
4097 a sequence to add a 32-bit offset using a scratch register. */
4098
4099 static void
4100 load_got_offset (int dest, expressionS *local)
4101 {
4102 expressionS global;
4103
4104 global = *local;
4105 global.X_add_number = 0;
4106
4107 relax_start (local->X_add_symbol);
4108 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4109 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4110 relax_switch ();
4111 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4112 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4113 relax_end ();
4114 }
4115
4116 static void
4117 add_got_offset (int dest, expressionS *local)
4118 {
4119 expressionS global;
4120
4121 global.X_op = O_constant;
4122 global.X_op_symbol = NULL;
4123 global.X_add_symbol = NULL;
4124 global.X_add_number = local->X_add_number;
4125
4126 relax_start (local->X_add_symbol);
4127 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4128 dest, dest, BFD_RELOC_LO16);
4129 relax_switch ();
4130 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4131 relax_end ();
4132 }
4133
4134 static void
4135 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4136 {
4137 expressionS global;
4138 int hold_mips_optimize;
4139
4140 global.X_op = O_constant;
4141 global.X_op_symbol = NULL;
4142 global.X_add_symbol = NULL;
4143 global.X_add_number = local->X_add_number;
4144
4145 relax_start (local->X_add_symbol);
4146 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4147 relax_switch ();
4148 /* Set mips_optimize around the lui instruction to avoid
4149 inserting an unnecessary nop after the lw. */
4150 hold_mips_optimize = mips_optimize;
4151 mips_optimize = 2;
4152 macro_build_lui (&global, tmp);
4153 mips_optimize = hold_mips_optimize;
4154 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4155 relax_end ();
4156
4157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4158 }
4159
4160 /*
4161 * Build macros
4162 * This routine implements the seemingly endless macro or synthesized
4163 * instructions and addressing modes in the mips assembly language. Many
4164 * of these macros are simple and are similar to each other. These could
4165 * probably be handled by some kind of table or grammar approach instead of
4166 * this verbose method. Others are not simple macros but are more like
4167 * optimizing code generation.
4168 * One interesting optimization is when several store macros appear
4169 * consecutively that would load AT with the upper half of the same address.
4170 * The ensuing load upper instructions are ommited. This implies some kind
4171 * of global optimization. We currently only optimize within a single macro.
4172 * For many of the load and store macros if the address is specified as a
4173 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4174 * first load register 'at' with zero and use it as the base register. The
4175 * mips assembler simply uses register $zero. Just one tiny optimization
4176 * we're missing.
4177 */
4178 static void
4179 macro (struct mips_cl_insn *ip)
4180 {
4181 register int treg, sreg, dreg, breg;
4182 int tempreg;
4183 int mask;
4184 int used_at = 0;
4185 expressionS expr1;
4186 const char *s;
4187 const char *s2;
4188 const char *fmt;
4189 int likely = 0;
4190 int dbl = 0;
4191 int coproc = 0;
4192 int lr = 0;
4193 int imm = 0;
4194 int call = 0;
4195 int off;
4196 offsetT maxnum;
4197 bfd_reloc_code_real_type r;
4198 int hold_mips_optimize;
4199
4200 assert (! mips_opts.mips16);
4201
4202 treg = (ip->insn_opcode >> 16) & 0x1f;
4203 dreg = (ip->insn_opcode >> 11) & 0x1f;
4204 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4205 mask = ip->insn_mo->mask;
4206
4207 expr1.X_op = O_constant;
4208 expr1.X_op_symbol = NULL;
4209 expr1.X_add_symbol = NULL;
4210 expr1.X_add_number = 1;
4211
4212 switch (mask)
4213 {
4214 case M_DABS:
4215 dbl = 1;
4216 case M_ABS:
4217 /* bgez $a0,.+12
4218 move v0,$a0
4219 sub v0,$zero,$a0
4220 */
4221
4222 mips_emit_delays (TRUE);
4223 ++mips_opts.noreorder;
4224 mips_any_noreorder = 1;
4225
4226 expr1.X_add_number = 8;
4227 macro_build (&expr1, "bgez", "s,p", sreg);
4228 if (dreg == sreg)
4229 macro_build (NULL, "nop", "", 0);
4230 else
4231 move_register (dreg, sreg);
4232 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4233
4234 --mips_opts.noreorder;
4235 break;
4236
4237 case M_ADD_I:
4238 s = "addi";
4239 s2 = "add";
4240 goto do_addi;
4241 case M_ADDU_I:
4242 s = "addiu";
4243 s2 = "addu";
4244 goto do_addi;
4245 case M_DADD_I:
4246 dbl = 1;
4247 s = "daddi";
4248 s2 = "dadd";
4249 goto do_addi;
4250 case M_DADDU_I:
4251 dbl = 1;
4252 s = "daddiu";
4253 s2 = "daddu";
4254 do_addi:
4255 if (imm_expr.X_op == O_constant
4256 && imm_expr.X_add_number >= -0x8000
4257 && imm_expr.X_add_number < 0x8000)
4258 {
4259 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4260 break;
4261 }
4262 used_at = 1;
4263 load_register (AT, &imm_expr, dbl);
4264 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4265 break;
4266
4267 case M_AND_I:
4268 s = "andi";
4269 s2 = "and";
4270 goto do_bit;
4271 case M_OR_I:
4272 s = "ori";
4273 s2 = "or";
4274 goto do_bit;
4275 case M_NOR_I:
4276 s = "";
4277 s2 = "nor";
4278 goto do_bit;
4279 case M_XOR_I:
4280 s = "xori";
4281 s2 = "xor";
4282 do_bit:
4283 if (imm_expr.X_op == O_constant
4284 && imm_expr.X_add_number >= 0
4285 && imm_expr.X_add_number < 0x10000)
4286 {
4287 if (mask != M_NOR_I)
4288 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4289 else
4290 {
4291 macro_build (&imm_expr, "ori", "t,r,i",
4292 treg, sreg, BFD_RELOC_LO16);
4293 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4294 }
4295 break;
4296 }
4297
4298 used_at = 1;
4299 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4300 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4301 break;
4302
4303 case M_BEQ_I:
4304 s = "beq";
4305 goto beq_i;
4306 case M_BEQL_I:
4307 s = "beql";
4308 likely = 1;
4309 goto beq_i;
4310 case M_BNE_I:
4311 s = "bne";
4312 goto beq_i;
4313 case M_BNEL_I:
4314 s = "bnel";
4315 likely = 1;
4316 beq_i:
4317 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4318 {
4319 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4320 break;
4321 }
4322 used_at = 1;
4323 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4324 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4325 break;
4326
4327 case M_BGEL:
4328 likely = 1;
4329 case M_BGE:
4330 if (treg == 0)
4331 {
4332 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4333 break;
4334 }
4335 if (sreg == 0)
4336 {
4337 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4338 break;
4339 }
4340 used_at = 1;
4341 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4342 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4343 break;
4344
4345 case M_BGTL_I:
4346 likely = 1;
4347 case M_BGT_I:
4348 /* check for > max integer */
4349 maxnum = 0x7fffffff;
4350 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4351 {
4352 maxnum <<= 16;
4353 maxnum |= 0xffff;
4354 maxnum <<= 16;
4355 maxnum |= 0xffff;
4356 }
4357 if (imm_expr.X_op == O_constant
4358 && imm_expr.X_add_number >= maxnum
4359 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4360 {
4361 do_false:
4362 /* result is always false */
4363 if (! likely)
4364 macro_build (NULL, "nop", "", 0);
4365 else
4366 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4367 break;
4368 }
4369 if (imm_expr.X_op != O_constant)
4370 as_bad (_("Unsupported large constant"));
4371 ++imm_expr.X_add_number;
4372 /* FALLTHROUGH */
4373 case M_BGE_I:
4374 case M_BGEL_I:
4375 if (mask == M_BGEL_I)
4376 likely = 1;
4377 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4378 {
4379 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4380 break;
4381 }
4382 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4383 {
4384 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4385 break;
4386 }
4387 maxnum = 0x7fffffff;
4388 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4389 {
4390 maxnum <<= 16;
4391 maxnum |= 0xffff;
4392 maxnum <<= 16;
4393 maxnum |= 0xffff;
4394 }
4395 maxnum = - maxnum - 1;
4396 if (imm_expr.X_op == O_constant
4397 && imm_expr.X_add_number <= maxnum
4398 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4399 {
4400 do_true:
4401 /* result is always true */
4402 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4403 macro_build (&offset_expr, "b", "p");
4404 break;
4405 }
4406 used_at = 1;
4407 set_at (sreg, 0);
4408 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4409 break;
4410
4411 case M_BGEUL:
4412 likely = 1;
4413 case M_BGEU:
4414 if (treg == 0)
4415 goto do_true;
4416 if (sreg == 0)
4417 {
4418 macro_build (&offset_expr, likely ? "beql" : "beq",
4419 "s,t,p", 0, treg);
4420 break;
4421 }
4422 used_at = 1;
4423 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4424 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4425 break;
4426
4427 case M_BGTUL_I:
4428 likely = 1;
4429 case M_BGTU_I:
4430 if (sreg == 0
4431 || (HAVE_32BIT_GPRS
4432 && imm_expr.X_op == O_constant
4433 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4434 goto do_false;
4435 if (imm_expr.X_op != O_constant)
4436 as_bad (_("Unsupported large constant"));
4437 ++imm_expr.X_add_number;
4438 /* FALLTHROUGH */
4439 case M_BGEU_I:
4440 case M_BGEUL_I:
4441 if (mask == M_BGEUL_I)
4442 likely = 1;
4443 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4444 goto do_true;
4445 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4446 {
4447 macro_build (&offset_expr, likely ? "bnel" : "bne",
4448 "s,t,p", sreg, 0);
4449 break;
4450 }
4451 used_at = 1;
4452 set_at (sreg, 1);
4453 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4454 break;
4455
4456 case M_BGTL:
4457 likely = 1;
4458 case M_BGT:
4459 if (treg == 0)
4460 {
4461 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4462 break;
4463 }
4464 if (sreg == 0)
4465 {
4466 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4467 break;
4468 }
4469 used_at = 1;
4470 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4471 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4472 break;
4473
4474 case M_BGTUL:
4475 likely = 1;
4476 case M_BGTU:
4477 if (treg == 0)
4478 {
4479 macro_build (&offset_expr, likely ? "bnel" : "bne",
4480 "s,t,p", sreg, 0);
4481 break;
4482 }
4483 if (sreg == 0)
4484 goto do_false;
4485 used_at = 1;
4486 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4487 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4488 break;
4489
4490 case M_BLEL:
4491 likely = 1;
4492 case M_BLE:
4493 if (treg == 0)
4494 {
4495 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4496 break;
4497 }
4498 if (sreg == 0)
4499 {
4500 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4501 break;
4502 }
4503 used_at = 1;
4504 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4505 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4506 break;
4507
4508 case M_BLEL_I:
4509 likely = 1;
4510 case M_BLE_I:
4511 maxnum = 0x7fffffff;
4512 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4513 {
4514 maxnum <<= 16;
4515 maxnum |= 0xffff;
4516 maxnum <<= 16;
4517 maxnum |= 0xffff;
4518 }
4519 if (imm_expr.X_op == O_constant
4520 && imm_expr.X_add_number >= maxnum
4521 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4522 goto do_true;
4523 if (imm_expr.X_op != O_constant)
4524 as_bad (_("Unsupported large constant"));
4525 ++imm_expr.X_add_number;
4526 /* FALLTHROUGH */
4527 case M_BLT_I:
4528 case M_BLTL_I:
4529 if (mask == M_BLTL_I)
4530 likely = 1;
4531 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4532 {
4533 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4534 break;
4535 }
4536 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4537 {
4538 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4539 break;
4540 }
4541 used_at = 1;
4542 set_at (sreg, 0);
4543 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4544 break;
4545
4546 case M_BLEUL:
4547 likely = 1;
4548 case M_BLEU:
4549 if (treg == 0)
4550 {
4551 macro_build (&offset_expr, likely ? "beql" : "beq",
4552 "s,t,p", sreg, 0);
4553 break;
4554 }
4555 if (sreg == 0)
4556 goto do_true;
4557 used_at = 1;
4558 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4559 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4560 break;
4561
4562 case M_BLEUL_I:
4563 likely = 1;
4564 case M_BLEU_I:
4565 if (sreg == 0
4566 || (HAVE_32BIT_GPRS
4567 && imm_expr.X_op == O_constant
4568 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4569 goto do_true;
4570 if (imm_expr.X_op != O_constant)
4571 as_bad (_("Unsupported large constant"));
4572 ++imm_expr.X_add_number;
4573 /* FALLTHROUGH */
4574 case M_BLTU_I:
4575 case M_BLTUL_I:
4576 if (mask == M_BLTUL_I)
4577 likely = 1;
4578 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4579 goto do_false;
4580 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4581 {
4582 macro_build (&offset_expr, likely ? "beql" : "beq",
4583 "s,t,p", sreg, 0);
4584 break;
4585 }
4586 used_at = 1;
4587 set_at (sreg, 1);
4588 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4589 break;
4590
4591 case M_BLTL:
4592 likely = 1;
4593 case M_BLT:
4594 if (treg == 0)
4595 {
4596 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4597 break;
4598 }
4599 if (sreg == 0)
4600 {
4601 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4602 break;
4603 }
4604 used_at = 1;
4605 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4606 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4607 break;
4608
4609 case M_BLTUL:
4610 likely = 1;
4611 case M_BLTU:
4612 if (treg == 0)
4613 goto do_false;
4614 if (sreg == 0)
4615 {
4616 macro_build (&offset_expr, likely ? "bnel" : "bne",
4617 "s,t,p", 0, treg);
4618 break;
4619 }
4620 used_at = 1;
4621 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4622 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4623 break;
4624
4625 case M_DEXT:
4626 {
4627 unsigned long pos;
4628 unsigned long size;
4629
4630 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4631 {
4632 as_bad (_("Unsupported large constant"));
4633 pos = size = 1;
4634 }
4635 else
4636 {
4637 pos = (unsigned long) imm_expr.X_add_number;
4638 size = (unsigned long) imm2_expr.X_add_number;
4639 }
4640
4641 if (pos > 63)
4642 {
4643 as_bad (_("Improper position (%lu)"), pos);
4644 pos = 1;
4645 }
4646 if (size == 0 || size > 64
4647 || (pos + size - 1) > 63)
4648 {
4649 as_bad (_("Improper extract size (%lu, position %lu)"),
4650 size, pos);
4651 size = 1;
4652 }
4653
4654 if (size <= 32 && pos < 32)
4655 {
4656 s = "dext";
4657 fmt = "t,r,+A,+C";
4658 }
4659 else if (size <= 32)
4660 {
4661 s = "dextu";
4662 fmt = "t,r,+E,+H";
4663 }
4664 else
4665 {
4666 s = "dextm";
4667 fmt = "t,r,+A,+G";
4668 }
4669 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4670 }
4671 break;
4672
4673 case M_DINS:
4674 {
4675 unsigned long pos;
4676 unsigned long size;
4677
4678 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4679 {
4680 as_bad (_("Unsupported large constant"));
4681 pos = size = 1;
4682 }
4683 else
4684 {
4685 pos = (unsigned long) imm_expr.X_add_number;
4686 size = (unsigned long) imm2_expr.X_add_number;
4687 }
4688
4689 if (pos > 63)
4690 {
4691 as_bad (_("Improper position (%lu)"), pos);
4692 pos = 1;
4693 }
4694 if (size == 0 || size > 64
4695 || (pos + size - 1) > 63)
4696 {
4697 as_bad (_("Improper insert size (%lu, position %lu)"),
4698 size, pos);
4699 size = 1;
4700 }
4701
4702 if (pos < 32 && (pos + size - 1) < 32)
4703 {
4704 s = "dins";
4705 fmt = "t,r,+A,+B";
4706 }
4707 else if (pos >= 32)
4708 {
4709 s = "dinsu";
4710 fmt = "t,r,+E,+F";
4711 }
4712 else
4713 {
4714 s = "dinsm";
4715 fmt = "t,r,+A,+F";
4716 }
4717 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4718 pos + size - 1);
4719 }
4720 break;
4721
4722 case M_DDIV_3:
4723 dbl = 1;
4724 case M_DIV_3:
4725 s = "mflo";
4726 goto do_div3;
4727 case M_DREM_3:
4728 dbl = 1;
4729 case M_REM_3:
4730 s = "mfhi";
4731 do_div3:
4732 if (treg == 0)
4733 {
4734 as_warn (_("Divide by zero."));
4735 if (mips_trap)
4736 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4737 else
4738 macro_build (NULL, "break", "c", 7);
4739 break;
4740 }
4741
4742 mips_emit_delays (TRUE);
4743 ++mips_opts.noreorder;
4744 mips_any_noreorder = 1;
4745 if (mips_trap)
4746 {
4747 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4748 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4749 }
4750 else
4751 {
4752 expr1.X_add_number = 8;
4753 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4754 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4755 macro_build (NULL, "break", "c", 7);
4756 }
4757 expr1.X_add_number = -1;
4758 used_at = 1;
4759 load_register (AT, &expr1, dbl);
4760 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4761 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4762 if (dbl)
4763 {
4764 expr1.X_add_number = 1;
4765 load_register (AT, &expr1, dbl);
4766 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4767 }
4768 else
4769 {
4770 expr1.X_add_number = 0x80000000;
4771 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4772 }
4773 if (mips_trap)
4774 {
4775 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4776 /* We want to close the noreorder block as soon as possible, so
4777 that later insns are available for delay slot filling. */
4778 --mips_opts.noreorder;
4779 }
4780 else
4781 {
4782 expr1.X_add_number = 8;
4783 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4784 macro_build (NULL, "nop", "", 0);
4785
4786 /* We want to close the noreorder block as soon as possible, so
4787 that later insns are available for delay slot filling. */
4788 --mips_opts.noreorder;
4789
4790 macro_build (NULL, "break", "c", 6);
4791 }
4792 macro_build (NULL, s, "d", dreg);
4793 break;
4794
4795 case M_DIV_3I:
4796 s = "div";
4797 s2 = "mflo";
4798 goto do_divi;
4799 case M_DIVU_3I:
4800 s = "divu";
4801 s2 = "mflo";
4802 goto do_divi;
4803 case M_REM_3I:
4804 s = "div";
4805 s2 = "mfhi";
4806 goto do_divi;
4807 case M_REMU_3I:
4808 s = "divu";
4809 s2 = "mfhi";
4810 goto do_divi;
4811 case M_DDIV_3I:
4812 dbl = 1;
4813 s = "ddiv";
4814 s2 = "mflo";
4815 goto do_divi;
4816 case M_DDIVU_3I:
4817 dbl = 1;
4818 s = "ddivu";
4819 s2 = "mflo";
4820 goto do_divi;
4821 case M_DREM_3I:
4822 dbl = 1;
4823 s = "ddiv";
4824 s2 = "mfhi";
4825 goto do_divi;
4826 case M_DREMU_3I:
4827 dbl = 1;
4828 s = "ddivu";
4829 s2 = "mfhi";
4830 do_divi:
4831 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4832 {
4833 as_warn (_("Divide by zero."));
4834 if (mips_trap)
4835 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4836 else
4837 macro_build (NULL, "break", "c", 7);
4838 break;
4839 }
4840 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4841 {
4842 if (strcmp (s2, "mflo") == 0)
4843 move_register (dreg, sreg);
4844 else
4845 move_register (dreg, 0);
4846 break;
4847 }
4848 if (imm_expr.X_op == O_constant
4849 && imm_expr.X_add_number == -1
4850 && s[strlen (s) - 1] != 'u')
4851 {
4852 if (strcmp (s2, "mflo") == 0)
4853 {
4854 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4855 }
4856 else
4857 move_register (dreg, 0);
4858 break;
4859 }
4860
4861 used_at = 1;
4862 load_register (AT, &imm_expr, dbl);
4863 macro_build (NULL, s, "z,s,t", sreg, AT);
4864 macro_build (NULL, s2, "d", dreg);
4865 break;
4866
4867 case M_DIVU_3:
4868 s = "divu";
4869 s2 = "mflo";
4870 goto do_divu3;
4871 case M_REMU_3:
4872 s = "divu";
4873 s2 = "mfhi";
4874 goto do_divu3;
4875 case M_DDIVU_3:
4876 s = "ddivu";
4877 s2 = "mflo";
4878 goto do_divu3;
4879 case M_DREMU_3:
4880 s = "ddivu";
4881 s2 = "mfhi";
4882 do_divu3:
4883 mips_emit_delays (TRUE);
4884 ++mips_opts.noreorder;
4885 mips_any_noreorder = 1;
4886 if (mips_trap)
4887 {
4888 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4889 macro_build (NULL, s, "z,s,t", sreg, treg);
4890 /* We want to close the noreorder block as soon as possible, so
4891 that later insns are available for delay slot filling. */
4892 --mips_opts.noreorder;
4893 }
4894 else
4895 {
4896 expr1.X_add_number = 8;
4897 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4898 macro_build (NULL, s, "z,s,t", sreg, treg);
4899
4900 /* We want to close the noreorder block as soon as possible, so
4901 that later insns are available for delay slot filling. */
4902 --mips_opts.noreorder;
4903 macro_build (NULL, "break", "c", 7);
4904 }
4905 macro_build (NULL, s2, "d", dreg);
4906 break;
4907
4908 case M_DLCA_AB:
4909 dbl = 1;
4910 case M_LCA_AB:
4911 call = 1;
4912 goto do_la;
4913 case M_DLA_AB:
4914 dbl = 1;
4915 case M_LA_AB:
4916 do_la:
4917 /* Load the address of a symbol into a register. If breg is not
4918 zero, we then add a base register to it. */
4919
4920 if (dbl && HAVE_32BIT_GPRS)
4921 as_warn (_("dla used to load 32-bit register"));
4922
4923 if (! dbl && HAVE_64BIT_OBJECTS)
4924 as_warn (_("la used to load 64-bit address"));
4925
4926 if (offset_expr.X_op == O_constant
4927 && offset_expr.X_add_number >= -0x8000
4928 && offset_expr.X_add_number < 0x8000)
4929 {
4930 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4931 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4932 break;
4933 }
4934
4935 if (!mips_opts.noat && (treg == breg))
4936 {
4937 tempreg = AT;
4938 used_at = 1;
4939 }
4940 else
4941 {
4942 tempreg = treg;
4943 }
4944
4945 if (offset_expr.X_op != O_symbol
4946 && offset_expr.X_op != O_constant)
4947 {
4948 as_bad (_("expression too complex"));
4949 offset_expr.X_op = O_constant;
4950 }
4951
4952 if (offset_expr.X_op == O_constant)
4953 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4954 else if (mips_pic == NO_PIC)
4955 {
4956 /* If this is a reference to a GP relative symbol, we want
4957 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4958 Otherwise we want
4959 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4960 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4961 If we have a constant, we need two instructions anyhow,
4962 so we may as well always use the latter form.
4963
4964 With 64bit address space and a usable $at we want
4965 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4966 lui $at,<sym> (BFD_RELOC_HI16_S)
4967 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4968 daddiu $at,<sym> (BFD_RELOC_LO16)
4969 dsll32 $tempreg,0
4970 daddu $tempreg,$tempreg,$at
4971
4972 If $at is already in use, we use a path which is suboptimal
4973 on superscalar processors.
4974 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4975 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4976 dsll $tempreg,16
4977 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4978 dsll $tempreg,16
4979 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4980
4981 For GP relative symbols in 64bit address space we can use
4982 the same sequence as in 32bit address space. */
4983 if (HAVE_64BIT_SYMBOLS)
4984 {
4985 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4986 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4987 {
4988 relax_start (offset_expr.X_add_symbol);
4989 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4990 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4991 relax_switch ();
4992 }
4993
4994 if (used_at == 0 && !mips_opts.noat)
4995 {
4996 macro_build (&offset_expr, "lui", "t,u",
4997 tempreg, BFD_RELOC_MIPS_HIGHEST);
4998 macro_build (&offset_expr, "lui", "t,u",
4999 AT, BFD_RELOC_HI16_S);
5000 macro_build (&offset_expr, "daddiu", "t,r,j",
5001 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5002 macro_build (&offset_expr, "daddiu", "t,r,j",
5003 AT, AT, BFD_RELOC_LO16);
5004 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5005 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5006 used_at = 1;
5007 }
5008 else
5009 {
5010 macro_build (&offset_expr, "lui", "t,u",
5011 tempreg, BFD_RELOC_MIPS_HIGHEST);
5012 macro_build (&offset_expr, "daddiu", "t,r,j",
5013 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5014 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5015 macro_build (&offset_expr, "daddiu", "t,r,j",
5016 tempreg, tempreg, BFD_RELOC_HI16_S);
5017 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5018 macro_build (&offset_expr, "daddiu", "t,r,j",
5019 tempreg, tempreg, BFD_RELOC_LO16);
5020 }
5021
5022 if (mips_relax.sequence)
5023 relax_end ();
5024 }
5025 else
5026 {
5027 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5028 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5029 {
5030 relax_start (offset_expr.X_add_symbol);
5031 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5032 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5033 relax_switch ();
5034 }
5035 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5036 as_bad (_("offset too large"));
5037 macro_build_lui (&offset_expr, tempreg);
5038 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5039 tempreg, tempreg, BFD_RELOC_LO16);
5040 if (mips_relax.sequence)
5041 relax_end ();
5042 }
5043 }
5044 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5045 {
5046 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5047
5048 /* If this is a reference to an external symbol, and there
5049 is no constant, we want
5050 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5051 or for lca or if tempreg is PIC_CALL_REG
5052 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5053 For a local symbol, we want
5054 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5055 nop
5056 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5057
5058 If we have a small constant, and this is a reference to
5059 an external symbol, we want
5060 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5061 nop
5062 addiu $tempreg,$tempreg,<constant>
5063 For a local symbol, we want the same instruction
5064 sequence, but we output a BFD_RELOC_LO16 reloc on the
5065 addiu instruction.
5066
5067 If we have a large constant, and this is a reference to
5068 an external symbol, we want
5069 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5070 lui $at,<hiconstant>
5071 addiu $at,$at,<loconstant>
5072 addu $tempreg,$tempreg,$at
5073 For a local symbol, we want the same instruction
5074 sequence, but we output a BFD_RELOC_LO16 reloc on the
5075 addiu instruction.
5076 */
5077
5078 if (offset_expr.X_add_number == 0)
5079 {
5080 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5081 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5082
5083 relax_start (offset_expr.X_add_symbol);
5084 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5085 lw_reloc_type, mips_gp_register);
5086 if (breg != 0)
5087 {
5088 /* We're going to put in an addu instruction using
5089 tempreg, so we may as well insert the nop right
5090 now. */
5091 load_delay_nop ();
5092 }
5093 relax_switch ();
5094 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5095 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5096 load_delay_nop ();
5097 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5098 tempreg, tempreg, BFD_RELOC_LO16);
5099 relax_end ();
5100 /* FIXME: If breg == 0, and the next instruction uses
5101 $tempreg, then if this variant case is used an extra
5102 nop will be generated. */
5103 }
5104 else if (offset_expr.X_add_number >= -0x8000
5105 && offset_expr.X_add_number < 0x8000)
5106 {
5107 load_got_offset (tempreg, &offset_expr);
5108 load_delay_nop ();
5109 add_got_offset (tempreg, &offset_expr);
5110 }
5111 else
5112 {
5113 expr1.X_add_number = offset_expr.X_add_number;
5114 offset_expr.X_add_number =
5115 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5116 load_got_offset (tempreg, &offset_expr);
5117 offset_expr.X_add_number = expr1.X_add_number;
5118 /* If we are going to add in a base register, and the
5119 target register and the base register are the same,
5120 then we are using AT as a temporary register. Since
5121 we want to load the constant into AT, we add our
5122 current AT (from the global offset table) and the
5123 register into the register now, and pretend we were
5124 not using a base register. */
5125 if (breg == treg)
5126 {
5127 load_delay_nop ();
5128 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5129 treg, AT, breg);
5130 breg = 0;
5131 tempreg = treg;
5132 }
5133 add_got_offset_hilo (tempreg, &offset_expr, AT);
5134 used_at = 1;
5135 }
5136 }
5137 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5138 {
5139 int add_breg_early = 0;
5140
5141 /* If this is a reference to an external, and there is no
5142 constant, or local symbol (*), with or without a
5143 constant, we want
5144 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5145 or for lca or if tempreg is PIC_CALL_REG
5146 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5147
5148 If we have a small constant, and this is a reference to
5149 an external symbol, we want
5150 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5151 addiu $tempreg,$tempreg,<constant>
5152
5153 If we have a large constant, and this is a reference to
5154 an external symbol, we want
5155 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5156 lui $at,<hiconstant>
5157 addiu $at,$at,<loconstant>
5158 addu $tempreg,$tempreg,$at
5159
5160 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5161 local symbols, even though it introduces an additional
5162 instruction. */
5163
5164 if (offset_expr.X_add_number)
5165 {
5166 expr1.X_add_number = offset_expr.X_add_number;
5167 offset_expr.X_add_number = 0;
5168
5169 relax_start (offset_expr.X_add_symbol);
5170 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5171 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5172
5173 if (expr1.X_add_number >= -0x8000
5174 && expr1.X_add_number < 0x8000)
5175 {
5176 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5177 tempreg, tempreg, BFD_RELOC_LO16);
5178 }
5179 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5180 {
5181 int dreg;
5182
5183 /* If we are going to add in a base register, and the
5184 target register and the base register are the same,
5185 then we are using AT as a temporary register. Since
5186 we want to load the constant into AT, we add our
5187 current AT (from the global offset table) and the
5188 register into the register now, and pretend we were
5189 not using a base register. */
5190 if (breg != treg)
5191 dreg = tempreg;
5192 else
5193 {
5194 assert (tempreg == AT);
5195 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5196 treg, AT, breg);
5197 dreg = treg;
5198 add_breg_early = 1;
5199 }
5200
5201 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5202 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5203 dreg, dreg, AT);
5204
5205 used_at = 1;
5206 }
5207 else
5208 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5209
5210 relax_switch ();
5211 offset_expr.X_add_number = expr1.X_add_number;
5212
5213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5214 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5215 if (add_breg_early)
5216 {
5217 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5218 treg, tempreg, breg);
5219 breg = 0;
5220 tempreg = treg;
5221 }
5222 relax_end ();
5223 }
5224 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5225 {
5226 relax_start (offset_expr.X_add_symbol);
5227 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5228 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5229 relax_switch ();
5230 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5231 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5232 relax_end ();
5233 }
5234 else
5235 {
5236 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5237 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5238 }
5239 }
5240 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5241 {
5242 int gpdelay;
5243 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5244 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5245 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5246
5247 /* This is the large GOT case. If this is a reference to an
5248 external symbol, and there is no constant, we want
5249 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5250 addu $tempreg,$tempreg,$gp
5251 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5252 or for lca or if tempreg is PIC_CALL_REG
5253 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5254 addu $tempreg,$tempreg,$gp
5255 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5256 For a local symbol, we want
5257 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5258 nop
5259 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5260
5261 If we have a small constant, and this is a reference to
5262 an external symbol, we want
5263 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5264 addu $tempreg,$tempreg,$gp
5265 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5266 nop
5267 addiu $tempreg,$tempreg,<constant>
5268 For a local symbol, we want
5269 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5270 nop
5271 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5272
5273 If we have a large constant, and this is a reference to
5274 an external symbol, we want
5275 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5276 addu $tempreg,$tempreg,$gp
5277 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5278 lui $at,<hiconstant>
5279 addiu $at,$at,<loconstant>
5280 addu $tempreg,$tempreg,$at
5281 For a local symbol, we want
5282 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5283 lui $at,<hiconstant>
5284 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5285 addu $tempreg,$tempreg,$at
5286 */
5287
5288 expr1.X_add_number = offset_expr.X_add_number;
5289 offset_expr.X_add_number = 0;
5290 relax_start (offset_expr.X_add_symbol);
5291 gpdelay = reg_needs_delay (mips_gp_register);
5292 if (expr1.X_add_number == 0 && breg == 0
5293 && (call || tempreg == PIC_CALL_REG))
5294 {
5295 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5296 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5297 }
5298 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5299 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5300 tempreg, tempreg, mips_gp_register);
5301 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5302 tempreg, lw_reloc_type, tempreg);
5303 if (expr1.X_add_number == 0)
5304 {
5305 if (breg != 0)
5306 {
5307 /* We're going to put in an addu instruction using
5308 tempreg, so we may as well insert the nop right
5309 now. */
5310 load_delay_nop ();
5311 }
5312 }
5313 else if (expr1.X_add_number >= -0x8000
5314 && expr1.X_add_number < 0x8000)
5315 {
5316 load_delay_nop ();
5317 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5318 tempreg, tempreg, BFD_RELOC_LO16);
5319 }
5320 else
5321 {
5322 int dreg;
5323
5324 /* If we are going to add in a base register, and the
5325 target register and the base register are the same,
5326 then we are using AT as a temporary register. Since
5327 we want to load the constant into AT, we add our
5328 current AT (from the global offset table) and the
5329 register into the register now, and pretend we were
5330 not using a base register. */
5331 if (breg != treg)
5332 dreg = tempreg;
5333 else
5334 {
5335 assert (tempreg == AT);
5336 load_delay_nop ();
5337 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5338 treg, AT, breg);
5339 dreg = treg;
5340 }
5341
5342 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5343 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5344
5345 used_at = 1;
5346 }
5347 offset_expr.X_add_number =
5348 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5349 relax_switch ();
5350
5351 if (gpdelay)
5352 {
5353 /* This is needed because this instruction uses $gp, but
5354 the first instruction on the main stream does not. */
5355 macro_build (NULL, "nop", "");
5356 }
5357
5358 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5359 local_reloc_type, mips_gp_register);
5360 if (expr1.X_add_number >= -0x8000
5361 && expr1.X_add_number < 0x8000)
5362 {
5363 load_delay_nop ();
5364 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5365 tempreg, tempreg, BFD_RELOC_LO16);
5366 /* FIXME: If add_number is 0, and there was no base
5367 register, the external symbol case ended with a load,
5368 so if the symbol turns out to not be external, and
5369 the next instruction uses tempreg, an unnecessary nop
5370 will be inserted. */
5371 }
5372 else
5373 {
5374 if (breg == treg)
5375 {
5376 /* We must add in the base register now, as in the
5377 external symbol case. */
5378 assert (tempreg == AT);
5379 load_delay_nop ();
5380 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5381 treg, AT, breg);
5382 tempreg = treg;
5383 /* We set breg to 0 because we have arranged to add
5384 it in in both cases. */
5385 breg = 0;
5386 }
5387
5388 macro_build_lui (&expr1, AT);
5389 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5390 AT, AT, BFD_RELOC_LO16);
5391 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5392 tempreg, tempreg, AT);
5393 used_at = 1;
5394 }
5395 relax_end ();
5396 }
5397 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5398 {
5399 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5400 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5401 int add_breg_early = 0;
5402
5403 /* This is the large GOT case. If this is a reference to an
5404 external symbol, and there is no constant, we want
5405 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5406 add $tempreg,$tempreg,$gp
5407 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5408 or for lca or if tempreg is PIC_CALL_REG
5409 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5410 add $tempreg,$tempreg,$gp
5411 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5412
5413 If we have a small constant, and this is a reference to
5414 an external symbol, we want
5415 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5416 add $tempreg,$tempreg,$gp
5417 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5418 addi $tempreg,$tempreg,<constant>
5419
5420 If we have a large constant, and this is a reference to
5421 an external symbol, we want
5422 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5423 addu $tempreg,$tempreg,$gp
5424 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5425 lui $at,<hiconstant>
5426 addi $at,$at,<loconstant>
5427 add $tempreg,$tempreg,$at
5428
5429 If we have NewABI, and we know it's a local symbol, we want
5430 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5431 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5432 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5433
5434 relax_start (offset_expr.X_add_symbol);
5435
5436 expr1.X_add_number = offset_expr.X_add_number;
5437 offset_expr.X_add_number = 0;
5438
5439 if (expr1.X_add_number == 0 && breg == 0
5440 && (call || tempreg == PIC_CALL_REG))
5441 {
5442 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5443 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5444 }
5445 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5446 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5447 tempreg, tempreg, mips_gp_register);
5448 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5449 tempreg, lw_reloc_type, tempreg);
5450
5451 if (expr1.X_add_number == 0)
5452 ;
5453 else if (expr1.X_add_number >= -0x8000
5454 && expr1.X_add_number < 0x8000)
5455 {
5456 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5457 tempreg, tempreg, BFD_RELOC_LO16);
5458 }
5459 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5460 {
5461 int dreg;
5462
5463 /* If we are going to add in a base register, and the
5464 target register and the base register are the same,
5465 then we are using AT as a temporary register. Since
5466 we want to load the constant into AT, we add our
5467 current AT (from the global offset table) and the
5468 register into the register now, and pretend we were
5469 not using a base register. */
5470 if (breg != treg)
5471 dreg = tempreg;
5472 else
5473 {
5474 assert (tempreg == AT);
5475 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5476 treg, AT, breg);
5477 dreg = treg;
5478 add_breg_early = 1;
5479 }
5480
5481 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5482 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5483
5484 used_at = 1;
5485 }
5486 else
5487 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5488
5489 relax_switch ();
5490 offset_expr.X_add_number = expr1.X_add_number;
5491 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5492 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5493 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5494 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5495 if (add_breg_early)
5496 {
5497 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5498 treg, tempreg, breg);
5499 breg = 0;
5500 tempreg = treg;
5501 }
5502 relax_end ();
5503 }
5504 else
5505 abort ();
5506
5507 if (breg != 0)
5508 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5509 break;
5510
5511 case M_J_A:
5512 /* The j instruction may not be used in PIC code, since it
5513 requires an absolute address. We convert it to a b
5514 instruction. */
5515 if (mips_pic == NO_PIC)
5516 macro_build (&offset_expr, "j", "a");
5517 else
5518 macro_build (&offset_expr, "b", "p");
5519 break;
5520
5521 /* The jal instructions must be handled as macros because when
5522 generating PIC code they expand to multi-instruction
5523 sequences. Normally they are simple instructions. */
5524 case M_JAL_1:
5525 dreg = RA;
5526 /* Fall through. */
5527 case M_JAL_2:
5528 if (mips_pic == NO_PIC)
5529 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5530 else if (mips_pic == SVR4_PIC)
5531 {
5532 if (sreg != PIC_CALL_REG)
5533 as_warn (_("MIPS PIC call to register other than $25"));
5534
5535 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5536 if (! HAVE_NEWABI)
5537 {
5538 if (mips_cprestore_offset < 0)
5539 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5540 else
5541 {
5542 if (! mips_frame_reg_valid)
5543 {
5544 as_warn (_("No .frame pseudo-op used in PIC code"));
5545 /* Quiet this warning. */
5546 mips_frame_reg_valid = 1;
5547 }
5548 if (! mips_cprestore_valid)
5549 {
5550 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5551 /* Quiet this warning. */
5552 mips_cprestore_valid = 1;
5553 }
5554 expr1.X_add_number = mips_cprestore_offset;
5555 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5556 mips_gp_register,
5557 mips_frame_reg,
5558 HAVE_64BIT_ADDRESSES);
5559 }
5560 }
5561 }
5562 else
5563 abort ();
5564
5565 break;
5566
5567 case M_JAL_A:
5568 if (mips_pic == NO_PIC)
5569 macro_build (&offset_expr, "jal", "a");
5570 else if (mips_pic == SVR4_PIC)
5571 {
5572 /* If this is a reference to an external symbol, and we are
5573 using a small GOT, we want
5574 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5575 nop
5576 jalr $ra,$25
5577 nop
5578 lw $gp,cprestore($sp)
5579 The cprestore value is set using the .cprestore
5580 pseudo-op. If we are using a big GOT, we want
5581 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5582 addu $25,$25,$gp
5583 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5584 nop
5585 jalr $ra,$25
5586 nop
5587 lw $gp,cprestore($sp)
5588 If the symbol is not external, we want
5589 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5590 nop
5591 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5592 jalr $ra,$25
5593 nop
5594 lw $gp,cprestore($sp)
5595
5596 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5597 sequences above, minus nops, unless the symbol is local,
5598 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5599 GOT_DISP. */
5600 if (HAVE_NEWABI)
5601 {
5602 if (! mips_big_got)
5603 {
5604 relax_start (offset_expr.X_add_symbol);
5605 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5606 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5607 mips_gp_register);
5608 relax_switch ();
5609 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5610 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5611 mips_gp_register);
5612 relax_end ();
5613 }
5614 else
5615 {
5616 relax_start (offset_expr.X_add_symbol);
5617 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5618 BFD_RELOC_MIPS_CALL_HI16);
5619 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5620 PIC_CALL_REG, mips_gp_register);
5621 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5622 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5623 PIC_CALL_REG);
5624 relax_switch ();
5625 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5626 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5627 mips_gp_register);
5628 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5629 PIC_CALL_REG, PIC_CALL_REG,
5630 BFD_RELOC_MIPS_GOT_OFST);
5631 relax_end ();
5632 }
5633
5634 macro_build_jalr (&offset_expr);
5635 }
5636 else
5637 {
5638 relax_start (offset_expr.X_add_symbol);
5639 if (! mips_big_got)
5640 {
5641 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5642 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5643 mips_gp_register);
5644 load_delay_nop ();
5645 relax_switch ();
5646 }
5647 else
5648 {
5649 int gpdelay;
5650
5651 gpdelay = reg_needs_delay (mips_gp_register);
5652 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5653 BFD_RELOC_MIPS_CALL_HI16);
5654 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5655 PIC_CALL_REG, mips_gp_register);
5656 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5657 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5658 PIC_CALL_REG);
5659 load_delay_nop ();
5660 relax_switch ();
5661 if (gpdelay)
5662 macro_build (NULL, "nop", "");
5663 }
5664 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5665 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5666 mips_gp_register);
5667 load_delay_nop ();
5668 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5669 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5670 relax_end ();
5671 macro_build_jalr (&offset_expr);
5672
5673 if (mips_cprestore_offset < 0)
5674 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5675 else
5676 {
5677 if (! mips_frame_reg_valid)
5678 {
5679 as_warn (_("No .frame pseudo-op used in PIC code"));
5680 /* Quiet this warning. */
5681 mips_frame_reg_valid = 1;
5682 }
5683 if (! mips_cprestore_valid)
5684 {
5685 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5686 /* Quiet this warning. */
5687 mips_cprestore_valid = 1;
5688 }
5689 if (mips_opts.noreorder)
5690 macro_build (NULL, "nop", "");
5691 expr1.X_add_number = mips_cprestore_offset;
5692 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5693 mips_gp_register,
5694 mips_frame_reg,
5695 HAVE_64BIT_ADDRESSES);
5696 }
5697 }
5698 }
5699 else
5700 abort ();
5701
5702 break;
5703
5704 case M_LB_AB:
5705 s = "lb";
5706 goto ld;
5707 case M_LBU_AB:
5708 s = "lbu";
5709 goto ld;
5710 case M_LH_AB:
5711 s = "lh";
5712 goto ld;
5713 case M_LHU_AB:
5714 s = "lhu";
5715 goto ld;
5716 case M_LW_AB:
5717 s = "lw";
5718 goto ld;
5719 case M_LWC0_AB:
5720 s = "lwc0";
5721 /* Itbl support may require additional care here. */
5722 coproc = 1;
5723 goto ld;
5724 case M_LWC1_AB:
5725 s = "lwc1";
5726 /* Itbl support may require additional care here. */
5727 coproc = 1;
5728 goto ld;
5729 case M_LWC2_AB:
5730 s = "lwc2";
5731 /* Itbl support may require additional care here. */
5732 coproc = 1;
5733 goto ld;
5734 case M_LWC3_AB:
5735 s = "lwc3";
5736 /* Itbl support may require additional care here. */
5737 coproc = 1;
5738 goto ld;
5739 case M_LWL_AB:
5740 s = "lwl";
5741 lr = 1;
5742 goto ld;
5743 case M_LWR_AB:
5744 s = "lwr";
5745 lr = 1;
5746 goto ld;
5747 case M_LDC1_AB:
5748 if (mips_opts.arch == CPU_R4650)
5749 {
5750 as_bad (_("opcode not supported on this processor"));
5751 break;
5752 }
5753 s = "ldc1";
5754 /* Itbl support may require additional care here. */
5755 coproc = 1;
5756 goto ld;
5757 case M_LDC2_AB:
5758 s = "ldc2";
5759 /* Itbl support may require additional care here. */
5760 coproc = 1;
5761 goto ld;
5762 case M_LDC3_AB:
5763 s = "ldc3";
5764 /* Itbl support may require additional care here. */
5765 coproc = 1;
5766 goto ld;
5767 case M_LDL_AB:
5768 s = "ldl";
5769 lr = 1;
5770 goto ld;
5771 case M_LDR_AB:
5772 s = "ldr";
5773 lr = 1;
5774 goto ld;
5775 case M_LL_AB:
5776 s = "ll";
5777 goto ld;
5778 case M_LLD_AB:
5779 s = "lld";
5780 goto ld;
5781 case M_LWU_AB:
5782 s = "lwu";
5783 ld:
5784 if (breg == treg || coproc || lr)
5785 {
5786 tempreg = AT;
5787 used_at = 1;
5788 }
5789 else
5790 {
5791 tempreg = treg;
5792 }
5793 goto ld_st;
5794 case M_SB_AB:
5795 s = "sb";
5796 goto st;
5797 case M_SH_AB:
5798 s = "sh";
5799 goto st;
5800 case M_SW_AB:
5801 s = "sw";
5802 goto st;
5803 case M_SWC0_AB:
5804 s = "swc0";
5805 /* Itbl support may require additional care here. */
5806 coproc = 1;
5807 goto st;
5808 case M_SWC1_AB:
5809 s = "swc1";
5810 /* Itbl support may require additional care here. */
5811 coproc = 1;
5812 goto st;
5813 case M_SWC2_AB:
5814 s = "swc2";
5815 /* Itbl support may require additional care here. */
5816 coproc = 1;
5817 goto st;
5818 case M_SWC3_AB:
5819 s = "swc3";
5820 /* Itbl support may require additional care here. */
5821 coproc = 1;
5822 goto st;
5823 case M_SWL_AB:
5824 s = "swl";
5825 goto st;
5826 case M_SWR_AB:
5827 s = "swr";
5828 goto st;
5829 case M_SC_AB:
5830 s = "sc";
5831 goto st;
5832 case M_SCD_AB:
5833 s = "scd";
5834 goto st;
5835 case M_SDC1_AB:
5836 if (mips_opts.arch == CPU_R4650)
5837 {
5838 as_bad (_("opcode not supported on this processor"));
5839 break;
5840 }
5841 s = "sdc1";
5842 coproc = 1;
5843 /* Itbl support may require additional care here. */
5844 goto st;
5845 case M_SDC2_AB:
5846 s = "sdc2";
5847 /* Itbl support may require additional care here. */
5848 coproc = 1;
5849 goto st;
5850 case M_SDC3_AB:
5851 s = "sdc3";
5852 /* Itbl support may require additional care here. */
5853 coproc = 1;
5854 goto st;
5855 case M_SDL_AB:
5856 s = "sdl";
5857 goto st;
5858 case M_SDR_AB:
5859 s = "sdr";
5860 st:
5861 tempreg = AT;
5862 used_at = 1;
5863 ld_st:
5864 /* Itbl support may require additional care here. */
5865 if (mask == M_LWC1_AB
5866 || mask == M_SWC1_AB
5867 || mask == M_LDC1_AB
5868 || mask == M_SDC1_AB
5869 || mask == M_L_DAB
5870 || mask == M_S_DAB)
5871 fmt = "T,o(b)";
5872 else if (coproc)
5873 fmt = "E,o(b)";
5874 else
5875 fmt = "t,o(b)";
5876
5877 if (offset_expr.X_op != O_constant
5878 && offset_expr.X_op != O_symbol)
5879 {
5880 as_bad (_("expression too complex"));
5881 offset_expr.X_op = O_constant;
5882 }
5883
5884 /* A constant expression in PIC code can be handled just as it
5885 is in non PIC code. */
5886 if (offset_expr.X_op == O_constant)
5887 {
5888 if (HAVE_32BIT_ADDRESSES
5889 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5890 as_bad (_("constant too large"));
5891
5892 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5893 & ~(bfd_vma) 0xffff);
5894 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5895 if (breg != 0)
5896 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5897 tempreg, tempreg, breg);
5898 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5899 }
5900 else if (mips_pic == NO_PIC)
5901 {
5902 /* If this is a reference to a GP relative symbol, and there
5903 is no base register, we want
5904 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5905 Otherwise, if there is no base register, we want
5906 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5907 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5908 If we have a constant, we need two instructions anyhow,
5909 so we always use the latter form.
5910
5911 If we have a base register, and this is a reference to a
5912 GP relative symbol, we want
5913 addu $tempreg,$breg,$gp
5914 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5915 Otherwise we want
5916 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5917 addu $tempreg,$tempreg,$breg
5918 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5919 With a constant we always use the latter case.
5920
5921 With 64bit address space and no base register and $at usable,
5922 we want
5923 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5924 lui $at,<sym> (BFD_RELOC_HI16_S)
5925 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5926 dsll32 $tempreg,0
5927 daddu $tempreg,$at
5928 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5929 If we have a base register, we want
5930 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5931 lui $at,<sym> (BFD_RELOC_HI16_S)
5932 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5933 daddu $at,$breg
5934 dsll32 $tempreg,0
5935 daddu $tempreg,$at
5936 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5937
5938 Without $at we can't generate the optimal path for superscalar
5939 processors here since this would require two temporary registers.
5940 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5941 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5942 dsll $tempreg,16
5943 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5944 dsll $tempreg,16
5945 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5946 If we have a base register, we want
5947 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5948 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5949 dsll $tempreg,16
5950 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5951 dsll $tempreg,16
5952 daddu $tempreg,$tempreg,$breg
5953 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5954
5955 For GP relative symbols in 64bit address space we can use
5956 the same sequence as in 32bit address space. */
5957 if (HAVE_64BIT_SYMBOLS)
5958 {
5959 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5960 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5961 {
5962 relax_start (offset_expr.X_add_symbol);
5963 if (breg == 0)
5964 {
5965 macro_build (&offset_expr, s, fmt, treg,
5966 BFD_RELOC_GPREL16, mips_gp_register);
5967 }
5968 else
5969 {
5970 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5971 tempreg, breg, mips_gp_register);
5972 macro_build (&offset_expr, s, fmt, treg,
5973 BFD_RELOC_GPREL16, tempreg);
5974 }
5975 relax_switch ();
5976 }
5977
5978 if (used_at == 0 && !mips_opts.noat)
5979 {
5980 macro_build (&offset_expr, "lui", "t,u", tempreg,
5981 BFD_RELOC_MIPS_HIGHEST);
5982 macro_build (&offset_expr, "lui", "t,u", AT,
5983 BFD_RELOC_HI16_S);
5984 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5985 tempreg, BFD_RELOC_MIPS_HIGHER);
5986 if (breg != 0)
5987 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5988 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5989 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5990 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5991 tempreg);
5992 used_at = 1;
5993 }
5994 else
5995 {
5996 macro_build (&offset_expr, "lui", "t,u", tempreg,
5997 BFD_RELOC_MIPS_HIGHEST);
5998 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5999 tempreg, BFD_RELOC_MIPS_HIGHER);
6000 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6001 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6002 tempreg, BFD_RELOC_HI16_S);
6003 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6004 if (breg != 0)
6005 macro_build (NULL, "daddu", "d,v,t",
6006 tempreg, tempreg, breg);
6007 macro_build (&offset_expr, s, fmt, treg,
6008 BFD_RELOC_LO16, tempreg);
6009 }
6010
6011 if (mips_relax.sequence)
6012 relax_end ();
6013 break;
6014 }
6015
6016 if (breg == 0)
6017 {
6018 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6019 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6020 {
6021 relax_start (offset_expr.X_add_symbol);
6022 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6023 mips_gp_register);
6024 relax_switch ();
6025 }
6026 macro_build_lui (&offset_expr, tempreg);
6027 macro_build (&offset_expr, s, fmt, treg,
6028 BFD_RELOC_LO16, tempreg);
6029 if (mips_relax.sequence)
6030 relax_end ();
6031 }
6032 else
6033 {
6034 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6035 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6036 {
6037 relax_start (offset_expr.X_add_symbol);
6038 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6039 tempreg, breg, mips_gp_register);
6040 macro_build (&offset_expr, s, fmt, treg,
6041 BFD_RELOC_GPREL16, tempreg);
6042 relax_switch ();
6043 }
6044 macro_build_lui (&offset_expr, tempreg);
6045 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6046 tempreg, tempreg, breg);
6047 macro_build (&offset_expr, s, fmt, treg,
6048 BFD_RELOC_LO16, tempreg);
6049 if (mips_relax.sequence)
6050 relax_end ();
6051 }
6052 }
6053 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6054 {
6055 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6056
6057 /* If this is a reference to an external symbol, we want
6058 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6059 nop
6060 <op> $treg,0($tempreg)
6061 Otherwise we want
6062 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6063 nop
6064 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6065 <op> $treg,0($tempreg)
6066
6067 For NewABI, we want
6068 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6069 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6070
6071 If there is a base register, we add it to $tempreg before
6072 the <op>. If there is a constant, we stick it in the
6073 <op> instruction. We don't handle constants larger than
6074 16 bits, because we have no way to load the upper 16 bits
6075 (actually, we could handle them for the subset of cases
6076 in which we are not using $at). */
6077 assert (offset_expr.X_op == O_symbol);
6078 if (HAVE_NEWABI)
6079 {
6080 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6081 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6082 if (breg != 0)
6083 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6084 tempreg, tempreg, breg);
6085 macro_build (&offset_expr, s, fmt, treg,
6086 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6087 break;
6088 }
6089 expr1.X_add_number = offset_expr.X_add_number;
6090 offset_expr.X_add_number = 0;
6091 if (expr1.X_add_number < -0x8000
6092 || expr1.X_add_number >= 0x8000)
6093 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6094 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6095 lw_reloc_type, mips_gp_register);
6096 load_delay_nop ();
6097 relax_start (offset_expr.X_add_symbol);
6098 relax_switch ();
6099 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6100 tempreg, BFD_RELOC_LO16);
6101 relax_end ();
6102 if (breg != 0)
6103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6104 tempreg, tempreg, breg);
6105 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6106 }
6107 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6108 {
6109 int gpdelay;
6110
6111 /* If this is a reference to an external symbol, we want
6112 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6113 addu $tempreg,$tempreg,$gp
6114 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6115 <op> $treg,0($tempreg)
6116 Otherwise we want
6117 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6118 nop
6119 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6120 <op> $treg,0($tempreg)
6121 If there is a base register, we add it to $tempreg before
6122 the <op>. If there is a constant, we stick it in the
6123 <op> instruction. We don't handle constants larger than
6124 16 bits, because we have no way to load the upper 16 bits
6125 (actually, we could handle them for the subset of cases
6126 in which we are not using $at). */
6127 assert (offset_expr.X_op == O_symbol);
6128 expr1.X_add_number = offset_expr.X_add_number;
6129 offset_expr.X_add_number = 0;
6130 if (expr1.X_add_number < -0x8000
6131 || expr1.X_add_number >= 0x8000)
6132 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6133 gpdelay = reg_needs_delay (mips_gp_register);
6134 relax_start (offset_expr.X_add_symbol);
6135 macro_build (&offset_expr, "lui", "t,u", tempreg,
6136 BFD_RELOC_MIPS_GOT_HI16);
6137 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6138 mips_gp_register);
6139 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6140 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6141 relax_switch ();
6142 if (gpdelay)
6143 macro_build (NULL, "nop", "");
6144 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6145 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6146 load_delay_nop ();
6147 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6148 tempreg, BFD_RELOC_LO16);
6149 relax_end ();
6150
6151 if (breg != 0)
6152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6153 tempreg, tempreg, breg);
6154 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6155 }
6156 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6157 {
6158 /* If this is a reference to an external symbol, we want
6159 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6160 add $tempreg,$tempreg,$gp
6161 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6162 <op> $treg,<ofst>($tempreg)
6163 Otherwise, for local symbols, we want:
6164 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6165 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6166 assert (offset_expr.X_op == O_symbol);
6167 expr1.X_add_number = offset_expr.X_add_number;
6168 offset_expr.X_add_number = 0;
6169 if (expr1.X_add_number < -0x8000
6170 || expr1.X_add_number >= 0x8000)
6171 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6172 relax_start (offset_expr.X_add_symbol);
6173 macro_build (&offset_expr, "lui", "t,u", tempreg,
6174 BFD_RELOC_MIPS_GOT_HI16);
6175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6176 mips_gp_register);
6177 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6178 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6179 if (breg != 0)
6180 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6181 tempreg, tempreg, breg);
6182 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6183
6184 relax_switch ();
6185 offset_expr.X_add_number = expr1.X_add_number;
6186 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6187 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6188 if (breg != 0)
6189 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6190 tempreg, tempreg, breg);
6191 macro_build (&offset_expr, s, fmt, treg,
6192 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6193 relax_end ();
6194 }
6195 else
6196 abort ();
6197
6198 break;
6199
6200 case M_LI:
6201 case M_LI_S:
6202 load_register (treg, &imm_expr, 0);
6203 break;
6204
6205 case M_DLI:
6206 load_register (treg, &imm_expr, 1);
6207 break;
6208
6209 case M_LI_SS:
6210 if (imm_expr.X_op == O_constant)
6211 {
6212 used_at = 1;
6213 load_register (AT, &imm_expr, 0);
6214 macro_build (NULL, "mtc1", "t,G", AT, treg);
6215 break;
6216 }
6217 else
6218 {
6219 assert (offset_expr.X_op == O_symbol
6220 && strcmp (segment_name (S_GET_SEGMENT
6221 (offset_expr.X_add_symbol)),
6222 ".lit4") == 0
6223 && offset_expr.X_add_number == 0);
6224 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6225 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6226 break;
6227 }
6228
6229 case M_LI_D:
6230 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6231 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6232 order 32 bits of the value and the low order 32 bits are either
6233 zero or in OFFSET_EXPR. */
6234 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6235 {
6236 if (HAVE_64BIT_GPRS)
6237 load_register (treg, &imm_expr, 1);
6238 else
6239 {
6240 int hreg, lreg;
6241
6242 if (target_big_endian)
6243 {
6244 hreg = treg;
6245 lreg = treg + 1;
6246 }
6247 else
6248 {
6249 hreg = treg + 1;
6250 lreg = treg;
6251 }
6252
6253 if (hreg <= 31)
6254 load_register (hreg, &imm_expr, 0);
6255 if (lreg <= 31)
6256 {
6257 if (offset_expr.X_op == O_absent)
6258 move_register (lreg, 0);
6259 else
6260 {
6261 assert (offset_expr.X_op == O_constant);
6262 load_register (lreg, &offset_expr, 0);
6263 }
6264 }
6265 }
6266 break;
6267 }
6268
6269 /* We know that sym is in the .rdata section. First we get the
6270 upper 16 bits of the address. */
6271 if (mips_pic == NO_PIC)
6272 {
6273 macro_build_lui (&offset_expr, AT);
6274 used_at = 1;
6275 }
6276 else if (mips_pic == SVR4_PIC)
6277 {
6278 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6279 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6280 used_at = 1;
6281 }
6282 else
6283 abort ();
6284
6285 /* Now we load the register(s). */
6286 if (HAVE_64BIT_GPRS)
6287 {
6288 used_at = 1;
6289 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6290 }
6291 else
6292 {
6293 used_at = 1;
6294 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6295 if (treg != RA)
6296 {
6297 /* FIXME: How in the world do we deal with the possible
6298 overflow here? */
6299 offset_expr.X_add_number += 4;
6300 macro_build (&offset_expr, "lw", "t,o(b)",
6301 treg + 1, BFD_RELOC_LO16, AT);
6302 }
6303 }
6304 break;
6305
6306 case M_LI_DD:
6307 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6308 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6309 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6310 the value and the low order 32 bits are either zero or in
6311 OFFSET_EXPR. */
6312 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6313 {
6314 used_at = 1;
6315 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6316 if (HAVE_64BIT_FPRS)
6317 {
6318 assert (HAVE_64BIT_GPRS);
6319 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6320 }
6321 else
6322 {
6323 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6324 if (offset_expr.X_op == O_absent)
6325 macro_build (NULL, "mtc1", "t,G", 0, treg);
6326 else
6327 {
6328 assert (offset_expr.X_op == O_constant);
6329 load_register (AT, &offset_expr, 0);
6330 macro_build (NULL, "mtc1", "t,G", AT, treg);
6331 }
6332 }
6333 break;
6334 }
6335
6336 assert (offset_expr.X_op == O_symbol
6337 && offset_expr.X_add_number == 0);
6338 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6339 if (strcmp (s, ".lit8") == 0)
6340 {
6341 if (mips_opts.isa != ISA_MIPS1)
6342 {
6343 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6344 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6345 break;
6346 }
6347 breg = mips_gp_register;
6348 r = BFD_RELOC_MIPS_LITERAL;
6349 goto dob;
6350 }
6351 else
6352 {
6353 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6354 used_at = 1;
6355 if (mips_pic == SVR4_PIC)
6356 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6357 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6358 else
6359 {
6360 /* FIXME: This won't work for a 64 bit address. */
6361 macro_build_lui (&offset_expr, AT);
6362 }
6363
6364 if (mips_opts.isa != ISA_MIPS1)
6365 {
6366 macro_build (&offset_expr, "ldc1", "T,o(b)",
6367 treg, BFD_RELOC_LO16, AT);
6368 break;
6369 }
6370 breg = AT;
6371 r = BFD_RELOC_LO16;
6372 goto dob;
6373 }
6374
6375 case M_L_DOB:
6376 if (mips_opts.arch == CPU_R4650)
6377 {
6378 as_bad (_("opcode not supported on this processor"));
6379 break;
6380 }
6381 /* Even on a big endian machine $fn comes before $fn+1. We have
6382 to adjust when loading from memory. */
6383 r = BFD_RELOC_LO16;
6384 dob:
6385 assert (mips_opts.isa == ISA_MIPS1);
6386 macro_build (&offset_expr, "lwc1", "T,o(b)",
6387 target_big_endian ? treg + 1 : treg, r, breg);
6388 /* FIXME: A possible overflow which I don't know how to deal
6389 with. */
6390 offset_expr.X_add_number += 4;
6391 macro_build (&offset_expr, "lwc1", "T,o(b)",
6392 target_big_endian ? treg : treg + 1, r, breg);
6393 break;
6394
6395 case M_L_DAB:
6396 /*
6397 * The MIPS assembler seems to check for X_add_number not
6398 * being double aligned and generating:
6399 * lui at,%hi(foo+1)
6400 * addu at,at,v1
6401 * addiu at,at,%lo(foo+1)
6402 * lwc1 f2,0(at)
6403 * lwc1 f3,4(at)
6404 * But, the resulting address is the same after relocation so why
6405 * generate the extra instruction?
6406 */
6407 if (mips_opts.arch == CPU_R4650)
6408 {
6409 as_bad (_("opcode not supported on this processor"));
6410 break;
6411 }
6412 /* Itbl support may require additional care here. */
6413 coproc = 1;
6414 if (mips_opts.isa != ISA_MIPS1)
6415 {
6416 s = "ldc1";
6417 goto ld;
6418 }
6419
6420 s = "lwc1";
6421 fmt = "T,o(b)";
6422 goto ldd_std;
6423
6424 case M_S_DAB:
6425 if (mips_opts.arch == CPU_R4650)
6426 {
6427 as_bad (_("opcode not supported on this processor"));
6428 break;
6429 }
6430
6431 if (mips_opts.isa != ISA_MIPS1)
6432 {
6433 s = "sdc1";
6434 goto st;
6435 }
6436
6437 s = "swc1";
6438 fmt = "T,o(b)";
6439 /* Itbl support may require additional care here. */
6440 coproc = 1;
6441 goto ldd_std;
6442
6443 case M_LD_AB:
6444 if (HAVE_64BIT_GPRS)
6445 {
6446 s = "ld";
6447 goto ld;
6448 }
6449
6450 s = "lw";
6451 fmt = "t,o(b)";
6452 goto ldd_std;
6453
6454 case M_SD_AB:
6455 if (HAVE_64BIT_GPRS)
6456 {
6457 s = "sd";
6458 goto st;
6459 }
6460
6461 s = "sw";
6462 fmt = "t,o(b)";
6463
6464 ldd_std:
6465 if (offset_expr.X_op != O_symbol
6466 && offset_expr.X_op != O_constant)
6467 {
6468 as_bad (_("expression too complex"));
6469 offset_expr.X_op = O_constant;
6470 }
6471
6472 /* Even on a big endian machine $fn comes before $fn+1. We have
6473 to adjust when loading from memory. We set coproc if we must
6474 load $fn+1 first. */
6475 /* Itbl support may require additional care here. */
6476 if (! target_big_endian)
6477 coproc = 0;
6478
6479 if (mips_pic == NO_PIC
6480 || offset_expr.X_op == O_constant)
6481 {
6482 /* If this is a reference to a GP relative symbol, we want
6483 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6484 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6485 If we have a base register, we use this
6486 addu $at,$breg,$gp
6487 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6488 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6489 If this is not a GP relative symbol, we want
6490 lui $at,<sym> (BFD_RELOC_HI16_S)
6491 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6492 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6493 If there is a base register, we add it to $at after the
6494 lui instruction. If there is a constant, we always use
6495 the last case. */
6496 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6497 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6498 {
6499 relax_start (offset_expr.X_add_symbol);
6500 if (breg == 0)
6501 {
6502 tempreg = mips_gp_register;
6503 }
6504 else
6505 {
6506 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6507 AT, breg, mips_gp_register);
6508 tempreg = AT;
6509 used_at = 1;
6510 }
6511
6512 /* Itbl support may require additional care here. */
6513 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6514 BFD_RELOC_GPREL16, tempreg);
6515 offset_expr.X_add_number += 4;
6516
6517 /* Set mips_optimize to 2 to avoid inserting an
6518 undesired nop. */
6519 hold_mips_optimize = mips_optimize;
6520 mips_optimize = 2;
6521 /* Itbl support may require additional care here. */
6522 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6523 BFD_RELOC_GPREL16, tempreg);
6524 mips_optimize = hold_mips_optimize;
6525
6526 relax_switch ();
6527
6528 /* We just generated two relocs. When tc_gen_reloc
6529 handles this case, it will skip the first reloc and
6530 handle the second. The second reloc already has an
6531 extra addend of 4, which we added above. We must
6532 subtract it out, and then subtract another 4 to make
6533 the first reloc come out right. The second reloc
6534 will come out right because we are going to add 4 to
6535 offset_expr when we build its instruction below.
6536
6537 If we have a symbol, then we don't want to include
6538 the offset, because it will wind up being included
6539 when we generate the reloc. */
6540
6541 if (offset_expr.X_op == O_constant)
6542 offset_expr.X_add_number -= 8;
6543 else
6544 {
6545 offset_expr.X_add_number = -4;
6546 offset_expr.X_op = O_constant;
6547 }
6548 }
6549 used_at = 1;
6550 macro_build_lui (&offset_expr, AT);
6551 if (breg != 0)
6552 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6553 /* Itbl support may require additional care here. */
6554 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6555 BFD_RELOC_LO16, AT);
6556 /* FIXME: How do we handle overflow here? */
6557 offset_expr.X_add_number += 4;
6558 /* Itbl support may require additional care here. */
6559 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6560 BFD_RELOC_LO16, AT);
6561 if (mips_relax.sequence)
6562 relax_end ();
6563 }
6564 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6565 {
6566 /* If this is a reference to an external symbol, we want
6567 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6568 nop
6569 <op> $treg,0($at)
6570 <op> $treg+1,4($at)
6571 Otherwise we want
6572 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6573 nop
6574 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6575 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6576 If there is a base register we add it to $at before the
6577 lwc1 instructions. If there is a constant we include it
6578 in the lwc1 instructions. */
6579 used_at = 1;
6580 expr1.X_add_number = offset_expr.X_add_number;
6581 if (expr1.X_add_number < -0x8000
6582 || expr1.X_add_number >= 0x8000 - 4)
6583 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6584 load_got_offset (AT, &offset_expr);
6585 load_delay_nop ();
6586 if (breg != 0)
6587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6588
6589 /* Set mips_optimize to 2 to avoid inserting an undesired
6590 nop. */
6591 hold_mips_optimize = mips_optimize;
6592 mips_optimize = 2;
6593
6594 /* Itbl support may require additional care here. */
6595 relax_start (offset_expr.X_add_symbol);
6596 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6597 BFD_RELOC_LO16, AT);
6598 expr1.X_add_number += 4;
6599 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6600 BFD_RELOC_LO16, AT);
6601 relax_switch ();
6602 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6603 BFD_RELOC_LO16, AT);
6604 offset_expr.X_add_number += 4;
6605 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6606 BFD_RELOC_LO16, AT);
6607 relax_end ();
6608
6609 mips_optimize = hold_mips_optimize;
6610 }
6611 else if (mips_pic == SVR4_PIC)
6612 {
6613 int gpdelay;
6614
6615 /* If this is a reference to an external symbol, we want
6616 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6617 addu $at,$at,$gp
6618 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6619 nop
6620 <op> $treg,0($at)
6621 <op> $treg+1,4($at)
6622 Otherwise we want
6623 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6624 nop
6625 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6626 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6627 If there is a base register we add it to $at before the
6628 lwc1 instructions. If there is a constant we include it
6629 in the lwc1 instructions. */
6630 used_at = 1;
6631 expr1.X_add_number = offset_expr.X_add_number;
6632 offset_expr.X_add_number = 0;
6633 if (expr1.X_add_number < -0x8000
6634 || expr1.X_add_number >= 0x8000 - 4)
6635 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6636 gpdelay = reg_needs_delay (mips_gp_register);
6637 relax_start (offset_expr.X_add_symbol);
6638 macro_build (&offset_expr, "lui", "t,u",
6639 AT, BFD_RELOC_MIPS_GOT_HI16);
6640 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6641 AT, AT, mips_gp_register);
6642 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6643 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6644 load_delay_nop ();
6645 if (breg != 0)
6646 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6647 /* Itbl support may require additional care here. */
6648 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6649 BFD_RELOC_LO16, AT);
6650 expr1.X_add_number += 4;
6651
6652 /* Set mips_optimize to 2 to avoid inserting an undesired
6653 nop. */
6654 hold_mips_optimize = mips_optimize;
6655 mips_optimize = 2;
6656 /* Itbl support may require additional care here. */
6657 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6658 BFD_RELOC_LO16, AT);
6659 mips_optimize = hold_mips_optimize;
6660 expr1.X_add_number -= 4;
6661
6662 relax_switch ();
6663 offset_expr.X_add_number = expr1.X_add_number;
6664 if (gpdelay)
6665 macro_build (NULL, "nop", "");
6666 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6667 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6668 load_delay_nop ();
6669 if (breg != 0)
6670 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6671 /* Itbl support may require additional care here. */
6672 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6673 BFD_RELOC_LO16, AT);
6674 offset_expr.X_add_number += 4;
6675
6676 /* Set mips_optimize to 2 to avoid inserting an undesired
6677 nop. */
6678 hold_mips_optimize = mips_optimize;
6679 mips_optimize = 2;
6680 /* Itbl support may require additional care here. */
6681 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6682 BFD_RELOC_LO16, AT);
6683 mips_optimize = hold_mips_optimize;
6684 relax_end ();
6685 }
6686 else
6687 abort ();
6688
6689 break;
6690
6691 case M_LD_OB:
6692 s = "lw";
6693 goto sd_ob;
6694 case M_SD_OB:
6695 s = "sw";
6696 sd_ob:
6697 assert (HAVE_32BIT_ADDRESSES);
6698 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6699 offset_expr.X_add_number += 4;
6700 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6701 break;
6702
6703 /* New code added to support COPZ instructions.
6704 This code builds table entries out of the macros in mip_opcodes.
6705 R4000 uses interlocks to handle coproc delays.
6706 Other chips (like the R3000) require nops to be inserted for delays.
6707
6708 FIXME: Currently, we require that the user handle delays.
6709 In order to fill delay slots for non-interlocked chips,
6710 we must have a way to specify delays based on the coprocessor.
6711 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6712 What are the side-effects of the cop instruction?
6713 What cache support might we have and what are its effects?
6714 Both coprocessor & memory require delays. how long???
6715 What registers are read/set/modified?
6716
6717 If an itbl is provided to interpret cop instructions,
6718 this knowledge can be encoded in the itbl spec. */
6719
6720 case M_COP0:
6721 s = "c0";
6722 goto copz;
6723 case M_COP1:
6724 s = "c1";
6725 goto copz;
6726 case M_COP2:
6727 s = "c2";
6728 goto copz;
6729 case M_COP3:
6730 s = "c3";
6731 copz:
6732 /* For now we just do C (same as Cz). The parameter will be
6733 stored in insn_opcode by mips_ip. */
6734 macro_build (NULL, s, "C", ip->insn_opcode);
6735 break;
6736
6737 case M_MOVE:
6738 move_register (dreg, sreg);
6739 break;
6740
6741 #ifdef LOSING_COMPILER
6742 default:
6743 /* Try and see if this is a new itbl instruction.
6744 This code builds table entries out of the macros in mip_opcodes.
6745 FIXME: For now we just assemble the expression and pass it's
6746 value along as a 32-bit immediate.
6747 We may want to have the assembler assemble this value,
6748 so that we gain the assembler's knowledge of delay slots,
6749 symbols, etc.
6750 Would it be more efficient to use mask (id) here? */
6751 if (itbl_have_entries
6752 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6753 {
6754 s = ip->insn_mo->name;
6755 s2 = "cop3";
6756 coproc = ITBL_DECODE_PNUM (immed_expr);;
6757 macro_build (&immed_expr, s, "C");
6758 break;
6759 }
6760 macro2 (ip);
6761 break;
6762 }
6763 if (mips_opts.noat && used_at)
6764 as_bad (_("Macro used $at after \".set noat\""));
6765 }
6766
6767 static void
6768 macro2 (struct mips_cl_insn *ip)
6769 {
6770 register int treg, sreg, dreg, breg;
6771 int tempreg;
6772 int mask;
6773 int used_at;
6774 expressionS expr1;
6775 const char *s;
6776 const char *s2;
6777 const char *fmt;
6778 int likely = 0;
6779 int dbl = 0;
6780 int coproc = 0;
6781 int lr = 0;
6782 int imm = 0;
6783 int off;
6784 offsetT maxnum;
6785 bfd_reloc_code_real_type r;
6786
6787 treg = (ip->insn_opcode >> 16) & 0x1f;
6788 dreg = (ip->insn_opcode >> 11) & 0x1f;
6789 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6790 mask = ip->insn_mo->mask;
6791
6792 expr1.X_op = O_constant;
6793 expr1.X_op_symbol = NULL;
6794 expr1.X_add_symbol = NULL;
6795 expr1.X_add_number = 1;
6796
6797 switch (mask)
6798 {
6799 #endif /* LOSING_COMPILER */
6800
6801 case M_DMUL:
6802 dbl = 1;
6803 case M_MUL:
6804 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6805 macro_build (NULL, "mflo", "d", dreg);
6806 break;
6807
6808 case M_DMUL_I:
6809 dbl = 1;
6810 case M_MUL_I:
6811 /* The MIPS assembler some times generates shifts and adds. I'm
6812 not trying to be that fancy. GCC should do this for us
6813 anyway. */
6814 used_at = 1;
6815 load_register (AT, &imm_expr, dbl);
6816 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6817 macro_build (NULL, "mflo", "d", dreg);
6818 break;
6819
6820 case M_DMULO_I:
6821 dbl = 1;
6822 case M_MULO_I:
6823 imm = 1;
6824 goto do_mulo;
6825
6826 case M_DMULO:
6827 dbl = 1;
6828 case M_MULO:
6829 do_mulo:
6830 mips_emit_delays (TRUE);
6831 ++mips_opts.noreorder;
6832 mips_any_noreorder = 1;
6833 used_at = 1;
6834 if (imm)
6835 load_register (AT, &imm_expr, dbl);
6836 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6837 macro_build (NULL, "mflo", "d", dreg);
6838 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6839 macro_build (NULL, "mfhi", "d", AT);
6840 if (mips_trap)
6841 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6842 else
6843 {
6844 expr1.X_add_number = 8;
6845 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6846 macro_build (NULL, "nop", "", 0);
6847 macro_build (NULL, "break", "c", 6);
6848 }
6849 --mips_opts.noreorder;
6850 macro_build (NULL, "mflo", "d", dreg);
6851 break;
6852
6853 case M_DMULOU_I:
6854 dbl = 1;
6855 case M_MULOU_I:
6856 imm = 1;
6857 goto do_mulou;
6858
6859 case M_DMULOU:
6860 dbl = 1;
6861 case M_MULOU:
6862 do_mulou:
6863 mips_emit_delays (TRUE);
6864 ++mips_opts.noreorder;
6865 mips_any_noreorder = 1;
6866 used_at = 1;
6867 if (imm)
6868 load_register (AT, &imm_expr, dbl);
6869 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6870 sreg, imm ? AT : treg);
6871 macro_build (NULL, "mfhi", "d", AT);
6872 macro_build (NULL, "mflo", "d", dreg);
6873 if (mips_trap)
6874 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6875 else
6876 {
6877 expr1.X_add_number = 8;
6878 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6879 macro_build (NULL, "nop", "", 0);
6880 macro_build (NULL, "break", "c", 6);
6881 }
6882 --mips_opts.noreorder;
6883 break;
6884
6885 case M_DROL:
6886 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6887 {
6888 if (dreg == sreg)
6889 {
6890 tempreg = AT;
6891 used_at = 1;
6892 }
6893 else
6894 {
6895 tempreg = dreg;
6896 }
6897 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6898 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6899 break;
6900 }
6901 used_at = 1;
6902 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6903 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6904 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6905 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6906 break;
6907
6908 case M_ROL:
6909 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6910 {
6911 if (dreg == sreg)
6912 {
6913 tempreg = AT;
6914 used_at = 1;
6915 }
6916 else
6917 {
6918 tempreg = dreg;
6919 }
6920 macro_build (NULL, "negu", "d,w", tempreg, treg);
6921 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6922 break;
6923 }
6924 used_at = 1;
6925 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6926 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6927 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6928 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6929 break;
6930
6931 case M_DROL_I:
6932 {
6933 unsigned int rot;
6934 char *l, *r;
6935
6936 if (imm_expr.X_op != O_constant)
6937 as_bad (_("Improper rotate count"));
6938 rot = imm_expr.X_add_number & 0x3f;
6939 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6940 {
6941 rot = (64 - rot) & 0x3f;
6942 if (rot >= 32)
6943 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6944 else
6945 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6946 break;
6947 }
6948 if (rot == 0)
6949 {
6950 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6951 break;
6952 }
6953 l = (rot < 0x20) ? "dsll" : "dsll32";
6954 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6955 rot &= 0x1f;
6956 used_at = 1;
6957 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6958 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6959 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6960 }
6961 break;
6962
6963 case M_ROL_I:
6964 {
6965 unsigned int rot;
6966
6967 if (imm_expr.X_op != O_constant)
6968 as_bad (_("Improper rotate count"));
6969 rot = imm_expr.X_add_number & 0x1f;
6970 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6971 {
6972 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6973 break;
6974 }
6975 if (rot == 0)
6976 {
6977 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6978 break;
6979 }
6980 used_at = 1;
6981 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6982 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6983 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6984 }
6985 break;
6986
6987 case M_DROR:
6988 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6989 {
6990 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6991 break;
6992 }
6993 used_at = 1;
6994 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6995 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6996 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6997 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6998 break;
6999
7000 case M_ROR:
7001 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7002 {
7003 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7004 break;
7005 }
7006 used_at = 1;
7007 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7008 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7009 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7010 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7011 break;
7012
7013 case M_DROR_I:
7014 {
7015 unsigned int rot;
7016 char *l, *r;
7017
7018 if (imm_expr.X_op != O_constant)
7019 as_bad (_("Improper rotate count"));
7020 rot = imm_expr.X_add_number & 0x3f;
7021 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7022 {
7023 if (rot >= 32)
7024 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7025 else
7026 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7027 break;
7028 }
7029 if (rot == 0)
7030 {
7031 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7032 break;
7033 }
7034 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7035 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7036 rot &= 0x1f;
7037 used_at = 1;
7038 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7039 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7040 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7041 }
7042 break;
7043
7044 case M_ROR_I:
7045 {
7046 unsigned int rot;
7047
7048 if (imm_expr.X_op != O_constant)
7049 as_bad (_("Improper rotate count"));
7050 rot = imm_expr.X_add_number & 0x1f;
7051 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7052 {
7053 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7054 break;
7055 }
7056 if (rot == 0)
7057 {
7058 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7059 break;
7060 }
7061 used_at = 1;
7062 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7063 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7064 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7065 }
7066 break;
7067
7068 case M_S_DOB:
7069 if (mips_opts.arch == CPU_R4650)
7070 {
7071 as_bad (_("opcode not supported on this processor"));
7072 break;
7073 }
7074 assert (mips_opts.isa == ISA_MIPS1);
7075 /* Even on a big endian machine $fn comes before $fn+1. We have
7076 to adjust when storing to memory. */
7077 macro_build (&offset_expr, "swc1", "T,o(b)",
7078 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7079 offset_expr.X_add_number += 4;
7080 macro_build (&offset_expr, "swc1", "T,o(b)",
7081 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7082 break;
7083
7084 case M_SEQ:
7085 if (sreg == 0)
7086 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7087 else if (treg == 0)
7088 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7089 else
7090 {
7091 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7092 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7093 }
7094 break;
7095
7096 case M_SEQ_I:
7097 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7098 {
7099 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7100 break;
7101 }
7102 if (sreg == 0)
7103 {
7104 as_warn (_("Instruction %s: result is always false"),
7105 ip->insn_mo->name);
7106 move_register (dreg, 0);
7107 break;
7108 }
7109 if (imm_expr.X_op == O_constant
7110 && imm_expr.X_add_number >= 0
7111 && imm_expr.X_add_number < 0x10000)
7112 {
7113 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7114 }
7115 else if (imm_expr.X_op == O_constant
7116 && imm_expr.X_add_number > -0x8000
7117 && imm_expr.X_add_number < 0)
7118 {
7119 imm_expr.X_add_number = -imm_expr.X_add_number;
7120 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7121 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7122 }
7123 else
7124 {
7125 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7126 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7127 used_at = 1;
7128 }
7129 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7130 break;
7131
7132 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7133 s = "slt";
7134 goto sge;
7135 case M_SGEU:
7136 s = "sltu";
7137 sge:
7138 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7139 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7140 break;
7141
7142 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7143 case M_SGEU_I:
7144 if (imm_expr.X_op == O_constant
7145 && imm_expr.X_add_number >= -0x8000
7146 && imm_expr.X_add_number < 0x8000)
7147 {
7148 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7149 dreg, sreg, BFD_RELOC_LO16);
7150 }
7151 else
7152 {
7153 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7154 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7155 dreg, sreg, AT);
7156 used_at = 1;
7157 }
7158 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7159 break;
7160
7161 case M_SGT: /* sreg > treg <==> treg < sreg */
7162 s = "slt";
7163 goto sgt;
7164 case M_SGTU:
7165 s = "sltu";
7166 sgt:
7167 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7168 break;
7169
7170 case M_SGT_I: /* sreg > I <==> I < sreg */
7171 s = "slt";
7172 goto sgti;
7173 case M_SGTU_I:
7174 s = "sltu";
7175 sgti:
7176 used_at = 1;
7177 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7178 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7179 break;
7180
7181 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7182 s = "slt";
7183 goto sle;
7184 case M_SLEU:
7185 s = "sltu";
7186 sle:
7187 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7188 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7189 break;
7190
7191 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7192 s = "slt";
7193 goto slei;
7194 case M_SLEU_I:
7195 s = "sltu";
7196 slei:
7197 used_at = 1;
7198 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7199 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7200 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7201 break;
7202
7203 case M_SLT_I:
7204 if (imm_expr.X_op == O_constant
7205 && imm_expr.X_add_number >= -0x8000
7206 && imm_expr.X_add_number < 0x8000)
7207 {
7208 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7209 break;
7210 }
7211 used_at = 1;
7212 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7213 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7214 break;
7215
7216 case M_SLTU_I:
7217 if (imm_expr.X_op == O_constant
7218 && imm_expr.X_add_number >= -0x8000
7219 && imm_expr.X_add_number < 0x8000)
7220 {
7221 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7222 BFD_RELOC_LO16);
7223 break;
7224 }
7225 used_at = 1;
7226 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7227 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7228 break;
7229
7230 case M_SNE:
7231 if (sreg == 0)
7232 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7233 else if (treg == 0)
7234 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7235 else
7236 {
7237 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7238 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7239 }
7240 break;
7241
7242 case M_SNE_I:
7243 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7244 {
7245 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7246 break;
7247 }
7248 if (sreg == 0)
7249 {
7250 as_warn (_("Instruction %s: result is always true"),
7251 ip->insn_mo->name);
7252 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7253 dreg, 0, BFD_RELOC_LO16);
7254 break;
7255 }
7256 if (imm_expr.X_op == O_constant
7257 && imm_expr.X_add_number >= 0
7258 && imm_expr.X_add_number < 0x10000)
7259 {
7260 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7261 }
7262 else if (imm_expr.X_op == O_constant
7263 && imm_expr.X_add_number > -0x8000
7264 && imm_expr.X_add_number < 0)
7265 {
7266 imm_expr.X_add_number = -imm_expr.X_add_number;
7267 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7268 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7269 }
7270 else
7271 {
7272 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7273 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7274 used_at = 1;
7275 }
7276 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7277 break;
7278
7279 case M_DSUB_I:
7280 dbl = 1;
7281 case M_SUB_I:
7282 if (imm_expr.X_op == O_constant
7283 && imm_expr.X_add_number > -0x8000
7284 && imm_expr.X_add_number <= 0x8000)
7285 {
7286 imm_expr.X_add_number = -imm_expr.X_add_number;
7287 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7288 dreg, sreg, BFD_RELOC_LO16);
7289 break;
7290 }
7291 used_at = 1;
7292 load_register (AT, &imm_expr, dbl);
7293 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7294 break;
7295
7296 case M_DSUBU_I:
7297 dbl = 1;
7298 case M_SUBU_I:
7299 if (imm_expr.X_op == O_constant
7300 && imm_expr.X_add_number > -0x8000
7301 && imm_expr.X_add_number <= 0x8000)
7302 {
7303 imm_expr.X_add_number = -imm_expr.X_add_number;
7304 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7305 dreg, sreg, BFD_RELOC_LO16);
7306 break;
7307 }
7308 used_at = 1;
7309 load_register (AT, &imm_expr, dbl);
7310 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7311 break;
7312
7313 case M_TEQ_I:
7314 s = "teq";
7315 goto trap;
7316 case M_TGE_I:
7317 s = "tge";
7318 goto trap;
7319 case M_TGEU_I:
7320 s = "tgeu";
7321 goto trap;
7322 case M_TLT_I:
7323 s = "tlt";
7324 goto trap;
7325 case M_TLTU_I:
7326 s = "tltu";
7327 goto trap;
7328 case M_TNE_I:
7329 s = "tne";
7330 trap:
7331 used_at = 1;
7332 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7333 macro_build (NULL, s, "s,t", sreg, AT);
7334 break;
7335
7336 case M_TRUNCWS:
7337 case M_TRUNCWD:
7338 assert (mips_opts.isa == ISA_MIPS1);
7339 used_at = 1;
7340 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7341 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7342
7343 /*
7344 * Is the double cfc1 instruction a bug in the mips assembler;
7345 * or is there a reason for it?
7346 */
7347 mips_emit_delays (TRUE);
7348 ++mips_opts.noreorder;
7349 mips_any_noreorder = 1;
7350 macro_build (NULL, "cfc1", "t,G", treg, RA);
7351 macro_build (NULL, "cfc1", "t,G", treg, RA);
7352 macro_build (NULL, "nop", "");
7353 expr1.X_add_number = 3;
7354 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7355 expr1.X_add_number = 2;
7356 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7357 macro_build (NULL, "ctc1", "t,G", AT, RA);
7358 macro_build (NULL, "nop", "");
7359 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7360 dreg, sreg);
7361 macro_build (NULL, "ctc1", "t,G", treg, RA);
7362 macro_build (NULL, "nop", "");
7363 --mips_opts.noreorder;
7364 break;
7365
7366 case M_ULH:
7367 s = "lb";
7368 goto ulh;
7369 case M_ULHU:
7370 s = "lbu";
7371 ulh:
7372 used_at = 1;
7373 if (offset_expr.X_add_number >= 0x7fff)
7374 as_bad (_("operand overflow"));
7375 if (! target_big_endian)
7376 ++offset_expr.X_add_number;
7377 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7378 if (! target_big_endian)
7379 --offset_expr.X_add_number;
7380 else
7381 ++offset_expr.X_add_number;
7382 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7383 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7384 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7385 break;
7386
7387 case M_ULD:
7388 s = "ldl";
7389 s2 = "ldr";
7390 off = 7;
7391 goto ulw;
7392 case M_ULW:
7393 s = "lwl";
7394 s2 = "lwr";
7395 off = 3;
7396 ulw:
7397 if (offset_expr.X_add_number >= 0x8000 - off)
7398 as_bad (_("operand overflow"));
7399 if (treg != breg)
7400 tempreg = treg;
7401 else
7402 {
7403 used_at = 1;
7404 tempreg = AT;
7405 }
7406 if (! target_big_endian)
7407 offset_expr.X_add_number += off;
7408 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7409 if (! target_big_endian)
7410 offset_expr.X_add_number -= off;
7411 else
7412 offset_expr.X_add_number += off;
7413 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7414
7415 /* If necessary, move the result in tempreg the final destination. */
7416 if (treg == tempreg)
7417 break;
7418 /* Protect second load's delay slot. */
7419 load_delay_nop ();
7420 move_register (treg, tempreg);
7421 break;
7422
7423 case M_ULD_A:
7424 s = "ldl";
7425 s2 = "ldr";
7426 off = 7;
7427 goto ulwa;
7428 case M_ULW_A:
7429 s = "lwl";
7430 s2 = "lwr";
7431 off = 3;
7432 ulwa:
7433 used_at = 1;
7434 load_address (AT, &offset_expr, &used_at);
7435 if (breg != 0)
7436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7437 if (! target_big_endian)
7438 expr1.X_add_number = off;
7439 else
7440 expr1.X_add_number = 0;
7441 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7442 if (! target_big_endian)
7443 expr1.X_add_number = 0;
7444 else
7445 expr1.X_add_number = off;
7446 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7447 break;
7448
7449 case M_ULH_A:
7450 case M_ULHU_A:
7451 used_at = 1;
7452 load_address (AT, &offset_expr, &used_at);
7453 if (breg != 0)
7454 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7455 if (target_big_endian)
7456 expr1.X_add_number = 0;
7457 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7458 treg, BFD_RELOC_LO16, AT);
7459 if (target_big_endian)
7460 expr1.X_add_number = 1;
7461 else
7462 expr1.X_add_number = 0;
7463 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7464 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7465 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7466 break;
7467
7468 case M_USH:
7469 used_at = 1;
7470 if (offset_expr.X_add_number >= 0x7fff)
7471 as_bad (_("operand overflow"));
7472 if (target_big_endian)
7473 ++offset_expr.X_add_number;
7474 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7475 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7476 if (target_big_endian)
7477 --offset_expr.X_add_number;
7478 else
7479 ++offset_expr.X_add_number;
7480 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7481 break;
7482
7483 case M_USD:
7484 s = "sdl";
7485 s2 = "sdr";
7486 off = 7;
7487 goto usw;
7488 case M_USW:
7489 s = "swl";
7490 s2 = "swr";
7491 off = 3;
7492 usw:
7493 if (offset_expr.X_add_number >= 0x8000 - off)
7494 as_bad (_("operand overflow"));
7495 if (! target_big_endian)
7496 offset_expr.X_add_number += off;
7497 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7498 if (! target_big_endian)
7499 offset_expr.X_add_number -= off;
7500 else
7501 offset_expr.X_add_number += off;
7502 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7503 break;
7504
7505 case M_USD_A:
7506 s = "sdl";
7507 s2 = "sdr";
7508 off = 7;
7509 goto uswa;
7510 case M_USW_A:
7511 s = "swl";
7512 s2 = "swr";
7513 off = 3;
7514 uswa:
7515 used_at = 1;
7516 load_address (AT, &offset_expr, &used_at);
7517 if (breg != 0)
7518 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7519 if (! target_big_endian)
7520 expr1.X_add_number = off;
7521 else
7522 expr1.X_add_number = 0;
7523 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7524 if (! target_big_endian)
7525 expr1.X_add_number = 0;
7526 else
7527 expr1.X_add_number = off;
7528 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7529 break;
7530
7531 case M_USH_A:
7532 used_at = 1;
7533 load_address (AT, &offset_expr, &used_at);
7534 if (breg != 0)
7535 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7536 if (! target_big_endian)
7537 expr1.X_add_number = 0;
7538 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7539 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7540 if (! target_big_endian)
7541 expr1.X_add_number = 1;
7542 else
7543 expr1.X_add_number = 0;
7544 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7545 if (! target_big_endian)
7546 expr1.X_add_number = 0;
7547 else
7548 expr1.X_add_number = 1;
7549 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7550 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7551 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7552 break;
7553
7554 default:
7555 /* FIXME: Check if this is one of the itbl macros, since they
7556 are added dynamically. */
7557 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7558 break;
7559 }
7560 if (mips_opts.noat && used_at)
7561 as_bad (_("Macro used $at after \".set noat\""));
7562 }
7563
7564 /* Implement macros in mips16 mode. */
7565
7566 static void
7567 mips16_macro (struct mips_cl_insn *ip)
7568 {
7569 int mask;
7570 int xreg, yreg, zreg, tmp;
7571 expressionS expr1;
7572 int dbl;
7573 const char *s, *s2, *s3;
7574
7575 mask = ip->insn_mo->mask;
7576
7577 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7578 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7579 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7580
7581 expr1.X_op = O_constant;
7582 expr1.X_op_symbol = NULL;
7583 expr1.X_add_symbol = NULL;
7584 expr1.X_add_number = 1;
7585
7586 dbl = 0;
7587
7588 switch (mask)
7589 {
7590 default:
7591 internalError ();
7592
7593 case M_DDIV_3:
7594 dbl = 1;
7595 case M_DIV_3:
7596 s = "mflo";
7597 goto do_div3;
7598 case M_DREM_3:
7599 dbl = 1;
7600 case M_REM_3:
7601 s = "mfhi";
7602 do_div3:
7603 mips_emit_delays (TRUE);
7604 ++mips_opts.noreorder;
7605 mips_any_noreorder = 1;
7606 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7607 expr1.X_add_number = 2;
7608 macro_build (&expr1, "bnez", "x,p", yreg);
7609 macro_build (NULL, "break", "6", 7);
7610
7611 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7612 since that causes an overflow. We should do that as well,
7613 but I don't see how to do the comparisons without a temporary
7614 register. */
7615 --mips_opts.noreorder;
7616 macro_build (NULL, s, "x", zreg);
7617 break;
7618
7619 case M_DIVU_3:
7620 s = "divu";
7621 s2 = "mflo";
7622 goto do_divu3;
7623 case M_REMU_3:
7624 s = "divu";
7625 s2 = "mfhi";
7626 goto do_divu3;
7627 case M_DDIVU_3:
7628 s = "ddivu";
7629 s2 = "mflo";
7630 goto do_divu3;
7631 case M_DREMU_3:
7632 s = "ddivu";
7633 s2 = "mfhi";
7634 do_divu3:
7635 mips_emit_delays (TRUE);
7636 ++mips_opts.noreorder;
7637 mips_any_noreorder = 1;
7638 macro_build (NULL, s, "0,x,y", xreg, yreg);
7639 expr1.X_add_number = 2;
7640 macro_build (&expr1, "bnez", "x,p", yreg);
7641 macro_build (NULL, "break", "6", 7);
7642 --mips_opts.noreorder;
7643 macro_build (NULL, s2, "x", zreg);
7644 break;
7645
7646 case M_DMUL:
7647 dbl = 1;
7648 case M_MUL:
7649 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7650 macro_build (NULL, "mflo", "x", zreg);
7651 break;
7652
7653 case M_DSUBU_I:
7654 dbl = 1;
7655 goto do_subu;
7656 case M_SUBU_I:
7657 do_subu:
7658 if (imm_expr.X_op != O_constant)
7659 as_bad (_("Unsupported large constant"));
7660 imm_expr.X_add_number = -imm_expr.X_add_number;
7661 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7662 break;
7663
7664 case M_SUBU_I_2:
7665 if (imm_expr.X_op != O_constant)
7666 as_bad (_("Unsupported large constant"));
7667 imm_expr.X_add_number = -imm_expr.X_add_number;
7668 macro_build (&imm_expr, "addiu", "x,k", xreg);
7669 break;
7670
7671 case M_DSUBU_I_2:
7672 if (imm_expr.X_op != O_constant)
7673 as_bad (_("Unsupported large constant"));
7674 imm_expr.X_add_number = -imm_expr.X_add_number;
7675 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7676 break;
7677
7678 case M_BEQ:
7679 s = "cmp";
7680 s2 = "bteqz";
7681 goto do_branch;
7682 case M_BNE:
7683 s = "cmp";
7684 s2 = "btnez";
7685 goto do_branch;
7686 case M_BLT:
7687 s = "slt";
7688 s2 = "btnez";
7689 goto do_branch;
7690 case M_BLTU:
7691 s = "sltu";
7692 s2 = "btnez";
7693 goto do_branch;
7694 case M_BLE:
7695 s = "slt";
7696 s2 = "bteqz";
7697 goto do_reverse_branch;
7698 case M_BLEU:
7699 s = "sltu";
7700 s2 = "bteqz";
7701 goto do_reverse_branch;
7702 case M_BGE:
7703 s = "slt";
7704 s2 = "bteqz";
7705 goto do_branch;
7706 case M_BGEU:
7707 s = "sltu";
7708 s2 = "bteqz";
7709 goto do_branch;
7710 case M_BGT:
7711 s = "slt";
7712 s2 = "btnez";
7713 goto do_reverse_branch;
7714 case M_BGTU:
7715 s = "sltu";
7716 s2 = "btnez";
7717
7718 do_reverse_branch:
7719 tmp = xreg;
7720 xreg = yreg;
7721 yreg = tmp;
7722
7723 do_branch:
7724 macro_build (NULL, s, "x,y", xreg, yreg);
7725 macro_build (&offset_expr, s2, "p");
7726 break;
7727
7728 case M_BEQ_I:
7729 s = "cmpi";
7730 s2 = "bteqz";
7731 s3 = "x,U";
7732 goto do_branch_i;
7733 case M_BNE_I:
7734 s = "cmpi";
7735 s2 = "btnez";
7736 s3 = "x,U";
7737 goto do_branch_i;
7738 case M_BLT_I:
7739 s = "slti";
7740 s2 = "btnez";
7741 s3 = "x,8";
7742 goto do_branch_i;
7743 case M_BLTU_I:
7744 s = "sltiu";
7745 s2 = "btnez";
7746 s3 = "x,8";
7747 goto do_branch_i;
7748 case M_BLE_I:
7749 s = "slti";
7750 s2 = "btnez";
7751 s3 = "x,8";
7752 goto do_addone_branch_i;
7753 case M_BLEU_I:
7754 s = "sltiu";
7755 s2 = "btnez";
7756 s3 = "x,8";
7757 goto do_addone_branch_i;
7758 case M_BGE_I:
7759 s = "slti";
7760 s2 = "bteqz";
7761 s3 = "x,8";
7762 goto do_branch_i;
7763 case M_BGEU_I:
7764 s = "sltiu";
7765 s2 = "bteqz";
7766 s3 = "x,8";
7767 goto do_branch_i;
7768 case M_BGT_I:
7769 s = "slti";
7770 s2 = "bteqz";
7771 s3 = "x,8";
7772 goto do_addone_branch_i;
7773 case M_BGTU_I:
7774 s = "sltiu";
7775 s2 = "bteqz";
7776 s3 = "x,8";
7777
7778 do_addone_branch_i:
7779 if (imm_expr.X_op != O_constant)
7780 as_bad (_("Unsupported large constant"));
7781 ++imm_expr.X_add_number;
7782
7783 do_branch_i:
7784 macro_build (&imm_expr, s, s3, xreg);
7785 macro_build (&offset_expr, s2, "p");
7786 break;
7787
7788 case M_ABS:
7789 expr1.X_add_number = 0;
7790 macro_build (&expr1, "slti", "x,8", yreg);
7791 if (xreg != yreg)
7792 move_register (xreg, yreg);
7793 expr1.X_add_number = 2;
7794 macro_build (&expr1, "bteqz", "p");
7795 macro_build (NULL, "neg", "x,w", xreg, xreg);
7796 }
7797 }
7798
7799 /* For consistency checking, verify that all bits are specified either
7800 by the match/mask part of the instruction definition, or by the
7801 operand list. */
7802 static int
7803 validate_mips_insn (const struct mips_opcode *opc)
7804 {
7805 const char *p = opc->args;
7806 char c;
7807 unsigned long used_bits = opc->mask;
7808
7809 if ((used_bits & opc->match) != opc->match)
7810 {
7811 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7812 opc->name, opc->args);
7813 return 0;
7814 }
7815 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7816 while (*p)
7817 switch (c = *p++)
7818 {
7819 case ',': break;
7820 case '(': break;
7821 case ')': break;
7822 case '+':
7823 switch (c = *p++)
7824 {
7825 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7826 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7827 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7828 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7829 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7830 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7831 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7832 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7833 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7834 case 'I': break;
7835 default:
7836 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7837 c, opc->name, opc->args);
7838 return 0;
7839 }
7840 break;
7841 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7842 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7843 case 'A': break;
7844 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7845 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7846 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7847 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7848 case 'F': break;
7849 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7850 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7851 case 'I': break;
7852 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7853 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7854 case 'L': break;
7855 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7856 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7857 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7858 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7859 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7860 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7861 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7862 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7863 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7864 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7865 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7866 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7867 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7868 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7869 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7870 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7871 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7872 case 'f': break;
7873 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7874 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7875 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7876 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7877 case 'l': break;
7878 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7879 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7880 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7881 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7882 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7883 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7884 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7885 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7886 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7887 case 'x': break;
7888 case 'z': break;
7889 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7890 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7891 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7892 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7893 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7894 case '[': break;
7895 case ']': break;
7896 default:
7897 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7898 c, opc->name, opc->args);
7899 return 0;
7900 }
7901 #undef USE_BITS
7902 if (used_bits != 0xffffffff)
7903 {
7904 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7905 ~used_bits & 0xffffffff, opc->name, opc->args);
7906 return 0;
7907 }
7908 return 1;
7909 }
7910
7911 /* This routine assembles an instruction into its binary format. As a
7912 side effect, it sets one of the global variables imm_reloc or
7913 offset_reloc to the type of relocation to do if one of the operands
7914 is an address expression. */
7915
7916 static void
7917 mips_ip (char *str, struct mips_cl_insn *ip)
7918 {
7919 char *s;
7920 const char *args;
7921 char c = 0;
7922 struct mips_opcode *insn;
7923 char *argsStart;
7924 unsigned int regno;
7925 unsigned int lastregno = 0;
7926 unsigned int lastpos = 0;
7927 unsigned int limlo, limhi;
7928 char *s_reset;
7929 char save_c = 0;
7930
7931 insn_error = NULL;
7932
7933 /* If the instruction contains a '.', we first try to match an instruction
7934 including the '.'. Then we try again without the '.'. */
7935 insn = NULL;
7936 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7937 continue;
7938
7939 /* If we stopped on whitespace, then replace the whitespace with null for
7940 the call to hash_find. Save the character we replaced just in case we
7941 have to re-parse the instruction. */
7942 if (ISSPACE (*s))
7943 {
7944 save_c = *s;
7945 *s++ = '\0';
7946 }
7947
7948 insn = (struct mips_opcode *) hash_find (op_hash, str);
7949
7950 /* If we didn't find the instruction in the opcode table, try again, but
7951 this time with just the instruction up to, but not including the
7952 first '.'. */
7953 if (insn == NULL)
7954 {
7955 /* Restore the character we overwrite above (if any). */
7956 if (save_c)
7957 *(--s) = save_c;
7958
7959 /* Scan up to the first '.' or whitespace. */
7960 for (s = str;
7961 *s != '\0' && *s != '.' && !ISSPACE (*s);
7962 ++s)
7963 continue;
7964
7965 /* If we did not find a '.', then we can quit now. */
7966 if (*s != '.')
7967 {
7968 insn_error = "unrecognized opcode";
7969 return;
7970 }
7971
7972 /* Lookup the instruction in the hash table. */
7973 *s++ = '\0';
7974 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7975 {
7976 insn_error = "unrecognized opcode";
7977 return;
7978 }
7979 }
7980
7981 argsStart = s;
7982 for (;;)
7983 {
7984 bfd_boolean ok;
7985
7986 assert (strcmp (insn->name, str) == 0);
7987
7988 if (OPCODE_IS_MEMBER (insn,
7989 (mips_opts.isa
7990 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7991 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7992 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7993 mips_opts.arch))
7994 ok = TRUE;
7995 else
7996 ok = FALSE;
7997
7998 if (insn->pinfo != INSN_MACRO)
7999 {
8000 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8001 ok = FALSE;
8002 }
8003
8004 if (! ok)
8005 {
8006 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8007 && strcmp (insn->name, insn[1].name) == 0)
8008 {
8009 ++insn;
8010 continue;
8011 }
8012 else
8013 {
8014 if (!insn_error)
8015 {
8016 static char buf[100];
8017 sprintf (buf,
8018 _("opcode not supported on this processor: %s (%s)"),
8019 mips_cpu_info_from_arch (mips_opts.arch)->name,
8020 mips_cpu_info_from_isa (mips_opts.isa)->name);
8021 insn_error = buf;
8022 }
8023 if (save_c)
8024 *(--s) = save_c;
8025 return;
8026 }
8027 }
8028
8029 ip->insn_mo = insn;
8030 ip->insn_opcode = insn->match;
8031 insn_error = NULL;
8032 for (args = insn->args;; ++args)
8033 {
8034 int is_mdmx;
8035
8036 s += strspn (s, " \t");
8037 is_mdmx = 0;
8038 switch (*args)
8039 {
8040 case '\0': /* end of args */
8041 if (*s == '\0')
8042 return;
8043 break;
8044
8045 case ',':
8046 if (*s++ == *args)
8047 continue;
8048 s--;
8049 switch (*++args)
8050 {
8051 case 'r':
8052 case 'v':
8053 ip->insn_opcode |= lastregno << OP_SH_RS;
8054 continue;
8055
8056 case 'w':
8057 ip->insn_opcode |= lastregno << OP_SH_RT;
8058 continue;
8059
8060 case 'W':
8061 ip->insn_opcode |= lastregno << OP_SH_FT;
8062 continue;
8063
8064 case 'V':
8065 ip->insn_opcode |= lastregno << OP_SH_FS;
8066 continue;
8067 }
8068 break;
8069
8070 case '(':
8071 /* Handle optional base register.
8072 Either the base register is omitted or
8073 we must have a left paren. */
8074 /* This is dependent on the next operand specifier
8075 is a base register specification. */
8076 assert (args[1] == 'b' || args[1] == '5'
8077 || args[1] == '-' || args[1] == '4');
8078 if (*s == '\0')
8079 return;
8080
8081 case ')': /* these must match exactly */
8082 case '[':
8083 case ']':
8084 if (*s++ == *args)
8085 continue;
8086 break;
8087
8088 case '+': /* Opcode extension character. */
8089 switch (*++args)
8090 {
8091 case 'A': /* ins/ext position, becomes LSB. */
8092 limlo = 0;
8093 limhi = 31;
8094 goto do_lsb;
8095 case 'E':
8096 limlo = 32;
8097 limhi = 63;
8098 goto do_lsb;
8099 do_lsb:
8100 my_getExpression (&imm_expr, s);
8101 check_absolute_expr (ip, &imm_expr);
8102 if ((unsigned long) imm_expr.X_add_number < limlo
8103 || (unsigned long) imm_expr.X_add_number > limhi)
8104 {
8105 as_bad (_("Improper position (%lu)"),
8106 (unsigned long) imm_expr.X_add_number);
8107 imm_expr.X_add_number = limlo;
8108 }
8109 lastpos = imm_expr.X_add_number;
8110 ip->insn_opcode |= (imm_expr.X_add_number
8111 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8112 imm_expr.X_op = O_absent;
8113 s = expr_end;
8114 continue;
8115
8116 case 'B': /* ins size, becomes MSB. */
8117 limlo = 1;
8118 limhi = 32;
8119 goto do_msb;
8120 case 'F':
8121 limlo = 33;
8122 limhi = 64;
8123 goto do_msb;
8124 do_msb:
8125 my_getExpression (&imm_expr, s);
8126 check_absolute_expr (ip, &imm_expr);
8127 /* Check for negative input so that small negative numbers
8128 will not succeed incorrectly. The checks against
8129 (pos+size) transitively check "size" itself,
8130 assuming that "pos" is reasonable. */
8131 if ((long) imm_expr.X_add_number < 0
8132 || ((unsigned long) imm_expr.X_add_number
8133 + lastpos) < limlo
8134 || ((unsigned long) imm_expr.X_add_number
8135 + lastpos) > limhi)
8136 {
8137 as_bad (_("Improper insert size (%lu, position %lu)"),
8138 (unsigned long) imm_expr.X_add_number,
8139 (unsigned long) lastpos);
8140 imm_expr.X_add_number = limlo - lastpos;
8141 }
8142 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8143 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8144 imm_expr.X_op = O_absent;
8145 s = expr_end;
8146 continue;
8147
8148 case 'C': /* ext size, becomes MSBD. */
8149 limlo = 1;
8150 limhi = 32;
8151 goto do_msbd;
8152 case 'G':
8153 limlo = 33;
8154 limhi = 64;
8155 goto do_msbd;
8156 case 'H':
8157 limlo = 33;
8158 limhi = 64;
8159 goto do_msbd;
8160 do_msbd:
8161 my_getExpression (&imm_expr, s);
8162 check_absolute_expr (ip, &imm_expr);
8163 /* Check for negative input so that small negative numbers
8164 will not succeed incorrectly. The checks against
8165 (pos+size) transitively check "size" itself,
8166 assuming that "pos" is reasonable. */
8167 if ((long) imm_expr.X_add_number < 0
8168 || ((unsigned long) imm_expr.X_add_number
8169 + lastpos) < limlo
8170 || ((unsigned long) imm_expr.X_add_number
8171 + lastpos) > limhi)
8172 {
8173 as_bad (_("Improper extract size (%lu, position %lu)"),
8174 (unsigned long) imm_expr.X_add_number,
8175 (unsigned long) lastpos);
8176 imm_expr.X_add_number = limlo - lastpos;
8177 }
8178 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8179 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8180 imm_expr.X_op = O_absent;
8181 s = expr_end;
8182 continue;
8183
8184 case 'D':
8185 /* +D is for disassembly only; never match. */
8186 break;
8187
8188 case 'I':
8189 /* "+I" is like "I", except that imm2_expr is used. */
8190 my_getExpression (&imm2_expr, s);
8191 if (imm2_expr.X_op != O_big
8192 && imm2_expr.X_op != O_constant)
8193 insn_error = _("absolute expression required");
8194 normalize_constant_expr (&imm2_expr);
8195 s = expr_end;
8196 continue;
8197
8198 default:
8199 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8200 *args, insn->name, insn->args);
8201 /* Further processing is fruitless. */
8202 return;
8203 }
8204 break;
8205
8206 case '<': /* must be at least one digit */
8207 /*
8208 * According to the manual, if the shift amount is greater
8209 * than 31 or less than 0, then the shift amount should be
8210 * mod 32. In reality the mips assembler issues an error.
8211 * We issue a warning and mask out all but the low 5 bits.
8212 */
8213 my_getExpression (&imm_expr, s);
8214 check_absolute_expr (ip, &imm_expr);
8215 if ((unsigned long) imm_expr.X_add_number > 31)
8216 {
8217 as_warn (_("Improper shift amount (%lu)"),
8218 (unsigned long) imm_expr.X_add_number);
8219 imm_expr.X_add_number &= OP_MASK_SHAMT;
8220 }
8221 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8222 imm_expr.X_op = O_absent;
8223 s = expr_end;
8224 continue;
8225
8226 case '>': /* shift amount minus 32 */
8227 my_getExpression (&imm_expr, s);
8228 check_absolute_expr (ip, &imm_expr);
8229 if ((unsigned long) imm_expr.X_add_number < 32
8230 || (unsigned long) imm_expr.X_add_number > 63)
8231 break;
8232 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8233 imm_expr.X_op = O_absent;
8234 s = expr_end;
8235 continue;
8236
8237 case 'k': /* cache code */
8238 case 'h': /* prefx code */
8239 my_getExpression (&imm_expr, s);
8240 check_absolute_expr (ip, &imm_expr);
8241 if ((unsigned long) imm_expr.X_add_number > 31)
8242 {
8243 as_warn (_("Invalid value for `%s' (%lu)"),
8244 ip->insn_mo->name,
8245 (unsigned long) imm_expr.X_add_number);
8246 imm_expr.X_add_number &= 0x1f;
8247 }
8248 if (*args == 'k')
8249 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8250 else
8251 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8252 imm_expr.X_op = O_absent;
8253 s = expr_end;
8254 continue;
8255
8256 case 'c': /* break code */
8257 my_getExpression (&imm_expr, s);
8258 check_absolute_expr (ip, &imm_expr);
8259 if ((unsigned long) imm_expr.X_add_number > 1023)
8260 {
8261 as_warn (_("Illegal break code (%lu)"),
8262 (unsigned long) imm_expr.X_add_number);
8263 imm_expr.X_add_number &= OP_MASK_CODE;
8264 }
8265 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8266 imm_expr.X_op = O_absent;
8267 s = expr_end;
8268 continue;
8269
8270 case 'q': /* lower break code */
8271 my_getExpression (&imm_expr, s);
8272 check_absolute_expr (ip, &imm_expr);
8273 if ((unsigned long) imm_expr.X_add_number > 1023)
8274 {
8275 as_warn (_("Illegal lower break code (%lu)"),
8276 (unsigned long) imm_expr.X_add_number);
8277 imm_expr.X_add_number &= OP_MASK_CODE2;
8278 }
8279 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8280 imm_expr.X_op = O_absent;
8281 s = expr_end;
8282 continue;
8283
8284 case 'B': /* 20-bit syscall/break code. */
8285 my_getExpression (&imm_expr, s);
8286 check_absolute_expr (ip, &imm_expr);
8287 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8288 as_warn (_("Illegal 20-bit code (%lu)"),
8289 (unsigned long) imm_expr.X_add_number);
8290 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8291 imm_expr.X_op = O_absent;
8292 s = expr_end;
8293 continue;
8294
8295 case 'C': /* Coprocessor code */
8296 my_getExpression (&imm_expr, s);
8297 check_absolute_expr (ip, &imm_expr);
8298 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8299 {
8300 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8301 (unsigned long) imm_expr.X_add_number);
8302 imm_expr.X_add_number &= ((1 << 25) - 1);
8303 }
8304 ip->insn_opcode |= imm_expr.X_add_number;
8305 imm_expr.X_op = O_absent;
8306 s = expr_end;
8307 continue;
8308
8309 case 'J': /* 19-bit wait code. */
8310 my_getExpression (&imm_expr, s);
8311 check_absolute_expr (ip, &imm_expr);
8312 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8313 as_warn (_("Illegal 19-bit code (%lu)"),
8314 (unsigned long) imm_expr.X_add_number);
8315 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8316 imm_expr.X_op = O_absent;
8317 s = expr_end;
8318 continue;
8319
8320 case 'P': /* Performance register */
8321 my_getExpression (&imm_expr, s);
8322 check_absolute_expr (ip, &imm_expr);
8323 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8324 {
8325 as_warn (_("Invalid performance register (%lu)"),
8326 (unsigned long) imm_expr.X_add_number);
8327 imm_expr.X_add_number &= OP_MASK_PERFREG;
8328 }
8329 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8330 imm_expr.X_op = O_absent;
8331 s = expr_end;
8332 continue;
8333
8334 case 'b': /* base register */
8335 case 'd': /* destination register */
8336 case 's': /* source register */
8337 case 't': /* target register */
8338 case 'r': /* both target and source */
8339 case 'v': /* both dest and source */
8340 case 'w': /* both dest and target */
8341 case 'E': /* coprocessor target register */
8342 case 'G': /* coprocessor destination register */
8343 case 'K': /* 'rdhwr' destination register */
8344 case 'x': /* ignore register name */
8345 case 'z': /* must be zero register */
8346 case 'U': /* destination register (clo/clz). */
8347 s_reset = s;
8348 if (s[0] == '$')
8349 {
8350
8351 if (ISDIGIT (s[1]))
8352 {
8353 ++s;
8354 regno = 0;
8355 do
8356 {
8357 regno *= 10;
8358 regno += *s - '0';
8359 ++s;
8360 }
8361 while (ISDIGIT (*s));
8362 if (regno > 31)
8363 as_bad (_("Invalid register number (%d)"), regno);
8364 }
8365 else if (*args == 'E' || *args == 'G' || *args == 'K')
8366 goto notreg;
8367 else
8368 {
8369 if (s[1] == 'r' && s[2] == 'a')
8370 {
8371 s += 3;
8372 regno = RA;
8373 }
8374 else if (s[1] == 'f' && s[2] == 'p')
8375 {
8376 s += 3;
8377 regno = FP;
8378 }
8379 else if (s[1] == 's' && s[2] == 'p')
8380 {
8381 s += 3;
8382 regno = SP;
8383 }
8384 else if (s[1] == 'g' && s[2] == 'p')
8385 {
8386 s += 3;
8387 regno = GP;
8388 }
8389 else if (s[1] == 'a' && s[2] == 't')
8390 {
8391 s += 3;
8392 regno = AT;
8393 }
8394 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8395 {
8396 s += 4;
8397 regno = KT0;
8398 }
8399 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8400 {
8401 s += 4;
8402 regno = KT1;
8403 }
8404 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8405 {
8406 s += 5;
8407 regno = ZERO;
8408 }
8409 else if (itbl_have_entries)
8410 {
8411 char *p, *n;
8412 unsigned long r;
8413
8414 p = s + 1; /* advance past '$' */
8415 n = itbl_get_field (&p); /* n is name */
8416
8417 /* See if this is a register defined in an
8418 itbl entry. */
8419 if (itbl_get_reg_val (n, &r))
8420 {
8421 /* Get_field advances to the start of
8422 the next field, so we need to back
8423 rack to the end of the last field. */
8424 if (p)
8425 s = p - 1;
8426 else
8427 s = strchr (s, '\0');
8428 regno = r;
8429 }
8430 else
8431 goto notreg;
8432 }
8433 else
8434 goto notreg;
8435 }
8436 if (regno == AT
8437 && ! mips_opts.noat
8438 && *args != 'E'
8439 && *args != 'G'
8440 && *args != 'K')
8441 as_warn (_("Used $at without \".set noat\""));
8442 c = *args;
8443 if (*s == ' ')
8444 ++s;
8445 if (args[1] != *s)
8446 {
8447 if (c == 'r' || c == 'v' || c == 'w')
8448 {
8449 regno = lastregno;
8450 s = s_reset;
8451 ++args;
8452 }
8453 }
8454 /* 'z' only matches $0. */
8455 if (c == 'z' && regno != 0)
8456 break;
8457
8458 /* Now that we have assembled one operand, we use the args string
8459 * to figure out where it goes in the instruction. */
8460 switch (c)
8461 {
8462 case 'r':
8463 case 's':
8464 case 'v':
8465 case 'b':
8466 ip->insn_opcode |= regno << OP_SH_RS;
8467 break;
8468 case 'd':
8469 case 'G':
8470 case 'K':
8471 ip->insn_opcode |= regno << OP_SH_RD;
8472 break;
8473 case 'U':
8474 ip->insn_opcode |= regno << OP_SH_RD;
8475 ip->insn_opcode |= regno << OP_SH_RT;
8476 break;
8477 case 'w':
8478 case 't':
8479 case 'E':
8480 ip->insn_opcode |= regno << OP_SH_RT;
8481 break;
8482 case 'x':
8483 /* This case exists because on the r3000 trunc
8484 expands into a macro which requires a gp
8485 register. On the r6000 or r4000 it is
8486 assembled into a single instruction which
8487 ignores the register. Thus the insn version
8488 is MIPS_ISA2 and uses 'x', and the macro
8489 version is MIPS_ISA1 and uses 't'. */
8490 break;
8491 case 'z':
8492 /* This case is for the div instruction, which
8493 acts differently if the destination argument
8494 is $0. This only matches $0, and is checked
8495 outside the switch. */
8496 break;
8497 case 'D':
8498 /* Itbl operand; not yet implemented. FIXME ?? */
8499 break;
8500 /* What about all other operands like 'i', which
8501 can be specified in the opcode table? */
8502 }
8503 lastregno = regno;
8504 continue;
8505 }
8506 notreg:
8507 switch (*args++)
8508 {
8509 case 'r':
8510 case 'v':
8511 ip->insn_opcode |= lastregno << OP_SH_RS;
8512 continue;
8513 case 'w':
8514 ip->insn_opcode |= lastregno << OP_SH_RT;
8515 continue;
8516 }
8517 break;
8518
8519 case 'O': /* MDMX alignment immediate constant. */
8520 my_getExpression (&imm_expr, s);
8521 check_absolute_expr (ip, &imm_expr);
8522 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8523 {
8524 as_warn ("Improper align amount (%ld), using low bits",
8525 (long) imm_expr.X_add_number);
8526 imm_expr.X_add_number &= OP_MASK_ALN;
8527 }
8528 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8529 imm_expr.X_op = O_absent;
8530 s = expr_end;
8531 continue;
8532
8533 case 'Q': /* MDMX vector, element sel, or const. */
8534 if (s[0] != '$')
8535 {
8536 /* MDMX Immediate. */
8537 my_getExpression (&imm_expr, s);
8538 check_absolute_expr (ip, &imm_expr);
8539 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8540 {
8541 as_warn (_("Invalid MDMX Immediate (%ld)"),
8542 (long) imm_expr.X_add_number);
8543 imm_expr.X_add_number &= OP_MASK_FT;
8544 }
8545 imm_expr.X_add_number &= OP_MASK_FT;
8546 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8547 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8548 else
8549 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8550 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8551 imm_expr.X_op = O_absent;
8552 s = expr_end;
8553 continue;
8554 }
8555 /* Not MDMX Immediate. Fall through. */
8556 case 'X': /* MDMX destination register. */
8557 case 'Y': /* MDMX source register. */
8558 case 'Z': /* MDMX target register. */
8559 is_mdmx = 1;
8560 case 'D': /* floating point destination register */
8561 case 'S': /* floating point source register */
8562 case 'T': /* floating point target register */
8563 case 'R': /* floating point source register */
8564 case 'V':
8565 case 'W':
8566 s_reset = s;
8567 /* Accept $fN for FP and MDMX register numbers, and in
8568 addition accept $vN for MDMX register numbers. */
8569 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8570 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8571 && ISDIGIT (s[2])))
8572 {
8573 s += 2;
8574 regno = 0;
8575 do
8576 {
8577 regno *= 10;
8578 regno += *s - '0';
8579 ++s;
8580 }
8581 while (ISDIGIT (*s));
8582
8583 if (regno > 31)
8584 as_bad (_("Invalid float register number (%d)"), regno);
8585
8586 if ((regno & 1) != 0
8587 && HAVE_32BIT_FPRS
8588 && ! (strcmp (str, "mtc1") == 0
8589 || strcmp (str, "mfc1") == 0
8590 || strcmp (str, "lwc1") == 0
8591 || strcmp (str, "swc1") == 0
8592 || strcmp (str, "l.s") == 0
8593 || strcmp (str, "s.s") == 0))
8594 as_warn (_("Float register should be even, was %d"),
8595 regno);
8596
8597 c = *args;
8598 if (*s == ' ')
8599 ++s;
8600 if (args[1] != *s)
8601 {
8602 if (c == 'V' || c == 'W')
8603 {
8604 regno = lastregno;
8605 s = s_reset;
8606 ++args;
8607 }
8608 }
8609 switch (c)
8610 {
8611 case 'D':
8612 case 'X':
8613 ip->insn_opcode |= regno << OP_SH_FD;
8614 break;
8615 case 'V':
8616 case 'S':
8617 case 'Y':
8618 ip->insn_opcode |= regno << OP_SH_FS;
8619 break;
8620 case 'Q':
8621 /* This is like 'Z', but also needs to fix the MDMX
8622 vector/scalar select bits. Note that the
8623 scalar immediate case is handled above. */
8624 if (*s == '[')
8625 {
8626 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8627 int max_el = (is_qh ? 3 : 7);
8628 s++;
8629 my_getExpression(&imm_expr, s);
8630 check_absolute_expr (ip, &imm_expr);
8631 s = expr_end;
8632 if (imm_expr.X_add_number > max_el)
8633 as_bad(_("Bad element selector %ld"),
8634 (long) imm_expr.X_add_number);
8635 imm_expr.X_add_number &= max_el;
8636 ip->insn_opcode |= (imm_expr.X_add_number
8637 << (OP_SH_VSEL +
8638 (is_qh ? 2 : 1)));
8639 imm_expr.X_op = O_absent;
8640 if (*s != ']')
8641 as_warn(_("Expecting ']' found '%s'"), s);
8642 else
8643 s++;
8644 }
8645 else
8646 {
8647 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8648 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8649 << OP_SH_VSEL);
8650 else
8651 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8652 OP_SH_VSEL);
8653 }
8654 /* Fall through */
8655 case 'W':
8656 case 'T':
8657 case 'Z':
8658 ip->insn_opcode |= regno << OP_SH_FT;
8659 break;
8660 case 'R':
8661 ip->insn_opcode |= regno << OP_SH_FR;
8662 break;
8663 }
8664 lastregno = regno;
8665 continue;
8666 }
8667
8668 switch (*args++)
8669 {
8670 case 'V':
8671 ip->insn_opcode |= lastregno << OP_SH_FS;
8672 continue;
8673 case 'W':
8674 ip->insn_opcode |= lastregno << OP_SH_FT;
8675 continue;
8676 }
8677 break;
8678
8679 case 'I':
8680 my_getExpression (&imm_expr, s);
8681 if (imm_expr.X_op != O_big
8682 && imm_expr.X_op != O_constant)
8683 insn_error = _("absolute expression required");
8684 normalize_constant_expr (&imm_expr);
8685 s = expr_end;
8686 continue;
8687
8688 case 'A':
8689 my_getExpression (&offset_expr, s);
8690 *imm_reloc = BFD_RELOC_32;
8691 s = expr_end;
8692 continue;
8693
8694 case 'F':
8695 case 'L':
8696 case 'f':
8697 case 'l':
8698 {
8699 int f64;
8700 int using_gprs;
8701 char *save_in;
8702 char *err;
8703 unsigned char temp[8];
8704 int len;
8705 unsigned int length;
8706 segT seg;
8707 subsegT subseg;
8708 char *p;
8709
8710 /* These only appear as the last operand in an
8711 instruction, and every instruction that accepts
8712 them in any variant accepts them in all variants.
8713 This means we don't have to worry about backing out
8714 any changes if the instruction does not match.
8715
8716 The difference between them is the size of the
8717 floating point constant and where it goes. For 'F'
8718 and 'L' the constant is 64 bits; for 'f' and 'l' it
8719 is 32 bits. Where the constant is placed is based
8720 on how the MIPS assembler does things:
8721 F -- .rdata
8722 L -- .lit8
8723 f -- immediate value
8724 l -- .lit4
8725
8726 The .lit4 and .lit8 sections are only used if
8727 permitted by the -G argument.
8728
8729 The code below needs to know whether the target register
8730 is 32 or 64 bits wide. It relies on the fact 'f' and
8731 'F' are used with GPR-based instructions and 'l' and
8732 'L' are used with FPR-based instructions. */
8733
8734 f64 = *args == 'F' || *args == 'L';
8735 using_gprs = *args == 'F' || *args == 'f';
8736
8737 save_in = input_line_pointer;
8738 input_line_pointer = s;
8739 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8740 length = len;
8741 s = input_line_pointer;
8742 input_line_pointer = save_in;
8743 if (err != NULL && *err != '\0')
8744 {
8745 as_bad (_("Bad floating point constant: %s"), err);
8746 memset (temp, '\0', sizeof temp);
8747 length = f64 ? 8 : 4;
8748 }
8749
8750 assert (length == (unsigned) (f64 ? 8 : 4));
8751
8752 if (*args == 'f'
8753 || (*args == 'l'
8754 && (g_switch_value < 4
8755 || (temp[0] == 0 && temp[1] == 0)
8756 || (temp[2] == 0 && temp[3] == 0))))
8757 {
8758 imm_expr.X_op = O_constant;
8759 if (! target_big_endian)
8760 imm_expr.X_add_number = bfd_getl32 (temp);
8761 else
8762 imm_expr.X_add_number = bfd_getb32 (temp);
8763 }
8764 else if (length > 4
8765 && ! mips_disable_float_construction
8766 /* Constants can only be constructed in GPRs and
8767 copied to FPRs if the GPRs are at least as wide
8768 as the FPRs. Force the constant into memory if
8769 we are using 64-bit FPRs but the GPRs are only
8770 32 bits wide. */
8771 && (using_gprs
8772 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8773 && ((temp[0] == 0 && temp[1] == 0)
8774 || (temp[2] == 0 && temp[3] == 0))
8775 && ((temp[4] == 0 && temp[5] == 0)
8776 || (temp[6] == 0 && temp[7] == 0)))
8777 {
8778 /* The value is simple enough to load with a couple of
8779 instructions. If using 32-bit registers, set
8780 imm_expr to the high order 32 bits and offset_expr to
8781 the low order 32 bits. Otherwise, set imm_expr to
8782 the entire 64 bit constant. */
8783 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8784 {
8785 imm_expr.X_op = O_constant;
8786 offset_expr.X_op = O_constant;
8787 if (! target_big_endian)
8788 {
8789 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8790 offset_expr.X_add_number = bfd_getl32 (temp);
8791 }
8792 else
8793 {
8794 imm_expr.X_add_number = bfd_getb32 (temp);
8795 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8796 }
8797 if (offset_expr.X_add_number == 0)
8798 offset_expr.X_op = O_absent;
8799 }
8800 else if (sizeof (imm_expr.X_add_number) > 4)
8801 {
8802 imm_expr.X_op = O_constant;
8803 if (! target_big_endian)
8804 imm_expr.X_add_number = bfd_getl64 (temp);
8805 else
8806 imm_expr.X_add_number = bfd_getb64 (temp);
8807 }
8808 else
8809 {
8810 imm_expr.X_op = O_big;
8811 imm_expr.X_add_number = 4;
8812 if (! target_big_endian)
8813 {
8814 generic_bignum[0] = bfd_getl16 (temp);
8815 generic_bignum[1] = bfd_getl16 (temp + 2);
8816 generic_bignum[2] = bfd_getl16 (temp + 4);
8817 generic_bignum[3] = bfd_getl16 (temp + 6);
8818 }
8819 else
8820 {
8821 generic_bignum[0] = bfd_getb16 (temp + 6);
8822 generic_bignum[1] = bfd_getb16 (temp + 4);
8823 generic_bignum[2] = bfd_getb16 (temp + 2);
8824 generic_bignum[3] = bfd_getb16 (temp);
8825 }
8826 }
8827 }
8828 else
8829 {
8830 const char *newname;
8831 segT new_seg;
8832
8833 /* Switch to the right section. */
8834 seg = now_seg;
8835 subseg = now_subseg;
8836 switch (*args)
8837 {
8838 default: /* unused default case avoids warnings. */
8839 case 'L':
8840 newname = RDATA_SECTION_NAME;
8841 if (g_switch_value >= 8)
8842 newname = ".lit8";
8843 break;
8844 case 'F':
8845 newname = RDATA_SECTION_NAME;
8846 break;
8847 case 'l':
8848 assert (g_switch_value >= 4);
8849 newname = ".lit4";
8850 break;
8851 }
8852 new_seg = subseg_new (newname, (subsegT) 0);
8853 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8854 bfd_set_section_flags (stdoutput, new_seg,
8855 (SEC_ALLOC
8856 | SEC_LOAD
8857 | SEC_READONLY
8858 | SEC_DATA));
8859 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8860 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8861 && strcmp (TARGET_OS, "elf") != 0)
8862 record_alignment (new_seg, 4);
8863 else
8864 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8865 if (seg == now_seg)
8866 as_bad (_("Can't use floating point insn in this section"));
8867
8868 /* Set the argument to the current address in the
8869 section. */
8870 offset_expr.X_op = O_symbol;
8871 offset_expr.X_add_symbol =
8872 symbol_new ("L0\001", now_seg,
8873 (valueT) frag_now_fix (), frag_now);
8874 offset_expr.X_add_number = 0;
8875
8876 /* Put the floating point number into the section. */
8877 p = frag_more ((int) length);
8878 memcpy (p, temp, length);
8879
8880 /* Switch back to the original section. */
8881 subseg_set (seg, subseg);
8882 }
8883 }
8884 continue;
8885
8886 case 'i': /* 16 bit unsigned immediate */
8887 case 'j': /* 16 bit signed immediate */
8888 *imm_reloc = BFD_RELOC_LO16;
8889 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8890 {
8891 int more;
8892 offsetT minval, maxval;
8893
8894 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8895 && strcmp (insn->name, insn[1].name) == 0);
8896
8897 /* If the expression was written as an unsigned number,
8898 only treat it as signed if there are no more
8899 alternatives. */
8900 if (more
8901 && *args == 'j'
8902 && sizeof (imm_expr.X_add_number) <= 4
8903 && imm_expr.X_op == O_constant
8904 && imm_expr.X_add_number < 0
8905 && imm_expr.X_unsigned
8906 && HAVE_64BIT_GPRS)
8907 break;
8908
8909 /* For compatibility with older assemblers, we accept
8910 0x8000-0xffff as signed 16-bit numbers when only
8911 signed numbers are allowed. */
8912 if (*args == 'i')
8913 minval = 0, maxval = 0xffff;
8914 else if (more)
8915 minval = -0x8000, maxval = 0x7fff;
8916 else
8917 minval = -0x8000, maxval = 0xffff;
8918
8919 if (imm_expr.X_op != O_constant
8920 || imm_expr.X_add_number < minval
8921 || imm_expr.X_add_number > maxval)
8922 {
8923 if (more)
8924 break;
8925 if (imm_expr.X_op == O_constant
8926 || imm_expr.X_op == O_big)
8927 as_bad (_("expression out of range"));
8928 }
8929 }
8930 s = expr_end;
8931 continue;
8932
8933 case 'o': /* 16 bit offset */
8934 /* Check whether there is only a single bracketed expression
8935 left. If so, it must be the base register and the
8936 constant must be zero. */
8937 if (*s == '(' && strchr (s + 1, '(') == 0)
8938 {
8939 offset_expr.X_op = O_constant;
8940 offset_expr.X_add_number = 0;
8941 continue;
8942 }
8943
8944 /* If this value won't fit into a 16 bit offset, then go
8945 find a macro that will generate the 32 bit offset
8946 code pattern. */
8947 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8948 && (offset_expr.X_op != O_constant
8949 || offset_expr.X_add_number >= 0x8000
8950 || offset_expr.X_add_number < -0x8000))
8951 break;
8952
8953 s = expr_end;
8954 continue;
8955
8956 case 'p': /* pc relative offset */
8957 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8958 my_getExpression (&offset_expr, s);
8959 s = expr_end;
8960 continue;
8961
8962 case 'u': /* upper 16 bits */
8963 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8964 && imm_expr.X_op == O_constant
8965 && (imm_expr.X_add_number < 0
8966 || imm_expr.X_add_number >= 0x10000))
8967 as_bad (_("lui expression not in range 0..65535"));
8968 s = expr_end;
8969 continue;
8970
8971 case 'a': /* 26 bit address */
8972 my_getExpression (&offset_expr, s);
8973 s = expr_end;
8974 *offset_reloc = BFD_RELOC_MIPS_JMP;
8975 continue;
8976
8977 case 'N': /* 3 bit branch condition code */
8978 case 'M': /* 3 bit compare condition code */
8979 if (strncmp (s, "$fcc", 4) != 0)
8980 break;
8981 s += 4;
8982 regno = 0;
8983 do
8984 {
8985 regno *= 10;
8986 regno += *s - '0';
8987 ++s;
8988 }
8989 while (ISDIGIT (*s));
8990 if (regno > 7)
8991 as_bad (_("Invalid condition code register $fcc%d"), regno);
8992 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8993 || strcmp(str + strlen(str) - 5, "any2f") == 0
8994 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8995 && (regno & 1) != 0)
8996 as_warn(_("Condition code register should be even for %s, was %d"),
8997 str, regno);
8998 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8999 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9000 && (regno & 3) != 0)
9001 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9002 str, regno);
9003 if (*args == 'N')
9004 ip->insn_opcode |= regno << OP_SH_BCC;
9005 else
9006 ip->insn_opcode |= regno << OP_SH_CCC;
9007 continue;
9008
9009 case 'H':
9010 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9011 s += 2;
9012 if (ISDIGIT (*s))
9013 {
9014 c = 0;
9015 do
9016 {
9017 c *= 10;
9018 c += *s - '0';
9019 ++s;
9020 }
9021 while (ISDIGIT (*s));
9022 }
9023 else
9024 c = 8; /* Invalid sel value. */
9025
9026 if (c > 7)
9027 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9028 ip->insn_opcode |= c;
9029 continue;
9030
9031 case 'e':
9032 /* Must be at least one digit. */
9033 my_getExpression (&imm_expr, s);
9034 check_absolute_expr (ip, &imm_expr);
9035
9036 if ((unsigned long) imm_expr.X_add_number
9037 > (unsigned long) OP_MASK_VECBYTE)
9038 {
9039 as_bad (_("bad byte vector index (%ld)"),
9040 (long) imm_expr.X_add_number);
9041 imm_expr.X_add_number = 0;
9042 }
9043
9044 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9045 imm_expr.X_op = O_absent;
9046 s = expr_end;
9047 continue;
9048
9049 case '%':
9050 my_getExpression (&imm_expr, s);
9051 check_absolute_expr (ip, &imm_expr);
9052
9053 if ((unsigned long) imm_expr.X_add_number
9054 > (unsigned long) OP_MASK_VECALIGN)
9055 {
9056 as_bad (_("bad byte vector index (%ld)"),
9057 (long) imm_expr.X_add_number);
9058 imm_expr.X_add_number = 0;
9059 }
9060
9061 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9062 imm_expr.X_op = O_absent;
9063 s = expr_end;
9064 continue;
9065
9066 default:
9067 as_bad (_("bad char = '%c'\n"), *args);
9068 internalError ();
9069 }
9070 break;
9071 }
9072 /* Args don't match. */
9073 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9074 !strcmp (insn->name, insn[1].name))
9075 {
9076 ++insn;
9077 s = argsStart;
9078 insn_error = _("illegal operands");
9079 continue;
9080 }
9081 if (save_c)
9082 *(--s) = save_c;
9083 insn_error = _("illegal operands");
9084 return;
9085 }
9086 }
9087
9088 /* This routine assembles an instruction into its binary format when
9089 assembling for the mips16. As a side effect, it sets one of the
9090 global variables imm_reloc or offset_reloc to the type of
9091 relocation to do if one of the operands is an address expression.
9092 It also sets mips16_small and mips16_ext if the user explicitly
9093 requested a small or extended instruction. */
9094
9095 static void
9096 mips16_ip (char *str, struct mips_cl_insn *ip)
9097 {
9098 char *s;
9099 const char *args;
9100 struct mips_opcode *insn;
9101 char *argsstart;
9102 unsigned int regno;
9103 unsigned int lastregno = 0;
9104 char *s_reset;
9105 size_t i;
9106
9107 insn_error = NULL;
9108
9109 mips16_small = FALSE;
9110 mips16_ext = FALSE;
9111
9112 for (s = str; ISLOWER (*s); ++s)
9113 ;
9114 switch (*s)
9115 {
9116 case '\0':
9117 break;
9118
9119 case ' ':
9120 *s++ = '\0';
9121 break;
9122
9123 case '.':
9124 if (s[1] == 't' && s[2] == ' ')
9125 {
9126 *s = '\0';
9127 mips16_small = TRUE;
9128 s += 3;
9129 break;
9130 }
9131 else if (s[1] == 'e' && s[2] == ' ')
9132 {
9133 *s = '\0';
9134 mips16_ext = TRUE;
9135 s += 3;
9136 break;
9137 }
9138 /* Fall through. */
9139 default:
9140 insn_error = _("unknown opcode");
9141 return;
9142 }
9143
9144 if (mips_opts.noautoextend && ! mips16_ext)
9145 mips16_small = TRUE;
9146
9147 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9148 {
9149 insn_error = _("unrecognized opcode");
9150 return;
9151 }
9152
9153 argsstart = s;
9154 for (;;)
9155 {
9156 assert (strcmp (insn->name, str) == 0);
9157
9158 ip->insn_mo = insn;
9159 ip->insn_opcode = insn->match;
9160 ip->use_extend = FALSE;
9161 imm_expr.X_op = O_absent;
9162 imm_reloc[0] = BFD_RELOC_UNUSED;
9163 imm_reloc[1] = BFD_RELOC_UNUSED;
9164 imm_reloc[2] = BFD_RELOC_UNUSED;
9165 imm2_expr.X_op = O_absent;
9166 offset_expr.X_op = O_absent;
9167 offset_reloc[0] = BFD_RELOC_UNUSED;
9168 offset_reloc[1] = BFD_RELOC_UNUSED;
9169 offset_reloc[2] = BFD_RELOC_UNUSED;
9170 for (args = insn->args; 1; ++args)
9171 {
9172 int c;
9173
9174 if (*s == ' ')
9175 ++s;
9176
9177 /* In this switch statement we call break if we did not find
9178 a match, continue if we did find a match, or return if we
9179 are done. */
9180
9181 c = *args;
9182 switch (c)
9183 {
9184 case '\0':
9185 if (*s == '\0')
9186 {
9187 /* Stuff the immediate value in now, if we can. */
9188 if (imm_expr.X_op == O_constant
9189 && *imm_reloc > BFD_RELOC_UNUSED
9190 && insn->pinfo != INSN_MACRO)
9191 {
9192 valueT tmp;
9193
9194 switch (*offset_reloc)
9195 {
9196 case BFD_RELOC_MIPS16_HI16_S:
9197 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9198 break;
9199
9200 case BFD_RELOC_MIPS16_HI16:
9201 tmp = imm_expr.X_add_number >> 16;
9202 break;
9203
9204 case BFD_RELOC_MIPS16_LO16:
9205 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9206 - 0x8000;
9207 break;
9208
9209 case BFD_RELOC_UNUSED:
9210 tmp = imm_expr.X_add_number;
9211 break;
9212
9213 default:
9214 internalError ();
9215 }
9216 *offset_reloc = BFD_RELOC_UNUSED;
9217
9218 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9219 tmp, TRUE, mips16_small,
9220 mips16_ext, &ip->insn_opcode,
9221 &ip->use_extend, &ip->extend);
9222 imm_expr.X_op = O_absent;
9223 *imm_reloc = BFD_RELOC_UNUSED;
9224 }
9225
9226 return;
9227 }
9228 break;
9229
9230 case ',':
9231 if (*s++ == c)
9232 continue;
9233 s--;
9234 switch (*++args)
9235 {
9236 case 'v':
9237 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9238 continue;
9239 case 'w':
9240 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9241 continue;
9242 }
9243 break;
9244
9245 case '(':
9246 case ')':
9247 if (*s++ == c)
9248 continue;
9249 break;
9250
9251 case 'v':
9252 case 'w':
9253 if (s[0] != '$')
9254 {
9255 if (c == 'v')
9256 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9257 else
9258 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9259 ++args;
9260 continue;
9261 }
9262 /* Fall through. */
9263 case 'x':
9264 case 'y':
9265 case 'z':
9266 case 'Z':
9267 case '0':
9268 case 'S':
9269 case 'R':
9270 case 'X':
9271 case 'Y':
9272 if (s[0] != '$')
9273 break;
9274 s_reset = s;
9275 if (ISDIGIT (s[1]))
9276 {
9277 ++s;
9278 regno = 0;
9279 do
9280 {
9281 regno *= 10;
9282 regno += *s - '0';
9283 ++s;
9284 }
9285 while (ISDIGIT (*s));
9286 if (regno > 31)
9287 {
9288 as_bad (_("invalid register number (%d)"), regno);
9289 regno = 2;
9290 }
9291 }
9292 else
9293 {
9294 if (s[1] == 'r' && s[2] == 'a')
9295 {
9296 s += 3;
9297 regno = RA;
9298 }
9299 else if (s[1] == 'f' && s[2] == 'p')
9300 {
9301 s += 3;
9302 regno = FP;
9303 }
9304 else if (s[1] == 's' && s[2] == 'p')
9305 {
9306 s += 3;
9307 regno = SP;
9308 }
9309 else if (s[1] == 'g' && s[2] == 'p')
9310 {
9311 s += 3;
9312 regno = GP;
9313 }
9314 else if (s[1] == 'a' && s[2] == 't')
9315 {
9316 s += 3;
9317 regno = AT;
9318 }
9319 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9320 {
9321 s += 4;
9322 regno = KT0;
9323 }
9324 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9325 {
9326 s += 4;
9327 regno = KT1;
9328 }
9329 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9330 {
9331 s += 5;
9332 regno = ZERO;
9333 }
9334 else
9335 break;
9336 }
9337
9338 if (*s == ' ')
9339 ++s;
9340 if (args[1] != *s)
9341 {
9342 if (c == 'v' || c == 'w')
9343 {
9344 regno = mips16_to_32_reg_map[lastregno];
9345 s = s_reset;
9346 ++args;
9347 }
9348 }
9349
9350 switch (c)
9351 {
9352 case 'x':
9353 case 'y':
9354 case 'z':
9355 case 'v':
9356 case 'w':
9357 case 'Z':
9358 regno = mips32_to_16_reg_map[regno];
9359 break;
9360
9361 case '0':
9362 if (regno != 0)
9363 regno = ILLEGAL_REG;
9364 break;
9365
9366 case 'S':
9367 if (regno != SP)
9368 regno = ILLEGAL_REG;
9369 break;
9370
9371 case 'R':
9372 if (regno != RA)
9373 regno = ILLEGAL_REG;
9374 break;
9375
9376 case 'X':
9377 case 'Y':
9378 if (regno == AT && ! mips_opts.noat)
9379 as_warn (_("used $at without \".set noat\""));
9380 break;
9381
9382 default:
9383 internalError ();
9384 }
9385
9386 if (regno == ILLEGAL_REG)
9387 break;
9388
9389 switch (c)
9390 {
9391 case 'x':
9392 case 'v':
9393 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9394 break;
9395 case 'y':
9396 case 'w':
9397 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9398 break;
9399 case 'z':
9400 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9401 break;
9402 case 'Z':
9403 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9404 case '0':
9405 case 'S':
9406 case 'R':
9407 break;
9408 case 'X':
9409 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9410 break;
9411 case 'Y':
9412 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9413 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9414 break;
9415 default:
9416 internalError ();
9417 }
9418
9419 lastregno = regno;
9420 continue;
9421
9422 case 'P':
9423 if (strncmp (s, "$pc", 3) == 0)
9424 {
9425 s += 3;
9426 continue;
9427 }
9428 break;
9429
9430 case '5':
9431 case 'H':
9432 case 'W':
9433 case 'D':
9434 case 'j':
9435 case 'V':
9436 case 'C':
9437 case 'U':
9438 case 'k':
9439 case 'K':
9440 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9441 if (i > 0)
9442 {
9443 if (imm_expr.X_op != O_constant)
9444 {
9445 mips16_ext = TRUE;
9446 ip->use_extend = TRUE;
9447 ip->extend = 0;
9448 }
9449 else
9450 {
9451 /* We need to relax this instruction. */
9452 *offset_reloc = *imm_reloc;
9453 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9454 }
9455 s = expr_end;
9456 continue;
9457 }
9458 *imm_reloc = BFD_RELOC_UNUSED;
9459 /* Fall through. */
9460 case '<':
9461 case '>':
9462 case '[':
9463 case ']':
9464 case '4':
9465 case '8':
9466 my_getExpression (&imm_expr, s);
9467 if (imm_expr.X_op == O_register)
9468 {
9469 /* What we thought was an expression turned out to
9470 be a register. */
9471
9472 if (s[0] == '(' && args[1] == '(')
9473 {
9474 /* It looks like the expression was omitted
9475 before a register indirection, which means
9476 that the expression is implicitly zero. We
9477 still set up imm_expr, so that we handle
9478 explicit extensions correctly. */
9479 imm_expr.X_op = O_constant;
9480 imm_expr.X_add_number = 0;
9481 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9482 continue;
9483 }
9484
9485 break;
9486 }
9487
9488 /* We need to relax this instruction. */
9489 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9490 s = expr_end;
9491 continue;
9492
9493 case 'p':
9494 case 'q':
9495 case 'A':
9496 case 'B':
9497 case 'E':
9498 /* We use offset_reloc rather than imm_reloc for the PC
9499 relative operands. This lets macros with both
9500 immediate and address operands work correctly. */
9501 my_getExpression (&offset_expr, s);
9502
9503 if (offset_expr.X_op == O_register)
9504 break;
9505
9506 /* We need to relax this instruction. */
9507 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9508 s = expr_end;
9509 continue;
9510
9511 case '6': /* break code */
9512 my_getExpression (&imm_expr, s);
9513 check_absolute_expr (ip, &imm_expr);
9514 if ((unsigned long) imm_expr.X_add_number > 63)
9515 {
9516 as_warn (_("Invalid value for `%s' (%lu)"),
9517 ip->insn_mo->name,
9518 (unsigned long) imm_expr.X_add_number);
9519 imm_expr.X_add_number &= 0x3f;
9520 }
9521 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9522 imm_expr.X_op = O_absent;
9523 s = expr_end;
9524 continue;
9525
9526 case 'a': /* 26 bit address */
9527 my_getExpression (&offset_expr, s);
9528 s = expr_end;
9529 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9530 ip->insn_opcode <<= 16;
9531 continue;
9532
9533 case 'l': /* register list for entry macro */
9534 case 'L': /* register list for exit macro */
9535 {
9536 int mask;
9537
9538 if (c == 'l')
9539 mask = 0;
9540 else
9541 mask = 7 << 3;
9542 while (*s != '\0')
9543 {
9544 int freg, reg1, reg2;
9545
9546 while (*s == ' ' || *s == ',')
9547 ++s;
9548 if (*s != '$')
9549 {
9550 as_bad (_("can't parse register list"));
9551 break;
9552 }
9553 ++s;
9554 if (*s != 'f')
9555 freg = 0;
9556 else
9557 {
9558 freg = 1;
9559 ++s;
9560 }
9561 reg1 = 0;
9562 while (ISDIGIT (*s))
9563 {
9564 reg1 *= 10;
9565 reg1 += *s - '0';
9566 ++s;
9567 }
9568 if (*s == ' ')
9569 ++s;
9570 if (*s != '-')
9571 reg2 = reg1;
9572 else
9573 {
9574 ++s;
9575 if (*s != '$')
9576 break;
9577 ++s;
9578 if (freg)
9579 {
9580 if (*s == 'f')
9581 ++s;
9582 else
9583 {
9584 as_bad (_("invalid register list"));
9585 break;
9586 }
9587 }
9588 reg2 = 0;
9589 while (ISDIGIT (*s))
9590 {
9591 reg2 *= 10;
9592 reg2 += *s - '0';
9593 ++s;
9594 }
9595 }
9596 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9597 {
9598 mask &= ~ (7 << 3);
9599 mask |= 5 << 3;
9600 }
9601 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9602 {
9603 mask &= ~ (7 << 3);
9604 mask |= 6 << 3;
9605 }
9606 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9607 mask |= (reg2 - 3) << 3;
9608 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9609 mask |= (reg2 - 15) << 1;
9610 else if (reg1 == RA && reg2 == RA)
9611 mask |= 1;
9612 else
9613 {
9614 as_bad (_("invalid register list"));
9615 break;
9616 }
9617 }
9618 /* The mask is filled in in the opcode table for the
9619 benefit of the disassembler. We remove it before
9620 applying the actual mask. */
9621 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9622 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9623 }
9624 continue;
9625
9626 case 'e': /* extend code */
9627 my_getExpression (&imm_expr, s);
9628 check_absolute_expr (ip, &imm_expr);
9629 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9630 {
9631 as_warn (_("Invalid value for `%s' (%lu)"),
9632 ip->insn_mo->name,
9633 (unsigned long) imm_expr.X_add_number);
9634 imm_expr.X_add_number &= 0x7ff;
9635 }
9636 ip->insn_opcode |= imm_expr.X_add_number;
9637 imm_expr.X_op = O_absent;
9638 s = expr_end;
9639 continue;
9640
9641 default:
9642 internalError ();
9643 }
9644 break;
9645 }
9646
9647 /* Args don't match. */
9648 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9649 strcmp (insn->name, insn[1].name) == 0)
9650 {
9651 ++insn;
9652 s = argsstart;
9653 continue;
9654 }
9655
9656 insn_error = _("illegal operands");
9657
9658 return;
9659 }
9660 }
9661
9662 /* This structure holds information we know about a mips16 immediate
9663 argument type. */
9664
9665 struct mips16_immed_operand
9666 {
9667 /* The type code used in the argument string in the opcode table. */
9668 int type;
9669 /* The number of bits in the short form of the opcode. */
9670 int nbits;
9671 /* The number of bits in the extended form of the opcode. */
9672 int extbits;
9673 /* The amount by which the short form is shifted when it is used;
9674 for example, the sw instruction has a shift count of 2. */
9675 int shift;
9676 /* The amount by which the short form is shifted when it is stored
9677 into the instruction code. */
9678 int op_shift;
9679 /* Non-zero if the short form is unsigned. */
9680 int unsp;
9681 /* Non-zero if the extended form is unsigned. */
9682 int extu;
9683 /* Non-zero if the value is PC relative. */
9684 int pcrel;
9685 };
9686
9687 /* The mips16 immediate operand types. */
9688
9689 static const struct mips16_immed_operand mips16_immed_operands[] =
9690 {
9691 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9692 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9693 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9694 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9695 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9696 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9697 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9698 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9699 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9700 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9701 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9702 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9703 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9704 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9705 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9706 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9707 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9708 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9709 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9710 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9711 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9712 };
9713
9714 #define MIPS16_NUM_IMMED \
9715 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9716
9717 /* Handle a mips16 instruction with an immediate value. This or's the
9718 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9719 whether an extended value is needed; if one is needed, it sets
9720 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9721 If SMALL is true, an unextended opcode was explicitly requested.
9722 If EXT is true, an extended opcode was explicitly requested. If
9723 WARN is true, warn if EXT does not match reality. */
9724
9725 static void
9726 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9727 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9728 unsigned long *insn, bfd_boolean *use_extend,
9729 unsigned short *extend)
9730 {
9731 register const struct mips16_immed_operand *op;
9732 int mintiny, maxtiny;
9733 bfd_boolean needext;
9734
9735 op = mips16_immed_operands;
9736 while (op->type != type)
9737 {
9738 ++op;
9739 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9740 }
9741
9742 if (op->unsp)
9743 {
9744 if (type == '<' || type == '>' || type == '[' || type == ']')
9745 {
9746 mintiny = 1;
9747 maxtiny = 1 << op->nbits;
9748 }
9749 else
9750 {
9751 mintiny = 0;
9752 maxtiny = (1 << op->nbits) - 1;
9753 }
9754 }
9755 else
9756 {
9757 mintiny = - (1 << (op->nbits - 1));
9758 maxtiny = (1 << (op->nbits - 1)) - 1;
9759 }
9760
9761 /* Branch offsets have an implicit 0 in the lowest bit. */
9762 if (type == 'p' || type == 'q')
9763 val /= 2;
9764
9765 if ((val & ((1 << op->shift) - 1)) != 0
9766 || val < (mintiny << op->shift)
9767 || val > (maxtiny << op->shift))
9768 needext = TRUE;
9769 else
9770 needext = FALSE;
9771
9772 if (warn && ext && ! needext)
9773 as_warn_where (file, line,
9774 _("extended operand requested but not required"));
9775 if (small && needext)
9776 as_bad_where (file, line, _("invalid unextended operand value"));
9777
9778 if (small || (! ext && ! needext))
9779 {
9780 int insnval;
9781
9782 *use_extend = FALSE;
9783 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9784 insnval <<= op->op_shift;
9785 *insn |= insnval;
9786 }
9787 else
9788 {
9789 long minext, maxext;
9790 int extval;
9791
9792 if (op->extu)
9793 {
9794 minext = 0;
9795 maxext = (1 << op->extbits) - 1;
9796 }
9797 else
9798 {
9799 minext = - (1 << (op->extbits - 1));
9800 maxext = (1 << (op->extbits - 1)) - 1;
9801 }
9802 if (val < minext || val > maxext)
9803 as_bad_where (file, line,
9804 _("operand value out of range for instruction"));
9805
9806 *use_extend = TRUE;
9807 if (op->extbits == 16)
9808 {
9809 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9810 val &= 0x1f;
9811 }
9812 else if (op->extbits == 15)
9813 {
9814 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9815 val &= 0xf;
9816 }
9817 else
9818 {
9819 extval = ((val & 0x1f) << 6) | (val & 0x20);
9820 val = 0;
9821 }
9822
9823 *extend = (unsigned short) extval;
9824 *insn |= val;
9825 }
9826 }
9827 \f
9828 struct percent_op_match
9829 {
9830 const char *str;
9831 bfd_reloc_code_real_type reloc;
9832 };
9833
9834 static const struct percent_op_match mips_percent_op[] =
9835 {
9836 {"%lo", BFD_RELOC_LO16},
9837 #ifdef OBJ_ELF
9838 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9839 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9840 {"%call16", BFD_RELOC_MIPS_CALL16},
9841 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9842 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9843 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9844 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9845 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9846 {"%got", BFD_RELOC_MIPS_GOT16},
9847 {"%gp_rel", BFD_RELOC_GPREL16},
9848 {"%half", BFD_RELOC_16},
9849 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9850 {"%higher", BFD_RELOC_MIPS_HIGHER},
9851 {"%neg", BFD_RELOC_MIPS_SUB},
9852 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9853 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9854 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9855 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9856 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9857 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9858 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9859 #endif
9860 {"%hi", BFD_RELOC_HI16_S}
9861 };
9862
9863 static const struct percent_op_match mips16_percent_op[] =
9864 {
9865 {"%lo", BFD_RELOC_MIPS16_LO16},
9866 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9867 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9868 };
9869
9870
9871 /* Return true if *STR points to a relocation operator. When returning true,
9872 move *STR over the operator and store its relocation code in *RELOC.
9873 Leave both *STR and *RELOC alone when returning false. */
9874
9875 static bfd_boolean
9876 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9877 {
9878 const struct percent_op_match *percent_op;
9879 size_t limit, i;
9880
9881 if (mips_opts.mips16)
9882 {
9883 percent_op = mips16_percent_op;
9884 limit = ARRAY_SIZE (mips16_percent_op);
9885 }
9886 else
9887 {
9888 percent_op = mips_percent_op;
9889 limit = ARRAY_SIZE (mips_percent_op);
9890 }
9891
9892 for (i = 0; i < limit; i++)
9893 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9894 {
9895 int len = strlen (percent_op[i].str);
9896
9897 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9898 continue;
9899
9900 *str += strlen (percent_op[i].str);
9901 *reloc = percent_op[i].reloc;
9902
9903 /* Check whether the output BFD supports this relocation.
9904 If not, issue an error and fall back on something safe. */
9905 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9906 {
9907 as_bad ("relocation %s isn't supported by the current ABI",
9908 percent_op[i].str);
9909 *reloc = BFD_RELOC_UNUSED;
9910 }
9911 return TRUE;
9912 }
9913 return FALSE;
9914 }
9915
9916
9917 /* Parse string STR as a 16-bit relocatable operand. Store the
9918 expression in *EP and the relocations in the array starting
9919 at RELOC. Return the number of relocation operators used.
9920
9921 On exit, EXPR_END points to the first character after the expression. */
9922
9923 static size_t
9924 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9925 char *str)
9926 {
9927 bfd_reloc_code_real_type reversed_reloc[3];
9928 size_t reloc_index, i;
9929 int crux_depth, str_depth;
9930 char *crux;
9931
9932 /* Search for the start of the main expression, recoding relocations
9933 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9934 of the main expression and with CRUX_DEPTH containing the number
9935 of open brackets at that point. */
9936 reloc_index = -1;
9937 str_depth = 0;
9938 do
9939 {
9940 reloc_index++;
9941 crux = str;
9942 crux_depth = str_depth;
9943
9944 /* Skip over whitespace and brackets, keeping count of the number
9945 of brackets. */
9946 while (*str == ' ' || *str == '\t' || *str == '(')
9947 if (*str++ == '(')
9948 str_depth++;
9949 }
9950 while (*str == '%'
9951 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9952 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9953
9954 my_getExpression (ep, crux);
9955 str = expr_end;
9956
9957 /* Match every open bracket. */
9958 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9959 if (*str++ == ')')
9960 crux_depth--;
9961
9962 if (crux_depth > 0)
9963 as_bad ("unclosed '('");
9964
9965 expr_end = str;
9966
9967 if (reloc_index != 0)
9968 {
9969 prev_reloc_op_frag = frag_now;
9970 for (i = 0; i < reloc_index; i++)
9971 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9972 }
9973
9974 return reloc_index;
9975 }
9976
9977 static void
9978 my_getExpression (expressionS *ep, char *str)
9979 {
9980 char *save_in;
9981 valueT val;
9982
9983 save_in = input_line_pointer;
9984 input_line_pointer = str;
9985 expression (ep);
9986 expr_end = input_line_pointer;
9987 input_line_pointer = save_in;
9988
9989 /* If we are in mips16 mode, and this is an expression based on `.',
9990 then we bump the value of the symbol by 1 since that is how other
9991 text symbols are handled. We don't bother to handle complex
9992 expressions, just `.' plus or minus a constant. */
9993 if (mips_opts.mips16
9994 && ep->X_op == O_symbol
9995 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9996 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9997 && symbol_get_frag (ep->X_add_symbol) == frag_now
9998 && symbol_constant_p (ep->X_add_symbol)
9999 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10000 S_SET_VALUE (ep->X_add_symbol, val + 1);
10001 }
10002
10003 /* Turn a string in input_line_pointer into a floating point constant
10004 of type TYPE, and store the appropriate bytes in *LITP. The number
10005 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10006 returned, or NULL on OK. */
10007
10008 char *
10009 md_atof (int type, char *litP, int *sizeP)
10010 {
10011 int prec;
10012 LITTLENUM_TYPE words[4];
10013 char *t;
10014 int i;
10015
10016 switch (type)
10017 {
10018 case 'f':
10019 prec = 2;
10020 break;
10021
10022 case 'd':
10023 prec = 4;
10024 break;
10025
10026 default:
10027 *sizeP = 0;
10028 return _("bad call to md_atof");
10029 }
10030
10031 t = atof_ieee (input_line_pointer, type, words);
10032 if (t)
10033 input_line_pointer = t;
10034
10035 *sizeP = prec * 2;
10036
10037 if (! target_big_endian)
10038 {
10039 for (i = prec - 1; i >= 0; i--)
10040 {
10041 md_number_to_chars (litP, words[i], 2);
10042 litP += 2;
10043 }
10044 }
10045 else
10046 {
10047 for (i = 0; i < prec; i++)
10048 {
10049 md_number_to_chars (litP, words[i], 2);
10050 litP += 2;
10051 }
10052 }
10053
10054 return NULL;
10055 }
10056
10057 void
10058 md_number_to_chars (char *buf, valueT val, int n)
10059 {
10060 if (target_big_endian)
10061 number_to_chars_bigendian (buf, val, n);
10062 else
10063 number_to_chars_littleendian (buf, val, n);
10064 }
10065 \f
10066 #ifdef OBJ_ELF
10067 static int support_64bit_objects(void)
10068 {
10069 const char **list, **l;
10070 int yes;
10071
10072 list = bfd_target_list ();
10073 for (l = list; *l != NULL; l++)
10074 #ifdef TE_TMIPS
10075 /* This is traditional mips */
10076 if (strcmp (*l, "elf64-tradbigmips") == 0
10077 || strcmp (*l, "elf64-tradlittlemips") == 0)
10078 #else
10079 if (strcmp (*l, "elf64-bigmips") == 0
10080 || strcmp (*l, "elf64-littlemips") == 0)
10081 #endif
10082 break;
10083 yes = (*l != NULL);
10084 free (list);
10085 return yes;
10086 }
10087 #endif /* OBJ_ELF */
10088
10089 const char *md_shortopts = "O::g::G:";
10090
10091 struct option md_longopts[] =
10092 {
10093 /* Options which specify architecture. */
10094 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10095 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10096 {"march", required_argument, NULL, OPTION_MARCH},
10097 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10098 {"mtune", required_argument, NULL, OPTION_MTUNE},
10099 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10100 {"mips0", no_argument, NULL, OPTION_MIPS1},
10101 {"mips1", no_argument, NULL, OPTION_MIPS1},
10102 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10103 {"mips2", no_argument, NULL, OPTION_MIPS2},
10104 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10105 {"mips3", no_argument, NULL, OPTION_MIPS3},
10106 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10107 {"mips4", no_argument, NULL, OPTION_MIPS4},
10108 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10109 {"mips5", no_argument, NULL, OPTION_MIPS5},
10110 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10111 {"mips32", no_argument, NULL, OPTION_MIPS32},
10112 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10113 {"mips64", no_argument, NULL, OPTION_MIPS64},
10114 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10115 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10116 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10117 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10118
10119 /* Options which specify Application Specific Extensions (ASEs). */
10120 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10121 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10122 {"mips16", no_argument, NULL, OPTION_MIPS16},
10123 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10124 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10125 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10126 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10127 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10128 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10129 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10130 {"mdmx", no_argument, NULL, OPTION_MDMX},
10131 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10132 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10133
10134 /* Old-style architecture options. Don't add more of these. */
10135 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10136 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10137 {"m4650", no_argument, NULL, OPTION_M4650},
10138 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10139 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10140 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10141 {"m4010", no_argument, NULL, OPTION_M4010},
10142 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10143 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10144 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10145 {"m4100", no_argument, NULL, OPTION_M4100},
10146 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10147 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10148 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10149 {"m3900", no_argument, NULL, OPTION_M3900},
10150 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10151 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10152
10153 /* Options which enable bug fixes. */
10154 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10155 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10156 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10157 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10158 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10159 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10160 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10161 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10162 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10163 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10164
10165 /* Miscellaneous options. */
10166 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10167 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10168 {"trap", no_argument, NULL, OPTION_TRAP},
10169 {"no-break", no_argument, NULL, OPTION_TRAP},
10170 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10171 {"break", no_argument, NULL, OPTION_BREAK},
10172 {"no-trap", no_argument, NULL, OPTION_BREAK},
10173 #define OPTION_EB (OPTION_MISC_BASE + 2)
10174 {"EB", no_argument, NULL, OPTION_EB},
10175 #define OPTION_EL (OPTION_MISC_BASE + 3)
10176 {"EL", no_argument, NULL, OPTION_EL},
10177 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10178 {"mfp32", no_argument, NULL, OPTION_FP32},
10179 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10180 {"mgp32", no_argument, NULL, OPTION_GP32},
10181 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10182 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10183 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10184 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10185 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10186 {"mfp64", no_argument, NULL, OPTION_FP64},
10187 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10188 {"mgp64", no_argument, NULL, OPTION_GP64},
10189 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10190 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10191 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10192 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10193 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10194 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10195 {"mshared", no_argument, NULL, OPTION_MSHARED},
10196 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10197 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10198 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10199 {"msym32", no_argument, NULL, OPTION_MSYM32},
10200 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10201
10202 /* ELF-specific options. */
10203 #ifdef OBJ_ELF
10204 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10205 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10206 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10207 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10208 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10209 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10210 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10211 {"xgot", no_argument, NULL, OPTION_XGOT},
10212 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10213 {"mabi", required_argument, NULL, OPTION_MABI},
10214 #define OPTION_32 (OPTION_ELF_BASE + 4)
10215 {"32", no_argument, NULL, OPTION_32},
10216 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10217 {"n32", no_argument, NULL, OPTION_N32},
10218 #define OPTION_64 (OPTION_ELF_BASE + 6)
10219 {"64", no_argument, NULL, OPTION_64},
10220 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10221 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10222 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10223 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10224 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10225 {"mpdr", no_argument, NULL, OPTION_PDR},
10226 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10227 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10228 #endif /* OBJ_ELF */
10229
10230 {NULL, no_argument, NULL, 0}
10231 };
10232 size_t md_longopts_size = sizeof (md_longopts);
10233
10234 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10235 NEW_VALUE. Warn if another value was already specified. Note:
10236 we have to defer parsing the -march and -mtune arguments in order
10237 to handle 'from-abi' correctly, since the ABI might be specified
10238 in a later argument. */
10239
10240 static void
10241 mips_set_option_string (const char **string_ptr, const char *new_value)
10242 {
10243 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10244 as_warn (_("A different %s was already specified, is now %s"),
10245 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10246 new_value);
10247
10248 *string_ptr = new_value;
10249 }
10250
10251 int
10252 md_parse_option (int c, char *arg)
10253 {
10254 switch (c)
10255 {
10256 case OPTION_CONSTRUCT_FLOATS:
10257 mips_disable_float_construction = 0;
10258 break;
10259
10260 case OPTION_NO_CONSTRUCT_FLOATS:
10261 mips_disable_float_construction = 1;
10262 break;
10263
10264 case OPTION_TRAP:
10265 mips_trap = 1;
10266 break;
10267
10268 case OPTION_BREAK:
10269 mips_trap = 0;
10270 break;
10271
10272 case OPTION_EB:
10273 target_big_endian = 1;
10274 break;
10275
10276 case OPTION_EL:
10277 target_big_endian = 0;
10278 break;
10279
10280 case 'O':
10281 if (arg && arg[1] == '0')
10282 mips_optimize = 1;
10283 else
10284 mips_optimize = 2;
10285 break;
10286
10287 case 'g':
10288 if (arg == NULL)
10289 mips_debug = 2;
10290 else
10291 mips_debug = atoi (arg);
10292 /* When the MIPS assembler sees -g or -g2, it does not do
10293 optimizations which limit full symbolic debugging. We take
10294 that to be equivalent to -O0. */
10295 if (mips_debug == 2)
10296 mips_optimize = 1;
10297 break;
10298
10299 case OPTION_MIPS1:
10300 file_mips_isa = ISA_MIPS1;
10301 break;
10302
10303 case OPTION_MIPS2:
10304 file_mips_isa = ISA_MIPS2;
10305 break;
10306
10307 case OPTION_MIPS3:
10308 file_mips_isa = ISA_MIPS3;
10309 break;
10310
10311 case OPTION_MIPS4:
10312 file_mips_isa = ISA_MIPS4;
10313 break;
10314
10315 case OPTION_MIPS5:
10316 file_mips_isa = ISA_MIPS5;
10317 break;
10318
10319 case OPTION_MIPS32:
10320 file_mips_isa = ISA_MIPS32;
10321 break;
10322
10323 case OPTION_MIPS32R2:
10324 file_mips_isa = ISA_MIPS32R2;
10325 break;
10326
10327 case OPTION_MIPS64R2:
10328 file_mips_isa = ISA_MIPS64R2;
10329 break;
10330
10331 case OPTION_MIPS64:
10332 file_mips_isa = ISA_MIPS64;
10333 break;
10334
10335 case OPTION_MTUNE:
10336 mips_set_option_string (&mips_tune_string, arg);
10337 break;
10338
10339 case OPTION_MARCH:
10340 mips_set_option_string (&mips_arch_string, arg);
10341 break;
10342
10343 case OPTION_M4650:
10344 mips_set_option_string (&mips_arch_string, "4650");
10345 mips_set_option_string (&mips_tune_string, "4650");
10346 break;
10347
10348 case OPTION_NO_M4650:
10349 break;
10350
10351 case OPTION_M4010:
10352 mips_set_option_string (&mips_arch_string, "4010");
10353 mips_set_option_string (&mips_tune_string, "4010");
10354 break;
10355
10356 case OPTION_NO_M4010:
10357 break;
10358
10359 case OPTION_M4100:
10360 mips_set_option_string (&mips_arch_string, "4100");
10361 mips_set_option_string (&mips_tune_string, "4100");
10362 break;
10363
10364 case OPTION_NO_M4100:
10365 break;
10366
10367 case OPTION_M3900:
10368 mips_set_option_string (&mips_arch_string, "3900");
10369 mips_set_option_string (&mips_tune_string, "3900");
10370 break;
10371
10372 case OPTION_NO_M3900:
10373 break;
10374
10375 case OPTION_MDMX:
10376 mips_opts.ase_mdmx = 1;
10377 break;
10378
10379 case OPTION_NO_MDMX:
10380 mips_opts.ase_mdmx = 0;
10381 break;
10382
10383 case OPTION_MIPS16:
10384 mips_opts.mips16 = 1;
10385 mips_no_prev_insn (FALSE);
10386 break;
10387
10388 case OPTION_NO_MIPS16:
10389 mips_opts.mips16 = 0;
10390 mips_no_prev_insn (FALSE);
10391 break;
10392
10393 case OPTION_MIPS3D:
10394 mips_opts.ase_mips3d = 1;
10395 break;
10396
10397 case OPTION_NO_MIPS3D:
10398 mips_opts.ase_mips3d = 0;
10399 break;
10400
10401 case OPTION_FIX_VR4120:
10402 mips_fix_vr4120 = 1;
10403 break;
10404
10405 case OPTION_NO_FIX_VR4120:
10406 mips_fix_vr4120 = 0;
10407 break;
10408
10409 case OPTION_RELAX_BRANCH:
10410 mips_relax_branch = 1;
10411 break;
10412
10413 case OPTION_NO_RELAX_BRANCH:
10414 mips_relax_branch = 0;
10415 break;
10416
10417 case OPTION_MSHARED:
10418 mips_in_shared = TRUE;
10419 break;
10420
10421 case OPTION_MNO_SHARED:
10422 mips_in_shared = FALSE;
10423 break;
10424
10425 case OPTION_MSYM32:
10426 mips_opts.sym32 = TRUE;
10427 break;
10428
10429 case OPTION_MNO_SYM32:
10430 mips_opts.sym32 = FALSE;
10431 break;
10432
10433 #ifdef OBJ_ELF
10434 /* When generating ELF code, we permit -KPIC and -call_shared to
10435 select SVR4_PIC, and -non_shared to select no PIC. This is
10436 intended to be compatible with Irix 5. */
10437 case OPTION_CALL_SHARED:
10438 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10439 {
10440 as_bad (_("-call_shared is supported only for ELF format"));
10441 return 0;
10442 }
10443 mips_pic = SVR4_PIC;
10444 mips_abicalls = TRUE;
10445 if (g_switch_seen && g_switch_value != 0)
10446 {
10447 as_bad (_("-G may not be used with SVR4 PIC code"));
10448 return 0;
10449 }
10450 g_switch_value = 0;
10451 break;
10452
10453 case OPTION_NON_SHARED:
10454 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10455 {
10456 as_bad (_("-non_shared is supported only for ELF format"));
10457 return 0;
10458 }
10459 mips_pic = NO_PIC;
10460 mips_abicalls = FALSE;
10461 break;
10462
10463 /* The -xgot option tells the assembler to use 32 offsets when
10464 accessing the got in SVR4_PIC mode. It is for Irix
10465 compatibility. */
10466 case OPTION_XGOT:
10467 mips_big_got = 1;
10468 break;
10469 #endif /* OBJ_ELF */
10470
10471 case 'G':
10472 g_switch_value = atoi (arg);
10473 g_switch_seen = 1;
10474 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10475 {
10476 as_bad (_("-G may not be used with SVR4 PIC code"));
10477 return 0;
10478 }
10479 break;
10480
10481 #ifdef OBJ_ELF
10482 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10483 and -mabi=64. */
10484 case OPTION_32:
10485 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10486 {
10487 as_bad (_("-32 is supported for ELF format only"));
10488 return 0;
10489 }
10490 mips_abi = O32_ABI;
10491 break;
10492
10493 case OPTION_N32:
10494 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10495 {
10496 as_bad (_("-n32 is supported for ELF format only"));
10497 return 0;
10498 }
10499 mips_abi = N32_ABI;
10500 break;
10501
10502 case OPTION_64:
10503 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10504 {
10505 as_bad (_("-64 is supported for ELF format only"));
10506 return 0;
10507 }
10508 mips_abi = N64_ABI;
10509 if (! support_64bit_objects())
10510 as_fatal (_("No compiled in support for 64 bit object file format"));
10511 break;
10512 #endif /* OBJ_ELF */
10513
10514 case OPTION_GP32:
10515 file_mips_gp32 = 1;
10516 break;
10517
10518 case OPTION_GP64:
10519 file_mips_gp32 = 0;
10520 break;
10521
10522 case OPTION_FP32:
10523 file_mips_fp32 = 1;
10524 break;
10525
10526 case OPTION_FP64:
10527 file_mips_fp32 = 0;
10528 break;
10529
10530 #ifdef OBJ_ELF
10531 case OPTION_MABI:
10532 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10533 {
10534 as_bad (_("-mabi is supported for ELF format only"));
10535 return 0;
10536 }
10537 if (strcmp (arg, "32") == 0)
10538 mips_abi = O32_ABI;
10539 else if (strcmp (arg, "o64") == 0)
10540 mips_abi = O64_ABI;
10541 else if (strcmp (arg, "n32") == 0)
10542 mips_abi = N32_ABI;
10543 else if (strcmp (arg, "64") == 0)
10544 {
10545 mips_abi = N64_ABI;
10546 if (! support_64bit_objects())
10547 as_fatal (_("No compiled in support for 64 bit object file "
10548 "format"));
10549 }
10550 else if (strcmp (arg, "eabi") == 0)
10551 mips_abi = EABI_ABI;
10552 else
10553 {
10554 as_fatal (_("invalid abi -mabi=%s"), arg);
10555 return 0;
10556 }
10557 break;
10558 #endif /* OBJ_ELF */
10559
10560 case OPTION_M7000_HILO_FIX:
10561 mips_7000_hilo_fix = TRUE;
10562 break;
10563
10564 case OPTION_MNO_7000_HILO_FIX:
10565 mips_7000_hilo_fix = FALSE;
10566 break;
10567
10568 #ifdef OBJ_ELF
10569 case OPTION_MDEBUG:
10570 mips_flag_mdebug = TRUE;
10571 break;
10572
10573 case OPTION_NO_MDEBUG:
10574 mips_flag_mdebug = FALSE;
10575 break;
10576
10577 case OPTION_PDR:
10578 mips_flag_pdr = TRUE;
10579 break;
10580
10581 case OPTION_NO_PDR:
10582 mips_flag_pdr = FALSE;
10583 break;
10584 #endif /* OBJ_ELF */
10585
10586 default:
10587 return 0;
10588 }
10589
10590 return 1;
10591 }
10592 \f
10593 /* Set up globals to generate code for the ISA or processor
10594 described by INFO. */
10595
10596 static void
10597 mips_set_architecture (const struct mips_cpu_info *info)
10598 {
10599 if (info != 0)
10600 {
10601 file_mips_arch = info->cpu;
10602 mips_opts.arch = info->cpu;
10603 mips_opts.isa = info->isa;
10604 }
10605 }
10606
10607
10608 /* Likewise for tuning. */
10609
10610 static void
10611 mips_set_tune (const struct mips_cpu_info *info)
10612 {
10613 if (info != 0)
10614 mips_tune = info->cpu;
10615 }
10616
10617
10618 void
10619 mips_after_parse_args (void)
10620 {
10621 const struct mips_cpu_info *arch_info = 0;
10622 const struct mips_cpu_info *tune_info = 0;
10623
10624 /* GP relative stuff not working for PE */
10625 if (strncmp (TARGET_OS, "pe", 2) == 0)
10626 {
10627 if (g_switch_seen && g_switch_value != 0)
10628 as_bad (_("-G not supported in this configuration."));
10629 g_switch_value = 0;
10630 }
10631
10632 if (mips_abi == NO_ABI)
10633 mips_abi = MIPS_DEFAULT_ABI;
10634
10635 /* The following code determines the architecture and register size.
10636 Similar code was added to GCC 3.3 (see override_options() in
10637 config/mips/mips.c). The GAS and GCC code should be kept in sync
10638 as much as possible. */
10639
10640 if (mips_arch_string != 0)
10641 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10642
10643 if (file_mips_isa != ISA_UNKNOWN)
10644 {
10645 /* Handle -mipsN. At this point, file_mips_isa contains the
10646 ISA level specified by -mipsN, while arch_info->isa contains
10647 the -march selection (if any). */
10648 if (arch_info != 0)
10649 {
10650 /* -march takes precedence over -mipsN, since it is more descriptive.
10651 There's no harm in specifying both as long as the ISA levels
10652 are the same. */
10653 if (file_mips_isa != arch_info->isa)
10654 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10655 mips_cpu_info_from_isa (file_mips_isa)->name,
10656 mips_cpu_info_from_isa (arch_info->isa)->name);
10657 }
10658 else
10659 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10660 }
10661
10662 if (arch_info == 0)
10663 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10664
10665 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10666 as_bad ("-march=%s is not compatible with the selected ABI",
10667 arch_info->name);
10668
10669 mips_set_architecture (arch_info);
10670
10671 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10672 if (mips_tune_string != 0)
10673 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10674
10675 if (tune_info == 0)
10676 mips_set_tune (arch_info);
10677 else
10678 mips_set_tune (tune_info);
10679
10680 if (file_mips_gp32 >= 0)
10681 {
10682 /* The user specified the size of the integer registers. Make sure
10683 it agrees with the ABI and ISA. */
10684 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10685 as_bad (_("-mgp64 used with a 32-bit processor"));
10686 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10687 as_bad (_("-mgp32 used with a 64-bit ABI"));
10688 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10689 as_bad (_("-mgp64 used with a 32-bit ABI"));
10690 }
10691 else
10692 {
10693 /* Infer the integer register size from the ABI and processor.
10694 Restrict ourselves to 32-bit registers if that's all the
10695 processor has, or if the ABI cannot handle 64-bit registers. */
10696 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10697 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10698 }
10699
10700 /* ??? GAS treats single-float processors as though they had 64-bit
10701 float registers (although it complains when double-precision
10702 instructions are used). As things stand, saying they have 32-bit
10703 registers would lead to spurious "register must be even" messages.
10704 So here we assume float registers are always the same size as
10705 integer ones, unless the user says otherwise. */
10706 if (file_mips_fp32 < 0)
10707 file_mips_fp32 = file_mips_gp32;
10708
10709 /* End of GCC-shared inference code. */
10710
10711 /* This flag is set when we have a 64-bit capable CPU but use only
10712 32-bit wide registers. Note that EABI does not use it. */
10713 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10714 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10715 || mips_abi == O32_ABI))
10716 mips_32bitmode = 1;
10717
10718 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10719 as_bad (_("trap exception not supported at ISA 1"));
10720
10721 /* If the selected architecture includes support for ASEs, enable
10722 generation of code for them. */
10723 if (mips_opts.mips16 == -1)
10724 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10725 if (mips_opts.ase_mips3d == -1)
10726 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10727 if (mips_opts.ase_mdmx == -1)
10728 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10729
10730 file_mips_isa = mips_opts.isa;
10731 file_ase_mips16 = mips_opts.mips16;
10732 file_ase_mips3d = mips_opts.ase_mips3d;
10733 file_ase_mdmx = mips_opts.ase_mdmx;
10734 mips_opts.gp32 = file_mips_gp32;
10735 mips_opts.fp32 = file_mips_fp32;
10736
10737 if (mips_flag_mdebug < 0)
10738 {
10739 #ifdef OBJ_MAYBE_ECOFF
10740 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10741 mips_flag_mdebug = 1;
10742 else
10743 #endif /* OBJ_MAYBE_ECOFF */
10744 mips_flag_mdebug = 0;
10745 }
10746 }
10747 \f
10748 void
10749 mips_init_after_args (void)
10750 {
10751 /* initialize opcodes */
10752 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10753 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10754 }
10755
10756 long
10757 md_pcrel_from (fixS *fixP)
10758 {
10759 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10760 switch (fixP->fx_r_type)
10761 {
10762 case BFD_RELOC_16_PCREL_S2:
10763 case BFD_RELOC_MIPS_JMP:
10764 /* Return the address of the delay slot. */
10765 return addr + 4;
10766 default:
10767 return addr;
10768 }
10769 }
10770
10771 /* This is called before the symbol table is processed. In order to
10772 work with gcc when using mips-tfile, we must keep all local labels.
10773 However, in other cases, we want to discard them. If we were
10774 called with -g, but we didn't see any debugging information, it may
10775 mean that gcc is smuggling debugging information through to
10776 mips-tfile, in which case we must generate all local labels. */
10777
10778 void
10779 mips_frob_file_before_adjust (void)
10780 {
10781 #ifndef NO_ECOFF_DEBUGGING
10782 if (ECOFF_DEBUGGING
10783 && mips_debug != 0
10784 && ! ecoff_debugging_seen)
10785 flag_keep_locals = 1;
10786 #endif
10787 }
10788
10789 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10790 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10791 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10792 relocation operators.
10793
10794 For our purposes, a %lo() expression matches a %got() or %hi()
10795 expression if:
10796
10797 (a) it refers to the same symbol; and
10798 (b) the offset applied in the %lo() expression is no lower than
10799 the offset applied in the %got() or %hi().
10800
10801 (b) allows us to cope with code like:
10802
10803 lui $4,%hi(foo)
10804 lh $4,%lo(foo+2)($4)
10805
10806 ...which is legal on RELA targets, and has a well-defined behaviour
10807 if the user knows that adding 2 to "foo" will not induce a carry to
10808 the high 16 bits.
10809
10810 When several %lo()s match a particular %got() or %hi(), we use the
10811 following rules to distinguish them:
10812
10813 (1) %lo()s with smaller offsets are a better match than %lo()s with
10814 higher offsets.
10815
10816 (2) %lo()s with no matching %got() or %hi() are better than those
10817 that already have a matching %got() or %hi().
10818
10819 (3) later %lo()s are better than earlier %lo()s.
10820
10821 These rules are applied in order.
10822
10823 (1) means, among other things, that %lo()s with identical offsets are
10824 chosen if they exist.
10825
10826 (2) means that we won't associate several high-part relocations with
10827 the same low-part relocation unless there's no alternative. Having
10828 several high parts for the same low part is a GNU extension; this rule
10829 allows careful users to avoid it.
10830
10831 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10832 with the last high-part relocation being at the front of the list.
10833 It therefore makes sense to choose the last matching low-part
10834 relocation, all other things being equal. It's also easier
10835 to code that way. */
10836
10837 void
10838 mips_frob_file (void)
10839 {
10840 struct mips_hi_fixup *l;
10841
10842 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10843 {
10844 segment_info_type *seginfo;
10845 bfd_boolean matched_lo_p;
10846 fixS **hi_pos, **lo_pos, **pos;
10847
10848 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10849
10850 /* If a GOT16 relocation turns out to be against a global symbol,
10851 there isn't supposed to be a matching LO. */
10852 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10853 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10854 continue;
10855
10856 /* Check quickly whether the next fixup happens to be a matching %lo. */
10857 if (fixup_has_matching_lo_p (l->fixp))
10858 continue;
10859
10860 seginfo = seg_info (l->seg);
10861
10862 /* Set HI_POS to the position of this relocation in the chain.
10863 Set LO_POS to the position of the chosen low-part relocation.
10864 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10865 relocation that matches an immediately-preceding high-part
10866 relocation. */
10867 hi_pos = NULL;
10868 lo_pos = NULL;
10869 matched_lo_p = FALSE;
10870 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10871 {
10872 if (*pos == l->fixp)
10873 hi_pos = pos;
10874
10875 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10876 && (*pos)->fx_addsy == l->fixp->fx_addsy
10877 && (*pos)->fx_offset >= l->fixp->fx_offset
10878 && (lo_pos == NULL
10879 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10880 || (!matched_lo_p
10881 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10882 lo_pos = pos;
10883
10884 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10885 && fixup_has_matching_lo_p (*pos));
10886 }
10887
10888 /* If we found a match, remove the high-part relocation from its
10889 current position and insert it before the low-part relocation.
10890 Make the offsets match so that fixup_has_matching_lo_p()
10891 will return true.
10892
10893 We don't warn about unmatched high-part relocations since some
10894 versions of gcc have been known to emit dead "lui ...%hi(...)"
10895 instructions. */
10896 if (lo_pos != NULL)
10897 {
10898 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10899 if (l->fixp->fx_next != *lo_pos)
10900 {
10901 *hi_pos = l->fixp->fx_next;
10902 l->fixp->fx_next = *lo_pos;
10903 *lo_pos = l->fixp;
10904 }
10905 }
10906 }
10907 }
10908
10909 /* We may have combined relocations without symbols in the N32/N64 ABI.
10910 We have to prevent gas from dropping them. */
10911
10912 int
10913 mips_force_relocation (fixS *fixp)
10914 {
10915 if (generic_force_reloc (fixp))
10916 return 1;
10917
10918 if (HAVE_NEWABI
10919 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10920 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10921 || fixp->fx_r_type == BFD_RELOC_HI16_S
10922 || fixp->fx_r_type == BFD_RELOC_LO16))
10923 return 1;
10924
10925 return 0;
10926 }
10927
10928 /* This hook is called before a fix is simplified. We don't really
10929 decide whether to skip a fix here. Rather, we turn global symbols
10930 used as branch targets into local symbols, such that they undergo
10931 simplification. We can only do this if the symbol is defined and
10932 it is in the same section as the branch. If this doesn't hold, we
10933 emit a better error message than just saying the relocation is not
10934 valid for the selected object format.
10935
10936 FIXP is the fix-up we're going to try to simplify, SEG is the
10937 segment in which the fix up occurs. The return value should be
10938 non-zero to indicate the fix-up is valid for further
10939 simplifications. */
10940
10941 int
10942 mips_validate_fix (struct fix *fixP, asection *seg)
10943 {
10944 /* There's a lot of discussion on whether it should be possible to
10945 use R_MIPS_PC16 to represent branch relocations. The outcome
10946 seems to be that it can, but gas/bfd are very broken in creating
10947 RELA relocations for this, so for now we only accept branches to
10948 symbols in the same section. Anything else is of dubious value,
10949 since there's no guarantee that at link time the symbol would be
10950 in range. Even for branches to local symbols this is arguably
10951 wrong, since it we assume the symbol is not going to be
10952 overridden, which should be possible per ELF library semantics,
10953 but then, there isn't a dynamic relocation that could be used to
10954 this effect, and the target would likely be out of range as well.
10955
10956 Unfortunately, it seems that there is too much code out there
10957 that relies on branches to symbols that are global to be resolved
10958 as if they were local, like the IRIX tools do, so we do it as
10959 well, but with a warning so that people are reminded to fix their
10960 code. If we ever get back to using R_MIPS_PC16 for branch
10961 targets, this entire block should go away (and probably the
10962 whole function). */
10963
10964 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10965 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10966 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10967 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10968 && fixP->fx_addsy)
10969 {
10970 if (! S_IS_DEFINED (fixP->fx_addsy))
10971 {
10972 as_bad_where (fixP->fx_file, fixP->fx_line,
10973 _("Cannot branch to undefined symbol."));
10974 /* Avoid any further errors about this fixup. */
10975 fixP->fx_done = 1;
10976 }
10977 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10978 {
10979 as_bad_where (fixP->fx_file, fixP->fx_line,
10980 _("Cannot branch to symbol in another section."));
10981 fixP->fx_done = 1;
10982 }
10983 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10984 {
10985 symbolS *sym = fixP->fx_addsy;
10986
10987 if (mips_pic == SVR4_PIC)
10988 as_warn_where (fixP->fx_file, fixP->fx_line,
10989 _("Pretending global symbol used as branch target is local."));
10990
10991 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10992 S_GET_SEGMENT (sym),
10993 S_GET_VALUE (sym),
10994 symbol_get_frag (sym));
10995 copy_symbol_attributes (fixP->fx_addsy, sym);
10996 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10997 assert (symbol_resolved_p (sym));
10998 symbol_mark_resolved (fixP->fx_addsy);
10999 }
11000 }
11001
11002 return 1;
11003 }
11004
11005 /* Apply a fixup to the object file. */
11006
11007 void
11008 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11009 {
11010 bfd_byte *buf;
11011 long insn;
11012 reloc_howto_type *howto;
11013
11014 /* We ignore generic BFD relocations we don't know about. */
11015 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11016 if (! howto)
11017 return;
11018
11019 assert (fixP->fx_size == 4
11020 || fixP->fx_r_type == BFD_RELOC_16
11021 || fixP->fx_r_type == BFD_RELOC_64
11022 || fixP->fx_r_type == BFD_RELOC_CTOR
11023 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11024 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11025 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11026
11027 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11028
11029 assert (! fixP->fx_pcrel);
11030
11031 /* Don't treat parts of a composite relocation as done. There are two
11032 reasons for this:
11033
11034 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11035 should nevertheless be emitted if the first part is.
11036
11037 (2) In normal usage, composite relocations are never assembly-time
11038 constants. The easiest way of dealing with the pathological
11039 exceptions is to generate a relocation against STN_UNDEF and
11040 leave everything up to the linker. */
11041 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
11042 fixP->fx_done = 1;
11043
11044 switch (fixP->fx_r_type)
11045 {
11046 case BFD_RELOC_MIPS_TLS_GD:
11047 case BFD_RELOC_MIPS_TLS_LDM:
11048 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11049 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11050 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11051 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11052 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11053 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11054 /* fall through */
11055
11056 case BFD_RELOC_MIPS_JMP:
11057 case BFD_RELOC_MIPS_SHIFT5:
11058 case BFD_RELOC_MIPS_SHIFT6:
11059 case BFD_RELOC_MIPS_GOT_DISP:
11060 case BFD_RELOC_MIPS_GOT_PAGE:
11061 case BFD_RELOC_MIPS_GOT_OFST:
11062 case BFD_RELOC_MIPS_SUB:
11063 case BFD_RELOC_MIPS_INSERT_A:
11064 case BFD_RELOC_MIPS_INSERT_B:
11065 case BFD_RELOC_MIPS_DELETE:
11066 case BFD_RELOC_MIPS_HIGHEST:
11067 case BFD_RELOC_MIPS_HIGHER:
11068 case BFD_RELOC_MIPS_SCN_DISP:
11069 case BFD_RELOC_MIPS_REL16:
11070 case BFD_RELOC_MIPS_RELGOT:
11071 case BFD_RELOC_MIPS_JALR:
11072 case BFD_RELOC_HI16:
11073 case BFD_RELOC_HI16_S:
11074 case BFD_RELOC_GPREL16:
11075 case BFD_RELOC_MIPS_LITERAL:
11076 case BFD_RELOC_MIPS_CALL16:
11077 case BFD_RELOC_MIPS_GOT16:
11078 case BFD_RELOC_GPREL32:
11079 case BFD_RELOC_MIPS_GOT_HI16:
11080 case BFD_RELOC_MIPS_GOT_LO16:
11081 case BFD_RELOC_MIPS_CALL_HI16:
11082 case BFD_RELOC_MIPS_CALL_LO16:
11083 case BFD_RELOC_MIPS16_GPREL:
11084 case BFD_RELOC_MIPS16_HI16:
11085 case BFD_RELOC_MIPS16_HI16_S:
11086 assert (! fixP->fx_pcrel);
11087 /* Nothing needed to do. The value comes from the reloc entry */
11088 break;
11089
11090 case BFD_RELOC_MIPS16_JMP:
11091 /* We currently always generate a reloc against a symbol, which
11092 means that we don't want an addend even if the symbol is
11093 defined. */
11094 *valP = 0;
11095 break;
11096
11097 case BFD_RELOC_64:
11098 /* This is handled like BFD_RELOC_32, but we output a sign
11099 extended value if we are only 32 bits. */
11100 if (fixP->fx_done)
11101 {
11102 if (8 <= sizeof (valueT))
11103 md_number_to_chars ((char *) buf, *valP, 8);
11104 else
11105 {
11106 valueT hiv;
11107
11108 if ((*valP & 0x80000000) != 0)
11109 hiv = 0xffffffff;
11110 else
11111 hiv = 0;
11112 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11113 *valP, 4);
11114 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11115 hiv, 4);
11116 }
11117 }
11118 break;
11119
11120 case BFD_RELOC_RVA:
11121 case BFD_RELOC_32:
11122 /* If we are deleting this reloc entry, we must fill in the
11123 value now. This can happen if we have a .word which is not
11124 resolved when it appears but is later defined. */
11125 if (fixP->fx_done)
11126 md_number_to_chars ((char *) buf, *valP, 4);
11127 break;
11128
11129 case BFD_RELOC_16:
11130 /* If we are deleting this reloc entry, we must fill in the
11131 value now. */
11132 if (fixP->fx_done)
11133 md_number_to_chars ((char *) buf, *valP, 2);
11134 break;
11135
11136 case BFD_RELOC_LO16:
11137 case BFD_RELOC_MIPS16_LO16:
11138 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11139 may be safe to remove, but if so it's not obvious. */
11140 /* When handling an embedded PIC switch statement, we can wind
11141 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11142 if (fixP->fx_done)
11143 {
11144 if (*valP + 0x8000 > 0xffff)
11145 as_bad_where (fixP->fx_file, fixP->fx_line,
11146 _("relocation overflow"));
11147 if (target_big_endian)
11148 buf += 2;
11149 md_number_to_chars ((char *) buf, *valP, 2);
11150 }
11151 break;
11152
11153 case BFD_RELOC_16_PCREL_S2:
11154 if ((*valP & 0x3) != 0)
11155 as_bad_where (fixP->fx_file, fixP->fx_line,
11156 _("Branch to odd address (%lx)"), (long) *valP);
11157
11158 /*
11159 * We need to save the bits in the instruction since fixup_segment()
11160 * might be deleting the relocation entry (i.e., a branch within
11161 * the current segment).
11162 */
11163 if (! fixP->fx_done)
11164 break;
11165
11166 /* update old instruction data */
11167 if (target_big_endian)
11168 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11169 else
11170 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11171
11172 if (*valP + 0x20000 <= 0x3ffff)
11173 {
11174 insn |= (*valP >> 2) & 0xffff;
11175 md_number_to_chars ((char *) buf, insn, 4);
11176 }
11177 else if (mips_pic == NO_PIC
11178 && fixP->fx_done
11179 && fixP->fx_frag->fr_address >= text_section->vma
11180 && (fixP->fx_frag->fr_address
11181 < text_section->vma + bfd_get_section_size (text_section))
11182 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11183 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11184 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11185 {
11186 /* The branch offset is too large. If this is an
11187 unconditional branch, and we are not generating PIC code,
11188 we can convert it to an absolute jump instruction. */
11189 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11190 insn = 0x0c000000; /* jal */
11191 else
11192 insn = 0x08000000; /* j */
11193 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11194 fixP->fx_done = 0;
11195 fixP->fx_addsy = section_symbol (text_section);
11196 *valP += md_pcrel_from (fixP);
11197 md_number_to_chars ((char *) buf, insn, 4);
11198 }
11199 else
11200 {
11201 /* If we got here, we have branch-relaxation disabled,
11202 and there's nothing we can do to fix this instruction
11203 without turning it into a longer sequence. */
11204 as_bad_where (fixP->fx_file, fixP->fx_line,
11205 _("Branch out of range"));
11206 }
11207 break;
11208
11209 case BFD_RELOC_VTABLE_INHERIT:
11210 fixP->fx_done = 0;
11211 if (fixP->fx_addsy
11212 && !S_IS_DEFINED (fixP->fx_addsy)
11213 && !S_IS_WEAK (fixP->fx_addsy))
11214 S_SET_WEAK (fixP->fx_addsy);
11215 break;
11216
11217 case BFD_RELOC_VTABLE_ENTRY:
11218 fixP->fx_done = 0;
11219 break;
11220
11221 default:
11222 internalError ();
11223 }
11224
11225 /* Remember value for tc_gen_reloc. */
11226 fixP->fx_addnumber = *valP;
11227 }
11228
11229 static symbolS *
11230 get_symbol (void)
11231 {
11232 int c;
11233 char *name;
11234 symbolS *p;
11235
11236 name = input_line_pointer;
11237 c = get_symbol_end ();
11238 p = (symbolS *) symbol_find_or_make (name);
11239 *input_line_pointer = c;
11240 return p;
11241 }
11242
11243 /* Align the current frag to a given power of two. The MIPS assembler
11244 also automatically adjusts any preceding label. */
11245
11246 static void
11247 mips_align (int to, int fill, symbolS *label)
11248 {
11249 mips_emit_delays (FALSE);
11250 frag_align (to, fill, 0);
11251 record_alignment (now_seg, to);
11252 if (label != NULL)
11253 {
11254 assert (S_GET_SEGMENT (label) == now_seg);
11255 symbol_set_frag (label, frag_now);
11256 S_SET_VALUE (label, (valueT) frag_now_fix ());
11257 }
11258 }
11259
11260 /* Align to a given power of two. .align 0 turns off the automatic
11261 alignment used by the data creating pseudo-ops. */
11262
11263 static void
11264 s_align (int x ATTRIBUTE_UNUSED)
11265 {
11266 register int temp;
11267 register long temp_fill;
11268 long max_alignment = 15;
11269
11270 /*
11271
11272 o Note that the assembler pulls down any immediately preceding label
11273 to the aligned address.
11274 o It's not documented but auto alignment is reinstated by
11275 a .align pseudo instruction.
11276 o Note also that after auto alignment is turned off the mips assembler
11277 issues an error on attempt to assemble an improperly aligned data item.
11278 We don't.
11279
11280 */
11281
11282 temp = get_absolute_expression ();
11283 if (temp > max_alignment)
11284 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11285 else if (temp < 0)
11286 {
11287 as_warn (_("Alignment negative: 0 assumed."));
11288 temp = 0;
11289 }
11290 if (*input_line_pointer == ',')
11291 {
11292 ++input_line_pointer;
11293 temp_fill = get_absolute_expression ();
11294 }
11295 else
11296 temp_fill = 0;
11297 if (temp)
11298 {
11299 auto_align = 1;
11300 mips_align (temp, (int) temp_fill,
11301 insn_labels != NULL ? insn_labels->label : NULL);
11302 }
11303 else
11304 {
11305 auto_align = 0;
11306 }
11307
11308 demand_empty_rest_of_line ();
11309 }
11310
11311 void
11312 mips_flush_pending_output (void)
11313 {
11314 mips_emit_delays (FALSE);
11315 mips_clear_insn_labels ();
11316 }
11317
11318 static void
11319 s_change_sec (int sec)
11320 {
11321 segT seg;
11322
11323 #ifdef OBJ_ELF
11324 /* The ELF backend needs to know that we are changing sections, so
11325 that .previous works correctly. We could do something like check
11326 for an obj_section_change_hook macro, but that might be confusing
11327 as it would not be appropriate to use it in the section changing
11328 functions in read.c, since obj-elf.c intercepts those. FIXME:
11329 This should be cleaner, somehow. */
11330 obj_elf_section_change_hook ();
11331 #endif
11332
11333 mips_emit_delays (FALSE);
11334 switch (sec)
11335 {
11336 case 't':
11337 s_text (0);
11338 break;
11339 case 'd':
11340 s_data (0);
11341 break;
11342 case 'b':
11343 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11344 demand_empty_rest_of_line ();
11345 break;
11346
11347 case 'r':
11348 seg = subseg_new (RDATA_SECTION_NAME,
11349 (subsegT) get_absolute_expression ());
11350 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11351 {
11352 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11353 | SEC_READONLY | SEC_RELOC
11354 | SEC_DATA));
11355 if (strcmp (TARGET_OS, "elf") != 0)
11356 record_alignment (seg, 4);
11357 }
11358 demand_empty_rest_of_line ();
11359 break;
11360
11361 case 's':
11362 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11363 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11364 {
11365 bfd_set_section_flags (stdoutput, seg,
11366 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11367 if (strcmp (TARGET_OS, "elf") != 0)
11368 record_alignment (seg, 4);
11369 }
11370 demand_empty_rest_of_line ();
11371 break;
11372 }
11373
11374 auto_align = 1;
11375 }
11376
11377 void
11378 s_change_section (int ignore ATTRIBUTE_UNUSED)
11379 {
11380 #ifdef OBJ_ELF
11381 char *section_name;
11382 char c;
11383 char next_c = 0;
11384 int section_type;
11385 int section_flag;
11386 int section_entry_size;
11387 int section_alignment;
11388
11389 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11390 return;
11391
11392 section_name = input_line_pointer;
11393 c = get_symbol_end ();
11394 if (c)
11395 next_c = *(input_line_pointer + 1);
11396
11397 /* Do we have .section Name<,"flags">? */
11398 if (c != ',' || (c == ',' && next_c == '"'))
11399 {
11400 /* just after name is now '\0'. */
11401 *input_line_pointer = c;
11402 input_line_pointer = section_name;
11403 obj_elf_section (ignore);
11404 return;
11405 }
11406 input_line_pointer++;
11407
11408 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11409 if (c == ',')
11410 section_type = get_absolute_expression ();
11411 else
11412 section_type = 0;
11413 if (*input_line_pointer++ == ',')
11414 section_flag = get_absolute_expression ();
11415 else
11416 section_flag = 0;
11417 if (*input_line_pointer++ == ',')
11418 section_entry_size = get_absolute_expression ();
11419 else
11420 section_entry_size = 0;
11421 if (*input_line_pointer++ == ',')
11422 section_alignment = get_absolute_expression ();
11423 else
11424 section_alignment = 0;
11425
11426 section_name = xstrdup (section_name);
11427
11428 /* When using the generic form of .section (as implemented by obj-elf.c),
11429 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11430 traditionally had to fall back on the more common @progbits instead.
11431
11432 There's nothing really harmful in this, since bfd will correct
11433 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11434 means that, for backwards compatibiltiy, the special_section entries
11435 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11436
11437 Even so, we shouldn't force users of the MIPS .section syntax to
11438 incorrectly label the sections as SHT_PROGBITS. The best compromise
11439 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11440 generic type-checking code. */
11441 if (section_type == SHT_MIPS_DWARF)
11442 section_type = SHT_PROGBITS;
11443
11444 obj_elf_change_section (section_name, section_type, section_flag,
11445 section_entry_size, 0, 0, 0);
11446
11447 if (now_seg->name != section_name)
11448 free (section_name);
11449 #endif /* OBJ_ELF */
11450 }
11451
11452 void
11453 mips_enable_auto_align (void)
11454 {
11455 auto_align = 1;
11456 }
11457
11458 static void
11459 s_cons (int log_size)
11460 {
11461 symbolS *label;
11462
11463 label = insn_labels != NULL ? insn_labels->label : NULL;
11464 mips_emit_delays (FALSE);
11465 if (log_size > 0 && auto_align)
11466 mips_align (log_size, 0, label);
11467 mips_clear_insn_labels ();
11468 cons (1 << log_size);
11469 }
11470
11471 static void
11472 s_float_cons (int type)
11473 {
11474 symbolS *label;
11475
11476 label = insn_labels != NULL ? insn_labels->label : NULL;
11477
11478 mips_emit_delays (FALSE);
11479
11480 if (auto_align)
11481 {
11482 if (type == 'd')
11483 mips_align (3, 0, label);
11484 else
11485 mips_align (2, 0, label);
11486 }
11487
11488 mips_clear_insn_labels ();
11489
11490 float_cons (type);
11491 }
11492
11493 /* Handle .globl. We need to override it because on Irix 5 you are
11494 permitted to say
11495 .globl foo .text
11496 where foo is an undefined symbol, to mean that foo should be
11497 considered to be the address of a function. */
11498
11499 static void
11500 s_mips_globl (int x ATTRIBUTE_UNUSED)
11501 {
11502 char *name;
11503 int c;
11504 symbolS *symbolP;
11505 flagword flag;
11506
11507 name = input_line_pointer;
11508 c = get_symbol_end ();
11509 symbolP = symbol_find_or_make (name);
11510 *input_line_pointer = c;
11511 SKIP_WHITESPACE ();
11512
11513 /* On Irix 5, every global symbol that is not explicitly labelled as
11514 being a function is apparently labelled as being an object. */
11515 flag = BSF_OBJECT;
11516
11517 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11518 {
11519 char *secname;
11520 asection *sec;
11521
11522 secname = input_line_pointer;
11523 c = get_symbol_end ();
11524 sec = bfd_get_section_by_name (stdoutput, secname);
11525 if (sec == NULL)
11526 as_bad (_("%s: no such section"), secname);
11527 *input_line_pointer = c;
11528
11529 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11530 flag = BSF_FUNCTION;
11531 }
11532
11533 symbol_get_bfdsym (symbolP)->flags |= flag;
11534
11535 S_SET_EXTERNAL (symbolP);
11536 demand_empty_rest_of_line ();
11537 }
11538
11539 static void
11540 s_option (int x ATTRIBUTE_UNUSED)
11541 {
11542 char *opt;
11543 char c;
11544
11545 opt = input_line_pointer;
11546 c = get_symbol_end ();
11547
11548 if (*opt == 'O')
11549 {
11550 /* FIXME: What does this mean? */
11551 }
11552 else if (strncmp (opt, "pic", 3) == 0)
11553 {
11554 int i;
11555
11556 i = atoi (opt + 3);
11557 if (i == 0)
11558 mips_pic = NO_PIC;
11559 else if (i == 2)
11560 {
11561 mips_pic = SVR4_PIC;
11562 mips_abicalls = TRUE;
11563 }
11564 else
11565 as_bad (_(".option pic%d not supported"), i);
11566
11567 if (mips_pic == SVR4_PIC)
11568 {
11569 if (g_switch_seen && g_switch_value != 0)
11570 as_warn (_("-G may not be used with SVR4 PIC code"));
11571 g_switch_value = 0;
11572 bfd_set_gp_size (stdoutput, 0);
11573 }
11574 }
11575 else
11576 as_warn (_("Unrecognized option \"%s\""), opt);
11577
11578 *input_line_pointer = c;
11579 demand_empty_rest_of_line ();
11580 }
11581
11582 /* This structure is used to hold a stack of .set values. */
11583
11584 struct mips_option_stack
11585 {
11586 struct mips_option_stack *next;
11587 struct mips_set_options options;
11588 };
11589
11590 static struct mips_option_stack *mips_opts_stack;
11591
11592 /* Handle the .set pseudo-op. */
11593
11594 static void
11595 s_mipsset (int x ATTRIBUTE_UNUSED)
11596 {
11597 char *name = input_line_pointer, ch;
11598
11599 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11600 ++input_line_pointer;
11601 ch = *input_line_pointer;
11602 *input_line_pointer = '\0';
11603
11604 if (strcmp (name, "reorder") == 0)
11605 {
11606 if (mips_opts.noreorder && prev_nop_frag != NULL)
11607 {
11608 /* If we still have pending nops, we can discard them. The
11609 usual nop handling will insert any that are still
11610 needed. */
11611 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11612 * (mips_opts.mips16 ? 2 : 4));
11613 prev_nop_frag = NULL;
11614 }
11615 mips_opts.noreorder = 0;
11616 }
11617 else if (strcmp (name, "noreorder") == 0)
11618 {
11619 mips_emit_delays (TRUE);
11620 mips_opts.noreorder = 1;
11621 mips_any_noreorder = 1;
11622 }
11623 else if (strcmp (name, "at") == 0)
11624 {
11625 mips_opts.noat = 0;
11626 }
11627 else if (strcmp (name, "noat") == 0)
11628 {
11629 mips_opts.noat = 1;
11630 }
11631 else if (strcmp (name, "macro") == 0)
11632 {
11633 mips_opts.warn_about_macros = 0;
11634 }
11635 else if (strcmp (name, "nomacro") == 0)
11636 {
11637 if (mips_opts.noreorder == 0)
11638 as_bad (_("`noreorder' must be set before `nomacro'"));
11639 mips_opts.warn_about_macros = 1;
11640 }
11641 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11642 {
11643 mips_opts.nomove = 0;
11644 }
11645 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11646 {
11647 mips_opts.nomove = 1;
11648 }
11649 else if (strcmp (name, "bopt") == 0)
11650 {
11651 mips_opts.nobopt = 0;
11652 }
11653 else if (strcmp (name, "nobopt") == 0)
11654 {
11655 mips_opts.nobopt = 1;
11656 }
11657 else if (strcmp (name, "mips16") == 0
11658 || strcmp (name, "MIPS-16") == 0)
11659 mips_opts.mips16 = 1;
11660 else if (strcmp (name, "nomips16") == 0
11661 || strcmp (name, "noMIPS-16") == 0)
11662 mips_opts.mips16 = 0;
11663 else if (strcmp (name, "mips3d") == 0)
11664 mips_opts.ase_mips3d = 1;
11665 else if (strcmp (name, "nomips3d") == 0)
11666 mips_opts.ase_mips3d = 0;
11667 else if (strcmp (name, "mdmx") == 0)
11668 mips_opts.ase_mdmx = 1;
11669 else if (strcmp (name, "nomdmx") == 0)
11670 mips_opts.ase_mdmx = 0;
11671 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11672 {
11673 int reset = 0;
11674
11675 /* Permit the user to change the ISA and architecture on the fly.
11676 Needless to say, misuse can cause serious problems. */
11677 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11678 {
11679 reset = 1;
11680 mips_opts.isa = file_mips_isa;
11681 mips_opts.arch = file_mips_arch;
11682 }
11683 else if (strncmp (name, "arch=", 5) == 0)
11684 {
11685 const struct mips_cpu_info *p;
11686
11687 p = mips_parse_cpu("internal use", name + 5);
11688 if (!p)
11689 as_bad (_("unknown architecture %s"), name + 5);
11690 else
11691 {
11692 mips_opts.arch = p->cpu;
11693 mips_opts.isa = p->isa;
11694 }
11695 }
11696 else if (strncmp (name, "mips", 4) == 0)
11697 {
11698 const struct mips_cpu_info *p;
11699
11700 p = mips_parse_cpu("internal use", name);
11701 if (!p)
11702 as_bad (_("unknown ISA level %s"), name + 4);
11703 else
11704 {
11705 mips_opts.arch = p->cpu;
11706 mips_opts.isa = p->isa;
11707 }
11708 }
11709 else
11710 as_bad (_("unknown ISA or architecture %s"), name);
11711
11712 switch (mips_opts.isa)
11713 {
11714 case 0:
11715 break;
11716 case ISA_MIPS1:
11717 case ISA_MIPS2:
11718 case ISA_MIPS32:
11719 case ISA_MIPS32R2:
11720 mips_opts.gp32 = 1;
11721 mips_opts.fp32 = 1;
11722 break;
11723 case ISA_MIPS3:
11724 case ISA_MIPS4:
11725 case ISA_MIPS5:
11726 case ISA_MIPS64:
11727 case ISA_MIPS64R2:
11728 mips_opts.gp32 = 0;
11729 mips_opts.fp32 = 0;
11730 break;
11731 default:
11732 as_bad (_("unknown ISA level %s"), name + 4);
11733 break;
11734 }
11735 if (reset)
11736 {
11737 mips_opts.gp32 = file_mips_gp32;
11738 mips_opts.fp32 = file_mips_fp32;
11739 }
11740 }
11741 else if (strcmp (name, "autoextend") == 0)
11742 mips_opts.noautoextend = 0;
11743 else if (strcmp (name, "noautoextend") == 0)
11744 mips_opts.noautoextend = 1;
11745 else if (strcmp (name, "push") == 0)
11746 {
11747 struct mips_option_stack *s;
11748
11749 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11750 s->next = mips_opts_stack;
11751 s->options = mips_opts;
11752 mips_opts_stack = s;
11753 }
11754 else if (strcmp (name, "pop") == 0)
11755 {
11756 struct mips_option_stack *s;
11757
11758 s = mips_opts_stack;
11759 if (s == NULL)
11760 as_bad (_(".set pop with no .set push"));
11761 else
11762 {
11763 /* If we're changing the reorder mode we need to handle
11764 delay slots correctly. */
11765 if (s->options.noreorder && ! mips_opts.noreorder)
11766 mips_emit_delays (TRUE);
11767 else if (! s->options.noreorder && mips_opts.noreorder)
11768 {
11769 if (prev_nop_frag != NULL)
11770 {
11771 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11772 * (mips_opts.mips16 ? 2 : 4));
11773 prev_nop_frag = NULL;
11774 }
11775 }
11776
11777 mips_opts = s->options;
11778 mips_opts_stack = s->next;
11779 free (s);
11780 }
11781 }
11782 else if (strcmp (name, "sym32") == 0)
11783 mips_opts.sym32 = TRUE;
11784 else if (strcmp (name, "nosym32") == 0)
11785 mips_opts.sym32 = FALSE;
11786 else
11787 {
11788 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11789 }
11790 *input_line_pointer = ch;
11791 demand_empty_rest_of_line ();
11792 }
11793
11794 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11795 .option pic2. It means to generate SVR4 PIC calls. */
11796
11797 static void
11798 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11799 {
11800 mips_pic = SVR4_PIC;
11801 mips_abicalls = TRUE;
11802
11803 if (g_switch_seen && g_switch_value != 0)
11804 as_warn (_("-G may not be used with SVR4 PIC code"));
11805 g_switch_value = 0;
11806
11807 bfd_set_gp_size (stdoutput, 0);
11808 demand_empty_rest_of_line ();
11809 }
11810
11811 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11812 PIC code. It sets the $gp register for the function based on the
11813 function address, which is in the register named in the argument.
11814 This uses a relocation against _gp_disp, which is handled specially
11815 by the linker. The result is:
11816 lui $gp,%hi(_gp_disp)
11817 addiu $gp,$gp,%lo(_gp_disp)
11818 addu $gp,$gp,.cpload argument
11819 The .cpload argument is normally $25 == $t9.
11820
11821 The -mno-shared option changes this to:
11822 lui $gp,%hi(_gp)
11823 addiu $gp,$gp,%lo(_gp)
11824 and the argument is ignored. This saves an instruction, but the
11825 resulting code is not position independent; it uses an absolute
11826 address for _gp. Thus code assembled with -mno-shared can go into
11827 an ordinary executable, but not into a shared library. */
11828
11829 static void
11830 s_cpload (int ignore ATTRIBUTE_UNUSED)
11831 {
11832 expressionS ex;
11833 int reg;
11834 int in_shared;
11835
11836 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11837 .cpload is ignored. */
11838 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11839 {
11840 s_ignore (0);
11841 return;
11842 }
11843
11844 /* .cpload should be in a .set noreorder section. */
11845 if (mips_opts.noreorder == 0)
11846 as_warn (_(".cpload not in noreorder section"));
11847
11848 reg = tc_get_register (0);
11849
11850 /* If we need to produce a 64-bit address, we are better off using
11851 the default instruction sequence. */
11852 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11853
11854 ex.X_op = O_symbol;
11855 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" : "_gp");
11856 ex.X_op_symbol = NULL;
11857 ex.X_add_number = 0;
11858
11859 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11860 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11861
11862 macro_start ();
11863 macro_build_lui (&ex, mips_gp_register);
11864 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11865 mips_gp_register, BFD_RELOC_LO16);
11866 if (in_shared)
11867 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11868 mips_gp_register, reg);
11869 macro_end ();
11870
11871 demand_empty_rest_of_line ();
11872 }
11873
11874 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11875 .cpsetup $reg1, offset|$reg2, label
11876
11877 If offset is given, this results in:
11878 sd $gp, offset($sp)
11879 lui $gp, %hi(%neg(%gp_rel(label)))
11880 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11881 daddu $gp, $gp, $reg1
11882
11883 If $reg2 is given, this results in:
11884 daddu $reg2, $gp, $0
11885 lui $gp, %hi(%neg(%gp_rel(label)))
11886 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11887 daddu $gp, $gp, $reg1
11888 $reg1 is normally $25 == $t9.
11889
11890 The -mno-shared option replaces the last three instructions with
11891 lui $gp,%hi(_gp)
11892 addiu $gp,$gp,%lo(_gp)
11893 */
11894
11895 static void
11896 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11897 {
11898 expressionS ex_off;
11899 expressionS ex_sym;
11900 int reg1;
11901
11902 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11903 We also need NewABI support. */
11904 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11905 {
11906 s_ignore (0);
11907 return;
11908 }
11909
11910 reg1 = tc_get_register (0);
11911 SKIP_WHITESPACE ();
11912 if (*input_line_pointer != ',')
11913 {
11914 as_bad (_("missing argument separator ',' for .cpsetup"));
11915 return;
11916 }
11917 else
11918 ++input_line_pointer;
11919 SKIP_WHITESPACE ();
11920 if (*input_line_pointer == '$')
11921 {
11922 mips_cpreturn_register = tc_get_register (0);
11923 mips_cpreturn_offset = -1;
11924 }
11925 else
11926 {
11927 mips_cpreturn_offset = get_absolute_expression ();
11928 mips_cpreturn_register = -1;
11929 }
11930 SKIP_WHITESPACE ();
11931 if (*input_line_pointer != ',')
11932 {
11933 as_bad (_("missing argument separator ',' for .cpsetup"));
11934 return;
11935 }
11936 else
11937 ++input_line_pointer;
11938 SKIP_WHITESPACE ();
11939 expression (&ex_sym);
11940
11941 macro_start ();
11942 if (mips_cpreturn_register == -1)
11943 {
11944 ex_off.X_op = O_constant;
11945 ex_off.X_add_symbol = NULL;
11946 ex_off.X_op_symbol = NULL;
11947 ex_off.X_add_number = mips_cpreturn_offset;
11948
11949 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11950 BFD_RELOC_LO16, SP);
11951 }
11952 else
11953 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11954 mips_gp_register, 0);
11955
11956 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11957 {
11958 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11959 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11960 BFD_RELOC_HI16_S);
11961
11962 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11963 mips_gp_register, -1, BFD_RELOC_GPREL16,
11964 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11965
11966 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11967 mips_gp_register, reg1);
11968 }
11969 else
11970 {
11971 expressionS ex;
11972
11973 ex.X_op = O_symbol;
11974 ex.X_add_symbol = symbol_find_or_make ("_gp");
11975 ex.X_op_symbol = NULL;
11976 ex.X_add_number = 0;
11977
11978 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11979 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11980
11981 macro_build_lui (&ex, mips_gp_register);
11982 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11983 mips_gp_register, BFD_RELOC_LO16);
11984 }
11985
11986 macro_end ();
11987
11988 demand_empty_rest_of_line ();
11989 }
11990
11991 static void
11992 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11993 {
11994 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11995 .cplocal is ignored. */
11996 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11997 {
11998 s_ignore (0);
11999 return;
12000 }
12001
12002 mips_gp_register = tc_get_register (0);
12003 demand_empty_rest_of_line ();
12004 }
12005
12006 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12007 offset from $sp. The offset is remembered, and after making a PIC
12008 call $gp is restored from that location. */
12009
12010 static void
12011 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12012 {
12013 expressionS ex;
12014
12015 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12016 .cprestore is ignored. */
12017 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12018 {
12019 s_ignore (0);
12020 return;
12021 }
12022
12023 mips_cprestore_offset = get_absolute_expression ();
12024 mips_cprestore_valid = 1;
12025
12026 ex.X_op = O_constant;
12027 ex.X_add_symbol = NULL;
12028 ex.X_op_symbol = NULL;
12029 ex.X_add_number = mips_cprestore_offset;
12030
12031 macro_start ();
12032 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12033 SP, HAVE_64BIT_ADDRESSES);
12034 macro_end ();
12035
12036 demand_empty_rest_of_line ();
12037 }
12038
12039 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12040 was given in the preceding .cpsetup, it results in:
12041 ld $gp, offset($sp)
12042
12043 If a register $reg2 was given there, it results in:
12044 daddu $gp, $reg2, $0
12045 */
12046 static void
12047 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12048 {
12049 expressionS ex;
12050
12051 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12052 We also need NewABI support. */
12053 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12054 {
12055 s_ignore (0);
12056 return;
12057 }
12058
12059 macro_start ();
12060 if (mips_cpreturn_register == -1)
12061 {
12062 ex.X_op = O_constant;
12063 ex.X_add_symbol = NULL;
12064 ex.X_op_symbol = NULL;
12065 ex.X_add_number = mips_cpreturn_offset;
12066
12067 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12068 }
12069 else
12070 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12071 mips_cpreturn_register, 0);
12072 macro_end ();
12073
12074 demand_empty_rest_of_line ();
12075 }
12076
12077 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12078 code. It sets the offset to use in gp_rel relocations. */
12079
12080 static void
12081 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12082 {
12083 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12084 We also need NewABI support. */
12085 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12086 {
12087 s_ignore (0);
12088 return;
12089 }
12090
12091 mips_gprel_offset = get_absolute_expression ();
12092
12093 demand_empty_rest_of_line ();
12094 }
12095
12096 /* Handle the .gpword pseudo-op. This is used when generating PIC
12097 code. It generates a 32 bit GP relative reloc. */
12098
12099 static void
12100 s_gpword (int ignore ATTRIBUTE_UNUSED)
12101 {
12102 symbolS *label;
12103 expressionS ex;
12104 char *p;
12105
12106 /* When not generating PIC code, this is treated as .word. */
12107 if (mips_pic != SVR4_PIC)
12108 {
12109 s_cons (2);
12110 return;
12111 }
12112
12113 label = insn_labels != NULL ? insn_labels->label : NULL;
12114 mips_emit_delays (TRUE);
12115 if (auto_align)
12116 mips_align (2, 0, label);
12117 mips_clear_insn_labels ();
12118
12119 expression (&ex);
12120
12121 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12122 {
12123 as_bad (_("Unsupported use of .gpword"));
12124 ignore_rest_of_line ();
12125 }
12126
12127 p = frag_more (4);
12128 md_number_to_chars (p, 0, 4);
12129 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12130 BFD_RELOC_GPREL32);
12131
12132 demand_empty_rest_of_line ();
12133 }
12134
12135 static void
12136 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12137 {
12138 symbolS *label;
12139 expressionS ex;
12140 char *p;
12141
12142 /* When not generating PIC code, this is treated as .dword. */
12143 if (mips_pic != SVR4_PIC)
12144 {
12145 s_cons (3);
12146 return;
12147 }
12148
12149 label = insn_labels != NULL ? insn_labels->label : NULL;
12150 mips_emit_delays (TRUE);
12151 if (auto_align)
12152 mips_align (3, 0, label);
12153 mips_clear_insn_labels ();
12154
12155 expression (&ex);
12156
12157 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12158 {
12159 as_bad (_("Unsupported use of .gpdword"));
12160 ignore_rest_of_line ();
12161 }
12162
12163 p = frag_more (8);
12164 md_number_to_chars (p, 0, 8);
12165 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12166 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12167
12168 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12169 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12170 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12171
12172 demand_empty_rest_of_line ();
12173 }
12174
12175 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12176 tables in SVR4 PIC code. */
12177
12178 static void
12179 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12180 {
12181 int reg;
12182
12183 /* This is ignored when not generating SVR4 PIC code. */
12184 if (mips_pic != SVR4_PIC)
12185 {
12186 s_ignore (0);
12187 return;
12188 }
12189
12190 /* Add $gp to the register named as an argument. */
12191 macro_start ();
12192 reg = tc_get_register (0);
12193 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12194 macro_end ();
12195
12196 demand_empty_rest_of_line ();
12197 }
12198
12199 /* Handle the .insn pseudo-op. This marks instruction labels in
12200 mips16 mode. This permits the linker to handle them specially,
12201 such as generating jalx instructions when needed. We also make
12202 them odd for the duration of the assembly, in order to generate the
12203 right sort of code. We will make them even in the adjust_symtab
12204 routine, while leaving them marked. This is convenient for the
12205 debugger and the disassembler. The linker knows to make them odd
12206 again. */
12207
12208 static void
12209 s_insn (int ignore ATTRIBUTE_UNUSED)
12210 {
12211 mips16_mark_labels ();
12212
12213 demand_empty_rest_of_line ();
12214 }
12215
12216 /* Handle a .stabn directive. We need these in order to mark a label
12217 as being a mips16 text label correctly. Sometimes the compiler
12218 will emit a label, followed by a .stabn, and then switch sections.
12219 If the label and .stabn are in mips16 mode, then the label is
12220 really a mips16 text label. */
12221
12222 static void
12223 s_mips_stab (int type)
12224 {
12225 if (type == 'n')
12226 mips16_mark_labels ();
12227
12228 s_stab (type);
12229 }
12230
12231 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12232 */
12233
12234 static void
12235 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12236 {
12237 char *name;
12238 int c;
12239 symbolS *symbolP;
12240 expressionS exp;
12241
12242 name = input_line_pointer;
12243 c = get_symbol_end ();
12244 symbolP = symbol_find_or_make (name);
12245 S_SET_WEAK (symbolP);
12246 *input_line_pointer = c;
12247
12248 SKIP_WHITESPACE ();
12249
12250 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12251 {
12252 if (S_IS_DEFINED (symbolP))
12253 {
12254 as_bad ("ignoring attempt to redefine symbol %s",
12255 S_GET_NAME (symbolP));
12256 ignore_rest_of_line ();
12257 return;
12258 }
12259
12260 if (*input_line_pointer == ',')
12261 {
12262 ++input_line_pointer;
12263 SKIP_WHITESPACE ();
12264 }
12265
12266 expression (&exp);
12267 if (exp.X_op != O_symbol)
12268 {
12269 as_bad ("bad .weakext directive");
12270 ignore_rest_of_line ();
12271 return;
12272 }
12273 symbol_set_value_expression (symbolP, &exp);
12274 }
12275
12276 demand_empty_rest_of_line ();
12277 }
12278
12279 /* Parse a register string into a number. Called from the ECOFF code
12280 to parse .frame. The argument is non-zero if this is the frame
12281 register, so that we can record it in mips_frame_reg. */
12282
12283 int
12284 tc_get_register (int frame)
12285 {
12286 int reg;
12287
12288 SKIP_WHITESPACE ();
12289 if (*input_line_pointer++ != '$')
12290 {
12291 as_warn (_("expected `$'"));
12292 reg = ZERO;
12293 }
12294 else if (ISDIGIT (*input_line_pointer))
12295 {
12296 reg = get_absolute_expression ();
12297 if (reg < 0 || reg >= 32)
12298 {
12299 as_warn (_("Bad register number"));
12300 reg = ZERO;
12301 }
12302 }
12303 else
12304 {
12305 if (strncmp (input_line_pointer, "ra", 2) == 0)
12306 {
12307 reg = RA;
12308 input_line_pointer += 2;
12309 }
12310 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12311 {
12312 reg = FP;
12313 input_line_pointer += 2;
12314 }
12315 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12316 {
12317 reg = SP;
12318 input_line_pointer += 2;
12319 }
12320 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12321 {
12322 reg = GP;
12323 input_line_pointer += 2;
12324 }
12325 else if (strncmp (input_line_pointer, "at", 2) == 0)
12326 {
12327 reg = AT;
12328 input_line_pointer += 2;
12329 }
12330 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12331 {
12332 reg = KT0;
12333 input_line_pointer += 3;
12334 }
12335 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12336 {
12337 reg = KT1;
12338 input_line_pointer += 3;
12339 }
12340 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12341 {
12342 reg = ZERO;
12343 input_line_pointer += 4;
12344 }
12345 else
12346 {
12347 as_warn (_("Unrecognized register name"));
12348 reg = ZERO;
12349 while (ISALNUM(*input_line_pointer))
12350 input_line_pointer++;
12351 }
12352 }
12353 if (frame)
12354 {
12355 mips_frame_reg = reg != 0 ? reg : SP;
12356 mips_frame_reg_valid = 1;
12357 mips_cprestore_valid = 0;
12358 }
12359 return reg;
12360 }
12361
12362 valueT
12363 md_section_align (asection *seg, valueT addr)
12364 {
12365 int align = bfd_get_section_alignment (stdoutput, seg);
12366
12367 #ifdef OBJ_ELF
12368 /* We don't need to align ELF sections to the full alignment.
12369 However, Irix 5 may prefer that we align them at least to a 16
12370 byte boundary. We don't bother to align the sections if we are
12371 targeted for an embedded system. */
12372 if (strcmp (TARGET_OS, "elf") == 0)
12373 return addr;
12374 if (align > 4)
12375 align = 4;
12376 #endif
12377
12378 return ((addr + (1 << align) - 1) & (-1 << align));
12379 }
12380
12381 /* Utility routine, called from above as well. If called while the
12382 input file is still being read, it's only an approximation. (For
12383 example, a symbol may later become defined which appeared to be
12384 undefined earlier.) */
12385
12386 static int
12387 nopic_need_relax (symbolS *sym, int before_relaxing)
12388 {
12389 if (sym == 0)
12390 return 0;
12391
12392 if (g_switch_value > 0)
12393 {
12394 const char *symname;
12395 int change;
12396
12397 /* Find out whether this symbol can be referenced off the $gp
12398 register. It can be if it is smaller than the -G size or if
12399 it is in the .sdata or .sbss section. Certain symbols can
12400 not be referenced off the $gp, although it appears as though
12401 they can. */
12402 symname = S_GET_NAME (sym);
12403 if (symname != (const char *) NULL
12404 && (strcmp (symname, "eprol") == 0
12405 || strcmp (symname, "etext") == 0
12406 || strcmp (symname, "_gp") == 0
12407 || strcmp (symname, "edata") == 0
12408 || strcmp (symname, "_fbss") == 0
12409 || strcmp (symname, "_fdata") == 0
12410 || strcmp (symname, "_ftext") == 0
12411 || strcmp (symname, "end") == 0
12412 || strcmp (symname, "_gp_disp") == 0))
12413 change = 1;
12414 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12415 && (0
12416 #ifndef NO_ECOFF_DEBUGGING
12417 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12418 && (symbol_get_obj (sym)->ecoff_extern_size
12419 <= g_switch_value))
12420 #endif
12421 /* We must defer this decision until after the whole
12422 file has been read, since there might be a .extern
12423 after the first use of this symbol. */
12424 || (before_relaxing
12425 #ifndef NO_ECOFF_DEBUGGING
12426 && symbol_get_obj (sym)->ecoff_extern_size == 0
12427 #endif
12428 && S_GET_VALUE (sym) == 0)
12429 || (S_GET_VALUE (sym) != 0
12430 && S_GET_VALUE (sym) <= g_switch_value)))
12431 change = 0;
12432 else
12433 {
12434 const char *segname;
12435
12436 segname = segment_name (S_GET_SEGMENT (sym));
12437 assert (strcmp (segname, ".lit8") != 0
12438 && strcmp (segname, ".lit4") != 0);
12439 change = (strcmp (segname, ".sdata") != 0
12440 && strcmp (segname, ".sbss") != 0
12441 && strncmp (segname, ".sdata.", 7) != 0
12442 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12443 }
12444 return change;
12445 }
12446 else
12447 /* We are not optimizing for the $gp register. */
12448 return 1;
12449 }
12450
12451
12452 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12453
12454 static bfd_boolean
12455 pic_need_relax (symbolS *sym, asection *segtype)
12456 {
12457 asection *symsec;
12458 bfd_boolean linkonce;
12459
12460 /* Handle the case of a symbol equated to another symbol. */
12461 while (symbol_equated_reloc_p (sym))
12462 {
12463 symbolS *n;
12464
12465 /* It's possible to get a loop here in a badly written
12466 program. */
12467 n = symbol_get_value_expression (sym)->X_add_symbol;
12468 if (n == sym)
12469 break;
12470 sym = n;
12471 }
12472
12473 symsec = S_GET_SEGMENT (sym);
12474
12475 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12476 linkonce = FALSE;
12477 if (symsec != segtype && ! S_IS_LOCAL (sym))
12478 {
12479 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12480 != 0)
12481 linkonce = TRUE;
12482
12483 /* The GNU toolchain uses an extension for ELF: a section
12484 beginning with the magic string .gnu.linkonce is a linkonce
12485 section. */
12486 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12487 sizeof ".gnu.linkonce" - 1) == 0)
12488 linkonce = TRUE;
12489 }
12490
12491 /* This must duplicate the test in adjust_reloc_syms. */
12492 return (symsec != &bfd_und_section
12493 && symsec != &bfd_abs_section
12494 && ! bfd_is_com_section (symsec)
12495 && !linkonce
12496 #ifdef OBJ_ELF
12497 /* A global or weak symbol is treated as external. */
12498 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12499 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12500 #endif
12501 );
12502 }
12503
12504
12505 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12506 extended opcode. SEC is the section the frag is in. */
12507
12508 static int
12509 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12510 {
12511 int type;
12512 register const struct mips16_immed_operand *op;
12513 offsetT val;
12514 int mintiny, maxtiny;
12515 segT symsec;
12516 fragS *sym_frag;
12517
12518 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12519 return 0;
12520 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12521 return 1;
12522
12523 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12524 op = mips16_immed_operands;
12525 while (op->type != type)
12526 {
12527 ++op;
12528 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12529 }
12530
12531 if (op->unsp)
12532 {
12533 if (type == '<' || type == '>' || type == '[' || type == ']')
12534 {
12535 mintiny = 1;
12536 maxtiny = 1 << op->nbits;
12537 }
12538 else
12539 {
12540 mintiny = 0;
12541 maxtiny = (1 << op->nbits) - 1;
12542 }
12543 }
12544 else
12545 {
12546 mintiny = - (1 << (op->nbits - 1));
12547 maxtiny = (1 << (op->nbits - 1)) - 1;
12548 }
12549
12550 sym_frag = symbol_get_frag (fragp->fr_symbol);
12551 val = S_GET_VALUE (fragp->fr_symbol);
12552 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12553
12554 if (op->pcrel)
12555 {
12556 addressT addr;
12557
12558 /* We won't have the section when we are called from
12559 mips_relax_frag. However, we will always have been called
12560 from md_estimate_size_before_relax first. If this is a
12561 branch to a different section, we mark it as such. If SEC is
12562 NULL, and the frag is not marked, then it must be a branch to
12563 the same section. */
12564 if (sec == NULL)
12565 {
12566 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12567 return 1;
12568 }
12569 else
12570 {
12571 /* Must have been called from md_estimate_size_before_relax. */
12572 if (symsec != sec)
12573 {
12574 fragp->fr_subtype =
12575 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12576
12577 /* FIXME: We should support this, and let the linker
12578 catch branches and loads that are out of range. */
12579 as_bad_where (fragp->fr_file, fragp->fr_line,
12580 _("unsupported PC relative reference to different section"));
12581
12582 return 1;
12583 }
12584 if (fragp != sym_frag && sym_frag->fr_address == 0)
12585 /* Assume non-extended on the first relaxation pass.
12586 The address we have calculated will be bogus if this is
12587 a forward branch to another frag, as the forward frag
12588 will have fr_address == 0. */
12589 return 0;
12590 }
12591
12592 /* In this case, we know for sure that the symbol fragment is in
12593 the same section. If the relax_marker of the symbol fragment
12594 differs from the relax_marker of this fragment, we have not
12595 yet adjusted the symbol fragment fr_address. We want to add
12596 in STRETCH in order to get a better estimate of the address.
12597 This particularly matters because of the shift bits. */
12598 if (stretch != 0
12599 && sym_frag->relax_marker != fragp->relax_marker)
12600 {
12601 fragS *f;
12602
12603 /* Adjust stretch for any alignment frag. Note that if have
12604 been expanding the earlier code, the symbol may be
12605 defined in what appears to be an earlier frag. FIXME:
12606 This doesn't handle the fr_subtype field, which specifies
12607 a maximum number of bytes to skip when doing an
12608 alignment. */
12609 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12610 {
12611 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12612 {
12613 if (stretch < 0)
12614 stretch = - ((- stretch)
12615 & ~ ((1 << (int) f->fr_offset) - 1));
12616 else
12617 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12618 if (stretch == 0)
12619 break;
12620 }
12621 }
12622 if (f != NULL)
12623 val += stretch;
12624 }
12625
12626 addr = fragp->fr_address + fragp->fr_fix;
12627
12628 /* The base address rules are complicated. The base address of
12629 a branch is the following instruction. The base address of a
12630 PC relative load or add is the instruction itself, but if it
12631 is in a delay slot (in which case it can not be extended) use
12632 the address of the instruction whose delay slot it is in. */
12633 if (type == 'p' || type == 'q')
12634 {
12635 addr += 2;
12636
12637 /* If we are currently assuming that this frag should be
12638 extended, then, the current address is two bytes
12639 higher. */
12640 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12641 addr += 2;
12642
12643 /* Ignore the low bit in the target, since it will be set
12644 for a text label. */
12645 if ((val & 1) != 0)
12646 --val;
12647 }
12648 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12649 addr -= 4;
12650 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12651 addr -= 2;
12652
12653 val -= addr & ~ ((1 << op->shift) - 1);
12654
12655 /* Branch offsets have an implicit 0 in the lowest bit. */
12656 if (type == 'p' || type == 'q')
12657 val /= 2;
12658
12659 /* If any of the shifted bits are set, we must use an extended
12660 opcode. If the address depends on the size of this
12661 instruction, this can lead to a loop, so we arrange to always
12662 use an extended opcode. We only check this when we are in
12663 the main relaxation loop, when SEC is NULL. */
12664 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12665 {
12666 fragp->fr_subtype =
12667 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12668 return 1;
12669 }
12670
12671 /* If we are about to mark a frag as extended because the value
12672 is precisely maxtiny + 1, then there is a chance of an
12673 infinite loop as in the following code:
12674 la $4,foo
12675 .skip 1020
12676 .align 2
12677 foo:
12678 In this case when the la is extended, foo is 0x3fc bytes
12679 away, so the la can be shrunk, but then foo is 0x400 away, so
12680 the la must be extended. To avoid this loop, we mark the
12681 frag as extended if it was small, and is about to become
12682 extended with a value of maxtiny + 1. */
12683 if (val == ((maxtiny + 1) << op->shift)
12684 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12685 && sec == NULL)
12686 {
12687 fragp->fr_subtype =
12688 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12689 return 1;
12690 }
12691 }
12692 else if (symsec != absolute_section && sec != NULL)
12693 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12694
12695 if ((val & ((1 << op->shift) - 1)) != 0
12696 || val < (mintiny << op->shift)
12697 || val > (maxtiny << op->shift))
12698 return 1;
12699 else
12700 return 0;
12701 }
12702
12703 /* Compute the length of a branch sequence, and adjust the
12704 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12705 worst-case length is computed, with UPDATE being used to indicate
12706 whether an unconditional (-1), branch-likely (+1) or regular (0)
12707 branch is to be computed. */
12708 static int
12709 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12710 {
12711 bfd_boolean toofar;
12712 int length;
12713
12714 if (fragp
12715 && S_IS_DEFINED (fragp->fr_symbol)
12716 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12717 {
12718 addressT addr;
12719 offsetT val;
12720
12721 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12722
12723 addr = fragp->fr_address + fragp->fr_fix + 4;
12724
12725 val -= addr;
12726
12727 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12728 }
12729 else if (fragp)
12730 /* If the symbol is not defined or it's in a different segment,
12731 assume the user knows what's going on and emit a short
12732 branch. */
12733 toofar = FALSE;
12734 else
12735 toofar = TRUE;
12736
12737 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12738 fragp->fr_subtype
12739 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12740 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12741 RELAX_BRANCH_LINK (fragp->fr_subtype),
12742 toofar);
12743
12744 length = 4;
12745 if (toofar)
12746 {
12747 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12748 length += 8;
12749
12750 if (mips_pic != NO_PIC)
12751 {
12752 /* Additional space for PIC loading of target address. */
12753 length += 8;
12754 if (mips_opts.isa == ISA_MIPS1)
12755 /* Additional space for $at-stabilizing nop. */
12756 length += 4;
12757 }
12758
12759 /* If branch is conditional. */
12760 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12761 length += 8;
12762 }
12763
12764 return length;
12765 }
12766
12767 /* Estimate the size of a frag before relaxing. Unless this is the
12768 mips16, we are not really relaxing here, and the final size is
12769 encoded in the subtype information. For the mips16, we have to
12770 decide whether we are using an extended opcode or not. */
12771
12772 int
12773 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12774 {
12775 int change;
12776
12777 if (RELAX_BRANCH_P (fragp->fr_subtype))
12778 {
12779
12780 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12781
12782 return fragp->fr_var;
12783 }
12784
12785 if (RELAX_MIPS16_P (fragp->fr_subtype))
12786 /* We don't want to modify the EXTENDED bit here; it might get us
12787 into infinite loops. We change it only in mips_relax_frag(). */
12788 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12789
12790 if (mips_pic == NO_PIC)
12791 change = nopic_need_relax (fragp->fr_symbol, 0);
12792 else if (mips_pic == SVR4_PIC)
12793 change = pic_need_relax (fragp->fr_symbol, segtype);
12794 else
12795 abort ();
12796
12797 if (change)
12798 {
12799 fragp->fr_subtype |= RELAX_USE_SECOND;
12800 return -RELAX_FIRST (fragp->fr_subtype);
12801 }
12802 else
12803 return -RELAX_SECOND (fragp->fr_subtype);
12804 }
12805
12806 /* This is called to see whether a reloc against a defined symbol
12807 should be converted into a reloc against a section. */
12808
12809 int
12810 mips_fix_adjustable (fixS *fixp)
12811 {
12812 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12813 about the format of the offset in the .o file. */
12814 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12815 return 0;
12816
12817 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12818 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12819 return 0;
12820
12821 if (fixp->fx_addsy == NULL)
12822 return 1;
12823
12824 /* If symbol SYM is in a mergeable section, relocations of the form
12825 SYM + 0 can usually be made section-relative. The mergeable data
12826 is then identified by the section offset rather than by the symbol.
12827
12828 However, if we're generating REL LO16 relocations, the offset is split
12829 between the LO16 and parterning high part relocation. The linker will
12830 need to recalculate the complete offset in order to correctly identify
12831 the merge data.
12832
12833 The linker has traditionally not looked for the parterning high part
12834 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12835 placed anywhere. Rather than break backwards compatibility by changing
12836 this, it seems better not to force the issue, and instead keep the
12837 original symbol. This will work with either linker behavior. */
12838 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12839 && HAVE_IN_PLACE_ADDENDS
12840 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12841 return 0;
12842
12843 #ifdef OBJ_ELF
12844 /* Don't adjust relocations against mips16 symbols, so that the linker
12845 can find them if it needs to set up a stub. */
12846 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12847 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12848 && fixp->fx_subsy == NULL)
12849 return 0;
12850 #endif
12851
12852 return 1;
12853 }
12854
12855 /* Translate internal representation of relocation info to BFD target
12856 format. */
12857
12858 arelent **
12859 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12860 {
12861 static arelent *retval[4];
12862 arelent *reloc;
12863 bfd_reloc_code_real_type code;
12864
12865 memset (retval, 0, sizeof(retval));
12866 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12867 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12868 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12869 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12870
12871 assert (! fixp->fx_pcrel);
12872 reloc->addend = fixp->fx_addnumber;
12873
12874 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12875 entry to be used in the relocation's section offset. */
12876 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12877 {
12878 reloc->address = reloc->addend;
12879 reloc->addend = 0;
12880 }
12881
12882 code = fixp->fx_r_type;
12883
12884 /* To support a PC relative reloc, we used a Cygnus extension.
12885 We check for that here to make sure that we don't let such a
12886 reloc escape normally. (FIXME: This was formerly used by
12887 embedded-PIC support, but is now used by branch handling in
12888 general. That probably should be fixed.) */
12889 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12890 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12891 && code == BFD_RELOC_16_PCREL_S2)
12892 reloc->howto = NULL;
12893 else
12894 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12895
12896 if (reloc->howto == NULL)
12897 {
12898 as_bad_where (fixp->fx_file, fixp->fx_line,
12899 _("Can not represent %s relocation in this object file format"),
12900 bfd_get_reloc_code_name (code));
12901 retval[0] = NULL;
12902 }
12903
12904 return retval;
12905 }
12906
12907 /* Relax a machine dependent frag. This returns the amount by which
12908 the current size of the frag should change. */
12909
12910 int
12911 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12912 {
12913 if (RELAX_BRANCH_P (fragp->fr_subtype))
12914 {
12915 offsetT old_var = fragp->fr_var;
12916
12917 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12918
12919 return fragp->fr_var - old_var;
12920 }
12921
12922 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12923 return 0;
12924
12925 if (mips16_extended_frag (fragp, NULL, stretch))
12926 {
12927 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12928 return 0;
12929 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12930 return 2;
12931 }
12932 else
12933 {
12934 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12935 return 0;
12936 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12937 return -2;
12938 }
12939
12940 return 0;
12941 }
12942
12943 /* Convert a machine dependent frag. */
12944
12945 void
12946 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12947 {
12948 if (RELAX_BRANCH_P (fragp->fr_subtype))
12949 {
12950 bfd_byte *buf;
12951 unsigned long insn;
12952 expressionS exp;
12953 fixS *fixp;
12954
12955 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12956
12957 if (target_big_endian)
12958 insn = bfd_getb32 (buf);
12959 else
12960 insn = bfd_getl32 (buf);
12961
12962 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12963 {
12964 /* We generate a fixup instead of applying it right now
12965 because, if there are linker relaxations, we're going to
12966 need the relocations. */
12967 exp.X_op = O_symbol;
12968 exp.X_add_symbol = fragp->fr_symbol;
12969 exp.X_add_number = fragp->fr_offset;
12970
12971 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12972 4, &exp, 1,
12973 BFD_RELOC_16_PCREL_S2);
12974 fixp->fx_file = fragp->fr_file;
12975 fixp->fx_line = fragp->fr_line;
12976
12977 md_number_to_chars ((char *) buf, insn, 4);
12978 buf += 4;
12979 }
12980 else
12981 {
12982 int i;
12983
12984 as_warn_where (fragp->fr_file, fragp->fr_line,
12985 _("relaxed out-of-range branch into a jump"));
12986
12987 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12988 goto uncond;
12989
12990 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12991 {
12992 /* Reverse the branch. */
12993 switch ((insn >> 28) & 0xf)
12994 {
12995 case 4:
12996 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12997 have the condition reversed by tweaking a single
12998 bit, and their opcodes all have 0x4???????. */
12999 assert ((insn & 0xf1000000) == 0x41000000);
13000 insn ^= 0x00010000;
13001 break;
13002
13003 case 0:
13004 /* bltz 0x04000000 bgez 0x04010000
13005 bltzal 0x04100000 bgezal 0x04110000 */
13006 assert ((insn & 0xfc0e0000) == 0x04000000);
13007 insn ^= 0x00010000;
13008 break;
13009
13010 case 1:
13011 /* beq 0x10000000 bne 0x14000000
13012 blez 0x18000000 bgtz 0x1c000000 */
13013 insn ^= 0x04000000;
13014 break;
13015
13016 default:
13017 abort ();
13018 }
13019 }
13020
13021 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13022 {
13023 /* Clear the and-link bit. */
13024 assert ((insn & 0xfc1c0000) == 0x04100000);
13025
13026 /* bltzal 0x04100000 bgezal 0x04110000
13027 bltzall 0x04120000 bgezall 0x04130000 */
13028 insn &= ~0x00100000;
13029 }
13030
13031 /* Branch over the branch (if the branch was likely) or the
13032 full jump (not likely case). Compute the offset from the
13033 current instruction to branch to. */
13034 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13035 i = 16;
13036 else
13037 {
13038 /* How many bytes in instructions we've already emitted? */
13039 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13040 /* How many bytes in instructions from here to the end? */
13041 i = fragp->fr_var - i;
13042 }
13043 /* Convert to instruction count. */
13044 i >>= 2;
13045 /* Branch counts from the next instruction. */
13046 i--;
13047 insn |= i;
13048 /* Branch over the jump. */
13049 md_number_to_chars ((char *) buf, insn, 4);
13050 buf += 4;
13051
13052 /* Nop */
13053 md_number_to_chars ((char *) buf, 0, 4);
13054 buf += 4;
13055
13056 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13057 {
13058 /* beql $0, $0, 2f */
13059 insn = 0x50000000;
13060 /* Compute the PC offset from the current instruction to
13061 the end of the variable frag. */
13062 /* How many bytes in instructions we've already emitted? */
13063 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13064 /* How many bytes in instructions from here to the end? */
13065 i = fragp->fr_var - i;
13066 /* Convert to instruction count. */
13067 i >>= 2;
13068 /* Don't decrement i, because we want to branch over the
13069 delay slot. */
13070
13071 insn |= i;
13072 md_number_to_chars ((char *) buf, insn, 4);
13073 buf += 4;
13074
13075 md_number_to_chars ((char *) buf, 0, 4);
13076 buf += 4;
13077 }
13078
13079 uncond:
13080 if (mips_pic == NO_PIC)
13081 {
13082 /* j or jal. */
13083 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13084 ? 0x0c000000 : 0x08000000);
13085 exp.X_op = O_symbol;
13086 exp.X_add_symbol = fragp->fr_symbol;
13087 exp.X_add_number = fragp->fr_offset;
13088
13089 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13090 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13091 fixp->fx_file = fragp->fr_file;
13092 fixp->fx_line = fragp->fr_line;
13093
13094 md_number_to_chars ((char *) buf, insn, 4);
13095 buf += 4;
13096 }
13097 else
13098 {
13099 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13100 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13101 exp.X_op = O_symbol;
13102 exp.X_add_symbol = fragp->fr_symbol;
13103 exp.X_add_number = fragp->fr_offset;
13104
13105 if (fragp->fr_offset)
13106 {
13107 exp.X_add_symbol = make_expr_symbol (&exp);
13108 exp.X_add_number = 0;
13109 }
13110
13111 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13112 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13113 fixp->fx_file = fragp->fr_file;
13114 fixp->fx_line = fragp->fr_line;
13115
13116 md_number_to_chars ((char *) buf, insn, 4);
13117 buf += 4;
13118
13119 if (mips_opts.isa == ISA_MIPS1)
13120 {
13121 /* nop */
13122 md_number_to_chars ((char *) buf, 0, 4);
13123 buf += 4;
13124 }
13125
13126 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13127 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13128
13129 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13130 4, &exp, 0, BFD_RELOC_LO16);
13131 fixp->fx_file = fragp->fr_file;
13132 fixp->fx_line = fragp->fr_line;
13133
13134 md_number_to_chars ((char *) buf, insn, 4);
13135 buf += 4;
13136
13137 /* j(al)r $at. */
13138 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13139 insn = 0x0020f809;
13140 else
13141 insn = 0x00200008;
13142
13143 md_number_to_chars ((char *) buf, insn, 4);
13144 buf += 4;
13145 }
13146 }
13147
13148 assert (buf == (bfd_byte *)fragp->fr_literal
13149 + fragp->fr_fix + fragp->fr_var);
13150
13151 fragp->fr_fix += fragp->fr_var;
13152
13153 return;
13154 }
13155
13156 if (RELAX_MIPS16_P (fragp->fr_subtype))
13157 {
13158 int type;
13159 register const struct mips16_immed_operand *op;
13160 bfd_boolean small, ext;
13161 offsetT val;
13162 bfd_byte *buf;
13163 unsigned long insn;
13164 bfd_boolean use_extend;
13165 unsigned short extend;
13166
13167 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13168 op = mips16_immed_operands;
13169 while (op->type != type)
13170 ++op;
13171
13172 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13173 {
13174 small = FALSE;
13175 ext = TRUE;
13176 }
13177 else
13178 {
13179 small = TRUE;
13180 ext = FALSE;
13181 }
13182
13183 resolve_symbol_value (fragp->fr_symbol);
13184 val = S_GET_VALUE (fragp->fr_symbol);
13185 if (op->pcrel)
13186 {
13187 addressT addr;
13188
13189 addr = fragp->fr_address + fragp->fr_fix;
13190
13191 /* The rules for the base address of a PC relative reloc are
13192 complicated; see mips16_extended_frag. */
13193 if (type == 'p' || type == 'q')
13194 {
13195 addr += 2;
13196 if (ext)
13197 addr += 2;
13198 /* Ignore the low bit in the target, since it will be
13199 set for a text label. */
13200 if ((val & 1) != 0)
13201 --val;
13202 }
13203 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13204 addr -= 4;
13205 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13206 addr -= 2;
13207
13208 addr &= ~ (addressT) ((1 << op->shift) - 1);
13209 val -= addr;
13210
13211 /* Make sure the section winds up with the alignment we have
13212 assumed. */
13213 if (op->shift > 0)
13214 record_alignment (asec, op->shift);
13215 }
13216
13217 if (ext
13218 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13219 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13220 as_warn_where (fragp->fr_file, fragp->fr_line,
13221 _("extended instruction in delay slot"));
13222
13223 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13224
13225 if (target_big_endian)
13226 insn = bfd_getb16 (buf);
13227 else
13228 insn = bfd_getl16 (buf);
13229
13230 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13231 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13232 small, ext, &insn, &use_extend, &extend);
13233
13234 if (use_extend)
13235 {
13236 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13237 fragp->fr_fix += 2;
13238 buf += 2;
13239 }
13240
13241 md_number_to_chars ((char *) buf, insn, 2);
13242 fragp->fr_fix += 2;
13243 buf += 2;
13244 }
13245 else
13246 {
13247 int first, second;
13248 fixS *fixp;
13249
13250 first = RELAX_FIRST (fragp->fr_subtype);
13251 second = RELAX_SECOND (fragp->fr_subtype);
13252 fixp = (fixS *) fragp->fr_opcode;
13253
13254 /* Possibly emit a warning if we've chosen the longer option. */
13255 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13256 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13257 {
13258 const char *msg = macro_warning (fragp->fr_subtype);
13259 if (msg != 0)
13260 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13261 }
13262
13263 /* Go through all the fixups for the first sequence. Disable them
13264 (by marking them as done) if we're going to use the second
13265 sequence instead. */
13266 while (fixp
13267 && fixp->fx_frag == fragp
13268 && fixp->fx_where < fragp->fr_fix - second)
13269 {
13270 if (fragp->fr_subtype & RELAX_USE_SECOND)
13271 fixp->fx_done = 1;
13272 fixp = fixp->fx_next;
13273 }
13274
13275 /* Go through the fixups for the second sequence. Disable them if
13276 we're going to use the first sequence, otherwise adjust their
13277 addresses to account for the relaxation. */
13278 while (fixp && fixp->fx_frag == fragp)
13279 {
13280 if (fragp->fr_subtype & RELAX_USE_SECOND)
13281 fixp->fx_where -= first;
13282 else
13283 fixp->fx_done = 1;
13284 fixp = fixp->fx_next;
13285 }
13286
13287 /* Now modify the frag contents. */
13288 if (fragp->fr_subtype & RELAX_USE_SECOND)
13289 {
13290 char *start;
13291
13292 start = fragp->fr_literal + fragp->fr_fix - first - second;
13293 memmove (start, start + first, second);
13294 fragp->fr_fix -= first;
13295 }
13296 else
13297 fragp->fr_fix -= second;
13298 }
13299 }
13300
13301 #ifdef OBJ_ELF
13302
13303 /* This function is called after the relocs have been generated.
13304 We've been storing mips16 text labels as odd. Here we convert them
13305 back to even for the convenience of the debugger. */
13306
13307 void
13308 mips_frob_file_after_relocs (void)
13309 {
13310 asymbol **syms;
13311 unsigned int count, i;
13312
13313 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13314 return;
13315
13316 syms = bfd_get_outsymbols (stdoutput);
13317 count = bfd_get_symcount (stdoutput);
13318 for (i = 0; i < count; i++, syms++)
13319 {
13320 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13321 && ((*syms)->value & 1) != 0)
13322 {
13323 (*syms)->value &= ~1;
13324 /* If the symbol has an odd size, it was probably computed
13325 incorrectly, so adjust that as well. */
13326 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13327 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13328 }
13329 }
13330 }
13331
13332 #endif
13333
13334 /* This function is called whenever a label is defined. It is used
13335 when handling branch delays; if a branch has a label, we assume we
13336 can not move it. */
13337
13338 void
13339 mips_define_label (symbolS *sym)
13340 {
13341 struct insn_label_list *l;
13342
13343 if (free_insn_labels == NULL)
13344 l = (struct insn_label_list *) xmalloc (sizeof *l);
13345 else
13346 {
13347 l = free_insn_labels;
13348 free_insn_labels = l->next;
13349 }
13350
13351 l->label = sym;
13352 l->next = insn_labels;
13353 insn_labels = l;
13354 }
13355 \f
13356 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13357
13358 /* Some special processing for a MIPS ELF file. */
13359
13360 void
13361 mips_elf_final_processing (void)
13362 {
13363 /* Write out the register information. */
13364 if (mips_abi != N64_ABI)
13365 {
13366 Elf32_RegInfo s;
13367
13368 s.ri_gprmask = mips_gprmask;
13369 s.ri_cprmask[0] = mips_cprmask[0];
13370 s.ri_cprmask[1] = mips_cprmask[1];
13371 s.ri_cprmask[2] = mips_cprmask[2];
13372 s.ri_cprmask[3] = mips_cprmask[3];
13373 /* The gp_value field is set by the MIPS ELF backend. */
13374
13375 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13376 ((Elf32_External_RegInfo *)
13377 mips_regmask_frag));
13378 }
13379 else
13380 {
13381 Elf64_Internal_RegInfo s;
13382
13383 s.ri_gprmask = mips_gprmask;
13384 s.ri_pad = 0;
13385 s.ri_cprmask[0] = mips_cprmask[0];
13386 s.ri_cprmask[1] = mips_cprmask[1];
13387 s.ri_cprmask[2] = mips_cprmask[2];
13388 s.ri_cprmask[3] = mips_cprmask[3];
13389 /* The gp_value field is set by the MIPS ELF backend. */
13390
13391 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13392 ((Elf64_External_RegInfo *)
13393 mips_regmask_frag));
13394 }
13395
13396 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13397 sort of BFD interface for this. */
13398 if (mips_any_noreorder)
13399 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13400 if (mips_pic != NO_PIC)
13401 {
13402 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13403 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13404 }
13405 if (mips_abicalls)
13406 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13407
13408 /* Set MIPS ELF flags for ASEs. */
13409 if (file_ase_mips16)
13410 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13411 #if 0 /* XXX FIXME */
13412 if (file_ase_mips3d)
13413 elf_elfheader (stdoutput)->e_flags |= ???;
13414 #endif
13415 if (file_ase_mdmx)
13416 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13417
13418 /* Set the MIPS ELF ABI flags. */
13419 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13420 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13421 else if (mips_abi == O64_ABI)
13422 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13423 else if (mips_abi == EABI_ABI)
13424 {
13425 if (!file_mips_gp32)
13426 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13427 else
13428 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13429 }
13430 else if (mips_abi == N32_ABI)
13431 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13432
13433 /* Nothing to do for N64_ABI. */
13434
13435 if (mips_32bitmode)
13436 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13437 }
13438
13439 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13440 \f
13441 typedef struct proc {
13442 symbolS *func_sym;
13443 symbolS *func_end_sym;
13444 unsigned long reg_mask;
13445 unsigned long reg_offset;
13446 unsigned long fpreg_mask;
13447 unsigned long fpreg_offset;
13448 unsigned long frame_offset;
13449 unsigned long frame_reg;
13450 unsigned long pc_reg;
13451 } procS;
13452
13453 static procS cur_proc;
13454 static procS *cur_proc_ptr;
13455 static int numprocs;
13456
13457 /* Fill in an rs_align_code fragment. */
13458
13459 void
13460 mips_handle_align (fragS *fragp)
13461 {
13462 if (fragp->fr_type != rs_align_code)
13463 return;
13464
13465 if (mips_opts.mips16)
13466 {
13467 static const unsigned char be_nop[] = { 0x65, 0x00 };
13468 static const unsigned char le_nop[] = { 0x00, 0x65 };
13469
13470 int bytes;
13471 char *p;
13472
13473 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13474 p = fragp->fr_literal + fragp->fr_fix;
13475
13476 if (bytes & 1)
13477 {
13478 *p++ = 0;
13479 fragp->fr_fix++;
13480 }
13481
13482 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13483 fragp->fr_var = 2;
13484 }
13485
13486 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13487 }
13488
13489 static void
13490 md_obj_begin (void)
13491 {
13492 }
13493
13494 static void
13495 md_obj_end (void)
13496 {
13497 /* check for premature end, nesting errors, etc */
13498 if (cur_proc_ptr)
13499 as_warn (_("missing .end at end of assembly"));
13500 }
13501
13502 static long
13503 get_number (void)
13504 {
13505 int negative = 0;
13506 long val = 0;
13507
13508 if (*input_line_pointer == '-')
13509 {
13510 ++input_line_pointer;
13511 negative = 1;
13512 }
13513 if (!ISDIGIT (*input_line_pointer))
13514 as_bad (_("expected simple number"));
13515 if (input_line_pointer[0] == '0')
13516 {
13517 if (input_line_pointer[1] == 'x')
13518 {
13519 input_line_pointer += 2;
13520 while (ISXDIGIT (*input_line_pointer))
13521 {
13522 val <<= 4;
13523 val |= hex_value (*input_line_pointer++);
13524 }
13525 return negative ? -val : val;
13526 }
13527 else
13528 {
13529 ++input_line_pointer;
13530 while (ISDIGIT (*input_line_pointer))
13531 {
13532 val <<= 3;
13533 val |= *input_line_pointer++ - '0';
13534 }
13535 return negative ? -val : val;
13536 }
13537 }
13538 if (!ISDIGIT (*input_line_pointer))
13539 {
13540 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13541 *input_line_pointer, *input_line_pointer);
13542 as_warn (_("invalid number"));
13543 return -1;
13544 }
13545 while (ISDIGIT (*input_line_pointer))
13546 {
13547 val *= 10;
13548 val += *input_line_pointer++ - '0';
13549 }
13550 return negative ? -val : val;
13551 }
13552
13553 /* The .file directive; just like the usual .file directive, but there
13554 is an initial number which is the ECOFF file index. In the non-ECOFF
13555 case .file implies DWARF-2. */
13556
13557 static void
13558 s_mips_file (int x ATTRIBUTE_UNUSED)
13559 {
13560 static int first_file_directive = 0;
13561
13562 if (ECOFF_DEBUGGING)
13563 {
13564 get_number ();
13565 s_app_file (0);
13566 }
13567 else
13568 {
13569 char *filename;
13570
13571 filename = dwarf2_directive_file (0);
13572
13573 /* Versions of GCC up to 3.1 start files with a ".file"
13574 directive even for stabs output. Make sure that this
13575 ".file" is handled. Note that you need a version of GCC
13576 after 3.1 in order to support DWARF-2 on MIPS. */
13577 if (filename != NULL && ! first_file_directive)
13578 {
13579 (void) new_logical_line (filename, -1);
13580 s_app_file_string (filename, 0);
13581 }
13582 first_file_directive = 1;
13583 }
13584 }
13585
13586 /* The .loc directive, implying DWARF-2. */
13587
13588 static void
13589 s_mips_loc (int x ATTRIBUTE_UNUSED)
13590 {
13591 if (!ECOFF_DEBUGGING)
13592 dwarf2_directive_loc (0);
13593 }
13594
13595 /* The .end directive. */
13596
13597 static void
13598 s_mips_end (int x ATTRIBUTE_UNUSED)
13599 {
13600 symbolS *p;
13601
13602 /* Following functions need their own .frame and .cprestore directives. */
13603 mips_frame_reg_valid = 0;
13604 mips_cprestore_valid = 0;
13605
13606 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13607 {
13608 p = get_symbol ();
13609 demand_empty_rest_of_line ();
13610 }
13611 else
13612 p = NULL;
13613
13614 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13615 as_warn (_(".end not in text section"));
13616
13617 if (!cur_proc_ptr)
13618 {
13619 as_warn (_(".end directive without a preceding .ent directive."));
13620 demand_empty_rest_of_line ();
13621 return;
13622 }
13623
13624 if (p != NULL)
13625 {
13626 assert (S_GET_NAME (p));
13627 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13628 as_warn (_(".end symbol does not match .ent symbol."));
13629
13630 if (debug_type == DEBUG_STABS)
13631 stabs_generate_asm_endfunc (S_GET_NAME (p),
13632 S_GET_NAME (p));
13633 }
13634 else
13635 as_warn (_(".end directive missing or unknown symbol"));
13636
13637 #ifdef OBJ_ELF
13638 /* Create an expression to calculate the size of the function. */
13639 if (p && cur_proc_ptr)
13640 {
13641 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13642 expressionS *exp = xmalloc (sizeof (expressionS));
13643
13644 obj->size = exp;
13645 exp->X_op = O_subtract;
13646 exp->X_add_symbol = symbol_temp_new_now ();
13647 exp->X_op_symbol = p;
13648 exp->X_add_number = 0;
13649
13650 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13651 }
13652
13653 /* Generate a .pdr section. */
13654 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13655 && mips_flag_pdr)
13656 {
13657 segT saved_seg = now_seg;
13658 subsegT saved_subseg = now_subseg;
13659 valueT dot;
13660 expressionS exp;
13661 char *fragp;
13662
13663 dot = frag_now_fix ();
13664
13665 #ifdef md_flush_pending_output
13666 md_flush_pending_output ();
13667 #endif
13668
13669 assert (pdr_seg);
13670 subseg_set (pdr_seg, 0);
13671
13672 /* Write the symbol. */
13673 exp.X_op = O_symbol;
13674 exp.X_add_symbol = p;
13675 exp.X_add_number = 0;
13676 emit_expr (&exp, 4);
13677
13678 fragp = frag_more (7 * 4);
13679
13680 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13681 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13682 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13683 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13684 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13685 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13686 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13687
13688 subseg_set (saved_seg, saved_subseg);
13689 }
13690 #endif /* OBJ_ELF */
13691
13692 cur_proc_ptr = NULL;
13693 }
13694
13695 /* The .aent and .ent directives. */
13696
13697 static void
13698 s_mips_ent (int aent)
13699 {
13700 symbolS *symbolP;
13701
13702 symbolP = get_symbol ();
13703 if (*input_line_pointer == ',')
13704 ++input_line_pointer;
13705 SKIP_WHITESPACE ();
13706 if (ISDIGIT (*input_line_pointer)
13707 || *input_line_pointer == '-')
13708 get_number ();
13709
13710 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13711 as_warn (_(".ent or .aent not in text section."));
13712
13713 if (!aent && cur_proc_ptr)
13714 as_warn (_("missing .end"));
13715
13716 if (!aent)
13717 {
13718 /* This function needs its own .frame and .cprestore directives. */
13719 mips_frame_reg_valid = 0;
13720 mips_cprestore_valid = 0;
13721
13722 cur_proc_ptr = &cur_proc;
13723 memset (cur_proc_ptr, '\0', sizeof (procS));
13724
13725 cur_proc_ptr->func_sym = symbolP;
13726
13727 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13728
13729 ++numprocs;
13730
13731 if (debug_type == DEBUG_STABS)
13732 stabs_generate_asm_func (S_GET_NAME (symbolP),
13733 S_GET_NAME (symbolP));
13734 }
13735
13736 demand_empty_rest_of_line ();
13737 }
13738
13739 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13740 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13741 s_mips_frame is used so that we can set the PDR information correctly.
13742 We can't use the ecoff routines because they make reference to the ecoff
13743 symbol table (in the mdebug section). */
13744
13745 static void
13746 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13747 {
13748 #ifdef OBJ_ELF
13749 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13750 {
13751 long val;
13752
13753 if (cur_proc_ptr == (procS *) NULL)
13754 {
13755 as_warn (_(".frame outside of .ent"));
13756 demand_empty_rest_of_line ();
13757 return;
13758 }
13759
13760 cur_proc_ptr->frame_reg = tc_get_register (1);
13761
13762 SKIP_WHITESPACE ();
13763 if (*input_line_pointer++ != ','
13764 || get_absolute_expression_and_terminator (&val) != ',')
13765 {
13766 as_warn (_("Bad .frame directive"));
13767 --input_line_pointer;
13768 demand_empty_rest_of_line ();
13769 return;
13770 }
13771
13772 cur_proc_ptr->frame_offset = val;
13773 cur_proc_ptr->pc_reg = tc_get_register (0);
13774
13775 demand_empty_rest_of_line ();
13776 }
13777 else
13778 #endif /* OBJ_ELF */
13779 s_ignore (ignore);
13780 }
13781
13782 /* The .fmask and .mask directives. If the mdebug section is present
13783 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13784 embedded targets, s_mips_mask is used so that we can set the PDR
13785 information correctly. We can't use the ecoff routines because they
13786 make reference to the ecoff symbol table (in the mdebug section). */
13787
13788 static void
13789 s_mips_mask (int reg_type)
13790 {
13791 #ifdef OBJ_ELF
13792 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13793 {
13794 long mask, off;
13795
13796 if (cur_proc_ptr == (procS *) NULL)
13797 {
13798 as_warn (_(".mask/.fmask outside of .ent"));
13799 demand_empty_rest_of_line ();
13800 return;
13801 }
13802
13803 if (get_absolute_expression_and_terminator (&mask) != ',')
13804 {
13805 as_warn (_("Bad .mask/.fmask directive"));
13806 --input_line_pointer;
13807 demand_empty_rest_of_line ();
13808 return;
13809 }
13810
13811 off = get_absolute_expression ();
13812
13813 if (reg_type == 'F')
13814 {
13815 cur_proc_ptr->fpreg_mask = mask;
13816 cur_proc_ptr->fpreg_offset = off;
13817 }
13818 else
13819 {
13820 cur_proc_ptr->reg_mask = mask;
13821 cur_proc_ptr->reg_offset = off;
13822 }
13823
13824 demand_empty_rest_of_line ();
13825 }
13826 else
13827 #endif /* OBJ_ELF */
13828 s_ignore (reg_type);
13829 }
13830
13831 /* A table describing all the processors gas knows about. Names are
13832 matched in the order listed.
13833
13834 To ease comparison, please keep this table in the same order as
13835 gcc's mips_cpu_info_table[]. */
13836 static const struct mips_cpu_info mips_cpu_info_table[] =
13837 {
13838 /* Entries for generic ISAs */
13839 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13840 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13841 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13842 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13843 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13844 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13845 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13846 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13847 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13848
13849 /* MIPS I */
13850 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13851 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13852 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13853
13854 /* MIPS II */
13855 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13856
13857 /* MIPS III */
13858 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13859 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13860 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13861 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13862 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13863 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13864 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13865 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13866 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13867 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13868 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13869 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13870
13871 /* MIPS IV */
13872 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13873 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13874 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13875 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13876 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13877 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13878 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13879 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13880 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13881 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13882 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13883 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13884 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13885
13886 /* MIPS 32 */
13887 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13888 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13889 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13890
13891 /* MIPS 64 */
13892 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13893 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13894
13895 /* Broadcom SB-1 CPU core */
13896 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13897
13898 /* End marker */
13899 { NULL, 0, 0, 0 }
13900 };
13901
13902
13903 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13904 with a final "000" replaced by "k". Ignore case.
13905
13906 Note: this function is shared between GCC and GAS. */
13907
13908 static bfd_boolean
13909 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13910 {
13911 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13912 given++, canonical++;
13913
13914 return ((*given == 0 && *canonical == 0)
13915 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13916 }
13917
13918
13919 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13920 CPU name. We've traditionally allowed a lot of variation here.
13921
13922 Note: this function is shared between GCC and GAS. */
13923
13924 static bfd_boolean
13925 mips_matching_cpu_name_p (const char *canonical, const char *given)
13926 {
13927 /* First see if the name matches exactly, or with a final "000"
13928 turned into "k". */
13929 if (mips_strict_matching_cpu_name_p (canonical, given))
13930 return TRUE;
13931
13932 /* If not, try comparing based on numerical designation alone.
13933 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13934 if (TOLOWER (*given) == 'r')
13935 given++;
13936 if (!ISDIGIT (*given))
13937 return FALSE;
13938
13939 /* Skip over some well-known prefixes in the canonical name,
13940 hoping to find a number there too. */
13941 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13942 canonical += 2;
13943 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13944 canonical += 2;
13945 else if (TOLOWER (canonical[0]) == 'r')
13946 canonical += 1;
13947
13948 return mips_strict_matching_cpu_name_p (canonical, given);
13949 }
13950
13951
13952 /* Parse an option that takes the name of a processor as its argument.
13953 OPTION is the name of the option and CPU_STRING is the argument.
13954 Return the corresponding processor enumeration if the CPU_STRING is
13955 recognized, otherwise report an error and return null.
13956
13957 A similar function exists in GCC. */
13958
13959 static const struct mips_cpu_info *
13960 mips_parse_cpu (const char *option, const char *cpu_string)
13961 {
13962 const struct mips_cpu_info *p;
13963
13964 /* 'from-abi' selects the most compatible architecture for the given
13965 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13966 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13967 version. Look first at the -mgp options, if given, otherwise base
13968 the choice on MIPS_DEFAULT_64BIT.
13969
13970 Treat NO_ABI like the EABIs. One reason to do this is that the
13971 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13972 architecture. This code picks MIPS I for 'mips' and MIPS III for
13973 'mips64', just as we did in the days before 'from-abi'. */
13974 if (strcasecmp (cpu_string, "from-abi") == 0)
13975 {
13976 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13977 return mips_cpu_info_from_isa (ISA_MIPS1);
13978
13979 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13980 return mips_cpu_info_from_isa (ISA_MIPS3);
13981
13982 if (file_mips_gp32 >= 0)
13983 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13984
13985 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13986 ? ISA_MIPS3
13987 : ISA_MIPS1);
13988 }
13989
13990 /* 'default' has traditionally been a no-op. Probably not very useful. */
13991 if (strcasecmp (cpu_string, "default") == 0)
13992 return 0;
13993
13994 for (p = mips_cpu_info_table; p->name != 0; p++)
13995 if (mips_matching_cpu_name_p (p->name, cpu_string))
13996 return p;
13997
13998 as_bad ("Bad value (%s) for %s", cpu_string, option);
13999 return 0;
14000 }
14001
14002 /* Return the canonical processor information for ISA (a member of the
14003 ISA_MIPS* enumeration). */
14004
14005 static const struct mips_cpu_info *
14006 mips_cpu_info_from_isa (int isa)
14007 {
14008 int i;
14009
14010 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14011 if (mips_cpu_info_table[i].is_isa
14012 && isa == mips_cpu_info_table[i].isa)
14013 return (&mips_cpu_info_table[i]);
14014
14015 return NULL;
14016 }
14017
14018 static const struct mips_cpu_info *
14019 mips_cpu_info_from_arch (int arch)
14020 {
14021 int i;
14022
14023 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14024 if (arch == mips_cpu_info_table[i].cpu)
14025 return (&mips_cpu_info_table[i]);
14026
14027 return NULL;
14028 }
14029 \f
14030 static void
14031 show (FILE *stream, const char *string, int *col_p, int *first_p)
14032 {
14033 if (*first_p)
14034 {
14035 fprintf (stream, "%24s", "");
14036 *col_p = 24;
14037 }
14038 else
14039 {
14040 fprintf (stream, ", ");
14041 *col_p += 2;
14042 }
14043
14044 if (*col_p + strlen (string) > 72)
14045 {
14046 fprintf (stream, "\n%24s", "");
14047 *col_p = 24;
14048 }
14049
14050 fprintf (stream, "%s", string);
14051 *col_p += strlen (string);
14052
14053 *first_p = 0;
14054 }
14055
14056 void
14057 md_show_usage (FILE *stream)
14058 {
14059 int column, first;
14060 size_t i;
14061
14062 fprintf (stream, _("\
14063 MIPS options:\n\
14064 -EB generate big endian output\n\
14065 -EL generate little endian output\n\
14066 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14067 -G NUM allow referencing objects up to NUM bytes\n\
14068 implicitly with the gp register [default 8]\n"));
14069 fprintf (stream, _("\
14070 -mips1 generate MIPS ISA I instructions\n\
14071 -mips2 generate MIPS ISA II instructions\n\
14072 -mips3 generate MIPS ISA III instructions\n\
14073 -mips4 generate MIPS ISA IV instructions\n\
14074 -mips5 generate MIPS ISA V instructions\n\
14075 -mips32 generate MIPS32 ISA instructions\n\
14076 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14077 -mips64 generate MIPS64 ISA instructions\n\
14078 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14079 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14080
14081 first = 1;
14082
14083 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14084 show (stream, mips_cpu_info_table[i].name, &column, &first);
14085 show (stream, "from-abi", &column, &first);
14086 fputc ('\n', stream);
14087
14088 fprintf (stream, _("\
14089 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14090 -no-mCPU don't generate code specific to CPU.\n\
14091 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14092
14093 first = 1;
14094
14095 show (stream, "3900", &column, &first);
14096 show (stream, "4010", &column, &first);
14097 show (stream, "4100", &column, &first);
14098 show (stream, "4650", &column, &first);
14099 fputc ('\n', stream);
14100
14101 fprintf (stream, _("\
14102 -mips16 generate mips16 instructions\n\
14103 -no-mips16 do not generate mips16 instructions\n"));
14104 fprintf (stream, _("\
14105 -mfix-vr4120 work around certain VR4120 errata\n\
14106 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14107 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14108 -mno-shared optimize output for executables\n\
14109 -msym32 assume all symbols have 32-bit values\n\
14110 -O0 remove unneeded NOPs, do not swap branches\n\
14111 -O remove unneeded NOPs and swap branches\n\
14112 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14113 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14114 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14115 #ifdef OBJ_ELF
14116 fprintf (stream, _("\
14117 -KPIC, -call_shared generate SVR4 position independent code\n\
14118 -non_shared do not generate position independent code\n\
14119 -xgot assume a 32 bit GOT\n\
14120 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14121 -mabi=ABI create ABI conformant object file for:\n"));
14122
14123 first = 1;
14124
14125 show (stream, "32", &column, &first);
14126 show (stream, "o64", &column, &first);
14127 show (stream, "n32", &column, &first);
14128 show (stream, "64", &column, &first);
14129 show (stream, "eabi", &column, &first);
14130
14131 fputc ('\n', stream);
14132
14133 fprintf (stream, _("\
14134 -32 create o32 ABI object file (default)\n\
14135 -n32 create n32 ABI object file\n\
14136 -64 create 64 ABI object file\n"));
14137 #endif
14138 }
14139
14140 enum dwarf2_format
14141 mips_dwarf2_format (void)
14142 {
14143 if (mips_abi == N64_ABI)
14144 {
14145 #ifdef TE_IRIX
14146 return dwarf2_format_64bit_irix;
14147 #else
14148 return dwarf2_format_64bit;
14149 #endif
14150 }
14151 else
14152 return dwarf2_format_32bit;
14153 }
14154
14155 int
14156 mips_dwarf2_addr_size (void)
14157 {
14158 if (mips_abi == N64_ABI)
14159 return 8;
14160 else
14161 return 4;
14162 }