2005-04-10 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/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 #include "dw2gencfi.h"
37
38 #ifdef DEBUG
39 #define DBG(x) printf x
40 #else
41 #define DBG(x)
42 #endif
43
44 #ifdef OBJ_MAYBE_ELF
45 /* Clean up namespace so we can include obj-elf.h too. */
46 static int mips_output_flavor (void);
47 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
48 #undef OBJ_PROCESS_STAB
49 #undef OUTPUT_FLAVOR
50 #undef S_GET_ALIGN
51 #undef S_GET_SIZE
52 #undef S_SET_ALIGN
53 #undef S_SET_SIZE
54 #undef obj_frob_file
55 #undef obj_frob_file_after_relocs
56 #undef obj_frob_symbol
57 #undef obj_pop_insert
58 #undef obj_sec_sym_ok_for_reloc
59 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
60
61 #include "obj-elf.h"
62 /* Fix any of them that we actually care about. */
63 #undef OUTPUT_FLAVOR
64 #define OUTPUT_FLAVOR mips_output_flavor()
65 #endif
66
67 #if defined (OBJ_ELF)
68 #include "elf/mips.h"
69 #endif
70
71 #ifndef ECOFF_DEBUGGING
72 #define NO_ECOFF_DEBUGGING
73 #define ECOFF_DEBUGGING 0
74 #endif
75
76 int mips_flag_mdebug = -1;
77
78 /* Control generation of .pdr sections. Off by default on IRIX: the native
79 linker doesn't know about and discards them, but relocations against them
80 remain, leading to rld crashes. */
81 #ifdef TE_IRIX
82 int mips_flag_pdr = FALSE;
83 #else
84 int mips_flag_pdr = TRUE;
85 #endif
86
87 #include "ecoff.h"
88
89 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
90 static char *mips_regmask_frag;
91 #endif
92
93 #define ZERO 0
94 #define AT 1
95 #define TREG 24
96 #define PIC_CALL_REG 25
97 #define KT0 26
98 #define KT1 27
99 #define GP 28
100 #define SP 29
101 #define FP 30
102 #define RA 31
103
104 #define ILLEGAL_REG (32)
105
106 /* Allow override of standard little-endian ECOFF format. */
107
108 #ifndef ECOFF_LITTLE_FORMAT
109 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
110 #endif
111
112 extern int target_big_endian;
113
114 /* The name of the readonly data section. */
115 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
116 ? ".rdata" \
117 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
118 ? ".rdata" \
119 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
120 ? ".rodata" \
121 : (abort (), ""))
122
123 /* Information about an instruction, including its format, operands
124 and fixups. */
125 struct mips_cl_insn
126 {
127 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
128 const struct mips_opcode *insn_mo;
129
130 /* True if this is a mips16 instruction and if we want the extended
131 form of INSN_MO. */
132 bfd_boolean use_extend;
133
134 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
135 unsigned short extend;
136
137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
138 a copy of INSN_MO->match with the operands filled in. */
139 unsigned long insn_opcode;
140
141 /* The frag that contains the instruction. */
142 struct frag *frag;
143
144 /* The offset into FRAG of the first instruction byte. */
145 long where;
146
147 /* The relocs associated with the instruction, if any. */
148 fixS *fixp[3];
149
150 /* True if this entry cannot be moved from its current position. */
151 unsigned int fixed_p : 1;
152
153 /* True if this instruction occured in a .set noreorder block. */
154 unsigned int noreorder_p : 1;
155
156 /* True for mips16 instructions that jump to an absolute address. */
157 unsigned int mips16_absolute_jump_p : 1;
158 };
159
160 /* The ABI to use. */
161 enum mips_abi_level
162 {
163 NO_ABI = 0,
164 O32_ABI,
165 O64_ABI,
166 N32_ABI,
167 N64_ABI,
168 EABI_ABI
169 };
170
171 /* MIPS ABI we are using for this output file. */
172 static enum mips_abi_level mips_abi = NO_ABI;
173
174 /* Whether or not we have code that can call pic code. */
175 int mips_abicalls = FALSE;
176
177 /* Whether or not we have code which can be put into a shared
178 library. */
179 static bfd_boolean mips_in_shared = TRUE;
180
181 /* This is the set of options which may be modified by the .set
182 pseudo-op. We use a struct so that .set push and .set pop are more
183 reliable. */
184
185 struct mips_set_options
186 {
187 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
188 if it has not been initialized. Changed by `.set mipsN', and the
189 -mipsN command line option, and the default CPU. */
190 int isa;
191 /* Enabled Application Specific Extensions (ASEs). These are set to -1
192 if they have not been initialized. Changed by `.set <asename>', by
193 command line options, and based on the default architecture. */
194 int ase_mips3d;
195 int ase_mdmx;
196 /* Whether we are assembling for the mips16 processor. 0 if we are
197 not, 1 if we are, and -1 if the value has not been initialized.
198 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
199 -nomips16 command line options, and the default CPU. */
200 int mips16;
201 /* Non-zero if we should not reorder instructions. Changed by `.set
202 reorder' and `.set noreorder'. */
203 int noreorder;
204 /* Non-zero if we should not permit the $at ($1) register to be used
205 in instructions. Changed by `.set at' and `.set noat'. */
206 int noat;
207 /* Non-zero if we should warn when a macro instruction expands into
208 more than one machine instruction. Changed by `.set nomacro' and
209 `.set macro'. */
210 int warn_about_macros;
211 /* Non-zero if we should not move instructions. Changed by `.set
212 move', `.set volatile', `.set nomove', and `.set novolatile'. */
213 int nomove;
214 /* Non-zero if we should not optimize branches by moving the target
215 of the branch into the delay slot. Actually, we don't perform
216 this optimization anyhow. Changed by `.set bopt' and `.set
217 nobopt'. */
218 int nobopt;
219 /* Non-zero if we should not autoextend mips16 instructions.
220 Changed by `.set autoextend' and `.set noautoextend'. */
221 int noautoextend;
222 /* Restrict general purpose registers and floating point registers
223 to 32 bit. This is initially determined when -mgp32 or -mfp32
224 is passed but can changed if the assembler code uses .set mipsN. */
225 int gp32;
226 int fp32;
227 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
228 command line option, and the default CPU. */
229 int arch;
230 /* True if ".set sym32" is in effect. */
231 bfd_boolean sym32;
232 };
233
234 /* True if -mgp32 was passed. */
235 static int file_mips_gp32 = -1;
236
237 /* True if -mfp32 was passed. */
238 static int file_mips_fp32 = -1;
239
240 /* This is the struct we use to hold the current set of options. Note
241 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
242 -1 to indicate that they have not been initialized. */
243
244 static struct mips_set_options mips_opts =
245 {
246 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
247 };
248
249 /* These variables are filled in with the masks of registers used.
250 The object format code reads them and puts them in the appropriate
251 place. */
252 unsigned long mips_gprmask;
253 unsigned long mips_cprmask[4];
254
255 /* MIPS ISA we are using for this output file. */
256 static int file_mips_isa = ISA_UNKNOWN;
257
258 /* True if -mips16 was passed or implied by arguments passed on the
259 command line (e.g., by -march). */
260 static int file_ase_mips16;
261
262 /* True if -mips3d was passed or implied by arguments passed on the
263 command line (e.g., by -march). */
264 static int file_ase_mips3d;
265
266 /* True if -mdmx was passed or implied by arguments passed on the
267 command line (e.g., by -march). */
268 static int file_ase_mdmx;
269
270 /* The argument of the -march= flag. The architecture we are assembling. */
271 static int file_mips_arch = CPU_UNKNOWN;
272 static const char *mips_arch_string;
273
274 /* The argument of the -mtune= flag. The architecture for which we
275 are optimizing. */
276 static int mips_tune = CPU_UNKNOWN;
277 static const char *mips_tune_string;
278
279 /* True when generating 32-bit code for a 64-bit processor. */
280 static int mips_32bitmode = 0;
281
282 /* True if the given ABI requires 32-bit registers. */
283 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
284
285 /* Likewise 64-bit registers. */
286 #define ABI_NEEDS_64BIT_REGS(ABI) \
287 ((ABI) == N32_ABI \
288 || (ABI) == N64_ABI \
289 || (ABI) == O64_ABI)
290
291 /* Return true if ISA supports 64 bit gp register instructions. */
292 #define ISA_HAS_64BIT_REGS(ISA) ( \
293 (ISA) == ISA_MIPS3 \
294 || (ISA) == ISA_MIPS4 \
295 || (ISA) == ISA_MIPS5 \
296 || (ISA) == ISA_MIPS64 \
297 || (ISA) == ISA_MIPS64R2 \
298 )
299
300 /* Return true if ISA supports 64-bit right rotate (dror et al.)
301 instructions. */
302 #define ISA_HAS_DROR(ISA) ( \
303 (ISA) == ISA_MIPS64R2 \
304 )
305
306 /* Return true if ISA supports 32-bit right rotate (ror et al.)
307 instructions. */
308 #define ISA_HAS_ROR(ISA) ( \
309 (ISA) == ISA_MIPS32R2 \
310 || (ISA) == ISA_MIPS64R2 \
311 )
312
313 #define HAVE_32BIT_GPRS \
314 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
315
316 #define HAVE_32BIT_FPRS \
317 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
318
319 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
320 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
321
322 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
323
324 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
325
326 /* True if relocations are stored in-place. */
327 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
328
329 /* The ABI-derived address size. */
330 #define HAVE_64BIT_ADDRESSES \
331 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
332 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
333
334 /* The size of symbolic constants (i.e., expressions of the form
335 "SYMBOL" or "SYMBOL + OFFSET"). */
336 #define HAVE_32BIT_SYMBOLS \
337 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
338 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
339
340 /* Addresses are loaded in different ways, depending on the address size
341 in use. The n32 ABI Documentation also mandates the use of additions
342 with overflow checking, but existing implementations don't follow it. */
343 #define ADDRESS_ADD_INSN \
344 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
345
346 #define ADDRESS_ADDI_INSN \
347 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
348
349 #define ADDRESS_LOAD_INSN \
350 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
351
352 #define ADDRESS_STORE_INSN \
353 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
354
355 /* Return true if the given CPU supports the MIPS16 ASE. */
356 #define CPU_HAS_MIPS16(cpu) \
357 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
358 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
359
360 /* Return true if the given CPU supports the MIPS3D ASE. */
361 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
362 )
363
364 /* Return true if the given CPU supports the MDMX ASE. */
365 #define CPU_HAS_MDMX(cpu) (FALSE \
366 )
367
368 /* True if CPU has a dror instruction. */
369 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
370
371 /* True if CPU has a ror instruction. */
372 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
373
374 /* True if mflo and mfhi can be immediately followed by instructions
375 which write to the HI and LO registers.
376
377 According to MIPS specifications, MIPS ISAs I, II, and III need
378 (at least) two instructions between the reads of HI/LO and
379 instructions which write them, and later ISAs do not. Contradicting
380 the MIPS specifications, some MIPS IV processor user manuals (e.g.
381 the UM for the NEC Vr5000) document needing the instructions between
382 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
383 MIPS64 and later ISAs to have the interlocks, plus any specific
384 earlier-ISA CPUs for which CPU documentation declares that the
385 instructions are really interlocked. */
386 #define hilo_interlocks \
387 (mips_opts.isa == ISA_MIPS32 \
388 || mips_opts.isa == ISA_MIPS32R2 \
389 || mips_opts.isa == ISA_MIPS64 \
390 || mips_opts.isa == ISA_MIPS64R2 \
391 || mips_opts.arch == CPU_R4010 \
392 || mips_opts.arch == CPU_R10000 \
393 || mips_opts.arch == CPU_R12000 \
394 || mips_opts.arch == CPU_RM7000 \
395 || mips_opts.arch == CPU_VR5500 \
396 )
397
398 /* Whether the processor uses hardware interlocks to protect reads
399 from the GPRs after they are loaded from memory, and thus does not
400 require nops to be inserted. This applies to instructions marked
401 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
402 level I. */
403 #define gpr_interlocks \
404 (mips_opts.isa != ISA_MIPS1 \
405 || mips_opts.arch == CPU_R3900)
406
407 /* Whether the processor uses hardware interlocks to avoid delays
408 required by coprocessor instructions, and thus does not require
409 nops to be inserted. This applies to instructions marked
410 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
411 between instructions marked INSN_WRITE_COND_CODE and ones marked
412 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
413 levels I, II, and III. */
414 /* Itbl support may require additional care here. */
415 #define cop_interlocks \
416 ((mips_opts.isa != ISA_MIPS1 \
417 && mips_opts.isa != ISA_MIPS2 \
418 && mips_opts.isa != ISA_MIPS3) \
419 || mips_opts.arch == CPU_R4300 \
420 )
421
422 /* Whether the processor uses hardware interlocks to protect reads
423 from coprocessor registers after they are loaded from memory, and
424 thus does not require nops to be inserted. This applies to
425 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
426 requires at MIPS ISA level I. */
427 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
428
429 /* Is this a mfhi or mflo instruction? */
430 #define MF_HILO_INSN(PINFO) \
431 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
432
433 /* MIPS PIC level. */
434
435 enum mips_pic_level mips_pic;
436
437 /* 1 if we should generate 32 bit offsets from the $gp register in
438 SVR4_PIC mode. Currently has no meaning in other modes. */
439 static int mips_big_got = 0;
440
441 /* 1 if trap instructions should used for overflow rather than break
442 instructions. */
443 static int mips_trap = 0;
444
445 /* 1 if double width floating point constants should not be constructed
446 by assembling two single width halves into two single width floating
447 point registers which just happen to alias the double width destination
448 register. On some architectures this aliasing can be disabled by a bit
449 in the status register, and the setting of this bit cannot be determined
450 automatically at assemble time. */
451 static int mips_disable_float_construction;
452
453 /* Non-zero if any .set noreorder directives were used. */
454
455 static int mips_any_noreorder;
456
457 /* Non-zero if nops should be inserted when the register referenced in
458 an mfhi/mflo instruction is read in the next two instructions. */
459 static int mips_7000_hilo_fix;
460
461 /* The size of the small data section. */
462 static unsigned int g_switch_value = 8;
463 /* Whether the -G option was used. */
464 static int g_switch_seen = 0;
465
466 #define N_RMASK 0xc4
467 #define N_VFP 0xd4
468
469 /* If we can determine in advance that GP optimization won't be
470 possible, we can skip the relaxation stuff that tries to produce
471 GP-relative references. This makes delay slot optimization work
472 better.
473
474 This function can only provide a guess, but it seems to work for
475 gcc output. It needs to guess right for gcc, otherwise gcc
476 will put what it thinks is a GP-relative instruction in a branch
477 delay slot.
478
479 I don't know if a fix is needed for the SVR4_PIC mode. I've only
480 fixed it for the non-PIC mode. KR 95/04/07 */
481 static int nopic_need_relax (symbolS *, int);
482
483 /* handle of the OPCODE hash table */
484 static struct hash_control *op_hash = NULL;
485
486 /* The opcode hash table we use for the mips16. */
487 static struct hash_control *mips16_op_hash = NULL;
488
489 /* This array holds the chars that always start a comment. If the
490 pre-processor is disabled, these aren't very useful */
491 const char comment_chars[] = "#";
492
493 /* This array holds the chars that only start a comment at the beginning of
494 a line. If the line seems to have the form '# 123 filename'
495 .line and .file directives will appear in the pre-processed output */
496 /* Note that input_file.c hand checks for '#' at the beginning of the
497 first line of the input file. This is because the compiler outputs
498 #NO_APP at the beginning of its output. */
499 /* Also note that C style comments are always supported. */
500 const char line_comment_chars[] = "#";
501
502 /* This array holds machine specific line separator characters. */
503 const char line_separator_chars[] = ";";
504
505 /* Chars that can be used to separate mant from exp in floating point nums */
506 const char EXP_CHARS[] = "eE";
507
508 /* Chars that mean this number is a floating point constant */
509 /* As in 0f12.456 */
510 /* or 0d1.2345e12 */
511 const char FLT_CHARS[] = "rRsSfFdDxXpP";
512
513 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
514 changed in read.c . Ideally it shouldn't have to know about it at all,
515 but nothing is ideal around here.
516 */
517
518 static char *insn_error;
519
520 static int auto_align = 1;
521
522 /* When outputting SVR4 PIC code, the assembler needs to know the
523 offset in the stack frame from which to restore the $gp register.
524 This is set by the .cprestore pseudo-op, and saved in this
525 variable. */
526 static offsetT mips_cprestore_offset = -1;
527
528 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
529 more optimizations, it can use a register value instead of a memory-saved
530 offset and even an other register than $gp as global pointer. */
531 static offsetT mips_cpreturn_offset = -1;
532 static int mips_cpreturn_register = -1;
533 static int mips_gp_register = GP;
534 static int mips_gprel_offset = 0;
535
536 /* Whether mips_cprestore_offset has been set in the current function
537 (or whether it has already been warned about, if not). */
538 static int mips_cprestore_valid = 0;
539
540 /* This is the register which holds the stack frame, as set by the
541 .frame pseudo-op. This is needed to implement .cprestore. */
542 static int mips_frame_reg = SP;
543
544 /* Whether mips_frame_reg has been set in the current function
545 (or whether it has already been warned about, if not). */
546 static int mips_frame_reg_valid = 0;
547
548 /* To output NOP instructions correctly, we need to keep information
549 about the previous two instructions. */
550
551 /* Whether we are optimizing. The default value of 2 means to remove
552 unneeded NOPs and swap branch instructions when possible. A value
553 of 1 means to not swap branches. A value of 0 means to always
554 insert NOPs. */
555 static int mips_optimize = 2;
556
557 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
558 equivalent to seeing no -g option at all. */
559 static int mips_debug = 0;
560
561 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
562 #define MAX_VR4130_NOPS 4
563
564 /* The maximum number of NOPs needed to fill delay slots. */
565 #define MAX_DELAY_NOPS 2
566
567 /* The maximum number of NOPs needed for any purpose. */
568 #define MAX_NOPS 4
569
570 /* A list of previous instructions, with index 0 being the most recent.
571 We need to look back MAX_NOPS instructions when filling delay slots
572 or working around processor errata. We need to look back one
573 instruction further if we're thinking about using history[0] to
574 fill a branch delay slot. */
575 static struct mips_cl_insn history[1 + MAX_NOPS];
576
577 /* Nop instructions used by emit_nop. */
578 static struct mips_cl_insn nop_insn, mips16_nop_insn;
579
580 /* The appropriate nop for the current mode. */
581 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
582
583 /* If this is set, it points to a frag holding nop instructions which
584 were inserted before the start of a noreorder section. If those
585 nops turn out to be unnecessary, the size of the frag can be
586 decreased. */
587 static fragS *prev_nop_frag;
588
589 /* The number of nop instructions we created in prev_nop_frag. */
590 static int prev_nop_frag_holds;
591
592 /* The number of nop instructions that we know we need in
593 prev_nop_frag. */
594 static int prev_nop_frag_required;
595
596 /* The number of instructions we've seen since prev_nop_frag. */
597 static int prev_nop_frag_since;
598
599 /* For ECOFF and ELF, relocations against symbols are done in two
600 parts, with a HI relocation and a LO relocation. Each relocation
601 has only 16 bits of space to store an addend. This means that in
602 order for the linker to handle carries correctly, it must be able
603 to locate both the HI and the LO relocation. This means that the
604 relocations must appear in order in the relocation table.
605
606 In order to implement this, we keep track of each unmatched HI
607 relocation. We then sort them so that they immediately precede the
608 corresponding LO relocation. */
609
610 struct mips_hi_fixup
611 {
612 /* Next HI fixup. */
613 struct mips_hi_fixup *next;
614 /* This fixup. */
615 fixS *fixp;
616 /* The section this fixup is in. */
617 segT seg;
618 };
619
620 /* The list of unmatched HI relocs. */
621
622 static struct mips_hi_fixup *mips_hi_fixup_list;
623
624 /* The frag containing the last explicit relocation operator.
625 Null if explicit relocations have not been used. */
626
627 static fragS *prev_reloc_op_frag;
628
629 /* Map normal MIPS register numbers to mips16 register numbers. */
630
631 #define X ILLEGAL_REG
632 static const int mips32_to_16_reg_map[] =
633 {
634 X, X, 2, 3, 4, 5, 6, 7,
635 X, X, X, X, X, X, X, X,
636 0, 1, X, X, X, X, X, X,
637 X, X, X, X, X, X, X, X
638 };
639 #undef X
640
641 /* Map mips16 register numbers to normal MIPS register numbers. */
642
643 static const unsigned int mips16_to_32_reg_map[] =
644 {
645 16, 17, 2, 3, 4, 5, 6, 7
646 };
647
648 /* Classifies the kind of instructions we're interested in when
649 implementing -mfix-vr4120. */
650 enum fix_vr4120_class {
651 FIX_VR4120_MACC,
652 FIX_VR4120_DMACC,
653 FIX_VR4120_MULT,
654 FIX_VR4120_DMULT,
655 FIX_VR4120_DIV,
656 FIX_VR4120_MTHILO,
657 NUM_FIX_VR4120_CLASSES
658 };
659
660 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
661 there must be at least one other instruction between an instruction
662 of type X and an instruction of type Y. */
663 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
664
665 /* True if -mfix-vr4120 is in force. */
666 static int mips_fix_vr4120;
667
668 /* ...likewise -mfix-vr4130. */
669 static int mips_fix_vr4130;
670
671 /* We don't relax branches by default, since this causes us to expand
672 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
673 fail to compute the offset before expanding the macro to the most
674 efficient expansion. */
675
676 static int mips_relax_branch;
677 \f
678 /* The expansion of many macros depends on the type of symbol that
679 they refer to. For example, when generating position-dependent code,
680 a macro that refers to a symbol may have two different expansions,
681 one which uses GP-relative addresses and one which uses absolute
682 addresses. When generating SVR4-style PIC, a macro may have
683 different expansions for local and global symbols.
684
685 We handle these situations by generating both sequences and putting
686 them in variant frags. In position-dependent code, the first sequence
687 will be the GP-relative one and the second sequence will be the
688 absolute one. In SVR4 PIC, the first sequence will be for global
689 symbols and the second will be for local symbols.
690
691 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
692 SECOND are the lengths of the two sequences in bytes. These fields
693 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
694 the subtype has the following flags:
695
696 RELAX_USE_SECOND
697 Set if it has been decided that we should use the second
698 sequence instead of the first.
699
700 RELAX_SECOND_LONGER
701 Set in the first variant frag if the macro's second implementation
702 is longer than its first. This refers to the macro as a whole,
703 not an individual relaxation.
704
705 RELAX_NOMACRO
706 Set in the first variant frag if the macro appeared in a .set nomacro
707 block and if one alternative requires a warning but the other does not.
708
709 RELAX_DELAY_SLOT
710 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
711 delay slot.
712
713 The frag's "opcode" points to the first fixup for relaxable code.
714
715 Relaxable macros are generated using a sequence such as:
716
717 relax_start (SYMBOL);
718 ... generate first expansion ...
719 relax_switch ();
720 ... generate second expansion ...
721 relax_end ();
722
723 The code and fixups for the unwanted alternative are discarded
724 by md_convert_frag. */
725 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
726
727 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
728 #define RELAX_SECOND(X) ((X) & 0xff)
729 #define RELAX_USE_SECOND 0x10000
730 #define RELAX_SECOND_LONGER 0x20000
731 #define RELAX_NOMACRO 0x40000
732 #define RELAX_DELAY_SLOT 0x80000
733
734 /* Branch without likely bit. If label is out of range, we turn:
735
736 beq reg1, reg2, label
737 delay slot
738
739 into
740
741 bne reg1, reg2, 0f
742 nop
743 j label
744 0: delay slot
745
746 with the following opcode replacements:
747
748 beq <-> bne
749 blez <-> bgtz
750 bltz <-> bgez
751 bc1f <-> bc1t
752
753 bltzal <-> bgezal (with jal label instead of j label)
754
755 Even though keeping the delay slot instruction in the delay slot of
756 the branch would be more efficient, it would be very tricky to do
757 correctly, because we'd have to introduce a variable frag *after*
758 the delay slot instruction, and expand that instead. Let's do it
759 the easy way for now, even if the branch-not-taken case now costs
760 one additional instruction. Out-of-range branches are not supposed
761 to be common, anyway.
762
763 Branch likely. If label is out of range, we turn:
764
765 beql reg1, reg2, label
766 delay slot (annulled if branch not taken)
767
768 into
769
770 beql reg1, reg2, 1f
771 nop
772 beql $0, $0, 2f
773 nop
774 1: j[al] label
775 delay slot (executed only if branch taken)
776 2:
777
778 It would be possible to generate a shorter sequence by losing the
779 likely bit, generating something like:
780
781 bne reg1, reg2, 0f
782 nop
783 j[al] label
784 delay slot (executed only if branch taken)
785 0:
786
787 beql -> bne
788 bnel -> beq
789 blezl -> bgtz
790 bgtzl -> blez
791 bltzl -> bgez
792 bgezl -> bltz
793 bc1fl -> bc1t
794 bc1tl -> bc1f
795
796 bltzall -> bgezal (with jal label instead of j label)
797 bgezall -> bltzal (ditto)
798
799
800 but it's not clear that it would actually improve performance. */
801 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
802 ((relax_substateT) \
803 (0xc0000000 \
804 | ((toofar) ? 1 : 0) \
805 | ((link) ? 2 : 0) \
806 | ((likely) ? 4 : 0) \
807 | ((uncond) ? 8 : 0)))
808 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
809 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
810 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
811 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
812 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
813
814 /* For mips16 code, we use an entirely different form of relaxation.
815 mips16 supports two versions of most instructions which take
816 immediate values: a small one which takes some small value, and a
817 larger one which takes a 16 bit value. Since branches also follow
818 this pattern, relaxing these values is required.
819
820 We can assemble both mips16 and normal MIPS code in a single
821 object. Therefore, we need to support this type of relaxation at
822 the same time that we support the relaxation described above. We
823 use the high bit of the subtype field to distinguish these cases.
824
825 The information we store for this type of relaxation is the
826 argument code found in the opcode file for this relocation, whether
827 the user explicitly requested a small or extended form, and whether
828 the relocation is in a jump or jal delay slot. That tells us the
829 size of the value, and how it should be stored. We also store
830 whether the fragment is considered to be extended or not. We also
831 store whether this is known to be a branch to a different section,
832 whether we have tried to relax this frag yet, and whether we have
833 ever extended a PC relative fragment because of a shift count. */
834 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
835 (0x80000000 \
836 | ((type) & 0xff) \
837 | ((small) ? 0x100 : 0) \
838 | ((ext) ? 0x200 : 0) \
839 | ((dslot) ? 0x400 : 0) \
840 | ((jal_dslot) ? 0x800 : 0))
841 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
842 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
843 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
844 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
845 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
846 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
847 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
848 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
849 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
850 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
851 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
852 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
853
854 /* Is the given value a sign-extended 32-bit value? */
855 #define IS_SEXT_32BIT_NUM(x) \
856 (((x) &~ (offsetT) 0x7fffffff) == 0 \
857 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
858
859 /* Is the given value a sign-extended 16-bit value? */
860 #define IS_SEXT_16BIT_NUM(x) \
861 (((x) &~ (offsetT) 0x7fff) == 0 \
862 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
863
864 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
865 VALUE << SHIFT. VALUE is evaluated exactly once. */
866 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
867 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
868 | (((VALUE) & (MASK)) << (SHIFT)))
869
870 /* Extract bits MASK << SHIFT from STRUCT and shift them right
871 SHIFT places. */
872 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
873 (((STRUCT) >> (SHIFT)) & (MASK))
874
875 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
876 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
877
878 include/opcode/mips.h specifies operand fields using the macros
879 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
880 with "MIPS16OP" instead of "OP". */
881 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
882 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
883 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
884 INSERT_BITS ((INSN).insn_opcode, VALUE, \
885 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
886
887 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
888 #define EXTRACT_OPERAND(FIELD, INSN) \
889 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
890 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
891 EXTRACT_BITS ((INSN).insn_opcode, \
892 MIPS16OP_MASK_##FIELD, \
893 MIPS16OP_SH_##FIELD)
894 \f
895 /* Global variables used when generating relaxable macros. See the
896 comment above RELAX_ENCODE for more details about how relaxation
897 is used. */
898 static struct {
899 /* 0 if we're not emitting a relaxable macro.
900 1 if we're emitting the first of the two relaxation alternatives.
901 2 if we're emitting the second alternative. */
902 int sequence;
903
904 /* The first relaxable fixup in the current frag. (In other words,
905 the first fixup that refers to relaxable code.) */
906 fixS *first_fixup;
907
908 /* sizes[0] says how many bytes of the first alternative are stored in
909 the current frag. Likewise sizes[1] for the second alternative. */
910 unsigned int sizes[2];
911
912 /* The symbol on which the choice of sequence depends. */
913 symbolS *symbol;
914 } mips_relax;
915 \f
916 /* Global variables used to decide whether a macro needs a warning. */
917 static struct {
918 /* True if the macro is in a branch delay slot. */
919 bfd_boolean delay_slot_p;
920
921 /* For relaxable macros, sizes[0] is the length of the first alternative
922 in bytes and sizes[1] is the length of the second alternative.
923 For non-relaxable macros, both elements give the length of the
924 macro in bytes. */
925 unsigned int sizes[2];
926
927 /* The first variant frag for this macro. */
928 fragS *first_frag;
929 } mips_macro_warning;
930 \f
931 /* Prototypes for static functions. */
932
933 #define internalError() \
934 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
935
936 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
937
938 static void append_insn
939 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
940 static void mips_no_prev_insn (void);
941 static void mips16_macro_build
942 (expressionS *, const char *, const char *, va_list);
943 static void load_register (int, expressionS *, int);
944 static void macro_start (void);
945 static void macro_end (void);
946 static void macro (struct mips_cl_insn * ip);
947 static void mips16_macro (struct mips_cl_insn * ip);
948 #ifdef LOSING_COMPILER
949 static void macro2 (struct mips_cl_insn * ip);
950 #endif
951 static void mips_ip (char *str, struct mips_cl_insn * ip);
952 static void mips16_ip (char *str, struct mips_cl_insn * ip);
953 static void mips16_immed
954 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
955 unsigned long *, bfd_boolean *, unsigned short *);
956 static size_t my_getSmallExpression
957 (expressionS *, bfd_reloc_code_real_type *, char *);
958 static void my_getExpression (expressionS *, char *);
959 static void s_align (int);
960 static void s_change_sec (int);
961 static void s_change_section (int);
962 static void s_cons (int);
963 static void s_float_cons (int);
964 static void s_mips_globl (int);
965 static void s_option (int);
966 static void s_mipsset (int);
967 static void s_abicalls (int);
968 static void s_cpload (int);
969 static void s_cpsetup (int);
970 static void s_cplocal (int);
971 static void s_cprestore (int);
972 static void s_cpreturn (int);
973 static void s_gpvalue (int);
974 static void s_gpword (int);
975 static void s_gpdword (int);
976 static void s_cpadd (int);
977 static void s_insn (int);
978 static void md_obj_begin (void);
979 static void md_obj_end (void);
980 static void s_mips_ent (int);
981 static void s_mips_end (int);
982 static void s_mips_frame (int);
983 static void s_mips_mask (int reg_type);
984 static void s_mips_stab (int);
985 static void s_mips_weakext (int);
986 static void s_mips_file (int);
987 static void s_mips_loc (int);
988 static bfd_boolean pic_need_relax (symbolS *, asection *);
989 static int relaxed_branch_length (fragS *, asection *, int);
990 static int validate_mips_insn (const struct mips_opcode *);
991
992 /* Table and functions used to map between CPU/ISA names, and
993 ISA levels, and CPU numbers. */
994
995 struct mips_cpu_info
996 {
997 const char *name; /* CPU or ISA name. */
998 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
999 int isa; /* ISA level. */
1000 int cpu; /* CPU number (default CPU if ISA). */
1001 };
1002
1003 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1004 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1005 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1006 \f
1007 /* Pseudo-op table.
1008
1009 The following pseudo-ops from the Kane and Heinrich MIPS book
1010 should be defined here, but are currently unsupported: .alias,
1011 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1012
1013 The following pseudo-ops from the Kane and Heinrich MIPS book are
1014 specific to the type of debugging information being generated, and
1015 should be defined by the object format: .aent, .begin, .bend,
1016 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1017 .vreg.
1018
1019 The following pseudo-ops from the Kane and Heinrich MIPS book are
1020 not MIPS CPU specific, but are also not specific to the object file
1021 format. This file is probably the best place to define them, but
1022 they are not currently supported: .asm0, .endr, .lab, .repeat,
1023 .struct. */
1024
1025 static const pseudo_typeS mips_pseudo_table[] =
1026 {
1027 /* MIPS specific pseudo-ops. */
1028 {"option", s_option, 0},
1029 {"set", s_mipsset, 0},
1030 {"rdata", s_change_sec, 'r'},
1031 {"sdata", s_change_sec, 's'},
1032 {"livereg", s_ignore, 0},
1033 {"abicalls", s_abicalls, 0},
1034 {"cpload", s_cpload, 0},
1035 {"cpsetup", s_cpsetup, 0},
1036 {"cplocal", s_cplocal, 0},
1037 {"cprestore", s_cprestore, 0},
1038 {"cpreturn", s_cpreturn, 0},
1039 {"gpvalue", s_gpvalue, 0},
1040 {"gpword", s_gpword, 0},
1041 {"gpdword", s_gpdword, 0},
1042 {"cpadd", s_cpadd, 0},
1043 {"insn", s_insn, 0},
1044
1045 /* Relatively generic pseudo-ops that happen to be used on MIPS
1046 chips. */
1047 {"asciiz", stringer, 1},
1048 {"bss", s_change_sec, 'b'},
1049 {"err", s_err, 0},
1050 {"half", s_cons, 1},
1051 {"dword", s_cons, 3},
1052 {"weakext", s_mips_weakext, 0},
1053
1054 /* These pseudo-ops are defined in read.c, but must be overridden
1055 here for one reason or another. */
1056 {"align", s_align, 0},
1057 {"byte", s_cons, 0},
1058 {"data", s_change_sec, 'd'},
1059 {"double", s_float_cons, 'd'},
1060 {"float", s_float_cons, 'f'},
1061 {"globl", s_mips_globl, 0},
1062 {"global", s_mips_globl, 0},
1063 {"hword", s_cons, 1},
1064 {"int", s_cons, 2},
1065 {"long", s_cons, 2},
1066 {"octa", s_cons, 4},
1067 {"quad", s_cons, 3},
1068 {"section", s_change_section, 0},
1069 {"short", s_cons, 1},
1070 {"single", s_float_cons, 'f'},
1071 {"stabn", s_mips_stab, 'n'},
1072 {"text", s_change_sec, 't'},
1073 {"word", s_cons, 2},
1074
1075 { "extern", ecoff_directive_extern, 0},
1076
1077 { NULL, NULL, 0 },
1078 };
1079
1080 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1081 {
1082 /* These pseudo-ops should be defined by the object file format.
1083 However, a.out doesn't support them, so we have versions here. */
1084 {"aent", s_mips_ent, 1},
1085 {"bgnb", s_ignore, 0},
1086 {"end", s_mips_end, 0},
1087 {"endb", s_ignore, 0},
1088 {"ent", s_mips_ent, 0},
1089 {"file", s_mips_file, 0},
1090 {"fmask", s_mips_mask, 'F'},
1091 {"frame", s_mips_frame, 0},
1092 {"loc", s_mips_loc, 0},
1093 {"mask", s_mips_mask, 'R'},
1094 {"verstamp", s_ignore, 0},
1095 { NULL, NULL, 0 },
1096 };
1097
1098 extern void pop_insert (const pseudo_typeS *);
1099
1100 void
1101 mips_pop_insert (void)
1102 {
1103 pop_insert (mips_pseudo_table);
1104 if (! ECOFF_DEBUGGING)
1105 pop_insert (mips_nonecoff_pseudo_table);
1106 }
1107 \f
1108 /* Symbols labelling the current insn. */
1109
1110 struct insn_label_list
1111 {
1112 struct insn_label_list *next;
1113 symbolS *label;
1114 };
1115
1116 static struct insn_label_list *insn_labels;
1117 static struct insn_label_list *free_insn_labels;
1118
1119 static void mips_clear_insn_labels (void);
1120
1121 static inline void
1122 mips_clear_insn_labels (void)
1123 {
1124 register struct insn_label_list **pl;
1125
1126 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1127 ;
1128 *pl = insn_labels;
1129 insn_labels = NULL;
1130 }
1131 \f
1132 static char *expr_end;
1133
1134 /* Expressions which appear in instructions. These are set by
1135 mips_ip. */
1136
1137 static expressionS imm_expr;
1138 static expressionS imm2_expr;
1139 static expressionS offset_expr;
1140
1141 /* Relocs associated with imm_expr and offset_expr. */
1142
1143 static bfd_reloc_code_real_type imm_reloc[3]
1144 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1145 static bfd_reloc_code_real_type offset_reloc[3]
1146 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1147
1148 /* These are set by mips16_ip if an explicit extension is used. */
1149
1150 static bfd_boolean mips16_small, mips16_ext;
1151
1152 #ifdef OBJ_ELF
1153 /* The pdr segment for per procedure frame/regmask info. Not used for
1154 ECOFF debugging. */
1155
1156 static segT pdr_seg;
1157 #endif
1158
1159 /* The default target format to use. */
1160
1161 const char *
1162 mips_target_format (void)
1163 {
1164 switch (OUTPUT_FLAVOR)
1165 {
1166 case bfd_target_ecoff_flavour:
1167 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1168 case bfd_target_coff_flavour:
1169 return "pe-mips";
1170 case bfd_target_elf_flavour:
1171 #ifdef TE_TMIPS
1172 /* This is traditional mips. */
1173 return (target_big_endian
1174 ? (HAVE_64BIT_OBJECTS
1175 ? "elf64-tradbigmips"
1176 : (HAVE_NEWABI
1177 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1178 : (HAVE_64BIT_OBJECTS
1179 ? "elf64-tradlittlemips"
1180 : (HAVE_NEWABI
1181 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1182 #else
1183 return (target_big_endian
1184 ? (HAVE_64BIT_OBJECTS
1185 ? "elf64-bigmips"
1186 : (HAVE_NEWABI
1187 ? "elf32-nbigmips" : "elf32-bigmips"))
1188 : (HAVE_64BIT_OBJECTS
1189 ? "elf64-littlemips"
1190 : (HAVE_NEWABI
1191 ? "elf32-nlittlemips" : "elf32-littlemips")));
1192 #endif
1193 default:
1194 abort ();
1195 return NULL;
1196 }
1197 }
1198
1199 /* Return the length of instruction INSN. */
1200
1201 static inline unsigned int
1202 insn_length (const struct mips_cl_insn *insn)
1203 {
1204 if (!mips_opts.mips16)
1205 return 4;
1206 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1207 }
1208
1209 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1210
1211 static void
1212 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1213 {
1214 size_t i;
1215
1216 insn->insn_mo = mo;
1217 insn->use_extend = FALSE;
1218 insn->extend = 0;
1219 insn->insn_opcode = mo->match;
1220 insn->frag = NULL;
1221 insn->where = 0;
1222 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1223 insn->fixp[i] = NULL;
1224 insn->fixed_p = (mips_opts.noreorder > 0);
1225 insn->noreorder_p = (mips_opts.noreorder > 0);
1226 insn->mips16_absolute_jump_p = 0;
1227 }
1228
1229 /* Install INSN at the location specified by its "frag" and "where" fields. */
1230
1231 static void
1232 install_insn (const struct mips_cl_insn *insn)
1233 {
1234 char *f = insn->frag->fr_literal + insn->where;
1235 if (!mips_opts.mips16)
1236 md_number_to_chars (f, insn->insn_opcode, 4);
1237 else if (insn->mips16_absolute_jump_p)
1238 {
1239 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1240 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1241 }
1242 else
1243 {
1244 if (insn->use_extend)
1245 {
1246 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1247 f += 2;
1248 }
1249 md_number_to_chars (f, insn->insn_opcode, 2);
1250 }
1251 }
1252
1253 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1254 and install the opcode in the new location. */
1255
1256 static void
1257 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1258 {
1259 size_t i;
1260
1261 insn->frag = frag;
1262 insn->where = where;
1263 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1264 if (insn->fixp[i] != NULL)
1265 {
1266 insn->fixp[i]->fx_frag = frag;
1267 insn->fixp[i]->fx_where = where;
1268 }
1269 install_insn (insn);
1270 }
1271
1272 /* Add INSN to the end of the output. */
1273
1274 static void
1275 add_fixed_insn (struct mips_cl_insn *insn)
1276 {
1277 char *f = frag_more (insn_length (insn));
1278 move_insn (insn, frag_now, f - frag_now->fr_literal);
1279 }
1280
1281 /* Start a variant frag and move INSN to the start of the variant part,
1282 marking it as fixed. The other arguments are as for frag_var. */
1283
1284 static void
1285 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1286 relax_substateT subtype, symbolS *symbol, offsetT offset)
1287 {
1288 frag_grow (max_chars);
1289 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1290 insn->fixed_p = 1;
1291 frag_var (rs_machine_dependent, max_chars, var,
1292 subtype, symbol, offset, NULL);
1293 }
1294
1295 /* Insert N copies of INSN into the history buffer, starting at
1296 position FIRST. Neither FIRST nor N need to be clipped. */
1297
1298 static void
1299 insert_into_history (unsigned int first, unsigned int n,
1300 const struct mips_cl_insn *insn)
1301 {
1302 if (mips_relax.sequence != 2)
1303 {
1304 unsigned int i;
1305
1306 for (i = ARRAY_SIZE (history); i-- > first;)
1307 if (i >= first + n)
1308 history[i] = history[i - n];
1309 else
1310 history[i] = *insn;
1311 }
1312 }
1313
1314 /* Emit a nop instruction, recording it in the history buffer. */
1315
1316 static void
1317 emit_nop (void)
1318 {
1319 add_fixed_insn (NOP_INSN);
1320 insert_into_history (0, 1, NOP_INSN);
1321 }
1322
1323 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1324 the idea is to make it obvious at a glance that each errata is
1325 included. */
1326
1327 static void
1328 init_vr4120_conflicts (void)
1329 {
1330 #define CONFLICT(FIRST, SECOND) \
1331 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1332
1333 /* Errata 21 - [D]DIV[U] after [D]MACC */
1334 CONFLICT (MACC, DIV);
1335 CONFLICT (DMACC, DIV);
1336
1337 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1338 CONFLICT (DMULT, DMULT);
1339 CONFLICT (DMULT, DMACC);
1340 CONFLICT (DMACC, DMULT);
1341 CONFLICT (DMACC, DMACC);
1342
1343 /* Errata 24 - MT{LO,HI} after [D]MACC */
1344 CONFLICT (MACC, MTHILO);
1345 CONFLICT (DMACC, MTHILO);
1346
1347 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1348 instruction is executed immediately after a MACC or DMACC
1349 instruction, the result of [either instruction] is incorrect." */
1350 CONFLICT (MACC, MULT);
1351 CONFLICT (MACC, DMULT);
1352 CONFLICT (DMACC, MULT);
1353 CONFLICT (DMACC, DMULT);
1354
1355 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1356 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1357 DDIV or DDIVU instruction, the result of the MACC or
1358 DMACC instruction is incorrect.". */
1359 CONFLICT (DMULT, MACC);
1360 CONFLICT (DMULT, DMACC);
1361 CONFLICT (DIV, MACC);
1362 CONFLICT (DIV, DMACC);
1363
1364 #undef CONFLICT
1365 }
1366
1367 /* This function is called once, at assembler startup time. It should
1368 set up all the tables, etc. that the MD part of the assembler will need. */
1369
1370 void
1371 md_begin (void)
1372 {
1373 register const char *retval = NULL;
1374 int i = 0;
1375 int broken = 0;
1376
1377 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1378 as_warn (_("Could not set architecture and machine"));
1379
1380 op_hash = hash_new ();
1381
1382 for (i = 0; i < NUMOPCODES;)
1383 {
1384 const char *name = mips_opcodes[i].name;
1385
1386 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1387 if (retval != NULL)
1388 {
1389 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1390 mips_opcodes[i].name, retval);
1391 /* Probably a memory allocation problem? Give up now. */
1392 as_fatal (_("Broken assembler. No assembly attempted."));
1393 }
1394 do
1395 {
1396 if (mips_opcodes[i].pinfo != INSN_MACRO)
1397 {
1398 if (!validate_mips_insn (&mips_opcodes[i]))
1399 broken = 1;
1400 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1401 {
1402 create_insn (&nop_insn, mips_opcodes + i);
1403 nop_insn.fixed_p = 1;
1404 }
1405 }
1406 ++i;
1407 }
1408 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1409 }
1410
1411 mips16_op_hash = hash_new ();
1412
1413 i = 0;
1414 while (i < bfd_mips16_num_opcodes)
1415 {
1416 const char *name = mips16_opcodes[i].name;
1417
1418 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1419 if (retval != NULL)
1420 as_fatal (_("internal: can't hash `%s': %s"),
1421 mips16_opcodes[i].name, retval);
1422 do
1423 {
1424 if (mips16_opcodes[i].pinfo != INSN_MACRO
1425 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1426 != mips16_opcodes[i].match))
1427 {
1428 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1429 mips16_opcodes[i].name, mips16_opcodes[i].args);
1430 broken = 1;
1431 }
1432 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1433 {
1434 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1435 mips16_nop_insn.fixed_p = 1;
1436 }
1437 ++i;
1438 }
1439 while (i < bfd_mips16_num_opcodes
1440 && strcmp (mips16_opcodes[i].name, name) == 0);
1441 }
1442
1443 if (broken)
1444 as_fatal (_("Broken assembler. No assembly attempted."));
1445
1446 /* We add all the general register names to the symbol table. This
1447 helps us detect invalid uses of them. */
1448 for (i = 0; i < 32; i++)
1449 {
1450 char buf[5];
1451
1452 sprintf (buf, "$%d", i);
1453 symbol_table_insert (symbol_new (buf, reg_section, i,
1454 &zero_address_frag));
1455 }
1456 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1457 &zero_address_frag));
1458 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1459 &zero_address_frag));
1460 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1461 &zero_address_frag));
1462 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1463 &zero_address_frag));
1464 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1465 &zero_address_frag));
1466 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1467 &zero_address_frag));
1468 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1469 &zero_address_frag));
1470 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1471 &zero_address_frag));
1472 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1473 &zero_address_frag));
1474
1475 /* If we don't add these register names to the symbol table, they
1476 may end up being added as regular symbols by operand(), and then
1477 make it to the object file as undefined in case they're not
1478 regarded as local symbols. They're local in o32, since `$' is a
1479 local symbol prefix, but not in n32 or n64. */
1480 for (i = 0; i < 8; i++)
1481 {
1482 char buf[6];
1483
1484 sprintf (buf, "$fcc%i", i);
1485 symbol_table_insert (symbol_new (buf, reg_section, -1,
1486 &zero_address_frag));
1487 }
1488
1489 mips_no_prev_insn ();
1490
1491 mips_gprmask = 0;
1492 mips_cprmask[0] = 0;
1493 mips_cprmask[1] = 0;
1494 mips_cprmask[2] = 0;
1495 mips_cprmask[3] = 0;
1496
1497 /* set the default alignment for the text section (2**2) */
1498 record_alignment (text_section, 2);
1499
1500 bfd_set_gp_size (stdoutput, g_switch_value);
1501
1502 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1503 {
1504 /* On a native system, sections must be aligned to 16 byte
1505 boundaries. When configured for an embedded ELF target, we
1506 don't bother. */
1507 if (strcmp (TARGET_OS, "elf") != 0)
1508 {
1509 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1510 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1511 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1512 }
1513
1514 /* Create a .reginfo section for register masks and a .mdebug
1515 section for debugging information. */
1516 {
1517 segT seg;
1518 subsegT subseg;
1519 flagword flags;
1520 segT sec;
1521
1522 seg = now_seg;
1523 subseg = now_subseg;
1524
1525 /* The ABI says this section should be loaded so that the
1526 running program can access it. However, we don't load it
1527 if we are configured for an embedded target */
1528 flags = SEC_READONLY | SEC_DATA;
1529 if (strcmp (TARGET_OS, "elf") != 0)
1530 flags |= SEC_ALLOC | SEC_LOAD;
1531
1532 if (mips_abi != N64_ABI)
1533 {
1534 sec = subseg_new (".reginfo", (subsegT) 0);
1535
1536 bfd_set_section_flags (stdoutput, sec, flags);
1537 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1538
1539 #ifdef OBJ_ELF
1540 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1541 #endif
1542 }
1543 else
1544 {
1545 /* The 64-bit ABI uses a .MIPS.options section rather than
1546 .reginfo section. */
1547 sec = subseg_new (".MIPS.options", (subsegT) 0);
1548 bfd_set_section_flags (stdoutput, sec, flags);
1549 bfd_set_section_alignment (stdoutput, sec, 3);
1550
1551 #ifdef OBJ_ELF
1552 /* Set up the option header. */
1553 {
1554 Elf_Internal_Options opthdr;
1555 char *f;
1556
1557 opthdr.kind = ODK_REGINFO;
1558 opthdr.size = (sizeof (Elf_External_Options)
1559 + sizeof (Elf64_External_RegInfo));
1560 opthdr.section = 0;
1561 opthdr.info = 0;
1562 f = frag_more (sizeof (Elf_External_Options));
1563 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1564 (Elf_External_Options *) f);
1565
1566 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1567 }
1568 #endif
1569 }
1570
1571 if (ECOFF_DEBUGGING)
1572 {
1573 sec = subseg_new (".mdebug", (subsegT) 0);
1574 (void) bfd_set_section_flags (stdoutput, sec,
1575 SEC_HAS_CONTENTS | SEC_READONLY);
1576 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1577 }
1578 #ifdef OBJ_ELF
1579 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1580 {
1581 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1582 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1583 SEC_READONLY | SEC_RELOC
1584 | SEC_DEBUGGING);
1585 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1586 }
1587 #endif
1588
1589 subseg_set (seg, subseg);
1590 }
1591 }
1592
1593 if (! ECOFF_DEBUGGING)
1594 md_obj_begin ();
1595
1596 if (mips_fix_vr4120)
1597 init_vr4120_conflicts ();
1598 }
1599
1600 void
1601 md_mips_end (void)
1602 {
1603 if (! ECOFF_DEBUGGING)
1604 md_obj_end ();
1605 }
1606
1607 void
1608 md_assemble (char *str)
1609 {
1610 struct mips_cl_insn insn;
1611 bfd_reloc_code_real_type unused_reloc[3]
1612 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1613
1614 imm_expr.X_op = O_absent;
1615 imm2_expr.X_op = O_absent;
1616 offset_expr.X_op = O_absent;
1617 imm_reloc[0] = BFD_RELOC_UNUSED;
1618 imm_reloc[1] = BFD_RELOC_UNUSED;
1619 imm_reloc[2] = BFD_RELOC_UNUSED;
1620 offset_reloc[0] = BFD_RELOC_UNUSED;
1621 offset_reloc[1] = BFD_RELOC_UNUSED;
1622 offset_reloc[2] = BFD_RELOC_UNUSED;
1623
1624 if (mips_opts.mips16)
1625 mips16_ip (str, &insn);
1626 else
1627 {
1628 mips_ip (str, &insn);
1629 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1630 str, insn.insn_opcode));
1631 }
1632
1633 if (insn_error)
1634 {
1635 as_bad ("%s `%s'", insn_error, str);
1636 return;
1637 }
1638
1639 if (insn.insn_mo->pinfo == INSN_MACRO)
1640 {
1641 macro_start ();
1642 if (mips_opts.mips16)
1643 mips16_macro (&insn);
1644 else
1645 macro (&insn);
1646 macro_end ();
1647 }
1648 else
1649 {
1650 if (imm_expr.X_op != O_absent)
1651 append_insn (&insn, &imm_expr, imm_reloc);
1652 else if (offset_expr.X_op != O_absent)
1653 append_insn (&insn, &offset_expr, offset_reloc);
1654 else
1655 append_insn (&insn, NULL, unused_reloc);
1656 }
1657 }
1658
1659 /* Return true if the given relocation might need a matching %lo().
1660 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1661 applied to local symbols. */
1662
1663 static inline bfd_boolean
1664 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1665 {
1666 return (HAVE_IN_PLACE_ADDENDS
1667 && (reloc == BFD_RELOC_HI16_S
1668 || reloc == BFD_RELOC_MIPS_GOT16
1669 || reloc == BFD_RELOC_MIPS16_HI16_S));
1670 }
1671
1672 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1673 relocation. */
1674
1675 static inline bfd_boolean
1676 fixup_has_matching_lo_p (fixS *fixp)
1677 {
1678 return (fixp->fx_next != NULL
1679 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1680 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1681 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1682 && fixp->fx_offset == fixp->fx_next->fx_offset);
1683 }
1684
1685 /* See whether instruction IP reads register REG. CLASS is the type
1686 of register. */
1687
1688 static int
1689 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
1690 enum mips_regclass class)
1691 {
1692 if (class == MIPS16_REG)
1693 {
1694 assert (mips_opts.mips16);
1695 reg = mips16_to_32_reg_map[reg];
1696 class = MIPS_GR_REG;
1697 }
1698
1699 /* Don't report on general register ZERO, since it never changes. */
1700 if (class == MIPS_GR_REG && reg == ZERO)
1701 return 0;
1702
1703 if (class == MIPS_FP_REG)
1704 {
1705 assert (! mips_opts.mips16);
1706 /* If we are called with either $f0 or $f1, we must check $f0.
1707 This is not optimal, because it will introduce an unnecessary
1708 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1709 need to distinguish reading both $f0 and $f1 or just one of
1710 them. Note that we don't have to check the other way,
1711 because there is no instruction that sets both $f0 and $f1
1712 and requires a delay. */
1713 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1714 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
1715 == (reg &~ (unsigned) 1)))
1716 return 1;
1717 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1718 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
1719 == (reg &~ (unsigned) 1)))
1720 return 1;
1721 }
1722 else if (! mips_opts.mips16)
1723 {
1724 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1725 && EXTRACT_OPERAND (RS, *ip) == reg)
1726 return 1;
1727 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1728 && EXTRACT_OPERAND (RT, *ip) == reg)
1729 return 1;
1730 }
1731 else
1732 {
1733 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1734 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
1735 return 1;
1736 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1737 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
1738 return 1;
1739 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1740 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
1741 == reg))
1742 return 1;
1743 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1744 return 1;
1745 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1746 return 1;
1747 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1748 return 1;
1749 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1750 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
1751 return 1;
1752 }
1753
1754 return 0;
1755 }
1756
1757 /* This function returns true if modifying a register requires a
1758 delay. */
1759
1760 static int
1761 reg_needs_delay (unsigned int reg)
1762 {
1763 unsigned long prev_pinfo;
1764
1765 prev_pinfo = history[0].insn_mo->pinfo;
1766 if (! mips_opts.noreorder
1767 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1768 && ! gpr_interlocks)
1769 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1770 && ! cop_interlocks)))
1771 {
1772 /* A load from a coprocessor or from memory. All load delays
1773 delay the use of general register rt for one instruction. */
1774 /* Itbl support may require additional care here. */
1775 know (prev_pinfo & INSN_WRITE_GPR_T);
1776 if (reg == EXTRACT_OPERAND (RT, history[0]))
1777 return 1;
1778 }
1779
1780 return 0;
1781 }
1782
1783 /* Move all labels in insn_labels to the current insertion point. */
1784
1785 static void
1786 mips_move_labels (void)
1787 {
1788 struct insn_label_list *l;
1789 valueT val;
1790
1791 for (l = insn_labels; l != NULL; l = l->next)
1792 {
1793 assert (S_GET_SEGMENT (l->label) == now_seg);
1794 symbol_set_frag (l->label, frag_now);
1795 val = (valueT) frag_now_fix ();
1796 /* mips16 text labels are stored as odd. */
1797 if (mips_opts.mips16)
1798 ++val;
1799 S_SET_VALUE (l->label, val);
1800 }
1801 }
1802
1803 /* Mark instruction labels in mips16 mode. This permits the linker to
1804 handle them specially, such as generating jalx instructions when
1805 needed. We also make them odd for the duration of the assembly, in
1806 order to generate the right sort of code. We will make them even
1807 in the adjust_symtab routine, while leaving them marked. This is
1808 convenient for the debugger and the disassembler. The linker knows
1809 to make them odd again. */
1810
1811 static void
1812 mips16_mark_labels (void)
1813 {
1814 if (mips_opts.mips16)
1815 {
1816 struct insn_label_list *l;
1817 valueT val;
1818
1819 for (l = insn_labels; l != NULL; l = l->next)
1820 {
1821 #ifdef OBJ_ELF
1822 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1823 S_SET_OTHER (l->label, STO_MIPS16);
1824 #endif
1825 val = S_GET_VALUE (l->label);
1826 if ((val & 1) == 0)
1827 S_SET_VALUE (l->label, val + 1);
1828 }
1829 }
1830 }
1831
1832 /* End the current frag. Make it a variant frag and record the
1833 relaxation info. */
1834
1835 static void
1836 relax_close_frag (void)
1837 {
1838 mips_macro_warning.first_frag = frag_now;
1839 frag_var (rs_machine_dependent, 0, 0,
1840 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1841 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1842
1843 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1844 mips_relax.first_fixup = 0;
1845 }
1846
1847 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1848 See the comment above RELAX_ENCODE for more details. */
1849
1850 static void
1851 relax_start (symbolS *symbol)
1852 {
1853 assert (mips_relax.sequence == 0);
1854 mips_relax.sequence = 1;
1855 mips_relax.symbol = symbol;
1856 }
1857
1858 /* Start generating the second version of a relaxable sequence.
1859 See the comment above RELAX_ENCODE for more details. */
1860
1861 static void
1862 relax_switch (void)
1863 {
1864 assert (mips_relax.sequence == 1);
1865 mips_relax.sequence = 2;
1866 }
1867
1868 /* End the current relaxable sequence. */
1869
1870 static void
1871 relax_end (void)
1872 {
1873 assert (mips_relax.sequence == 2);
1874 relax_close_frag ();
1875 mips_relax.sequence = 0;
1876 }
1877
1878 /* Classify an instruction according to the FIX_VR4120_* enumeration.
1879 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1880 by VR4120 errata. */
1881
1882 static unsigned int
1883 classify_vr4120_insn (const char *name)
1884 {
1885 if (strncmp (name, "macc", 4) == 0)
1886 return FIX_VR4120_MACC;
1887 if (strncmp (name, "dmacc", 5) == 0)
1888 return FIX_VR4120_DMACC;
1889 if (strncmp (name, "mult", 4) == 0)
1890 return FIX_VR4120_MULT;
1891 if (strncmp (name, "dmult", 5) == 0)
1892 return FIX_VR4120_DMULT;
1893 if (strstr (name, "div"))
1894 return FIX_VR4120_DIV;
1895 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1896 return FIX_VR4120_MTHILO;
1897 return NUM_FIX_VR4120_CLASSES;
1898 }
1899
1900 /* Return the number of instructions that must separate INSN1 and INSN2,
1901 where INSN1 is the earlier instruction. Return the worst-case value
1902 for any INSN2 if INSN2 is null. */
1903
1904 static unsigned int
1905 insns_between (const struct mips_cl_insn *insn1,
1906 const struct mips_cl_insn *insn2)
1907 {
1908 unsigned long pinfo1, pinfo2;
1909
1910 /* This function needs to know which pinfo flags are set for INSN2
1911 and which registers INSN2 uses. The former is stored in PINFO2 and
1912 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
1913 will have every flag set and INSN2_USES_REG will always return true. */
1914 pinfo1 = insn1->insn_mo->pinfo;
1915 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
1916
1917 #define INSN2_USES_REG(REG, CLASS) \
1918 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1919
1920 /* For most targets, write-after-read dependencies on the HI and LO
1921 registers must be separated by at least two instructions. */
1922 if (!hilo_interlocks)
1923 {
1924 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1925 return 2;
1926 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1927 return 2;
1928 }
1929
1930 /* If we're working around r7000 errata, there must be two instructions
1931 between an mfhi or mflo and any instruction that uses the result. */
1932 if (mips_7000_hilo_fix
1933 && MF_HILO_INSN (pinfo1)
1934 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1935 return 2;
1936
1937 /* If working around VR4120 errata, check for combinations that need
1938 a single intervening instruction. */
1939 if (mips_fix_vr4120)
1940 {
1941 unsigned int class1, class2;
1942
1943 class1 = classify_vr4120_insn (insn1->insn_mo->name);
1944 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
1945 {
1946 if (insn2 == NULL)
1947 return 1;
1948 class2 = classify_vr4120_insn (insn2->insn_mo->name);
1949 if (vr4120_conflicts[class1] & (1 << class2))
1950 return 1;
1951 }
1952 }
1953
1954 if (!mips_opts.mips16)
1955 {
1956 /* Check for GPR or coprocessor load delays. All such delays
1957 are on the RT register. */
1958 /* Itbl support may require additional care here. */
1959 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
1960 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
1961 {
1962 know (pinfo1 & INSN_WRITE_GPR_T);
1963 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
1964 return 1;
1965 }
1966
1967 /* Check for generic coprocessor hazards.
1968
1969 This case is not handled very well. There is no special
1970 knowledge of CP0 handling, and the coprocessors other than
1971 the floating point unit are not distinguished at all. */
1972 /* Itbl support may require additional care here. FIXME!
1973 Need to modify this to include knowledge about
1974 user specified delays! */
1975 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
1976 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
1977 {
1978 /* Handle cases where INSN1 writes to a known general coprocessor
1979 register. There must be a one instruction delay before INSN2
1980 if INSN2 reads that register, otherwise no delay is needed. */
1981 if (pinfo1 & INSN_WRITE_FPR_T)
1982 {
1983 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
1984 return 1;
1985 }
1986 else if (pinfo1 & INSN_WRITE_FPR_S)
1987 {
1988 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
1989 return 1;
1990 }
1991 else
1992 {
1993 /* Read-after-write dependencies on the control registers
1994 require a two-instruction gap. */
1995 if ((pinfo1 & INSN_WRITE_COND_CODE)
1996 && (pinfo2 & INSN_READ_COND_CODE))
1997 return 2;
1998
1999 /* We don't know exactly what INSN1 does. If INSN2 is
2000 also a coprocessor instruction, assume there must be
2001 a one instruction gap. */
2002 if (pinfo2 & INSN_COP)
2003 return 1;
2004 }
2005 }
2006
2007 /* Check for read-after-write dependencies on the coprocessor
2008 control registers in cases where INSN1 does not need a general
2009 coprocessor delay. This means that INSN1 is a floating point
2010 comparison instruction. */
2011 /* Itbl support may require additional care here. */
2012 else if (!cop_interlocks
2013 && (pinfo1 & INSN_WRITE_COND_CODE)
2014 && (pinfo2 & INSN_READ_COND_CODE))
2015 return 1;
2016 }
2017
2018 #undef INSN2_USES_REG
2019
2020 return 0;
2021 }
2022
2023 /* Return the number of nops that would be needed to work around the
2024 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2025 the MAX_VR4130_NOPS instructions described by HISTORY. */
2026
2027 static int
2028 nops_for_vr4130 (const struct mips_cl_insn *history,
2029 const struct mips_cl_insn *insn)
2030 {
2031 int i, j, reg;
2032
2033 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2034 are not affected by the errata. */
2035 if (insn != 0
2036 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2037 || strcmp (insn->insn_mo->name, "mtlo") == 0
2038 || strcmp (insn->insn_mo->name, "mthi") == 0))
2039 return 0;
2040
2041 /* Search for the first MFLO or MFHI. */
2042 for (i = 0; i < MAX_VR4130_NOPS; i++)
2043 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2044 {
2045 /* Extract the destination register. */
2046 if (mips_opts.mips16)
2047 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2048 else
2049 reg = EXTRACT_OPERAND (RD, history[i]);
2050
2051 /* No nops are needed if INSN reads that register. */
2052 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2053 return 0;
2054
2055 /* ...or if any of the intervening instructions do. */
2056 for (j = 0; j < i; j++)
2057 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2058 return 0;
2059
2060 return MAX_VR4130_NOPS - i;
2061 }
2062 return 0;
2063 }
2064
2065 /* Return the number of nops that would be needed if instruction INSN
2066 immediately followed the MAX_NOPS instructions given by HISTORY,
2067 where HISTORY[0] is the most recent instruction. If INSN is null,
2068 return the worse-case number of nops for any instruction. */
2069
2070 static int
2071 nops_for_insn (const struct mips_cl_insn *history,
2072 const struct mips_cl_insn *insn)
2073 {
2074 int i, nops, tmp_nops;
2075
2076 nops = 0;
2077 for (i = 0; i < MAX_DELAY_NOPS; i++)
2078 if (!history[i].noreorder_p)
2079 {
2080 tmp_nops = insns_between (history + i, insn) - i;
2081 if (tmp_nops > nops)
2082 nops = tmp_nops;
2083 }
2084
2085 if (mips_fix_vr4130)
2086 {
2087 tmp_nops = nops_for_vr4130 (history, insn);
2088 if (tmp_nops > nops)
2089 nops = tmp_nops;
2090 }
2091
2092 return nops;
2093 }
2094
2095 /* The variable arguments provide NUM_INSNS extra instructions that
2096 might be added to HISTORY. Return the largest number of nops that
2097 would be needed after the extended sequence. */
2098
2099 static int
2100 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2101 {
2102 va_list args;
2103 struct mips_cl_insn buffer[MAX_NOPS];
2104 struct mips_cl_insn *cursor;
2105 int nops;
2106
2107 va_start (args, history);
2108 cursor = buffer + num_insns;
2109 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2110 while (cursor > buffer)
2111 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2112
2113 nops = nops_for_insn (buffer, NULL);
2114 va_end (args);
2115 return nops;
2116 }
2117
2118 /* Like nops_for_insn, but if INSN is a branch, take into account the
2119 worst-case delay for the branch target. */
2120
2121 static int
2122 nops_for_insn_or_target (const struct mips_cl_insn *history,
2123 const struct mips_cl_insn *insn)
2124 {
2125 int nops, tmp_nops;
2126
2127 nops = nops_for_insn (history, insn);
2128 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2129 | INSN_COND_BRANCH_DELAY
2130 | INSN_COND_BRANCH_LIKELY))
2131 {
2132 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2133 if (tmp_nops > nops)
2134 nops = tmp_nops;
2135 }
2136 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2137 {
2138 tmp_nops = nops_for_sequence (1, history, insn);
2139 if (tmp_nops > nops)
2140 nops = tmp_nops;
2141 }
2142 return nops;
2143 }
2144
2145 /* Output an instruction. IP is the instruction information.
2146 ADDRESS_EXPR is an operand of the instruction to be used with
2147 RELOC_TYPE. */
2148
2149 static void
2150 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2151 bfd_reloc_code_real_type *reloc_type)
2152 {
2153 register unsigned long prev_pinfo, pinfo;
2154 relax_stateT prev_insn_frag_type = 0;
2155 bfd_boolean relaxed_branch = FALSE;
2156
2157 /* Mark instruction labels in mips16 mode. */
2158 mips16_mark_labels ();
2159
2160 prev_pinfo = history[0].insn_mo->pinfo;
2161 pinfo = ip->insn_mo->pinfo;
2162
2163 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2164 {
2165 /* There are a lot of optimizations we could do that we don't.
2166 In particular, we do not, in general, reorder instructions.
2167 If you use gcc with optimization, it will reorder
2168 instructions and generally do much more optimization then we
2169 do here; repeating all that work in the assembler would only
2170 benefit hand written assembly code, and does not seem worth
2171 it. */
2172 int nops = (mips_optimize == 0
2173 ? nops_for_insn (history, NULL)
2174 : nops_for_insn_or_target (history, ip));
2175 if (nops > 0)
2176 {
2177 fragS *old_frag;
2178 unsigned long old_frag_offset;
2179 int i;
2180
2181 old_frag = frag_now;
2182 old_frag_offset = frag_now_fix ();
2183
2184 for (i = 0; i < nops; i++)
2185 emit_nop ();
2186
2187 if (listing)
2188 {
2189 listing_prev_line ();
2190 /* We may be at the start of a variant frag. In case we
2191 are, make sure there is enough space for the frag
2192 after the frags created by listing_prev_line. The
2193 argument to frag_grow here must be at least as large
2194 as the argument to all other calls to frag_grow in
2195 this file. We don't have to worry about being in the
2196 middle of a variant frag, because the variants insert
2197 all needed nop instructions themselves. */
2198 frag_grow (40);
2199 }
2200
2201 mips_move_labels ();
2202
2203 #ifndef NO_ECOFF_DEBUGGING
2204 if (ECOFF_DEBUGGING)
2205 ecoff_fix_loc (old_frag, old_frag_offset);
2206 #endif
2207 }
2208 }
2209 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2210 {
2211 /* Work out how many nops in prev_nop_frag are needed by IP. */
2212 int nops = nops_for_insn_or_target (history, ip);
2213 assert (nops <= prev_nop_frag_holds);
2214
2215 /* Enforce NOPS as a minimum. */
2216 if (nops > prev_nop_frag_required)
2217 prev_nop_frag_required = nops;
2218
2219 if (prev_nop_frag_holds == prev_nop_frag_required)
2220 {
2221 /* Settle for the current number of nops. Update the history
2222 accordingly (for the benefit of any future .set reorder code). */
2223 prev_nop_frag = NULL;
2224 insert_into_history (prev_nop_frag_since,
2225 prev_nop_frag_holds, NOP_INSN);
2226 }
2227 else
2228 {
2229 /* Allow this instruction to replace one of the nops that was
2230 tentatively added to prev_nop_frag. */
2231 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2232 prev_nop_frag_holds--;
2233 prev_nop_frag_since++;
2234 }
2235 }
2236
2237 #ifdef OBJ_ELF
2238 /* The value passed to dwarf2_emit_insn is the distance between
2239 the beginning of the current instruction and the address that
2240 should be recorded in the debug tables. For MIPS16 debug info
2241 we want to use ISA-encoded addresses, so we pass -1 for an
2242 address higher by one than the current. */
2243 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2244 #endif
2245
2246 /* Record the frag type before frag_var. */
2247 if (history[0].frag)
2248 prev_insn_frag_type = history[0].frag->fr_type;
2249
2250 if (address_expr
2251 && *reloc_type == BFD_RELOC_16_PCREL_S2
2252 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2253 || pinfo & INSN_COND_BRANCH_LIKELY)
2254 && mips_relax_branch
2255 /* Don't try branch relaxation within .set nomacro, or within
2256 .set noat if we use $at for PIC computations. If it turns
2257 out that the branch was out-of-range, we'll get an error. */
2258 && !mips_opts.warn_about_macros
2259 && !(mips_opts.noat && mips_pic != NO_PIC)
2260 && !mips_opts.mips16)
2261 {
2262 relaxed_branch = TRUE;
2263 add_relaxed_insn (ip, (relaxed_branch_length
2264 (NULL, NULL,
2265 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2266 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2267 : 0)), 4,
2268 RELAX_BRANCH_ENCODE
2269 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2270 pinfo & INSN_COND_BRANCH_LIKELY,
2271 pinfo & INSN_WRITE_GPR_31,
2272 0),
2273 address_expr->X_add_symbol,
2274 address_expr->X_add_number);
2275 *reloc_type = BFD_RELOC_UNUSED;
2276 }
2277 else if (*reloc_type > BFD_RELOC_UNUSED)
2278 {
2279 /* We need to set up a variant frag. */
2280 assert (mips_opts.mips16 && address_expr != NULL);
2281 add_relaxed_insn (ip, 4, 0,
2282 RELAX_MIPS16_ENCODE
2283 (*reloc_type - BFD_RELOC_UNUSED,
2284 mips16_small, mips16_ext,
2285 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2286 history[0].mips16_absolute_jump_p),
2287 make_expr_symbol (address_expr), 0);
2288 }
2289 else if (mips_opts.mips16
2290 && ! ip->use_extend
2291 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2292 {
2293 /* Make sure there is enough room to swap this instruction with
2294 a following jump instruction. */
2295 frag_grow (6);
2296 add_fixed_insn (ip);
2297 }
2298 else
2299 {
2300 if (mips_opts.mips16
2301 && mips_opts.noreorder
2302 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2303 as_warn (_("extended instruction in delay slot"));
2304
2305 if (mips_relax.sequence)
2306 {
2307 /* If we've reached the end of this frag, turn it into a variant
2308 frag and record the information for the instructions we've
2309 written so far. */
2310 if (frag_room () < 4)
2311 relax_close_frag ();
2312 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2313 }
2314
2315 if (mips_relax.sequence != 2)
2316 mips_macro_warning.sizes[0] += 4;
2317 if (mips_relax.sequence != 1)
2318 mips_macro_warning.sizes[1] += 4;
2319
2320 if (mips_opts.mips16)
2321 {
2322 ip->fixed_p = 1;
2323 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2324 }
2325 add_fixed_insn (ip);
2326 }
2327
2328 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2329 {
2330 if (address_expr->X_op == O_constant)
2331 {
2332 unsigned int tmp;
2333
2334 switch (*reloc_type)
2335 {
2336 case BFD_RELOC_32:
2337 ip->insn_opcode |= address_expr->X_add_number;
2338 break;
2339
2340 case BFD_RELOC_MIPS_HIGHEST:
2341 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2342 ip->insn_opcode |= tmp & 0xffff;
2343 break;
2344
2345 case BFD_RELOC_MIPS_HIGHER:
2346 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2347 ip->insn_opcode |= tmp & 0xffff;
2348 break;
2349
2350 case BFD_RELOC_HI16_S:
2351 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2352 ip->insn_opcode |= tmp & 0xffff;
2353 break;
2354
2355 case BFD_RELOC_HI16:
2356 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2357 break;
2358
2359 case BFD_RELOC_UNUSED:
2360 case BFD_RELOC_LO16:
2361 case BFD_RELOC_MIPS_GOT_DISP:
2362 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2363 break;
2364
2365 case BFD_RELOC_MIPS_JMP:
2366 if ((address_expr->X_add_number & 3) != 0)
2367 as_bad (_("jump to misaligned address (0x%lx)"),
2368 (unsigned long) address_expr->X_add_number);
2369 if (address_expr->X_add_number & ~0xfffffff)
2370 as_bad (_("jump address range overflow (0x%lx)"),
2371 (unsigned long) address_expr->X_add_number);
2372 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2373 break;
2374
2375 case BFD_RELOC_MIPS16_JMP:
2376 if ((address_expr->X_add_number & 3) != 0)
2377 as_bad (_("jump to misaligned address (0x%lx)"),
2378 (unsigned long) address_expr->X_add_number);
2379 if (address_expr->X_add_number & ~0xfffffff)
2380 as_bad (_("jump address range overflow (0x%lx)"),
2381 (unsigned long) address_expr->X_add_number);
2382 ip->insn_opcode |=
2383 (((address_expr->X_add_number & 0x7c0000) << 3)
2384 | ((address_expr->X_add_number & 0xf800000) >> 7)
2385 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2386 break;
2387
2388 case BFD_RELOC_16_PCREL_S2:
2389 goto need_reloc;
2390
2391 default:
2392 internalError ();
2393 }
2394 }
2395 else if (*reloc_type < BFD_RELOC_UNUSED)
2396 need_reloc:
2397 {
2398 reloc_howto_type *howto;
2399 int i;
2400
2401 /* In a compound relocation, it is the final (outermost)
2402 operator that determines the relocated field. */
2403 for (i = 1; i < 3; i++)
2404 if (reloc_type[i] == BFD_RELOC_UNUSED)
2405 break;
2406
2407 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2408 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2409 bfd_get_reloc_size (howto),
2410 address_expr,
2411 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2412 reloc_type[0]);
2413
2414 /* These relocations can have an addend that won't fit in
2415 4 octets for 64bit assembly. */
2416 if (HAVE_64BIT_GPRS
2417 && ! howto->partial_inplace
2418 && (reloc_type[0] == BFD_RELOC_16
2419 || reloc_type[0] == BFD_RELOC_32
2420 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2421 || reloc_type[0] == BFD_RELOC_HI16_S
2422 || reloc_type[0] == BFD_RELOC_LO16
2423 || reloc_type[0] == BFD_RELOC_GPREL16
2424 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2425 || reloc_type[0] == BFD_RELOC_GPREL32
2426 || reloc_type[0] == BFD_RELOC_64
2427 || reloc_type[0] == BFD_RELOC_CTOR
2428 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2429 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2430 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2431 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2432 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2433 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2434 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2435 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2436 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2437 ip->fixp[0]->fx_no_overflow = 1;
2438
2439 if (mips_relax.sequence)
2440 {
2441 if (mips_relax.first_fixup == 0)
2442 mips_relax.first_fixup = ip->fixp[0];
2443 }
2444 else if (reloc_needs_lo_p (*reloc_type))
2445 {
2446 struct mips_hi_fixup *hi_fixup;
2447
2448 /* Reuse the last entry if it already has a matching %lo. */
2449 hi_fixup = mips_hi_fixup_list;
2450 if (hi_fixup == 0
2451 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2452 {
2453 hi_fixup = ((struct mips_hi_fixup *)
2454 xmalloc (sizeof (struct mips_hi_fixup)));
2455 hi_fixup->next = mips_hi_fixup_list;
2456 mips_hi_fixup_list = hi_fixup;
2457 }
2458 hi_fixup->fixp = ip->fixp[0];
2459 hi_fixup->seg = now_seg;
2460 }
2461
2462 /* Add fixups for the second and third relocations, if given.
2463 Note that the ABI allows the second relocation to be
2464 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2465 moment we only use RSS_UNDEF, but we could add support
2466 for the others if it ever becomes necessary. */
2467 for (i = 1; i < 3; i++)
2468 if (reloc_type[i] != BFD_RELOC_UNUSED)
2469 {
2470 ip->fixp[i] = fix_new (ip->frag, ip->where,
2471 ip->fixp[0]->fx_size, NULL, 0,
2472 FALSE, reloc_type[i]);
2473
2474 /* Use fx_tcbit to mark compound relocs. */
2475 ip->fixp[0]->fx_tcbit = 1;
2476 ip->fixp[i]->fx_tcbit = 1;
2477 }
2478 }
2479 }
2480 install_insn (ip);
2481
2482 /* Update the register mask information. */
2483 if (! mips_opts.mips16)
2484 {
2485 if (pinfo & INSN_WRITE_GPR_D)
2486 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2487 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2488 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2489 if (pinfo & INSN_READ_GPR_S)
2490 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2491 if (pinfo & INSN_WRITE_GPR_31)
2492 mips_gprmask |= 1 << RA;
2493 if (pinfo & INSN_WRITE_FPR_D)
2494 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2495 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2496 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2497 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2498 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2499 if ((pinfo & INSN_READ_FPR_R) != 0)
2500 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2501 if (pinfo & INSN_COP)
2502 {
2503 /* We don't keep enough information to sort these cases out.
2504 The itbl support does keep this information however, although
2505 we currently don't support itbl fprmats as part of the cop
2506 instruction. May want to add this support in the future. */
2507 }
2508 /* Never set the bit for $0, which is always zero. */
2509 mips_gprmask &= ~1 << 0;
2510 }
2511 else
2512 {
2513 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2514 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2515 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2516 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2517 if (pinfo & MIPS16_INSN_WRITE_Z)
2518 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2519 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2520 mips_gprmask |= 1 << TREG;
2521 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2522 mips_gprmask |= 1 << SP;
2523 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2524 mips_gprmask |= 1 << RA;
2525 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2526 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2527 if (pinfo & MIPS16_INSN_READ_Z)
2528 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2529 if (pinfo & MIPS16_INSN_READ_GPR_X)
2530 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2531 }
2532
2533 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2534 {
2535 /* Filling the branch delay slot is more complex. We try to
2536 switch the branch with the previous instruction, which we can
2537 do if the previous instruction does not set up a condition
2538 that the branch tests and if the branch is not itself the
2539 target of any branch. */
2540 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2541 || (pinfo & INSN_COND_BRANCH_DELAY))
2542 {
2543 if (mips_optimize < 2
2544 /* If we have seen .set volatile or .set nomove, don't
2545 optimize. */
2546 || mips_opts.nomove != 0
2547 /* We can't swap if the previous instruction's position
2548 is fixed. */
2549 || history[0].fixed_p
2550 /* If the previous previous insn was in a .set
2551 noreorder, we can't swap. Actually, the MIPS
2552 assembler will swap in this situation. However, gcc
2553 configured -with-gnu-as will generate code like
2554 .set noreorder
2555 lw $4,XXX
2556 .set reorder
2557 INSN
2558 bne $4,$0,foo
2559 in which we can not swap the bne and INSN. If gcc is
2560 not configured -with-gnu-as, it does not output the
2561 .set pseudo-ops. */
2562 || history[1].noreorder_p
2563 /* If the branch is itself the target of a branch, we
2564 can not swap. We cheat on this; all we check for is
2565 whether there is a label on this instruction. If
2566 there are any branches to anything other than a
2567 label, users must use .set noreorder. */
2568 || insn_labels != NULL
2569 /* If the previous instruction is in a variant frag
2570 other than this branch's one, we cannot do the swap.
2571 This does not apply to the mips16, which uses variant
2572 frags for different purposes. */
2573 || (! mips_opts.mips16
2574 && prev_insn_frag_type == rs_machine_dependent)
2575 /* Check for conflicts between the branch and the instructions
2576 before the candidate delay slot. */
2577 || nops_for_insn (history + 1, ip) > 0
2578 /* Check for conflicts between the swapped sequence and the
2579 target of the branch. */
2580 || nops_for_sequence (2, history + 1, ip, history) > 0
2581 /* We do not swap with a trap instruction, since it
2582 complicates trap handlers to have the trap
2583 instruction be in a delay slot. */
2584 || (prev_pinfo & INSN_TRAP)
2585 /* If the branch reads a register that the previous
2586 instruction sets, we can not swap. */
2587 || (! mips_opts.mips16
2588 && (prev_pinfo & INSN_WRITE_GPR_T)
2589 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2590 MIPS_GR_REG))
2591 || (! mips_opts.mips16
2592 && (prev_pinfo & INSN_WRITE_GPR_D)
2593 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2594 MIPS_GR_REG))
2595 || (mips_opts.mips16
2596 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2597 && (insn_uses_reg
2598 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2599 MIPS16_REG)))
2600 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2601 && (insn_uses_reg
2602 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2603 MIPS16_REG)))
2604 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2605 && (insn_uses_reg
2606 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2607 MIPS16_REG)))
2608 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2609 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2610 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2611 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2612 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2613 && insn_uses_reg (ip,
2614 MIPS16OP_EXTRACT_REG32R
2615 (history[0].insn_opcode),
2616 MIPS_GR_REG))))
2617 /* If the branch writes a register that the previous
2618 instruction sets, we can not swap (we know that
2619 branches write only to RD or to $31). */
2620 || (! mips_opts.mips16
2621 && (prev_pinfo & INSN_WRITE_GPR_T)
2622 && (((pinfo & INSN_WRITE_GPR_D)
2623 && (EXTRACT_OPERAND (RT, history[0])
2624 == EXTRACT_OPERAND (RD, *ip)))
2625 || ((pinfo & INSN_WRITE_GPR_31)
2626 && EXTRACT_OPERAND (RT, history[0]) == RA)))
2627 || (! mips_opts.mips16
2628 && (prev_pinfo & INSN_WRITE_GPR_D)
2629 && (((pinfo & INSN_WRITE_GPR_D)
2630 && (EXTRACT_OPERAND (RD, history[0])
2631 == EXTRACT_OPERAND (RD, *ip)))
2632 || ((pinfo & INSN_WRITE_GPR_31)
2633 && EXTRACT_OPERAND (RD, history[0]) == RA)))
2634 || (mips_opts.mips16
2635 && (pinfo & MIPS16_INSN_WRITE_31)
2636 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2637 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2638 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2639 == RA))))
2640 /* If the branch writes a register that the previous
2641 instruction reads, we can not swap (we know that
2642 branches only write to RD or to $31). */
2643 || (! mips_opts.mips16
2644 && (pinfo & INSN_WRITE_GPR_D)
2645 && insn_uses_reg (&history[0],
2646 EXTRACT_OPERAND (RD, *ip),
2647 MIPS_GR_REG))
2648 || (! mips_opts.mips16
2649 && (pinfo & INSN_WRITE_GPR_31)
2650 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2651 || (mips_opts.mips16
2652 && (pinfo & MIPS16_INSN_WRITE_31)
2653 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2654 /* If one instruction sets a condition code and the
2655 other one uses a condition code, we can not swap. */
2656 || ((pinfo & INSN_READ_COND_CODE)
2657 && (prev_pinfo & INSN_WRITE_COND_CODE))
2658 || ((pinfo & INSN_WRITE_COND_CODE)
2659 && (prev_pinfo & INSN_READ_COND_CODE))
2660 /* If the previous instruction uses the PC, we can not
2661 swap. */
2662 || (mips_opts.mips16
2663 && (prev_pinfo & MIPS16_INSN_READ_PC))
2664 /* If the previous instruction had a fixup in mips16
2665 mode, we can not swap. This normally means that the
2666 previous instruction was a 4 byte branch anyhow. */
2667 || (mips_opts.mips16 && history[0].fixp[0])
2668 /* If the previous instruction is a sync, sync.l, or
2669 sync.p, we can not swap. */
2670 || (prev_pinfo & INSN_SYNC))
2671 {
2672 /* We could do even better for unconditional branches to
2673 portions of this object file; we could pick up the
2674 instruction at the destination, put it in the delay
2675 slot, and bump the destination address. */
2676 insert_into_history (0, 1, ip);
2677 emit_nop ();
2678 if (mips_relax.sequence)
2679 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2680 }
2681 else
2682 {
2683 /* It looks like we can actually do the swap. */
2684 struct mips_cl_insn delay = history[0];
2685 if (mips_opts.mips16)
2686 {
2687 know (delay.frag == ip->frag);
2688 move_insn (ip, delay.frag, delay.where);
2689 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
2690 }
2691 else if (relaxed_branch)
2692 {
2693 /* Add the delay slot instruction to the end of the
2694 current frag and shrink the fixed part of the
2695 original frag. If the branch occupies the tail of
2696 the latter, move it backwards to cover the gap. */
2697 delay.frag->fr_fix -= 4;
2698 if (delay.frag == ip->frag)
2699 move_insn (ip, ip->frag, ip->where - 4);
2700 add_fixed_insn (&delay);
2701 }
2702 else
2703 {
2704 move_insn (&delay, ip->frag, ip->where);
2705 move_insn (ip, history[0].frag, history[0].where);
2706 }
2707 history[0] = *ip;
2708 delay.fixed_p = 1;
2709 insert_into_history (0, 1, &delay);
2710 }
2711
2712 /* If that was an unconditional branch, forget the previous
2713 insn information. */
2714 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2715 mips_no_prev_insn ();
2716 }
2717 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2718 {
2719 /* We don't yet optimize a branch likely. What we should do
2720 is look at the target, copy the instruction found there
2721 into the delay slot, and increment the branch to jump to
2722 the next instruction. */
2723 insert_into_history (0, 1, ip);
2724 emit_nop ();
2725 }
2726 else
2727 insert_into_history (0, 1, ip);
2728 }
2729 else
2730 insert_into_history (0, 1, ip);
2731
2732 /* We just output an insn, so the next one doesn't have a label. */
2733 mips_clear_insn_labels ();
2734 }
2735
2736 /* Forget that there was any previous instruction or label. */
2737
2738 static void
2739 mips_no_prev_insn (void)
2740 {
2741 prev_nop_frag = NULL;
2742 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
2743 mips_clear_insn_labels ();
2744 }
2745
2746 /* This function must be called before we emit something other than
2747 instructions. It is like mips_no_prev_insn except that it inserts
2748 any NOPS that might be needed by previous instructions. */
2749
2750 void
2751 mips_emit_delays (void)
2752 {
2753 if (! mips_opts.noreorder)
2754 {
2755 int nops = nops_for_insn (history, NULL);
2756 if (nops > 0)
2757 {
2758 while (nops-- > 0)
2759 add_fixed_insn (NOP_INSN);
2760 mips_move_labels ();
2761 }
2762 }
2763 mips_no_prev_insn ();
2764 }
2765
2766 /* Start a (possibly nested) noreorder block. */
2767
2768 static void
2769 start_noreorder (void)
2770 {
2771 if (mips_opts.noreorder == 0)
2772 {
2773 unsigned int i;
2774 int nops;
2775
2776 /* None of the instructions before the .set noreorder can be moved. */
2777 for (i = 0; i < ARRAY_SIZE (history); i++)
2778 history[i].fixed_p = 1;
2779
2780 /* Insert any nops that might be needed between the .set noreorder
2781 block and the previous instructions. We will later remove any
2782 nops that turn out not to be needed. */
2783 nops = nops_for_insn (history, NULL);
2784 if (nops > 0)
2785 {
2786 if (mips_optimize != 0)
2787 {
2788 /* Record the frag which holds the nop instructions, so
2789 that we can remove them if we don't need them. */
2790 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2791 prev_nop_frag = frag_now;
2792 prev_nop_frag_holds = nops;
2793 prev_nop_frag_required = 0;
2794 prev_nop_frag_since = 0;
2795 }
2796
2797 for (; nops > 0; --nops)
2798 add_fixed_insn (NOP_INSN);
2799
2800 /* Move on to a new frag, so that it is safe to simply
2801 decrease the size of prev_nop_frag. */
2802 frag_wane (frag_now);
2803 frag_new (0);
2804 mips_move_labels ();
2805 }
2806 mips16_mark_labels ();
2807 mips_clear_insn_labels ();
2808 }
2809 mips_opts.noreorder++;
2810 mips_any_noreorder = 1;
2811 }
2812
2813 /* End a nested noreorder block. */
2814
2815 static void
2816 end_noreorder (void)
2817 {
2818 mips_opts.noreorder--;
2819 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2820 {
2821 /* Commit to inserting prev_nop_frag_required nops and go back to
2822 handling nop insertion the .set reorder way. */
2823 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2824 * (mips_opts.mips16 ? 2 : 4));
2825 insert_into_history (prev_nop_frag_since,
2826 prev_nop_frag_required, NOP_INSN);
2827 prev_nop_frag = NULL;
2828 }
2829 }
2830
2831 /* Set up global variables for the start of a new macro. */
2832
2833 static void
2834 macro_start (void)
2835 {
2836 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2837 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2838 && (history[0].insn_mo->pinfo
2839 & (INSN_UNCOND_BRANCH_DELAY
2840 | INSN_COND_BRANCH_DELAY
2841 | INSN_COND_BRANCH_LIKELY)) != 0);
2842 }
2843
2844 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2845 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2846 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2847
2848 static const char *
2849 macro_warning (relax_substateT subtype)
2850 {
2851 if (subtype & RELAX_DELAY_SLOT)
2852 return _("Macro instruction expanded into multiple instructions"
2853 " in a branch delay slot");
2854 else if (subtype & RELAX_NOMACRO)
2855 return _("Macro instruction expanded into multiple instructions");
2856 else
2857 return 0;
2858 }
2859
2860 /* Finish up a macro. Emit warnings as appropriate. */
2861
2862 static void
2863 macro_end (void)
2864 {
2865 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2866 {
2867 relax_substateT subtype;
2868
2869 /* Set up the relaxation warning flags. */
2870 subtype = 0;
2871 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2872 subtype |= RELAX_SECOND_LONGER;
2873 if (mips_opts.warn_about_macros)
2874 subtype |= RELAX_NOMACRO;
2875 if (mips_macro_warning.delay_slot_p)
2876 subtype |= RELAX_DELAY_SLOT;
2877
2878 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2879 {
2880 /* Either the macro has a single implementation or both
2881 implementations are longer than 4 bytes. Emit the
2882 warning now. */
2883 const char *msg = macro_warning (subtype);
2884 if (msg != 0)
2885 as_warn (msg);
2886 }
2887 else
2888 {
2889 /* One implementation might need a warning but the other
2890 definitely doesn't. */
2891 mips_macro_warning.first_frag->fr_subtype |= subtype;
2892 }
2893 }
2894 }
2895
2896 /* Read a macro's relocation codes from *ARGS and store them in *R.
2897 The first argument in *ARGS will be either the code for a single
2898 relocation or -1 followed by the three codes that make up a
2899 composite relocation. */
2900
2901 static void
2902 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2903 {
2904 int i, next;
2905
2906 next = va_arg (*args, int);
2907 if (next >= 0)
2908 r[0] = (bfd_reloc_code_real_type) next;
2909 else
2910 for (i = 0; i < 3; i++)
2911 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2912 }
2913
2914 /* Build an instruction created by a macro expansion. This is passed
2915 a pointer to the count of instructions created so far, an
2916 expression, the name of the instruction to build, an operand format
2917 string, and corresponding arguments. */
2918
2919 static void
2920 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2921 {
2922 const struct mips_opcode *mo;
2923 struct mips_cl_insn insn;
2924 bfd_reloc_code_real_type r[3];
2925 va_list args;
2926
2927 va_start (args, fmt);
2928
2929 if (mips_opts.mips16)
2930 {
2931 mips16_macro_build (ep, name, fmt, args);
2932 va_end (args);
2933 return;
2934 }
2935
2936 r[0] = BFD_RELOC_UNUSED;
2937 r[1] = BFD_RELOC_UNUSED;
2938 r[2] = BFD_RELOC_UNUSED;
2939 mo = (struct mips_opcode *) hash_find (op_hash, name);
2940 assert (mo);
2941 assert (strcmp (name, mo->name) == 0);
2942
2943 /* Search until we get a match for NAME. It is assumed here that
2944 macros will never generate MDMX or MIPS-3D instructions. */
2945 while (strcmp (fmt, mo->args) != 0
2946 || mo->pinfo == INSN_MACRO
2947 || !OPCODE_IS_MEMBER (mo,
2948 (mips_opts.isa
2949 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2950 mips_opts.arch)
2951 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
2952 {
2953 ++mo;
2954 assert (mo->name);
2955 assert (strcmp (name, mo->name) == 0);
2956 }
2957
2958 create_insn (&insn, mo);
2959 for (;;)
2960 {
2961 switch (*fmt++)
2962 {
2963 case '\0':
2964 break;
2965
2966 case ',':
2967 case '(':
2968 case ')':
2969 continue;
2970
2971 case '+':
2972 switch (*fmt++)
2973 {
2974 case 'A':
2975 case 'E':
2976 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
2977 continue;
2978
2979 case 'B':
2980 case 'F':
2981 /* Note that in the macro case, these arguments are already
2982 in MSB form. (When handling the instruction in the
2983 non-macro case, these arguments are sizes from which
2984 MSB values must be calculated.) */
2985 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
2986 continue;
2987
2988 case 'C':
2989 case 'G':
2990 case 'H':
2991 /* Note that in the macro case, these arguments are already
2992 in MSBD form. (When handling the instruction in the
2993 non-macro case, these arguments are sizes from which
2994 MSBD values must be calculated.) */
2995 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
2996 continue;
2997
2998 default:
2999 internalError ();
3000 }
3001 continue;
3002
3003 case 't':
3004 case 'w':
3005 case 'E':
3006 INSERT_OPERAND (RT, insn, va_arg (args, int));
3007 continue;
3008
3009 case 'c':
3010 INSERT_OPERAND (CODE, insn, va_arg (args, int));
3011 continue;
3012
3013 case 'T':
3014 case 'W':
3015 INSERT_OPERAND (FT, insn, va_arg (args, int));
3016 continue;
3017
3018 case 'd':
3019 case 'G':
3020 case 'K':
3021 INSERT_OPERAND (RD, insn, va_arg (args, int));
3022 continue;
3023
3024 case 'U':
3025 {
3026 int tmp = va_arg (args, int);
3027
3028 INSERT_OPERAND (RT, insn, tmp);
3029 INSERT_OPERAND (RD, insn, tmp);
3030 continue;
3031 }
3032
3033 case 'V':
3034 case 'S':
3035 INSERT_OPERAND (FS, insn, va_arg (args, int));
3036 continue;
3037
3038 case 'z':
3039 continue;
3040
3041 case '<':
3042 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3043 continue;
3044
3045 case 'D':
3046 INSERT_OPERAND (FD, insn, va_arg (args, int));
3047 continue;
3048
3049 case 'B':
3050 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3051 continue;
3052
3053 case 'J':
3054 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3055 continue;
3056
3057 case 'q':
3058 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3059 continue;
3060
3061 case 'b':
3062 case 's':
3063 case 'r':
3064 case 'v':
3065 INSERT_OPERAND (RS, insn, va_arg (args, int));
3066 continue;
3067
3068 case 'i':
3069 case 'j':
3070 case 'o':
3071 macro_read_relocs (&args, r);
3072 assert (*r == BFD_RELOC_GPREL16
3073 || *r == BFD_RELOC_MIPS_LITERAL
3074 || *r == BFD_RELOC_MIPS_HIGHER
3075 || *r == BFD_RELOC_HI16_S
3076 || *r == BFD_RELOC_LO16
3077 || *r == BFD_RELOC_MIPS_GOT16
3078 || *r == BFD_RELOC_MIPS_CALL16
3079 || *r == BFD_RELOC_MIPS_GOT_DISP
3080 || *r == BFD_RELOC_MIPS_GOT_PAGE
3081 || *r == BFD_RELOC_MIPS_GOT_OFST
3082 || *r == BFD_RELOC_MIPS_GOT_LO16
3083 || *r == BFD_RELOC_MIPS_CALL_LO16);
3084 continue;
3085
3086 case 'u':
3087 macro_read_relocs (&args, r);
3088 assert (ep != NULL
3089 && (ep->X_op == O_constant
3090 || (ep->X_op == O_symbol
3091 && (*r == BFD_RELOC_MIPS_HIGHEST
3092 || *r == BFD_RELOC_HI16_S
3093 || *r == BFD_RELOC_HI16
3094 || *r == BFD_RELOC_GPREL16
3095 || *r == BFD_RELOC_MIPS_GOT_HI16
3096 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3097 continue;
3098
3099 case 'p':
3100 assert (ep != NULL);
3101 /*
3102 * This allows macro() to pass an immediate expression for
3103 * creating short branches without creating a symbol.
3104 * Note that the expression still might come from the assembly
3105 * input, in which case the value is not checked for range nor
3106 * is a relocation entry generated (yuck).
3107 */
3108 if (ep->X_op == O_constant)
3109 {
3110 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3111 ep = NULL;
3112 }
3113 else
3114 *r = BFD_RELOC_16_PCREL_S2;
3115 continue;
3116
3117 case 'a':
3118 assert (ep != NULL);
3119 *r = BFD_RELOC_MIPS_JMP;
3120 continue;
3121
3122 case 'C':
3123 insn.insn_opcode |= va_arg (args, unsigned long);
3124 continue;
3125
3126 default:
3127 internalError ();
3128 }
3129 break;
3130 }
3131 va_end (args);
3132 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3133
3134 append_insn (&insn, ep, r);
3135 }
3136
3137 static void
3138 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3139 va_list args)
3140 {
3141 struct mips_opcode *mo;
3142 struct mips_cl_insn insn;
3143 bfd_reloc_code_real_type r[3]
3144 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3145
3146 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3147 assert (mo);
3148 assert (strcmp (name, mo->name) == 0);
3149
3150 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3151 {
3152 ++mo;
3153 assert (mo->name);
3154 assert (strcmp (name, mo->name) == 0);
3155 }
3156
3157 create_insn (&insn, mo);
3158 for (;;)
3159 {
3160 int c;
3161
3162 c = *fmt++;
3163 switch (c)
3164 {
3165 case '\0':
3166 break;
3167
3168 case ',':
3169 case '(':
3170 case ')':
3171 continue;
3172
3173 case 'y':
3174 case 'w':
3175 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3176 continue;
3177
3178 case 'x':
3179 case 'v':
3180 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3181 continue;
3182
3183 case 'z':
3184 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3185 continue;
3186
3187 case 'Z':
3188 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3189 continue;
3190
3191 case '0':
3192 case 'S':
3193 case 'P':
3194 case 'R':
3195 continue;
3196
3197 case 'X':
3198 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3199 continue;
3200
3201 case 'Y':
3202 {
3203 int regno;
3204
3205 regno = va_arg (args, int);
3206 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3207 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3208 }
3209 continue;
3210
3211 case '<':
3212 case '>':
3213 case '4':
3214 case '5':
3215 case 'H':
3216 case 'W':
3217 case 'D':
3218 case 'j':
3219 case '8':
3220 case 'V':
3221 case 'C':
3222 case 'U':
3223 case 'k':
3224 case 'K':
3225 case 'p':
3226 case 'q':
3227 {
3228 assert (ep != NULL);
3229
3230 if (ep->X_op != O_constant)
3231 *r = (int) BFD_RELOC_UNUSED + c;
3232 else
3233 {
3234 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3235 FALSE, &insn.insn_opcode, &insn.use_extend,
3236 &insn.extend);
3237 ep = NULL;
3238 *r = BFD_RELOC_UNUSED;
3239 }
3240 }
3241 continue;
3242
3243 case '6':
3244 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3245 continue;
3246 }
3247
3248 break;
3249 }
3250
3251 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3252
3253 append_insn (&insn, ep, r);
3254 }
3255
3256 /*
3257 * Generate a "jalr" instruction with a relocation hint to the called
3258 * function. This occurs in NewABI PIC code.
3259 */
3260 static void
3261 macro_build_jalr (expressionS *ep)
3262 {
3263 char *f = NULL;
3264
3265 if (HAVE_NEWABI)
3266 {
3267 frag_grow (8);
3268 f = frag_more (0);
3269 }
3270 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3271 if (HAVE_NEWABI)
3272 fix_new_exp (frag_now, f - frag_now->fr_literal,
3273 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3274 }
3275
3276 /*
3277 * Generate a "lui" instruction.
3278 */
3279 static void
3280 macro_build_lui (expressionS *ep, int regnum)
3281 {
3282 expressionS high_expr;
3283 const struct mips_opcode *mo;
3284 struct mips_cl_insn insn;
3285 bfd_reloc_code_real_type r[3]
3286 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3287 const char *name = "lui";
3288 const char *fmt = "t,u";
3289
3290 assert (! mips_opts.mips16);
3291
3292 high_expr = *ep;
3293
3294 if (high_expr.X_op == O_constant)
3295 {
3296 /* we can compute the instruction now without a relocation entry */
3297 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3298 >> 16) & 0xffff;
3299 *r = BFD_RELOC_UNUSED;
3300 }
3301 else
3302 {
3303 assert (ep->X_op == O_symbol);
3304 /* _gp_disp is a special case, used from s_cpload.
3305 __gnu_local_gp is used if mips_no_shared. */
3306 assert (mips_pic == NO_PIC
3307 || (! HAVE_NEWABI
3308 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3309 || (! mips_in_shared
3310 && strcmp (S_GET_NAME (ep->X_add_symbol),
3311 "__gnu_local_gp") == 0));
3312 *r = BFD_RELOC_HI16_S;
3313 }
3314
3315 mo = hash_find (op_hash, name);
3316 assert (strcmp (name, mo->name) == 0);
3317 assert (strcmp (fmt, mo->args) == 0);
3318 create_insn (&insn, mo);
3319
3320 insn.insn_opcode = insn.insn_mo->match;
3321 INSERT_OPERAND (RT, insn, regnum);
3322 if (*r == BFD_RELOC_UNUSED)
3323 {
3324 insn.insn_opcode |= high_expr.X_add_number;
3325 append_insn (&insn, NULL, r);
3326 }
3327 else
3328 append_insn (&insn, &high_expr, r);
3329 }
3330
3331 /* Generate a sequence of instructions to do a load or store from a constant
3332 offset off of a base register (breg) into/from a target register (treg),
3333 using AT if necessary. */
3334 static void
3335 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3336 int treg, int breg, int dbl)
3337 {
3338 assert (ep->X_op == O_constant);
3339
3340 /* Sign-extending 32-bit constants makes their handling easier. */
3341 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3342 == ~((bfd_vma) 0x7fffffff)))
3343 {
3344 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3345 as_bad (_("constant too large"));
3346
3347 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3348 - 0x80000000);
3349 }
3350
3351 /* Right now, this routine can only handle signed 32-bit constants. */
3352 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3353 as_warn (_("operand overflow"));
3354
3355 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3356 {
3357 /* Signed 16-bit offset will fit in the op. Easy! */
3358 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3359 }
3360 else
3361 {
3362 /* 32-bit offset, need multiple instructions and AT, like:
3363 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3364 addu $tempreg,$tempreg,$breg
3365 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3366 to handle the complete offset. */
3367 macro_build_lui (ep, AT);
3368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3369 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3370
3371 if (mips_opts.noat)
3372 as_bad (_("Macro used $at after \".set noat\""));
3373 }
3374 }
3375
3376 /* set_at()
3377 * Generates code to set the $at register to true (one)
3378 * if reg is less than the immediate expression.
3379 */
3380 static void
3381 set_at (int reg, int unsignedp)
3382 {
3383 if (imm_expr.X_op == O_constant
3384 && imm_expr.X_add_number >= -0x8000
3385 && imm_expr.X_add_number < 0x8000)
3386 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3387 AT, reg, BFD_RELOC_LO16);
3388 else
3389 {
3390 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3391 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3392 }
3393 }
3394
3395 static void
3396 normalize_constant_expr (expressionS *ex)
3397 {
3398 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3399 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3400 - 0x80000000);
3401 }
3402
3403 /* Warn if an expression is not a constant. */
3404
3405 static void
3406 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3407 {
3408 if (ex->X_op == O_big)
3409 as_bad (_("unsupported large constant"));
3410 else if (ex->X_op != O_constant)
3411 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3412
3413 normalize_constant_expr (ex);
3414 }
3415
3416 /* Count the leading zeroes by performing a binary chop. This is a
3417 bulky bit of source, but performance is a LOT better for the
3418 majority of values than a simple loop to count the bits:
3419 for (lcnt = 0; (lcnt < 32); lcnt++)
3420 if ((v) & (1 << (31 - lcnt)))
3421 break;
3422 However it is not code size friendly, and the gain will drop a bit
3423 on certain cached systems.
3424 */
3425 #define COUNT_TOP_ZEROES(v) \
3426 (((v) & ~0xffff) == 0 \
3427 ? ((v) & ~0xff) == 0 \
3428 ? ((v) & ~0xf) == 0 \
3429 ? ((v) & ~0x3) == 0 \
3430 ? ((v) & ~0x1) == 0 \
3431 ? !(v) \
3432 ? 32 \
3433 : 31 \
3434 : 30 \
3435 : ((v) & ~0x7) == 0 \
3436 ? 29 \
3437 : 28 \
3438 : ((v) & ~0x3f) == 0 \
3439 ? ((v) & ~0x1f) == 0 \
3440 ? 27 \
3441 : 26 \
3442 : ((v) & ~0x7f) == 0 \
3443 ? 25 \
3444 : 24 \
3445 : ((v) & ~0xfff) == 0 \
3446 ? ((v) & ~0x3ff) == 0 \
3447 ? ((v) & ~0x1ff) == 0 \
3448 ? 23 \
3449 : 22 \
3450 : ((v) & ~0x7ff) == 0 \
3451 ? 21 \
3452 : 20 \
3453 : ((v) & ~0x3fff) == 0 \
3454 ? ((v) & ~0x1fff) == 0 \
3455 ? 19 \
3456 : 18 \
3457 : ((v) & ~0x7fff) == 0 \
3458 ? 17 \
3459 : 16 \
3460 : ((v) & ~0xffffff) == 0 \
3461 ? ((v) & ~0xfffff) == 0 \
3462 ? ((v) & ~0x3ffff) == 0 \
3463 ? ((v) & ~0x1ffff) == 0 \
3464 ? 15 \
3465 : 14 \
3466 : ((v) & ~0x7ffff) == 0 \
3467 ? 13 \
3468 : 12 \
3469 : ((v) & ~0x3fffff) == 0 \
3470 ? ((v) & ~0x1fffff) == 0 \
3471 ? 11 \
3472 : 10 \
3473 : ((v) & ~0x7fffff) == 0 \
3474 ? 9 \
3475 : 8 \
3476 : ((v) & ~0xfffffff) == 0 \
3477 ? ((v) & ~0x3ffffff) == 0 \
3478 ? ((v) & ~0x1ffffff) == 0 \
3479 ? 7 \
3480 : 6 \
3481 : ((v) & ~0x7ffffff) == 0 \
3482 ? 5 \
3483 : 4 \
3484 : ((v) & ~0x3fffffff) == 0 \
3485 ? ((v) & ~0x1fffffff) == 0 \
3486 ? 3 \
3487 : 2 \
3488 : ((v) & ~0x7fffffff) == 0 \
3489 ? 1 \
3490 : 0)
3491
3492 /* load_register()
3493 * This routine generates the least number of instructions necessary to load
3494 * an absolute expression value into a register.
3495 */
3496 static void
3497 load_register (int reg, expressionS *ep, int dbl)
3498 {
3499 int freg;
3500 expressionS hi32, lo32;
3501
3502 if (ep->X_op != O_big)
3503 {
3504 assert (ep->X_op == O_constant);
3505
3506 /* Sign-extending 32-bit constants makes their handling easier. */
3507 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3508 == ~((bfd_vma) 0x7fffffff)))
3509 {
3510 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3511 as_bad (_("constant too large"));
3512
3513 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3514 - 0x80000000);
3515 }
3516
3517 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3518 {
3519 /* We can handle 16 bit signed values with an addiu to
3520 $zero. No need to ever use daddiu here, since $zero and
3521 the result are always correct in 32 bit mode. */
3522 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3523 return;
3524 }
3525 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3526 {
3527 /* We can handle 16 bit unsigned values with an ori to
3528 $zero. */
3529 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3530 return;
3531 }
3532 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3533 {
3534 /* 32 bit values require an lui. */
3535 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3536 if ((ep->X_add_number & 0xffff) != 0)
3537 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3538 return;
3539 }
3540 }
3541
3542 /* The value is larger than 32 bits. */
3543
3544 if (HAVE_32BIT_GPRS)
3545 {
3546 as_bad (_("Number (0x%lx) larger than 32 bits"),
3547 (unsigned long) ep->X_add_number);
3548 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3549 return;
3550 }
3551
3552 if (ep->X_op != O_big)
3553 {
3554 hi32 = *ep;
3555 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3556 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3557 hi32.X_add_number &= 0xffffffff;
3558 lo32 = *ep;
3559 lo32.X_add_number &= 0xffffffff;
3560 }
3561 else
3562 {
3563 assert (ep->X_add_number > 2);
3564 if (ep->X_add_number == 3)
3565 generic_bignum[3] = 0;
3566 else if (ep->X_add_number > 4)
3567 as_bad (_("Number larger than 64 bits"));
3568 lo32.X_op = O_constant;
3569 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3570 hi32.X_op = O_constant;
3571 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3572 }
3573
3574 if (hi32.X_add_number == 0)
3575 freg = 0;
3576 else
3577 {
3578 int shift, bit;
3579 unsigned long hi, lo;
3580
3581 if (hi32.X_add_number == (offsetT) 0xffffffff)
3582 {
3583 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3584 {
3585 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3586 return;
3587 }
3588 if (lo32.X_add_number & 0x80000000)
3589 {
3590 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3591 if (lo32.X_add_number & 0xffff)
3592 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3593 return;
3594 }
3595 }
3596
3597 /* Check for 16bit shifted constant. We know that hi32 is
3598 non-zero, so start the mask on the first bit of the hi32
3599 value. */
3600 shift = 17;
3601 do
3602 {
3603 unsigned long himask, lomask;
3604
3605 if (shift < 32)
3606 {
3607 himask = 0xffff >> (32 - shift);
3608 lomask = (0xffff << shift) & 0xffffffff;
3609 }
3610 else
3611 {
3612 himask = 0xffff << (shift - 32);
3613 lomask = 0;
3614 }
3615 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3616 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3617 {
3618 expressionS tmp;
3619
3620 tmp.X_op = O_constant;
3621 if (shift < 32)
3622 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3623 | (lo32.X_add_number >> shift));
3624 else
3625 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3626 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3627 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3628 reg, reg, (shift >= 32) ? shift - 32 : shift);
3629 return;
3630 }
3631 ++shift;
3632 }
3633 while (shift <= (64 - 16));
3634
3635 /* Find the bit number of the lowest one bit, and store the
3636 shifted value in hi/lo. */
3637 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3638 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3639 if (lo != 0)
3640 {
3641 bit = 0;
3642 while ((lo & 1) == 0)
3643 {
3644 lo >>= 1;
3645 ++bit;
3646 }
3647 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3648 hi >>= bit;
3649 }
3650 else
3651 {
3652 bit = 32;
3653 while ((hi & 1) == 0)
3654 {
3655 hi >>= 1;
3656 ++bit;
3657 }
3658 lo = hi;
3659 hi = 0;
3660 }
3661
3662 /* Optimize if the shifted value is a (power of 2) - 1. */
3663 if ((hi == 0 && ((lo + 1) & lo) == 0)
3664 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3665 {
3666 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3667 if (shift != 0)
3668 {
3669 expressionS tmp;
3670
3671 /* This instruction will set the register to be all
3672 ones. */
3673 tmp.X_op = O_constant;
3674 tmp.X_add_number = (offsetT) -1;
3675 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3676 if (bit != 0)
3677 {
3678 bit += shift;
3679 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3680 reg, reg, (bit >= 32) ? bit - 32 : bit);
3681 }
3682 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3683 reg, reg, (shift >= 32) ? shift - 32 : shift);
3684 return;
3685 }
3686 }
3687
3688 /* Sign extend hi32 before calling load_register, because we can
3689 generally get better code when we load a sign extended value. */
3690 if ((hi32.X_add_number & 0x80000000) != 0)
3691 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3692 load_register (reg, &hi32, 0);
3693 freg = reg;
3694 }
3695 if ((lo32.X_add_number & 0xffff0000) == 0)
3696 {
3697 if (freg != 0)
3698 {
3699 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3700 freg = reg;
3701 }
3702 }
3703 else
3704 {
3705 expressionS mid16;
3706
3707 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3708 {
3709 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3710 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3711 return;
3712 }
3713
3714 if (freg != 0)
3715 {
3716 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3717 freg = reg;
3718 }
3719 mid16 = lo32;
3720 mid16.X_add_number >>= 16;
3721 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3722 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3723 freg = reg;
3724 }
3725 if ((lo32.X_add_number & 0xffff) != 0)
3726 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3727 }
3728
3729 static inline void
3730 load_delay_nop (void)
3731 {
3732 if (!gpr_interlocks)
3733 macro_build (NULL, "nop", "");
3734 }
3735
3736 /* Load an address into a register. */
3737
3738 static void
3739 load_address (int reg, expressionS *ep, int *used_at)
3740 {
3741 if (ep->X_op != O_constant
3742 && ep->X_op != O_symbol)
3743 {
3744 as_bad (_("expression too complex"));
3745 ep->X_op = O_constant;
3746 }
3747
3748 if (ep->X_op == O_constant)
3749 {
3750 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3751 return;
3752 }
3753
3754 if (mips_pic == NO_PIC)
3755 {
3756 /* If this is a reference to a GP relative symbol, we want
3757 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3758 Otherwise we want
3759 lui $reg,<sym> (BFD_RELOC_HI16_S)
3760 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3761 If we have an addend, we always use the latter form.
3762
3763 With 64bit address space and a usable $at we want
3764 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3765 lui $at,<sym> (BFD_RELOC_HI16_S)
3766 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3767 daddiu $at,<sym> (BFD_RELOC_LO16)
3768 dsll32 $reg,0
3769 daddu $reg,$reg,$at
3770
3771 If $at is already in use, we use a path which is suboptimal
3772 on superscalar processors.
3773 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3774 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3775 dsll $reg,16
3776 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3777 dsll $reg,16
3778 daddiu $reg,<sym> (BFD_RELOC_LO16)
3779
3780 For GP relative symbols in 64bit address space we can use
3781 the same sequence as in 32bit address space. */
3782 if (HAVE_64BIT_SYMBOLS)
3783 {
3784 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3785 && !nopic_need_relax (ep->X_add_symbol, 1))
3786 {
3787 relax_start (ep->X_add_symbol);
3788 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3789 mips_gp_register, BFD_RELOC_GPREL16);
3790 relax_switch ();
3791 }
3792
3793 if (*used_at == 0 && !mips_opts.noat)
3794 {
3795 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3796 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3797 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3798 BFD_RELOC_MIPS_HIGHER);
3799 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3800 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3801 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3802 *used_at = 1;
3803 }
3804 else
3805 {
3806 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3807 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3808 BFD_RELOC_MIPS_HIGHER);
3809 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3810 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3811 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3812 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3813 }
3814
3815 if (mips_relax.sequence)
3816 relax_end ();
3817 }
3818 else
3819 {
3820 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3821 && !nopic_need_relax (ep->X_add_symbol, 1))
3822 {
3823 relax_start (ep->X_add_symbol);
3824 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3825 mips_gp_register, BFD_RELOC_GPREL16);
3826 relax_switch ();
3827 }
3828 macro_build_lui (ep, reg);
3829 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3830 reg, reg, BFD_RELOC_LO16);
3831 if (mips_relax.sequence)
3832 relax_end ();
3833 }
3834 }
3835 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3836 {
3837 expressionS ex;
3838
3839 /* If this is a reference to an external symbol, we want
3840 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3841 Otherwise we want
3842 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3843 nop
3844 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3845 If there is a constant, it must be added in after.
3846
3847 If we have NewABI, we want
3848 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3849 unless we're referencing a global symbol with a non-zero
3850 offset, in which case cst must be added separately. */
3851 if (HAVE_NEWABI)
3852 {
3853 if (ep->X_add_number)
3854 {
3855 ex.X_add_number = ep->X_add_number;
3856 ep->X_add_number = 0;
3857 relax_start (ep->X_add_symbol);
3858 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3859 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3860 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3861 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3862 ex.X_op = O_constant;
3863 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3864 reg, reg, BFD_RELOC_LO16);
3865 ep->X_add_number = ex.X_add_number;
3866 relax_switch ();
3867 }
3868 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3869 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3870 if (mips_relax.sequence)
3871 relax_end ();
3872 }
3873 else
3874 {
3875 ex.X_add_number = ep->X_add_number;
3876 ep->X_add_number = 0;
3877 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3878 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3879 load_delay_nop ();
3880 relax_start (ep->X_add_symbol);
3881 relax_switch ();
3882 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3883 BFD_RELOC_LO16);
3884 relax_end ();
3885
3886 if (ex.X_add_number != 0)
3887 {
3888 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3889 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3890 ex.X_op = O_constant;
3891 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3892 reg, reg, BFD_RELOC_LO16);
3893 }
3894 }
3895 }
3896 else if (mips_pic == SVR4_PIC)
3897 {
3898 expressionS ex;
3899
3900 /* This is the large GOT case. If this is a reference to an
3901 external symbol, we want
3902 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3903 addu $reg,$reg,$gp
3904 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3905
3906 Otherwise, for a reference to a local symbol in old ABI, we want
3907 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3908 nop
3909 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3910 If there is a constant, it must be added in after.
3911
3912 In the NewABI, for local symbols, with or without offsets, we want:
3913 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3914 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3915 */
3916 if (HAVE_NEWABI)
3917 {
3918 ex.X_add_number = ep->X_add_number;
3919 ep->X_add_number = 0;
3920 relax_start (ep->X_add_symbol);
3921 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3922 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3923 reg, reg, mips_gp_register);
3924 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3925 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3926 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3927 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3928 else if (ex.X_add_number)
3929 {
3930 ex.X_op = O_constant;
3931 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3932 BFD_RELOC_LO16);
3933 }
3934
3935 ep->X_add_number = ex.X_add_number;
3936 relax_switch ();
3937 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3938 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3939 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3940 BFD_RELOC_MIPS_GOT_OFST);
3941 relax_end ();
3942 }
3943 else
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, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3949 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3950 reg, reg, mips_gp_register);
3951 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3952 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3953 relax_switch ();
3954 if (reg_needs_delay (mips_gp_register))
3955 {
3956 /* We need a nop before loading from $gp. This special
3957 check is required because the lui which starts the main
3958 instruction stream does not refer to $gp, and so will not
3959 insert the nop which may be required. */
3960 macro_build (NULL, "nop", "");
3961 }
3962 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3963 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3964 load_delay_nop ();
3965 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3966 BFD_RELOC_LO16);
3967 relax_end ();
3968
3969 if (ex.X_add_number != 0)
3970 {
3971 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3972 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3973 ex.X_op = O_constant;
3974 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3975 BFD_RELOC_LO16);
3976 }
3977 }
3978 }
3979 else
3980 abort ();
3981
3982 if (mips_opts.noat && *used_at == 1)
3983 as_bad (_("Macro used $at after \".set noat\""));
3984 }
3985
3986 /* Move the contents of register SOURCE into register DEST. */
3987
3988 static void
3989 move_register (int dest, int source)
3990 {
3991 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
3992 dest, source, 0);
3993 }
3994
3995 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
3996 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
3997 The two alternatives are:
3998
3999 Global symbol Local sybmol
4000 ------------- ------------
4001 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4002 ... ...
4003 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4004
4005 load_got_offset emits the first instruction and add_got_offset
4006 emits the second for a 16-bit offset or add_got_offset_hilo emits
4007 a sequence to add a 32-bit offset using a scratch register. */
4008
4009 static void
4010 load_got_offset (int dest, expressionS *local)
4011 {
4012 expressionS global;
4013
4014 global = *local;
4015 global.X_add_number = 0;
4016
4017 relax_start (local->X_add_symbol);
4018 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4019 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4020 relax_switch ();
4021 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4022 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4023 relax_end ();
4024 }
4025
4026 static void
4027 add_got_offset (int dest, expressionS *local)
4028 {
4029 expressionS global;
4030
4031 global.X_op = O_constant;
4032 global.X_op_symbol = NULL;
4033 global.X_add_symbol = NULL;
4034 global.X_add_number = local->X_add_number;
4035
4036 relax_start (local->X_add_symbol);
4037 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4038 dest, dest, BFD_RELOC_LO16);
4039 relax_switch ();
4040 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4041 relax_end ();
4042 }
4043
4044 static void
4045 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4046 {
4047 expressionS global;
4048 int hold_mips_optimize;
4049
4050 global.X_op = O_constant;
4051 global.X_op_symbol = NULL;
4052 global.X_add_symbol = NULL;
4053 global.X_add_number = local->X_add_number;
4054
4055 relax_start (local->X_add_symbol);
4056 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4057 relax_switch ();
4058 /* Set mips_optimize around the lui instruction to avoid
4059 inserting an unnecessary nop after the lw. */
4060 hold_mips_optimize = mips_optimize;
4061 mips_optimize = 2;
4062 macro_build_lui (&global, tmp);
4063 mips_optimize = hold_mips_optimize;
4064 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4065 relax_end ();
4066
4067 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4068 }
4069
4070 /*
4071 * Build macros
4072 * This routine implements the seemingly endless macro or synthesized
4073 * instructions and addressing modes in the mips assembly language. Many
4074 * of these macros are simple and are similar to each other. These could
4075 * probably be handled by some kind of table or grammar approach instead of
4076 * this verbose method. Others are not simple macros but are more like
4077 * optimizing code generation.
4078 * One interesting optimization is when several store macros appear
4079 * consecutively that would load AT with the upper half of the same address.
4080 * The ensuing load upper instructions are ommited. This implies some kind
4081 * of global optimization. We currently only optimize within a single macro.
4082 * For many of the load and store macros if the address is specified as a
4083 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4084 * first load register 'at' with zero and use it as the base register. The
4085 * mips assembler simply uses register $zero. Just one tiny optimization
4086 * we're missing.
4087 */
4088 static void
4089 macro (struct mips_cl_insn *ip)
4090 {
4091 register int treg, sreg, dreg, breg;
4092 int tempreg;
4093 int mask;
4094 int used_at = 0;
4095 expressionS expr1;
4096 const char *s;
4097 const char *s2;
4098 const char *fmt;
4099 int likely = 0;
4100 int dbl = 0;
4101 int coproc = 0;
4102 int lr = 0;
4103 int imm = 0;
4104 int call = 0;
4105 int off;
4106 offsetT maxnum;
4107 bfd_reloc_code_real_type r;
4108 int hold_mips_optimize;
4109
4110 assert (! mips_opts.mips16);
4111
4112 treg = (ip->insn_opcode >> 16) & 0x1f;
4113 dreg = (ip->insn_opcode >> 11) & 0x1f;
4114 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4115 mask = ip->insn_mo->mask;
4116
4117 expr1.X_op = O_constant;
4118 expr1.X_op_symbol = NULL;
4119 expr1.X_add_symbol = NULL;
4120 expr1.X_add_number = 1;
4121
4122 switch (mask)
4123 {
4124 case M_DABS:
4125 dbl = 1;
4126 case M_ABS:
4127 /* bgez $a0,.+12
4128 move v0,$a0
4129 sub v0,$zero,$a0
4130 */
4131
4132 start_noreorder ();
4133
4134 expr1.X_add_number = 8;
4135 macro_build (&expr1, "bgez", "s,p", sreg);
4136 if (dreg == sreg)
4137 macro_build (NULL, "nop", "", 0);
4138 else
4139 move_register (dreg, sreg);
4140 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4141
4142 end_noreorder ();
4143 break;
4144
4145 case M_ADD_I:
4146 s = "addi";
4147 s2 = "add";
4148 goto do_addi;
4149 case M_ADDU_I:
4150 s = "addiu";
4151 s2 = "addu";
4152 goto do_addi;
4153 case M_DADD_I:
4154 dbl = 1;
4155 s = "daddi";
4156 s2 = "dadd";
4157 goto do_addi;
4158 case M_DADDU_I:
4159 dbl = 1;
4160 s = "daddiu";
4161 s2 = "daddu";
4162 do_addi:
4163 if (imm_expr.X_op == O_constant
4164 && imm_expr.X_add_number >= -0x8000
4165 && imm_expr.X_add_number < 0x8000)
4166 {
4167 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4168 break;
4169 }
4170 used_at = 1;
4171 load_register (AT, &imm_expr, dbl);
4172 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4173 break;
4174
4175 case M_AND_I:
4176 s = "andi";
4177 s2 = "and";
4178 goto do_bit;
4179 case M_OR_I:
4180 s = "ori";
4181 s2 = "or";
4182 goto do_bit;
4183 case M_NOR_I:
4184 s = "";
4185 s2 = "nor";
4186 goto do_bit;
4187 case M_XOR_I:
4188 s = "xori";
4189 s2 = "xor";
4190 do_bit:
4191 if (imm_expr.X_op == O_constant
4192 && imm_expr.X_add_number >= 0
4193 && imm_expr.X_add_number < 0x10000)
4194 {
4195 if (mask != M_NOR_I)
4196 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4197 else
4198 {
4199 macro_build (&imm_expr, "ori", "t,r,i",
4200 treg, sreg, BFD_RELOC_LO16);
4201 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4202 }
4203 break;
4204 }
4205
4206 used_at = 1;
4207 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4208 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4209 break;
4210
4211 case M_BEQ_I:
4212 s = "beq";
4213 goto beq_i;
4214 case M_BEQL_I:
4215 s = "beql";
4216 likely = 1;
4217 goto beq_i;
4218 case M_BNE_I:
4219 s = "bne";
4220 goto beq_i;
4221 case M_BNEL_I:
4222 s = "bnel";
4223 likely = 1;
4224 beq_i:
4225 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4226 {
4227 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4228 break;
4229 }
4230 used_at = 1;
4231 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4232 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4233 break;
4234
4235 case M_BGEL:
4236 likely = 1;
4237 case M_BGE:
4238 if (treg == 0)
4239 {
4240 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4241 break;
4242 }
4243 if (sreg == 0)
4244 {
4245 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4246 break;
4247 }
4248 used_at = 1;
4249 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4250 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4251 break;
4252
4253 case M_BGTL_I:
4254 likely = 1;
4255 case M_BGT_I:
4256 /* check for > max integer */
4257 maxnum = 0x7fffffff;
4258 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4259 {
4260 maxnum <<= 16;
4261 maxnum |= 0xffff;
4262 maxnum <<= 16;
4263 maxnum |= 0xffff;
4264 }
4265 if (imm_expr.X_op == O_constant
4266 && imm_expr.X_add_number >= maxnum
4267 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4268 {
4269 do_false:
4270 /* result is always false */
4271 if (! likely)
4272 macro_build (NULL, "nop", "", 0);
4273 else
4274 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4275 break;
4276 }
4277 if (imm_expr.X_op != O_constant)
4278 as_bad (_("Unsupported large constant"));
4279 ++imm_expr.X_add_number;
4280 /* FALLTHROUGH */
4281 case M_BGE_I:
4282 case M_BGEL_I:
4283 if (mask == M_BGEL_I)
4284 likely = 1;
4285 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4286 {
4287 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4288 break;
4289 }
4290 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4291 {
4292 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4293 break;
4294 }
4295 maxnum = 0x7fffffff;
4296 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4297 {
4298 maxnum <<= 16;
4299 maxnum |= 0xffff;
4300 maxnum <<= 16;
4301 maxnum |= 0xffff;
4302 }
4303 maxnum = - maxnum - 1;
4304 if (imm_expr.X_op == O_constant
4305 && imm_expr.X_add_number <= maxnum
4306 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4307 {
4308 do_true:
4309 /* result is always true */
4310 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4311 macro_build (&offset_expr, "b", "p");
4312 break;
4313 }
4314 used_at = 1;
4315 set_at (sreg, 0);
4316 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4317 break;
4318
4319 case M_BGEUL:
4320 likely = 1;
4321 case M_BGEU:
4322 if (treg == 0)
4323 goto do_true;
4324 if (sreg == 0)
4325 {
4326 macro_build (&offset_expr, likely ? "beql" : "beq",
4327 "s,t,p", 0, treg);
4328 break;
4329 }
4330 used_at = 1;
4331 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4332 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4333 break;
4334
4335 case M_BGTUL_I:
4336 likely = 1;
4337 case M_BGTU_I:
4338 if (sreg == 0
4339 || (HAVE_32BIT_GPRS
4340 && imm_expr.X_op == O_constant
4341 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4342 goto do_false;
4343 if (imm_expr.X_op != O_constant)
4344 as_bad (_("Unsupported large constant"));
4345 ++imm_expr.X_add_number;
4346 /* FALLTHROUGH */
4347 case M_BGEU_I:
4348 case M_BGEUL_I:
4349 if (mask == M_BGEUL_I)
4350 likely = 1;
4351 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4352 goto do_true;
4353 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4354 {
4355 macro_build (&offset_expr, likely ? "bnel" : "bne",
4356 "s,t,p", sreg, 0);
4357 break;
4358 }
4359 used_at = 1;
4360 set_at (sreg, 1);
4361 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4362 break;
4363
4364 case M_BGTL:
4365 likely = 1;
4366 case M_BGT:
4367 if (treg == 0)
4368 {
4369 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4370 break;
4371 }
4372 if (sreg == 0)
4373 {
4374 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4375 break;
4376 }
4377 used_at = 1;
4378 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4379 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4380 break;
4381
4382 case M_BGTUL:
4383 likely = 1;
4384 case M_BGTU:
4385 if (treg == 0)
4386 {
4387 macro_build (&offset_expr, likely ? "bnel" : "bne",
4388 "s,t,p", sreg, 0);
4389 break;
4390 }
4391 if (sreg == 0)
4392 goto do_false;
4393 used_at = 1;
4394 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4395 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4396 break;
4397
4398 case M_BLEL:
4399 likely = 1;
4400 case M_BLE:
4401 if (treg == 0)
4402 {
4403 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4404 break;
4405 }
4406 if (sreg == 0)
4407 {
4408 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4409 break;
4410 }
4411 used_at = 1;
4412 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4413 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4414 break;
4415
4416 case M_BLEL_I:
4417 likely = 1;
4418 case M_BLE_I:
4419 maxnum = 0x7fffffff;
4420 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4421 {
4422 maxnum <<= 16;
4423 maxnum |= 0xffff;
4424 maxnum <<= 16;
4425 maxnum |= 0xffff;
4426 }
4427 if (imm_expr.X_op == O_constant
4428 && imm_expr.X_add_number >= maxnum
4429 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4430 goto do_true;
4431 if (imm_expr.X_op != O_constant)
4432 as_bad (_("Unsupported large constant"));
4433 ++imm_expr.X_add_number;
4434 /* FALLTHROUGH */
4435 case M_BLT_I:
4436 case M_BLTL_I:
4437 if (mask == M_BLTL_I)
4438 likely = 1;
4439 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4440 {
4441 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4442 break;
4443 }
4444 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4445 {
4446 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4447 break;
4448 }
4449 used_at = 1;
4450 set_at (sreg, 0);
4451 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4452 break;
4453
4454 case M_BLEUL:
4455 likely = 1;
4456 case M_BLEU:
4457 if (treg == 0)
4458 {
4459 macro_build (&offset_expr, likely ? "beql" : "beq",
4460 "s,t,p", sreg, 0);
4461 break;
4462 }
4463 if (sreg == 0)
4464 goto do_true;
4465 used_at = 1;
4466 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4467 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4468 break;
4469
4470 case M_BLEUL_I:
4471 likely = 1;
4472 case M_BLEU_I:
4473 if (sreg == 0
4474 || (HAVE_32BIT_GPRS
4475 && imm_expr.X_op == O_constant
4476 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4477 goto do_true;
4478 if (imm_expr.X_op != O_constant)
4479 as_bad (_("Unsupported large constant"));
4480 ++imm_expr.X_add_number;
4481 /* FALLTHROUGH */
4482 case M_BLTU_I:
4483 case M_BLTUL_I:
4484 if (mask == M_BLTUL_I)
4485 likely = 1;
4486 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4487 goto do_false;
4488 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4489 {
4490 macro_build (&offset_expr, likely ? "beql" : "beq",
4491 "s,t,p", sreg, 0);
4492 break;
4493 }
4494 used_at = 1;
4495 set_at (sreg, 1);
4496 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4497 break;
4498
4499 case M_BLTL:
4500 likely = 1;
4501 case M_BLT:
4502 if (treg == 0)
4503 {
4504 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4505 break;
4506 }
4507 if (sreg == 0)
4508 {
4509 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4510 break;
4511 }
4512 used_at = 1;
4513 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4514 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4515 break;
4516
4517 case M_BLTUL:
4518 likely = 1;
4519 case M_BLTU:
4520 if (treg == 0)
4521 goto do_false;
4522 if (sreg == 0)
4523 {
4524 macro_build (&offset_expr, likely ? "bnel" : "bne",
4525 "s,t,p", 0, treg);
4526 break;
4527 }
4528 used_at = 1;
4529 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4530 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4531 break;
4532
4533 case M_DEXT:
4534 {
4535 unsigned long pos;
4536 unsigned long size;
4537
4538 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4539 {
4540 as_bad (_("Unsupported large constant"));
4541 pos = size = 1;
4542 }
4543 else
4544 {
4545 pos = (unsigned long) imm_expr.X_add_number;
4546 size = (unsigned long) imm2_expr.X_add_number;
4547 }
4548
4549 if (pos > 63)
4550 {
4551 as_bad (_("Improper position (%lu)"), pos);
4552 pos = 1;
4553 }
4554 if (size == 0 || size > 64
4555 || (pos + size - 1) > 63)
4556 {
4557 as_bad (_("Improper extract size (%lu, position %lu)"),
4558 size, pos);
4559 size = 1;
4560 }
4561
4562 if (size <= 32 && pos < 32)
4563 {
4564 s = "dext";
4565 fmt = "t,r,+A,+C";
4566 }
4567 else if (size <= 32)
4568 {
4569 s = "dextu";
4570 fmt = "t,r,+E,+H";
4571 }
4572 else
4573 {
4574 s = "dextm";
4575 fmt = "t,r,+A,+G";
4576 }
4577 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4578 }
4579 break;
4580
4581 case M_DINS:
4582 {
4583 unsigned long pos;
4584 unsigned long size;
4585
4586 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4587 {
4588 as_bad (_("Unsupported large constant"));
4589 pos = size = 1;
4590 }
4591 else
4592 {
4593 pos = (unsigned long) imm_expr.X_add_number;
4594 size = (unsigned long) imm2_expr.X_add_number;
4595 }
4596
4597 if (pos > 63)
4598 {
4599 as_bad (_("Improper position (%lu)"), pos);
4600 pos = 1;
4601 }
4602 if (size == 0 || size > 64
4603 || (pos + size - 1) > 63)
4604 {
4605 as_bad (_("Improper insert size (%lu, position %lu)"),
4606 size, pos);
4607 size = 1;
4608 }
4609
4610 if (pos < 32 && (pos + size - 1) < 32)
4611 {
4612 s = "dins";
4613 fmt = "t,r,+A,+B";
4614 }
4615 else if (pos >= 32)
4616 {
4617 s = "dinsu";
4618 fmt = "t,r,+E,+F";
4619 }
4620 else
4621 {
4622 s = "dinsm";
4623 fmt = "t,r,+A,+F";
4624 }
4625 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4626 pos + size - 1);
4627 }
4628 break;
4629
4630 case M_DDIV_3:
4631 dbl = 1;
4632 case M_DIV_3:
4633 s = "mflo";
4634 goto do_div3;
4635 case M_DREM_3:
4636 dbl = 1;
4637 case M_REM_3:
4638 s = "mfhi";
4639 do_div3:
4640 if (treg == 0)
4641 {
4642 as_warn (_("Divide by zero."));
4643 if (mips_trap)
4644 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4645 else
4646 macro_build (NULL, "break", "c", 7);
4647 break;
4648 }
4649
4650 start_noreorder ();
4651 if (mips_trap)
4652 {
4653 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4654 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4655 }
4656 else
4657 {
4658 expr1.X_add_number = 8;
4659 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4660 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4661 macro_build (NULL, "break", "c", 7);
4662 }
4663 expr1.X_add_number = -1;
4664 used_at = 1;
4665 load_register (AT, &expr1, dbl);
4666 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4667 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4668 if (dbl)
4669 {
4670 expr1.X_add_number = 1;
4671 load_register (AT, &expr1, dbl);
4672 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4673 }
4674 else
4675 {
4676 expr1.X_add_number = 0x80000000;
4677 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4678 }
4679 if (mips_trap)
4680 {
4681 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4682 /* We want to close the noreorder block as soon as possible, so
4683 that later insns are available for delay slot filling. */
4684 end_noreorder ();
4685 }
4686 else
4687 {
4688 expr1.X_add_number = 8;
4689 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4690 macro_build (NULL, "nop", "", 0);
4691
4692 /* We want to close the noreorder block as soon as possible, so
4693 that later insns are available for delay slot filling. */
4694 end_noreorder ();
4695
4696 macro_build (NULL, "break", "c", 6);
4697 }
4698 macro_build (NULL, s, "d", dreg);
4699 break;
4700
4701 case M_DIV_3I:
4702 s = "div";
4703 s2 = "mflo";
4704 goto do_divi;
4705 case M_DIVU_3I:
4706 s = "divu";
4707 s2 = "mflo";
4708 goto do_divi;
4709 case M_REM_3I:
4710 s = "div";
4711 s2 = "mfhi";
4712 goto do_divi;
4713 case M_REMU_3I:
4714 s = "divu";
4715 s2 = "mfhi";
4716 goto do_divi;
4717 case M_DDIV_3I:
4718 dbl = 1;
4719 s = "ddiv";
4720 s2 = "mflo";
4721 goto do_divi;
4722 case M_DDIVU_3I:
4723 dbl = 1;
4724 s = "ddivu";
4725 s2 = "mflo";
4726 goto do_divi;
4727 case M_DREM_3I:
4728 dbl = 1;
4729 s = "ddiv";
4730 s2 = "mfhi";
4731 goto do_divi;
4732 case M_DREMU_3I:
4733 dbl = 1;
4734 s = "ddivu";
4735 s2 = "mfhi";
4736 do_divi:
4737 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4738 {
4739 as_warn (_("Divide by zero."));
4740 if (mips_trap)
4741 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4742 else
4743 macro_build (NULL, "break", "c", 7);
4744 break;
4745 }
4746 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4747 {
4748 if (strcmp (s2, "mflo") == 0)
4749 move_register (dreg, sreg);
4750 else
4751 move_register (dreg, 0);
4752 break;
4753 }
4754 if (imm_expr.X_op == O_constant
4755 && imm_expr.X_add_number == -1
4756 && s[strlen (s) - 1] != 'u')
4757 {
4758 if (strcmp (s2, "mflo") == 0)
4759 {
4760 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4761 }
4762 else
4763 move_register (dreg, 0);
4764 break;
4765 }
4766
4767 used_at = 1;
4768 load_register (AT, &imm_expr, dbl);
4769 macro_build (NULL, s, "z,s,t", sreg, AT);
4770 macro_build (NULL, s2, "d", dreg);
4771 break;
4772
4773 case M_DIVU_3:
4774 s = "divu";
4775 s2 = "mflo";
4776 goto do_divu3;
4777 case M_REMU_3:
4778 s = "divu";
4779 s2 = "mfhi";
4780 goto do_divu3;
4781 case M_DDIVU_3:
4782 s = "ddivu";
4783 s2 = "mflo";
4784 goto do_divu3;
4785 case M_DREMU_3:
4786 s = "ddivu";
4787 s2 = "mfhi";
4788 do_divu3:
4789 start_noreorder ();
4790 if (mips_trap)
4791 {
4792 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4793 macro_build (NULL, s, "z,s,t", sreg, treg);
4794 /* We want to close the noreorder block as soon as possible, so
4795 that later insns are available for delay slot filling. */
4796 end_noreorder ();
4797 }
4798 else
4799 {
4800 expr1.X_add_number = 8;
4801 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4802 macro_build (NULL, s, "z,s,t", sreg, treg);
4803
4804 /* We want to close the noreorder block as soon as possible, so
4805 that later insns are available for delay slot filling. */
4806 end_noreorder ();
4807 macro_build (NULL, "break", "c", 7);
4808 }
4809 macro_build (NULL, s2, "d", dreg);
4810 break;
4811
4812 case M_DLCA_AB:
4813 dbl = 1;
4814 case M_LCA_AB:
4815 call = 1;
4816 goto do_la;
4817 case M_DLA_AB:
4818 dbl = 1;
4819 case M_LA_AB:
4820 do_la:
4821 /* Load the address of a symbol into a register. If breg is not
4822 zero, we then add a base register to it. */
4823
4824 if (dbl && HAVE_32BIT_GPRS)
4825 as_warn (_("dla used to load 32-bit register"));
4826
4827 if (! dbl && HAVE_64BIT_OBJECTS)
4828 as_warn (_("la used to load 64-bit address"));
4829
4830 if (offset_expr.X_op == O_constant
4831 && offset_expr.X_add_number >= -0x8000
4832 && offset_expr.X_add_number < 0x8000)
4833 {
4834 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4835 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4836 break;
4837 }
4838
4839 if (!mips_opts.noat && (treg == breg))
4840 {
4841 tempreg = AT;
4842 used_at = 1;
4843 }
4844 else
4845 {
4846 tempreg = treg;
4847 }
4848
4849 if (offset_expr.X_op != O_symbol
4850 && offset_expr.X_op != O_constant)
4851 {
4852 as_bad (_("expression too complex"));
4853 offset_expr.X_op = O_constant;
4854 }
4855
4856 if (offset_expr.X_op == O_constant)
4857 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4858 else if (mips_pic == NO_PIC)
4859 {
4860 /* If this is a reference to a GP relative symbol, we want
4861 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4862 Otherwise we want
4863 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4864 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4865 If we have a constant, we need two instructions anyhow,
4866 so we may as well always use the latter form.
4867
4868 With 64bit address space and a usable $at we want
4869 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4870 lui $at,<sym> (BFD_RELOC_HI16_S)
4871 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4872 daddiu $at,<sym> (BFD_RELOC_LO16)
4873 dsll32 $tempreg,0
4874 daddu $tempreg,$tempreg,$at
4875
4876 If $at is already in use, we use a path which is suboptimal
4877 on superscalar processors.
4878 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4879 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4880 dsll $tempreg,16
4881 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4882 dsll $tempreg,16
4883 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4884
4885 For GP relative symbols in 64bit address space we can use
4886 the same sequence as in 32bit address space. */
4887 if (HAVE_64BIT_SYMBOLS)
4888 {
4889 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4890 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4891 {
4892 relax_start (offset_expr.X_add_symbol);
4893 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4894 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4895 relax_switch ();
4896 }
4897
4898 if (used_at == 0 && !mips_opts.noat)
4899 {
4900 macro_build (&offset_expr, "lui", "t,u",
4901 tempreg, BFD_RELOC_MIPS_HIGHEST);
4902 macro_build (&offset_expr, "lui", "t,u",
4903 AT, BFD_RELOC_HI16_S);
4904 macro_build (&offset_expr, "daddiu", "t,r,j",
4905 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4906 macro_build (&offset_expr, "daddiu", "t,r,j",
4907 AT, AT, BFD_RELOC_LO16);
4908 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4909 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4910 used_at = 1;
4911 }
4912 else
4913 {
4914 macro_build (&offset_expr, "lui", "t,u",
4915 tempreg, BFD_RELOC_MIPS_HIGHEST);
4916 macro_build (&offset_expr, "daddiu", "t,r,j",
4917 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4918 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4919 macro_build (&offset_expr, "daddiu", "t,r,j",
4920 tempreg, tempreg, BFD_RELOC_HI16_S);
4921 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4922 macro_build (&offset_expr, "daddiu", "t,r,j",
4923 tempreg, tempreg, BFD_RELOC_LO16);
4924 }
4925
4926 if (mips_relax.sequence)
4927 relax_end ();
4928 }
4929 else
4930 {
4931 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4932 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4933 {
4934 relax_start (offset_expr.X_add_symbol);
4935 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4936 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4937 relax_switch ();
4938 }
4939 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
4940 as_bad (_("offset too large"));
4941 macro_build_lui (&offset_expr, tempreg);
4942 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4943 tempreg, tempreg, BFD_RELOC_LO16);
4944 if (mips_relax.sequence)
4945 relax_end ();
4946 }
4947 }
4948 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4949 {
4950 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4951
4952 /* If this is a reference to an external symbol, and there
4953 is no constant, we want
4954 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4955 or for lca or if tempreg is PIC_CALL_REG
4956 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4957 For a local symbol, we want
4958 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4959 nop
4960 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4961
4962 If we have a small constant, and this is a reference to
4963 an external symbol, we want
4964 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4965 nop
4966 addiu $tempreg,$tempreg,<constant>
4967 For a local symbol, we want the same instruction
4968 sequence, but we output a BFD_RELOC_LO16 reloc on the
4969 addiu instruction.
4970
4971 If we have a large constant, and this is a reference to
4972 an external symbol, we want
4973 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4974 lui $at,<hiconstant>
4975 addiu $at,$at,<loconstant>
4976 addu $tempreg,$tempreg,$at
4977 For a local symbol, we want the same instruction
4978 sequence, but we output a BFD_RELOC_LO16 reloc on the
4979 addiu instruction.
4980 */
4981
4982 if (offset_expr.X_add_number == 0)
4983 {
4984 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
4985 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4986
4987 relax_start (offset_expr.X_add_symbol);
4988 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
4989 lw_reloc_type, mips_gp_register);
4990 if (breg != 0)
4991 {
4992 /* We're going to put in an addu instruction using
4993 tempreg, so we may as well insert the nop right
4994 now. */
4995 load_delay_nop ();
4996 }
4997 relax_switch ();
4998 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4999 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5000 load_delay_nop ();
5001 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5002 tempreg, tempreg, BFD_RELOC_LO16);
5003 relax_end ();
5004 /* FIXME: If breg == 0, and the next instruction uses
5005 $tempreg, then if this variant case is used an extra
5006 nop will be generated. */
5007 }
5008 else if (offset_expr.X_add_number >= -0x8000
5009 && offset_expr.X_add_number < 0x8000)
5010 {
5011 load_got_offset (tempreg, &offset_expr);
5012 load_delay_nop ();
5013 add_got_offset (tempreg, &offset_expr);
5014 }
5015 else
5016 {
5017 expr1.X_add_number = offset_expr.X_add_number;
5018 offset_expr.X_add_number =
5019 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5020 load_got_offset (tempreg, &offset_expr);
5021 offset_expr.X_add_number = expr1.X_add_number;
5022 /* If we are going to add in a base register, and the
5023 target register and the base register are the same,
5024 then we are using AT as a temporary register. Since
5025 we want to load the constant into AT, we add our
5026 current AT (from the global offset table) and the
5027 register into the register now, and pretend we were
5028 not using a base register. */
5029 if (breg == treg)
5030 {
5031 load_delay_nop ();
5032 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5033 treg, AT, breg);
5034 breg = 0;
5035 tempreg = treg;
5036 }
5037 add_got_offset_hilo (tempreg, &offset_expr, AT);
5038 used_at = 1;
5039 }
5040 }
5041 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5042 {
5043 int add_breg_early = 0;
5044
5045 /* If this is a reference to an external, and there is no
5046 constant, or local symbol (*), with or without a
5047 constant, we want
5048 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5049 or for lca or if tempreg is PIC_CALL_REG
5050 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5051
5052 If we have a small constant, and this is a reference to
5053 an external symbol, we want
5054 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5055 addiu $tempreg,$tempreg,<constant>
5056
5057 If we have a large constant, and this is a reference to
5058 an external symbol, we want
5059 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5060 lui $at,<hiconstant>
5061 addiu $at,$at,<loconstant>
5062 addu $tempreg,$tempreg,$at
5063
5064 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5065 local symbols, even though it introduces an additional
5066 instruction. */
5067
5068 if (offset_expr.X_add_number)
5069 {
5070 expr1.X_add_number = offset_expr.X_add_number;
5071 offset_expr.X_add_number = 0;
5072
5073 relax_start (offset_expr.X_add_symbol);
5074 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5075 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5076
5077 if (expr1.X_add_number >= -0x8000
5078 && expr1.X_add_number < 0x8000)
5079 {
5080 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5081 tempreg, tempreg, BFD_RELOC_LO16);
5082 }
5083 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5084 {
5085 int dreg;
5086
5087 /* If we are going to add in a base register, and the
5088 target register and the base register are the same,
5089 then we are using AT as a temporary register. Since
5090 we want to load the constant into AT, we add our
5091 current AT (from the global offset table) and the
5092 register into the register now, and pretend we were
5093 not using a base register. */
5094 if (breg != treg)
5095 dreg = tempreg;
5096 else
5097 {
5098 assert (tempreg == AT);
5099 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5100 treg, AT, breg);
5101 dreg = treg;
5102 add_breg_early = 1;
5103 }
5104
5105 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5106 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5107 dreg, dreg, AT);
5108
5109 used_at = 1;
5110 }
5111 else
5112 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5113
5114 relax_switch ();
5115 offset_expr.X_add_number = expr1.X_add_number;
5116
5117 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5118 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5119 if (add_breg_early)
5120 {
5121 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5122 treg, tempreg, breg);
5123 breg = 0;
5124 tempreg = treg;
5125 }
5126 relax_end ();
5127 }
5128 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5129 {
5130 relax_start (offset_expr.X_add_symbol);
5131 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5132 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5133 relax_switch ();
5134 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5135 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5136 relax_end ();
5137 }
5138 else
5139 {
5140 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5141 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5142 }
5143 }
5144 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5145 {
5146 int gpdelay;
5147 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5148 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5149 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5150
5151 /* This is the large GOT case. If this is a reference to an
5152 external symbol, and there is no constant, we want
5153 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5154 addu $tempreg,$tempreg,$gp
5155 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5156 or for lca or if tempreg is PIC_CALL_REG
5157 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5158 addu $tempreg,$tempreg,$gp
5159 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5160 For a local symbol, we want
5161 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5162 nop
5163 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5164
5165 If we have a small constant, and this is a reference to
5166 an external symbol, we want
5167 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5168 addu $tempreg,$tempreg,$gp
5169 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5170 nop
5171 addiu $tempreg,$tempreg,<constant>
5172 For a local symbol, we want
5173 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5174 nop
5175 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5176
5177 If we have a large constant, and this is a reference to
5178 an external symbol, we want
5179 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5180 addu $tempreg,$tempreg,$gp
5181 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5182 lui $at,<hiconstant>
5183 addiu $at,$at,<loconstant>
5184 addu $tempreg,$tempreg,$at
5185 For a local symbol, we want
5186 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5187 lui $at,<hiconstant>
5188 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5189 addu $tempreg,$tempreg,$at
5190 */
5191
5192 expr1.X_add_number = offset_expr.X_add_number;
5193 offset_expr.X_add_number = 0;
5194 relax_start (offset_expr.X_add_symbol);
5195 gpdelay = reg_needs_delay (mips_gp_register);
5196 if (expr1.X_add_number == 0 && breg == 0
5197 && (call || tempreg == PIC_CALL_REG))
5198 {
5199 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5200 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5201 }
5202 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5203 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5204 tempreg, tempreg, mips_gp_register);
5205 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5206 tempreg, lw_reloc_type, tempreg);
5207 if (expr1.X_add_number == 0)
5208 {
5209 if (breg != 0)
5210 {
5211 /* We're going to put in an addu instruction using
5212 tempreg, so we may as well insert the nop right
5213 now. */
5214 load_delay_nop ();
5215 }
5216 }
5217 else if (expr1.X_add_number >= -0x8000
5218 && expr1.X_add_number < 0x8000)
5219 {
5220 load_delay_nop ();
5221 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5222 tempreg, tempreg, BFD_RELOC_LO16);
5223 }
5224 else
5225 {
5226 int dreg;
5227
5228 /* If we are going to add in a base register, and the
5229 target register and the base register are the same,
5230 then we are using AT as a temporary register. Since
5231 we want to load the constant into AT, we add our
5232 current AT (from the global offset table) and the
5233 register into the register now, and pretend we were
5234 not using a base register. */
5235 if (breg != treg)
5236 dreg = tempreg;
5237 else
5238 {
5239 assert (tempreg == AT);
5240 load_delay_nop ();
5241 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5242 treg, AT, breg);
5243 dreg = treg;
5244 }
5245
5246 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5247 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5248
5249 used_at = 1;
5250 }
5251 offset_expr.X_add_number =
5252 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5253 relax_switch ();
5254
5255 if (gpdelay)
5256 {
5257 /* This is needed because this instruction uses $gp, but
5258 the first instruction on the main stream does not. */
5259 macro_build (NULL, "nop", "");
5260 }
5261
5262 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5263 local_reloc_type, mips_gp_register);
5264 if (expr1.X_add_number >= -0x8000
5265 && expr1.X_add_number < 0x8000)
5266 {
5267 load_delay_nop ();
5268 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5269 tempreg, tempreg, BFD_RELOC_LO16);
5270 /* FIXME: If add_number is 0, and there was no base
5271 register, the external symbol case ended with a load,
5272 so if the symbol turns out to not be external, and
5273 the next instruction uses tempreg, an unnecessary nop
5274 will be inserted. */
5275 }
5276 else
5277 {
5278 if (breg == treg)
5279 {
5280 /* We must add in the base register now, as in the
5281 external symbol case. */
5282 assert (tempreg == AT);
5283 load_delay_nop ();
5284 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5285 treg, AT, breg);
5286 tempreg = treg;
5287 /* We set breg to 0 because we have arranged to add
5288 it in in both cases. */
5289 breg = 0;
5290 }
5291
5292 macro_build_lui (&expr1, AT);
5293 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5294 AT, AT, BFD_RELOC_LO16);
5295 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5296 tempreg, tempreg, AT);
5297 used_at = 1;
5298 }
5299 relax_end ();
5300 }
5301 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5302 {
5303 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5304 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5305 int add_breg_early = 0;
5306
5307 /* This is the large GOT case. If this is a reference to an
5308 external symbol, and there is no constant, we want
5309 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5310 add $tempreg,$tempreg,$gp
5311 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5312 or for lca or if tempreg is PIC_CALL_REG
5313 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5314 add $tempreg,$tempreg,$gp
5315 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5316
5317 If we have a small constant, and this is a reference to
5318 an external symbol, we want
5319 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5320 add $tempreg,$tempreg,$gp
5321 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5322 addi $tempreg,$tempreg,<constant>
5323
5324 If we have a large constant, and this is a reference to
5325 an external symbol, we want
5326 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5327 addu $tempreg,$tempreg,$gp
5328 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5329 lui $at,<hiconstant>
5330 addi $at,$at,<loconstant>
5331 add $tempreg,$tempreg,$at
5332
5333 If we have NewABI, and we know it's a local symbol, we want
5334 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5335 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5336 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5337
5338 relax_start (offset_expr.X_add_symbol);
5339
5340 expr1.X_add_number = offset_expr.X_add_number;
5341 offset_expr.X_add_number = 0;
5342
5343 if (expr1.X_add_number == 0 && breg == 0
5344 && (call || tempreg == PIC_CALL_REG))
5345 {
5346 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5347 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5348 }
5349 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5351 tempreg, tempreg, mips_gp_register);
5352 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5353 tempreg, lw_reloc_type, tempreg);
5354
5355 if (expr1.X_add_number == 0)
5356 ;
5357 else if (expr1.X_add_number >= -0x8000
5358 && expr1.X_add_number < 0x8000)
5359 {
5360 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5361 tempreg, tempreg, BFD_RELOC_LO16);
5362 }
5363 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5364 {
5365 int dreg;
5366
5367 /* If we are going to add in a base register, and the
5368 target register and the base register are the same,
5369 then we are using AT as a temporary register. Since
5370 we want to load the constant into AT, we add our
5371 current AT (from the global offset table) and the
5372 register into the register now, and pretend we were
5373 not using a base register. */
5374 if (breg != treg)
5375 dreg = tempreg;
5376 else
5377 {
5378 assert (tempreg == AT);
5379 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5380 treg, AT, breg);
5381 dreg = treg;
5382 add_breg_early = 1;
5383 }
5384
5385 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5386 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5387
5388 used_at = 1;
5389 }
5390 else
5391 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5392
5393 relax_switch ();
5394 offset_expr.X_add_number = expr1.X_add_number;
5395 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5396 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5397 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5398 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5399 if (add_breg_early)
5400 {
5401 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5402 treg, tempreg, breg);
5403 breg = 0;
5404 tempreg = treg;
5405 }
5406 relax_end ();
5407 }
5408 else
5409 abort ();
5410
5411 if (breg != 0)
5412 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5413 break;
5414
5415 case M_J_A:
5416 /* The j instruction may not be used in PIC code, since it
5417 requires an absolute address. We convert it to a b
5418 instruction. */
5419 if (mips_pic == NO_PIC)
5420 macro_build (&offset_expr, "j", "a");
5421 else
5422 macro_build (&offset_expr, "b", "p");
5423 break;
5424
5425 /* The jal instructions must be handled as macros because when
5426 generating PIC code they expand to multi-instruction
5427 sequences. Normally they are simple instructions. */
5428 case M_JAL_1:
5429 dreg = RA;
5430 /* Fall through. */
5431 case M_JAL_2:
5432 if (mips_pic == NO_PIC)
5433 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5434 else if (mips_pic == SVR4_PIC)
5435 {
5436 if (sreg != PIC_CALL_REG)
5437 as_warn (_("MIPS PIC call to register other than $25"));
5438
5439 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5440 if (! HAVE_NEWABI)
5441 {
5442 if (mips_cprestore_offset < 0)
5443 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5444 else
5445 {
5446 if (! mips_frame_reg_valid)
5447 {
5448 as_warn (_("No .frame pseudo-op used in PIC code"));
5449 /* Quiet this warning. */
5450 mips_frame_reg_valid = 1;
5451 }
5452 if (! mips_cprestore_valid)
5453 {
5454 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5455 /* Quiet this warning. */
5456 mips_cprestore_valid = 1;
5457 }
5458 expr1.X_add_number = mips_cprestore_offset;
5459 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5460 mips_gp_register,
5461 mips_frame_reg,
5462 HAVE_64BIT_ADDRESSES);
5463 }
5464 }
5465 }
5466 else
5467 abort ();
5468
5469 break;
5470
5471 case M_JAL_A:
5472 if (mips_pic == NO_PIC)
5473 macro_build (&offset_expr, "jal", "a");
5474 else if (mips_pic == SVR4_PIC)
5475 {
5476 /* If this is a reference to an external symbol, and we are
5477 using a small GOT, we want
5478 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5479 nop
5480 jalr $ra,$25
5481 nop
5482 lw $gp,cprestore($sp)
5483 The cprestore value is set using the .cprestore
5484 pseudo-op. If we are using a big GOT, we want
5485 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5486 addu $25,$25,$gp
5487 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5488 nop
5489 jalr $ra,$25
5490 nop
5491 lw $gp,cprestore($sp)
5492 If the symbol is not external, we want
5493 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5494 nop
5495 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5496 jalr $ra,$25
5497 nop
5498 lw $gp,cprestore($sp)
5499
5500 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5501 sequences above, minus nops, unless the symbol is local,
5502 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5503 GOT_DISP. */
5504 if (HAVE_NEWABI)
5505 {
5506 if (! mips_big_got)
5507 {
5508 relax_start (offset_expr.X_add_symbol);
5509 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5510 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5511 mips_gp_register);
5512 relax_switch ();
5513 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5514 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5515 mips_gp_register);
5516 relax_end ();
5517 }
5518 else
5519 {
5520 relax_start (offset_expr.X_add_symbol);
5521 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5522 BFD_RELOC_MIPS_CALL_HI16);
5523 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5524 PIC_CALL_REG, mips_gp_register);
5525 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5526 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5527 PIC_CALL_REG);
5528 relax_switch ();
5529 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5530 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5531 mips_gp_register);
5532 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5533 PIC_CALL_REG, PIC_CALL_REG,
5534 BFD_RELOC_MIPS_GOT_OFST);
5535 relax_end ();
5536 }
5537
5538 macro_build_jalr (&offset_expr);
5539 }
5540 else
5541 {
5542 relax_start (offset_expr.X_add_symbol);
5543 if (! mips_big_got)
5544 {
5545 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5546 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5547 mips_gp_register);
5548 load_delay_nop ();
5549 relax_switch ();
5550 }
5551 else
5552 {
5553 int gpdelay;
5554
5555 gpdelay = reg_needs_delay (mips_gp_register);
5556 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5557 BFD_RELOC_MIPS_CALL_HI16);
5558 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5559 PIC_CALL_REG, mips_gp_register);
5560 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5561 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5562 PIC_CALL_REG);
5563 load_delay_nop ();
5564 relax_switch ();
5565 if (gpdelay)
5566 macro_build (NULL, "nop", "");
5567 }
5568 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5569 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5570 mips_gp_register);
5571 load_delay_nop ();
5572 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5573 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5574 relax_end ();
5575 macro_build_jalr (&offset_expr);
5576
5577 if (mips_cprestore_offset < 0)
5578 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5579 else
5580 {
5581 if (! mips_frame_reg_valid)
5582 {
5583 as_warn (_("No .frame pseudo-op used in PIC code"));
5584 /* Quiet this warning. */
5585 mips_frame_reg_valid = 1;
5586 }
5587 if (! mips_cprestore_valid)
5588 {
5589 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5590 /* Quiet this warning. */
5591 mips_cprestore_valid = 1;
5592 }
5593 if (mips_opts.noreorder)
5594 macro_build (NULL, "nop", "");
5595 expr1.X_add_number = mips_cprestore_offset;
5596 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5597 mips_gp_register,
5598 mips_frame_reg,
5599 HAVE_64BIT_ADDRESSES);
5600 }
5601 }
5602 }
5603 else
5604 abort ();
5605
5606 break;
5607
5608 case M_LB_AB:
5609 s = "lb";
5610 goto ld;
5611 case M_LBU_AB:
5612 s = "lbu";
5613 goto ld;
5614 case M_LH_AB:
5615 s = "lh";
5616 goto ld;
5617 case M_LHU_AB:
5618 s = "lhu";
5619 goto ld;
5620 case M_LW_AB:
5621 s = "lw";
5622 goto ld;
5623 case M_LWC0_AB:
5624 s = "lwc0";
5625 /* Itbl support may require additional care here. */
5626 coproc = 1;
5627 goto ld;
5628 case M_LWC1_AB:
5629 s = "lwc1";
5630 /* Itbl support may require additional care here. */
5631 coproc = 1;
5632 goto ld;
5633 case M_LWC2_AB:
5634 s = "lwc2";
5635 /* Itbl support may require additional care here. */
5636 coproc = 1;
5637 goto ld;
5638 case M_LWC3_AB:
5639 s = "lwc3";
5640 /* Itbl support may require additional care here. */
5641 coproc = 1;
5642 goto ld;
5643 case M_LWL_AB:
5644 s = "lwl";
5645 lr = 1;
5646 goto ld;
5647 case M_LWR_AB:
5648 s = "lwr";
5649 lr = 1;
5650 goto ld;
5651 case M_LDC1_AB:
5652 if (mips_opts.arch == CPU_R4650)
5653 {
5654 as_bad (_("opcode not supported on this processor"));
5655 break;
5656 }
5657 s = "ldc1";
5658 /* Itbl support may require additional care here. */
5659 coproc = 1;
5660 goto ld;
5661 case M_LDC2_AB:
5662 s = "ldc2";
5663 /* Itbl support may require additional care here. */
5664 coproc = 1;
5665 goto ld;
5666 case M_LDC3_AB:
5667 s = "ldc3";
5668 /* Itbl support may require additional care here. */
5669 coproc = 1;
5670 goto ld;
5671 case M_LDL_AB:
5672 s = "ldl";
5673 lr = 1;
5674 goto ld;
5675 case M_LDR_AB:
5676 s = "ldr";
5677 lr = 1;
5678 goto ld;
5679 case M_LL_AB:
5680 s = "ll";
5681 goto ld;
5682 case M_LLD_AB:
5683 s = "lld";
5684 goto ld;
5685 case M_LWU_AB:
5686 s = "lwu";
5687 ld:
5688 if (breg == treg || coproc || lr)
5689 {
5690 tempreg = AT;
5691 used_at = 1;
5692 }
5693 else
5694 {
5695 tempreg = treg;
5696 }
5697 goto ld_st;
5698 case M_SB_AB:
5699 s = "sb";
5700 goto st;
5701 case M_SH_AB:
5702 s = "sh";
5703 goto st;
5704 case M_SW_AB:
5705 s = "sw";
5706 goto st;
5707 case M_SWC0_AB:
5708 s = "swc0";
5709 /* Itbl support may require additional care here. */
5710 coproc = 1;
5711 goto st;
5712 case M_SWC1_AB:
5713 s = "swc1";
5714 /* Itbl support may require additional care here. */
5715 coproc = 1;
5716 goto st;
5717 case M_SWC2_AB:
5718 s = "swc2";
5719 /* Itbl support may require additional care here. */
5720 coproc = 1;
5721 goto st;
5722 case M_SWC3_AB:
5723 s = "swc3";
5724 /* Itbl support may require additional care here. */
5725 coproc = 1;
5726 goto st;
5727 case M_SWL_AB:
5728 s = "swl";
5729 goto st;
5730 case M_SWR_AB:
5731 s = "swr";
5732 goto st;
5733 case M_SC_AB:
5734 s = "sc";
5735 goto st;
5736 case M_SCD_AB:
5737 s = "scd";
5738 goto st;
5739 case M_SDC1_AB:
5740 if (mips_opts.arch == CPU_R4650)
5741 {
5742 as_bad (_("opcode not supported on this processor"));
5743 break;
5744 }
5745 s = "sdc1";
5746 coproc = 1;
5747 /* Itbl support may require additional care here. */
5748 goto st;
5749 case M_SDC2_AB:
5750 s = "sdc2";
5751 /* Itbl support may require additional care here. */
5752 coproc = 1;
5753 goto st;
5754 case M_SDC3_AB:
5755 s = "sdc3";
5756 /* Itbl support may require additional care here. */
5757 coproc = 1;
5758 goto st;
5759 case M_SDL_AB:
5760 s = "sdl";
5761 goto st;
5762 case M_SDR_AB:
5763 s = "sdr";
5764 st:
5765 tempreg = AT;
5766 used_at = 1;
5767 ld_st:
5768 /* Itbl support may require additional care here. */
5769 if (mask == M_LWC1_AB
5770 || mask == M_SWC1_AB
5771 || mask == M_LDC1_AB
5772 || mask == M_SDC1_AB
5773 || mask == M_L_DAB
5774 || mask == M_S_DAB)
5775 fmt = "T,o(b)";
5776 else if (coproc)
5777 fmt = "E,o(b)";
5778 else
5779 fmt = "t,o(b)";
5780
5781 if (offset_expr.X_op != O_constant
5782 && offset_expr.X_op != O_symbol)
5783 {
5784 as_bad (_("expression too complex"));
5785 offset_expr.X_op = O_constant;
5786 }
5787
5788 /* A constant expression in PIC code can be handled just as it
5789 is in non PIC code. */
5790 if (offset_expr.X_op == O_constant)
5791 {
5792 if (HAVE_32BIT_ADDRESSES
5793 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5794 as_bad (_("constant too large"));
5795
5796 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5797 & ~(bfd_vma) 0xffff);
5798 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5799 if (breg != 0)
5800 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5801 tempreg, tempreg, breg);
5802 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5803 }
5804 else if (mips_pic == NO_PIC)
5805 {
5806 /* If this is a reference to a GP relative symbol, and there
5807 is no base register, we want
5808 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5809 Otherwise, if there is no base register, we want
5810 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5811 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5812 If we have a constant, we need two instructions anyhow,
5813 so we always use the latter form.
5814
5815 If we have a base register, and this is a reference to a
5816 GP relative symbol, we want
5817 addu $tempreg,$breg,$gp
5818 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5819 Otherwise we want
5820 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5821 addu $tempreg,$tempreg,$breg
5822 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5823 With a constant we always use the latter case.
5824
5825 With 64bit address space and no base register and $at usable,
5826 we want
5827 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5828 lui $at,<sym> (BFD_RELOC_HI16_S)
5829 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5830 dsll32 $tempreg,0
5831 daddu $tempreg,$at
5832 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5833 If we have a base register, we want
5834 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5835 lui $at,<sym> (BFD_RELOC_HI16_S)
5836 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5837 daddu $at,$breg
5838 dsll32 $tempreg,0
5839 daddu $tempreg,$at
5840 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5841
5842 Without $at we can't generate the optimal path for superscalar
5843 processors here since this would require two temporary registers.
5844 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5845 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5846 dsll $tempreg,16
5847 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5848 dsll $tempreg,16
5849 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5850 If we have a base register, we want
5851 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5852 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5853 dsll $tempreg,16
5854 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5855 dsll $tempreg,16
5856 daddu $tempreg,$tempreg,$breg
5857 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5858
5859 For GP relative symbols in 64bit address space we can use
5860 the same sequence as in 32bit address space. */
5861 if (HAVE_64BIT_SYMBOLS)
5862 {
5863 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5864 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5865 {
5866 relax_start (offset_expr.X_add_symbol);
5867 if (breg == 0)
5868 {
5869 macro_build (&offset_expr, s, fmt, treg,
5870 BFD_RELOC_GPREL16, mips_gp_register);
5871 }
5872 else
5873 {
5874 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5875 tempreg, breg, mips_gp_register);
5876 macro_build (&offset_expr, s, fmt, treg,
5877 BFD_RELOC_GPREL16, tempreg);
5878 }
5879 relax_switch ();
5880 }
5881
5882 if (used_at == 0 && !mips_opts.noat)
5883 {
5884 macro_build (&offset_expr, "lui", "t,u", tempreg,
5885 BFD_RELOC_MIPS_HIGHEST);
5886 macro_build (&offset_expr, "lui", "t,u", AT,
5887 BFD_RELOC_HI16_S);
5888 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5889 tempreg, BFD_RELOC_MIPS_HIGHER);
5890 if (breg != 0)
5891 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5892 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5893 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5894 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5895 tempreg);
5896 used_at = 1;
5897 }
5898 else
5899 {
5900 macro_build (&offset_expr, "lui", "t,u", tempreg,
5901 BFD_RELOC_MIPS_HIGHEST);
5902 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5903 tempreg, BFD_RELOC_MIPS_HIGHER);
5904 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5905 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5906 tempreg, BFD_RELOC_HI16_S);
5907 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5908 if (breg != 0)
5909 macro_build (NULL, "daddu", "d,v,t",
5910 tempreg, tempreg, breg);
5911 macro_build (&offset_expr, s, fmt, treg,
5912 BFD_RELOC_LO16, tempreg);
5913 }
5914
5915 if (mips_relax.sequence)
5916 relax_end ();
5917 break;
5918 }
5919
5920 if (breg == 0)
5921 {
5922 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5923 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5924 {
5925 relax_start (offset_expr.X_add_symbol);
5926 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5927 mips_gp_register);
5928 relax_switch ();
5929 }
5930 macro_build_lui (&offset_expr, tempreg);
5931 macro_build (&offset_expr, s, fmt, treg,
5932 BFD_RELOC_LO16, tempreg);
5933 if (mips_relax.sequence)
5934 relax_end ();
5935 }
5936 else
5937 {
5938 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5939 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5940 {
5941 relax_start (offset_expr.X_add_symbol);
5942 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5943 tempreg, breg, mips_gp_register);
5944 macro_build (&offset_expr, s, fmt, treg,
5945 BFD_RELOC_GPREL16, tempreg);
5946 relax_switch ();
5947 }
5948 macro_build_lui (&offset_expr, tempreg);
5949 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5950 tempreg, tempreg, breg);
5951 macro_build (&offset_expr, s, fmt, treg,
5952 BFD_RELOC_LO16, tempreg);
5953 if (mips_relax.sequence)
5954 relax_end ();
5955 }
5956 }
5957 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5958 {
5959 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5960
5961 /* If this is a reference to an external symbol, we want
5962 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5963 nop
5964 <op> $treg,0($tempreg)
5965 Otherwise we want
5966 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5967 nop
5968 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5969 <op> $treg,0($tempreg)
5970
5971 For NewABI, we want
5972 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5973 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
5974
5975 If there is a base register, we add it to $tempreg before
5976 the <op>. If there is a constant, we stick it in the
5977 <op> instruction. We don't handle constants larger than
5978 16 bits, because we have no way to load the upper 16 bits
5979 (actually, we could handle them for the subset of cases
5980 in which we are not using $at). */
5981 assert (offset_expr.X_op == O_symbol);
5982 if (HAVE_NEWABI)
5983 {
5984 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5985 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5986 if (breg != 0)
5987 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5988 tempreg, tempreg, breg);
5989 macro_build (&offset_expr, s, fmt, treg,
5990 BFD_RELOC_MIPS_GOT_OFST, tempreg);
5991 break;
5992 }
5993 expr1.X_add_number = offset_expr.X_add_number;
5994 offset_expr.X_add_number = 0;
5995 if (expr1.X_add_number < -0x8000
5996 || expr1.X_add_number >= 0x8000)
5997 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5998 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5999 lw_reloc_type, mips_gp_register);
6000 load_delay_nop ();
6001 relax_start (offset_expr.X_add_symbol);
6002 relax_switch ();
6003 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6004 tempreg, BFD_RELOC_LO16);
6005 relax_end ();
6006 if (breg != 0)
6007 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6008 tempreg, tempreg, breg);
6009 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6010 }
6011 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6012 {
6013 int gpdelay;
6014
6015 /* If this is a reference to an external symbol, we want
6016 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6017 addu $tempreg,$tempreg,$gp
6018 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6019 <op> $treg,0($tempreg)
6020 Otherwise we want
6021 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6022 nop
6023 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6024 <op> $treg,0($tempreg)
6025 If there is a base register, we add it to $tempreg before
6026 the <op>. If there is a constant, we stick it in the
6027 <op> instruction. We don't handle constants larger than
6028 16 bits, because we have no way to load the upper 16 bits
6029 (actually, we could handle them for the subset of cases
6030 in which we are not using $at). */
6031 assert (offset_expr.X_op == O_symbol);
6032 expr1.X_add_number = offset_expr.X_add_number;
6033 offset_expr.X_add_number = 0;
6034 if (expr1.X_add_number < -0x8000
6035 || expr1.X_add_number >= 0x8000)
6036 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6037 gpdelay = reg_needs_delay (mips_gp_register);
6038 relax_start (offset_expr.X_add_symbol);
6039 macro_build (&offset_expr, "lui", "t,u", tempreg,
6040 BFD_RELOC_MIPS_GOT_HI16);
6041 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6042 mips_gp_register);
6043 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6044 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6045 relax_switch ();
6046 if (gpdelay)
6047 macro_build (NULL, "nop", "");
6048 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6049 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6050 load_delay_nop ();
6051 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6052 tempreg, BFD_RELOC_LO16);
6053 relax_end ();
6054
6055 if (breg != 0)
6056 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6057 tempreg, tempreg, breg);
6058 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6059 }
6060 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6061 {
6062 /* If this is a reference to an external symbol, we want
6063 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6064 add $tempreg,$tempreg,$gp
6065 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6066 <op> $treg,<ofst>($tempreg)
6067 Otherwise, for local symbols, we want:
6068 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6069 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6070 assert (offset_expr.X_op == O_symbol);
6071 expr1.X_add_number = offset_expr.X_add_number;
6072 offset_expr.X_add_number = 0;
6073 if (expr1.X_add_number < -0x8000
6074 || expr1.X_add_number >= 0x8000)
6075 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6076 relax_start (offset_expr.X_add_symbol);
6077 macro_build (&offset_expr, "lui", "t,u", tempreg,
6078 BFD_RELOC_MIPS_GOT_HI16);
6079 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6080 mips_gp_register);
6081 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6082 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6083 if (breg != 0)
6084 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6085 tempreg, tempreg, breg);
6086 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6087
6088 relax_switch ();
6089 offset_expr.X_add_number = expr1.X_add_number;
6090 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6091 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6092 if (breg != 0)
6093 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6094 tempreg, tempreg, breg);
6095 macro_build (&offset_expr, s, fmt, treg,
6096 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6097 relax_end ();
6098 }
6099 else
6100 abort ();
6101
6102 break;
6103
6104 case M_LI:
6105 case M_LI_S:
6106 load_register (treg, &imm_expr, 0);
6107 break;
6108
6109 case M_DLI:
6110 load_register (treg, &imm_expr, 1);
6111 break;
6112
6113 case M_LI_SS:
6114 if (imm_expr.X_op == O_constant)
6115 {
6116 used_at = 1;
6117 load_register (AT, &imm_expr, 0);
6118 macro_build (NULL, "mtc1", "t,G", AT, treg);
6119 break;
6120 }
6121 else
6122 {
6123 assert (offset_expr.X_op == O_symbol
6124 && strcmp (segment_name (S_GET_SEGMENT
6125 (offset_expr.X_add_symbol)),
6126 ".lit4") == 0
6127 && offset_expr.X_add_number == 0);
6128 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6129 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6130 break;
6131 }
6132
6133 case M_LI_D:
6134 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6135 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6136 order 32 bits of the value and the low order 32 bits are either
6137 zero or in OFFSET_EXPR. */
6138 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6139 {
6140 if (HAVE_64BIT_GPRS)
6141 load_register (treg, &imm_expr, 1);
6142 else
6143 {
6144 int hreg, lreg;
6145
6146 if (target_big_endian)
6147 {
6148 hreg = treg;
6149 lreg = treg + 1;
6150 }
6151 else
6152 {
6153 hreg = treg + 1;
6154 lreg = treg;
6155 }
6156
6157 if (hreg <= 31)
6158 load_register (hreg, &imm_expr, 0);
6159 if (lreg <= 31)
6160 {
6161 if (offset_expr.X_op == O_absent)
6162 move_register (lreg, 0);
6163 else
6164 {
6165 assert (offset_expr.X_op == O_constant);
6166 load_register (lreg, &offset_expr, 0);
6167 }
6168 }
6169 }
6170 break;
6171 }
6172
6173 /* We know that sym is in the .rdata section. First we get the
6174 upper 16 bits of the address. */
6175 if (mips_pic == NO_PIC)
6176 {
6177 macro_build_lui (&offset_expr, AT);
6178 used_at = 1;
6179 }
6180 else if (mips_pic == SVR4_PIC)
6181 {
6182 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6183 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6184 used_at = 1;
6185 }
6186 else
6187 abort ();
6188
6189 /* Now we load the register(s). */
6190 if (HAVE_64BIT_GPRS)
6191 {
6192 used_at = 1;
6193 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6194 }
6195 else
6196 {
6197 used_at = 1;
6198 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6199 if (treg != RA)
6200 {
6201 /* FIXME: How in the world do we deal with the possible
6202 overflow here? */
6203 offset_expr.X_add_number += 4;
6204 macro_build (&offset_expr, "lw", "t,o(b)",
6205 treg + 1, BFD_RELOC_LO16, AT);
6206 }
6207 }
6208 break;
6209
6210 case M_LI_DD:
6211 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6212 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6213 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6214 the value and the low order 32 bits are either zero or in
6215 OFFSET_EXPR. */
6216 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6217 {
6218 used_at = 1;
6219 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6220 if (HAVE_64BIT_FPRS)
6221 {
6222 assert (HAVE_64BIT_GPRS);
6223 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6224 }
6225 else
6226 {
6227 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6228 if (offset_expr.X_op == O_absent)
6229 macro_build (NULL, "mtc1", "t,G", 0, treg);
6230 else
6231 {
6232 assert (offset_expr.X_op == O_constant);
6233 load_register (AT, &offset_expr, 0);
6234 macro_build (NULL, "mtc1", "t,G", AT, treg);
6235 }
6236 }
6237 break;
6238 }
6239
6240 assert (offset_expr.X_op == O_symbol
6241 && offset_expr.X_add_number == 0);
6242 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6243 if (strcmp (s, ".lit8") == 0)
6244 {
6245 if (mips_opts.isa != ISA_MIPS1)
6246 {
6247 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6248 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6249 break;
6250 }
6251 breg = mips_gp_register;
6252 r = BFD_RELOC_MIPS_LITERAL;
6253 goto dob;
6254 }
6255 else
6256 {
6257 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6258 used_at = 1;
6259 if (mips_pic == SVR4_PIC)
6260 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6261 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6262 else
6263 {
6264 /* FIXME: This won't work for a 64 bit address. */
6265 macro_build_lui (&offset_expr, AT);
6266 }
6267
6268 if (mips_opts.isa != ISA_MIPS1)
6269 {
6270 macro_build (&offset_expr, "ldc1", "T,o(b)",
6271 treg, BFD_RELOC_LO16, AT);
6272 break;
6273 }
6274 breg = AT;
6275 r = BFD_RELOC_LO16;
6276 goto dob;
6277 }
6278
6279 case M_L_DOB:
6280 if (mips_opts.arch == CPU_R4650)
6281 {
6282 as_bad (_("opcode not supported on this processor"));
6283 break;
6284 }
6285 /* Even on a big endian machine $fn comes before $fn+1. We have
6286 to adjust when loading from memory. */
6287 r = BFD_RELOC_LO16;
6288 dob:
6289 assert (mips_opts.isa == ISA_MIPS1);
6290 macro_build (&offset_expr, "lwc1", "T,o(b)",
6291 target_big_endian ? treg + 1 : treg, r, breg);
6292 /* FIXME: A possible overflow which I don't know how to deal
6293 with. */
6294 offset_expr.X_add_number += 4;
6295 macro_build (&offset_expr, "lwc1", "T,o(b)",
6296 target_big_endian ? treg : treg + 1, r, breg);
6297 break;
6298
6299 case M_L_DAB:
6300 /*
6301 * The MIPS assembler seems to check for X_add_number not
6302 * being double aligned and generating:
6303 * lui at,%hi(foo+1)
6304 * addu at,at,v1
6305 * addiu at,at,%lo(foo+1)
6306 * lwc1 f2,0(at)
6307 * lwc1 f3,4(at)
6308 * But, the resulting address is the same after relocation so why
6309 * generate the extra instruction?
6310 */
6311 if (mips_opts.arch == CPU_R4650)
6312 {
6313 as_bad (_("opcode not supported on this processor"));
6314 break;
6315 }
6316 /* Itbl support may require additional care here. */
6317 coproc = 1;
6318 if (mips_opts.isa != ISA_MIPS1)
6319 {
6320 s = "ldc1";
6321 goto ld;
6322 }
6323
6324 s = "lwc1";
6325 fmt = "T,o(b)";
6326 goto ldd_std;
6327
6328 case M_S_DAB:
6329 if (mips_opts.arch == CPU_R4650)
6330 {
6331 as_bad (_("opcode not supported on this processor"));
6332 break;
6333 }
6334
6335 if (mips_opts.isa != ISA_MIPS1)
6336 {
6337 s = "sdc1";
6338 goto st;
6339 }
6340
6341 s = "swc1";
6342 fmt = "T,o(b)";
6343 /* Itbl support may require additional care here. */
6344 coproc = 1;
6345 goto ldd_std;
6346
6347 case M_LD_AB:
6348 if (HAVE_64BIT_GPRS)
6349 {
6350 s = "ld";
6351 goto ld;
6352 }
6353
6354 s = "lw";
6355 fmt = "t,o(b)";
6356 goto ldd_std;
6357
6358 case M_SD_AB:
6359 if (HAVE_64BIT_GPRS)
6360 {
6361 s = "sd";
6362 goto st;
6363 }
6364
6365 s = "sw";
6366 fmt = "t,o(b)";
6367
6368 ldd_std:
6369 if (offset_expr.X_op != O_symbol
6370 && offset_expr.X_op != O_constant)
6371 {
6372 as_bad (_("expression too complex"));
6373 offset_expr.X_op = O_constant;
6374 }
6375
6376 /* Even on a big endian machine $fn comes before $fn+1. We have
6377 to adjust when loading from memory. We set coproc if we must
6378 load $fn+1 first. */
6379 /* Itbl support may require additional care here. */
6380 if (! target_big_endian)
6381 coproc = 0;
6382
6383 if (mips_pic == NO_PIC
6384 || offset_expr.X_op == O_constant)
6385 {
6386 /* If this is a reference to a GP relative symbol, we want
6387 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6388 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6389 If we have a base register, we use this
6390 addu $at,$breg,$gp
6391 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6392 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6393 If this is not a GP relative symbol, we want
6394 lui $at,<sym> (BFD_RELOC_HI16_S)
6395 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6396 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6397 If there is a base register, we add it to $at after the
6398 lui instruction. If there is a constant, we always use
6399 the last case. */
6400 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6401 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6402 {
6403 relax_start (offset_expr.X_add_symbol);
6404 if (breg == 0)
6405 {
6406 tempreg = mips_gp_register;
6407 }
6408 else
6409 {
6410 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6411 AT, breg, mips_gp_register);
6412 tempreg = AT;
6413 used_at = 1;
6414 }
6415
6416 /* Itbl support may require additional care here. */
6417 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6418 BFD_RELOC_GPREL16, tempreg);
6419 offset_expr.X_add_number += 4;
6420
6421 /* Set mips_optimize to 2 to avoid inserting an
6422 undesired nop. */
6423 hold_mips_optimize = mips_optimize;
6424 mips_optimize = 2;
6425 /* Itbl support may require additional care here. */
6426 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6427 BFD_RELOC_GPREL16, tempreg);
6428 mips_optimize = hold_mips_optimize;
6429
6430 relax_switch ();
6431
6432 /* We just generated two relocs. When tc_gen_reloc
6433 handles this case, it will skip the first reloc and
6434 handle the second. The second reloc already has an
6435 extra addend of 4, which we added above. We must
6436 subtract it out, and then subtract another 4 to make
6437 the first reloc come out right. The second reloc
6438 will come out right because we are going to add 4 to
6439 offset_expr when we build its instruction below.
6440
6441 If we have a symbol, then we don't want to include
6442 the offset, because it will wind up being included
6443 when we generate the reloc. */
6444
6445 if (offset_expr.X_op == O_constant)
6446 offset_expr.X_add_number -= 8;
6447 else
6448 {
6449 offset_expr.X_add_number = -4;
6450 offset_expr.X_op = O_constant;
6451 }
6452 }
6453 used_at = 1;
6454 macro_build_lui (&offset_expr, AT);
6455 if (breg != 0)
6456 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6457 /* Itbl support may require additional care here. */
6458 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6459 BFD_RELOC_LO16, AT);
6460 /* FIXME: How do we handle overflow here? */
6461 offset_expr.X_add_number += 4;
6462 /* Itbl support may require additional care here. */
6463 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6464 BFD_RELOC_LO16, AT);
6465 if (mips_relax.sequence)
6466 relax_end ();
6467 }
6468 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6469 {
6470 /* If this is a reference to an external symbol, we want
6471 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6472 nop
6473 <op> $treg,0($at)
6474 <op> $treg+1,4($at)
6475 Otherwise we want
6476 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6477 nop
6478 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6479 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6480 If there is a base register we add it to $at before the
6481 lwc1 instructions. If there is a constant we include it
6482 in the lwc1 instructions. */
6483 used_at = 1;
6484 expr1.X_add_number = offset_expr.X_add_number;
6485 if (expr1.X_add_number < -0x8000
6486 || expr1.X_add_number >= 0x8000 - 4)
6487 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6488 load_got_offset (AT, &offset_expr);
6489 load_delay_nop ();
6490 if (breg != 0)
6491 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6492
6493 /* Set mips_optimize to 2 to avoid inserting an undesired
6494 nop. */
6495 hold_mips_optimize = mips_optimize;
6496 mips_optimize = 2;
6497
6498 /* Itbl support may require additional care here. */
6499 relax_start (offset_expr.X_add_symbol);
6500 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6501 BFD_RELOC_LO16, AT);
6502 expr1.X_add_number += 4;
6503 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6504 BFD_RELOC_LO16, AT);
6505 relax_switch ();
6506 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6507 BFD_RELOC_LO16, AT);
6508 offset_expr.X_add_number += 4;
6509 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6510 BFD_RELOC_LO16, AT);
6511 relax_end ();
6512
6513 mips_optimize = hold_mips_optimize;
6514 }
6515 else if (mips_pic == SVR4_PIC)
6516 {
6517 int gpdelay;
6518
6519 /* If this is a reference to an external symbol, we want
6520 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6521 addu $at,$at,$gp
6522 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6523 nop
6524 <op> $treg,0($at)
6525 <op> $treg+1,4($at)
6526 Otherwise we want
6527 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6528 nop
6529 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6530 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6531 If there is a base register we add it to $at before the
6532 lwc1 instructions. If there is a constant we include it
6533 in the lwc1 instructions. */
6534 used_at = 1;
6535 expr1.X_add_number = offset_expr.X_add_number;
6536 offset_expr.X_add_number = 0;
6537 if (expr1.X_add_number < -0x8000
6538 || expr1.X_add_number >= 0x8000 - 4)
6539 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6540 gpdelay = reg_needs_delay (mips_gp_register);
6541 relax_start (offset_expr.X_add_symbol);
6542 macro_build (&offset_expr, "lui", "t,u",
6543 AT, BFD_RELOC_MIPS_GOT_HI16);
6544 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6545 AT, AT, mips_gp_register);
6546 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6547 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6548 load_delay_nop ();
6549 if (breg != 0)
6550 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6551 /* Itbl support may require additional care here. */
6552 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6553 BFD_RELOC_LO16, AT);
6554 expr1.X_add_number += 4;
6555
6556 /* Set mips_optimize to 2 to avoid inserting an undesired
6557 nop. */
6558 hold_mips_optimize = mips_optimize;
6559 mips_optimize = 2;
6560 /* Itbl support may require additional care here. */
6561 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6562 BFD_RELOC_LO16, AT);
6563 mips_optimize = hold_mips_optimize;
6564 expr1.X_add_number -= 4;
6565
6566 relax_switch ();
6567 offset_expr.X_add_number = expr1.X_add_number;
6568 if (gpdelay)
6569 macro_build (NULL, "nop", "");
6570 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6571 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6572 load_delay_nop ();
6573 if (breg != 0)
6574 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6575 /* Itbl support may require additional care here. */
6576 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6577 BFD_RELOC_LO16, AT);
6578 offset_expr.X_add_number += 4;
6579
6580 /* Set mips_optimize to 2 to avoid inserting an undesired
6581 nop. */
6582 hold_mips_optimize = mips_optimize;
6583 mips_optimize = 2;
6584 /* Itbl support may require additional care here. */
6585 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6586 BFD_RELOC_LO16, AT);
6587 mips_optimize = hold_mips_optimize;
6588 relax_end ();
6589 }
6590 else
6591 abort ();
6592
6593 break;
6594
6595 case M_LD_OB:
6596 s = "lw";
6597 goto sd_ob;
6598 case M_SD_OB:
6599 s = "sw";
6600 sd_ob:
6601 assert (HAVE_32BIT_ADDRESSES);
6602 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6603 offset_expr.X_add_number += 4;
6604 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6605 break;
6606
6607 /* New code added to support COPZ instructions.
6608 This code builds table entries out of the macros in mip_opcodes.
6609 R4000 uses interlocks to handle coproc delays.
6610 Other chips (like the R3000) require nops to be inserted for delays.
6611
6612 FIXME: Currently, we require that the user handle delays.
6613 In order to fill delay slots for non-interlocked chips,
6614 we must have a way to specify delays based on the coprocessor.
6615 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6616 What are the side-effects of the cop instruction?
6617 What cache support might we have and what are its effects?
6618 Both coprocessor & memory require delays. how long???
6619 What registers are read/set/modified?
6620
6621 If an itbl is provided to interpret cop instructions,
6622 this knowledge can be encoded in the itbl spec. */
6623
6624 case M_COP0:
6625 s = "c0";
6626 goto copz;
6627 case M_COP1:
6628 s = "c1";
6629 goto copz;
6630 case M_COP2:
6631 s = "c2";
6632 goto copz;
6633 case M_COP3:
6634 s = "c3";
6635 copz:
6636 /* For now we just do C (same as Cz). The parameter will be
6637 stored in insn_opcode by mips_ip. */
6638 macro_build (NULL, s, "C", ip->insn_opcode);
6639 break;
6640
6641 case M_MOVE:
6642 move_register (dreg, sreg);
6643 break;
6644
6645 #ifdef LOSING_COMPILER
6646 default:
6647 /* Try and see if this is a new itbl instruction.
6648 This code builds table entries out of the macros in mip_opcodes.
6649 FIXME: For now we just assemble the expression and pass it's
6650 value along as a 32-bit immediate.
6651 We may want to have the assembler assemble this value,
6652 so that we gain the assembler's knowledge of delay slots,
6653 symbols, etc.
6654 Would it be more efficient to use mask (id) here? */
6655 if (itbl_have_entries
6656 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6657 {
6658 s = ip->insn_mo->name;
6659 s2 = "cop3";
6660 coproc = ITBL_DECODE_PNUM (immed_expr);;
6661 macro_build (&immed_expr, s, "C");
6662 break;
6663 }
6664 macro2 (ip);
6665 break;
6666 }
6667 if (mips_opts.noat && used_at)
6668 as_bad (_("Macro used $at after \".set noat\""));
6669 }
6670
6671 static void
6672 macro2 (struct mips_cl_insn *ip)
6673 {
6674 register int treg, sreg, dreg, breg;
6675 int tempreg;
6676 int mask;
6677 int used_at;
6678 expressionS expr1;
6679 const char *s;
6680 const char *s2;
6681 const char *fmt;
6682 int likely = 0;
6683 int dbl = 0;
6684 int coproc = 0;
6685 int lr = 0;
6686 int imm = 0;
6687 int off;
6688 offsetT maxnum;
6689 bfd_reloc_code_real_type r;
6690
6691 treg = (ip->insn_opcode >> 16) & 0x1f;
6692 dreg = (ip->insn_opcode >> 11) & 0x1f;
6693 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6694 mask = ip->insn_mo->mask;
6695
6696 expr1.X_op = O_constant;
6697 expr1.X_op_symbol = NULL;
6698 expr1.X_add_symbol = NULL;
6699 expr1.X_add_number = 1;
6700
6701 switch (mask)
6702 {
6703 #endif /* LOSING_COMPILER */
6704
6705 case M_DMUL:
6706 dbl = 1;
6707 case M_MUL:
6708 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6709 macro_build (NULL, "mflo", "d", dreg);
6710 break;
6711
6712 case M_DMUL_I:
6713 dbl = 1;
6714 case M_MUL_I:
6715 /* The MIPS assembler some times generates shifts and adds. I'm
6716 not trying to be that fancy. GCC should do this for us
6717 anyway. */
6718 used_at = 1;
6719 load_register (AT, &imm_expr, dbl);
6720 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6721 macro_build (NULL, "mflo", "d", dreg);
6722 break;
6723
6724 case M_DMULO_I:
6725 dbl = 1;
6726 case M_MULO_I:
6727 imm = 1;
6728 goto do_mulo;
6729
6730 case M_DMULO:
6731 dbl = 1;
6732 case M_MULO:
6733 do_mulo:
6734 start_noreorder ();
6735 used_at = 1;
6736 if (imm)
6737 load_register (AT, &imm_expr, dbl);
6738 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6739 macro_build (NULL, "mflo", "d", dreg);
6740 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6741 macro_build (NULL, "mfhi", "d", AT);
6742 if (mips_trap)
6743 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6744 else
6745 {
6746 expr1.X_add_number = 8;
6747 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6748 macro_build (NULL, "nop", "", 0);
6749 macro_build (NULL, "break", "c", 6);
6750 }
6751 end_noreorder ();
6752 macro_build (NULL, "mflo", "d", dreg);
6753 break;
6754
6755 case M_DMULOU_I:
6756 dbl = 1;
6757 case M_MULOU_I:
6758 imm = 1;
6759 goto do_mulou;
6760
6761 case M_DMULOU:
6762 dbl = 1;
6763 case M_MULOU:
6764 do_mulou:
6765 start_noreorder ();
6766 used_at = 1;
6767 if (imm)
6768 load_register (AT, &imm_expr, dbl);
6769 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6770 sreg, imm ? AT : treg);
6771 macro_build (NULL, "mfhi", "d", AT);
6772 macro_build (NULL, "mflo", "d", dreg);
6773 if (mips_trap)
6774 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6775 else
6776 {
6777 expr1.X_add_number = 8;
6778 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6779 macro_build (NULL, "nop", "", 0);
6780 macro_build (NULL, "break", "c", 6);
6781 }
6782 end_noreorder ();
6783 break;
6784
6785 case M_DROL:
6786 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6787 {
6788 if (dreg == sreg)
6789 {
6790 tempreg = AT;
6791 used_at = 1;
6792 }
6793 else
6794 {
6795 tempreg = dreg;
6796 }
6797 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6798 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6799 break;
6800 }
6801 used_at = 1;
6802 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6803 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6804 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6805 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6806 break;
6807
6808 case M_ROL:
6809 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6810 {
6811 if (dreg == sreg)
6812 {
6813 tempreg = AT;
6814 used_at = 1;
6815 }
6816 else
6817 {
6818 tempreg = dreg;
6819 }
6820 macro_build (NULL, "negu", "d,w", tempreg, treg);
6821 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6822 break;
6823 }
6824 used_at = 1;
6825 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6826 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6827 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6828 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6829 break;
6830
6831 case M_DROL_I:
6832 {
6833 unsigned int rot;
6834 char *l, *r;
6835
6836 if (imm_expr.X_op != O_constant)
6837 as_bad (_("Improper rotate count"));
6838 rot = imm_expr.X_add_number & 0x3f;
6839 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6840 {
6841 rot = (64 - rot) & 0x3f;
6842 if (rot >= 32)
6843 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6844 else
6845 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6846 break;
6847 }
6848 if (rot == 0)
6849 {
6850 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6851 break;
6852 }
6853 l = (rot < 0x20) ? "dsll" : "dsll32";
6854 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6855 rot &= 0x1f;
6856 used_at = 1;
6857 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6858 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6859 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6860 }
6861 break;
6862
6863 case M_ROL_I:
6864 {
6865 unsigned int rot;
6866
6867 if (imm_expr.X_op != O_constant)
6868 as_bad (_("Improper rotate count"));
6869 rot = imm_expr.X_add_number & 0x1f;
6870 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6871 {
6872 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6873 break;
6874 }
6875 if (rot == 0)
6876 {
6877 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6878 break;
6879 }
6880 used_at = 1;
6881 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6882 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6883 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6884 }
6885 break;
6886
6887 case M_DROR:
6888 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6889 {
6890 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6891 break;
6892 }
6893 used_at = 1;
6894 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6895 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6896 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6897 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6898 break;
6899
6900 case M_ROR:
6901 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6902 {
6903 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
6904 break;
6905 }
6906 used_at = 1;
6907 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6908 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6909 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6910 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6911 break;
6912
6913 case M_DROR_I:
6914 {
6915 unsigned int rot;
6916 char *l, *r;
6917
6918 if (imm_expr.X_op != O_constant)
6919 as_bad (_("Improper rotate count"));
6920 rot = imm_expr.X_add_number & 0x3f;
6921 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6922 {
6923 if (rot >= 32)
6924 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6925 else
6926 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6927 break;
6928 }
6929 if (rot == 0)
6930 {
6931 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6932 break;
6933 }
6934 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6935 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6936 rot &= 0x1f;
6937 used_at = 1;
6938 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
6939 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6940 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6941 }
6942 break;
6943
6944 case M_ROR_I:
6945 {
6946 unsigned int rot;
6947
6948 if (imm_expr.X_op != O_constant)
6949 as_bad (_("Improper rotate count"));
6950 rot = imm_expr.X_add_number & 0x1f;
6951 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6952 {
6953 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
6954 break;
6955 }
6956 if (rot == 0)
6957 {
6958 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6959 break;
6960 }
6961 used_at = 1;
6962 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
6963 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6964 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6965 }
6966 break;
6967
6968 case M_S_DOB:
6969 if (mips_opts.arch == CPU_R4650)
6970 {
6971 as_bad (_("opcode not supported on this processor"));
6972 break;
6973 }
6974 assert (mips_opts.isa == ISA_MIPS1);
6975 /* Even on a big endian machine $fn comes before $fn+1. We have
6976 to adjust when storing to memory. */
6977 macro_build (&offset_expr, "swc1", "T,o(b)",
6978 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
6979 offset_expr.X_add_number += 4;
6980 macro_build (&offset_expr, "swc1", "T,o(b)",
6981 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
6982 break;
6983
6984 case M_SEQ:
6985 if (sreg == 0)
6986 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
6987 else if (treg == 0)
6988 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6989 else
6990 {
6991 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
6992 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
6993 }
6994 break;
6995
6996 case M_SEQ_I:
6997 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6998 {
6999 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7000 break;
7001 }
7002 if (sreg == 0)
7003 {
7004 as_warn (_("Instruction %s: result is always false"),
7005 ip->insn_mo->name);
7006 move_register (dreg, 0);
7007 break;
7008 }
7009 if (imm_expr.X_op == O_constant
7010 && imm_expr.X_add_number >= 0
7011 && imm_expr.X_add_number < 0x10000)
7012 {
7013 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7014 }
7015 else if (imm_expr.X_op == O_constant
7016 && imm_expr.X_add_number > -0x8000
7017 && imm_expr.X_add_number < 0)
7018 {
7019 imm_expr.X_add_number = -imm_expr.X_add_number;
7020 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7021 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7022 }
7023 else
7024 {
7025 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7026 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7027 used_at = 1;
7028 }
7029 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7030 break;
7031
7032 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7033 s = "slt";
7034 goto sge;
7035 case M_SGEU:
7036 s = "sltu";
7037 sge:
7038 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7039 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7040 break;
7041
7042 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7043 case M_SGEU_I:
7044 if (imm_expr.X_op == O_constant
7045 && imm_expr.X_add_number >= -0x8000
7046 && imm_expr.X_add_number < 0x8000)
7047 {
7048 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7049 dreg, sreg, BFD_RELOC_LO16);
7050 }
7051 else
7052 {
7053 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7054 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7055 dreg, sreg, AT);
7056 used_at = 1;
7057 }
7058 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7059 break;
7060
7061 case M_SGT: /* sreg > treg <==> treg < sreg */
7062 s = "slt";
7063 goto sgt;
7064 case M_SGTU:
7065 s = "sltu";
7066 sgt:
7067 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7068 break;
7069
7070 case M_SGT_I: /* sreg > I <==> I < sreg */
7071 s = "slt";
7072 goto sgti;
7073 case M_SGTU_I:
7074 s = "sltu";
7075 sgti:
7076 used_at = 1;
7077 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7078 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7079 break;
7080
7081 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7082 s = "slt";
7083 goto sle;
7084 case M_SLEU:
7085 s = "sltu";
7086 sle:
7087 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7088 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7089 break;
7090
7091 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7092 s = "slt";
7093 goto slei;
7094 case M_SLEU_I:
7095 s = "sltu";
7096 slei:
7097 used_at = 1;
7098 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7099 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7100 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7101 break;
7102
7103 case M_SLT_I:
7104 if (imm_expr.X_op == O_constant
7105 && imm_expr.X_add_number >= -0x8000
7106 && imm_expr.X_add_number < 0x8000)
7107 {
7108 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7109 break;
7110 }
7111 used_at = 1;
7112 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7113 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7114 break;
7115
7116 case M_SLTU_I:
7117 if (imm_expr.X_op == O_constant
7118 && imm_expr.X_add_number >= -0x8000
7119 && imm_expr.X_add_number < 0x8000)
7120 {
7121 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7122 BFD_RELOC_LO16);
7123 break;
7124 }
7125 used_at = 1;
7126 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7127 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7128 break;
7129
7130 case M_SNE:
7131 if (sreg == 0)
7132 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7133 else if (treg == 0)
7134 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7135 else
7136 {
7137 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7138 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7139 }
7140 break;
7141
7142 case M_SNE_I:
7143 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7144 {
7145 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7146 break;
7147 }
7148 if (sreg == 0)
7149 {
7150 as_warn (_("Instruction %s: result is always true"),
7151 ip->insn_mo->name);
7152 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7153 dreg, 0, BFD_RELOC_LO16);
7154 break;
7155 }
7156 if (imm_expr.X_op == O_constant
7157 && imm_expr.X_add_number >= 0
7158 && imm_expr.X_add_number < 0x10000)
7159 {
7160 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7161 }
7162 else if (imm_expr.X_op == O_constant
7163 && imm_expr.X_add_number > -0x8000
7164 && imm_expr.X_add_number < 0)
7165 {
7166 imm_expr.X_add_number = -imm_expr.X_add_number;
7167 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7168 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7169 }
7170 else
7171 {
7172 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7173 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7174 used_at = 1;
7175 }
7176 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7177 break;
7178
7179 case M_DSUB_I:
7180 dbl = 1;
7181 case M_SUB_I:
7182 if (imm_expr.X_op == O_constant
7183 && imm_expr.X_add_number > -0x8000
7184 && imm_expr.X_add_number <= 0x8000)
7185 {
7186 imm_expr.X_add_number = -imm_expr.X_add_number;
7187 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7188 dreg, sreg, BFD_RELOC_LO16);
7189 break;
7190 }
7191 used_at = 1;
7192 load_register (AT, &imm_expr, dbl);
7193 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7194 break;
7195
7196 case M_DSUBU_I:
7197 dbl = 1;
7198 case M_SUBU_I:
7199 if (imm_expr.X_op == O_constant
7200 && imm_expr.X_add_number > -0x8000
7201 && imm_expr.X_add_number <= 0x8000)
7202 {
7203 imm_expr.X_add_number = -imm_expr.X_add_number;
7204 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7205 dreg, sreg, BFD_RELOC_LO16);
7206 break;
7207 }
7208 used_at = 1;
7209 load_register (AT, &imm_expr, dbl);
7210 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7211 break;
7212
7213 case M_TEQ_I:
7214 s = "teq";
7215 goto trap;
7216 case M_TGE_I:
7217 s = "tge";
7218 goto trap;
7219 case M_TGEU_I:
7220 s = "tgeu";
7221 goto trap;
7222 case M_TLT_I:
7223 s = "tlt";
7224 goto trap;
7225 case M_TLTU_I:
7226 s = "tltu";
7227 goto trap;
7228 case M_TNE_I:
7229 s = "tne";
7230 trap:
7231 used_at = 1;
7232 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7233 macro_build (NULL, s, "s,t", sreg, AT);
7234 break;
7235
7236 case M_TRUNCWS:
7237 case M_TRUNCWD:
7238 assert (mips_opts.isa == ISA_MIPS1);
7239 used_at = 1;
7240 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7241 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7242
7243 /*
7244 * Is the double cfc1 instruction a bug in the mips assembler;
7245 * or is there a reason for it?
7246 */
7247 start_noreorder ();
7248 macro_build (NULL, "cfc1", "t,G", treg, RA);
7249 macro_build (NULL, "cfc1", "t,G", treg, RA);
7250 macro_build (NULL, "nop", "");
7251 expr1.X_add_number = 3;
7252 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7253 expr1.X_add_number = 2;
7254 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7255 macro_build (NULL, "ctc1", "t,G", AT, RA);
7256 macro_build (NULL, "nop", "");
7257 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7258 dreg, sreg);
7259 macro_build (NULL, "ctc1", "t,G", treg, RA);
7260 macro_build (NULL, "nop", "");
7261 end_noreorder ();
7262 break;
7263
7264 case M_ULH:
7265 s = "lb";
7266 goto ulh;
7267 case M_ULHU:
7268 s = "lbu";
7269 ulh:
7270 used_at = 1;
7271 if (offset_expr.X_add_number >= 0x7fff)
7272 as_bad (_("operand overflow"));
7273 if (! target_big_endian)
7274 ++offset_expr.X_add_number;
7275 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7276 if (! target_big_endian)
7277 --offset_expr.X_add_number;
7278 else
7279 ++offset_expr.X_add_number;
7280 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7281 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7282 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7283 break;
7284
7285 case M_ULD:
7286 s = "ldl";
7287 s2 = "ldr";
7288 off = 7;
7289 goto ulw;
7290 case M_ULW:
7291 s = "lwl";
7292 s2 = "lwr";
7293 off = 3;
7294 ulw:
7295 if (offset_expr.X_add_number >= 0x8000 - off)
7296 as_bad (_("operand overflow"));
7297 if (treg != breg)
7298 tempreg = treg;
7299 else
7300 {
7301 used_at = 1;
7302 tempreg = AT;
7303 }
7304 if (! target_big_endian)
7305 offset_expr.X_add_number += off;
7306 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7307 if (! target_big_endian)
7308 offset_expr.X_add_number -= off;
7309 else
7310 offset_expr.X_add_number += off;
7311 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7312
7313 /* If necessary, move the result in tempreg the final destination. */
7314 if (treg == tempreg)
7315 break;
7316 /* Protect second load's delay slot. */
7317 load_delay_nop ();
7318 move_register (treg, tempreg);
7319 break;
7320
7321 case M_ULD_A:
7322 s = "ldl";
7323 s2 = "ldr";
7324 off = 7;
7325 goto ulwa;
7326 case M_ULW_A:
7327 s = "lwl";
7328 s2 = "lwr";
7329 off = 3;
7330 ulwa:
7331 used_at = 1;
7332 load_address (AT, &offset_expr, &used_at);
7333 if (breg != 0)
7334 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7335 if (! target_big_endian)
7336 expr1.X_add_number = off;
7337 else
7338 expr1.X_add_number = 0;
7339 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7340 if (! target_big_endian)
7341 expr1.X_add_number = 0;
7342 else
7343 expr1.X_add_number = off;
7344 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7345 break;
7346
7347 case M_ULH_A:
7348 case M_ULHU_A:
7349 used_at = 1;
7350 load_address (AT, &offset_expr, &used_at);
7351 if (breg != 0)
7352 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7353 if (target_big_endian)
7354 expr1.X_add_number = 0;
7355 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7356 treg, BFD_RELOC_LO16, AT);
7357 if (target_big_endian)
7358 expr1.X_add_number = 1;
7359 else
7360 expr1.X_add_number = 0;
7361 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7362 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7363 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7364 break;
7365
7366 case M_USH:
7367 used_at = 1;
7368 if (offset_expr.X_add_number >= 0x7fff)
7369 as_bad (_("operand overflow"));
7370 if (target_big_endian)
7371 ++offset_expr.X_add_number;
7372 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7373 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7374 if (target_big_endian)
7375 --offset_expr.X_add_number;
7376 else
7377 ++offset_expr.X_add_number;
7378 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7379 break;
7380
7381 case M_USD:
7382 s = "sdl";
7383 s2 = "sdr";
7384 off = 7;
7385 goto usw;
7386 case M_USW:
7387 s = "swl";
7388 s2 = "swr";
7389 off = 3;
7390 usw:
7391 if (offset_expr.X_add_number >= 0x8000 - off)
7392 as_bad (_("operand overflow"));
7393 if (! target_big_endian)
7394 offset_expr.X_add_number += off;
7395 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7396 if (! target_big_endian)
7397 offset_expr.X_add_number -= off;
7398 else
7399 offset_expr.X_add_number += off;
7400 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7401 break;
7402
7403 case M_USD_A:
7404 s = "sdl";
7405 s2 = "sdr";
7406 off = 7;
7407 goto uswa;
7408 case M_USW_A:
7409 s = "swl";
7410 s2 = "swr";
7411 off = 3;
7412 uswa:
7413 used_at = 1;
7414 load_address (AT, &offset_expr, &used_at);
7415 if (breg != 0)
7416 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7417 if (! target_big_endian)
7418 expr1.X_add_number = off;
7419 else
7420 expr1.X_add_number = 0;
7421 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7422 if (! target_big_endian)
7423 expr1.X_add_number = 0;
7424 else
7425 expr1.X_add_number = off;
7426 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7427 break;
7428
7429 case M_USH_A:
7430 used_at = 1;
7431 load_address (AT, &offset_expr, &used_at);
7432 if (breg != 0)
7433 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7434 if (! target_big_endian)
7435 expr1.X_add_number = 0;
7436 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7437 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7438 if (! target_big_endian)
7439 expr1.X_add_number = 1;
7440 else
7441 expr1.X_add_number = 0;
7442 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7443 if (! target_big_endian)
7444 expr1.X_add_number = 0;
7445 else
7446 expr1.X_add_number = 1;
7447 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7448 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7449 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7450 break;
7451
7452 default:
7453 /* FIXME: Check if this is one of the itbl macros, since they
7454 are added dynamically. */
7455 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7456 break;
7457 }
7458 if (mips_opts.noat && used_at)
7459 as_bad (_("Macro used $at after \".set noat\""));
7460 }
7461
7462 /* Implement macros in mips16 mode. */
7463
7464 static void
7465 mips16_macro (struct mips_cl_insn *ip)
7466 {
7467 int mask;
7468 int xreg, yreg, zreg, tmp;
7469 expressionS expr1;
7470 int dbl;
7471 const char *s, *s2, *s3;
7472
7473 mask = ip->insn_mo->mask;
7474
7475 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7476 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7477 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7478
7479 expr1.X_op = O_constant;
7480 expr1.X_op_symbol = NULL;
7481 expr1.X_add_symbol = NULL;
7482 expr1.X_add_number = 1;
7483
7484 dbl = 0;
7485
7486 switch (mask)
7487 {
7488 default:
7489 internalError ();
7490
7491 case M_DDIV_3:
7492 dbl = 1;
7493 case M_DIV_3:
7494 s = "mflo";
7495 goto do_div3;
7496 case M_DREM_3:
7497 dbl = 1;
7498 case M_REM_3:
7499 s = "mfhi";
7500 do_div3:
7501 start_noreorder ();
7502 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7503 expr1.X_add_number = 2;
7504 macro_build (&expr1, "bnez", "x,p", yreg);
7505 macro_build (NULL, "break", "6", 7);
7506
7507 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7508 since that causes an overflow. We should do that as well,
7509 but I don't see how to do the comparisons without a temporary
7510 register. */
7511 end_noreorder ();
7512 macro_build (NULL, s, "x", zreg);
7513 break;
7514
7515 case M_DIVU_3:
7516 s = "divu";
7517 s2 = "mflo";
7518 goto do_divu3;
7519 case M_REMU_3:
7520 s = "divu";
7521 s2 = "mfhi";
7522 goto do_divu3;
7523 case M_DDIVU_3:
7524 s = "ddivu";
7525 s2 = "mflo";
7526 goto do_divu3;
7527 case M_DREMU_3:
7528 s = "ddivu";
7529 s2 = "mfhi";
7530 do_divu3:
7531 start_noreorder ();
7532 macro_build (NULL, s, "0,x,y", xreg, yreg);
7533 expr1.X_add_number = 2;
7534 macro_build (&expr1, "bnez", "x,p", yreg);
7535 macro_build (NULL, "break", "6", 7);
7536 end_noreorder ();
7537 macro_build (NULL, s2, "x", zreg);
7538 break;
7539
7540 case M_DMUL:
7541 dbl = 1;
7542 case M_MUL:
7543 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7544 macro_build (NULL, "mflo", "x", zreg);
7545 break;
7546
7547 case M_DSUBU_I:
7548 dbl = 1;
7549 goto do_subu;
7550 case M_SUBU_I:
7551 do_subu:
7552 if (imm_expr.X_op != O_constant)
7553 as_bad (_("Unsupported large constant"));
7554 imm_expr.X_add_number = -imm_expr.X_add_number;
7555 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7556 break;
7557
7558 case M_SUBU_I_2:
7559 if (imm_expr.X_op != O_constant)
7560 as_bad (_("Unsupported large constant"));
7561 imm_expr.X_add_number = -imm_expr.X_add_number;
7562 macro_build (&imm_expr, "addiu", "x,k", xreg);
7563 break;
7564
7565 case M_DSUBU_I_2:
7566 if (imm_expr.X_op != O_constant)
7567 as_bad (_("Unsupported large constant"));
7568 imm_expr.X_add_number = -imm_expr.X_add_number;
7569 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7570 break;
7571
7572 case M_BEQ:
7573 s = "cmp";
7574 s2 = "bteqz";
7575 goto do_branch;
7576 case M_BNE:
7577 s = "cmp";
7578 s2 = "btnez";
7579 goto do_branch;
7580 case M_BLT:
7581 s = "slt";
7582 s2 = "btnez";
7583 goto do_branch;
7584 case M_BLTU:
7585 s = "sltu";
7586 s2 = "btnez";
7587 goto do_branch;
7588 case M_BLE:
7589 s = "slt";
7590 s2 = "bteqz";
7591 goto do_reverse_branch;
7592 case M_BLEU:
7593 s = "sltu";
7594 s2 = "bteqz";
7595 goto do_reverse_branch;
7596 case M_BGE:
7597 s = "slt";
7598 s2 = "bteqz";
7599 goto do_branch;
7600 case M_BGEU:
7601 s = "sltu";
7602 s2 = "bteqz";
7603 goto do_branch;
7604 case M_BGT:
7605 s = "slt";
7606 s2 = "btnez";
7607 goto do_reverse_branch;
7608 case M_BGTU:
7609 s = "sltu";
7610 s2 = "btnez";
7611
7612 do_reverse_branch:
7613 tmp = xreg;
7614 xreg = yreg;
7615 yreg = tmp;
7616
7617 do_branch:
7618 macro_build (NULL, s, "x,y", xreg, yreg);
7619 macro_build (&offset_expr, s2, "p");
7620 break;
7621
7622 case M_BEQ_I:
7623 s = "cmpi";
7624 s2 = "bteqz";
7625 s3 = "x,U";
7626 goto do_branch_i;
7627 case M_BNE_I:
7628 s = "cmpi";
7629 s2 = "btnez";
7630 s3 = "x,U";
7631 goto do_branch_i;
7632 case M_BLT_I:
7633 s = "slti";
7634 s2 = "btnez";
7635 s3 = "x,8";
7636 goto do_branch_i;
7637 case M_BLTU_I:
7638 s = "sltiu";
7639 s2 = "btnez";
7640 s3 = "x,8";
7641 goto do_branch_i;
7642 case M_BLE_I:
7643 s = "slti";
7644 s2 = "btnez";
7645 s3 = "x,8";
7646 goto do_addone_branch_i;
7647 case M_BLEU_I:
7648 s = "sltiu";
7649 s2 = "btnez";
7650 s3 = "x,8";
7651 goto do_addone_branch_i;
7652 case M_BGE_I:
7653 s = "slti";
7654 s2 = "bteqz";
7655 s3 = "x,8";
7656 goto do_branch_i;
7657 case M_BGEU_I:
7658 s = "sltiu";
7659 s2 = "bteqz";
7660 s3 = "x,8";
7661 goto do_branch_i;
7662 case M_BGT_I:
7663 s = "slti";
7664 s2 = "bteqz";
7665 s3 = "x,8";
7666 goto do_addone_branch_i;
7667 case M_BGTU_I:
7668 s = "sltiu";
7669 s2 = "bteqz";
7670 s3 = "x,8";
7671
7672 do_addone_branch_i:
7673 if (imm_expr.X_op != O_constant)
7674 as_bad (_("Unsupported large constant"));
7675 ++imm_expr.X_add_number;
7676
7677 do_branch_i:
7678 macro_build (&imm_expr, s, s3, xreg);
7679 macro_build (&offset_expr, s2, "p");
7680 break;
7681
7682 case M_ABS:
7683 expr1.X_add_number = 0;
7684 macro_build (&expr1, "slti", "x,8", yreg);
7685 if (xreg != yreg)
7686 move_register (xreg, yreg);
7687 expr1.X_add_number = 2;
7688 macro_build (&expr1, "bteqz", "p");
7689 macro_build (NULL, "neg", "x,w", xreg, xreg);
7690 }
7691 }
7692
7693 /* For consistency checking, verify that all bits are specified either
7694 by the match/mask part of the instruction definition, or by the
7695 operand list. */
7696 static int
7697 validate_mips_insn (const struct mips_opcode *opc)
7698 {
7699 const char *p = opc->args;
7700 char c;
7701 unsigned long used_bits = opc->mask;
7702
7703 if ((used_bits & opc->match) != opc->match)
7704 {
7705 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7706 opc->name, opc->args);
7707 return 0;
7708 }
7709 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7710 while (*p)
7711 switch (c = *p++)
7712 {
7713 case ',': break;
7714 case '(': break;
7715 case ')': break;
7716 case '+':
7717 switch (c = *p++)
7718 {
7719 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7720 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7721 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7722 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7723 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7724 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7725 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7726 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7727 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7728 case 'I': break;
7729 default:
7730 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7731 c, opc->name, opc->args);
7732 return 0;
7733 }
7734 break;
7735 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7736 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7737 case 'A': break;
7738 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7739 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7740 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7741 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7742 case 'F': break;
7743 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7744 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7745 case 'I': break;
7746 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7747 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7748 case 'L': break;
7749 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7750 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7751 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7752 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7753 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7754 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7755 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7756 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7757 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7758 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7759 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7760 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7761 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7762 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7763 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7764 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7765 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7766 case 'f': break;
7767 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7768 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7769 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7770 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7771 case 'l': break;
7772 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7773 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7774 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7775 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7776 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7777 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7778 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7779 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7780 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7781 case 'x': break;
7782 case 'z': break;
7783 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7784 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7785 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7786 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7787 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7788 case '[': break;
7789 case ']': break;
7790 default:
7791 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7792 c, opc->name, opc->args);
7793 return 0;
7794 }
7795 #undef USE_BITS
7796 if (used_bits != 0xffffffff)
7797 {
7798 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7799 ~used_bits & 0xffffffff, opc->name, opc->args);
7800 return 0;
7801 }
7802 return 1;
7803 }
7804
7805 /* This routine assembles an instruction into its binary format. As a
7806 side effect, it sets one of the global variables imm_reloc or
7807 offset_reloc to the type of relocation to do if one of the operands
7808 is an address expression. */
7809
7810 static void
7811 mips_ip (char *str, struct mips_cl_insn *ip)
7812 {
7813 char *s;
7814 const char *args;
7815 char c = 0;
7816 struct mips_opcode *insn;
7817 char *argsStart;
7818 unsigned int regno;
7819 unsigned int lastregno = 0;
7820 unsigned int lastpos = 0;
7821 unsigned int limlo, limhi;
7822 char *s_reset;
7823 char save_c = 0;
7824
7825 insn_error = NULL;
7826
7827 /* If the instruction contains a '.', we first try to match an instruction
7828 including the '.'. Then we try again without the '.'. */
7829 insn = NULL;
7830 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7831 continue;
7832
7833 /* If we stopped on whitespace, then replace the whitespace with null for
7834 the call to hash_find. Save the character we replaced just in case we
7835 have to re-parse the instruction. */
7836 if (ISSPACE (*s))
7837 {
7838 save_c = *s;
7839 *s++ = '\0';
7840 }
7841
7842 insn = (struct mips_opcode *) hash_find (op_hash, str);
7843
7844 /* If we didn't find the instruction in the opcode table, try again, but
7845 this time with just the instruction up to, but not including the
7846 first '.'. */
7847 if (insn == NULL)
7848 {
7849 /* Restore the character we overwrite above (if any). */
7850 if (save_c)
7851 *(--s) = save_c;
7852
7853 /* Scan up to the first '.' or whitespace. */
7854 for (s = str;
7855 *s != '\0' && *s != '.' && !ISSPACE (*s);
7856 ++s)
7857 continue;
7858
7859 /* If we did not find a '.', then we can quit now. */
7860 if (*s != '.')
7861 {
7862 insn_error = "unrecognized opcode";
7863 return;
7864 }
7865
7866 /* Lookup the instruction in the hash table. */
7867 *s++ = '\0';
7868 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7869 {
7870 insn_error = "unrecognized opcode";
7871 return;
7872 }
7873 }
7874
7875 argsStart = s;
7876 for (;;)
7877 {
7878 bfd_boolean ok;
7879
7880 assert (strcmp (insn->name, str) == 0);
7881
7882 if (OPCODE_IS_MEMBER (insn,
7883 (mips_opts.isa
7884 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7885 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7886 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7887 mips_opts.arch))
7888 ok = TRUE;
7889 else
7890 ok = FALSE;
7891
7892 if (insn->pinfo != INSN_MACRO)
7893 {
7894 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7895 ok = FALSE;
7896 }
7897
7898 if (! ok)
7899 {
7900 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7901 && strcmp (insn->name, insn[1].name) == 0)
7902 {
7903 ++insn;
7904 continue;
7905 }
7906 else
7907 {
7908 if (!insn_error)
7909 {
7910 static char buf[100];
7911 sprintf (buf,
7912 _("opcode not supported on this processor: %s (%s)"),
7913 mips_cpu_info_from_arch (mips_opts.arch)->name,
7914 mips_cpu_info_from_isa (mips_opts.isa)->name);
7915 insn_error = buf;
7916 }
7917 if (save_c)
7918 *(--s) = save_c;
7919 return;
7920 }
7921 }
7922
7923 create_insn (ip, insn);
7924 insn_error = NULL;
7925 for (args = insn->args;; ++args)
7926 {
7927 int is_mdmx;
7928
7929 s += strspn (s, " \t");
7930 is_mdmx = 0;
7931 switch (*args)
7932 {
7933 case '\0': /* end of args */
7934 if (*s == '\0')
7935 return;
7936 break;
7937
7938 case ',':
7939 if (*s++ == *args)
7940 continue;
7941 s--;
7942 switch (*++args)
7943 {
7944 case 'r':
7945 case 'v':
7946 INSERT_OPERAND (RS, *ip, lastregno);
7947 continue;
7948
7949 case 'w':
7950 INSERT_OPERAND (RT, *ip, lastregno);
7951 continue;
7952
7953 case 'W':
7954 INSERT_OPERAND (FT, *ip, lastregno);
7955 continue;
7956
7957 case 'V':
7958 INSERT_OPERAND (FS, *ip, lastregno);
7959 continue;
7960 }
7961 break;
7962
7963 case '(':
7964 /* Handle optional base register.
7965 Either the base register is omitted or
7966 we must have a left paren. */
7967 /* This is dependent on the next operand specifier
7968 is a base register specification. */
7969 assert (args[1] == 'b' || args[1] == '5'
7970 || args[1] == '-' || args[1] == '4');
7971 if (*s == '\0')
7972 return;
7973
7974 case ')': /* these must match exactly */
7975 case '[':
7976 case ']':
7977 if (*s++ == *args)
7978 continue;
7979 break;
7980
7981 case '+': /* Opcode extension character. */
7982 switch (*++args)
7983 {
7984 case 'A': /* ins/ext position, becomes LSB. */
7985 limlo = 0;
7986 limhi = 31;
7987 goto do_lsb;
7988 case 'E':
7989 limlo = 32;
7990 limhi = 63;
7991 goto do_lsb;
7992 do_lsb:
7993 my_getExpression (&imm_expr, s);
7994 check_absolute_expr (ip, &imm_expr);
7995 if ((unsigned long) imm_expr.X_add_number < limlo
7996 || (unsigned long) imm_expr.X_add_number > limhi)
7997 {
7998 as_bad (_("Improper position (%lu)"),
7999 (unsigned long) imm_expr.X_add_number);
8000 imm_expr.X_add_number = limlo;
8001 }
8002 lastpos = imm_expr.X_add_number;
8003 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8004 imm_expr.X_op = O_absent;
8005 s = expr_end;
8006 continue;
8007
8008 case 'B': /* ins size, becomes MSB. */
8009 limlo = 1;
8010 limhi = 32;
8011 goto do_msb;
8012 case 'F':
8013 limlo = 33;
8014 limhi = 64;
8015 goto do_msb;
8016 do_msb:
8017 my_getExpression (&imm_expr, s);
8018 check_absolute_expr (ip, &imm_expr);
8019 /* Check for negative input so that small negative numbers
8020 will not succeed incorrectly. The checks against
8021 (pos+size) transitively check "size" itself,
8022 assuming that "pos" is reasonable. */
8023 if ((long) imm_expr.X_add_number < 0
8024 || ((unsigned long) imm_expr.X_add_number
8025 + lastpos) < limlo
8026 || ((unsigned long) imm_expr.X_add_number
8027 + lastpos) > limhi)
8028 {
8029 as_bad (_("Improper insert size (%lu, position %lu)"),
8030 (unsigned long) imm_expr.X_add_number,
8031 (unsigned long) lastpos);
8032 imm_expr.X_add_number = limlo - lastpos;
8033 }
8034 INSERT_OPERAND (INSMSB, *ip,
8035 lastpos + imm_expr.X_add_number - 1);
8036 imm_expr.X_op = O_absent;
8037 s = expr_end;
8038 continue;
8039
8040 case 'C': /* ext size, becomes MSBD. */
8041 limlo = 1;
8042 limhi = 32;
8043 goto do_msbd;
8044 case 'G':
8045 limlo = 33;
8046 limhi = 64;
8047 goto do_msbd;
8048 case 'H':
8049 limlo = 33;
8050 limhi = 64;
8051 goto do_msbd;
8052 do_msbd:
8053 my_getExpression (&imm_expr, s);
8054 check_absolute_expr (ip, &imm_expr);
8055 /* Check for negative input so that small negative numbers
8056 will not succeed incorrectly. The checks against
8057 (pos+size) transitively check "size" itself,
8058 assuming that "pos" is reasonable. */
8059 if ((long) imm_expr.X_add_number < 0
8060 || ((unsigned long) imm_expr.X_add_number
8061 + lastpos) < limlo
8062 || ((unsigned long) imm_expr.X_add_number
8063 + lastpos) > limhi)
8064 {
8065 as_bad (_("Improper extract size (%lu, position %lu)"),
8066 (unsigned long) imm_expr.X_add_number,
8067 (unsigned long) lastpos);
8068 imm_expr.X_add_number = limlo - lastpos;
8069 }
8070 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8071 imm_expr.X_op = O_absent;
8072 s = expr_end;
8073 continue;
8074
8075 case 'D':
8076 /* +D is for disassembly only; never match. */
8077 break;
8078
8079 case 'I':
8080 /* "+I" is like "I", except that imm2_expr is used. */
8081 my_getExpression (&imm2_expr, s);
8082 if (imm2_expr.X_op != O_big
8083 && imm2_expr.X_op != O_constant)
8084 insn_error = _("absolute expression required");
8085 normalize_constant_expr (&imm2_expr);
8086 s = expr_end;
8087 continue;
8088
8089 default:
8090 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8091 *args, insn->name, insn->args);
8092 /* Further processing is fruitless. */
8093 return;
8094 }
8095 break;
8096
8097 case '<': /* must be at least one digit */
8098 /*
8099 * According to the manual, if the shift amount is greater
8100 * than 31 or less than 0, then the shift amount should be
8101 * mod 32. In reality the mips assembler issues an error.
8102 * We issue a warning and mask out all but the low 5 bits.
8103 */
8104 my_getExpression (&imm_expr, s);
8105 check_absolute_expr (ip, &imm_expr);
8106 if ((unsigned long) imm_expr.X_add_number > 31)
8107 as_warn (_("Improper shift amount (%lu)"),
8108 (unsigned long) imm_expr.X_add_number);
8109 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8110 imm_expr.X_op = O_absent;
8111 s = expr_end;
8112 continue;
8113
8114 case '>': /* shift amount minus 32 */
8115 my_getExpression (&imm_expr, s);
8116 check_absolute_expr (ip, &imm_expr);
8117 if ((unsigned long) imm_expr.X_add_number < 32
8118 || (unsigned long) imm_expr.X_add_number > 63)
8119 break;
8120 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8121 imm_expr.X_op = O_absent;
8122 s = expr_end;
8123 continue;
8124
8125 case 'k': /* cache code */
8126 case 'h': /* prefx code */
8127 my_getExpression (&imm_expr, s);
8128 check_absolute_expr (ip, &imm_expr);
8129 if ((unsigned long) imm_expr.X_add_number > 31)
8130 as_warn (_("Invalid value for `%s' (%lu)"),
8131 ip->insn_mo->name,
8132 (unsigned long) imm_expr.X_add_number);
8133 if (*args == 'k')
8134 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8135 else
8136 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8137 imm_expr.X_op = O_absent;
8138 s = expr_end;
8139 continue;
8140
8141 case 'c': /* break code */
8142 my_getExpression (&imm_expr, s);
8143 check_absolute_expr (ip, &imm_expr);
8144 if ((unsigned long) imm_expr.X_add_number > 1023)
8145 as_warn (_("Illegal break code (%lu)"),
8146 (unsigned long) imm_expr.X_add_number);
8147 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8148 imm_expr.X_op = O_absent;
8149 s = expr_end;
8150 continue;
8151
8152 case 'q': /* lower break code */
8153 my_getExpression (&imm_expr, s);
8154 check_absolute_expr (ip, &imm_expr);
8155 if ((unsigned long) imm_expr.X_add_number > 1023)
8156 as_warn (_("Illegal lower break code (%lu)"),
8157 (unsigned long) imm_expr.X_add_number);
8158 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8159 imm_expr.X_op = O_absent;
8160 s = expr_end;
8161 continue;
8162
8163 case 'B': /* 20-bit syscall/break code. */
8164 my_getExpression (&imm_expr, s);
8165 check_absolute_expr (ip, &imm_expr);
8166 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8167 as_warn (_("Illegal 20-bit code (%lu)"),
8168 (unsigned long) imm_expr.X_add_number);
8169 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8170 imm_expr.X_op = O_absent;
8171 s = expr_end;
8172 continue;
8173
8174 case 'C': /* Coprocessor code */
8175 my_getExpression (&imm_expr, s);
8176 check_absolute_expr (ip, &imm_expr);
8177 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8178 {
8179 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8180 (unsigned long) imm_expr.X_add_number);
8181 imm_expr.X_add_number &= ((1 << 25) - 1);
8182 }
8183 ip->insn_opcode |= imm_expr.X_add_number;
8184 imm_expr.X_op = O_absent;
8185 s = expr_end;
8186 continue;
8187
8188 case 'J': /* 19-bit wait code. */
8189 my_getExpression (&imm_expr, s);
8190 check_absolute_expr (ip, &imm_expr);
8191 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8192 as_warn (_("Illegal 19-bit code (%lu)"),
8193 (unsigned long) imm_expr.X_add_number);
8194 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8195 imm_expr.X_op = O_absent;
8196 s = expr_end;
8197 continue;
8198
8199 case 'P': /* Performance register */
8200 my_getExpression (&imm_expr, s);
8201 check_absolute_expr (ip, &imm_expr);
8202 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8203 as_warn (_("Invalid performance register (%lu)"),
8204 (unsigned long) imm_expr.X_add_number);
8205 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8206 imm_expr.X_op = O_absent;
8207 s = expr_end;
8208 continue;
8209
8210 case 'b': /* base register */
8211 case 'd': /* destination register */
8212 case 's': /* source register */
8213 case 't': /* target register */
8214 case 'r': /* both target and source */
8215 case 'v': /* both dest and source */
8216 case 'w': /* both dest and target */
8217 case 'E': /* coprocessor target register */
8218 case 'G': /* coprocessor destination register */
8219 case 'K': /* 'rdhwr' destination register */
8220 case 'x': /* ignore register name */
8221 case 'z': /* must be zero register */
8222 case 'U': /* destination register (clo/clz). */
8223 s_reset = s;
8224 if (s[0] == '$')
8225 {
8226
8227 if (ISDIGIT (s[1]))
8228 {
8229 ++s;
8230 regno = 0;
8231 do
8232 {
8233 regno *= 10;
8234 regno += *s - '0';
8235 ++s;
8236 }
8237 while (ISDIGIT (*s));
8238 if (regno > 31)
8239 as_bad (_("Invalid register number (%d)"), regno);
8240 }
8241 else if (*args == 'E' || *args == 'G' || *args == 'K')
8242 goto notreg;
8243 else
8244 {
8245 if (s[1] == 'r' && s[2] == 'a')
8246 {
8247 s += 3;
8248 regno = RA;
8249 }
8250 else if (s[1] == 'f' && s[2] == 'p')
8251 {
8252 s += 3;
8253 regno = FP;
8254 }
8255 else if (s[1] == 's' && s[2] == 'p')
8256 {
8257 s += 3;
8258 regno = SP;
8259 }
8260 else if (s[1] == 'g' && s[2] == 'p')
8261 {
8262 s += 3;
8263 regno = GP;
8264 }
8265 else if (s[1] == 'a' && s[2] == 't')
8266 {
8267 s += 3;
8268 regno = AT;
8269 }
8270 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8271 {
8272 s += 4;
8273 regno = KT0;
8274 }
8275 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8276 {
8277 s += 4;
8278 regno = KT1;
8279 }
8280 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8281 {
8282 s += 5;
8283 regno = ZERO;
8284 }
8285 else if (itbl_have_entries)
8286 {
8287 char *p, *n;
8288 unsigned long r;
8289
8290 p = s + 1; /* advance past '$' */
8291 n = itbl_get_field (&p); /* n is name */
8292
8293 /* See if this is a register defined in an
8294 itbl entry. */
8295 if (itbl_get_reg_val (n, &r))
8296 {
8297 /* Get_field advances to the start of
8298 the next field, so we need to back
8299 rack to the end of the last field. */
8300 if (p)
8301 s = p - 1;
8302 else
8303 s = strchr (s, '\0');
8304 regno = r;
8305 }
8306 else
8307 goto notreg;
8308 }
8309 else
8310 goto notreg;
8311 }
8312 if (regno == AT
8313 && ! mips_opts.noat
8314 && *args != 'E'
8315 && *args != 'G'
8316 && *args != 'K')
8317 as_warn (_("Used $at without \".set noat\""));
8318 c = *args;
8319 if (*s == ' ')
8320 ++s;
8321 if (args[1] != *s)
8322 {
8323 if (c == 'r' || c == 'v' || c == 'w')
8324 {
8325 regno = lastregno;
8326 s = s_reset;
8327 ++args;
8328 }
8329 }
8330 /* 'z' only matches $0. */
8331 if (c == 'z' && regno != 0)
8332 break;
8333
8334 /* Now that we have assembled one operand, we use the args string
8335 * to figure out where it goes in the instruction. */
8336 switch (c)
8337 {
8338 case 'r':
8339 case 's':
8340 case 'v':
8341 case 'b':
8342 INSERT_OPERAND (RS, *ip, regno);
8343 break;
8344 case 'd':
8345 case 'G':
8346 case 'K':
8347 INSERT_OPERAND (RD, *ip, regno);
8348 break;
8349 case 'U':
8350 INSERT_OPERAND (RD, *ip, regno);
8351 INSERT_OPERAND (RT, *ip, regno);
8352 break;
8353 case 'w':
8354 case 't':
8355 case 'E':
8356 INSERT_OPERAND (RT, *ip, regno);
8357 break;
8358 case 'x':
8359 /* This case exists because on the r3000 trunc
8360 expands into a macro which requires a gp
8361 register. On the r6000 or r4000 it is
8362 assembled into a single instruction which
8363 ignores the register. Thus the insn version
8364 is MIPS_ISA2 and uses 'x', and the macro
8365 version is MIPS_ISA1 and uses 't'. */
8366 break;
8367 case 'z':
8368 /* This case is for the div instruction, which
8369 acts differently if the destination argument
8370 is $0. This only matches $0, and is checked
8371 outside the switch. */
8372 break;
8373 case 'D':
8374 /* Itbl operand; not yet implemented. FIXME ?? */
8375 break;
8376 /* What about all other operands like 'i', which
8377 can be specified in the opcode table? */
8378 }
8379 lastregno = regno;
8380 continue;
8381 }
8382 notreg:
8383 switch (*args++)
8384 {
8385 case 'r':
8386 case 'v':
8387 INSERT_OPERAND (RS, *ip, lastregno);
8388 continue;
8389 case 'w':
8390 INSERT_OPERAND (RT, *ip, lastregno);
8391 continue;
8392 }
8393 break;
8394
8395 case 'O': /* MDMX alignment immediate constant. */
8396 my_getExpression (&imm_expr, s);
8397 check_absolute_expr (ip, &imm_expr);
8398 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8399 as_warn ("Improper align amount (%ld), using low bits",
8400 (long) imm_expr.X_add_number);
8401 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
8402 imm_expr.X_op = O_absent;
8403 s = expr_end;
8404 continue;
8405
8406 case 'Q': /* MDMX vector, element sel, or const. */
8407 if (s[0] != '$')
8408 {
8409 /* MDMX Immediate. */
8410 my_getExpression (&imm_expr, s);
8411 check_absolute_expr (ip, &imm_expr);
8412 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8413 as_warn (_("Invalid MDMX Immediate (%ld)"),
8414 (long) imm_expr.X_add_number);
8415 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
8416 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8417 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8418 else
8419 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8420 imm_expr.X_op = O_absent;
8421 s = expr_end;
8422 continue;
8423 }
8424 /* Not MDMX Immediate. Fall through. */
8425 case 'X': /* MDMX destination register. */
8426 case 'Y': /* MDMX source register. */
8427 case 'Z': /* MDMX target register. */
8428 is_mdmx = 1;
8429 case 'D': /* floating point destination register */
8430 case 'S': /* floating point source register */
8431 case 'T': /* floating point target register */
8432 case 'R': /* floating point source register */
8433 case 'V':
8434 case 'W':
8435 s_reset = s;
8436 /* Accept $fN for FP and MDMX register numbers, and in
8437 addition accept $vN for MDMX register numbers. */
8438 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8439 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8440 && ISDIGIT (s[2])))
8441 {
8442 s += 2;
8443 regno = 0;
8444 do
8445 {
8446 regno *= 10;
8447 regno += *s - '0';
8448 ++s;
8449 }
8450 while (ISDIGIT (*s));
8451
8452 if (regno > 31)
8453 as_bad (_("Invalid float register number (%d)"), regno);
8454
8455 if ((regno & 1) != 0
8456 && HAVE_32BIT_FPRS
8457 && ! (strcmp (str, "mtc1") == 0
8458 || strcmp (str, "mfc1") == 0
8459 || strcmp (str, "lwc1") == 0
8460 || strcmp (str, "swc1") == 0
8461 || strcmp (str, "l.s") == 0
8462 || strcmp (str, "s.s") == 0))
8463 as_warn (_("Float register should be even, was %d"),
8464 regno);
8465
8466 c = *args;
8467 if (*s == ' ')
8468 ++s;
8469 if (args[1] != *s)
8470 {
8471 if (c == 'V' || c == 'W')
8472 {
8473 regno = lastregno;
8474 s = s_reset;
8475 ++args;
8476 }
8477 }
8478 switch (c)
8479 {
8480 case 'D':
8481 case 'X':
8482 INSERT_OPERAND (FD, *ip, regno);
8483 break;
8484 case 'V':
8485 case 'S':
8486 case 'Y':
8487 INSERT_OPERAND (FS, *ip, regno);
8488 break;
8489 case 'Q':
8490 /* This is like 'Z', but also needs to fix the MDMX
8491 vector/scalar select bits. Note that the
8492 scalar immediate case is handled above. */
8493 if (*s == '[')
8494 {
8495 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8496 int max_el = (is_qh ? 3 : 7);
8497 s++;
8498 my_getExpression(&imm_expr, s);
8499 check_absolute_expr (ip, &imm_expr);
8500 s = expr_end;
8501 if (imm_expr.X_add_number > max_el)
8502 as_bad(_("Bad element selector %ld"),
8503 (long) imm_expr.X_add_number);
8504 imm_expr.X_add_number &= max_el;
8505 ip->insn_opcode |= (imm_expr.X_add_number
8506 << (OP_SH_VSEL +
8507 (is_qh ? 2 : 1)));
8508 imm_expr.X_op = O_absent;
8509 if (*s != ']')
8510 as_warn(_("Expecting ']' found '%s'"), s);
8511 else
8512 s++;
8513 }
8514 else
8515 {
8516 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8517 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8518 << OP_SH_VSEL);
8519 else
8520 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8521 OP_SH_VSEL);
8522 }
8523 /* Fall through */
8524 case 'W':
8525 case 'T':
8526 case 'Z':
8527 INSERT_OPERAND (FT, *ip, regno);
8528 break;
8529 case 'R':
8530 INSERT_OPERAND (FR, *ip, regno);
8531 break;
8532 }
8533 lastregno = regno;
8534 continue;
8535 }
8536
8537 switch (*args++)
8538 {
8539 case 'V':
8540 INSERT_OPERAND (FS, *ip, lastregno);
8541 continue;
8542 case 'W':
8543 INSERT_OPERAND (FT, *ip, lastregno);
8544 continue;
8545 }
8546 break;
8547
8548 case 'I':
8549 my_getExpression (&imm_expr, s);
8550 if (imm_expr.X_op != O_big
8551 && imm_expr.X_op != O_constant)
8552 insn_error = _("absolute expression required");
8553 normalize_constant_expr (&imm_expr);
8554 s = expr_end;
8555 continue;
8556
8557 case 'A':
8558 my_getExpression (&offset_expr, s);
8559 *imm_reloc = BFD_RELOC_32;
8560 s = expr_end;
8561 continue;
8562
8563 case 'F':
8564 case 'L':
8565 case 'f':
8566 case 'l':
8567 {
8568 int f64;
8569 int using_gprs;
8570 char *save_in;
8571 char *err;
8572 unsigned char temp[8];
8573 int len;
8574 unsigned int length;
8575 segT seg;
8576 subsegT subseg;
8577 char *p;
8578
8579 /* These only appear as the last operand in an
8580 instruction, and every instruction that accepts
8581 them in any variant accepts them in all variants.
8582 This means we don't have to worry about backing out
8583 any changes if the instruction does not match.
8584
8585 The difference between them is the size of the
8586 floating point constant and where it goes. For 'F'
8587 and 'L' the constant is 64 bits; for 'f' and 'l' it
8588 is 32 bits. Where the constant is placed is based
8589 on how the MIPS assembler does things:
8590 F -- .rdata
8591 L -- .lit8
8592 f -- immediate value
8593 l -- .lit4
8594
8595 The .lit4 and .lit8 sections are only used if
8596 permitted by the -G argument.
8597
8598 The code below needs to know whether the target register
8599 is 32 or 64 bits wide. It relies on the fact 'f' and
8600 'F' are used with GPR-based instructions and 'l' and
8601 'L' are used with FPR-based instructions. */
8602
8603 f64 = *args == 'F' || *args == 'L';
8604 using_gprs = *args == 'F' || *args == 'f';
8605
8606 save_in = input_line_pointer;
8607 input_line_pointer = s;
8608 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8609 length = len;
8610 s = input_line_pointer;
8611 input_line_pointer = save_in;
8612 if (err != NULL && *err != '\0')
8613 {
8614 as_bad (_("Bad floating point constant: %s"), err);
8615 memset (temp, '\0', sizeof temp);
8616 length = f64 ? 8 : 4;
8617 }
8618
8619 assert (length == (unsigned) (f64 ? 8 : 4));
8620
8621 if (*args == 'f'
8622 || (*args == 'l'
8623 && (g_switch_value < 4
8624 || (temp[0] == 0 && temp[1] == 0)
8625 || (temp[2] == 0 && temp[3] == 0))))
8626 {
8627 imm_expr.X_op = O_constant;
8628 if (! target_big_endian)
8629 imm_expr.X_add_number = bfd_getl32 (temp);
8630 else
8631 imm_expr.X_add_number = bfd_getb32 (temp);
8632 }
8633 else if (length > 4
8634 && ! mips_disable_float_construction
8635 /* Constants can only be constructed in GPRs and
8636 copied to FPRs if the GPRs are at least as wide
8637 as the FPRs. Force the constant into memory if
8638 we are using 64-bit FPRs but the GPRs are only
8639 32 bits wide. */
8640 && (using_gprs
8641 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8642 && ((temp[0] == 0 && temp[1] == 0)
8643 || (temp[2] == 0 && temp[3] == 0))
8644 && ((temp[4] == 0 && temp[5] == 0)
8645 || (temp[6] == 0 && temp[7] == 0)))
8646 {
8647 /* The value is simple enough to load with a couple of
8648 instructions. If using 32-bit registers, set
8649 imm_expr to the high order 32 bits and offset_expr to
8650 the low order 32 bits. Otherwise, set imm_expr to
8651 the entire 64 bit constant. */
8652 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8653 {
8654 imm_expr.X_op = O_constant;
8655 offset_expr.X_op = O_constant;
8656 if (! target_big_endian)
8657 {
8658 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8659 offset_expr.X_add_number = bfd_getl32 (temp);
8660 }
8661 else
8662 {
8663 imm_expr.X_add_number = bfd_getb32 (temp);
8664 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8665 }
8666 if (offset_expr.X_add_number == 0)
8667 offset_expr.X_op = O_absent;
8668 }
8669 else if (sizeof (imm_expr.X_add_number) > 4)
8670 {
8671 imm_expr.X_op = O_constant;
8672 if (! target_big_endian)
8673 imm_expr.X_add_number = bfd_getl64 (temp);
8674 else
8675 imm_expr.X_add_number = bfd_getb64 (temp);
8676 }
8677 else
8678 {
8679 imm_expr.X_op = O_big;
8680 imm_expr.X_add_number = 4;
8681 if (! target_big_endian)
8682 {
8683 generic_bignum[0] = bfd_getl16 (temp);
8684 generic_bignum[1] = bfd_getl16 (temp + 2);
8685 generic_bignum[2] = bfd_getl16 (temp + 4);
8686 generic_bignum[3] = bfd_getl16 (temp + 6);
8687 }
8688 else
8689 {
8690 generic_bignum[0] = bfd_getb16 (temp + 6);
8691 generic_bignum[1] = bfd_getb16 (temp + 4);
8692 generic_bignum[2] = bfd_getb16 (temp + 2);
8693 generic_bignum[3] = bfd_getb16 (temp);
8694 }
8695 }
8696 }
8697 else
8698 {
8699 const char *newname;
8700 segT new_seg;
8701
8702 /* Switch to the right section. */
8703 seg = now_seg;
8704 subseg = now_subseg;
8705 switch (*args)
8706 {
8707 default: /* unused default case avoids warnings. */
8708 case 'L':
8709 newname = RDATA_SECTION_NAME;
8710 if (g_switch_value >= 8)
8711 newname = ".lit8";
8712 break;
8713 case 'F':
8714 newname = RDATA_SECTION_NAME;
8715 break;
8716 case 'l':
8717 assert (g_switch_value >= 4);
8718 newname = ".lit4";
8719 break;
8720 }
8721 new_seg = subseg_new (newname, (subsegT) 0);
8722 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8723 bfd_set_section_flags (stdoutput, new_seg,
8724 (SEC_ALLOC
8725 | SEC_LOAD
8726 | SEC_READONLY
8727 | SEC_DATA));
8728 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8729 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8730 && strcmp (TARGET_OS, "elf") != 0)
8731 record_alignment (new_seg, 4);
8732 else
8733 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8734 if (seg == now_seg)
8735 as_bad (_("Can't use floating point insn in this section"));
8736
8737 /* Set the argument to the current address in the
8738 section. */
8739 offset_expr.X_op = O_symbol;
8740 offset_expr.X_add_symbol =
8741 symbol_new ("L0\001", now_seg,
8742 (valueT) frag_now_fix (), frag_now);
8743 offset_expr.X_add_number = 0;
8744
8745 /* Put the floating point number into the section. */
8746 p = frag_more ((int) length);
8747 memcpy (p, temp, length);
8748
8749 /* Switch back to the original section. */
8750 subseg_set (seg, subseg);
8751 }
8752 }
8753 continue;
8754
8755 case 'i': /* 16 bit unsigned immediate */
8756 case 'j': /* 16 bit signed immediate */
8757 *imm_reloc = BFD_RELOC_LO16;
8758 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8759 {
8760 int more;
8761 offsetT minval, maxval;
8762
8763 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8764 && strcmp (insn->name, insn[1].name) == 0);
8765
8766 /* If the expression was written as an unsigned number,
8767 only treat it as signed if there are no more
8768 alternatives. */
8769 if (more
8770 && *args == 'j'
8771 && sizeof (imm_expr.X_add_number) <= 4
8772 && imm_expr.X_op == O_constant
8773 && imm_expr.X_add_number < 0
8774 && imm_expr.X_unsigned
8775 && HAVE_64BIT_GPRS)
8776 break;
8777
8778 /* For compatibility with older assemblers, we accept
8779 0x8000-0xffff as signed 16-bit numbers when only
8780 signed numbers are allowed. */
8781 if (*args == 'i')
8782 minval = 0, maxval = 0xffff;
8783 else if (more)
8784 minval = -0x8000, maxval = 0x7fff;
8785 else
8786 minval = -0x8000, maxval = 0xffff;
8787
8788 if (imm_expr.X_op != O_constant
8789 || imm_expr.X_add_number < minval
8790 || imm_expr.X_add_number > maxval)
8791 {
8792 if (more)
8793 break;
8794 if (imm_expr.X_op == O_constant
8795 || imm_expr.X_op == O_big)
8796 as_bad (_("expression out of range"));
8797 }
8798 }
8799 s = expr_end;
8800 continue;
8801
8802 case 'o': /* 16 bit offset */
8803 /* Check whether there is only a single bracketed expression
8804 left. If so, it must be the base register and the
8805 constant must be zero. */
8806 if (*s == '(' && strchr (s + 1, '(') == 0)
8807 {
8808 offset_expr.X_op = O_constant;
8809 offset_expr.X_add_number = 0;
8810 continue;
8811 }
8812
8813 /* If this value won't fit into a 16 bit offset, then go
8814 find a macro that will generate the 32 bit offset
8815 code pattern. */
8816 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8817 && (offset_expr.X_op != O_constant
8818 || offset_expr.X_add_number >= 0x8000
8819 || offset_expr.X_add_number < -0x8000))
8820 break;
8821
8822 s = expr_end;
8823 continue;
8824
8825 case 'p': /* pc relative offset */
8826 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8827 my_getExpression (&offset_expr, s);
8828 s = expr_end;
8829 continue;
8830
8831 case 'u': /* upper 16 bits */
8832 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8833 && imm_expr.X_op == O_constant
8834 && (imm_expr.X_add_number < 0
8835 || imm_expr.X_add_number >= 0x10000))
8836 as_bad (_("lui expression not in range 0..65535"));
8837 s = expr_end;
8838 continue;
8839
8840 case 'a': /* 26 bit address */
8841 my_getExpression (&offset_expr, s);
8842 s = expr_end;
8843 *offset_reloc = BFD_RELOC_MIPS_JMP;
8844 continue;
8845
8846 case 'N': /* 3 bit branch condition code */
8847 case 'M': /* 3 bit compare condition code */
8848 if (strncmp (s, "$fcc", 4) != 0)
8849 break;
8850 s += 4;
8851 regno = 0;
8852 do
8853 {
8854 regno *= 10;
8855 regno += *s - '0';
8856 ++s;
8857 }
8858 while (ISDIGIT (*s));
8859 if (regno > 7)
8860 as_bad (_("Invalid condition code register $fcc%d"), regno);
8861 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8862 || strcmp(str + strlen(str) - 5, "any2f") == 0
8863 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8864 && (regno & 1) != 0)
8865 as_warn(_("Condition code register should be even for %s, was %d"),
8866 str, regno);
8867 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8868 || strcmp(str + strlen(str) - 5, "any4t") == 0)
8869 && (regno & 3) != 0)
8870 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
8871 str, regno);
8872 if (*args == 'N')
8873 INSERT_OPERAND (BCC, *ip, regno);
8874 else
8875 INSERT_OPERAND (CCC, *ip, regno);
8876 continue;
8877
8878 case 'H':
8879 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8880 s += 2;
8881 if (ISDIGIT (*s))
8882 {
8883 c = 0;
8884 do
8885 {
8886 c *= 10;
8887 c += *s - '0';
8888 ++s;
8889 }
8890 while (ISDIGIT (*s));
8891 }
8892 else
8893 c = 8; /* Invalid sel value. */
8894
8895 if (c > 7)
8896 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8897 ip->insn_opcode |= c;
8898 continue;
8899
8900 case 'e':
8901 /* Must be at least one digit. */
8902 my_getExpression (&imm_expr, s);
8903 check_absolute_expr (ip, &imm_expr);
8904
8905 if ((unsigned long) imm_expr.X_add_number
8906 > (unsigned long) OP_MASK_VECBYTE)
8907 {
8908 as_bad (_("bad byte vector index (%ld)"),
8909 (long) imm_expr.X_add_number);
8910 imm_expr.X_add_number = 0;
8911 }
8912
8913 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
8914 imm_expr.X_op = O_absent;
8915 s = expr_end;
8916 continue;
8917
8918 case '%':
8919 my_getExpression (&imm_expr, s);
8920 check_absolute_expr (ip, &imm_expr);
8921
8922 if ((unsigned long) imm_expr.X_add_number
8923 > (unsigned long) OP_MASK_VECALIGN)
8924 {
8925 as_bad (_("bad byte vector index (%ld)"),
8926 (long) imm_expr.X_add_number);
8927 imm_expr.X_add_number = 0;
8928 }
8929
8930 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
8931 imm_expr.X_op = O_absent;
8932 s = expr_end;
8933 continue;
8934
8935 default:
8936 as_bad (_("bad char = '%c'\n"), *args);
8937 internalError ();
8938 }
8939 break;
8940 }
8941 /* Args don't match. */
8942 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8943 !strcmp (insn->name, insn[1].name))
8944 {
8945 ++insn;
8946 s = argsStart;
8947 insn_error = _("illegal operands");
8948 continue;
8949 }
8950 if (save_c)
8951 *(--s) = save_c;
8952 insn_error = _("illegal operands");
8953 return;
8954 }
8955 }
8956
8957 /* This routine assembles an instruction into its binary format when
8958 assembling for the mips16. As a side effect, it sets one of the
8959 global variables imm_reloc or offset_reloc to the type of
8960 relocation to do if one of the operands is an address expression.
8961 It also sets mips16_small and mips16_ext if the user explicitly
8962 requested a small or extended instruction. */
8963
8964 static void
8965 mips16_ip (char *str, struct mips_cl_insn *ip)
8966 {
8967 char *s;
8968 const char *args;
8969 struct mips_opcode *insn;
8970 char *argsstart;
8971 unsigned int regno;
8972 unsigned int lastregno = 0;
8973 char *s_reset;
8974 size_t i;
8975
8976 insn_error = NULL;
8977
8978 mips16_small = FALSE;
8979 mips16_ext = FALSE;
8980
8981 for (s = str; ISLOWER (*s); ++s)
8982 ;
8983 switch (*s)
8984 {
8985 case '\0':
8986 break;
8987
8988 case ' ':
8989 *s++ = '\0';
8990 break;
8991
8992 case '.':
8993 if (s[1] == 't' && s[2] == ' ')
8994 {
8995 *s = '\0';
8996 mips16_small = TRUE;
8997 s += 3;
8998 break;
8999 }
9000 else if (s[1] == 'e' && s[2] == ' ')
9001 {
9002 *s = '\0';
9003 mips16_ext = TRUE;
9004 s += 3;
9005 break;
9006 }
9007 /* Fall through. */
9008 default:
9009 insn_error = _("unknown opcode");
9010 return;
9011 }
9012
9013 if (mips_opts.noautoextend && ! mips16_ext)
9014 mips16_small = TRUE;
9015
9016 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9017 {
9018 insn_error = _("unrecognized opcode");
9019 return;
9020 }
9021
9022 argsstart = s;
9023 for (;;)
9024 {
9025 assert (strcmp (insn->name, str) == 0);
9026
9027 create_insn (ip, insn);
9028 imm_expr.X_op = O_absent;
9029 imm_reloc[0] = BFD_RELOC_UNUSED;
9030 imm_reloc[1] = BFD_RELOC_UNUSED;
9031 imm_reloc[2] = BFD_RELOC_UNUSED;
9032 imm2_expr.X_op = O_absent;
9033 offset_expr.X_op = O_absent;
9034 offset_reloc[0] = BFD_RELOC_UNUSED;
9035 offset_reloc[1] = BFD_RELOC_UNUSED;
9036 offset_reloc[2] = BFD_RELOC_UNUSED;
9037 for (args = insn->args; 1; ++args)
9038 {
9039 int c;
9040
9041 if (*s == ' ')
9042 ++s;
9043
9044 /* In this switch statement we call break if we did not find
9045 a match, continue if we did find a match, or return if we
9046 are done. */
9047
9048 c = *args;
9049 switch (c)
9050 {
9051 case '\0':
9052 if (*s == '\0')
9053 {
9054 /* Stuff the immediate value in now, if we can. */
9055 if (imm_expr.X_op == O_constant
9056 && *imm_reloc > BFD_RELOC_UNUSED
9057 && insn->pinfo != INSN_MACRO)
9058 {
9059 valueT tmp;
9060
9061 switch (*offset_reloc)
9062 {
9063 case BFD_RELOC_MIPS16_HI16_S:
9064 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9065 break;
9066
9067 case BFD_RELOC_MIPS16_HI16:
9068 tmp = imm_expr.X_add_number >> 16;
9069 break;
9070
9071 case BFD_RELOC_MIPS16_LO16:
9072 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9073 - 0x8000;
9074 break;
9075
9076 case BFD_RELOC_UNUSED:
9077 tmp = imm_expr.X_add_number;
9078 break;
9079
9080 default:
9081 internalError ();
9082 }
9083 *offset_reloc = BFD_RELOC_UNUSED;
9084
9085 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9086 tmp, TRUE, mips16_small,
9087 mips16_ext, &ip->insn_opcode,
9088 &ip->use_extend, &ip->extend);
9089 imm_expr.X_op = O_absent;
9090 *imm_reloc = BFD_RELOC_UNUSED;
9091 }
9092
9093 return;
9094 }
9095 break;
9096
9097 case ',':
9098 if (*s++ == c)
9099 continue;
9100 s--;
9101 switch (*++args)
9102 {
9103 case 'v':
9104 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9105 continue;
9106 case 'w':
9107 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9108 continue;
9109 }
9110 break;
9111
9112 case '(':
9113 case ')':
9114 if (*s++ == c)
9115 continue;
9116 break;
9117
9118 case 'v':
9119 case 'w':
9120 if (s[0] != '$')
9121 {
9122 if (c == 'v')
9123 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9124 else
9125 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9126 ++args;
9127 continue;
9128 }
9129 /* Fall through. */
9130 case 'x':
9131 case 'y':
9132 case 'z':
9133 case 'Z':
9134 case '0':
9135 case 'S':
9136 case 'R':
9137 case 'X':
9138 case 'Y':
9139 if (s[0] != '$')
9140 break;
9141 s_reset = s;
9142 if (ISDIGIT (s[1]))
9143 {
9144 ++s;
9145 regno = 0;
9146 do
9147 {
9148 regno *= 10;
9149 regno += *s - '0';
9150 ++s;
9151 }
9152 while (ISDIGIT (*s));
9153 if (regno > 31)
9154 {
9155 as_bad (_("invalid register number (%d)"), regno);
9156 regno = 2;
9157 }
9158 }
9159 else
9160 {
9161 if (s[1] == 'r' && s[2] == 'a')
9162 {
9163 s += 3;
9164 regno = RA;
9165 }
9166 else if (s[1] == 'f' && s[2] == 'p')
9167 {
9168 s += 3;
9169 regno = FP;
9170 }
9171 else if (s[1] == 's' && s[2] == 'p')
9172 {
9173 s += 3;
9174 regno = SP;
9175 }
9176 else if (s[1] == 'g' && s[2] == 'p')
9177 {
9178 s += 3;
9179 regno = GP;
9180 }
9181 else if (s[1] == 'a' && s[2] == 't')
9182 {
9183 s += 3;
9184 regno = AT;
9185 }
9186 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9187 {
9188 s += 4;
9189 regno = KT0;
9190 }
9191 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9192 {
9193 s += 4;
9194 regno = KT1;
9195 }
9196 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9197 {
9198 s += 5;
9199 regno = ZERO;
9200 }
9201 else
9202 break;
9203 }
9204
9205 if (*s == ' ')
9206 ++s;
9207 if (args[1] != *s)
9208 {
9209 if (c == 'v' || c == 'w')
9210 {
9211 regno = mips16_to_32_reg_map[lastregno];
9212 s = s_reset;
9213 ++args;
9214 }
9215 }
9216
9217 switch (c)
9218 {
9219 case 'x':
9220 case 'y':
9221 case 'z':
9222 case 'v':
9223 case 'w':
9224 case 'Z':
9225 regno = mips32_to_16_reg_map[regno];
9226 break;
9227
9228 case '0':
9229 if (regno != 0)
9230 regno = ILLEGAL_REG;
9231 break;
9232
9233 case 'S':
9234 if (regno != SP)
9235 regno = ILLEGAL_REG;
9236 break;
9237
9238 case 'R':
9239 if (regno != RA)
9240 regno = ILLEGAL_REG;
9241 break;
9242
9243 case 'X':
9244 case 'Y':
9245 if (regno == AT && ! mips_opts.noat)
9246 as_warn (_("used $at without \".set noat\""));
9247 break;
9248
9249 default:
9250 internalError ();
9251 }
9252
9253 if (regno == ILLEGAL_REG)
9254 break;
9255
9256 switch (c)
9257 {
9258 case 'x':
9259 case 'v':
9260 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9261 break;
9262 case 'y':
9263 case 'w':
9264 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9265 break;
9266 case 'z':
9267 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9268 break;
9269 case 'Z':
9270 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9271 case '0':
9272 case 'S':
9273 case 'R':
9274 break;
9275 case 'X':
9276 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9277 break;
9278 case 'Y':
9279 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9280 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9281 break;
9282 default:
9283 internalError ();
9284 }
9285
9286 lastregno = regno;
9287 continue;
9288
9289 case 'P':
9290 if (strncmp (s, "$pc", 3) == 0)
9291 {
9292 s += 3;
9293 continue;
9294 }
9295 break;
9296
9297 case '5':
9298 case 'H':
9299 case 'W':
9300 case 'D':
9301 case 'j':
9302 case 'V':
9303 case 'C':
9304 case 'U':
9305 case 'k':
9306 case 'K':
9307 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9308 if (i > 0)
9309 {
9310 if (imm_expr.X_op != O_constant)
9311 {
9312 mips16_ext = TRUE;
9313 ip->use_extend = TRUE;
9314 ip->extend = 0;
9315 }
9316 else
9317 {
9318 /* We need to relax this instruction. */
9319 *offset_reloc = *imm_reloc;
9320 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9321 }
9322 s = expr_end;
9323 continue;
9324 }
9325 *imm_reloc = BFD_RELOC_UNUSED;
9326 /* Fall through. */
9327 case '<':
9328 case '>':
9329 case '[':
9330 case ']':
9331 case '4':
9332 case '8':
9333 my_getExpression (&imm_expr, s);
9334 if (imm_expr.X_op == O_register)
9335 {
9336 /* What we thought was an expression turned out to
9337 be a register. */
9338
9339 if (s[0] == '(' && args[1] == '(')
9340 {
9341 /* It looks like the expression was omitted
9342 before a register indirection, which means
9343 that the expression is implicitly zero. We
9344 still set up imm_expr, so that we handle
9345 explicit extensions correctly. */
9346 imm_expr.X_op = O_constant;
9347 imm_expr.X_add_number = 0;
9348 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9349 continue;
9350 }
9351
9352 break;
9353 }
9354
9355 /* We need to relax this instruction. */
9356 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9357 s = expr_end;
9358 continue;
9359
9360 case 'p':
9361 case 'q':
9362 case 'A':
9363 case 'B':
9364 case 'E':
9365 /* We use offset_reloc rather than imm_reloc for the PC
9366 relative operands. This lets macros with both
9367 immediate and address operands work correctly. */
9368 my_getExpression (&offset_expr, s);
9369
9370 if (offset_expr.X_op == O_register)
9371 break;
9372
9373 /* We need to relax this instruction. */
9374 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9375 s = expr_end;
9376 continue;
9377
9378 case '6': /* break code */
9379 my_getExpression (&imm_expr, s);
9380 check_absolute_expr (ip, &imm_expr);
9381 if ((unsigned long) imm_expr.X_add_number > 63)
9382 as_warn (_("Invalid value for `%s' (%lu)"),
9383 ip->insn_mo->name,
9384 (unsigned long) imm_expr.X_add_number);
9385 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
9386 imm_expr.X_op = O_absent;
9387 s = expr_end;
9388 continue;
9389
9390 case 'a': /* 26 bit address */
9391 my_getExpression (&offset_expr, s);
9392 s = expr_end;
9393 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9394 ip->insn_opcode <<= 16;
9395 continue;
9396
9397 case 'l': /* register list for entry macro */
9398 case 'L': /* register list for exit macro */
9399 {
9400 int mask;
9401
9402 if (c == 'l')
9403 mask = 0;
9404 else
9405 mask = 7 << 3;
9406 while (*s != '\0')
9407 {
9408 int freg, reg1, reg2;
9409
9410 while (*s == ' ' || *s == ',')
9411 ++s;
9412 if (*s != '$')
9413 {
9414 as_bad (_("can't parse register list"));
9415 break;
9416 }
9417 ++s;
9418 if (*s != 'f')
9419 freg = 0;
9420 else
9421 {
9422 freg = 1;
9423 ++s;
9424 }
9425 reg1 = 0;
9426 while (ISDIGIT (*s))
9427 {
9428 reg1 *= 10;
9429 reg1 += *s - '0';
9430 ++s;
9431 }
9432 if (*s == ' ')
9433 ++s;
9434 if (*s != '-')
9435 reg2 = reg1;
9436 else
9437 {
9438 ++s;
9439 if (*s != '$')
9440 break;
9441 ++s;
9442 if (freg)
9443 {
9444 if (*s == 'f')
9445 ++s;
9446 else
9447 {
9448 as_bad (_("invalid register list"));
9449 break;
9450 }
9451 }
9452 reg2 = 0;
9453 while (ISDIGIT (*s))
9454 {
9455 reg2 *= 10;
9456 reg2 += *s - '0';
9457 ++s;
9458 }
9459 }
9460 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9461 {
9462 mask &= ~ (7 << 3);
9463 mask |= 5 << 3;
9464 }
9465 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9466 {
9467 mask &= ~ (7 << 3);
9468 mask |= 6 << 3;
9469 }
9470 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9471 mask |= (reg2 - 3) << 3;
9472 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9473 mask |= (reg2 - 15) << 1;
9474 else if (reg1 == RA && reg2 == RA)
9475 mask |= 1;
9476 else
9477 {
9478 as_bad (_("invalid register list"));
9479 break;
9480 }
9481 }
9482 /* The mask is filled in in the opcode table for the
9483 benefit of the disassembler. We remove it before
9484 applying the actual mask. */
9485 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9486 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9487 }
9488 continue;
9489
9490 case 'e': /* extend code */
9491 my_getExpression (&imm_expr, s);
9492 check_absolute_expr (ip, &imm_expr);
9493 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9494 {
9495 as_warn (_("Invalid value for `%s' (%lu)"),
9496 ip->insn_mo->name,
9497 (unsigned long) imm_expr.X_add_number);
9498 imm_expr.X_add_number &= 0x7ff;
9499 }
9500 ip->insn_opcode |= imm_expr.X_add_number;
9501 imm_expr.X_op = O_absent;
9502 s = expr_end;
9503 continue;
9504
9505 default:
9506 internalError ();
9507 }
9508 break;
9509 }
9510
9511 /* Args don't match. */
9512 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9513 strcmp (insn->name, insn[1].name) == 0)
9514 {
9515 ++insn;
9516 s = argsstart;
9517 continue;
9518 }
9519
9520 insn_error = _("illegal operands");
9521
9522 return;
9523 }
9524 }
9525
9526 /* This structure holds information we know about a mips16 immediate
9527 argument type. */
9528
9529 struct mips16_immed_operand
9530 {
9531 /* The type code used in the argument string in the opcode table. */
9532 int type;
9533 /* The number of bits in the short form of the opcode. */
9534 int nbits;
9535 /* The number of bits in the extended form of the opcode. */
9536 int extbits;
9537 /* The amount by which the short form is shifted when it is used;
9538 for example, the sw instruction has a shift count of 2. */
9539 int shift;
9540 /* The amount by which the short form is shifted when it is stored
9541 into the instruction code. */
9542 int op_shift;
9543 /* Non-zero if the short form is unsigned. */
9544 int unsp;
9545 /* Non-zero if the extended form is unsigned. */
9546 int extu;
9547 /* Non-zero if the value is PC relative. */
9548 int pcrel;
9549 };
9550
9551 /* The mips16 immediate operand types. */
9552
9553 static const struct mips16_immed_operand mips16_immed_operands[] =
9554 {
9555 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9556 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9557 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9558 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9559 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9560 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9561 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9562 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9563 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9564 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9565 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9566 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9567 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9568 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9569 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9570 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9571 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9572 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9573 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9574 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9575 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9576 };
9577
9578 #define MIPS16_NUM_IMMED \
9579 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9580
9581 /* Handle a mips16 instruction with an immediate value. This or's the
9582 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9583 whether an extended value is needed; if one is needed, it sets
9584 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9585 If SMALL is true, an unextended opcode was explicitly requested.
9586 If EXT is true, an extended opcode was explicitly requested. If
9587 WARN is true, warn if EXT does not match reality. */
9588
9589 static void
9590 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9591 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9592 unsigned long *insn, bfd_boolean *use_extend,
9593 unsigned short *extend)
9594 {
9595 register const struct mips16_immed_operand *op;
9596 int mintiny, maxtiny;
9597 bfd_boolean needext;
9598
9599 op = mips16_immed_operands;
9600 while (op->type != type)
9601 {
9602 ++op;
9603 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9604 }
9605
9606 if (op->unsp)
9607 {
9608 if (type == '<' || type == '>' || type == '[' || type == ']')
9609 {
9610 mintiny = 1;
9611 maxtiny = 1 << op->nbits;
9612 }
9613 else
9614 {
9615 mintiny = 0;
9616 maxtiny = (1 << op->nbits) - 1;
9617 }
9618 }
9619 else
9620 {
9621 mintiny = - (1 << (op->nbits - 1));
9622 maxtiny = (1 << (op->nbits - 1)) - 1;
9623 }
9624
9625 /* Branch offsets have an implicit 0 in the lowest bit. */
9626 if (type == 'p' || type == 'q')
9627 val /= 2;
9628
9629 if ((val & ((1 << op->shift) - 1)) != 0
9630 || val < (mintiny << op->shift)
9631 || val > (maxtiny << op->shift))
9632 needext = TRUE;
9633 else
9634 needext = FALSE;
9635
9636 if (warn && ext && ! needext)
9637 as_warn_where (file, line,
9638 _("extended operand requested but not required"));
9639 if (small && needext)
9640 as_bad_where (file, line, _("invalid unextended operand value"));
9641
9642 if (small || (! ext && ! needext))
9643 {
9644 int insnval;
9645
9646 *use_extend = FALSE;
9647 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9648 insnval <<= op->op_shift;
9649 *insn |= insnval;
9650 }
9651 else
9652 {
9653 long minext, maxext;
9654 int extval;
9655
9656 if (op->extu)
9657 {
9658 minext = 0;
9659 maxext = (1 << op->extbits) - 1;
9660 }
9661 else
9662 {
9663 minext = - (1 << (op->extbits - 1));
9664 maxext = (1 << (op->extbits - 1)) - 1;
9665 }
9666 if (val < minext || val > maxext)
9667 as_bad_where (file, line,
9668 _("operand value out of range for instruction"));
9669
9670 *use_extend = TRUE;
9671 if (op->extbits == 16)
9672 {
9673 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9674 val &= 0x1f;
9675 }
9676 else if (op->extbits == 15)
9677 {
9678 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9679 val &= 0xf;
9680 }
9681 else
9682 {
9683 extval = ((val & 0x1f) << 6) | (val & 0x20);
9684 val = 0;
9685 }
9686
9687 *extend = (unsigned short) extval;
9688 *insn |= val;
9689 }
9690 }
9691 \f
9692 struct percent_op_match
9693 {
9694 const char *str;
9695 bfd_reloc_code_real_type reloc;
9696 };
9697
9698 static const struct percent_op_match mips_percent_op[] =
9699 {
9700 {"%lo", BFD_RELOC_LO16},
9701 #ifdef OBJ_ELF
9702 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9703 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9704 {"%call16", BFD_RELOC_MIPS_CALL16},
9705 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9706 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9707 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9708 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9709 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9710 {"%got", BFD_RELOC_MIPS_GOT16},
9711 {"%gp_rel", BFD_RELOC_GPREL16},
9712 {"%half", BFD_RELOC_16},
9713 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9714 {"%higher", BFD_RELOC_MIPS_HIGHER},
9715 {"%neg", BFD_RELOC_MIPS_SUB},
9716 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9717 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9718 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9719 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9720 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9721 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9722 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9723 #endif
9724 {"%hi", BFD_RELOC_HI16_S}
9725 };
9726
9727 static const struct percent_op_match mips16_percent_op[] =
9728 {
9729 {"%lo", BFD_RELOC_MIPS16_LO16},
9730 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9731 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9732 };
9733
9734
9735 /* Return true if *STR points to a relocation operator. When returning true,
9736 move *STR over the operator and store its relocation code in *RELOC.
9737 Leave both *STR and *RELOC alone when returning false. */
9738
9739 static bfd_boolean
9740 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9741 {
9742 const struct percent_op_match *percent_op;
9743 size_t limit, i;
9744
9745 if (mips_opts.mips16)
9746 {
9747 percent_op = mips16_percent_op;
9748 limit = ARRAY_SIZE (mips16_percent_op);
9749 }
9750 else
9751 {
9752 percent_op = mips_percent_op;
9753 limit = ARRAY_SIZE (mips_percent_op);
9754 }
9755
9756 for (i = 0; i < limit; i++)
9757 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9758 {
9759 int len = strlen (percent_op[i].str);
9760
9761 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9762 continue;
9763
9764 *str += strlen (percent_op[i].str);
9765 *reloc = percent_op[i].reloc;
9766
9767 /* Check whether the output BFD supports this relocation.
9768 If not, issue an error and fall back on something safe. */
9769 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9770 {
9771 as_bad ("relocation %s isn't supported by the current ABI",
9772 percent_op[i].str);
9773 *reloc = BFD_RELOC_UNUSED;
9774 }
9775 return TRUE;
9776 }
9777 return FALSE;
9778 }
9779
9780
9781 /* Parse string STR as a 16-bit relocatable operand. Store the
9782 expression in *EP and the relocations in the array starting
9783 at RELOC. Return the number of relocation operators used.
9784
9785 On exit, EXPR_END points to the first character after the expression. */
9786
9787 static size_t
9788 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9789 char *str)
9790 {
9791 bfd_reloc_code_real_type reversed_reloc[3];
9792 size_t reloc_index, i;
9793 int crux_depth, str_depth;
9794 char *crux;
9795
9796 /* Search for the start of the main expression, recoding relocations
9797 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9798 of the main expression and with CRUX_DEPTH containing the number
9799 of open brackets at that point. */
9800 reloc_index = -1;
9801 str_depth = 0;
9802 do
9803 {
9804 reloc_index++;
9805 crux = str;
9806 crux_depth = str_depth;
9807
9808 /* Skip over whitespace and brackets, keeping count of the number
9809 of brackets. */
9810 while (*str == ' ' || *str == '\t' || *str == '(')
9811 if (*str++ == '(')
9812 str_depth++;
9813 }
9814 while (*str == '%'
9815 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9816 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9817
9818 my_getExpression (ep, crux);
9819 str = expr_end;
9820
9821 /* Match every open bracket. */
9822 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9823 if (*str++ == ')')
9824 crux_depth--;
9825
9826 if (crux_depth > 0)
9827 as_bad ("unclosed '('");
9828
9829 expr_end = str;
9830
9831 if (reloc_index != 0)
9832 {
9833 prev_reloc_op_frag = frag_now;
9834 for (i = 0; i < reloc_index; i++)
9835 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9836 }
9837
9838 return reloc_index;
9839 }
9840
9841 static void
9842 my_getExpression (expressionS *ep, char *str)
9843 {
9844 char *save_in;
9845 valueT val;
9846
9847 save_in = input_line_pointer;
9848 input_line_pointer = str;
9849 expression (ep);
9850 expr_end = input_line_pointer;
9851 input_line_pointer = save_in;
9852
9853 /* If we are in mips16 mode, and this is an expression based on `.',
9854 then we bump the value of the symbol by 1 since that is how other
9855 text symbols are handled. We don't bother to handle complex
9856 expressions, just `.' plus or minus a constant. */
9857 if (mips_opts.mips16
9858 && ep->X_op == O_symbol
9859 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9860 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9861 && symbol_get_frag (ep->X_add_symbol) == frag_now
9862 && symbol_constant_p (ep->X_add_symbol)
9863 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9864 S_SET_VALUE (ep->X_add_symbol, val + 1);
9865 }
9866
9867 /* Turn a string in input_line_pointer into a floating point constant
9868 of type TYPE, and store the appropriate bytes in *LITP. The number
9869 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9870 returned, or NULL on OK. */
9871
9872 char *
9873 md_atof (int type, char *litP, int *sizeP)
9874 {
9875 int prec;
9876 LITTLENUM_TYPE words[4];
9877 char *t;
9878 int i;
9879
9880 switch (type)
9881 {
9882 case 'f':
9883 prec = 2;
9884 break;
9885
9886 case 'd':
9887 prec = 4;
9888 break;
9889
9890 default:
9891 *sizeP = 0;
9892 return _("bad call to md_atof");
9893 }
9894
9895 t = atof_ieee (input_line_pointer, type, words);
9896 if (t)
9897 input_line_pointer = t;
9898
9899 *sizeP = prec * 2;
9900
9901 if (! target_big_endian)
9902 {
9903 for (i = prec - 1; i >= 0; i--)
9904 {
9905 md_number_to_chars (litP, words[i], 2);
9906 litP += 2;
9907 }
9908 }
9909 else
9910 {
9911 for (i = 0; i < prec; i++)
9912 {
9913 md_number_to_chars (litP, words[i], 2);
9914 litP += 2;
9915 }
9916 }
9917
9918 return NULL;
9919 }
9920
9921 void
9922 md_number_to_chars (char *buf, valueT val, int n)
9923 {
9924 if (target_big_endian)
9925 number_to_chars_bigendian (buf, val, n);
9926 else
9927 number_to_chars_littleendian (buf, val, n);
9928 }
9929 \f
9930 #ifdef OBJ_ELF
9931 static int support_64bit_objects(void)
9932 {
9933 const char **list, **l;
9934 int yes;
9935
9936 list = bfd_target_list ();
9937 for (l = list; *l != NULL; l++)
9938 #ifdef TE_TMIPS
9939 /* This is traditional mips */
9940 if (strcmp (*l, "elf64-tradbigmips") == 0
9941 || strcmp (*l, "elf64-tradlittlemips") == 0)
9942 #else
9943 if (strcmp (*l, "elf64-bigmips") == 0
9944 || strcmp (*l, "elf64-littlemips") == 0)
9945 #endif
9946 break;
9947 yes = (*l != NULL);
9948 free (list);
9949 return yes;
9950 }
9951 #endif /* OBJ_ELF */
9952
9953 const char *md_shortopts = "O::g::G:";
9954
9955 struct option md_longopts[] =
9956 {
9957 /* Options which specify architecture. */
9958 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
9959 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
9960 {"march", required_argument, NULL, OPTION_MARCH},
9961 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
9962 {"mtune", required_argument, NULL, OPTION_MTUNE},
9963 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
9964 {"mips0", no_argument, NULL, OPTION_MIPS1},
9965 {"mips1", no_argument, NULL, OPTION_MIPS1},
9966 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
9967 {"mips2", no_argument, NULL, OPTION_MIPS2},
9968 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
9969 {"mips3", no_argument, NULL, OPTION_MIPS3},
9970 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
9971 {"mips4", no_argument, NULL, OPTION_MIPS4},
9972 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
9973 {"mips5", no_argument, NULL, OPTION_MIPS5},
9974 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
9975 {"mips32", no_argument, NULL, OPTION_MIPS32},
9976 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
9977 {"mips64", no_argument, NULL, OPTION_MIPS64},
9978 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
9979 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
9980 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
9981 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
9982
9983 /* Options which specify Application Specific Extensions (ASEs). */
9984 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
9985 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
9986 {"mips16", no_argument, NULL, OPTION_MIPS16},
9987 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
9988 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9989 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
9990 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9991 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
9992 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9993 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
9994 {"mdmx", no_argument, NULL, OPTION_MDMX},
9995 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
9996 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9997
9998 /* Old-style architecture options. Don't add more of these. */
9999 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10000 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10001 {"m4650", no_argument, NULL, OPTION_M4650},
10002 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10003 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10004 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10005 {"m4010", no_argument, NULL, OPTION_M4010},
10006 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10007 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10008 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10009 {"m4100", no_argument, NULL, OPTION_M4100},
10010 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10011 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10012 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10013 {"m3900", no_argument, NULL, OPTION_M3900},
10014 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10015 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10016
10017 /* Options which enable bug fixes. */
10018 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10019 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10020 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10021 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10022 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10023 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10024 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10025 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10026 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10027 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10028 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10029 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10030 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10031 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10032
10033 /* Miscellaneous options. */
10034 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10035 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10036 {"trap", no_argument, NULL, OPTION_TRAP},
10037 {"no-break", no_argument, NULL, OPTION_TRAP},
10038 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10039 {"break", no_argument, NULL, OPTION_BREAK},
10040 {"no-trap", no_argument, NULL, OPTION_BREAK},
10041 #define OPTION_EB (OPTION_MISC_BASE + 2)
10042 {"EB", no_argument, NULL, OPTION_EB},
10043 #define OPTION_EL (OPTION_MISC_BASE + 3)
10044 {"EL", no_argument, NULL, OPTION_EL},
10045 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10046 {"mfp32", no_argument, NULL, OPTION_FP32},
10047 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10048 {"mgp32", no_argument, NULL, OPTION_GP32},
10049 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10050 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10051 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10052 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10053 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10054 {"mfp64", no_argument, NULL, OPTION_FP64},
10055 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10056 {"mgp64", no_argument, NULL, OPTION_GP64},
10057 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10058 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10059 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10060 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10061 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10062 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10063 {"mshared", no_argument, NULL, OPTION_MSHARED},
10064 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10065 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10066 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10067 {"msym32", no_argument, NULL, OPTION_MSYM32},
10068 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10069
10070 /* ELF-specific options. */
10071 #ifdef OBJ_ELF
10072 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10073 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10074 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10075 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10076 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10077 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10078 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10079 {"xgot", no_argument, NULL, OPTION_XGOT},
10080 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10081 {"mabi", required_argument, NULL, OPTION_MABI},
10082 #define OPTION_32 (OPTION_ELF_BASE + 4)
10083 {"32", no_argument, NULL, OPTION_32},
10084 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10085 {"n32", no_argument, NULL, OPTION_N32},
10086 #define OPTION_64 (OPTION_ELF_BASE + 6)
10087 {"64", no_argument, NULL, OPTION_64},
10088 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10089 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10090 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10091 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10092 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10093 {"mpdr", no_argument, NULL, OPTION_PDR},
10094 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10095 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10096 #endif /* OBJ_ELF */
10097
10098 {NULL, no_argument, NULL, 0}
10099 };
10100 size_t md_longopts_size = sizeof (md_longopts);
10101
10102 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10103 NEW_VALUE. Warn if another value was already specified. Note:
10104 we have to defer parsing the -march and -mtune arguments in order
10105 to handle 'from-abi' correctly, since the ABI might be specified
10106 in a later argument. */
10107
10108 static void
10109 mips_set_option_string (const char **string_ptr, const char *new_value)
10110 {
10111 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10112 as_warn (_("A different %s was already specified, is now %s"),
10113 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10114 new_value);
10115
10116 *string_ptr = new_value;
10117 }
10118
10119 int
10120 md_parse_option (int c, char *arg)
10121 {
10122 switch (c)
10123 {
10124 case OPTION_CONSTRUCT_FLOATS:
10125 mips_disable_float_construction = 0;
10126 break;
10127
10128 case OPTION_NO_CONSTRUCT_FLOATS:
10129 mips_disable_float_construction = 1;
10130 break;
10131
10132 case OPTION_TRAP:
10133 mips_trap = 1;
10134 break;
10135
10136 case OPTION_BREAK:
10137 mips_trap = 0;
10138 break;
10139
10140 case OPTION_EB:
10141 target_big_endian = 1;
10142 break;
10143
10144 case OPTION_EL:
10145 target_big_endian = 0;
10146 break;
10147
10148 case 'O':
10149 if (arg && arg[1] == '0')
10150 mips_optimize = 1;
10151 else
10152 mips_optimize = 2;
10153 break;
10154
10155 case 'g':
10156 if (arg == NULL)
10157 mips_debug = 2;
10158 else
10159 mips_debug = atoi (arg);
10160 /* When the MIPS assembler sees -g or -g2, it does not do
10161 optimizations which limit full symbolic debugging. We take
10162 that to be equivalent to -O0. */
10163 if (mips_debug == 2)
10164 mips_optimize = 1;
10165 break;
10166
10167 case OPTION_MIPS1:
10168 file_mips_isa = ISA_MIPS1;
10169 break;
10170
10171 case OPTION_MIPS2:
10172 file_mips_isa = ISA_MIPS2;
10173 break;
10174
10175 case OPTION_MIPS3:
10176 file_mips_isa = ISA_MIPS3;
10177 break;
10178
10179 case OPTION_MIPS4:
10180 file_mips_isa = ISA_MIPS4;
10181 break;
10182
10183 case OPTION_MIPS5:
10184 file_mips_isa = ISA_MIPS5;
10185 break;
10186
10187 case OPTION_MIPS32:
10188 file_mips_isa = ISA_MIPS32;
10189 break;
10190
10191 case OPTION_MIPS32R2:
10192 file_mips_isa = ISA_MIPS32R2;
10193 break;
10194
10195 case OPTION_MIPS64R2:
10196 file_mips_isa = ISA_MIPS64R2;
10197 break;
10198
10199 case OPTION_MIPS64:
10200 file_mips_isa = ISA_MIPS64;
10201 break;
10202
10203 case OPTION_MTUNE:
10204 mips_set_option_string (&mips_tune_string, arg);
10205 break;
10206
10207 case OPTION_MARCH:
10208 mips_set_option_string (&mips_arch_string, arg);
10209 break;
10210
10211 case OPTION_M4650:
10212 mips_set_option_string (&mips_arch_string, "4650");
10213 mips_set_option_string (&mips_tune_string, "4650");
10214 break;
10215
10216 case OPTION_NO_M4650:
10217 break;
10218
10219 case OPTION_M4010:
10220 mips_set_option_string (&mips_arch_string, "4010");
10221 mips_set_option_string (&mips_tune_string, "4010");
10222 break;
10223
10224 case OPTION_NO_M4010:
10225 break;
10226
10227 case OPTION_M4100:
10228 mips_set_option_string (&mips_arch_string, "4100");
10229 mips_set_option_string (&mips_tune_string, "4100");
10230 break;
10231
10232 case OPTION_NO_M4100:
10233 break;
10234
10235 case OPTION_M3900:
10236 mips_set_option_string (&mips_arch_string, "3900");
10237 mips_set_option_string (&mips_tune_string, "3900");
10238 break;
10239
10240 case OPTION_NO_M3900:
10241 break;
10242
10243 case OPTION_MDMX:
10244 mips_opts.ase_mdmx = 1;
10245 break;
10246
10247 case OPTION_NO_MDMX:
10248 mips_opts.ase_mdmx = 0;
10249 break;
10250
10251 case OPTION_MIPS16:
10252 mips_opts.mips16 = 1;
10253 mips_no_prev_insn ();
10254 break;
10255
10256 case OPTION_NO_MIPS16:
10257 mips_opts.mips16 = 0;
10258 mips_no_prev_insn ();
10259 break;
10260
10261 case OPTION_MIPS3D:
10262 mips_opts.ase_mips3d = 1;
10263 break;
10264
10265 case OPTION_NO_MIPS3D:
10266 mips_opts.ase_mips3d = 0;
10267 break;
10268
10269 case OPTION_FIX_VR4120:
10270 mips_fix_vr4120 = 1;
10271 break;
10272
10273 case OPTION_NO_FIX_VR4120:
10274 mips_fix_vr4120 = 0;
10275 break;
10276
10277 case OPTION_FIX_VR4130:
10278 mips_fix_vr4130 = 1;
10279 break;
10280
10281 case OPTION_NO_FIX_VR4130:
10282 mips_fix_vr4130 = 0;
10283 break;
10284
10285 case OPTION_RELAX_BRANCH:
10286 mips_relax_branch = 1;
10287 break;
10288
10289 case OPTION_NO_RELAX_BRANCH:
10290 mips_relax_branch = 0;
10291 break;
10292
10293 case OPTION_MSHARED:
10294 mips_in_shared = TRUE;
10295 break;
10296
10297 case OPTION_MNO_SHARED:
10298 mips_in_shared = FALSE;
10299 break;
10300
10301 case OPTION_MSYM32:
10302 mips_opts.sym32 = TRUE;
10303 break;
10304
10305 case OPTION_MNO_SYM32:
10306 mips_opts.sym32 = FALSE;
10307 break;
10308
10309 #ifdef OBJ_ELF
10310 /* When generating ELF code, we permit -KPIC and -call_shared to
10311 select SVR4_PIC, and -non_shared to select no PIC. This is
10312 intended to be compatible with Irix 5. */
10313 case OPTION_CALL_SHARED:
10314 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10315 {
10316 as_bad (_("-call_shared is supported only for ELF format"));
10317 return 0;
10318 }
10319 mips_pic = SVR4_PIC;
10320 mips_abicalls = TRUE;
10321 if (g_switch_seen && g_switch_value != 0)
10322 {
10323 as_bad (_("-G may not be used with SVR4 PIC code"));
10324 return 0;
10325 }
10326 g_switch_value = 0;
10327 break;
10328
10329 case OPTION_NON_SHARED:
10330 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10331 {
10332 as_bad (_("-non_shared is supported only for ELF format"));
10333 return 0;
10334 }
10335 mips_pic = NO_PIC;
10336 mips_abicalls = FALSE;
10337 break;
10338
10339 /* The -xgot option tells the assembler to use 32 offsets when
10340 accessing the got in SVR4_PIC mode. It is for Irix
10341 compatibility. */
10342 case OPTION_XGOT:
10343 mips_big_got = 1;
10344 break;
10345 #endif /* OBJ_ELF */
10346
10347 case 'G':
10348 g_switch_value = atoi (arg);
10349 g_switch_seen = 1;
10350 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10351 {
10352 as_bad (_("-G may not be used with SVR4 PIC code"));
10353 return 0;
10354 }
10355 break;
10356
10357 #ifdef OBJ_ELF
10358 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10359 and -mabi=64. */
10360 case OPTION_32:
10361 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10362 {
10363 as_bad (_("-32 is supported for ELF format only"));
10364 return 0;
10365 }
10366 mips_abi = O32_ABI;
10367 break;
10368
10369 case OPTION_N32:
10370 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10371 {
10372 as_bad (_("-n32 is supported for ELF format only"));
10373 return 0;
10374 }
10375 mips_abi = N32_ABI;
10376 break;
10377
10378 case OPTION_64:
10379 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10380 {
10381 as_bad (_("-64 is supported for ELF format only"));
10382 return 0;
10383 }
10384 mips_abi = N64_ABI;
10385 if (! support_64bit_objects())
10386 as_fatal (_("No compiled in support for 64 bit object file format"));
10387 break;
10388 #endif /* OBJ_ELF */
10389
10390 case OPTION_GP32:
10391 file_mips_gp32 = 1;
10392 break;
10393
10394 case OPTION_GP64:
10395 file_mips_gp32 = 0;
10396 break;
10397
10398 case OPTION_FP32:
10399 file_mips_fp32 = 1;
10400 break;
10401
10402 case OPTION_FP64:
10403 file_mips_fp32 = 0;
10404 break;
10405
10406 #ifdef OBJ_ELF
10407 case OPTION_MABI:
10408 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10409 {
10410 as_bad (_("-mabi is supported for ELF format only"));
10411 return 0;
10412 }
10413 if (strcmp (arg, "32") == 0)
10414 mips_abi = O32_ABI;
10415 else if (strcmp (arg, "o64") == 0)
10416 mips_abi = O64_ABI;
10417 else if (strcmp (arg, "n32") == 0)
10418 mips_abi = N32_ABI;
10419 else if (strcmp (arg, "64") == 0)
10420 {
10421 mips_abi = N64_ABI;
10422 if (! support_64bit_objects())
10423 as_fatal (_("No compiled in support for 64 bit object file "
10424 "format"));
10425 }
10426 else if (strcmp (arg, "eabi") == 0)
10427 mips_abi = EABI_ABI;
10428 else
10429 {
10430 as_fatal (_("invalid abi -mabi=%s"), arg);
10431 return 0;
10432 }
10433 break;
10434 #endif /* OBJ_ELF */
10435
10436 case OPTION_M7000_HILO_FIX:
10437 mips_7000_hilo_fix = TRUE;
10438 break;
10439
10440 case OPTION_MNO_7000_HILO_FIX:
10441 mips_7000_hilo_fix = FALSE;
10442 break;
10443
10444 #ifdef OBJ_ELF
10445 case OPTION_MDEBUG:
10446 mips_flag_mdebug = TRUE;
10447 break;
10448
10449 case OPTION_NO_MDEBUG:
10450 mips_flag_mdebug = FALSE;
10451 break;
10452
10453 case OPTION_PDR:
10454 mips_flag_pdr = TRUE;
10455 break;
10456
10457 case OPTION_NO_PDR:
10458 mips_flag_pdr = FALSE;
10459 break;
10460 #endif /* OBJ_ELF */
10461
10462 default:
10463 return 0;
10464 }
10465
10466 return 1;
10467 }
10468 \f
10469 /* Set up globals to generate code for the ISA or processor
10470 described by INFO. */
10471
10472 static void
10473 mips_set_architecture (const struct mips_cpu_info *info)
10474 {
10475 if (info != 0)
10476 {
10477 file_mips_arch = info->cpu;
10478 mips_opts.arch = info->cpu;
10479 mips_opts.isa = info->isa;
10480 }
10481 }
10482
10483
10484 /* Likewise for tuning. */
10485
10486 static void
10487 mips_set_tune (const struct mips_cpu_info *info)
10488 {
10489 if (info != 0)
10490 mips_tune = info->cpu;
10491 }
10492
10493
10494 void
10495 mips_after_parse_args (void)
10496 {
10497 const struct mips_cpu_info *arch_info = 0;
10498 const struct mips_cpu_info *tune_info = 0;
10499
10500 /* GP relative stuff not working for PE */
10501 if (strncmp (TARGET_OS, "pe", 2) == 0)
10502 {
10503 if (g_switch_seen && g_switch_value != 0)
10504 as_bad (_("-G not supported in this configuration."));
10505 g_switch_value = 0;
10506 }
10507
10508 if (mips_abi == NO_ABI)
10509 mips_abi = MIPS_DEFAULT_ABI;
10510
10511 /* The following code determines the architecture and register size.
10512 Similar code was added to GCC 3.3 (see override_options() in
10513 config/mips/mips.c). The GAS and GCC code should be kept in sync
10514 as much as possible. */
10515
10516 if (mips_arch_string != 0)
10517 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10518
10519 if (file_mips_isa != ISA_UNKNOWN)
10520 {
10521 /* Handle -mipsN. At this point, file_mips_isa contains the
10522 ISA level specified by -mipsN, while arch_info->isa contains
10523 the -march selection (if any). */
10524 if (arch_info != 0)
10525 {
10526 /* -march takes precedence over -mipsN, since it is more descriptive.
10527 There's no harm in specifying both as long as the ISA levels
10528 are the same. */
10529 if (file_mips_isa != arch_info->isa)
10530 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10531 mips_cpu_info_from_isa (file_mips_isa)->name,
10532 mips_cpu_info_from_isa (arch_info->isa)->name);
10533 }
10534 else
10535 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10536 }
10537
10538 if (arch_info == 0)
10539 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10540
10541 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10542 as_bad ("-march=%s is not compatible with the selected ABI",
10543 arch_info->name);
10544
10545 mips_set_architecture (arch_info);
10546
10547 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10548 if (mips_tune_string != 0)
10549 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10550
10551 if (tune_info == 0)
10552 mips_set_tune (arch_info);
10553 else
10554 mips_set_tune (tune_info);
10555
10556 if (file_mips_gp32 >= 0)
10557 {
10558 /* The user specified the size of the integer registers. Make sure
10559 it agrees with the ABI and ISA. */
10560 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10561 as_bad (_("-mgp64 used with a 32-bit processor"));
10562 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10563 as_bad (_("-mgp32 used with a 64-bit ABI"));
10564 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10565 as_bad (_("-mgp64 used with a 32-bit ABI"));
10566 }
10567 else
10568 {
10569 /* Infer the integer register size from the ABI and processor.
10570 Restrict ourselves to 32-bit registers if that's all the
10571 processor has, or if the ABI cannot handle 64-bit registers. */
10572 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10573 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10574 }
10575
10576 /* ??? GAS treats single-float processors as though they had 64-bit
10577 float registers (although it complains when double-precision
10578 instructions are used). As things stand, saying they have 32-bit
10579 registers would lead to spurious "register must be even" messages.
10580 So here we assume float registers are always the same size as
10581 integer ones, unless the user says otherwise. */
10582 if (file_mips_fp32 < 0)
10583 file_mips_fp32 = file_mips_gp32;
10584
10585 /* End of GCC-shared inference code. */
10586
10587 /* This flag is set when we have a 64-bit capable CPU but use only
10588 32-bit wide registers. Note that EABI does not use it. */
10589 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10590 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10591 || mips_abi == O32_ABI))
10592 mips_32bitmode = 1;
10593
10594 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10595 as_bad (_("trap exception not supported at ISA 1"));
10596
10597 /* If the selected architecture includes support for ASEs, enable
10598 generation of code for them. */
10599 if (mips_opts.mips16 == -1)
10600 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10601 if (mips_opts.ase_mips3d == -1)
10602 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10603 if (mips_opts.ase_mdmx == -1)
10604 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10605
10606 file_mips_isa = mips_opts.isa;
10607 file_ase_mips16 = mips_opts.mips16;
10608 file_ase_mips3d = mips_opts.ase_mips3d;
10609 file_ase_mdmx = mips_opts.ase_mdmx;
10610 mips_opts.gp32 = file_mips_gp32;
10611 mips_opts.fp32 = file_mips_fp32;
10612
10613 if (mips_flag_mdebug < 0)
10614 {
10615 #ifdef OBJ_MAYBE_ECOFF
10616 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10617 mips_flag_mdebug = 1;
10618 else
10619 #endif /* OBJ_MAYBE_ECOFF */
10620 mips_flag_mdebug = 0;
10621 }
10622 }
10623 \f
10624 void
10625 mips_init_after_args (void)
10626 {
10627 /* initialize opcodes */
10628 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10629 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10630 }
10631
10632 long
10633 md_pcrel_from (fixS *fixP)
10634 {
10635 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10636 switch (fixP->fx_r_type)
10637 {
10638 case BFD_RELOC_16_PCREL_S2:
10639 case BFD_RELOC_MIPS_JMP:
10640 /* Return the address of the delay slot. */
10641 return addr + 4;
10642 default:
10643 return addr;
10644 }
10645 }
10646
10647 /* This is called before the symbol table is processed. In order to
10648 work with gcc when using mips-tfile, we must keep all local labels.
10649 However, in other cases, we want to discard them. If we were
10650 called with -g, but we didn't see any debugging information, it may
10651 mean that gcc is smuggling debugging information through to
10652 mips-tfile, in which case we must generate all local labels. */
10653
10654 void
10655 mips_frob_file_before_adjust (void)
10656 {
10657 #ifndef NO_ECOFF_DEBUGGING
10658 if (ECOFF_DEBUGGING
10659 && mips_debug != 0
10660 && ! ecoff_debugging_seen)
10661 flag_keep_locals = 1;
10662 #endif
10663 }
10664
10665 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10666 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10667 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10668 relocation operators.
10669
10670 For our purposes, a %lo() expression matches a %got() or %hi()
10671 expression if:
10672
10673 (a) it refers to the same symbol; and
10674 (b) the offset applied in the %lo() expression is no lower than
10675 the offset applied in the %got() or %hi().
10676
10677 (b) allows us to cope with code like:
10678
10679 lui $4,%hi(foo)
10680 lh $4,%lo(foo+2)($4)
10681
10682 ...which is legal on RELA targets, and has a well-defined behaviour
10683 if the user knows that adding 2 to "foo" will not induce a carry to
10684 the high 16 bits.
10685
10686 When several %lo()s match a particular %got() or %hi(), we use the
10687 following rules to distinguish them:
10688
10689 (1) %lo()s with smaller offsets are a better match than %lo()s with
10690 higher offsets.
10691
10692 (2) %lo()s with no matching %got() or %hi() are better than those
10693 that already have a matching %got() or %hi().
10694
10695 (3) later %lo()s are better than earlier %lo()s.
10696
10697 These rules are applied in order.
10698
10699 (1) means, among other things, that %lo()s with identical offsets are
10700 chosen if they exist.
10701
10702 (2) means that we won't associate several high-part relocations with
10703 the same low-part relocation unless there's no alternative. Having
10704 several high parts for the same low part is a GNU extension; this rule
10705 allows careful users to avoid it.
10706
10707 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10708 with the last high-part relocation being at the front of the list.
10709 It therefore makes sense to choose the last matching low-part
10710 relocation, all other things being equal. It's also easier
10711 to code that way. */
10712
10713 void
10714 mips_frob_file (void)
10715 {
10716 struct mips_hi_fixup *l;
10717
10718 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10719 {
10720 segment_info_type *seginfo;
10721 bfd_boolean matched_lo_p;
10722 fixS **hi_pos, **lo_pos, **pos;
10723
10724 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10725
10726 /* If a GOT16 relocation turns out to be against a global symbol,
10727 there isn't supposed to be a matching LO. */
10728 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10729 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10730 continue;
10731
10732 /* Check quickly whether the next fixup happens to be a matching %lo. */
10733 if (fixup_has_matching_lo_p (l->fixp))
10734 continue;
10735
10736 seginfo = seg_info (l->seg);
10737
10738 /* Set HI_POS to the position of this relocation in the chain.
10739 Set LO_POS to the position of the chosen low-part relocation.
10740 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10741 relocation that matches an immediately-preceding high-part
10742 relocation. */
10743 hi_pos = NULL;
10744 lo_pos = NULL;
10745 matched_lo_p = FALSE;
10746 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10747 {
10748 if (*pos == l->fixp)
10749 hi_pos = pos;
10750
10751 if (((*pos)->fx_r_type == BFD_RELOC_LO16
10752 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
10753 && (*pos)->fx_addsy == l->fixp->fx_addsy
10754 && (*pos)->fx_offset >= l->fixp->fx_offset
10755 && (lo_pos == NULL
10756 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10757 || (!matched_lo_p
10758 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10759 lo_pos = pos;
10760
10761 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10762 && fixup_has_matching_lo_p (*pos));
10763 }
10764
10765 /* If we found a match, remove the high-part relocation from its
10766 current position and insert it before the low-part relocation.
10767 Make the offsets match so that fixup_has_matching_lo_p()
10768 will return true.
10769
10770 We don't warn about unmatched high-part relocations since some
10771 versions of gcc have been known to emit dead "lui ...%hi(...)"
10772 instructions. */
10773 if (lo_pos != NULL)
10774 {
10775 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10776 if (l->fixp->fx_next != *lo_pos)
10777 {
10778 *hi_pos = l->fixp->fx_next;
10779 l->fixp->fx_next = *lo_pos;
10780 *lo_pos = l->fixp;
10781 }
10782 }
10783 }
10784 }
10785
10786 /* We may have combined relocations without symbols in the N32/N64 ABI.
10787 We have to prevent gas from dropping them. */
10788
10789 int
10790 mips_force_relocation (fixS *fixp)
10791 {
10792 if (generic_force_reloc (fixp))
10793 return 1;
10794
10795 if (HAVE_NEWABI
10796 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10797 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10798 || fixp->fx_r_type == BFD_RELOC_HI16_S
10799 || fixp->fx_r_type == BFD_RELOC_LO16))
10800 return 1;
10801
10802 return 0;
10803 }
10804
10805 /* This hook is called before a fix is simplified. We don't really
10806 decide whether to skip a fix here. Rather, we turn global symbols
10807 used as branch targets into local symbols, such that they undergo
10808 simplification. We can only do this if the symbol is defined and
10809 it is in the same section as the branch. If this doesn't hold, we
10810 emit a better error message than just saying the relocation is not
10811 valid for the selected object format.
10812
10813 FIXP is the fix-up we're going to try to simplify, SEG is the
10814 segment in which the fix up occurs. The return value should be
10815 non-zero to indicate the fix-up is valid for further
10816 simplifications. */
10817
10818 int
10819 mips_validate_fix (struct fix *fixP, asection *seg)
10820 {
10821 /* There's a lot of discussion on whether it should be possible to
10822 use R_MIPS_PC16 to represent branch relocations. The outcome
10823 seems to be that it can, but gas/bfd are very broken in creating
10824 RELA relocations for this, so for now we only accept branches to
10825 symbols in the same section. Anything else is of dubious value,
10826 since there's no guarantee that at link time the symbol would be
10827 in range. Even for branches to local symbols this is arguably
10828 wrong, since it we assume the symbol is not going to be
10829 overridden, which should be possible per ELF library semantics,
10830 but then, there isn't a dynamic relocation that could be used to
10831 this effect, and the target would likely be out of range as well.
10832
10833 Unfortunately, it seems that there is too much code out there
10834 that relies on branches to symbols that are global to be resolved
10835 as if they were local, like the IRIX tools do, so we do it as
10836 well, but with a warning so that people are reminded to fix their
10837 code. If we ever get back to using R_MIPS_PC16 for branch
10838 targets, this entire block should go away (and probably the
10839 whole function). */
10840
10841 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10842 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10843 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10844 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10845 && fixP->fx_addsy)
10846 {
10847 if (! S_IS_DEFINED (fixP->fx_addsy))
10848 {
10849 as_bad_where (fixP->fx_file, fixP->fx_line,
10850 _("Cannot branch to undefined symbol."));
10851 /* Avoid any further errors about this fixup. */
10852 fixP->fx_done = 1;
10853 }
10854 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10855 {
10856 as_bad_where (fixP->fx_file, fixP->fx_line,
10857 _("Cannot branch to symbol in another section."));
10858 fixP->fx_done = 1;
10859 }
10860 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10861 {
10862 symbolS *sym = fixP->fx_addsy;
10863
10864 if (mips_pic == SVR4_PIC)
10865 as_warn_where (fixP->fx_file, fixP->fx_line,
10866 _("Pretending global symbol used as branch target is local."));
10867
10868 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10869 S_GET_SEGMENT (sym),
10870 S_GET_VALUE (sym),
10871 symbol_get_frag (sym));
10872 copy_symbol_attributes (fixP->fx_addsy, sym);
10873 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10874 assert (symbol_resolved_p (sym));
10875 symbol_mark_resolved (fixP->fx_addsy);
10876 }
10877 }
10878
10879 return 1;
10880 }
10881
10882 /* Apply a fixup to the object file. */
10883
10884 void
10885 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
10886 {
10887 bfd_byte *buf;
10888 long insn;
10889 reloc_howto_type *howto;
10890
10891 /* We ignore generic BFD relocations we don't know about. */
10892 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10893 if (! howto)
10894 return;
10895
10896 assert (fixP->fx_size == 4
10897 || fixP->fx_r_type == BFD_RELOC_16
10898 || fixP->fx_r_type == BFD_RELOC_64
10899 || fixP->fx_r_type == BFD_RELOC_CTOR
10900 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10901 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10902 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10903
10904 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
10905
10906 assert (! fixP->fx_pcrel);
10907
10908 /* Don't treat parts of a composite relocation as done. There are two
10909 reasons for this:
10910
10911 (1) The second and third parts will be against 0 (RSS_UNDEF) but
10912 should nevertheless be emitted if the first part is.
10913
10914 (2) In normal usage, composite relocations are never assembly-time
10915 constants. The easiest way of dealing with the pathological
10916 exceptions is to generate a relocation against STN_UNDEF and
10917 leave everything up to the linker. */
10918 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
10919 fixP->fx_done = 1;
10920
10921 switch (fixP->fx_r_type)
10922 {
10923 case BFD_RELOC_MIPS_TLS_GD:
10924 case BFD_RELOC_MIPS_TLS_LDM:
10925 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
10926 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
10927 case BFD_RELOC_MIPS_TLS_GOTTPREL:
10928 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
10929 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
10930 S_SET_THREAD_LOCAL (fixP->fx_addsy);
10931 /* fall through */
10932
10933 case BFD_RELOC_MIPS_JMP:
10934 case BFD_RELOC_MIPS_SHIFT5:
10935 case BFD_RELOC_MIPS_SHIFT6:
10936 case BFD_RELOC_MIPS_GOT_DISP:
10937 case BFD_RELOC_MIPS_GOT_PAGE:
10938 case BFD_RELOC_MIPS_GOT_OFST:
10939 case BFD_RELOC_MIPS_SUB:
10940 case BFD_RELOC_MIPS_INSERT_A:
10941 case BFD_RELOC_MIPS_INSERT_B:
10942 case BFD_RELOC_MIPS_DELETE:
10943 case BFD_RELOC_MIPS_HIGHEST:
10944 case BFD_RELOC_MIPS_HIGHER:
10945 case BFD_RELOC_MIPS_SCN_DISP:
10946 case BFD_RELOC_MIPS_REL16:
10947 case BFD_RELOC_MIPS_RELGOT:
10948 case BFD_RELOC_MIPS_JALR:
10949 case BFD_RELOC_HI16:
10950 case BFD_RELOC_HI16_S:
10951 case BFD_RELOC_GPREL16:
10952 case BFD_RELOC_MIPS_LITERAL:
10953 case BFD_RELOC_MIPS_CALL16:
10954 case BFD_RELOC_MIPS_GOT16:
10955 case BFD_RELOC_GPREL32:
10956 case BFD_RELOC_MIPS_GOT_HI16:
10957 case BFD_RELOC_MIPS_GOT_LO16:
10958 case BFD_RELOC_MIPS_CALL_HI16:
10959 case BFD_RELOC_MIPS_CALL_LO16:
10960 case BFD_RELOC_MIPS16_GPREL:
10961 case BFD_RELOC_MIPS16_HI16:
10962 case BFD_RELOC_MIPS16_HI16_S:
10963 assert (! fixP->fx_pcrel);
10964 /* Nothing needed to do. The value comes from the reloc entry */
10965 break;
10966
10967 case BFD_RELOC_MIPS16_JMP:
10968 /* We currently always generate a reloc against a symbol, which
10969 means that we don't want an addend even if the symbol is
10970 defined. */
10971 *valP = 0;
10972 break;
10973
10974 case BFD_RELOC_64:
10975 /* This is handled like BFD_RELOC_32, but we output a sign
10976 extended value if we are only 32 bits. */
10977 if (fixP->fx_done)
10978 {
10979 if (8 <= sizeof (valueT))
10980 md_number_to_chars ((char *) buf, *valP, 8);
10981 else
10982 {
10983 valueT hiv;
10984
10985 if ((*valP & 0x80000000) != 0)
10986 hiv = 0xffffffff;
10987 else
10988 hiv = 0;
10989 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
10990 *valP, 4);
10991 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
10992 hiv, 4);
10993 }
10994 }
10995 break;
10996
10997 case BFD_RELOC_RVA:
10998 case BFD_RELOC_32:
10999 /* If we are deleting this reloc entry, we must fill in the
11000 value now. This can happen if we have a .word which is not
11001 resolved when it appears but is later defined. */
11002 if (fixP->fx_done)
11003 md_number_to_chars ((char *) buf, *valP, 4);
11004 break;
11005
11006 case BFD_RELOC_16:
11007 /* If we are deleting this reloc entry, we must fill in the
11008 value now. */
11009 if (fixP->fx_done)
11010 md_number_to_chars ((char *) buf, *valP, 2);
11011 break;
11012
11013 case BFD_RELOC_LO16:
11014 case BFD_RELOC_MIPS16_LO16:
11015 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11016 may be safe to remove, but if so it's not obvious. */
11017 /* When handling an embedded PIC switch statement, we can wind
11018 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11019 if (fixP->fx_done)
11020 {
11021 if (*valP + 0x8000 > 0xffff)
11022 as_bad_where (fixP->fx_file, fixP->fx_line,
11023 _("relocation overflow"));
11024 if (target_big_endian)
11025 buf += 2;
11026 md_number_to_chars ((char *) buf, *valP, 2);
11027 }
11028 break;
11029
11030 case BFD_RELOC_16_PCREL_S2:
11031 if ((*valP & 0x3) != 0)
11032 as_bad_where (fixP->fx_file, fixP->fx_line,
11033 _("Branch to odd address (%lx)"), (long) *valP);
11034
11035 /*
11036 * We need to save the bits in the instruction since fixup_segment()
11037 * might be deleting the relocation entry (i.e., a branch within
11038 * the current segment).
11039 */
11040 if (! fixP->fx_done)
11041 break;
11042
11043 /* update old instruction data */
11044 if (target_big_endian)
11045 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11046 else
11047 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11048
11049 if (*valP + 0x20000 <= 0x3ffff)
11050 {
11051 insn |= (*valP >> 2) & 0xffff;
11052 md_number_to_chars ((char *) buf, insn, 4);
11053 }
11054 else if (mips_pic == NO_PIC
11055 && fixP->fx_done
11056 && fixP->fx_frag->fr_address >= text_section->vma
11057 && (fixP->fx_frag->fr_address
11058 < text_section->vma + bfd_get_section_size (text_section))
11059 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11060 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11061 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11062 {
11063 /* The branch offset is too large. If this is an
11064 unconditional branch, and we are not generating PIC code,
11065 we can convert it to an absolute jump instruction. */
11066 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11067 insn = 0x0c000000; /* jal */
11068 else
11069 insn = 0x08000000; /* j */
11070 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11071 fixP->fx_done = 0;
11072 fixP->fx_addsy = section_symbol (text_section);
11073 *valP += md_pcrel_from (fixP);
11074 md_number_to_chars ((char *) buf, insn, 4);
11075 }
11076 else
11077 {
11078 /* If we got here, we have branch-relaxation disabled,
11079 and there's nothing we can do to fix this instruction
11080 without turning it into a longer sequence. */
11081 as_bad_where (fixP->fx_file, fixP->fx_line,
11082 _("Branch out of range"));
11083 }
11084 break;
11085
11086 case BFD_RELOC_VTABLE_INHERIT:
11087 fixP->fx_done = 0;
11088 if (fixP->fx_addsy
11089 && !S_IS_DEFINED (fixP->fx_addsy)
11090 && !S_IS_WEAK (fixP->fx_addsy))
11091 S_SET_WEAK (fixP->fx_addsy);
11092 break;
11093
11094 case BFD_RELOC_VTABLE_ENTRY:
11095 fixP->fx_done = 0;
11096 break;
11097
11098 default:
11099 internalError ();
11100 }
11101
11102 /* Remember value for tc_gen_reloc. */
11103 fixP->fx_addnumber = *valP;
11104 }
11105
11106 static symbolS *
11107 get_symbol (void)
11108 {
11109 int c;
11110 char *name;
11111 symbolS *p;
11112
11113 name = input_line_pointer;
11114 c = get_symbol_end ();
11115 p = (symbolS *) symbol_find_or_make (name);
11116 *input_line_pointer = c;
11117 return p;
11118 }
11119
11120 /* Align the current frag to a given power of two. The MIPS assembler
11121 also automatically adjusts any preceding label. */
11122
11123 static void
11124 mips_align (int to, int fill, symbolS *label)
11125 {
11126 mips_emit_delays ();
11127 frag_align (to, fill, 0);
11128 record_alignment (now_seg, to);
11129 if (label != NULL)
11130 {
11131 assert (S_GET_SEGMENT (label) == now_seg);
11132 symbol_set_frag (label, frag_now);
11133 S_SET_VALUE (label, (valueT) frag_now_fix ());
11134 }
11135 }
11136
11137 /* Align to a given power of two. .align 0 turns off the automatic
11138 alignment used by the data creating pseudo-ops. */
11139
11140 static void
11141 s_align (int x ATTRIBUTE_UNUSED)
11142 {
11143 register int temp;
11144 register long temp_fill;
11145 long max_alignment = 15;
11146
11147 /*
11148
11149 o Note that the assembler pulls down any immediately preceding label
11150 to the aligned address.
11151 o It's not documented but auto alignment is reinstated by
11152 a .align pseudo instruction.
11153 o Note also that after auto alignment is turned off the mips assembler
11154 issues an error on attempt to assemble an improperly aligned data item.
11155 We don't.
11156
11157 */
11158
11159 temp = get_absolute_expression ();
11160 if (temp > max_alignment)
11161 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11162 else if (temp < 0)
11163 {
11164 as_warn (_("Alignment negative: 0 assumed."));
11165 temp = 0;
11166 }
11167 if (*input_line_pointer == ',')
11168 {
11169 ++input_line_pointer;
11170 temp_fill = get_absolute_expression ();
11171 }
11172 else
11173 temp_fill = 0;
11174 if (temp)
11175 {
11176 auto_align = 1;
11177 mips_align (temp, (int) temp_fill,
11178 insn_labels != NULL ? insn_labels->label : NULL);
11179 }
11180 else
11181 {
11182 auto_align = 0;
11183 }
11184
11185 demand_empty_rest_of_line ();
11186 }
11187
11188 static void
11189 s_change_sec (int sec)
11190 {
11191 segT seg;
11192
11193 #ifdef OBJ_ELF
11194 /* The ELF backend needs to know that we are changing sections, so
11195 that .previous works correctly. We could do something like check
11196 for an obj_section_change_hook macro, but that might be confusing
11197 as it would not be appropriate to use it in the section changing
11198 functions in read.c, since obj-elf.c intercepts those. FIXME:
11199 This should be cleaner, somehow. */
11200 obj_elf_section_change_hook ();
11201 #endif
11202
11203 mips_emit_delays ();
11204 switch (sec)
11205 {
11206 case 't':
11207 s_text (0);
11208 break;
11209 case 'd':
11210 s_data (0);
11211 break;
11212 case 'b':
11213 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11214 demand_empty_rest_of_line ();
11215 break;
11216
11217 case 'r':
11218 seg = subseg_new (RDATA_SECTION_NAME,
11219 (subsegT) get_absolute_expression ());
11220 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11221 {
11222 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11223 | SEC_READONLY | SEC_RELOC
11224 | SEC_DATA));
11225 if (strcmp (TARGET_OS, "elf") != 0)
11226 record_alignment (seg, 4);
11227 }
11228 demand_empty_rest_of_line ();
11229 break;
11230
11231 case 's':
11232 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11233 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11234 {
11235 bfd_set_section_flags (stdoutput, seg,
11236 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11237 if (strcmp (TARGET_OS, "elf") != 0)
11238 record_alignment (seg, 4);
11239 }
11240 demand_empty_rest_of_line ();
11241 break;
11242 }
11243
11244 auto_align = 1;
11245 }
11246
11247 void
11248 s_change_section (int ignore ATTRIBUTE_UNUSED)
11249 {
11250 #ifdef OBJ_ELF
11251 char *section_name;
11252 char c;
11253 char next_c = 0;
11254 int section_type;
11255 int section_flag;
11256 int section_entry_size;
11257 int section_alignment;
11258
11259 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11260 return;
11261
11262 section_name = input_line_pointer;
11263 c = get_symbol_end ();
11264 if (c)
11265 next_c = *(input_line_pointer + 1);
11266
11267 /* Do we have .section Name<,"flags">? */
11268 if (c != ',' || (c == ',' && next_c == '"'))
11269 {
11270 /* just after name is now '\0'. */
11271 *input_line_pointer = c;
11272 input_line_pointer = section_name;
11273 obj_elf_section (ignore);
11274 return;
11275 }
11276 input_line_pointer++;
11277
11278 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11279 if (c == ',')
11280 section_type = get_absolute_expression ();
11281 else
11282 section_type = 0;
11283 if (*input_line_pointer++ == ',')
11284 section_flag = get_absolute_expression ();
11285 else
11286 section_flag = 0;
11287 if (*input_line_pointer++ == ',')
11288 section_entry_size = get_absolute_expression ();
11289 else
11290 section_entry_size = 0;
11291 if (*input_line_pointer++ == ',')
11292 section_alignment = get_absolute_expression ();
11293 else
11294 section_alignment = 0;
11295
11296 section_name = xstrdup (section_name);
11297
11298 /* When using the generic form of .section (as implemented by obj-elf.c),
11299 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11300 traditionally had to fall back on the more common @progbits instead.
11301
11302 There's nothing really harmful in this, since bfd will correct
11303 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11304 means that, for backwards compatibiltiy, the special_section entries
11305 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11306
11307 Even so, we shouldn't force users of the MIPS .section syntax to
11308 incorrectly label the sections as SHT_PROGBITS. The best compromise
11309 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11310 generic type-checking code. */
11311 if (section_type == SHT_MIPS_DWARF)
11312 section_type = SHT_PROGBITS;
11313
11314 obj_elf_change_section (section_name, section_type, section_flag,
11315 section_entry_size, 0, 0, 0);
11316
11317 if (now_seg->name != section_name)
11318 free (section_name);
11319 #endif /* OBJ_ELF */
11320 }
11321
11322 void
11323 mips_enable_auto_align (void)
11324 {
11325 auto_align = 1;
11326 }
11327
11328 static void
11329 s_cons (int log_size)
11330 {
11331 symbolS *label;
11332
11333 label = insn_labels != NULL ? insn_labels->label : NULL;
11334 mips_emit_delays ();
11335 if (log_size > 0 && auto_align)
11336 mips_align (log_size, 0, label);
11337 mips_clear_insn_labels ();
11338 cons (1 << log_size);
11339 }
11340
11341 static void
11342 s_float_cons (int type)
11343 {
11344 symbolS *label;
11345
11346 label = insn_labels != NULL ? insn_labels->label : NULL;
11347
11348 mips_emit_delays ();
11349
11350 if (auto_align)
11351 {
11352 if (type == 'd')
11353 mips_align (3, 0, label);
11354 else
11355 mips_align (2, 0, label);
11356 }
11357
11358 mips_clear_insn_labels ();
11359
11360 float_cons (type);
11361 }
11362
11363 /* Handle .globl. We need to override it because on Irix 5 you are
11364 permitted to say
11365 .globl foo .text
11366 where foo is an undefined symbol, to mean that foo should be
11367 considered to be the address of a function. */
11368
11369 static void
11370 s_mips_globl (int x ATTRIBUTE_UNUSED)
11371 {
11372 char *name;
11373 int c;
11374 symbolS *symbolP;
11375 flagword flag;
11376
11377 name = input_line_pointer;
11378 c = get_symbol_end ();
11379 symbolP = symbol_find_or_make (name);
11380 *input_line_pointer = c;
11381 SKIP_WHITESPACE ();
11382
11383 /* On Irix 5, every global symbol that is not explicitly labelled as
11384 being a function is apparently labelled as being an object. */
11385 flag = BSF_OBJECT;
11386
11387 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11388 {
11389 char *secname;
11390 asection *sec;
11391
11392 secname = input_line_pointer;
11393 c = get_symbol_end ();
11394 sec = bfd_get_section_by_name (stdoutput, secname);
11395 if (sec == NULL)
11396 as_bad (_("%s: no such section"), secname);
11397 *input_line_pointer = c;
11398
11399 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11400 flag = BSF_FUNCTION;
11401 }
11402
11403 symbol_get_bfdsym (symbolP)->flags |= flag;
11404
11405 S_SET_EXTERNAL (symbolP);
11406 demand_empty_rest_of_line ();
11407 }
11408
11409 static void
11410 s_option (int x ATTRIBUTE_UNUSED)
11411 {
11412 char *opt;
11413 char c;
11414
11415 opt = input_line_pointer;
11416 c = get_symbol_end ();
11417
11418 if (*opt == 'O')
11419 {
11420 /* FIXME: What does this mean? */
11421 }
11422 else if (strncmp (opt, "pic", 3) == 0)
11423 {
11424 int i;
11425
11426 i = atoi (opt + 3);
11427 if (i == 0)
11428 mips_pic = NO_PIC;
11429 else if (i == 2)
11430 {
11431 mips_pic = SVR4_PIC;
11432 mips_abicalls = TRUE;
11433 }
11434 else
11435 as_bad (_(".option pic%d not supported"), i);
11436
11437 if (mips_pic == SVR4_PIC)
11438 {
11439 if (g_switch_seen && g_switch_value != 0)
11440 as_warn (_("-G may not be used with SVR4 PIC code"));
11441 g_switch_value = 0;
11442 bfd_set_gp_size (stdoutput, 0);
11443 }
11444 }
11445 else
11446 as_warn (_("Unrecognized option \"%s\""), opt);
11447
11448 *input_line_pointer = c;
11449 demand_empty_rest_of_line ();
11450 }
11451
11452 /* This structure is used to hold a stack of .set values. */
11453
11454 struct mips_option_stack
11455 {
11456 struct mips_option_stack *next;
11457 struct mips_set_options options;
11458 };
11459
11460 static struct mips_option_stack *mips_opts_stack;
11461
11462 /* Handle the .set pseudo-op. */
11463
11464 static void
11465 s_mipsset (int x ATTRIBUTE_UNUSED)
11466 {
11467 char *name = input_line_pointer, ch;
11468
11469 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11470 ++input_line_pointer;
11471 ch = *input_line_pointer;
11472 *input_line_pointer = '\0';
11473
11474 if (strcmp (name, "reorder") == 0)
11475 {
11476 if (mips_opts.noreorder)
11477 end_noreorder ();
11478 }
11479 else if (strcmp (name, "noreorder") == 0)
11480 {
11481 if (!mips_opts.noreorder)
11482 start_noreorder ();
11483 }
11484 else if (strcmp (name, "at") == 0)
11485 {
11486 mips_opts.noat = 0;
11487 }
11488 else if (strcmp (name, "noat") == 0)
11489 {
11490 mips_opts.noat = 1;
11491 }
11492 else if (strcmp (name, "macro") == 0)
11493 {
11494 mips_opts.warn_about_macros = 0;
11495 }
11496 else if (strcmp (name, "nomacro") == 0)
11497 {
11498 if (mips_opts.noreorder == 0)
11499 as_bad (_("`noreorder' must be set before `nomacro'"));
11500 mips_opts.warn_about_macros = 1;
11501 }
11502 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11503 {
11504 mips_opts.nomove = 0;
11505 }
11506 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11507 {
11508 mips_opts.nomove = 1;
11509 }
11510 else if (strcmp (name, "bopt") == 0)
11511 {
11512 mips_opts.nobopt = 0;
11513 }
11514 else if (strcmp (name, "nobopt") == 0)
11515 {
11516 mips_opts.nobopt = 1;
11517 }
11518 else if (strcmp (name, "mips16") == 0
11519 || strcmp (name, "MIPS-16") == 0)
11520 mips_opts.mips16 = 1;
11521 else if (strcmp (name, "nomips16") == 0
11522 || strcmp (name, "noMIPS-16") == 0)
11523 mips_opts.mips16 = 0;
11524 else if (strcmp (name, "mips3d") == 0)
11525 mips_opts.ase_mips3d = 1;
11526 else if (strcmp (name, "nomips3d") == 0)
11527 mips_opts.ase_mips3d = 0;
11528 else if (strcmp (name, "mdmx") == 0)
11529 mips_opts.ase_mdmx = 1;
11530 else if (strcmp (name, "nomdmx") == 0)
11531 mips_opts.ase_mdmx = 0;
11532 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11533 {
11534 int reset = 0;
11535
11536 /* Permit the user to change the ISA and architecture on the fly.
11537 Needless to say, misuse can cause serious problems. */
11538 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11539 {
11540 reset = 1;
11541 mips_opts.isa = file_mips_isa;
11542 mips_opts.arch = file_mips_arch;
11543 }
11544 else if (strncmp (name, "arch=", 5) == 0)
11545 {
11546 const struct mips_cpu_info *p;
11547
11548 p = mips_parse_cpu("internal use", name + 5);
11549 if (!p)
11550 as_bad (_("unknown architecture %s"), name + 5);
11551 else
11552 {
11553 mips_opts.arch = p->cpu;
11554 mips_opts.isa = p->isa;
11555 }
11556 }
11557 else if (strncmp (name, "mips", 4) == 0)
11558 {
11559 const struct mips_cpu_info *p;
11560
11561 p = mips_parse_cpu("internal use", name);
11562 if (!p)
11563 as_bad (_("unknown ISA level %s"), name + 4);
11564 else
11565 {
11566 mips_opts.arch = p->cpu;
11567 mips_opts.isa = p->isa;
11568 }
11569 }
11570 else
11571 as_bad (_("unknown ISA or architecture %s"), name);
11572
11573 switch (mips_opts.isa)
11574 {
11575 case 0:
11576 break;
11577 case ISA_MIPS1:
11578 case ISA_MIPS2:
11579 case ISA_MIPS32:
11580 case ISA_MIPS32R2:
11581 mips_opts.gp32 = 1;
11582 mips_opts.fp32 = 1;
11583 break;
11584 case ISA_MIPS3:
11585 case ISA_MIPS4:
11586 case ISA_MIPS5:
11587 case ISA_MIPS64:
11588 case ISA_MIPS64R2:
11589 mips_opts.gp32 = 0;
11590 mips_opts.fp32 = 0;
11591 break;
11592 default:
11593 as_bad (_("unknown ISA level %s"), name + 4);
11594 break;
11595 }
11596 if (reset)
11597 {
11598 mips_opts.gp32 = file_mips_gp32;
11599 mips_opts.fp32 = file_mips_fp32;
11600 }
11601 }
11602 else if (strcmp (name, "autoextend") == 0)
11603 mips_opts.noautoextend = 0;
11604 else if (strcmp (name, "noautoextend") == 0)
11605 mips_opts.noautoextend = 1;
11606 else if (strcmp (name, "push") == 0)
11607 {
11608 struct mips_option_stack *s;
11609
11610 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11611 s->next = mips_opts_stack;
11612 s->options = mips_opts;
11613 mips_opts_stack = s;
11614 }
11615 else if (strcmp (name, "pop") == 0)
11616 {
11617 struct mips_option_stack *s;
11618
11619 s = mips_opts_stack;
11620 if (s == NULL)
11621 as_bad (_(".set pop with no .set push"));
11622 else
11623 {
11624 /* If we're changing the reorder mode we need to handle
11625 delay slots correctly. */
11626 if (s->options.noreorder && ! mips_opts.noreorder)
11627 start_noreorder ();
11628 else if (! s->options.noreorder && mips_opts.noreorder)
11629 end_noreorder ();
11630
11631 mips_opts = s->options;
11632 mips_opts_stack = s->next;
11633 free (s);
11634 }
11635 }
11636 else if (strcmp (name, "sym32") == 0)
11637 mips_opts.sym32 = TRUE;
11638 else if (strcmp (name, "nosym32") == 0)
11639 mips_opts.sym32 = FALSE;
11640 else
11641 {
11642 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11643 }
11644 *input_line_pointer = ch;
11645 demand_empty_rest_of_line ();
11646 }
11647
11648 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11649 .option pic2. It means to generate SVR4 PIC calls. */
11650
11651 static void
11652 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11653 {
11654 mips_pic = SVR4_PIC;
11655 mips_abicalls = TRUE;
11656
11657 if (g_switch_seen && g_switch_value != 0)
11658 as_warn (_("-G may not be used with SVR4 PIC code"));
11659 g_switch_value = 0;
11660
11661 bfd_set_gp_size (stdoutput, 0);
11662 demand_empty_rest_of_line ();
11663 }
11664
11665 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11666 PIC code. It sets the $gp register for the function based on the
11667 function address, which is in the register named in the argument.
11668 This uses a relocation against _gp_disp, which is handled specially
11669 by the linker. The result is:
11670 lui $gp,%hi(_gp_disp)
11671 addiu $gp,$gp,%lo(_gp_disp)
11672 addu $gp,$gp,.cpload argument
11673 The .cpload argument is normally $25 == $t9.
11674
11675 The -mno-shared option changes this to:
11676 lui $gp,%hi(__gnu_local_gp)
11677 addiu $gp,$gp,%lo(__gnu_local_gp)
11678 and the argument is ignored. This saves an instruction, but the
11679 resulting code is not position independent; it uses an absolute
11680 address for __gnu_local_gp. Thus code assembled with -mno-shared
11681 can go into an ordinary executable, but not into a shared library. */
11682
11683 static void
11684 s_cpload (int ignore ATTRIBUTE_UNUSED)
11685 {
11686 expressionS ex;
11687 int reg;
11688 int in_shared;
11689
11690 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11691 .cpload is ignored. */
11692 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11693 {
11694 s_ignore (0);
11695 return;
11696 }
11697
11698 /* .cpload should be in a .set noreorder section. */
11699 if (mips_opts.noreorder == 0)
11700 as_warn (_(".cpload not in noreorder section"));
11701
11702 reg = tc_get_register (0);
11703
11704 /* If we need to produce a 64-bit address, we are better off using
11705 the default instruction sequence. */
11706 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11707
11708 ex.X_op = O_symbol;
11709 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11710 "__gnu_local_gp");
11711 ex.X_op_symbol = NULL;
11712 ex.X_add_number = 0;
11713
11714 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11715 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11716
11717 macro_start ();
11718 macro_build_lui (&ex, mips_gp_register);
11719 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11720 mips_gp_register, BFD_RELOC_LO16);
11721 if (in_shared)
11722 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11723 mips_gp_register, reg);
11724 macro_end ();
11725
11726 demand_empty_rest_of_line ();
11727 }
11728
11729 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11730 .cpsetup $reg1, offset|$reg2, label
11731
11732 If offset is given, this results in:
11733 sd $gp, offset($sp)
11734 lui $gp, %hi(%neg(%gp_rel(label)))
11735 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11736 daddu $gp, $gp, $reg1
11737
11738 If $reg2 is given, this results in:
11739 daddu $reg2, $gp, $0
11740 lui $gp, %hi(%neg(%gp_rel(label)))
11741 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11742 daddu $gp, $gp, $reg1
11743 $reg1 is normally $25 == $t9.
11744
11745 The -mno-shared option replaces the last three instructions with
11746 lui $gp,%hi(_gp)
11747 addiu $gp,$gp,%lo(_gp)
11748 */
11749
11750 static void
11751 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11752 {
11753 expressionS ex_off;
11754 expressionS ex_sym;
11755 int reg1;
11756
11757 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11758 We also need NewABI support. */
11759 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11760 {
11761 s_ignore (0);
11762 return;
11763 }
11764
11765 reg1 = tc_get_register (0);
11766 SKIP_WHITESPACE ();
11767 if (*input_line_pointer != ',')
11768 {
11769 as_bad (_("missing argument separator ',' for .cpsetup"));
11770 return;
11771 }
11772 else
11773 ++input_line_pointer;
11774 SKIP_WHITESPACE ();
11775 if (*input_line_pointer == '$')
11776 {
11777 mips_cpreturn_register = tc_get_register (0);
11778 mips_cpreturn_offset = -1;
11779 }
11780 else
11781 {
11782 mips_cpreturn_offset = get_absolute_expression ();
11783 mips_cpreturn_register = -1;
11784 }
11785 SKIP_WHITESPACE ();
11786 if (*input_line_pointer != ',')
11787 {
11788 as_bad (_("missing argument separator ',' for .cpsetup"));
11789 return;
11790 }
11791 else
11792 ++input_line_pointer;
11793 SKIP_WHITESPACE ();
11794 expression (&ex_sym);
11795
11796 macro_start ();
11797 if (mips_cpreturn_register == -1)
11798 {
11799 ex_off.X_op = O_constant;
11800 ex_off.X_add_symbol = NULL;
11801 ex_off.X_op_symbol = NULL;
11802 ex_off.X_add_number = mips_cpreturn_offset;
11803
11804 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11805 BFD_RELOC_LO16, SP);
11806 }
11807 else
11808 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11809 mips_gp_register, 0);
11810
11811 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11812 {
11813 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11814 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11815 BFD_RELOC_HI16_S);
11816
11817 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11818 mips_gp_register, -1, BFD_RELOC_GPREL16,
11819 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11820
11821 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11822 mips_gp_register, reg1);
11823 }
11824 else
11825 {
11826 expressionS ex;
11827
11828 ex.X_op = O_symbol;
11829 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
11830 ex.X_op_symbol = NULL;
11831 ex.X_add_number = 0;
11832
11833 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11834 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11835
11836 macro_build_lui (&ex, mips_gp_register);
11837 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11838 mips_gp_register, BFD_RELOC_LO16);
11839 }
11840
11841 macro_end ();
11842
11843 demand_empty_rest_of_line ();
11844 }
11845
11846 static void
11847 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11848 {
11849 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11850 .cplocal is ignored. */
11851 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11852 {
11853 s_ignore (0);
11854 return;
11855 }
11856
11857 mips_gp_register = tc_get_register (0);
11858 demand_empty_rest_of_line ();
11859 }
11860
11861 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11862 offset from $sp. The offset is remembered, and after making a PIC
11863 call $gp is restored from that location. */
11864
11865 static void
11866 s_cprestore (int ignore ATTRIBUTE_UNUSED)
11867 {
11868 expressionS ex;
11869
11870 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11871 .cprestore is ignored. */
11872 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11873 {
11874 s_ignore (0);
11875 return;
11876 }
11877
11878 mips_cprestore_offset = get_absolute_expression ();
11879 mips_cprestore_valid = 1;
11880
11881 ex.X_op = O_constant;
11882 ex.X_add_symbol = NULL;
11883 ex.X_op_symbol = NULL;
11884 ex.X_add_number = mips_cprestore_offset;
11885
11886 macro_start ();
11887 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
11888 SP, HAVE_64BIT_ADDRESSES);
11889 macro_end ();
11890
11891 demand_empty_rest_of_line ();
11892 }
11893
11894 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11895 was given in the preceding .cpsetup, it results in:
11896 ld $gp, offset($sp)
11897
11898 If a register $reg2 was given there, it results in:
11899 daddu $gp, $reg2, $0
11900 */
11901 static void
11902 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
11903 {
11904 expressionS ex;
11905
11906 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11907 We also need NewABI support. */
11908 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11909 {
11910 s_ignore (0);
11911 return;
11912 }
11913
11914 macro_start ();
11915 if (mips_cpreturn_register == -1)
11916 {
11917 ex.X_op = O_constant;
11918 ex.X_add_symbol = NULL;
11919 ex.X_op_symbol = NULL;
11920 ex.X_add_number = mips_cpreturn_offset;
11921
11922 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
11923 }
11924 else
11925 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
11926 mips_cpreturn_register, 0);
11927 macro_end ();
11928
11929 demand_empty_rest_of_line ();
11930 }
11931
11932 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11933 code. It sets the offset to use in gp_rel relocations. */
11934
11935 static void
11936 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
11937 {
11938 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11939 We also need NewABI support. */
11940 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11941 {
11942 s_ignore (0);
11943 return;
11944 }
11945
11946 mips_gprel_offset = get_absolute_expression ();
11947
11948 demand_empty_rest_of_line ();
11949 }
11950
11951 /* Handle the .gpword pseudo-op. This is used when generating PIC
11952 code. It generates a 32 bit GP relative reloc. */
11953
11954 static void
11955 s_gpword (int ignore ATTRIBUTE_UNUSED)
11956 {
11957 symbolS *label;
11958 expressionS ex;
11959 char *p;
11960
11961 /* When not generating PIC code, this is treated as .word. */
11962 if (mips_pic != SVR4_PIC)
11963 {
11964 s_cons (2);
11965 return;
11966 }
11967
11968 label = insn_labels != NULL ? insn_labels->label : NULL;
11969 mips_emit_delays ();
11970 if (auto_align)
11971 mips_align (2, 0, label);
11972 mips_clear_insn_labels ();
11973
11974 expression (&ex);
11975
11976 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11977 {
11978 as_bad (_("Unsupported use of .gpword"));
11979 ignore_rest_of_line ();
11980 }
11981
11982 p = frag_more (4);
11983 md_number_to_chars (p, 0, 4);
11984 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
11985 BFD_RELOC_GPREL32);
11986
11987 demand_empty_rest_of_line ();
11988 }
11989
11990 static void
11991 s_gpdword (int ignore ATTRIBUTE_UNUSED)
11992 {
11993 symbolS *label;
11994 expressionS ex;
11995 char *p;
11996
11997 /* When not generating PIC code, this is treated as .dword. */
11998 if (mips_pic != SVR4_PIC)
11999 {
12000 s_cons (3);
12001 return;
12002 }
12003
12004 label = insn_labels != NULL ? insn_labels->label : NULL;
12005 mips_emit_delays ();
12006 if (auto_align)
12007 mips_align (3, 0, label);
12008 mips_clear_insn_labels ();
12009
12010 expression (&ex);
12011
12012 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12013 {
12014 as_bad (_("Unsupported use of .gpdword"));
12015 ignore_rest_of_line ();
12016 }
12017
12018 p = frag_more (8);
12019 md_number_to_chars (p, 0, 8);
12020 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12021 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12022
12023 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12024 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12025 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12026
12027 demand_empty_rest_of_line ();
12028 }
12029
12030 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12031 tables in SVR4 PIC code. */
12032
12033 static void
12034 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12035 {
12036 int reg;
12037
12038 /* This is ignored when not generating SVR4 PIC code. */
12039 if (mips_pic != SVR4_PIC)
12040 {
12041 s_ignore (0);
12042 return;
12043 }
12044
12045 /* Add $gp to the register named as an argument. */
12046 macro_start ();
12047 reg = tc_get_register (0);
12048 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12049 macro_end ();
12050
12051 demand_empty_rest_of_line ();
12052 }
12053
12054 /* Handle the .insn pseudo-op. This marks instruction labels in
12055 mips16 mode. This permits the linker to handle them specially,
12056 such as generating jalx instructions when needed. We also make
12057 them odd for the duration of the assembly, in order to generate the
12058 right sort of code. We will make them even in the adjust_symtab
12059 routine, while leaving them marked. This is convenient for the
12060 debugger and the disassembler. The linker knows to make them odd
12061 again. */
12062
12063 static void
12064 s_insn (int ignore ATTRIBUTE_UNUSED)
12065 {
12066 mips16_mark_labels ();
12067
12068 demand_empty_rest_of_line ();
12069 }
12070
12071 /* Handle a .stabn directive. We need these in order to mark a label
12072 as being a mips16 text label correctly. Sometimes the compiler
12073 will emit a label, followed by a .stabn, and then switch sections.
12074 If the label and .stabn are in mips16 mode, then the label is
12075 really a mips16 text label. */
12076
12077 static void
12078 s_mips_stab (int type)
12079 {
12080 if (type == 'n')
12081 mips16_mark_labels ();
12082
12083 s_stab (type);
12084 }
12085
12086 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12087 */
12088
12089 static void
12090 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12091 {
12092 char *name;
12093 int c;
12094 symbolS *symbolP;
12095 expressionS exp;
12096
12097 name = input_line_pointer;
12098 c = get_symbol_end ();
12099 symbolP = symbol_find_or_make (name);
12100 S_SET_WEAK (symbolP);
12101 *input_line_pointer = c;
12102
12103 SKIP_WHITESPACE ();
12104
12105 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12106 {
12107 if (S_IS_DEFINED (symbolP))
12108 {
12109 as_bad ("ignoring attempt to redefine symbol %s",
12110 S_GET_NAME (symbolP));
12111 ignore_rest_of_line ();
12112 return;
12113 }
12114
12115 if (*input_line_pointer == ',')
12116 {
12117 ++input_line_pointer;
12118 SKIP_WHITESPACE ();
12119 }
12120
12121 expression (&exp);
12122 if (exp.X_op != O_symbol)
12123 {
12124 as_bad ("bad .weakext directive");
12125 ignore_rest_of_line ();
12126 return;
12127 }
12128 symbol_set_value_expression (symbolP, &exp);
12129 }
12130
12131 demand_empty_rest_of_line ();
12132 }
12133
12134 /* Parse a register string into a number. Called from the ECOFF code
12135 to parse .frame. The argument is non-zero if this is the frame
12136 register, so that we can record it in mips_frame_reg. */
12137
12138 int
12139 tc_get_register (int frame)
12140 {
12141 int reg;
12142
12143 SKIP_WHITESPACE ();
12144 if (*input_line_pointer++ != '$')
12145 {
12146 as_warn (_("expected `$'"));
12147 reg = ZERO;
12148 }
12149 else if (ISDIGIT (*input_line_pointer))
12150 {
12151 reg = get_absolute_expression ();
12152 if (reg < 0 || reg >= 32)
12153 {
12154 as_warn (_("Bad register number"));
12155 reg = ZERO;
12156 }
12157 }
12158 else
12159 {
12160 if (strncmp (input_line_pointer, "ra", 2) == 0)
12161 {
12162 reg = RA;
12163 input_line_pointer += 2;
12164 }
12165 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12166 {
12167 reg = FP;
12168 input_line_pointer += 2;
12169 }
12170 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12171 {
12172 reg = SP;
12173 input_line_pointer += 2;
12174 }
12175 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12176 {
12177 reg = GP;
12178 input_line_pointer += 2;
12179 }
12180 else if (strncmp (input_line_pointer, "at", 2) == 0)
12181 {
12182 reg = AT;
12183 input_line_pointer += 2;
12184 }
12185 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12186 {
12187 reg = KT0;
12188 input_line_pointer += 3;
12189 }
12190 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12191 {
12192 reg = KT1;
12193 input_line_pointer += 3;
12194 }
12195 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12196 {
12197 reg = ZERO;
12198 input_line_pointer += 4;
12199 }
12200 else
12201 {
12202 as_warn (_("Unrecognized register name"));
12203 reg = ZERO;
12204 while (ISALNUM(*input_line_pointer))
12205 input_line_pointer++;
12206 }
12207 }
12208 if (frame)
12209 {
12210 mips_frame_reg = reg != 0 ? reg : SP;
12211 mips_frame_reg_valid = 1;
12212 mips_cprestore_valid = 0;
12213 }
12214 return reg;
12215 }
12216
12217 valueT
12218 md_section_align (asection *seg, valueT addr)
12219 {
12220 int align = bfd_get_section_alignment (stdoutput, seg);
12221
12222 #ifdef OBJ_ELF
12223 /* We don't need to align ELF sections to the full alignment.
12224 However, Irix 5 may prefer that we align them at least to a 16
12225 byte boundary. We don't bother to align the sections if we are
12226 targeted for an embedded system. */
12227 if (strcmp (TARGET_OS, "elf") == 0)
12228 return addr;
12229 if (align > 4)
12230 align = 4;
12231 #endif
12232
12233 return ((addr + (1 << align) - 1) & (-1 << align));
12234 }
12235
12236 /* Utility routine, called from above as well. If called while the
12237 input file is still being read, it's only an approximation. (For
12238 example, a symbol may later become defined which appeared to be
12239 undefined earlier.) */
12240
12241 static int
12242 nopic_need_relax (symbolS *sym, int before_relaxing)
12243 {
12244 if (sym == 0)
12245 return 0;
12246
12247 if (g_switch_value > 0)
12248 {
12249 const char *symname;
12250 int change;
12251
12252 /* Find out whether this symbol can be referenced off the $gp
12253 register. It can be if it is smaller than the -G size or if
12254 it is in the .sdata or .sbss section. Certain symbols can
12255 not be referenced off the $gp, although it appears as though
12256 they can. */
12257 symname = S_GET_NAME (sym);
12258 if (symname != (const char *) NULL
12259 && (strcmp (symname, "eprol") == 0
12260 || strcmp (symname, "etext") == 0
12261 || strcmp (symname, "_gp") == 0
12262 || strcmp (symname, "edata") == 0
12263 || strcmp (symname, "_fbss") == 0
12264 || strcmp (symname, "_fdata") == 0
12265 || strcmp (symname, "_ftext") == 0
12266 || strcmp (symname, "end") == 0
12267 || strcmp (symname, "_gp_disp") == 0))
12268 change = 1;
12269 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12270 && (0
12271 #ifndef NO_ECOFF_DEBUGGING
12272 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12273 && (symbol_get_obj (sym)->ecoff_extern_size
12274 <= g_switch_value))
12275 #endif
12276 /* We must defer this decision until after the whole
12277 file has been read, since there might be a .extern
12278 after the first use of this symbol. */
12279 || (before_relaxing
12280 #ifndef NO_ECOFF_DEBUGGING
12281 && symbol_get_obj (sym)->ecoff_extern_size == 0
12282 #endif
12283 && S_GET_VALUE (sym) == 0)
12284 || (S_GET_VALUE (sym) != 0
12285 && S_GET_VALUE (sym) <= g_switch_value)))
12286 change = 0;
12287 else
12288 {
12289 const char *segname;
12290
12291 segname = segment_name (S_GET_SEGMENT (sym));
12292 assert (strcmp (segname, ".lit8") != 0
12293 && strcmp (segname, ".lit4") != 0);
12294 change = (strcmp (segname, ".sdata") != 0
12295 && strcmp (segname, ".sbss") != 0
12296 && strncmp (segname, ".sdata.", 7) != 0
12297 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12298 }
12299 return change;
12300 }
12301 else
12302 /* We are not optimizing for the $gp register. */
12303 return 1;
12304 }
12305
12306
12307 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12308
12309 static bfd_boolean
12310 pic_need_relax (symbolS *sym, asection *segtype)
12311 {
12312 asection *symsec;
12313 bfd_boolean linkonce;
12314
12315 /* Handle the case of a symbol equated to another symbol. */
12316 while (symbol_equated_reloc_p (sym))
12317 {
12318 symbolS *n;
12319
12320 /* It's possible to get a loop here in a badly written
12321 program. */
12322 n = symbol_get_value_expression (sym)->X_add_symbol;
12323 if (n == sym)
12324 break;
12325 sym = n;
12326 }
12327
12328 symsec = S_GET_SEGMENT (sym);
12329
12330 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12331 linkonce = FALSE;
12332 if (symsec != segtype && ! S_IS_LOCAL (sym))
12333 {
12334 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12335 != 0)
12336 linkonce = TRUE;
12337
12338 /* The GNU toolchain uses an extension for ELF: a section
12339 beginning with the magic string .gnu.linkonce is a linkonce
12340 section. */
12341 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12342 sizeof ".gnu.linkonce" - 1) == 0)
12343 linkonce = TRUE;
12344 }
12345
12346 /* This must duplicate the test in adjust_reloc_syms. */
12347 return (symsec != &bfd_und_section
12348 && symsec != &bfd_abs_section
12349 && ! bfd_is_com_section (symsec)
12350 && !linkonce
12351 #ifdef OBJ_ELF
12352 /* A global or weak symbol is treated as external. */
12353 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12354 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12355 #endif
12356 );
12357 }
12358
12359
12360 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12361 extended opcode. SEC is the section the frag is in. */
12362
12363 static int
12364 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12365 {
12366 int type;
12367 register const struct mips16_immed_operand *op;
12368 offsetT val;
12369 int mintiny, maxtiny;
12370 segT symsec;
12371 fragS *sym_frag;
12372
12373 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12374 return 0;
12375 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12376 return 1;
12377
12378 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12379 op = mips16_immed_operands;
12380 while (op->type != type)
12381 {
12382 ++op;
12383 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12384 }
12385
12386 if (op->unsp)
12387 {
12388 if (type == '<' || type == '>' || type == '[' || type == ']')
12389 {
12390 mintiny = 1;
12391 maxtiny = 1 << op->nbits;
12392 }
12393 else
12394 {
12395 mintiny = 0;
12396 maxtiny = (1 << op->nbits) - 1;
12397 }
12398 }
12399 else
12400 {
12401 mintiny = - (1 << (op->nbits - 1));
12402 maxtiny = (1 << (op->nbits - 1)) - 1;
12403 }
12404
12405 sym_frag = symbol_get_frag (fragp->fr_symbol);
12406 val = S_GET_VALUE (fragp->fr_symbol);
12407 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12408
12409 if (op->pcrel)
12410 {
12411 addressT addr;
12412
12413 /* We won't have the section when we are called from
12414 mips_relax_frag. However, we will always have been called
12415 from md_estimate_size_before_relax first. If this is a
12416 branch to a different section, we mark it as such. If SEC is
12417 NULL, and the frag is not marked, then it must be a branch to
12418 the same section. */
12419 if (sec == NULL)
12420 {
12421 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12422 return 1;
12423 }
12424 else
12425 {
12426 /* Must have been called from md_estimate_size_before_relax. */
12427 if (symsec != sec)
12428 {
12429 fragp->fr_subtype =
12430 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12431
12432 /* FIXME: We should support this, and let the linker
12433 catch branches and loads that are out of range. */
12434 as_bad_where (fragp->fr_file, fragp->fr_line,
12435 _("unsupported PC relative reference to different section"));
12436
12437 return 1;
12438 }
12439 if (fragp != sym_frag && sym_frag->fr_address == 0)
12440 /* Assume non-extended on the first relaxation pass.
12441 The address we have calculated will be bogus if this is
12442 a forward branch to another frag, as the forward frag
12443 will have fr_address == 0. */
12444 return 0;
12445 }
12446
12447 /* In this case, we know for sure that the symbol fragment is in
12448 the same section. If the relax_marker of the symbol fragment
12449 differs from the relax_marker of this fragment, we have not
12450 yet adjusted the symbol fragment fr_address. We want to add
12451 in STRETCH in order to get a better estimate of the address.
12452 This particularly matters because of the shift bits. */
12453 if (stretch != 0
12454 && sym_frag->relax_marker != fragp->relax_marker)
12455 {
12456 fragS *f;
12457
12458 /* Adjust stretch for any alignment frag. Note that if have
12459 been expanding the earlier code, the symbol may be
12460 defined in what appears to be an earlier frag. FIXME:
12461 This doesn't handle the fr_subtype field, which specifies
12462 a maximum number of bytes to skip when doing an
12463 alignment. */
12464 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12465 {
12466 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12467 {
12468 if (stretch < 0)
12469 stretch = - ((- stretch)
12470 & ~ ((1 << (int) f->fr_offset) - 1));
12471 else
12472 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12473 if (stretch == 0)
12474 break;
12475 }
12476 }
12477 if (f != NULL)
12478 val += stretch;
12479 }
12480
12481 addr = fragp->fr_address + fragp->fr_fix;
12482
12483 /* The base address rules are complicated. The base address of
12484 a branch is the following instruction. The base address of a
12485 PC relative load or add is the instruction itself, but if it
12486 is in a delay slot (in which case it can not be extended) use
12487 the address of the instruction whose delay slot it is in. */
12488 if (type == 'p' || type == 'q')
12489 {
12490 addr += 2;
12491
12492 /* If we are currently assuming that this frag should be
12493 extended, then, the current address is two bytes
12494 higher. */
12495 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12496 addr += 2;
12497
12498 /* Ignore the low bit in the target, since it will be set
12499 for a text label. */
12500 if ((val & 1) != 0)
12501 --val;
12502 }
12503 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12504 addr -= 4;
12505 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12506 addr -= 2;
12507
12508 val -= addr & ~ ((1 << op->shift) - 1);
12509
12510 /* Branch offsets have an implicit 0 in the lowest bit. */
12511 if (type == 'p' || type == 'q')
12512 val /= 2;
12513
12514 /* If any of the shifted bits are set, we must use an extended
12515 opcode. If the address depends on the size of this
12516 instruction, this can lead to a loop, so we arrange to always
12517 use an extended opcode. We only check this when we are in
12518 the main relaxation loop, when SEC is NULL. */
12519 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12520 {
12521 fragp->fr_subtype =
12522 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12523 return 1;
12524 }
12525
12526 /* If we are about to mark a frag as extended because the value
12527 is precisely maxtiny + 1, then there is a chance of an
12528 infinite loop as in the following code:
12529 la $4,foo
12530 .skip 1020
12531 .align 2
12532 foo:
12533 In this case when the la is extended, foo is 0x3fc bytes
12534 away, so the la can be shrunk, but then foo is 0x400 away, so
12535 the la must be extended. To avoid this loop, we mark the
12536 frag as extended if it was small, and is about to become
12537 extended with a value of maxtiny + 1. */
12538 if (val == ((maxtiny + 1) << op->shift)
12539 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12540 && sec == NULL)
12541 {
12542 fragp->fr_subtype =
12543 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12544 return 1;
12545 }
12546 }
12547 else if (symsec != absolute_section && sec != NULL)
12548 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12549
12550 if ((val & ((1 << op->shift) - 1)) != 0
12551 || val < (mintiny << op->shift)
12552 || val > (maxtiny << op->shift))
12553 return 1;
12554 else
12555 return 0;
12556 }
12557
12558 /* Compute the length of a branch sequence, and adjust the
12559 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12560 worst-case length is computed, with UPDATE being used to indicate
12561 whether an unconditional (-1), branch-likely (+1) or regular (0)
12562 branch is to be computed. */
12563 static int
12564 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12565 {
12566 bfd_boolean toofar;
12567 int length;
12568
12569 if (fragp
12570 && S_IS_DEFINED (fragp->fr_symbol)
12571 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12572 {
12573 addressT addr;
12574 offsetT val;
12575
12576 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12577
12578 addr = fragp->fr_address + fragp->fr_fix + 4;
12579
12580 val -= addr;
12581
12582 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12583 }
12584 else if (fragp)
12585 /* If the symbol is not defined or it's in a different segment,
12586 assume the user knows what's going on and emit a short
12587 branch. */
12588 toofar = FALSE;
12589 else
12590 toofar = TRUE;
12591
12592 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12593 fragp->fr_subtype
12594 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12595 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12596 RELAX_BRANCH_LINK (fragp->fr_subtype),
12597 toofar);
12598
12599 length = 4;
12600 if (toofar)
12601 {
12602 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12603 length += 8;
12604
12605 if (mips_pic != NO_PIC)
12606 {
12607 /* Additional space for PIC loading of target address. */
12608 length += 8;
12609 if (mips_opts.isa == ISA_MIPS1)
12610 /* Additional space for $at-stabilizing nop. */
12611 length += 4;
12612 }
12613
12614 /* If branch is conditional. */
12615 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12616 length += 8;
12617 }
12618
12619 return length;
12620 }
12621
12622 /* Estimate the size of a frag before relaxing. Unless this is the
12623 mips16, we are not really relaxing here, and the final size is
12624 encoded in the subtype information. For the mips16, we have to
12625 decide whether we are using an extended opcode or not. */
12626
12627 int
12628 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12629 {
12630 int change;
12631
12632 if (RELAX_BRANCH_P (fragp->fr_subtype))
12633 {
12634
12635 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12636
12637 return fragp->fr_var;
12638 }
12639
12640 if (RELAX_MIPS16_P (fragp->fr_subtype))
12641 /* We don't want to modify the EXTENDED bit here; it might get us
12642 into infinite loops. We change it only in mips_relax_frag(). */
12643 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12644
12645 if (mips_pic == NO_PIC)
12646 change = nopic_need_relax (fragp->fr_symbol, 0);
12647 else if (mips_pic == SVR4_PIC)
12648 change = pic_need_relax (fragp->fr_symbol, segtype);
12649 else
12650 abort ();
12651
12652 if (change)
12653 {
12654 fragp->fr_subtype |= RELAX_USE_SECOND;
12655 return -RELAX_FIRST (fragp->fr_subtype);
12656 }
12657 else
12658 return -RELAX_SECOND (fragp->fr_subtype);
12659 }
12660
12661 /* This is called to see whether a reloc against a defined symbol
12662 should be converted into a reloc against a section. */
12663
12664 int
12665 mips_fix_adjustable (fixS *fixp)
12666 {
12667 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12668 about the format of the offset in the .o file. */
12669 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12670 return 0;
12671
12672 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12673 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12674 return 0;
12675
12676 if (fixp->fx_addsy == NULL)
12677 return 1;
12678
12679 /* If symbol SYM is in a mergeable section, relocations of the form
12680 SYM + 0 can usually be made section-relative. The mergeable data
12681 is then identified by the section offset rather than by the symbol.
12682
12683 However, if we're generating REL LO16 relocations, the offset is split
12684 between the LO16 and parterning high part relocation. The linker will
12685 need to recalculate the complete offset in order to correctly identify
12686 the merge data.
12687
12688 The linker has traditionally not looked for the parterning high part
12689 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12690 placed anywhere. Rather than break backwards compatibility by changing
12691 this, it seems better not to force the issue, and instead keep the
12692 original symbol. This will work with either linker behavior. */
12693 if ((fixp->fx_r_type == BFD_RELOC_LO16
12694 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
12695 || reloc_needs_lo_p (fixp->fx_r_type))
12696 && HAVE_IN_PLACE_ADDENDS
12697 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12698 return 0;
12699
12700 #ifdef OBJ_ELF
12701 /* Don't adjust relocations against mips16 symbols, so that the linker
12702 can find them if it needs to set up a stub. */
12703 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12704 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12705 && fixp->fx_subsy == NULL)
12706 return 0;
12707 #endif
12708
12709 return 1;
12710 }
12711
12712 /* Translate internal representation of relocation info to BFD target
12713 format. */
12714
12715 arelent **
12716 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12717 {
12718 static arelent *retval[4];
12719 arelent *reloc;
12720 bfd_reloc_code_real_type code;
12721
12722 memset (retval, 0, sizeof(retval));
12723 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12724 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12725 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12726 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12727
12728 assert (! fixp->fx_pcrel);
12729 reloc->addend = fixp->fx_addnumber;
12730
12731 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12732 entry to be used in the relocation's section offset. */
12733 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12734 {
12735 reloc->address = reloc->addend;
12736 reloc->addend = 0;
12737 }
12738
12739 code = fixp->fx_r_type;
12740
12741 /* To support a PC relative reloc, we used a Cygnus extension.
12742 We check for that here to make sure that we don't let such a
12743 reloc escape normally. (FIXME: This was formerly used by
12744 embedded-PIC support, but is now used by branch handling in
12745 general. That probably should be fixed.) */
12746 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12747 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12748 && code == BFD_RELOC_16_PCREL_S2)
12749 reloc->howto = NULL;
12750 else
12751 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12752
12753 if (reloc->howto == NULL)
12754 {
12755 as_bad_where (fixp->fx_file, fixp->fx_line,
12756 _("Can not represent %s relocation in this object file format"),
12757 bfd_get_reloc_code_name (code));
12758 retval[0] = NULL;
12759 }
12760
12761 return retval;
12762 }
12763
12764 /* Relax a machine dependent frag. This returns the amount by which
12765 the current size of the frag should change. */
12766
12767 int
12768 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12769 {
12770 if (RELAX_BRANCH_P (fragp->fr_subtype))
12771 {
12772 offsetT old_var = fragp->fr_var;
12773
12774 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12775
12776 return fragp->fr_var - old_var;
12777 }
12778
12779 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12780 return 0;
12781
12782 if (mips16_extended_frag (fragp, NULL, stretch))
12783 {
12784 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12785 return 0;
12786 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12787 return 2;
12788 }
12789 else
12790 {
12791 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12792 return 0;
12793 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12794 return -2;
12795 }
12796
12797 return 0;
12798 }
12799
12800 /* Convert a machine dependent frag. */
12801
12802 void
12803 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12804 {
12805 if (RELAX_BRANCH_P (fragp->fr_subtype))
12806 {
12807 bfd_byte *buf;
12808 unsigned long insn;
12809 expressionS exp;
12810 fixS *fixp;
12811
12812 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12813
12814 if (target_big_endian)
12815 insn = bfd_getb32 (buf);
12816 else
12817 insn = bfd_getl32 (buf);
12818
12819 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12820 {
12821 /* We generate a fixup instead of applying it right now
12822 because, if there are linker relaxations, we're going to
12823 need the relocations. */
12824 exp.X_op = O_symbol;
12825 exp.X_add_symbol = fragp->fr_symbol;
12826 exp.X_add_number = fragp->fr_offset;
12827
12828 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12829 4, &exp, 1,
12830 BFD_RELOC_16_PCREL_S2);
12831 fixp->fx_file = fragp->fr_file;
12832 fixp->fx_line = fragp->fr_line;
12833
12834 md_number_to_chars ((char *) buf, insn, 4);
12835 buf += 4;
12836 }
12837 else
12838 {
12839 int i;
12840
12841 as_warn_where (fragp->fr_file, fragp->fr_line,
12842 _("relaxed out-of-range branch into a jump"));
12843
12844 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12845 goto uncond;
12846
12847 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12848 {
12849 /* Reverse the branch. */
12850 switch ((insn >> 28) & 0xf)
12851 {
12852 case 4:
12853 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12854 have the condition reversed by tweaking a single
12855 bit, and their opcodes all have 0x4???????. */
12856 assert ((insn & 0xf1000000) == 0x41000000);
12857 insn ^= 0x00010000;
12858 break;
12859
12860 case 0:
12861 /* bltz 0x04000000 bgez 0x04010000
12862 bltzal 0x04100000 bgezal 0x04110000 */
12863 assert ((insn & 0xfc0e0000) == 0x04000000);
12864 insn ^= 0x00010000;
12865 break;
12866
12867 case 1:
12868 /* beq 0x10000000 bne 0x14000000
12869 blez 0x18000000 bgtz 0x1c000000 */
12870 insn ^= 0x04000000;
12871 break;
12872
12873 default:
12874 abort ();
12875 }
12876 }
12877
12878 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12879 {
12880 /* Clear the and-link bit. */
12881 assert ((insn & 0xfc1c0000) == 0x04100000);
12882
12883 /* bltzal 0x04100000 bgezal 0x04110000
12884 bltzall 0x04120000 bgezall 0x04130000 */
12885 insn &= ~0x00100000;
12886 }
12887
12888 /* Branch over the branch (if the branch was likely) or the
12889 full jump (not likely case). Compute the offset from the
12890 current instruction to branch to. */
12891 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12892 i = 16;
12893 else
12894 {
12895 /* How many bytes in instructions we've already emitted? */
12896 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12897 /* How many bytes in instructions from here to the end? */
12898 i = fragp->fr_var - i;
12899 }
12900 /* Convert to instruction count. */
12901 i >>= 2;
12902 /* Branch counts from the next instruction. */
12903 i--;
12904 insn |= i;
12905 /* Branch over the jump. */
12906 md_number_to_chars ((char *) buf, insn, 4);
12907 buf += 4;
12908
12909 /* Nop */
12910 md_number_to_chars ((char *) buf, 0, 4);
12911 buf += 4;
12912
12913 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12914 {
12915 /* beql $0, $0, 2f */
12916 insn = 0x50000000;
12917 /* Compute the PC offset from the current instruction to
12918 the end of the variable frag. */
12919 /* How many bytes in instructions we've already emitted? */
12920 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12921 /* How many bytes in instructions from here to the end? */
12922 i = fragp->fr_var - i;
12923 /* Convert to instruction count. */
12924 i >>= 2;
12925 /* Don't decrement i, because we want to branch over the
12926 delay slot. */
12927
12928 insn |= i;
12929 md_number_to_chars ((char *) buf, insn, 4);
12930 buf += 4;
12931
12932 md_number_to_chars ((char *) buf, 0, 4);
12933 buf += 4;
12934 }
12935
12936 uncond:
12937 if (mips_pic == NO_PIC)
12938 {
12939 /* j or jal. */
12940 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
12941 ? 0x0c000000 : 0x08000000);
12942 exp.X_op = O_symbol;
12943 exp.X_add_symbol = fragp->fr_symbol;
12944 exp.X_add_number = fragp->fr_offset;
12945
12946 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12947 4, &exp, 0, BFD_RELOC_MIPS_JMP);
12948 fixp->fx_file = fragp->fr_file;
12949 fixp->fx_line = fragp->fr_line;
12950
12951 md_number_to_chars ((char *) buf, insn, 4);
12952 buf += 4;
12953 }
12954 else
12955 {
12956 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
12957 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
12958 exp.X_op = O_symbol;
12959 exp.X_add_symbol = fragp->fr_symbol;
12960 exp.X_add_number = fragp->fr_offset;
12961
12962 if (fragp->fr_offset)
12963 {
12964 exp.X_add_symbol = make_expr_symbol (&exp);
12965 exp.X_add_number = 0;
12966 }
12967
12968 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12969 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
12970 fixp->fx_file = fragp->fr_file;
12971 fixp->fx_line = fragp->fr_line;
12972
12973 md_number_to_chars ((char *) buf, insn, 4);
12974 buf += 4;
12975
12976 if (mips_opts.isa == ISA_MIPS1)
12977 {
12978 /* nop */
12979 md_number_to_chars ((char *) buf, 0, 4);
12980 buf += 4;
12981 }
12982
12983 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
12984 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
12985
12986 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12987 4, &exp, 0, BFD_RELOC_LO16);
12988 fixp->fx_file = fragp->fr_file;
12989 fixp->fx_line = fragp->fr_line;
12990
12991 md_number_to_chars ((char *) buf, insn, 4);
12992 buf += 4;
12993
12994 /* j(al)r $at. */
12995 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12996 insn = 0x0020f809;
12997 else
12998 insn = 0x00200008;
12999
13000 md_number_to_chars ((char *) buf, insn, 4);
13001 buf += 4;
13002 }
13003 }
13004
13005 assert (buf == (bfd_byte *)fragp->fr_literal
13006 + fragp->fr_fix + fragp->fr_var);
13007
13008 fragp->fr_fix += fragp->fr_var;
13009
13010 return;
13011 }
13012
13013 if (RELAX_MIPS16_P (fragp->fr_subtype))
13014 {
13015 int type;
13016 register const struct mips16_immed_operand *op;
13017 bfd_boolean small, ext;
13018 offsetT val;
13019 bfd_byte *buf;
13020 unsigned long insn;
13021 bfd_boolean use_extend;
13022 unsigned short extend;
13023
13024 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13025 op = mips16_immed_operands;
13026 while (op->type != type)
13027 ++op;
13028
13029 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13030 {
13031 small = FALSE;
13032 ext = TRUE;
13033 }
13034 else
13035 {
13036 small = TRUE;
13037 ext = FALSE;
13038 }
13039
13040 resolve_symbol_value (fragp->fr_symbol);
13041 val = S_GET_VALUE (fragp->fr_symbol);
13042 if (op->pcrel)
13043 {
13044 addressT addr;
13045
13046 addr = fragp->fr_address + fragp->fr_fix;
13047
13048 /* The rules for the base address of a PC relative reloc are
13049 complicated; see mips16_extended_frag. */
13050 if (type == 'p' || type == 'q')
13051 {
13052 addr += 2;
13053 if (ext)
13054 addr += 2;
13055 /* Ignore the low bit in the target, since it will be
13056 set for a text label. */
13057 if ((val & 1) != 0)
13058 --val;
13059 }
13060 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13061 addr -= 4;
13062 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13063 addr -= 2;
13064
13065 addr &= ~ (addressT) ((1 << op->shift) - 1);
13066 val -= addr;
13067
13068 /* Make sure the section winds up with the alignment we have
13069 assumed. */
13070 if (op->shift > 0)
13071 record_alignment (asec, op->shift);
13072 }
13073
13074 if (ext
13075 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13076 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13077 as_warn_where (fragp->fr_file, fragp->fr_line,
13078 _("extended instruction in delay slot"));
13079
13080 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13081
13082 if (target_big_endian)
13083 insn = bfd_getb16 (buf);
13084 else
13085 insn = bfd_getl16 (buf);
13086
13087 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13088 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13089 small, ext, &insn, &use_extend, &extend);
13090
13091 if (use_extend)
13092 {
13093 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13094 fragp->fr_fix += 2;
13095 buf += 2;
13096 }
13097
13098 md_number_to_chars ((char *) buf, insn, 2);
13099 fragp->fr_fix += 2;
13100 buf += 2;
13101 }
13102 else
13103 {
13104 int first, second;
13105 fixS *fixp;
13106
13107 first = RELAX_FIRST (fragp->fr_subtype);
13108 second = RELAX_SECOND (fragp->fr_subtype);
13109 fixp = (fixS *) fragp->fr_opcode;
13110
13111 /* Possibly emit a warning if we've chosen the longer option. */
13112 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13113 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13114 {
13115 const char *msg = macro_warning (fragp->fr_subtype);
13116 if (msg != 0)
13117 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13118 }
13119
13120 /* Go through all the fixups for the first sequence. Disable them
13121 (by marking them as done) if we're going to use the second
13122 sequence instead. */
13123 while (fixp
13124 && fixp->fx_frag == fragp
13125 && fixp->fx_where < fragp->fr_fix - second)
13126 {
13127 if (fragp->fr_subtype & RELAX_USE_SECOND)
13128 fixp->fx_done = 1;
13129 fixp = fixp->fx_next;
13130 }
13131
13132 /* Go through the fixups for the second sequence. Disable them if
13133 we're going to use the first sequence, otherwise adjust their
13134 addresses to account for the relaxation. */
13135 while (fixp && fixp->fx_frag == fragp)
13136 {
13137 if (fragp->fr_subtype & RELAX_USE_SECOND)
13138 fixp->fx_where -= first;
13139 else
13140 fixp->fx_done = 1;
13141 fixp = fixp->fx_next;
13142 }
13143
13144 /* Now modify the frag contents. */
13145 if (fragp->fr_subtype & RELAX_USE_SECOND)
13146 {
13147 char *start;
13148
13149 start = fragp->fr_literal + fragp->fr_fix - first - second;
13150 memmove (start, start + first, second);
13151 fragp->fr_fix -= first;
13152 }
13153 else
13154 fragp->fr_fix -= second;
13155 }
13156 }
13157
13158 #ifdef OBJ_ELF
13159
13160 /* This function is called after the relocs have been generated.
13161 We've been storing mips16 text labels as odd. Here we convert them
13162 back to even for the convenience of the debugger. */
13163
13164 void
13165 mips_frob_file_after_relocs (void)
13166 {
13167 asymbol **syms;
13168 unsigned int count, i;
13169
13170 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13171 return;
13172
13173 syms = bfd_get_outsymbols (stdoutput);
13174 count = bfd_get_symcount (stdoutput);
13175 for (i = 0; i < count; i++, syms++)
13176 {
13177 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13178 && ((*syms)->value & 1) != 0)
13179 {
13180 (*syms)->value &= ~1;
13181 /* If the symbol has an odd size, it was probably computed
13182 incorrectly, so adjust that as well. */
13183 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13184 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13185 }
13186 }
13187 }
13188
13189 #endif
13190
13191 /* This function is called whenever a label is defined. It is used
13192 when handling branch delays; if a branch has a label, we assume we
13193 can not move it. */
13194
13195 void
13196 mips_define_label (symbolS *sym)
13197 {
13198 struct insn_label_list *l;
13199
13200 if (free_insn_labels == NULL)
13201 l = (struct insn_label_list *) xmalloc (sizeof *l);
13202 else
13203 {
13204 l = free_insn_labels;
13205 free_insn_labels = l->next;
13206 }
13207
13208 l->label = sym;
13209 l->next = insn_labels;
13210 insn_labels = l;
13211 }
13212 \f
13213 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13214
13215 /* Some special processing for a MIPS ELF file. */
13216
13217 void
13218 mips_elf_final_processing (void)
13219 {
13220 /* Write out the register information. */
13221 if (mips_abi != N64_ABI)
13222 {
13223 Elf32_RegInfo s;
13224
13225 s.ri_gprmask = mips_gprmask;
13226 s.ri_cprmask[0] = mips_cprmask[0];
13227 s.ri_cprmask[1] = mips_cprmask[1];
13228 s.ri_cprmask[2] = mips_cprmask[2];
13229 s.ri_cprmask[3] = mips_cprmask[3];
13230 /* The gp_value field is set by the MIPS ELF backend. */
13231
13232 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13233 ((Elf32_External_RegInfo *)
13234 mips_regmask_frag));
13235 }
13236 else
13237 {
13238 Elf64_Internal_RegInfo s;
13239
13240 s.ri_gprmask = mips_gprmask;
13241 s.ri_pad = 0;
13242 s.ri_cprmask[0] = mips_cprmask[0];
13243 s.ri_cprmask[1] = mips_cprmask[1];
13244 s.ri_cprmask[2] = mips_cprmask[2];
13245 s.ri_cprmask[3] = mips_cprmask[3];
13246 /* The gp_value field is set by the MIPS ELF backend. */
13247
13248 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13249 ((Elf64_External_RegInfo *)
13250 mips_regmask_frag));
13251 }
13252
13253 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13254 sort of BFD interface for this. */
13255 if (mips_any_noreorder)
13256 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13257 if (mips_pic != NO_PIC)
13258 {
13259 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13260 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13261 }
13262 if (mips_abicalls)
13263 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13264
13265 /* Set MIPS ELF flags for ASEs. */
13266 if (file_ase_mips16)
13267 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13268 #if 0 /* XXX FIXME */
13269 if (file_ase_mips3d)
13270 elf_elfheader (stdoutput)->e_flags |= ???;
13271 #endif
13272 if (file_ase_mdmx)
13273 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13274
13275 /* Set the MIPS ELF ABI flags. */
13276 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13277 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13278 else if (mips_abi == O64_ABI)
13279 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13280 else if (mips_abi == EABI_ABI)
13281 {
13282 if (!file_mips_gp32)
13283 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13284 else
13285 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13286 }
13287 else if (mips_abi == N32_ABI)
13288 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13289
13290 /* Nothing to do for N64_ABI. */
13291
13292 if (mips_32bitmode)
13293 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13294 }
13295
13296 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13297 \f
13298 typedef struct proc {
13299 symbolS *func_sym;
13300 symbolS *func_end_sym;
13301 unsigned long reg_mask;
13302 unsigned long reg_offset;
13303 unsigned long fpreg_mask;
13304 unsigned long fpreg_offset;
13305 unsigned long frame_offset;
13306 unsigned long frame_reg;
13307 unsigned long pc_reg;
13308 } procS;
13309
13310 static procS cur_proc;
13311 static procS *cur_proc_ptr;
13312 static int numprocs;
13313
13314 /* Fill in an rs_align_code fragment. */
13315
13316 void
13317 mips_handle_align (fragS *fragp)
13318 {
13319 if (fragp->fr_type != rs_align_code)
13320 return;
13321
13322 if (mips_opts.mips16)
13323 {
13324 static const unsigned char be_nop[] = { 0x65, 0x00 };
13325 static const unsigned char le_nop[] = { 0x00, 0x65 };
13326
13327 int bytes;
13328 char *p;
13329
13330 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13331 p = fragp->fr_literal + fragp->fr_fix;
13332
13333 if (bytes & 1)
13334 {
13335 *p++ = 0;
13336 fragp->fr_fix++;
13337 }
13338
13339 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13340 fragp->fr_var = 2;
13341 }
13342
13343 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13344 }
13345
13346 static void
13347 md_obj_begin (void)
13348 {
13349 }
13350
13351 static void
13352 md_obj_end (void)
13353 {
13354 /* check for premature end, nesting errors, etc */
13355 if (cur_proc_ptr)
13356 as_warn (_("missing .end at end of assembly"));
13357 }
13358
13359 static long
13360 get_number (void)
13361 {
13362 int negative = 0;
13363 long val = 0;
13364
13365 if (*input_line_pointer == '-')
13366 {
13367 ++input_line_pointer;
13368 negative = 1;
13369 }
13370 if (!ISDIGIT (*input_line_pointer))
13371 as_bad (_("expected simple number"));
13372 if (input_line_pointer[0] == '0')
13373 {
13374 if (input_line_pointer[1] == 'x')
13375 {
13376 input_line_pointer += 2;
13377 while (ISXDIGIT (*input_line_pointer))
13378 {
13379 val <<= 4;
13380 val |= hex_value (*input_line_pointer++);
13381 }
13382 return negative ? -val : val;
13383 }
13384 else
13385 {
13386 ++input_line_pointer;
13387 while (ISDIGIT (*input_line_pointer))
13388 {
13389 val <<= 3;
13390 val |= *input_line_pointer++ - '0';
13391 }
13392 return negative ? -val : val;
13393 }
13394 }
13395 if (!ISDIGIT (*input_line_pointer))
13396 {
13397 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13398 *input_line_pointer, *input_line_pointer);
13399 as_warn (_("invalid number"));
13400 return -1;
13401 }
13402 while (ISDIGIT (*input_line_pointer))
13403 {
13404 val *= 10;
13405 val += *input_line_pointer++ - '0';
13406 }
13407 return negative ? -val : val;
13408 }
13409
13410 /* The .file directive; just like the usual .file directive, but there
13411 is an initial number which is the ECOFF file index. In the non-ECOFF
13412 case .file implies DWARF-2. */
13413
13414 static void
13415 s_mips_file (int x ATTRIBUTE_UNUSED)
13416 {
13417 static int first_file_directive = 0;
13418
13419 if (ECOFF_DEBUGGING)
13420 {
13421 get_number ();
13422 s_app_file (0);
13423 }
13424 else
13425 {
13426 char *filename;
13427
13428 filename = dwarf2_directive_file (0);
13429
13430 /* Versions of GCC up to 3.1 start files with a ".file"
13431 directive even for stabs output. Make sure that this
13432 ".file" is handled. Note that you need a version of GCC
13433 after 3.1 in order to support DWARF-2 on MIPS. */
13434 if (filename != NULL && ! first_file_directive)
13435 {
13436 (void) new_logical_line (filename, -1);
13437 s_app_file_string (filename, 0);
13438 }
13439 first_file_directive = 1;
13440 }
13441 }
13442
13443 /* The .loc directive, implying DWARF-2. */
13444
13445 static void
13446 s_mips_loc (int x ATTRIBUTE_UNUSED)
13447 {
13448 if (!ECOFF_DEBUGGING)
13449 dwarf2_directive_loc (0);
13450 }
13451
13452 /* The .end directive. */
13453
13454 static void
13455 s_mips_end (int x ATTRIBUTE_UNUSED)
13456 {
13457 symbolS *p;
13458
13459 /* Following functions need their own .frame and .cprestore directives. */
13460 mips_frame_reg_valid = 0;
13461 mips_cprestore_valid = 0;
13462
13463 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13464 {
13465 p = get_symbol ();
13466 demand_empty_rest_of_line ();
13467 }
13468 else
13469 p = NULL;
13470
13471 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13472 as_warn (_(".end not in text section"));
13473
13474 if (!cur_proc_ptr)
13475 {
13476 as_warn (_(".end directive without a preceding .ent directive."));
13477 demand_empty_rest_of_line ();
13478 return;
13479 }
13480
13481 if (p != NULL)
13482 {
13483 assert (S_GET_NAME (p));
13484 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13485 as_warn (_(".end symbol does not match .ent symbol."));
13486
13487 if (debug_type == DEBUG_STABS)
13488 stabs_generate_asm_endfunc (S_GET_NAME (p),
13489 S_GET_NAME (p));
13490 }
13491 else
13492 as_warn (_(".end directive missing or unknown symbol"));
13493
13494 #ifdef OBJ_ELF
13495 /* Create an expression to calculate the size of the function. */
13496 if (p && cur_proc_ptr)
13497 {
13498 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13499 expressionS *exp = xmalloc (sizeof (expressionS));
13500
13501 obj->size = exp;
13502 exp->X_op = O_subtract;
13503 exp->X_add_symbol = symbol_temp_new_now ();
13504 exp->X_op_symbol = p;
13505 exp->X_add_number = 0;
13506
13507 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13508 }
13509
13510 /* Generate a .pdr section. */
13511 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13512 && mips_flag_pdr)
13513 {
13514 segT saved_seg = now_seg;
13515 subsegT saved_subseg = now_subseg;
13516 valueT dot;
13517 expressionS exp;
13518 char *fragp;
13519
13520 dot = frag_now_fix ();
13521
13522 #ifdef md_flush_pending_output
13523 md_flush_pending_output ();
13524 #endif
13525
13526 assert (pdr_seg);
13527 subseg_set (pdr_seg, 0);
13528
13529 /* Write the symbol. */
13530 exp.X_op = O_symbol;
13531 exp.X_add_symbol = p;
13532 exp.X_add_number = 0;
13533 emit_expr (&exp, 4);
13534
13535 fragp = frag_more (7 * 4);
13536
13537 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13538 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13539 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13540 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13541 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13542 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13543 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13544
13545 subseg_set (saved_seg, saved_subseg);
13546 }
13547 #endif /* OBJ_ELF */
13548
13549 cur_proc_ptr = NULL;
13550 }
13551
13552 /* The .aent and .ent directives. */
13553
13554 static void
13555 s_mips_ent (int aent)
13556 {
13557 symbolS *symbolP;
13558
13559 symbolP = get_symbol ();
13560 if (*input_line_pointer == ',')
13561 ++input_line_pointer;
13562 SKIP_WHITESPACE ();
13563 if (ISDIGIT (*input_line_pointer)
13564 || *input_line_pointer == '-')
13565 get_number ();
13566
13567 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13568 as_warn (_(".ent or .aent not in text section."));
13569
13570 if (!aent && cur_proc_ptr)
13571 as_warn (_("missing .end"));
13572
13573 if (!aent)
13574 {
13575 /* This function needs its own .frame and .cprestore directives. */
13576 mips_frame_reg_valid = 0;
13577 mips_cprestore_valid = 0;
13578
13579 cur_proc_ptr = &cur_proc;
13580 memset (cur_proc_ptr, '\0', sizeof (procS));
13581
13582 cur_proc_ptr->func_sym = symbolP;
13583
13584 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13585
13586 ++numprocs;
13587
13588 if (debug_type == DEBUG_STABS)
13589 stabs_generate_asm_func (S_GET_NAME (symbolP),
13590 S_GET_NAME (symbolP));
13591 }
13592
13593 demand_empty_rest_of_line ();
13594 }
13595
13596 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13597 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13598 s_mips_frame is used so that we can set the PDR information correctly.
13599 We can't use the ecoff routines because they make reference to the ecoff
13600 symbol table (in the mdebug section). */
13601
13602 static void
13603 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13604 {
13605 #ifdef OBJ_ELF
13606 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13607 {
13608 long val;
13609
13610 if (cur_proc_ptr == (procS *) NULL)
13611 {
13612 as_warn (_(".frame outside of .ent"));
13613 demand_empty_rest_of_line ();
13614 return;
13615 }
13616
13617 cur_proc_ptr->frame_reg = tc_get_register (1);
13618
13619 SKIP_WHITESPACE ();
13620 if (*input_line_pointer++ != ','
13621 || get_absolute_expression_and_terminator (&val) != ',')
13622 {
13623 as_warn (_("Bad .frame directive"));
13624 --input_line_pointer;
13625 demand_empty_rest_of_line ();
13626 return;
13627 }
13628
13629 cur_proc_ptr->frame_offset = val;
13630 cur_proc_ptr->pc_reg = tc_get_register (0);
13631
13632 demand_empty_rest_of_line ();
13633 }
13634 else
13635 #endif /* OBJ_ELF */
13636 s_ignore (ignore);
13637 }
13638
13639 /* The .fmask and .mask directives. If the mdebug section is present
13640 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13641 embedded targets, s_mips_mask is used so that we can set the PDR
13642 information correctly. We can't use the ecoff routines because they
13643 make reference to the ecoff symbol table (in the mdebug section). */
13644
13645 static void
13646 s_mips_mask (int reg_type)
13647 {
13648 #ifdef OBJ_ELF
13649 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13650 {
13651 long mask, off;
13652
13653 if (cur_proc_ptr == (procS *) NULL)
13654 {
13655 as_warn (_(".mask/.fmask outside of .ent"));
13656 demand_empty_rest_of_line ();
13657 return;
13658 }
13659
13660 if (get_absolute_expression_and_terminator (&mask) != ',')
13661 {
13662 as_warn (_("Bad .mask/.fmask directive"));
13663 --input_line_pointer;
13664 demand_empty_rest_of_line ();
13665 return;
13666 }
13667
13668 off = get_absolute_expression ();
13669
13670 if (reg_type == 'F')
13671 {
13672 cur_proc_ptr->fpreg_mask = mask;
13673 cur_proc_ptr->fpreg_offset = off;
13674 }
13675 else
13676 {
13677 cur_proc_ptr->reg_mask = mask;
13678 cur_proc_ptr->reg_offset = off;
13679 }
13680
13681 demand_empty_rest_of_line ();
13682 }
13683 else
13684 #endif /* OBJ_ELF */
13685 s_ignore (reg_type);
13686 }
13687
13688 /* A table describing all the processors gas knows about. Names are
13689 matched in the order listed.
13690
13691 To ease comparison, please keep this table in the same order as
13692 gcc's mips_cpu_info_table[]. */
13693 static const struct mips_cpu_info mips_cpu_info_table[] =
13694 {
13695 /* Entries for generic ISAs */
13696 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13697 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13698 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13699 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13700 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13701 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13702 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13703 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13704 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13705
13706 /* MIPS I */
13707 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13708 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13709 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13710
13711 /* MIPS II */
13712 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13713
13714 /* MIPS III */
13715 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13716 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13717 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13718 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13719 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13720 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13721 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13722 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13723 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13724 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13725 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13726 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13727
13728 /* MIPS IV */
13729 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13730 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13731 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13732 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13733 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13734 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13735 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13736 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13737 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13738 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13739 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13740 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13741 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13742
13743 /* MIPS 32 */
13744 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13745 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13746 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13747
13748 /* MIPS 64 */
13749 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13750 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13751
13752 /* Broadcom SB-1 CPU core */
13753 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13754
13755 /* End marker */
13756 { NULL, 0, 0, 0 }
13757 };
13758
13759
13760 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13761 with a final "000" replaced by "k". Ignore case.
13762
13763 Note: this function is shared between GCC and GAS. */
13764
13765 static bfd_boolean
13766 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13767 {
13768 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13769 given++, canonical++;
13770
13771 return ((*given == 0 && *canonical == 0)
13772 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13773 }
13774
13775
13776 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13777 CPU name. We've traditionally allowed a lot of variation here.
13778
13779 Note: this function is shared between GCC and GAS. */
13780
13781 static bfd_boolean
13782 mips_matching_cpu_name_p (const char *canonical, const char *given)
13783 {
13784 /* First see if the name matches exactly, or with a final "000"
13785 turned into "k". */
13786 if (mips_strict_matching_cpu_name_p (canonical, given))
13787 return TRUE;
13788
13789 /* If not, try comparing based on numerical designation alone.
13790 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13791 if (TOLOWER (*given) == 'r')
13792 given++;
13793 if (!ISDIGIT (*given))
13794 return FALSE;
13795
13796 /* Skip over some well-known prefixes in the canonical name,
13797 hoping to find a number there too. */
13798 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13799 canonical += 2;
13800 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13801 canonical += 2;
13802 else if (TOLOWER (canonical[0]) == 'r')
13803 canonical += 1;
13804
13805 return mips_strict_matching_cpu_name_p (canonical, given);
13806 }
13807
13808
13809 /* Parse an option that takes the name of a processor as its argument.
13810 OPTION is the name of the option and CPU_STRING is the argument.
13811 Return the corresponding processor enumeration if the CPU_STRING is
13812 recognized, otherwise report an error and return null.
13813
13814 A similar function exists in GCC. */
13815
13816 static const struct mips_cpu_info *
13817 mips_parse_cpu (const char *option, const char *cpu_string)
13818 {
13819 const struct mips_cpu_info *p;
13820
13821 /* 'from-abi' selects the most compatible architecture for the given
13822 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13823 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13824 version. Look first at the -mgp options, if given, otherwise base
13825 the choice on MIPS_DEFAULT_64BIT.
13826
13827 Treat NO_ABI like the EABIs. One reason to do this is that the
13828 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13829 architecture. This code picks MIPS I for 'mips' and MIPS III for
13830 'mips64', just as we did in the days before 'from-abi'. */
13831 if (strcasecmp (cpu_string, "from-abi") == 0)
13832 {
13833 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13834 return mips_cpu_info_from_isa (ISA_MIPS1);
13835
13836 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13837 return mips_cpu_info_from_isa (ISA_MIPS3);
13838
13839 if (file_mips_gp32 >= 0)
13840 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13841
13842 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13843 ? ISA_MIPS3
13844 : ISA_MIPS1);
13845 }
13846
13847 /* 'default' has traditionally been a no-op. Probably not very useful. */
13848 if (strcasecmp (cpu_string, "default") == 0)
13849 return 0;
13850
13851 for (p = mips_cpu_info_table; p->name != 0; p++)
13852 if (mips_matching_cpu_name_p (p->name, cpu_string))
13853 return p;
13854
13855 as_bad ("Bad value (%s) for %s", cpu_string, option);
13856 return 0;
13857 }
13858
13859 /* Return the canonical processor information for ISA (a member of the
13860 ISA_MIPS* enumeration). */
13861
13862 static const struct mips_cpu_info *
13863 mips_cpu_info_from_isa (int isa)
13864 {
13865 int i;
13866
13867 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13868 if (mips_cpu_info_table[i].is_isa
13869 && isa == mips_cpu_info_table[i].isa)
13870 return (&mips_cpu_info_table[i]);
13871
13872 return NULL;
13873 }
13874
13875 static const struct mips_cpu_info *
13876 mips_cpu_info_from_arch (int arch)
13877 {
13878 int i;
13879
13880 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13881 if (arch == mips_cpu_info_table[i].cpu)
13882 return (&mips_cpu_info_table[i]);
13883
13884 return NULL;
13885 }
13886 \f
13887 static void
13888 show (FILE *stream, const char *string, int *col_p, int *first_p)
13889 {
13890 if (*first_p)
13891 {
13892 fprintf (stream, "%24s", "");
13893 *col_p = 24;
13894 }
13895 else
13896 {
13897 fprintf (stream, ", ");
13898 *col_p += 2;
13899 }
13900
13901 if (*col_p + strlen (string) > 72)
13902 {
13903 fprintf (stream, "\n%24s", "");
13904 *col_p = 24;
13905 }
13906
13907 fprintf (stream, "%s", string);
13908 *col_p += strlen (string);
13909
13910 *first_p = 0;
13911 }
13912
13913 void
13914 md_show_usage (FILE *stream)
13915 {
13916 int column, first;
13917 size_t i;
13918
13919 fprintf (stream, _("\
13920 MIPS options:\n\
13921 -EB generate big endian output\n\
13922 -EL generate little endian output\n\
13923 -g, -g2 do not remove unneeded NOPs or swap branches\n\
13924 -G NUM allow referencing objects up to NUM bytes\n\
13925 implicitly with the gp register [default 8]\n"));
13926 fprintf (stream, _("\
13927 -mips1 generate MIPS ISA I instructions\n\
13928 -mips2 generate MIPS ISA II instructions\n\
13929 -mips3 generate MIPS ISA III instructions\n\
13930 -mips4 generate MIPS ISA IV instructions\n\
13931 -mips5 generate MIPS ISA V instructions\n\
13932 -mips32 generate MIPS32 ISA instructions\n\
13933 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
13934 -mips64 generate MIPS64 ISA instructions\n\
13935 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
13936 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13937
13938 first = 1;
13939
13940 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13941 show (stream, mips_cpu_info_table[i].name, &column, &first);
13942 show (stream, "from-abi", &column, &first);
13943 fputc ('\n', stream);
13944
13945 fprintf (stream, _("\
13946 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13947 -no-mCPU don't generate code specific to CPU.\n\
13948 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13949
13950 first = 1;
13951
13952 show (stream, "3900", &column, &first);
13953 show (stream, "4010", &column, &first);
13954 show (stream, "4100", &column, &first);
13955 show (stream, "4650", &column, &first);
13956 fputc ('\n', stream);
13957
13958 fprintf (stream, _("\
13959 -mips16 generate mips16 instructions\n\
13960 -no-mips16 do not generate mips16 instructions\n"));
13961 fprintf (stream, _("\
13962 -mfix-vr4120 work around certain VR4120 errata\n\
13963 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
13964 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13965 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13966 -mno-shared optimize output for executables\n\
13967 -msym32 assume all symbols have 32-bit values\n\
13968 -O0 remove unneeded NOPs, do not swap branches\n\
13969 -O remove unneeded NOPs and swap branches\n\
13970 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13971 --trap, --no-break trap exception on div by 0 and mult overflow\n\
13972 --break, --no-trap break exception on div by 0 and mult overflow\n"));
13973 #ifdef OBJ_ELF
13974 fprintf (stream, _("\
13975 -KPIC, -call_shared generate SVR4 position independent code\n\
13976 -non_shared do not generate position independent code\n\
13977 -xgot assume a 32 bit GOT\n\
13978 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
13979 -mshared, -mno-shared disable/enable .cpload optimization for\n\
13980 non-shared code\n\
13981 -mabi=ABI create ABI conformant object file for:\n"));
13982
13983 first = 1;
13984
13985 show (stream, "32", &column, &first);
13986 show (stream, "o64", &column, &first);
13987 show (stream, "n32", &column, &first);
13988 show (stream, "64", &column, &first);
13989 show (stream, "eabi", &column, &first);
13990
13991 fputc ('\n', stream);
13992
13993 fprintf (stream, _("\
13994 -32 create o32 ABI object file (default)\n\
13995 -n32 create n32 ABI object file\n\
13996 -64 create 64 ABI object file\n"));
13997 #endif
13998 }
13999
14000 enum dwarf2_format
14001 mips_dwarf2_format (void)
14002 {
14003 if (mips_abi == N64_ABI)
14004 {
14005 #ifdef TE_IRIX
14006 return dwarf2_format_64bit_irix;
14007 #else
14008 return dwarf2_format_64bit;
14009 #endif
14010 }
14011 else
14012 return dwarf2_format_32bit;
14013 }
14014
14015 int
14016 mips_dwarf2_addr_size (void)
14017 {
14018 if (mips_abi == N64_ABI)
14019 return 8;
14020 else
14021 return 4;
14022 }
14023
14024 /* Standard calling conventions leave the CFA at SP on entry. */
14025 void
14026 mips_cfi_frame_initial_instructions (void)
14027 {
14028 cfi_add_CFA_def_cfa_register (SP);
14029 }
14030
This page took 0.326028 seconds and 5 git commands to generate.