[ gas/ChangeLog ]
[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, 51 Franklin Street - Fifth Floor, Boston, MA
24 02110-1301, 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 occurred 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 int ase_smartmips;
197 int ase_dsp;
198 int ase_mt;
199 /* Whether we are assembling for the mips16 processor. 0 if we are
200 not, 1 if we are, and -1 if the value has not been initialized.
201 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
202 -nomips16 command line options, and the default CPU. */
203 int mips16;
204 /* Non-zero if we should not reorder instructions. Changed by `.set
205 reorder' and `.set noreorder'. */
206 int noreorder;
207 /* Non-zero if we should not permit the $at ($1) register to be used
208 in instructions. Changed by `.set at' and `.set noat'. */
209 int noat;
210 /* Non-zero if we should warn when a macro instruction expands into
211 more than one machine instruction. Changed by `.set nomacro' and
212 `.set macro'. */
213 int warn_about_macros;
214 /* Non-zero if we should not move instructions. Changed by `.set
215 move', `.set volatile', `.set nomove', and `.set novolatile'. */
216 int nomove;
217 /* Non-zero if we should not optimize branches by moving the target
218 of the branch into the delay slot. Actually, we don't perform
219 this optimization anyhow. Changed by `.set bopt' and `.set
220 nobopt'. */
221 int nobopt;
222 /* Non-zero if we should not autoextend mips16 instructions.
223 Changed by `.set autoextend' and `.set noautoextend'. */
224 int noautoextend;
225 /* Restrict general purpose registers and floating point registers
226 to 32 bit. This is initially determined when -mgp32 or -mfp32
227 is passed but can changed if the assembler code uses .set mipsN. */
228 int gp32;
229 int fp32;
230 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
231 command line option, and the default CPU. */
232 int arch;
233 /* True if ".set sym32" is in effect. */
234 bfd_boolean sym32;
235 };
236
237 /* True if -mgp32 was passed. */
238 static int file_mips_gp32 = -1;
239
240 /* True if -mfp32 was passed. */
241 static int file_mips_fp32 = -1;
242
243 /* This is the struct we use to hold the current set of options. Note
244 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
245 -1 to indicate that they have not been initialized. */
246
247 static struct mips_set_options mips_opts =
248 {
249 ISA_UNKNOWN, -1, -1, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
250 };
251
252 /* These variables are filled in with the masks of registers used.
253 The object format code reads them and puts them in the appropriate
254 place. */
255 unsigned long mips_gprmask;
256 unsigned long mips_cprmask[4];
257
258 /* MIPS ISA we are using for this output file. */
259 static int file_mips_isa = ISA_UNKNOWN;
260
261 /* True if -mips16 was passed or implied by arguments passed on the
262 command line (e.g., by -march). */
263 static int file_ase_mips16;
264
265 /* True if -mips3d was passed or implied by arguments passed on the
266 command line (e.g., by -march). */
267 static int file_ase_mips3d;
268
269 /* True if -mdmx was passed or implied by arguments passed on the
270 command line (e.g., by -march). */
271 static int file_ase_mdmx;
272
273 /* True if -msmartmips was passed or implied by arguments passed on the
274 command line (e.g., by -march). */
275 static int file_ase_smartmips;
276
277 #define ISA_SUPPORT_SMARTMIPS (mips_opts.isa == ISA_MIPS32 \
278 || mips_opts.isa == ISA_MIPS32R2)
279
280 /* True if -mdsp was passed or implied by arguments passed on the
281 command line (e.g., by -march). */
282 static int file_ase_dsp;
283
284 /* True if -mmt was passed or implied by arguments passed on the
285 command line (e.g., by -march). */
286 static int file_ase_mt;
287
288 /* The argument of the -march= flag. The architecture we are assembling. */
289 static int file_mips_arch = CPU_UNKNOWN;
290 static const char *mips_arch_string;
291
292 /* The argument of the -mtune= flag. The architecture for which we
293 are optimizing. */
294 static int mips_tune = CPU_UNKNOWN;
295 static const char *mips_tune_string;
296
297 /* True when generating 32-bit code for a 64-bit processor. */
298 static int mips_32bitmode = 0;
299
300 /* True if the given ABI requires 32-bit registers. */
301 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
302
303 /* Likewise 64-bit registers. */
304 #define ABI_NEEDS_64BIT_REGS(ABI) \
305 ((ABI) == N32_ABI \
306 || (ABI) == N64_ABI \
307 || (ABI) == O64_ABI)
308
309 /* Return true if ISA supports 64 bit gp register instructions. */
310 #define ISA_HAS_64BIT_REGS(ISA) ( \
311 (ISA) == ISA_MIPS3 \
312 || (ISA) == ISA_MIPS4 \
313 || (ISA) == ISA_MIPS5 \
314 || (ISA) == ISA_MIPS64 \
315 || (ISA) == ISA_MIPS64R2 \
316 )
317
318 /* Return true if ISA supports 64-bit right rotate (dror et al.)
319 instructions. */
320 #define ISA_HAS_DROR(ISA) ( \
321 (ISA) == ISA_MIPS64R2 \
322 )
323
324 /* Return true if ISA supports 32-bit right rotate (ror et al.)
325 instructions. */
326 #define ISA_HAS_ROR(ISA) ( \
327 (ISA) == ISA_MIPS32R2 \
328 || (ISA) == ISA_MIPS64R2 \
329 || mips_opts.ase_smartmips \
330 )
331
332 #define HAVE_32BIT_GPRS \
333 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
334
335 #define HAVE_32BIT_FPRS \
336 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
337
338 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
339 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
340
341 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
342
343 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
344
345 /* True if relocations are stored in-place. */
346 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
347
348 /* The ABI-derived address size. */
349 #define HAVE_64BIT_ADDRESSES \
350 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
351 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
352
353 /* The size of symbolic constants (i.e., expressions of the form
354 "SYMBOL" or "SYMBOL + OFFSET"). */
355 #define HAVE_32BIT_SYMBOLS \
356 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
357 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
358
359 /* Addresses are loaded in different ways, depending on the address size
360 in use. The n32 ABI Documentation also mandates the use of additions
361 with overflow checking, but existing implementations don't follow it. */
362 #define ADDRESS_ADD_INSN \
363 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
364
365 #define ADDRESS_ADDI_INSN \
366 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
367
368 #define ADDRESS_LOAD_INSN \
369 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
370
371 #define ADDRESS_STORE_INSN \
372 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
373
374 /* Return true if the given CPU supports the MIPS16 ASE. */
375 #define CPU_HAS_MIPS16(cpu) \
376 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
377 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
378
379 /* Return true if the given CPU supports the MIPS3D ASE. */
380 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
381 )
382
383 /* Return true if the given CPU supports the MDMX ASE. */
384 #define CPU_HAS_MDMX(cpu) (FALSE \
385 )
386
387 /* Return true if the given CPU supports the DSP ASE. */
388 #define CPU_HAS_DSP(cpu) (FALSE \
389 )
390
391 /* Return true if the given CPU supports the MT ASE. */
392 #define CPU_HAS_MT(cpu) (FALSE \
393 )
394
395 /* True if CPU has a dror instruction. */
396 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
397
398 /* True if CPU has a ror instruction. */
399 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
400
401 /* True if mflo and mfhi can be immediately followed by instructions
402 which write to the HI and LO registers.
403
404 According to MIPS specifications, MIPS ISAs I, II, and III need
405 (at least) two instructions between the reads of HI/LO and
406 instructions which write them, and later ISAs do not. Contradicting
407 the MIPS specifications, some MIPS IV processor user manuals (e.g.
408 the UM for the NEC Vr5000) document needing the instructions between
409 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
410 MIPS64 and later ISAs to have the interlocks, plus any specific
411 earlier-ISA CPUs for which CPU documentation declares that the
412 instructions are really interlocked. */
413 #define hilo_interlocks \
414 (mips_opts.isa == ISA_MIPS32 \
415 || mips_opts.isa == ISA_MIPS32R2 \
416 || mips_opts.isa == ISA_MIPS64 \
417 || mips_opts.isa == ISA_MIPS64R2 \
418 || mips_opts.arch == CPU_R4010 \
419 || mips_opts.arch == CPU_R10000 \
420 || mips_opts.arch == CPU_R12000 \
421 || mips_opts.arch == CPU_RM7000 \
422 || mips_opts.arch == CPU_VR5500 \
423 )
424
425 /* Whether the processor uses hardware interlocks to protect reads
426 from the GPRs after they are loaded from memory, and thus does not
427 require nops to be inserted. This applies to instructions marked
428 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
429 level I. */
430 #define gpr_interlocks \
431 (mips_opts.isa != ISA_MIPS1 \
432 || mips_opts.arch == CPU_R3900)
433
434 /* Whether the processor uses hardware interlocks to avoid delays
435 required by coprocessor instructions, and thus does not require
436 nops to be inserted. This applies to instructions marked
437 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
438 between instructions marked INSN_WRITE_COND_CODE and ones marked
439 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
440 levels I, II, and III. */
441 /* Itbl support may require additional care here. */
442 #define cop_interlocks \
443 ((mips_opts.isa != ISA_MIPS1 \
444 && mips_opts.isa != ISA_MIPS2 \
445 && mips_opts.isa != ISA_MIPS3) \
446 || mips_opts.arch == CPU_R4300 \
447 )
448
449 /* Whether the processor uses hardware interlocks to protect reads
450 from coprocessor registers after they are loaded from memory, and
451 thus does not require nops to be inserted. This applies to
452 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
453 requires at MIPS ISA level I. */
454 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
455
456 /* Is this a mfhi or mflo instruction? */
457 #define MF_HILO_INSN(PINFO) \
458 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
459
460 /* MIPS PIC level. */
461
462 enum mips_pic_level mips_pic;
463
464 /* 1 if we should generate 32 bit offsets from the $gp register in
465 SVR4_PIC mode. Currently has no meaning in other modes. */
466 static int mips_big_got = 0;
467
468 /* 1 if trap instructions should used for overflow rather than break
469 instructions. */
470 static int mips_trap = 0;
471
472 /* 1 if double width floating point constants should not be constructed
473 by assembling two single width halves into two single width floating
474 point registers which just happen to alias the double width destination
475 register. On some architectures this aliasing can be disabled by a bit
476 in the status register, and the setting of this bit cannot be determined
477 automatically at assemble time. */
478 static int mips_disable_float_construction;
479
480 /* Non-zero if any .set noreorder directives were used. */
481
482 static int mips_any_noreorder;
483
484 /* Non-zero if nops should be inserted when the register referenced in
485 an mfhi/mflo instruction is read in the next two instructions. */
486 static int mips_7000_hilo_fix;
487
488 /* The size of the small data section. */
489 static unsigned int g_switch_value = 8;
490 /* Whether the -G option was used. */
491 static int g_switch_seen = 0;
492
493 #define N_RMASK 0xc4
494 #define N_VFP 0xd4
495
496 /* If we can determine in advance that GP optimization won't be
497 possible, we can skip the relaxation stuff that tries to produce
498 GP-relative references. This makes delay slot optimization work
499 better.
500
501 This function can only provide a guess, but it seems to work for
502 gcc output. It needs to guess right for gcc, otherwise gcc
503 will put what it thinks is a GP-relative instruction in a branch
504 delay slot.
505
506 I don't know if a fix is needed for the SVR4_PIC mode. I've only
507 fixed it for the non-PIC mode. KR 95/04/07 */
508 static int nopic_need_relax (symbolS *, int);
509
510 /* handle of the OPCODE hash table */
511 static struct hash_control *op_hash = NULL;
512
513 /* The opcode hash table we use for the mips16. */
514 static struct hash_control *mips16_op_hash = NULL;
515
516 /* This array holds the chars that always start a comment. If the
517 pre-processor is disabled, these aren't very useful */
518 const char comment_chars[] = "#";
519
520 /* This array holds the chars that only start a comment at the beginning of
521 a line. If the line seems to have the form '# 123 filename'
522 .line and .file directives will appear in the pre-processed output */
523 /* Note that input_file.c hand checks for '#' at the beginning of the
524 first line of the input file. This is because the compiler outputs
525 #NO_APP at the beginning of its output. */
526 /* Also note that C style comments are always supported. */
527 const char line_comment_chars[] = "#";
528
529 /* This array holds machine specific line separator characters. */
530 const char line_separator_chars[] = ";";
531
532 /* Chars that can be used to separate mant from exp in floating point nums */
533 const char EXP_CHARS[] = "eE";
534
535 /* Chars that mean this number is a floating point constant */
536 /* As in 0f12.456 */
537 /* or 0d1.2345e12 */
538 const char FLT_CHARS[] = "rRsSfFdDxXpP";
539
540 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
541 changed in read.c . Ideally it shouldn't have to know about it at all,
542 but nothing is ideal around here.
543 */
544
545 static char *insn_error;
546
547 static int auto_align = 1;
548
549 /* When outputting SVR4 PIC code, the assembler needs to know the
550 offset in the stack frame from which to restore the $gp register.
551 This is set by the .cprestore pseudo-op, and saved in this
552 variable. */
553 static offsetT mips_cprestore_offset = -1;
554
555 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
556 more optimizations, it can use a register value instead of a memory-saved
557 offset and even an other register than $gp as global pointer. */
558 static offsetT mips_cpreturn_offset = -1;
559 static int mips_cpreturn_register = -1;
560 static int mips_gp_register = GP;
561 static int mips_gprel_offset = 0;
562
563 /* Whether mips_cprestore_offset has been set in the current function
564 (or whether it has already been warned about, if not). */
565 static int mips_cprestore_valid = 0;
566
567 /* This is the register which holds the stack frame, as set by the
568 .frame pseudo-op. This is needed to implement .cprestore. */
569 static int mips_frame_reg = SP;
570
571 /* Whether mips_frame_reg has been set in the current function
572 (or whether it has already been warned about, if not). */
573 static int mips_frame_reg_valid = 0;
574
575 /* To output NOP instructions correctly, we need to keep information
576 about the previous two instructions. */
577
578 /* Whether we are optimizing. The default value of 2 means to remove
579 unneeded NOPs and swap branch instructions when possible. A value
580 of 1 means to not swap branches. A value of 0 means to always
581 insert NOPs. */
582 static int mips_optimize = 2;
583
584 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
585 equivalent to seeing no -g option at all. */
586 static int mips_debug = 0;
587
588 /* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
589 #define MAX_VR4130_NOPS 4
590
591 /* The maximum number of NOPs needed to fill delay slots. */
592 #define MAX_DELAY_NOPS 2
593
594 /* The maximum number of NOPs needed for any purpose. */
595 #define MAX_NOPS 4
596
597 /* A list of previous instructions, with index 0 being the most recent.
598 We need to look back MAX_NOPS instructions when filling delay slots
599 or working around processor errata. We need to look back one
600 instruction further if we're thinking about using history[0] to
601 fill a branch delay slot. */
602 static struct mips_cl_insn history[1 + MAX_NOPS];
603
604 /* Nop instructions used by emit_nop. */
605 static struct mips_cl_insn nop_insn, mips16_nop_insn;
606
607 /* The appropriate nop for the current mode. */
608 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
609
610 /* If this is set, it points to a frag holding nop instructions which
611 were inserted before the start of a noreorder section. If those
612 nops turn out to be unnecessary, the size of the frag can be
613 decreased. */
614 static fragS *prev_nop_frag;
615
616 /* The number of nop instructions we created in prev_nop_frag. */
617 static int prev_nop_frag_holds;
618
619 /* The number of nop instructions that we know we need in
620 prev_nop_frag. */
621 static int prev_nop_frag_required;
622
623 /* The number of instructions we've seen since prev_nop_frag. */
624 static int prev_nop_frag_since;
625
626 /* For ECOFF and ELF, relocations against symbols are done in two
627 parts, with a HI relocation and a LO relocation. Each relocation
628 has only 16 bits of space to store an addend. This means that in
629 order for the linker to handle carries correctly, it must be able
630 to locate both the HI and the LO relocation. This means that the
631 relocations must appear in order in the relocation table.
632
633 In order to implement this, we keep track of each unmatched HI
634 relocation. We then sort them so that they immediately precede the
635 corresponding LO relocation. */
636
637 struct mips_hi_fixup
638 {
639 /* Next HI fixup. */
640 struct mips_hi_fixup *next;
641 /* This fixup. */
642 fixS *fixp;
643 /* The section this fixup is in. */
644 segT seg;
645 };
646
647 /* The list of unmatched HI relocs. */
648
649 static struct mips_hi_fixup *mips_hi_fixup_list;
650
651 /* The frag containing the last explicit relocation operator.
652 Null if explicit relocations have not been used. */
653
654 static fragS *prev_reloc_op_frag;
655
656 /* Map normal MIPS register numbers to mips16 register numbers. */
657
658 #define X ILLEGAL_REG
659 static const int mips32_to_16_reg_map[] =
660 {
661 X, X, 2, 3, 4, 5, 6, 7,
662 X, X, X, X, X, X, X, X,
663 0, 1, X, X, X, X, X, X,
664 X, X, X, X, X, X, X, X
665 };
666 #undef X
667
668 /* Map mips16 register numbers to normal MIPS register numbers. */
669
670 static const unsigned int mips16_to_32_reg_map[] =
671 {
672 16, 17, 2, 3, 4, 5, 6, 7
673 };
674
675 /* Classifies the kind of instructions we're interested in when
676 implementing -mfix-vr4120. */
677 enum fix_vr4120_class {
678 FIX_VR4120_MACC,
679 FIX_VR4120_DMACC,
680 FIX_VR4120_MULT,
681 FIX_VR4120_DMULT,
682 FIX_VR4120_DIV,
683 FIX_VR4120_MTHILO,
684 NUM_FIX_VR4120_CLASSES
685 };
686
687 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
688 there must be at least one other instruction between an instruction
689 of type X and an instruction of type Y. */
690 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
691
692 /* True if -mfix-vr4120 is in force. */
693 static int mips_fix_vr4120;
694
695 /* ...likewise -mfix-vr4130. */
696 static int mips_fix_vr4130;
697
698 /* We don't relax branches by default, since this causes us to expand
699 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
700 fail to compute the offset before expanding the macro to the most
701 efficient expansion. */
702
703 static int mips_relax_branch;
704 \f
705 /* The expansion of many macros depends on the type of symbol that
706 they refer to. For example, when generating position-dependent code,
707 a macro that refers to a symbol may have two different expansions,
708 one which uses GP-relative addresses and one which uses absolute
709 addresses. When generating SVR4-style PIC, a macro may have
710 different expansions for local and global symbols.
711
712 We handle these situations by generating both sequences and putting
713 them in variant frags. In position-dependent code, the first sequence
714 will be the GP-relative one and the second sequence will be the
715 absolute one. In SVR4 PIC, the first sequence will be for global
716 symbols and the second will be for local symbols.
717
718 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
719 SECOND are the lengths of the two sequences in bytes. These fields
720 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
721 the subtype has the following flags:
722
723 RELAX_USE_SECOND
724 Set if it has been decided that we should use the second
725 sequence instead of the first.
726
727 RELAX_SECOND_LONGER
728 Set in the first variant frag if the macro's second implementation
729 is longer than its first. This refers to the macro as a whole,
730 not an individual relaxation.
731
732 RELAX_NOMACRO
733 Set in the first variant frag if the macro appeared in a .set nomacro
734 block and if one alternative requires a warning but the other does not.
735
736 RELAX_DELAY_SLOT
737 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
738 delay slot.
739
740 The frag's "opcode" points to the first fixup for relaxable code.
741
742 Relaxable macros are generated using a sequence such as:
743
744 relax_start (SYMBOL);
745 ... generate first expansion ...
746 relax_switch ();
747 ... generate second expansion ...
748 relax_end ();
749
750 The code and fixups for the unwanted alternative are discarded
751 by md_convert_frag. */
752 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
753
754 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
755 #define RELAX_SECOND(X) ((X) & 0xff)
756 #define RELAX_USE_SECOND 0x10000
757 #define RELAX_SECOND_LONGER 0x20000
758 #define RELAX_NOMACRO 0x40000
759 #define RELAX_DELAY_SLOT 0x80000
760
761 /* Branch without likely bit. If label is out of range, we turn:
762
763 beq reg1, reg2, label
764 delay slot
765
766 into
767
768 bne reg1, reg2, 0f
769 nop
770 j label
771 0: delay slot
772
773 with the following opcode replacements:
774
775 beq <-> bne
776 blez <-> bgtz
777 bltz <-> bgez
778 bc1f <-> bc1t
779
780 bltzal <-> bgezal (with jal label instead of j label)
781
782 Even though keeping the delay slot instruction in the delay slot of
783 the branch would be more efficient, it would be very tricky to do
784 correctly, because we'd have to introduce a variable frag *after*
785 the delay slot instruction, and expand that instead. Let's do it
786 the easy way for now, even if the branch-not-taken case now costs
787 one additional instruction. Out-of-range branches are not supposed
788 to be common, anyway.
789
790 Branch likely. If label is out of range, we turn:
791
792 beql reg1, reg2, label
793 delay slot (annulled if branch not taken)
794
795 into
796
797 beql reg1, reg2, 1f
798 nop
799 beql $0, $0, 2f
800 nop
801 1: j[al] label
802 delay slot (executed only if branch taken)
803 2:
804
805 It would be possible to generate a shorter sequence by losing the
806 likely bit, generating something like:
807
808 bne reg1, reg2, 0f
809 nop
810 j[al] label
811 delay slot (executed only if branch taken)
812 0:
813
814 beql -> bne
815 bnel -> beq
816 blezl -> bgtz
817 bgtzl -> blez
818 bltzl -> bgez
819 bgezl -> bltz
820 bc1fl -> bc1t
821 bc1tl -> bc1f
822
823 bltzall -> bgezal (with jal label instead of j label)
824 bgezall -> bltzal (ditto)
825
826
827 but it's not clear that it would actually improve performance. */
828 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
829 ((relax_substateT) \
830 (0xc0000000 \
831 | ((toofar) ? 1 : 0) \
832 | ((link) ? 2 : 0) \
833 | ((likely) ? 4 : 0) \
834 | ((uncond) ? 8 : 0)))
835 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
836 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
837 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
838 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
839 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
840
841 /* For mips16 code, we use an entirely different form of relaxation.
842 mips16 supports two versions of most instructions which take
843 immediate values: a small one which takes some small value, and a
844 larger one which takes a 16 bit value. Since branches also follow
845 this pattern, relaxing these values is required.
846
847 We can assemble both mips16 and normal MIPS code in a single
848 object. Therefore, we need to support this type of relaxation at
849 the same time that we support the relaxation described above. We
850 use the high bit of the subtype field to distinguish these cases.
851
852 The information we store for this type of relaxation is the
853 argument code found in the opcode file for this relocation, whether
854 the user explicitly requested a small or extended form, and whether
855 the relocation is in a jump or jal delay slot. That tells us the
856 size of the value, and how it should be stored. We also store
857 whether the fragment is considered to be extended or not. We also
858 store whether this is known to be a branch to a different section,
859 whether we have tried to relax this frag yet, and whether we have
860 ever extended a PC relative fragment because of a shift count. */
861 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
862 (0x80000000 \
863 | ((type) & 0xff) \
864 | ((small) ? 0x100 : 0) \
865 | ((ext) ? 0x200 : 0) \
866 | ((dslot) ? 0x400 : 0) \
867 | ((jal_dslot) ? 0x800 : 0))
868 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
869 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
870 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
871 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
872 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
873 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
874 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
875 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
876 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
877 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
878 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
879 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
880
881 /* Is the given value a sign-extended 32-bit value? */
882 #define IS_SEXT_32BIT_NUM(x) \
883 (((x) &~ (offsetT) 0x7fffffff) == 0 \
884 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
885
886 /* Is the given value a sign-extended 16-bit value? */
887 #define IS_SEXT_16BIT_NUM(x) \
888 (((x) &~ (offsetT) 0x7fff) == 0 \
889 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
890
891 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
892 #define IS_ZEXT_32BIT_NUM(x) \
893 (((x) &~ (offsetT) 0xffffffff) == 0 \
894 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
895
896 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
897 VALUE << SHIFT. VALUE is evaluated exactly once. */
898 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
899 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
900 | (((VALUE) & (MASK)) << (SHIFT)))
901
902 /* Extract bits MASK << SHIFT from STRUCT and shift them right
903 SHIFT places. */
904 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
905 (((STRUCT) >> (SHIFT)) & (MASK))
906
907 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
908 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
909
910 include/opcode/mips.h specifies operand fields using the macros
911 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
912 with "MIPS16OP" instead of "OP". */
913 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
914 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
915 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
916 INSERT_BITS ((INSN).insn_opcode, VALUE, \
917 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
918
919 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
920 #define EXTRACT_OPERAND(FIELD, INSN) \
921 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
922 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
923 EXTRACT_BITS ((INSN).insn_opcode, \
924 MIPS16OP_MASK_##FIELD, \
925 MIPS16OP_SH_##FIELD)
926 \f
927 /* Global variables used when generating relaxable macros. See the
928 comment above RELAX_ENCODE for more details about how relaxation
929 is used. */
930 static struct {
931 /* 0 if we're not emitting a relaxable macro.
932 1 if we're emitting the first of the two relaxation alternatives.
933 2 if we're emitting the second alternative. */
934 int sequence;
935
936 /* The first relaxable fixup in the current frag. (In other words,
937 the first fixup that refers to relaxable code.) */
938 fixS *first_fixup;
939
940 /* sizes[0] says how many bytes of the first alternative are stored in
941 the current frag. Likewise sizes[1] for the second alternative. */
942 unsigned int sizes[2];
943
944 /* The symbol on which the choice of sequence depends. */
945 symbolS *symbol;
946 } mips_relax;
947 \f
948 /* Global variables used to decide whether a macro needs a warning. */
949 static struct {
950 /* True if the macro is in a branch delay slot. */
951 bfd_boolean delay_slot_p;
952
953 /* For relaxable macros, sizes[0] is the length of the first alternative
954 in bytes and sizes[1] is the length of the second alternative.
955 For non-relaxable macros, both elements give the length of the
956 macro in bytes. */
957 unsigned int sizes[2];
958
959 /* The first variant frag for this macro. */
960 fragS *first_frag;
961 } mips_macro_warning;
962 \f
963 /* Prototypes for static functions. */
964
965 #define internalError() \
966 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
967
968 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
969
970 static void append_insn
971 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
972 static void mips_no_prev_insn (void);
973 static void mips16_macro_build
974 (expressionS *, const char *, const char *, va_list);
975 static void load_register (int, expressionS *, int);
976 static void macro_start (void);
977 static void macro_end (void);
978 static void macro (struct mips_cl_insn * ip);
979 static void mips16_macro (struct mips_cl_insn * ip);
980 #ifdef LOSING_COMPILER
981 static void macro2 (struct mips_cl_insn * ip);
982 #endif
983 static void mips_ip (char *str, struct mips_cl_insn * ip);
984 static void mips16_ip (char *str, struct mips_cl_insn * ip);
985 static void mips16_immed
986 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
987 unsigned long *, bfd_boolean *, unsigned short *);
988 static size_t my_getSmallExpression
989 (expressionS *, bfd_reloc_code_real_type *, char *);
990 static void my_getExpression (expressionS *, char *);
991 static void s_align (int);
992 static void s_change_sec (int);
993 static void s_change_section (int);
994 static void s_cons (int);
995 static void s_float_cons (int);
996 static void s_mips_globl (int);
997 static void s_option (int);
998 static void s_mipsset (int);
999 static void s_abicalls (int);
1000 static void s_cpload (int);
1001 static void s_cpsetup (int);
1002 static void s_cplocal (int);
1003 static void s_cprestore (int);
1004 static void s_cpreturn (int);
1005 static void s_gpvalue (int);
1006 static void s_gpword (int);
1007 static void s_gpdword (int);
1008 static void s_cpadd (int);
1009 static void s_insn (int);
1010 static void md_obj_begin (void);
1011 static void md_obj_end (void);
1012 static void s_mips_ent (int);
1013 static void s_mips_end (int);
1014 static void s_mips_frame (int);
1015 static void s_mips_mask (int reg_type);
1016 static void s_mips_stab (int);
1017 static void s_mips_weakext (int);
1018 static void s_mips_file (int);
1019 static void s_mips_loc (int);
1020 static bfd_boolean pic_need_relax (symbolS *, asection *);
1021 static int relaxed_branch_length (fragS *, asection *, int);
1022 static int validate_mips_insn (const struct mips_opcode *);
1023
1024 /* Table and functions used to map between CPU/ISA names, and
1025 ISA levels, and CPU numbers. */
1026
1027 struct mips_cpu_info
1028 {
1029 const char *name; /* CPU or ISA name. */
1030 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
1031 int isa; /* ISA level. */
1032 int cpu; /* CPU number (default CPU if ISA). */
1033 };
1034
1035 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1036 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1037 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
1038 \f
1039 /* Pseudo-op table.
1040
1041 The following pseudo-ops from the Kane and Heinrich MIPS book
1042 should be defined here, but are currently unsupported: .alias,
1043 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1044
1045 The following pseudo-ops from the Kane and Heinrich MIPS book are
1046 specific to the type of debugging information being generated, and
1047 should be defined by the object format: .aent, .begin, .bend,
1048 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1049 .vreg.
1050
1051 The following pseudo-ops from the Kane and Heinrich MIPS book are
1052 not MIPS CPU specific, but are also not specific to the object file
1053 format. This file is probably the best place to define them, but
1054 they are not currently supported: .asm0, .endr, .lab, .repeat,
1055 .struct. */
1056
1057 static const pseudo_typeS mips_pseudo_table[] =
1058 {
1059 /* MIPS specific pseudo-ops. */
1060 {"option", s_option, 0},
1061 {"set", s_mipsset, 0},
1062 {"rdata", s_change_sec, 'r'},
1063 {"sdata", s_change_sec, 's'},
1064 {"livereg", s_ignore, 0},
1065 {"abicalls", s_abicalls, 0},
1066 {"cpload", s_cpload, 0},
1067 {"cpsetup", s_cpsetup, 0},
1068 {"cplocal", s_cplocal, 0},
1069 {"cprestore", s_cprestore, 0},
1070 {"cpreturn", s_cpreturn, 0},
1071 {"gpvalue", s_gpvalue, 0},
1072 {"gpword", s_gpword, 0},
1073 {"gpdword", s_gpdword, 0},
1074 {"cpadd", s_cpadd, 0},
1075 {"insn", s_insn, 0},
1076
1077 /* Relatively generic pseudo-ops that happen to be used on MIPS
1078 chips. */
1079 {"asciiz", stringer, 1},
1080 {"bss", s_change_sec, 'b'},
1081 {"err", s_err, 0},
1082 {"half", s_cons, 1},
1083 {"dword", s_cons, 3},
1084 {"weakext", s_mips_weakext, 0},
1085
1086 /* These pseudo-ops are defined in read.c, but must be overridden
1087 here for one reason or another. */
1088 {"align", s_align, 0},
1089 {"byte", s_cons, 0},
1090 {"data", s_change_sec, 'd'},
1091 {"double", s_float_cons, 'd'},
1092 {"float", s_float_cons, 'f'},
1093 {"globl", s_mips_globl, 0},
1094 {"global", s_mips_globl, 0},
1095 {"hword", s_cons, 1},
1096 {"int", s_cons, 2},
1097 {"long", s_cons, 2},
1098 {"octa", s_cons, 4},
1099 {"quad", s_cons, 3},
1100 {"section", s_change_section, 0},
1101 {"short", s_cons, 1},
1102 {"single", s_float_cons, 'f'},
1103 {"stabn", s_mips_stab, 'n'},
1104 {"text", s_change_sec, 't'},
1105 {"word", s_cons, 2},
1106
1107 { "extern", ecoff_directive_extern, 0},
1108
1109 { NULL, NULL, 0 },
1110 };
1111
1112 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1113 {
1114 /* These pseudo-ops should be defined by the object file format.
1115 However, a.out doesn't support them, so we have versions here. */
1116 {"aent", s_mips_ent, 1},
1117 {"bgnb", s_ignore, 0},
1118 {"end", s_mips_end, 0},
1119 {"endb", s_ignore, 0},
1120 {"ent", s_mips_ent, 0},
1121 {"file", s_mips_file, 0},
1122 {"fmask", s_mips_mask, 'F'},
1123 {"frame", s_mips_frame, 0},
1124 {"loc", s_mips_loc, 0},
1125 {"mask", s_mips_mask, 'R'},
1126 {"verstamp", s_ignore, 0},
1127 { NULL, NULL, 0 },
1128 };
1129
1130 extern void pop_insert (const pseudo_typeS *);
1131
1132 void
1133 mips_pop_insert (void)
1134 {
1135 pop_insert (mips_pseudo_table);
1136 if (! ECOFF_DEBUGGING)
1137 pop_insert (mips_nonecoff_pseudo_table);
1138 }
1139 \f
1140 /* Symbols labelling the current insn. */
1141
1142 struct insn_label_list
1143 {
1144 struct insn_label_list *next;
1145 symbolS *label;
1146 };
1147
1148 static struct insn_label_list *insn_labels;
1149 static struct insn_label_list *free_insn_labels;
1150
1151 static void mips_clear_insn_labels (void);
1152
1153 static inline void
1154 mips_clear_insn_labels (void)
1155 {
1156 register struct insn_label_list **pl;
1157
1158 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1159 ;
1160 *pl = insn_labels;
1161 insn_labels = NULL;
1162 }
1163 \f
1164 static char *expr_end;
1165
1166 /* Expressions which appear in instructions. These are set by
1167 mips_ip. */
1168
1169 static expressionS imm_expr;
1170 static expressionS imm2_expr;
1171 static expressionS offset_expr;
1172
1173 /* Relocs associated with imm_expr and offset_expr. */
1174
1175 static bfd_reloc_code_real_type imm_reloc[3]
1176 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1177 static bfd_reloc_code_real_type offset_reloc[3]
1178 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1179
1180 /* These are set by mips16_ip if an explicit extension is used. */
1181
1182 static bfd_boolean mips16_small, mips16_ext;
1183
1184 #ifdef OBJ_ELF
1185 /* The pdr segment for per procedure frame/regmask info. Not used for
1186 ECOFF debugging. */
1187
1188 static segT pdr_seg;
1189 #endif
1190
1191 /* The default target format to use. */
1192
1193 const char *
1194 mips_target_format (void)
1195 {
1196 switch (OUTPUT_FLAVOR)
1197 {
1198 case bfd_target_ecoff_flavour:
1199 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1200 case bfd_target_coff_flavour:
1201 return "pe-mips";
1202 case bfd_target_elf_flavour:
1203 #ifdef TE_VXWORKS
1204 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1205 return (target_big_endian
1206 ? "elf32-bigmips-vxworks"
1207 : "elf32-littlemips-vxworks");
1208 #endif
1209 #ifdef TE_TMIPS
1210 /* This is traditional mips. */
1211 return (target_big_endian
1212 ? (HAVE_64BIT_OBJECTS
1213 ? "elf64-tradbigmips"
1214 : (HAVE_NEWABI
1215 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1216 : (HAVE_64BIT_OBJECTS
1217 ? "elf64-tradlittlemips"
1218 : (HAVE_NEWABI
1219 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1220 #else
1221 return (target_big_endian
1222 ? (HAVE_64BIT_OBJECTS
1223 ? "elf64-bigmips"
1224 : (HAVE_NEWABI
1225 ? "elf32-nbigmips" : "elf32-bigmips"))
1226 : (HAVE_64BIT_OBJECTS
1227 ? "elf64-littlemips"
1228 : (HAVE_NEWABI
1229 ? "elf32-nlittlemips" : "elf32-littlemips")));
1230 #endif
1231 default:
1232 abort ();
1233 return NULL;
1234 }
1235 }
1236
1237 /* Return the length of instruction INSN. */
1238
1239 static inline unsigned int
1240 insn_length (const struct mips_cl_insn *insn)
1241 {
1242 if (!mips_opts.mips16)
1243 return 4;
1244 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1245 }
1246
1247 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1248
1249 static void
1250 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1251 {
1252 size_t i;
1253
1254 insn->insn_mo = mo;
1255 insn->use_extend = FALSE;
1256 insn->extend = 0;
1257 insn->insn_opcode = mo->match;
1258 insn->frag = NULL;
1259 insn->where = 0;
1260 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1261 insn->fixp[i] = NULL;
1262 insn->fixed_p = (mips_opts.noreorder > 0);
1263 insn->noreorder_p = (mips_opts.noreorder > 0);
1264 insn->mips16_absolute_jump_p = 0;
1265 }
1266
1267 /* Install INSN at the location specified by its "frag" and "where" fields. */
1268
1269 static void
1270 install_insn (const struct mips_cl_insn *insn)
1271 {
1272 char *f = insn->frag->fr_literal + insn->where;
1273 if (!mips_opts.mips16)
1274 md_number_to_chars (f, insn->insn_opcode, 4);
1275 else if (insn->mips16_absolute_jump_p)
1276 {
1277 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1278 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1279 }
1280 else
1281 {
1282 if (insn->use_extend)
1283 {
1284 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1285 f += 2;
1286 }
1287 md_number_to_chars (f, insn->insn_opcode, 2);
1288 }
1289 }
1290
1291 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1292 and install the opcode in the new location. */
1293
1294 static void
1295 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1296 {
1297 size_t i;
1298
1299 insn->frag = frag;
1300 insn->where = where;
1301 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1302 if (insn->fixp[i] != NULL)
1303 {
1304 insn->fixp[i]->fx_frag = frag;
1305 insn->fixp[i]->fx_where = where;
1306 }
1307 install_insn (insn);
1308 }
1309
1310 /* Add INSN to the end of the output. */
1311
1312 static void
1313 add_fixed_insn (struct mips_cl_insn *insn)
1314 {
1315 char *f = frag_more (insn_length (insn));
1316 move_insn (insn, frag_now, f - frag_now->fr_literal);
1317 }
1318
1319 /* Start a variant frag and move INSN to the start of the variant part,
1320 marking it as fixed. The other arguments are as for frag_var. */
1321
1322 static void
1323 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1324 relax_substateT subtype, symbolS *symbol, offsetT offset)
1325 {
1326 frag_grow (max_chars);
1327 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1328 insn->fixed_p = 1;
1329 frag_var (rs_machine_dependent, max_chars, var,
1330 subtype, symbol, offset, NULL);
1331 }
1332
1333 /* Insert N copies of INSN into the history buffer, starting at
1334 position FIRST. Neither FIRST nor N need to be clipped. */
1335
1336 static void
1337 insert_into_history (unsigned int first, unsigned int n,
1338 const struct mips_cl_insn *insn)
1339 {
1340 if (mips_relax.sequence != 2)
1341 {
1342 unsigned int i;
1343
1344 for (i = ARRAY_SIZE (history); i-- > first;)
1345 if (i >= first + n)
1346 history[i] = history[i - n];
1347 else
1348 history[i] = *insn;
1349 }
1350 }
1351
1352 /* Emit a nop instruction, recording it in the history buffer. */
1353
1354 static void
1355 emit_nop (void)
1356 {
1357 add_fixed_insn (NOP_INSN);
1358 insert_into_history (0, 1, NOP_INSN);
1359 }
1360
1361 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1362 the idea is to make it obvious at a glance that each errata is
1363 included. */
1364
1365 static void
1366 init_vr4120_conflicts (void)
1367 {
1368 #define CONFLICT(FIRST, SECOND) \
1369 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1370
1371 /* Errata 21 - [D]DIV[U] after [D]MACC */
1372 CONFLICT (MACC, DIV);
1373 CONFLICT (DMACC, DIV);
1374
1375 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1376 CONFLICT (DMULT, DMULT);
1377 CONFLICT (DMULT, DMACC);
1378 CONFLICT (DMACC, DMULT);
1379 CONFLICT (DMACC, DMACC);
1380
1381 /* Errata 24 - MT{LO,HI} after [D]MACC */
1382 CONFLICT (MACC, MTHILO);
1383 CONFLICT (DMACC, MTHILO);
1384
1385 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1386 instruction is executed immediately after a MACC or DMACC
1387 instruction, the result of [either instruction] is incorrect." */
1388 CONFLICT (MACC, MULT);
1389 CONFLICT (MACC, DMULT);
1390 CONFLICT (DMACC, MULT);
1391 CONFLICT (DMACC, DMULT);
1392
1393 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1394 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1395 DDIV or DDIVU instruction, the result of the MACC or
1396 DMACC instruction is incorrect.". */
1397 CONFLICT (DMULT, MACC);
1398 CONFLICT (DMULT, DMACC);
1399 CONFLICT (DIV, MACC);
1400 CONFLICT (DIV, DMACC);
1401
1402 #undef CONFLICT
1403 }
1404
1405 /* This function is called once, at assembler startup time. It should
1406 set up all the tables, etc. that the MD part of the assembler will need. */
1407
1408 void
1409 md_begin (void)
1410 {
1411 register const char *retval = NULL;
1412 int i = 0;
1413 int broken = 0;
1414
1415 if (mips_pic != NO_PIC)
1416 {
1417 if (g_switch_seen && g_switch_value != 0)
1418 as_bad (_("-G may not be used in position-independent code"));
1419 g_switch_value = 0;
1420 }
1421
1422 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1423 as_warn (_("Could not set architecture and machine"));
1424
1425 op_hash = hash_new ();
1426
1427 for (i = 0; i < NUMOPCODES;)
1428 {
1429 const char *name = mips_opcodes[i].name;
1430
1431 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1432 if (retval != NULL)
1433 {
1434 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1435 mips_opcodes[i].name, retval);
1436 /* Probably a memory allocation problem? Give up now. */
1437 as_fatal (_("Broken assembler. No assembly attempted."));
1438 }
1439 do
1440 {
1441 if (mips_opcodes[i].pinfo != INSN_MACRO)
1442 {
1443 if (!validate_mips_insn (&mips_opcodes[i]))
1444 broken = 1;
1445 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1446 {
1447 create_insn (&nop_insn, mips_opcodes + i);
1448 nop_insn.fixed_p = 1;
1449 }
1450 }
1451 ++i;
1452 }
1453 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1454 }
1455
1456 mips16_op_hash = hash_new ();
1457
1458 i = 0;
1459 while (i < bfd_mips16_num_opcodes)
1460 {
1461 const char *name = mips16_opcodes[i].name;
1462
1463 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1464 if (retval != NULL)
1465 as_fatal (_("internal: can't hash `%s': %s"),
1466 mips16_opcodes[i].name, retval);
1467 do
1468 {
1469 if (mips16_opcodes[i].pinfo != INSN_MACRO
1470 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1471 != mips16_opcodes[i].match))
1472 {
1473 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1474 mips16_opcodes[i].name, mips16_opcodes[i].args);
1475 broken = 1;
1476 }
1477 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1478 {
1479 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1480 mips16_nop_insn.fixed_p = 1;
1481 }
1482 ++i;
1483 }
1484 while (i < bfd_mips16_num_opcodes
1485 && strcmp (mips16_opcodes[i].name, name) == 0);
1486 }
1487
1488 if (broken)
1489 as_fatal (_("Broken assembler. No assembly attempted."));
1490
1491 /* We add all the general register names to the symbol table. This
1492 helps us detect invalid uses of them. */
1493 for (i = 0; i < 32; i++)
1494 {
1495 char buf[5];
1496
1497 sprintf (buf, "$%d", i);
1498 symbol_table_insert (symbol_new (buf, reg_section, i,
1499 &zero_address_frag));
1500 }
1501 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1502 &zero_address_frag));
1503 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1504 &zero_address_frag));
1505 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1506 &zero_address_frag));
1507 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1508 &zero_address_frag));
1509 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1510 &zero_address_frag));
1511 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1512 &zero_address_frag));
1513 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1514 &zero_address_frag));
1515 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1516 &zero_address_frag));
1517 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1518 &zero_address_frag));
1519
1520 /* If we don't add these register names to the symbol table, they
1521 may end up being added as regular symbols by operand(), and then
1522 make it to the object file as undefined in case they're not
1523 regarded as local symbols. They're local in o32, since `$' is a
1524 local symbol prefix, but not in n32 or n64. */
1525 for (i = 0; i < 8; i++)
1526 {
1527 char buf[6];
1528
1529 sprintf (buf, "$fcc%i", i);
1530 symbol_table_insert (symbol_new (buf, reg_section, -1,
1531 &zero_address_frag));
1532 }
1533
1534 mips_no_prev_insn ();
1535
1536 mips_gprmask = 0;
1537 mips_cprmask[0] = 0;
1538 mips_cprmask[1] = 0;
1539 mips_cprmask[2] = 0;
1540 mips_cprmask[3] = 0;
1541
1542 /* set the default alignment for the text section (2**2) */
1543 record_alignment (text_section, 2);
1544
1545 bfd_set_gp_size (stdoutput, g_switch_value);
1546
1547 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1548 {
1549 /* On a native system other than VxWorks, sections must be aligned
1550 to 16 byte boundaries. When configured for an embedded ELF
1551 target, we don't bother. */
1552 if (strcmp (TARGET_OS, "elf") != 0
1553 && strcmp (TARGET_OS, "vxworks") != 0)
1554 {
1555 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1556 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1557 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1558 }
1559
1560 /* Create a .reginfo section for register masks and a .mdebug
1561 section for debugging information. */
1562 {
1563 segT seg;
1564 subsegT subseg;
1565 flagword flags;
1566 segT sec;
1567
1568 seg = now_seg;
1569 subseg = now_subseg;
1570
1571 /* The ABI says this section should be loaded so that the
1572 running program can access it. However, we don't load it
1573 if we are configured for an embedded target */
1574 flags = SEC_READONLY | SEC_DATA;
1575 if (strcmp (TARGET_OS, "elf") != 0)
1576 flags |= SEC_ALLOC | SEC_LOAD;
1577
1578 if (mips_abi != N64_ABI)
1579 {
1580 sec = subseg_new (".reginfo", (subsegT) 0);
1581
1582 bfd_set_section_flags (stdoutput, sec, flags);
1583 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1584
1585 #ifdef OBJ_ELF
1586 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1587 #endif
1588 }
1589 else
1590 {
1591 /* The 64-bit ABI uses a .MIPS.options section rather than
1592 .reginfo section. */
1593 sec = subseg_new (".MIPS.options", (subsegT) 0);
1594 bfd_set_section_flags (stdoutput, sec, flags);
1595 bfd_set_section_alignment (stdoutput, sec, 3);
1596
1597 #ifdef OBJ_ELF
1598 /* Set up the option header. */
1599 {
1600 Elf_Internal_Options opthdr;
1601 char *f;
1602
1603 opthdr.kind = ODK_REGINFO;
1604 opthdr.size = (sizeof (Elf_External_Options)
1605 + sizeof (Elf64_External_RegInfo));
1606 opthdr.section = 0;
1607 opthdr.info = 0;
1608 f = frag_more (sizeof (Elf_External_Options));
1609 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1610 (Elf_External_Options *) f);
1611
1612 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1613 }
1614 #endif
1615 }
1616
1617 if (ECOFF_DEBUGGING)
1618 {
1619 sec = subseg_new (".mdebug", (subsegT) 0);
1620 (void) bfd_set_section_flags (stdoutput, sec,
1621 SEC_HAS_CONTENTS | SEC_READONLY);
1622 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1623 }
1624 #ifdef OBJ_ELF
1625 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1626 {
1627 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1628 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1629 SEC_READONLY | SEC_RELOC
1630 | SEC_DEBUGGING);
1631 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1632 }
1633 #endif
1634
1635 subseg_set (seg, subseg);
1636 }
1637 }
1638
1639 if (! ECOFF_DEBUGGING)
1640 md_obj_begin ();
1641
1642 if (mips_fix_vr4120)
1643 init_vr4120_conflicts ();
1644 }
1645
1646 void
1647 md_mips_end (void)
1648 {
1649 if (! ECOFF_DEBUGGING)
1650 md_obj_end ();
1651 }
1652
1653 void
1654 md_assemble (char *str)
1655 {
1656 struct mips_cl_insn insn;
1657 bfd_reloc_code_real_type unused_reloc[3]
1658 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1659
1660 imm_expr.X_op = O_absent;
1661 imm2_expr.X_op = O_absent;
1662 offset_expr.X_op = O_absent;
1663 imm_reloc[0] = BFD_RELOC_UNUSED;
1664 imm_reloc[1] = BFD_RELOC_UNUSED;
1665 imm_reloc[2] = BFD_RELOC_UNUSED;
1666 offset_reloc[0] = BFD_RELOC_UNUSED;
1667 offset_reloc[1] = BFD_RELOC_UNUSED;
1668 offset_reloc[2] = BFD_RELOC_UNUSED;
1669
1670 if (mips_opts.mips16)
1671 mips16_ip (str, &insn);
1672 else
1673 {
1674 mips_ip (str, &insn);
1675 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1676 str, insn.insn_opcode));
1677 }
1678
1679 if (insn_error)
1680 {
1681 as_bad ("%s `%s'", insn_error, str);
1682 return;
1683 }
1684
1685 if (insn.insn_mo->pinfo == INSN_MACRO)
1686 {
1687 macro_start ();
1688 if (mips_opts.mips16)
1689 mips16_macro (&insn);
1690 else
1691 macro (&insn);
1692 macro_end ();
1693 }
1694 else
1695 {
1696 if (imm_expr.X_op != O_absent)
1697 append_insn (&insn, &imm_expr, imm_reloc);
1698 else if (offset_expr.X_op != O_absent)
1699 append_insn (&insn, &offset_expr, offset_reloc);
1700 else
1701 append_insn (&insn, NULL, unused_reloc);
1702 }
1703 }
1704
1705 /* Return true if the given relocation might need a matching %lo().
1706 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1707 need a matching %lo() when applied to local symbols. */
1708
1709 static inline bfd_boolean
1710 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1711 {
1712 return (HAVE_IN_PLACE_ADDENDS
1713 && (reloc == BFD_RELOC_HI16_S
1714 || reloc == BFD_RELOC_MIPS16_HI16_S
1715 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
1716 all GOT16 relocations evaluate to "G". */
1717 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
1718 }
1719
1720 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1721 relocation. */
1722
1723 static inline bfd_boolean
1724 fixup_has_matching_lo_p (fixS *fixp)
1725 {
1726 return (fixp->fx_next != NULL
1727 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1728 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1729 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1730 && fixp->fx_offset == fixp->fx_next->fx_offset);
1731 }
1732
1733 /* See whether instruction IP reads register REG. CLASS is the type
1734 of register. */
1735
1736 static int
1737 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
1738 enum mips_regclass class)
1739 {
1740 if (class == MIPS16_REG)
1741 {
1742 assert (mips_opts.mips16);
1743 reg = mips16_to_32_reg_map[reg];
1744 class = MIPS_GR_REG;
1745 }
1746
1747 /* Don't report on general register ZERO, since it never changes. */
1748 if (class == MIPS_GR_REG && reg == ZERO)
1749 return 0;
1750
1751 if (class == MIPS_FP_REG)
1752 {
1753 assert (! mips_opts.mips16);
1754 /* If we are called with either $f0 or $f1, we must check $f0.
1755 This is not optimal, because it will introduce an unnecessary
1756 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1757 need to distinguish reading both $f0 and $f1 or just one of
1758 them. Note that we don't have to check the other way,
1759 because there is no instruction that sets both $f0 and $f1
1760 and requires a delay. */
1761 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1762 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
1763 == (reg &~ (unsigned) 1)))
1764 return 1;
1765 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1766 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
1767 == (reg &~ (unsigned) 1)))
1768 return 1;
1769 }
1770 else if (! mips_opts.mips16)
1771 {
1772 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1773 && EXTRACT_OPERAND (RS, *ip) == reg)
1774 return 1;
1775 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1776 && EXTRACT_OPERAND (RT, *ip) == reg)
1777 return 1;
1778 }
1779 else
1780 {
1781 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1782 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
1783 return 1;
1784 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1785 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
1786 return 1;
1787 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1788 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
1789 == reg))
1790 return 1;
1791 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1792 return 1;
1793 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1794 return 1;
1795 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1796 return 1;
1797 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1798 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
1799 return 1;
1800 }
1801
1802 return 0;
1803 }
1804
1805 /* This function returns true if modifying a register requires a
1806 delay. */
1807
1808 static int
1809 reg_needs_delay (unsigned int reg)
1810 {
1811 unsigned long prev_pinfo;
1812
1813 prev_pinfo = history[0].insn_mo->pinfo;
1814 if (! mips_opts.noreorder
1815 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1816 && ! gpr_interlocks)
1817 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1818 && ! cop_interlocks)))
1819 {
1820 /* A load from a coprocessor or from memory. All load delays
1821 delay the use of general register rt for one instruction. */
1822 /* Itbl support may require additional care here. */
1823 know (prev_pinfo & INSN_WRITE_GPR_T);
1824 if (reg == EXTRACT_OPERAND (RT, history[0]))
1825 return 1;
1826 }
1827
1828 return 0;
1829 }
1830
1831 /* Move all labels in insn_labels to the current insertion point. */
1832
1833 static void
1834 mips_move_labels (void)
1835 {
1836 struct insn_label_list *l;
1837 valueT val;
1838
1839 for (l = insn_labels; l != NULL; l = l->next)
1840 {
1841 assert (S_GET_SEGMENT (l->label) == now_seg);
1842 symbol_set_frag (l->label, frag_now);
1843 val = (valueT) frag_now_fix ();
1844 /* mips16 text labels are stored as odd. */
1845 if (mips_opts.mips16)
1846 ++val;
1847 S_SET_VALUE (l->label, val);
1848 }
1849 }
1850
1851 /* Mark instruction labels in mips16 mode. This permits the linker to
1852 handle them specially, such as generating jalx instructions when
1853 needed. We also make them odd for the duration of the assembly, in
1854 order to generate the right sort of code. We will make them even
1855 in the adjust_symtab routine, while leaving them marked. This is
1856 convenient for the debugger and the disassembler. The linker knows
1857 to make them odd again. */
1858
1859 static void
1860 mips16_mark_labels (void)
1861 {
1862 if (mips_opts.mips16)
1863 {
1864 struct insn_label_list *l;
1865 valueT val;
1866
1867 for (l = insn_labels; l != NULL; l = l->next)
1868 {
1869 #ifdef OBJ_ELF
1870 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1871 S_SET_OTHER (l->label, STO_MIPS16);
1872 #endif
1873 val = S_GET_VALUE (l->label);
1874 if ((val & 1) == 0)
1875 S_SET_VALUE (l->label, val + 1);
1876 }
1877 }
1878 }
1879
1880 /* End the current frag. Make it a variant frag and record the
1881 relaxation info. */
1882
1883 static void
1884 relax_close_frag (void)
1885 {
1886 mips_macro_warning.first_frag = frag_now;
1887 frag_var (rs_machine_dependent, 0, 0,
1888 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1889 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1890
1891 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1892 mips_relax.first_fixup = 0;
1893 }
1894
1895 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1896 See the comment above RELAX_ENCODE for more details. */
1897
1898 static void
1899 relax_start (symbolS *symbol)
1900 {
1901 assert (mips_relax.sequence == 0);
1902 mips_relax.sequence = 1;
1903 mips_relax.symbol = symbol;
1904 }
1905
1906 /* Start generating the second version of a relaxable sequence.
1907 See the comment above RELAX_ENCODE for more details. */
1908
1909 static void
1910 relax_switch (void)
1911 {
1912 assert (mips_relax.sequence == 1);
1913 mips_relax.sequence = 2;
1914 }
1915
1916 /* End the current relaxable sequence. */
1917
1918 static void
1919 relax_end (void)
1920 {
1921 assert (mips_relax.sequence == 2);
1922 relax_close_frag ();
1923 mips_relax.sequence = 0;
1924 }
1925
1926 /* Classify an instruction according to the FIX_VR4120_* enumeration.
1927 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1928 by VR4120 errata. */
1929
1930 static unsigned int
1931 classify_vr4120_insn (const char *name)
1932 {
1933 if (strncmp (name, "macc", 4) == 0)
1934 return FIX_VR4120_MACC;
1935 if (strncmp (name, "dmacc", 5) == 0)
1936 return FIX_VR4120_DMACC;
1937 if (strncmp (name, "mult", 4) == 0)
1938 return FIX_VR4120_MULT;
1939 if (strncmp (name, "dmult", 5) == 0)
1940 return FIX_VR4120_DMULT;
1941 if (strstr (name, "div"))
1942 return FIX_VR4120_DIV;
1943 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1944 return FIX_VR4120_MTHILO;
1945 return NUM_FIX_VR4120_CLASSES;
1946 }
1947
1948 /* Return the number of instructions that must separate INSN1 and INSN2,
1949 where INSN1 is the earlier instruction. Return the worst-case value
1950 for any INSN2 if INSN2 is null. */
1951
1952 static unsigned int
1953 insns_between (const struct mips_cl_insn *insn1,
1954 const struct mips_cl_insn *insn2)
1955 {
1956 unsigned long pinfo1, pinfo2;
1957
1958 /* This function needs to know which pinfo flags are set for INSN2
1959 and which registers INSN2 uses. The former is stored in PINFO2 and
1960 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
1961 will have every flag set and INSN2_USES_REG will always return true. */
1962 pinfo1 = insn1->insn_mo->pinfo;
1963 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
1964
1965 #define INSN2_USES_REG(REG, CLASS) \
1966 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1967
1968 /* For most targets, write-after-read dependencies on the HI and LO
1969 registers must be separated by at least two instructions. */
1970 if (!hilo_interlocks)
1971 {
1972 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1973 return 2;
1974 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1975 return 2;
1976 }
1977
1978 /* If we're working around r7000 errata, there must be two instructions
1979 between an mfhi or mflo and any instruction that uses the result. */
1980 if (mips_7000_hilo_fix
1981 && MF_HILO_INSN (pinfo1)
1982 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1983 return 2;
1984
1985 /* If working around VR4120 errata, check for combinations that need
1986 a single intervening instruction. */
1987 if (mips_fix_vr4120)
1988 {
1989 unsigned int class1, class2;
1990
1991 class1 = classify_vr4120_insn (insn1->insn_mo->name);
1992 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
1993 {
1994 if (insn2 == NULL)
1995 return 1;
1996 class2 = classify_vr4120_insn (insn2->insn_mo->name);
1997 if (vr4120_conflicts[class1] & (1 << class2))
1998 return 1;
1999 }
2000 }
2001
2002 if (!mips_opts.mips16)
2003 {
2004 /* Check for GPR or coprocessor load delays. All such delays
2005 are on the RT register. */
2006 /* Itbl support may require additional care here. */
2007 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2008 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
2009 {
2010 know (pinfo1 & INSN_WRITE_GPR_T);
2011 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2012 return 1;
2013 }
2014
2015 /* Check for generic coprocessor hazards.
2016
2017 This case is not handled very well. There is no special
2018 knowledge of CP0 handling, and the coprocessors other than
2019 the floating point unit are not distinguished at all. */
2020 /* Itbl support may require additional care here. FIXME!
2021 Need to modify this to include knowledge about
2022 user specified delays! */
2023 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2024 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2025 {
2026 /* Handle cases where INSN1 writes to a known general coprocessor
2027 register. There must be a one instruction delay before INSN2
2028 if INSN2 reads that register, otherwise no delay is needed. */
2029 if (pinfo1 & INSN_WRITE_FPR_T)
2030 {
2031 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2032 return 1;
2033 }
2034 else if (pinfo1 & INSN_WRITE_FPR_S)
2035 {
2036 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2037 return 1;
2038 }
2039 else
2040 {
2041 /* Read-after-write dependencies on the control registers
2042 require a two-instruction gap. */
2043 if ((pinfo1 & INSN_WRITE_COND_CODE)
2044 && (pinfo2 & INSN_READ_COND_CODE))
2045 return 2;
2046
2047 /* We don't know exactly what INSN1 does. If INSN2 is
2048 also a coprocessor instruction, assume there must be
2049 a one instruction gap. */
2050 if (pinfo2 & INSN_COP)
2051 return 1;
2052 }
2053 }
2054
2055 /* Check for read-after-write dependencies on the coprocessor
2056 control registers in cases where INSN1 does not need a general
2057 coprocessor delay. This means that INSN1 is a floating point
2058 comparison instruction. */
2059 /* Itbl support may require additional care here. */
2060 else if (!cop_interlocks
2061 && (pinfo1 & INSN_WRITE_COND_CODE)
2062 && (pinfo2 & INSN_READ_COND_CODE))
2063 return 1;
2064 }
2065
2066 #undef INSN2_USES_REG
2067
2068 return 0;
2069 }
2070
2071 /* Return the number of nops that would be needed to work around the
2072 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2073 the MAX_VR4130_NOPS instructions described by HISTORY. */
2074
2075 static int
2076 nops_for_vr4130 (const struct mips_cl_insn *history,
2077 const struct mips_cl_insn *insn)
2078 {
2079 int i, j, reg;
2080
2081 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2082 are not affected by the errata. */
2083 if (insn != 0
2084 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2085 || strcmp (insn->insn_mo->name, "mtlo") == 0
2086 || strcmp (insn->insn_mo->name, "mthi") == 0))
2087 return 0;
2088
2089 /* Search for the first MFLO or MFHI. */
2090 for (i = 0; i < MAX_VR4130_NOPS; i++)
2091 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2092 {
2093 /* Extract the destination register. */
2094 if (mips_opts.mips16)
2095 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2096 else
2097 reg = EXTRACT_OPERAND (RD, history[i]);
2098
2099 /* No nops are needed if INSN reads that register. */
2100 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2101 return 0;
2102
2103 /* ...or if any of the intervening instructions do. */
2104 for (j = 0; j < i; j++)
2105 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2106 return 0;
2107
2108 return MAX_VR4130_NOPS - i;
2109 }
2110 return 0;
2111 }
2112
2113 /* Return the number of nops that would be needed if instruction INSN
2114 immediately followed the MAX_NOPS instructions given by HISTORY,
2115 where HISTORY[0] is the most recent instruction. If INSN is null,
2116 return the worse-case number of nops for any instruction. */
2117
2118 static int
2119 nops_for_insn (const struct mips_cl_insn *history,
2120 const struct mips_cl_insn *insn)
2121 {
2122 int i, nops, tmp_nops;
2123
2124 nops = 0;
2125 for (i = 0; i < MAX_DELAY_NOPS; i++)
2126 if (!history[i].noreorder_p)
2127 {
2128 tmp_nops = insns_between (history + i, insn) - i;
2129 if (tmp_nops > nops)
2130 nops = tmp_nops;
2131 }
2132
2133 if (mips_fix_vr4130)
2134 {
2135 tmp_nops = nops_for_vr4130 (history, insn);
2136 if (tmp_nops > nops)
2137 nops = tmp_nops;
2138 }
2139
2140 return nops;
2141 }
2142
2143 /* The variable arguments provide NUM_INSNS extra instructions that
2144 might be added to HISTORY. Return the largest number of nops that
2145 would be needed after the extended sequence. */
2146
2147 static int
2148 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2149 {
2150 va_list args;
2151 struct mips_cl_insn buffer[MAX_NOPS];
2152 struct mips_cl_insn *cursor;
2153 int nops;
2154
2155 va_start (args, history);
2156 cursor = buffer + num_insns;
2157 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2158 while (cursor > buffer)
2159 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2160
2161 nops = nops_for_insn (buffer, NULL);
2162 va_end (args);
2163 return nops;
2164 }
2165
2166 /* Like nops_for_insn, but if INSN is a branch, take into account the
2167 worst-case delay for the branch target. */
2168
2169 static int
2170 nops_for_insn_or_target (const struct mips_cl_insn *history,
2171 const struct mips_cl_insn *insn)
2172 {
2173 int nops, tmp_nops;
2174
2175 nops = nops_for_insn (history, insn);
2176 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2177 | INSN_COND_BRANCH_DELAY
2178 | INSN_COND_BRANCH_LIKELY))
2179 {
2180 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2181 if (tmp_nops > nops)
2182 nops = tmp_nops;
2183 }
2184 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2185 {
2186 tmp_nops = nops_for_sequence (1, history, insn);
2187 if (tmp_nops > nops)
2188 nops = tmp_nops;
2189 }
2190 return nops;
2191 }
2192
2193 /* Output an instruction. IP is the instruction information.
2194 ADDRESS_EXPR is an operand of the instruction to be used with
2195 RELOC_TYPE. */
2196
2197 static void
2198 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2199 bfd_reloc_code_real_type *reloc_type)
2200 {
2201 register unsigned long prev_pinfo, pinfo;
2202 relax_stateT prev_insn_frag_type = 0;
2203 bfd_boolean relaxed_branch = FALSE;
2204
2205 /* Mark instruction labels in mips16 mode. */
2206 mips16_mark_labels ();
2207
2208 prev_pinfo = history[0].insn_mo->pinfo;
2209 pinfo = ip->insn_mo->pinfo;
2210
2211 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2212 {
2213 /* There are a lot of optimizations we could do that we don't.
2214 In particular, we do not, in general, reorder instructions.
2215 If you use gcc with optimization, it will reorder
2216 instructions and generally do much more optimization then we
2217 do here; repeating all that work in the assembler would only
2218 benefit hand written assembly code, and does not seem worth
2219 it. */
2220 int nops = (mips_optimize == 0
2221 ? nops_for_insn (history, NULL)
2222 : nops_for_insn_or_target (history, ip));
2223 if (nops > 0)
2224 {
2225 fragS *old_frag;
2226 unsigned long old_frag_offset;
2227 int i;
2228
2229 old_frag = frag_now;
2230 old_frag_offset = frag_now_fix ();
2231
2232 for (i = 0; i < nops; i++)
2233 emit_nop ();
2234
2235 if (listing)
2236 {
2237 listing_prev_line ();
2238 /* We may be at the start of a variant frag. In case we
2239 are, make sure there is enough space for the frag
2240 after the frags created by listing_prev_line. The
2241 argument to frag_grow here must be at least as large
2242 as the argument to all other calls to frag_grow in
2243 this file. We don't have to worry about being in the
2244 middle of a variant frag, because the variants insert
2245 all needed nop instructions themselves. */
2246 frag_grow (40);
2247 }
2248
2249 mips_move_labels ();
2250
2251 #ifndef NO_ECOFF_DEBUGGING
2252 if (ECOFF_DEBUGGING)
2253 ecoff_fix_loc (old_frag, old_frag_offset);
2254 #endif
2255 }
2256 }
2257 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2258 {
2259 /* Work out how many nops in prev_nop_frag are needed by IP. */
2260 int nops = nops_for_insn_or_target (history, ip);
2261 assert (nops <= prev_nop_frag_holds);
2262
2263 /* Enforce NOPS as a minimum. */
2264 if (nops > prev_nop_frag_required)
2265 prev_nop_frag_required = nops;
2266
2267 if (prev_nop_frag_holds == prev_nop_frag_required)
2268 {
2269 /* Settle for the current number of nops. Update the history
2270 accordingly (for the benefit of any future .set reorder code). */
2271 prev_nop_frag = NULL;
2272 insert_into_history (prev_nop_frag_since,
2273 prev_nop_frag_holds, NOP_INSN);
2274 }
2275 else
2276 {
2277 /* Allow this instruction to replace one of the nops that was
2278 tentatively added to prev_nop_frag. */
2279 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2280 prev_nop_frag_holds--;
2281 prev_nop_frag_since++;
2282 }
2283 }
2284
2285 #ifdef OBJ_ELF
2286 /* The value passed to dwarf2_emit_insn is the distance between
2287 the beginning of the current instruction and the address that
2288 should be recorded in the debug tables. For MIPS16 debug info
2289 we want to use ISA-encoded addresses, so we pass -1 for an
2290 address higher by one than the current. */
2291 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2292 #endif
2293
2294 /* Record the frag type before frag_var. */
2295 if (history[0].frag)
2296 prev_insn_frag_type = history[0].frag->fr_type;
2297
2298 if (address_expr
2299 && *reloc_type == BFD_RELOC_16_PCREL_S2
2300 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2301 || pinfo & INSN_COND_BRANCH_LIKELY)
2302 && mips_relax_branch
2303 /* Don't try branch relaxation within .set nomacro, or within
2304 .set noat if we use $at for PIC computations. If it turns
2305 out that the branch was out-of-range, we'll get an error. */
2306 && !mips_opts.warn_about_macros
2307 && !(mips_opts.noat && mips_pic != NO_PIC)
2308 && !mips_opts.mips16)
2309 {
2310 relaxed_branch = TRUE;
2311 add_relaxed_insn (ip, (relaxed_branch_length
2312 (NULL, NULL,
2313 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2314 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2315 : 0)), 4,
2316 RELAX_BRANCH_ENCODE
2317 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2318 pinfo & INSN_COND_BRANCH_LIKELY,
2319 pinfo & INSN_WRITE_GPR_31,
2320 0),
2321 address_expr->X_add_symbol,
2322 address_expr->X_add_number);
2323 *reloc_type = BFD_RELOC_UNUSED;
2324 }
2325 else if (*reloc_type > BFD_RELOC_UNUSED)
2326 {
2327 /* We need to set up a variant frag. */
2328 assert (mips_opts.mips16 && address_expr != NULL);
2329 add_relaxed_insn (ip, 4, 0,
2330 RELAX_MIPS16_ENCODE
2331 (*reloc_type - BFD_RELOC_UNUSED,
2332 mips16_small, mips16_ext,
2333 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2334 history[0].mips16_absolute_jump_p),
2335 make_expr_symbol (address_expr), 0);
2336 }
2337 else if (mips_opts.mips16
2338 && ! ip->use_extend
2339 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2340 {
2341 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2342 /* Make sure there is enough room to swap this instruction with
2343 a following jump instruction. */
2344 frag_grow (6);
2345 add_fixed_insn (ip);
2346 }
2347 else
2348 {
2349 if (mips_opts.mips16
2350 && mips_opts.noreorder
2351 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2352 as_warn (_("extended instruction in delay slot"));
2353
2354 if (mips_relax.sequence)
2355 {
2356 /* If we've reached the end of this frag, turn it into a variant
2357 frag and record the information for the instructions we've
2358 written so far. */
2359 if (frag_room () < 4)
2360 relax_close_frag ();
2361 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2362 }
2363
2364 if (mips_relax.sequence != 2)
2365 mips_macro_warning.sizes[0] += 4;
2366 if (mips_relax.sequence != 1)
2367 mips_macro_warning.sizes[1] += 4;
2368
2369 if (mips_opts.mips16)
2370 {
2371 ip->fixed_p = 1;
2372 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2373 }
2374 add_fixed_insn (ip);
2375 }
2376
2377 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2378 {
2379 if (address_expr->X_op == O_constant)
2380 {
2381 unsigned int tmp;
2382
2383 switch (*reloc_type)
2384 {
2385 case BFD_RELOC_32:
2386 ip->insn_opcode |= address_expr->X_add_number;
2387 break;
2388
2389 case BFD_RELOC_MIPS_HIGHEST:
2390 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2391 ip->insn_opcode |= tmp & 0xffff;
2392 break;
2393
2394 case BFD_RELOC_MIPS_HIGHER:
2395 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2396 ip->insn_opcode |= tmp & 0xffff;
2397 break;
2398
2399 case BFD_RELOC_HI16_S:
2400 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2401 ip->insn_opcode |= tmp & 0xffff;
2402 break;
2403
2404 case BFD_RELOC_HI16:
2405 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2406 break;
2407
2408 case BFD_RELOC_UNUSED:
2409 case BFD_RELOC_LO16:
2410 case BFD_RELOC_MIPS_GOT_DISP:
2411 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2412 break;
2413
2414 case BFD_RELOC_MIPS_JMP:
2415 if ((address_expr->X_add_number & 3) != 0)
2416 as_bad (_("jump to misaligned address (0x%lx)"),
2417 (unsigned long) address_expr->X_add_number);
2418 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2419 break;
2420
2421 case BFD_RELOC_MIPS16_JMP:
2422 if ((address_expr->X_add_number & 3) != 0)
2423 as_bad (_("jump to misaligned address (0x%lx)"),
2424 (unsigned long) address_expr->X_add_number);
2425 ip->insn_opcode |=
2426 (((address_expr->X_add_number & 0x7c0000) << 3)
2427 | ((address_expr->X_add_number & 0xf800000) >> 7)
2428 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2429 break;
2430
2431 case BFD_RELOC_16_PCREL_S2:
2432 if ((address_expr->X_add_number & 3) != 0)
2433 as_bad (_("branch to misaligned address (0x%lx)"),
2434 (unsigned long) address_expr->X_add_number);
2435 if (mips_relax_branch)
2436 goto need_reloc;
2437 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2438 as_bad (_("branch address range overflow (0x%lx)"),
2439 (unsigned long) address_expr->X_add_number);
2440 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2441 break;
2442
2443 default:
2444 internalError ();
2445 }
2446 }
2447 else if (*reloc_type < BFD_RELOC_UNUSED)
2448 need_reloc:
2449 {
2450 reloc_howto_type *howto;
2451 int i;
2452
2453 /* In a compound relocation, it is the final (outermost)
2454 operator that determines the relocated field. */
2455 for (i = 1; i < 3; i++)
2456 if (reloc_type[i] == BFD_RELOC_UNUSED)
2457 break;
2458
2459 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2460 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2461 bfd_get_reloc_size (howto),
2462 address_expr,
2463 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2464 reloc_type[0]);
2465
2466 /* These relocations can have an addend that won't fit in
2467 4 octets for 64bit assembly. */
2468 if (HAVE_64BIT_GPRS
2469 && ! howto->partial_inplace
2470 && (reloc_type[0] == BFD_RELOC_16
2471 || reloc_type[0] == BFD_RELOC_32
2472 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2473 || reloc_type[0] == BFD_RELOC_HI16_S
2474 || reloc_type[0] == BFD_RELOC_LO16
2475 || reloc_type[0] == BFD_RELOC_GPREL16
2476 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2477 || reloc_type[0] == BFD_RELOC_GPREL32
2478 || reloc_type[0] == BFD_RELOC_64
2479 || reloc_type[0] == BFD_RELOC_CTOR
2480 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2481 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2482 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2483 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2484 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2485 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2486 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2487 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2488 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2489 ip->fixp[0]->fx_no_overflow = 1;
2490
2491 if (mips_relax.sequence)
2492 {
2493 if (mips_relax.first_fixup == 0)
2494 mips_relax.first_fixup = ip->fixp[0];
2495 }
2496 else if (reloc_needs_lo_p (*reloc_type))
2497 {
2498 struct mips_hi_fixup *hi_fixup;
2499
2500 /* Reuse the last entry if it already has a matching %lo. */
2501 hi_fixup = mips_hi_fixup_list;
2502 if (hi_fixup == 0
2503 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2504 {
2505 hi_fixup = ((struct mips_hi_fixup *)
2506 xmalloc (sizeof (struct mips_hi_fixup)));
2507 hi_fixup->next = mips_hi_fixup_list;
2508 mips_hi_fixup_list = hi_fixup;
2509 }
2510 hi_fixup->fixp = ip->fixp[0];
2511 hi_fixup->seg = now_seg;
2512 }
2513
2514 /* Add fixups for the second and third relocations, if given.
2515 Note that the ABI allows the second relocation to be
2516 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2517 moment we only use RSS_UNDEF, but we could add support
2518 for the others if it ever becomes necessary. */
2519 for (i = 1; i < 3; i++)
2520 if (reloc_type[i] != BFD_RELOC_UNUSED)
2521 {
2522 ip->fixp[i] = fix_new (ip->frag, ip->where,
2523 ip->fixp[0]->fx_size, NULL, 0,
2524 FALSE, reloc_type[i]);
2525
2526 /* Use fx_tcbit to mark compound relocs. */
2527 ip->fixp[0]->fx_tcbit = 1;
2528 ip->fixp[i]->fx_tcbit = 1;
2529 }
2530 }
2531 }
2532 install_insn (ip);
2533
2534 /* Update the register mask information. */
2535 if (! mips_opts.mips16)
2536 {
2537 if (pinfo & INSN_WRITE_GPR_D)
2538 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2539 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2540 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2541 if (pinfo & INSN_READ_GPR_S)
2542 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2543 if (pinfo & INSN_WRITE_GPR_31)
2544 mips_gprmask |= 1 << RA;
2545 if (pinfo & INSN_WRITE_FPR_D)
2546 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2547 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2548 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2549 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2550 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2551 if ((pinfo & INSN_READ_FPR_R) != 0)
2552 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2553 if (pinfo & INSN_COP)
2554 {
2555 /* We don't keep enough information to sort these cases out.
2556 The itbl support does keep this information however, although
2557 we currently don't support itbl fprmats as part of the cop
2558 instruction. May want to add this support in the future. */
2559 }
2560 /* Never set the bit for $0, which is always zero. */
2561 mips_gprmask &= ~1 << 0;
2562 }
2563 else
2564 {
2565 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2566 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2567 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2568 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2569 if (pinfo & MIPS16_INSN_WRITE_Z)
2570 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2571 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2572 mips_gprmask |= 1 << TREG;
2573 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2574 mips_gprmask |= 1 << SP;
2575 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2576 mips_gprmask |= 1 << RA;
2577 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2578 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2579 if (pinfo & MIPS16_INSN_READ_Z)
2580 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2581 if (pinfo & MIPS16_INSN_READ_GPR_X)
2582 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2583 }
2584
2585 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2586 {
2587 /* Filling the branch delay slot is more complex. We try to
2588 switch the branch with the previous instruction, which we can
2589 do if the previous instruction does not set up a condition
2590 that the branch tests and if the branch is not itself the
2591 target of any branch. */
2592 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2593 || (pinfo & INSN_COND_BRANCH_DELAY))
2594 {
2595 if (mips_optimize < 2
2596 /* If we have seen .set volatile or .set nomove, don't
2597 optimize. */
2598 || mips_opts.nomove != 0
2599 /* We can't swap if the previous instruction's position
2600 is fixed. */
2601 || history[0].fixed_p
2602 /* If the previous previous insn was in a .set
2603 noreorder, we can't swap. Actually, the MIPS
2604 assembler will swap in this situation. However, gcc
2605 configured -with-gnu-as will generate code like
2606 .set noreorder
2607 lw $4,XXX
2608 .set reorder
2609 INSN
2610 bne $4,$0,foo
2611 in which we can not swap the bne and INSN. If gcc is
2612 not configured -with-gnu-as, it does not output the
2613 .set pseudo-ops. */
2614 || history[1].noreorder_p
2615 /* If the branch is itself the target of a branch, we
2616 can not swap. We cheat on this; all we check for is
2617 whether there is a label on this instruction. If
2618 there are any branches to anything other than a
2619 label, users must use .set noreorder. */
2620 || insn_labels != NULL
2621 /* If the previous instruction is in a variant frag
2622 other than this branch's one, we cannot do the swap.
2623 This does not apply to the mips16, which uses variant
2624 frags for different purposes. */
2625 || (! mips_opts.mips16
2626 && prev_insn_frag_type == rs_machine_dependent)
2627 /* Check for conflicts between the branch and the instructions
2628 before the candidate delay slot. */
2629 || nops_for_insn (history + 1, ip) > 0
2630 /* Check for conflicts between the swapped sequence and the
2631 target of the branch. */
2632 || nops_for_sequence (2, history + 1, ip, history) > 0
2633 /* We do not swap with a trap instruction, since it
2634 complicates trap handlers to have the trap
2635 instruction be in a delay slot. */
2636 || (prev_pinfo & INSN_TRAP)
2637 /* If the branch reads a register that the previous
2638 instruction sets, we can not swap. */
2639 || (! mips_opts.mips16
2640 && (prev_pinfo & INSN_WRITE_GPR_T)
2641 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2642 MIPS_GR_REG))
2643 || (! mips_opts.mips16
2644 && (prev_pinfo & INSN_WRITE_GPR_D)
2645 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2646 MIPS_GR_REG))
2647 || (mips_opts.mips16
2648 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2649 && (insn_uses_reg
2650 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2651 MIPS16_REG)))
2652 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2653 && (insn_uses_reg
2654 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2655 MIPS16_REG)))
2656 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2657 && (insn_uses_reg
2658 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2659 MIPS16_REG)))
2660 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2661 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2662 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2663 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2664 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2665 && insn_uses_reg (ip,
2666 MIPS16OP_EXTRACT_REG32R
2667 (history[0].insn_opcode),
2668 MIPS_GR_REG))))
2669 /* If the branch writes a register that the previous
2670 instruction sets, we can not swap (we know that
2671 branches write only to RD or to $31). */
2672 || (! mips_opts.mips16
2673 && (prev_pinfo & INSN_WRITE_GPR_T)
2674 && (((pinfo & INSN_WRITE_GPR_D)
2675 && (EXTRACT_OPERAND (RT, history[0])
2676 == EXTRACT_OPERAND (RD, *ip)))
2677 || ((pinfo & INSN_WRITE_GPR_31)
2678 && EXTRACT_OPERAND (RT, history[0]) == RA)))
2679 || (! mips_opts.mips16
2680 && (prev_pinfo & INSN_WRITE_GPR_D)
2681 && (((pinfo & INSN_WRITE_GPR_D)
2682 && (EXTRACT_OPERAND (RD, history[0])
2683 == EXTRACT_OPERAND (RD, *ip)))
2684 || ((pinfo & INSN_WRITE_GPR_31)
2685 && EXTRACT_OPERAND (RD, history[0]) == RA)))
2686 || (mips_opts.mips16
2687 && (pinfo & MIPS16_INSN_WRITE_31)
2688 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2689 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2690 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2691 == RA))))
2692 /* If the branch writes a register that the previous
2693 instruction reads, we can not swap (we know that
2694 branches only write to RD or to $31). */
2695 || (! mips_opts.mips16
2696 && (pinfo & INSN_WRITE_GPR_D)
2697 && insn_uses_reg (&history[0],
2698 EXTRACT_OPERAND (RD, *ip),
2699 MIPS_GR_REG))
2700 || (! mips_opts.mips16
2701 && (pinfo & INSN_WRITE_GPR_31)
2702 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2703 || (mips_opts.mips16
2704 && (pinfo & MIPS16_INSN_WRITE_31)
2705 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2706 /* If one instruction sets a condition code and the
2707 other one uses a condition code, we can not swap. */
2708 || ((pinfo & INSN_READ_COND_CODE)
2709 && (prev_pinfo & INSN_WRITE_COND_CODE))
2710 || ((pinfo & INSN_WRITE_COND_CODE)
2711 && (prev_pinfo & INSN_READ_COND_CODE))
2712 /* If the previous instruction uses the PC, we can not
2713 swap. */
2714 || (mips_opts.mips16
2715 && (prev_pinfo & MIPS16_INSN_READ_PC))
2716 /* If the previous instruction had a fixup in mips16
2717 mode, we can not swap. This normally means that the
2718 previous instruction was a 4 byte branch anyhow. */
2719 || (mips_opts.mips16 && history[0].fixp[0])
2720 /* If the previous instruction is a sync, sync.l, or
2721 sync.p, we can not swap. */
2722 || (prev_pinfo & INSN_SYNC))
2723 {
2724 if (mips_opts.mips16
2725 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
2726 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
2727 && (mips_opts.isa == ISA_MIPS32
2728 || mips_opts.isa == ISA_MIPS32R2
2729 || mips_opts.isa == ISA_MIPS64
2730 || mips_opts.isa == ISA_MIPS64R2))
2731 {
2732 /* Convert MIPS16 jr/jalr into a "compact" jump. */
2733 ip->insn_opcode |= 0x0080;
2734 install_insn (ip);
2735 insert_into_history (0, 1, ip);
2736 }
2737 else
2738 {
2739 /* We could do even better for unconditional branches to
2740 portions of this object file; we could pick up the
2741 instruction at the destination, put it in the delay
2742 slot, and bump the destination address. */
2743 insert_into_history (0, 1, ip);
2744 emit_nop ();
2745 }
2746
2747 if (mips_relax.sequence)
2748 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2749 }
2750 else
2751 {
2752 /* It looks like we can actually do the swap. */
2753 struct mips_cl_insn delay = history[0];
2754 if (mips_opts.mips16)
2755 {
2756 know (delay.frag == ip->frag);
2757 move_insn (ip, delay.frag, delay.where);
2758 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
2759 }
2760 else if (relaxed_branch)
2761 {
2762 /* Add the delay slot instruction to the end of the
2763 current frag and shrink the fixed part of the
2764 original frag. If the branch occupies the tail of
2765 the latter, move it backwards to cover the gap. */
2766 delay.frag->fr_fix -= 4;
2767 if (delay.frag == ip->frag)
2768 move_insn (ip, ip->frag, ip->where - 4);
2769 add_fixed_insn (&delay);
2770 }
2771 else
2772 {
2773 move_insn (&delay, ip->frag, ip->where);
2774 move_insn (ip, history[0].frag, history[0].where);
2775 }
2776 history[0] = *ip;
2777 delay.fixed_p = 1;
2778 insert_into_history (0, 1, &delay);
2779 }
2780
2781 /* If that was an unconditional branch, forget the previous
2782 insn information. */
2783 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2784 mips_no_prev_insn ();
2785 }
2786 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2787 {
2788 /* We don't yet optimize a branch likely. What we should do
2789 is look at the target, copy the instruction found there
2790 into the delay slot, and increment the branch to jump to
2791 the next instruction. */
2792 insert_into_history (0, 1, ip);
2793 emit_nop ();
2794 }
2795 else
2796 insert_into_history (0, 1, ip);
2797 }
2798 else
2799 insert_into_history (0, 1, ip);
2800
2801 /* We just output an insn, so the next one doesn't have a label. */
2802 mips_clear_insn_labels ();
2803 }
2804
2805 /* Forget that there was any previous instruction or label. */
2806
2807 static void
2808 mips_no_prev_insn (void)
2809 {
2810 prev_nop_frag = NULL;
2811 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
2812 mips_clear_insn_labels ();
2813 }
2814
2815 /* This function must be called before we emit something other than
2816 instructions. It is like mips_no_prev_insn except that it inserts
2817 any NOPS that might be needed by previous instructions. */
2818
2819 void
2820 mips_emit_delays (void)
2821 {
2822 if (! mips_opts.noreorder)
2823 {
2824 int nops = nops_for_insn (history, NULL);
2825 if (nops > 0)
2826 {
2827 while (nops-- > 0)
2828 add_fixed_insn (NOP_INSN);
2829 mips_move_labels ();
2830 }
2831 }
2832 mips_no_prev_insn ();
2833 }
2834
2835 /* Start a (possibly nested) noreorder block. */
2836
2837 static void
2838 start_noreorder (void)
2839 {
2840 if (mips_opts.noreorder == 0)
2841 {
2842 unsigned int i;
2843 int nops;
2844
2845 /* None of the instructions before the .set noreorder can be moved. */
2846 for (i = 0; i < ARRAY_SIZE (history); i++)
2847 history[i].fixed_p = 1;
2848
2849 /* Insert any nops that might be needed between the .set noreorder
2850 block and the previous instructions. We will later remove any
2851 nops that turn out not to be needed. */
2852 nops = nops_for_insn (history, NULL);
2853 if (nops > 0)
2854 {
2855 if (mips_optimize != 0)
2856 {
2857 /* Record the frag which holds the nop instructions, so
2858 that we can remove them if we don't need them. */
2859 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2860 prev_nop_frag = frag_now;
2861 prev_nop_frag_holds = nops;
2862 prev_nop_frag_required = 0;
2863 prev_nop_frag_since = 0;
2864 }
2865
2866 for (; nops > 0; --nops)
2867 add_fixed_insn (NOP_INSN);
2868
2869 /* Move on to a new frag, so that it is safe to simply
2870 decrease the size of prev_nop_frag. */
2871 frag_wane (frag_now);
2872 frag_new (0);
2873 mips_move_labels ();
2874 }
2875 mips16_mark_labels ();
2876 mips_clear_insn_labels ();
2877 }
2878 mips_opts.noreorder++;
2879 mips_any_noreorder = 1;
2880 }
2881
2882 /* End a nested noreorder block. */
2883
2884 static void
2885 end_noreorder (void)
2886 {
2887 mips_opts.noreorder--;
2888 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2889 {
2890 /* Commit to inserting prev_nop_frag_required nops and go back to
2891 handling nop insertion the .set reorder way. */
2892 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2893 * (mips_opts.mips16 ? 2 : 4));
2894 insert_into_history (prev_nop_frag_since,
2895 prev_nop_frag_required, NOP_INSN);
2896 prev_nop_frag = NULL;
2897 }
2898 }
2899
2900 /* Set up global variables for the start of a new macro. */
2901
2902 static void
2903 macro_start (void)
2904 {
2905 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2906 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2907 && (history[0].insn_mo->pinfo
2908 & (INSN_UNCOND_BRANCH_DELAY
2909 | INSN_COND_BRANCH_DELAY
2910 | INSN_COND_BRANCH_LIKELY)) != 0);
2911 }
2912
2913 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2914 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2915 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2916
2917 static const char *
2918 macro_warning (relax_substateT subtype)
2919 {
2920 if (subtype & RELAX_DELAY_SLOT)
2921 return _("Macro instruction expanded into multiple instructions"
2922 " in a branch delay slot");
2923 else if (subtype & RELAX_NOMACRO)
2924 return _("Macro instruction expanded into multiple instructions");
2925 else
2926 return 0;
2927 }
2928
2929 /* Finish up a macro. Emit warnings as appropriate. */
2930
2931 static void
2932 macro_end (void)
2933 {
2934 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2935 {
2936 relax_substateT subtype;
2937
2938 /* Set up the relaxation warning flags. */
2939 subtype = 0;
2940 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2941 subtype |= RELAX_SECOND_LONGER;
2942 if (mips_opts.warn_about_macros)
2943 subtype |= RELAX_NOMACRO;
2944 if (mips_macro_warning.delay_slot_p)
2945 subtype |= RELAX_DELAY_SLOT;
2946
2947 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2948 {
2949 /* Either the macro has a single implementation or both
2950 implementations are longer than 4 bytes. Emit the
2951 warning now. */
2952 const char *msg = macro_warning (subtype);
2953 if (msg != 0)
2954 as_warn (msg);
2955 }
2956 else
2957 {
2958 /* One implementation might need a warning but the other
2959 definitely doesn't. */
2960 mips_macro_warning.first_frag->fr_subtype |= subtype;
2961 }
2962 }
2963 }
2964
2965 /* Read a macro's relocation codes from *ARGS and store them in *R.
2966 The first argument in *ARGS will be either the code for a single
2967 relocation or -1 followed by the three codes that make up a
2968 composite relocation. */
2969
2970 static void
2971 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2972 {
2973 int i, next;
2974
2975 next = va_arg (*args, int);
2976 if (next >= 0)
2977 r[0] = (bfd_reloc_code_real_type) next;
2978 else
2979 for (i = 0; i < 3; i++)
2980 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2981 }
2982
2983 /* Build an instruction created by a macro expansion. This is passed
2984 a pointer to the count of instructions created so far, an
2985 expression, the name of the instruction to build, an operand format
2986 string, and corresponding arguments. */
2987
2988 static void
2989 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2990 {
2991 const struct mips_opcode *mo;
2992 struct mips_cl_insn insn;
2993 bfd_reloc_code_real_type r[3];
2994 va_list args;
2995
2996 va_start (args, fmt);
2997
2998 if (mips_opts.mips16)
2999 {
3000 mips16_macro_build (ep, name, fmt, args);
3001 va_end (args);
3002 return;
3003 }
3004
3005 r[0] = BFD_RELOC_UNUSED;
3006 r[1] = BFD_RELOC_UNUSED;
3007 r[2] = BFD_RELOC_UNUSED;
3008 mo = (struct mips_opcode *) hash_find (op_hash, name);
3009 assert (mo);
3010 assert (strcmp (name, mo->name) == 0);
3011
3012 /* Search until we get a match for NAME. It is assumed here that
3013 macros will never generate MDMX or MIPS-3D instructions. */
3014 while (strcmp (fmt, mo->args) != 0
3015 || mo->pinfo == INSN_MACRO
3016 || !OPCODE_IS_MEMBER (mo,
3017 (mips_opts.isa
3018 | (file_ase_mips16 ? INSN_MIPS16 : 0)
3019 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
3020 mips_opts.arch)
3021 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3022 {
3023 ++mo;
3024 assert (mo->name);
3025 assert (strcmp (name, mo->name) == 0);
3026 }
3027
3028 create_insn (&insn, mo);
3029 for (;;)
3030 {
3031 switch (*fmt++)
3032 {
3033 case '\0':
3034 break;
3035
3036 case ',':
3037 case '(':
3038 case ')':
3039 continue;
3040
3041 case '+':
3042 switch (*fmt++)
3043 {
3044 case 'A':
3045 case 'E':
3046 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3047 continue;
3048
3049 case 'B':
3050 case 'F':
3051 /* Note that in the macro case, these arguments are already
3052 in MSB form. (When handling the instruction in the
3053 non-macro case, these arguments are sizes from which
3054 MSB values must be calculated.) */
3055 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
3056 continue;
3057
3058 case 'C':
3059 case 'G':
3060 case 'H':
3061 /* Note that in the macro case, these arguments are already
3062 in MSBD form. (When handling the instruction in the
3063 non-macro case, these arguments are sizes from which
3064 MSBD values must be calculated.) */
3065 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
3066 continue;
3067
3068 default:
3069 internalError ();
3070 }
3071 continue;
3072
3073 case 't':
3074 case 'w':
3075 case 'E':
3076 INSERT_OPERAND (RT, insn, va_arg (args, int));
3077 continue;
3078
3079 case 'c':
3080 INSERT_OPERAND (CODE, insn, va_arg (args, int));
3081 continue;
3082
3083 case 'T':
3084 case 'W':
3085 INSERT_OPERAND (FT, insn, va_arg (args, int));
3086 continue;
3087
3088 case 'd':
3089 case 'G':
3090 case 'K':
3091 INSERT_OPERAND (RD, insn, va_arg (args, int));
3092 continue;
3093
3094 case 'U':
3095 {
3096 int tmp = va_arg (args, int);
3097
3098 INSERT_OPERAND (RT, insn, tmp);
3099 INSERT_OPERAND (RD, insn, tmp);
3100 continue;
3101 }
3102
3103 case 'V':
3104 case 'S':
3105 INSERT_OPERAND (FS, insn, va_arg (args, int));
3106 continue;
3107
3108 case 'z':
3109 continue;
3110
3111 case '<':
3112 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
3113 continue;
3114
3115 case 'D':
3116 INSERT_OPERAND (FD, insn, va_arg (args, int));
3117 continue;
3118
3119 case 'B':
3120 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
3121 continue;
3122
3123 case 'J':
3124 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
3125 continue;
3126
3127 case 'q':
3128 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3129 continue;
3130
3131 case 'b':
3132 case 's':
3133 case 'r':
3134 case 'v':
3135 INSERT_OPERAND (RS, insn, va_arg (args, int));
3136 continue;
3137
3138 case 'i':
3139 case 'j':
3140 case 'o':
3141 macro_read_relocs (&args, r);
3142 assert (*r == BFD_RELOC_GPREL16
3143 || *r == BFD_RELOC_MIPS_LITERAL
3144 || *r == BFD_RELOC_MIPS_HIGHER
3145 || *r == BFD_RELOC_HI16_S
3146 || *r == BFD_RELOC_LO16
3147 || *r == BFD_RELOC_MIPS_GOT16
3148 || *r == BFD_RELOC_MIPS_CALL16
3149 || *r == BFD_RELOC_MIPS_GOT_DISP
3150 || *r == BFD_RELOC_MIPS_GOT_PAGE
3151 || *r == BFD_RELOC_MIPS_GOT_OFST
3152 || *r == BFD_RELOC_MIPS_GOT_LO16
3153 || *r == BFD_RELOC_MIPS_CALL_LO16);
3154 continue;
3155
3156 case 'u':
3157 macro_read_relocs (&args, r);
3158 assert (ep != NULL
3159 && (ep->X_op == O_constant
3160 || (ep->X_op == O_symbol
3161 && (*r == BFD_RELOC_MIPS_HIGHEST
3162 || *r == BFD_RELOC_HI16_S
3163 || *r == BFD_RELOC_HI16
3164 || *r == BFD_RELOC_GPREL16
3165 || *r == BFD_RELOC_MIPS_GOT_HI16
3166 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3167 continue;
3168
3169 case 'p':
3170 assert (ep != NULL);
3171
3172 /*
3173 * This allows macro() to pass an immediate expression for
3174 * creating short branches without creating a symbol.
3175 *
3176 * We don't allow branch relaxation for these branches, as
3177 * they should only appear in ".set nomacro" anyway.
3178 */
3179 if (ep->X_op == O_constant)
3180 {
3181 if ((ep->X_add_number & 3) != 0)
3182 as_bad (_("branch to misaligned address (0x%lx)"),
3183 (unsigned long) ep->X_add_number);
3184 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3185 as_bad (_("branch address range overflow (0x%lx)"),
3186 (unsigned long) ep->X_add_number);
3187 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3188 ep = NULL;
3189 }
3190 else
3191 *r = BFD_RELOC_16_PCREL_S2;
3192 continue;
3193
3194 case 'a':
3195 assert (ep != NULL);
3196 *r = BFD_RELOC_MIPS_JMP;
3197 continue;
3198
3199 case 'C':
3200 insn.insn_opcode |= va_arg (args, unsigned long);
3201 continue;
3202
3203 case 'k':
3204 insn.insn_opcode |= va_arg (args, unsigned long) << OP_SH_CACHE;
3205 continue;
3206
3207 default:
3208 internalError ();
3209 }
3210 break;
3211 }
3212 va_end (args);
3213 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3214
3215 append_insn (&insn, ep, r);
3216 }
3217
3218 static void
3219 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3220 va_list args)
3221 {
3222 struct mips_opcode *mo;
3223 struct mips_cl_insn insn;
3224 bfd_reloc_code_real_type r[3]
3225 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3226
3227 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3228 assert (mo);
3229 assert (strcmp (name, mo->name) == 0);
3230
3231 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3232 {
3233 ++mo;
3234 assert (mo->name);
3235 assert (strcmp (name, mo->name) == 0);
3236 }
3237
3238 create_insn (&insn, mo);
3239 for (;;)
3240 {
3241 int c;
3242
3243 c = *fmt++;
3244 switch (c)
3245 {
3246 case '\0':
3247 break;
3248
3249 case ',':
3250 case '(':
3251 case ')':
3252 continue;
3253
3254 case 'y':
3255 case 'w':
3256 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3257 continue;
3258
3259 case 'x':
3260 case 'v':
3261 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3262 continue;
3263
3264 case 'z':
3265 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3266 continue;
3267
3268 case 'Z':
3269 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3270 continue;
3271
3272 case '0':
3273 case 'S':
3274 case 'P':
3275 case 'R':
3276 continue;
3277
3278 case 'X':
3279 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3280 continue;
3281
3282 case 'Y':
3283 {
3284 int regno;
3285
3286 regno = va_arg (args, int);
3287 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3288 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3289 }
3290 continue;
3291
3292 case '<':
3293 case '>':
3294 case '4':
3295 case '5':
3296 case 'H':
3297 case 'W':
3298 case 'D':
3299 case 'j':
3300 case '8':
3301 case 'V':
3302 case 'C':
3303 case 'U':
3304 case 'k':
3305 case 'K':
3306 case 'p':
3307 case 'q':
3308 {
3309 assert (ep != NULL);
3310
3311 if (ep->X_op != O_constant)
3312 *r = (int) BFD_RELOC_UNUSED + c;
3313 else
3314 {
3315 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3316 FALSE, &insn.insn_opcode, &insn.use_extend,
3317 &insn.extend);
3318 ep = NULL;
3319 *r = BFD_RELOC_UNUSED;
3320 }
3321 }
3322 continue;
3323
3324 case '6':
3325 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3326 continue;
3327 }
3328
3329 break;
3330 }
3331
3332 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3333
3334 append_insn (&insn, ep, r);
3335 }
3336
3337 /*
3338 * Sign-extend 32-bit mode constants that have bit 31 set and all
3339 * higher bits unset.
3340 */
3341 static void
3342 normalize_constant_expr (expressionS *ex)
3343 {
3344 if (ex->X_op == O_constant
3345 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3346 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3347 - 0x80000000);
3348 }
3349
3350 /*
3351 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3352 * all higher bits unset.
3353 */
3354 static void
3355 normalize_address_expr (expressionS *ex)
3356 {
3357 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3358 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3359 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3360 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3361 - 0x80000000);
3362 }
3363
3364 /*
3365 * Generate a "jalr" instruction with a relocation hint to the called
3366 * function. This occurs in NewABI PIC code.
3367 */
3368 static void
3369 macro_build_jalr (expressionS *ep)
3370 {
3371 char *f = NULL;
3372
3373 if (HAVE_NEWABI)
3374 {
3375 frag_grow (8);
3376 f = frag_more (0);
3377 }
3378 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3379 if (HAVE_NEWABI)
3380 fix_new_exp (frag_now, f - frag_now->fr_literal,
3381 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3382 }
3383
3384 /*
3385 * Generate a "lui" instruction.
3386 */
3387 static void
3388 macro_build_lui (expressionS *ep, int regnum)
3389 {
3390 expressionS high_expr;
3391 const struct mips_opcode *mo;
3392 struct mips_cl_insn insn;
3393 bfd_reloc_code_real_type r[3]
3394 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3395 const char *name = "lui";
3396 const char *fmt = "t,u";
3397
3398 assert (! mips_opts.mips16);
3399
3400 high_expr = *ep;
3401
3402 if (high_expr.X_op == O_constant)
3403 {
3404 /* we can compute the instruction now without a relocation entry */
3405 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3406 >> 16) & 0xffff;
3407 *r = BFD_RELOC_UNUSED;
3408 }
3409 else
3410 {
3411 assert (ep->X_op == O_symbol);
3412 /* _gp_disp is a special case, used from s_cpload.
3413 __gnu_local_gp is used if mips_no_shared. */
3414 assert (mips_pic == NO_PIC
3415 || (! HAVE_NEWABI
3416 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3417 || (! mips_in_shared
3418 && strcmp (S_GET_NAME (ep->X_add_symbol),
3419 "__gnu_local_gp") == 0));
3420 *r = BFD_RELOC_HI16_S;
3421 }
3422
3423 mo = hash_find (op_hash, name);
3424 assert (strcmp (name, mo->name) == 0);
3425 assert (strcmp (fmt, mo->args) == 0);
3426 create_insn (&insn, mo);
3427
3428 insn.insn_opcode = insn.insn_mo->match;
3429 INSERT_OPERAND (RT, insn, regnum);
3430 if (*r == BFD_RELOC_UNUSED)
3431 {
3432 insn.insn_opcode |= high_expr.X_add_number;
3433 append_insn (&insn, NULL, r);
3434 }
3435 else
3436 append_insn (&insn, &high_expr, r);
3437 }
3438
3439 /* Generate a sequence of instructions to do a load or store from a constant
3440 offset off of a base register (breg) into/from a target register (treg),
3441 using AT if necessary. */
3442 static void
3443 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3444 int treg, int breg, int dbl)
3445 {
3446 assert (ep->X_op == O_constant);
3447
3448 /* Sign-extending 32-bit constants makes their handling easier. */
3449 if (!dbl)
3450 normalize_constant_expr (ep);
3451
3452 /* Right now, this routine can only handle signed 32-bit constants. */
3453 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3454 as_warn (_("operand overflow"));
3455
3456 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3457 {
3458 /* Signed 16-bit offset will fit in the op. Easy! */
3459 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3460 }
3461 else
3462 {
3463 /* 32-bit offset, need multiple instructions and AT, like:
3464 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3465 addu $tempreg,$tempreg,$breg
3466 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3467 to handle the complete offset. */
3468 macro_build_lui (ep, AT);
3469 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3470 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3471
3472 if (mips_opts.noat)
3473 as_bad (_("Macro used $at after \".set noat\""));
3474 }
3475 }
3476
3477 /* set_at()
3478 * Generates code to set the $at register to true (one)
3479 * if reg is less than the immediate expression.
3480 */
3481 static void
3482 set_at (int reg, int unsignedp)
3483 {
3484 if (imm_expr.X_op == O_constant
3485 && imm_expr.X_add_number >= -0x8000
3486 && imm_expr.X_add_number < 0x8000)
3487 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3488 AT, reg, BFD_RELOC_LO16);
3489 else
3490 {
3491 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3492 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3493 }
3494 }
3495
3496 /* Warn if an expression is not a constant. */
3497
3498 static void
3499 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3500 {
3501 if (ex->X_op == O_big)
3502 as_bad (_("unsupported large constant"));
3503 else if (ex->X_op != O_constant)
3504 as_bad (_("Instruction %s requires absolute expression"),
3505 ip->insn_mo->name);
3506
3507 if (HAVE_32BIT_GPRS)
3508 normalize_constant_expr (ex);
3509 }
3510
3511 /* Count the leading zeroes by performing a binary chop. This is a
3512 bulky bit of source, but performance is a LOT better for the
3513 majority of values than a simple loop to count the bits:
3514 for (lcnt = 0; (lcnt < 32); lcnt++)
3515 if ((v) & (1 << (31 - lcnt)))
3516 break;
3517 However it is not code size friendly, and the gain will drop a bit
3518 on certain cached systems.
3519 */
3520 #define COUNT_TOP_ZEROES(v) \
3521 (((v) & ~0xffff) == 0 \
3522 ? ((v) & ~0xff) == 0 \
3523 ? ((v) & ~0xf) == 0 \
3524 ? ((v) & ~0x3) == 0 \
3525 ? ((v) & ~0x1) == 0 \
3526 ? !(v) \
3527 ? 32 \
3528 : 31 \
3529 : 30 \
3530 : ((v) & ~0x7) == 0 \
3531 ? 29 \
3532 : 28 \
3533 : ((v) & ~0x3f) == 0 \
3534 ? ((v) & ~0x1f) == 0 \
3535 ? 27 \
3536 : 26 \
3537 : ((v) & ~0x7f) == 0 \
3538 ? 25 \
3539 : 24 \
3540 : ((v) & ~0xfff) == 0 \
3541 ? ((v) & ~0x3ff) == 0 \
3542 ? ((v) & ~0x1ff) == 0 \
3543 ? 23 \
3544 : 22 \
3545 : ((v) & ~0x7ff) == 0 \
3546 ? 21 \
3547 : 20 \
3548 : ((v) & ~0x3fff) == 0 \
3549 ? ((v) & ~0x1fff) == 0 \
3550 ? 19 \
3551 : 18 \
3552 : ((v) & ~0x7fff) == 0 \
3553 ? 17 \
3554 : 16 \
3555 : ((v) & ~0xffffff) == 0 \
3556 ? ((v) & ~0xfffff) == 0 \
3557 ? ((v) & ~0x3ffff) == 0 \
3558 ? ((v) & ~0x1ffff) == 0 \
3559 ? 15 \
3560 : 14 \
3561 : ((v) & ~0x7ffff) == 0 \
3562 ? 13 \
3563 : 12 \
3564 : ((v) & ~0x3fffff) == 0 \
3565 ? ((v) & ~0x1fffff) == 0 \
3566 ? 11 \
3567 : 10 \
3568 : ((v) & ~0x7fffff) == 0 \
3569 ? 9 \
3570 : 8 \
3571 : ((v) & ~0xfffffff) == 0 \
3572 ? ((v) & ~0x3ffffff) == 0 \
3573 ? ((v) & ~0x1ffffff) == 0 \
3574 ? 7 \
3575 : 6 \
3576 : ((v) & ~0x7ffffff) == 0 \
3577 ? 5 \
3578 : 4 \
3579 : ((v) & ~0x3fffffff) == 0 \
3580 ? ((v) & ~0x1fffffff) == 0 \
3581 ? 3 \
3582 : 2 \
3583 : ((v) & ~0x7fffffff) == 0 \
3584 ? 1 \
3585 : 0)
3586
3587 /* load_register()
3588 * This routine generates the least number of instructions necessary to load
3589 * an absolute expression value into a register.
3590 */
3591 static void
3592 load_register (int reg, expressionS *ep, int dbl)
3593 {
3594 int freg;
3595 expressionS hi32, lo32;
3596
3597 if (ep->X_op != O_big)
3598 {
3599 assert (ep->X_op == O_constant);
3600
3601 /* Sign-extending 32-bit constants makes their handling easier. */
3602 if (!dbl)
3603 normalize_constant_expr (ep);
3604
3605 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3606 {
3607 /* We can handle 16 bit signed values with an addiu to
3608 $zero. No need to ever use daddiu here, since $zero and
3609 the result are always correct in 32 bit mode. */
3610 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3611 return;
3612 }
3613 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3614 {
3615 /* We can handle 16 bit unsigned values with an ori to
3616 $zero. */
3617 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3618 return;
3619 }
3620 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3621 {
3622 /* 32 bit values require an lui. */
3623 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3624 if ((ep->X_add_number & 0xffff) != 0)
3625 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3626 return;
3627 }
3628 }
3629
3630 /* The value is larger than 32 bits. */
3631
3632 if (!dbl || HAVE_32BIT_GPRS)
3633 {
3634 char value[32];
3635
3636 sprintf_vma (value, ep->X_add_number);
3637 as_bad (_("Number (0x%s) larger than 32 bits"), value);
3638 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3639 return;
3640 }
3641
3642 if (ep->X_op != O_big)
3643 {
3644 hi32 = *ep;
3645 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3646 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3647 hi32.X_add_number &= 0xffffffff;
3648 lo32 = *ep;
3649 lo32.X_add_number &= 0xffffffff;
3650 }
3651 else
3652 {
3653 assert (ep->X_add_number > 2);
3654 if (ep->X_add_number == 3)
3655 generic_bignum[3] = 0;
3656 else if (ep->X_add_number > 4)
3657 as_bad (_("Number larger than 64 bits"));
3658 lo32.X_op = O_constant;
3659 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3660 hi32.X_op = O_constant;
3661 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3662 }
3663
3664 if (hi32.X_add_number == 0)
3665 freg = 0;
3666 else
3667 {
3668 int shift, bit;
3669 unsigned long hi, lo;
3670
3671 if (hi32.X_add_number == (offsetT) 0xffffffff)
3672 {
3673 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3674 {
3675 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3676 return;
3677 }
3678 if (lo32.X_add_number & 0x80000000)
3679 {
3680 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3681 if (lo32.X_add_number & 0xffff)
3682 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3683 return;
3684 }
3685 }
3686
3687 /* Check for 16bit shifted constant. We know that hi32 is
3688 non-zero, so start the mask on the first bit of the hi32
3689 value. */
3690 shift = 17;
3691 do
3692 {
3693 unsigned long himask, lomask;
3694
3695 if (shift < 32)
3696 {
3697 himask = 0xffff >> (32 - shift);
3698 lomask = (0xffff << shift) & 0xffffffff;
3699 }
3700 else
3701 {
3702 himask = 0xffff << (shift - 32);
3703 lomask = 0;
3704 }
3705 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3706 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3707 {
3708 expressionS tmp;
3709
3710 tmp.X_op = O_constant;
3711 if (shift < 32)
3712 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3713 | (lo32.X_add_number >> shift));
3714 else
3715 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3716 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3717 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3718 reg, reg, (shift >= 32) ? shift - 32 : shift);
3719 return;
3720 }
3721 ++shift;
3722 }
3723 while (shift <= (64 - 16));
3724
3725 /* Find the bit number of the lowest one bit, and store the
3726 shifted value in hi/lo. */
3727 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3728 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3729 if (lo != 0)
3730 {
3731 bit = 0;
3732 while ((lo & 1) == 0)
3733 {
3734 lo >>= 1;
3735 ++bit;
3736 }
3737 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3738 hi >>= bit;
3739 }
3740 else
3741 {
3742 bit = 32;
3743 while ((hi & 1) == 0)
3744 {
3745 hi >>= 1;
3746 ++bit;
3747 }
3748 lo = hi;
3749 hi = 0;
3750 }
3751
3752 /* Optimize if the shifted value is a (power of 2) - 1. */
3753 if ((hi == 0 && ((lo + 1) & lo) == 0)
3754 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3755 {
3756 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3757 if (shift != 0)
3758 {
3759 expressionS tmp;
3760
3761 /* This instruction will set the register to be all
3762 ones. */
3763 tmp.X_op = O_constant;
3764 tmp.X_add_number = (offsetT) -1;
3765 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3766 if (bit != 0)
3767 {
3768 bit += shift;
3769 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3770 reg, reg, (bit >= 32) ? bit - 32 : bit);
3771 }
3772 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3773 reg, reg, (shift >= 32) ? shift - 32 : shift);
3774 return;
3775 }
3776 }
3777
3778 /* Sign extend hi32 before calling load_register, because we can
3779 generally get better code when we load a sign extended value. */
3780 if ((hi32.X_add_number & 0x80000000) != 0)
3781 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3782 load_register (reg, &hi32, 0);
3783 freg = reg;
3784 }
3785 if ((lo32.X_add_number & 0xffff0000) == 0)
3786 {
3787 if (freg != 0)
3788 {
3789 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3790 freg = reg;
3791 }
3792 }
3793 else
3794 {
3795 expressionS mid16;
3796
3797 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3798 {
3799 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3800 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3801 return;
3802 }
3803
3804 if (freg != 0)
3805 {
3806 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3807 freg = reg;
3808 }
3809 mid16 = lo32;
3810 mid16.X_add_number >>= 16;
3811 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3812 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3813 freg = reg;
3814 }
3815 if ((lo32.X_add_number & 0xffff) != 0)
3816 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3817 }
3818
3819 static inline void
3820 load_delay_nop (void)
3821 {
3822 if (!gpr_interlocks)
3823 macro_build (NULL, "nop", "");
3824 }
3825
3826 /* Load an address into a register. */
3827
3828 static void
3829 load_address (int reg, expressionS *ep, int *used_at)
3830 {
3831 if (ep->X_op != O_constant
3832 && ep->X_op != O_symbol)
3833 {
3834 as_bad (_("expression too complex"));
3835 ep->X_op = O_constant;
3836 }
3837
3838 if (ep->X_op == O_constant)
3839 {
3840 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3841 return;
3842 }
3843
3844 if (mips_pic == NO_PIC)
3845 {
3846 /* If this is a reference to a GP relative symbol, we want
3847 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3848 Otherwise we want
3849 lui $reg,<sym> (BFD_RELOC_HI16_S)
3850 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3851 If we have an addend, we always use the latter form.
3852
3853 With 64bit address space and a usable $at we want
3854 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3855 lui $at,<sym> (BFD_RELOC_HI16_S)
3856 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3857 daddiu $at,<sym> (BFD_RELOC_LO16)
3858 dsll32 $reg,0
3859 daddu $reg,$reg,$at
3860
3861 If $at is already in use, we use a path which is suboptimal
3862 on superscalar processors.
3863 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3864 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3865 dsll $reg,16
3866 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3867 dsll $reg,16
3868 daddiu $reg,<sym> (BFD_RELOC_LO16)
3869
3870 For GP relative symbols in 64bit address space we can use
3871 the same sequence as in 32bit address space. */
3872 if (HAVE_64BIT_SYMBOLS)
3873 {
3874 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3875 && !nopic_need_relax (ep->X_add_symbol, 1))
3876 {
3877 relax_start (ep->X_add_symbol);
3878 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3879 mips_gp_register, BFD_RELOC_GPREL16);
3880 relax_switch ();
3881 }
3882
3883 if (*used_at == 0 && !mips_opts.noat)
3884 {
3885 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3886 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3887 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3888 BFD_RELOC_MIPS_HIGHER);
3889 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3890 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3891 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3892 *used_at = 1;
3893 }
3894 else
3895 {
3896 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3897 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3898 BFD_RELOC_MIPS_HIGHER);
3899 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3900 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3901 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3902 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3903 }
3904
3905 if (mips_relax.sequence)
3906 relax_end ();
3907 }
3908 else
3909 {
3910 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3911 && !nopic_need_relax (ep->X_add_symbol, 1))
3912 {
3913 relax_start (ep->X_add_symbol);
3914 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3915 mips_gp_register, BFD_RELOC_GPREL16);
3916 relax_switch ();
3917 }
3918 macro_build_lui (ep, reg);
3919 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3920 reg, reg, BFD_RELOC_LO16);
3921 if (mips_relax.sequence)
3922 relax_end ();
3923 }
3924 }
3925 else if (!mips_big_got)
3926 {
3927 expressionS ex;
3928
3929 /* If this is a reference to an external symbol, we want
3930 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3931 Otherwise we want
3932 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3933 nop
3934 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3935 If there is a constant, it must be added in after.
3936
3937 If we have NewABI, we want
3938 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3939 unless we're referencing a global symbol with a non-zero
3940 offset, in which case cst must be added separately. */
3941 if (HAVE_NEWABI)
3942 {
3943 if (ep->X_add_number)
3944 {
3945 ex.X_add_number = ep->X_add_number;
3946 ep->X_add_number = 0;
3947 relax_start (ep->X_add_symbol);
3948 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3949 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3950 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3951 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3952 ex.X_op = O_constant;
3953 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3954 reg, reg, BFD_RELOC_LO16);
3955 ep->X_add_number = ex.X_add_number;
3956 relax_switch ();
3957 }
3958 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3959 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3960 if (mips_relax.sequence)
3961 relax_end ();
3962 }
3963 else
3964 {
3965 ex.X_add_number = ep->X_add_number;
3966 ep->X_add_number = 0;
3967 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3968 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3969 load_delay_nop ();
3970 relax_start (ep->X_add_symbol);
3971 relax_switch ();
3972 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3973 BFD_RELOC_LO16);
3974 relax_end ();
3975
3976 if (ex.X_add_number != 0)
3977 {
3978 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3979 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3980 ex.X_op = O_constant;
3981 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3982 reg, reg, BFD_RELOC_LO16);
3983 }
3984 }
3985 }
3986 else if (mips_big_got)
3987 {
3988 expressionS ex;
3989
3990 /* This is the large GOT case. If this is a reference to an
3991 external symbol, we want
3992 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3993 addu $reg,$reg,$gp
3994 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3995
3996 Otherwise, for a reference to a local symbol in old ABI, we want
3997 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3998 nop
3999 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4000 If there is a constant, it must be added in after.
4001
4002 In the NewABI, for local symbols, with or without offsets, we want:
4003 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4004 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4005 */
4006 if (HAVE_NEWABI)
4007 {
4008 ex.X_add_number = ep->X_add_number;
4009 ep->X_add_number = 0;
4010 relax_start (ep->X_add_symbol);
4011 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4012 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4013 reg, reg, mips_gp_register);
4014 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4015 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4016 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4017 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4018 else if (ex.X_add_number)
4019 {
4020 ex.X_op = O_constant;
4021 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4022 BFD_RELOC_LO16);
4023 }
4024
4025 ep->X_add_number = ex.X_add_number;
4026 relax_switch ();
4027 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4028 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4029 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4030 BFD_RELOC_MIPS_GOT_OFST);
4031 relax_end ();
4032 }
4033 else
4034 {
4035 ex.X_add_number = ep->X_add_number;
4036 ep->X_add_number = 0;
4037 relax_start (ep->X_add_symbol);
4038 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4040 reg, reg, mips_gp_register);
4041 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4042 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4043 relax_switch ();
4044 if (reg_needs_delay (mips_gp_register))
4045 {
4046 /* We need a nop before loading from $gp. This special
4047 check is required because the lui which starts the main
4048 instruction stream does not refer to $gp, and so will not
4049 insert the nop which may be required. */
4050 macro_build (NULL, "nop", "");
4051 }
4052 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4053 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4054 load_delay_nop ();
4055 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4056 BFD_RELOC_LO16);
4057 relax_end ();
4058
4059 if (ex.X_add_number != 0)
4060 {
4061 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4062 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4063 ex.X_op = O_constant;
4064 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4065 BFD_RELOC_LO16);
4066 }
4067 }
4068 }
4069 else
4070 abort ();
4071
4072 if (mips_opts.noat && *used_at == 1)
4073 as_bad (_("Macro used $at after \".set noat\""));
4074 }
4075
4076 /* Move the contents of register SOURCE into register DEST. */
4077
4078 static void
4079 move_register (int dest, int source)
4080 {
4081 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4082 dest, source, 0);
4083 }
4084
4085 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4086 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4087 The two alternatives are:
4088
4089 Global symbol Local sybmol
4090 ------------- ------------
4091 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4092 ... ...
4093 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4094
4095 load_got_offset emits the first instruction and add_got_offset
4096 emits the second for a 16-bit offset or add_got_offset_hilo emits
4097 a sequence to add a 32-bit offset using a scratch register. */
4098
4099 static void
4100 load_got_offset (int dest, expressionS *local)
4101 {
4102 expressionS global;
4103
4104 global = *local;
4105 global.X_add_number = 0;
4106
4107 relax_start (local->X_add_symbol);
4108 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4109 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4110 relax_switch ();
4111 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4112 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4113 relax_end ();
4114 }
4115
4116 static void
4117 add_got_offset (int dest, expressionS *local)
4118 {
4119 expressionS global;
4120
4121 global.X_op = O_constant;
4122 global.X_op_symbol = NULL;
4123 global.X_add_symbol = NULL;
4124 global.X_add_number = local->X_add_number;
4125
4126 relax_start (local->X_add_symbol);
4127 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4128 dest, dest, BFD_RELOC_LO16);
4129 relax_switch ();
4130 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4131 relax_end ();
4132 }
4133
4134 static void
4135 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4136 {
4137 expressionS global;
4138 int hold_mips_optimize;
4139
4140 global.X_op = O_constant;
4141 global.X_op_symbol = NULL;
4142 global.X_add_symbol = NULL;
4143 global.X_add_number = local->X_add_number;
4144
4145 relax_start (local->X_add_symbol);
4146 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4147 relax_switch ();
4148 /* Set mips_optimize around the lui instruction to avoid
4149 inserting an unnecessary nop after the lw. */
4150 hold_mips_optimize = mips_optimize;
4151 mips_optimize = 2;
4152 macro_build_lui (&global, tmp);
4153 mips_optimize = hold_mips_optimize;
4154 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4155 relax_end ();
4156
4157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4158 }
4159
4160 /*
4161 * Build macros
4162 * This routine implements the seemingly endless macro or synthesized
4163 * instructions and addressing modes in the mips assembly language. Many
4164 * of these macros are simple and are similar to each other. These could
4165 * probably be handled by some kind of table or grammar approach instead of
4166 * this verbose method. Others are not simple macros but are more like
4167 * optimizing code generation.
4168 * One interesting optimization is when several store macros appear
4169 * consecutively that would load AT with the upper half of the same address.
4170 * The ensuing load upper instructions are ommited. This implies some kind
4171 * of global optimization. We currently only optimize within a single macro.
4172 * For many of the load and store macros if the address is specified as a
4173 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4174 * first load register 'at' with zero and use it as the base register. The
4175 * mips assembler simply uses register $zero. Just one tiny optimization
4176 * we're missing.
4177 */
4178 static void
4179 macro (struct mips_cl_insn *ip)
4180 {
4181 register int treg, sreg, dreg, breg;
4182 int tempreg;
4183 int mask;
4184 int used_at = 0;
4185 expressionS expr1;
4186 const char *s;
4187 const char *s2;
4188 const char *fmt;
4189 int likely = 0;
4190 int dbl = 0;
4191 int coproc = 0;
4192 int lr = 0;
4193 int imm = 0;
4194 int call = 0;
4195 int off;
4196 offsetT maxnum;
4197 bfd_reloc_code_real_type r;
4198 int hold_mips_optimize;
4199
4200 assert (! mips_opts.mips16);
4201
4202 treg = (ip->insn_opcode >> 16) & 0x1f;
4203 dreg = (ip->insn_opcode >> 11) & 0x1f;
4204 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4205 mask = ip->insn_mo->mask;
4206
4207 expr1.X_op = O_constant;
4208 expr1.X_op_symbol = NULL;
4209 expr1.X_add_symbol = NULL;
4210 expr1.X_add_number = 1;
4211
4212 switch (mask)
4213 {
4214 case M_DABS:
4215 dbl = 1;
4216 case M_ABS:
4217 /* bgez $a0,.+12
4218 move v0,$a0
4219 sub v0,$zero,$a0
4220 */
4221
4222 start_noreorder ();
4223
4224 expr1.X_add_number = 8;
4225 macro_build (&expr1, "bgez", "s,p", sreg);
4226 if (dreg == sreg)
4227 macro_build (NULL, "nop", "", 0);
4228 else
4229 move_register (dreg, sreg);
4230 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4231
4232 end_noreorder ();
4233 break;
4234
4235 case M_ADD_I:
4236 s = "addi";
4237 s2 = "add";
4238 goto do_addi;
4239 case M_ADDU_I:
4240 s = "addiu";
4241 s2 = "addu";
4242 goto do_addi;
4243 case M_DADD_I:
4244 dbl = 1;
4245 s = "daddi";
4246 s2 = "dadd";
4247 goto do_addi;
4248 case M_DADDU_I:
4249 dbl = 1;
4250 s = "daddiu";
4251 s2 = "daddu";
4252 do_addi:
4253 if (imm_expr.X_op == O_constant
4254 && imm_expr.X_add_number >= -0x8000
4255 && imm_expr.X_add_number < 0x8000)
4256 {
4257 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4258 break;
4259 }
4260 used_at = 1;
4261 load_register (AT, &imm_expr, dbl);
4262 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4263 break;
4264
4265 case M_AND_I:
4266 s = "andi";
4267 s2 = "and";
4268 goto do_bit;
4269 case M_OR_I:
4270 s = "ori";
4271 s2 = "or";
4272 goto do_bit;
4273 case M_NOR_I:
4274 s = "";
4275 s2 = "nor";
4276 goto do_bit;
4277 case M_XOR_I:
4278 s = "xori";
4279 s2 = "xor";
4280 do_bit:
4281 if (imm_expr.X_op == O_constant
4282 && imm_expr.X_add_number >= 0
4283 && imm_expr.X_add_number < 0x10000)
4284 {
4285 if (mask != M_NOR_I)
4286 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4287 else
4288 {
4289 macro_build (&imm_expr, "ori", "t,r,i",
4290 treg, sreg, BFD_RELOC_LO16);
4291 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4292 }
4293 break;
4294 }
4295
4296 used_at = 1;
4297 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4298 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4299 break;
4300
4301 case M_BEQ_I:
4302 s = "beq";
4303 goto beq_i;
4304 case M_BEQL_I:
4305 s = "beql";
4306 likely = 1;
4307 goto beq_i;
4308 case M_BNE_I:
4309 s = "bne";
4310 goto beq_i;
4311 case M_BNEL_I:
4312 s = "bnel";
4313 likely = 1;
4314 beq_i:
4315 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4316 {
4317 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4318 break;
4319 }
4320 used_at = 1;
4321 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4322 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4323 break;
4324
4325 case M_BGEL:
4326 likely = 1;
4327 case M_BGE:
4328 if (treg == 0)
4329 {
4330 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4331 break;
4332 }
4333 if (sreg == 0)
4334 {
4335 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4336 break;
4337 }
4338 used_at = 1;
4339 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4340 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4341 break;
4342
4343 case M_BGTL_I:
4344 likely = 1;
4345 case M_BGT_I:
4346 /* check for > max integer */
4347 maxnum = 0x7fffffff;
4348 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4349 {
4350 maxnum <<= 16;
4351 maxnum |= 0xffff;
4352 maxnum <<= 16;
4353 maxnum |= 0xffff;
4354 }
4355 if (imm_expr.X_op == O_constant
4356 && imm_expr.X_add_number >= maxnum
4357 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4358 {
4359 do_false:
4360 /* result is always false */
4361 if (! likely)
4362 macro_build (NULL, "nop", "", 0);
4363 else
4364 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4365 break;
4366 }
4367 if (imm_expr.X_op != O_constant)
4368 as_bad (_("Unsupported large constant"));
4369 ++imm_expr.X_add_number;
4370 /* FALLTHROUGH */
4371 case M_BGE_I:
4372 case M_BGEL_I:
4373 if (mask == M_BGEL_I)
4374 likely = 1;
4375 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4376 {
4377 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4378 break;
4379 }
4380 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4381 {
4382 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4383 break;
4384 }
4385 maxnum = 0x7fffffff;
4386 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4387 {
4388 maxnum <<= 16;
4389 maxnum |= 0xffff;
4390 maxnum <<= 16;
4391 maxnum |= 0xffff;
4392 }
4393 maxnum = - maxnum - 1;
4394 if (imm_expr.X_op == O_constant
4395 && imm_expr.X_add_number <= maxnum
4396 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4397 {
4398 do_true:
4399 /* result is always true */
4400 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4401 macro_build (&offset_expr, "b", "p");
4402 break;
4403 }
4404 used_at = 1;
4405 set_at (sreg, 0);
4406 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4407 break;
4408
4409 case M_BGEUL:
4410 likely = 1;
4411 case M_BGEU:
4412 if (treg == 0)
4413 goto do_true;
4414 if (sreg == 0)
4415 {
4416 macro_build (&offset_expr, likely ? "beql" : "beq",
4417 "s,t,p", 0, treg);
4418 break;
4419 }
4420 used_at = 1;
4421 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4422 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4423 break;
4424
4425 case M_BGTUL_I:
4426 likely = 1;
4427 case M_BGTU_I:
4428 if (sreg == 0
4429 || (HAVE_32BIT_GPRS
4430 && imm_expr.X_op == O_constant
4431 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4432 goto do_false;
4433 if (imm_expr.X_op != O_constant)
4434 as_bad (_("Unsupported large constant"));
4435 ++imm_expr.X_add_number;
4436 /* FALLTHROUGH */
4437 case M_BGEU_I:
4438 case M_BGEUL_I:
4439 if (mask == M_BGEUL_I)
4440 likely = 1;
4441 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4442 goto do_true;
4443 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4444 {
4445 macro_build (&offset_expr, likely ? "bnel" : "bne",
4446 "s,t,p", sreg, 0);
4447 break;
4448 }
4449 used_at = 1;
4450 set_at (sreg, 1);
4451 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4452 break;
4453
4454 case M_BGTL:
4455 likely = 1;
4456 case M_BGT:
4457 if (treg == 0)
4458 {
4459 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4460 break;
4461 }
4462 if (sreg == 0)
4463 {
4464 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4465 break;
4466 }
4467 used_at = 1;
4468 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4469 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4470 break;
4471
4472 case M_BGTUL:
4473 likely = 1;
4474 case M_BGTU:
4475 if (treg == 0)
4476 {
4477 macro_build (&offset_expr, likely ? "bnel" : "bne",
4478 "s,t,p", sreg, 0);
4479 break;
4480 }
4481 if (sreg == 0)
4482 goto do_false;
4483 used_at = 1;
4484 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4485 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4486 break;
4487
4488 case M_BLEL:
4489 likely = 1;
4490 case M_BLE:
4491 if (treg == 0)
4492 {
4493 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4494 break;
4495 }
4496 if (sreg == 0)
4497 {
4498 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4499 break;
4500 }
4501 used_at = 1;
4502 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4503 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4504 break;
4505
4506 case M_BLEL_I:
4507 likely = 1;
4508 case M_BLE_I:
4509 maxnum = 0x7fffffff;
4510 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4511 {
4512 maxnum <<= 16;
4513 maxnum |= 0xffff;
4514 maxnum <<= 16;
4515 maxnum |= 0xffff;
4516 }
4517 if (imm_expr.X_op == O_constant
4518 && imm_expr.X_add_number >= maxnum
4519 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4520 goto do_true;
4521 if (imm_expr.X_op != O_constant)
4522 as_bad (_("Unsupported large constant"));
4523 ++imm_expr.X_add_number;
4524 /* FALLTHROUGH */
4525 case M_BLT_I:
4526 case M_BLTL_I:
4527 if (mask == M_BLTL_I)
4528 likely = 1;
4529 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4530 {
4531 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4532 break;
4533 }
4534 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4535 {
4536 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4537 break;
4538 }
4539 used_at = 1;
4540 set_at (sreg, 0);
4541 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4542 break;
4543
4544 case M_BLEUL:
4545 likely = 1;
4546 case M_BLEU:
4547 if (treg == 0)
4548 {
4549 macro_build (&offset_expr, likely ? "beql" : "beq",
4550 "s,t,p", sreg, 0);
4551 break;
4552 }
4553 if (sreg == 0)
4554 goto do_true;
4555 used_at = 1;
4556 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4557 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4558 break;
4559
4560 case M_BLEUL_I:
4561 likely = 1;
4562 case M_BLEU_I:
4563 if (sreg == 0
4564 || (HAVE_32BIT_GPRS
4565 && imm_expr.X_op == O_constant
4566 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4567 goto do_true;
4568 if (imm_expr.X_op != O_constant)
4569 as_bad (_("Unsupported large constant"));
4570 ++imm_expr.X_add_number;
4571 /* FALLTHROUGH */
4572 case M_BLTU_I:
4573 case M_BLTUL_I:
4574 if (mask == M_BLTUL_I)
4575 likely = 1;
4576 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4577 goto do_false;
4578 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4579 {
4580 macro_build (&offset_expr, likely ? "beql" : "beq",
4581 "s,t,p", sreg, 0);
4582 break;
4583 }
4584 used_at = 1;
4585 set_at (sreg, 1);
4586 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4587 break;
4588
4589 case M_BLTL:
4590 likely = 1;
4591 case M_BLT:
4592 if (treg == 0)
4593 {
4594 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4595 break;
4596 }
4597 if (sreg == 0)
4598 {
4599 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4600 break;
4601 }
4602 used_at = 1;
4603 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4604 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4605 break;
4606
4607 case M_BLTUL:
4608 likely = 1;
4609 case M_BLTU:
4610 if (treg == 0)
4611 goto do_false;
4612 if (sreg == 0)
4613 {
4614 macro_build (&offset_expr, likely ? "bnel" : "bne",
4615 "s,t,p", 0, treg);
4616 break;
4617 }
4618 used_at = 1;
4619 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4620 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4621 break;
4622
4623 case M_DEXT:
4624 {
4625 unsigned long pos;
4626 unsigned long size;
4627
4628 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4629 {
4630 as_bad (_("Unsupported large constant"));
4631 pos = size = 1;
4632 }
4633 else
4634 {
4635 pos = (unsigned long) imm_expr.X_add_number;
4636 size = (unsigned long) imm2_expr.X_add_number;
4637 }
4638
4639 if (pos > 63)
4640 {
4641 as_bad (_("Improper position (%lu)"), pos);
4642 pos = 1;
4643 }
4644 if (size == 0 || size > 64
4645 || (pos + size - 1) > 63)
4646 {
4647 as_bad (_("Improper extract size (%lu, position %lu)"),
4648 size, pos);
4649 size = 1;
4650 }
4651
4652 if (size <= 32 && pos < 32)
4653 {
4654 s = "dext";
4655 fmt = "t,r,+A,+C";
4656 }
4657 else if (size <= 32)
4658 {
4659 s = "dextu";
4660 fmt = "t,r,+E,+H";
4661 }
4662 else
4663 {
4664 s = "dextm";
4665 fmt = "t,r,+A,+G";
4666 }
4667 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4668 }
4669 break;
4670
4671 case M_DINS:
4672 {
4673 unsigned long pos;
4674 unsigned long size;
4675
4676 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4677 {
4678 as_bad (_("Unsupported large constant"));
4679 pos = size = 1;
4680 }
4681 else
4682 {
4683 pos = (unsigned long) imm_expr.X_add_number;
4684 size = (unsigned long) imm2_expr.X_add_number;
4685 }
4686
4687 if (pos > 63)
4688 {
4689 as_bad (_("Improper position (%lu)"), pos);
4690 pos = 1;
4691 }
4692 if (size == 0 || size > 64
4693 || (pos + size - 1) > 63)
4694 {
4695 as_bad (_("Improper insert size (%lu, position %lu)"),
4696 size, pos);
4697 size = 1;
4698 }
4699
4700 if (pos < 32 && (pos + size - 1) < 32)
4701 {
4702 s = "dins";
4703 fmt = "t,r,+A,+B";
4704 }
4705 else if (pos >= 32)
4706 {
4707 s = "dinsu";
4708 fmt = "t,r,+E,+F";
4709 }
4710 else
4711 {
4712 s = "dinsm";
4713 fmt = "t,r,+A,+F";
4714 }
4715 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4716 pos + size - 1);
4717 }
4718 break;
4719
4720 case M_DDIV_3:
4721 dbl = 1;
4722 case M_DIV_3:
4723 s = "mflo";
4724 goto do_div3;
4725 case M_DREM_3:
4726 dbl = 1;
4727 case M_REM_3:
4728 s = "mfhi";
4729 do_div3:
4730 if (treg == 0)
4731 {
4732 as_warn (_("Divide by zero."));
4733 if (mips_trap)
4734 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4735 else
4736 macro_build (NULL, "break", "c", 7);
4737 break;
4738 }
4739
4740 start_noreorder ();
4741 if (mips_trap)
4742 {
4743 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4744 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4745 }
4746 else
4747 {
4748 expr1.X_add_number = 8;
4749 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4750 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4751 macro_build (NULL, "break", "c", 7);
4752 }
4753 expr1.X_add_number = -1;
4754 used_at = 1;
4755 load_register (AT, &expr1, dbl);
4756 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4757 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4758 if (dbl)
4759 {
4760 expr1.X_add_number = 1;
4761 load_register (AT, &expr1, dbl);
4762 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4763 }
4764 else
4765 {
4766 expr1.X_add_number = 0x80000000;
4767 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4768 }
4769 if (mips_trap)
4770 {
4771 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4772 /* We want to close the noreorder block as soon as possible, so
4773 that later insns are available for delay slot filling. */
4774 end_noreorder ();
4775 }
4776 else
4777 {
4778 expr1.X_add_number = 8;
4779 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4780 macro_build (NULL, "nop", "", 0);
4781
4782 /* We want to close the noreorder block as soon as possible, so
4783 that later insns are available for delay slot filling. */
4784 end_noreorder ();
4785
4786 macro_build (NULL, "break", "c", 6);
4787 }
4788 macro_build (NULL, s, "d", dreg);
4789 break;
4790
4791 case M_DIV_3I:
4792 s = "div";
4793 s2 = "mflo";
4794 goto do_divi;
4795 case M_DIVU_3I:
4796 s = "divu";
4797 s2 = "mflo";
4798 goto do_divi;
4799 case M_REM_3I:
4800 s = "div";
4801 s2 = "mfhi";
4802 goto do_divi;
4803 case M_REMU_3I:
4804 s = "divu";
4805 s2 = "mfhi";
4806 goto do_divi;
4807 case M_DDIV_3I:
4808 dbl = 1;
4809 s = "ddiv";
4810 s2 = "mflo";
4811 goto do_divi;
4812 case M_DDIVU_3I:
4813 dbl = 1;
4814 s = "ddivu";
4815 s2 = "mflo";
4816 goto do_divi;
4817 case M_DREM_3I:
4818 dbl = 1;
4819 s = "ddiv";
4820 s2 = "mfhi";
4821 goto do_divi;
4822 case M_DREMU_3I:
4823 dbl = 1;
4824 s = "ddivu";
4825 s2 = "mfhi";
4826 do_divi:
4827 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4828 {
4829 as_warn (_("Divide by zero."));
4830 if (mips_trap)
4831 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4832 else
4833 macro_build (NULL, "break", "c", 7);
4834 break;
4835 }
4836 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4837 {
4838 if (strcmp (s2, "mflo") == 0)
4839 move_register (dreg, sreg);
4840 else
4841 move_register (dreg, 0);
4842 break;
4843 }
4844 if (imm_expr.X_op == O_constant
4845 && imm_expr.X_add_number == -1
4846 && s[strlen (s) - 1] != 'u')
4847 {
4848 if (strcmp (s2, "mflo") == 0)
4849 {
4850 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4851 }
4852 else
4853 move_register (dreg, 0);
4854 break;
4855 }
4856
4857 used_at = 1;
4858 load_register (AT, &imm_expr, dbl);
4859 macro_build (NULL, s, "z,s,t", sreg, AT);
4860 macro_build (NULL, s2, "d", dreg);
4861 break;
4862
4863 case M_DIVU_3:
4864 s = "divu";
4865 s2 = "mflo";
4866 goto do_divu3;
4867 case M_REMU_3:
4868 s = "divu";
4869 s2 = "mfhi";
4870 goto do_divu3;
4871 case M_DDIVU_3:
4872 s = "ddivu";
4873 s2 = "mflo";
4874 goto do_divu3;
4875 case M_DREMU_3:
4876 s = "ddivu";
4877 s2 = "mfhi";
4878 do_divu3:
4879 start_noreorder ();
4880 if (mips_trap)
4881 {
4882 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4883 macro_build (NULL, s, "z,s,t", sreg, treg);
4884 /* We want to close the noreorder block as soon as possible, so
4885 that later insns are available for delay slot filling. */
4886 end_noreorder ();
4887 }
4888 else
4889 {
4890 expr1.X_add_number = 8;
4891 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4892 macro_build (NULL, s, "z,s,t", sreg, treg);
4893
4894 /* We want to close the noreorder block as soon as possible, so
4895 that later insns are available for delay slot filling. */
4896 end_noreorder ();
4897 macro_build (NULL, "break", "c", 7);
4898 }
4899 macro_build (NULL, s2, "d", dreg);
4900 break;
4901
4902 case M_DLCA_AB:
4903 dbl = 1;
4904 case M_LCA_AB:
4905 call = 1;
4906 goto do_la;
4907 case M_DLA_AB:
4908 dbl = 1;
4909 case M_LA_AB:
4910 do_la:
4911 /* Load the address of a symbol into a register. If breg is not
4912 zero, we then add a base register to it. */
4913
4914 if (dbl && HAVE_32BIT_GPRS)
4915 as_warn (_("dla used to load 32-bit register"));
4916
4917 if (! dbl && HAVE_64BIT_OBJECTS)
4918 as_warn (_("la used to load 64-bit address"));
4919
4920 if (offset_expr.X_op == O_constant
4921 && offset_expr.X_add_number >= -0x8000
4922 && offset_expr.X_add_number < 0x8000)
4923 {
4924 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4925 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4926 break;
4927 }
4928
4929 if (!mips_opts.noat && (treg == breg))
4930 {
4931 tempreg = AT;
4932 used_at = 1;
4933 }
4934 else
4935 {
4936 tempreg = treg;
4937 }
4938
4939 if (offset_expr.X_op != O_symbol
4940 && offset_expr.X_op != O_constant)
4941 {
4942 as_bad (_("expression too complex"));
4943 offset_expr.X_op = O_constant;
4944 }
4945
4946 if (offset_expr.X_op == O_constant)
4947 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4948 else if (mips_pic == NO_PIC)
4949 {
4950 /* If this is a reference to a GP relative symbol, we want
4951 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4952 Otherwise we want
4953 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4954 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4955 If we have a constant, we need two instructions anyhow,
4956 so we may as well always use the latter form.
4957
4958 With 64bit address space and a usable $at we want
4959 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4960 lui $at,<sym> (BFD_RELOC_HI16_S)
4961 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4962 daddiu $at,<sym> (BFD_RELOC_LO16)
4963 dsll32 $tempreg,0
4964 daddu $tempreg,$tempreg,$at
4965
4966 If $at is already in use, we use a path which is suboptimal
4967 on superscalar processors.
4968 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4969 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4970 dsll $tempreg,16
4971 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4972 dsll $tempreg,16
4973 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4974
4975 For GP relative symbols in 64bit address space we can use
4976 the same sequence as in 32bit address space. */
4977 if (HAVE_64BIT_SYMBOLS)
4978 {
4979 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4980 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4981 {
4982 relax_start (offset_expr.X_add_symbol);
4983 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4984 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4985 relax_switch ();
4986 }
4987
4988 if (used_at == 0 && !mips_opts.noat)
4989 {
4990 macro_build (&offset_expr, "lui", "t,u",
4991 tempreg, BFD_RELOC_MIPS_HIGHEST);
4992 macro_build (&offset_expr, "lui", "t,u",
4993 AT, BFD_RELOC_HI16_S);
4994 macro_build (&offset_expr, "daddiu", "t,r,j",
4995 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4996 macro_build (&offset_expr, "daddiu", "t,r,j",
4997 AT, AT, BFD_RELOC_LO16);
4998 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4999 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5000 used_at = 1;
5001 }
5002 else
5003 {
5004 macro_build (&offset_expr, "lui", "t,u",
5005 tempreg, BFD_RELOC_MIPS_HIGHEST);
5006 macro_build (&offset_expr, "daddiu", "t,r,j",
5007 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5008 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5009 macro_build (&offset_expr, "daddiu", "t,r,j",
5010 tempreg, tempreg, BFD_RELOC_HI16_S);
5011 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5012 macro_build (&offset_expr, "daddiu", "t,r,j",
5013 tempreg, tempreg, BFD_RELOC_LO16);
5014 }
5015
5016 if (mips_relax.sequence)
5017 relax_end ();
5018 }
5019 else
5020 {
5021 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5022 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5023 {
5024 relax_start (offset_expr.X_add_symbol);
5025 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5026 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5027 relax_switch ();
5028 }
5029 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5030 as_bad (_("offset too large"));
5031 macro_build_lui (&offset_expr, tempreg);
5032 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5033 tempreg, tempreg, BFD_RELOC_LO16);
5034 if (mips_relax.sequence)
5035 relax_end ();
5036 }
5037 }
5038 else if (!mips_big_got && !HAVE_NEWABI)
5039 {
5040 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5041
5042 /* If this is a reference to an external symbol, and there
5043 is no constant, we want
5044 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5045 or for lca or if tempreg is PIC_CALL_REG
5046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5047 For a local symbol, we want
5048 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5049 nop
5050 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
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_GOT16)
5055 nop
5056 addiu $tempreg,$tempreg,<constant>
5057 For a local symbol, we want the same instruction
5058 sequence, but we output a BFD_RELOC_LO16 reloc on the
5059 addiu instruction.
5060
5061 If we have a large constant, and this is a reference to
5062 an external symbol, we want
5063 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5064 lui $at,<hiconstant>
5065 addiu $at,$at,<loconstant>
5066 addu $tempreg,$tempreg,$at
5067 For a local symbol, we want the same instruction
5068 sequence, but we output a BFD_RELOC_LO16 reloc on the
5069 addiu instruction.
5070 */
5071
5072 if (offset_expr.X_add_number == 0)
5073 {
5074 if (mips_pic == SVR4_PIC
5075 && breg == 0
5076 && (call || tempreg == PIC_CALL_REG))
5077 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5078
5079 relax_start (offset_expr.X_add_symbol);
5080 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5081 lw_reloc_type, mips_gp_register);
5082 if (breg != 0)
5083 {
5084 /* We're going to put in an addu instruction using
5085 tempreg, so we may as well insert the nop right
5086 now. */
5087 load_delay_nop ();
5088 }
5089 relax_switch ();
5090 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5091 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5092 load_delay_nop ();
5093 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5094 tempreg, tempreg, BFD_RELOC_LO16);
5095 relax_end ();
5096 /* FIXME: If breg == 0, and the next instruction uses
5097 $tempreg, then if this variant case is used an extra
5098 nop will be generated. */
5099 }
5100 else if (offset_expr.X_add_number >= -0x8000
5101 && offset_expr.X_add_number < 0x8000)
5102 {
5103 load_got_offset (tempreg, &offset_expr);
5104 load_delay_nop ();
5105 add_got_offset (tempreg, &offset_expr);
5106 }
5107 else
5108 {
5109 expr1.X_add_number = offset_expr.X_add_number;
5110 offset_expr.X_add_number =
5111 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5112 load_got_offset (tempreg, &offset_expr);
5113 offset_expr.X_add_number = expr1.X_add_number;
5114 /* If we are going to add in a base register, and the
5115 target register and the base register are the same,
5116 then we are using AT as a temporary register. Since
5117 we want to load the constant into AT, we add our
5118 current AT (from the global offset table) and the
5119 register into the register now, and pretend we were
5120 not using a base register. */
5121 if (breg == treg)
5122 {
5123 load_delay_nop ();
5124 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5125 treg, AT, breg);
5126 breg = 0;
5127 tempreg = treg;
5128 }
5129 add_got_offset_hilo (tempreg, &offset_expr, AT);
5130 used_at = 1;
5131 }
5132 }
5133 else if (!mips_big_got && HAVE_NEWABI)
5134 {
5135 int add_breg_early = 0;
5136
5137 /* If this is a reference to an external, and there is no
5138 constant, or local symbol (*), with or without a
5139 constant, we want
5140 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5141 or for lca or if tempreg is PIC_CALL_REG
5142 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5143
5144 If we have a small constant, and this is a reference to
5145 an external symbol, we want
5146 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5147 addiu $tempreg,$tempreg,<constant>
5148
5149 If we have a large constant, and this is a reference to
5150 an external symbol, we want
5151 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5152 lui $at,<hiconstant>
5153 addiu $at,$at,<loconstant>
5154 addu $tempreg,$tempreg,$at
5155
5156 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5157 local symbols, even though it introduces an additional
5158 instruction. */
5159
5160 if (offset_expr.X_add_number)
5161 {
5162 expr1.X_add_number = offset_expr.X_add_number;
5163 offset_expr.X_add_number = 0;
5164
5165 relax_start (offset_expr.X_add_symbol);
5166 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5167 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5168
5169 if (expr1.X_add_number >= -0x8000
5170 && expr1.X_add_number < 0x8000)
5171 {
5172 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5173 tempreg, tempreg, BFD_RELOC_LO16);
5174 }
5175 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5176 {
5177 int dreg;
5178
5179 /* If we are going to add in a base register, and the
5180 target register and the base register are the same,
5181 then we are using AT as a temporary register. Since
5182 we want to load the constant into AT, we add our
5183 current AT (from the global offset table) and the
5184 register into the register now, and pretend we were
5185 not using a base register. */
5186 if (breg != treg)
5187 dreg = tempreg;
5188 else
5189 {
5190 assert (tempreg == AT);
5191 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5192 treg, AT, breg);
5193 dreg = treg;
5194 add_breg_early = 1;
5195 }
5196
5197 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5198 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5199 dreg, dreg, AT);
5200
5201 used_at = 1;
5202 }
5203 else
5204 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5205
5206 relax_switch ();
5207 offset_expr.X_add_number = expr1.X_add_number;
5208
5209 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5210 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5211 if (add_breg_early)
5212 {
5213 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5214 treg, tempreg, breg);
5215 breg = 0;
5216 tempreg = treg;
5217 }
5218 relax_end ();
5219 }
5220 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5221 {
5222 relax_start (offset_expr.X_add_symbol);
5223 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5224 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5225 relax_switch ();
5226 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5227 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5228 relax_end ();
5229 }
5230 else
5231 {
5232 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5233 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5234 }
5235 }
5236 else if (mips_big_got && !HAVE_NEWABI)
5237 {
5238 int gpdelay;
5239 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5240 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5241 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5242
5243 /* This is the large GOT case. If this is a reference to an
5244 external symbol, and there is no constant, we want
5245 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5246 addu $tempreg,$tempreg,$gp
5247 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5248 or for lca or if tempreg is PIC_CALL_REG
5249 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5250 addu $tempreg,$tempreg,$gp
5251 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5252 For a local symbol, we want
5253 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5254 nop
5255 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5256
5257 If we have a small constant, and this is a reference to
5258 an external symbol, we want
5259 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5260 addu $tempreg,$tempreg,$gp
5261 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5262 nop
5263 addiu $tempreg,$tempreg,<constant>
5264 For a local symbol, we want
5265 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5266 nop
5267 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5268
5269 If we have a large constant, and this is a reference to
5270 an external symbol, we want
5271 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5272 addu $tempreg,$tempreg,$gp
5273 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5274 lui $at,<hiconstant>
5275 addiu $at,$at,<loconstant>
5276 addu $tempreg,$tempreg,$at
5277 For a local symbol, we want
5278 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5279 lui $at,<hiconstant>
5280 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5281 addu $tempreg,$tempreg,$at
5282 */
5283
5284 expr1.X_add_number = offset_expr.X_add_number;
5285 offset_expr.X_add_number = 0;
5286 relax_start (offset_expr.X_add_symbol);
5287 gpdelay = reg_needs_delay (mips_gp_register);
5288 if (expr1.X_add_number == 0 && breg == 0
5289 && (call || tempreg == PIC_CALL_REG))
5290 {
5291 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5292 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5293 }
5294 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5295 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5296 tempreg, tempreg, mips_gp_register);
5297 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5298 tempreg, lw_reloc_type, tempreg);
5299 if (expr1.X_add_number == 0)
5300 {
5301 if (breg != 0)
5302 {
5303 /* We're going to put in an addu instruction using
5304 tempreg, so we may as well insert the nop right
5305 now. */
5306 load_delay_nop ();
5307 }
5308 }
5309 else if (expr1.X_add_number >= -0x8000
5310 && expr1.X_add_number < 0x8000)
5311 {
5312 load_delay_nop ();
5313 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5314 tempreg, tempreg, BFD_RELOC_LO16);
5315 }
5316 else
5317 {
5318 int dreg;
5319
5320 /* If we are going to add in a base register, and the
5321 target register and the base register are the same,
5322 then we are using AT as a temporary register. Since
5323 we want to load the constant into AT, we add our
5324 current AT (from the global offset table) and the
5325 register into the register now, and pretend we were
5326 not using a base register. */
5327 if (breg != treg)
5328 dreg = tempreg;
5329 else
5330 {
5331 assert (tempreg == AT);
5332 load_delay_nop ();
5333 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5334 treg, AT, breg);
5335 dreg = treg;
5336 }
5337
5338 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5339 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5340
5341 used_at = 1;
5342 }
5343 offset_expr.X_add_number =
5344 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5345 relax_switch ();
5346
5347 if (gpdelay)
5348 {
5349 /* This is needed because this instruction uses $gp, but
5350 the first instruction on the main stream does not. */
5351 macro_build (NULL, "nop", "");
5352 }
5353
5354 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5355 local_reloc_type, mips_gp_register);
5356 if (expr1.X_add_number >= -0x8000
5357 && expr1.X_add_number < 0x8000)
5358 {
5359 load_delay_nop ();
5360 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5361 tempreg, tempreg, BFD_RELOC_LO16);
5362 /* FIXME: If add_number is 0, and there was no base
5363 register, the external symbol case ended with a load,
5364 so if the symbol turns out to not be external, and
5365 the next instruction uses tempreg, an unnecessary nop
5366 will be inserted. */
5367 }
5368 else
5369 {
5370 if (breg == treg)
5371 {
5372 /* We must add in the base register now, as in the
5373 external symbol case. */
5374 assert (tempreg == AT);
5375 load_delay_nop ();
5376 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5377 treg, AT, breg);
5378 tempreg = treg;
5379 /* We set breg to 0 because we have arranged to add
5380 it in in both cases. */
5381 breg = 0;
5382 }
5383
5384 macro_build_lui (&expr1, AT);
5385 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5386 AT, AT, BFD_RELOC_LO16);
5387 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5388 tempreg, tempreg, AT);
5389 used_at = 1;
5390 }
5391 relax_end ();
5392 }
5393 else if (mips_big_got && HAVE_NEWABI)
5394 {
5395 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5396 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5397 int add_breg_early = 0;
5398
5399 /* This is the large GOT case. If this is a reference to an
5400 external symbol, and there is no constant, we want
5401 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5402 add $tempreg,$tempreg,$gp
5403 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5404 or for lca or if tempreg is PIC_CALL_REG
5405 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5406 add $tempreg,$tempreg,$gp
5407 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5408
5409 If we have a small constant, and this is a reference to
5410 an external symbol, we want
5411 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5412 add $tempreg,$tempreg,$gp
5413 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5414 addi $tempreg,$tempreg,<constant>
5415
5416 If we have a large constant, and this is a reference to
5417 an external symbol, we want
5418 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5419 addu $tempreg,$tempreg,$gp
5420 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5421 lui $at,<hiconstant>
5422 addi $at,$at,<loconstant>
5423 add $tempreg,$tempreg,$at
5424
5425 If we have NewABI, and we know it's a local symbol, we want
5426 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5427 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5428 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5429
5430 relax_start (offset_expr.X_add_symbol);
5431
5432 expr1.X_add_number = offset_expr.X_add_number;
5433 offset_expr.X_add_number = 0;
5434
5435 if (expr1.X_add_number == 0 && breg == 0
5436 && (call || tempreg == PIC_CALL_REG))
5437 {
5438 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5439 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5440 }
5441 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5442 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5443 tempreg, tempreg, mips_gp_register);
5444 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5445 tempreg, lw_reloc_type, tempreg);
5446
5447 if (expr1.X_add_number == 0)
5448 ;
5449 else if (expr1.X_add_number >= -0x8000
5450 && expr1.X_add_number < 0x8000)
5451 {
5452 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5453 tempreg, tempreg, BFD_RELOC_LO16);
5454 }
5455 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5456 {
5457 int dreg;
5458
5459 /* If we are going to add in a base register, and the
5460 target register and the base register are the same,
5461 then we are using AT as a temporary register. Since
5462 we want to load the constant into AT, we add our
5463 current AT (from the global offset table) and the
5464 register into the register now, and pretend we were
5465 not using a base register. */
5466 if (breg != treg)
5467 dreg = tempreg;
5468 else
5469 {
5470 assert (tempreg == AT);
5471 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5472 treg, AT, breg);
5473 dreg = treg;
5474 add_breg_early = 1;
5475 }
5476
5477 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5478 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5479
5480 used_at = 1;
5481 }
5482 else
5483 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5484
5485 relax_switch ();
5486 offset_expr.X_add_number = expr1.X_add_number;
5487 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5488 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5489 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5490 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5491 if (add_breg_early)
5492 {
5493 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5494 treg, tempreg, breg);
5495 breg = 0;
5496 tempreg = treg;
5497 }
5498 relax_end ();
5499 }
5500 else
5501 abort ();
5502
5503 if (breg != 0)
5504 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5505 break;
5506
5507 case M_J_A:
5508 /* The j instruction may not be used in PIC code, since it
5509 requires an absolute address. We convert it to a b
5510 instruction. */
5511 if (mips_pic == NO_PIC)
5512 macro_build (&offset_expr, "j", "a");
5513 else
5514 macro_build (&offset_expr, "b", "p");
5515 break;
5516
5517 /* The jal instructions must be handled as macros because when
5518 generating PIC code they expand to multi-instruction
5519 sequences. Normally they are simple instructions. */
5520 case M_JAL_1:
5521 dreg = RA;
5522 /* Fall through. */
5523 case M_JAL_2:
5524 if (mips_pic == NO_PIC)
5525 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5526 else
5527 {
5528 if (sreg != PIC_CALL_REG)
5529 as_warn (_("MIPS PIC call to register other than $25"));
5530
5531 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5532 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
5533 {
5534 if (mips_cprestore_offset < 0)
5535 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5536 else
5537 {
5538 if (! mips_frame_reg_valid)
5539 {
5540 as_warn (_("No .frame pseudo-op used in PIC code"));
5541 /* Quiet this warning. */
5542 mips_frame_reg_valid = 1;
5543 }
5544 if (! mips_cprestore_valid)
5545 {
5546 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5547 /* Quiet this warning. */
5548 mips_cprestore_valid = 1;
5549 }
5550 expr1.X_add_number = mips_cprestore_offset;
5551 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5552 mips_gp_register,
5553 mips_frame_reg,
5554 HAVE_64BIT_ADDRESSES);
5555 }
5556 }
5557 }
5558
5559 break;
5560
5561 case M_JAL_A:
5562 if (mips_pic == NO_PIC)
5563 macro_build (&offset_expr, "jal", "a");
5564 else if (mips_pic == SVR4_PIC)
5565 {
5566 /* If this is a reference to an external symbol, and we are
5567 using a small GOT, we want
5568 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5569 nop
5570 jalr $ra,$25
5571 nop
5572 lw $gp,cprestore($sp)
5573 The cprestore value is set using the .cprestore
5574 pseudo-op. If we are using a big GOT, we want
5575 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5576 addu $25,$25,$gp
5577 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5578 nop
5579 jalr $ra,$25
5580 nop
5581 lw $gp,cprestore($sp)
5582 If the symbol is not external, we want
5583 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5584 nop
5585 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5586 jalr $ra,$25
5587 nop
5588 lw $gp,cprestore($sp)
5589
5590 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5591 sequences above, minus nops, unless the symbol is local,
5592 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5593 GOT_DISP. */
5594 if (HAVE_NEWABI)
5595 {
5596 if (! mips_big_got)
5597 {
5598 relax_start (offset_expr.X_add_symbol);
5599 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5600 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5601 mips_gp_register);
5602 relax_switch ();
5603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5604 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5605 mips_gp_register);
5606 relax_end ();
5607 }
5608 else
5609 {
5610 relax_start (offset_expr.X_add_symbol);
5611 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5612 BFD_RELOC_MIPS_CALL_HI16);
5613 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5614 PIC_CALL_REG, mips_gp_register);
5615 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5616 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5617 PIC_CALL_REG);
5618 relax_switch ();
5619 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5620 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5621 mips_gp_register);
5622 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5623 PIC_CALL_REG, PIC_CALL_REG,
5624 BFD_RELOC_MIPS_GOT_OFST);
5625 relax_end ();
5626 }
5627
5628 macro_build_jalr (&offset_expr);
5629 }
5630 else
5631 {
5632 relax_start (offset_expr.X_add_symbol);
5633 if (! mips_big_got)
5634 {
5635 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5636 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5637 mips_gp_register);
5638 load_delay_nop ();
5639 relax_switch ();
5640 }
5641 else
5642 {
5643 int gpdelay;
5644
5645 gpdelay = reg_needs_delay (mips_gp_register);
5646 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5647 BFD_RELOC_MIPS_CALL_HI16);
5648 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5649 PIC_CALL_REG, mips_gp_register);
5650 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5651 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5652 PIC_CALL_REG);
5653 load_delay_nop ();
5654 relax_switch ();
5655 if (gpdelay)
5656 macro_build (NULL, "nop", "");
5657 }
5658 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5659 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5660 mips_gp_register);
5661 load_delay_nop ();
5662 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5663 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5664 relax_end ();
5665 macro_build_jalr (&offset_expr);
5666
5667 if (mips_cprestore_offset < 0)
5668 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5669 else
5670 {
5671 if (! mips_frame_reg_valid)
5672 {
5673 as_warn (_("No .frame pseudo-op used in PIC code"));
5674 /* Quiet this warning. */
5675 mips_frame_reg_valid = 1;
5676 }
5677 if (! mips_cprestore_valid)
5678 {
5679 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5680 /* Quiet this warning. */
5681 mips_cprestore_valid = 1;
5682 }
5683 if (mips_opts.noreorder)
5684 macro_build (NULL, "nop", "");
5685 expr1.X_add_number = mips_cprestore_offset;
5686 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5687 mips_gp_register,
5688 mips_frame_reg,
5689 HAVE_64BIT_ADDRESSES);
5690 }
5691 }
5692 }
5693 else if (mips_pic == VXWORKS_PIC)
5694 as_bad (_("Non-PIC jump used in PIC library"));
5695 else
5696 abort ();
5697
5698 break;
5699
5700 case M_LB_AB:
5701 s = "lb";
5702 goto ld;
5703 case M_LBU_AB:
5704 s = "lbu";
5705 goto ld;
5706 case M_LH_AB:
5707 s = "lh";
5708 goto ld;
5709 case M_LHU_AB:
5710 s = "lhu";
5711 goto ld;
5712 case M_LW_AB:
5713 s = "lw";
5714 goto ld;
5715 case M_LWC0_AB:
5716 s = "lwc0";
5717 /* Itbl support may require additional care here. */
5718 coproc = 1;
5719 goto ld;
5720 case M_LWC1_AB:
5721 s = "lwc1";
5722 /* Itbl support may require additional care here. */
5723 coproc = 1;
5724 goto ld;
5725 case M_LWC2_AB:
5726 s = "lwc2";
5727 /* Itbl support may require additional care here. */
5728 coproc = 1;
5729 goto ld;
5730 case M_LWC3_AB:
5731 s = "lwc3";
5732 /* Itbl support may require additional care here. */
5733 coproc = 1;
5734 goto ld;
5735 case M_LWL_AB:
5736 s = "lwl";
5737 lr = 1;
5738 goto ld;
5739 case M_LWR_AB:
5740 s = "lwr";
5741 lr = 1;
5742 goto ld;
5743 case M_LDC1_AB:
5744 if (mips_opts.arch == CPU_R4650)
5745 {
5746 as_bad (_("opcode not supported on this processor"));
5747 break;
5748 }
5749 s = "ldc1";
5750 /* Itbl support may require additional care here. */
5751 coproc = 1;
5752 goto ld;
5753 case M_LDC2_AB:
5754 s = "ldc2";
5755 /* Itbl support may require additional care here. */
5756 coproc = 1;
5757 goto ld;
5758 case M_LDC3_AB:
5759 s = "ldc3";
5760 /* Itbl support may require additional care here. */
5761 coproc = 1;
5762 goto ld;
5763 case M_LDL_AB:
5764 s = "ldl";
5765 lr = 1;
5766 goto ld;
5767 case M_LDR_AB:
5768 s = "ldr";
5769 lr = 1;
5770 goto ld;
5771 case M_LL_AB:
5772 s = "ll";
5773 goto ld;
5774 case M_LLD_AB:
5775 s = "lld";
5776 goto ld;
5777 case M_LWU_AB:
5778 s = "lwu";
5779 ld:
5780 if (breg == treg || coproc || lr)
5781 {
5782 tempreg = AT;
5783 used_at = 1;
5784 }
5785 else
5786 {
5787 tempreg = treg;
5788 }
5789 goto ld_st;
5790 case M_SB_AB:
5791 s = "sb";
5792 goto st;
5793 case M_SH_AB:
5794 s = "sh";
5795 goto st;
5796 case M_SW_AB:
5797 s = "sw";
5798 goto st;
5799 case M_SWC0_AB:
5800 s = "swc0";
5801 /* Itbl support may require additional care here. */
5802 coproc = 1;
5803 goto st;
5804 case M_SWC1_AB:
5805 s = "swc1";
5806 /* Itbl support may require additional care here. */
5807 coproc = 1;
5808 goto st;
5809 case M_SWC2_AB:
5810 s = "swc2";
5811 /* Itbl support may require additional care here. */
5812 coproc = 1;
5813 goto st;
5814 case M_SWC3_AB:
5815 s = "swc3";
5816 /* Itbl support may require additional care here. */
5817 coproc = 1;
5818 goto st;
5819 case M_SWL_AB:
5820 s = "swl";
5821 goto st;
5822 case M_SWR_AB:
5823 s = "swr";
5824 goto st;
5825 case M_SC_AB:
5826 s = "sc";
5827 goto st;
5828 case M_SCD_AB:
5829 s = "scd";
5830 goto st;
5831 case M_CACHE_AB:
5832 s = "cache";
5833 goto st;
5834 case M_SDC1_AB:
5835 if (mips_opts.arch == CPU_R4650)
5836 {
5837 as_bad (_("opcode not supported on this processor"));
5838 break;
5839 }
5840 s = "sdc1";
5841 coproc = 1;
5842 /* Itbl support may require additional care here. */
5843 goto st;
5844 case M_SDC2_AB:
5845 s = "sdc2";
5846 /* Itbl support may require additional care here. */
5847 coproc = 1;
5848 goto st;
5849 case M_SDC3_AB:
5850 s = "sdc3";
5851 /* Itbl support may require additional care here. */
5852 coproc = 1;
5853 goto st;
5854 case M_SDL_AB:
5855 s = "sdl";
5856 goto st;
5857 case M_SDR_AB:
5858 s = "sdr";
5859 st:
5860 tempreg = AT;
5861 used_at = 1;
5862 ld_st:
5863 /* Itbl support may require additional care here. */
5864 if (mask == M_LWC1_AB
5865 || mask == M_SWC1_AB
5866 || mask == M_LDC1_AB
5867 || mask == M_SDC1_AB
5868 || mask == M_L_DAB
5869 || mask == M_S_DAB)
5870 fmt = "T,o(b)";
5871 else if (mask == M_CACHE_AB)
5872 fmt = "k,o(b)";
5873 else if (coproc)
5874 fmt = "E,o(b)";
5875 else
5876 fmt = "t,o(b)";
5877
5878 if (offset_expr.X_op != O_constant
5879 && offset_expr.X_op != O_symbol)
5880 {
5881 as_bad (_("expression too complex"));
5882 offset_expr.X_op = O_constant;
5883 }
5884
5885 if (HAVE_32BIT_ADDRESSES
5886 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5887 {
5888 char value [32];
5889
5890 sprintf_vma (value, offset_expr.X_add_number);
5891 as_bad (_("Number (0x%s) larger than 32 bits"), value);
5892 }
5893
5894 /* A constant expression in PIC code can be handled just as it
5895 is in non PIC code. */
5896 if (offset_expr.X_op == O_constant)
5897 {
5898 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5899 & ~(bfd_vma) 0xffff);
5900 normalize_address_expr (&expr1);
5901 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5902 if (breg != 0)
5903 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5904 tempreg, tempreg, breg);
5905 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5906 }
5907 else if (mips_pic == NO_PIC)
5908 {
5909 /* If this is a reference to a GP relative symbol, and there
5910 is no base register, we want
5911 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5912 Otherwise, if there is no base register, we want
5913 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5914 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5915 If we have a constant, we need two instructions anyhow,
5916 so we always use the latter form.
5917
5918 If we have a base register, and this is a reference to a
5919 GP relative symbol, we want
5920 addu $tempreg,$breg,$gp
5921 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5922 Otherwise we want
5923 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5924 addu $tempreg,$tempreg,$breg
5925 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5926 With a constant we always use the latter case.
5927
5928 With 64bit address space and no base register and $at usable,
5929 we want
5930 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5931 lui $at,<sym> (BFD_RELOC_HI16_S)
5932 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5933 dsll32 $tempreg,0
5934 daddu $tempreg,$at
5935 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5936 If we have a base register, we want
5937 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5938 lui $at,<sym> (BFD_RELOC_HI16_S)
5939 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5940 daddu $at,$breg
5941 dsll32 $tempreg,0
5942 daddu $tempreg,$at
5943 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5944
5945 Without $at we can't generate the optimal path for superscalar
5946 processors here since this would require two temporary registers.
5947 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5948 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5949 dsll $tempreg,16
5950 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5951 dsll $tempreg,16
5952 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5953 If we have a base register, we want
5954 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5955 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5956 dsll $tempreg,16
5957 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5958 dsll $tempreg,16
5959 daddu $tempreg,$tempreg,$breg
5960 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5961
5962 For GP relative symbols in 64bit address space we can use
5963 the same sequence as in 32bit address space. */
5964 if (HAVE_64BIT_SYMBOLS)
5965 {
5966 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5967 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5968 {
5969 relax_start (offset_expr.X_add_symbol);
5970 if (breg == 0)
5971 {
5972 macro_build (&offset_expr, s, fmt, treg,
5973 BFD_RELOC_GPREL16, mips_gp_register);
5974 }
5975 else
5976 {
5977 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5978 tempreg, breg, mips_gp_register);
5979 macro_build (&offset_expr, s, fmt, treg,
5980 BFD_RELOC_GPREL16, tempreg);
5981 }
5982 relax_switch ();
5983 }
5984
5985 if (used_at == 0 && !mips_opts.noat)
5986 {
5987 macro_build (&offset_expr, "lui", "t,u", tempreg,
5988 BFD_RELOC_MIPS_HIGHEST);
5989 macro_build (&offset_expr, "lui", "t,u", AT,
5990 BFD_RELOC_HI16_S);
5991 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5992 tempreg, BFD_RELOC_MIPS_HIGHER);
5993 if (breg != 0)
5994 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5995 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5996 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5997 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5998 tempreg);
5999 used_at = 1;
6000 }
6001 else
6002 {
6003 macro_build (&offset_expr, "lui", "t,u", tempreg,
6004 BFD_RELOC_MIPS_HIGHEST);
6005 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6006 tempreg, BFD_RELOC_MIPS_HIGHER);
6007 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6008 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6009 tempreg, BFD_RELOC_HI16_S);
6010 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6011 if (breg != 0)
6012 macro_build (NULL, "daddu", "d,v,t",
6013 tempreg, tempreg, breg);
6014 macro_build (&offset_expr, s, fmt, treg,
6015 BFD_RELOC_LO16, tempreg);
6016 }
6017
6018 if (mips_relax.sequence)
6019 relax_end ();
6020 break;
6021 }
6022
6023 if (breg == 0)
6024 {
6025 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6026 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6027 {
6028 relax_start (offset_expr.X_add_symbol);
6029 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6030 mips_gp_register);
6031 relax_switch ();
6032 }
6033 macro_build_lui (&offset_expr, tempreg);
6034 macro_build (&offset_expr, s, fmt, treg,
6035 BFD_RELOC_LO16, tempreg);
6036 if (mips_relax.sequence)
6037 relax_end ();
6038 }
6039 else
6040 {
6041 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6042 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6043 {
6044 relax_start (offset_expr.X_add_symbol);
6045 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6046 tempreg, breg, mips_gp_register);
6047 macro_build (&offset_expr, s, fmt, treg,
6048 BFD_RELOC_GPREL16, tempreg);
6049 relax_switch ();
6050 }
6051 macro_build_lui (&offset_expr, tempreg);
6052 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6053 tempreg, tempreg, breg);
6054 macro_build (&offset_expr, s, fmt, treg,
6055 BFD_RELOC_LO16, tempreg);
6056 if (mips_relax.sequence)
6057 relax_end ();
6058 }
6059 }
6060 else if (!mips_big_got)
6061 {
6062 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6063
6064 /* If this is a reference to an external symbol, we want
6065 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6066 nop
6067 <op> $treg,0($tempreg)
6068 Otherwise we want
6069 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6070 nop
6071 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6072 <op> $treg,0($tempreg)
6073
6074 For NewABI, we want
6075 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6076 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6077
6078 If there is a base register, we add it to $tempreg before
6079 the <op>. If there is a constant, we stick it in the
6080 <op> instruction. We don't handle constants larger than
6081 16 bits, because we have no way to load the upper 16 bits
6082 (actually, we could handle them for the subset of cases
6083 in which we are not using $at). */
6084 assert (offset_expr.X_op == O_symbol);
6085 if (HAVE_NEWABI)
6086 {
6087 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6088 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6089 if (breg != 0)
6090 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6091 tempreg, tempreg, breg);
6092 macro_build (&offset_expr, s, fmt, treg,
6093 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6094 break;
6095 }
6096 expr1.X_add_number = offset_expr.X_add_number;
6097 offset_expr.X_add_number = 0;
6098 if (expr1.X_add_number < -0x8000
6099 || expr1.X_add_number >= 0x8000)
6100 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6101 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6102 lw_reloc_type, mips_gp_register);
6103 load_delay_nop ();
6104 relax_start (offset_expr.X_add_symbol);
6105 relax_switch ();
6106 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6107 tempreg, BFD_RELOC_LO16);
6108 relax_end ();
6109 if (breg != 0)
6110 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6111 tempreg, tempreg, breg);
6112 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6113 }
6114 else if (mips_big_got && !HAVE_NEWABI)
6115 {
6116 int gpdelay;
6117
6118 /* If this is a reference to an external symbol, we want
6119 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6120 addu $tempreg,$tempreg,$gp
6121 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6122 <op> $treg,0($tempreg)
6123 Otherwise we want
6124 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6125 nop
6126 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6127 <op> $treg,0($tempreg)
6128 If there is a base register, we add it to $tempreg before
6129 the <op>. If there is a constant, we stick it in the
6130 <op> instruction. We don't handle constants larger than
6131 16 bits, because we have no way to load the upper 16 bits
6132 (actually, we could handle them for the subset of cases
6133 in which we are not using $at). */
6134 assert (offset_expr.X_op == O_symbol);
6135 expr1.X_add_number = offset_expr.X_add_number;
6136 offset_expr.X_add_number = 0;
6137 if (expr1.X_add_number < -0x8000
6138 || expr1.X_add_number >= 0x8000)
6139 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6140 gpdelay = reg_needs_delay (mips_gp_register);
6141 relax_start (offset_expr.X_add_symbol);
6142 macro_build (&offset_expr, "lui", "t,u", tempreg,
6143 BFD_RELOC_MIPS_GOT_HI16);
6144 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6145 mips_gp_register);
6146 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6147 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6148 relax_switch ();
6149 if (gpdelay)
6150 macro_build (NULL, "nop", "");
6151 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6152 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6153 load_delay_nop ();
6154 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6155 tempreg, BFD_RELOC_LO16);
6156 relax_end ();
6157
6158 if (breg != 0)
6159 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6160 tempreg, tempreg, breg);
6161 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6162 }
6163 else if (mips_big_got && HAVE_NEWABI)
6164 {
6165 /* If this is a reference to an external symbol, we want
6166 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6167 add $tempreg,$tempreg,$gp
6168 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6169 <op> $treg,<ofst>($tempreg)
6170 Otherwise, for local symbols, we want:
6171 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6172 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6173 assert (offset_expr.X_op == O_symbol);
6174 expr1.X_add_number = offset_expr.X_add_number;
6175 offset_expr.X_add_number = 0;
6176 if (expr1.X_add_number < -0x8000
6177 || expr1.X_add_number >= 0x8000)
6178 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6179 relax_start (offset_expr.X_add_symbol);
6180 macro_build (&offset_expr, "lui", "t,u", tempreg,
6181 BFD_RELOC_MIPS_GOT_HI16);
6182 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6183 mips_gp_register);
6184 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6185 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6186 if (breg != 0)
6187 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6188 tempreg, tempreg, breg);
6189 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6190
6191 relax_switch ();
6192 offset_expr.X_add_number = expr1.X_add_number;
6193 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6194 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6195 if (breg != 0)
6196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6197 tempreg, tempreg, breg);
6198 macro_build (&offset_expr, s, fmt, treg,
6199 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6200 relax_end ();
6201 }
6202 else
6203 abort ();
6204
6205 break;
6206
6207 case M_LI:
6208 case M_LI_S:
6209 load_register (treg, &imm_expr, 0);
6210 break;
6211
6212 case M_DLI:
6213 load_register (treg, &imm_expr, 1);
6214 break;
6215
6216 case M_LI_SS:
6217 if (imm_expr.X_op == O_constant)
6218 {
6219 used_at = 1;
6220 load_register (AT, &imm_expr, 0);
6221 macro_build (NULL, "mtc1", "t,G", AT, treg);
6222 break;
6223 }
6224 else
6225 {
6226 assert (offset_expr.X_op == O_symbol
6227 && strcmp (segment_name (S_GET_SEGMENT
6228 (offset_expr.X_add_symbol)),
6229 ".lit4") == 0
6230 && offset_expr.X_add_number == 0);
6231 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6232 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6233 break;
6234 }
6235
6236 case M_LI_D:
6237 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6238 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6239 order 32 bits of the value and the low order 32 bits are either
6240 zero or in OFFSET_EXPR. */
6241 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6242 {
6243 if (HAVE_64BIT_GPRS)
6244 load_register (treg, &imm_expr, 1);
6245 else
6246 {
6247 int hreg, lreg;
6248
6249 if (target_big_endian)
6250 {
6251 hreg = treg;
6252 lreg = treg + 1;
6253 }
6254 else
6255 {
6256 hreg = treg + 1;
6257 lreg = treg;
6258 }
6259
6260 if (hreg <= 31)
6261 load_register (hreg, &imm_expr, 0);
6262 if (lreg <= 31)
6263 {
6264 if (offset_expr.X_op == O_absent)
6265 move_register (lreg, 0);
6266 else
6267 {
6268 assert (offset_expr.X_op == O_constant);
6269 load_register (lreg, &offset_expr, 0);
6270 }
6271 }
6272 }
6273 break;
6274 }
6275
6276 /* We know that sym is in the .rdata section. First we get the
6277 upper 16 bits of the address. */
6278 if (mips_pic == NO_PIC)
6279 {
6280 macro_build_lui (&offset_expr, AT);
6281 used_at = 1;
6282 }
6283 else
6284 {
6285 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6286 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6287 used_at = 1;
6288 }
6289
6290 /* Now we load the register(s). */
6291 if (HAVE_64BIT_GPRS)
6292 {
6293 used_at = 1;
6294 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6295 }
6296 else
6297 {
6298 used_at = 1;
6299 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6300 if (treg != RA)
6301 {
6302 /* FIXME: How in the world do we deal with the possible
6303 overflow here? */
6304 offset_expr.X_add_number += 4;
6305 macro_build (&offset_expr, "lw", "t,o(b)",
6306 treg + 1, BFD_RELOC_LO16, AT);
6307 }
6308 }
6309 break;
6310
6311 case M_LI_DD:
6312 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6313 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6314 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6315 the value and the low order 32 bits are either zero or in
6316 OFFSET_EXPR. */
6317 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6318 {
6319 used_at = 1;
6320 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6321 if (HAVE_64BIT_FPRS)
6322 {
6323 assert (HAVE_64BIT_GPRS);
6324 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6325 }
6326 else
6327 {
6328 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6329 if (offset_expr.X_op == O_absent)
6330 macro_build (NULL, "mtc1", "t,G", 0, treg);
6331 else
6332 {
6333 assert (offset_expr.X_op == O_constant);
6334 load_register (AT, &offset_expr, 0);
6335 macro_build (NULL, "mtc1", "t,G", AT, treg);
6336 }
6337 }
6338 break;
6339 }
6340
6341 assert (offset_expr.X_op == O_symbol
6342 && offset_expr.X_add_number == 0);
6343 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6344 if (strcmp (s, ".lit8") == 0)
6345 {
6346 if (mips_opts.isa != ISA_MIPS1)
6347 {
6348 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6349 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6350 break;
6351 }
6352 breg = mips_gp_register;
6353 r = BFD_RELOC_MIPS_LITERAL;
6354 goto dob;
6355 }
6356 else
6357 {
6358 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6359 used_at = 1;
6360 if (mips_pic != NO_PIC)
6361 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6362 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6363 else
6364 {
6365 /* FIXME: This won't work for a 64 bit address. */
6366 macro_build_lui (&offset_expr, AT);
6367 }
6368
6369 if (mips_opts.isa != ISA_MIPS1)
6370 {
6371 macro_build (&offset_expr, "ldc1", "T,o(b)",
6372 treg, BFD_RELOC_LO16, AT);
6373 break;
6374 }
6375 breg = AT;
6376 r = BFD_RELOC_LO16;
6377 goto dob;
6378 }
6379
6380 case M_L_DOB:
6381 if (mips_opts.arch == CPU_R4650)
6382 {
6383 as_bad (_("opcode not supported on this processor"));
6384 break;
6385 }
6386 /* Even on a big endian machine $fn comes before $fn+1. We have
6387 to adjust when loading from memory. */
6388 r = BFD_RELOC_LO16;
6389 dob:
6390 assert (mips_opts.isa == ISA_MIPS1);
6391 macro_build (&offset_expr, "lwc1", "T,o(b)",
6392 target_big_endian ? treg + 1 : treg, r, breg);
6393 /* FIXME: A possible overflow which I don't know how to deal
6394 with. */
6395 offset_expr.X_add_number += 4;
6396 macro_build (&offset_expr, "lwc1", "T,o(b)",
6397 target_big_endian ? treg : treg + 1, r, breg);
6398 break;
6399
6400 case M_L_DAB:
6401 /*
6402 * The MIPS assembler seems to check for X_add_number not
6403 * being double aligned and generating:
6404 * lui at,%hi(foo+1)
6405 * addu at,at,v1
6406 * addiu at,at,%lo(foo+1)
6407 * lwc1 f2,0(at)
6408 * lwc1 f3,4(at)
6409 * But, the resulting address is the same after relocation so why
6410 * generate the extra instruction?
6411 */
6412 if (mips_opts.arch == CPU_R4650)
6413 {
6414 as_bad (_("opcode not supported on this processor"));
6415 break;
6416 }
6417 /* Itbl support may require additional care here. */
6418 coproc = 1;
6419 if (mips_opts.isa != ISA_MIPS1)
6420 {
6421 s = "ldc1";
6422 goto ld;
6423 }
6424
6425 s = "lwc1";
6426 fmt = "T,o(b)";
6427 goto ldd_std;
6428
6429 case M_S_DAB:
6430 if (mips_opts.arch == CPU_R4650)
6431 {
6432 as_bad (_("opcode not supported on this processor"));
6433 break;
6434 }
6435
6436 if (mips_opts.isa != ISA_MIPS1)
6437 {
6438 s = "sdc1";
6439 goto st;
6440 }
6441
6442 s = "swc1";
6443 fmt = "T,o(b)";
6444 /* Itbl support may require additional care here. */
6445 coproc = 1;
6446 goto ldd_std;
6447
6448 case M_LD_AB:
6449 if (HAVE_64BIT_GPRS)
6450 {
6451 s = "ld";
6452 goto ld;
6453 }
6454
6455 s = "lw";
6456 fmt = "t,o(b)";
6457 goto ldd_std;
6458
6459 case M_SD_AB:
6460 if (HAVE_64BIT_GPRS)
6461 {
6462 s = "sd";
6463 goto st;
6464 }
6465
6466 s = "sw";
6467 fmt = "t,o(b)";
6468
6469 ldd_std:
6470 if (offset_expr.X_op != O_symbol
6471 && offset_expr.X_op != O_constant)
6472 {
6473 as_bad (_("expression too complex"));
6474 offset_expr.X_op = O_constant;
6475 }
6476
6477 if (HAVE_32BIT_ADDRESSES
6478 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6479 {
6480 char value [32];
6481
6482 sprintf_vma (value, offset_expr.X_add_number);
6483 as_bad (_("Number (0x%s) larger than 32 bits"), value);
6484 }
6485
6486 /* Even on a big endian machine $fn comes before $fn+1. We have
6487 to adjust when loading from memory. We set coproc if we must
6488 load $fn+1 first. */
6489 /* Itbl support may require additional care here. */
6490 if (! target_big_endian)
6491 coproc = 0;
6492
6493 if (mips_pic == NO_PIC
6494 || offset_expr.X_op == O_constant)
6495 {
6496 /* If this is a reference to a GP relative symbol, we want
6497 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6498 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6499 If we have a base register, we use this
6500 addu $at,$breg,$gp
6501 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6502 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6503 If this is not a GP relative symbol, we want
6504 lui $at,<sym> (BFD_RELOC_HI16_S)
6505 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6506 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6507 If there is a base register, we add it to $at after the
6508 lui instruction. If there is a constant, we always use
6509 the last case. */
6510 if (offset_expr.X_op == O_symbol
6511 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6512 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6513 {
6514 relax_start (offset_expr.X_add_symbol);
6515 if (breg == 0)
6516 {
6517 tempreg = mips_gp_register;
6518 }
6519 else
6520 {
6521 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6522 AT, breg, mips_gp_register);
6523 tempreg = AT;
6524 used_at = 1;
6525 }
6526
6527 /* Itbl support may require additional care here. */
6528 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6529 BFD_RELOC_GPREL16, tempreg);
6530 offset_expr.X_add_number += 4;
6531
6532 /* Set mips_optimize to 2 to avoid inserting an
6533 undesired nop. */
6534 hold_mips_optimize = mips_optimize;
6535 mips_optimize = 2;
6536 /* Itbl support may require additional care here. */
6537 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6538 BFD_RELOC_GPREL16, tempreg);
6539 mips_optimize = hold_mips_optimize;
6540
6541 relax_switch ();
6542
6543 /* We just generated two relocs. When tc_gen_reloc
6544 handles this case, it will skip the first reloc and
6545 handle the second. The second reloc already has an
6546 extra addend of 4, which we added above. We must
6547 subtract it out, and then subtract another 4 to make
6548 the first reloc come out right. The second reloc
6549 will come out right because we are going to add 4 to
6550 offset_expr when we build its instruction below.
6551
6552 If we have a symbol, then we don't want to include
6553 the offset, because it will wind up being included
6554 when we generate the reloc. */
6555
6556 if (offset_expr.X_op == O_constant)
6557 offset_expr.X_add_number -= 8;
6558 else
6559 {
6560 offset_expr.X_add_number = -4;
6561 offset_expr.X_op = O_constant;
6562 }
6563 }
6564 used_at = 1;
6565 macro_build_lui (&offset_expr, AT);
6566 if (breg != 0)
6567 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6568 /* Itbl support may require additional care here. */
6569 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6570 BFD_RELOC_LO16, AT);
6571 /* FIXME: How do we handle overflow here? */
6572 offset_expr.X_add_number += 4;
6573 /* Itbl support may require additional care here. */
6574 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6575 BFD_RELOC_LO16, AT);
6576 if (mips_relax.sequence)
6577 relax_end ();
6578 }
6579 else if (!mips_big_got)
6580 {
6581 /* If this is a reference to an external symbol, we want
6582 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6583 nop
6584 <op> $treg,0($at)
6585 <op> $treg+1,4($at)
6586 Otherwise we want
6587 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6588 nop
6589 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6590 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6591 If there is a base register we add it to $at before the
6592 lwc1 instructions. If there is a constant we include it
6593 in the lwc1 instructions. */
6594 used_at = 1;
6595 expr1.X_add_number = offset_expr.X_add_number;
6596 if (expr1.X_add_number < -0x8000
6597 || expr1.X_add_number >= 0x8000 - 4)
6598 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6599 load_got_offset (AT, &offset_expr);
6600 load_delay_nop ();
6601 if (breg != 0)
6602 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6603
6604 /* Set mips_optimize to 2 to avoid inserting an undesired
6605 nop. */
6606 hold_mips_optimize = mips_optimize;
6607 mips_optimize = 2;
6608
6609 /* Itbl support may require additional care here. */
6610 relax_start (offset_expr.X_add_symbol);
6611 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6612 BFD_RELOC_LO16, AT);
6613 expr1.X_add_number += 4;
6614 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6615 BFD_RELOC_LO16, AT);
6616 relax_switch ();
6617 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6618 BFD_RELOC_LO16, AT);
6619 offset_expr.X_add_number += 4;
6620 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6621 BFD_RELOC_LO16, AT);
6622 relax_end ();
6623
6624 mips_optimize = hold_mips_optimize;
6625 }
6626 else if (mips_big_got)
6627 {
6628 int gpdelay;
6629
6630 /* If this is a reference to an external symbol, we want
6631 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6632 addu $at,$at,$gp
6633 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6634 nop
6635 <op> $treg,0($at)
6636 <op> $treg+1,4($at)
6637 Otherwise we want
6638 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6639 nop
6640 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6641 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6642 If there is a base register we add it to $at before the
6643 lwc1 instructions. If there is a constant we include it
6644 in the lwc1 instructions. */
6645 used_at = 1;
6646 expr1.X_add_number = offset_expr.X_add_number;
6647 offset_expr.X_add_number = 0;
6648 if (expr1.X_add_number < -0x8000
6649 || expr1.X_add_number >= 0x8000 - 4)
6650 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6651 gpdelay = reg_needs_delay (mips_gp_register);
6652 relax_start (offset_expr.X_add_symbol);
6653 macro_build (&offset_expr, "lui", "t,u",
6654 AT, BFD_RELOC_MIPS_GOT_HI16);
6655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6656 AT, AT, mips_gp_register);
6657 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6658 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6659 load_delay_nop ();
6660 if (breg != 0)
6661 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6662 /* Itbl support may require additional care here. */
6663 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6664 BFD_RELOC_LO16, AT);
6665 expr1.X_add_number += 4;
6666
6667 /* Set mips_optimize to 2 to avoid inserting an undesired
6668 nop. */
6669 hold_mips_optimize = mips_optimize;
6670 mips_optimize = 2;
6671 /* Itbl support may require additional care here. */
6672 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6673 BFD_RELOC_LO16, AT);
6674 mips_optimize = hold_mips_optimize;
6675 expr1.X_add_number -= 4;
6676
6677 relax_switch ();
6678 offset_expr.X_add_number = expr1.X_add_number;
6679 if (gpdelay)
6680 macro_build (NULL, "nop", "");
6681 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6682 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6683 load_delay_nop ();
6684 if (breg != 0)
6685 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6686 /* Itbl support may require additional care here. */
6687 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6688 BFD_RELOC_LO16, AT);
6689 offset_expr.X_add_number += 4;
6690
6691 /* Set mips_optimize to 2 to avoid inserting an undesired
6692 nop. */
6693 hold_mips_optimize = mips_optimize;
6694 mips_optimize = 2;
6695 /* Itbl support may require additional care here. */
6696 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6697 BFD_RELOC_LO16, AT);
6698 mips_optimize = hold_mips_optimize;
6699 relax_end ();
6700 }
6701 else
6702 abort ();
6703
6704 break;
6705
6706 case M_LD_OB:
6707 s = "lw";
6708 goto sd_ob;
6709 case M_SD_OB:
6710 s = "sw";
6711 sd_ob:
6712 assert (HAVE_32BIT_ADDRESSES);
6713 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6714 offset_expr.X_add_number += 4;
6715 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6716 break;
6717
6718 /* New code added to support COPZ instructions.
6719 This code builds table entries out of the macros in mip_opcodes.
6720 R4000 uses interlocks to handle coproc delays.
6721 Other chips (like the R3000) require nops to be inserted for delays.
6722
6723 FIXME: Currently, we require that the user handle delays.
6724 In order to fill delay slots for non-interlocked chips,
6725 we must have a way to specify delays based on the coprocessor.
6726 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6727 What are the side-effects of the cop instruction?
6728 What cache support might we have and what are its effects?
6729 Both coprocessor & memory require delays. how long???
6730 What registers are read/set/modified?
6731
6732 If an itbl is provided to interpret cop instructions,
6733 this knowledge can be encoded in the itbl spec. */
6734
6735 case M_COP0:
6736 s = "c0";
6737 goto copz;
6738 case M_COP1:
6739 s = "c1";
6740 goto copz;
6741 case M_COP2:
6742 s = "c2";
6743 goto copz;
6744 case M_COP3:
6745 s = "c3";
6746 copz:
6747 /* For now we just do C (same as Cz). The parameter will be
6748 stored in insn_opcode by mips_ip. */
6749 macro_build (NULL, s, "C", ip->insn_opcode);
6750 break;
6751
6752 case M_MOVE:
6753 move_register (dreg, sreg);
6754 break;
6755
6756 #ifdef LOSING_COMPILER
6757 default:
6758 /* Try and see if this is a new itbl instruction.
6759 This code builds table entries out of the macros in mip_opcodes.
6760 FIXME: For now we just assemble the expression and pass it's
6761 value along as a 32-bit immediate.
6762 We may want to have the assembler assemble this value,
6763 so that we gain the assembler's knowledge of delay slots,
6764 symbols, etc.
6765 Would it be more efficient to use mask (id) here? */
6766 if (itbl_have_entries
6767 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6768 {
6769 s = ip->insn_mo->name;
6770 s2 = "cop3";
6771 coproc = ITBL_DECODE_PNUM (immed_expr);;
6772 macro_build (&immed_expr, s, "C");
6773 break;
6774 }
6775 macro2 (ip);
6776 break;
6777 }
6778 if (mips_opts.noat && used_at)
6779 as_bad (_("Macro used $at after \".set noat\""));
6780 }
6781
6782 static void
6783 macro2 (struct mips_cl_insn *ip)
6784 {
6785 register int treg, sreg, dreg, breg;
6786 int tempreg;
6787 int mask;
6788 int used_at;
6789 expressionS expr1;
6790 const char *s;
6791 const char *s2;
6792 const char *fmt;
6793 int likely = 0;
6794 int dbl = 0;
6795 int coproc = 0;
6796 int lr = 0;
6797 int imm = 0;
6798 int off;
6799 offsetT maxnum;
6800 bfd_reloc_code_real_type r;
6801
6802 treg = (ip->insn_opcode >> 16) & 0x1f;
6803 dreg = (ip->insn_opcode >> 11) & 0x1f;
6804 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6805 mask = ip->insn_mo->mask;
6806
6807 expr1.X_op = O_constant;
6808 expr1.X_op_symbol = NULL;
6809 expr1.X_add_symbol = NULL;
6810 expr1.X_add_number = 1;
6811
6812 switch (mask)
6813 {
6814 #endif /* LOSING_COMPILER */
6815
6816 case M_DMUL:
6817 dbl = 1;
6818 case M_MUL:
6819 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6820 macro_build (NULL, "mflo", "d", dreg);
6821 break;
6822
6823 case M_DMUL_I:
6824 dbl = 1;
6825 case M_MUL_I:
6826 /* The MIPS assembler some times generates shifts and adds. I'm
6827 not trying to be that fancy. GCC should do this for us
6828 anyway. */
6829 used_at = 1;
6830 load_register (AT, &imm_expr, dbl);
6831 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6832 macro_build (NULL, "mflo", "d", dreg);
6833 break;
6834
6835 case M_DMULO_I:
6836 dbl = 1;
6837 case M_MULO_I:
6838 imm = 1;
6839 goto do_mulo;
6840
6841 case M_DMULO:
6842 dbl = 1;
6843 case M_MULO:
6844 do_mulo:
6845 start_noreorder ();
6846 used_at = 1;
6847 if (imm)
6848 load_register (AT, &imm_expr, dbl);
6849 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6850 macro_build (NULL, "mflo", "d", dreg);
6851 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6852 macro_build (NULL, "mfhi", "d", AT);
6853 if (mips_trap)
6854 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6855 else
6856 {
6857 expr1.X_add_number = 8;
6858 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6859 macro_build (NULL, "nop", "", 0);
6860 macro_build (NULL, "break", "c", 6);
6861 }
6862 end_noreorder ();
6863 macro_build (NULL, "mflo", "d", dreg);
6864 break;
6865
6866 case M_DMULOU_I:
6867 dbl = 1;
6868 case M_MULOU_I:
6869 imm = 1;
6870 goto do_mulou;
6871
6872 case M_DMULOU:
6873 dbl = 1;
6874 case M_MULOU:
6875 do_mulou:
6876 start_noreorder ();
6877 used_at = 1;
6878 if (imm)
6879 load_register (AT, &imm_expr, dbl);
6880 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6881 sreg, imm ? AT : treg);
6882 macro_build (NULL, "mfhi", "d", AT);
6883 macro_build (NULL, "mflo", "d", dreg);
6884 if (mips_trap)
6885 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6886 else
6887 {
6888 expr1.X_add_number = 8;
6889 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6890 macro_build (NULL, "nop", "", 0);
6891 macro_build (NULL, "break", "c", 6);
6892 }
6893 end_noreorder ();
6894 break;
6895
6896 case M_DROL:
6897 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6898 {
6899 if (dreg == sreg)
6900 {
6901 tempreg = AT;
6902 used_at = 1;
6903 }
6904 else
6905 {
6906 tempreg = dreg;
6907 }
6908 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6909 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6910 break;
6911 }
6912 used_at = 1;
6913 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6914 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6915 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6916 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6917 break;
6918
6919 case M_ROL:
6920 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6921 {
6922 if (dreg == sreg)
6923 {
6924 tempreg = AT;
6925 used_at = 1;
6926 }
6927 else
6928 {
6929 tempreg = dreg;
6930 }
6931 macro_build (NULL, "negu", "d,w", tempreg, treg);
6932 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6933 break;
6934 }
6935 used_at = 1;
6936 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6937 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6938 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6939 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6940 break;
6941
6942 case M_DROL_I:
6943 {
6944 unsigned int rot;
6945 char *l, *r;
6946
6947 if (imm_expr.X_op != O_constant)
6948 as_bad (_("Improper rotate count"));
6949 rot = imm_expr.X_add_number & 0x3f;
6950 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6951 {
6952 rot = (64 - rot) & 0x3f;
6953 if (rot >= 32)
6954 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6955 else
6956 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6957 break;
6958 }
6959 if (rot == 0)
6960 {
6961 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6962 break;
6963 }
6964 l = (rot < 0x20) ? "dsll" : "dsll32";
6965 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6966 rot &= 0x1f;
6967 used_at = 1;
6968 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6969 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6970 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6971 }
6972 break;
6973
6974 case M_ROL_I:
6975 {
6976 unsigned int rot;
6977
6978 if (imm_expr.X_op != O_constant)
6979 as_bad (_("Improper rotate count"));
6980 rot = imm_expr.X_add_number & 0x1f;
6981 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6982 {
6983 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6984 break;
6985 }
6986 if (rot == 0)
6987 {
6988 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6989 break;
6990 }
6991 used_at = 1;
6992 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6993 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6994 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6995 }
6996 break;
6997
6998 case M_DROR:
6999 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7000 {
7001 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
7002 break;
7003 }
7004 used_at = 1;
7005 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7006 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7007 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7008 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7009 break;
7010
7011 case M_ROR:
7012 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7013 {
7014 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7015 break;
7016 }
7017 used_at = 1;
7018 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7019 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7020 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7021 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7022 break;
7023
7024 case M_DROR_I:
7025 {
7026 unsigned int rot;
7027 char *l, *r;
7028
7029 if (imm_expr.X_op != O_constant)
7030 as_bad (_("Improper rotate count"));
7031 rot = imm_expr.X_add_number & 0x3f;
7032 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7033 {
7034 if (rot >= 32)
7035 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7036 else
7037 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7038 break;
7039 }
7040 if (rot == 0)
7041 {
7042 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7043 break;
7044 }
7045 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7046 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7047 rot &= 0x1f;
7048 used_at = 1;
7049 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7050 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7051 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7052 }
7053 break;
7054
7055 case M_ROR_I:
7056 {
7057 unsigned int rot;
7058
7059 if (imm_expr.X_op != O_constant)
7060 as_bad (_("Improper rotate count"));
7061 rot = imm_expr.X_add_number & 0x1f;
7062 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7063 {
7064 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7065 break;
7066 }
7067 if (rot == 0)
7068 {
7069 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7070 break;
7071 }
7072 used_at = 1;
7073 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7074 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7075 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7076 }
7077 break;
7078
7079 case M_S_DOB:
7080 if (mips_opts.arch == CPU_R4650)
7081 {
7082 as_bad (_("opcode not supported on this processor"));
7083 break;
7084 }
7085 assert (mips_opts.isa == ISA_MIPS1);
7086 /* Even on a big endian machine $fn comes before $fn+1. We have
7087 to adjust when storing to memory. */
7088 macro_build (&offset_expr, "swc1", "T,o(b)",
7089 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7090 offset_expr.X_add_number += 4;
7091 macro_build (&offset_expr, "swc1", "T,o(b)",
7092 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7093 break;
7094
7095 case M_SEQ:
7096 if (sreg == 0)
7097 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7098 else if (treg == 0)
7099 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7100 else
7101 {
7102 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7103 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7104 }
7105 break;
7106
7107 case M_SEQ_I:
7108 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7109 {
7110 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7111 break;
7112 }
7113 if (sreg == 0)
7114 {
7115 as_warn (_("Instruction %s: result is always false"),
7116 ip->insn_mo->name);
7117 move_register (dreg, 0);
7118 break;
7119 }
7120 if (imm_expr.X_op == O_constant
7121 && imm_expr.X_add_number >= 0
7122 && imm_expr.X_add_number < 0x10000)
7123 {
7124 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7125 }
7126 else if (imm_expr.X_op == O_constant
7127 && imm_expr.X_add_number > -0x8000
7128 && imm_expr.X_add_number < 0)
7129 {
7130 imm_expr.X_add_number = -imm_expr.X_add_number;
7131 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7132 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7133 }
7134 else
7135 {
7136 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7137 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7138 used_at = 1;
7139 }
7140 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7141 break;
7142
7143 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7144 s = "slt";
7145 goto sge;
7146 case M_SGEU:
7147 s = "sltu";
7148 sge:
7149 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7150 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7151 break;
7152
7153 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7154 case M_SGEU_I:
7155 if (imm_expr.X_op == O_constant
7156 && imm_expr.X_add_number >= -0x8000
7157 && imm_expr.X_add_number < 0x8000)
7158 {
7159 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7160 dreg, sreg, BFD_RELOC_LO16);
7161 }
7162 else
7163 {
7164 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7165 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7166 dreg, sreg, AT);
7167 used_at = 1;
7168 }
7169 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7170 break;
7171
7172 case M_SGT: /* sreg > treg <==> treg < sreg */
7173 s = "slt";
7174 goto sgt;
7175 case M_SGTU:
7176 s = "sltu";
7177 sgt:
7178 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7179 break;
7180
7181 case M_SGT_I: /* sreg > I <==> I < sreg */
7182 s = "slt";
7183 goto sgti;
7184 case M_SGTU_I:
7185 s = "sltu";
7186 sgti:
7187 used_at = 1;
7188 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7189 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7190 break;
7191
7192 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7193 s = "slt";
7194 goto sle;
7195 case M_SLEU:
7196 s = "sltu";
7197 sle:
7198 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7199 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7200 break;
7201
7202 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7203 s = "slt";
7204 goto slei;
7205 case M_SLEU_I:
7206 s = "sltu";
7207 slei:
7208 used_at = 1;
7209 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7210 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7211 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7212 break;
7213
7214 case M_SLT_I:
7215 if (imm_expr.X_op == O_constant
7216 && imm_expr.X_add_number >= -0x8000
7217 && imm_expr.X_add_number < 0x8000)
7218 {
7219 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7220 break;
7221 }
7222 used_at = 1;
7223 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7224 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7225 break;
7226
7227 case M_SLTU_I:
7228 if (imm_expr.X_op == O_constant
7229 && imm_expr.X_add_number >= -0x8000
7230 && imm_expr.X_add_number < 0x8000)
7231 {
7232 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7233 BFD_RELOC_LO16);
7234 break;
7235 }
7236 used_at = 1;
7237 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7238 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7239 break;
7240
7241 case M_SNE:
7242 if (sreg == 0)
7243 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7244 else if (treg == 0)
7245 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7246 else
7247 {
7248 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7249 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7250 }
7251 break;
7252
7253 case M_SNE_I:
7254 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7255 {
7256 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7257 break;
7258 }
7259 if (sreg == 0)
7260 {
7261 as_warn (_("Instruction %s: result is always true"),
7262 ip->insn_mo->name);
7263 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7264 dreg, 0, BFD_RELOC_LO16);
7265 break;
7266 }
7267 if (imm_expr.X_op == O_constant
7268 && imm_expr.X_add_number >= 0
7269 && imm_expr.X_add_number < 0x10000)
7270 {
7271 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7272 }
7273 else if (imm_expr.X_op == O_constant
7274 && imm_expr.X_add_number > -0x8000
7275 && imm_expr.X_add_number < 0)
7276 {
7277 imm_expr.X_add_number = -imm_expr.X_add_number;
7278 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7279 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7280 }
7281 else
7282 {
7283 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7284 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7285 used_at = 1;
7286 }
7287 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7288 break;
7289
7290 case M_DSUB_I:
7291 dbl = 1;
7292 case M_SUB_I:
7293 if (imm_expr.X_op == O_constant
7294 && imm_expr.X_add_number > -0x8000
7295 && imm_expr.X_add_number <= 0x8000)
7296 {
7297 imm_expr.X_add_number = -imm_expr.X_add_number;
7298 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7299 dreg, sreg, BFD_RELOC_LO16);
7300 break;
7301 }
7302 used_at = 1;
7303 load_register (AT, &imm_expr, dbl);
7304 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7305 break;
7306
7307 case M_DSUBU_I:
7308 dbl = 1;
7309 case M_SUBU_I:
7310 if (imm_expr.X_op == O_constant
7311 && imm_expr.X_add_number > -0x8000
7312 && imm_expr.X_add_number <= 0x8000)
7313 {
7314 imm_expr.X_add_number = -imm_expr.X_add_number;
7315 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7316 dreg, sreg, BFD_RELOC_LO16);
7317 break;
7318 }
7319 used_at = 1;
7320 load_register (AT, &imm_expr, dbl);
7321 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7322 break;
7323
7324 case M_TEQ_I:
7325 s = "teq";
7326 goto trap;
7327 case M_TGE_I:
7328 s = "tge";
7329 goto trap;
7330 case M_TGEU_I:
7331 s = "tgeu";
7332 goto trap;
7333 case M_TLT_I:
7334 s = "tlt";
7335 goto trap;
7336 case M_TLTU_I:
7337 s = "tltu";
7338 goto trap;
7339 case M_TNE_I:
7340 s = "tne";
7341 trap:
7342 used_at = 1;
7343 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7344 macro_build (NULL, s, "s,t", sreg, AT);
7345 break;
7346
7347 case M_TRUNCWS:
7348 case M_TRUNCWD:
7349 assert (mips_opts.isa == ISA_MIPS1);
7350 used_at = 1;
7351 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7352 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7353
7354 /*
7355 * Is the double cfc1 instruction a bug in the mips assembler;
7356 * or is there a reason for it?
7357 */
7358 start_noreorder ();
7359 macro_build (NULL, "cfc1", "t,G", treg, RA);
7360 macro_build (NULL, "cfc1", "t,G", treg, RA);
7361 macro_build (NULL, "nop", "");
7362 expr1.X_add_number = 3;
7363 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7364 expr1.X_add_number = 2;
7365 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7366 macro_build (NULL, "ctc1", "t,G", AT, RA);
7367 macro_build (NULL, "nop", "");
7368 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7369 dreg, sreg);
7370 macro_build (NULL, "ctc1", "t,G", treg, RA);
7371 macro_build (NULL, "nop", "");
7372 end_noreorder ();
7373 break;
7374
7375 case M_ULH:
7376 s = "lb";
7377 goto ulh;
7378 case M_ULHU:
7379 s = "lbu";
7380 ulh:
7381 used_at = 1;
7382 if (offset_expr.X_add_number >= 0x7fff)
7383 as_bad (_("operand overflow"));
7384 if (! target_big_endian)
7385 ++offset_expr.X_add_number;
7386 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7387 if (! target_big_endian)
7388 --offset_expr.X_add_number;
7389 else
7390 ++offset_expr.X_add_number;
7391 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7392 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7393 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7394 break;
7395
7396 case M_ULD:
7397 s = "ldl";
7398 s2 = "ldr";
7399 off = 7;
7400 goto ulw;
7401 case M_ULW:
7402 s = "lwl";
7403 s2 = "lwr";
7404 off = 3;
7405 ulw:
7406 if (offset_expr.X_add_number >= 0x8000 - off)
7407 as_bad (_("operand overflow"));
7408 if (treg != breg)
7409 tempreg = treg;
7410 else
7411 {
7412 used_at = 1;
7413 tempreg = AT;
7414 }
7415 if (! target_big_endian)
7416 offset_expr.X_add_number += off;
7417 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7418 if (! target_big_endian)
7419 offset_expr.X_add_number -= off;
7420 else
7421 offset_expr.X_add_number += off;
7422 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7423
7424 /* If necessary, move the result in tempreg the final destination. */
7425 if (treg == tempreg)
7426 break;
7427 /* Protect second load's delay slot. */
7428 load_delay_nop ();
7429 move_register (treg, tempreg);
7430 break;
7431
7432 case M_ULD_A:
7433 s = "ldl";
7434 s2 = "ldr";
7435 off = 7;
7436 goto ulwa;
7437 case M_ULW_A:
7438 s = "lwl";
7439 s2 = "lwr";
7440 off = 3;
7441 ulwa:
7442 used_at = 1;
7443 load_address (AT, &offset_expr, &used_at);
7444 if (breg != 0)
7445 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7446 if (! target_big_endian)
7447 expr1.X_add_number = off;
7448 else
7449 expr1.X_add_number = 0;
7450 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7451 if (! target_big_endian)
7452 expr1.X_add_number = 0;
7453 else
7454 expr1.X_add_number = off;
7455 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7456 break;
7457
7458 case M_ULH_A:
7459 case M_ULHU_A:
7460 used_at = 1;
7461 load_address (AT, &offset_expr, &used_at);
7462 if (breg != 0)
7463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7464 if (target_big_endian)
7465 expr1.X_add_number = 0;
7466 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7467 treg, BFD_RELOC_LO16, AT);
7468 if (target_big_endian)
7469 expr1.X_add_number = 1;
7470 else
7471 expr1.X_add_number = 0;
7472 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7473 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7474 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7475 break;
7476
7477 case M_USH:
7478 used_at = 1;
7479 if (offset_expr.X_add_number >= 0x7fff)
7480 as_bad (_("operand overflow"));
7481 if (target_big_endian)
7482 ++offset_expr.X_add_number;
7483 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7484 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7485 if (target_big_endian)
7486 --offset_expr.X_add_number;
7487 else
7488 ++offset_expr.X_add_number;
7489 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7490 break;
7491
7492 case M_USD:
7493 s = "sdl";
7494 s2 = "sdr";
7495 off = 7;
7496 goto usw;
7497 case M_USW:
7498 s = "swl";
7499 s2 = "swr";
7500 off = 3;
7501 usw:
7502 if (offset_expr.X_add_number >= 0x8000 - off)
7503 as_bad (_("operand overflow"));
7504 if (! target_big_endian)
7505 offset_expr.X_add_number += off;
7506 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7507 if (! target_big_endian)
7508 offset_expr.X_add_number -= off;
7509 else
7510 offset_expr.X_add_number += off;
7511 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7512 break;
7513
7514 case M_USD_A:
7515 s = "sdl";
7516 s2 = "sdr";
7517 off = 7;
7518 goto uswa;
7519 case M_USW_A:
7520 s = "swl";
7521 s2 = "swr";
7522 off = 3;
7523 uswa:
7524 used_at = 1;
7525 load_address (AT, &offset_expr, &used_at);
7526 if (breg != 0)
7527 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7528 if (! target_big_endian)
7529 expr1.X_add_number = off;
7530 else
7531 expr1.X_add_number = 0;
7532 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7533 if (! target_big_endian)
7534 expr1.X_add_number = 0;
7535 else
7536 expr1.X_add_number = off;
7537 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7538 break;
7539
7540 case M_USH_A:
7541 used_at = 1;
7542 load_address (AT, &offset_expr, &used_at);
7543 if (breg != 0)
7544 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7545 if (! target_big_endian)
7546 expr1.X_add_number = 0;
7547 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7548 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7549 if (! target_big_endian)
7550 expr1.X_add_number = 1;
7551 else
7552 expr1.X_add_number = 0;
7553 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7554 if (! target_big_endian)
7555 expr1.X_add_number = 0;
7556 else
7557 expr1.X_add_number = 1;
7558 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7559 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7560 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7561 break;
7562
7563 default:
7564 /* FIXME: Check if this is one of the itbl macros, since they
7565 are added dynamically. */
7566 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7567 break;
7568 }
7569 if (mips_opts.noat && used_at)
7570 as_bad (_("Macro used $at after \".set noat\""));
7571 }
7572
7573 /* Implement macros in mips16 mode. */
7574
7575 static void
7576 mips16_macro (struct mips_cl_insn *ip)
7577 {
7578 int mask;
7579 int xreg, yreg, zreg, tmp;
7580 expressionS expr1;
7581 int dbl;
7582 const char *s, *s2, *s3;
7583
7584 mask = ip->insn_mo->mask;
7585
7586 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7587 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7588 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7589
7590 expr1.X_op = O_constant;
7591 expr1.X_op_symbol = NULL;
7592 expr1.X_add_symbol = NULL;
7593 expr1.X_add_number = 1;
7594
7595 dbl = 0;
7596
7597 switch (mask)
7598 {
7599 default:
7600 internalError ();
7601
7602 case M_DDIV_3:
7603 dbl = 1;
7604 case M_DIV_3:
7605 s = "mflo";
7606 goto do_div3;
7607 case M_DREM_3:
7608 dbl = 1;
7609 case M_REM_3:
7610 s = "mfhi";
7611 do_div3:
7612 start_noreorder ();
7613 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7614 expr1.X_add_number = 2;
7615 macro_build (&expr1, "bnez", "x,p", yreg);
7616 macro_build (NULL, "break", "6", 7);
7617
7618 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7619 since that causes an overflow. We should do that as well,
7620 but I don't see how to do the comparisons without a temporary
7621 register. */
7622 end_noreorder ();
7623 macro_build (NULL, s, "x", zreg);
7624 break;
7625
7626 case M_DIVU_3:
7627 s = "divu";
7628 s2 = "mflo";
7629 goto do_divu3;
7630 case M_REMU_3:
7631 s = "divu";
7632 s2 = "mfhi";
7633 goto do_divu3;
7634 case M_DDIVU_3:
7635 s = "ddivu";
7636 s2 = "mflo";
7637 goto do_divu3;
7638 case M_DREMU_3:
7639 s = "ddivu";
7640 s2 = "mfhi";
7641 do_divu3:
7642 start_noreorder ();
7643 macro_build (NULL, s, "0,x,y", xreg, yreg);
7644 expr1.X_add_number = 2;
7645 macro_build (&expr1, "bnez", "x,p", yreg);
7646 macro_build (NULL, "break", "6", 7);
7647 end_noreorder ();
7648 macro_build (NULL, s2, "x", zreg);
7649 break;
7650
7651 case M_DMUL:
7652 dbl = 1;
7653 case M_MUL:
7654 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7655 macro_build (NULL, "mflo", "x", zreg);
7656 break;
7657
7658 case M_DSUBU_I:
7659 dbl = 1;
7660 goto do_subu;
7661 case M_SUBU_I:
7662 do_subu:
7663 if (imm_expr.X_op != O_constant)
7664 as_bad (_("Unsupported large constant"));
7665 imm_expr.X_add_number = -imm_expr.X_add_number;
7666 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7667 break;
7668
7669 case M_SUBU_I_2:
7670 if (imm_expr.X_op != O_constant)
7671 as_bad (_("Unsupported large constant"));
7672 imm_expr.X_add_number = -imm_expr.X_add_number;
7673 macro_build (&imm_expr, "addiu", "x,k", xreg);
7674 break;
7675
7676 case M_DSUBU_I_2:
7677 if (imm_expr.X_op != O_constant)
7678 as_bad (_("Unsupported large constant"));
7679 imm_expr.X_add_number = -imm_expr.X_add_number;
7680 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7681 break;
7682
7683 case M_BEQ:
7684 s = "cmp";
7685 s2 = "bteqz";
7686 goto do_branch;
7687 case M_BNE:
7688 s = "cmp";
7689 s2 = "btnez";
7690 goto do_branch;
7691 case M_BLT:
7692 s = "slt";
7693 s2 = "btnez";
7694 goto do_branch;
7695 case M_BLTU:
7696 s = "sltu";
7697 s2 = "btnez";
7698 goto do_branch;
7699 case M_BLE:
7700 s = "slt";
7701 s2 = "bteqz";
7702 goto do_reverse_branch;
7703 case M_BLEU:
7704 s = "sltu";
7705 s2 = "bteqz";
7706 goto do_reverse_branch;
7707 case M_BGE:
7708 s = "slt";
7709 s2 = "bteqz";
7710 goto do_branch;
7711 case M_BGEU:
7712 s = "sltu";
7713 s2 = "bteqz";
7714 goto do_branch;
7715 case M_BGT:
7716 s = "slt";
7717 s2 = "btnez";
7718 goto do_reverse_branch;
7719 case M_BGTU:
7720 s = "sltu";
7721 s2 = "btnez";
7722
7723 do_reverse_branch:
7724 tmp = xreg;
7725 xreg = yreg;
7726 yreg = tmp;
7727
7728 do_branch:
7729 macro_build (NULL, s, "x,y", xreg, yreg);
7730 macro_build (&offset_expr, s2, "p");
7731 break;
7732
7733 case M_BEQ_I:
7734 s = "cmpi";
7735 s2 = "bteqz";
7736 s3 = "x,U";
7737 goto do_branch_i;
7738 case M_BNE_I:
7739 s = "cmpi";
7740 s2 = "btnez";
7741 s3 = "x,U";
7742 goto do_branch_i;
7743 case M_BLT_I:
7744 s = "slti";
7745 s2 = "btnez";
7746 s3 = "x,8";
7747 goto do_branch_i;
7748 case M_BLTU_I:
7749 s = "sltiu";
7750 s2 = "btnez";
7751 s3 = "x,8";
7752 goto do_branch_i;
7753 case M_BLE_I:
7754 s = "slti";
7755 s2 = "btnez";
7756 s3 = "x,8";
7757 goto do_addone_branch_i;
7758 case M_BLEU_I:
7759 s = "sltiu";
7760 s2 = "btnez";
7761 s3 = "x,8";
7762 goto do_addone_branch_i;
7763 case M_BGE_I:
7764 s = "slti";
7765 s2 = "bteqz";
7766 s3 = "x,8";
7767 goto do_branch_i;
7768 case M_BGEU_I:
7769 s = "sltiu";
7770 s2 = "bteqz";
7771 s3 = "x,8";
7772 goto do_branch_i;
7773 case M_BGT_I:
7774 s = "slti";
7775 s2 = "bteqz";
7776 s3 = "x,8";
7777 goto do_addone_branch_i;
7778 case M_BGTU_I:
7779 s = "sltiu";
7780 s2 = "bteqz";
7781 s3 = "x,8";
7782
7783 do_addone_branch_i:
7784 if (imm_expr.X_op != O_constant)
7785 as_bad (_("Unsupported large constant"));
7786 ++imm_expr.X_add_number;
7787
7788 do_branch_i:
7789 macro_build (&imm_expr, s, s3, xreg);
7790 macro_build (&offset_expr, s2, "p");
7791 break;
7792
7793 case M_ABS:
7794 expr1.X_add_number = 0;
7795 macro_build (&expr1, "slti", "x,8", yreg);
7796 if (xreg != yreg)
7797 move_register (xreg, yreg);
7798 expr1.X_add_number = 2;
7799 macro_build (&expr1, "bteqz", "p");
7800 macro_build (NULL, "neg", "x,w", xreg, xreg);
7801 }
7802 }
7803
7804 /* For consistency checking, verify that all bits are specified either
7805 by the match/mask part of the instruction definition, or by the
7806 operand list. */
7807 static int
7808 validate_mips_insn (const struct mips_opcode *opc)
7809 {
7810 const char *p = opc->args;
7811 char c;
7812 unsigned long used_bits = opc->mask;
7813
7814 if ((used_bits & opc->match) != opc->match)
7815 {
7816 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7817 opc->name, opc->args);
7818 return 0;
7819 }
7820 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7821 while (*p)
7822 switch (c = *p++)
7823 {
7824 case ',': break;
7825 case '(': break;
7826 case ')': break;
7827 case '+':
7828 switch (c = *p++)
7829 {
7830 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
7831 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
7832 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
7833 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
7834 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7835 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7836 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7837 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7838 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7839 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7840 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7841 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7842 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7843 case 'I': break;
7844 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7845 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
7846 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7847 default:
7848 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7849 c, opc->name, opc->args);
7850 return 0;
7851 }
7852 break;
7853 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7854 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7855 case 'A': break;
7856 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7857 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7858 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7859 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7860 case 'F': break;
7861 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7862 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7863 case 'I': break;
7864 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7865 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7866 case 'L': break;
7867 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7868 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7869 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7870 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7871 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7872 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7873 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7874 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7875 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7876 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7877 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7878 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7879 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7880 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7881 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7882 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7883 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7884 case 'f': break;
7885 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7886 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7887 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7888 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7889 case 'l': break;
7890 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7891 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7892 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7893 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7894 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7895 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7896 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7897 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7898 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7899 case 'x': break;
7900 case 'z': break;
7901 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7902 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7903 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7904 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7905 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7906 case '[': break;
7907 case ']': break;
7908 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
7909 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
7910 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
7911 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7912 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
7913 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
7914 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
7915 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
7916 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
7917 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
7918 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
7919 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
7920 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
7921 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
7922 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
7923 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7924 default:
7925 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7926 c, opc->name, opc->args);
7927 return 0;
7928 }
7929 #undef USE_BITS
7930 if (used_bits != 0xffffffff)
7931 {
7932 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7933 ~used_bits & 0xffffffff, opc->name, opc->args);
7934 return 0;
7935 }
7936 return 1;
7937 }
7938
7939 /* UDI immediates. */
7940 struct mips_immed {
7941 char type;
7942 unsigned int shift;
7943 unsigned long mask;
7944 const char * desc;
7945 };
7946
7947 static const struct mips_immed mips_immed[] = {
7948 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
7949 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
7950 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
7951 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
7952 { 0,0,0,0 }
7953 };
7954
7955 /* This routine assembles an instruction into its binary format. As a
7956 side effect, it sets one of the global variables imm_reloc or
7957 offset_reloc to the type of relocation to do if one of the operands
7958 is an address expression. */
7959
7960 static void
7961 mips_ip (char *str, struct mips_cl_insn *ip)
7962 {
7963 char *s;
7964 const char *args;
7965 char c = 0;
7966 struct mips_opcode *insn;
7967 char *argsStart;
7968 unsigned int regno;
7969 unsigned int lastregno = 0;
7970 unsigned int lastpos = 0;
7971 unsigned int limlo, limhi;
7972 char *s_reset;
7973 char save_c = 0;
7974 offsetT min_range, max_range;
7975
7976 insn_error = NULL;
7977
7978 /* If the instruction contains a '.', we first try to match an instruction
7979 including the '.'. Then we try again without the '.'. */
7980 insn = NULL;
7981 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7982 continue;
7983
7984 /* If we stopped on whitespace, then replace the whitespace with null for
7985 the call to hash_find. Save the character we replaced just in case we
7986 have to re-parse the instruction. */
7987 if (ISSPACE (*s))
7988 {
7989 save_c = *s;
7990 *s++ = '\0';
7991 }
7992
7993 insn = (struct mips_opcode *) hash_find (op_hash, str);
7994
7995 /* If we didn't find the instruction in the opcode table, try again, but
7996 this time with just the instruction up to, but not including the
7997 first '.'. */
7998 if (insn == NULL)
7999 {
8000 /* Restore the character we overwrite above (if any). */
8001 if (save_c)
8002 *(--s) = save_c;
8003
8004 /* Scan up to the first '.' or whitespace. */
8005 for (s = str;
8006 *s != '\0' && *s != '.' && !ISSPACE (*s);
8007 ++s)
8008 continue;
8009
8010 /* If we did not find a '.', then we can quit now. */
8011 if (*s != '.')
8012 {
8013 insn_error = "unrecognized opcode";
8014 return;
8015 }
8016
8017 /* Lookup the instruction in the hash table. */
8018 *s++ = '\0';
8019 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8020 {
8021 insn_error = "unrecognized opcode";
8022 return;
8023 }
8024 }
8025
8026 argsStart = s;
8027 for (;;)
8028 {
8029 bfd_boolean ok;
8030
8031 assert (strcmp (insn->name, str) == 0);
8032
8033 if (OPCODE_IS_MEMBER (insn,
8034 (mips_opts.isa
8035 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8036 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8037 | (mips_opts.ase_dsp ? INSN_DSP : 0)
8038 | (mips_opts.ase_mt ? INSN_MT : 0)
8039 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)
8040 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
8041 mips_opts.arch))
8042 ok = TRUE;
8043 else
8044 ok = FALSE;
8045
8046 if (insn->pinfo != INSN_MACRO)
8047 {
8048 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8049 ok = FALSE;
8050 }
8051
8052 if (! ok)
8053 {
8054 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8055 && strcmp (insn->name, insn[1].name) == 0)
8056 {
8057 ++insn;
8058 continue;
8059 }
8060 else
8061 {
8062 if (!insn_error)
8063 {
8064 static char buf[100];
8065 sprintf (buf,
8066 _("opcode not supported on this processor: %s (%s)"),
8067 mips_cpu_info_from_arch (mips_opts.arch)->name,
8068 mips_cpu_info_from_isa (mips_opts.isa)->name);
8069 insn_error = buf;
8070 }
8071 if (save_c)
8072 *(--s) = save_c;
8073 return;
8074 }
8075 }
8076
8077 create_insn (ip, insn);
8078 insn_error = NULL;
8079 for (args = insn->args;; ++args)
8080 {
8081 int is_mdmx;
8082
8083 s += strspn (s, " \t");
8084 is_mdmx = 0;
8085 switch (*args)
8086 {
8087 case '\0': /* end of args */
8088 if (*s == '\0')
8089 return;
8090 break;
8091
8092 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8093 my_getExpression (&imm_expr, s);
8094 check_absolute_expr (ip, &imm_expr);
8095 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8096 {
8097 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8098 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8099 imm_expr.X_add_number &= OP_MASK_SA3;
8100 }
8101 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3;
8102 imm_expr.X_op = O_absent;
8103 s = expr_end;
8104 continue;
8105
8106 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8107 my_getExpression (&imm_expr, s);
8108 check_absolute_expr (ip, &imm_expr);
8109 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8110 {
8111 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8112 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8113 imm_expr.X_add_number &= OP_MASK_SA4;
8114 }
8115 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4;
8116 imm_expr.X_op = O_absent;
8117 s = expr_end;
8118 continue;
8119
8120 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8121 my_getExpression (&imm_expr, s);
8122 check_absolute_expr (ip, &imm_expr);
8123 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8124 {
8125 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8126 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8127 imm_expr.X_add_number &= OP_MASK_IMM8;
8128 }
8129 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8;
8130 imm_expr.X_op = O_absent;
8131 s = expr_end;
8132 continue;
8133
8134 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8135 my_getExpression (&imm_expr, s);
8136 check_absolute_expr (ip, &imm_expr);
8137 if (imm_expr.X_add_number & ~OP_MASK_RS)
8138 {
8139 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8140 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8141 imm_expr.X_add_number &= OP_MASK_RS;
8142 }
8143 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS;
8144 imm_expr.X_op = O_absent;
8145 s = expr_end;
8146 continue;
8147
8148 case '7': /* four dsp accumulators in bits 11,12 */
8149 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8150 s[3] >= '0' && s[3] <= '3')
8151 {
8152 regno = s[3] - '0';
8153 s += 4;
8154 ip->insn_opcode |= regno << OP_SH_DSPACC;
8155 continue;
8156 }
8157 else
8158 as_bad (_("Invalid dsp acc register"));
8159 break;
8160
8161 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8162 my_getExpression (&imm_expr, s);
8163 check_absolute_expr (ip, &imm_expr);
8164 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8165 {
8166 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8167 OP_MASK_WRDSP,
8168 (unsigned long) imm_expr.X_add_number);
8169 imm_expr.X_add_number &= OP_MASK_WRDSP;
8170 }
8171 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP;
8172 imm_expr.X_op = O_absent;
8173 s = expr_end;
8174 continue;
8175
8176 case '9': /* four dsp accumulators in bits 21,22 */
8177 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8178 s[3] >= '0' && s[3] <= '3')
8179 {
8180 regno = s[3] - '0';
8181 s += 4;
8182 ip->insn_opcode |= regno << OP_SH_DSPACC_S;
8183 continue;
8184 }
8185 else
8186 as_bad (_("Invalid dsp acc register"));
8187 break;
8188
8189 case '0': /* dsp 6-bit signed immediate in bit 20 */
8190 my_getExpression (&imm_expr, s);
8191 check_absolute_expr (ip, &imm_expr);
8192 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8193 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8194 if (imm_expr.X_add_number < min_range ||
8195 imm_expr.X_add_number > max_range)
8196 {
8197 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8198 (long) min_range, (long) max_range,
8199 (long) imm_expr.X_add_number);
8200 }
8201 imm_expr.X_add_number &= OP_MASK_DSPSFT;
8202 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8203 << OP_SH_DSPSFT);
8204 imm_expr.X_op = O_absent;
8205 s = expr_end;
8206 continue;
8207
8208 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8209 my_getExpression (&imm_expr, s);
8210 check_absolute_expr (ip, &imm_expr);
8211 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8212 {
8213 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8214 OP_MASK_RDDSP,
8215 (unsigned long) imm_expr.X_add_number);
8216 imm_expr.X_add_number &= OP_MASK_RDDSP;
8217 }
8218 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP;
8219 imm_expr.X_op = O_absent;
8220 s = expr_end;
8221 continue;
8222
8223 case ':': /* dsp 7-bit signed immediate in bit 19 */
8224 my_getExpression (&imm_expr, s);
8225 check_absolute_expr (ip, &imm_expr);
8226 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8227 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8228 if (imm_expr.X_add_number < min_range ||
8229 imm_expr.X_add_number > max_range)
8230 {
8231 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8232 (long) min_range, (long) max_range,
8233 (long) imm_expr.X_add_number);
8234 }
8235 imm_expr.X_add_number &= OP_MASK_DSPSFT_7;
8236 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8237 << OP_SH_DSPSFT_7);
8238 imm_expr.X_op = O_absent;
8239 s = expr_end;
8240 continue;
8241
8242 case '@': /* dsp 10-bit signed immediate in bit 16 */
8243 my_getExpression (&imm_expr, s);
8244 check_absolute_expr (ip, &imm_expr);
8245 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8246 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8247 if (imm_expr.X_add_number < min_range ||
8248 imm_expr.X_add_number > max_range)
8249 {
8250 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8251 (long) min_range, (long) max_range,
8252 (long) imm_expr.X_add_number);
8253 }
8254 imm_expr.X_add_number &= OP_MASK_IMM10;
8255 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8256 << OP_SH_IMM10);
8257 imm_expr.X_op = O_absent;
8258 s = expr_end;
8259 continue;
8260
8261 case '!': /* mt 1-bit unsigned immediate in bit 5 */
8262 my_getExpression (&imm_expr, s);
8263 check_absolute_expr (ip, &imm_expr);
8264 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8265 {
8266 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8267 OP_MASK_MT_U, (unsigned long) imm_expr.X_add_number);
8268 imm_expr.X_add_number &= OP_MASK_MT_U;
8269 }
8270 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_U;
8271 imm_expr.X_op = O_absent;
8272 s = expr_end;
8273 continue;
8274
8275 case '$': /* mt 1-bit unsigned immediate in bit 4 */
8276 my_getExpression (&imm_expr, s);
8277 check_absolute_expr (ip, &imm_expr);
8278 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8279 {
8280 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8281 OP_MASK_MT_H, (unsigned long) imm_expr.X_add_number);
8282 imm_expr.X_add_number &= OP_MASK_MT_H;
8283 }
8284 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_H;
8285 imm_expr.X_op = O_absent;
8286 s = expr_end;
8287 continue;
8288
8289 case '*': /* four dsp accumulators in bits 18,19 */
8290 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8291 s[3] >= '0' && s[3] <= '3')
8292 {
8293 regno = s[3] - '0';
8294 s += 4;
8295 ip->insn_opcode |= regno << OP_SH_MTACC_T;
8296 continue;
8297 }
8298 else
8299 as_bad (_("Invalid dsp/smartmips acc register"));
8300 break;
8301
8302 case '&': /* four dsp accumulators in bits 13,14 */
8303 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8304 s[3] >= '0' && s[3] <= '3')
8305 {
8306 regno = s[3] - '0';
8307 s += 4;
8308 ip->insn_opcode |= regno << OP_SH_MTACC_D;
8309 continue;
8310 }
8311 else
8312 as_bad (_("Invalid dsp/smartmips acc register"));
8313 break;
8314
8315 case ',':
8316 if (*s++ == *args)
8317 continue;
8318 s--;
8319 switch (*++args)
8320 {
8321 case 'r':
8322 case 'v':
8323 INSERT_OPERAND (RS, *ip, lastregno);
8324 continue;
8325
8326 case 'w':
8327 INSERT_OPERAND (RT, *ip, lastregno);
8328 continue;
8329
8330 case 'W':
8331 INSERT_OPERAND (FT, *ip, lastregno);
8332 continue;
8333
8334 case 'V':
8335 INSERT_OPERAND (FS, *ip, lastregno);
8336 continue;
8337 }
8338 break;
8339
8340 case '(':
8341 /* Handle optional base register.
8342 Either the base register is omitted or
8343 we must have a left paren. */
8344 /* This is dependent on the next operand specifier
8345 is a base register specification. */
8346 assert (args[1] == 'b' || args[1] == '5'
8347 || args[1] == '-' || args[1] == '4');
8348 if (*s == '\0')
8349 return;
8350
8351 case ')': /* these must match exactly */
8352 case '[':
8353 case ']':
8354 if (*s++ == *args)
8355 continue;
8356 break;
8357
8358 case '+': /* Opcode extension character. */
8359 switch (*++args)
8360 {
8361 case '1': /* UDI immediates. */
8362 case '2':
8363 case '3':
8364 case '4':
8365 {
8366 const struct mips_immed *imm = mips_immed;
8367
8368 while (imm->type && imm->type != *args)
8369 ++imm;
8370 if (! imm->type)
8371 internalError ();
8372 my_getExpression (&imm_expr, s);
8373 check_absolute_expr (ip, &imm_expr);
8374 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8375 {
8376 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8377 imm->desc ? imm->desc : ip->insn_mo->name,
8378 (unsigned long) imm_expr.X_add_number,
8379 (unsigned long) imm_expr.X_add_number);
8380 imm_expr.X_add_number &= imm->mask;
8381 }
8382 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8383 << imm->shift);
8384 imm_expr.X_op = O_absent;
8385 s = expr_end;
8386 }
8387 continue;
8388
8389 case 'A': /* ins/ext position, becomes LSB. */
8390 limlo = 0;
8391 limhi = 31;
8392 goto do_lsb;
8393 case 'E':
8394 limlo = 32;
8395 limhi = 63;
8396 goto do_lsb;
8397 do_lsb:
8398 my_getExpression (&imm_expr, s);
8399 check_absolute_expr (ip, &imm_expr);
8400 if ((unsigned long) imm_expr.X_add_number < limlo
8401 || (unsigned long) imm_expr.X_add_number > limhi)
8402 {
8403 as_bad (_("Improper position (%lu)"),
8404 (unsigned long) imm_expr.X_add_number);
8405 imm_expr.X_add_number = limlo;
8406 }
8407 lastpos = imm_expr.X_add_number;
8408 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8409 imm_expr.X_op = O_absent;
8410 s = expr_end;
8411 continue;
8412
8413 case 'B': /* ins size, becomes MSB. */
8414 limlo = 1;
8415 limhi = 32;
8416 goto do_msb;
8417 case 'F':
8418 limlo = 33;
8419 limhi = 64;
8420 goto do_msb;
8421 do_msb:
8422 my_getExpression (&imm_expr, s);
8423 check_absolute_expr (ip, &imm_expr);
8424 /* Check for negative input so that small negative numbers
8425 will not succeed incorrectly. The checks against
8426 (pos+size) transitively check "size" itself,
8427 assuming that "pos" is reasonable. */
8428 if ((long) imm_expr.X_add_number < 0
8429 || ((unsigned long) imm_expr.X_add_number
8430 + lastpos) < limlo
8431 || ((unsigned long) imm_expr.X_add_number
8432 + lastpos) > limhi)
8433 {
8434 as_bad (_("Improper insert size (%lu, position %lu)"),
8435 (unsigned long) imm_expr.X_add_number,
8436 (unsigned long) lastpos);
8437 imm_expr.X_add_number = limlo - lastpos;
8438 }
8439 INSERT_OPERAND (INSMSB, *ip,
8440 lastpos + imm_expr.X_add_number - 1);
8441 imm_expr.X_op = O_absent;
8442 s = expr_end;
8443 continue;
8444
8445 case 'C': /* ext size, becomes MSBD. */
8446 limlo = 1;
8447 limhi = 32;
8448 goto do_msbd;
8449 case 'G':
8450 limlo = 33;
8451 limhi = 64;
8452 goto do_msbd;
8453 case 'H':
8454 limlo = 33;
8455 limhi = 64;
8456 goto do_msbd;
8457 do_msbd:
8458 my_getExpression (&imm_expr, s);
8459 check_absolute_expr (ip, &imm_expr);
8460 /* Check for negative input so that small negative numbers
8461 will not succeed incorrectly. The checks against
8462 (pos+size) transitively check "size" itself,
8463 assuming that "pos" is reasonable. */
8464 if ((long) imm_expr.X_add_number < 0
8465 || ((unsigned long) imm_expr.X_add_number
8466 + lastpos) < limlo
8467 || ((unsigned long) imm_expr.X_add_number
8468 + lastpos) > limhi)
8469 {
8470 as_bad (_("Improper extract size (%lu, position %lu)"),
8471 (unsigned long) imm_expr.X_add_number,
8472 (unsigned long) lastpos);
8473 imm_expr.X_add_number = limlo - lastpos;
8474 }
8475 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8476 imm_expr.X_op = O_absent;
8477 s = expr_end;
8478 continue;
8479
8480 case 'D':
8481 /* +D is for disassembly only; never match. */
8482 break;
8483
8484 case 'I':
8485 /* "+I" is like "I", except that imm2_expr is used. */
8486 my_getExpression (&imm2_expr, s);
8487 if (imm2_expr.X_op != O_big
8488 && imm2_expr.X_op != O_constant)
8489 insn_error = _("absolute expression required");
8490 if (HAVE_32BIT_GPRS)
8491 normalize_constant_expr (&imm2_expr);
8492 s = expr_end;
8493 continue;
8494
8495 case 'T': /* Coprocessor register */
8496 /* +T is for disassembly only; never match. */
8497 break;
8498
8499 case 't': /* Coprocessor register number */
8500 if (s[0] == '$' && ISDIGIT (s[1]))
8501 {
8502 ++s;
8503 regno = 0;
8504 do
8505 {
8506 regno *= 10;
8507 regno += *s - '0';
8508 ++s;
8509 }
8510 while (ISDIGIT (*s));
8511 if (regno > 31)
8512 as_bad (_("Invalid register number (%d)"), regno);
8513 else
8514 {
8515 ip->insn_opcode |= regno << OP_SH_RT;
8516 continue;
8517 }
8518 }
8519 else
8520 as_bad (_("Invalid coprocessor 0 register number"));
8521 break;
8522
8523 default:
8524 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8525 *args, insn->name, insn->args);
8526 /* Further processing is fruitless. */
8527 return;
8528 }
8529 break;
8530
8531 case '<': /* must be at least one digit */
8532 /*
8533 * According to the manual, if the shift amount is greater
8534 * than 31 or less than 0, then the shift amount should be
8535 * mod 32. In reality the mips assembler issues an error.
8536 * We issue a warning and mask out all but the low 5 bits.
8537 */
8538 my_getExpression (&imm_expr, s);
8539 check_absolute_expr (ip, &imm_expr);
8540 if ((unsigned long) imm_expr.X_add_number > 31)
8541 as_warn (_("Improper shift amount (%lu)"),
8542 (unsigned long) imm_expr.X_add_number);
8543 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8544 imm_expr.X_op = O_absent;
8545 s = expr_end;
8546 continue;
8547
8548 case '>': /* shift amount minus 32 */
8549 my_getExpression (&imm_expr, s);
8550 check_absolute_expr (ip, &imm_expr);
8551 if ((unsigned long) imm_expr.X_add_number < 32
8552 || (unsigned long) imm_expr.X_add_number > 63)
8553 break;
8554 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8555 imm_expr.X_op = O_absent;
8556 s = expr_end;
8557 continue;
8558
8559 case 'k': /* cache code */
8560 case 'h': /* prefx code */
8561 my_getExpression (&imm_expr, s);
8562 check_absolute_expr (ip, &imm_expr);
8563 if ((unsigned long) imm_expr.X_add_number > 31)
8564 as_warn (_("Invalid value for `%s' (%lu)"),
8565 ip->insn_mo->name,
8566 (unsigned long) imm_expr.X_add_number);
8567 if (*args == 'k')
8568 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8569 else
8570 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8571 imm_expr.X_op = O_absent;
8572 s = expr_end;
8573 continue;
8574
8575 case 'c': /* break code */
8576 my_getExpression (&imm_expr, s);
8577 check_absolute_expr (ip, &imm_expr);
8578 if ((unsigned long) imm_expr.X_add_number > 1023)
8579 as_warn (_("Illegal break code (%lu)"),
8580 (unsigned long) imm_expr.X_add_number);
8581 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8582 imm_expr.X_op = O_absent;
8583 s = expr_end;
8584 continue;
8585
8586 case 'q': /* lower break code */
8587 my_getExpression (&imm_expr, s);
8588 check_absolute_expr (ip, &imm_expr);
8589 if ((unsigned long) imm_expr.X_add_number > 1023)
8590 as_warn (_("Illegal lower break code (%lu)"),
8591 (unsigned long) imm_expr.X_add_number);
8592 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8593 imm_expr.X_op = O_absent;
8594 s = expr_end;
8595 continue;
8596
8597 case 'B': /* 20-bit syscall/break code. */
8598 my_getExpression (&imm_expr, s);
8599 check_absolute_expr (ip, &imm_expr);
8600 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8601 as_warn (_("Illegal 20-bit code (%lu)"),
8602 (unsigned long) imm_expr.X_add_number);
8603 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8604 imm_expr.X_op = O_absent;
8605 s = expr_end;
8606 continue;
8607
8608 case 'C': /* Coprocessor code */
8609 my_getExpression (&imm_expr, s);
8610 check_absolute_expr (ip, &imm_expr);
8611 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8612 {
8613 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8614 (unsigned long) imm_expr.X_add_number);
8615 imm_expr.X_add_number &= ((1 << 25) - 1);
8616 }
8617 ip->insn_opcode |= imm_expr.X_add_number;
8618 imm_expr.X_op = O_absent;
8619 s = expr_end;
8620 continue;
8621
8622 case 'J': /* 19-bit wait code. */
8623 my_getExpression (&imm_expr, s);
8624 check_absolute_expr (ip, &imm_expr);
8625 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8626 as_warn (_("Illegal 19-bit code (%lu)"),
8627 (unsigned long) imm_expr.X_add_number);
8628 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8629 imm_expr.X_op = O_absent;
8630 s = expr_end;
8631 continue;
8632
8633 case 'P': /* Performance register */
8634 my_getExpression (&imm_expr, s);
8635 check_absolute_expr (ip, &imm_expr);
8636 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8637 as_warn (_("Invalid performance register (%lu)"),
8638 (unsigned long) imm_expr.X_add_number);
8639 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8640 imm_expr.X_op = O_absent;
8641 s = expr_end;
8642 continue;
8643
8644 case 'b': /* base register */
8645 case 'd': /* destination register */
8646 case 's': /* source register */
8647 case 't': /* target register */
8648 case 'r': /* both target and source */
8649 case 'v': /* both dest and source */
8650 case 'w': /* both dest and target */
8651 case 'E': /* coprocessor target register */
8652 case 'G': /* coprocessor destination register */
8653 case 'K': /* 'rdhwr' destination register */
8654 case 'x': /* ignore register name */
8655 case 'z': /* must be zero register */
8656 case 'U': /* destination register (clo/clz). */
8657 case 'g': /* coprocessor destination register */
8658 s_reset = s;
8659 if (s[0] == '$')
8660 {
8661 if (ISDIGIT (s[1]))
8662 {
8663 ++s;
8664 regno = 0;
8665 do
8666 {
8667 regno *= 10;
8668 regno += *s - '0';
8669 ++s;
8670 }
8671 while (ISDIGIT (*s));
8672 if (regno > 31)
8673 as_bad (_("Invalid register number (%d)"), regno);
8674 }
8675 else if (*args == 'E' || *args == 'G' || *args == 'K')
8676 goto notreg;
8677 else
8678 {
8679 if (s[1] == 'r' && s[2] == 'a')
8680 {
8681 s += 3;
8682 regno = RA;
8683 }
8684 else if (s[1] == 'f' && s[2] == 'p')
8685 {
8686 s += 3;
8687 regno = FP;
8688 }
8689 else if (s[1] == 's' && s[2] == 'p')
8690 {
8691 s += 3;
8692 regno = SP;
8693 }
8694 else if (s[1] == 'g' && s[2] == 'p')
8695 {
8696 s += 3;
8697 regno = GP;
8698 }
8699 else if (s[1] == 'a' && s[2] == 't')
8700 {
8701 s += 3;
8702 regno = AT;
8703 }
8704 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8705 {
8706 s += 4;
8707 regno = KT0;
8708 }
8709 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8710 {
8711 s += 4;
8712 regno = KT1;
8713 }
8714 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8715 {
8716 s += 5;
8717 regno = ZERO;
8718 }
8719 else if (itbl_have_entries)
8720 {
8721 char *p, *n;
8722 unsigned long r;
8723
8724 p = s + 1; /* advance past '$' */
8725 n = itbl_get_field (&p); /* n is name */
8726
8727 /* See if this is a register defined in an
8728 itbl entry. */
8729 if (itbl_get_reg_val (n, &r))
8730 {
8731 /* Get_field advances to the start of
8732 the next field, so we need to back
8733 rack to the end of the last field. */
8734 if (p)
8735 s = p - 1;
8736 else
8737 s = strchr (s, '\0');
8738 regno = r;
8739 }
8740 else
8741 goto notreg;
8742 }
8743 else
8744 goto notreg;
8745 }
8746 if (regno == AT
8747 && ! mips_opts.noat
8748 && *args != 'E'
8749 && *args != 'G'
8750 && *args != 'K')
8751 as_warn (_("Used $at without \".set noat\""));
8752 c = *args;
8753 if (*s == ' ')
8754 ++s;
8755 if (args[1] != *s)
8756 {
8757 if (c == 'r' || c == 'v' || c == 'w')
8758 {
8759 regno = lastregno;
8760 s = s_reset;
8761 ++args;
8762 }
8763 }
8764 /* 'z' only matches $0. */
8765 if (c == 'z' && regno != 0)
8766 break;
8767
8768 /* Now that we have assembled one operand, we use the args string
8769 * to figure out where it goes in the instruction. */
8770 switch (c)
8771 {
8772 case 'r':
8773 case 's':
8774 case 'v':
8775 case 'b':
8776 INSERT_OPERAND (RS, *ip, regno);
8777 break;
8778 case 'd':
8779 case 'G':
8780 case 'K':
8781 case 'g':
8782 INSERT_OPERAND (RD, *ip, regno);
8783 break;
8784 case 'U':
8785 INSERT_OPERAND (RD, *ip, regno);
8786 INSERT_OPERAND (RT, *ip, regno);
8787 break;
8788 case 'w':
8789 case 't':
8790 case 'E':
8791 INSERT_OPERAND (RT, *ip, regno);
8792 break;
8793 case 'x':
8794 /* This case exists because on the r3000 trunc
8795 expands into a macro which requires a gp
8796 register. On the r6000 or r4000 it is
8797 assembled into a single instruction which
8798 ignores the register. Thus the insn version
8799 is MIPS_ISA2 and uses 'x', and the macro
8800 version is MIPS_ISA1 and uses 't'. */
8801 break;
8802 case 'z':
8803 /* This case is for the div instruction, which
8804 acts differently if the destination argument
8805 is $0. This only matches $0, and is checked
8806 outside the switch. */
8807 break;
8808 case 'D':
8809 /* Itbl operand; not yet implemented. FIXME ?? */
8810 break;
8811 /* What about all other operands like 'i', which
8812 can be specified in the opcode table? */
8813 }
8814 lastregno = regno;
8815 continue;
8816 }
8817 notreg:
8818 switch (*args++)
8819 {
8820 case 'r':
8821 case 'v':
8822 INSERT_OPERAND (RS, *ip, lastregno);
8823 continue;
8824 case 'w':
8825 INSERT_OPERAND (RT, *ip, lastregno);
8826 continue;
8827 }
8828 break;
8829
8830 case 'O': /* MDMX alignment immediate constant. */
8831 my_getExpression (&imm_expr, s);
8832 check_absolute_expr (ip, &imm_expr);
8833 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8834 as_warn ("Improper align amount (%ld), using low bits",
8835 (long) imm_expr.X_add_number);
8836 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
8837 imm_expr.X_op = O_absent;
8838 s = expr_end;
8839 continue;
8840
8841 case 'Q': /* MDMX vector, element sel, or const. */
8842 if (s[0] != '$')
8843 {
8844 /* MDMX Immediate. */
8845 my_getExpression (&imm_expr, s);
8846 check_absolute_expr (ip, &imm_expr);
8847 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8848 as_warn (_("Invalid MDMX Immediate (%ld)"),
8849 (long) imm_expr.X_add_number);
8850 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
8851 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8852 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8853 else
8854 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8855 imm_expr.X_op = O_absent;
8856 s = expr_end;
8857 continue;
8858 }
8859 /* Not MDMX Immediate. Fall through. */
8860 case 'X': /* MDMX destination register. */
8861 case 'Y': /* MDMX source register. */
8862 case 'Z': /* MDMX target register. */
8863 is_mdmx = 1;
8864 case 'D': /* floating point destination register */
8865 case 'S': /* floating point source register */
8866 case 'T': /* floating point target register */
8867 case 'R': /* floating point source register */
8868 case 'V':
8869 case 'W':
8870 s_reset = s;
8871 /* Accept $fN for FP and MDMX register numbers, and in
8872 addition accept $vN for MDMX register numbers. */
8873 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8874 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8875 && ISDIGIT (s[2])))
8876 {
8877 s += 2;
8878 regno = 0;
8879 do
8880 {
8881 regno *= 10;
8882 regno += *s - '0';
8883 ++s;
8884 }
8885 while (ISDIGIT (*s));
8886
8887 if (regno > 31)
8888 as_bad (_("Invalid float register number (%d)"), regno);
8889
8890 if ((regno & 1) != 0
8891 && HAVE_32BIT_FPRS
8892 && ! (strcmp (str, "mtc1") == 0
8893 || strcmp (str, "mfc1") == 0
8894 || strcmp (str, "lwc1") == 0
8895 || strcmp (str, "swc1") == 0
8896 || strcmp (str, "l.s") == 0
8897 || strcmp (str, "s.s") == 0
8898 || strcmp (str, "mftc1") == 0
8899 || strcmp (str, "mfthc1") == 0
8900 || strcmp (str, "cftc1") == 0
8901 || strcmp (str, "mttc1") == 0
8902 || strcmp (str, "mtthc1") == 0
8903 || strcmp (str, "cttc1") == 0))
8904 as_warn (_("Float register should be even, was %d"),
8905 regno);
8906
8907 c = *args;
8908 if (*s == ' ')
8909 ++s;
8910 if (args[1] != *s)
8911 {
8912 if (c == 'V' || c == 'W')
8913 {
8914 regno = lastregno;
8915 s = s_reset;
8916 ++args;
8917 }
8918 }
8919 switch (c)
8920 {
8921 case 'D':
8922 case 'X':
8923 INSERT_OPERAND (FD, *ip, regno);
8924 break;
8925 case 'V':
8926 case 'S':
8927 case 'Y':
8928 INSERT_OPERAND (FS, *ip, regno);
8929 break;
8930 case 'Q':
8931 /* This is like 'Z', but also needs to fix the MDMX
8932 vector/scalar select bits. Note that the
8933 scalar immediate case is handled above. */
8934 if (*s == '[')
8935 {
8936 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8937 int max_el = (is_qh ? 3 : 7);
8938 s++;
8939 my_getExpression(&imm_expr, s);
8940 check_absolute_expr (ip, &imm_expr);
8941 s = expr_end;
8942 if (imm_expr.X_add_number > max_el)
8943 as_bad(_("Bad element selector %ld"),
8944 (long) imm_expr.X_add_number);
8945 imm_expr.X_add_number &= max_el;
8946 ip->insn_opcode |= (imm_expr.X_add_number
8947 << (OP_SH_VSEL +
8948 (is_qh ? 2 : 1)));
8949 imm_expr.X_op = O_absent;
8950 if (*s != ']')
8951 as_warn(_("Expecting ']' found '%s'"), s);
8952 else
8953 s++;
8954 }
8955 else
8956 {
8957 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8958 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8959 << OP_SH_VSEL);
8960 else
8961 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8962 OP_SH_VSEL);
8963 }
8964 /* Fall through */
8965 case 'W':
8966 case 'T':
8967 case 'Z':
8968 INSERT_OPERAND (FT, *ip, regno);
8969 break;
8970 case 'R':
8971 INSERT_OPERAND (FR, *ip, regno);
8972 break;
8973 }
8974 lastregno = regno;
8975 continue;
8976 }
8977
8978 switch (*args++)
8979 {
8980 case 'V':
8981 INSERT_OPERAND (FS, *ip, lastregno);
8982 continue;
8983 case 'W':
8984 INSERT_OPERAND (FT, *ip, lastregno);
8985 continue;
8986 }
8987 break;
8988
8989 case 'I':
8990 my_getExpression (&imm_expr, s);
8991 if (imm_expr.X_op != O_big
8992 && imm_expr.X_op != O_constant)
8993 insn_error = _("absolute expression required");
8994 if (HAVE_32BIT_GPRS)
8995 normalize_constant_expr (&imm_expr);
8996 s = expr_end;
8997 continue;
8998
8999 case 'A':
9000 my_getExpression (&offset_expr, s);
9001 normalize_address_expr (&offset_expr);
9002 *imm_reloc = BFD_RELOC_32;
9003 s = expr_end;
9004 continue;
9005
9006 case 'F':
9007 case 'L':
9008 case 'f':
9009 case 'l':
9010 {
9011 int f64;
9012 int using_gprs;
9013 char *save_in;
9014 char *err;
9015 unsigned char temp[8];
9016 int len;
9017 unsigned int length;
9018 segT seg;
9019 subsegT subseg;
9020 char *p;
9021
9022 /* These only appear as the last operand in an
9023 instruction, and every instruction that accepts
9024 them in any variant accepts them in all variants.
9025 This means we don't have to worry about backing out
9026 any changes if the instruction does not match.
9027
9028 The difference between them is the size of the
9029 floating point constant and where it goes. For 'F'
9030 and 'L' the constant is 64 bits; for 'f' and 'l' it
9031 is 32 bits. Where the constant is placed is based
9032 on how the MIPS assembler does things:
9033 F -- .rdata
9034 L -- .lit8
9035 f -- immediate value
9036 l -- .lit4
9037
9038 The .lit4 and .lit8 sections are only used if
9039 permitted by the -G argument.
9040
9041 The code below needs to know whether the target register
9042 is 32 or 64 bits wide. It relies on the fact 'f' and
9043 'F' are used with GPR-based instructions and 'l' and
9044 'L' are used with FPR-based instructions. */
9045
9046 f64 = *args == 'F' || *args == 'L';
9047 using_gprs = *args == 'F' || *args == 'f';
9048
9049 save_in = input_line_pointer;
9050 input_line_pointer = s;
9051 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9052 length = len;
9053 s = input_line_pointer;
9054 input_line_pointer = save_in;
9055 if (err != NULL && *err != '\0')
9056 {
9057 as_bad (_("Bad floating point constant: %s"), err);
9058 memset (temp, '\0', sizeof temp);
9059 length = f64 ? 8 : 4;
9060 }
9061
9062 assert (length == (unsigned) (f64 ? 8 : 4));
9063
9064 if (*args == 'f'
9065 || (*args == 'l'
9066 && (g_switch_value < 4
9067 || (temp[0] == 0 && temp[1] == 0)
9068 || (temp[2] == 0 && temp[3] == 0))))
9069 {
9070 imm_expr.X_op = O_constant;
9071 if (! target_big_endian)
9072 imm_expr.X_add_number = bfd_getl32 (temp);
9073 else
9074 imm_expr.X_add_number = bfd_getb32 (temp);
9075 }
9076 else if (length > 4
9077 && ! mips_disable_float_construction
9078 /* Constants can only be constructed in GPRs and
9079 copied to FPRs if the GPRs are at least as wide
9080 as the FPRs. Force the constant into memory if
9081 we are using 64-bit FPRs but the GPRs are only
9082 32 bits wide. */
9083 && (using_gprs
9084 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9085 && ((temp[0] == 0 && temp[1] == 0)
9086 || (temp[2] == 0 && temp[3] == 0))
9087 && ((temp[4] == 0 && temp[5] == 0)
9088 || (temp[6] == 0 && temp[7] == 0)))
9089 {
9090 /* The value is simple enough to load with a couple of
9091 instructions. If using 32-bit registers, set
9092 imm_expr to the high order 32 bits and offset_expr to
9093 the low order 32 bits. Otherwise, set imm_expr to
9094 the entire 64 bit constant. */
9095 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9096 {
9097 imm_expr.X_op = O_constant;
9098 offset_expr.X_op = O_constant;
9099 if (! target_big_endian)
9100 {
9101 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9102 offset_expr.X_add_number = bfd_getl32 (temp);
9103 }
9104 else
9105 {
9106 imm_expr.X_add_number = bfd_getb32 (temp);
9107 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9108 }
9109 if (offset_expr.X_add_number == 0)
9110 offset_expr.X_op = O_absent;
9111 }
9112 else if (sizeof (imm_expr.X_add_number) > 4)
9113 {
9114 imm_expr.X_op = O_constant;
9115 if (! target_big_endian)
9116 imm_expr.X_add_number = bfd_getl64 (temp);
9117 else
9118 imm_expr.X_add_number = bfd_getb64 (temp);
9119 }
9120 else
9121 {
9122 imm_expr.X_op = O_big;
9123 imm_expr.X_add_number = 4;
9124 if (! target_big_endian)
9125 {
9126 generic_bignum[0] = bfd_getl16 (temp);
9127 generic_bignum[1] = bfd_getl16 (temp + 2);
9128 generic_bignum[2] = bfd_getl16 (temp + 4);
9129 generic_bignum[3] = bfd_getl16 (temp + 6);
9130 }
9131 else
9132 {
9133 generic_bignum[0] = bfd_getb16 (temp + 6);
9134 generic_bignum[1] = bfd_getb16 (temp + 4);
9135 generic_bignum[2] = bfd_getb16 (temp + 2);
9136 generic_bignum[3] = bfd_getb16 (temp);
9137 }
9138 }
9139 }
9140 else
9141 {
9142 const char *newname;
9143 segT new_seg;
9144
9145 /* Switch to the right section. */
9146 seg = now_seg;
9147 subseg = now_subseg;
9148 switch (*args)
9149 {
9150 default: /* unused default case avoids warnings. */
9151 case 'L':
9152 newname = RDATA_SECTION_NAME;
9153 if (g_switch_value >= 8)
9154 newname = ".lit8";
9155 break;
9156 case 'F':
9157 newname = RDATA_SECTION_NAME;
9158 break;
9159 case 'l':
9160 assert (g_switch_value >= 4);
9161 newname = ".lit4";
9162 break;
9163 }
9164 new_seg = subseg_new (newname, (subsegT) 0);
9165 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9166 bfd_set_section_flags (stdoutput, new_seg,
9167 (SEC_ALLOC
9168 | SEC_LOAD
9169 | SEC_READONLY
9170 | SEC_DATA));
9171 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9172 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9173 && strcmp (TARGET_OS, "elf") != 0)
9174 record_alignment (new_seg, 4);
9175 else
9176 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9177 if (seg == now_seg)
9178 as_bad (_("Can't use floating point insn in this section"));
9179
9180 /* Set the argument to the current address in the
9181 section. */
9182 offset_expr.X_op = O_symbol;
9183 offset_expr.X_add_symbol =
9184 symbol_new ("L0\001", now_seg,
9185 (valueT) frag_now_fix (), frag_now);
9186 offset_expr.X_add_number = 0;
9187
9188 /* Put the floating point number into the section. */
9189 p = frag_more ((int) length);
9190 memcpy (p, temp, length);
9191
9192 /* Switch back to the original section. */
9193 subseg_set (seg, subseg);
9194 }
9195 }
9196 continue;
9197
9198 case 'i': /* 16 bit unsigned immediate */
9199 case 'j': /* 16 bit signed immediate */
9200 *imm_reloc = BFD_RELOC_LO16;
9201 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9202 {
9203 int more;
9204 offsetT minval, maxval;
9205
9206 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9207 && strcmp (insn->name, insn[1].name) == 0);
9208
9209 /* If the expression was written as an unsigned number,
9210 only treat it as signed if there are no more
9211 alternatives. */
9212 if (more
9213 && *args == 'j'
9214 && sizeof (imm_expr.X_add_number) <= 4
9215 && imm_expr.X_op == O_constant
9216 && imm_expr.X_add_number < 0
9217 && imm_expr.X_unsigned
9218 && HAVE_64BIT_GPRS)
9219 break;
9220
9221 /* For compatibility with older assemblers, we accept
9222 0x8000-0xffff as signed 16-bit numbers when only
9223 signed numbers are allowed. */
9224 if (*args == 'i')
9225 minval = 0, maxval = 0xffff;
9226 else if (more)
9227 minval = -0x8000, maxval = 0x7fff;
9228 else
9229 minval = -0x8000, maxval = 0xffff;
9230
9231 if (imm_expr.X_op != O_constant
9232 || imm_expr.X_add_number < minval
9233 || imm_expr.X_add_number > maxval)
9234 {
9235 if (more)
9236 break;
9237 if (imm_expr.X_op == O_constant
9238 || imm_expr.X_op == O_big)
9239 as_bad (_("expression out of range"));
9240 }
9241 }
9242 s = expr_end;
9243 continue;
9244
9245 case 'o': /* 16 bit offset */
9246 /* Check whether there is only a single bracketed expression
9247 left. If so, it must be the base register and the
9248 constant must be zero. */
9249 if (*s == '(' && strchr (s + 1, '(') == 0)
9250 {
9251 offset_expr.X_op = O_constant;
9252 offset_expr.X_add_number = 0;
9253 continue;
9254 }
9255
9256 /* If this value won't fit into a 16 bit offset, then go
9257 find a macro that will generate the 32 bit offset
9258 code pattern. */
9259 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9260 && (offset_expr.X_op != O_constant
9261 || offset_expr.X_add_number >= 0x8000
9262 || offset_expr.X_add_number < -0x8000))
9263 break;
9264
9265 s = expr_end;
9266 continue;
9267
9268 case 'p': /* pc relative offset */
9269 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9270 my_getExpression (&offset_expr, s);
9271 s = expr_end;
9272 continue;
9273
9274 case 'u': /* upper 16 bits */
9275 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9276 && imm_expr.X_op == O_constant
9277 && (imm_expr.X_add_number < 0
9278 || imm_expr.X_add_number >= 0x10000))
9279 as_bad (_("lui expression not in range 0..65535"));
9280 s = expr_end;
9281 continue;
9282
9283 case 'a': /* 26 bit address */
9284 my_getExpression (&offset_expr, s);
9285 s = expr_end;
9286 *offset_reloc = BFD_RELOC_MIPS_JMP;
9287 continue;
9288
9289 case 'N': /* 3 bit branch condition code */
9290 case 'M': /* 3 bit compare condition code */
9291 if (strncmp (s, "$fcc", 4) != 0)
9292 break;
9293 s += 4;
9294 regno = 0;
9295 do
9296 {
9297 regno *= 10;
9298 regno += *s - '0';
9299 ++s;
9300 }
9301 while (ISDIGIT (*s));
9302 if (regno > 7)
9303 as_bad (_("Invalid condition code register $fcc%d"), regno);
9304 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9305 || strcmp(str + strlen(str) - 5, "any2f") == 0
9306 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9307 && (regno & 1) != 0)
9308 as_warn(_("Condition code register should be even for %s, was %d"),
9309 str, regno);
9310 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9311 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9312 && (regno & 3) != 0)
9313 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9314 str, regno);
9315 if (*args == 'N')
9316 INSERT_OPERAND (BCC, *ip, regno);
9317 else
9318 INSERT_OPERAND (CCC, *ip, regno);
9319 continue;
9320
9321 case 'H':
9322 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9323 s += 2;
9324 if (ISDIGIT (*s))
9325 {
9326 c = 0;
9327 do
9328 {
9329 c *= 10;
9330 c += *s - '0';
9331 ++s;
9332 }
9333 while (ISDIGIT (*s));
9334 }
9335 else
9336 c = 8; /* Invalid sel value. */
9337
9338 if (c > 7)
9339 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9340 ip->insn_opcode |= c;
9341 continue;
9342
9343 case 'e':
9344 /* Must be at least one digit. */
9345 my_getExpression (&imm_expr, s);
9346 check_absolute_expr (ip, &imm_expr);
9347
9348 if ((unsigned long) imm_expr.X_add_number
9349 > (unsigned long) OP_MASK_VECBYTE)
9350 {
9351 as_bad (_("bad byte vector index (%ld)"),
9352 (long) imm_expr.X_add_number);
9353 imm_expr.X_add_number = 0;
9354 }
9355
9356 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
9357 imm_expr.X_op = O_absent;
9358 s = expr_end;
9359 continue;
9360
9361 case '%':
9362 my_getExpression (&imm_expr, s);
9363 check_absolute_expr (ip, &imm_expr);
9364
9365 if ((unsigned long) imm_expr.X_add_number
9366 > (unsigned long) OP_MASK_VECALIGN)
9367 {
9368 as_bad (_("bad byte vector index (%ld)"),
9369 (long) imm_expr.X_add_number);
9370 imm_expr.X_add_number = 0;
9371 }
9372
9373 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
9374 imm_expr.X_op = O_absent;
9375 s = expr_end;
9376 continue;
9377
9378 default:
9379 as_bad (_("bad char = '%c'\n"), *args);
9380 internalError ();
9381 }
9382 break;
9383 }
9384 /* Args don't match. */
9385 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9386 !strcmp (insn->name, insn[1].name))
9387 {
9388 ++insn;
9389 s = argsStart;
9390 insn_error = _("illegal operands");
9391 continue;
9392 }
9393 if (save_c)
9394 *(--s) = save_c;
9395 insn_error = _("illegal operands");
9396 return;
9397 }
9398 }
9399
9400 #define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9401
9402 /* This routine assembles an instruction into its binary format when
9403 assembling for the mips16. As a side effect, it sets one of the
9404 global variables imm_reloc or offset_reloc to the type of
9405 relocation to do if one of the operands is an address expression.
9406 It also sets mips16_small and mips16_ext if the user explicitly
9407 requested a small or extended instruction. */
9408
9409 static void
9410 mips16_ip (char *str, struct mips_cl_insn *ip)
9411 {
9412 char *s;
9413 const char *args;
9414 struct mips_opcode *insn;
9415 char *argsstart;
9416 unsigned int regno;
9417 unsigned int lastregno = 0;
9418 char *s_reset;
9419 size_t i;
9420
9421 insn_error = NULL;
9422
9423 mips16_small = FALSE;
9424 mips16_ext = FALSE;
9425
9426 for (s = str; ISLOWER (*s); ++s)
9427 ;
9428 switch (*s)
9429 {
9430 case '\0':
9431 break;
9432
9433 case ' ':
9434 *s++ = '\0';
9435 break;
9436
9437 case '.':
9438 if (s[1] == 't' && s[2] == ' ')
9439 {
9440 *s = '\0';
9441 mips16_small = TRUE;
9442 s += 3;
9443 break;
9444 }
9445 else if (s[1] == 'e' && s[2] == ' ')
9446 {
9447 *s = '\0';
9448 mips16_ext = TRUE;
9449 s += 3;
9450 break;
9451 }
9452 /* Fall through. */
9453 default:
9454 insn_error = _("unknown opcode");
9455 return;
9456 }
9457
9458 if (mips_opts.noautoextend && ! mips16_ext)
9459 mips16_small = TRUE;
9460
9461 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9462 {
9463 insn_error = _("unrecognized opcode");
9464 return;
9465 }
9466
9467 argsstart = s;
9468 for (;;)
9469 {
9470 assert (strcmp (insn->name, str) == 0);
9471
9472 create_insn (ip, insn);
9473 imm_expr.X_op = O_absent;
9474 imm_reloc[0] = BFD_RELOC_UNUSED;
9475 imm_reloc[1] = BFD_RELOC_UNUSED;
9476 imm_reloc[2] = BFD_RELOC_UNUSED;
9477 imm2_expr.X_op = O_absent;
9478 offset_expr.X_op = O_absent;
9479 offset_reloc[0] = BFD_RELOC_UNUSED;
9480 offset_reloc[1] = BFD_RELOC_UNUSED;
9481 offset_reloc[2] = BFD_RELOC_UNUSED;
9482 for (args = insn->args; 1; ++args)
9483 {
9484 int c;
9485
9486 if (*s == ' ')
9487 ++s;
9488
9489 /* In this switch statement we call break if we did not find
9490 a match, continue if we did find a match, or return if we
9491 are done. */
9492
9493 c = *args;
9494 switch (c)
9495 {
9496 case '\0':
9497 if (*s == '\0')
9498 {
9499 /* Stuff the immediate value in now, if we can. */
9500 if (imm_expr.X_op == O_constant
9501 && *imm_reloc > BFD_RELOC_UNUSED
9502 && insn->pinfo != INSN_MACRO)
9503 {
9504 valueT tmp;
9505
9506 switch (*offset_reloc)
9507 {
9508 case BFD_RELOC_MIPS16_HI16_S:
9509 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9510 break;
9511
9512 case BFD_RELOC_MIPS16_HI16:
9513 tmp = imm_expr.X_add_number >> 16;
9514 break;
9515
9516 case BFD_RELOC_MIPS16_LO16:
9517 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9518 - 0x8000;
9519 break;
9520
9521 case BFD_RELOC_UNUSED:
9522 tmp = imm_expr.X_add_number;
9523 break;
9524
9525 default:
9526 internalError ();
9527 }
9528 *offset_reloc = BFD_RELOC_UNUSED;
9529
9530 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9531 tmp, TRUE, mips16_small,
9532 mips16_ext, &ip->insn_opcode,
9533 &ip->use_extend, &ip->extend);
9534 imm_expr.X_op = O_absent;
9535 *imm_reloc = BFD_RELOC_UNUSED;
9536 }
9537
9538 return;
9539 }
9540 break;
9541
9542 case ',':
9543 if (*s++ == c)
9544 continue;
9545 s--;
9546 switch (*++args)
9547 {
9548 case 'v':
9549 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9550 continue;
9551 case 'w':
9552 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9553 continue;
9554 }
9555 break;
9556
9557 case '(':
9558 case ')':
9559 if (*s++ == c)
9560 continue;
9561 break;
9562
9563 case 'v':
9564 case 'w':
9565 if (s[0] != '$')
9566 {
9567 if (c == 'v')
9568 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9569 else
9570 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9571 ++args;
9572 continue;
9573 }
9574 /* Fall through. */
9575 case 'x':
9576 case 'y':
9577 case 'z':
9578 case 'Z':
9579 case '0':
9580 case 'S':
9581 case 'R':
9582 case 'X':
9583 case 'Y':
9584 if (s[0] != '$')
9585 break;
9586 s_reset = s;
9587 if (ISDIGIT (s[1]))
9588 {
9589 ++s;
9590 regno = 0;
9591 do
9592 {
9593 regno *= 10;
9594 regno += *s - '0';
9595 ++s;
9596 }
9597 while (ISDIGIT (*s));
9598 if (regno > 31)
9599 {
9600 as_bad (_("invalid register number (%d)"), regno);
9601 regno = 2;
9602 }
9603 }
9604 else
9605 {
9606 if (s[1] == 'r' && s[2] == 'a')
9607 {
9608 s += 3;
9609 regno = RA;
9610 }
9611 else if (s[1] == 'f' && s[2] == 'p')
9612 {
9613 s += 3;
9614 regno = FP;
9615 }
9616 else if (s[1] == 's' && s[2] == 'p')
9617 {
9618 s += 3;
9619 regno = SP;
9620 }
9621 else if (s[1] == 'g' && s[2] == 'p')
9622 {
9623 s += 3;
9624 regno = GP;
9625 }
9626 else if (s[1] == 'a' && s[2] == 't')
9627 {
9628 s += 3;
9629 regno = AT;
9630 }
9631 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9632 {
9633 s += 4;
9634 regno = KT0;
9635 }
9636 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9637 {
9638 s += 4;
9639 regno = KT1;
9640 }
9641 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9642 {
9643 s += 5;
9644 regno = ZERO;
9645 }
9646 else
9647 break;
9648 }
9649
9650 if (*s == ' ')
9651 ++s;
9652 if (args[1] != *s)
9653 {
9654 if (c == 'v' || c == 'w')
9655 {
9656 regno = mips16_to_32_reg_map[lastregno];
9657 s = s_reset;
9658 ++args;
9659 }
9660 }
9661
9662 switch (c)
9663 {
9664 case 'x':
9665 case 'y':
9666 case 'z':
9667 case 'v':
9668 case 'w':
9669 case 'Z':
9670 regno = mips32_to_16_reg_map[regno];
9671 break;
9672
9673 case '0':
9674 if (regno != 0)
9675 regno = ILLEGAL_REG;
9676 break;
9677
9678 case 'S':
9679 if (regno != SP)
9680 regno = ILLEGAL_REG;
9681 break;
9682
9683 case 'R':
9684 if (regno != RA)
9685 regno = ILLEGAL_REG;
9686 break;
9687
9688 case 'X':
9689 case 'Y':
9690 if (regno == AT && ! mips_opts.noat)
9691 as_warn (_("used $at without \".set noat\""));
9692 break;
9693
9694 default:
9695 internalError ();
9696 }
9697
9698 if (regno == ILLEGAL_REG)
9699 break;
9700
9701 switch (c)
9702 {
9703 case 'x':
9704 case 'v':
9705 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9706 break;
9707 case 'y':
9708 case 'w':
9709 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9710 break;
9711 case 'z':
9712 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9713 break;
9714 case 'Z':
9715 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9716 case '0':
9717 case 'S':
9718 case 'R':
9719 break;
9720 case 'X':
9721 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9722 break;
9723 case 'Y':
9724 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9725 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9726 break;
9727 default:
9728 internalError ();
9729 }
9730
9731 lastregno = regno;
9732 continue;
9733
9734 case 'P':
9735 if (strncmp (s, "$pc", 3) == 0)
9736 {
9737 s += 3;
9738 continue;
9739 }
9740 break;
9741
9742 case '5':
9743 case 'H':
9744 case 'W':
9745 case 'D':
9746 case 'j':
9747 case 'V':
9748 case 'C':
9749 case 'U':
9750 case 'k':
9751 case 'K':
9752 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9753 if (i > 0)
9754 {
9755 if (imm_expr.X_op != O_constant)
9756 {
9757 mips16_ext = TRUE;
9758 ip->use_extend = TRUE;
9759 ip->extend = 0;
9760 }
9761 else
9762 {
9763 /* We need to relax this instruction. */
9764 *offset_reloc = *imm_reloc;
9765 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9766 }
9767 s = expr_end;
9768 continue;
9769 }
9770 *imm_reloc = BFD_RELOC_UNUSED;
9771 /* Fall through. */
9772 case '<':
9773 case '>':
9774 case '[':
9775 case ']':
9776 case '4':
9777 case '8':
9778 my_getExpression (&imm_expr, s);
9779 if (imm_expr.X_op == O_register)
9780 {
9781 /* What we thought was an expression turned out to
9782 be a register. */
9783
9784 if (s[0] == '(' && args[1] == '(')
9785 {
9786 /* It looks like the expression was omitted
9787 before a register indirection, which means
9788 that the expression is implicitly zero. We
9789 still set up imm_expr, so that we handle
9790 explicit extensions correctly. */
9791 imm_expr.X_op = O_constant;
9792 imm_expr.X_add_number = 0;
9793 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9794 continue;
9795 }
9796
9797 break;
9798 }
9799
9800 /* We need to relax this instruction. */
9801 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9802 s = expr_end;
9803 continue;
9804
9805 case 'p':
9806 case 'q':
9807 case 'A':
9808 case 'B':
9809 case 'E':
9810 /* We use offset_reloc rather than imm_reloc for the PC
9811 relative operands. This lets macros with both
9812 immediate and address operands work correctly. */
9813 my_getExpression (&offset_expr, s);
9814
9815 if (offset_expr.X_op == O_register)
9816 break;
9817
9818 /* We need to relax this instruction. */
9819 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9820 s = expr_end;
9821 continue;
9822
9823 case '6': /* break code */
9824 my_getExpression (&imm_expr, s);
9825 check_absolute_expr (ip, &imm_expr);
9826 if ((unsigned long) imm_expr.X_add_number > 63)
9827 as_warn (_("Invalid value for `%s' (%lu)"),
9828 ip->insn_mo->name,
9829 (unsigned long) imm_expr.X_add_number);
9830 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
9831 imm_expr.X_op = O_absent;
9832 s = expr_end;
9833 continue;
9834
9835 case 'a': /* 26 bit address */
9836 my_getExpression (&offset_expr, s);
9837 s = expr_end;
9838 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9839 ip->insn_opcode <<= 16;
9840 continue;
9841
9842 case 'l': /* register list for entry macro */
9843 case 'L': /* register list for exit macro */
9844 {
9845 int mask;
9846
9847 if (c == 'l')
9848 mask = 0;
9849 else
9850 mask = 7 << 3;
9851 while (*s != '\0')
9852 {
9853 int freg, reg1, reg2;
9854
9855 while (*s == ' ' || *s == ',')
9856 ++s;
9857 if (*s != '$')
9858 {
9859 as_bad (_("can't parse register list"));
9860 break;
9861 }
9862 ++s;
9863 if (*s != 'f')
9864 freg = 0;
9865 else
9866 {
9867 freg = 1;
9868 ++s;
9869 }
9870 reg1 = 0;
9871 while (ISDIGIT (*s))
9872 {
9873 reg1 *= 10;
9874 reg1 += *s - '0';
9875 ++s;
9876 }
9877 if (*s == ' ')
9878 ++s;
9879 if (*s != '-')
9880 reg2 = reg1;
9881 else
9882 {
9883 ++s;
9884 if (*s != '$')
9885 break;
9886 ++s;
9887 if (freg)
9888 {
9889 if (*s == 'f')
9890 ++s;
9891 else
9892 {
9893 as_bad (_("invalid register list"));
9894 break;
9895 }
9896 }
9897 reg2 = 0;
9898 while (ISDIGIT (*s))
9899 {
9900 reg2 *= 10;
9901 reg2 += *s - '0';
9902 ++s;
9903 }
9904 }
9905 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9906 {
9907 mask &= ~ (7 << 3);
9908 mask |= 5 << 3;
9909 }
9910 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9911 {
9912 mask &= ~ (7 << 3);
9913 mask |= 6 << 3;
9914 }
9915 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9916 mask |= (reg2 - 3) << 3;
9917 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9918 mask |= (reg2 - 15) << 1;
9919 else if (reg1 == RA && reg2 == RA)
9920 mask |= 1;
9921 else
9922 {
9923 as_bad (_("invalid register list"));
9924 break;
9925 }
9926 }
9927 /* The mask is filled in in the opcode table for the
9928 benefit of the disassembler. We remove it before
9929 applying the actual mask. */
9930 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9931 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9932 }
9933 continue;
9934
9935 case 'm': /* Register list for save insn. */
9936 case 'M': /* Register list for restore insn. */
9937 {
9938 int opcode = 0;
9939 int framesz = 0, seen_framesz = 0;
9940 int args = 0, statics = 0, sregs = 0;
9941
9942 while (*s != '\0')
9943 {
9944 unsigned int reg1, reg2;
9945
9946 SKIP_SPACE_TABS (s);
9947 while (*s == ',')
9948 ++s;
9949 SKIP_SPACE_TABS (s);
9950
9951 my_getExpression (&imm_expr, s);
9952 if (imm_expr.X_op == O_constant)
9953 {
9954 /* Handle the frame size. */
9955 if (seen_framesz)
9956 {
9957 as_bad (_("more than one frame size in list"));
9958 break;
9959 }
9960 seen_framesz = 1;
9961 framesz = imm_expr.X_add_number;
9962 imm_expr.X_op = O_absent;
9963 s = expr_end;
9964 continue;
9965 }
9966
9967 if (*s != '$')
9968 {
9969 as_bad (_("can't parse register list"));
9970 break;
9971 }
9972 ++s;
9973
9974 reg1 = 0;
9975 while (ISDIGIT (*s))
9976 {
9977 reg1 *= 10;
9978 reg1 += *s - '0';
9979 ++s;
9980 }
9981 SKIP_SPACE_TABS (s);
9982 if (*s != '-')
9983 reg2 = reg1;
9984 else
9985 {
9986 ++s;
9987 if (*s != '$')
9988 {
9989 as_bad (_("can't parse register list"));
9990 break;
9991 }
9992 ++s;
9993 reg2 = 0;
9994 while (ISDIGIT (*s))
9995 {
9996 reg2 *= 10;
9997 reg2 += *s - '0';
9998 ++s;
9999 }
10000 }
10001
10002 while (reg1 <= reg2)
10003 {
10004 if (reg1 >= 4 && reg1 <= 7)
10005 {
10006 if (c == 'm' && !seen_framesz)
10007 /* args $a0-$a3 */
10008 args |= 1 << (reg1 - 4);
10009 else
10010 /* statics $a0-$a3 */
10011 statics |= 1 << (reg1 - 4);
10012 }
10013 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10014 {
10015 /* $s0-$s8 */
10016 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10017 }
10018 else if (reg1 == 31)
10019 {
10020 /* Add $ra to insn. */
10021 opcode |= 0x40;
10022 }
10023 else
10024 {
10025 as_bad (_("unexpected register in list"));
10026 break;
10027 }
10028 if (++reg1 == 24)
10029 reg1 = 30;
10030 }
10031 }
10032
10033 /* Encode args/statics combination. */
10034 if (args & statics)
10035 as_bad (_("arg/static registers overlap"));
10036 else if (args == 0xf)
10037 /* All $a0-$a3 are args. */
10038 opcode |= MIPS16_ALL_ARGS << 16;
10039 else if (statics == 0xf)
10040 /* All $a0-$a3 are statics. */
10041 opcode |= MIPS16_ALL_STATICS << 16;
10042 else
10043 {
10044 int narg = 0, nstat = 0;
10045
10046 /* Count arg registers. */
10047 while (args & 0x1)
10048 {
10049 args >>= 1;
10050 narg++;
10051 }
10052 if (args != 0)
10053 as_bad (_("invalid arg register list"));
10054
10055 /* Count static registers. */
10056 while (statics & 0x8)
10057 {
10058 statics = (statics << 1) & 0xf;
10059 nstat++;
10060 }
10061 if (statics != 0)
10062 as_bad (_("invalid static register list"));
10063
10064 /* Encode args/statics. */
10065 opcode |= ((narg << 2) | nstat) << 16;
10066 }
10067
10068 /* Encode $s0/$s1. */
10069 if (sregs & (1 << 0)) /* $s0 */
10070 opcode |= 0x20;
10071 if (sregs & (1 << 1)) /* $s1 */
10072 opcode |= 0x10;
10073 sregs >>= 2;
10074
10075 if (sregs != 0)
10076 {
10077 /* Count regs $s2-$s8. */
10078 int nsreg = 0;
10079 while (sregs & 1)
10080 {
10081 sregs >>= 1;
10082 nsreg++;
10083 }
10084 if (sregs != 0)
10085 as_bad (_("invalid static register list"));
10086 /* Encode $s2-$s8. */
10087 opcode |= nsreg << 24;
10088 }
10089
10090 /* Encode frame size. */
10091 if (!seen_framesz)
10092 as_bad (_("missing frame size"));
10093 else if ((framesz & 7) != 0 || framesz < 0
10094 || framesz > 0xff * 8)
10095 as_bad (_("invalid frame size"));
10096 else if (framesz != 128 || (opcode >> 16) != 0)
10097 {
10098 framesz /= 8;
10099 opcode |= (((framesz & 0xf0) << 16)
10100 | (framesz & 0x0f));
10101 }
10102
10103 /* Finally build the instruction. */
10104 if ((opcode >> 16) != 0 || framesz == 0)
10105 {
10106 ip->use_extend = TRUE;
10107 ip->extend = opcode >> 16;
10108 }
10109 ip->insn_opcode |= opcode & 0x7f;
10110 }
10111 continue;
10112
10113 case 'e': /* extend code */
10114 my_getExpression (&imm_expr, s);
10115 check_absolute_expr (ip, &imm_expr);
10116 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10117 {
10118 as_warn (_("Invalid value for `%s' (%lu)"),
10119 ip->insn_mo->name,
10120 (unsigned long) imm_expr.X_add_number);
10121 imm_expr.X_add_number &= 0x7ff;
10122 }
10123 ip->insn_opcode |= imm_expr.X_add_number;
10124 imm_expr.X_op = O_absent;
10125 s = expr_end;
10126 continue;
10127
10128 default:
10129 internalError ();
10130 }
10131 break;
10132 }
10133
10134 /* Args don't match. */
10135 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10136 strcmp (insn->name, insn[1].name) == 0)
10137 {
10138 ++insn;
10139 s = argsstart;
10140 continue;
10141 }
10142
10143 insn_error = _("illegal operands");
10144
10145 return;
10146 }
10147 }
10148
10149 /* This structure holds information we know about a mips16 immediate
10150 argument type. */
10151
10152 struct mips16_immed_operand
10153 {
10154 /* The type code used in the argument string in the opcode table. */
10155 int type;
10156 /* The number of bits in the short form of the opcode. */
10157 int nbits;
10158 /* The number of bits in the extended form of the opcode. */
10159 int extbits;
10160 /* The amount by which the short form is shifted when it is used;
10161 for example, the sw instruction has a shift count of 2. */
10162 int shift;
10163 /* The amount by which the short form is shifted when it is stored
10164 into the instruction code. */
10165 int op_shift;
10166 /* Non-zero if the short form is unsigned. */
10167 int unsp;
10168 /* Non-zero if the extended form is unsigned. */
10169 int extu;
10170 /* Non-zero if the value is PC relative. */
10171 int pcrel;
10172 };
10173
10174 /* The mips16 immediate operand types. */
10175
10176 static const struct mips16_immed_operand mips16_immed_operands[] =
10177 {
10178 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10179 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10180 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10181 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10182 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10183 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10184 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10185 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10186 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10187 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10188 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10189 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10190 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10191 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10192 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10193 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10194 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10195 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10196 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10197 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10198 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10199 };
10200
10201 #define MIPS16_NUM_IMMED \
10202 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10203
10204 /* Handle a mips16 instruction with an immediate value. This or's the
10205 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10206 whether an extended value is needed; if one is needed, it sets
10207 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10208 If SMALL is true, an unextended opcode was explicitly requested.
10209 If EXT is true, an extended opcode was explicitly requested. If
10210 WARN is true, warn if EXT does not match reality. */
10211
10212 static void
10213 mips16_immed (char *file, unsigned int line, int type, offsetT val,
10214 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10215 unsigned long *insn, bfd_boolean *use_extend,
10216 unsigned short *extend)
10217 {
10218 register const struct mips16_immed_operand *op;
10219 int mintiny, maxtiny;
10220 bfd_boolean needext;
10221
10222 op = mips16_immed_operands;
10223 while (op->type != type)
10224 {
10225 ++op;
10226 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10227 }
10228
10229 if (op->unsp)
10230 {
10231 if (type == '<' || type == '>' || type == '[' || type == ']')
10232 {
10233 mintiny = 1;
10234 maxtiny = 1 << op->nbits;
10235 }
10236 else
10237 {
10238 mintiny = 0;
10239 maxtiny = (1 << op->nbits) - 1;
10240 }
10241 }
10242 else
10243 {
10244 mintiny = - (1 << (op->nbits - 1));
10245 maxtiny = (1 << (op->nbits - 1)) - 1;
10246 }
10247
10248 /* Branch offsets have an implicit 0 in the lowest bit. */
10249 if (type == 'p' || type == 'q')
10250 val /= 2;
10251
10252 if ((val & ((1 << op->shift) - 1)) != 0
10253 || val < (mintiny << op->shift)
10254 || val > (maxtiny << op->shift))
10255 needext = TRUE;
10256 else
10257 needext = FALSE;
10258
10259 if (warn && ext && ! needext)
10260 as_warn_where (file, line,
10261 _("extended operand requested but not required"));
10262 if (small && needext)
10263 as_bad_where (file, line, _("invalid unextended operand value"));
10264
10265 if (small || (! ext && ! needext))
10266 {
10267 int insnval;
10268
10269 *use_extend = FALSE;
10270 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10271 insnval <<= op->op_shift;
10272 *insn |= insnval;
10273 }
10274 else
10275 {
10276 long minext, maxext;
10277 int extval;
10278
10279 if (op->extu)
10280 {
10281 minext = 0;
10282 maxext = (1 << op->extbits) - 1;
10283 }
10284 else
10285 {
10286 minext = - (1 << (op->extbits - 1));
10287 maxext = (1 << (op->extbits - 1)) - 1;
10288 }
10289 if (val < minext || val > maxext)
10290 as_bad_where (file, line,
10291 _("operand value out of range for instruction"));
10292
10293 *use_extend = TRUE;
10294 if (op->extbits == 16)
10295 {
10296 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10297 val &= 0x1f;
10298 }
10299 else if (op->extbits == 15)
10300 {
10301 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10302 val &= 0xf;
10303 }
10304 else
10305 {
10306 extval = ((val & 0x1f) << 6) | (val & 0x20);
10307 val = 0;
10308 }
10309
10310 *extend = (unsigned short) extval;
10311 *insn |= val;
10312 }
10313 }
10314 \f
10315 struct percent_op_match
10316 {
10317 const char *str;
10318 bfd_reloc_code_real_type reloc;
10319 };
10320
10321 static const struct percent_op_match mips_percent_op[] =
10322 {
10323 {"%lo", BFD_RELOC_LO16},
10324 #ifdef OBJ_ELF
10325 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10326 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10327 {"%call16", BFD_RELOC_MIPS_CALL16},
10328 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10329 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10330 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10331 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10332 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10333 {"%got", BFD_RELOC_MIPS_GOT16},
10334 {"%gp_rel", BFD_RELOC_GPREL16},
10335 {"%half", BFD_RELOC_16},
10336 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10337 {"%higher", BFD_RELOC_MIPS_HIGHER},
10338 {"%neg", BFD_RELOC_MIPS_SUB},
10339 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10340 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10341 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10342 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10343 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10344 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10345 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
10346 #endif
10347 {"%hi", BFD_RELOC_HI16_S}
10348 };
10349
10350 static const struct percent_op_match mips16_percent_op[] =
10351 {
10352 {"%lo", BFD_RELOC_MIPS16_LO16},
10353 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10354 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10355 };
10356
10357
10358 /* Return true if *STR points to a relocation operator. When returning true,
10359 move *STR over the operator and store its relocation code in *RELOC.
10360 Leave both *STR and *RELOC alone when returning false. */
10361
10362 static bfd_boolean
10363 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
10364 {
10365 const struct percent_op_match *percent_op;
10366 size_t limit, i;
10367
10368 if (mips_opts.mips16)
10369 {
10370 percent_op = mips16_percent_op;
10371 limit = ARRAY_SIZE (mips16_percent_op);
10372 }
10373 else
10374 {
10375 percent_op = mips_percent_op;
10376 limit = ARRAY_SIZE (mips_percent_op);
10377 }
10378
10379 for (i = 0; i < limit; i++)
10380 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10381 {
10382 int len = strlen (percent_op[i].str);
10383
10384 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10385 continue;
10386
10387 *str += strlen (percent_op[i].str);
10388 *reloc = percent_op[i].reloc;
10389
10390 /* Check whether the output BFD supports this relocation.
10391 If not, issue an error and fall back on something safe. */
10392 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10393 {
10394 as_bad ("relocation %s isn't supported by the current ABI",
10395 percent_op[i].str);
10396 *reloc = BFD_RELOC_UNUSED;
10397 }
10398 return TRUE;
10399 }
10400 return FALSE;
10401 }
10402
10403
10404 /* Parse string STR as a 16-bit relocatable operand. Store the
10405 expression in *EP and the relocations in the array starting
10406 at RELOC. Return the number of relocation operators used.
10407
10408 On exit, EXPR_END points to the first character after the expression. */
10409
10410 static size_t
10411 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10412 char *str)
10413 {
10414 bfd_reloc_code_real_type reversed_reloc[3];
10415 size_t reloc_index, i;
10416 int crux_depth, str_depth;
10417 char *crux;
10418
10419 /* Search for the start of the main expression, recoding relocations
10420 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10421 of the main expression and with CRUX_DEPTH containing the number
10422 of open brackets at that point. */
10423 reloc_index = -1;
10424 str_depth = 0;
10425 do
10426 {
10427 reloc_index++;
10428 crux = str;
10429 crux_depth = str_depth;
10430
10431 /* Skip over whitespace and brackets, keeping count of the number
10432 of brackets. */
10433 while (*str == ' ' || *str == '\t' || *str == '(')
10434 if (*str++ == '(')
10435 str_depth++;
10436 }
10437 while (*str == '%'
10438 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10439 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10440
10441 my_getExpression (ep, crux);
10442 str = expr_end;
10443
10444 /* Match every open bracket. */
10445 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10446 if (*str++ == ')')
10447 crux_depth--;
10448
10449 if (crux_depth > 0)
10450 as_bad ("unclosed '('");
10451
10452 expr_end = str;
10453
10454 if (reloc_index != 0)
10455 {
10456 prev_reloc_op_frag = frag_now;
10457 for (i = 0; i < reloc_index; i++)
10458 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10459 }
10460
10461 return reloc_index;
10462 }
10463
10464 static void
10465 my_getExpression (expressionS *ep, char *str)
10466 {
10467 char *save_in;
10468 valueT val;
10469
10470 save_in = input_line_pointer;
10471 input_line_pointer = str;
10472 expression (ep);
10473 expr_end = input_line_pointer;
10474 input_line_pointer = save_in;
10475
10476 /* If we are in mips16 mode, and this is an expression based on `.',
10477 then we bump the value of the symbol by 1 since that is how other
10478 text symbols are handled. We don't bother to handle complex
10479 expressions, just `.' plus or minus a constant. */
10480 if (mips_opts.mips16
10481 && ep->X_op == O_symbol
10482 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10483 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10484 && symbol_get_frag (ep->X_add_symbol) == frag_now
10485 && symbol_constant_p (ep->X_add_symbol)
10486 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10487 S_SET_VALUE (ep->X_add_symbol, val + 1);
10488 }
10489
10490 /* Turn a string in input_line_pointer into a floating point constant
10491 of type TYPE, and store the appropriate bytes in *LITP. The number
10492 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10493 returned, or NULL on OK. */
10494
10495 char *
10496 md_atof (int type, char *litP, int *sizeP)
10497 {
10498 int prec;
10499 LITTLENUM_TYPE words[4];
10500 char *t;
10501 int i;
10502
10503 switch (type)
10504 {
10505 case 'f':
10506 prec = 2;
10507 break;
10508
10509 case 'd':
10510 prec = 4;
10511 break;
10512
10513 default:
10514 *sizeP = 0;
10515 return _("bad call to md_atof");
10516 }
10517
10518 t = atof_ieee (input_line_pointer, type, words);
10519 if (t)
10520 input_line_pointer = t;
10521
10522 *sizeP = prec * 2;
10523
10524 if (! target_big_endian)
10525 {
10526 for (i = prec - 1; i >= 0; i--)
10527 {
10528 md_number_to_chars (litP, words[i], 2);
10529 litP += 2;
10530 }
10531 }
10532 else
10533 {
10534 for (i = 0; i < prec; i++)
10535 {
10536 md_number_to_chars (litP, words[i], 2);
10537 litP += 2;
10538 }
10539 }
10540
10541 return NULL;
10542 }
10543
10544 void
10545 md_number_to_chars (char *buf, valueT val, int n)
10546 {
10547 if (target_big_endian)
10548 number_to_chars_bigendian (buf, val, n);
10549 else
10550 number_to_chars_littleendian (buf, val, n);
10551 }
10552 \f
10553 #ifdef OBJ_ELF
10554 static int support_64bit_objects(void)
10555 {
10556 const char **list, **l;
10557 int yes;
10558
10559 list = bfd_target_list ();
10560 for (l = list; *l != NULL; l++)
10561 #ifdef TE_TMIPS
10562 /* This is traditional mips */
10563 if (strcmp (*l, "elf64-tradbigmips") == 0
10564 || strcmp (*l, "elf64-tradlittlemips") == 0)
10565 #else
10566 if (strcmp (*l, "elf64-bigmips") == 0
10567 || strcmp (*l, "elf64-littlemips") == 0)
10568 #endif
10569 break;
10570 yes = (*l != NULL);
10571 free (list);
10572 return yes;
10573 }
10574 #endif /* OBJ_ELF */
10575
10576 const char *md_shortopts = "O::g::G:";
10577
10578 struct option md_longopts[] =
10579 {
10580 /* Options which specify architecture. */
10581 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10582 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10583 {"march", required_argument, NULL, OPTION_MARCH},
10584 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10585 {"mtune", required_argument, NULL, OPTION_MTUNE},
10586 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10587 {"mips0", no_argument, NULL, OPTION_MIPS1},
10588 {"mips1", no_argument, NULL, OPTION_MIPS1},
10589 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10590 {"mips2", no_argument, NULL, OPTION_MIPS2},
10591 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10592 {"mips3", no_argument, NULL, OPTION_MIPS3},
10593 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10594 {"mips4", no_argument, NULL, OPTION_MIPS4},
10595 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10596 {"mips5", no_argument, NULL, OPTION_MIPS5},
10597 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10598 {"mips32", no_argument, NULL, OPTION_MIPS32},
10599 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10600 {"mips64", no_argument, NULL, OPTION_MIPS64},
10601 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10602 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10603 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10604 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10605
10606 /* Options which specify Application Specific Extensions (ASEs). */
10607 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10608 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10609 {"mips16", no_argument, NULL, OPTION_MIPS16},
10610 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10611 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10612 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10613 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10614 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10615 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10616 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10617 {"mdmx", no_argument, NULL, OPTION_MDMX},
10618 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10619 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10620 #define OPTION_DSP (OPTION_ASE_BASE + 6)
10621 {"mdsp", no_argument, NULL, OPTION_DSP},
10622 #define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10623 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
10624 #define OPTION_MT (OPTION_ASE_BASE + 8)
10625 {"mmt", no_argument, NULL, OPTION_MT},
10626 #define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10627 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
10628 #define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
10629 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
10630 #define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
10631 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
10632
10633 /* Old-style architecture options. Don't add more of these. */
10634 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 12)
10635 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10636 {"m4650", no_argument, NULL, OPTION_M4650},
10637 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10638 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10639 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10640 {"m4010", no_argument, NULL, OPTION_M4010},
10641 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10642 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10643 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10644 {"m4100", no_argument, NULL, OPTION_M4100},
10645 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10646 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10647 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10648 {"m3900", no_argument, NULL, OPTION_M3900},
10649 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10650 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10651
10652 /* Options which enable bug fixes. */
10653 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10654 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10655 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10656 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10657 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10658 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10659 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10660 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10661 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10662 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10663 #define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10664 #define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10665 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10666 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
10667
10668 /* Miscellaneous options. */
10669 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
10670 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10671 {"trap", no_argument, NULL, OPTION_TRAP},
10672 {"no-break", no_argument, NULL, OPTION_TRAP},
10673 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10674 {"break", no_argument, NULL, OPTION_BREAK},
10675 {"no-trap", no_argument, NULL, OPTION_BREAK},
10676 #define OPTION_EB (OPTION_MISC_BASE + 2)
10677 {"EB", no_argument, NULL, OPTION_EB},
10678 #define OPTION_EL (OPTION_MISC_BASE + 3)
10679 {"EL", no_argument, NULL, OPTION_EL},
10680 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10681 {"mfp32", no_argument, NULL, OPTION_FP32},
10682 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10683 {"mgp32", no_argument, NULL, OPTION_GP32},
10684 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10685 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10686 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10687 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10688 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10689 {"mfp64", no_argument, NULL, OPTION_FP64},
10690 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10691 {"mgp64", no_argument, NULL, OPTION_GP64},
10692 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10693 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10694 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10695 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10696 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10697 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10698 {"mshared", no_argument, NULL, OPTION_MSHARED},
10699 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10700 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10701 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10702 {"msym32", no_argument, NULL, OPTION_MSYM32},
10703 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10704
10705 /* ELF-specific options. */
10706 #ifdef OBJ_ELF
10707 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10708 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10709 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10710 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10711 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10712 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10713 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10714 {"xgot", no_argument, NULL, OPTION_XGOT},
10715 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10716 {"mabi", required_argument, NULL, OPTION_MABI},
10717 #define OPTION_32 (OPTION_ELF_BASE + 4)
10718 {"32", no_argument, NULL, OPTION_32},
10719 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10720 {"n32", no_argument, NULL, OPTION_N32},
10721 #define OPTION_64 (OPTION_ELF_BASE + 6)
10722 {"64", no_argument, NULL, OPTION_64},
10723 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10724 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10725 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10726 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10727 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10728 {"mpdr", no_argument, NULL, OPTION_PDR},
10729 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10730 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10731 #define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10732 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
10733 #endif /* OBJ_ELF */
10734
10735 {NULL, no_argument, NULL, 0}
10736 };
10737 size_t md_longopts_size = sizeof (md_longopts);
10738
10739 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10740 NEW_VALUE. Warn if another value was already specified. Note:
10741 we have to defer parsing the -march and -mtune arguments in order
10742 to handle 'from-abi' correctly, since the ABI might be specified
10743 in a later argument. */
10744
10745 static void
10746 mips_set_option_string (const char **string_ptr, const char *new_value)
10747 {
10748 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10749 as_warn (_("A different %s was already specified, is now %s"),
10750 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10751 new_value);
10752
10753 *string_ptr = new_value;
10754 }
10755
10756 int
10757 md_parse_option (int c, char *arg)
10758 {
10759 switch (c)
10760 {
10761 case OPTION_CONSTRUCT_FLOATS:
10762 mips_disable_float_construction = 0;
10763 break;
10764
10765 case OPTION_NO_CONSTRUCT_FLOATS:
10766 mips_disable_float_construction = 1;
10767 break;
10768
10769 case OPTION_TRAP:
10770 mips_trap = 1;
10771 break;
10772
10773 case OPTION_BREAK:
10774 mips_trap = 0;
10775 break;
10776
10777 case OPTION_EB:
10778 target_big_endian = 1;
10779 break;
10780
10781 case OPTION_EL:
10782 target_big_endian = 0;
10783 break;
10784
10785 case 'O':
10786 if (arg && arg[1] == '0')
10787 mips_optimize = 1;
10788 else
10789 mips_optimize = 2;
10790 break;
10791
10792 case 'g':
10793 if (arg == NULL)
10794 mips_debug = 2;
10795 else
10796 mips_debug = atoi (arg);
10797 /* When the MIPS assembler sees -g or -g2, it does not do
10798 optimizations which limit full symbolic debugging. We take
10799 that to be equivalent to -O0. */
10800 if (mips_debug == 2)
10801 mips_optimize = 1;
10802 break;
10803
10804 case OPTION_MIPS1:
10805 file_mips_isa = ISA_MIPS1;
10806 break;
10807
10808 case OPTION_MIPS2:
10809 file_mips_isa = ISA_MIPS2;
10810 break;
10811
10812 case OPTION_MIPS3:
10813 file_mips_isa = ISA_MIPS3;
10814 break;
10815
10816 case OPTION_MIPS4:
10817 file_mips_isa = ISA_MIPS4;
10818 break;
10819
10820 case OPTION_MIPS5:
10821 file_mips_isa = ISA_MIPS5;
10822 break;
10823
10824 case OPTION_MIPS32:
10825 file_mips_isa = ISA_MIPS32;
10826 break;
10827
10828 case OPTION_MIPS32R2:
10829 file_mips_isa = ISA_MIPS32R2;
10830 break;
10831
10832 case OPTION_MIPS64R2:
10833 file_mips_isa = ISA_MIPS64R2;
10834 break;
10835
10836 case OPTION_MIPS64:
10837 file_mips_isa = ISA_MIPS64;
10838 break;
10839
10840 case OPTION_MTUNE:
10841 mips_set_option_string (&mips_tune_string, arg);
10842 break;
10843
10844 case OPTION_MARCH:
10845 mips_set_option_string (&mips_arch_string, arg);
10846 break;
10847
10848 case OPTION_M4650:
10849 mips_set_option_string (&mips_arch_string, "4650");
10850 mips_set_option_string (&mips_tune_string, "4650");
10851 break;
10852
10853 case OPTION_NO_M4650:
10854 break;
10855
10856 case OPTION_M4010:
10857 mips_set_option_string (&mips_arch_string, "4010");
10858 mips_set_option_string (&mips_tune_string, "4010");
10859 break;
10860
10861 case OPTION_NO_M4010:
10862 break;
10863
10864 case OPTION_M4100:
10865 mips_set_option_string (&mips_arch_string, "4100");
10866 mips_set_option_string (&mips_tune_string, "4100");
10867 break;
10868
10869 case OPTION_NO_M4100:
10870 break;
10871
10872 case OPTION_M3900:
10873 mips_set_option_string (&mips_arch_string, "3900");
10874 mips_set_option_string (&mips_tune_string, "3900");
10875 break;
10876
10877 case OPTION_NO_M3900:
10878 break;
10879
10880 case OPTION_MDMX:
10881 mips_opts.ase_mdmx = 1;
10882 break;
10883
10884 case OPTION_NO_MDMX:
10885 mips_opts.ase_mdmx = 0;
10886 break;
10887
10888 case OPTION_DSP:
10889 mips_opts.ase_dsp = 1;
10890 break;
10891
10892 case OPTION_NO_DSP:
10893 mips_opts.ase_dsp = 0;
10894 break;
10895
10896 case OPTION_MT:
10897 mips_opts.ase_mt = 1;
10898 break;
10899
10900 case OPTION_NO_MT:
10901 mips_opts.ase_mt = 0;
10902 break;
10903
10904 case OPTION_MIPS16:
10905 mips_opts.mips16 = 1;
10906 mips_no_prev_insn ();
10907 break;
10908
10909 case OPTION_NO_MIPS16:
10910 mips_opts.mips16 = 0;
10911 mips_no_prev_insn ();
10912 break;
10913
10914 case OPTION_MIPS3D:
10915 mips_opts.ase_mips3d = 1;
10916 break;
10917
10918 case OPTION_NO_MIPS3D:
10919 mips_opts.ase_mips3d = 0;
10920 break;
10921
10922 case OPTION_SMARTMIPS:
10923 mips_opts.ase_smartmips = 1;
10924 break;
10925
10926 case OPTION_NO_SMARTMIPS:
10927 mips_opts.ase_smartmips = 0;
10928 break;
10929
10930 case OPTION_FIX_VR4120:
10931 mips_fix_vr4120 = 1;
10932 break;
10933
10934 case OPTION_NO_FIX_VR4120:
10935 mips_fix_vr4120 = 0;
10936 break;
10937
10938 case OPTION_FIX_VR4130:
10939 mips_fix_vr4130 = 1;
10940 break;
10941
10942 case OPTION_NO_FIX_VR4130:
10943 mips_fix_vr4130 = 0;
10944 break;
10945
10946 case OPTION_RELAX_BRANCH:
10947 mips_relax_branch = 1;
10948 break;
10949
10950 case OPTION_NO_RELAX_BRANCH:
10951 mips_relax_branch = 0;
10952 break;
10953
10954 case OPTION_MSHARED:
10955 mips_in_shared = TRUE;
10956 break;
10957
10958 case OPTION_MNO_SHARED:
10959 mips_in_shared = FALSE;
10960 break;
10961
10962 case OPTION_MSYM32:
10963 mips_opts.sym32 = TRUE;
10964 break;
10965
10966 case OPTION_MNO_SYM32:
10967 mips_opts.sym32 = FALSE;
10968 break;
10969
10970 #ifdef OBJ_ELF
10971 /* When generating ELF code, we permit -KPIC and -call_shared to
10972 select SVR4_PIC, and -non_shared to select no PIC. This is
10973 intended to be compatible with Irix 5. */
10974 case OPTION_CALL_SHARED:
10975 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10976 {
10977 as_bad (_("-call_shared is supported only for ELF format"));
10978 return 0;
10979 }
10980 mips_pic = SVR4_PIC;
10981 mips_abicalls = TRUE;
10982 break;
10983
10984 case OPTION_NON_SHARED:
10985 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10986 {
10987 as_bad (_("-non_shared is supported only for ELF format"));
10988 return 0;
10989 }
10990 mips_pic = NO_PIC;
10991 mips_abicalls = FALSE;
10992 break;
10993
10994 /* The -xgot option tells the assembler to use 32 bit offsets
10995 when accessing the got in SVR4_PIC mode. It is for Irix
10996 compatibility. */
10997 case OPTION_XGOT:
10998 mips_big_got = 1;
10999 break;
11000 #endif /* OBJ_ELF */
11001
11002 case 'G':
11003 g_switch_value = atoi (arg);
11004 g_switch_seen = 1;
11005 break;
11006
11007 #ifdef OBJ_ELF
11008 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11009 and -mabi=64. */
11010 case OPTION_32:
11011 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11012 {
11013 as_bad (_("-32 is supported for ELF format only"));
11014 return 0;
11015 }
11016 mips_abi = O32_ABI;
11017 break;
11018
11019 case OPTION_N32:
11020 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11021 {
11022 as_bad (_("-n32 is supported for ELF format only"));
11023 return 0;
11024 }
11025 mips_abi = N32_ABI;
11026 break;
11027
11028 case OPTION_64:
11029 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11030 {
11031 as_bad (_("-64 is supported for ELF format only"));
11032 return 0;
11033 }
11034 mips_abi = N64_ABI;
11035 if (! support_64bit_objects())
11036 as_fatal (_("No compiled in support for 64 bit object file format"));
11037 break;
11038 #endif /* OBJ_ELF */
11039
11040 case OPTION_GP32:
11041 file_mips_gp32 = 1;
11042 break;
11043
11044 case OPTION_GP64:
11045 file_mips_gp32 = 0;
11046 break;
11047
11048 case OPTION_FP32:
11049 file_mips_fp32 = 1;
11050 break;
11051
11052 case OPTION_FP64:
11053 file_mips_fp32 = 0;
11054 break;
11055
11056 #ifdef OBJ_ELF
11057 case OPTION_MABI:
11058 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11059 {
11060 as_bad (_("-mabi is supported for ELF format only"));
11061 return 0;
11062 }
11063 if (strcmp (arg, "32") == 0)
11064 mips_abi = O32_ABI;
11065 else if (strcmp (arg, "o64") == 0)
11066 mips_abi = O64_ABI;
11067 else if (strcmp (arg, "n32") == 0)
11068 mips_abi = N32_ABI;
11069 else if (strcmp (arg, "64") == 0)
11070 {
11071 mips_abi = N64_ABI;
11072 if (! support_64bit_objects())
11073 as_fatal (_("No compiled in support for 64 bit object file "
11074 "format"));
11075 }
11076 else if (strcmp (arg, "eabi") == 0)
11077 mips_abi = EABI_ABI;
11078 else
11079 {
11080 as_fatal (_("invalid abi -mabi=%s"), arg);
11081 return 0;
11082 }
11083 break;
11084 #endif /* OBJ_ELF */
11085
11086 case OPTION_M7000_HILO_FIX:
11087 mips_7000_hilo_fix = TRUE;
11088 break;
11089
11090 case OPTION_MNO_7000_HILO_FIX:
11091 mips_7000_hilo_fix = FALSE;
11092 break;
11093
11094 #ifdef OBJ_ELF
11095 case OPTION_MDEBUG:
11096 mips_flag_mdebug = TRUE;
11097 break;
11098
11099 case OPTION_NO_MDEBUG:
11100 mips_flag_mdebug = FALSE;
11101 break;
11102
11103 case OPTION_PDR:
11104 mips_flag_pdr = TRUE;
11105 break;
11106
11107 case OPTION_NO_PDR:
11108 mips_flag_pdr = FALSE;
11109 break;
11110
11111 case OPTION_MVXWORKS_PIC:
11112 mips_pic = VXWORKS_PIC;
11113 break;
11114 #endif /* OBJ_ELF */
11115
11116 default:
11117 return 0;
11118 }
11119
11120 return 1;
11121 }
11122 \f
11123 /* Set up globals to generate code for the ISA or processor
11124 described by INFO. */
11125
11126 static void
11127 mips_set_architecture (const struct mips_cpu_info *info)
11128 {
11129 if (info != 0)
11130 {
11131 file_mips_arch = info->cpu;
11132 mips_opts.arch = info->cpu;
11133 mips_opts.isa = info->isa;
11134 }
11135 }
11136
11137
11138 /* Likewise for tuning. */
11139
11140 static void
11141 mips_set_tune (const struct mips_cpu_info *info)
11142 {
11143 if (info != 0)
11144 mips_tune = info->cpu;
11145 }
11146
11147
11148 void
11149 mips_after_parse_args (void)
11150 {
11151 const struct mips_cpu_info *arch_info = 0;
11152 const struct mips_cpu_info *tune_info = 0;
11153
11154 /* GP relative stuff not working for PE */
11155 if (strncmp (TARGET_OS, "pe", 2) == 0)
11156 {
11157 if (g_switch_seen && g_switch_value != 0)
11158 as_bad (_("-G not supported in this configuration."));
11159 g_switch_value = 0;
11160 }
11161
11162 if (mips_abi == NO_ABI)
11163 mips_abi = MIPS_DEFAULT_ABI;
11164
11165 /* The following code determines the architecture and register size.
11166 Similar code was added to GCC 3.3 (see override_options() in
11167 config/mips/mips.c). The GAS and GCC code should be kept in sync
11168 as much as possible. */
11169
11170 if (mips_arch_string != 0)
11171 arch_info = mips_parse_cpu ("-march", mips_arch_string);
11172
11173 if (file_mips_isa != ISA_UNKNOWN)
11174 {
11175 /* Handle -mipsN. At this point, file_mips_isa contains the
11176 ISA level specified by -mipsN, while arch_info->isa contains
11177 the -march selection (if any). */
11178 if (arch_info != 0)
11179 {
11180 /* -march takes precedence over -mipsN, since it is more descriptive.
11181 There's no harm in specifying both as long as the ISA levels
11182 are the same. */
11183 if (file_mips_isa != arch_info->isa)
11184 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11185 mips_cpu_info_from_isa (file_mips_isa)->name,
11186 mips_cpu_info_from_isa (arch_info->isa)->name);
11187 }
11188 else
11189 arch_info = mips_cpu_info_from_isa (file_mips_isa);
11190 }
11191
11192 if (arch_info == 0)
11193 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
11194
11195 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
11196 as_bad ("-march=%s is not compatible with the selected ABI",
11197 arch_info->name);
11198
11199 mips_set_architecture (arch_info);
11200
11201 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11202 if (mips_tune_string != 0)
11203 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
11204
11205 if (tune_info == 0)
11206 mips_set_tune (arch_info);
11207 else
11208 mips_set_tune (tune_info);
11209
11210 if (file_mips_gp32 >= 0)
11211 {
11212 /* The user specified the size of the integer registers. Make sure
11213 it agrees with the ABI and ISA. */
11214 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11215 as_bad (_("-mgp64 used with a 32-bit processor"));
11216 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11217 as_bad (_("-mgp32 used with a 64-bit ABI"));
11218 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11219 as_bad (_("-mgp64 used with a 32-bit ABI"));
11220 }
11221 else
11222 {
11223 /* Infer the integer register size from the ABI and processor.
11224 Restrict ourselves to 32-bit registers if that's all the
11225 processor has, or if the ABI cannot handle 64-bit registers. */
11226 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11227 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11228 }
11229
11230 /* ??? GAS treats single-float processors as though they had 64-bit
11231 float registers (although it complains when double-precision
11232 instructions are used). As things stand, saying they have 32-bit
11233 registers would lead to spurious "register must be even" messages.
11234 So here we assume float registers are always the same size as
11235 integer ones, unless the user says otherwise. */
11236 if (file_mips_fp32 < 0)
11237 file_mips_fp32 = file_mips_gp32;
11238
11239 /* End of GCC-shared inference code. */
11240
11241 /* This flag is set when we have a 64-bit capable CPU but use only
11242 32-bit wide registers. Note that EABI does not use it. */
11243 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11244 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11245 || mips_abi == O32_ABI))
11246 mips_32bitmode = 1;
11247
11248 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11249 as_bad (_("trap exception not supported at ISA 1"));
11250
11251 /* If the selected architecture includes support for ASEs, enable
11252 generation of code for them. */
11253 if (mips_opts.mips16 == -1)
11254 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
11255 if (mips_opts.ase_mips3d == -1)
11256 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
11257 if (mips_opts.ase_mdmx == -1)
11258 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
11259 if (mips_opts.ase_dsp == -1)
11260 mips_opts.ase_dsp = (CPU_HAS_DSP (file_mips_arch)) ? 1 : 0;
11261 if (mips_opts.ase_mt == -1)
11262 mips_opts.ase_mt = (CPU_HAS_MT (file_mips_arch)) ? 1 : 0;
11263
11264 file_mips_isa = mips_opts.isa;
11265 file_ase_mips16 = mips_opts.mips16;
11266 file_ase_mips3d = mips_opts.ase_mips3d;
11267 file_ase_mdmx = mips_opts.ase_mdmx;
11268 file_ase_smartmips = mips_opts.ase_smartmips;
11269 file_ase_dsp = mips_opts.ase_dsp;
11270 file_ase_mt = mips_opts.ase_mt;
11271 mips_opts.gp32 = file_mips_gp32;
11272 mips_opts.fp32 = file_mips_fp32;
11273
11274 if (mips_flag_mdebug < 0)
11275 {
11276 #ifdef OBJ_MAYBE_ECOFF
11277 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11278 mips_flag_mdebug = 1;
11279 else
11280 #endif /* OBJ_MAYBE_ECOFF */
11281 mips_flag_mdebug = 0;
11282 }
11283 }
11284 \f
11285 void
11286 mips_init_after_args (void)
11287 {
11288 /* initialize opcodes */
11289 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11290 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11291 }
11292
11293 long
11294 md_pcrel_from (fixS *fixP)
11295 {
11296 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11297 switch (fixP->fx_r_type)
11298 {
11299 case BFD_RELOC_16_PCREL_S2:
11300 case BFD_RELOC_MIPS_JMP:
11301 /* Return the address of the delay slot. */
11302 return addr + 4;
11303 default:
11304 return addr;
11305 }
11306 }
11307
11308 /* This is called before the symbol table is processed. In order to
11309 work with gcc when using mips-tfile, we must keep all local labels.
11310 However, in other cases, we want to discard them. If we were
11311 called with -g, but we didn't see any debugging information, it may
11312 mean that gcc is smuggling debugging information through to
11313 mips-tfile, in which case we must generate all local labels. */
11314
11315 void
11316 mips_frob_file_before_adjust (void)
11317 {
11318 #ifndef NO_ECOFF_DEBUGGING
11319 if (ECOFF_DEBUGGING
11320 && mips_debug != 0
11321 && ! ecoff_debugging_seen)
11322 flag_keep_locals = 1;
11323 #endif
11324 }
11325
11326 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
11327 the corresponding LO16 reloc. This is called before md_apply_fix and
11328 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11329 relocation operators.
11330
11331 For our purposes, a %lo() expression matches a %got() or %hi()
11332 expression if:
11333
11334 (a) it refers to the same symbol; and
11335 (b) the offset applied in the %lo() expression is no lower than
11336 the offset applied in the %got() or %hi().
11337
11338 (b) allows us to cope with code like:
11339
11340 lui $4,%hi(foo)
11341 lh $4,%lo(foo+2)($4)
11342
11343 ...which is legal on RELA targets, and has a well-defined behaviour
11344 if the user knows that adding 2 to "foo" will not induce a carry to
11345 the high 16 bits.
11346
11347 When several %lo()s match a particular %got() or %hi(), we use the
11348 following rules to distinguish them:
11349
11350 (1) %lo()s with smaller offsets are a better match than %lo()s with
11351 higher offsets.
11352
11353 (2) %lo()s with no matching %got() or %hi() are better than those
11354 that already have a matching %got() or %hi().
11355
11356 (3) later %lo()s are better than earlier %lo()s.
11357
11358 These rules are applied in order.
11359
11360 (1) means, among other things, that %lo()s with identical offsets are
11361 chosen if they exist.
11362
11363 (2) means that we won't associate several high-part relocations with
11364 the same low-part relocation unless there's no alternative. Having
11365 several high parts for the same low part is a GNU extension; this rule
11366 allows careful users to avoid it.
11367
11368 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11369 with the last high-part relocation being at the front of the list.
11370 It therefore makes sense to choose the last matching low-part
11371 relocation, all other things being equal. It's also easier
11372 to code that way. */
11373
11374 void
11375 mips_frob_file (void)
11376 {
11377 struct mips_hi_fixup *l;
11378
11379 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11380 {
11381 segment_info_type *seginfo;
11382 bfd_boolean matched_lo_p;
11383 fixS **hi_pos, **lo_pos, **pos;
11384
11385 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11386
11387 /* If a GOT16 relocation turns out to be against a global symbol,
11388 there isn't supposed to be a matching LO. */
11389 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11390 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11391 continue;
11392
11393 /* Check quickly whether the next fixup happens to be a matching %lo. */
11394 if (fixup_has_matching_lo_p (l->fixp))
11395 continue;
11396
11397 seginfo = seg_info (l->seg);
11398
11399 /* Set HI_POS to the position of this relocation in the chain.
11400 Set LO_POS to the position of the chosen low-part relocation.
11401 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11402 relocation that matches an immediately-preceding high-part
11403 relocation. */
11404 hi_pos = NULL;
11405 lo_pos = NULL;
11406 matched_lo_p = FALSE;
11407 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11408 {
11409 if (*pos == l->fixp)
11410 hi_pos = pos;
11411
11412 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11413 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
11414 && (*pos)->fx_addsy == l->fixp->fx_addsy
11415 && (*pos)->fx_offset >= l->fixp->fx_offset
11416 && (lo_pos == NULL
11417 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11418 || (!matched_lo_p
11419 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11420 lo_pos = pos;
11421
11422 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11423 && fixup_has_matching_lo_p (*pos));
11424 }
11425
11426 /* If we found a match, remove the high-part relocation from its
11427 current position and insert it before the low-part relocation.
11428 Make the offsets match so that fixup_has_matching_lo_p()
11429 will return true.
11430
11431 We don't warn about unmatched high-part relocations since some
11432 versions of gcc have been known to emit dead "lui ...%hi(...)"
11433 instructions. */
11434 if (lo_pos != NULL)
11435 {
11436 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11437 if (l->fixp->fx_next != *lo_pos)
11438 {
11439 *hi_pos = l->fixp->fx_next;
11440 l->fixp->fx_next = *lo_pos;
11441 *lo_pos = l->fixp;
11442 }
11443 }
11444 }
11445 }
11446
11447 /* We may have combined relocations without symbols in the N32/N64 ABI.
11448 We have to prevent gas from dropping them. */
11449
11450 int
11451 mips_force_relocation (fixS *fixp)
11452 {
11453 if (generic_force_reloc (fixp))
11454 return 1;
11455
11456 if (HAVE_NEWABI
11457 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11458 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11459 || fixp->fx_r_type == BFD_RELOC_HI16_S
11460 || fixp->fx_r_type == BFD_RELOC_LO16))
11461 return 1;
11462
11463 return 0;
11464 }
11465
11466 /* Apply a fixup to the object file. */
11467
11468 void
11469 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11470 {
11471 bfd_byte *buf;
11472 long insn;
11473 reloc_howto_type *howto;
11474
11475 /* We ignore generic BFD relocations we don't know about. */
11476 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11477 if (! howto)
11478 return;
11479
11480 assert (fixP->fx_size == 4
11481 || fixP->fx_r_type == BFD_RELOC_16
11482 || fixP->fx_r_type == BFD_RELOC_64
11483 || fixP->fx_r_type == BFD_RELOC_CTOR
11484 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11485 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11486 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11487
11488 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11489
11490 assert (! fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
11491
11492 /* Don't treat parts of a composite relocation as done. There are two
11493 reasons for this:
11494
11495 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11496 should nevertheless be emitted if the first part is.
11497
11498 (2) In normal usage, composite relocations are never assembly-time
11499 constants. The easiest way of dealing with the pathological
11500 exceptions is to generate a relocation against STN_UNDEF and
11501 leave everything up to the linker. */
11502 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel && fixP->fx_tcbit == 0)
11503 fixP->fx_done = 1;
11504
11505 switch (fixP->fx_r_type)
11506 {
11507 case BFD_RELOC_MIPS_TLS_GD:
11508 case BFD_RELOC_MIPS_TLS_LDM:
11509 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11510 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11511 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11512 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11513 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11514 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11515 /* fall through */
11516
11517 case BFD_RELOC_MIPS_JMP:
11518 case BFD_RELOC_MIPS_SHIFT5:
11519 case BFD_RELOC_MIPS_SHIFT6:
11520 case BFD_RELOC_MIPS_GOT_DISP:
11521 case BFD_RELOC_MIPS_GOT_PAGE:
11522 case BFD_RELOC_MIPS_GOT_OFST:
11523 case BFD_RELOC_MIPS_SUB:
11524 case BFD_RELOC_MIPS_INSERT_A:
11525 case BFD_RELOC_MIPS_INSERT_B:
11526 case BFD_RELOC_MIPS_DELETE:
11527 case BFD_RELOC_MIPS_HIGHEST:
11528 case BFD_RELOC_MIPS_HIGHER:
11529 case BFD_RELOC_MIPS_SCN_DISP:
11530 case BFD_RELOC_MIPS_REL16:
11531 case BFD_RELOC_MIPS_RELGOT:
11532 case BFD_RELOC_MIPS_JALR:
11533 case BFD_RELOC_HI16:
11534 case BFD_RELOC_HI16_S:
11535 case BFD_RELOC_GPREL16:
11536 case BFD_RELOC_MIPS_LITERAL:
11537 case BFD_RELOC_MIPS_CALL16:
11538 case BFD_RELOC_MIPS_GOT16:
11539 case BFD_RELOC_GPREL32:
11540 case BFD_RELOC_MIPS_GOT_HI16:
11541 case BFD_RELOC_MIPS_GOT_LO16:
11542 case BFD_RELOC_MIPS_CALL_HI16:
11543 case BFD_RELOC_MIPS_CALL_LO16:
11544 case BFD_RELOC_MIPS16_GPREL:
11545 case BFD_RELOC_MIPS16_HI16:
11546 case BFD_RELOC_MIPS16_HI16_S:
11547 /* Nothing needed to do. The value comes from the reloc entry */
11548 break;
11549
11550 case BFD_RELOC_MIPS16_JMP:
11551 /* We currently always generate a reloc against a symbol, which
11552 means that we don't want an addend even if the symbol is
11553 defined. */
11554 *valP = 0;
11555 break;
11556
11557 case BFD_RELOC_64:
11558 /* This is handled like BFD_RELOC_32, but we output a sign
11559 extended value if we are only 32 bits. */
11560 if (fixP->fx_done)
11561 {
11562 if (8 <= sizeof (valueT))
11563 md_number_to_chars ((char *) buf, *valP, 8);
11564 else
11565 {
11566 valueT hiv;
11567
11568 if ((*valP & 0x80000000) != 0)
11569 hiv = 0xffffffff;
11570 else
11571 hiv = 0;
11572 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
11573 *valP, 4);
11574 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
11575 hiv, 4);
11576 }
11577 }
11578 break;
11579
11580 case BFD_RELOC_RVA:
11581 case BFD_RELOC_32:
11582 /* If we are deleting this reloc entry, we must fill in the
11583 value now. This can happen if we have a .word which is not
11584 resolved when it appears but is later defined. */
11585 if (fixP->fx_done)
11586 md_number_to_chars ((char *) buf, *valP, 4);
11587 break;
11588
11589 case BFD_RELOC_16:
11590 /* If we are deleting this reloc entry, we must fill in the
11591 value now. */
11592 if (fixP->fx_done)
11593 md_number_to_chars ((char *) buf, *valP, 2);
11594 break;
11595
11596 case BFD_RELOC_LO16:
11597 case BFD_RELOC_MIPS16_LO16:
11598 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11599 may be safe to remove, but if so it's not obvious. */
11600 /* When handling an embedded PIC switch statement, we can wind
11601 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11602 if (fixP->fx_done)
11603 {
11604 if (*valP + 0x8000 > 0xffff)
11605 as_bad_where (fixP->fx_file, fixP->fx_line,
11606 _("relocation overflow"));
11607 if (target_big_endian)
11608 buf += 2;
11609 md_number_to_chars ((char *) buf, *valP, 2);
11610 }
11611 break;
11612
11613 case BFD_RELOC_16_PCREL_S2:
11614 if ((*valP & 0x3) != 0)
11615 as_bad_where (fixP->fx_file, fixP->fx_line,
11616 _("Branch to misaligned address (%lx)"), (long) *valP);
11617
11618 /*
11619 * We need to save the bits in the instruction since fixup_segment()
11620 * might be deleting the relocation entry (i.e., a branch within
11621 * the current segment).
11622 */
11623 if (! fixP->fx_done)
11624 break;
11625
11626 /* update old instruction data */
11627 if (target_big_endian)
11628 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11629 else
11630 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11631
11632 if (*valP + 0x20000 <= 0x3ffff)
11633 {
11634 insn |= (*valP >> 2) & 0xffff;
11635 md_number_to_chars ((char *) buf, insn, 4);
11636 }
11637 else if (mips_pic == NO_PIC
11638 && fixP->fx_done
11639 && fixP->fx_frag->fr_address >= text_section->vma
11640 && (fixP->fx_frag->fr_address
11641 < text_section->vma + bfd_get_section_size (text_section))
11642 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11643 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11644 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11645 {
11646 /* The branch offset is too large. If this is an
11647 unconditional branch, and we are not generating PIC code,
11648 we can convert it to an absolute jump instruction. */
11649 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11650 insn = 0x0c000000; /* jal */
11651 else
11652 insn = 0x08000000; /* j */
11653 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11654 fixP->fx_done = 0;
11655 fixP->fx_addsy = section_symbol (text_section);
11656 *valP += md_pcrel_from (fixP);
11657 md_number_to_chars ((char *) buf, insn, 4);
11658 }
11659 else
11660 {
11661 /* If we got here, we have branch-relaxation disabled,
11662 and there's nothing we can do to fix this instruction
11663 without turning it into a longer sequence. */
11664 as_bad_where (fixP->fx_file, fixP->fx_line,
11665 _("Branch out of range"));
11666 }
11667 break;
11668
11669 case BFD_RELOC_VTABLE_INHERIT:
11670 fixP->fx_done = 0;
11671 if (fixP->fx_addsy
11672 && !S_IS_DEFINED (fixP->fx_addsy)
11673 && !S_IS_WEAK (fixP->fx_addsy))
11674 S_SET_WEAK (fixP->fx_addsy);
11675 break;
11676
11677 case BFD_RELOC_VTABLE_ENTRY:
11678 fixP->fx_done = 0;
11679 break;
11680
11681 default:
11682 internalError ();
11683 }
11684
11685 /* Remember value for tc_gen_reloc. */
11686 fixP->fx_addnumber = *valP;
11687 }
11688
11689 static symbolS *
11690 get_symbol (void)
11691 {
11692 int c;
11693 char *name;
11694 symbolS *p;
11695
11696 name = input_line_pointer;
11697 c = get_symbol_end ();
11698 p = (symbolS *) symbol_find_or_make (name);
11699 *input_line_pointer = c;
11700 return p;
11701 }
11702
11703 /* Align the current frag to a given power of two. The MIPS assembler
11704 also automatically adjusts any preceding label. */
11705
11706 static void
11707 mips_align (int to, int fill, symbolS *label)
11708 {
11709 mips_emit_delays ();
11710 frag_align (to, fill, 0);
11711 record_alignment (now_seg, to);
11712 if (label != NULL)
11713 {
11714 assert (S_GET_SEGMENT (label) == now_seg);
11715 symbol_set_frag (label, frag_now);
11716 S_SET_VALUE (label, (valueT) frag_now_fix ());
11717 }
11718 }
11719
11720 /* Align to a given power of two. .align 0 turns off the automatic
11721 alignment used by the data creating pseudo-ops. */
11722
11723 static void
11724 s_align (int x ATTRIBUTE_UNUSED)
11725 {
11726 register int temp;
11727 register long temp_fill;
11728 long max_alignment = 15;
11729
11730 /*
11731
11732 o Note that the assembler pulls down any immediately preceding label
11733 to the aligned address.
11734 o It's not documented but auto alignment is reinstated by
11735 a .align pseudo instruction.
11736 o Note also that after auto alignment is turned off the mips assembler
11737 issues an error on attempt to assemble an improperly aligned data item.
11738 We don't.
11739
11740 */
11741
11742 temp = get_absolute_expression ();
11743 if (temp > max_alignment)
11744 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11745 else if (temp < 0)
11746 {
11747 as_warn (_("Alignment negative: 0 assumed."));
11748 temp = 0;
11749 }
11750 if (*input_line_pointer == ',')
11751 {
11752 ++input_line_pointer;
11753 temp_fill = get_absolute_expression ();
11754 }
11755 else
11756 temp_fill = 0;
11757 if (temp)
11758 {
11759 auto_align = 1;
11760 mips_align (temp, (int) temp_fill,
11761 insn_labels != NULL ? insn_labels->label : NULL);
11762 }
11763 else
11764 {
11765 auto_align = 0;
11766 }
11767
11768 demand_empty_rest_of_line ();
11769 }
11770
11771 static void
11772 s_change_sec (int sec)
11773 {
11774 segT seg;
11775
11776 #ifdef OBJ_ELF
11777 /* The ELF backend needs to know that we are changing sections, so
11778 that .previous works correctly. We could do something like check
11779 for an obj_section_change_hook macro, but that might be confusing
11780 as it would not be appropriate to use it in the section changing
11781 functions in read.c, since obj-elf.c intercepts those. FIXME:
11782 This should be cleaner, somehow. */
11783 obj_elf_section_change_hook ();
11784 #endif
11785
11786 mips_emit_delays ();
11787 switch (sec)
11788 {
11789 case 't':
11790 s_text (0);
11791 break;
11792 case 'd':
11793 s_data (0);
11794 break;
11795 case 'b':
11796 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11797 demand_empty_rest_of_line ();
11798 break;
11799
11800 case 'r':
11801 seg = subseg_new (RDATA_SECTION_NAME,
11802 (subsegT) get_absolute_expression ());
11803 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11804 {
11805 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11806 | SEC_READONLY | SEC_RELOC
11807 | SEC_DATA));
11808 if (strcmp (TARGET_OS, "elf") != 0)
11809 record_alignment (seg, 4);
11810 }
11811 demand_empty_rest_of_line ();
11812 break;
11813
11814 case 's':
11815 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11816 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11817 {
11818 bfd_set_section_flags (stdoutput, seg,
11819 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11820 if (strcmp (TARGET_OS, "elf") != 0)
11821 record_alignment (seg, 4);
11822 }
11823 demand_empty_rest_of_line ();
11824 break;
11825 }
11826
11827 auto_align = 1;
11828 }
11829
11830 void
11831 s_change_section (int ignore ATTRIBUTE_UNUSED)
11832 {
11833 #ifdef OBJ_ELF
11834 char *section_name;
11835 char c;
11836 char next_c = 0;
11837 int section_type;
11838 int section_flag;
11839 int section_entry_size;
11840 int section_alignment;
11841
11842 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11843 return;
11844
11845 section_name = input_line_pointer;
11846 c = get_symbol_end ();
11847 if (c)
11848 next_c = *(input_line_pointer + 1);
11849
11850 /* Do we have .section Name<,"flags">? */
11851 if (c != ',' || (c == ',' && next_c == '"'))
11852 {
11853 /* just after name is now '\0'. */
11854 *input_line_pointer = c;
11855 input_line_pointer = section_name;
11856 obj_elf_section (ignore);
11857 return;
11858 }
11859 input_line_pointer++;
11860
11861 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11862 if (c == ',')
11863 section_type = get_absolute_expression ();
11864 else
11865 section_type = 0;
11866 if (*input_line_pointer++ == ',')
11867 section_flag = get_absolute_expression ();
11868 else
11869 section_flag = 0;
11870 if (*input_line_pointer++ == ',')
11871 section_entry_size = get_absolute_expression ();
11872 else
11873 section_entry_size = 0;
11874 if (*input_line_pointer++ == ',')
11875 section_alignment = get_absolute_expression ();
11876 else
11877 section_alignment = 0;
11878
11879 section_name = xstrdup (section_name);
11880
11881 /* When using the generic form of .section (as implemented by obj-elf.c),
11882 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11883 traditionally had to fall back on the more common @progbits instead.
11884
11885 There's nothing really harmful in this, since bfd will correct
11886 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11887 means that, for backwards compatibility, the special_section entries
11888 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11889
11890 Even so, we shouldn't force users of the MIPS .section syntax to
11891 incorrectly label the sections as SHT_PROGBITS. The best compromise
11892 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11893 generic type-checking code. */
11894 if (section_type == SHT_MIPS_DWARF)
11895 section_type = SHT_PROGBITS;
11896
11897 obj_elf_change_section (section_name, section_type, section_flag,
11898 section_entry_size, 0, 0, 0);
11899
11900 if (now_seg->name != section_name)
11901 free (section_name);
11902 #endif /* OBJ_ELF */
11903 }
11904
11905 void
11906 mips_enable_auto_align (void)
11907 {
11908 auto_align = 1;
11909 }
11910
11911 static void
11912 s_cons (int log_size)
11913 {
11914 symbolS *label;
11915
11916 label = insn_labels != NULL ? insn_labels->label : NULL;
11917 mips_emit_delays ();
11918 if (log_size > 0 && auto_align)
11919 mips_align (log_size, 0, label);
11920 mips_clear_insn_labels ();
11921 cons (1 << log_size);
11922 }
11923
11924 static void
11925 s_float_cons (int type)
11926 {
11927 symbolS *label;
11928
11929 label = insn_labels != NULL ? insn_labels->label : NULL;
11930
11931 mips_emit_delays ();
11932
11933 if (auto_align)
11934 {
11935 if (type == 'd')
11936 mips_align (3, 0, label);
11937 else
11938 mips_align (2, 0, label);
11939 }
11940
11941 mips_clear_insn_labels ();
11942
11943 float_cons (type);
11944 }
11945
11946 /* Handle .globl. We need to override it because on Irix 5 you are
11947 permitted to say
11948 .globl foo .text
11949 where foo is an undefined symbol, to mean that foo should be
11950 considered to be the address of a function. */
11951
11952 static void
11953 s_mips_globl (int x ATTRIBUTE_UNUSED)
11954 {
11955 char *name;
11956 int c;
11957 symbolS *symbolP;
11958 flagword flag;
11959
11960 do
11961 {
11962 name = input_line_pointer;
11963 c = get_symbol_end ();
11964 symbolP = symbol_find_or_make (name);
11965 S_SET_EXTERNAL (symbolP);
11966
11967 *input_line_pointer = c;
11968 SKIP_WHITESPACE ();
11969
11970 /* On Irix 5, every global symbol that is not explicitly labelled as
11971 being a function is apparently labelled as being an object. */
11972 flag = BSF_OBJECT;
11973
11974 if (!is_end_of_line[(unsigned char) *input_line_pointer]
11975 && (*input_line_pointer != ','))
11976 {
11977 char *secname;
11978 asection *sec;
11979
11980 secname = input_line_pointer;
11981 c = get_symbol_end ();
11982 sec = bfd_get_section_by_name (stdoutput, secname);
11983 if (sec == NULL)
11984 as_bad (_("%s: no such section"), secname);
11985 *input_line_pointer = c;
11986
11987 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11988 flag = BSF_FUNCTION;
11989 }
11990
11991 symbol_get_bfdsym (symbolP)->flags |= flag;
11992
11993 c = *input_line_pointer;
11994 if (c == ',')
11995 {
11996 input_line_pointer++;
11997 SKIP_WHITESPACE ();
11998 if (is_end_of_line[(unsigned char) *input_line_pointer])
11999 c = '\n';
12000 }
12001 }
12002 while (c == ',');
12003
12004 demand_empty_rest_of_line ();
12005 }
12006
12007 static void
12008 s_option (int x ATTRIBUTE_UNUSED)
12009 {
12010 char *opt;
12011 char c;
12012
12013 opt = input_line_pointer;
12014 c = get_symbol_end ();
12015
12016 if (*opt == 'O')
12017 {
12018 /* FIXME: What does this mean? */
12019 }
12020 else if (strncmp (opt, "pic", 3) == 0)
12021 {
12022 int i;
12023
12024 i = atoi (opt + 3);
12025 if (i == 0)
12026 mips_pic = NO_PIC;
12027 else if (i == 2)
12028 {
12029 mips_pic = SVR4_PIC;
12030 mips_abicalls = TRUE;
12031 }
12032 else
12033 as_bad (_(".option pic%d not supported"), i);
12034
12035 if (mips_pic == SVR4_PIC)
12036 {
12037 if (g_switch_seen && g_switch_value != 0)
12038 as_warn (_("-G may not be used with SVR4 PIC code"));
12039 g_switch_value = 0;
12040 bfd_set_gp_size (stdoutput, 0);
12041 }
12042 }
12043 else
12044 as_warn (_("Unrecognized option \"%s\""), opt);
12045
12046 *input_line_pointer = c;
12047 demand_empty_rest_of_line ();
12048 }
12049
12050 /* This structure is used to hold a stack of .set values. */
12051
12052 struct mips_option_stack
12053 {
12054 struct mips_option_stack *next;
12055 struct mips_set_options options;
12056 };
12057
12058 static struct mips_option_stack *mips_opts_stack;
12059
12060 /* Handle the .set pseudo-op. */
12061
12062 static void
12063 s_mipsset (int x ATTRIBUTE_UNUSED)
12064 {
12065 char *name = input_line_pointer, ch;
12066
12067 while (!is_end_of_line[(unsigned char) *input_line_pointer])
12068 ++input_line_pointer;
12069 ch = *input_line_pointer;
12070 *input_line_pointer = '\0';
12071
12072 if (strcmp (name, "reorder") == 0)
12073 {
12074 if (mips_opts.noreorder)
12075 end_noreorder ();
12076 }
12077 else if (strcmp (name, "noreorder") == 0)
12078 {
12079 if (!mips_opts.noreorder)
12080 start_noreorder ();
12081 }
12082 else if (strcmp (name, "at") == 0)
12083 {
12084 mips_opts.noat = 0;
12085 }
12086 else if (strcmp (name, "noat") == 0)
12087 {
12088 mips_opts.noat = 1;
12089 }
12090 else if (strcmp (name, "macro") == 0)
12091 {
12092 mips_opts.warn_about_macros = 0;
12093 }
12094 else if (strcmp (name, "nomacro") == 0)
12095 {
12096 if (mips_opts.noreorder == 0)
12097 as_bad (_("`noreorder' must be set before `nomacro'"));
12098 mips_opts.warn_about_macros = 1;
12099 }
12100 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12101 {
12102 mips_opts.nomove = 0;
12103 }
12104 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12105 {
12106 mips_opts.nomove = 1;
12107 }
12108 else if (strcmp (name, "bopt") == 0)
12109 {
12110 mips_opts.nobopt = 0;
12111 }
12112 else if (strcmp (name, "nobopt") == 0)
12113 {
12114 mips_opts.nobopt = 1;
12115 }
12116 else if (strcmp (name, "mips16") == 0
12117 || strcmp (name, "MIPS-16") == 0)
12118 mips_opts.mips16 = 1;
12119 else if (strcmp (name, "nomips16") == 0
12120 || strcmp (name, "noMIPS-16") == 0)
12121 mips_opts.mips16 = 0;
12122 else if (strcmp (name, "smartmips") == 0)
12123 {
12124 if (!ISA_SUPPORT_SMARTMIPS)
12125 as_warn ("%s ISA does not support SmartMIPS ASE",
12126 mips_cpu_info_from_isa (mips_opts.isa)->name);
12127 mips_opts.ase_smartmips = 1;
12128 }
12129 else if (strcmp (name, "nosmartmips") == 0)
12130 mips_opts.ase_smartmips = 0;
12131 else if (strcmp (name, "mips3d") == 0)
12132 mips_opts.ase_mips3d = 1;
12133 else if (strcmp (name, "nomips3d") == 0)
12134 mips_opts.ase_mips3d = 0;
12135 else if (strcmp (name, "mdmx") == 0)
12136 mips_opts.ase_mdmx = 1;
12137 else if (strcmp (name, "nomdmx") == 0)
12138 mips_opts.ase_mdmx = 0;
12139 else if (strcmp (name, "dsp") == 0)
12140 mips_opts.ase_dsp = 1;
12141 else if (strcmp (name, "nodsp") == 0)
12142 mips_opts.ase_dsp = 0;
12143 else if (strcmp (name, "mt") == 0)
12144 mips_opts.ase_mt = 1;
12145 else if (strcmp (name, "nomt") == 0)
12146 mips_opts.ase_mt = 0;
12147 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
12148 {
12149 int reset = 0;
12150
12151 /* Permit the user to change the ISA and architecture on the fly.
12152 Needless to say, misuse can cause serious problems. */
12153 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
12154 {
12155 reset = 1;
12156 mips_opts.isa = file_mips_isa;
12157 mips_opts.arch = file_mips_arch;
12158 }
12159 else if (strncmp (name, "arch=", 5) == 0)
12160 {
12161 const struct mips_cpu_info *p;
12162
12163 p = mips_parse_cpu("internal use", name + 5);
12164 if (!p)
12165 as_bad (_("unknown architecture %s"), name + 5);
12166 else
12167 {
12168 mips_opts.arch = p->cpu;
12169 mips_opts.isa = p->isa;
12170 }
12171 }
12172 else if (strncmp (name, "mips", 4) == 0)
12173 {
12174 const struct mips_cpu_info *p;
12175
12176 p = mips_parse_cpu("internal use", name);
12177 if (!p)
12178 as_bad (_("unknown ISA level %s"), name + 4);
12179 else
12180 {
12181 mips_opts.arch = p->cpu;
12182 mips_opts.isa = p->isa;
12183 }
12184 }
12185 else
12186 as_bad (_("unknown ISA or architecture %s"), name);
12187
12188 switch (mips_opts.isa)
12189 {
12190 case 0:
12191 break;
12192 case ISA_MIPS1:
12193 case ISA_MIPS2:
12194 case ISA_MIPS32:
12195 case ISA_MIPS32R2:
12196 mips_opts.gp32 = 1;
12197 mips_opts.fp32 = 1;
12198 break;
12199 case ISA_MIPS3:
12200 case ISA_MIPS4:
12201 case ISA_MIPS5:
12202 case ISA_MIPS64:
12203 case ISA_MIPS64R2:
12204 mips_opts.gp32 = 0;
12205 mips_opts.fp32 = 0;
12206 break;
12207 default:
12208 as_bad (_("unknown ISA level %s"), name + 4);
12209 break;
12210 }
12211 if (reset)
12212 {
12213 mips_opts.gp32 = file_mips_gp32;
12214 mips_opts.fp32 = file_mips_fp32;
12215 }
12216 }
12217 else if (strcmp (name, "autoextend") == 0)
12218 mips_opts.noautoextend = 0;
12219 else if (strcmp (name, "noautoextend") == 0)
12220 mips_opts.noautoextend = 1;
12221 else if (strcmp (name, "push") == 0)
12222 {
12223 struct mips_option_stack *s;
12224
12225 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12226 s->next = mips_opts_stack;
12227 s->options = mips_opts;
12228 mips_opts_stack = s;
12229 }
12230 else if (strcmp (name, "pop") == 0)
12231 {
12232 struct mips_option_stack *s;
12233
12234 s = mips_opts_stack;
12235 if (s == NULL)
12236 as_bad (_(".set pop with no .set push"));
12237 else
12238 {
12239 /* If we're changing the reorder mode we need to handle
12240 delay slots correctly. */
12241 if (s->options.noreorder && ! mips_opts.noreorder)
12242 start_noreorder ();
12243 else if (! s->options.noreorder && mips_opts.noreorder)
12244 end_noreorder ();
12245
12246 mips_opts = s->options;
12247 mips_opts_stack = s->next;
12248 free (s);
12249 }
12250 }
12251 else if (strcmp (name, "sym32") == 0)
12252 mips_opts.sym32 = TRUE;
12253 else if (strcmp (name, "nosym32") == 0)
12254 mips_opts.sym32 = FALSE;
12255 else
12256 {
12257 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12258 }
12259 *input_line_pointer = ch;
12260 demand_empty_rest_of_line ();
12261 }
12262
12263 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12264 .option pic2. It means to generate SVR4 PIC calls. */
12265
12266 static void
12267 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12268 {
12269 mips_pic = SVR4_PIC;
12270 mips_abicalls = TRUE;
12271
12272 if (g_switch_seen && g_switch_value != 0)
12273 as_warn (_("-G may not be used with SVR4 PIC code"));
12274 g_switch_value = 0;
12275
12276 bfd_set_gp_size (stdoutput, 0);
12277 demand_empty_rest_of_line ();
12278 }
12279
12280 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12281 PIC code. It sets the $gp register for the function based on the
12282 function address, which is in the register named in the argument.
12283 This uses a relocation against _gp_disp, which is handled specially
12284 by the linker. The result is:
12285 lui $gp,%hi(_gp_disp)
12286 addiu $gp,$gp,%lo(_gp_disp)
12287 addu $gp,$gp,.cpload argument
12288 The .cpload argument is normally $25 == $t9.
12289
12290 The -mno-shared option changes this to:
12291 lui $gp,%hi(__gnu_local_gp)
12292 addiu $gp,$gp,%lo(__gnu_local_gp)
12293 and the argument is ignored. This saves an instruction, but the
12294 resulting code is not position independent; it uses an absolute
12295 address for __gnu_local_gp. Thus code assembled with -mno-shared
12296 can go into an ordinary executable, but not into a shared library. */
12297
12298 static void
12299 s_cpload (int ignore ATTRIBUTE_UNUSED)
12300 {
12301 expressionS ex;
12302 int reg;
12303 int in_shared;
12304
12305 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12306 .cpload is ignored. */
12307 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12308 {
12309 s_ignore (0);
12310 return;
12311 }
12312
12313 /* .cpload should be in a .set noreorder section. */
12314 if (mips_opts.noreorder == 0)
12315 as_warn (_(".cpload not in noreorder section"));
12316
12317 reg = tc_get_register (0);
12318
12319 /* If we need to produce a 64-bit address, we are better off using
12320 the default instruction sequence. */
12321 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
12322
12323 ex.X_op = O_symbol;
12324 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12325 "__gnu_local_gp");
12326 ex.X_op_symbol = NULL;
12327 ex.X_add_number = 0;
12328
12329 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12330 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12331
12332 macro_start ();
12333 macro_build_lui (&ex, mips_gp_register);
12334 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12335 mips_gp_register, BFD_RELOC_LO16);
12336 if (in_shared)
12337 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12338 mips_gp_register, reg);
12339 macro_end ();
12340
12341 demand_empty_rest_of_line ();
12342 }
12343
12344 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12345 .cpsetup $reg1, offset|$reg2, label
12346
12347 If offset is given, this results in:
12348 sd $gp, offset($sp)
12349 lui $gp, %hi(%neg(%gp_rel(label)))
12350 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12351 daddu $gp, $gp, $reg1
12352
12353 If $reg2 is given, this results in:
12354 daddu $reg2, $gp, $0
12355 lui $gp, %hi(%neg(%gp_rel(label)))
12356 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12357 daddu $gp, $gp, $reg1
12358 $reg1 is normally $25 == $t9.
12359
12360 The -mno-shared option replaces the last three instructions with
12361 lui $gp,%hi(_gp)
12362 addiu $gp,$gp,%lo(_gp)
12363 */
12364
12365 static void
12366 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12367 {
12368 expressionS ex_off;
12369 expressionS ex_sym;
12370 int reg1;
12371
12372 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12373 We also need NewABI support. */
12374 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12375 {
12376 s_ignore (0);
12377 return;
12378 }
12379
12380 reg1 = tc_get_register (0);
12381 SKIP_WHITESPACE ();
12382 if (*input_line_pointer != ',')
12383 {
12384 as_bad (_("missing argument separator ',' for .cpsetup"));
12385 return;
12386 }
12387 else
12388 ++input_line_pointer;
12389 SKIP_WHITESPACE ();
12390 if (*input_line_pointer == '$')
12391 {
12392 mips_cpreturn_register = tc_get_register (0);
12393 mips_cpreturn_offset = -1;
12394 }
12395 else
12396 {
12397 mips_cpreturn_offset = get_absolute_expression ();
12398 mips_cpreturn_register = -1;
12399 }
12400 SKIP_WHITESPACE ();
12401 if (*input_line_pointer != ',')
12402 {
12403 as_bad (_("missing argument separator ',' for .cpsetup"));
12404 return;
12405 }
12406 else
12407 ++input_line_pointer;
12408 SKIP_WHITESPACE ();
12409 expression (&ex_sym);
12410
12411 macro_start ();
12412 if (mips_cpreturn_register == -1)
12413 {
12414 ex_off.X_op = O_constant;
12415 ex_off.X_add_symbol = NULL;
12416 ex_off.X_op_symbol = NULL;
12417 ex_off.X_add_number = mips_cpreturn_offset;
12418
12419 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
12420 BFD_RELOC_LO16, SP);
12421 }
12422 else
12423 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
12424 mips_gp_register, 0);
12425
12426 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
12427 {
12428 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12429 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12430 BFD_RELOC_HI16_S);
12431
12432 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12433 mips_gp_register, -1, BFD_RELOC_GPREL16,
12434 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12435
12436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12437 mips_gp_register, reg1);
12438 }
12439 else
12440 {
12441 expressionS ex;
12442
12443 ex.X_op = O_symbol;
12444 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
12445 ex.X_op_symbol = NULL;
12446 ex.X_add_number = 0;
12447
12448 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12449 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12450
12451 macro_build_lui (&ex, mips_gp_register);
12452 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12453 mips_gp_register, BFD_RELOC_LO16);
12454 }
12455
12456 macro_end ();
12457
12458 demand_empty_rest_of_line ();
12459 }
12460
12461 static void
12462 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12463 {
12464 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12465 .cplocal is ignored. */
12466 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12467 {
12468 s_ignore (0);
12469 return;
12470 }
12471
12472 mips_gp_register = tc_get_register (0);
12473 demand_empty_rest_of_line ();
12474 }
12475
12476 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12477 offset from $sp. The offset is remembered, and after making a PIC
12478 call $gp is restored from that location. */
12479
12480 static void
12481 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12482 {
12483 expressionS ex;
12484
12485 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12486 .cprestore is ignored. */
12487 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12488 {
12489 s_ignore (0);
12490 return;
12491 }
12492
12493 mips_cprestore_offset = get_absolute_expression ();
12494 mips_cprestore_valid = 1;
12495
12496 ex.X_op = O_constant;
12497 ex.X_add_symbol = NULL;
12498 ex.X_op_symbol = NULL;
12499 ex.X_add_number = mips_cprestore_offset;
12500
12501 macro_start ();
12502 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12503 SP, HAVE_64BIT_ADDRESSES);
12504 macro_end ();
12505
12506 demand_empty_rest_of_line ();
12507 }
12508
12509 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12510 was given in the preceding .cpsetup, it results in:
12511 ld $gp, offset($sp)
12512
12513 If a register $reg2 was given there, it results in:
12514 daddu $gp, $reg2, $0
12515 */
12516 static void
12517 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12518 {
12519 expressionS ex;
12520
12521 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12522 We also need NewABI support. */
12523 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12524 {
12525 s_ignore (0);
12526 return;
12527 }
12528
12529 macro_start ();
12530 if (mips_cpreturn_register == -1)
12531 {
12532 ex.X_op = O_constant;
12533 ex.X_add_symbol = NULL;
12534 ex.X_op_symbol = NULL;
12535 ex.X_add_number = mips_cpreturn_offset;
12536
12537 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12538 }
12539 else
12540 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12541 mips_cpreturn_register, 0);
12542 macro_end ();
12543
12544 demand_empty_rest_of_line ();
12545 }
12546
12547 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12548 code. It sets the offset to use in gp_rel relocations. */
12549
12550 static void
12551 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12552 {
12553 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12554 We also need NewABI support. */
12555 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12556 {
12557 s_ignore (0);
12558 return;
12559 }
12560
12561 mips_gprel_offset = get_absolute_expression ();
12562
12563 demand_empty_rest_of_line ();
12564 }
12565
12566 /* Handle the .gpword pseudo-op. This is used when generating PIC
12567 code. It generates a 32 bit GP relative reloc. */
12568
12569 static void
12570 s_gpword (int ignore ATTRIBUTE_UNUSED)
12571 {
12572 symbolS *label;
12573 expressionS ex;
12574 char *p;
12575
12576 /* When not generating PIC code, this is treated as .word. */
12577 if (mips_pic != SVR4_PIC)
12578 {
12579 s_cons (2);
12580 return;
12581 }
12582
12583 label = insn_labels != NULL ? insn_labels->label : NULL;
12584 mips_emit_delays ();
12585 if (auto_align)
12586 mips_align (2, 0, label);
12587 mips_clear_insn_labels ();
12588
12589 expression (&ex);
12590
12591 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12592 {
12593 as_bad (_("Unsupported use of .gpword"));
12594 ignore_rest_of_line ();
12595 }
12596
12597 p = frag_more (4);
12598 md_number_to_chars (p, 0, 4);
12599 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12600 BFD_RELOC_GPREL32);
12601
12602 demand_empty_rest_of_line ();
12603 }
12604
12605 static void
12606 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12607 {
12608 symbolS *label;
12609 expressionS ex;
12610 char *p;
12611
12612 /* When not generating PIC code, this is treated as .dword. */
12613 if (mips_pic != SVR4_PIC)
12614 {
12615 s_cons (3);
12616 return;
12617 }
12618
12619 label = insn_labels != NULL ? insn_labels->label : NULL;
12620 mips_emit_delays ();
12621 if (auto_align)
12622 mips_align (3, 0, label);
12623 mips_clear_insn_labels ();
12624
12625 expression (&ex);
12626
12627 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12628 {
12629 as_bad (_("Unsupported use of .gpdword"));
12630 ignore_rest_of_line ();
12631 }
12632
12633 p = frag_more (8);
12634 md_number_to_chars (p, 0, 8);
12635 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12636 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12637
12638 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12639 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12640 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12641
12642 demand_empty_rest_of_line ();
12643 }
12644
12645 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12646 tables in SVR4 PIC code. */
12647
12648 static void
12649 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12650 {
12651 int reg;
12652
12653 /* This is ignored when not generating SVR4 PIC code. */
12654 if (mips_pic != SVR4_PIC)
12655 {
12656 s_ignore (0);
12657 return;
12658 }
12659
12660 /* Add $gp to the register named as an argument. */
12661 macro_start ();
12662 reg = tc_get_register (0);
12663 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12664 macro_end ();
12665
12666 demand_empty_rest_of_line ();
12667 }
12668
12669 /* Handle the .insn pseudo-op. This marks instruction labels in
12670 mips16 mode. This permits the linker to handle them specially,
12671 such as generating jalx instructions when needed. We also make
12672 them odd for the duration of the assembly, in order to generate the
12673 right sort of code. We will make them even in the adjust_symtab
12674 routine, while leaving them marked. This is convenient for the
12675 debugger and the disassembler. The linker knows to make them odd
12676 again. */
12677
12678 static void
12679 s_insn (int ignore ATTRIBUTE_UNUSED)
12680 {
12681 mips16_mark_labels ();
12682
12683 demand_empty_rest_of_line ();
12684 }
12685
12686 /* Handle a .stabn directive. We need these in order to mark a label
12687 as being a mips16 text label correctly. Sometimes the compiler
12688 will emit a label, followed by a .stabn, and then switch sections.
12689 If the label and .stabn are in mips16 mode, then the label is
12690 really a mips16 text label. */
12691
12692 static void
12693 s_mips_stab (int type)
12694 {
12695 if (type == 'n')
12696 mips16_mark_labels ();
12697
12698 s_stab (type);
12699 }
12700
12701 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12702 */
12703
12704 static void
12705 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12706 {
12707 char *name;
12708 int c;
12709 symbolS *symbolP;
12710 expressionS exp;
12711
12712 name = input_line_pointer;
12713 c = get_symbol_end ();
12714 symbolP = symbol_find_or_make (name);
12715 S_SET_WEAK (symbolP);
12716 *input_line_pointer = c;
12717
12718 SKIP_WHITESPACE ();
12719
12720 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12721 {
12722 if (S_IS_DEFINED (symbolP))
12723 {
12724 as_bad ("ignoring attempt to redefine symbol %s",
12725 S_GET_NAME (symbolP));
12726 ignore_rest_of_line ();
12727 return;
12728 }
12729
12730 if (*input_line_pointer == ',')
12731 {
12732 ++input_line_pointer;
12733 SKIP_WHITESPACE ();
12734 }
12735
12736 expression (&exp);
12737 if (exp.X_op != O_symbol)
12738 {
12739 as_bad ("bad .weakext directive");
12740 ignore_rest_of_line ();
12741 return;
12742 }
12743 symbol_set_value_expression (symbolP, &exp);
12744 }
12745
12746 demand_empty_rest_of_line ();
12747 }
12748
12749 /* Parse a register string into a number. Called from the ECOFF code
12750 to parse .frame. The argument is non-zero if this is the frame
12751 register, so that we can record it in mips_frame_reg. */
12752
12753 int
12754 tc_get_register (int frame)
12755 {
12756 int reg;
12757
12758 SKIP_WHITESPACE ();
12759 if (*input_line_pointer++ != '$')
12760 {
12761 as_warn (_("expected `$'"));
12762 reg = ZERO;
12763 }
12764 else if (ISDIGIT (*input_line_pointer))
12765 {
12766 reg = get_absolute_expression ();
12767 if (reg < 0 || reg >= 32)
12768 {
12769 as_warn (_("Bad register number"));
12770 reg = ZERO;
12771 }
12772 }
12773 else
12774 {
12775 if (strncmp (input_line_pointer, "ra", 2) == 0)
12776 {
12777 reg = RA;
12778 input_line_pointer += 2;
12779 }
12780 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12781 {
12782 reg = FP;
12783 input_line_pointer += 2;
12784 }
12785 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12786 {
12787 reg = SP;
12788 input_line_pointer += 2;
12789 }
12790 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12791 {
12792 reg = GP;
12793 input_line_pointer += 2;
12794 }
12795 else if (strncmp (input_line_pointer, "at", 2) == 0)
12796 {
12797 reg = AT;
12798 input_line_pointer += 2;
12799 }
12800 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12801 {
12802 reg = KT0;
12803 input_line_pointer += 3;
12804 }
12805 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12806 {
12807 reg = KT1;
12808 input_line_pointer += 3;
12809 }
12810 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12811 {
12812 reg = ZERO;
12813 input_line_pointer += 4;
12814 }
12815 else
12816 {
12817 as_warn (_("Unrecognized register name"));
12818 reg = ZERO;
12819 while (ISALNUM(*input_line_pointer))
12820 input_line_pointer++;
12821 }
12822 }
12823 if (frame)
12824 {
12825 mips_frame_reg = reg != 0 ? reg : SP;
12826 mips_frame_reg_valid = 1;
12827 mips_cprestore_valid = 0;
12828 }
12829 return reg;
12830 }
12831
12832 valueT
12833 md_section_align (asection *seg, valueT addr)
12834 {
12835 int align = bfd_get_section_alignment (stdoutput, seg);
12836
12837 #ifdef OBJ_ELF
12838 /* We don't need to align ELF sections to the full alignment.
12839 However, Irix 5 may prefer that we align them at least to a 16
12840 byte boundary. We don't bother to align the sections if we are
12841 targeted for an embedded system. */
12842 if (strcmp (TARGET_OS, "elf") == 0)
12843 return addr;
12844 if (align > 4)
12845 align = 4;
12846 #endif
12847
12848 return ((addr + (1 << align) - 1) & (-1 << align));
12849 }
12850
12851 /* Utility routine, called from above as well. If called while the
12852 input file is still being read, it's only an approximation. (For
12853 example, a symbol may later become defined which appeared to be
12854 undefined earlier.) */
12855
12856 static int
12857 nopic_need_relax (symbolS *sym, int before_relaxing)
12858 {
12859 if (sym == 0)
12860 return 0;
12861
12862 if (g_switch_value > 0)
12863 {
12864 const char *symname;
12865 int change;
12866
12867 /* Find out whether this symbol can be referenced off the $gp
12868 register. It can be if it is smaller than the -G size or if
12869 it is in the .sdata or .sbss section. Certain symbols can
12870 not be referenced off the $gp, although it appears as though
12871 they can. */
12872 symname = S_GET_NAME (sym);
12873 if (symname != (const char *) NULL
12874 && (strcmp (symname, "eprol") == 0
12875 || strcmp (symname, "etext") == 0
12876 || strcmp (symname, "_gp") == 0
12877 || strcmp (symname, "edata") == 0
12878 || strcmp (symname, "_fbss") == 0
12879 || strcmp (symname, "_fdata") == 0
12880 || strcmp (symname, "_ftext") == 0
12881 || strcmp (symname, "end") == 0
12882 || strcmp (symname, "_gp_disp") == 0))
12883 change = 1;
12884 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12885 && (0
12886 #ifndef NO_ECOFF_DEBUGGING
12887 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12888 && (symbol_get_obj (sym)->ecoff_extern_size
12889 <= g_switch_value))
12890 #endif
12891 /* We must defer this decision until after the whole
12892 file has been read, since there might be a .extern
12893 after the first use of this symbol. */
12894 || (before_relaxing
12895 #ifndef NO_ECOFF_DEBUGGING
12896 && symbol_get_obj (sym)->ecoff_extern_size == 0
12897 #endif
12898 && S_GET_VALUE (sym) == 0)
12899 || (S_GET_VALUE (sym) != 0
12900 && S_GET_VALUE (sym) <= g_switch_value)))
12901 change = 0;
12902 else
12903 {
12904 const char *segname;
12905
12906 segname = segment_name (S_GET_SEGMENT (sym));
12907 assert (strcmp (segname, ".lit8") != 0
12908 && strcmp (segname, ".lit4") != 0);
12909 change = (strcmp (segname, ".sdata") != 0
12910 && strcmp (segname, ".sbss") != 0
12911 && strncmp (segname, ".sdata.", 7) != 0
12912 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12913 }
12914 return change;
12915 }
12916 else
12917 /* We are not optimizing for the $gp register. */
12918 return 1;
12919 }
12920
12921
12922 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12923
12924 static bfd_boolean
12925 pic_need_relax (symbolS *sym, asection *segtype)
12926 {
12927 asection *symsec;
12928 bfd_boolean linkonce;
12929
12930 /* Handle the case of a symbol equated to another symbol. */
12931 while (symbol_equated_reloc_p (sym))
12932 {
12933 symbolS *n;
12934
12935 /* It's possible to get a loop here in a badly written
12936 program. */
12937 n = symbol_get_value_expression (sym)->X_add_symbol;
12938 if (n == sym)
12939 break;
12940 sym = n;
12941 }
12942
12943 symsec = S_GET_SEGMENT (sym);
12944
12945 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12946 linkonce = FALSE;
12947 if (symsec != segtype && ! S_IS_LOCAL (sym))
12948 {
12949 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12950 != 0)
12951 linkonce = TRUE;
12952
12953 /* The GNU toolchain uses an extension for ELF: a section
12954 beginning with the magic string .gnu.linkonce is a linkonce
12955 section. */
12956 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12957 sizeof ".gnu.linkonce" - 1) == 0)
12958 linkonce = TRUE;
12959 }
12960
12961 /* This must duplicate the test in adjust_reloc_syms. */
12962 return (symsec != &bfd_und_section
12963 && symsec != &bfd_abs_section
12964 && ! bfd_is_com_section (symsec)
12965 && !linkonce
12966 #ifdef OBJ_ELF
12967 /* A global or weak symbol is treated as external. */
12968 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12969 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12970 #endif
12971 );
12972 }
12973
12974
12975 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12976 extended opcode. SEC is the section the frag is in. */
12977
12978 static int
12979 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12980 {
12981 int type;
12982 register const struct mips16_immed_operand *op;
12983 offsetT val;
12984 int mintiny, maxtiny;
12985 segT symsec;
12986 fragS *sym_frag;
12987
12988 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12989 return 0;
12990 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12991 return 1;
12992
12993 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12994 op = mips16_immed_operands;
12995 while (op->type != type)
12996 {
12997 ++op;
12998 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12999 }
13000
13001 if (op->unsp)
13002 {
13003 if (type == '<' || type == '>' || type == '[' || type == ']')
13004 {
13005 mintiny = 1;
13006 maxtiny = 1 << op->nbits;
13007 }
13008 else
13009 {
13010 mintiny = 0;
13011 maxtiny = (1 << op->nbits) - 1;
13012 }
13013 }
13014 else
13015 {
13016 mintiny = - (1 << (op->nbits - 1));
13017 maxtiny = (1 << (op->nbits - 1)) - 1;
13018 }
13019
13020 sym_frag = symbol_get_frag (fragp->fr_symbol);
13021 val = S_GET_VALUE (fragp->fr_symbol);
13022 symsec = S_GET_SEGMENT (fragp->fr_symbol);
13023
13024 if (op->pcrel)
13025 {
13026 addressT addr;
13027
13028 /* We won't have the section when we are called from
13029 mips_relax_frag. However, we will always have been called
13030 from md_estimate_size_before_relax first. If this is a
13031 branch to a different section, we mark it as such. If SEC is
13032 NULL, and the frag is not marked, then it must be a branch to
13033 the same section. */
13034 if (sec == NULL)
13035 {
13036 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13037 return 1;
13038 }
13039 else
13040 {
13041 /* Must have been called from md_estimate_size_before_relax. */
13042 if (symsec != sec)
13043 {
13044 fragp->fr_subtype =
13045 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13046
13047 /* FIXME: We should support this, and let the linker
13048 catch branches and loads that are out of range. */
13049 as_bad_where (fragp->fr_file, fragp->fr_line,
13050 _("unsupported PC relative reference to different section"));
13051
13052 return 1;
13053 }
13054 if (fragp != sym_frag && sym_frag->fr_address == 0)
13055 /* Assume non-extended on the first relaxation pass.
13056 The address we have calculated will be bogus if this is
13057 a forward branch to another frag, as the forward frag
13058 will have fr_address == 0. */
13059 return 0;
13060 }
13061
13062 /* In this case, we know for sure that the symbol fragment is in
13063 the same section. If the relax_marker of the symbol fragment
13064 differs from the relax_marker of this fragment, we have not
13065 yet adjusted the symbol fragment fr_address. We want to add
13066 in STRETCH in order to get a better estimate of the address.
13067 This particularly matters because of the shift bits. */
13068 if (stretch != 0
13069 && sym_frag->relax_marker != fragp->relax_marker)
13070 {
13071 fragS *f;
13072
13073 /* Adjust stretch for any alignment frag. Note that if have
13074 been expanding the earlier code, the symbol may be
13075 defined in what appears to be an earlier frag. FIXME:
13076 This doesn't handle the fr_subtype field, which specifies
13077 a maximum number of bytes to skip when doing an
13078 alignment. */
13079 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13080 {
13081 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13082 {
13083 if (stretch < 0)
13084 stretch = - ((- stretch)
13085 & ~ ((1 << (int) f->fr_offset) - 1));
13086 else
13087 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13088 if (stretch == 0)
13089 break;
13090 }
13091 }
13092 if (f != NULL)
13093 val += stretch;
13094 }
13095
13096 addr = fragp->fr_address + fragp->fr_fix;
13097
13098 /* The base address rules are complicated. The base address of
13099 a branch is the following instruction. The base address of a
13100 PC relative load or add is the instruction itself, but if it
13101 is in a delay slot (in which case it can not be extended) use
13102 the address of the instruction whose delay slot it is in. */
13103 if (type == 'p' || type == 'q')
13104 {
13105 addr += 2;
13106
13107 /* If we are currently assuming that this frag should be
13108 extended, then, the current address is two bytes
13109 higher. */
13110 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13111 addr += 2;
13112
13113 /* Ignore the low bit in the target, since it will be set
13114 for a text label. */
13115 if ((val & 1) != 0)
13116 --val;
13117 }
13118 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13119 addr -= 4;
13120 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13121 addr -= 2;
13122
13123 val -= addr & ~ ((1 << op->shift) - 1);
13124
13125 /* Branch offsets have an implicit 0 in the lowest bit. */
13126 if (type == 'p' || type == 'q')
13127 val /= 2;
13128
13129 /* If any of the shifted bits are set, we must use an extended
13130 opcode. If the address depends on the size of this
13131 instruction, this can lead to a loop, so we arrange to always
13132 use an extended opcode. We only check this when we are in
13133 the main relaxation loop, when SEC is NULL. */
13134 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13135 {
13136 fragp->fr_subtype =
13137 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13138 return 1;
13139 }
13140
13141 /* If we are about to mark a frag as extended because the value
13142 is precisely maxtiny + 1, then there is a chance of an
13143 infinite loop as in the following code:
13144 la $4,foo
13145 .skip 1020
13146 .align 2
13147 foo:
13148 In this case when the la is extended, foo is 0x3fc bytes
13149 away, so the la can be shrunk, but then foo is 0x400 away, so
13150 the la must be extended. To avoid this loop, we mark the
13151 frag as extended if it was small, and is about to become
13152 extended with a value of maxtiny + 1. */
13153 if (val == ((maxtiny + 1) << op->shift)
13154 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13155 && sec == NULL)
13156 {
13157 fragp->fr_subtype =
13158 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13159 return 1;
13160 }
13161 }
13162 else if (symsec != absolute_section && sec != NULL)
13163 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13164
13165 if ((val & ((1 << op->shift) - 1)) != 0
13166 || val < (mintiny << op->shift)
13167 || val > (maxtiny << op->shift))
13168 return 1;
13169 else
13170 return 0;
13171 }
13172
13173 /* Compute the length of a branch sequence, and adjust the
13174 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13175 worst-case length is computed, with UPDATE being used to indicate
13176 whether an unconditional (-1), branch-likely (+1) or regular (0)
13177 branch is to be computed. */
13178 static int
13179 relaxed_branch_length (fragS *fragp, asection *sec, int update)
13180 {
13181 bfd_boolean toofar;
13182 int length;
13183
13184 if (fragp
13185 && S_IS_DEFINED (fragp->fr_symbol)
13186 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13187 {
13188 addressT addr;
13189 offsetT val;
13190
13191 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13192
13193 addr = fragp->fr_address + fragp->fr_fix + 4;
13194
13195 val -= addr;
13196
13197 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13198 }
13199 else if (fragp)
13200 /* If the symbol is not defined or it's in a different segment,
13201 assume the user knows what's going on and emit a short
13202 branch. */
13203 toofar = FALSE;
13204 else
13205 toofar = TRUE;
13206
13207 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13208 fragp->fr_subtype
13209 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13210 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13211 RELAX_BRANCH_LINK (fragp->fr_subtype),
13212 toofar);
13213
13214 length = 4;
13215 if (toofar)
13216 {
13217 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13218 length += 8;
13219
13220 if (mips_pic != NO_PIC)
13221 {
13222 /* Additional space for PIC loading of target address. */
13223 length += 8;
13224 if (mips_opts.isa == ISA_MIPS1)
13225 /* Additional space for $at-stabilizing nop. */
13226 length += 4;
13227 }
13228
13229 /* If branch is conditional. */
13230 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13231 length += 8;
13232 }
13233
13234 return length;
13235 }
13236
13237 /* Estimate the size of a frag before relaxing. Unless this is the
13238 mips16, we are not really relaxing here, and the final size is
13239 encoded in the subtype information. For the mips16, we have to
13240 decide whether we are using an extended opcode or not. */
13241
13242 int
13243 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13244 {
13245 int change;
13246
13247 if (RELAX_BRANCH_P (fragp->fr_subtype))
13248 {
13249
13250 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13251
13252 return fragp->fr_var;
13253 }
13254
13255 if (RELAX_MIPS16_P (fragp->fr_subtype))
13256 /* We don't want to modify the EXTENDED bit here; it might get us
13257 into infinite loops. We change it only in mips_relax_frag(). */
13258 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13259
13260 if (mips_pic == NO_PIC)
13261 change = nopic_need_relax (fragp->fr_symbol, 0);
13262 else if (mips_pic == SVR4_PIC)
13263 change = pic_need_relax (fragp->fr_symbol, segtype);
13264 else if (mips_pic == VXWORKS_PIC)
13265 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13266 change = 0;
13267 else
13268 abort ();
13269
13270 if (change)
13271 {
13272 fragp->fr_subtype |= RELAX_USE_SECOND;
13273 return -RELAX_FIRST (fragp->fr_subtype);
13274 }
13275 else
13276 return -RELAX_SECOND (fragp->fr_subtype);
13277 }
13278
13279 /* This is called to see whether a reloc against a defined symbol
13280 should be converted into a reloc against a section. */
13281
13282 int
13283 mips_fix_adjustable (fixS *fixp)
13284 {
13285 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13286 about the format of the offset in the .o file. */
13287 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13288 return 0;
13289
13290 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13291 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13292 return 0;
13293
13294 if (fixp->fx_addsy == NULL)
13295 return 1;
13296
13297 /* If symbol SYM is in a mergeable section, relocations of the form
13298 SYM + 0 can usually be made section-relative. The mergeable data
13299 is then identified by the section offset rather than by the symbol.
13300
13301 However, if we're generating REL LO16 relocations, the offset is split
13302 between the LO16 and parterning high part relocation. The linker will
13303 need to recalculate the complete offset in order to correctly identify
13304 the merge data.
13305
13306 The linker has traditionally not looked for the parterning high part
13307 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13308 placed anywhere. Rather than break backwards compatibility by changing
13309 this, it seems better not to force the issue, and instead keep the
13310 original symbol. This will work with either linker behavior. */
13311 if ((fixp->fx_r_type == BFD_RELOC_LO16
13312 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13313 || reloc_needs_lo_p (fixp->fx_r_type))
13314 && HAVE_IN_PLACE_ADDENDS
13315 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13316 return 0;
13317
13318 #ifdef OBJ_ELF
13319 /* Don't adjust relocations against mips16 symbols, so that the linker
13320 can find them if it needs to set up a stub. */
13321 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13322 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13323 && fixp->fx_subsy == NULL)
13324 return 0;
13325 #endif
13326
13327 return 1;
13328 }
13329
13330 /* Translate internal representation of relocation info to BFD target
13331 format. */
13332
13333 arelent **
13334 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13335 {
13336 static arelent *retval[4];
13337 arelent *reloc;
13338 bfd_reloc_code_real_type code;
13339
13340 memset (retval, 0, sizeof(retval));
13341 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13342 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13343 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13344 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13345
13346 if (fixp->fx_pcrel)
13347 {
13348 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13349
13350 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13351 Relocations want only the symbol offset. */
13352 reloc->addend = fixp->fx_addnumber + reloc->address;
13353 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13354 {
13355 /* A gruesome hack which is a result of the gruesome gas
13356 reloc handling. What's worse, for COFF (as opposed to
13357 ECOFF), we might need yet another copy of reloc->address.
13358 See bfd_install_relocation. */
13359 reloc->addend += reloc->address;
13360 }
13361 }
13362 else
13363 reloc->addend = fixp->fx_addnumber;
13364
13365 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13366 entry to be used in the relocation's section offset. */
13367 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13368 {
13369 reloc->address = reloc->addend;
13370 reloc->addend = 0;
13371 }
13372
13373 code = fixp->fx_r_type;
13374
13375 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13376 if (reloc->howto == NULL)
13377 {
13378 as_bad_where (fixp->fx_file, fixp->fx_line,
13379 _("Can not represent %s relocation in this object file format"),
13380 bfd_get_reloc_code_name (code));
13381 retval[0] = NULL;
13382 }
13383
13384 return retval;
13385 }
13386
13387 /* Relax a machine dependent frag. This returns the amount by which
13388 the current size of the frag should change. */
13389
13390 int
13391 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13392 {
13393 if (RELAX_BRANCH_P (fragp->fr_subtype))
13394 {
13395 offsetT old_var = fragp->fr_var;
13396
13397 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13398
13399 return fragp->fr_var - old_var;
13400 }
13401
13402 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13403 return 0;
13404
13405 if (mips16_extended_frag (fragp, NULL, stretch))
13406 {
13407 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13408 return 0;
13409 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13410 return 2;
13411 }
13412 else
13413 {
13414 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13415 return 0;
13416 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13417 return -2;
13418 }
13419
13420 return 0;
13421 }
13422
13423 /* Convert a machine dependent frag. */
13424
13425 void
13426 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13427 {
13428 if (RELAX_BRANCH_P (fragp->fr_subtype))
13429 {
13430 bfd_byte *buf;
13431 unsigned long insn;
13432 expressionS exp;
13433 fixS *fixp;
13434
13435 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13436
13437 if (target_big_endian)
13438 insn = bfd_getb32 (buf);
13439 else
13440 insn = bfd_getl32 (buf);
13441
13442 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13443 {
13444 /* We generate a fixup instead of applying it right now
13445 because, if there are linker relaxations, we're going to
13446 need the relocations. */
13447 exp.X_op = O_symbol;
13448 exp.X_add_symbol = fragp->fr_symbol;
13449 exp.X_add_number = fragp->fr_offset;
13450
13451 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13452 4, &exp, 1, BFD_RELOC_16_PCREL_S2);
13453 fixp->fx_file = fragp->fr_file;
13454 fixp->fx_line = fragp->fr_line;
13455
13456 md_number_to_chars ((char *) buf, insn, 4);
13457 buf += 4;
13458 }
13459 else
13460 {
13461 int i;
13462
13463 as_warn_where (fragp->fr_file, fragp->fr_line,
13464 _("relaxed out-of-range branch into a jump"));
13465
13466 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13467 goto uncond;
13468
13469 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13470 {
13471 /* Reverse the branch. */
13472 switch ((insn >> 28) & 0xf)
13473 {
13474 case 4:
13475 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13476 have the condition reversed by tweaking a single
13477 bit, and their opcodes all have 0x4???????. */
13478 assert ((insn & 0xf1000000) == 0x41000000);
13479 insn ^= 0x00010000;
13480 break;
13481
13482 case 0:
13483 /* bltz 0x04000000 bgez 0x04010000
13484 bltzal 0x04100000 bgezal 0x04110000 */
13485 assert ((insn & 0xfc0e0000) == 0x04000000);
13486 insn ^= 0x00010000;
13487 break;
13488
13489 case 1:
13490 /* beq 0x10000000 bne 0x14000000
13491 blez 0x18000000 bgtz 0x1c000000 */
13492 insn ^= 0x04000000;
13493 break;
13494
13495 default:
13496 abort ();
13497 }
13498 }
13499
13500 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13501 {
13502 /* Clear the and-link bit. */
13503 assert ((insn & 0xfc1c0000) == 0x04100000);
13504
13505 /* bltzal 0x04100000 bgezal 0x04110000
13506 bltzall 0x04120000 bgezall 0x04130000 */
13507 insn &= ~0x00100000;
13508 }
13509
13510 /* Branch over the branch (if the branch was likely) or the
13511 full jump (not likely case). Compute the offset from the
13512 current instruction to branch to. */
13513 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13514 i = 16;
13515 else
13516 {
13517 /* How many bytes in instructions we've already emitted? */
13518 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13519 /* How many bytes in instructions from here to the end? */
13520 i = fragp->fr_var - i;
13521 }
13522 /* Convert to instruction count. */
13523 i >>= 2;
13524 /* Branch counts from the next instruction. */
13525 i--;
13526 insn |= i;
13527 /* Branch over the jump. */
13528 md_number_to_chars ((char *) buf, insn, 4);
13529 buf += 4;
13530
13531 /* Nop */
13532 md_number_to_chars ((char *) buf, 0, 4);
13533 buf += 4;
13534
13535 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13536 {
13537 /* beql $0, $0, 2f */
13538 insn = 0x50000000;
13539 /* Compute the PC offset from the current instruction to
13540 the end of the variable frag. */
13541 /* How many bytes in instructions we've already emitted? */
13542 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13543 /* How many bytes in instructions from here to the end? */
13544 i = fragp->fr_var - i;
13545 /* Convert to instruction count. */
13546 i >>= 2;
13547 /* Don't decrement i, because we want to branch over the
13548 delay slot. */
13549
13550 insn |= i;
13551 md_number_to_chars ((char *) buf, insn, 4);
13552 buf += 4;
13553
13554 md_number_to_chars ((char *) buf, 0, 4);
13555 buf += 4;
13556 }
13557
13558 uncond:
13559 if (mips_pic == NO_PIC)
13560 {
13561 /* j or jal. */
13562 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13563 ? 0x0c000000 : 0x08000000);
13564 exp.X_op = O_symbol;
13565 exp.X_add_symbol = fragp->fr_symbol;
13566 exp.X_add_number = fragp->fr_offset;
13567
13568 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13569 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13570 fixp->fx_file = fragp->fr_file;
13571 fixp->fx_line = fragp->fr_line;
13572
13573 md_number_to_chars ((char *) buf, insn, 4);
13574 buf += 4;
13575 }
13576 else
13577 {
13578 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13579 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13580 exp.X_op = O_symbol;
13581 exp.X_add_symbol = fragp->fr_symbol;
13582 exp.X_add_number = fragp->fr_offset;
13583
13584 if (fragp->fr_offset)
13585 {
13586 exp.X_add_symbol = make_expr_symbol (&exp);
13587 exp.X_add_number = 0;
13588 }
13589
13590 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13591 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13592 fixp->fx_file = fragp->fr_file;
13593 fixp->fx_line = fragp->fr_line;
13594
13595 md_number_to_chars ((char *) buf, insn, 4);
13596 buf += 4;
13597
13598 if (mips_opts.isa == ISA_MIPS1)
13599 {
13600 /* nop */
13601 md_number_to_chars ((char *) buf, 0, 4);
13602 buf += 4;
13603 }
13604
13605 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13606 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13607
13608 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13609 4, &exp, 0, BFD_RELOC_LO16);
13610 fixp->fx_file = fragp->fr_file;
13611 fixp->fx_line = fragp->fr_line;
13612
13613 md_number_to_chars ((char *) buf, insn, 4);
13614 buf += 4;
13615
13616 /* j(al)r $at. */
13617 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13618 insn = 0x0020f809;
13619 else
13620 insn = 0x00200008;
13621
13622 md_number_to_chars ((char *) buf, insn, 4);
13623 buf += 4;
13624 }
13625 }
13626
13627 assert (buf == (bfd_byte *)fragp->fr_literal
13628 + fragp->fr_fix + fragp->fr_var);
13629
13630 fragp->fr_fix += fragp->fr_var;
13631
13632 return;
13633 }
13634
13635 if (RELAX_MIPS16_P (fragp->fr_subtype))
13636 {
13637 int type;
13638 register const struct mips16_immed_operand *op;
13639 bfd_boolean small, ext;
13640 offsetT val;
13641 bfd_byte *buf;
13642 unsigned long insn;
13643 bfd_boolean use_extend;
13644 unsigned short extend;
13645
13646 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13647 op = mips16_immed_operands;
13648 while (op->type != type)
13649 ++op;
13650
13651 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13652 {
13653 small = FALSE;
13654 ext = TRUE;
13655 }
13656 else
13657 {
13658 small = TRUE;
13659 ext = FALSE;
13660 }
13661
13662 resolve_symbol_value (fragp->fr_symbol);
13663 val = S_GET_VALUE (fragp->fr_symbol);
13664 if (op->pcrel)
13665 {
13666 addressT addr;
13667
13668 addr = fragp->fr_address + fragp->fr_fix;
13669
13670 /* The rules for the base address of a PC relative reloc are
13671 complicated; see mips16_extended_frag. */
13672 if (type == 'p' || type == 'q')
13673 {
13674 addr += 2;
13675 if (ext)
13676 addr += 2;
13677 /* Ignore the low bit in the target, since it will be
13678 set for a text label. */
13679 if ((val & 1) != 0)
13680 --val;
13681 }
13682 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13683 addr -= 4;
13684 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13685 addr -= 2;
13686
13687 addr &= ~ (addressT) ((1 << op->shift) - 1);
13688 val -= addr;
13689
13690 /* Make sure the section winds up with the alignment we have
13691 assumed. */
13692 if (op->shift > 0)
13693 record_alignment (asec, op->shift);
13694 }
13695
13696 if (ext
13697 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13698 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13699 as_warn_where (fragp->fr_file, fragp->fr_line,
13700 _("extended instruction in delay slot"));
13701
13702 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13703
13704 if (target_big_endian)
13705 insn = bfd_getb16 (buf);
13706 else
13707 insn = bfd_getl16 (buf);
13708
13709 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13710 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13711 small, ext, &insn, &use_extend, &extend);
13712
13713 if (use_extend)
13714 {
13715 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13716 fragp->fr_fix += 2;
13717 buf += 2;
13718 }
13719
13720 md_number_to_chars ((char *) buf, insn, 2);
13721 fragp->fr_fix += 2;
13722 buf += 2;
13723 }
13724 else
13725 {
13726 int first, second;
13727 fixS *fixp;
13728
13729 first = RELAX_FIRST (fragp->fr_subtype);
13730 second = RELAX_SECOND (fragp->fr_subtype);
13731 fixp = (fixS *) fragp->fr_opcode;
13732
13733 /* Possibly emit a warning if we've chosen the longer option. */
13734 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13735 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13736 {
13737 const char *msg = macro_warning (fragp->fr_subtype);
13738 if (msg != 0)
13739 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13740 }
13741
13742 /* Go through all the fixups for the first sequence. Disable them
13743 (by marking them as done) if we're going to use the second
13744 sequence instead. */
13745 while (fixp
13746 && fixp->fx_frag == fragp
13747 && fixp->fx_where < fragp->fr_fix - second)
13748 {
13749 if (fragp->fr_subtype & RELAX_USE_SECOND)
13750 fixp->fx_done = 1;
13751 fixp = fixp->fx_next;
13752 }
13753
13754 /* Go through the fixups for the second sequence. Disable them if
13755 we're going to use the first sequence, otherwise adjust their
13756 addresses to account for the relaxation. */
13757 while (fixp && fixp->fx_frag == fragp)
13758 {
13759 if (fragp->fr_subtype & RELAX_USE_SECOND)
13760 fixp->fx_where -= first;
13761 else
13762 fixp->fx_done = 1;
13763 fixp = fixp->fx_next;
13764 }
13765
13766 /* Now modify the frag contents. */
13767 if (fragp->fr_subtype & RELAX_USE_SECOND)
13768 {
13769 char *start;
13770
13771 start = fragp->fr_literal + fragp->fr_fix - first - second;
13772 memmove (start, start + first, second);
13773 fragp->fr_fix -= first;
13774 }
13775 else
13776 fragp->fr_fix -= second;
13777 }
13778 }
13779
13780 #ifdef OBJ_ELF
13781
13782 /* This function is called after the relocs have been generated.
13783 We've been storing mips16 text labels as odd. Here we convert them
13784 back to even for the convenience of the debugger. */
13785
13786 void
13787 mips_frob_file_after_relocs (void)
13788 {
13789 asymbol **syms;
13790 unsigned int count, i;
13791
13792 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13793 return;
13794
13795 syms = bfd_get_outsymbols (stdoutput);
13796 count = bfd_get_symcount (stdoutput);
13797 for (i = 0; i < count; i++, syms++)
13798 {
13799 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13800 && ((*syms)->value & 1) != 0)
13801 {
13802 (*syms)->value &= ~1;
13803 /* If the symbol has an odd size, it was probably computed
13804 incorrectly, so adjust that as well. */
13805 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13806 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13807 }
13808 }
13809 }
13810
13811 #endif
13812
13813 /* This function is called whenever a label is defined. It is used
13814 when handling branch delays; if a branch has a label, we assume we
13815 can not move it. */
13816
13817 void
13818 mips_define_label (symbolS *sym)
13819 {
13820 struct insn_label_list *l;
13821
13822 if (free_insn_labels == NULL)
13823 l = (struct insn_label_list *) xmalloc (sizeof *l);
13824 else
13825 {
13826 l = free_insn_labels;
13827 free_insn_labels = l->next;
13828 }
13829
13830 l->label = sym;
13831 l->next = insn_labels;
13832 insn_labels = l;
13833
13834 #ifdef OBJ_ELF
13835 dwarf2_emit_label (sym);
13836 #endif
13837 }
13838 \f
13839 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13840
13841 /* Some special processing for a MIPS ELF file. */
13842
13843 void
13844 mips_elf_final_processing (void)
13845 {
13846 /* Write out the register information. */
13847 if (mips_abi != N64_ABI)
13848 {
13849 Elf32_RegInfo s;
13850
13851 s.ri_gprmask = mips_gprmask;
13852 s.ri_cprmask[0] = mips_cprmask[0];
13853 s.ri_cprmask[1] = mips_cprmask[1];
13854 s.ri_cprmask[2] = mips_cprmask[2];
13855 s.ri_cprmask[3] = mips_cprmask[3];
13856 /* The gp_value field is set by the MIPS ELF backend. */
13857
13858 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13859 ((Elf32_External_RegInfo *)
13860 mips_regmask_frag));
13861 }
13862 else
13863 {
13864 Elf64_Internal_RegInfo s;
13865
13866 s.ri_gprmask = mips_gprmask;
13867 s.ri_pad = 0;
13868 s.ri_cprmask[0] = mips_cprmask[0];
13869 s.ri_cprmask[1] = mips_cprmask[1];
13870 s.ri_cprmask[2] = mips_cprmask[2];
13871 s.ri_cprmask[3] = mips_cprmask[3];
13872 /* The gp_value field is set by the MIPS ELF backend. */
13873
13874 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13875 ((Elf64_External_RegInfo *)
13876 mips_regmask_frag));
13877 }
13878
13879 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13880 sort of BFD interface for this. */
13881 if (mips_any_noreorder)
13882 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13883 if (mips_pic != NO_PIC)
13884 {
13885 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13886 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13887 }
13888 if (mips_abicalls)
13889 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13890
13891 /* Set MIPS ELF flags for ASEs. */
13892 /* We may need to define a new flag for DSP ASE, and set this flag when
13893 file_ase_dsp is true. */
13894 /* We may need to define a new flag for MT ASE, and set this flag when
13895 file_ase_mt is true. */
13896 if (file_ase_mips16)
13897 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13898 #if 0 /* XXX FIXME */
13899 if (file_ase_mips3d)
13900 elf_elfheader (stdoutput)->e_flags |= ???;
13901 #endif
13902 if (file_ase_mdmx)
13903 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13904
13905 /* Set the MIPS ELF ABI flags. */
13906 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13907 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13908 else if (mips_abi == O64_ABI)
13909 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13910 else if (mips_abi == EABI_ABI)
13911 {
13912 if (!file_mips_gp32)
13913 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13914 else
13915 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13916 }
13917 else if (mips_abi == N32_ABI)
13918 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13919
13920 /* Nothing to do for N64_ABI. */
13921
13922 if (mips_32bitmode)
13923 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13924 }
13925
13926 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13927 \f
13928 typedef struct proc {
13929 symbolS *func_sym;
13930 symbolS *func_end_sym;
13931 unsigned long reg_mask;
13932 unsigned long reg_offset;
13933 unsigned long fpreg_mask;
13934 unsigned long fpreg_offset;
13935 unsigned long frame_offset;
13936 unsigned long frame_reg;
13937 unsigned long pc_reg;
13938 } procS;
13939
13940 static procS cur_proc;
13941 static procS *cur_proc_ptr;
13942 static int numprocs;
13943
13944 /* Fill in an rs_align_code fragment. */
13945
13946 void
13947 mips_handle_align (fragS *fragp)
13948 {
13949 if (fragp->fr_type != rs_align_code)
13950 return;
13951
13952 if (mips_opts.mips16)
13953 {
13954 static const unsigned char be_nop[] = { 0x65, 0x00 };
13955 static const unsigned char le_nop[] = { 0x00, 0x65 };
13956
13957 int bytes;
13958 char *p;
13959
13960 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13961 p = fragp->fr_literal + fragp->fr_fix;
13962
13963 if (bytes & 1)
13964 {
13965 *p++ = 0;
13966 fragp->fr_fix++;
13967 }
13968
13969 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13970 fragp->fr_var = 2;
13971 }
13972
13973 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13974 }
13975
13976 static void
13977 md_obj_begin (void)
13978 {
13979 }
13980
13981 static void
13982 md_obj_end (void)
13983 {
13984 /* check for premature end, nesting errors, etc */
13985 if (cur_proc_ptr)
13986 as_warn (_("missing .end at end of assembly"));
13987 }
13988
13989 static long
13990 get_number (void)
13991 {
13992 int negative = 0;
13993 long val = 0;
13994
13995 if (*input_line_pointer == '-')
13996 {
13997 ++input_line_pointer;
13998 negative = 1;
13999 }
14000 if (!ISDIGIT (*input_line_pointer))
14001 as_bad (_("expected simple number"));
14002 if (input_line_pointer[0] == '0')
14003 {
14004 if (input_line_pointer[1] == 'x')
14005 {
14006 input_line_pointer += 2;
14007 while (ISXDIGIT (*input_line_pointer))
14008 {
14009 val <<= 4;
14010 val |= hex_value (*input_line_pointer++);
14011 }
14012 return negative ? -val : val;
14013 }
14014 else
14015 {
14016 ++input_line_pointer;
14017 while (ISDIGIT (*input_line_pointer))
14018 {
14019 val <<= 3;
14020 val |= *input_line_pointer++ - '0';
14021 }
14022 return negative ? -val : val;
14023 }
14024 }
14025 if (!ISDIGIT (*input_line_pointer))
14026 {
14027 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14028 *input_line_pointer, *input_line_pointer);
14029 as_warn (_("invalid number"));
14030 return -1;
14031 }
14032 while (ISDIGIT (*input_line_pointer))
14033 {
14034 val *= 10;
14035 val += *input_line_pointer++ - '0';
14036 }
14037 return negative ? -val : val;
14038 }
14039
14040 /* The .file directive; just like the usual .file directive, but there
14041 is an initial number which is the ECOFF file index. In the non-ECOFF
14042 case .file implies DWARF-2. */
14043
14044 static void
14045 s_mips_file (int x ATTRIBUTE_UNUSED)
14046 {
14047 static int first_file_directive = 0;
14048
14049 if (ECOFF_DEBUGGING)
14050 {
14051 get_number ();
14052 s_app_file (0);
14053 }
14054 else
14055 {
14056 char *filename;
14057
14058 filename = dwarf2_directive_file (0);
14059
14060 /* Versions of GCC up to 3.1 start files with a ".file"
14061 directive even for stabs output. Make sure that this
14062 ".file" is handled. Note that you need a version of GCC
14063 after 3.1 in order to support DWARF-2 on MIPS. */
14064 if (filename != NULL && ! first_file_directive)
14065 {
14066 (void) new_logical_line (filename, -1);
14067 s_app_file_string (filename, 0);
14068 }
14069 first_file_directive = 1;
14070 }
14071 }
14072
14073 /* The .loc directive, implying DWARF-2. */
14074
14075 static void
14076 s_mips_loc (int x ATTRIBUTE_UNUSED)
14077 {
14078 if (!ECOFF_DEBUGGING)
14079 dwarf2_directive_loc (0);
14080 }
14081
14082 /* The .end directive. */
14083
14084 static void
14085 s_mips_end (int x ATTRIBUTE_UNUSED)
14086 {
14087 symbolS *p;
14088
14089 /* Following functions need their own .frame and .cprestore directives. */
14090 mips_frame_reg_valid = 0;
14091 mips_cprestore_valid = 0;
14092
14093 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14094 {
14095 p = get_symbol ();
14096 demand_empty_rest_of_line ();
14097 }
14098 else
14099 p = NULL;
14100
14101 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14102 as_warn (_(".end not in text section"));
14103
14104 if (!cur_proc_ptr)
14105 {
14106 as_warn (_(".end directive without a preceding .ent directive."));
14107 demand_empty_rest_of_line ();
14108 return;
14109 }
14110
14111 if (p != NULL)
14112 {
14113 assert (S_GET_NAME (p));
14114 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
14115 as_warn (_(".end symbol does not match .ent symbol."));
14116
14117 if (debug_type == DEBUG_STABS)
14118 stabs_generate_asm_endfunc (S_GET_NAME (p),
14119 S_GET_NAME (p));
14120 }
14121 else
14122 as_warn (_(".end directive missing or unknown symbol"));
14123
14124 #ifdef OBJ_ELF
14125 /* Create an expression to calculate the size of the function. */
14126 if (p && cur_proc_ptr)
14127 {
14128 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14129 expressionS *exp = xmalloc (sizeof (expressionS));
14130
14131 obj->size = exp;
14132 exp->X_op = O_subtract;
14133 exp->X_add_symbol = symbol_temp_new_now ();
14134 exp->X_op_symbol = p;
14135 exp->X_add_number = 0;
14136
14137 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14138 }
14139
14140 /* Generate a .pdr section. */
14141 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14142 && mips_flag_pdr)
14143 {
14144 segT saved_seg = now_seg;
14145 subsegT saved_subseg = now_subseg;
14146 valueT dot;
14147 expressionS exp;
14148 char *fragp;
14149
14150 dot = frag_now_fix ();
14151
14152 #ifdef md_flush_pending_output
14153 md_flush_pending_output ();
14154 #endif
14155
14156 assert (pdr_seg);
14157 subseg_set (pdr_seg, 0);
14158
14159 /* Write the symbol. */
14160 exp.X_op = O_symbol;
14161 exp.X_add_symbol = p;
14162 exp.X_add_number = 0;
14163 emit_expr (&exp, 4);
14164
14165 fragp = frag_more (7 * 4);
14166
14167 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14168 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14169 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14170 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14171 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14172 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14173 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14174
14175 subseg_set (saved_seg, saved_subseg);
14176 }
14177 #endif /* OBJ_ELF */
14178
14179 cur_proc_ptr = NULL;
14180 }
14181
14182 /* The .aent and .ent directives. */
14183
14184 static void
14185 s_mips_ent (int aent)
14186 {
14187 symbolS *symbolP;
14188
14189 symbolP = get_symbol ();
14190 if (*input_line_pointer == ',')
14191 ++input_line_pointer;
14192 SKIP_WHITESPACE ();
14193 if (ISDIGIT (*input_line_pointer)
14194 || *input_line_pointer == '-')
14195 get_number ();
14196
14197 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14198 as_warn (_(".ent or .aent not in text section."));
14199
14200 if (!aent && cur_proc_ptr)
14201 as_warn (_("missing .end"));
14202
14203 if (!aent)
14204 {
14205 /* This function needs its own .frame and .cprestore directives. */
14206 mips_frame_reg_valid = 0;
14207 mips_cprestore_valid = 0;
14208
14209 cur_proc_ptr = &cur_proc;
14210 memset (cur_proc_ptr, '\0', sizeof (procS));
14211
14212 cur_proc_ptr->func_sym = symbolP;
14213
14214 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14215
14216 ++numprocs;
14217
14218 if (debug_type == DEBUG_STABS)
14219 stabs_generate_asm_func (S_GET_NAME (symbolP),
14220 S_GET_NAME (symbolP));
14221 }
14222
14223 demand_empty_rest_of_line ();
14224 }
14225
14226 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14227 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14228 s_mips_frame is used so that we can set the PDR information correctly.
14229 We can't use the ecoff routines because they make reference to the ecoff
14230 symbol table (in the mdebug section). */
14231
14232 static void
14233 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14234 {
14235 #ifdef OBJ_ELF
14236 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14237 {
14238 long val;
14239
14240 if (cur_proc_ptr == (procS *) NULL)
14241 {
14242 as_warn (_(".frame outside of .ent"));
14243 demand_empty_rest_of_line ();
14244 return;
14245 }
14246
14247 cur_proc_ptr->frame_reg = tc_get_register (1);
14248
14249 SKIP_WHITESPACE ();
14250 if (*input_line_pointer++ != ','
14251 || get_absolute_expression_and_terminator (&val) != ',')
14252 {
14253 as_warn (_("Bad .frame directive"));
14254 --input_line_pointer;
14255 demand_empty_rest_of_line ();
14256 return;
14257 }
14258
14259 cur_proc_ptr->frame_offset = val;
14260 cur_proc_ptr->pc_reg = tc_get_register (0);
14261
14262 demand_empty_rest_of_line ();
14263 }
14264 else
14265 #endif /* OBJ_ELF */
14266 s_ignore (ignore);
14267 }
14268
14269 /* The .fmask and .mask directives. If the mdebug section is present
14270 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14271 embedded targets, s_mips_mask is used so that we can set the PDR
14272 information correctly. We can't use the ecoff routines because they
14273 make reference to the ecoff symbol table (in the mdebug section). */
14274
14275 static void
14276 s_mips_mask (int reg_type)
14277 {
14278 #ifdef OBJ_ELF
14279 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14280 {
14281 long mask, off;
14282
14283 if (cur_proc_ptr == (procS *) NULL)
14284 {
14285 as_warn (_(".mask/.fmask outside of .ent"));
14286 demand_empty_rest_of_line ();
14287 return;
14288 }
14289
14290 if (get_absolute_expression_and_terminator (&mask) != ',')
14291 {
14292 as_warn (_("Bad .mask/.fmask directive"));
14293 --input_line_pointer;
14294 demand_empty_rest_of_line ();
14295 return;
14296 }
14297
14298 off = get_absolute_expression ();
14299
14300 if (reg_type == 'F')
14301 {
14302 cur_proc_ptr->fpreg_mask = mask;
14303 cur_proc_ptr->fpreg_offset = off;
14304 }
14305 else
14306 {
14307 cur_proc_ptr->reg_mask = mask;
14308 cur_proc_ptr->reg_offset = off;
14309 }
14310
14311 demand_empty_rest_of_line ();
14312 }
14313 else
14314 #endif /* OBJ_ELF */
14315 s_ignore (reg_type);
14316 }
14317
14318 /* A table describing all the processors gas knows about. Names are
14319 matched in the order listed.
14320
14321 To ease comparison, please keep this table in the same order as
14322 gcc's mips_cpu_info_table[]. */
14323 static const struct mips_cpu_info mips_cpu_info_table[] =
14324 {
14325 /* Entries for generic ISAs */
14326 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14327 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14328 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14329 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14330 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14331 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14332 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
14333 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14334 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
14335
14336 /* MIPS I */
14337 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14338 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14339 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14340
14341 /* MIPS II */
14342 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14343
14344 /* MIPS III */
14345 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14346 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14347 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14348 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14349 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14350 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14351 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14352 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14353 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14354 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14355 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14356 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14357
14358 /* MIPS IV */
14359 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14360 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14361 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14362 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14363 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14364 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14365 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14366 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14367 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14368 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14369 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14370 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14371 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
14372
14373 /* MIPS 32 */
14374 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14375 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14376 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14377
14378 /* MIPS32 Release 2 */
14379 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14380 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14381 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14382 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14383 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14384
14385 /* MIPS 64 */
14386 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14387 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
14388 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14389
14390 /* Broadcom SB-1 CPU core */
14391 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14392
14393 /* End marker */
14394 { NULL, 0, 0, 0 }
14395 };
14396
14397
14398 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14399 with a final "000" replaced by "k". Ignore case.
14400
14401 Note: this function is shared between GCC and GAS. */
14402
14403 static bfd_boolean
14404 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14405 {
14406 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14407 given++, canonical++;
14408
14409 return ((*given == 0 && *canonical == 0)
14410 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14411 }
14412
14413
14414 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14415 CPU name. We've traditionally allowed a lot of variation here.
14416
14417 Note: this function is shared between GCC and GAS. */
14418
14419 static bfd_boolean
14420 mips_matching_cpu_name_p (const char *canonical, const char *given)
14421 {
14422 /* First see if the name matches exactly, or with a final "000"
14423 turned into "k". */
14424 if (mips_strict_matching_cpu_name_p (canonical, given))
14425 return TRUE;
14426
14427 /* If not, try comparing based on numerical designation alone.
14428 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14429 if (TOLOWER (*given) == 'r')
14430 given++;
14431 if (!ISDIGIT (*given))
14432 return FALSE;
14433
14434 /* Skip over some well-known prefixes in the canonical name,
14435 hoping to find a number there too. */
14436 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14437 canonical += 2;
14438 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14439 canonical += 2;
14440 else if (TOLOWER (canonical[0]) == 'r')
14441 canonical += 1;
14442
14443 return mips_strict_matching_cpu_name_p (canonical, given);
14444 }
14445
14446
14447 /* Parse an option that takes the name of a processor as its argument.
14448 OPTION is the name of the option and CPU_STRING is the argument.
14449 Return the corresponding processor enumeration if the CPU_STRING is
14450 recognized, otherwise report an error and return null.
14451
14452 A similar function exists in GCC. */
14453
14454 static const struct mips_cpu_info *
14455 mips_parse_cpu (const char *option, const char *cpu_string)
14456 {
14457 const struct mips_cpu_info *p;
14458
14459 /* 'from-abi' selects the most compatible architecture for the given
14460 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14461 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14462 version. Look first at the -mgp options, if given, otherwise base
14463 the choice on MIPS_DEFAULT_64BIT.
14464
14465 Treat NO_ABI like the EABIs. One reason to do this is that the
14466 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14467 architecture. This code picks MIPS I for 'mips' and MIPS III for
14468 'mips64', just as we did in the days before 'from-abi'. */
14469 if (strcasecmp (cpu_string, "from-abi") == 0)
14470 {
14471 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14472 return mips_cpu_info_from_isa (ISA_MIPS1);
14473
14474 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14475 return mips_cpu_info_from_isa (ISA_MIPS3);
14476
14477 if (file_mips_gp32 >= 0)
14478 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14479
14480 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14481 ? ISA_MIPS3
14482 : ISA_MIPS1);
14483 }
14484
14485 /* 'default' has traditionally been a no-op. Probably not very useful. */
14486 if (strcasecmp (cpu_string, "default") == 0)
14487 return 0;
14488
14489 for (p = mips_cpu_info_table; p->name != 0; p++)
14490 if (mips_matching_cpu_name_p (p->name, cpu_string))
14491 return p;
14492
14493 as_bad ("Bad value (%s) for %s", cpu_string, option);
14494 return 0;
14495 }
14496
14497 /* Return the canonical processor information for ISA (a member of the
14498 ISA_MIPS* enumeration). */
14499
14500 static const struct mips_cpu_info *
14501 mips_cpu_info_from_isa (int isa)
14502 {
14503 int i;
14504
14505 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14506 if (mips_cpu_info_table[i].is_isa
14507 && isa == mips_cpu_info_table[i].isa)
14508 return (&mips_cpu_info_table[i]);
14509
14510 return NULL;
14511 }
14512
14513 static const struct mips_cpu_info *
14514 mips_cpu_info_from_arch (int arch)
14515 {
14516 int i;
14517
14518 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14519 if (arch == mips_cpu_info_table[i].cpu)
14520 return (&mips_cpu_info_table[i]);
14521
14522 return NULL;
14523 }
14524 \f
14525 static void
14526 show (FILE *stream, const char *string, int *col_p, int *first_p)
14527 {
14528 if (*first_p)
14529 {
14530 fprintf (stream, "%24s", "");
14531 *col_p = 24;
14532 }
14533 else
14534 {
14535 fprintf (stream, ", ");
14536 *col_p += 2;
14537 }
14538
14539 if (*col_p + strlen (string) > 72)
14540 {
14541 fprintf (stream, "\n%24s", "");
14542 *col_p = 24;
14543 }
14544
14545 fprintf (stream, "%s", string);
14546 *col_p += strlen (string);
14547
14548 *first_p = 0;
14549 }
14550
14551 void
14552 md_show_usage (FILE *stream)
14553 {
14554 int column, first;
14555 size_t i;
14556
14557 fprintf (stream, _("\
14558 MIPS options:\n\
14559 -EB generate big endian output\n\
14560 -EL generate little endian output\n\
14561 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14562 -G NUM allow referencing objects up to NUM bytes\n\
14563 implicitly with the gp register [default 8]\n"));
14564 fprintf (stream, _("\
14565 -mips1 generate MIPS ISA I instructions\n\
14566 -mips2 generate MIPS ISA II instructions\n\
14567 -mips3 generate MIPS ISA III instructions\n\
14568 -mips4 generate MIPS ISA IV instructions\n\
14569 -mips5 generate MIPS ISA V instructions\n\
14570 -mips32 generate MIPS32 ISA instructions\n\
14571 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14572 -mips64 generate MIPS64 ISA instructions\n\
14573 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14574 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14575
14576 first = 1;
14577
14578 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14579 show (stream, mips_cpu_info_table[i].name, &column, &first);
14580 show (stream, "from-abi", &column, &first);
14581 fputc ('\n', stream);
14582
14583 fprintf (stream, _("\
14584 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14585 -no-mCPU don't generate code specific to CPU.\n\
14586 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14587
14588 first = 1;
14589
14590 show (stream, "3900", &column, &first);
14591 show (stream, "4010", &column, &first);
14592 show (stream, "4100", &column, &first);
14593 show (stream, "4650", &column, &first);
14594 fputc ('\n', stream);
14595
14596 fprintf (stream, _("\
14597 -mips16 generate mips16 instructions\n\
14598 -no-mips16 do not generate mips16 instructions\n"));
14599 fprintf (stream, _("\
14600 -msmartmips generate smartmips instructions\n\
14601 -mno-smartmips do not generate smartmips instructions\n"));
14602 fprintf (stream, _("\
14603 -mdsp generate DSP instructions\n\
14604 -mno-dsp do not generate DSP instructions\n"));
14605 fprintf (stream, _("\
14606 -mmt generate MT instructions\n\
14607 -mno-mt do not generate MT instructions\n"));
14608 fprintf (stream, _("\
14609 -mfix-vr4120 work around certain VR4120 errata\n\
14610 -mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
14611 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14612 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14613 -mno-shared optimize output for executables\n\
14614 -msym32 assume all symbols have 32-bit values\n\
14615 -O0 remove unneeded NOPs, do not swap branches\n\
14616 -O remove unneeded NOPs and swap branches\n\
14617 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14618 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14619 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14620 #ifdef OBJ_ELF
14621 fprintf (stream, _("\
14622 -KPIC, -call_shared generate SVR4 position independent code\n\
14623 -non_shared do not generate position independent code\n\
14624 -xgot assume a 32 bit GOT\n\
14625 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14626 -mshared, -mno-shared disable/enable .cpload optimization for\n\
14627 non-shared code\n\
14628 -mabi=ABI create ABI conformant object file for:\n"));
14629
14630 first = 1;
14631
14632 show (stream, "32", &column, &first);
14633 show (stream, "o64", &column, &first);
14634 show (stream, "n32", &column, &first);
14635 show (stream, "64", &column, &first);
14636 show (stream, "eabi", &column, &first);
14637
14638 fputc ('\n', stream);
14639
14640 fprintf (stream, _("\
14641 -32 create o32 ABI object file (default)\n\
14642 -n32 create n32 ABI object file\n\
14643 -64 create 64 ABI object file\n"));
14644 #endif
14645 }
14646
14647 enum dwarf2_format
14648 mips_dwarf2_format (void)
14649 {
14650 if (mips_abi == N64_ABI)
14651 {
14652 #ifdef TE_IRIX
14653 return dwarf2_format_64bit_irix;
14654 #else
14655 return dwarf2_format_64bit;
14656 #endif
14657 }
14658 else
14659 return dwarf2_format_32bit;
14660 }
14661
14662 int
14663 mips_dwarf2_addr_size (void)
14664 {
14665 if (mips_abi == N64_ABI)
14666 return 8;
14667 else
14668 return 4;
14669 }
14670
14671 /* Standard calling conventions leave the CFA at SP on entry. */
14672 void
14673 mips_cfi_frame_initial_instructions (void)
14674 {
14675 cfi_add_CFA_def_cfa_register (SP);
14676 }
14677
This page took 0.426045 seconds and 5 git commands to generate.