* config/tc-mips.c (mips_move_labels): New function, taken from...
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too. */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about. */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT 1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP 28
99 #define SP 29
100 #define FP 30
101 #define RA 31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format. */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
122 /* Information about an instruction, including its format, operands
123 and fixups. */
124 struct mips_cl_insn
125 {
126 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
127 const struct mips_opcode *insn_mo;
128
129 /* True if this is a mips16 instruction and if we want the extended
130 form of INSN_MO. */
131 bfd_boolean use_extend;
132
133 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
134 unsigned short extend;
135
136 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
137 a copy of INSN_MO->match with the operands filled in. */
138 unsigned long insn_opcode;
139
140 /* The frag that contains the instruction. */
141 struct frag *frag;
142
143 /* The offset into FRAG of the first instruction byte. */
144 long where;
145
146 /* The relocs associated with the instruction, if any. */
147 fixS *fixp[3];
148
149 /* True if this entry cannot be moved from its current position. */
150 unsigned int fixed_p : 1;
151
152 /* True if this instruction occured in a .set noreorder block. */
153 unsigned int noreorder_p : 1;
154
155 /* True for mips16 instructions that jump to an absolute address. */
156 unsigned int mips16_absolute_jump_p : 1;
157 };
158
159 /* The ABI to use. */
160 enum mips_abi_level
161 {
162 NO_ABI = 0,
163 O32_ABI,
164 O64_ABI,
165 N32_ABI,
166 N64_ABI,
167 EABI_ABI
168 };
169
170 /* MIPS ABI we are using for this output file. */
171 static enum mips_abi_level mips_abi = NO_ABI;
172
173 /* Whether or not we have code that can call pic code. */
174 int mips_abicalls = FALSE;
175
176 /* Whether or not we have code which can be put into a shared
177 library. */
178 static bfd_boolean mips_in_shared = TRUE;
179
180 /* This is the set of options which may be modified by the .set
181 pseudo-op. We use a struct so that .set push and .set pop are more
182 reliable. */
183
184 struct mips_set_options
185 {
186 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
187 if it has not been initialized. Changed by `.set mipsN', and the
188 -mipsN command line option, and the default CPU. */
189 int isa;
190 /* Enabled Application Specific Extensions (ASEs). These are set to -1
191 if they have not been initialized. Changed by `.set <asename>', by
192 command line options, and based on the default architecture. */
193 int ase_mips3d;
194 int ase_mdmx;
195 /* Whether we are assembling for the mips16 processor. 0 if we are
196 not, 1 if we are, and -1 if the value has not been initialized.
197 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
198 -nomips16 command line options, and the default CPU. */
199 int mips16;
200 /* Non-zero if we should not reorder instructions. Changed by `.set
201 reorder' and `.set noreorder'. */
202 int noreorder;
203 /* Non-zero if we should not permit the $at ($1) register to be used
204 in instructions. Changed by `.set at' and `.set noat'. */
205 int noat;
206 /* Non-zero if we should warn when a macro instruction expands into
207 more than one machine instruction. Changed by `.set nomacro' and
208 `.set macro'. */
209 int warn_about_macros;
210 /* Non-zero if we should not move instructions. Changed by `.set
211 move', `.set volatile', `.set nomove', and `.set novolatile'. */
212 int nomove;
213 /* Non-zero if we should not optimize branches by moving the target
214 of the branch into the delay slot. Actually, we don't perform
215 this optimization anyhow. Changed by `.set bopt' and `.set
216 nobopt'. */
217 int nobopt;
218 /* Non-zero if we should not autoextend mips16 instructions.
219 Changed by `.set autoextend' and `.set noautoextend'. */
220 int noautoextend;
221 /* Restrict general purpose registers and floating point registers
222 to 32 bit. This is initially determined when -mgp32 or -mfp32
223 is passed but can changed if the assembler code uses .set mipsN. */
224 int gp32;
225 int fp32;
226 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
227 command line option, and the default CPU. */
228 int arch;
229 /* True if ".set sym32" is in effect. */
230 bfd_boolean sym32;
231 };
232
233 /* True if -mgp32 was passed. */
234 static int file_mips_gp32 = -1;
235
236 /* True if -mfp32 was passed. */
237 static int file_mips_fp32 = -1;
238
239 /* This is the struct we use to hold the current set of options. Note
240 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
241 -1 to indicate that they have not been initialized. */
242
243 static struct mips_set_options mips_opts =
244 {
245 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
246 };
247
248 /* These variables are filled in with the masks of registers used.
249 The object format code reads them and puts them in the appropriate
250 place. */
251 unsigned long mips_gprmask;
252 unsigned long mips_cprmask[4];
253
254 /* MIPS ISA we are using for this output file. */
255 static int file_mips_isa = ISA_UNKNOWN;
256
257 /* True if -mips16 was passed or implied by arguments passed on the
258 command line (e.g., by -march). */
259 static int file_ase_mips16;
260
261 /* True if -mips3d was passed or implied by arguments passed on the
262 command line (e.g., by -march). */
263 static int file_ase_mips3d;
264
265 /* True if -mdmx was passed or implied by arguments passed on the
266 command line (e.g., by -march). */
267 static int file_ase_mdmx;
268
269 /* The argument of the -march= flag. The architecture we are assembling. */
270 static int file_mips_arch = CPU_UNKNOWN;
271 static const char *mips_arch_string;
272
273 /* The argument of the -mtune= flag. The architecture for which we
274 are optimizing. */
275 static int mips_tune = CPU_UNKNOWN;
276 static const char *mips_tune_string;
277
278 /* True when generating 32-bit code for a 64-bit processor. */
279 static int mips_32bitmode = 0;
280
281 /* True if the given ABI requires 32-bit registers. */
282 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
283
284 /* Likewise 64-bit registers. */
285 #define ABI_NEEDS_64BIT_REGS(ABI) \
286 ((ABI) == N32_ABI \
287 || (ABI) == N64_ABI \
288 || (ABI) == O64_ABI)
289
290 /* Return true if ISA supports 64 bit gp register instructions. */
291 #define ISA_HAS_64BIT_REGS(ISA) ( \
292 (ISA) == ISA_MIPS3 \
293 || (ISA) == ISA_MIPS4 \
294 || (ISA) == ISA_MIPS5 \
295 || (ISA) == ISA_MIPS64 \
296 || (ISA) == ISA_MIPS64R2 \
297 )
298
299 /* Return true if ISA supports 64-bit right rotate (dror et al.)
300 instructions. */
301 #define ISA_HAS_DROR(ISA) ( \
302 (ISA) == ISA_MIPS64R2 \
303 )
304
305 /* Return true if ISA supports 32-bit right rotate (ror et al.)
306 instructions. */
307 #define ISA_HAS_ROR(ISA) ( \
308 (ISA) == ISA_MIPS32R2 \
309 || (ISA) == ISA_MIPS64R2 \
310 )
311
312 #define HAVE_32BIT_GPRS \
313 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
314
315 #define HAVE_32BIT_FPRS \
316 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
317
318 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
319 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
320
321 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
322
323 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
324
325 /* True if relocations are stored in-place. */
326 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
327
328 /* The ABI-derived address size. */
329 #define HAVE_64BIT_ADDRESSES \
330 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
331 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
332
333 /* The size of symbolic constants (i.e., expressions of the form
334 "SYMBOL" or "SYMBOL + OFFSET"). */
335 #define HAVE_32BIT_SYMBOLS \
336 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
337 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
338
339 /* Addresses are loaded in different ways, depending on the address size
340 in use. The n32 ABI Documentation also mandates the use of additions
341 with overflow checking, but existing implementations don't follow it. */
342 #define ADDRESS_ADD_INSN \
343 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
344
345 #define ADDRESS_ADDI_INSN \
346 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
347
348 #define ADDRESS_LOAD_INSN \
349 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
350
351 #define ADDRESS_STORE_INSN \
352 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
353
354 /* Return true if the given CPU supports the MIPS16 ASE. */
355 #define CPU_HAS_MIPS16(cpu) \
356 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
357 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
358
359 /* Return true if the given CPU supports the MIPS3D ASE. */
360 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
361 )
362
363 /* Return true if the given CPU supports the MDMX ASE. */
364 #define CPU_HAS_MDMX(cpu) (FALSE \
365 )
366
367 /* True if CPU has a dror instruction. */
368 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
369
370 /* True if CPU has a ror instruction. */
371 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
372
373 /* True if mflo and mfhi can be immediately followed by instructions
374 which write to the HI and LO registers.
375
376 According to MIPS specifications, MIPS ISAs I, II, and III need
377 (at least) two instructions between the reads of HI/LO and
378 instructions which write them, and later ISAs do not. Contradicting
379 the MIPS specifications, some MIPS IV processor user manuals (e.g.
380 the UM for the NEC Vr5000) document needing the instructions between
381 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
382 MIPS64 and later ISAs to have the interlocks, plus any specific
383 earlier-ISA CPUs for which CPU documentation declares that the
384 instructions are really interlocked. */
385 #define hilo_interlocks \
386 (mips_opts.isa == ISA_MIPS32 \
387 || mips_opts.isa == ISA_MIPS32R2 \
388 || mips_opts.isa == ISA_MIPS64 \
389 || mips_opts.isa == ISA_MIPS64R2 \
390 || mips_opts.arch == CPU_R4010 \
391 || mips_opts.arch == CPU_R10000 \
392 || mips_opts.arch == CPU_R12000 \
393 || mips_opts.arch == CPU_RM7000 \
394 || mips_opts.arch == CPU_VR5500 \
395 )
396
397 /* Whether the processor uses hardware interlocks to protect reads
398 from the GPRs after they are loaded from memory, and thus does not
399 require nops to be inserted. This applies to instructions marked
400 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
401 level I. */
402 #define gpr_interlocks \
403 (mips_opts.isa != ISA_MIPS1 \
404 || mips_opts.arch == CPU_R3900)
405
406 /* Whether the processor uses hardware interlocks to avoid delays
407 required by coprocessor instructions, and thus does not require
408 nops to be inserted. This applies to instructions marked
409 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
410 between instructions marked INSN_WRITE_COND_CODE and ones marked
411 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
412 levels I, II, and III. */
413 /* Itbl support may require additional care here. */
414 #define cop_interlocks \
415 ((mips_opts.isa != ISA_MIPS1 \
416 && mips_opts.isa != ISA_MIPS2 \
417 && mips_opts.isa != ISA_MIPS3) \
418 || mips_opts.arch == CPU_R4300 \
419 )
420
421 /* Whether the processor uses hardware interlocks to protect reads
422 from coprocessor registers after they are loaded from memory, and
423 thus does not require nops to be inserted. This applies to
424 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
425 requires at MIPS ISA level I. */
426 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
427
428 /* Is this a mfhi or mflo instruction? */
429 #define MF_HILO_INSN(PINFO) \
430 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
431
432 /* MIPS PIC level. */
433
434 enum mips_pic_level mips_pic;
435
436 /* 1 if we should generate 32 bit offsets from the $gp register in
437 SVR4_PIC mode. Currently has no meaning in other modes. */
438 static int mips_big_got = 0;
439
440 /* 1 if trap instructions should used for overflow rather than break
441 instructions. */
442 static int mips_trap = 0;
443
444 /* 1 if double width floating point constants should not be constructed
445 by assembling two single width halves into two single width floating
446 point registers which just happen to alias the double width destination
447 register. On some architectures this aliasing can be disabled by a bit
448 in the status register, and the setting of this bit cannot be determined
449 automatically at assemble time. */
450 static int mips_disable_float_construction;
451
452 /* Non-zero if any .set noreorder directives were used. */
453
454 static int mips_any_noreorder;
455
456 /* Non-zero if nops should be inserted when the register referenced in
457 an mfhi/mflo instruction is read in the next two instructions. */
458 static int mips_7000_hilo_fix;
459
460 /* The size of the small data section. */
461 static unsigned int g_switch_value = 8;
462 /* Whether the -G option was used. */
463 static int g_switch_seen = 0;
464
465 #define N_RMASK 0xc4
466 #define N_VFP 0xd4
467
468 /* If we can determine in advance that GP optimization won't be
469 possible, we can skip the relaxation stuff that tries to produce
470 GP-relative references. This makes delay slot optimization work
471 better.
472
473 This function can only provide a guess, but it seems to work for
474 gcc output. It needs to guess right for gcc, otherwise gcc
475 will put what it thinks is a GP-relative instruction in a branch
476 delay slot.
477
478 I don't know if a fix is needed for the SVR4_PIC mode. I've only
479 fixed it for the non-PIC mode. KR 95/04/07 */
480 static int nopic_need_relax (symbolS *, int);
481
482 /* handle of the OPCODE hash table */
483 static struct hash_control *op_hash = NULL;
484
485 /* The opcode hash table we use for the mips16. */
486 static struct hash_control *mips16_op_hash = NULL;
487
488 /* This array holds the chars that always start a comment. If the
489 pre-processor is disabled, these aren't very useful */
490 const char comment_chars[] = "#";
491
492 /* This array holds the chars that only start a comment at the beginning of
493 a line. If the line seems to have the form '# 123 filename'
494 .line and .file directives will appear in the pre-processed output */
495 /* Note that input_file.c hand checks for '#' at the beginning of the
496 first line of the input file. This is because the compiler outputs
497 #NO_APP at the beginning of its output. */
498 /* Also note that C style comments are always supported. */
499 const char line_comment_chars[] = "#";
500
501 /* This array holds machine specific line separator characters. */
502 const char line_separator_chars[] = ";";
503
504 /* Chars that can be used to separate mant from exp in floating point nums */
505 const char EXP_CHARS[] = "eE";
506
507 /* Chars that mean this number is a floating point constant */
508 /* As in 0f12.456 */
509 /* or 0d1.2345e12 */
510 const char FLT_CHARS[] = "rRsSfFdDxXpP";
511
512 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
513 changed in read.c . Ideally it shouldn't have to know about it at all,
514 but nothing is ideal around here.
515 */
516
517 static char *insn_error;
518
519 static int auto_align = 1;
520
521 /* When outputting SVR4 PIC code, the assembler needs to know the
522 offset in the stack frame from which to restore the $gp register.
523 This is set by the .cprestore pseudo-op, and saved in this
524 variable. */
525 static offsetT mips_cprestore_offset = -1;
526
527 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
528 more optimizations, it can use a register value instead of a memory-saved
529 offset and even an other register than $gp as global pointer. */
530 static offsetT mips_cpreturn_offset = -1;
531 static int mips_cpreturn_register = -1;
532 static int mips_gp_register = GP;
533 static int mips_gprel_offset = 0;
534
535 /* Whether mips_cprestore_offset has been set in the current function
536 (or whether it has already been warned about, if not). */
537 static int mips_cprestore_valid = 0;
538
539 /* This is the register which holds the stack frame, as set by the
540 .frame pseudo-op. This is needed to implement .cprestore. */
541 static int mips_frame_reg = SP;
542
543 /* Whether mips_frame_reg has been set in the current function
544 (or whether it has already been warned about, if not). */
545 static int mips_frame_reg_valid = 0;
546
547 /* To output NOP instructions correctly, we need to keep information
548 about the previous two instructions. */
549
550 /* Whether we are optimizing. The default value of 2 means to remove
551 unneeded NOPs and swap branch instructions when possible. A value
552 of 1 means to not swap branches. A value of 0 means to always
553 insert NOPs. */
554 static int mips_optimize = 2;
555
556 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
557 equivalent to seeing no -g option at all. */
558 static int mips_debug = 0;
559
560 /* The maximum number of NOPs needed to satisfy a hardware hazard
561 or processor errata. */
562 #define MAX_NOPS 2
563
564 /* A list of previous instructions, with index 0 being the most recent.
565 We need to look back MAX_NOPS instructions when filling delay slots
566 or working around processor errata. We need to look back one
567 instruction further if we're thinking about using history[0] to
568 fill a branch delay slot. */
569 static struct mips_cl_insn history[1 + MAX_NOPS];
570
571 /* Nop instructions used by emit_nop. */
572 static struct mips_cl_insn nop_insn, mips16_nop_insn;
573
574 /* The appropriate nop for the current mode. */
575 #define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
576
577 /* If this is set, it points to a frag holding nop instructions which
578 were inserted before the start of a noreorder section. If those
579 nops turn out to be unnecessary, the size of the frag can be
580 decreased. */
581 static fragS *prev_nop_frag;
582
583 /* The number of nop instructions we created in prev_nop_frag. */
584 static int prev_nop_frag_holds;
585
586 /* The number of nop instructions that we know we need in
587 prev_nop_frag. */
588 static int prev_nop_frag_required;
589
590 /* The number of instructions we've seen since prev_nop_frag. */
591 static int prev_nop_frag_since;
592
593 /* For ECOFF and ELF, relocations against symbols are done in two
594 parts, with a HI relocation and a LO relocation. Each relocation
595 has only 16 bits of space to store an addend. This means that in
596 order for the linker to handle carries correctly, it must be able
597 to locate both the HI and the LO relocation. This means that the
598 relocations must appear in order in the relocation table.
599
600 In order to implement this, we keep track of each unmatched HI
601 relocation. We then sort them so that they immediately precede the
602 corresponding LO relocation. */
603
604 struct mips_hi_fixup
605 {
606 /* Next HI fixup. */
607 struct mips_hi_fixup *next;
608 /* This fixup. */
609 fixS *fixp;
610 /* The section this fixup is in. */
611 segT seg;
612 };
613
614 /* The list of unmatched HI relocs. */
615
616 static struct mips_hi_fixup *mips_hi_fixup_list;
617
618 /* The frag containing the last explicit relocation operator.
619 Null if explicit relocations have not been used. */
620
621 static fragS *prev_reloc_op_frag;
622
623 /* Map normal MIPS register numbers to mips16 register numbers. */
624
625 #define X ILLEGAL_REG
626 static const int mips32_to_16_reg_map[] =
627 {
628 X, X, 2, 3, 4, 5, 6, 7,
629 X, X, X, X, X, X, X, X,
630 0, 1, X, X, X, X, X, X,
631 X, X, X, X, X, X, X, X
632 };
633 #undef X
634
635 /* Map mips16 register numbers to normal MIPS register numbers. */
636
637 static const unsigned int mips16_to_32_reg_map[] =
638 {
639 16, 17, 2, 3, 4, 5, 6, 7
640 };
641
642 /* Classifies the kind of instructions we're interested in when
643 implementing -mfix-vr4120. */
644 enum fix_vr4120_class {
645 FIX_VR4120_MACC,
646 FIX_VR4120_DMACC,
647 FIX_VR4120_MULT,
648 FIX_VR4120_DMULT,
649 FIX_VR4120_DIV,
650 FIX_VR4120_MTHILO,
651 NUM_FIX_VR4120_CLASSES
652 };
653
654 /* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
655 there must be at least one other instruction between an instruction
656 of type X and an instruction of type Y. */
657 static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
658
659 /* True if -mfix-vr4120 is in force. */
660 static int mips_fix_vr4120;
661
662 /* We don't relax branches by default, since this causes us to expand
663 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
664 fail to compute the offset before expanding the macro to the most
665 efficient expansion. */
666
667 static int mips_relax_branch;
668 \f
669 /* The expansion of many macros depends on the type of symbol that
670 they refer to. For example, when generating position-dependent code,
671 a macro that refers to a symbol may have two different expansions,
672 one which uses GP-relative addresses and one which uses absolute
673 addresses. When generating SVR4-style PIC, a macro may have
674 different expansions for local and global symbols.
675
676 We handle these situations by generating both sequences and putting
677 them in variant frags. In position-dependent code, the first sequence
678 will be the GP-relative one and the second sequence will be the
679 absolute one. In SVR4 PIC, the first sequence will be for global
680 symbols and the second will be for local symbols.
681
682 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
683 SECOND are the lengths of the two sequences in bytes. These fields
684 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
685 the subtype has the following flags:
686
687 RELAX_USE_SECOND
688 Set if it has been decided that we should use the second
689 sequence instead of the first.
690
691 RELAX_SECOND_LONGER
692 Set in the first variant frag if the macro's second implementation
693 is longer than its first. This refers to the macro as a whole,
694 not an individual relaxation.
695
696 RELAX_NOMACRO
697 Set in the first variant frag if the macro appeared in a .set nomacro
698 block and if one alternative requires a warning but the other does not.
699
700 RELAX_DELAY_SLOT
701 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
702 delay slot.
703
704 The frag's "opcode" points to the first fixup for relaxable code.
705
706 Relaxable macros are generated using a sequence such as:
707
708 relax_start (SYMBOL);
709 ... generate first expansion ...
710 relax_switch ();
711 ... generate second expansion ...
712 relax_end ();
713
714 The code and fixups for the unwanted alternative are discarded
715 by md_convert_frag. */
716 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
717
718 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
719 #define RELAX_SECOND(X) ((X) & 0xff)
720 #define RELAX_USE_SECOND 0x10000
721 #define RELAX_SECOND_LONGER 0x20000
722 #define RELAX_NOMACRO 0x40000
723 #define RELAX_DELAY_SLOT 0x80000
724
725 /* Branch without likely bit. If label is out of range, we turn:
726
727 beq reg1, reg2, label
728 delay slot
729
730 into
731
732 bne reg1, reg2, 0f
733 nop
734 j label
735 0: delay slot
736
737 with the following opcode replacements:
738
739 beq <-> bne
740 blez <-> bgtz
741 bltz <-> bgez
742 bc1f <-> bc1t
743
744 bltzal <-> bgezal (with jal label instead of j label)
745
746 Even though keeping the delay slot instruction in the delay slot of
747 the branch would be more efficient, it would be very tricky to do
748 correctly, because we'd have to introduce a variable frag *after*
749 the delay slot instruction, and expand that instead. Let's do it
750 the easy way for now, even if the branch-not-taken case now costs
751 one additional instruction. Out-of-range branches are not supposed
752 to be common, anyway.
753
754 Branch likely. If label is out of range, we turn:
755
756 beql reg1, reg2, label
757 delay slot (annulled if branch not taken)
758
759 into
760
761 beql reg1, reg2, 1f
762 nop
763 beql $0, $0, 2f
764 nop
765 1: j[al] label
766 delay slot (executed only if branch taken)
767 2:
768
769 It would be possible to generate a shorter sequence by losing the
770 likely bit, generating something like:
771
772 bne reg1, reg2, 0f
773 nop
774 j[al] label
775 delay slot (executed only if branch taken)
776 0:
777
778 beql -> bne
779 bnel -> beq
780 blezl -> bgtz
781 bgtzl -> blez
782 bltzl -> bgez
783 bgezl -> bltz
784 bc1fl -> bc1t
785 bc1tl -> bc1f
786
787 bltzall -> bgezal (with jal label instead of j label)
788 bgezall -> bltzal (ditto)
789
790
791 but it's not clear that it would actually improve performance. */
792 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
793 ((relax_substateT) \
794 (0xc0000000 \
795 | ((toofar) ? 1 : 0) \
796 | ((link) ? 2 : 0) \
797 | ((likely) ? 4 : 0) \
798 | ((uncond) ? 8 : 0)))
799 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
800 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
801 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
802 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
803 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
804
805 /* For mips16 code, we use an entirely different form of relaxation.
806 mips16 supports two versions of most instructions which take
807 immediate values: a small one which takes some small value, and a
808 larger one which takes a 16 bit value. Since branches also follow
809 this pattern, relaxing these values is required.
810
811 We can assemble both mips16 and normal MIPS code in a single
812 object. Therefore, we need to support this type of relaxation at
813 the same time that we support the relaxation described above. We
814 use the high bit of the subtype field to distinguish these cases.
815
816 The information we store for this type of relaxation is the
817 argument code found in the opcode file for this relocation, whether
818 the user explicitly requested a small or extended form, and whether
819 the relocation is in a jump or jal delay slot. That tells us the
820 size of the value, and how it should be stored. We also store
821 whether the fragment is considered to be extended or not. We also
822 store whether this is known to be a branch to a different section,
823 whether we have tried to relax this frag yet, and whether we have
824 ever extended a PC relative fragment because of a shift count. */
825 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
826 (0x80000000 \
827 | ((type) & 0xff) \
828 | ((small) ? 0x100 : 0) \
829 | ((ext) ? 0x200 : 0) \
830 | ((dslot) ? 0x400 : 0) \
831 | ((jal_dslot) ? 0x800 : 0))
832 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
833 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
834 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
835 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
836 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
837 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
838 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
839 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
840 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
841 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
842 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
843 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
844
845 /* Is the given value a sign-extended 32-bit value? */
846 #define IS_SEXT_32BIT_NUM(x) \
847 (((x) &~ (offsetT) 0x7fffffff) == 0 \
848 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
849
850 /* Is the given value a sign-extended 16-bit value? */
851 #define IS_SEXT_16BIT_NUM(x) \
852 (((x) &~ (offsetT) 0x7fff) == 0 \
853 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
854
855 /* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
856 VALUE << SHIFT. VALUE is evaluated exactly once. */
857 #define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
858 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
859 | (((VALUE) & (MASK)) << (SHIFT)))
860
861 /* Extract bits MASK << SHIFT from STRUCT and shift them right
862 SHIFT places. */
863 #define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
864 (((STRUCT) >> (SHIFT)) & (MASK))
865
866 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
867 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
868
869 include/opcode/mips.h specifies operand fields using the macros
870 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
871 with "MIPS16OP" instead of "OP". */
872 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
873 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
874 #define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
875 INSERT_BITS ((INSN).insn_opcode, VALUE, \
876 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
877
878 /* Extract the operand given by FIELD from mips_cl_insn INSN. */
879 #define EXTRACT_OPERAND(FIELD, INSN) \
880 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
881 #define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
882 EXTRACT_BITS ((INSN).insn_opcode, \
883 MIPS16OP_MASK_##FIELD, \
884 MIPS16OP_SH_##FIELD)
885 \f
886 /* Global variables used when generating relaxable macros. See the
887 comment above RELAX_ENCODE for more details about how relaxation
888 is used. */
889 static struct {
890 /* 0 if we're not emitting a relaxable macro.
891 1 if we're emitting the first of the two relaxation alternatives.
892 2 if we're emitting the second alternative. */
893 int sequence;
894
895 /* The first relaxable fixup in the current frag. (In other words,
896 the first fixup that refers to relaxable code.) */
897 fixS *first_fixup;
898
899 /* sizes[0] says how many bytes of the first alternative are stored in
900 the current frag. Likewise sizes[1] for the second alternative. */
901 unsigned int sizes[2];
902
903 /* The symbol on which the choice of sequence depends. */
904 symbolS *symbol;
905 } mips_relax;
906 \f
907 /* Global variables used to decide whether a macro needs a warning. */
908 static struct {
909 /* True if the macro is in a branch delay slot. */
910 bfd_boolean delay_slot_p;
911
912 /* For relaxable macros, sizes[0] is the length of the first alternative
913 in bytes and sizes[1] is the length of the second alternative.
914 For non-relaxable macros, both elements give the length of the
915 macro in bytes. */
916 unsigned int sizes[2];
917
918 /* The first variant frag for this macro. */
919 fragS *first_frag;
920 } mips_macro_warning;
921 \f
922 /* Prototypes for static functions. */
923
924 #define internalError() \
925 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
926
927 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
928
929 static void append_insn
930 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
931 static void mips_no_prev_insn (int);
932 static void mips16_macro_build
933 (expressionS *, const char *, const char *, va_list);
934 static void load_register (int, expressionS *, int);
935 static void macro_start (void);
936 static void macro_end (void);
937 static void macro (struct mips_cl_insn * ip);
938 static void mips16_macro (struct mips_cl_insn * ip);
939 #ifdef LOSING_COMPILER
940 static void macro2 (struct mips_cl_insn * ip);
941 #endif
942 static void mips_ip (char *str, struct mips_cl_insn * ip);
943 static void mips16_ip (char *str, struct mips_cl_insn * ip);
944 static void mips16_immed
945 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
946 unsigned long *, bfd_boolean *, unsigned short *);
947 static size_t my_getSmallExpression
948 (expressionS *, bfd_reloc_code_real_type *, char *);
949 static void my_getExpression (expressionS *, char *);
950 static void s_align (int);
951 static void s_change_sec (int);
952 static void s_change_section (int);
953 static void s_cons (int);
954 static void s_float_cons (int);
955 static void s_mips_globl (int);
956 static void s_option (int);
957 static void s_mipsset (int);
958 static void s_abicalls (int);
959 static void s_cpload (int);
960 static void s_cpsetup (int);
961 static void s_cplocal (int);
962 static void s_cprestore (int);
963 static void s_cpreturn (int);
964 static void s_gpvalue (int);
965 static void s_gpword (int);
966 static void s_gpdword (int);
967 static void s_cpadd (int);
968 static void s_insn (int);
969 static void md_obj_begin (void);
970 static void md_obj_end (void);
971 static void s_mips_ent (int);
972 static void s_mips_end (int);
973 static void s_mips_frame (int);
974 static void s_mips_mask (int reg_type);
975 static void s_mips_stab (int);
976 static void s_mips_weakext (int);
977 static void s_mips_file (int);
978 static void s_mips_loc (int);
979 static bfd_boolean pic_need_relax (symbolS *, asection *);
980 static int relaxed_branch_length (fragS *, asection *, int);
981 static int validate_mips_insn (const struct mips_opcode *);
982
983 /* Table and functions used to map between CPU/ISA names, and
984 ISA levels, and CPU numbers. */
985
986 struct mips_cpu_info
987 {
988 const char *name; /* CPU or ISA name. */
989 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
990 int isa; /* ISA level. */
991 int cpu; /* CPU number (default CPU if ISA). */
992 };
993
994 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
995 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
996 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
997 \f
998 /* Pseudo-op table.
999
1000 The following pseudo-ops from the Kane and Heinrich MIPS book
1001 should be defined here, but are currently unsupported: .alias,
1002 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1003
1004 The following pseudo-ops from the Kane and Heinrich MIPS book are
1005 specific to the type of debugging information being generated, and
1006 should be defined by the object format: .aent, .begin, .bend,
1007 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1008 .vreg.
1009
1010 The following pseudo-ops from the Kane and Heinrich MIPS book are
1011 not MIPS CPU specific, but are also not specific to the object file
1012 format. This file is probably the best place to define them, but
1013 they are not currently supported: .asm0, .endr, .lab, .repeat,
1014 .struct. */
1015
1016 static const pseudo_typeS mips_pseudo_table[] =
1017 {
1018 /* MIPS specific pseudo-ops. */
1019 {"option", s_option, 0},
1020 {"set", s_mipsset, 0},
1021 {"rdata", s_change_sec, 'r'},
1022 {"sdata", s_change_sec, 's'},
1023 {"livereg", s_ignore, 0},
1024 {"abicalls", s_abicalls, 0},
1025 {"cpload", s_cpload, 0},
1026 {"cpsetup", s_cpsetup, 0},
1027 {"cplocal", s_cplocal, 0},
1028 {"cprestore", s_cprestore, 0},
1029 {"cpreturn", s_cpreturn, 0},
1030 {"gpvalue", s_gpvalue, 0},
1031 {"gpword", s_gpword, 0},
1032 {"gpdword", s_gpdword, 0},
1033 {"cpadd", s_cpadd, 0},
1034 {"insn", s_insn, 0},
1035
1036 /* Relatively generic pseudo-ops that happen to be used on MIPS
1037 chips. */
1038 {"asciiz", stringer, 1},
1039 {"bss", s_change_sec, 'b'},
1040 {"err", s_err, 0},
1041 {"half", s_cons, 1},
1042 {"dword", s_cons, 3},
1043 {"weakext", s_mips_weakext, 0},
1044
1045 /* These pseudo-ops are defined in read.c, but must be overridden
1046 here for one reason or another. */
1047 {"align", s_align, 0},
1048 {"byte", s_cons, 0},
1049 {"data", s_change_sec, 'd'},
1050 {"double", s_float_cons, 'd'},
1051 {"float", s_float_cons, 'f'},
1052 {"globl", s_mips_globl, 0},
1053 {"global", s_mips_globl, 0},
1054 {"hword", s_cons, 1},
1055 {"int", s_cons, 2},
1056 {"long", s_cons, 2},
1057 {"octa", s_cons, 4},
1058 {"quad", s_cons, 3},
1059 {"section", s_change_section, 0},
1060 {"short", s_cons, 1},
1061 {"single", s_float_cons, 'f'},
1062 {"stabn", s_mips_stab, 'n'},
1063 {"text", s_change_sec, 't'},
1064 {"word", s_cons, 2},
1065
1066 { "extern", ecoff_directive_extern, 0},
1067
1068 { NULL, NULL, 0 },
1069 };
1070
1071 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1072 {
1073 /* These pseudo-ops should be defined by the object file format.
1074 However, a.out doesn't support them, so we have versions here. */
1075 {"aent", s_mips_ent, 1},
1076 {"bgnb", s_ignore, 0},
1077 {"end", s_mips_end, 0},
1078 {"endb", s_ignore, 0},
1079 {"ent", s_mips_ent, 0},
1080 {"file", s_mips_file, 0},
1081 {"fmask", s_mips_mask, 'F'},
1082 {"frame", s_mips_frame, 0},
1083 {"loc", s_mips_loc, 0},
1084 {"mask", s_mips_mask, 'R'},
1085 {"verstamp", s_ignore, 0},
1086 { NULL, NULL, 0 },
1087 };
1088
1089 extern void pop_insert (const pseudo_typeS *);
1090
1091 void
1092 mips_pop_insert (void)
1093 {
1094 pop_insert (mips_pseudo_table);
1095 if (! ECOFF_DEBUGGING)
1096 pop_insert (mips_nonecoff_pseudo_table);
1097 }
1098 \f
1099 /* Symbols labelling the current insn. */
1100
1101 struct insn_label_list
1102 {
1103 struct insn_label_list *next;
1104 symbolS *label;
1105 };
1106
1107 static struct insn_label_list *insn_labels;
1108 static struct insn_label_list *free_insn_labels;
1109
1110 static void mips_clear_insn_labels (void);
1111
1112 static inline void
1113 mips_clear_insn_labels (void)
1114 {
1115 register struct insn_label_list **pl;
1116
1117 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1118 ;
1119 *pl = insn_labels;
1120 insn_labels = NULL;
1121 }
1122 \f
1123 static char *expr_end;
1124
1125 /* Expressions which appear in instructions. These are set by
1126 mips_ip. */
1127
1128 static expressionS imm_expr;
1129 static expressionS imm2_expr;
1130 static expressionS offset_expr;
1131
1132 /* Relocs associated with imm_expr and offset_expr. */
1133
1134 static bfd_reloc_code_real_type imm_reloc[3]
1135 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1136 static bfd_reloc_code_real_type offset_reloc[3]
1137 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1138
1139 /* These are set by mips16_ip if an explicit extension is used. */
1140
1141 static bfd_boolean mips16_small, mips16_ext;
1142
1143 #ifdef OBJ_ELF
1144 /* The pdr segment for per procedure frame/regmask info. Not used for
1145 ECOFF debugging. */
1146
1147 static segT pdr_seg;
1148 #endif
1149
1150 /* The default target format to use. */
1151
1152 const char *
1153 mips_target_format (void)
1154 {
1155 switch (OUTPUT_FLAVOR)
1156 {
1157 case bfd_target_ecoff_flavour:
1158 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1159 case bfd_target_coff_flavour:
1160 return "pe-mips";
1161 case bfd_target_elf_flavour:
1162 #ifdef TE_TMIPS
1163 /* This is traditional mips. */
1164 return (target_big_endian
1165 ? (HAVE_64BIT_OBJECTS
1166 ? "elf64-tradbigmips"
1167 : (HAVE_NEWABI
1168 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1169 : (HAVE_64BIT_OBJECTS
1170 ? "elf64-tradlittlemips"
1171 : (HAVE_NEWABI
1172 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1173 #else
1174 return (target_big_endian
1175 ? (HAVE_64BIT_OBJECTS
1176 ? "elf64-bigmips"
1177 : (HAVE_NEWABI
1178 ? "elf32-nbigmips" : "elf32-bigmips"))
1179 : (HAVE_64BIT_OBJECTS
1180 ? "elf64-littlemips"
1181 : (HAVE_NEWABI
1182 ? "elf32-nlittlemips" : "elf32-littlemips")));
1183 #endif
1184 default:
1185 abort ();
1186 return NULL;
1187 }
1188 }
1189
1190 /* Return the length of instruction INSN. */
1191
1192 static inline unsigned int
1193 insn_length (const struct mips_cl_insn *insn)
1194 {
1195 if (!mips_opts.mips16)
1196 return 4;
1197 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1198 }
1199
1200 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1201
1202 static void
1203 create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1204 {
1205 size_t i;
1206
1207 insn->insn_mo = mo;
1208 insn->use_extend = FALSE;
1209 insn->extend = 0;
1210 insn->insn_opcode = mo->match;
1211 insn->frag = NULL;
1212 insn->where = 0;
1213 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1214 insn->fixp[i] = NULL;
1215 insn->fixed_p = (mips_opts.noreorder > 0);
1216 insn->noreorder_p = (mips_opts.noreorder > 0);
1217 insn->mips16_absolute_jump_p = 0;
1218 }
1219
1220 /* Install INSN at the location specified by its "frag" and "where" fields. */
1221
1222 static void
1223 install_insn (const struct mips_cl_insn *insn)
1224 {
1225 char *f = insn->frag->fr_literal + insn->where;
1226 if (!mips_opts.mips16)
1227 md_number_to_chars (f, insn->insn_opcode, 4);
1228 else if (insn->mips16_absolute_jump_p)
1229 {
1230 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1231 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1232 }
1233 else
1234 {
1235 if (insn->use_extend)
1236 {
1237 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1238 f += 2;
1239 }
1240 md_number_to_chars (f, insn->insn_opcode, 2);
1241 }
1242 }
1243
1244 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1245 and install the opcode in the new location. */
1246
1247 static void
1248 move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1249 {
1250 size_t i;
1251
1252 insn->frag = frag;
1253 insn->where = where;
1254 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1255 if (insn->fixp[i] != NULL)
1256 {
1257 insn->fixp[i]->fx_frag = frag;
1258 insn->fixp[i]->fx_where = where;
1259 }
1260 install_insn (insn);
1261 }
1262
1263 /* Add INSN to the end of the output. */
1264
1265 static void
1266 add_fixed_insn (struct mips_cl_insn *insn)
1267 {
1268 char *f = frag_more (insn_length (insn));
1269 move_insn (insn, frag_now, f - frag_now->fr_literal);
1270 }
1271
1272 /* Start a variant frag and move INSN to the start of the variant part,
1273 marking it as fixed. The other arguments are as for frag_var. */
1274
1275 static void
1276 add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1277 relax_substateT subtype, symbolS *symbol, offsetT offset)
1278 {
1279 frag_grow (max_chars);
1280 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1281 insn->fixed_p = 1;
1282 frag_var (rs_machine_dependent, max_chars, var,
1283 subtype, symbol, offset, NULL);
1284 }
1285
1286 /* Insert N copies of INSN into the history buffer, starting at
1287 position FIRST. Neither FIRST nor N need to be clipped. */
1288
1289 static void
1290 insert_into_history (unsigned int first, unsigned int n,
1291 const struct mips_cl_insn *insn)
1292 {
1293 if (mips_relax.sequence != 2)
1294 {
1295 unsigned int i;
1296
1297 for (i = ARRAY_SIZE (history); i-- > first;)
1298 if (i >= first + n)
1299 history[i] = history[i - n];
1300 else
1301 history[i] = *insn;
1302 }
1303 }
1304
1305 /* Emit a nop instruction, recording it in the history buffer. */
1306
1307 static void
1308 emit_nop (void)
1309 {
1310 add_fixed_insn (NOP_INSN);
1311 insert_into_history (0, 1, NOP_INSN);
1312 }
1313
1314 /* Initialize vr4120_conflicts. There is a bit of duplication here:
1315 the idea is to make it obvious at a glance that each errata is
1316 included. */
1317
1318 static void
1319 init_vr4120_conflicts (void)
1320 {
1321 #define CONFLICT(FIRST, SECOND) \
1322 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1323
1324 /* Errata 21 - [D]DIV[U] after [D]MACC */
1325 CONFLICT (MACC, DIV);
1326 CONFLICT (DMACC, DIV);
1327
1328 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1329 CONFLICT (DMULT, DMULT);
1330 CONFLICT (DMULT, DMACC);
1331 CONFLICT (DMACC, DMULT);
1332 CONFLICT (DMACC, DMACC);
1333
1334 /* Errata 24 - MT{LO,HI} after [D]MACC */
1335 CONFLICT (MACC, MTHILO);
1336 CONFLICT (DMACC, MTHILO);
1337
1338 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1339 instruction is executed immediately after a MACC or DMACC
1340 instruction, the result of [either instruction] is incorrect." */
1341 CONFLICT (MACC, MULT);
1342 CONFLICT (MACC, DMULT);
1343 CONFLICT (DMACC, MULT);
1344 CONFLICT (DMACC, DMULT);
1345
1346 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1347 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1348 DDIV or DDIVU instruction, the result of the MACC or
1349 DMACC instruction is incorrect.". */
1350 CONFLICT (DMULT, MACC);
1351 CONFLICT (DMULT, DMACC);
1352 CONFLICT (DIV, MACC);
1353 CONFLICT (DIV, DMACC);
1354
1355 #undef CONFLICT
1356 }
1357
1358 /* This function is called once, at assembler startup time. It should
1359 set up all the tables, etc. that the MD part of the assembler will need. */
1360
1361 void
1362 md_begin (void)
1363 {
1364 register const char *retval = NULL;
1365 int i = 0;
1366 int broken = 0;
1367
1368 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1369 as_warn (_("Could not set architecture and machine"));
1370
1371 op_hash = hash_new ();
1372
1373 for (i = 0; i < NUMOPCODES;)
1374 {
1375 const char *name = mips_opcodes[i].name;
1376
1377 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1378 if (retval != NULL)
1379 {
1380 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1381 mips_opcodes[i].name, retval);
1382 /* Probably a memory allocation problem? Give up now. */
1383 as_fatal (_("Broken assembler. No assembly attempted."));
1384 }
1385 do
1386 {
1387 if (mips_opcodes[i].pinfo != INSN_MACRO)
1388 {
1389 if (!validate_mips_insn (&mips_opcodes[i]))
1390 broken = 1;
1391 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1392 {
1393 create_insn (&nop_insn, mips_opcodes + i);
1394 nop_insn.fixed_p = 1;
1395 }
1396 }
1397 ++i;
1398 }
1399 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1400 }
1401
1402 mips16_op_hash = hash_new ();
1403
1404 i = 0;
1405 while (i < bfd_mips16_num_opcodes)
1406 {
1407 const char *name = mips16_opcodes[i].name;
1408
1409 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1410 if (retval != NULL)
1411 as_fatal (_("internal: can't hash `%s': %s"),
1412 mips16_opcodes[i].name, retval);
1413 do
1414 {
1415 if (mips16_opcodes[i].pinfo != INSN_MACRO
1416 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1417 != mips16_opcodes[i].match))
1418 {
1419 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1420 mips16_opcodes[i].name, mips16_opcodes[i].args);
1421 broken = 1;
1422 }
1423 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1424 {
1425 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1426 mips16_nop_insn.fixed_p = 1;
1427 }
1428 ++i;
1429 }
1430 while (i < bfd_mips16_num_opcodes
1431 && strcmp (mips16_opcodes[i].name, name) == 0);
1432 }
1433
1434 if (broken)
1435 as_fatal (_("Broken assembler. No assembly attempted."));
1436
1437 /* We add all the general register names to the symbol table. This
1438 helps us detect invalid uses of them. */
1439 for (i = 0; i < 32; i++)
1440 {
1441 char buf[5];
1442
1443 sprintf (buf, "$%d", i);
1444 symbol_table_insert (symbol_new (buf, reg_section, i,
1445 &zero_address_frag));
1446 }
1447 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1448 &zero_address_frag));
1449 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1450 &zero_address_frag));
1451 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1452 &zero_address_frag));
1453 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1454 &zero_address_frag));
1455 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1456 &zero_address_frag));
1457 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1458 &zero_address_frag));
1459 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1460 &zero_address_frag));
1461 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1462 &zero_address_frag));
1463 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1464 &zero_address_frag));
1465
1466 /* If we don't add these register names to the symbol table, they
1467 may end up being added as regular symbols by operand(), and then
1468 make it to the object file as undefined in case they're not
1469 regarded as local symbols. They're local in o32, since `$' is a
1470 local symbol prefix, but not in n32 or n64. */
1471 for (i = 0; i < 8; i++)
1472 {
1473 char buf[6];
1474
1475 sprintf (buf, "$fcc%i", i);
1476 symbol_table_insert (symbol_new (buf, reg_section, -1,
1477 &zero_address_frag));
1478 }
1479
1480 mips_no_prev_insn (FALSE);
1481
1482 mips_gprmask = 0;
1483 mips_cprmask[0] = 0;
1484 mips_cprmask[1] = 0;
1485 mips_cprmask[2] = 0;
1486 mips_cprmask[3] = 0;
1487
1488 /* set the default alignment for the text section (2**2) */
1489 record_alignment (text_section, 2);
1490
1491 bfd_set_gp_size (stdoutput, g_switch_value);
1492
1493 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1494 {
1495 /* On a native system, sections must be aligned to 16 byte
1496 boundaries. When configured for an embedded ELF target, we
1497 don't bother. */
1498 if (strcmp (TARGET_OS, "elf") != 0)
1499 {
1500 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1501 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1502 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1503 }
1504
1505 /* Create a .reginfo section for register masks and a .mdebug
1506 section for debugging information. */
1507 {
1508 segT seg;
1509 subsegT subseg;
1510 flagword flags;
1511 segT sec;
1512
1513 seg = now_seg;
1514 subseg = now_subseg;
1515
1516 /* The ABI says this section should be loaded so that the
1517 running program can access it. However, we don't load it
1518 if we are configured for an embedded target */
1519 flags = SEC_READONLY | SEC_DATA;
1520 if (strcmp (TARGET_OS, "elf") != 0)
1521 flags |= SEC_ALLOC | SEC_LOAD;
1522
1523 if (mips_abi != N64_ABI)
1524 {
1525 sec = subseg_new (".reginfo", (subsegT) 0);
1526
1527 bfd_set_section_flags (stdoutput, sec, flags);
1528 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1529
1530 #ifdef OBJ_ELF
1531 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1532 #endif
1533 }
1534 else
1535 {
1536 /* The 64-bit ABI uses a .MIPS.options section rather than
1537 .reginfo section. */
1538 sec = subseg_new (".MIPS.options", (subsegT) 0);
1539 bfd_set_section_flags (stdoutput, sec, flags);
1540 bfd_set_section_alignment (stdoutput, sec, 3);
1541
1542 #ifdef OBJ_ELF
1543 /* Set up the option header. */
1544 {
1545 Elf_Internal_Options opthdr;
1546 char *f;
1547
1548 opthdr.kind = ODK_REGINFO;
1549 opthdr.size = (sizeof (Elf_External_Options)
1550 + sizeof (Elf64_External_RegInfo));
1551 opthdr.section = 0;
1552 opthdr.info = 0;
1553 f = frag_more (sizeof (Elf_External_Options));
1554 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1555 (Elf_External_Options *) f);
1556
1557 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1558 }
1559 #endif
1560 }
1561
1562 if (ECOFF_DEBUGGING)
1563 {
1564 sec = subseg_new (".mdebug", (subsegT) 0);
1565 (void) bfd_set_section_flags (stdoutput, sec,
1566 SEC_HAS_CONTENTS | SEC_READONLY);
1567 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1568 }
1569 #ifdef OBJ_ELF
1570 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1571 {
1572 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1573 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1574 SEC_READONLY | SEC_RELOC
1575 | SEC_DEBUGGING);
1576 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1577 }
1578 #endif
1579
1580 subseg_set (seg, subseg);
1581 }
1582 }
1583
1584 if (! ECOFF_DEBUGGING)
1585 md_obj_begin ();
1586
1587 if (mips_fix_vr4120)
1588 init_vr4120_conflicts ();
1589 }
1590
1591 void
1592 md_mips_end (void)
1593 {
1594 if (! ECOFF_DEBUGGING)
1595 md_obj_end ();
1596 }
1597
1598 void
1599 md_assemble (char *str)
1600 {
1601 struct mips_cl_insn insn;
1602 bfd_reloc_code_real_type unused_reloc[3]
1603 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1604
1605 imm_expr.X_op = O_absent;
1606 imm2_expr.X_op = O_absent;
1607 offset_expr.X_op = O_absent;
1608 imm_reloc[0] = BFD_RELOC_UNUSED;
1609 imm_reloc[1] = BFD_RELOC_UNUSED;
1610 imm_reloc[2] = BFD_RELOC_UNUSED;
1611 offset_reloc[0] = BFD_RELOC_UNUSED;
1612 offset_reloc[1] = BFD_RELOC_UNUSED;
1613 offset_reloc[2] = BFD_RELOC_UNUSED;
1614
1615 if (mips_opts.mips16)
1616 mips16_ip (str, &insn);
1617 else
1618 {
1619 mips_ip (str, &insn);
1620 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1621 str, insn.insn_opcode));
1622 }
1623
1624 if (insn_error)
1625 {
1626 as_bad ("%s `%s'", insn_error, str);
1627 return;
1628 }
1629
1630 if (insn.insn_mo->pinfo == INSN_MACRO)
1631 {
1632 macro_start ();
1633 if (mips_opts.mips16)
1634 mips16_macro (&insn);
1635 else
1636 macro (&insn);
1637 macro_end ();
1638 }
1639 else
1640 {
1641 if (imm_expr.X_op != O_absent)
1642 append_insn (&insn, &imm_expr, imm_reloc);
1643 else if (offset_expr.X_op != O_absent)
1644 append_insn (&insn, &offset_expr, offset_reloc);
1645 else
1646 append_insn (&insn, NULL, unused_reloc);
1647 }
1648 }
1649
1650 /* Return true if the given relocation might need a matching %lo().
1651 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1652 applied to local symbols. */
1653
1654 static inline bfd_boolean
1655 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1656 {
1657 return (HAVE_IN_PLACE_ADDENDS
1658 && (reloc == BFD_RELOC_HI16_S
1659 || reloc == BFD_RELOC_MIPS_GOT16
1660 || reloc == BFD_RELOC_MIPS16_HI16_S));
1661 }
1662
1663 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1664 relocation. */
1665
1666 static inline bfd_boolean
1667 fixup_has_matching_lo_p (fixS *fixp)
1668 {
1669 return (fixp->fx_next != NULL
1670 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1671 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1672 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1673 && fixp->fx_offset == fixp->fx_next->fx_offset);
1674 }
1675
1676 /* See whether instruction IP reads register REG. CLASS is the type
1677 of register. */
1678
1679 static int
1680 insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
1681 enum mips_regclass class)
1682 {
1683 if (class == MIPS16_REG)
1684 {
1685 assert (mips_opts.mips16);
1686 reg = mips16_to_32_reg_map[reg];
1687 class = MIPS_GR_REG;
1688 }
1689
1690 /* Don't report on general register ZERO, since it never changes. */
1691 if (class == MIPS_GR_REG && reg == ZERO)
1692 return 0;
1693
1694 if (class == MIPS_FP_REG)
1695 {
1696 assert (! mips_opts.mips16);
1697 /* If we are called with either $f0 or $f1, we must check $f0.
1698 This is not optimal, because it will introduce an unnecessary
1699 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1700 need to distinguish reading both $f0 and $f1 or just one of
1701 them. Note that we don't have to check the other way,
1702 because there is no instruction that sets both $f0 and $f1
1703 and requires a delay. */
1704 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1705 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
1706 == (reg &~ (unsigned) 1)))
1707 return 1;
1708 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1709 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
1710 == (reg &~ (unsigned) 1)))
1711 return 1;
1712 }
1713 else if (! mips_opts.mips16)
1714 {
1715 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1716 && EXTRACT_OPERAND (RS, *ip) == reg)
1717 return 1;
1718 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1719 && EXTRACT_OPERAND (RT, *ip) == reg)
1720 return 1;
1721 }
1722 else
1723 {
1724 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1725 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
1726 return 1;
1727 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1728 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
1729 return 1;
1730 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1731 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
1732 == reg))
1733 return 1;
1734 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1735 return 1;
1736 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1737 return 1;
1738 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1739 return 1;
1740 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1741 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
1742 return 1;
1743 }
1744
1745 return 0;
1746 }
1747
1748 /* This function returns true if modifying a register requires a
1749 delay. */
1750
1751 static int
1752 reg_needs_delay (unsigned int reg)
1753 {
1754 unsigned long prev_pinfo;
1755
1756 prev_pinfo = history[0].insn_mo->pinfo;
1757 if (! mips_opts.noreorder
1758 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1759 && ! gpr_interlocks)
1760 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1761 && ! cop_interlocks)))
1762 {
1763 /* A load from a coprocessor or from memory. All load delays
1764 delay the use of general register rt for one instruction. */
1765 /* Itbl support may require additional care here. */
1766 know (prev_pinfo & INSN_WRITE_GPR_T);
1767 if (reg == EXTRACT_OPERAND (RT, history[0]))
1768 return 1;
1769 }
1770
1771 return 0;
1772 }
1773
1774 /* Move all labels in insn_labels to the current insertion point. */
1775
1776 static void
1777 mips_move_labels (void)
1778 {
1779 struct insn_label_list *l;
1780 valueT val;
1781
1782 for (l = insn_labels; l != NULL; l = l->next)
1783 {
1784 assert (S_GET_SEGMENT (l->label) == now_seg);
1785 symbol_set_frag (l->label, frag_now);
1786 val = (valueT) frag_now_fix ();
1787 /* mips16 text labels are stored as odd. */
1788 if (mips_opts.mips16)
1789 ++val;
1790 S_SET_VALUE (l->label, val);
1791 }
1792 }
1793
1794 /* Mark instruction labels in mips16 mode. This permits the linker to
1795 handle them specially, such as generating jalx instructions when
1796 needed. We also make them odd for the duration of the assembly, in
1797 order to generate the right sort of code. We will make them even
1798 in the adjust_symtab routine, while leaving them marked. This is
1799 convenient for the debugger and the disassembler. The linker knows
1800 to make them odd again. */
1801
1802 static void
1803 mips16_mark_labels (void)
1804 {
1805 if (mips_opts.mips16)
1806 {
1807 struct insn_label_list *l;
1808 valueT val;
1809
1810 for (l = insn_labels; l != NULL; l = l->next)
1811 {
1812 #ifdef OBJ_ELF
1813 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1814 S_SET_OTHER (l->label, STO_MIPS16);
1815 #endif
1816 val = S_GET_VALUE (l->label);
1817 if ((val & 1) == 0)
1818 S_SET_VALUE (l->label, val + 1);
1819 }
1820 }
1821 }
1822
1823 /* End the current frag. Make it a variant frag and record the
1824 relaxation info. */
1825
1826 static void
1827 relax_close_frag (void)
1828 {
1829 mips_macro_warning.first_frag = frag_now;
1830 frag_var (rs_machine_dependent, 0, 0,
1831 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1832 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1833
1834 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1835 mips_relax.first_fixup = 0;
1836 }
1837
1838 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1839 See the comment above RELAX_ENCODE for more details. */
1840
1841 static void
1842 relax_start (symbolS *symbol)
1843 {
1844 assert (mips_relax.sequence == 0);
1845 mips_relax.sequence = 1;
1846 mips_relax.symbol = symbol;
1847 }
1848
1849 /* Start generating the second version of a relaxable sequence.
1850 See the comment above RELAX_ENCODE for more details. */
1851
1852 static void
1853 relax_switch (void)
1854 {
1855 assert (mips_relax.sequence == 1);
1856 mips_relax.sequence = 2;
1857 }
1858
1859 /* End the current relaxable sequence. */
1860
1861 static void
1862 relax_end (void)
1863 {
1864 assert (mips_relax.sequence == 2);
1865 relax_close_frag ();
1866 mips_relax.sequence = 0;
1867 }
1868
1869 /* Classify an instruction according to the FIX_VR4120_* enumeration.
1870 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1871 by VR4120 errata. */
1872
1873 static unsigned int
1874 classify_vr4120_insn (const char *name)
1875 {
1876 if (strncmp (name, "macc", 4) == 0)
1877 return FIX_VR4120_MACC;
1878 if (strncmp (name, "dmacc", 5) == 0)
1879 return FIX_VR4120_DMACC;
1880 if (strncmp (name, "mult", 4) == 0)
1881 return FIX_VR4120_MULT;
1882 if (strncmp (name, "dmult", 5) == 0)
1883 return FIX_VR4120_DMULT;
1884 if (strstr (name, "div"))
1885 return FIX_VR4120_DIV;
1886 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1887 return FIX_VR4120_MTHILO;
1888 return NUM_FIX_VR4120_CLASSES;
1889 }
1890
1891 /* Return the number of instructions that must separate INSN1 and INSN2,
1892 where INSN1 is the earlier instruction. Return the worst-case value
1893 for any INSN2 if INSN2 is null. */
1894
1895 static unsigned int
1896 insns_between (const struct mips_cl_insn *insn1,
1897 const struct mips_cl_insn *insn2)
1898 {
1899 unsigned long pinfo1, pinfo2;
1900
1901 /* This function needs to know which pinfo flags are set for INSN2
1902 and which registers INSN2 uses. The former is stored in PINFO2 and
1903 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
1904 will have every flag set and INSN2_USES_REG will always return true. */
1905 pinfo1 = insn1->insn_mo->pinfo;
1906 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
1907
1908 #define INSN2_USES_REG(REG, CLASS) \
1909 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1910
1911 /* For most targets, write-after-read dependencies on the HI and LO
1912 registers must be separated by at least two instructions. */
1913 if (!hilo_interlocks)
1914 {
1915 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1916 return 2;
1917 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1918 return 2;
1919 }
1920
1921 /* If we're working around r7000 errata, there must be two instructions
1922 between an mfhi or mflo and any instruction that uses the result. */
1923 if (mips_7000_hilo_fix
1924 && MF_HILO_INSN (pinfo1)
1925 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1926 return 2;
1927
1928 /* If working around VR4120 errata, check for combinations that need
1929 a single intervening instruction. */
1930 if (mips_fix_vr4120)
1931 {
1932 unsigned int class1, class2;
1933
1934 class1 = classify_vr4120_insn (insn1->insn_mo->name);
1935 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
1936 {
1937 if (insn2 == NULL)
1938 return 1;
1939 class2 = classify_vr4120_insn (insn2->insn_mo->name);
1940 if (vr4120_conflicts[class1] & (1 << class2))
1941 return 1;
1942 }
1943 }
1944
1945 if (!mips_opts.mips16)
1946 {
1947 /* Check for GPR or coprocessor load delays. All such delays
1948 are on the RT register. */
1949 /* Itbl support may require additional care here. */
1950 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
1951 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
1952 {
1953 know (pinfo1 & INSN_WRITE_GPR_T);
1954 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
1955 return 1;
1956 }
1957
1958 /* Check for generic coprocessor hazards.
1959
1960 This case is not handled very well. There is no special
1961 knowledge of CP0 handling, and the coprocessors other than
1962 the floating point unit are not distinguished at all. */
1963 /* Itbl support may require additional care here. FIXME!
1964 Need to modify this to include knowledge about
1965 user specified delays! */
1966 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
1967 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
1968 {
1969 /* Handle cases where INSN1 writes to a known general coprocessor
1970 register. There must be a one instruction delay before INSN2
1971 if INSN2 reads that register, otherwise no delay is needed. */
1972 if (pinfo1 & INSN_WRITE_FPR_T)
1973 {
1974 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
1975 return 1;
1976 }
1977 else if (pinfo1 & INSN_WRITE_FPR_S)
1978 {
1979 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
1980 return 1;
1981 }
1982 else
1983 {
1984 /* Read-after-write dependencies on the control registers
1985 require a two-instruction gap. */
1986 if ((pinfo1 & INSN_WRITE_COND_CODE)
1987 && (pinfo2 & INSN_READ_COND_CODE))
1988 return 2;
1989
1990 /* We don't know exactly what INSN1 does. If INSN2 is
1991 also a coprocessor instruction, assume there must be
1992 a one instruction gap. */
1993 if (pinfo2 & INSN_COP)
1994 return 1;
1995 }
1996 }
1997
1998 /* Check for read-after-write dependencies on the coprocessor
1999 control registers in cases where INSN1 does not need a general
2000 coprocessor delay. This means that INSN1 is a floating point
2001 comparison instruction. */
2002 /* Itbl support may require additional care here. */
2003 else if (!cop_interlocks
2004 && (pinfo1 & INSN_WRITE_COND_CODE)
2005 && (pinfo2 & INSN_READ_COND_CODE))
2006 return 1;
2007 }
2008
2009 #undef INSN2_USES_REG
2010
2011 return 0;
2012 }
2013
2014 /* Return the number of nops that would be needed if instruction INSN
2015 immediately followed the MAX_NOPS instructions given by HISTORY,
2016 where HISTORY[0] is the most recent instruction. If INSN is null,
2017 return the worse-case number of nops for any instruction. */
2018
2019 static int
2020 nops_for_insn (const struct mips_cl_insn *history,
2021 const struct mips_cl_insn *insn)
2022 {
2023 int i, nops, tmp_nops;
2024
2025 nops = 0;
2026 for (i = 0; i < MAX_NOPS; i++)
2027 if (!history[i].noreorder_p)
2028 {
2029 tmp_nops = insns_between (history + i, insn) - i;
2030 if (tmp_nops > nops)
2031 nops = tmp_nops;
2032 }
2033 return nops;
2034 }
2035
2036 /* The variable arguments provide NUM_INSNS extra instructions that
2037 might be added to HISTORY. Return the largest number of nops that
2038 would be needed after the extended sequence. */
2039
2040 static int
2041 nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2042 {
2043 va_list args;
2044 struct mips_cl_insn buffer[MAX_NOPS];
2045 struct mips_cl_insn *cursor;
2046 int nops;
2047
2048 va_start (args, history);
2049 cursor = buffer + num_insns;
2050 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2051 while (cursor > buffer)
2052 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2053
2054 nops = nops_for_insn (buffer, NULL);
2055 va_end (args);
2056 return nops;
2057 }
2058
2059 /* Like nops_for_insn, but if INSN is a branch, take into account the
2060 worst-case delay for the branch target. */
2061
2062 static int
2063 nops_for_insn_or_target (const struct mips_cl_insn *history,
2064 const struct mips_cl_insn *insn)
2065 {
2066 int nops, tmp_nops;
2067
2068 nops = nops_for_insn (history, insn);
2069 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2070 | INSN_COND_BRANCH_DELAY
2071 | INSN_COND_BRANCH_LIKELY))
2072 {
2073 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2074 if (tmp_nops > nops)
2075 nops = tmp_nops;
2076 }
2077 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2078 {
2079 tmp_nops = nops_for_sequence (1, history, insn);
2080 if (tmp_nops > nops)
2081 nops = tmp_nops;
2082 }
2083 return nops;
2084 }
2085
2086 /* Output an instruction. IP is the instruction information.
2087 ADDRESS_EXPR is an operand of the instruction to be used with
2088 RELOC_TYPE. */
2089
2090 static void
2091 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2092 bfd_reloc_code_real_type *reloc_type)
2093 {
2094 register unsigned long prev_pinfo, pinfo;
2095 relax_stateT prev_insn_frag_type = 0;
2096 bfd_boolean relaxed_branch = FALSE;
2097
2098 /* Mark instruction labels in mips16 mode. */
2099 mips16_mark_labels ();
2100
2101 prev_pinfo = history[0].insn_mo->pinfo;
2102 pinfo = ip->insn_mo->pinfo;
2103
2104 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2105 {
2106 /* There are a lot of optimizations we could do that we don't.
2107 In particular, we do not, in general, reorder instructions.
2108 If you use gcc with optimization, it will reorder
2109 instructions and generally do much more optimization then we
2110 do here; repeating all that work in the assembler would only
2111 benefit hand written assembly code, and does not seem worth
2112 it. */
2113 int nops = (mips_optimize == 0
2114 ? nops_for_insn (history, NULL)
2115 : nops_for_insn_or_target (history, ip));
2116 if (nops > 0)
2117 {
2118 fragS *old_frag;
2119 unsigned long old_frag_offset;
2120 int i;
2121
2122 old_frag = frag_now;
2123 old_frag_offset = frag_now_fix ();
2124
2125 for (i = 0; i < nops; i++)
2126 emit_nop ();
2127
2128 if (listing)
2129 {
2130 listing_prev_line ();
2131 /* We may be at the start of a variant frag. In case we
2132 are, make sure there is enough space for the frag
2133 after the frags created by listing_prev_line. The
2134 argument to frag_grow here must be at least as large
2135 as the argument to all other calls to frag_grow in
2136 this file. We don't have to worry about being in the
2137 middle of a variant frag, because the variants insert
2138 all needed nop instructions themselves. */
2139 frag_grow (40);
2140 }
2141
2142 mips_move_labels ();
2143
2144 #ifndef NO_ECOFF_DEBUGGING
2145 if (ECOFF_DEBUGGING)
2146 ecoff_fix_loc (old_frag, old_frag_offset);
2147 #endif
2148 }
2149 }
2150 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2151 {
2152 /* Work out how many nops in prev_nop_frag are needed by IP. */
2153 int nops = nops_for_insn_or_target (history, ip);
2154 assert (nops <= prev_nop_frag_holds);
2155
2156 /* Enforce NOPS as a minimum. */
2157 if (nops > prev_nop_frag_required)
2158 prev_nop_frag_required = nops;
2159
2160 if (prev_nop_frag_holds == prev_nop_frag_required)
2161 {
2162 /* Settle for the current number of nops. Update the history
2163 accordingly (for the benefit of any future .set reorder code). */
2164 prev_nop_frag = NULL;
2165 insert_into_history (prev_nop_frag_since,
2166 prev_nop_frag_holds, NOP_INSN);
2167 }
2168 else
2169 {
2170 /* Allow this instruction to replace one of the nops that was
2171 tentatively added to prev_nop_frag. */
2172 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2173 prev_nop_frag_holds--;
2174 prev_nop_frag_since++;
2175 }
2176 }
2177
2178 #ifdef OBJ_ELF
2179 /* The value passed to dwarf2_emit_insn is the distance between
2180 the beginning of the current instruction and the address that
2181 should be recorded in the debug tables. For MIPS16 debug info
2182 we want to use ISA-encoded addresses, so we pass -1 for an
2183 address higher by one than the current. */
2184 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2185 #endif
2186
2187 /* Record the frag type before frag_var. */
2188 if (history[0].frag)
2189 prev_insn_frag_type = history[0].frag->fr_type;
2190
2191 if (address_expr
2192 && *reloc_type == BFD_RELOC_16_PCREL_S2
2193 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2194 || pinfo & INSN_COND_BRANCH_LIKELY)
2195 && mips_relax_branch
2196 /* Don't try branch relaxation within .set nomacro, or within
2197 .set noat if we use $at for PIC computations. If it turns
2198 out that the branch was out-of-range, we'll get an error. */
2199 && !mips_opts.warn_about_macros
2200 && !(mips_opts.noat && mips_pic != NO_PIC)
2201 && !mips_opts.mips16)
2202 {
2203 relaxed_branch = TRUE;
2204 add_relaxed_insn (ip, (relaxed_branch_length
2205 (NULL, NULL,
2206 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2207 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2208 : 0)), 4,
2209 RELAX_BRANCH_ENCODE
2210 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2211 pinfo & INSN_COND_BRANCH_LIKELY,
2212 pinfo & INSN_WRITE_GPR_31,
2213 0),
2214 address_expr->X_add_symbol,
2215 address_expr->X_add_number);
2216 *reloc_type = BFD_RELOC_UNUSED;
2217 }
2218 else if (*reloc_type > BFD_RELOC_UNUSED)
2219 {
2220 /* We need to set up a variant frag. */
2221 assert (mips_opts.mips16 && address_expr != NULL);
2222 add_relaxed_insn (ip, 4, 0,
2223 RELAX_MIPS16_ENCODE
2224 (*reloc_type - BFD_RELOC_UNUSED,
2225 mips16_small, mips16_ext,
2226 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2227 history[0].mips16_absolute_jump_p),
2228 make_expr_symbol (address_expr), 0);
2229 }
2230 else if (mips_opts.mips16
2231 && ! ip->use_extend
2232 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2233 {
2234 /* Make sure there is enough room to swap this instruction with
2235 a following jump instruction. */
2236 frag_grow (6);
2237 add_fixed_insn (ip);
2238 }
2239 else
2240 {
2241 if (mips_opts.mips16
2242 && mips_opts.noreorder
2243 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2244 as_warn (_("extended instruction in delay slot"));
2245
2246 if (mips_relax.sequence)
2247 {
2248 /* If we've reached the end of this frag, turn it into a variant
2249 frag and record the information for the instructions we've
2250 written so far. */
2251 if (frag_room () < 4)
2252 relax_close_frag ();
2253 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2254 }
2255
2256 if (mips_relax.sequence != 2)
2257 mips_macro_warning.sizes[0] += 4;
2258 if (mips_relax.sequence != 1)
2259 mips_macro_warning.sizes[1] += 4;
2260
2261 if (mips_opts.mips16)
2262 {
2263 ip->fixed_p = 1;
2264 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2265 }
2266 add_fixed_insn (ip);
2267 }
2268
2269 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2270 {
2271 if (address_expr->X_op == O_constant)
2272 {
2273 unsigned int tmp;
2274
2275 switch (*reloc_type)
2276 {
2277 case BFD_RELOC_32:
2278 ip->insn_opcode |= address_expr->X_add_number;
2279 break;
2280
2281 case BFD_RELOC_MIPS_HIGHEST:
2282 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2283 ip->insn_opcode |= tmp & 0xffff;
2284 break;
2285
2286 case BFD_RELOC_MIPS_HIGHER:
2287 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2288 ip->insn_opcode |= tmp & 0xffff;
2289 break;
2290
2291 case BFD_RELOC_HI16_S:
2292 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2293 ip->insn_opcode |= tmp & 0xffff;
2294 break;
2295
2296 case BFD_RELOC_HI16:
2297 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2298 break;
2299
2300 case BFD_RELOC_UNUSED:
2301 case BFD_RELOC_LO16:
2302 case BFD_RELOC_MIPS_GOT_DISP:
2303 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2304 break;
2305
2306 case BFD_RELOC_MIPS_JMP:
2307 if ((address_expr->X_add_number & 3) != 0)
2308 as_bad (_("jump to misaligned address (0x%lx)"),
2309 (unsigned long) address_expr->X_add_number);
2310 if (address_expr->X_add_number & ~0xfffffff)
2311 as_bad (_("jump address range overflow (0x%lx)"),
2312 (unsigned long) address_expr->X_add_number);
2313 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2314 break;
2315
2316 case BFD_RELOC_MIPS16_JMP:
2317 if ((address_expr->X_add_number & 3) != 0)
2318 as_bad (_("jump to misaligned address (0x%lx)"),
2319 (unsigned long) address_expr->X_add_number);
2320 if (address_expr->X_add_number & ~0xfffffff)
2321 as_bad (_("jump address range overflow (0x%lx)"),
2322 (unsigned long) address_expr->X_add_number);
2323 ip->insn_opcode |=
2324 (((address_expr->X_add_number & 0x7c0000) << 3)
2325 | ((address_expr->X_add_number & 0xf800000) >> 7)
2326 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2327 break;
2328
2329 case BFD_RELOC_16_PCREL_S2:
2330 goto need_reloc;
2331
2332 default:
2333 internalError ();
2334 }
2335 }
2336 else if (*reloc_type < BFD_RELOC_UNUSED)
2337 need_reloc:
2338 {
2339 reloc_howto_type *howto;
2340 int i;
2341
2342 /* In a compound relocation, it is the final (outermost)
2343 operator that determines the relocated field. */
2344 for (i = 1; i < 3; i++)
2345 if (reloc_type[i] == BFD_RELOC_UNUSED)
2346 break;
2347
2348 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2349 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2350 bfd_get_reloc_size (howto),
2351 address_expr,
2352 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2353 reloc_type[0]);
2354
2355 /* These relocations can have an addend that won't fit in
2356 4 octets for 64bit assembly. */
2357 if (HAVE_64BIT_GPRS
2358 && ! howto->partial_inplace
2359 && (reloc_type[0] == BFD_RELOC_16
2360 || reloc_type[0] == BFD_RELOC_32
2361 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2362 || reloc_type[0] == BFD_RELOC_HI16_S
2363 || reloc_type[0] == BFD_RELOC_LO16
2364 || reloc_type[0] == BFD_RELOC_GPREL16
2365 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2366 || reloc_type[0] == BFD_RELOC_GPREL32
2367 || reloc_type[0] == BFD_RELOC_64
2368 || reloc_type[0] == BFD_RELOC_CTOR
2369 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2370 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2371 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2372 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2373 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2374 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2375 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2376 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2377 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2378 ip->fixp[0]->fx_no_overflow = 1;
2379
2380 if (mips_relax.sequence)
2381 {
2382 if (mips_relax.first_fixup == 0)
2383 mips_relax.first_fixup = ip->fixp[0];
2384 }
2385 else if (reloc_needs_lo_p (*reloc_type))
2386 {
2387 struct mips_hi_fixup *hi_fixup;
2388
2389 /* Reuse the last entry if it already has a matching %lo. */
2390 hi_fixup = mips_hi_fixup_list;
2391 if (hi_fixup == 0
2392 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2393 {
2394 hi_fixup = ((struct mips_hi_fixup *)
2395 xmalloc (sizeof (struct mips_hi_fixup)));
2396 hi_fixup->next = mips_hi_fixup_list;
2397 mips_hi_fixup_list = hi_fixup;
2398 }
2399 hi_fixup->fixp = ip->fixp[0];
2400 hi_fixup->seg = now_seg;
2401 }
2402
2403 /* Add fixups for the second and third relocations, if given.
2404 Note that the ABI allows the second relocation to be
2405 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2406 moment we only use RSS_UNDEF, but we could add support
2407 for the others if it ever becomes necessary. */
2408 for (i = 1; i < 3; i++)
2409 if (reloc_type[i] != BFD_RELOC_UNUSED)
2410 {
2411 ip->fixp[i] = fix_new (ip->frag, ip->where,
2412 ip->fixp[0]->fx_size, NULL, 0,
2413 FALSE, reloc_type[i]);
2414
2415 /* Use fx_tcbit to mark compound relocs. */
2416 ip->fixp[0]->fx_tcbit = 1;
2417 ip->fixp[i]->fx_tcbit = 1;
2418 }
2419 }
2420 }
2421 install_insn (ip);
2422
2423 /* Update the register mask information. */
2424 if (! mips_opts.mips16)
2425 {
2426 if (pinfo & INSN_WRITE_GPR_D)
2427 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
2428 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2429 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
2430 if (pinfo & INSN_READ_GPR_S)
2431 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
2432 if (pinfo & INSN_WRITE_GPR_31)
2433 mips_gprmask |= 1 << RA;
2434 if (pinfo & INSN_WRITE_FPR_D)
2435 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
2436 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2437 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
2438 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2439 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
2440 if ((pinfo & INSN_READ_FPR_R) != 0)
2441 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
2442 if (pinfo & INSN_COP)
2443 {
2444 /* We don't keep enough information to sort these cases out.
2445 The itbl support does keep this information however, although
2446 we currently don't support itbl fprmats as part of the cop
2447 instruction. May want to add this support in the future. */
2448 }
2449 /* Never set the bit for $0, which is always zero. */
2450 mips_gprmask &= ~1 << 0;
2451 }
2452 else
2453 {
2454 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2455 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
2456 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2457 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
2458 if (pinfo & MIPS16_INSN_WRITE_Z)
2459 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
2460 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2461 mips_gprmask |= 1 << TREG;
2462 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2463 mips_gprmask |= 1 << SP;
2464 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2465 mips_gprmask |= 1 << RA;
2466 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2467 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2468 if (pinfo & MIPS16_INSN_READ_Z)
2469 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
2470 if (pinfo & MIPS16_INSN_READ_GPR_X)
2471 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
2472 }
2473
2474 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2475 {
2476 /* Filling the branch delay slot is more complex. We try to
2477 switch the branch with the previous instruction, which we can
2478 do if the previous instruction does not set up a condition
2479 that the branch tests and if the branch is not itself the
2480 target of any branch. */
2481 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2482 || (pinfo & INSN_COND_BRANCH_DELAY))
2483 {
2484 if (mips_optimize < 2
2485 /* If we have seen .set volatile or .set nomove, don't
2486 optimize. */
2487 || mips_opts.nomove != 0
2488 /* We can't swap if the previous instruction's position
2489 is fixed. */
2490 || history[0].fixed_p
2491 /* If the previous previous insn was in a .set
2492 noreorder, we can't swap. Actually, the MIPS
2493 assembler will swap in this situation. However, gcc
2494 configured -with-gnu-as will generate code like
2495 .set noreorder
2496 lw $4,XXX
2497 .set reorder
2498 INSN
2499 bne $4,$0,foo
2500 in which we can not swap the bne and INSN. If gcc is
2501 not configured -with-gnu-as, it does not output the
2502 .set pseudo-ops. */
2503 || history[1].noreorder_p
2504 /* If the branch is itself the target of a branch, we
2505 can not swap. We cheat on this; all we check for is
2506 whether there is a label on this instruction. If
2507 there are any branches to anything other than a
2508 label, users must use .set noreorder. */
2509 || insn_labels != NULL
2510 /* If the previous instruction is in a variant frag
2511 other than this branch's one, we cannot do the swap.
2512 This does not apply to the mips16, which uses variant
2513 frags for different purposes. */
2514 || (! mips_opts.mips16
2515 && prev_insn_frag_type == rs_machine_dependent)
2516 /* If the branch reads the condition codes, we don't
2517 even try to swap, because in the sequence
2518 ctc1 $X,$31
2519 INSN
2520 INSN
2521 bc1t LABEL
2522 we can not swap, and I don't feel like handling that
2523 case. */
2524 || (! mips_opts.mips16
2525 && (pinfo & INSN_READ_COND_CODE)
2526 && ! cop_interlocks)
2527 /* Check for conflicts between the branch and the instructions
2528 before the candidate delay slot. */
2529 || nops_for_insn (history + 1, ip) > 0
2530 /* Check for conflicts between the swapped sequence and the
2531 target of the branch. */
2532 || nops_for_sequence (2, history + 1, ip, history) > 0
2533 /* We do not swap with a trap instruction, since it
2534 complicates trap handlers to have the trap
2535 instruction be in a delay slot. */
2536 || (prev_pinfo & INSN_TRAP)
2537 /* If the branch reads a register that the previous
2538 instruction sets, we can not swap. */
2539 || (! mips_opts.mips16
2540 && (prev_pinfo & INSN_WRITE_GPR_T)
2541 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2542 MIPS_GR_REG))
2543 || (! mips_opts.mips16
2544 && (prev_pinfo & INSN_WRITE_GPR_D)
2545 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2546 MIPS_GR_REG))
2547 || (mips_opts.mips16
2548 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2549 && (insn_uses_reg
2550 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2551 MIPS16_REG)))
2552 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2553 && (insn_uses_reg
2554 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2555 MIPS16_REG)))
2556 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2557 && (insn_uses_reg
2558 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2559 MIPS16_REG)))
2560 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2561 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2562 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2563 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2564 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2565 && insn_uses_reg (ip,
2566 MIPS16OP_EXTRACT_REG32R
2567 (history[0].insn_opcode),
2568 MIPS_GR_REG))))
2569 /* If the branch writes a register that the previous
2570 instruction sets, we can not swap (we know that
2571 branches write only to RD or to $31). */
2572 || (! mips_opts.mips16
2573 && (prev_pinfo & INSN_WRITE_GPR_T)
2574 && (((pinfo & INSN_WRITE_GPR_D)
2575 && (EXTRACT_OPERAND (RT, history[0])
2576 == EXTRACT_OPERAND (RD, *ip)))
2577 || ((pinfo & INSN_WRITE_GPR_31)
2578 && EXTRACT_OPERAND (RT, history[0]) == RA)))
2579 || (! mips_opts.mips16
2580 && (prev_pinfo & INSN_WRITE_GPR_D)
2581 && (((pinfo & INSN_WRITE_GPR_D)
2582 && (EXTRACT_OPERAND (RD, history[0])
2583 == EXTRACT_OPERAND (RD, *ip)))
2584 || ((pinfo & INSN_WRITE_GPR_31)
2585 && EXTRACT_OPERAND (RD, history[0]) == RA)))
2586 || (mips_opts.mips16
2587 && (pinfo & MIPS16_INSN_WRITE_31)
2588 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2589 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2590 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2591 == RA))))
2592 /* If the branch writes a register that the previous
2593 instruction reads, we can not swap (we know that
2594 branches only write to RD or to $31). */
2595 || (! mips_opts.mips16
2596 && (pinfo & INSN_WRITE_GPR_D)
2597 && insn_uses_reg (&history[0],
2598 EXTRACT_OPERAND (RD, *ip),
2599 MIPS_GR_REG))
2600 || (! mips_opts.mips16
2601 && (pinfo & INSN_WRITE_GPR_31)
2602 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2603 || (mips_opts.mips16
2604 && (pinfo & MIPS16_INSN_WRITE_31)
2605 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2606 /* If one instruction sets a condition code and the
2607 other one uses a condition code, we can not swap. */
2608 || ((pinfo & INSN_READ_COND_CODE)
2609 && (prev_pinfo & INSN_WRITE_COND_CODE))
2610 || ((pinfo & INSN_WRITE_COND_CODE)
2611 && (prev_pinfo & INSN_READ_COND_CODE))
2612 /* If the previous instruction uses the PC, we can not
2613 swap. */
2614 || (mips_opts.mips16
2615 && (prev_pinfo & MIPS16_INSN_READ_PC))
2616 /* If the previous instruction had a fixup in mips16
2617 mode, we can not swap. This normally means that the
2618 previous instruction was a 4 byte branch anyhow. */
2619 || (mips_opts.mips16 && history[0].fixp[0])
2620 /* If the previous instruction is a sync, sync.l, or
2621 sync.p, we can not swap. */
2622 || (prev_pinfo & INSN_SYNC))
2623 {
2624 /* We could do even better for unconditional branches to
2625 portions of this object file; we could pick up the
2626 instruction at the destination, put it in the delay
2627 slot, and bump the destination address. */
2628 insert_into_history (0, 1, ip);
2629 emit_nop ();
2630 if (mips_relax.sequence)
2631 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2632 }
2633 else
2634 {
2635 /* It looks like we can actually do the swap. */
2636 struct mips_cl_insn delay = history[0];
2637 if (mips_opts.mips16)
2638 {
2639 know (delay.frag == ip->frag);
2640 move_insn (ip, delay.frag, delay.where);
2641 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
2642 }
2643 else if (relaxed_branch)
2644 {
2645 /* Add the delay slot instruction to the end of the
2646 current frag and shrink the fixed part of the
2647 original frag. If the branch occupies the tail of
2648 the latter, move it backwards to cover the gap. */
2649 delay.frag->fr_fix -= 4;
2650 if (delay.frag == ip->frag)
2651 move_insn (ip, ip->frag, ip->where - 4);
2652 add_fixed_insn (&delay);
2653 }
2654 else
2655 {
2656 move_insn (&delay, ip->frag, ip->where);
2657 move_insn (ip, history[0].frag, history[0].where);
2658 }
2659 history[0] = *ip;
2660 delay.fixed_p = 1;
2661 insert_into_history (0, 1, &delay);
2662 }
2663
2664 /* If that was an unconditional branch, forget the previous
2665 insn information. */
2666 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2667 mips_no_prev_insn (FALSE);
2668 }
2669 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2670 {
2671 /* We don't yet optimize a branch likely. What we should do
2672 is look at the target, copy the instruction found there
2673 into the delay slot, and increment the branch to jump to
2674 the next instruction. */
2675 insert_into_history (0, 1, ip);
2676 emit_nop ();
2677 }
2678 else
2679 insert_into_history (0, 1, ip);
2680 }
2681 else
2682 insert_into_history (0, 1, ip);
2683
2684 /* We just output an insn, so the next one doesn't have a label. */
2685 mips_clear_insn_labels ();
2686 }
2687
2688 /* This function forgets that there was any previous instruction or
2689 label. If PRESERVE is non-zero, it remembers enough information to
2690 know whether nops are needed before a noreorder section. */
2691
2692 static void
2693 mips_no_prev_insn (int preserve)
2694 {
2695 size_t i;
2696
2697 if (! preserve)
2698 {
2699 prev_nop_frag = NULL;
2700 prev_nop_frag_holds = 0;
2701 prev_nop_frag_required = 0;
2702 prev_nop_frag_since = 0;
2703 for (i = 0; i < ARRAY_SIZE (history); i++)
2704 history[i] = (mips_opts.mips16 ? mips16_nop_insn : nop_insn);
2705 }
2706 else
2707 for (i = 0; i < ARRAY_SIZE (history); i++)
2708 {
2709 history[i].fixed_p = 1;
2710 history[i].noreorder_p = 0;
2711 history[i].mips16_absolute_jump_p = 0;
2712 }
2713 mips_clear_insn_labels ();
2714 }
2715
2716 /* This function must be called whenever we turn on noreorder or emit
2717 something other than instructions. It inserts any NOPS which might
2718 be needed by the previous instruction, and clears the information
2719 kept for the previous instructions. The INSNS parameter is true if
2720 instructions are to follow. */
2721
2722 static void
2723 mips_emit_delays (bfd_boolean insns)
2724 {
2725 if (! mips_opts.noreorder)
2726 {
2727 int nops = nops_for_insn (history, NULL);
2728 if (nops > 0)
2729 {
2730 if (insns && mips_optimize != 0)
2731 {
2732 /* Record the frag which holds the nop instructions, so
2733 that we can remove them if we don't need them. */
2734 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2735 prev_nop_frag = frag_now;
2736 prev_nop_frag_holds = nops;
2737 prev_nop_frag_required = 0;
2738 prev_nop_frag_since = 0;
2739 }
2740
2741 for (; nops > 0; --nops)
2742 add_fixed_insn (NOP_INSN);
2743
2744 if (insns)
2745 {
2746 /* Move on to a new frag, so that it is safe to simply
2747 decrease the size of prev_nop_frag. */
2748 frag_wane (frag_now);
2749 frag_new (0);
2750 }
2751
2752 mips_move_labels ();
2753 }
2754 }
2755
2756 /* Mark instruction labels in mips16 mode. */
2757 if (insns)
2758 mips16_mark_labels ();
2759
2760 mips_no_prev_insn (insns);
2761 }
2762
2763 /* Set up global variables for the start of a new macro. */
2764
2765 static void
2766 macro_start (void)
2767 {
2768 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2769 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2770 && (history[0].insn_mo->pinfo
2771 & (INSN_UNCOND_BRANCH_DELAY
2772 | INSN_COND_BRANCH_DELAY
2773 | INSN_COND_BRANCH_LIKELY)) != 0);
2774 }
2775
2776 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2777 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2778 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2779
2780 static const char *
2781 macro_warning (relax_substateT subtype)
2782 {
2783 if (subtype & RELAX_DELAY_SLOT)
2784 return _("Macro instruction expanded into multiple instructions"
2785 " in a branch delay slot");
2786 else if (subtype & RELAX_NOMACRO)
2787 return _("Macro instruction expanded into multiple instructions");
2788 else
2789 return 0;
2790 }
2791
2792 /* Finish up a macro. Emit warnings as appropriate. */
2793
2794 static void
2795 macro_end (void)
2796 {
2797 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2798 {
2799 relax_substateT subtype;
2800
2801 /* Set up the relaxation warning flags. */
2802 subtype = 0;
2803 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2804 subtype |= RELAX_SECOND_LONGER;
2805 if (mips_opts.warn_about_macros)
2806 subtype |= RELAX_NOMACRO;
2807 if (mips_macro_warning.delay_slot_p)
2808 subtype |= RELAX_DELAY_SLOT;
2809
2810 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2811 {
2812 /* Either the macro has a single implementation or both
2813 implementations are longer than 4 bytes. Emit the
2814 warning now. */
2815 const char *msg = macro_warning (subtype);
2816 if (msg != 0)
2817 as_warn (msg);
2818 }
2819 else
2820 {
2821 /* One implementation might need a warning but the other
2822 definitely doesn't. */
2823 mips_macro_warning.first_frag->fr_subtype |= subtype;
2824 }
2825 }
2826 }
2827
2828 /* Read a macro's relocation codes from *ARGS and store them in *R.
2829 The first argument in *ARGS will be either the code for a single
2830 relocation or -1 followed by the three codes that make up a
2831 composite relocation. */
2832
2833 static void
2834 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2835 {
2836 int i, next;
2837
2838 next = va_arg (*args, int);
2839 if (next >= 0)
2840 r[0] = (bfd_reloc_code_real_type) next;
2841 else
2842 for (i = 0; i < 3; i++)
2843 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2844 }
2845
2846 /* Build an instruction created by a macro expansion. This is passed
2847 a pointer to the count of instructions created so far, an
2848 expression, the name of the instruction to build, an operand format
2849 string, and corresponding arguments. */
2850
2851 static void
2852 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2853 {
2854 const struct mips_opcode *mo;
2855 struct mips_cl_insn insn;
2856 bfd_reloc_code_real_type r[3];
2857 va_list args;
2858
2859 va_start (args, fmt);
2860
2861 if (mips_opts.mips16)
2862 {
2863 mips16_macro_build (ep, name, fmt, args);
2864 va_end (args);
2865 return;
2866 }
2867
2868 r[0] = BFD_RELOC_UNUSED;
2869 r[1] = BFD_RELOC_UNUSED;
2870 r[2] = BFD_RELOC_UNUSED;
2871 mo = (struct mips_opcode *) hash_find (op_hash, name);
2872 assert (mo);
2873 assert (strcmp (name, mo->name) == 0);
2874
2875 /* Search until we get a match for NAME. It is assumed here that
2876 macros will never generate MDMX or MIPS-3D instructions. */
2877 while (strcmp (fmt, mo->args) != 0
2878 || mo->pinfo == INSN_MACRO
2879 || !OPCODE_IS_MEMBER (mo,
2880 (mips_opts.isa
2881 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2882 mips_opts.arch)
2883 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
2884 {
2885 ++mo;
2886 assert (mo->name);
2887 assert (strcmp (name, mo->name) == 0);
2888 }
2889
2890 create_insn (&insn, mo);
2891 for (;;)
2892 {
2893 switch (*fmt++)
2894 {
2895 case '\0':
2896 break;
2897
2898 case ',':
2899 case '(':
2900 case ')':
2901 continue;
2902
2903 case '+':
2904 switch (*fmt++)
2905 {
2906 case 'A':
2907 case 'E':
2908 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
2909 continue;
2910
2911 case 'B':
2912 case 'F':
2913 /* Note that in the macro case, these arguments are already
2914 in MSB form. (When handling the instruction in the
2915 non-macro case, these arguments are sizes from which
2916 MSB values must be calculated.) */
2917 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
2918 continue;
2919
2920 case 'C':
2921 case 'G':
2922 case 'H':
2923 /* Note that in the macro case, these arguments are already
2924 in MSBD form. (When handling the instruction in the
2925 non-macro case, these arguments are sizes from which
2926 MSBD values must be calculated.) */
2927 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
2928 continue;
2929
2930 default:
2931 internalError ();
2932 }
2933 continue;
2934
2935 case 't':
2936 case 'w':
2937 case 'E':
2938 INSERT_OPERAND (RT, insn, va_arg (args, int));
2939 continue;
2940
2941 case 'c':
2942 INSERT_OPERAND (CODE, insn, va_arg (args, int));
2943 continue;
2944
2945 case 'T':
2946 case 'W':
2947 INSERT_OPERAND (FT, insn, va_arg (args, int));
2948 continue;
2949
2950 case 'd':
2951 case 'G':
2952 case 'K':
2953 INSERT_OPERAND (RD, insn, va_arg (args, int));
2954 continue;
2955
2956 case 'U':
2957 {
2958 int tmp = va_arg (args, int);
2959
2960 INSERT_OPERAND (RT, insn, tmp);
2961 INSERT_OPERAND (RD, insn, tmp);
2962 continue;
2963 }
2964
2965 case 'V':
2966 case 'S':
2967 INSERT_OPERAND (FS, insn, va_arg (args, int));
2968 continue;
2969
2970 case 'z':
2971 continue;
2972
2973 case '<':
2974 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
2975 continue;
2976
2977 case 'D':
2978 INSERT_OPERAND (FD, insn, va_arg (args, int));
2979 continue;
2980
2981 case 'B':
2982 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
2983 continue;
2984
2985 case 'J':
2986 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
2987 continue;
2988
2989 case 'q':
2990 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
2991 continue;
2992
2993 case 'b':
2994 case 's':
2995 case 'r':
2996 case 'v':
2997 INSERT_OPERAND (RS, insn, va_arg (args, int));
2998 continue;
2999
3000 case 'i':
3001 case 'j':
3002 case 'o':
3003 macro_read_relocs (&args, r);
3004 assert (*r == BFD_RELOC_GPREL16
3005 || *r == BFD_RELOC_MIPS_LITERAL
3006 || *r == BFD_RELOC_MIPS_HIGHER
3007 || *r == BFD_RELOC_HI16_S
3008 || *r == BFD_RELOC_LO16
3009 || *r == BFD_RELOC_MIPS_GOT16
3010 || *r == BFD_RELOC_MIPS_CALL16
3011 || *r == BFD_RELOC_MIPS_GOT_DISP
3012 || *r == BFD_RELOC_MIPS_GOT_PAGE
3013 || *r == BFD_RELOC_MIPS_GOT_OFST
3014 || *r == BFD_RELOC_MIPS_GOT_LO16
3015 || *r == BFD_RELOC_MIPS_CALL_LO16);
3016 continue;
3017
3018 case 'u':
3019 macro_read_relocs (&args, r);
3020 assert (ep != NULL
3021 && (ep->X_op == O_constant
3022 || (ep->X_op == O_symbol
3023 && (*r == BFD_RELOC_MIPS_HIGHEST
3024 || *r == BFD_RELOC_HI16_S
3025 || *r == BFD_RELOC_HI16
3026 || *r == BFD_RELOC_GPREL16
3027 || *r == BFD_RELOC_MIPS_GOT_HI16
3028 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3029 continue;
3030
3031 case 'p':
3032 assert (ep != NULL);
3033 /*
3034 * This allows macro() to pass an immediate expression for
3035 * creating short branches without creating a symbol.
3036 * Note that the expression still might come from the assembly
3037 * input, in which case the value is not checked for range nor
3038 * is a relocation entry generated (yuck).
3039 */
3040 if (ep->X_op == O_constant)
3041 {
3042 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3043 ep = NULL;
3044 }
3045 else
3046 *r = BFD_RELOC_16_PCREL_S2;
3047 continue;
3048
3049 case 'a':
3050 assert (ep != NULL);
3051 *r = BFD_RELOC_MIPS_JMP;
3052 continue;
3053
3054 case 'C':
3055 insn.insn_opcode |= va_arg (args, unsigned long);
3056 continue;
3057
3058 default:
3059 internalError ();
3060 }
3061 break;
3062 }
3063 va_end (args);
3064 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3065
3066 append_insn (&insn, ep, r);
3067 }
3068
3069 static void
3070 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3071 va_list args)
3072 {
3073 struct mips_opcode *mo;
3074 struct mips_cl_insn insn;
3075 bfd_reloc_code_real_type r[3]
3076 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3077
3078 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3079 assert (mo);
3080 assert (strcmp (name, mo->name) == 0);
3081
3082 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3083 {
3084 ++mo;
3085 assert (mo->name);
3086 assert (strcmp (name, mo->name) == 0);
3087 }
3088
3089 create_insn (&insn, mo);
3090 for (;;)
3091 {
3092 int c;
3093
3094 c = *fmt++;
3095 switch (c)
3096 {
3097 case '\0':
3098 break;
3099
3100 case ',':
3101 case '(':
3102 case ')':
3103 continue;
3104
3105 case 'y':
3106 case 'w':
3107 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3108 continue;
3109
3110 case 'x':
3111 case 'v':
3112 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3113 continue;
3114
3115 case 'z':
3116 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3117 continue;
3118
3119 case 'Z':
3120 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3121 continue;
3122
3123 case '0':
3124 case 'S':
3125 case 'P':
3126 case 'R':
3127 continue;
3128
3129 case 'X':
3130 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3131 continue;
3132
3133 case 'Y':
3134 {
3135 int regno;
3136
3137 regno = va_arg (args, int);
3138 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3139 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3140 }
3141 continue;
3142
3143 case '<':
3144 case '>':
3145 case '4':
3146 case '5':
3147 case 'H':
3148 case 'W':
3149 case 'D':
3150 case 'j':
3151 case '8':
3152 case 'V':
3153 case 'C':
3154 case 'U':
3155 case 'k':
3156 case 'K':
3157 case 'p':
3158 case 'q':
3159 {
3160 assert (ep != NULL);
3161
3162 if (ep->X_op != O_constant)
3163 *r = (int) BFD_RELOC_UNUSED + c;
3164 else
3165 {
3166 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3167 FALSE, &insn.insn_opcode, &insn.use_extend,
3168 &insn.extend);
3169 ep = NULL;
3170 *r = BFD_RELOC_UNUSED;
3171 }
3172 }
3173 continue;
3174
3175 case '6':
3176 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3177 continue;
3178 }
3179
3180 break;
3181 }
3182
3183 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3184
3185 append_insn (&insn, ep, r);
3186 }
3187
3188 /*
3189 * Generate a "jalr" instruction with a relocation hint to the called
3190 * function. This occurs in NewABI PIC code.
3191 */
3192 static void
3193 macro_build_jalr (expressionS *ep)
3194 {
3195 char *f = NULL;
3196
3197 if (HAVE_NEWABI)
3198 {
3199 frag_grow (8);
3200 f = frag_more (0);
3201 }
3202 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3203 if (HAVE_NEWABI)
3204 fix_new_exp (frag_now, f - frag_now->fr_literal,
3205 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3206 }
3207
3208 /*
3209 * Generate a "lui" instruction.
3210 */
3211 static void
3212 macro_build_lui (expressionS *ep, int regnum)
3213 {
3214 expressionS high_expr;
3215 const struct mips_opcode *mo;
3216 struct mips_cl_insn insn;
3217 bfd_reloc_code_real_type r[3]
3218 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3219 const char *name = "lui";
3220 const char *fmt = "t,u";
3221
3222 assert (! mips_opts.mips16);
3223
3224 high_expr = *ep;
3225
3226 if (high_expr.X_op == O_constant)
3227 {
3228 /* we can compute the instruction now without a relocation entry */
3229 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3230 >> 16) & 0xffff;
3231 *r = BFD_RELOC_UNUSED;
3232 }
3233 else
3234 {
3235 assert (ep->X_op == O_symbol);
3236 /* _gp_disp is a special case, used from s_cpload.
3237 __gnu_local_gp is used if mips_no_shared. */
3238 assert (mips_pic == NO_PIC
3239 || (! HAVE_NEWABI
3240 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3241 || (! mips_in_shared
3242 && strcmp (S_GET_NAME (ep->X_add_symbol),
3243 "__gnu_local_gp") == 0));
3244 *r = BFD_RELOC_HI16_S;
3245 }
3246
3247 mo = hash_find (op_hash, name);
3248 assert (strcmp (name, mo->name) == 0);
3249 assert (strcmp (fmt, mo->args) == 0);
3250 create_insn (&insn, mo);
3251
3252 insn.insn_opcode = insn.insn_mo->match;
3253 INSERT_OPERAND (RT, insn, regnum);
3254 if (*r == BFD_RELOC_UNUSED)
3255 {
3256 insn.insn_opcode |= high_expr.X_add_number;
3257 append_insn (&insn, NULL, r);
3258 }
3259 else
3260 append_insn (&insn, &high_expr, r);
3261 }
3262
3263 /* Generate a sequence of instructions to do a load or store from a constant
3264 offset off of a base register (breg) into/from a target register (treg),
3265 using AT if necessary. */
3266 static void
3267 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3268 int treg, int breg, int dbl)
3269 {
3270 assert (ep->X_op == O_constant);
3271
3272 /* Sign-extending 32-bit constants makes their handling easier. */
3273 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3274 == ~((bfd_vma) 0x7fffffff)))
3275 {
3276 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3277 as_bad (_("constant too large"));
3278
3279 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3280 - 0x80000000);
3281 }
3282
3283 /* Right now, this routine can only handle signed 32-bit constants. */
3284 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3285 as_warn (_("operand overflow"));
3286
3287 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3288 {
3289 /* Signed 16-bit offset will fit in the op. Easy! */
3290 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3291 }
3292 else
3293 {
3294 /* 32-bit offset, need multiple instructions and AT, like:
3295 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3296 addu $tempreg,$tempreg,$breg
3297 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3298 to handle the complete offset. */
3299 macro_build_lui (ep, AT);
3300 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3301 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3302
3303 if (mips_opts.noat)
3304 as_bad (_("Macro used $at after \".set noat\""));
3305 }
3306 }
3307
3308 /* set_at()
3309 * Generates code to set the $at register to true (one)
3310 * if reg is less than the immediate expression.
3311 */
3312 static void
3313 set_at (int reg, int unsignedp)
3314 {
3315 if (imm_expr.X_op == O_constant
3316 && imm_expr.X_add_number >= -0x8000
3317 && imm_expr.X_add_number < 0x8000)
3318 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3319 AT, reg, BFD_RELOC_LO16);
3320 else
3321 {
3322 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3323 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3324 }
3325 }
3326
3327 static void
3328 normalize_constant_expr (expressionS *ex)
3329 {
3330 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3331 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3332 - 0x80000000);
3333 }
3334
3335 /* Warn if an expression is not a constant. */
3336
3337 static void
3338 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3339 {
3340 if (ex->X_op == O_big)
3341 as_bad (_("unsupported large constant"));
3342 else if (ex->X_op != O_constant)
3343 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3344
3345 normalize_constant_expr (ex);
3346 }
3347
3348 /* Count the leading zeroes by performing a binary chop. This is a
3349 bulky bit of source, but performance is a LOT better for the
3350 majority of values than a simple loop to count the bits:
3351 for (lcnt = 0; (lcnt < 32); lcnt++)
3352 if ((v) & (1 << (31 - lcnt)))
3353 break;
3354 However it is not code size friendly, and the gain will drop a bit
3355 on certain cached systems.
3356 */
3357 #define COUNT_TOP_ZEROES(v) \
3358 (((v) & ~0xffff) == 0 \
3359 ? ((v) & ~0xff) == 0 \
3360 ? ((v) & ~0xf) == 0 \
3361 ? ((v) & ~0x3) == 0 \
3362 ? ((v) & ~0x1) == 0 \
3363 ? !(v) \
3364 ? 32 \
3365 : 31 \
3366 : 30 \
3367 : ((v) & ~0x7) == 0 \
3368 ? 29 \
3369 : 28 \
3370 : ((v) & ~0x3f) == 0 \
3371 ? ((v) & ~0x1f) == 0 \
3372 ? 27 \
3373 : 26 \
3374 : ((v) & ~0x7f) == 0 \
3375 ? 25 \
3376 : 24 \
3377 : ((v) & ~0xfff) == 0 \
3378 ? ((v) & ~0x3ff) == 0 \
3379 ? ((v) & ~0x1ff) == 0 \
3380 ? 23 \
3381 : 22 \
3382 : ((v) & ~0x7ff) == 0 \
3383 ? 21 \
3384 : 20 \
3385 : ((v) & ~0x3fff) == 0 \
3386 ? ((v) & ~0x1fff) == 0 \
3387 ? 19 \
3388 : 18 \
3389 : ((v) & ~0x7fff) == 0 \
3390 ? 17 \
3391 : 16 \
3392 : ((v) & ~0xffffff) == 0 \
3393 ? ((v) & ~0xfffff) == 0 \
3394 ? ((v) & ~0x3ffff) == 0 \
3395 ? ((v) & ~0x1ffff) == 0 \
3396 ? 15 \
3397 : 14 \
3398 : ((v) & ~0x7ffff) == 0 \
3399 ? 13 \
3400 : 12 \
3401 : ((v) & ~0x3fffff) == 0 \
3402 ? ((v) & ~0x1fffff) == 0 \
3403 ? 11 \
3404 : 10 \
3405 : ((v) & ~0x7fffff) == 0 \
3406 ? 9 \
3407 : 8 \
3408 : ((v) & ~0xfffffff) == 0 \
3409 ? ((v) & ~0x3ffffff) == 0 \
3410 ? ((v) & ~0x1ffffff) == 0 \
3411 ? 7 \
3412 : 6 \
3413 : ((v) & ~0x7ffffff) == 0 \
3414 ? 5 \
3415 : 4 \
3416 : ((v) & ~0x3fffffff) == 0 \
3417 ? ((v) & ~0x1fffffff) == 0 \
3418 ? 3 \
3419 : 2 \
3420 : ((v) & ~0x7fffffff) == 0 \
3421 ? 1 \
3422 : 0)
3423
3424 /* load_register()
3425 * This routine generates the least number of instructions necessary to load
3426 * an absolute expression value into a register.
3427 */
3428 static void
3429 load_register (int reg, expressionS *ep, int dbl)
3430 {
3431 int freg;
3432 expressionS hi32, lo32;
3433
3434 if (ep->X_op != O_big)
3435 {
3436 assert (ep->X_op == O_constant);
3437
3438 /* Sign-extending 32-bit constants makes their handling easier. */
3439 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3440 == ~((bfd_vma) 0x7fffffff)))
3441 {
3442 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3443 as_bad (_("constant too large"));
3444
3445 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3446 - 0x80000000);
3447 }
3448
3449 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3450 {
3451 /* We can handle 16 bit signed values with an addiu to
3452 $zero. No need to ever use daddiu here, since $zero and
3453 the result are always correct in 32 bit mode. */
3454 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3455 return;
3456 }
3457 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3458 {
3459 /* We can handle 16 bit unsigned values with an ori to
3460 $zero. */
3461 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3462 return;
3463 }
3464 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3465 {
3466 /* 32 bit values require an lui. */
3467 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3468 if ((ep->X_add_number & 0xffff) != 0)
3469 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3470 return;
3471 }
3472 }
3473
3474 /* The value is larger than 32 bits. */
3475
3476 if (HAVE_32BIT_GPRS)
3477 {
3478 as_bad (_("Number (0x%lx) larger than 32 bits"),
3479 (unsigned long) ep->X_add_number);
3480 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3481 return;
3482 }
3483
3484 if (ep->X_op != O_big)
3485 {
3486 hi32 = *ep;
3487 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3488 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3489 hi32.X_add_number &= 0xffffffff;
3490 lo32 = *ep;
3491 lo32.X_add_number &= 0xffffffff;
3492 }
3493 else
3494 {
3495 assert (ep->X_add_number > 2);
3496 if (ep->X_add_number == 3)
3497 generic_bignum[3] = 0;
3498 else if (ep->X_add_number > 4)
3499 as_bad (_("Number larger than 64 bits"));
3500 lo32.X_op = O_constant;
3501 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3502 hi32.X_op = O_constant;
3503 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3504 }
3505
3506 if (hi32.X_add_number == 0)
3507 freg = 0;
3508 else
3509 {
3510 int shift, bit;
3511 unsigned long hi, lo;
3512
3513 if (hi32.X_add_number == (offsetT) 0xffffffff)
3514 {
3515 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3516 {
3517 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3518 return;
3519 }
3520 if (lo32.X_add_number & 0x80000000)
3521 {
3522 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3523 if (lo32.X_add_number & 0xffff)
3524 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3525 return;
3526 }
3527 }
3528
3529 /* Check for 16bit shifted constant. We know that hi32 is
3530 non-zero, so start the mask on the first bit of the hi32
3531 value. */
3532 shift = 17;
3533 do
3534 {
3535 unsigned long himask, lomask;
3536
3537 if (shift < 32)
3538 {
3539 himask = 0xffff >> (32 - shift);
3540 lomask = (0xffff << shift) & 0xffffffff;
3541 }
3542 else
3543 {
3544 himask = 0xffff << (shift - 32);
3545 lomask = 0;
3546 }
3547 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3548 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3549 {
3550 expressionS tmp;
3551
3552 tmp.X_op = O_constant;
3553 if (shift < 32)
3554 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3555 | (lo32.X_add_number >> shift));
3556 else
3557 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3558 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3559 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3560 reg, reg, (shift >= 32) ? shift - 32 : shift);
3561 return;
3562 }
3563 ++shift;
3564 }
3565 while (shift <= (64 - 16));
3566
3567 /* Find the bit number of the lowest one bit, and store the
3568 shifted value in hi/lo. */
3569 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3570 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3571 if (lo != 0)
3572 {
3573 bit = 0;
3574 while ((lo & 1) == 0)
3575 {
3576 lo >>= 1;
3577 ++bit;
3578 }
3579 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3580 hi >>= bit;
3581 }
3582 else
3583 {
3584 bit = 32;
3585 while ((hi & 1) == 0)
3586 {
3587 hi >>= 1;
3588 ++bit;
3589 }
3590 lo = hi;
3591 hi = 0;
3592 }
3593
3594 /* Optimize if the shifted value is a (power of 2) - 1. */
3595 if ((hi == 0 && ((lo + 1) & lo) == 0)
3596 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3597 {
3598 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3599 if (shift != 0)
3600 {
3601 expressionS tmp;
3602
3603 /* This instruction will set the register to be all
3604 ones. */
3605 tmp.X_op = O_constant;
3606 tmp.X_add_number = (offsetT) -1;
3607 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3608 if (bit != 0)
3609 {
3610 bit += shift;
3611 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3612 reg, reg, (bit >= 32) ? bit - 32 : bit);
3613 }
3614 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3615 reg, reg, (shift >= 32) ? shift - 32 : shift);
3616 return;
3617 }
3618 }
3619
3620 /* Sign extend hi32 before calling load_register, because we can
3621 generally get better code when we load a sign extended value. */
3622 if ((hi32.X_add_number & 0x80000000) != 0)
3623 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3624 load_register (reg, &hi32, 0);
3625 freg = reg;
3626 }
3627 if ((lo32.X_add_number & 0xffff0000) == 0)
3628 {
3629 if (freg != 0)
3630 {
3631 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3632 freg = reg;
3633 }
3634 }
3635 else
3636 {
3637 expressionS mid16;
3638
3639 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3640 {
3641 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3642 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3643 return;
3644 }
3645
3646 if (freg != 0)
3647 {
3648 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3649 freg = reg;
3650 }
3651 mid16 = lo32;
3652 mid16.X_add_number >>= 16;
3653 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3654 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3655 freg = reg;
3656 }
3657 if ((lo32.X_add_number & 0xffff) != 0)
3658 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3659 }
3660
3661 static inline void
3662 load_delay_nop (void)
3663 {
3664 if (!gpr_interlocks)
3665 macro_build (NULL, "nop", "");
3666 }
3667
3668 /* Load an address into a register. */
3669
3670 static void
3671 load_address (int reg, expressionS *ep, int *used_at)
3672 {
3673 if (ep->X_op != O_constant
3674 && ep->X_op != O_symbol)
3675 {
3676 as_bad (_("expression too complex"));
3677 ep->X_op = O_constant;
3678 }
3679
3680 if (ep->X_op == O_constant)
3681 {
3682 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3683 return;
3684 }
3685
3686 if (mips_pic == NO_PIC)
3687 {
3688 /* If this is a reference to a GP relative symbol, we want
3689 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3690 Otherwise we want
3691 lui $reg,<sym> (BFD_RELOC_HI16_S)
3692 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3693 If we have an addend, we always use the latter form.
3694
3695 With 64bit address space and a usable $at we want
3696 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3697 lui $at,<sym> (BFD_RELOC_HI16_S)
3698 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3699 daddiu $at,<sym> (BFD_RELOC_LO16)
3700 dsll32 $reg,0
3701 daddu $reg,$reg,$at
3702
3703 If $at is already in use, we use a path which is suboptimal
3704 on superscalar processors.
3705 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3706 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3707 dsll $reg,16
3708 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3709 dsll $reg,16
3710 daddiu $reg,<sym> (BFD_RELOC_LO16)
3711
3712 For GP relative symbols in 64bit address space we can use
3713 the same sequence as in 32bit address space. */
3714 if (HAVE_64BIT_SYMBOLS)
3715 {
3716 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3717 && !nopic_need_relax (ep->X_add_symbol, 1))
3718 {
3719 relax_start (ep->X_add_symbol);
3720 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3721 mips_gp_register, BFD_RELOC_GPREL16);
3722 relax_switch ();
3723 }
3724
3725 if (*used_at == 0 && !mips_opts.noat)
3726 {
3727 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3728 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3729 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3730 BFD_RELOC_MIPS_HIGHER);
3731 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3732 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3733 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3734 *used_at = 1;
3735 }
3736 else
3737 {
3738 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3739 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3740 BFD_RELOC_MIPS_HIGHER);
3741 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3742 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3743 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3744 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3745 }
3746
3747 if (mips_relax.sequence)
3748 relax_end ();
3749 }
3750 else
3751 {
3752 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3753 && !nopic_need_relax (ep->X_add_symbol, 1))
3754 {
3755 relax_start (ep->X_add_symbol);
3756 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3757 mips_gp_register, BFD_RELOC_GPREL16);
3758 relax_switch ();
3759 }
3760 macro_build_lui (ep, reg);
3761 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3762 reg, reg, BFD_RELOC_LO16);
3763 if (mips_relax.sequence)
3764 relax_end ();
3765 }
3766 }
3767 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3768 {
3769 expressionS ex;
3770
3771 /* If this is a reference to an external symbol, we want
3772 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3773 Otherwise we want
3774 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3775 nop
3776 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3777 If there is a constant, it must be added in after.
3778
3779 If we have NewABI, we want
3780 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3781 unless we're referencing a global symbol with a non-zero
3782 offset, in which case cst must be added separately. */
3783 if (HAVE_NEWABI)
3784 {
3785 if (ep->X_add_number)
3786 {
3787 ex.X_add_number = ep->X_add_number;
3788 ep->X_add_number = 0;
3789 relax_start (ep->X_add_symbol);
3790 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3791 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3792 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3793 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3794 ex.X_op = O_constant;
3795 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3796 reg, reg, BFD_RELOC_LO16);
3797 ep->X_add_number = ex.X_add_number;
3798 relax_switch ();
3799 }
3800 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3801 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3802 if (mips_relax.sequence)
3803 relax_end ();
3804 }
3805 else
3806 {
3807 ex.X_add_number = ep->X_add_number;
3808 ep->X_add_number = 0;
3809 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3810 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3811 load_delay_nop ();
3812 relax_start (ep->X_add_symbol);
3813 relax_switch ();
3814 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3815 BFD_RELOC_LO16);
3816 relax_end ();
3817
3818 if (ex.X_add_number != 0)
3819 {
3820 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3821 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3822 ex.X_op = O_constant;
3823 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3824 reg, reg, BFD_RELOC_LO16);
3825 }
3826 }
3827 }
3828 else if (mips_pic == SVR4_PIC)
3829 {
3830 expressionS ex;
3831
3832 /* This is the large GOT case. If this is a reference to an
3833 external symbol, we want
3834 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3835 addu $reg,$reg,$gp
3836 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3837
3838 Otherwise, for a reference to a local symbol in old ABI, we want
3839 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3840 nop
3841 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3842 If there is a constant, it must be added in after.
3843
3844 In the NewABI, for local symbols, with or without offsets, we want:
3845 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3846 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3847 */
3848 if (HAVE_NEWABI)
3849 {
3850 ex.X_add_number = ep->X_add_number;
3851 ep->X_add_number = 0;
3852 relax_start (ep->X_add_symbol);
3853 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3854 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3855 reg, reg, mips_gp_register);
3856 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3857 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3858 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3859 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3860 else if (ex.X_add_number)
3861 {
3862 ex.X_op = O_constant;
3863 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3864 BFD_RELOC_LO16);
3865 }
3866
3867 ep->X_add_number = ex.X_add_number;
3868 relax_switch ();
3869 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3870 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3871 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3872 BFD_RELOC_MIPS_GOT_OFST);
3873 relax_end ();
3874 }
3875 else
3876 {
3877 ex.X_add_number = ep->X_add_number;
3878 ep->X_add_number = 0;
3879 relax_start (ep->X_add_symbol);
3880 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3881 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3882 reg, reg, mips_gp_register);
3883 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3884 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3885 relax_switch ();
3886 if (reg_needs_delay (mips_gp_register))
3887 {
3888 /* We need a nop before loading from $gp. This special
3889 check is required because the lui which starts the main
3890 instruction stream does not refer to $gp, and so will not
3891 insert the nop which may be required. */
3892 macro_build (NULL, "nop", "");
3893 }
3894 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3895 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3896 load_delay_nop ();
3897 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3898 BFD_RELOC_LO16);
3899 relax_end ();
3900
3901 if (ex.X_add_number != 0)
3902 {
3903 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3904 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3905 ex.X_op = O_constant;
3906 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3907 BFD_RELOC_LO16);
3908 }
3909 }
3910 }
3911 else
3912 abort ();
3913
3914 if (mips_opts.noat && *used_at == 1)
3915 as_bad (_("Macro used $at after \".set noat\""));
3916 }
3917
3918 /* Move the contents of register SOURCE into register DEST. */
3919
3920 static void
3921 move_register (int dest, int source)
3922 {
3923 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
3924 dest, source, 0);
3925 }
3926
3927 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
3928 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
3929 The two alternatives are:
3930
3931 Global symbol Local sybmol
3932 ------------- ------------
3933 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
3934 ... ...
3935 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
3936
3937 load_got_offset emits the first instruction and add_got_offset
3938 emits the second for a 16-bit offset or add_got_offset_hilo emits
3939 a sequence to add a 32-bit offset using a scratch register. */
3940
3941 static void
3942 load_got_offset (int dest, expressionS *local)
3943 {
3944 expressionS global;
3945
3946 global = *local;
3947 global.X_add_number = 0;
3948
3949 relax_start (local->X_add_symbol);
3950 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
3951 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3952 relax_switch ();
3953 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
3954 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3955 relax_end ();
3956 }
3957
3958 static void
3959 add_got_offset (int dest, expressionS *local)
3960 {
3961 expressionS global;
3962
3963 global.X_op = O_constant;
3964 global.X_op_symbol = NULL;
3965 global.X_add_symbol = NULL;
3966 global.X_add_number = local->X_add_number;
3967
3968 relax_start (local->X_add_symbol);
3969 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
3970 dest, dest, BFD_RELOC_LO16);
3971 relax_switch ();
3972 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
3973 relax_end ();
3974 }
3975
3976 static void
3977 add_got_offset_hilo (int dest, expressionS *local, int tmp)
3978 {
3979 expressionS global;
3980 int hold_mips_optimize;
3981
3982 global.X_op = O_constant;
3983 global.X_op_symbol = NULL;
3984 global.X_add_symbol = NULL;
3985 global.X_add_number = local->X_add_number;
3986
3987 relax_start (local->X_add_symbol);
3988 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
3989 relax_switch ();
3990 /* Set mips_optimize around the lui instruction to avoid
3991 inserting an unnecessary nop after the lw. */
3992 hold_mips_optimize = mips_optimize;
3993 mips_optimize = 2;
3994 macro_build_lui (&global, tmp);
3995 mips_optimize = hold_mips_optimize;
3996 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
3997 relax_end ();
3998
3999 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4000 }
4001
4002 /*
4003 * Build macros
4004 * This routine implements the seemingly endless macro or synthesized
4005 * instructions and addressing modes in the mips assembly language. Many
4006 * of these macros are simple and are similar to each other. These could
4007 * probably be handled by some kind of table or grammar approach instead of
4008 * this verbose method. Others are not simple macros but are more like
4009 * optimizing code generation.
4010 * One interesting optimization is when several store macros appear
4011 * consecutively that would load AT with the upper half of the same address.
4012 * The ensuing load upper instructions are ommited. This implies some kind
4013 * of global optimization. We currently only optimize within a single macro.
4014 * For many of the load and store macros if the address is specified as a
4015 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4016 * first load register 'at' with zero and use it as the base register. The
4017 * mips assembler simply uses register $zero. Just one tiny optimization
4018 * we're missing.
4019 */
4020 static void
4021 macro (struct mips_cl_insn *ip)
4022 {
4023 register int treg, sreg, dreg, breg;
4024 int tempreg;
4025 int mask;
4026 int used_at = 0;
4027 expressionS expr1;
4028 const char *s;
4029 const char *s2;
4030 const char *fmt;
4031 int likely = 0;
4032 int dbl = 0;
4033 int coproc = 0;
4034 int lr = 0;
4035 int imm = 0;
4036 int call = 0;
4037 int off;
4038 offsetT maxnum;
4039 bfd_reloc_code_real_type r;
4040 int hold_mips_optimize;
4041
4042 assert (! mips_opts.mips16);
4043
4044 treg = (ip->insn_opcode >> 16) & 0x1f;
4045 dreg = (ip->insn_opcode >> 11) & 0x1f;
4046 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4047 mask = ip->insn_mo->mask;
4048
4049 expr1.X_op = O_constant;
4050 expr1.X_op_symbol = NULL;
4051 expr1.X_add_symbol = NULL;
4052 expr1.X_add_number = 1;
4053
4054 switch (mask)
4055 {
4056 case M_DABS:
4057 dbl = 1;
4058 case M_ABS:
4059 /* bgez $a0,.+12
4060 move v0,$a0
4061 sub v0,$zero,$a0
4062 */
4063
4064 mips_emit_delays (TRUE);
4065 ++mips_opts.noreorder;
4066 mips_any_noreorder = 1;
4067
4068 expr1.X_add_number = 8;
4069 macro_build (&expr1, "bgez", "s,p", sreg);
4070 if (dreg == sreg)
4071 macro_build (NULL, "nop", "", 0);
4072 else
4073 move_register (dreg, sreg);
4074 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4075
4076 --mips_opts.noreorder;
4077 break;
4078
4079 case M_ADD_I:
4080 s = "addi";
4081 s2 = "add";
4082 goto do_addi;
4083 case M_ADDU_I:
4084 s = "addiu";
4085 s2 = "addu";
4086 goto do_addi;
4087 case M_DADD_I:
4088 dbl = 1;
4089 s = "daddi";
4090 s2 = "dadd";
4091 goto do_addi;
4092 case M_DADDU_I:
4093 dbl = 1;
4094 s = "daddiu";
4095 s2 = "daddu";
4096 do_addi:
4097 if (imm_expr.X_op == O_constant
4098 && imm_expr.X_add_number >= -0x8000
4099 && imm_expr.X_add_number < 0x8000)
4100 {
4101 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4102 break;
4103 }
4104 used_at = 1;
4105 load_register (AT, &imm_expr, dbl);
4106 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4107 break;
4108
4109 case M_AND_I:
4110 s = "andi";
4111 s2 = "and";
4112 goto do_bit;
4113 case M_OR_I:
4114 s = "ori";
4115 s2 = "or";
4116 goto do_bit;
4117 case M_NOR_I:
4118 s = "";
4119 s2 = "nor";
4120 goto do_bit;
4121 case M_XOR_I:
4122 s = "xori";
4123 s2 = "xor";
4124 do_bit:
4125 if (imm_expr.X_op == O_constant
4126 && imm_expr.X_add_number >= 0
4127 && imm_expr.X_add_number < 0x10000)
4128 {
4129 if (mask != M_NOR_I)
4130 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4131 else
4132 {
4133 macro_build (&imm_expr, "ori", "t,r,i",
4134 treg, sreg, BFD_RELOC_LO16);
4135 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4136 }
4137 break;
4138 }
4139
4140 used_at = 1;
4141 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4142 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4143 break;
4144
4145 case M_BEQ_I:
4146 s = "beq";
4147 goto beq_i;
4148 case M_BEQL_I:
4149 s = "beql";
4150 likely = 1;
4151 goto beq_i;
4152 case M_BNE_I:
4153 s = "bne";
4154 goto beq_i;
4155 case M_BNEL_I:
4156 s = "bnel";
4157 likely = 1;
4158 beq_i:
4159 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4160 {
4161 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4162 break;
4163 }
4164 used_at = 1;
4165 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4166 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4167 break;
4168
4169 case M_BGEL:
4170 likely = 1;
4171 case M_BGE:
4172 if (treg == 0)
4173 {
4174 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4175 break;
4176 }
4177 if (sreg == 0)
4178 {
4179 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4180 break;
4181 }
4182 used_at = 1;
4183 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4184 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4185 break;
4186
4187 case M_BGTL_I:
4188 likely = 1;
4189 case M_BGT_I:
4190 /* check for > max integer */
4191 maxnum = 0x7fffffff;
4192 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4193 {
4194 maxnum <<= 16;
4195 maxnum |= 0xffff;
4196 maxnum <<= 16;
4197 maxnum |= 0xffff;
4198 }
4199 if (imm_expr.X_op == O_constant
4200 && imm_expr.X_add_number >= maxnum
4201 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4202 {
4203 do_false:
4204 /* result is always false */
4205 if (! likely)
4206 macro_build (NULL, "nop", "", 0);
4207 else
4208 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4209 break;
4210 }
4211 if (imm_expr.X_op != O_constant)
4212 as_bad (_("Unsupported large constant"));
4213 ++imm_expr.X_add_number;
4214 /* FALLTHROUGH */
4215 case M_BGE_I:
4216 case M_BGEL_I:
4217 if (mask == M_BGEL_I)
4218 likely = 1;
4219 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4220 {
4221 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4222 break;
4223 }
4224 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4225 {
4226 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4227 break;
4228 }
4229 maxnum = 0x7fffffff;
4230 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4231 {
4232 maxnum <<= 16;
4233 maxnum |= 0xffff;
4234 maxnum <<= 16;
4235 maxnum |= 0xffff;
4236 }
4237 maxnum = - maxnum - 1;
4238 if (imm_expr.X_op == O_constant
4239 && imm_expr.X_add_number <= maxnum
4240 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4241 {
4242 do_true:
4243 /* result is always true */
4244 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4245 macro_build (&offset_expr, "b", "p");
4246 break;
4247 }
4248 used_at = 1;
4249 set_at (sreg, 0);
4250 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4251 break;
4252
4253 case M_BGEUL:
4254 likely = 1;
4255 case M_BGEU:
4256 if (treg == 0)
4257 goto do_true;
4258 if (sreg == 0)
4259 {
4260 macro_build (&offset_expr, likely ? "beql" : "beq",
4261 "s,t,p", 0, treg);
4262 break;
4263 }
4264 used_at = 1;
4265 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4266 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4267 break;
4268
4269 case M_BGTUL_I:
4270 likely = 1;
4271 case M_BGTU_I:
4272 if (sreg == 0
4273 || (HAVE_32BIT_GPRS
4274 && imm_expr.X_op == O_constant
4275 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4276 goto do_false;
4277 if (imm_expr.X_op != O_constant)
4278 as_bad (_("Unsupported large constant"));
4279 ++imm_expr.X_add_number;
4280 /* FALLTHROUGH */
4281 case M_BGEU_I:
4282 case M_BGEUL_I:
4283 if (mask == M_BGEUL_I)
4284 likely = 1;
4285 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4286 goto do_true;
4287 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4288 {
4289 macro_build (&offset_expr, likely ? "bnel" : "bne",
4290 "s,t,p", sreg, 0);
4291 break;
4292 }
4293 used_at = 1;
4294 set_at (sreg, 1);
4295 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4296 break;
4297
4298 case M_BGTL:
4299 likely = 1;
4300 case M_BGT:
4301 if (treg == 0)
4302 {
4303 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4304 break;
4305 }
4306 if (sreg == 0)
4307 {
4308 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4309 break;
4310 }
4311 used_at = 1;
4312 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4313 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4314 break;
4315
4316 case M_BGTUL:
4317 likely = 1;
4318 case M_BGTU:
4319 if (treg == 0)
4320 {
4321 macro_build (&offset_expr, likely ? "bnel" : "bne",
4322 "s,t,p", sreg, 0);
4323 break;
4324 }
4325 if (sreg == 0)
4326 goto do_false;
4327 used_at = 1;
4328 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4329 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4330 break;
4331
4332 case M_BLEL:
4333 likely = 1;
4334 case M_BLE:
4335 if (treg == 0)
4336 {
4337 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4338 break;
4339 }
4340 if (sreg == 0)
4341 {
4342 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4343 break;
4344 }
4345 used_at = 1;
4346 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4347 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4348 break;
4349
4350 case M_BLEL_I:
4351 likely = 1;
4352 case M_BLE_I:
4353 maxnum = 0x7fffffff;
4354 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4355 {
4356 maxnum <<= 16;
4357 maxnum |= 0xffff;
4358 maxnum <<= 16;
4359 maxnum |= 0xffff;
4360 }
4361 if (imm_expr.X_op == O_constant
4362 && imm_expr.X_add_number >= maxnum
4363 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4364 goto do_true;
4365 if (imm_expr.X_op != O_constant)
4366 as_bad (_("Unsupported large constant"));
4367 ++imm_expr.X_add_number;
4368 /* FALLTHROUGH */
4369 case M_BLT_I:
4370 case M_BLTL_I:
4371 if (mask == M_BLTL_I)
4372 likely = 1;
4373 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4374 {
4375 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4376 break;
4377 }
4378 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4379 {
4380 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4381 break;
4382 }
4383 used_at = 1;
4384 set_at (sreg, 0);
4385 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4386 break;
4387
4388 case M_BLEUL:
4389 likely = 1;
4390 case M_BLEU:
4391 if (treg == 0)
4392 {
4393 macro_build (&offset_expr, likely ? "beql" : "beq",
4394 "s,t,p", sreg, 0);
4395 break;
4396 }
4397 if (sreg == 0)
4398 goto do_true;
4399 used_at = 1;
4400 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4401 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4402 break;
4403
4404 case M_BLEUL_I:
4405 likely = 1;
4406 case M_BLEU_I:
4407 if (sreg == 0
4408 || (HAVE_32BIT_GPRS
4409 && imm_expr.X_op == O_constant
4410 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4411 goto do_true;
4412 if (imm_expr.X_op != O_constant)
4413 as_bad (_("Unsupported large constant"));
4414 ++imm_expr.X_add_number;
4415 /* FALLTHROUGH */
4416 case M_BLTU_I:
4417 case M_BLTUL_I:
4418 if (mask == M_BLTUL_I)
4419 likely = 1;
4420 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4421 goto do_false;
4422 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4423 {
4424 macro_build (&offset_expr, likely ? "beql" : "beq",
4425 "s,t,p", sreg, 0);
4426 break;
4427 }
4428 used_at = 1;
4429 set_at (sreg, 1);
4430 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4431 break;
4432
4433 case M_BLTL:
4434 likely = 1;
4435 case M_BLT:
4436 if (treg == 0)
4437 {
4438 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4439 break;
4440 }
4441 if (sreg == 0)
4442 {
4443 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4444 break;
4445 }
4446 used_at = 1;
4447 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4448 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4449 break;
4450
4451 case M_BLTUL:
4452 likely = 1;
4453 case M_BLTU:
4454 if (treg == 0)
4455 goto do_false;
4456 if (sreg == 0)
4457 {
4458 macro_build (&offset_expr, likely ? "bnel" : "bne",
4459 "s,t,p", 0, treg);
4460 break;
4461 }
4462 used_at = 1;
4463 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4464 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4465 break;
4466
4467 case M_DEXT:
4468 {
4469 unsigned long pos;
4470 unsigned long size;
4471
4472 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4473 {
4474 as_bad (_("Unsupported large constant"));
4475 pos = size = 1;
4476 }
4477 else
4478 {
4479 pos = (unsigned long) imm_expr.X_add_number;
4480 size = (unsigned long) imm2_expr.X_add_number;
4481 }
4482
4483 if (pos > 63)
4484 {
4485 as_bad (_("Improper position (%lu)"), pos);
4486 pos = 1;
4487 }
4488 if (size == 0 || size > 64
4489 || (pos + size - 1) > 63)
4490 {
4491 as_bad (_("Improper extract size (%lu, position %lu)"),
4492 size, pos);
4493 size = 1;
4494 }
4495
4496 if (size <= 32 && pos < 32)
4497 {
4498 s = "dext";
4499 fmt = "t,r,+A,+C";
4500 }
4501 else if (size <= 32)
4502 {
4503 s = "dextu";
4504 fmt = "t,r,+E,+H";
4505 }
4506 else
4507 {
4508 s = "dextm";
4509 fmt = "t,r,+A,+G";
4510 }
4511 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4512 }
4513 break;
4514
4515 case M_DINS:
4516 {
4517 unsigned long pos;
4518 unsigned long size;
4519
4520 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4521 {
4522 as_bad (_("Unsupported large constant"));
4523 pos = size = 1;
4524 }
4525 else
4526 {
4527 pos = (unsigned long) imm_expr.X_add_number;
4528 size = (unsigned long) imm2_expr.X_add_number;
4529 }
4530
4531 if (pos > 63)
4532 {
4533 as_bad (_("Improper position (%lu)"), pos);
4534 pos = 1;
4535 }
4536 if (size == 0 || size > 64
4537 || (pos + size - 1) > 63)
4538 {
4539 as_bad (_("Improper insert size (%lu, position %lu)"),
4540 size, pos);
4541 size = 1;
4542 }
4543
4544 if (pos < 32 && (pos + size - 1) < 32)
4545 {
4546 s = "dins";
4547 fmt = "t,r,+A,+B";
4548 }
4549 else if (pos >= 32)
4550 {
4551 s = "dinsu";
4552 fmt = "t,r,+E,+F";
4553 }
4554 else
4555 {
4556 s = "dinsm";
4557 fmt = "t,r,+A,+F";
4558 }
4559 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4560 pos + size - 1);
4561 }
4562 break;
4563
4564 case M_DDIV_3:
4565 dbl = 1;
4566 case M_DIV_3:
4567 s = "mflo";
4568 goto do_div3;
4569 case M_DREM_3:
4570 dbl = 1;
4571 case M_REM_3:
4572 s = "mfhi";
4573 do_div3:
4574 if (treg == 0)
4575 {
4576 as_warn (_("Divide by zero."));
4577 if (mips_trap)
4578 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4579 else
4580 macro_build (NULL, "break", "c", 7);
4581 break;
4582 }
4583
4584 mips_emit_delays (TRUE);
4585 ++mips_opts.noreorder;
4586 mips_any_noreorder = 1;
4587 if (mips_trap)
4588 {
4589 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4590 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4591 }
4592 else
4593 {
4594 expr1.X_add_number = 8;
4595 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4596 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4597 macro_build (NULL, "break", "c", 7);
4598 }
4599 expr1.X_add_number = -1;
4600 used_at = 1;
4601 load_register (AT, &expr1, dbl);
4602 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4603 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4604 if (dbl)
4605 {
4606 expr1.X_add_number = 1;
4607 load_register (AT, &expr1, dbl);
4608 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4609 }
4610 else
4611 {
4612 expr1.X_add_number = 0x80000000;
4613 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4614 }
4615 if (mips_trap)
4616 {
4617 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4618 /* We want to close the noreorder block as soon as possible, so
4619 that later insns are available for delay slot filling. */
4620 --mips_opts.noreorder;
4621 }
4622 else
4623 {
4624 expr1.X_add_number = 8;
4625 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4626 macro_build (NULL, "nop", "", 0);
4627
4628 /* We want to close the noreorder block as soon as possible, so
4629 that later insns are available for delay slot filling. */
4630 --mips_opts.noreorder;
4631
4632 macro_build (NULL, "break", "c", 6);
4633 }
4634 macro_build (NULL, s, "d", dreg);
4635 break;
4636
4637 case M_DIV_3I:
4638 s = "div";
4639 s2 = "mflo";
4640 goto do_divi;
4641 case M_DIVU_3I:
4642 s = "divu";
4643 s2 = "mflo";
4644 goto do_divi;
4645 case M_REM_3I:
4646 s = "div";
4647 s2 = "mfhi";
4648 goto do_divi;
4649 case M_REMU_3I:
4650 s = "divu";
4651 s2 = "mfhi";
4652 goto do_divi;
4653 case M_DDIV_3I:
4654 dbl = 1;
4655 s = "ddiv";
4656 s2 = "mflo";
4657 goto do_divi;
4658 case M_DDIVU_3I:
4659 dbl = 1;
4660 s = "ddivu";
4661 s2 = "mflo";
4662 goto do_divi;
4663 case M_DREM_3I:
4664 dbl = 1;
4665 s = "ddiv";
4666 s2 = "mfhi";
4667 goto do_divi;
4668 case M_DREMU_3I:
4669 dbl = 1;
4670 s = "ddivu";
4671 s2 = "mfhi";
4672 do_divi:
4673 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4674 {
4675 as_warn (_("Divide by zero."));
4676 if (mips_trap)
4677 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4678 else
4679 macro_build (NULL, "break", "c", 7);
4680 break;
4681 }
4682 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4683 {
4684 if (strcmp (s2, "mflo") == 0)
4685 move_register (dreg, sreg);
4686 else
4687 move_register (dreg, 0);
4688 break;
4689 }
4690 if (imm_expr.X_op == O_constant
4691 && imm_expr.X_add_number == -1
4692 && s[strlen (s) - 1] != 'u')
4693 {
4694 if (strcmp (s2, "mflo") == 0)
4695 {
4696 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4697 }
4698 else
4699 move_register (dreg, 0);
4700 break;
4701 }
4702
4703 used_at = 1;
4704 load_register (AT, &imm_expr, dbl);
4705 macro_build (NULL, s, "z,s,t", sreg, AT);
4706 macro_build (NULL, s2, "d", dreg);
4707 break;
4708
4709 case M_DIVU_3:
4710 s = "divu";
4711 s2 = "mflo";
4712 goto do_divu3;
4713 case M_REMU_3:
4714 s = "divu";
4715 s2 = "mfhi";
4716 goto do_divu3;
4717 case M_DDIVU_3:
4718 s = "ddivu";
4719 s2 = "mflo";
4720 goto do_divu3;
4721 case M_DREMU_3:
4722 s = "ddivu";
4723 s2 = "mfhi";
4724 do_divu3:
4725 mips_emit_delays (TRUE);
4726 ++mips_opts.noreorder;
4727 mips_any_noreorder = 1;
4728 if (mips_trap)
4729 {
4730 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4731 macro_build (NULL, s, "z,s,t", sreg, treg);
4732 /* We want to close the noreorder block as soon as possible, so
4733 that later insns are available for delay slot filling. */
4734 --mips_opts.noreorder;
4735 }
4736 else
4737 {
4738 expr1.X_add_number = 8;
4739 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4740 macro_build (NULL, s, "z,s,t", sreg, treg);
4741
4742 /* We want to close the noreorder block as soon as possible, so
4743 that later insns are available for delay slot filling. */
4744 --mips_opts.noreorder;
4745 macro_build (NULL, "break", "c", 7);
4746 }
4747 macro_build (NULL, s2, "d", dreg);
4748 break;
4749
4750 case M_DLCA_AB:
4751 dbl = 1;
4752 case M_LCA_AB:
4753 call = 1;
4754 goto do_la;
4755 case M_DLA_AB:
4756 dbl = 1;
4757 case M_LA_AB:
4758 do_la:
4759 /* Load the address of a symbol into a register. If breg is not
4760 zero, we then add a base register to it. */
4761
4762 if (dbl && HAVE_32BIT_GPRS)
4763 as_warn (_("dla used to load 32-bit register"));
4764
4765 if (! dbl && HAVE_64BIT_OBJECTS)
4766 as_warn (_("la used to load 64-bit address"));
4767
4768 if (offset_expr.X_op == O_constant
4769 && offset_expr.X_add_number >= -0x8000
4770 && offset_expr.X_add_number < 0x8000)
4771 {
4772 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4773 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4774 break;
4775 }
4776
4777 if (!mips_opts.noat && (treg == breg))
4778 {
4779 tempreg = AT;
4780 used_at = 1;
4781 }
4782 else
4783 {
4784 tempreg = treg;
4785 }
4786
4787 if (offset_expr.X_op != O_symbol
4788 && offset_expr.X_op != O_constant)
4789 {
4790 as_bad (_("expression too complex"));
4791 offset_expr.X_op = O_constant;
4792 }
4793
4794 if (offset_expr.X_op == O_constant)
4795 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4796 else if (mips_pic == NO_PIC)
4797 {
4798 /* If this is a reference to a GP relative symbol, we want
4799 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4800 Otherwise we want
4801 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4802 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4803 If we have a constant, we need two instructions anyhow,
4804 so we may as well always use the latter form.
4805
4806 With 64bit address space and a usable $at we want
4807 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4808 lui $at,<sym> (BFD_RELOC_HI16_S)
4809 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4810 daddiu $at,<sym> (BFD_RELOC_LO16)
4811 dsll32 $tempreg,0
4812 daddu $tempreg,$tempreg,$at
4813
4814 If $at is already in use, we use a path which is suboptimal
4815 on superscalar processors.
4816 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4817 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4818 dsll $tempreg,16
4819 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4820 dsll $tempreg,16
4821 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4822
4823 For GP relative symbols in 64bit address space we can use
4824 the same sequence as in 32bit address space. */
4825 if (HAVE_64BIT_SYMBOLS)
4826 {
4827 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4828 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4829 {
4830 relax_start (offset_expr.X_add_symbol);
4831 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4832 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4833 relax_switch ();
4834 }
4835
4836 if (used_at == 0 && !mips_opts.noat)
4837 {
4838 macro_build (&offset_expr, "lui", "t,u",
4839 tempreg, BFD_RELOC_MIPS_HIGHEST);
4840 macro_build (&offset_expr, "lui", "t,u",
4841 AT, BFD_RELOC_HI16_S);
4842 macro_build (&offset_expr, "daddiu", "t,r,j",
4843 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4844 macro_build (&offset_expr, "daddiu", "t,r,j",
4845 AT, AT, BFD_RELOC_LO16);
4846 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4847 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4848 used_at = 1;
4849 }
4850 else
4851 {
4852 macro_build (&offset_expr, "lui", "t,u",
4853 tempreg, BFD_RELOC_MIPS_HIGHEST);
4854 macro_build (&offset_expr, "daddiu", "t,r,j",
4855 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4856 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4857 macro_build (&offset_expr, "daddiu", "t,r,j",
4858 tempreg, tempreg, BFD_RELOC_HI16_S);
4859 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4860 macro_build (&offset_expr, "daddiu", "t,r,j",
4861 tempreg, tempreg, BFD_RELOC_LO16);
4862 }
4863
4864 if (mips_relax.sequence)
4865 relax_end ();
4866 }
4867 else
4868 {
4869 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4870 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4871 {
4872 relax_start (offset_expr.X_add_symbol);
4873 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4874 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4875 relax_switch ();
4876 }
4877 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
4878 as_bad (_("offset too large"));
4879 macro_build_lui (&offset_expr, tempreg);
4880 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4881 tempreg, tempreg, BFD_RELOC_LO16);
4882 if (mips_relax.sequence)
4883 relax_end ();
4884 }
4885 }
4886 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4887 {
4888 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4889
4890 /* If this is a reference to an external symbol, and there
4891 is no constant, we want
4892 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4893 or for lca or if tempreg is PIC_CALL_REG
4894 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4895 For a local symbol, we want
4896 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4897 nop
4898 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4899
4900 If we have a small constant, and this is a reference to
4901 an external symbol, we want
4902 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4903 nop
4904 addiu $tempreg,$tempreg,<constant>
4905 For a local symbol, we want the same instruction
4906 sequence, but we output a BFD_RELOC_LO16 reloc on the
4907 addiu instruction.
4908
4909 If we have a large constant, and this is a reference to
4910 an external symbol, we want
4911 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4912 lui $at,<hiconstant>
4913 addiu $at,$at,<loconstant>
4914 addu $tempreg,$tempreg,$at
4915 For a local symbol, we want the same instruction
4916 sequence, but we output a BFD_RELOC_LO16 reloc on the
4917 addiu instruction.
4918 */
4919
4920 if (offset_expr.X_add_number == 0)
4921 {
4922 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
4923 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4924
4925 relax_start (offset_expr.X_add_symbol);
4926 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
4927 lw_reloc_type, mips_gp_register);
4928 if (breg != 0)
4929 {
4930 /* We're going to put in an addu instruction using
4931 tempreg, so we may as well insert the nop right
4932 now. */
4933 load_delay_nop ();
4934 }
4935 relax_switch ();
4936 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4937 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
4938 load_delay_nop ();
4939 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4940 tempreg, tempreg, BFD_RELOC_LO16);
4941 relax_end ();
4942 /* FIXME: If breg == 0, and the next instruction uses
4943 $tempreg, then if this variant case is used an extra
4944 nop will be generated. */
4945 }
4946 else if (offset_expr.X_add_number >= -0x8000
4947 && offset_expr.X_add_number < 0x8000)
4948 {
4949 load_got_offset (tempreg, &offset_expr);
4950 load_delay_nop ();
4951 add_got_offset (tempreg, &offset_expr);
4952 }
4953 else
4954 {
4955 expr1.X_add_number = offset_expr.X_add_number;
4956 offset_expr.X_add_number =
4957 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
4958 load_got_offset (tempreg, &offset_expr);
4959 offset_expr.X_add_number = expr1.X_add_number;
4960 /* If we are going to add in a base register, and the
4961 target register and the base register are the same,
4962 then we are using AT as a temporary register. Since
4963 we want to load the constant into AT, we add our
4964 current AT (from the global offset table) and the
4965 register into the register now, and pretend we were
4966 not using a base register. */
4967 if (breg == treg)
4968 {
4969 load_delay_nop ();
4970 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4971 treg, AT, breg);
4972 breg = 0;
4973 tempreg = treg;
4974 }
4975 add_got_offset_hilo (tempreg, &offset_expr, AT);
4976 used_at = 1;
4977 }
4978 }
4979 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
4980 {
4981 int add_breg_early = 0;
4982
4983 /* If this is a reference to an external, and there is no
4984 constant, or local symbol (*), with or without a
4985 constant, we want
4986 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4987 or for lca or if tempreg is PIC_CALL_REG
4988 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4989
4990 If we have a small constant, and this is a reference to
4991 an external symbol, we want
4992 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4993 addiu $tempreg,$tempreg,<constant>
4994
4995 If we have a large constant, and this is a reference to
4996 an external symbol, we want
4997 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4998 lui $at,<hiconstant>
4999 addiu $at,$at,<loconstant>
5000 addu $tempreg,$tempreg,$at
5001
5002 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5003 local symbols, even though it introduces an additional
5004 instruction. */
5005
5006 if (offset_expr.X_add_number)
5007 {
5008 expr1.X_add_number = offset_expr.X_add_number;
5009 offset_expr.X_add_number = 0;
5010
5011 relax_start (offset_expr.X_add_symbol);
5012 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5013 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5014
5015 if (expr1.X_add_number >= -0x8000
5016 && expr1.X_add_number < 0x8000)
5017 {
5018 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5019 tempreg, tempreg, BFD_RELOC_LO16);
5020 }
5021 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5022 {
5023 int dreg;
5024
5025 /* If we are going to add in a base register, and the
5026 target register and the base register are the same,
5027 then we are using AT as a temporary register. Since
5028 we want to load the constant into AT, we add our
5029 current AT (from the global offset table) and the
5030 register into the register now, and pretend we were
5031 not using a base register. */
5032 if (breg != treg)
5033 dreg = tempreg;
5034 else
5035 {
5036 assert (tempreg == AT);
5037 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5038 treg, AT, breg);
5039 dreg = treg;
5040 add_breg_early = 1;
5041 }
5042
5043 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5044 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5045 dreg, dreg, AT);
5046
5047 used_at = 1;
5048 }
5049 else
5050 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5051
5052 relax_switch ();
5053 offset_expr.X_add_number = expr1.X_add_number;
5054
5055 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5056 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5057 if (add_breg_early)
5058 {
5059 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5060 treg, tempreg, breg);
5061 breg = 0;
5062 tempreg = treg;
5063 }
5064 relax_end ();
5065 }
5066 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5067 {
5068 relax_start (offset_expr.X_add_symbol);
5069 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5070 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5071 relax_switch ();
5072 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5073 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5074 relax_end ();
5075 }
5076 else
5077 {
5078 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5079 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5080 }
5081 }
5082 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5083 {
5084 int gpdelay;
5085 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5086 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5087 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5088
5089 /* This is the large GOT case. If this is a reference to an
5090 external symbol, and there is no constant, we want
5091 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5092 addu $tempreg,$tempreg,$gp
5093 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5094 or for lca or if tempreg is PIC_CALL_REG
5095 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5096 addu $tempreg,$tempreg,$gp
5097 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5098 For a local symbol, we want
5099 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5100 nop
5101 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5102
5103 If we have a small constant, and this is a reference to
5104 an external symbol, we want
5105 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5106 addu $tempreg,$tempreg,$gp
5107 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5108 nop
5109 addiu $tempreg,$tempreg,<constant>
5110 For a local symbol, we want
5111 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5112 nop
5113 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5114
5115 If we have a large constant, and this is a reference to
5116 an external symbol, we want
5117 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5118 addu $tempreg,$tempreg,$gp
5119 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5120 lui $at,<hiconstant>
5121 addiu $at,$at,<loconstant>
5122 addu $tempreg,$tempreg,$at
5123 For a local symbol, we want
5124 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5125 lui $at,<hiconstant>
5126 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5127 addu $tempreg,$tempreg,$at
5128 */
5129
5130 expr1.X_add_number = offset_expr.X_add_number;
5131 offset_expr.X_add_number = 0;
5132 relax_start (offset_expr.X_add_symbol);
5133 gpdelay = reg_needs_delay (mips_gp_register);
5134 if (expr1.X_add_number == 0 && breg == 0
5135 && (call || tempreg == PIC_CALL_REG))
5136 {
5137 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5138 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5139 }
5140 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5141 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5142 tempreg, tempreg, mips_gp_register);
5143 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5144 tempreg, lw_reloc_type, tempreg);
5145 if (expr1.X_add_number == 0)
5146 {
5147 if (breg != 0)
5148 {
5149 /* We're going to put in an addu instruction using
5150 tempreg, so we may as well insert the nop right
5151 now. */
5152 load_delay_nop ();
5153 }
5154 }
5155 else if (expr1.X_add_number >= -0x8000
5156 && expr1.X_add_number < 0x8000)
5157 {
5158 load_delay_nop ();
5159 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5160 tempreg, tempreg, BFD_RELOC_LO16);
5161 }
5162 else
5163 {
5164 int dreg;
5165
5166 /* If we are going to add in a base register, and the
5167 target register and the base register are the same,
5168 then we are using AT as a temporary register. Since
5169 we want to load the constant into AT, we add our
5170 current AT (from the global offset table) and the
5171 register into the register now, and pretend we were
5172 not using a base register. */
5173 if (breg != treg)
5174 dreg = tempreg;
5175 else
5176 {
5177 assert (tempreg == AT);
5178 load_delay_nop ();
5179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5180 treg, AT, breg);
5181 dreg = treg;
5182 }
5183
5184 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5185 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5186
5187 used_at = 1;
5188 }
5189 offset_expr.X_add_number =
5190 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5191 relax_switch ();
5192
5193 if (gpdelay)
5194 {
5195 /* This is needed because this instruction uses $gp, but
5196 the first instruction on the main stream does not. */
5197 macro_build (NULL, "nop", "");
5198 }
5199
5200 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5201 local_reloc_type, mips_gp_register);
5202 if (expr1.X_add_number >= -0x8000
5203 && expr1.X_add_number < 0x8000)
5204 {
5205 load_delay_nop ();
5206 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5207 tempreg, tempreg, BFD_RELOC_LO16);
5208 /* FIXME: If add_number is 0, and there was no base
5209 register, the external symbol case ended with a load,
5210 so if the symbol turns out to not be external, and
5211 the next instruction uses tempreg, an unnecessary nop
5212 will be inserted. */
5213 }
5214 else
5215 {
5216 if (breg == treg)
5217 {
5218 /* We must add in the base register now, as in the
5219 external symbol case. */
5220 assert (tempreg == AT);
5221 load_delay_nop ();
5222 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5223 treg, AT, breg);
5224 tempreg = treg;
5225 /* We set breg to 0 because we have arranged to add
5226 it in in both cases. */
5227 breg = 0;
5228 }
5229
5230 macro_build_lui (&expr1, AT);
5231 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5232 AT, AT, BFD_RELOC_LO16);
5233 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5234 tempreg, tempreg, AT);
5235 used_at = 1;
5236 }
5237 relax_end ();
5238 }
5239 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5240 {
5241 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5242 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5243 int add_breg_early = 0;
5244
5245 /* This is the large GOT case. If this is a reference to an
5246 external symbol, and there is no constant, we want
5247 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5248 add $tempreg,$tempreg,$gp
5249 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5250 or for lca or if tempreg is PIC_CALL_REG
5251 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5252 add $tempreg,$tempreg,$gp
5253 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5254
5255 If we have a small constant, and this is a reference to
5256 an external symbol, we want
5257 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5258 add $tempreg,$tempreg,$gp
5259 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5260 addi $tempreg,$tempreg,<constant>
5261
5262 If we have a large constant, and this is a reference to
5263 an external symbol, we want
5264 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5265 addu $tempreg,$tempreg,$gp
5266 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5267 lui $at,<hiconstant>
5268 addi $at,$at,<loconstant>
5269 add $tempreg,$tempreg,$at
5270
5271 If we have NewABI, and we know it's a local symbol, we want
5272 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5273 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5274 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5275
5276 relax_start (offset_expr.X_add_symbol);
5277
5278 expr1.X_add_number = offset_expr.X_add_number;
5279 offset_expr.X_add_number = 0;
5280
5281 if (expr1.X_add_number == 0 && breg == 0
5282 && (call || tempreg == PIC_CALL_REG))
5283 {
5284 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5285 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5286 }
5287 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5288 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5289 tempreg, tempreg, mips_gp_register);
5290 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5291 tempreg, lw_reloc_type, tempreg);
5292
5293 if (expr1.X_add_number == 0)
5294 ;
5295 else if (expr1.X_add_number >= -0x8000
5296 && expr1.X_add_number < 0x8000)
5297 {
5298 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5299 tempreg, tempreg, BFD_RELOC_LO16);
5300 }
5301 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5302 {
5303 int dreg;
5304
5305 /* If we are going to add in a base register, and the
5306 target register and the base register are the same,
5307 then we are using AT as a temporary register. Since
5308 we want to load the constant into AT, we add our
5309 current AT (from the global offset table) and the
5310 register into the register now, and pretend we were
5311 not using a base register. */
5312 if (breg != treg)
5313 dreg = tempreg;
5314 else
5315 {
5316 assert (tempreg == AT);
5317 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5318 treg, AT, breg);
5319 dreg = treg;
5320 add_breg_early = 1;
5321 }
5322
5323 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5324 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5325
5326 used_at = 1;
5327 }
5328 else
5329 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5330
5331 relax_switch ();
5332 offset_expr.X_add_number = expr1.X_add_number;
5333 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5334 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5335 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5336 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5337 if (add_breg_early)
5338 {
5339 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5340 treg, tempreg, breg);
5341 breg = 0;
5342 tempreg = treg;
5343 }
5344 relax_end ();
5345 }
5346 else
5347 abort ();
5348
5349 if (breg != 0)
5350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5351 break;
5352
5353 case M_J_A:
5354 /* The j instruction may not be used in PIC code, since it
5355 requires an absolute address. We convert it to a b
5356 instruction. */
5357 if (mips_pic == NO_PIC)
5358 macro_build (&offset_expr, "j", "a");
5359 else
5360 macro_build (&offset_expr, "b", "p");
5361 break;
5362
5363 /* The jal instructions must be handled as macros because when
5364 generating PIC code they expand to multi-instruction
5365 sequences. Normally they are simple instructions. */
5366 case M_JAL_1:
5367 dreg = RA;
5368 /* Fall through. */
5369 case M_JAL_2:
5370 if (mips_pic == NO_PIC)
5371 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5372 else if (mips_pic == SVR4_PIC)
5373 {
5374 if (sreg != PIC_CALL_REG)
5375 as_warn (_("MIPS PIC call to register other than $25"));
5376
5377 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5378 if (! HAVE_NEWABI)
5379 {
5380 if (mips_cprestore_offset < 0)
5381 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5382 else
5383 {
5384 if (! mips_frame_reg_valid)
5385 {
5386 as_warn (_("No .frame pseudo-op used in PIC code"));
5387 /* Quiet this warning. */
5388 mips_frame_reg_valid = 1;
5389 }
5390 if (! mips_cprestore_valid)
5391 {
5392 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5393 /* Quiet this warning. */
5394 mips_cprestore_valid = 1;
5395 }
5396 expr1.X_add_number = mips_cprestore_offset;
5397 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5398 mips_gp_register,
5399 mips_frame_reg,
5400 HAVE_64BIT_ADDRESSES);
5401 }
5402 }
5403 }
5404 else
5405 abort ();
5406
5407 break;
5408
5409 case M_JAL_A:
5410 if (mips_pic == NO_PIC)
5411 macro_build (&offset_expr, "jal", "a");
5412 else if (mips_pic == SVR4_PIC)
5413 {
5414 /* If this is a reference to an external symbol, and we are
5415 using a small GOT, we want
5416 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5417 nop
5418 jalr $ra,$25
5419 nop
5420 lw $gp,cprestore($sp)
5421 The cprestore value is set using the .cprestore
5422 pseudo-op. If we are using a big GOT, we want
5423 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5424 addu $25,$25,$gp
5425 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5426 nop
5427 jalr $ra,$25
5428 nop
5429 lw $gp,cprestore($sp)
5430 If the symbol is not external, we want
5431 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5432 nop
5433 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5434 jalr $ra,$25
5435 nop
5436 lw $gp,cprestore($sp)
5437
5438 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5439 sequences above, minus nops, unless the symbol is local,
5440 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5441 GOT_DISP. */
5442 if (HAVE_NEWABI)
5443 {
5444 if (! mips_big_got)
5445 {
5446 relax_start (offset_expr.X_add_symbol);
5447 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5448 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5449 mips_gp_register);
5450 relax_switch ();
5451 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5452 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5453 mips_gp_register);
5454 relax_end ();
5455 }
5456 else
5457 {
5458 relax_start (offset_expr.X_add_symbol);
5459 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5460 BFD_RELOC_MIPS_CALL_HI16);
5461 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5462 PIC_CALL_REG, mips_gp_register);
5463 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5464 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5465 PIC_CALL_REG);
5466 relax_switch ();
5467 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5468 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5469 mips_gp_register);
5470 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5471 PIC_CALL_REG, PIC_CALL_REG,
5472 BFD_RELOC_MIPS_GOT_OFST);
5473 relax_end ();
5474 }
5475
5476 macro_build_jalr (&offset_expr);
5477 }
5478 else
5479 {
5480 relax_start (offset_expr.X_add_symbol);
5481 if (! mips_big_got)
5482 {
5483 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5484 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5485 mips_gp_register);
5486 load_delay_nop ();
5487 relax_switch ();
5488 }
5489 else
5490 {
5491 int gpdelay;
5492
5493 gpdelay = reg_needs_delay (mips_gp_register);
5494 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5495 BFD_RELOC_MIPS_CALL_HI16);
5496 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5497 PIC_CALL_REG, mips_gp_register);
5498 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5499 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5500 PIC_CALL_REG);
5501 load_delay_nop ();
5502 relax_switch ();
5503 if (gpdelay)
5504 macro_build (NULL, "nop", "");
5505 }
5506 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5507 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5508 mips_gp_register);
5509 load_delay_nop ();
5510 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5511 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5512 relax_end ();
5513 macro_build_jalr (&offset_expr);
5514
5515 if (mips_cprestore_offset < 0)
5516 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5517 else
5518 {
5519 if (! mips_frame_reg_valid)
5520 {
5521 as_warn (_("No .frame pseudo-op used in PIC code"));
5522 /* Quiet this warning. */
5523 mips_frame_reg_valid = 1;
5524 }
5525 if (! mips_cprestore_valid)
5526 {
5527 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5528 /* Quiet this warning. */
5529 mips_cprestore_valid = 1;
5530 }
5531 if (mips_opts.noreorder)
5532 macro_build (NULL, "nop", "");
5533 expr1.X_add_number = mips_cprestore_offset;
5534 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5535 mips_gp_register,
5536 mips_frame_reg,
5537 HAVE_64BIT_ADDRESSES);
5538 }
5539 }
5540 }
5541 else
5542 abort ();
5543
5544 break;
5545
5546 case M_LB_AB:
5547 s = "lb";
5548 goto ld;
5549 case M_LBU_AB:
5550 s = "lbu";
5551 goto ld;
5552 case M_LH_AB:
5553 s = "lh";
5554 goto ld;
5555 case M_LHU_AB:
5556 s = "lhu";
5557 goto ld;
5558 case M_LW_AB:
5559 s = "lw";
5560 goto ld;
5561 case M_LWC0_AB:
5562 s = "lwc0";
5563 /* Itbl support may require additional care here. */
5564 coproc = 1;
5565 goto ld;
5566 case M_LWC1_AB:
5567 s = "lwc1";
5568 /* Itbl support may require additional care here. */
5569 coproc = 1;
5570 goto ld;
5571 case M_LWC2_AB:
5572 s = "lwc2";
5573 /* Itbl support may require additional care here. */
5574 coproc = 1;
5575 goto ld;
5576 case M_LWC3_AB:
5577 s = "lwc3";
5578 /* Itbl support may require additional care here. */
5579 coproc = 1;
5580 goto ld;
5581 case M_LWL_AB:
5582 s = "lwl";
5583 lr = 1;
5584 goto ld;
5585 case M_LWR_AB:
5586 s = "lwr";
5587 lr = 1;
5588 goto ld;
5589 case M_LDC1_AB:
5590 if (mips_opts.arch == CPU_R4650)
5591 {
5592 as_bad (_("opcode not supported on this processor"));
5593 break;
5594 }
5595 s = "ldc1";
5596 /* Itbl support may require additional care here. */
5597 coproc = 1;
5598 goto ld;
5599 case M_LDC2_AB:
5600 s = "ldc2";
5601 /* Itbl support may require additional care here. */
5602 coproc = 1;
5603 goto ld;
5604 case M_LDC3_AB:
5605 s = "ldc3";
5606 /* Itbl support may require additional care here. */
5607 coproc = 1;
5608 goto ld;
5609 case M_LDL_AB:
5610 s = "ldl";
5611 lr = 1;
5612 goto ld;
5613 case M_LDR_AB:
5614 s = "ldr";
5615 lr = 1;
5616 goto ld;
5617 case M_LL_AB:
5618 s = "ll";
5619 goto ld;
5620 case M_LLD_AB:
5621 s = "lld";
5622 goto ld;
5623 case M_LWU_AB:
5624 s = "lwu";
5625 ld:
5626 if (breg == treg || coproc || lr)
5627 {
5628 tempreg = AT;
5629 used_at = 1;
5630 }
5631 else
5632 {
5633 tempreg = treg;
5634 }
5635 goto ld_st;
5636 case M_SB_AB:
5637 s = "sb";
5638 goto st;
5639 case M_SH_AB:
5640 s = "sh";
5641 goto st;
5642 case M_SW_AB:
5643 s = "sw";
5644 goto st;
5645 case M_SWC0_AB:
5646 s = "swc0";
5647 /* Itbl support may require additional care here. */
5648 coproc = 1;
5649 goto st;
5650 case M_SWC1_AB:
5651 s = "swc1";
5652 /* Itbl support may require additional care here. */
5653 coproc = 1;
5654 goto st;
5655 case M_SWC2_AB:
5656 s = "swc2";
5657 /* Itbl support may require additional care here. */
5658 coproc = 1;
5659 goto st;
5660 case M_SWC3_AB:
5661 s = "swc3";
5662 /* Itbl support may require additional care here. */
5663 coproc = 1;
5664 goto st;
5665 case M_SWL_AB:
5666 s = "swl";
5667 goto st;
5668 case M_SWR_AB:
5669 s = "swr";
5670 goto st;
5671 case M_SC_AB:
5672 s = "sc";
5673 goto st;
5674 case M_SCD_AB:
5675 s = "scd";
5676 goto st;
5677 case M_SDC1_AB:
5678 if (mips_opts.arch == CPU_R4650)
5679 {
5680 as_bad (_("opcode not supported on this processor"));
5681 break;
5682 }
5683 s = "sdc1";
5684 coproc = 1;
5685 /* Itbl support may require additional care here. */
5686 goto st;
5687 case M_SDC2_AB:
5688 s = "sdc2";
5689 /* Itbl support may require additional care here. */
5690 coproc = 1;
5691 goto st;
5692 case M_SDC3_AB:
5693 s = "sdc3";
5694 /* Itbl support may require additional care here. */
5695 coproc = 1;
5696 goto st;
5697 case M_SDL_AB:
5698 s = "sdl";
5699 goto st;
5700 case M_SDR_AB:
5701 s = "sdr";
5702 st:
5703 tempreg = AT;
5704 used_at = 1;
5705 ld_st:
5706 /* Itbl support may require additional care here. */
5707 if (mask == M_LWC1_AB
5708 || mask == M_SWC1_AB
5709 || mask == M_LDC1_AB
5710 || mask == M_SDC1_AB
5711 || mask == M_L_DAB
5712 || mask == M_S_DAB)
5713 fmt = "T,o(b)";
5714 else if (coproc)
5715 fmt = "E,o(b)";
5716 else
5717 fmt = "t,o(b)";
5718
5719 if (offset_expr.X_op != O_constant
5720 && offset_expr.X_op != O_symbol)
5721 {
5722 as_bad (_("expression too complex"));
5723 offset_expr.X_op = O_constant;
5724 }
5725
5726 /* A constant expression in PIC code can be handled just as it
5727 is in non PIC code. */
5728 if (offset_expr.X_op == O_constant)
5729 {
5730 if (HAVE_32BIT_ADDRESSES
5731 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5732 as_bad (_("constant too large"));
5733
5734 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5735 & ~(bfd_vma) 0xffff);
5736 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5737 if (breg != 0)
5738 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5739 tempreg, tempreg, breg);
5740 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5741 }
5742 else if (mips_pic == NO_PIC)
5743 {
5744 /* If this is a reference to a GP relative symbol, and there
5745 is no base register, we want
5746 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5747 Otherwise, if there is no base register, we want
5748 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5749 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5750 If we have a constant, we need two instructions anyhow,
5751 so we always use the latter form.
5752
5753 If we have a base register, and this is a reference to a
5754 GP relative symbol, we want
5755 addu $tempreg,$breg,$gp
5756 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5757 Otherwise we want
5758 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5759 addu $tempreg,$tempreg,$breg
5760 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5761 With a constant we always use the latter case.
5762
5763 With 64bit address space and no base register and $at usable,
5764 we want
5765 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5766 lui $at,<sym> (BFD_RELOC_HI16_S)
5767 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5768 dsll32 $tempreg,0
5769 daddu $tempreg,$at
5770 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5771 If we have a base register, we want
5772 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5773 lui $at,<sym> (BFD_RELOC_HI16_S)
5774 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5775 daddu $at,$breg
5776 dsll32 $tempreg,0
5777 daddu $tempreg,$at
5778 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5779
5780 Without $at we can't generate the optimal path for superscalar
5781 processors here since this would require two temporary registers.
5782 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5783 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5784 dsll $tempreg,16
5785 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5786 dsll $tempreg,16
5787 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5788 If we have a base register, we want
5789 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5790 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5791 dsll $tempreg,16
5792 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5793 dsll $tempreg,16
5794 daddu $tempreg,$tempreg,$breg
5795 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5796
5797 For GP relative symbols in 64bit address space we can use
5798 the same sequence as in 32bit address space. */
5799 if (HAVE_64BIT_SYMBOLS)
5800 {
5801 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5802 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5803 {
5804 relax_start (offset_expr.X_add_symbol);
5805 if (breg == 0)
5806 {
5807 macro_build (&offset_expr, s, fmt, treg,
5808 BFD_RELOC_GPREL16, mips_gp_register);
5809 }
5810 else
5811 {
5812 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5813 tempreg, breg, mips_gp_register);
5814 macro_build (&offset_expr, s, fmt, treg,
5815 BFD_RELOC_GPREL16, tempreg);
5816 }
5817 relax_switch ();
5818 }
5819
5820 if (used_at == 0 && !mips_opts.noat)
5821 {
5822 macro_build (&offset_expr, "lui", "t,u", tempreg,
5823 BFD_RELOC_MIPS_HIGHEST);
5824 macro_build (&offset_expr, "lui", "t,u", AT,
5825 BFD_RELOC_HI16_S);
5826 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5827 tempreg, BFD_RELOC_MIPS_HIGHER);
5828 if (breg != 0)
5829 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5830 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5831 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5832 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5833 tempreg);
5834 used_at = 1;
5835 }
5836 else
5837 {
5838 macro_build (&offset_expr, "lui", "t,u", tempreg,
5839 BFD_RELOC_MIPS_HIGHEST);
5840 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5841 tempreg, BFD_RELOC_MIPS_HIGHER);
5842 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5843 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5844 tempreg, BFD_RELOC_HI16_S);
5845 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5846 if (breg != 0)
5847 macro_build (NULL, "daddu", "d,v,t",
5848 tempreg, tempreg, breg);
5849 macro_build (&offset_expr, s, fmt, treg,
5850 BFD_RELOC_LO16, tempreg);
5851 }
5852
5853 if (mips_relax.sequence)
5854 relax_end ();
5855 break;
5856 }
5857
5858 if (breg == 0)
5859 {
5860 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5861 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5862 {
5863 relax_start (offset_expr.X_add_symbol);
5864 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5865 mips_gp_register);
5866 relax_switch ();
5867 }
5868 macro_build_lui (&offset_expr, tempreg);
5869 macro_build (&offset_expr, s, fmt, treg,
5870 BFD_RELOC_LO16, tempreg);
5871 if (mips_relax.sequence)
5872 relax_end ();
5873 }
5874 else
5875 {
5876 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5877 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5878 {
5879 relax_start (offset_expr.X_add_symbol);
5880 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5881 tempreg, breg, mips_gp_register);
5882 macro_build (&offset_expr, s, fmt, treg,
5883 BFD_RELOC_GPREL16, tempreg);
5884 relax_switch ();
5885 }
5886 macro_build_lui (&offset_expr, tempreg);
5887 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5888 tempreg, tempreg, breg);
5889 macro_build (&offset_expr, s, fmt, treg,
5890 BFD_RELOC_LO16, tempreg);
5891 if (mips_relax.sequence)
5892 relax_end ();
5893 }
5894 }
5895 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5896 {
5897 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5898
5899 /* If this is a reference to an external symbol, we want
5900 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5901 nop
5902 <op> $treg,0($tempreg)
5903 Otherwise we want
5904 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5905 nop
5906 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5907 <op> $treg,0($tempreg)
5908
5909 For NewABI, we want
5910 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5911 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
5912
5913 If there is a base register, we add it to $tempreg before
5914 the <op>. If there is a constant, we stick it in the
5915 <op> instruction. We don't handle constants larger than
5916 16 bits, because we have no way to load the upper 16 bits
5917 (actually, we could handle them for the subset of cases
5918 in which we are not using $at). */
5919 assert (offset_expr.X_op == O_symbol);
5920 if (HAVE_NEWABI)
5921 {
5922 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5923 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5924 if (breg != 0)
5925 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5926 tempreg, tempreg, breg);
5927 macro_build (&offset_expr, s, fmt, treg,
5928 BFD_RELOC_MIPS_GOT_OFST, tempreg);
5929 break;
5930 }
5931 expr1.X_add_number = offset_expr.X_add_number;
5932 offset_expr.X_add_number = 0;
5933 if (expr1.X_add_number < -0x8000
5934 || expr1.X_add_number >= 0x8000)
5935 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5936 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5937 lw_reloc_type, mips_gp_register);
5938 load_delay_nop ();
5939 relax_start (offset_expr.X_add_symbol);
5940 relax_switch ();
5941 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5942 tempreg, BFD_RELOC_LO16);
5943 relax_end ();
5944 if (breg != 0)
5945 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5946 tempreg, tempreg, breg);
5947 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5948 }
5949 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5950 {
5951 int gpdelay;
5952
5953 /* If this is a reference to an external symbol, we want
5954 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5955 addu $tempreg,$tempreg,$gp
5956 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5957 <op> $treg,0($tempreg)
5958 Otherwise we want
5959 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5960 nop
5961 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5962 <op> $treg,0($tempreg)
5963 If there is a base register, we add it to $tempreg before
5964 the <op>. If there is a constant, we stick it in the
5965 <op> instruction. We don't handle constants larger than
5966 16 bits, because we have no way to load the upper 16 bits
5967 (actually, we could handle them for the subset of cases
5968 in which we are not using $at). */
5969 assert (offset_expr.X_op == O_symbol);
5970 expr1.X_add_number = offset_expr.X_add_number;
5971 offset_expr.X_add_number = 0;
5972 if (expr1.X_add_number < -0x8000
5973 || expr1.X_add_number >= 0x8000)
5974 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5975 gpdelay = reg_needs_delay (mips_gp_register);
5976 relax_start (offset_expr.X_add_symbol);
5977 macro_build (&offset_expr, "lui", "t,u", tempreg,
5978 BFD_RELOC_MIPS_GOT_HI16);
5979 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
5980 mips_gp_register);
5981 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5982 BFD_RELOC_MIPS_GOT_LO16, tempreg);
5983 relax_switch ();
5984 if (gpdelay)
5985 macro_build (NULL, "nop", "");
5986 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5987 BFD_RELOC_MIPS_GOT16, mips_gp_register);
5988 load_delay_nop ();
5989 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5990 tempreg, BFD_RELOC_LO16);
5991 relax_end ();
5992
5993 if (breg != 0)
5994 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5995 tempreg, tempreg, breg);
5996 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5997 }
5998 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5999 {
6000 /* If this is a reference to an external symbol, we want
6001 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6002 add $tempreg,$tempreg,$gp
6003 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6004 <op> $treg,<ofst>($tempreg)
6005 Otherwise, for local symbols, we want:
6006 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6007 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6008 assert (offset_expr.X_op == O_symbol);
6009 expr1.X_add_number = offset_expr.X_add_number;
6010 offset_expr.X_add_number = 0;
6011 if (expr1.X_add_number < -0x8000
6012 || expr1.X_add_number >= 0x8000)
6013 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6014 relax_start (offset_expr.X_add_symbol);
6015 macro_build (&offset_expr, "lui", "t,u", tempreg,
6016 BFD_RELOC_MIPS_GOT_HI16);
6017 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6018 mips_gp_register);
6019 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6020 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6021 if (breg != 0)
6022 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6023 tempreg, tempreg, breg);
6024 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6025
6026 relax_switch ();
6027 offset_expr.X_add_number = expr1.X_add_number;
6028 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6029 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6030 if (breg != 0)
6031 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6032 tempreg, tempreg, breg);
6033 macro_build (&offset_expr, s, fmt, treg,
6034 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6035 relax_end ();
6036 }
6037 else
6038 abort ();
6039
6040 break;
6041
6042 case M_LI:
6043 case M_LI_S:
6044 load_register (treg, &imm_expr, 0);
6045 break;
6046
6047 case M_DLI:
6048 load_register (treg, &imm_expr, 1);
6049 break;
6050
6051 case M_LI_SS:
6052 if (imm_expr.X_op == O_constant)
6053 {
6054 used_at = 1;
6055 load_register (AT, &imm_expr, 0);
6056 macro_build (NULL, "mtc1", "t,G", AT, treg);
6057 break;
6058 }
6059 else
6060 {
6061 assert (offset_expr.X_op == O_symbol
6062 && strcmp (segment_name (S_GET_SEGMENT
6063 (offset_expr.X_add_symbol)),
6064 ".lit4") == 0
6065 && offset_expr.X_add_number == 0);
6066 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6067 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6068 break;
6069 }
6070
6071 case M_LI_D:
6072 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6073 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6074 order 32 bits of the value and the low order 32 bits are either
6075 zero or in OFFSET_EXPR. */
6076 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6077 {
6078 if (HAVE_64BIT_GPRS)
6079 load_register (treg, &imm_expr, 1);
6080 else
6081 {
6082 int hreg, lreg;
6083
6084 if (target_big_endian)
6085 {
6086 hreg = treg;
6087 lreg = treg + 1;
6088 }
6089 else
6090 {
6091 hreg = treg + 1;
6092 lreg = treg;
6093 }
6094
6095 if (hreg <= 31)
6096 load_register (hreg, &imm_expr, 0);
6097 if (lreg <= 31)
6098 {
6099 if (offset_expr.X_op == O_absent)
6100 move_register (lreg, 0);
6101 else
6102 {
6103 assert (offset_expr.X_op == O_constant);
6104 load_register (lreg, &offset_expr, 0);
6105 }
6106 }
6107 }
6108 break;
6109 }
6110
6111 /* We know that sym is in the .rdata section. First we get the
6112 upper 16 bits of the address. */
6113 if (mips_pic == NO_PIC)
6114 {
6115 macro_build_lui (&offset_expr, AT);
6116 used_at = 1;
6117 }
6118 else if (mips_pic == SVR4_PIC)
6119 {
6120 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6121 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6122 used_at = 1;
6123 }
6124 else
6125 abort ();
6126
6127 /* Now we load the register(s). */
6128 if (HAVE_64BIT_GPRS)
6129 {
6130 used_at = 1;
6131 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6132 }
6133 else
6134 {
6135 used_at = 1;
6136 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6137 if (treg != RA)
6138 {
6139 /* FIXME: How in the world do we deal with the possible
6140 overflow here? */
6141 offset_expr.X_add_number += 4;
6142 macro_build (&offset_expr, "lw", "t,o(b)",
6143 treg + 1, BFD_RELOC_LO16, AT);
6144 }
6145 }
6146 break;
6147
6148 case M_LI_DD:
6149 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6150 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6151 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6152 the value and the low order 32 bits are either zero or in
6153 OFFSET_EXPR. */
6154 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6155 {
6156 used_at = 1;
6157 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6158 if (HAVE_64BIT_FPRS)
6159 {
6160 assert (HAVE_64BIT_GPRS);
6161 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6162 }
6163 else
6164 {
6165 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6166 if (offset_expr.X_op == O_absent)
6167 macro_build (NULL, "mtc1", "t,G", 0, treg);
6168 else
6169 {
6170 assert (offset_expr.X_op == O_constant);
6171 load_register (AT, &offset_expr, 0);
6172 macro_build (NULL, "mtc1", "t,G", AT, treg);
6173 }
6174 }
6175 break;
6176 }
6177
6178 assert (offset_expr.X_op == O_symbol
6179 && offset_expr.X_add_number == 0);
6180 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6181 if (strcmp (s, ".lit8") == 0)
6182 {
6183 if (mips_opts.isa != ISA_MIPS1)
6184 {
6185 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6186 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6187 break;
6188 }
6189 breg = mips_gp_register;
6190 r = BFD_RELOC_MIPS_LITERAL;
6191 goto dob;
6192 }
6193 else
6194 {
6195 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6196 used_at = 1;
6197 if (mips_pic == SVR4_PIC)
6198 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6199 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6200 else
6201 {
6202 /* FIXME: This won't work for a 64 bit address. */
6203 macro_build_lui (&offset_expr, AT);
6204 }
6205
6206 if (mips_opts.isa != ISA_MIPS1)
6207 {
6208 macro_build (&offset_expr, "ldc1", "T,o(b)",
6209 treg, BFD_RELOC_LO16, AT);
6210 break;
6211 }
6212 breg = AT;
6213 r = BFD_RELOC_LO16;
6214 goto dob;
6215 }
6216
6217 case M_L_DOB:
6218 if (mips_opts.arch == CPU_R4650)
6219 {
6220 as_bad (_("opcode not supported on this processor"));
6221 break;
6222 }
6223 /* Even on a big endian machine $fn comes before $fn+1. We have
6224 to adjust when loading from memory. */
6225 r = BFD_RELOC_LO16;
6226 dob:
6227 assert (mips_opts.isa == ISA_MIPS1);
6228 macro_build (&offset_expr, "lwc1", "T,o(b)",
6229 target_big_endian ? treg + 1 : treg, r, breg);
6230 /* FIXME: A possible overflow which I don't know how to deal
6231 with. */
6232 offset_expr.X_add_number += 4;
6233 macro_build (&offset_expr, "lwc1", "T,o(b)",
6234 target_big_endian ? treg : treg + 1, r, breg);
6235 break;
6236
6237 case M_L_DAB:
6238 /*
6239 * The MIPS assembler seems to check for X_add_number not
6240 * being double aligned and generating:
6241 * lui at,%hi(foo+1)
6242 * addu at,at,v1
6243 * addiu at,at,%lo(foo+1)
6244 * lwc1 f2,0(at)
6245 * lwc1 f3,4(at)
6246 * But, the resulting address is the same after relocation so why
6247 * generate the extra instruction?
6248 */
6249 if (mips_opts.arch == CPU_R4650)
6250 {
6251 as_bad (_("opcode not supported on this processor"));
6252 break;
6253 }
6254 /* Itbl support may require additional care here. */
6255 coproc = 1;
6256 if (mips_opts.isa != ISA_MIPS1)
6257 {
6258 s = "ldc1";
6259 goto ld;
6260 }
6261
6262 s = "lwc1";
6263 fmt = "T,o(b)";
6264 goto ldd_std;
6265
6266 case M_S_DAB:
6267 if (mips_opts.arch == CPU_R4650)
6268 {
6269 as_bad (_("opcode not supported on this processor"));
6270 break;
6271 }
6272
6273 if (mips_opts.isa != ISA_MIPS1)
6274 {
6275 s = "sdc1";
6276 goto st;
6277 }
6278
6279 s = "swc1";
6280 fmt = "T,o(b)";
6281 /* Itbl support may require additional care here. */
6282 coproc = 1;
6283 goto ldd_std;
6284
6285 case M_LD_AB:
6286 if (HAVE_64BIT_GPRS)
6287 {
6288 s = "ld";
6289 goto ld;
6290 }
6291
6292 s = "lw";
6293 fmt = "t,o(b)";
6294 goto ldd_std;
6295
6296 case M_SD_AB:
6297 if (HAVE_64BIT_GPRS)
6298 {
6299 s = "sd";
6300 goto st;
6301 }
6302
6303 s = "sw";
6304 fmt = "t,o(b)";
6305
6306 ldd_std:
6307 if (offset_expr.X_op != O_symbol
6308 && offset_expr.X_op != O_constant)
6309 {
6310 as_bad (_("expression too complex"));
6311 offset_expr.X_op = O_constant;
6312 }
6313
6314 /* Even on a big endian machine $fn comes before $fn+1. We have
6315 to adjust when loading from memory. We set coproc if we must
6316 load $fn+1 first. */
6317 /* Itbl support may require additional care here. */
6318 if (! target_big_endian)
6319 coproc = 0;
6320
6321 if (mips_pic == NO_PIC
6322 || offset_expr.X_op == O_constant)
6323 {
6324 /* If this is a reference to a GP relative symbol, we want
6325 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6326 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6327 If we have a base register, we use this
6328 addu $at,$breg,$gp
6329 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6330 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6331 If this is not a GP relative symbol, we want
6332 lui $at,<sym> (BFD_RELOC_HI16_S)
6333 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6334 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6335 If there is a base register, we add it to $at after the
6336 lui instruction. If there is a constant, we always use
6337 the last case. */
6338 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6339 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6340 {
6341 relax_start (offset_expr.X_add_symbol);
6342 if (breg == 0)
6343 {
6344 tempreg = mips_gp_register;
6345 }
6346 else
6347 {
6348 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6349 AT, breg, mips_gp_register);
6350 tempreg = AT;
6351 used_at = 1;
6352 }
6353
6354 /* Itbl support may require additional care here. */
6355 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6356 BFD_RELOC_GPREL16, tempreg);
6357 offset_expr.X_add_number += 4;
6358
6359 /* Set mips_optimize to 2 to avoid inserting an
6360 undesired nop. */
6361 hold_mips_optimize = mips_optimize;
6362 mips_optimize = 2;
6363 /* Itbl support may require additional care here. */
6364 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6365 BFD_RELOC_GPREL16, tempreg);
6366 mips_optimize = hold_mips_optimize;
6367
6368 relax_switch ();
6369
6370 /* We just generated two relocs. When tc_gen_reloc
6371 handles this case, it will skip the first reloc and
6372 handle the second. The second reloc already has an
6373 extra addend of 4, which we added above. We must
6374 subtract it out, and then subtract another 4 to make
6375 the first reloc come out right. The second reloc
6376 will come out right because we are going to add 4 to
6377 offset_expr when we build its instruction below.
6378
6379 If we have a symbol, then we don't want to include
6380 the offset, because it will wind up being included
6381 when we generate the reloc. */
6382
6383 if (offset_expr.X_op == O_constant)
6384 offset_expr.X_add_number -= 8;
6385 else
6386 {
6387 offset_expr.X_add_number = -4;
6388 offset_expr.X_op = O_constant;
6389 }
6390 }
6391 used_at = 1;
6392 macro_build_lui (&offset_expr, AT);
6393 if (breg != 0)
6394 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6395 /* Itbl support may require additional care here. */
6396 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6397 BFD_RELOC_LO16, AT);
6398 /* FIXME: How do we handle overflow here? */
6399 offset_expr.X_add_number += 4;
6400 /* Itbl support may require additional care here. */
6401 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6402 BFD_RELOC_LO16, AT);
6403 if (mips_relax.sequence)
6404 relax_end ();
6405 }
6406 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6407 {
6408 /* If this is a reference to an external symbol, we want
6409 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6410 nop
6411 <op> $treg,0($at)
6412 <op> $treg+1,4($at)
6413 Otherwise we want
6414 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6415 nop
6416 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6417 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6418 If there is a base register we add it to $at before the
6419 lwc1 instructions. If there is a constant we include it
6420 in the lwc1 instructions. */
6421 used_at = 1;
6422 expr1.X_add_number = offset_expr.X_add_number;
6423 if (expr1.X_add_number < -0x8000
6424 || expr1.X_add_number >= 0x8000 - 4)
6425 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6426 load_got_offset (AT, &offset_expr);
6427 load_delay_nop ();
6428 if (breg != 0)
6429 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6430
6431 /* Set mips_optimize to 2 to avoid inserting an undesired
6432 nop. */
6433 hold_mips_optimize = mips_optimize;
6434 mips_optimize = 2;
6435
6436 /* Itbl support may require additional care here. */
6437 relax_start (offset_expr.X_add_symbol);
6438 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6439 BFD_RELOC_LO16, AT);
6440 expr1.X_add_number += 4;
6441 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6442 BFD_RELOC_LO16, AT);
6443 relax_switch ();
6444 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6445 BFD_RELOC_LO16, AT);
6446 offset_expr.X_add_number += 4;
6447 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6448 BFD_RELOC_LO16, AT);
6449 relax_end ();
6450
6451 mips_optimize = hold_mips_optimize;
6452 }
6453 else if (mips_pic == SVR4_PIC)
6454 {
6455 int gpdelay;
6456
6457 /* If this is a reference to an external symbol, we want
6458 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6459 addu $at,$at,$gp
6460 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6461 nop
6462 <op> $treg,0($at)
6463 <op> $treg+1,4($at)
6464 Otherwise we want
6465 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6466 nop
6467 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6468 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6469 If there is a base register we add it to $at before the
6470 lwc1 instructions. If there is a constant we include it
6471 in the lwc1 instructions. */
6472 used_at = 1;
6473 expr1.X_add_number = offset_expr.X_add_number;
6474 offset_expr.X_add_number = 0;
6475 if (expr1.X_add_number < -0x8000
6476 || expr1.X_add_number >= 0x8000 - 4)
6477 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6478 gpdelay = reg_needs_delay (mips_gp_register);
6479 relax_start (offset_expr.X_add_symbol);
6480 macro_build (&offset_expr, "lui", "t,u",
6481 AT, BFD_RELOC_MIPS_GOT_HI16);
6482 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6483 AT, AT, mips_gp_register);
6484 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6485 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6486 load_delay_nop ();
6487 if (breg != 0)
6488 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6489 /* Itbl support may require additional care here. */
6490 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6491 BFD_RELOC_LO16, AT);
6492 expr1.X_add_number += 4;
6493
6494 /* Set mips_optimize to 2 to avoid inserting an undesired
6495 nop. */
6496 hold_mips_optimize = mips_optimize;
6497 mips_optimize = 2;
6498 /* Itbl support may require additional care here. */
6499 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6500 BFD_RELOC_LO16, AT);
6501 mips_optimize = hold_mips_optimize;
6502 expr1.X_add_number -= 4;
6503
6504 relax_switch ();
6505 offset_expr.X_add_number = expr1.X_add_number;
6506 if (gpdelay)
6507 macro_build (NULL, "nop", "");
6508 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6509 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6510 load_delay_nop ();
6511 if (breg != 0)
6512 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6513 /* Itbl support may require additional care here. */
6514 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6515 BFD_RELOC_LO16, AT);
6516 offset_expr.X_add_number += 4;
6517
6518 /* Set mips_optimize to 2 to avoid inserting an undesired
6519 nop. */
6520 hold_mips_optimize = mips_optimize;
6521 mips_optimize = 2;
6522 /* Itbl support may require additional care here. */
6523 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6524 BFD_RELOC_LO16, AT);
6525 mips_optimize = hold_mips_optimize;
6526 relax_end ();
6527 }
6528 else
6529 abort ();
6530
6531 break;
6532
6533 case M_LD_OB:
6534 s = "lw";
6535 goto sd_ob;
6536 case M_SD_OB:
6537 s = "sw";
6538 sd_ob:
6539 assert (HAVE_32BIT_ADDRESSES);
6540 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6541 offset_expr.X_add_number += 4;
6542 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6543 break;
6544
6545 /* New code added to support COPZ instructions.
6546 This code builds table entries out of the macros in mip_opcodes.
6547 R4000 uses interlocks to handle coproc delays.
6548 Other chips (like the R3000) require nops to be inserted for delays.
6549
6550 FIXME: Currently, we require that the user handle delays.
6551 In order to fill delay slots for non-interlocked chips,
6552 we must have a way to specify delays based on the coprocessor.
6553 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6554 What are the side-effects of the cop instruction?
6555 What cache support might we have and what are its effects?
6556 Both coprocessor & memory require delays. how long???
6557 What registers are read/set/modified?
6558
6559 If an itbl is provided to interpret cop instructions,
6560 this knowledge can be encoded in the itbl spec. */
6561
6562 case M_COP0:
6563 s = "c0";
6564 goto copz;
6565 case M_COP1:
6566 s = "c1";
6567 goto copz;
6568 case M_COP2:
6569 s = "c2";
6570 goto copz;
6571 case M_COP3:
6572 s = "c3";
6573 copz:
6574 /* For now we just do C (same as Cz). The parameter will be
6575 stored in insn_opcode by mips_ip. */
6576 macro_build (NULL, s, "C", ip->insn_opcode);
6577 break;
6578
6579 case M_MOVE:
6580 move_register (dreg, sreg);
6581 break;
6582
6583 #ifdef LOSING_COMPILER
6584 default:
6585 /* Try and see if this is a new itbl instruction.
6586 This code builds table entries out of the macros in mip_opcodes.
6587 FIXME: For now we just assemble the expression and pass it's
6588 value along as a 32-bit immediate.
6589 We may want to have the assembler assemble this value,
6590 so that we gain the assembler's knowledge of delay slots,
6591 symbols, etc.
6592 Would it be more efficient to use mask (id) here? */
6593 if (itbl_have_entries
6594 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6595 {
6596 s = ip->insn_mo->name;
6597 s2 = "cop3";
6598 coproc = ITBL_DECODE_PNUM (immed_expr);;
6599 macro_build (&immed_expr, s, "C");
6600 break;
6601 }
6602 macro2 (ip);
6603 break;
6604 }
6605 if (mips_opts.noat && used_at)
6606 as_bad (_("Macro used $at after \".set noat\""));
6607 }
6608
6609 static void
6610 macro2 (struct mips_cl_insn *ip)
6611 {
6612 register int treg, sreg, dreg, breg;
6613 int tempreg;
6614 int mask;
6615 int used_at;
6616 expressionS expr1;
6617 const char *s;
6618 const char *s2;
6619 const char *fmt;
6620 int likely = 0;
6621 int dbl = 0;
6622 int coproc = 0;
6623 int lr = 0;
6624 int imm = 0;
6625 int off;
6626 offsetT maxnum;
6627 bfd_reloc_code_real_type r;
6628
6629 treg = (ip->insn_opcode >> 16) & 0x1f;
6630 dreg = (ip->insn_opcode >> 11) & 0x1f;
6631 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6632 mask = ip->insn_mo->mask;
6633
6634 expr1.X_op = O_constant;
6635 expr1.X_op_symbol = NULL;
6636 expr1.X_add_symbol = NULL;
6637 expr1.X_add_number = 1;
6638
6639 switch (mask)
6640 {
6641 #endif /* LOSING_COMPILER */
6642
6643 case M_DMUL:
6644 dbl = 1;
6645 case M_MUL:
6646 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6647 macro_build (NULL, "mflo", "d", dreg);
6648 break;
6649
6650 case M_DMUL_I:
6651 dbl = 1;
6652 case M_MUL_I:
6653 /* The MIPS assembler some times generates shifts and adds. I'm
6654 not trying to be that fancy. GCC should do this for us
6655 anyway. */
6656 used_at = 1;
6657 load_register (AT, &imm_expr, dbl);
6658 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6659 macro_build (NULL, "mflo", "d", dreg);
6660 break;
6661
6662 case M_DMULO_I:
6663 dbl = 1;
6664 case M_MULO_I:
6665 imm = 1;
6666 goto do_mulo;
6667
6668 case M_DMULO:
6669 dbl = 1;
6670 case M_MULO:
6671 do_mulo:
6672 mips_emit_delays (TRUE);
6673 ++mips_opts.noreorder;
6674 mips_any_noreorder = 1;
6675 used_at = 1;
6676 if (imm)
6677 load_register (AT, &imm_expr, dbl);
6678 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6679 macro_build (NULL, "mflo", "d", dreg);
6680 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6681 macro_build (NULL, "mfhi", "d", AT);
6682 if (mips_trap)
6683 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6684 else
6685 {
6686 expr1.X_add_number = 8;
6687 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6688 macro_build (NULL, "nop", "", 0);
6689 macro_build (NULL, "break", "c", 6);
6690 }
6691 --mips_opts.noreorder;
6692 macro_build (NULL, "mflo", "d", dreg);
6693 break;
6694
6695 case M_DMULOU_I:
6696 dbl = 1;
6697 case M_MULOU_I:
6698 imm = 1;
6699 goto do_mulou;
6700
6701 case M_DMULOU:
6702 dbl = 1;
6703 case M_MULOU:
6704 do_mulou:
6705 mips_emit_delays (TRUE);
6706 ++mips_opts.noreorder;
6707 mips_any_noreorder = 1;
6708 used_at = 1;
6709 if (imm)
6710 load_register (AT, &imm_expr, dbl);
6711 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6712 sreg, imm ? AT : treg);
6713 macro_build (NULL, "mfhi", "d", AT);
6714 macro_build (NULL, "mflo", "d", dreg);
6715 if (mips_trap)
6716 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6717 else
6718 {
6719 expr1.X_add_number = 8;
6720 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6721 macro_build (NULL, "nop", "", 0);
6722 macro_build (NULL, "break", "c", 6);
6723 }
6724 --mips_opts.noreorder;
6725 break;
6726
6727 case M_DROL:
6728 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6729 {
6730 if (dreg == sreg)
6731 {
6732 tempreg = AT;
6733 used_at = 1;
6734 }
6735 else
6736 {
6737 tempreg = dreg;
6738 }
6739 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6740 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6741 break;
6742 }
6743 used_at = 1;
6744 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6745 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6746 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6747 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6748 break;
6749
6750 case M_ROL:
6751 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6752 {
6753 if (dreg == sreg)
6754 {
6755 tempreg = AT;
6756 used_at = 1;
6757 }
6758 else
6759 {
6760 tempreg = dreg;
6761 }
6762 macro_build (NULL, "negu", "d,w", tempreg, treg);
6763 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6764 break;
6765 }
6766 used_at = 1;
6767 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6768 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6769 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6770 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6771 break;
6772
6773 case M_DROL_I:
6774 {
6775 unsigned int rot;
6776 char *l, *r;
6777
6778 if (imm_expr.X_op != O_constant)
6779 as_bad (_("Improper rotate count"));
6780 rot = imm_expr.X_add_number & 0x3f;
6781 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6782 {
6783 rot = (64 - rot) & 0x3f;
6784 if (rot >= 32)
6785 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6786 else
6787 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6788 break;
6789 }
6790 if (rot == 0)
6791 {
6792 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6793 break;
6794 }
6795 l = (rot < 0x20) ? "dsll" : "dsll32";
6796 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6797 rot &= 0x1f;
6798 used_at = 1;
6799 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6800 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6801 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6802 }
6803 break;
6804
6805 case M_ROL_I:
6806 {
6807 unsigned int rot;
6808
6809 if (imm_expr.X_op != O_constant)
6810 as_bad (_("Improper rotate count"));
6811 rot = imm_expr.X_add_number & 0x1f;
6812 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6813 {
6814 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6815 break;
6816 }
6817 if (rot == 0)
6818 {
6819 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6820 break;
6821 }
6822 used_at = 1;
6823 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6824 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6825 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6826 }
6827 break;
6828
6829 case M_DROR:
6830 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6831 {
6832 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6833 break;
6834 }
6835 used_at = 1;
6836 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6837 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6838 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6839 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6840 break;
6841
6842 case M_ROR:
6843 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6844 {
6845 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
6846 break;
6847 }
6848 used_at = 1;
6849 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6850 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6851 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6852 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6853 break;
6854
6855 case M_DROR_I:
6856 {
6857 unsigned int rot;
6858 char *l, *r;
6859
6860 if (imm_expr.X_op != O_constant)
6861 as_bad (_("Improper rotate count"));
6862 rot = imm_expr.X_add_number & 0x3f;
6863 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6864 {
6865 if (rot >= 32)
6866 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6867 else
6868 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6869 break;
6870 }
6871 if (rot == 0)
6872 {
6873 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6874 break;
6875 }
6876 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6877 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6878 rot &= 0x1f;
6879 used_at = 1;
6880 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
6881 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6882 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6883 }
6884 break;
6885
6886 case M_ROR_I:
6887 {
6888 unsigned int rot;
6889
6890 if (imm_expr.X_op != O_constant)
6891 as_bad (_("Improper rotate count"));
6892 rot = imm_expr.X_add_number & 0x1f;
6893 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6894 {
6895 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
6896 break;
6897 }
6898 if (rot == 0)
6899 {
6900 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6901 break;
6902 }
6903 used_at = 1;
6904 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
6905 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6906 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6907 }
6908 break;
6909
6910 case M_S_DOB:
6911 if (mips_opts.arch == CPU_R4650)
6912 {
6913 as_bad (_("opcode not supported on this processor"));
6914 break;
6915 }
6916 assert (mips_opts.isa == ISA_MIPS1);
6917 /* Even on a big endian machine $fn comes before $fn+1. We have
6918 to adjust when storing to memory. */
6919 macro_build (&offset_expr, "swc1", "T,o(b)",
6920 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
6921 offset_expr.X_add_number += 4;
6922 macro_build (&offset_expr, "swc1", "T,o(b)",
6923 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
6924 break;
6925
6926 case M_SEQ:
6927 if (sreg == 0)
6928 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
6929 else if (treg == 0)
6930 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6931 else
6932 {
6933 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
6934 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
6935 }
6936 break;
6937
6938 case M_SEQ_I:
6939 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6940 {
6941 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6942 break;
6943 }
6944 if (sreg == 0)
6945 {
6946 as_warn (_("Instruction %s: result is always false"),
6947 ip->insn_mo->name);
6948 move_register (dreg, 0);
6949 break;
6950 }
6951 if (imm_expr.X_op == O_constant
6952 && imm_expr.X_add_number >= 0
6953 && imm_expr.X_add_number < 0x10000)
6954 {
6955 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
6956 }
6957 else if (imm_expr.X_op == O_constant
6958 && imm_expr.X_add_number > -0x8000
6959 && imm_expr.X_add_number < 0)
6960 {
6961 imm_expr.X_add_number = -imm_expr.X_add_number;
6962 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6963 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6964 }
6965 else
6966 {
6967 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6968 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
6969 used_at = 1;
6970 }
6971 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
6972 break;
6973
6974 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6975 s = "slt";
6976 goto sge;
6977 case M_SGEU:
6978 s = "sltu";
6979 sge:
6980 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
6981 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
6982 break;
6983
6984 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6985 case M_SGEU_I:
6986 if (imm_expr.X_op == O_constant
6987 && imm_expr.X_add_number >= -0x8000
6988 && imm_expr.X_add_number < 0x8000)
6989 {
6990 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
6991 dreg, sreg, BFD_RELOC_LO16);
6992 }
6993 else
6994 {
6995 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6996 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
6997 dreg, sreg, AT);
6998 used_at = 1;
6999 }
7000 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7001 break;
7002
7003 case M_SGT: /* sreg > treg <==> treg < sreg */
7004 s = "slt";
7005 goto sgt;
7006 case M_SGTU:
7007 s = "sltu";
7008 sgt:
7009 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7010 break;
7011
7012 case M_SGT_I: /* sreg > I <==> I < sreg */
7013 s = "slt";
7014 goto sgti;
7015 case M_SGTU_I:
7016 s = "sltu";
7017 sgti:
7018 used_at = 1;
7019 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7020 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7021 break;
7022
7023 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7024 s = "slt";
7025 goto sle;
7026 case M_SLEU:
7027 s = "sltu";
7028 sle:
7029 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7030 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7031 break;
7032
7033 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7034 s = "slt";
7035 goto slei;
7036 case M_SLEU_I:
7037 s = "sltu";
7038 slei:
7039 used_at = 1;
7040 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7041 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7042 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7043 break;
7044
7045 case M_SLT_I:
7046 if (imm_expr.X_op == O_constant
7047 && imm_expr.X_add_number >= -0x8000
7048 && imm_expr.X_add_number < 0x8000)
7049 {
7050 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7051 break;
7052 }
7053 used_at = 1;
7054 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7055 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7056 break;
7057
7058 case M_SLTU_I:
7059 if (imm_expr.X_op == O_constant
7060 && imm_expr.X_add_number >= -0x8000
7061 && imm_expr.X_add_number < 0x8000)
7062 {
7063 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7064 BFD_RELOC_LO16);
7065 break;
7066 }
7067 used_at = 1;
7068 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7069 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7070 break;
7071
7072 case M_SNE:
7073 if (sreg == 0)
7074 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7075 else if (treg == 0)
7076 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7077 else
7078 {
7079 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7080 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7081 }
7082 break;
7083
7084 case M_SNE_I:
7085 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7086 {
7087 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7088 break;
7089 }
7090 if (sreg == 0)
7091 {
7092 as_warn (_("Instruction %s: result is always true"),
7093 ip->insn_mo->name);
7094 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7095 dreg, 0, BFD_RELOC_LO16);
7096 break;
7097 }
7098 if (imm_expr.X_op == O_constant
7099 && imm_expr.X_add_number >= 0
7100 && imm_expr.X_add_number < 0x10000)
7101 {
7102 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7103 }
7104 else if (imm_expr.X_op == O_constant
7105 && imm_expr.X_add_number > -0x8000
7106 && imm_expr.X_add_number < 0)
7107 {
7108 imm_expr.X_add_number = -imm_expr.X_add_number;
7109 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7110 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7111 }
7112 else
7113 {
7114 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7115 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7116 used_at = 1;
7117 }
7118 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7119 break;
7120
7121 case M_DSUB_I:
7122 dbl = 1;
7123 case M_SUB_I:
7124 if (imm_expr.X_op == O_constant
7125 && imm_expr.X_add_number > -0x8000
7126 && imm_expr.X_add_number <= 0x8000)
7127 {
7128 imm_expr.X_add_number = -imm_expr.X_add_number;
7129 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7130 dreg, sreg, BFD_RELOC_LO16);
7131 break;
7132 }
7133 used_at = 1;
7134 load_register (AT, &imm_expr, dbl);
7135 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7136 break;
7137
7138 case M_DSUBU_I:
7139 dbl = 1;
7140 case M_SUBU_I:
7141 if (imm_expr.X_op == O_constant
7142 && imm_expr.X_add_number > -0x8000
7143 && imm_expr.X_add_number <= 0x8000)
7144 {
7145 imm_expr.X_add_number = -imm_expr.X_add_number;
7146 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7147 dreg, sreg, BFD_RELOC_LO16);
7148 break;
7149 }
7150 used_at = 1;
7151 load_register (AT, &imm_expr, dbl);
7152 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7153 break;
7154
7155 case M_TEQ_I:
7156 s = "teq";
7157 goto trap;
7158 case M_TGE_I:
7159 s = "tge";
7160 goto trap;
7161 case M_TGEU_I:
7162 s = "tgeu";
7163 goto trap;
7164 case M_TLT_I:
7165 s = "tlt";
7166 goto trap;
7167 case M_TLTU_I:
7168 s = "tltu";
7169 goto trap;
7170 case M_TNE_I:
7171 s = "tne";
7172 trap:
7173 used_at = 1;
7174 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7175 macro_build (NULL, s, "s,t", sreg, AT);
7176 break;
7177
7178 case M_TRUNCWS:
7179 case M_TRUNCWD:
7180 assert (mips_opts.isa == ISA_MIPS1);
7181 used_at = 1;
7182 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7183 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7184
7185 /*
7186 * Is the double cfc1 instruction a bug in the mips assembler;
7187 * or is there a reason for it?
7188 */
7189 mips_emit_delays (TRUE);
7190 ++mips_opts.noreorder;
7191 mips_any_noreorder = 1;
7192 macro_build (NULL, "cfc1", "t,G", treg, RA);
7193 macro_build (NULL, "cfc1", "t,G", treg, RA);
7194 macro_build (NULL, "nop", "");
7195 expr1.X_add_number = 3;
7196 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7197 expr1.X_add_number = 2;
7198 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7199 macro_build (NULL, "ctc1", "t,G", AT, RA);
7200 macro_build (NULL, "nop", "");
7201 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7202 dreg, sreg);
7203 macro_build (NULL, "ctc1", "t,G", treg, RA);
7204 macro_build (NULL, "nop", "");
7205 --mips_opts.noreorder;
7206 break;
7207
7208 case M_ULH:
7209 s = "lb";
7210 goto ulh;
7211 case M_ULHU:
7212 s = "lbu";
7213 ulh:
7214 used_at = 1;
7215 if (offset_expr.X_add_number >= 0x7fff)
7216 as_bad (_("operand overflow"));
7217 if (! target_big_endian)
7218 ++offset_expr.X_add_number;
7219 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7220 if (! target_big_endian)
7221 --offset_expr.X_add_number;
7222 else
7223 ++offset_expr.X_add_number;
7224 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7225 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7226 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7227 break;
7228
7229 case M_ULD:
7230 s = "ldl";
7231 s2 = "ldr";
7232 off = 7;
7233 goto ulw;
7234 case M_ULW:
7235 s = "lwl";
7236 s2 = "lwr";
7237 off = 3;
7238 ulw:
7239 if (offset_expr.X_add_number >= 0x8000 - off)
7240 as_bad (_("operand overflow"));
7241 if (treg != breg)
7242 tempreg = treg;
7243 else
7244 {
7245 used_at = 1;
7246 tempreg = AT;
7247 }
7248 if (! target_big_endian)
7249 offset_expr.X_add_number += off;
7250 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7251 if (! target_big_endian)
7252 offset_expr.X_add_number -= off;
7253 else
7254 offset_expr.X_add_number += off;
7255 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7256
7257 /* If necessary, move the result in tempreg the final destination. */
7258 if (treg == tempreg)
7259 break;
7260 /* Protect second load's delay slot. */
7261 load_delay_nop ();
7262 move_register (treg, tempreg);
7263 break;
7264
7265 case M_ULD_A:
7266 s = "ldl";
7267 s2 = "ldr";
7268 off = 7;
7269 goto ulwa;
7270 case M_ULW_A:
7271 s = "lwl";
7272 s2 = "lwr";
7273 off = 3;
7274 ulwa:
7275 used_at = 1;
7276 load_address (AT, &offset_expr, &used_at);
7277 if (breg != 0)
7278 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7279 if (! target_big_endian)
7280 expr1.X_add_number = off;
7281 else
7282 expr1.X_add_number = 0;
7283 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7284 if (! target_big_endian)
7285 expr1.X_add_number = 0;
7286 else
7287 expr1.X_add_number = off;
7288 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7289 break;
7290
7291 case M_ULH_A:
7292 case M_ULHU_A:
7293 used_at = 1;
7294 load_address (AT, &offset_expr, &used_at);
7295 if (breg != 0)
7296 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7297 if (target_big_endian)
7298 expr1.X_add_number = 0;
7299 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7300 treg, BFD_RELOC_LO16, AT);
7301 if (target_big_endian)
7302 expr1.X_add_number = 1;
7303 else
7304 expr1.X_add_number = 0;
7305 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7306 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7307 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7308 break;
7309
7310 case M_USH:
7311 used_at = 1;
7312 if (offset_expr.X_add_number >= 0x7fff)
7313 as_bad (_("operand overflow"));
7314 if (target_big_endian)
7315 ++offset_expr.X_add_number;
7316 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7317 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7318 if (target_big_endian)
7319 --offset_expr.X_add_number;
7320 else
7321 ++offset_expr.X_add_number;
7322 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7323 break;
7324
7325 case M_USD:
7326 s = "sdl";
7327 s2 = "sdr";
7328 off = 7;
7329 goto usw;
7330 case M_USW:
7331 s = "swl";
7332 s2 = "swr";
7333 off = 3;
7334 usw:
7335 if (offset_expr.X_add_number >= 0x8000 - off)
7336 as_bad (_("operand overflow"));
7337 if (! target_big_endian)
7338 offset_expr.X_add_number += off;
7339 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7340 if (! target_big_endian)
7341 offset_expr.X_add_number -= off;
7342 else
7343 offset_expr.X_add_number += off;
7344 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7345 break;
7346
7347 case M_USD_A:
7348 s = "sdl";
7349 s2 = "sdr";
7350 off = 7;
7351 goto uswa;
7352 case M_USW_A:
7353 s = "swl";
7354 s2 = "swr";
7355 off = 3;
7356 uswa:
7357 used_at = 1;
7358 load_address (AT, &offset_expr, &used_at);
7359 if (breg != 0)
7360 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7361 if (! target_big_endian)
7362 expr1.X_add_number = off;
7363 else
7364 expr1.X_add_number = 0;
7365 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7366 if (! target_big_endian)
7367 expr1.X_add_number = 0;
7368 else
7369 expr1.X_add_number = off;
7370 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7371 break;
7372
7373 case M_USH_A:
7374 used_at = 1;
7375 load_address (AT, &offset_expr, &used_at);
7376 if (breg != 0)
7377 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7378 if (! target_big_endian)
7379 expr1.X_add_number = 0;
7380 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7381 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7382 if (! target_big_endian)
7383 expr1.X_add_number = 1;
7384 else
7385 expr1.X_add_number = 0;
7386 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7387 if (! target_big_endian)
7388 expr1.X_add_number = 0;
7389 else
7390 expr1.X_add_number = 1;
7391 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7392 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7393 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7394 break;
7395
7396 default:
7397 /* FIXME: Check if this is one of the itbl macros, since they
7398 are added dynamically. */
7399 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7400 break;
7401 }
7402 if (mips_opts.noat && used_at)
7403 as_bad (_("Macro used $at after \".set noat\""));
7404 }
7405
7406 /* Implement macros in mips16 mode. */
7407
7408 static void
7409 mips16_macro (struct mips_cl_insn *ip)
7410 {
7411 int mask;
7412 int xreg, yreg, zreg, tmp;
7413 expressionS expr1;
7414 int dbl;
7415 const char *s, *s2, *s3;
7416
7417 mask = ip->insn_mo->mask;
7418
7419 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7420 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7421 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7422
7423 expr1.X_op = O_constant;
7424 expr1.X_op_symbol = NULL;
7425 expr1.X_add_symbol = NULL;
7426 expr1.X_add_number = 1;
7427
7428 dbl = 0;
7429
7430 switch (mask)
7431 {
7432 default:
7433 internalError ();
7434
7435 case M_DDIV_3:
7436 dbl = 1;
7437 case M_DIV_3:
7438 s = "mflo";
7439 goto do_div3;
7440 case M_DREM_3:
7441 dbl = 1;
7442 case M_REM_3:
7443 s = "mfhi";
7444 do_div3:
7445 mips_emit_delays (TRUE);
7446 ++mips_opts.noreorder;
7447 mips_any_noreorder = 1;
7448 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7449 expr1.X_add_number = 2;
7450 macro_build (&expr1, "bnez", "x,p", yreg);
7451 macro_build (NULL, "break", "6", 7);
7452
7453 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7454 since that causes an overflow. We should do that as well,
7455 but I don't see how to do the comparisons without a temporary
7456 register. */
7457 --mips_opts.noreorder;
7458 macro_build (NULL, s, "x", zreg);
7459 break;
7460
7461 case M_DIVU_3:
7462 s = "divu";
7463 s2 = "mflo";
7464 goto do_divu3;
7465 case M_REMU_3:
7466 s = "divu";
7467 s2 = "mfhi";
7468 goto do_divu3;
7469 case M_DDIVU_3:
7470 s = "ddivu";
7471 s2 = "mflo";
7472 goto do_divu3;
7473 case M_DREMU_3:
7474 s = "ddivu";
7475 s2 = "mfhi";
7476 do_divu3:
7477 mips_emit_delays (TRUE);
7478 ++mips_opts.noreorder;
7479 mips_any_noreorder = 1;
7480 macro_build (NULL, s, "0,x,y", xreg, yreg);
7481 expr1.X_add_number = 2;
7482 macro_build (&expr1, "bnez", "x,p", yreg);
7483 macro_build (NULL, "break", "6", 7);
7484 --mips_opts.noreorder;
7485 macro_build (NULL, s2, "x", zreg);
7486 break;
7487
7488 case M_DMUL:
7489 dbl = 1;
7490 case M_MUL:
7491 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7492 macro_build (NULL, "mflo", "x", zreg);
7493 break;
7494
7495 case M_DSUBU_I:
7496 dbl = 1;
7497 goto do_subu;
7498 case M_SUBU_I:
7499 do_subu:
7500 if (imm_expr.X_op != O_constant)
7501 as_bad (_("Unsupported large constant"));
7502 imm_expr.X_add_number = -imm_expr.X_add_number;
7503 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7504 break;
7505
7506 case M_SUBU_I_2:
7507 if (imm_expr.X_op != O_constant)
7508 as_bad (_("Unsupported large constant"));
7509 imm_expr.X_add_number = -imm_expr.X_add_number;
7510 macro_build (&imm_expr, "addiu", "x,k", xreg);
7511 break;
7512
7513 case M_DSUBU_I_2:
7514 if (imm_expr.X_op != O_constant)
7515 as_bad (_("Unsupported large constant"));
7516 imm_expr.X_add_number = -imm_expr.X_add_number;
7517 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7518 break;
7519
7520 case M_BEQ:
7521 s = "cmp";
7522 s2 = "bteqz";
7523 goto do_branch;
7524 case M_BNE:
7525 s = "cmp";
7526 s2 = "btnez";
7527 goto do_branch;
7528 case M_BLT:
7529 s = "slt";
7530 s2 = "btnez";
7531 goto do_branch;
7532 case M_BLTU:
7533 s = "sltu";
7534 s2 = "btnez";
7535 goto do_branch;
7536 case M_BLE:
7537 s = "slt";
7538 s2 = "bteqz";
7539 goto do_reverse_branch;
7540 case M_BLEU:
7541 s = "sltu";
7542 s2 = "bteqz";
7543 goto do_reverse_branch;
7544 case M_BGE:
7545 s = "slt";
7546 s2 = "bteqz";
7547 goto do_branch;
7548 case M_BGEU:
7549 s = "sltu";
7550 s2 = "bteqz";
7551 goto do_branch;
7552 case M_BGT:
7553 s = "slt";
7554 s2 = "btnez";
7555 goto do_reverse_branch;
7556 case M_BGTU:
7557 s = "sltu";
7558 s2 = "btnez";
7559
7560 do_reverse_branch:
7561 tmp = xreg;
7562 xreg = yreg;
7563 yreg = tmp;
7564
7565 do_branch:
7566 macro_build (NULL, s, "x,y", xreg, yreg);
7567 macro_build (&offset_expr, s2, "p");
7568 break;
7569
7570 case M_BEQ_I:
7571 s = "cmpi";
7572 s2 = "bteqz";
7573 s3 = "x,U";
7574 goto do_branch_i;
7575 case M_BNE_I:
7576 s = "cmpi";
7577 s2 = "btnez";
7578 s3 = "x,U";
7579 goto do_branch_i;
7580 case M_BLT_I:
7581 s = "slti";
7582 s2 = "btnez";
7583 s3 = "x,8";
7584 goto do_branch_i;
7585 case M_BLTU_I:
7586 s = "sltiu";
7587 s2 = "btnez";
7588 s3 = "x,8";
7589 goto do_branch_i;
7590 case M_BLE_I:
7591 s = "slti";
7592 s2 = "btnez";
7593 s3 = "x,8";
7594 goto do_addone_branch_i;
7595 case M_BLEU_I:
7596 s = "sltiu";
7597 s2 = "btnez";
7598 s3 = "x,8";
7599 goto do_addone_branch_i;
7600 case M_BGE_I:
7601 s = "slti";
7602 s2 = "bteqz";
7603 s3 = "x,8";
7604 goto do_branch_i;
7605 case M_BGEU_I:
7606 s = "sltiu";
7607 s2 = "bteqz";
7608 s3 = "x,8";
7609 goto do_branch_i;
7610 case M_BGT_I:
7611 s = "slti";
7612 s2 = "bteqz";
7613 s3 = "x,8";
7614 goto do_addone_branch_i;
7615 case M_BGTU_I:
7616 s = "sltiu";
7617 s2 = "bteqz";
7618 s3 = "x,8";
7619
7620 do_addone_branch_i:
7621 if (imm_expr.X_op != O_constant)
7622 as_bad (_("Unsupported large constant"));
7623 ++imm_expr.X_add_number;
7624
7625 do_branch_i:
7626 macro_build (&imm_expr, s, s3, xreg);
7627 macro_build (&offset_expr, s2, "p");
7628 break;
7629
7630 case M_ABS:
7631 expr1.X_add_number = 0;
7632 macro_build (&expr1, "slti", "x,8", yreg);
7633 if (xreg != yreg)
7634 move_register (xreg, yreg);
7635 expr1.X_add_number = 2;
7636 macro_build (&expr1, "bteqz", "p");
7637 macro_build (NULL, "neg", "x,w", xreg, xreg);
7638 }
7639 }
7640
7641 /* For consistency checking, verify that all bits are specified either
7642 by the match/mask part of the instruction definition, or by the
7643 operand list. */
7644 static int
7645 validate_mips_insn (const struct mips_opcode *opc)
7646 {
7647 const char *p = opc->args;
7648 char c;
7649 unsigned long used_bits = opc->mask;
7650
7651 if ((used_bits & opc->match) != opc->match)
7652 {
7653 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7654 opc->name, opc->args);
7655 return 0;
7656 }
7657 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7658 while (*p)
7659 switch (c = *p++)
7660 {
7661 case ',': break;
7662 case '(': break;
7663 case ')': break;
7664 case '+':
7665 switch (c = *p++)
7666 {
7667 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7668 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7669 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7670 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7671 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7672 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7673 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7674 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7675 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7676 case 'I': break;
7677 default:
7678 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7679 c, opc->name, opc->args);
7680 return 0;
7681 }
7682 break;
7683 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7684 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7685 case 'A': break;
7686 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7687 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7688 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7689 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7690 case 'F': break;
7691 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7692 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7693 case 'I': break;
7694 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7695 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7696 case 'L': break;
7697 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7698 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7699 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7700 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7701 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7702 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7703 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7704 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7705 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7706 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7707 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7708 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7709 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7710 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7711 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7712 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7713 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7714 case 'f': break;
7715 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7716 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7717 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7718 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7719 case 'l': break;
7720 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7721 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7722 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7723 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7724 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7725 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7726 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7727 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7728 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7729 case 'x': break;
7730 case 'z': break;
7731 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7732 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7733 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7734 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7735 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7736 case '[': break;
7737 case ']': break;
7738 default:
7739 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7740 c, opc->name, opc->args);
7741 return 0;
7742 }
7743 #undef USE_BITS
7744 if (used_bits != 0xffffffff)
7745 {
7746 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7747 ~used_bits & 0xffffffff, opc->name, opc->args);
7748 return 0;
7749 }
7750 return 1;
7751 }
7752
7753 /* This routine assembles an instruction into its binary format. As a
7754 side effect, it sets one of the global variables imm_reloc or
7755 offset_reloc to the type of relocation to do if one of the operands
7756 is an address expression. */
7757
7758 static void
7759 mips_ip (char *str, struct mips_cl_insn *ip)
7760 {
7761 char *s;
7762 const char *args;
7763 char c = 0;
7764 struct mips_opcode *insn;
7765 char *argsStart;
7766 unsigned int regno;
7767 unsigned int lastregno = 0;
7768 unsigned int lastpos = 0;
7769 unsigned int limlo, limhi;
7770 char *s_reset;
7771 char save_c = 0;
7772
7773 insn_error = NULL;
7774
7775 /* If the instruction contains a '.', we first try to match an instruction
7776 including the '.'. Then we try again without the '.'. */
7777 insn = NULL;
7778 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7779 continue;
7780
7781 /* If we stopped on whitespace, then replace the whitespace with null for
7782 the call to hash_find. Save the character we replaced just in case we
7783 have to re-parse the instruction. */
7784 if (ISSPACE (*s))
7785 {
7786 save_c = *s;
7787 *s++ = '\0';
7788 }
7789
7790 insn = (struct mips_opcode *) hash_find (op_hash, str);
7791
7792 /* If we didn't find the instruction in the opcode table, try again, but
7793 this time with just the instruction up to, but not including the
7794 first '.'. */
7795 if (insn == NULL)
7796 {
7797 /* Restore the character we overwrite above (if any). */
7798 if (save_c)
7799 *(--s) = save_c;
7800
7801 /* Scan up to the first '.' or whitespace. */
7802 for (s = str;
7803 *s != '\0' && *s != '.' && !ISSPACE (*s);
7804 ++s)
7805 continue;
7806
7807 /* If we did not find a '.', then we can quit now. */
7808 if (*s != '.')
7809 {
7810 insn_error = "unrecognized opcode";
7811 return;
7812 }
7813
7814 /* Lookup the instruction in the hash table. */
7815 *s++ = '\0';
7816 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7817 {
7818 insn_error = "unrecognized opcode";
7819 return;
7820 }
7821 }
7822
7823 argsStart = s;
7824 for (;;)
7825 {
7826 bfd_boolean ok;
7827
7828 assert (strcmp (insn->name, str) == 0);
7829
7830 if (OPCODE_IS_MEMBER (insn,
7831 (mips_opts.isa
7832 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7833 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7834 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7835 mips_opts.arch))
7836 ok = TRUE;
7837 else
7838 ok = FALSE;
7839
7840 if (insn->pinfo != INSN_MACRO)
7841 {
7842 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7843 ok = FALSE;
7844 }
7845
7846 if (! ok)
7847 {
7848 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7849 && strcmp (insn->name, insn[1].name) == 0)
7850 {
7851 ++insn;
7852 continue;
7853 }
7854 else
7855 {
7856 if (!insn_error)
7857 {
7858 static char buf[100];
7859 sprintf (buf,
7860 _("opcode not supported on this processor: %s (%s)"),
7861 mips_cpu_info_from_arch (mips_opts.arch)->name,
7862 mips_cpu_info_from_isa (mips_opts.isa)->name);
7863 insn_error = buf;
7864 }
7865 if (save_c)
7866 *(--s) = save_c;
7867 return;
7868 }
7869 }
7870
7871 create_insn (ip, insn);
7872 insn_error = NULL;
7873 for (args = insn->args;; ++args)
7874 {
7875 int is_mdmx;
7876
7877 s += strspn (s, " \t");
7878 is_mdmx = 0;
7879 switch (*args)
7880 {
7881 case '\0': /* end of args */
7882 if (*s == '\0')
7883 return;
7884 break;
7885
7886 case ',':
7887 if (*s++ == *args)
7888 continue;
7889 s--;
7890 switch (*++args)
7891 {
7892 case 'r':
7893 case 'v':
7894 INSERT_OPERAND (RS, *ip, lastregno);
7895 continue;
7896
7897 case 'w':
7898 INSERT_OPERAND (RT, *ip, lastregno);
7899 continue;
7900
7901 case 'W':
7902 INSERT_OPERAND (FT, *ip, lastregno);
7903 continue;
7904
7905 case 'V':
7906 INSERT_OPERAND (FS, *ip, lastregno);
7907 continue;
7908 }
7909 break;
7910
7911 case '(':
7912 /* Handle optional base register.
7913 Either the base register is omitted or
7914 we must have a left paren. */
7915 /* This is dependent on the next operand specifier
7916 is a base register specification. */
7917 assert (args[1] == 'b' || args[1] == '5'
7918 || args[1] == '-' || args[1] == '4');
7919 if (*s == '\0')
7920 return;
7921
7922 case ')': /* these must match exactly */
7923 case '[':
7924 case ']':
7925 if (*s++ == *args)
7926 continue;
7927 break;
7928
7929 case '+': /* Opcode extension character. */
7930 switch (*++args)
7931 {
7932 case 'A': /* ins/ext position, becomes LSB. */
7933 limlo = 0;
7934 limhi = 31;
7935 goto do_lsb;
7936 case 'E':
7937 limlo = 32;
7938 limhi = 63;
7939 goto do_lsb;
7940 do_lsb:
7941 my_getExpression (&imm_expr, s);
7942 check_absolute_expr (ip, &imm_expr);
7943 if ((unsigned long) imm_expr.X_add_number < limlo
7944 || (unsigned long) imm_expr.X_add_number > limhi)
7945 {
7946 as_bad (_("Improper position (%lu)"),
7947 (unsigned long) imm_expr.X_add_number);
7948 imm_expr.X_add_number = limlo;
7949 }
7950 lastpos = imm_expr.X_add_number;
7951 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
7952 imm_expr.X_op = O_absent;
7953 s = expr_end;
7954 continue;
7955
7956 case 'B': /* ins size, becomes MSB. */
7957 limlo = 1;
7958 limhi = 32;
7959 goto do_msb;
7960 case 'F':
7961 limlo = 33;
7962 limhi = 64;
7963 goto do_msb;
7964 do_msb:
7965 my_getExpression (&imm_expr, s);
7966 check_absolute_expr (ip, &imm_expr);
7967 /* Check for negative input so that small negative numbers
7968 will not succeed incorrectly. The checks against
7969 (pos+size) transitively check "size" itself,
7970 assuming that "pos" is reasonable. */
7971 if ((long) imm_expr.X_add_number < 0
7972 || ((unsigned long) imm_expr.X_add_number
7973 + lastpos) < limlo
7974 || ((unsigned long) imm_expr.X_add_number
7975 + lastpos) > limhi)
7976 {
7977 as_bad (_("Improper insert size (%lu, position %lu)"),
7978 (unsigned long) imm_expr.X_add_number,
7979 (unsigned long) lastpos);
7980 imm_expr.X_add_number = limlo - lastpos;
7981 }
7982 INSERT_OPERAND (INSMSB, *ip,
7983 lastpos + imm_expr.X_add_number - 1);
7984 imm_expr.X_op = O_absent;
7985 s = expr_end;
7986 continue;
7987
7988 case 'C': /* ext size, becomes MSBD. */
7989 limlo = 1;
7990 limhi = 32;
7991 goto do_msbd;
7992 case 'G':
7993 limlo = 33;
7994 limhi = 64;
7995 goto do_msbd;
7996 case 'H':
7997 limlo = 33;
7998 limhi = 64;
7999 goto do_msbd;
8000 do_msbd:
8001 my_getExpression (&imm_expr, s);
8002 check_absolute_expr (ip, &imm_expr);
8003 /* Check for negative input so that small negative numbers
8004 will not succeed incorrectly. The checks against
8005 (pos+size) transitively check "size" itself,
8006 assuming that "pos" is reasonable. */
8007 if ((long) imm_expr.X_add_number < 0
8008 || ((unsigned long) imm_expr.X_add_number
8009 + lastpos) < limlo
8010 || ((unsigned long) imm_expr.X_add_number
8011 + lastpos) > limhi)
8012 {
8013 as_bad (_("Improper extract size (%lu, position %lu)"),
8014 (unsigned long) imm_expr.X_add_number,
8015 (unsigned long) lastpos);
8016 imm_expr.X_add_number = limlo - lastpos;
8017 }
8018 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8019 imm_expr.X_op = O_absent;
8020 s = expr_end;
8021 continue;
8022
8023 case 'D':
8024 /* +D is for disassembly only; never match. */
8025 break;
8026
8027 case 'I':
8028 /* "+I" is like "I", except that imm2_expr is used. */
8029 my_getExpression (&imm2_expr, s);
8030 if (imm2_expr.X_op != O_big
8031 && imm2_expr.X_op != O_constant)
8032 insn_error = _("absolute expression required");
8033 normalize_constant_expr (&imm2_expr);
8034 s = expr_end;
8035 continue;
8036
8037 default:
8038 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8039 *args, insn->name, insn->args);
8040 /* Further processing is fruitless. */
8041 return;
8042 }
8043 break;
8044
8045 case '<': /* must be at least one digit */
8046 /*
8047 * According to the manual, if the shift amount is greater
8048 * than 31 or less than 0, then the shift amount should be
8049 * mod 32. In reality the mips assembler issues an error.
8050 * We issue a warning and mask out all but the low 5 bits.
8051 */
8052 my_getExpression (&imm_expr, s);
8053 check_absolute_expr (ip, &imm_expr);
8054 if ((unsigned long) imm_expr.X_add_number > 31)
8055 as_warn (_("Improper shift amount (%lu)"),
8056 (unsigned long) imm_expr.X_add_number);
8057 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8058 imm_expr.X_op = O_absent;
8059 s = expr_end;
8060 continue;
8061
8062 case '>': /* shift amount minus 32 */
8063 my_getExpression (&imm_expr, s);
8064 check_absolute_expr (ip, &imm_expr);
8065 if ((unsigned long) imm_expr.X_add_number < 32
8066 || (unsigned long) imm_expr.X_add_number > 63)
8067 break;
8068 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8069 imm_expr.X_op = O_absent;
8070 s = expr_end;
8071 continue;
8072
8073 case 'k': /* cache code */
8074 case 'h': /* prefx code */
8075 my_getExpression (&imm_expr, s);
8076 check_absolute_expr (ip, &imm_expr);
8077 if ((unsigned long) imm_expr.X_add_number > 31)
8078 as_warn (_("Invalid value for `%s' (%lu)"),
8079 ip->insn_mo->name,
8080 (unsigned long) imm_expr.X_add_number);
8081 if (*args == 'k')
8082 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8083 else
8084 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8085 imm_expr.X_op = O_absent;
8086 s = expr_end;
8087 continue;
8088
8089 case 'c': /* break code */
8090 my_getExpression (&imm_expr, s);
8091 check_absolute_expr (ip, &imm_expr);
8092 if ((unsigned long) imm_expr.X_add_number > 1023)
8093 as_warn (_("Illegal break code (%lu)"),
8094 (unsigned long) imm_expr.X_add_number);
8095 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8096 imm_expr.X_op = O_absent;
8097 s = expr_end;
8098 continue;
8099
8100 case 'q': /* lower break code */
8101 my_getExpression (&imm_expr, s);
8102 check_absolute_expr (ip, &imm_expr);
8103 if ((unsigned long) imm_expr.X_add_number > 1023)
8104 as_warn (_("Illegal lower break code (%lu)"),
8105 (unsigned long) imm_expr.X_add_number);
8106 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8107 imm_expr.X_op = O_absent;
8108 s = expr_end;
8109 continue;
8110
8111 case 'B': /* 20-bit syscall/break code. */
8112 my_getExpression (&imm_expr, s);
8113 check_absolute_expr (ip, &imm_expr);
8114 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8115 as_warn (_("Illegal 20-bit code (%lu)"),
8116 (unsigned long) imm_expr.X_add_number);
8117 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8118 imm_expr.X_op = O_absent;
8119 s = expr_end;
8120 continue;
8121
8122 case 'C': /* Coprocessor code */
8123 my_getExpression (&imm_expr, s);
8124 check_absolute_expr (ip, &imm_expr);
8125 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8126 {
8127 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8128 (unsigned long) imm_expr.X_add_number);
8129 imm_expr.X_add_number &= ((1 << 25) - 1);
8130 }
8131 ip->insn_opcode |= imm_expr.X_add_number;
8132 imm_expr.X_op = O_absent;
8133 s = expr_end;
8134 continue;
8135
8136 case 'J': /* 19-bit wait code. */
8137 my_getExpression (&imm_expr, s);
8138 check_absolute_expr (ip, &imm_expr);
8139 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8140 as_warn (_("Illegal 19-bit code (%lu)"),
8141 (unsigned long) imm_expr.X_add_number);
8142 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8143 imm_expr.X_op = O_absent;
8144 s = expr_end;
8145 continue;
8146
8147 case 'P': /* Performance register */
8148 my_getExpression (&imm_expr, s);
8149 check_absolute_expr (ip, &imm_expr);
8150 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8151 as_warn (_("Invalid performance register (%lu)"),
8152 (unsigned long) imm_expr.X_add_number);
8153 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8154 imm_expr.X_op = O_absent;
8155 s = expr_end;
8156 continue;
8157
8158 case 'b': /* base register */
8159 case 'd': /* destination register */
8160 case 's': /* source register */
8161 case 't': /* target register */
8162 case 'r': /* both target and source */
8163 case 'v': /* both dest and source */
8164 case 'w': /* both dest and target */
8165 case 'E': /* coprocessor target register */
8166 case 'G': /* coprocessor destination register */
8167 case 'K': /* 'rdhwr' destination register */
8168 case 'x': /* ignore register name */
8169 case 'z': /* must be zero register */
8170 case 'U': /* destination register (clo/clz). */
8171 s_reset = s;
8172 if (s[0] == '$')
8173 {
8174
8175 if (ISDIGIT (s[1]))
8176 {
8177 ++s;
8178 regno = 0;
8179 do
8180 {
8181 regno *= 10;
8182 regno += *s - '0';
8183 ++s;
8184 }
8185 while (ISDIGIT (*s));
8186 if (regno > 31)
8187 as_bad (_("Invalid register number (%d)"), regno);
8188 }
8189 else if (*args == 'E' || *args == 'G' || *args == 'K')
8190 goto notreg;
8191 else
8192 {
8193 if (s[1] == 'r' && s[2] == 'a')
8194 {
8195 s += 3;
8196 regno = RA;
8197 }
8198 else if (s[1] == 'f' && s[2] == 'p')
8199 {
8200 s += 3;
8201 regno = FP;
8202 }
8203 else if (s[1] == 's' && s[2] == 'p')
8204 {
8205 s += 3;
8206 regno = SP;
8207 }
8208 else if (s[1] == 'g' && s[2] == 'p')
8209 {
8210 s += 3;
8211 regno = GP;
8212 }
8213 else if (s[1] == 'a' && s[2] == 't')
8214 {
8215 s += 3;
8216 regno = AT;
8217 }
8218 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8219 {
8220 s += 4;
8221 regno = KT0;
8222 }
8223 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8224 {
8225 s += 4;
8226 regno = KT1;
8227 }
8228 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8229 {
8230 s += 5;
8231 regno = ZERO;
8232 }
8233 else if (itbl_have_entries)
8234 {
8235 char *p, *n;
8236 unsigned long r;
8237
8238 p = s + 1; /* advance past '$' */
8239 n = itbl_get_field (&p); /* n is name */
8240
8241 /* See if this is a register defined in an
8242 itbl entry. */
8243 if (itbl_get_reg_val (n, &r))
8244 {
8245 /* Get_field advances to the start of
8246 the next field, so we need to back
8247 rack to the end of the last field. */
8248 if (p)
8249 s = p - 1;
8250 else
8251 s = strchr (s, '\0');
8252 regno = r;
8253 }
8254 else
8255 goto notreg;
8256 }
8257 else
8258 goto notreg;
8259 }
8260 if (regno == AT
8261 && ! mips_opts.noat
8262 && *args != 'E'
8263 && *args != 'G'
8264 && *args != 'K')
8265 as_warn (_("Used $at without \".set noat\""));
8266 c = *args;
8267 if (*s == ' ')
8268 ++s;
8269 if (args[1] != *s)
8270 {
8271 if (c == 'r' || c == 'v' || c == 'w')
8272 {
8273 regno = lastregno;
8274 s = s_reset;
8275 ++args;
8276 }
8277 }
8278 /* 'z' only matches $0. */
8279 if (c == 'z' && regno != 0)
8280 break;
8281
8282 /* Now that we have assembled one operand, we use the args string
8283 * to figure out where it goes in the instruction. */
8284 switch (c)
8285 {
8286 case 'r':
8287 case 's':
8288 case 'v':
8289 case 'b':
8290 INSERT_OPERAND (RS, *ip, regno);
8291 break;
8292 case 'd':
8293 case 'G':
8294 case 'K':
8295 INSERT_OPERAND (RD, *ip, regno);
8296 break;
8297 case 'U':
8298 INSERT_OPERAND (RD, *ip, regno);
8299 INSERT_OPERAND (RT, *ip, regno);
8300 break;
8301 case 'w':
8302 case 't':
8303 case 'E':
8304 INSERT_OPERAND (RT, *ip, regno);
8305 break;
8306 case 'x':
8307 /* This case exists because on the r3000 trunc
8308 expands into a macro which requires a gp
8309 register. On the r6000 or r4000 it is
8310 assembled into a single instruction which
8311 ignores the register. Thus the insn version
8312 is MIPS_ISA2 and uses 'x', and the macro
8313 version is MIPS_ISA1 and uses 't'. */
8314 break;
8315 case 'z':
8316 /* This case is for the div instruction, which
8317 acts differently if the destination argument
8318 is $0. This only matches $0, and is checked
8319 outside the switch. */
8320 break;
8321 case 'D':
8322 /* Itbl operand; not yet implemented. FIXME ?? */
8323 break;
8324 /* What about all other operands like 'i', which
8325 can be specified in the opcode table? */
8326 }
8327 lastregno = regno;
8328 continue;
8329 }
8330 notreg:
8331 switch (*args++)
8332 {
8333 case 'r':
8334 case 'v':
8335 INSERT_OPERAND (RS, *ip, lastregno);
8336 continue;
8337 case 'w':
8338 INSERT_OPERAND (RT, *ip, lastregno);
8339 continue;
8340 }
8341 break;
8342
8343 case 'O': /* MDMX alignment immediate constant. */
8344 my_getExpression (&imm_expr, s);
8345 check_absolute_expr (ip, &imm_expr);
8346 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8347 as_warn ("Improper align amount (%ld), using low bits",
8348 (long) imm_expr.X_add_number);
8349 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
8350 imm_expr.X_op = O_absent;
8351 s = expr_end;
8352 continue;
8353
8354 case 'Q': /* MDMX vector, element sel, or const. */
8355 if (s[0] != '$')
8356 {
8357 /* MDMX Immediate. */
8358 my_getExpression (&imm_expr, s);
8359 check_absolute_expr (ip, &imm_expr);
8360 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8361 as_warn (_("Invalid MDMX Immediate (%ld)"),
8362 (long) imm_expr.X_add_number);
8363 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
8364 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8365 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8366 else
8367 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8368 imm_expr.X_op = O_absent;
8369 s = expr_end;
8370 continue;
8371 }
8372 /* Not MDMX Immediate. Fall through. */
8373 case 'X': /* MDMX destination register. */
8374 case 'Y': /* MDMX source register. */
8375 case 'Z': /* MDMX target register. */
8376 is_mdmx = 1;
8377 case 'D': /* floating point destination register */
8378 case 'S': /* floating point source register */
8379 case 'T': /* floating point target register */
8380 case 'R': /* floating point source register */
8381 case 'V':
8382 case 'W':
8383 s_reset = s;
8384 /* Accept $fN for FP and MDMX register numbers, and in
8385 addition accept $vN for MDMX register numbers. */
8386 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8387 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8388 && ISDIGIT (s[2])))
8389 {
8390 s += 2;
8391 regno = 0;
8392 do
8393 {
8394 regno *= 10;
8395 regno += *s - '0';
8396 ++s;
8397 }
8398 while (ISDIGIT (*s));
8399
8400 if (regno > 31)
8401 as_bad (_("Invalid float register number (%d)"), regno);
8402
8403 if ((regno & 1) != 0
8404 && HAVE_32BIT_FPRS
8405 && ! (strcmp (str, "mtc1") == 0
8406 || strcmp (str, "mfc1") == 0
8407 || strcmp (str, "lwc1") == 0
8408 || strcmp (str, "swc1") == 0
8409 || strcmp (str, "l.s") == 0
8410 || strcmp (str, "s.s") == 0))
8411 as_warn (_("Float register should be even, was %d"),
8412 regno);
8413
8414 c = *args;
8415 if (*s == ' ')
8416 ++s;
8417 if (args[1] != *s)
8418 {
8419 if (c == 'V' || c == 'W')
8420 {
8421 regno = lastregno;
8422 s = s_reset;
8423 ++args;
8424 }
8425 }
8426 switch (c)
8427 {
8428 case 'D':
8429 case 'X':
8430 INSERT_OPERAND (FD, *ip, regno);
8431 break;
8432 case 'V':
8433 case 'S':
8434 case 'Y':
8435 INSERT_OPERAND (FS, *ip, regno);
8436 break;
8437 case 'Q':
8438 /* This is like 'Z', but also needs to fix the MDMX
8439 vector/scalar select bits. Note that the
8440 scalar immediate case is handled above. */
8441 if (*s == '[')
8442 {
8443 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8444 int max_el = (is_qh ? 3 : 7);
8445 s++;
8446 my_getExpression(&imm_expr, s);
8447 check_absolute_expr (ip, &imm_expr);
8448 s = expr_end;
8449 if (imm_expr.X_add_number > max_el)
8450 as_bad(_("Bad element selector %ld"),
8451 (long) imm_expr.X_add_number);
8452 imm_expr.X_add_number &= max_el;
8453 ip->insn_opcode |= (imm_expr.X_add_number
8454 << (OP_SH_VSEL +
8455 (is_qh ? 2 : 1)));
8456 imm_expr.X_op = O_absent;
8457 if (*s != ']')
8458 as_warn(_("Expecting ']' found '%s'"), s);
8459 else
8460 s++;
8461 }
8462 else
8463 {
8464 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8465 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8466 << OP_SH_VSEL);
8467 else
8468 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8469 OP_SH_VSEL);
8470 }
8471 /* Fall through */
8472 case 'W':
8473 case 'T':
8474 case 'Z':
8475 INSERT_OPERAND (FT, *ip, regno);
8476 break;
8477 case 'R':
8478 INSERT_OPERAND (FR, *ip, regno);
8479 break;
8480 }
8481 lastregno = regno;
8482 continue;
8483 }
8484
8485 switch (*args++)
8486 {
8487 case 'V':
8488 INSERT_OPERAND (FS, *ip, lastregno);
8489 continue;
8490 case 'W':
8491 INSERT_OPERAND (FT, *ip, lastregno);
8492 continue;
8493 }
8494 break;
8495
8496 case 'I':
8497 my_getExpression (&imm_expr, s);
8498 if (imm_expr.X_op != O_big
8499 && imm_expr.X_op != O_constant)
8500 insn_error = _("absolute expression required");
8501 normalize_constant_expr (&imm_expr);
8502 s = expr_end;
8503 continue;
8504
8505 case 'A':
8506 my_getExpression (&offset_expr, s);
8507 *imm_reloc = BFD_RELOC_32;
8508 s = expr_end;
8509 continue;
8510
8511 case 'F':
8512 case 'L':
8513 case 'f':
8514 case 'l':
8515 {
8516 int f64;
8517 int using_gprs;
8518 char *save_in;
8519 char *err;
8520 unsigned char temp[8];
8521 int len;
8522 unsigned int length;
8523 segT seg;
8524 subsegT subseg;
8525 char *p;
8526
8527 /* These only appear as the last operand in an
8528 instruction, and every instruction that accepts
8529 them in any variant accepts them in all variants.
8530 This means we don't have to worry about backing out
8531 any changes if the instruction does not match.
8532
8533 The difference between them is the size of the
8534 floating point constant and where it goes. For 'F'
8535 and 'L' the constant is 64 bits; for 'f' and 'l' it
8536 is 32 bits. Where the constant is placed is based
8537 on how the MIPS assembler does things:
8538 F -- .rdata
8539 L -- .lit8
8540 f -- immediate value
8541 l -- .lit4
8542
8543 The .lit4 and .lit8 sections are only used if
8544 permitted by the -G argument.
8545
8546 The code below needs to know whether the target register
8547 is 32 or 64 bits wide. It relies on the fact 'f' and
8548 'F' are used with GPR-based instructions and 'l' and
8549 'L' are used with FPR-based instructions. */
8550
8551 f64 = *args == 'F' || *args == 'L';
8552 using_gprs = *args == 'F' || *args == 'f';
8553
8554 save_in = input_line_pointer;
8555 input_line_pointer = s;
8556 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8557 length = len;
8558 s = input_line_pointer;
8559 input_line_pointer = save_in;
8560 if (err != NULL && *err != '\0')
8561 {
8562 as_bad (_("Bad floating point constant: %s"), err);
8563 memset (temp, '\0', sizeof temp);
8564 length = f64 ? 8 : 4;
8565 }
8566
8567 assert (length == (unsigned) (f64 ? 8 : 4));
8568
8569 if (*args == 'f'
8570 || (*args == 'l'
8571 && (g_switch_value < 4
8572 || (temp[0] == 0 && temp[1] == 0)
8573 || (temp[2] == 0 && temp[3] == 0))))
8574 {
8575 imm_expr.X_op = O_constant;
8576 if (! target_big_endian)
8577 imm_expr.X_add_number = bfd_getl32 (temp);
8578 else
8579 imm_expr.X_add_number = bfd_getb32 (temp);
8580 }
8581 else if (length > 4
8582 && ! mips_disable_float_construction
8583 /* Constants can only be constructed in GPRs and
8584 copied to FPRs if the GPRs are at least as wide
8585 as the FPRs. Force the constant into memory if
8586 we are using 64-bit FPRs but the GPRs are only
8587 32 bits wide. */
8588 && (using_gprs
8589 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8590 && ((temp[0] == 0 && temp[1] == 0)
8591 || (temp[2] == 0 && temp[3] == 0))
8592 && ((temp[4] == 0 && temp[5] == 0)
8593 || (temp[6] == 0 && temp[7] == 0)))
8594 {
8595 /* The value is simple enough to load with a couple of
8596 instructions. If using 32-bit registers, set
8597 imm_expr to the high order 32 bits and offset_expr to
8598 the low order 32 bits. Otherwise, set imm_expr to
8599 the entire 64 bit constant. */
8600 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8601 {
8602 imm_expr.X_op = O_constant;
8603 offset_expr.X_op = O_constant;
8604 if (! target_big_endian)
8605 {
8606 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8607 offset_expr.X_add_number = bfd_getl32 (temp);
8608 }
8609 else
8610 {
8611 imm_expr.X_add_number = bfd_getb32 (temp);
8612 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8613 }
8614 if (offset_expr.X_add_number == 0)
8615 offset_expr.X_op = O_absent;
8616 }
8617 else if (sizeof (imm_expr.X_add_number) > 4)
8618 {
8619 imm_expr.X_op = O_constant;
8620 if (! target_big_endian)
8621 imm_expr.X_add_number = bfd_getl64 (temp);
8622 else
8623 imm_expr.X_add_number = bfd_getb64 (temp);
8624 }
8625 else
8626 {
8627 imm_expr.X_op = O_big;
8628 imm_expr.X_add_number = 4;
8629 if (! target_big_endian)
8630 {
8631 generic_bignum[0] = bfd_getl16 (temp);
8632 generic_bignum[1] = bfd_getl16 (temp + 2);
8633 generic_bignum[2] = bfd_getl16 (temp + 4);
8634 generic_bignum[3] = bfd_getl16 (temp + 6);
8635 }
8636 else
8637 {
8638 generic_bignum[0] = bfd_getb16 (temp + 6);
8639 generic_bignum[1] = bfd_getb16 (temp + 4);
8640 generic_bignum[2] = bfd_getb16 (temp + 2);
8641 generic_bignum[3] = bfd_getb16 (temp);
8642 }
8643 }
8644 }
8645 else
8646 {
8647 const char *newname;
8648 segT new_seg;
8649
8650 /* Switch to the right section. */
8651 seg = now_seg;
8652 subseg = now_subseg;
8653 switch (*args)
8654 {
8655 default: /* unused default case avoids warnings. */
8656 case 'L':
8657 newname = RDATA_SECTION_NAME;
8658 if (g_switch_value >= 8)
8659 newname = ".lit8";
8660 break;
8661 case 'F':
8662 newname = RDATA_SECTION_NAME;
8663 break;
8664 case 'l':
8665 assert (g_switch_value >= 4);
8666 newname = ".lit4";
8667 break;
8668 }
8669 new_seg = subseg_new (newname, (subsegT) 0);
8670 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8671 bfd_set_section_flags (stdoutput, new_seg,
8672 (SEC_ALLOC
8673 | SEC_LOAD
8674 | SEC_READONLY
8675 | SEC_DATA));
8676 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8677 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8678 && strcmp (TARGET_OS, "elf") != 0)
8679 record_alignment (new_seg, 4);
8680 else
8681 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8682 if (seg == now_seg)
8683 as_bad (_("Can't use floating point insn in this section"));
8684
8685 /* Set the argument to the current address in the
8686 section. */
8687 offset_expr.X_op = O_symbol;
8688 offset_expr.X_add_symbol =
8689 symbol_new ("L0\001", now_seg,
8690 (valueT) frag_now_fix (), frag_now);
8691 offset_expr.X_add_number = 0;
8692
8693 /* Put the floating point number into the section. */
8694 p = frag_more ((int) length);
8695 memcpy (p, temp, length);
8696
8697 /* Switch back to the original section. */
8698 subseg_set (seg, subseg);
8699 }
8700 }
8701 continue;
8702
8703 case 'i': /* 16 bit unsigned immediate */
8704 case 'j': /* 16 bit signed immediate */
8705 *imm_reloc = BFD_RELOC_LO16;
8706 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8707 {
8708 int more;
8709 offsetT minval, maxval;
8710
8711 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8712 && strcmp (insn->name, insn[1].name) == 0);
8713
8714 /* If the expression was written as an unsigned number,
8715 only treat it as signed if there are no more
8716 alternatives. */
8717 if (more
8718 && *args == 'j'
8719 && sizeof (imm_expr.X_add_number) <= 4
8720 && imm_expr.X_op == O_constant
8721 && imm_expr.X_add_number < 0
8722 && imm_expr.X_unsigned
8723 && HAVE_64BIT_GPRS)
8724 break;
8725
8726 /* For compatibility with older assemblers, we accept
8727 0x8000-0xffff as signed 16-bit numbers when only
8728 signed numbers are allowed. */
8729 if (*args == 'i')
8730 minval = 0, maxval = 0xffff;
8731 else if (more)
8732 minval = -0x8000, maxval = 0x7fff;
8733 else
8734 minval = -0x8000, maxval = 0xffff;
8735
8736 if (imm_expr.X_op != O_constant
8737 || imm_expr.X_add_number < minval
8738 || imm_expr.X_add_number > maxval)
8739 {
8740 if (more)
8741 break;
8742 if (imm_expr.X_op == O_constant
8743 || imm_expr.X_op == O_big)
8744 as_bad (_("expression out of range"));
8745 }
8746 }
8747 s = expr_end;
8748 continue;
8749
8750 case 'o': /* 16 bit offset */
8751 /* Check whether there is only a single bracketed expression
8752 left. If so, it must be the base register and the
8753 constant must be zero. */
8754 if (*s == '(' && strchr (s + 1, '(') == 0)
8755 {
8756 offset_expr.X_op = O_constant;
8757 offset_expr.X_add_number = 0;
8758 continue;
8759 }
8760
8761 /* If this value won't fit into a 16 bit offset, then go
8762 find a macro that will generate the 32 bit offset
8763 code pattern. */
8764 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8765 && (offset_expr.X_op != O_constant
8766 || offset_expr.X_add_number >= 0x8000
8767 || offset_expr.X_add_number < -0x8000))
8768 break;
8769
8770 s = expr_end;
8771 continue;
8772
8773 case 'p': /* pc relative offset */
8774 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8775 my_getExpression (&offset_expr, s);
8776 s = expr_end;
8777 continue;
8778
8779 case 'u': /* upper 16 bits */
8780 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8781 && imm_expr.X_op == O_constant
8782 && (imm_expr.X_add_number < 0
8783 || imm_expr.X_add_number >= 0x10000))
8784 as_bad (_("lui expression not in range 0..65535"));
8785 s = expr_end;
8786 continue;
8787
8788 case 'a': /* 26 bit address */
8789 my_getExpression (&offset_expr, s);
8790 s = expr_end;
8791 *offset_reloc = BFD_RELOC_MIPS_JMP;
8792 continue;
8793
8794 case 'N': /* 3 bit branch condition code */
8795 case 'M': /* 3 bit compare condition code */
8796 if (strncmp (s, "$fcc", 4) != 0)
8797 break;
8798 s += 4;
8799 regno = 0;
8800 do
8801 {
8802 regno *= 10;
8803 regno += *s - '0';
8804 ++s;
8805 }
8806 while (ISDIGIT (*s));
8807 if (regno > 7)
8808 as_bad (_("Invalid condition code register $fcc%d"), regno);
8809 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8810 || strcmp(str + strlen(str) - 5, "any2f") == 0
8811 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8812 && (regno & 1) != 0)
8813 as_warn(_("Condition code register should be even for %s, was %d"),
8814 str, regno);
8815 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8816 || strcmp(str + strlen(str) - 5, "any4t") == 0)
8817 && (regno & 3) != 0)
8818 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
8819 str, regno);
8820 if (*args == 'N')
8821 INSERT_OPERAND (BCC, *ip, regno);
8822 else
8823 INSERT_OPERAND (CCC, *ip, regno);
8824 continue;
8825
8826 case 'H':
8827 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8828 s += 2;
8829 if (ISDIGIT (*s))
8830 {
8831 c = 0;
8832 do
8833 {
8834 c *= 10;
8835 c += *s - '0';
8836 ++s;
8837 }
8838 while (ISDIGIT (*s));
8839 }
8840 else
8841 c = 8; /* Invalid sel value. */
8842
8843 if (c > 7)
8844 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8845 ip->insn_opcode |= c;
8846 continue;
8847
8848 case 'e':
8849 /* Must be at least one digit. */
8850 my_getExpression (&imm_expr, s);
8851 check_absolute_expr (ip, &imm_expr);
8852
8853 if ((unsigned long) imm_expr.X_add_number
8854 > (unsigned long) OP_MASK_VECBYTE)
8855 {
8856 as_bad (_("bad byte vector index (%ld)"),
8857 (long) imm_expr.X_add_number);
8858 imm_expr.X_add_number = 0;
8859 }
8860
8861 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
8862 imm_expr.X_op = O_absent;
8863 s = expr_end;
8864 continue;
8865
8866 case '%':
8867 my_getExpression (&imm_expr, s);
8868 check_absolute_expr (ip, &imm_expr);
8869
8870 if ((unsigned long) imm_expr.X_add_number
8871 > (unsigned long) OP_MASK_VECALIGN)
8872 {
8873 as_bad (_("bad byte vector index (%ld)"),
8874 (long) imm_expr.X_add_number);
8875 imm_expr.X_add_number = 0;
8876 }
8877
8878 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
8879 imm_expr.X_op = O_absent;
8880 s = expr_end;
8881 continue;
8882
8883 default:
8884 as_bad (_("bad char = '%c'\n"), *args);
8885 internalError ();
8886 }
8887 break;
8888 }
8889 /* Args don't match. */
8890 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8891 !strcmp (insn->name, insn[1].name))
8892 {
8893 ++insn;
8894 s = argsStart;
8895 insn_error = _("illegal operands");
8896 continue;
8897 }
8898 if (save_c)
8899 *(--s) = save_c;
8900 insn_error = _("illegal operands");
8901 return;
8902 }
8903 }
8904
8905 /* This routine assembles an instruction into its binary format when
8906 assembling for the mips16. As a side effect, it sets one of the
8907 global variables imm_reloc or offset_reloc to the type of
8908 relocation to do if one of the operands is an address expression.
8909 It also sets mips16_small and mips16_ext if the user explicitly
8910 requested a small or extended instruction. */
8911
8912 static void
8913 mips16_ip (char *str, struct mips_cl_insn *ip)
8914 {
8915 char *s;
8916 const char *args;
8917 struct mips_opcode *insn;
8918 char *argsstart;
8919 unsigned int regno;
8920 unsigned int lastregno = 0;
8921 char *s_reset;
8922 size_t i;
8923
8924 insn_error = NULL;
8925
8926 mips16_small = FALSE;
8927 mips16_ext = FALSE;
8928
8929 for (s = str; ISLOWER (*s); ++s)
8930 ;
8931 switch (*s)
8932 {
8933 case '\0':
8934 break;
8935
8936 case ' ':
8937 *s++ = '\0';
8938 break;
8939
8940 case '.':
8941 if (s[1] == 't' && s[2] == ' ')
8942 {
8943 *s = '\0';
8944 mips16_small = TRUE;
8945 s += 3;
8946 break;
8947 }
8948 else if (s[1] == 'e' && s[2] == ' ')
8949 {
8950 *s = '\0';
8951 mips16_ext = TRUE;
8952 s += 3;
8953 break;
8954 }
8955 /* Fall through. */
8956 default:
8957 insn_error = _("unknown opcode");
8958 return;
8959 }
8960
8961 if (mips_opts.noautoextend && ! mips16_ext)
8962 mips16_small = TRUE;
8963
8964 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8965 {
8966 insn_error = _("unrecognized opcode");
8967 return;
8968 }
8969
8970 argsstart = s;
8971 for (;;)
8972 {
8973 assert (strcmp (insn->name, str) == 0);
8974
8975 create_insn (ip, insn);
8976 imm_expr.X_op = O_absent;
8977 imm_reloc[0] = BFD_RELOC_UNUSED;
8978 imm_reloc[1] = BFD_RELOC_UNUSED;
8979 imm_reloc[2] = BFD_RELOC_UNUSED;
8980 imm2_expr.X_op = O_absent;
8981 offset_expr.X_op = O_absent;
8982 offset_reloc[0] = BFD_RELOC_UNUSED;
8983 offset_reloc[1] = BFD_RELOC_UNUSED;
8984 offset_reloc[2] = BFD_RELOC_UNUSED;
8985 for (args = insn->args; 1; ++args)
8986 {
8987 int c;
8988
8989 if (*s == ' ')
8990 ++s;
8991
8992 /* In this switch statement we call break if we did not find
8993 a match, continue if we did find a match, or return if we
8994 are done. */
8995
8996 c = *args;
8997 switch (c)
8998 {
8999 case '\0':
9000 if (*s == '\0')
9001 {
9002 /* Stuff the immediate value in now, if we can. */
9003 if (imm_expr.X_op == O_constant
9004 && *imm_reloc > BFD_RELOC_UNUSED
9005 && insn->pinfo != INSN_MACRO)
9006 {
9007 valueT tmp;
9008
9009 switch (*offset_reloc)
9010 {
9011 case BFD_RELOC_MIPS16_HI16_S:
9012 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9013 break;
9014
9015 case BFD_RELOC_MIPS16_HI16:
9016 tmp = imm_expr.X_add_number >> 16;
9017 break;
9018
9019 case BFD_RELOC_MIPS16_LO16:
9020 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9021 - 0x8000;
9022 break;
9023
9024 case BFD_RELOC_UNUSED:
9025 tmp = imm_expr.X_add_number;
9026 break;
9027
9028 default:
9029 internalError ();
9030 }
9031 *offset_reloc = BFD_RELOC_UNUSED;
9032
9033 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9034 tmp, TRUE, mips16_small,
9035 mips16_ext, &ip->insn_opcode,
9036 &ip->use_extend, &ip->extend);
9037 imm_expr.X_op = O_absent;
9038 *imm_reloc = BFD_RELOC_UNUSED;
9039 }
9040
9041 return;
9042 }
9043 break;
9044
9045 case ',':
9046 if (*s++ == c)
9047 continue;
9048 s--;
9049 switch (*++args)
9050 {
9051 case 'v':
9052 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9053 continue;
9054 case 'w':
9055 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9056 continue;
9057 }
9058 break;
9059
9060 case '(':
9061 case ')':
9062 if (*s++ == c)
9063 continue;
9064 break;
9065
9066 case 'v':
9067 case 'w':
9068 if (s[0] != '$')
9069 {
9070 if (c == 'v')
9071 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9072 else
9073 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9074 ++args;
9075 continue;
9076 }
9077 /* Fall through. */
9078 case 'x':
9079 case 'y':
9080 case 'z':
9081 case 'Z':
9082 case '0':
9083 case 'S':
9084 case 'R':
9085 case 'X':
9086 case 'Y':
9087 if (s[0] != '$')
9088 break;
9089 s_reset = s;
9090 if (ISDIGIT (s[1]))
9091 {
9092 ++s;
9093 regno = 0;
9094 do
9095 {
9096 regno *= 10;
9097 regno += *s - '0';
9098 ++s;
9099 }
9100 while (ISDIGIT (*s));
9101 if (regno > 31)
9102 {
9103 as_bad (_("invalid register number (%d)"), regno);
9104 regno = 2;
9105 }
9106 }
9107 else
9108 {
9109 if (s[1] == 'r' && s[2] == 'a')
9110 {
9111 s += 3;
9112 regno = RA;
9113 }
9114 else if (s[1] == 'f' && s[2] == 'p')
9115 {
9116 s += 3;
9117 regno = FP;
9118 }
9119 else if (s[1] == 's' && s[2] == 'p')
9120 {
9121 s += 3;
9122 regno = SP;
9123 }
9124 else if (s[1] == 'g' && s[2] == 'p')
9125 {
9126 s += 3;
9127 regno = GP;
9128 }
9129 else if (s[1] == 'a' && s[2] == 't')
9130 {
9131 s += 3;
9132 regno = AT;
9133 }
9134 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9135 {
9136 s += 4;
9137 regno = KT0;
9138 }
9139 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9140 {
9141 s += 4;
9142 regno = KT1;
9143 }
9144 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9145 {
9146 s += 5;
9147 regno = ZERO;
9148 }
9149 else
9150 break;
9151 }
9152
9153 if (*s == ' ')
9154 ++s;
9155 if (args[1] != *s)
9156 {
9157 if (c == 'v' || c == 'w')
9158 {
9159 regno = mips16_to_32_reg_map[lastregno];
9160 s = s_reset;
9161 ++args;
9162 }
9163 }
9164
9165 switch (c)
9166 {
9167 case 'x':
9168 case 'y':
9169 case 'z':
9170 case 'v':
9171 case 'w':
9172 case 'Z':
9173 regno = mips32_to_16_reg_map[regno];
9174 break;
9175
9176 case '0':
9177 if (regno != 0)
9178 regno = ILLEGAL_REG;
9179 break;
9180
9181 case 'S':
9182 if (regno != SP)
9183 regno = ILLEGAL_REG;
9184 break;
9185
9186 case 'R':
9187 if (regno != RA)
9188 regno = ILLEGAL_REG;
9189 break;
9190
9191 case 'X':
9192 case 'Y':
9193 if (regno == AT && ! mips_opts.noat)
9194 as_warn (_("used $at without \".set noat\""));
9195 break;
9196
9197 default:
9198 internalError ();
9199 }
9200
9201 if (regno == ILLEGAL_REG)
9202 break;
9203
9204 switch (c)
9205 {
9206 case 'x':
9207 case 'v':
9208 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9209 break;
9210 case 'y':
9211 case 'w':
9212 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9213 break;
9214 case 'z':
9215 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9216 break;
9217 case 'Z':
9218 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9219 case '0':
9220 case 'S':
9221 case 'R':
9222 break;
9223 case 'X':
9224 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9225 break;
9226 case 'Y':
9227 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9228 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9229 break;
9230 default:
9231 internalError ();
9232 }
9233
9234 lastregno = regno;
9235 continue;
9236
9237 case 'P':
9238 if (strncmp (s, "$pc", 3) == 0)
9239 {
9240 s += 3;
9241 continue;
9242 }
9243 break;
9244
9245 case '5':
9246 case 'H':
9247 case 'W':
9248 case 'D':
9249 case 'j':
9250 case 'V':
9251 case 'C':
9252 case 'U':
9253 case 'k':
9254 case 'K':
9255 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9256 if (i > 0)
9257 {
9258 if (imm_expr.X_op != O_constant)
9259 {
9260 mips16_ext = TRUE;
9261 ip->use_extend = TRUE;
9262 ip->extend = 0;
9263 }
9264 else
9265 {
9266 /* We need to relax this instruction. */
9267 *offset_reloc = *imm_reloc;
9268 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9269 }
9270 s = expr_end;
9271 continue;
9272 }
9273 *imm_reloc = BFD_RELOC_UNUSED;
9274 /* Fall through. */
9275 case '<':
9276 case '>':
9277 case '[':
9278 case ']':
9279 case '4':
9280 case '8':
9281 my_getExpression (&imm_expr, s);
9282 if (imm_expr.X_op == O_register)
9283 {
9284 /* What we thought was an expression turned out to
9285 be a register. */
9286
9287 if (s[0] == '(' && args[1] == '(')
9288 {
9289 /* It looks like the expression was omitted
9290 before a register indirection, which means
9291 that the expression is implicitly zero. We
9292 still set up imm_expr, so that we handle
9293 explicit extensions correctly. */
9294 imm_expr.X_op = O_constant;
9295 imm_expr.X_add_number = 0;
9296 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9297 continue;
9298 }
9299
9300 break;
9301 }
9302
9303 /* We need to relax this instruction. */
9304 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9305 s = expr_end;
9306 continue;
9307
9308 case 'p':
9309 case 'q':
9310 case 'A':
9311 case 'B':
9312 case 'E':
9313 /* We use offset_reloc rather than imm_reloc for the PC
9314 relative operands. This lets macros with both
9315 immediate and address operands work correctly. */
9316 my_getExpression (&offset_expr, s);
9317
9318 if (offset_expr.X_op == O_register)
9319 break;
9320
9321 /* We need to relax this instruction. */
9322 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9323 s = expr_end;
9324 continue;
9325
9326 case '6': /* break code */
9327 my_getExpression (&imm_expr, s);
9328 check_absolute_expr (ip, &imm_expr);
9329 if ((unsigned long) imm_expr.X_add_number > 63)
9330 as_warn (_("Invalid value for `%s' (%lu)"),
9331 ip->insn_mo->name,
9332 (unsigned long) imm_expr.X_add_number);
9333 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
9334 imm_expr.X_op = O_absent;
9335 s = expr_end;
9336 continue;
9337
9338 case 'a': /* 26 bit address */
9339 my_getExpression (&offset_expr, s);
9340 s = expr_end;
9341 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9342 ip->insn_opcode <<= 16;
9343 continue;
9344
9345 case 'l': /* register list for entry macro */
9346 case 'L': /* register list for exit macro */
9347 {
9348 int mask;
9349
9350 if (c == 'l')
9351 mask = 0;
9352 else
9353 mask = 7 << 3;
9354 while (*s != '\0')
9355 {
9356 int freg, reg1, reg2;
9357
9358 while (*s == ' ' || *s == ',')
9359 ++s;
9360 if (*s != '$')
9361 {
9362 as_bad (_("can't parse register list"));
9363 break;
9364 }
9365 ++s;
9366 if (*s != 'f')
9367 freg = 0;
9368 else
9369 {
9370 freg = 1;
9371 ++s;
9372 }
9373 reg1 = 0;
9374 while (ISDIGIT (*s))
9375 {
9376 reg1 *= 10;
9377 reg1 += *s - '0';
9378 ++s;
9379 }
9380 if (*s == ' ')
9381 ++s;
9382 if (*s != '-')
9383 reg2 = reg1;
9384 else
9385 {
9386 ++s;
9387 if (*s != '$')
9388 break;
9389 ++s;
9390 if (freg)
9391 {
9392 if (*s == 'f')
9393 ++s;
9394 else
9395 {
9396 as_bad (_("invalid register list"));
9397 break;
9398 }
9399 }
9400 reg2 = 0;
9401 while (ISDIGIT (*s))
9402 {
9403 reg2 *= 10;
9404 reg2 += *s - '0';
9405 ++s;
9406 }
9407 }
9408 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9409 {
9410 mask &= ~ (7 << 3);
9411 mask |= 5 << 3;
9412 }
9413 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9414 {
9415 mask &= ~ (7 << 3);
9416 mask |= 6 << 3;
9417 }
9418 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9419 mask |= (reg2 - 3) << 3;
9420 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9421 mask |= (reg2 - 15) << 1;
9422 else if (reg1 == RA && reg2 == RA)
9423 mask |= 1;
9424 else
9425 {
9426 as_bad (_("invalid register list"));
9427 break;
9428 }
9429 }
9430 /* The mask is filled in in the opcode table for the
9431 benefit of the disassembler. We remove it before
9432 applying the actual mask. */
9433 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9434 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9435 }
9436 continue;
9437
9438 case 'e': /* extend code */
9439 my_getExpression (&imm_expr, s);
9440 check_absolute_expr (ip, &imm_expr);
9441 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9442 {
9443 as_warn (_("Invalid value for `%s' (%lu)"),
9444 ip->insn_mo->name,
9445 (unsigned long) imm_expr.X_add_number);
9446 imm_expr.X_add_number &= 0x7ff;
9447 }
9448 ip->insn_opcode |= imm_expr.X_add_number;
9449 imm_expr.X_op = O_absent;
9450 s = expr_end;
9451 continue;
9452
9453 default:
9454 internalError ();
9455 }
9456 break;
9457 }
9458
9459 /* Args don't match. */
9460 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9461 strcmp (insn->name, insn[1].name) == 0)
9462 {
9463 ++insn;
9464 s = argsstart;
9465 continue;
9466 }
9467
9468 insn_error = _("illegal operands");
9469
9470 return;
9471 }
9472 }
9473
9474 /* This structure holds information we know about a mips16 immediate
9475 argument type. */
9476
9477 struct mips16_immed_operand
9478 {
9479 /* The type code used in the argument string in the opcode table. */
9480 int type;
9481 /* The number of bits in the short form of the opcode. */
9482 int nbits;
9483 /* The number of bits in the extended form of the opcode. */
9484 int extbits;
9485 /* The amount by which the short form is shifted when it is used;
9486 for example, the sw instruction has a shift count of 2. */
9487 int shift;
9488 /* The amount by which the short form is shifted when it is stored
9489 into the instruction code. */
9490 int op_shift;
9491 /* Non-zero if the short form is unsigned. */
9492 int unsp;
9493 /* Non-zero if the extended form is unsigned. */
9494 int extu;
9495 /* Non-zero if the value is PC relative. */
9496 int pcrel;
9497 };
9498
9499 /* The mips16 immediate operand types. */
9500
9501 static const struct mips16_immed_operand mips16_immed_operands[] =
9502 {
9503 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9504 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9505 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9506 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9507 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9508 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9509 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9510 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9511 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9512 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9513 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9514 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9515 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9516 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9517 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9518 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9519 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9520 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9521 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9522 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9523 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9524 };
9525
9526 #define MIPS16_NUM_IMMED \
9527 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9528
9529 /* Handle a mips16 instruction with an immediate value. This or's the
9530 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9531 whether an extended value is needed; if one is needed, it sets
9532 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9533 If SMALL is true, an unextended opcode was explicitly requested.
9534 If EXT is true, an extended opcode was explicitly requested. If
9535 WARN is true, warn if EXT does not match reality. */
9536
9537 static void
9538 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9539 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9540 unsigned long *insn, bfd_boolean *use_extend,
9541 unsigned short *extend)
9542 {
9543 register const struct mips16_immed_operand *op;
9544 int mintiny, maxtiny;
9545 bfd_boolean needext;
9546
9547 op = mips16_immed_operands;
9548 while (op->type != type)
9549 {
9550 ++op;
9551 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9552 }
9553
9554 if (op->unsp)
9555 {
9556 if (type == '<' || type == '>' || type == '[' || type == ']')
9557 {
9558 mintiny = 1;
9559 maxtiny = 1 << op->nbits;
9560 }
9561 else
9562 {
9563 mintiny = 0;
9564 maxtiny = (1 << op->nbits) - 1;
9565 }
9566 }
9567 else
9568 {
9569 mintiny = - (1 << (op->nbits - 1));
9570 maxtiny = (1 << (op->nbits - 1)) - 1;
9571 }
9572
9573 /* Branch offsets have an implicit 0 in the lowest bit. */
9574 if (type == 'p' || type == 'q')
9575 val /= 2;
9576
9577 if ((val & ((1 << op->shift) - 1)) != 0
9578 || val < (mintiny << op->shift)
9579 || val > (maxtiny << op->shift))
9580 needext = TRUE;
9581 else
9582 needext = FALSE;
9583
9584 if (warn && ext && ! needext)
9585 as_warn_where (file, line,
9586 _("extended operand requested but not required"));
9587 if (small && needext)
9588 as_bad_where (file, line, _("invalid unextended operand value"));
9589
9590 if (small || (! ext && ! needext))
9591 {
9592 int insnval;
9593
9594 *use_extend = FALSE;
9595 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9596 insnval <<= op->op_shift;
9597 *insn |= insnval;
9598 }
9599 else
9600 {
9601 long minext, maxext;
9602 int extval;
9603
9604 if (op->extu)
9605 {
9606 minext = 0;
9607 maxext = (1 << op->extbits) - 1;
9608 }
9609 else
9610 {
9611 minext = - (1 << (op->extbits - 1));
9612 maxext = (1 << (op->extbits - 1)) - 1;
9613 }
9614 if (val < minext || val > maxext)
9615 as_bad_where (file, line,
9616 _("operand value out of range for instruction"));
9617
9618 *use_extend = TRUE;
9619 if (op->extbits == 16)
9620 {
9621 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9622 val &= 0x1f;
9623 }
9624 else if (op->extbits == 15)
9625 {
9626 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9627 val &= 0xf;
9628 }
9629 else
9630 {
9631 extval = ((val & 0x1f) << 6) | (val & 0x20);
9632 val = 0;
9633 }
9634
9635 *extend = (unsigned short) extval;
9636 *insn |= val;
9637 }
9638 }
9639 \f
9640 struct percent_op_match
9641 {
9642 const char *str;
9643 bfd_reloc_code_real_type reloc;
9644 };
9645
9646 static const struct percent_op_match mips_percent_op[] =
9647 {
9648 {"%lo", BFD_RELOC_LO16},
9649 #ifdef OBJ_ELF
9650 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9651 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9652 {"%call16", BFD_RELOC_MIPS_CALL16},
9653 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9654 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9655 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9656 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9657 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9658 {"%got", BFD_RELOC_MIPS_GOT16},
9659 {"%gp_rel", BFD_RELOC_GPREL16},
9660 {"%half", BFD_RELOC_16},
9661 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9662 {"%higher", BFD_RELOC_MIPS_HIGHER},
9663 {"%neg", BFD_RELOC_MIPS_SUB},
9664 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9665 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9666 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9667 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9668 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9669 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9670 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9671 #endif
9672 {"%hi", BFD_RELOC_HI16_S}
9673 };
9674
9675 static const struct percent_op_match mips16_percent_op[] =
9676 {
9677 {"%lo", BFD_RELOC_MIPS16_LO16},
9678 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9679 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9680 };
9681
9682
9683 /* Return true if *STR points to a relocation operator. When returning true,
9684 move *STR over the operator and store its relocation code in *RELOC.
9685 Leave both *STR and *RELOC alone when returning false. */
9686
9687 static bfd_boolean
9688 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9689 {
9690 const struct percent_op_match *percent_op;
9691 size_t limit, i;
9692
9693 if (mips_opts.mips16)
9694 {
9695 percent_op = mips16_percent_op;
9696 limit = ARRAY_SIZE (mips16_percent_op);
9697 }
9698 else
9699 {
9700 percent_op = mips_percent_op;
9701 limit = ARRAY_SIZE (mips_percent_op);
9702 }
9703
9704 for (i = 0; i < limit; i++)
9705 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9706 {
9707 int len = strlen (percent_op[i].str);
9708
9709 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9710 continue;
9711
9712 *str += strlen (percent_op[i].str);
9713 *reloc = percent_op[i].reloc;
9714
9715 /* Check whether the output BFD supports this relocation.
9716 If not, issue an error and fall back on something safe. */
9717 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9718 {
9719 as_bad ("relocation %s isn't supported by the current ABI",
9720 percent_op[i].str);
9721 *reloc = BFD_RELOC_UNUSED;
9722 }
9723 return TRUE;
9724 }
9725 return FALSE;
9726 }
9727
9728
9729 /* Parse string STR as a 16-bit relocatable operand. Store the
9730 expression in *EP and the relocations in the array starting
9731 at RELOC. Return the number of relocation operators used.
9732
9733 On exit, EXPR_END points to the first character after the expression. */
9734
9735 static size_t
9736 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9737 char *str)
9738 {
9739 bfd_reloc_code_real_type reversed_reloc[3];
9740 size_t reloc_index, i;
9741 int crux_depth, str_depth;
9742 char *crux;
9743
9744 /* Search for the start of the main expression, recoding relocations
9745 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9746 of the main expression and with CRUX_DEPTH containing the number
9747 of open brackets at that point. */
9748 reloc_index = -1;
9749 str_depth = 0;
9750 do
9751 {
9752 reloc_index++;
9753 crux = str;
9754 crux_depth = str_depth;
9755
9756 /* Skip over whitespace and brackets, keeping count of the number
9757 of brackets. */
9758 while (*str == ' ' || *str == '\t' || *str == '(')
9759 if (*str++ == '(')
9760 str_depth++;
9761 }
9762 while (*str == '%'
9763 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9764 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9765
9766 my_getExpression (ep, crux);
9767 str = expr_end;
9768
9769 /* Match every open bracket. */
9770 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9771 if (*str++ == ')')
9772 crux_depth--;
9773
9774 if (crux_depth > 0)
9775 as_bad ("unclosed '('");
9776
9777 expr_end = str;
9778
9779 if (reloc_index != 0)
9780 {
9781 prev_reloc_op_frag = frag_now;
9782 for (i = 0; i < reloc_index; i++)
9783 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9784 }
9785
9786 return reloc_index;
9787 }
9788
9789 static void
9790 my_getExpression (expressionS *ep, char *str)
9791 {
9792 char *save_in;
9793 valueT val;
9794
9795 save_in = input_line_pointer;
9796 input_line_pointer = str;
9797 expression (ep);
9798 expr_end = input_line_pointer;
9799 input_line_pointer = save_in;
9800
9801 /* If we are in mips16 mode, and this is an expression based on `.',
9802 then we bump the value of the symbol by 1 since that is how other
9803 text symbols are handled. We don't bother to handle complex
9804 expressions, just `.' plus or minus a constant. */
9805 if (mips_opts.mips16
9806 && ep->X_op == O_symbol
9807 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9808 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9809 && symbol_get_frag (ep->X_add_symbol) == frag_now
9810 && symbol_constant_p (ep->X_add_symbol)
9811 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9812 S_SET_VALUE (ep->X_add_symbol, val + 1);
9813 }
9814
9815 /* Turn a string in input_line_pointer into a floating point constant
9816 of type TYPE, and store the appropriate bytes in *LITP. The number
9817 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9818 returned, or NULL on OK. */
9819
9820 char *
9821 md_atof (int type, char *litP, int *sizeP)
9822 {
9823 int prec;
9824 LITTLENUM_TYPE words[4];
9825 char *t;
9826 int i;
9827
9828 switch (type)
9829 {
9830 case 'f':
9831 prec = 2;
9832 break;
9833
9834 case 'd':
9835 prec = 4;
9836 break;
9837
9838 default:
9839 *sizeP = 0;
9840 return _("bad call to md_atof");
9841 }
9842
9843 t = atof_ieee (input_line_pointer, type, words);
9844 if (t)
9845 input_line_pointer = t;
9846
9847 *sizeP = prec * 2;
9848
9849 if (! target_big_endian)
9850 {
9851 for (i = prec - 1; i >= 0; i--)
9852 {
9853 md_number_to_chars (litP, words[i], 2);
9854 litP += 2;
9855 }
9856 }
9857 else
9858 {
9859 for (i = 0; i < prec; i++)
9860 {
9861 md_number_to_chars (litP, words[i], 2);
9862 litP += 2;
9863 }
9864 }
9865
9866 return NULL;
9867 }
9868
9869 void
9870 md_number_to_chars (char *buf, valueT val, int n)
9871 {
9872 if (target_big_endian)
9873 number_to_chars_bigendian (buf, val, n);
9874 else
9875 number_to_chars_littleendian (buf, val, n);
9876 }
9877 \f
9878 #ifdef OBJ_ELF
9879 static int support_64bit_objects(void)
9880 {
9881 const char **list, **l;
9882 int yes;
9883
9884 list = bfd_target_list ();
9885 for (l = list; *l != NULL; l++)
9886 #ifdef TE_TMIPS
9887 /* This is traditional mips */
9888 if (strcmp (*l, "elf64-tradbigmips") == 0
9889 || strcmp (*l, "elf64-tradlittlemips") == 0)
9890 #else
9891 if (strcmp (*l, "elf64-bigmips") == 0
9892 || strcmp (*l, "elf64-littlemips") == 0)
9893 #endif
9894 break;
9895 yes = (*l != NULL);
9896 free (list);
9897 return yes;
9898 }
9899 #endif /* OBJ_ELF */
9900
9901 const char *md_shortopts = "O::g::G:";
9902
9903 struct option md_longopts[] =
9904 {
9905 /* Options which specify architecture. */
9906 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
9907 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
9908 {"march", required_argument, NULL, OPTION_MARCH},
9909 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
9910 {"mtune", required_argument, NULL, OPTION_MTUNE},
9911 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
9912 {"mips0", no_argument, NULL, OPTION_MIPS1},
9913 {"mips1", no_argument, NULL, OPTION_MIPS1},
9914 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
9915 {"mips2", no_argument, NULL, OPTION_MIPS2},
9916 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
9917 {"mips3", no_argument, NULL, OPTION_MIPS3},
9918 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
9919 {"mips4", no_argument, NULL, OPTION_MIPS4},
9920 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
9921 {"mips5", no_argument, NULL, OPTION_MIPS5},
9922 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
9923 {"mips32", no_argument, NULL, OPTION_MIPS32},
9924 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
9925 {"mips64", no_argument, NULL, OPTION_MIPS64},
9926 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
9927 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
9928 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
9929 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
9930
9931 /* Options which specify Application Specific Extensions (ASEs). */
9932 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
9933 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
9934 {"mips16", no_argument, NULL, OPTION_MIPS16},
9935 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
9936 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9937 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
9938 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9939 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
9940 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9941 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
9942 {"mdmx", no_argument, NULL, OPTION_MDMX},
9943 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
9944 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9945
9946 /* Old-style architecture options. Don't add more of these. */
9947 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
9948 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
9949 {"m4650", no_argument, NULL, OPTION_M4650},
9950 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
9951 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9952 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
9953 {"m4010", no_argument, NULL, OPTION_M4010},
9954 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
9955 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9956 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
9957 {"m4100", no_argument, NULL, OPTION_M4100},
9958 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
9959 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9960 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
9961 {"m3900", no_argument, NULL, OPTION_M3900},
9962 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
9963 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9964
9965 /* Options which enable bug fixes. */
9966 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
9967 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
9968 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9969 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
9970 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9971 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9972 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
9973 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
9974 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
9975 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
9976
9977 /* Miscellaneous options. */
9978 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
9979 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
9980 {"trap", no_argument, NULL, OPTION_TRAP},
9981 {"no-break", no_argument, NULL, OPTION_TRAP},
9982 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
9983 {"break", no_argument, NULL, OPTION_BREAK},
9984 {"no-trap", no_argument, NULL, OPTION_BREAK},
9985 #define OPTION_EB (OPTION_MISC_BASE + 2)
9986 {"EB", no_argument, NULL, OPTION_EB},
9987 #define OPTION_EL (OPTION_MISC_BASE + 3)
9988 {"EL", no_argument, NULL, OPTION_EL},
9989 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
9990 {"mfp32", no_argument, NULL, OPTION_FP32},
9991 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
9992 {"mgp32", no_argument, NULL, OPTION_GP32},
9993 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
9994 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9995 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
9996 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9997 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
9998 {"mfp64", no_argument, NULL, OPTION_FP64},
9999 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10000 {"mgp64", no_argument, NULL, OPTION_GP64},
10001 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10002 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10003 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10004 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10005 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10006 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10007 {"mshared", no_argument, NULL, OPTION_MSHARED},
10008 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10009 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10010 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10011 {"msym32", no_argument, NULL, OPTION_MSYM32},
10012 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10013
10014 /* ELF-specific options. */
10015 #ifdef OBJ_ELF
10016 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10017 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10018 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10019 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10020 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10021 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10022 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10023 {"xgot", no_argument, NULL, OPTION_XGOT},
10024 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10025 {"mabi", required_argument, NULL, OPTION_MABI},
10026 #define OPTION_32 (OPTION_ELF_BASE + 4)
10027 {"32", no_argument, NULL, OPTION_32},
10028 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10029 {"n32", no_argument, NULL, OPTION_N32},
10030 #define OPTION_64 (OPTION_ELF_BASE + 6)
10031 {"64", no_argument, NULL, OPTION_64},
10032 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10033 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10034 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10035 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10036 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10037 {"mpdr", no_argument, NULL, OPTION_PDR},
10038 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10039 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10040 #endif /* OBJ_ELF */
10041
10042 {NULL, no_argument, NULL, 0}
10043 };
10044 size_t md_longopts_size = sizeof (md_longopts);
10045
10046 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10047 NEW_VALUE. Warn if another value was already specified. Note:
10048 we have to defer parsing the -march and -mtune arguments in order
10049 to handle 'from-abi' correctly, since the ABI might be specified
10050 in a later argument. */
10051
10052 static void
10053 mips_set_option_string (const char **string_ptr, const char *new_value)
10054 {
10055 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10056 as_warn (_("A different %s was already specified, is now %s"),
10057 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10058 new_value);
10059
10060 *string_ptr = new_value;
10061 }
10062
10063 int
10064 md_parse_option (int c, char *arg)
10065 {
10066 switch (c)
10067 {
10068 case OPTION_CONSTRUCT_FLOATS:
10069 mips_disable_float_construction = 0;
10070 break;
10071
10072 case OPTION_NO_CONSTRUCT_FLOATS:
10073 mips_disable_float_construction = 1;
10074 break;
10075
10076 case OPTION_TRAP:
10077 mips_trap = 1;
10078 break;
10079
10080 case OPTION_BREAK:
10081 mips_trap = 0;
10082 break;
10083
10084 case OPTION_EB:
10085 target_big_endian = 1;
10086 break;
10087
10088 case OPTION_EL:
10089 target_big_endian = 0;
10090 break;
10091
10092 case 'O':
10093 if (arg && arg[1] == '0')
10094 mips_optimize = 1;
10095 else
10096 mips_optimize = 2;
10097 break;
10098
10099 case 'g':
10100 if (arg == NULL)
10101 mips_debug = 2;
10102 else
10103 mips_debug = atoi (arg);
10104 /* When the MIPS assembler sees -g or -g2, it does not do
10105 optimizations which limit full symbolic debugging. We take
10106 that to be equivalent to -O0. */
10107 if (mips_debug == 2)
10108 mips_optimize = 1;
10109 break;
10110
10111 case OPTION_MIPS1:
10112 file_mips_isa = ISA_MIPS1;
10113 break;
10114
10115 case OPTION_MIPS2:
10116 file_mips_isa = ISA_MIPS2;
10117 break;
10118
10119 case OPTION_MIPS3:
10120 file_mips_isa = ISA_MIPS3;
10121 break;
10122
10123 case OPTION_MIPS4:
10124 file_mips_isa = ISA_MIPS4;
10125 break;
10126
10127 case OPTION_MIPS5:
10128 file_mips_isa = ISA_MIPS5;
10129 break;
10130
10131 case OPTION_MIPS32:
10132 file_mips_isa = ISA_MIPS32;
10133 break;
10134
10135 case OPTION_MIPS32R2:
10136 file_mips_isa = ISA_MIPS32R2;
10137 break;
10138
10139 case OPTION_MIPS64R2:
10140 file_mips_isa = ISA_MIPS64R2;
10141 break;
10142
10143 case OPTION_MIPS64:
10144 file_mips_isa = ISA_MIPS64;
10145 break;
10146
10147 case OPTION_MTUNE:
10148 mips_set_option_string (&mips_tune_string, arg);
10149 break;
10150
10151 case OPTION_MARCH:
10152 mips_set_option_string (&mips_arch_string, arg);
10153 break;
10154
10155 case OPTION_M4650:
10156 mips_set_option_string (&mips_arch_string, "4650");
10157 mips_set_option_string (&mips_tune_string, "4650");
10158 break;
10159
10160 case OPTION_NO_M4650:
10161 break;
10162
10163 case OPTION_M4010:
10164 mips_set_option_string (&mips_arch_string, "4010");
10165 mips_set_option_string (&mips_tune_string, "4010");
10166 break;
10167
10168 case OPTION_NO_M4010:
10169 break;
10170
10171 case OPTION_M4100:
10172 mips_set_option_string (&mips_arch_string, "4100");
10173 mips_set_option_string (&mips_tune_string, "4100");
10174 break;
10175
10176 case OPTION_NO_M4100:
10177 break;
10178
10179 case OPTION_M3900:
10180 mips_set_option_string (&mips_arch_string, "3900");
10181 mips_set_option_string (&mips_tune_string, "3900");
10182 break;
10183
10184 case OPTION_NO_M3900:
10185 break;
10186
10187 case OPTION_MDMX:
10188 mips_opts.ase_mdmx = 1;
10189 break;
10190
10191 case OPTION_NO_MDMX:
10192 mips_opts.ase_mdmx = 0;
10193 break;
10194
10195 case OPTION_MIPS16:
10196 mips_opts.mips16 = 1;
10197 mips_no_prev_insn (FALSE);
10198 break;
10199
10200 case OPTION_NO_MIPS16:
10201 mips_opts.mips16 = 0;
10202 mips_no_prev_insn (FALSE);
10203 break;
10204
10205 case OPTION_MIPS3D:
10206 mips_opts.ase_mips3d = 1;
10207 break;
10208
10209 case OPTION_NO_MIPS3D:
10210 mips_opts.ase_mips3d = 0;
10211 break;
10212
10213 case OPTION_FIX_VR4120:
10214 mips_fix_vr4120 = 1;
10215 break;
10216
10217 case OPTION_NO_FIX_VR4120:
10218 mips_fix_vr4120 = 0;
10219 break;
10220
10221 case OPTION_RELAX_BRANCH:
10222 mips_relax_branch = 1;
10223 break;
10224
10225 case OPTION_NO_RELAX_BRANCH:
10226 mips_relax_branch = 0;
10227 break;
10228
10229 case OPTION_MSHARED:
10230 mips_in_shared = TRUE;
10231 break;
10232
10233 case OPTION_MNO_SHARED:
10234 mips_in_shared = FALSE;
10235 break;
10236
10237 case OPTION_MSYM32:
10238 mips_opts.sym32 = TRUE;
10239 break;
10240
10241 case OPTION_MNO_SYM32:
10242 mips_opts.sym32 = FALSE;
10243 break;
10244
10245 #ifdef OBJ_ELF
10246 /* When generating ELF code, we permit -KPIC and -call_shared to
10247 select SVR4_PIC, and -non_shared to select no PIC. This is
10248 intended to be compatible with Irix 5. */
10249 case OPTION_CALL_SHARED:
10250 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10251 {
10252 as_bad (_("-call_shared is supported only for ELF format"));
10253 return 0;
10254 }
10255 mips_pic = SVR4_PIC;
10256 mips_abicalls = TRUE;
10257 if (g_switch_seen && g_switch_value != 0)
10258 {
10259 as_bad (_("-G may not be used with SVR4 PIC code"));
10260 return 0;
10261 }
10262 g_switch_value = 0;
10263 break;
10264
10265 case OPTION_NON_SHARED:
10266 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10267 {
10268 as_bad (_("-non_shared is supported only for ELF format"));
10269 return 0;
10270 }
10271 mips_pic = NO_PIC;
10272 mips_abicalls = FALSE;
10273 break;
10274
10275 /* The -xgot option tells the assembler to use 32 offsets when
10276 accessing the got in SVR4_PIC mode. It is for Irix
10277 compatibility. */
10278 case OPTION_XGOT:
10279 mips_big_got = 1;
10280 break;
10281 #endif /* OBJ_ELF */
10282
10283 case 'G':
10284 g_switch_value = atoi (arg);
10285 g_switch_seen = 1;
10286 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10287 {
10288 as_bad (_("-G may not be used with SVR4 PIC code"));
10289 return 0;
10290 }
10291 break;
10292
10293 #ifdef OBJ_ELF
10294 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10295 and -mabi=64. */
10296 case OPTION_32:
10297 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10298 {
10299 as_bad (_("-32 is supported for ELF format only"));
10300 return 0;
10301 }
10302 mips_abi = O32_ABI;
10303 break;
10304
10305 case OPTION_N32:
10306 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10307 {
10308 as_bad (_("-n32 is supported for ELF format only"));
10309 return 0;
10310 }
10311 mips_abi = N32_ABI;
10312 break;
10313
10314 case OPTION_64:
10315 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10316 {
10317 as_bad (_("-64 is supported for ELF format only"));
10318 return 0;
10319 }
10320 mips_abi = N64_ABI;
10321 if (! support_64bit_objects())
10322 as_fatal (_("No compiled in support for 64 bit object file format"));
10323 break;
10324 #endif /* OBJ_ELF */
10325
10326 case OPTION_GP32:
10327 file_mips_gp32 = 1;
10328 break;
10329
10330 case OPTION_GP64:
10331 file_mips_gp32 = 0;
10332 break;
10333
10334 case OPTION_FP32:
10335 file_mips_fp32 = 1;
10336 break;
10337
10338 case OPTION_FP64:
10339 file_mips_fp32 = 0;
10340 break;
10341
10342 #ifdef OBJ_ELF
10343 case OPTION_MABI:
10344 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10345 {
10346 as_bad (_("-mabi is supported for ELF format only"));
10347 return 0;
10348 }
10349 if (strcmp (arg, "32") == 0)
10350 mips_abi = O32_ABI;
10351 else if (strcmp (arg, "o64") == 0)
10352 mips_abi = O64_ABI;
10353 else if (strcmp (arg, "n32") == 0)
10354 mips_abi = N32_ABI;
10355 else if (strcmp (arg, "64") == 0)
10356 {
10357 mips_abi = N64_ABI;
10358 if (! support_64bit_objects())
10359 as_fatal (_("No compiled in support for 64 bit object file "
10360 "format"));
10361 }
10362 else if (strcmp (arg, "eabi") == 0)
10363 mips_abi = EABI_ABI;
10364 else
10365 {
10366 as_fatal (_("invalid abi -mabi=%s"), arg);
10367 return 0;
10368 }
10369 break;
10370 #endif /* OBJ_ELF */
10371
10372 case OPTION_M7000_HILO_FIX:
10373 mips_7000_hilo_fix = TRUE;
10374 break;
10375
10376 case OPTION_MNO_7000_HILO_FIX:
10377 mips_7000_hilo_fix = FALSE;
10378 break;
10379
10380 #ifdef OBJ_ELF
10381 case OPTION_MDEBUG:
10382 mips_flag_mdebug = TRUE;
10383 break;
10384
10385 case OPTION_NO_MDEBUG:
10386 mips_flag_mdebug = FALSE;
10387 break;
10388
10389 case OPTION_PDR:
10390 mips_flag_pdr = TRUE;
10391 break;
10392
10393 case OPTION_NO_PDR:
10394 mips_flag_pdr = FALSE;
10395 break;
10396 #endif /* OBJ_ELF */
10397
10398 default:
10399 return 0;
10400 }
10401
10402 return 1;
10403 }
10404 \f
10405 /* Set up globals to generate code for the ISA or processor
10406 described by INFO. */
10407
10408 static void
10409 mips_set_architecture (const struct mips_cpu_info *info)
10410 {
10411 if (info != 0)
10412 {
10413 file_mips_arch = info->cpu;
10414 mips_opts.arch = info->cpu;
10415 mips_opts.isa = info->isa;
10416 }
10417 }
10418
10419
10420 /* Likewise for tuning. */
10421
10422 static void
10423 mips_set_tune (const struct mips_cpu_info *info)
10424 {
10425 if (info != 0)
10426 mips_tune = info->cpu;
10427 }
10428
10429
10430 void
10431 mips_after_parse_args (void)
10432 {
10433 const struct mips_cpu_info *arch_info = 0;
10434 const struct mips_cpu_info *tune_info = 0;
10435
10436 /* GP relative stuff not working for PE */
10437 if (strncmp (TARGET_OS, "pe", 2) == 0)
10438 {
10439 if (g_switch_seen && g_switch_value != 0)
10440 as_bad (_("-G not supported in this configuration."));
10441 g_switch_value = 0;
10442 }
10443
10444 if (mips_abi == NO_ABI)
10445 mips_abi = MIPS_DEFAULT_ABI;
10446
10447 /* The following code determines the architecture and register size.
10448 Similar code was added to GCC 3.3 (see override_options() in
10449 config/mips/mips.c). The GAS and GCC code should be kept in sync
10450 as much as possible. */
10451
10452 if (mips_arch_string != 0)
10453 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10454
10455 if (file_mips_isa != ISA_UNKNOWN)
10456 {
10457 /* Handle -mipsN. At this point, file_mips_isa contains the
10458 ISA level specified by -mipsN, while arch_info->isa contains
10459 the -march selection (if any). */
10460 if (arch_info != 0)
10461 {
10462 /* -march takes precedence over -mipsN, since it is more descriptive.
10463 There's no harm in specifying both as long as the ISA levels
10464 are the same. */
10465 if (file_mips_isa != arch_info->isa)
10466 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10467 mips_cpu_info_from_isa (file_mips_isa)->name,
10468 mips_cpu_info_from_isa (arch_info->isa)->name);
10469 }
10470 else
10471 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10472 }
10473
10474 if (arch_info == 0)
10475 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10476
10477 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10478 as_bad ("-march=%s is not compatible with the selected ABI",
10479 arch_info->name);
10480
10481 mips_set_architecture (arch_info);
10482
10483 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10484 if (mips_tune_string != 0)
10485 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10486
10487 if (tune_info == 0)
10488 mips_set_tune (arch_info);
10489 else
10490 mips_set_tune (tune_info);
10491
10492 if (file_mips_gp32 >= 0)
10493 {
10494 /* The user specified the size of the integer registers. Make sure
10495 it agrees with the ABI and ISA. */
10496 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10497 as_bad (_("-mgp64 used with a 32-bit processor"));
10498 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10499 as_bad (_("-mgp32 used with a 64-bit ABI"));
10500 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10501 as_bad (_("-mgp64 used with a 32-bit ABI"));
10502 }
10503 else
10504 {
10505 /* Infer the integer register size from the ABI and processor.
10506 Restrict ourselves to 32-bit registers if that's all the
10507 processor has, or if the ABI cannot handle 64-bit registers. */
10508 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10509 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10510 }
10511
10512 /* ??? GAS treats single-float processors as though they had 64-bit
10513 float registers (although it complains when double-precision
10514 instructions are used). As things stand, saying they have 32-bit
10515 registers would lead to spurious "register must be even" messages.
10516 So here we assume float registers are always the same size as
10517 integer ones, unless the user says otherwise. */
10518 if (file_mips_fp32 < 0)
10519 file_mips_fp32 = file_mips_gp32;
10520
10521 /* End of GCC-shared inference code. */
10522
10523 /* This flag is set when we have a 64-bit capable CPU but use only
10524 32-bit wide registers. Note that EABI does not use it. */
10525 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10526 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10527 || mips_abi == O32_ABI))
10528 mips_32bitmode = 1;
10529
10530 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10531 as_bad (_("trap exception not supported at ISA 1"));
10532
10533 /* If the selected architecture includes support for ASEs, enable
10534 generation of code for them. */
10535 if (mips_opts.mips16 == -1)
10536 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10537 if (mips_opts.ase_mips3d == -1)
10538 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10539 if (mips_opts.ase_mdmx == -1)
10540 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10541
10542 file_mips_isa = mips_opts.isa;
10543 file_ase_mips16 = mips_opts.mips16;
10544 file_ase_mips3d = mips_opts.ase_mips3d;
10545 file_ase_mdmx = mips_opts.ase_mdmx;
10546 mips_opts.gp32 = file_mips_gp32;
10547 mips_opts.fp32 = file_mips_fp32;
10548
10549 if (mips_flag_mdebug < 0)
10550 {
10551 #ifdef OBJ_MAYBE_ECOFF
10552 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10553 mips_flag_mdebug = 1;
10554 else
10555 #endif /* OBJ_MAYBE_ECOFF */
10556 mips_flag_mdebug = 0;
10557 }
10558 }
10559 \f
10560 void
10561 mips_init_after_args (void)
10562 {
10563 /* initialize opcodes */
10564 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10565 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10566 }
10567
10568 long
10569 md_pcrel_from (fixS *fixP)
10570 {
10571 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10572 switch (fixP->fx_r_type)
10573 {
10574 case BFD_RELOC_16_PCREL_S2:
10575 case BFD_RELOC_MIPS_JMP:
10576 /* Return the address of the delay slot. */
10577 return addr + 4;
10578 default:
10579 return addr;
10580 }
10581 }
10582
10583 /* This is called before the symbol table is processed. In order to
10584 work with gcc when using mips-tfile, we must keep all local labels.
10585 However, in other cases, we want to discard them. If we were
10586 called with -g, but we didn't see any debugging information, it may
10587 mean that gcc is smuggling debugging information through to
10588 mips-tfile, in which case we must generate all local labels. */
10589
10590 void
10591 mips_frob_file_before_adjust (void)
10592 {
10593 #ifndef NO_ECOFF_DEBUGGING
10594 if (ECOFF_DEBUGGING
10595 && mips_debug != 0
10596 && ! ecoff_debugging_seen)
10597 flag_keep_locals = 1;
10598 #endif
10599 }
10600
10601 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10602 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10603 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10604 relocation operators.
10605
10606 For our purposes, a %lo() expression matches a %got() or %hi()
10607 expression if:
10608
10609 (a) it refers to the same symbol; and
10610 (b) the offset applied in the %lo() expression is no lower than
10611 the offset applied in the %got() or %hi().
10612
10613 (b) allows us to cope with code like:
10614
10615 lui $4,%hi(foo)
10616 lh $4,%lo(foo+2)($4)
10617
10618 ...which is legal on RELA targets, and has a well-defined behaviour
10619 if the user knows that adding 2 to "foo" will not induce a carry to
10620 the high 16 bits.
10621
10622 When several %lo()s match a particular %got() or %hi(), we use the
10623 following rules to distinguish them:
10624
10625 (1) %lo()s with smaller offsets are a better match than %lo()s with
10626 higher offsets.
10627
10628 (2) %lo()s with no matching %got() or %hi() are better than those
10629 that already have a matching %got() or %hi().
10630
10631 (3) later %lo()s are better than earlier %lo()s.
10632
10633 These rules are applied in order.
10634
10635 (1) means, among other things, that %lo()s with identical offsets are
10636 chosen if they exist.
10637
10638 (2) means that we won't associate several high-part relocations with
10639 the same low-part relocation unless there's no alternative. Having
10640 several high parts for the same low part is a GNU extension; this rule
10641 allows careful users to avoid it.
10642
10643 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10644 with the last high-part relocation being at the front of the list.
10645 It therefore makes sense to choose the last matching low-part
10646 relocation, all other things being equal. It's also easier
10647 to code that way. */
10648
10649 void
10650 mips_frob_file (void)
10651 {
10652 struct mips_hi_fixup *l;
10653
10654 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10655 {
10656 segment_info_type *seginfo;
10657 bfd_boolean matched_lo_p;
10658 fixS **hi_pos, **lo_pos, **pos;
10659
10660 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10661
10662 /* If a GOT16 relocation turns out to be against a global symbol,
10663 there isn't supposed to be a matching LO. */
10664 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10665 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10666 continue;
10667
10668 /* Check quickly whether the next fixup happens to be a matching %lo. */
10669 if (fixup_has_matching_lo_p (l->fixp))
10670 continue;
10671
10672 seginfo = seg_info (l->seg);
10673
10674 /* Set HI_POS to the position of this relocation in the chain.
10675 Set LO_POS to the position of the chosen low-part relocation.
10676 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10677 relocation that matches an immediately-preceding high-part
10678 relocation. */
10679 hi_pos = NULL;
10680 lo_pos = NULL;
10681 matched_lo_p = FALSE;
10682 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10683 {
10684 if (*pos == l->fixp)
10685 hi_pos = pos;
10686
10687 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10688 && (*pos)->fx_addsy == l->fixp->fx_addsy
10689 && (*pos)->fx_offset >= l->fixp->fx_offset
10690 && (lo_pos == NULL
10691 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10692 || (!matched_lo_p
10693 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10694 lo_pos = pos;
10695
10696 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10697 && fixup_has_matching_lo_p (*pos));
10698 }
10699
10700 /* If we found a match, remove the high-part relocation from its
10701 current position and insert it before the low-part relocation.
10702 Make the offsets match so that fixup_has_matching_lo_p()
10703 will return true.
10704
10705 We don't warn about unmatched high-part relocations since some
10706 versions of gcc have been known to emit dead "lui ...%hi(...)"
10707 instructions. */
10708 if (lo_pos != NULL)
10709 {
10710 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10711 if (l->fixp->fx_next != *lo_pos)
10712 {
10713 *hi_pos = l->fixp->fx_next;
10714 l->fixp->fx_next = *lo_pos;
10715 *lo_pos = l->fixp;
10716 }
10717 }
10718 }
10719 }
10720
10721 /* We may have combined relocations without symbols in the N32/N64 ABI.
10722 We have to prevent gas from dropping them. */
10723
10724 int
10725 mips_force_relocation (fixS *fixp)
10726 {
10727 if (generic_force_reloc (fixp))
10728 return 1;
10729
10730 if (HAVE_NEWABI
10731 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10732 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10733 || fixp->fx_r_type == BFD_RELOC_HI16_S
10734 || fixp->fx_r_type == BFD_RELOC_LO16))
10735 return 1;
10736
10737 return 0;
10738 }
10739
10740 /* This hook is called before a fix is simplified. We don't really
10741 decide whether to skip a fix here. Rather, we turn global symbols
10742 used as branch targets into local symbols, such that they undergo
10743 simplification. We can only do this if the symbol is defined and
10744 it is in the same section as the branch. If this doesn't hold, we
10745 emit a better error message than just saying the relocation is not
10746 valid for the selected object format.
10747
10748 FIXP is the fix-up we're going to try to simplify, SEG is the
10749 segment in which the fix up occurs. The return value should be
10750 non-zero to indicate the fix-up is valid for further
10751 simplifications. */
10752
10753 int
10754 mips_validate_fix (struct fix *fixP, asection *seg)
10755 {
10756 /* There's a lot of discussion on whether it should be possible to
10757 use R_MIPS_PC16 to represent branch relocations. The outcome
10758 seems to be that it can, but gas/bfd are very broken in creating
10759 RELA relocations for this, so for now we only accept branches to
10760 symbols in the same section. Anything else is of dubious value,
10761 since there's no guarantee that at link time the symbol would be
10762 in range. Even for branches to local symbols this is arguably
10763 wrong, since it we assume the symbol is not going to be
10764 overridden, which should be possible per ELF library semantics,
10765 but then, there isn't a dynamic relocation that could be used to
10766 this effect, and the target would likely be out of range as well.
10767
10768 Unfortunately, it seems that there is too much code out there
10769 that relies on branches to symbols that are global to be resolved
10770 as if they were local, like the IRIX tools do, so we do it as
10771 well, but with a warning so that people are reminded to fix their
10772 code. If we ever get back to using R_MIPS_PC16 for branch
10773 targets, this entire block should go away (and probably the
10774 whole function). */
10775
10776 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10777 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10778 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10779 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10780 && fixP->fx_addsy)
10781 {
10782 if (! S_IS_DEFINED (fixP->fx_addsy))
10783 {
10784 as_bad_where (fixP->fx_file, fixP->fx_line,
10785 _("Cannot branch to undefined symbol."));
10786 /* Avoid any further errors about this fixup. */
10787 fixP->fx_done = 1;
10788 }
10789 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10790 {
10791 as_bad_where (fixP->fx_file, fixP->fx_line,
10792 _("Cannot branch to symbol in another section."));
10793 fixP->fx_done = 1;
10794 }
10795 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10796 {
10797 symbolS *sym = fixP->fx_addsy;
10798
10799 if (mips_pic == SVR4_PIC)
10800 as_warn_where (fixP->fx_file, fixP->fx_line,
10801 _("Pretending global symbol used as branch target is local."));
10802
10803 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10804 S_GET_SEGMENT (sym),
10805 S_GET_VALUE (sym),
10806 symbol_get_frag (sym));
10807 copy_symbol_attributes (fixP->fx_addsy, sym);
10808 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10809 assert (symbol_resolved_p (sym));
10810 symbol_mark_resolved (fixP->fx_addsy);
10811 }
10812 }
10813
10814 return 1;
10815 }
10816
10817 /* Apply a fixup to the object file. */
10818
10819 void
10820 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
10821 {
10822 bfd_byte *buf;
10823 long insn;
10824 reloc_howto_type *howto;
10825
10826 /* We ignore generic BFD relocations we don't know about. */
10827 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10828 if (! howto)
10829 return;
10830
10831 assert (fixP->fx_size == 4
10832 || fixP->fx_r_type == BFD_RELOC_16
10833 || fixP->fx_r_type == BFD_RELOC_64
10834 || fixP->fx_r_type == BFD_RELOC_CTOR
10835 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10836 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10837 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10838
10839 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
10840
10841 assert (! fixP->fx_pcrel);
10842
10843 /* Don't treat parts of a composite relocation as done. There are two
10844 reasons for this:
10845
10846 (1) The second and third parts will be against 0 (RSS_UNDEF) but
10847 should nevertheless be emitted if the first part is.
10848
10849 (2) In normal usage, composite relocations are never assembly-time
10850 constants. The easiest way of dealing with the pathological
10851 exceptions is to generate a relocation against STN_UNDEF and
10852 leave everything up to the linker. */
10853 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
10854 fixP->fx_done = 1;
10855
10856 switch (fixP->fx_r_type)
10857 {
10858 case BFD_RELOC_MIPS_TLS_GD:
10859 case BFD_RELOC_MIPS_TLS_LDM:
10860 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
10861 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
10862 case BFD_RELOC_MIPS_TLS_GOTTPREL:
10863 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
10864 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
10865 S_SET_THREAD_LOCAL (fixP->fx_addsy);
10866 /* fall through */
10867
10868 case BFD_RELOC_MIPS_JMP:
10869 case BFD_RELOC_MIPS_SHIFT5:
10870 case BFD_RELOC_MIPS_SHIFT6:
10871 case BFD_RELOC_MIPS_GOT_DISP:
10872 case BFD_RELOC_MIPS_GOT_PAGE:
10873 case BFD_RELOC_MIPS_GOT_OFST:
10874 case BFD_RELOC_MIPS_SUB:
10875 case BFD_RELOC_MIPS_INSERT_A:
10876 case BFD_RELOC_MIPS_INSERT_B:
10877 case BFD_RELOC_MIPS_DELETE:
10878 case BFD_RELOC_MIPS_HIGHEST:
10879 case BFD_RELOC_MIPS_HIGHER:
10880 case BFD_RELOC_MIPS_SCN_DISP:
10881 case BFD_RELOC_MIPS_REL16:
10882 case BFD_RELOC_MIPS_RELGOT:
10883 case BFD_RELOC_MIPS_JALR:
10884 case BFD_RELOC_HI16:
10885 case BFD_RELOC_HI16_S:
10886 case BFD_RELOC_GPREL16:
10887 case BFD_RELOC_MIPS_LITERAL:
10888 case BFD_RELOC_MIPS_CALL16:
10889 case BFD_RELOC_MIPS_GOT16:
10890 case BFD_RELOC_GPREL32:
10891 case BFD_RELOC_MIPS_GOT_HI16:
10892 case BFD_RELOC_MIPS_GOT_LO16:
10893 case BFD_RELOC_MIPS_CALL_HI16:
10894 case BFD_RELOC_MIPS_CALL_LO16:
10895 case BFD_RELOC_MIPS16_GPREL:
10896 case BFD_RELOC_MIPS16_HI16:
10897 case BFD_RELOC_MIPS16_HI16_S:
10898 assert (! fixP->fx_pcrel);
10899 /* Nothing needed to do. The value comes from the reloc entry */
10900 break;
10901
10902 case BFD_RELOC_MIPS16_JMP:
10903 /* We currently always generate a reloc against a symbol, which
10904 means that we don't want an addend even if the symbol is
10905 defined. */
10906 *valP = 0;
10907 break;
10908
10909 case BFD_RELOC_64:
10910 /* This is handled like BFD_RELOC_32, but we output a sign
10911 extended value if we are only 32 bits. */
10912 if (fixP->fx_done)
10913 {
10914 if (8 <= sizeof (valueT))
10915 md_number_to_chars ((char *) buf, *valP, 8);
10916 else
10917 {
10918 valueT hiv;
10919
10920 if ((*valP & 0x80000000) != 0)
10921 hiv = 0xffffffff;
10922 else
10923 hiv = 0;
10924 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
10925 *valP, 4);
10926 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
10927 hiv, 4);
10928 }
10929 }
10930 break;
10931
10932 case BFD_RELOC_RVA:
10933 case BFD_RELOC_32:
10934 /* If we are deleting this reloc entry, we must fill in the
10935 value now. This can happen if we have a .word which is not
10936 resolved when it appears but is later defined. */
10937 if (fixP->fx_done)
10938 md_number_to_chars ((char *) buf, *valP, 4);
10939 break;
10940
10941 case BFD_RELOC_16:
10942 /* If we are deleting this reloc entry, we must fill in the
10943 value now. */
10944 if (fixP->fx_done)
10945 md_number_to_chars ((char *) buf, *valP, 2);
10946 break;
10947
10948 case BFD_RELOC_LO16:
10949 case BFD_RELOC_MIPS16_LO16:
10950 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
10951 may be safe to remove, but if so it's not obvious. */
10952 /* When handling an embedded PIC switch statement, we can wind
10953 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10954 if (fixP->fx_done)
10955 {
10956 if (*valP + 0x8000 > 0xffff)
10957 as_bad_where (fixP->fx_file, fixP->fx_line,
10958 _("relocation overflow"));
10959 if (target_big_endian)
10960 buf += 2;
10961 md_number_to_chars ((char *) buf, *valP, 2);
10962 }
10963 break;
10964
10965 case BFD_RELOC_16_PCREL_S2:
10966 if ((*valP & 0x3) != 0)
10967 as_bad_where (fixP->fx_file, fixP->fx_line,
10968 _("Branch to odd address (%lx)"), (long) *valP);
10969
10970 /*
10971 * We need to save the bits in the instruction since fixup_segment()
10972 * might be deleting the relocation entry (i.e., a branch within
10973 * the current segment).
10974 */
10975 if (! fixP->fx_done)
10976 break;
10977
10978 /* update old instruction data */
10979 if (target_big_endian)
10980 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10981 else
10982 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10983
10984 if (*valP + 0x20000 <= 0x3ffff)
10985 {
10986 insn |= (*valP >> 2) & 0xffff;
10987 md_number_to_chars ((char *) buf, insn, 4);
10988 }
10989 else if (mips_pic == NO_PIC
10990 && fixP->fx_done
10991 && fixP->fx_frag->fr_address >= text_section->vma
10992 && (fixP->fx_frag->fr_address
10993 < text_section->vma + bfd_get_section_size (text_section))
10994 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10995 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10996 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10997 {
10998 /* The branch offset is too large. If this is an
10999 unconditional branch, and we are not generating PIC code,
11000 we can convert it to an absolute jump instruction. */
11001 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11002 insn = 0x0c000000; /* jal */
11003 else
11004 insn = 0x08000000; /* j */
11005 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11006 fixP->fx_done = 0;
11007 fixP->fx_addsy = section_symbol (text_section);
11008 *valP += md_pcrel_from (fixP);
11009 md_number_to_chars ((char *) buf, insn, 4);
11010 }
11011 else
11012 {
11013 /* If we got here, we have branch-relaxation disabled,
11014 and there's nothing we can do to fix this instruction
11015 without turning it into a longer sequence. */
11016 as_bad_where (fixP->fx_file, fixP->fx_line,
11017 _("Branch out of range"));
11018 }
11019 break;
11020
11021 case BFD_RELOC_VTABLE_INHERIT:
11022 fixP->fx_done = 0;
11023 if (fixP->fx_addsy
11024 && !S_IS_DEFINED (fixP->fx_addsy)
11025 && !S_IS_WEAK (fixP->fx_addsy))
11026 S_SET_WEAK (fixP->fx_addsy);
11027 break;
11028
11029 case BFD_RELOC_VTABLE_ENTRY:
11030 fixP->fx_done = 0;
11031 break;
11032
11033 default:
11034 internalError ();
11035 }
11036
11037 /* Remember value for tc_gen_reloc. */
11038 fixP->fx_addnumber = *valP;
11039 }
11040
11041 static symbolS *
11042 get_symbol (void)
11043 {
11044 int c;
11045 char *name;
11046 symbolS *p;
11047
11048 name = input_line_pointer;
11049 c = get_symbol_end ();
11050 p = (symbolS *) symbol_find_or_make (name);
11051 *input_line_pointer = c;
11052 return p;
11053 }
11054
11055 /* Align the current frag to a given power of two. The MIPS assembler
11056 also automatically adjusts any preceding label. */
11057
11058 static void
11059 mips_align (int to, int fill, symbolS *label)
11060 {
11061 mips_emit_delays (FALSE);
11062 frag_align (to, fill, 0);
11063 record_alignment (now_seg, to);
11064 if (label != NULL)
11065 {
11066 assert (S_GET_SEGMENT (label) == now_seg);
11067 symbol_set_frag (label, frag_now);
11068 S_SET_VALUE (label, (valueT) frag_now_fix ());
11069 }
11070 }
11071
11072 /* Align to a given power of two. .align 0 turns off the automatic
11073 alignment used by the data creating pseudo-ops. */
11074
11075 static void
11076 s_align (int x ATTRIBUTE_UNUSED)
11077 {
11078 register int temp;
11079 register long temp_fill;
11080 long max_alignment = 15;
11081
11082 /*
11083
11084 o Note that the assembler pulls down any immediately preceding label
11085 to the aligned address.
11086 o It's not documented but auto alignment is reinstated by
11087 a .align pseudo instruction.
11088 o Note also that after auto alignment is turned off the mips assembler
11089 issues an error on attempt to assemble an improperly aligned data item.
11090 We don't.
11091
11092 */
11093
11094 temp = get_absolute_expression ();
11095 if (temp > max_alignment)
11096 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11097 else if (temp < 0)
11098 {
11099 as_warn (_("Alignment negative: 0 assumed."));
11100 temp = 0;
11101 }
11102 if (*input_line_pointer == ',')
11103 {
11104 ++input_line_pointer;
11105 temp_fill = get_absolute_expression ();
11106 }
11107 else
11108 temp_fill = 0;
11109 if (temp)
11110 {
11111 auto_align = 1;
11112 mips_align (temp, (int) temp_fill,
11113 insn_labels != NULL ? insn_labels->label : NULL);
11114 }
11115 else
11116 {
11117 auto_align = 0;
11118 }
11119
11120 demand_empty_rest_of_line ();
11121 }
11122
11123 void
11124 mips_flush_pending_output (void)
11125 {
11126 mips_emit_delays (FALSE);
11127 mips_clear_insn_labels ();
11128 }
11129
11130 static void
11131 s_change_sec (int sec)
11132 {
11133 segT seg;
11134
11135 #ifdef OBJ_ELF
11136 /* The ELF backend needs to know that we are changing sections, so
11137 that .previous works correctly. We could do something like check
11138 for an obj_section_change_hook macro, but that might be confusing
11139 as it would not be appropriate to use it in the section changing
11140 functions in read.c, since obj-elf.c intercepts those. FIXME:
11141 This should be cleaner, somehow. */
11142 obj_elf_section_change_hook ();
11143 #endif
11144
11145 mips_emit_delays (FALSE);
11146 switch (sec)
11147 {
11148 case 't':
11149 s_text (0);
11150 break;
11151 case 'd':
11152 s_data (0);
11153 break;
11154 case 'b':
11155 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11156 demand_empty_rest_of_line ();
11157 break;
11158
11159 case 'r':
11160 seg = subseg_new (RDATA_SECTION_NAME,
11161 (subsegT) get_absolute_expression ());
11162 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11163 {
11164 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11165 | SEC_READONLY | SEC_RELOC
11166 | SEC_DATA));
11167 if (strcmp (TARGET_OS, "elf") != 0)
11168 record_alignment (seg, 4);
11169 }
11170 demand_empty_rest_of_line ();
11171 break;
11172
11173 case 's':
11174 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11175 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11176 {
11177 bfd_set_section_flags (stdoutput, seg,
11178 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11179 if (strcmp (TARGET_OS, "elf") != 0)
11180 record_alignment (seg, 4);
11181 }
11182 demand_empty_rest_of_line ();
11183 break;
11184 }
11185
11186 auto_align = 1;
11187 }
11188
11189 void
11190 s_change_section (int ignore ATTRIBUTE_UNUSED)
11191 {
11192 #ifdef OBJ_ELF
11193 char *section_name;
11194 char c;
11195 char next_c = 0;
11196 int section_type;
11197 int section_flag;
11198 int section_entry_size;
11199 int section_alignment;
11200
11201 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11202 return;
11203
11204 section_name = input_line_pointer;
11205 c = get_symbol_end ();
11206 if (c)
11207 next_c = *(input_line_pointer + 1);
11208
11209 /* Do we have .section Name<,"flags">? */
11210 if (c != ',' || (c == ',' && next_c == '"'))
11211 {
11212 /* just after name is now '\0'. */
11213 *input_line_pointer = c;
11214 input_line_pointer = section_name;
11215 obj_elf_section (ignore);
11216 return;
11217 }
11218 input_line_pointer++;
11219
11220 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11221 if (c == ',')
11222 section_type = get_absolute_expression ();
11223 else
11224 section_type = 0;
11225 if (*input_line_pointer++ == ',')
11226 section_flag = get_absolute_expression ();
11227 else
11228 section_flag = 0;
11229 if (*input_line_pointer++ == ',')
11230 section_entry_size = get_absolute_expression ();
11231 else
11232 section_entry_size = 0;
11233 if (*input_line_pointer++ == ',')
11234 section_alignment = get_absolute_expression ();
11235 else
11236 section_alignment = 0;
11237
11238 section_name = xstrdup (section_name);
11239
11240 /* When using the generic form of .section (as implemented by obj-elf.c),
11241 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11242 traditionally had to fall back on the more common @progbits instead.
11243
11244 There's nothing really harmful in this, since bfd will correct
11245 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11246 means that, for backwards compatibiltiy, the special_section entries
11247 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11248
11249 Even so, we shouldn't force users of the MIPS .section syntax to
11250 incorrectly label the sections as SHT_PROGBITS. The best compromise
11251 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11252 generic type-checking code. */
11253 if (section_type == SHT_MIPS_DWARF)
11254 section_type = SHT_PROGBITS;
11255
11256 obj_elf_change_section (section_name, section_type, section_flag,
11257 section_entry_size, 0, 0, 0);
11258
11259 if (now_seg->name != section_name)
11260 free (section_name);
11261 #endif /* OBJ_ELF */
11262 }
11263
11264 void
11265 mips_enable_auto_align (void)
11266 {
11267 auto_align = 1;
11268 }
11269
11270 static void
11271 s_cons (int log_size)
11272 {
11273 symbolS *label;
11274
11275 label = insn_labels != NULL ? insn_labels->label : NULL;
11276 mips_emit_delays (FALSE);
11277 if (log_size > 0 && auto_align)
11278 mips_align (log_size, 0, label);
11279 mips_clear_insn_labels ();
11280 cons (1 << log_size);
11281 }
11282
11283 static void
11284 s_float_cons (int type)
11285 {
11286 symbolS *label;
11287
11288 label = insn_labels != NULL ? insn_labels->label : NULL;
11289
11290 mips_emit_delays (FALSE);
11291
11292 if (auto_align)
11293 {
11294 if (type == 'd')
11295 mips_align (3, 0, label);
11296 else
11297 mips_align (2, 0, label);
11298 }
11299
11300 mips_clear_insn_labels ();
11301
11302 float_cons (type);
11303 }
11304
11305 /* Handle .globl. We need to override it because on Irix 5 you are
11306 permitted to say
11307 .globl foo .text
11308 where foo is an undefined symbol, to mean that foo should be
11309 considered to be the address of a function. */
11310
11311 static void
11312 s_mips_globl (int x ATTRIBUTE_UNUSED)
11313 {
11314 char *name;
11315 int c;
11316 symbolS *symbolP;
11317 flagword flag;
11318
11319 name = input_line_pointer;
11320 c = get_symbol_end ();
11321 symbolP = symbol_find_or_make (name);
11322 *input_line_pointer = c;
11323 SKIP_WHITESPACE ();
11324
11325 /* On Irix 5, every global symbol that is not explicitly labelled as
11326 being a function is apparently labelled as being an object. */
11327 flag = BSF_OBJECT;
11328
11329 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11330 {
11331 char *secname;
11332 asection *sec;
11333
11334 secname = input_line_pointer;
11335 c = get_symbol_end ();
11336 sec = bfd_get_section_by_name (stdoutput, secname);
11337 if (sec == NULL)
11338 as_bad (_("%s: no such section"), secname);
11339 *input_line_pointer = c;
11340
11341 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11342 flag = BSF_FUNCTION;
11343 }
11344
11345 symbol_get_bfdsym (symbolP)->flags |= flag;
11346
11347 S_SET_EXTERNAL (symbolP);
11348 demand_empty_rest_of_line ();
11349 }
11350
11351 static void
11352 s_option (int x ATTRIBUTE_UNUSED)
11353 {
11354 char *opt;
11355 char c;
11356
11357 opt = input_line_pointer;
11358 c = get_symbol_end ();
11359
11360 if (*opt == 'O')
11361 {
11362 /* FIXME: What does this mean? */
11363 }
11364 else if (strncmp (opt, "pic", 3) == 0)
11365 {
11366 int i;
11367
11368 i = atoi (opt + 3);
11369 if (i == 0)
11370 mips_pic = NO_PIC;
11371 else if (i == 2)
11372 {
11373 mips_pic = SVR4_PIC;
11374 mips_abicalls = TRUE;
11375 }
11376 else
11377 as_bad (_(".option pic%d not supported"), i);
11378
11379 if (mips_pic == SVR4_PIC)
11380 {
11381 if (g_switch_seen && g_switch_value != 0)
11382 as_warn (_("-G may not be used with SVR4 PIC code"));
11383 g_switch_value = 0;
11384 bfd_set_gp_size (stdoutput, 0);
11385 }
11386 }
11387 else
11388 as_warn (_("Unrecognized option \"%s\""), opt);
11389
11390 *input_line_pointer = c;
11391 demand_empty_rest_of_line ();
11392 }
11393
11394 /* This structure is used to hold a stack of .set values. */
11395
11396 struct mips_option_stack
11397 {
11398 struct mips_option_stack *next;
11399 struct mips_set_options options;
11400 };
11401
11402 static struct mips_option_stack *mips_opts_stack;
11403
11404 /* Handle the .set pseudo-op. */
11405
11406 static void
11407 s_mipsset (int x ATTRIBUTE_UNUSED)
11408 {
11409 char *name = input_line_pointer, ch;
11410
11411 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11412 ++input_line_pointer;
11413 ch = *input_line_pointer;
11414 *input_line_pointer = '\0';
11415
11416 if (strcmp (name, "reorder") == 0)
11417 {
11418 if (mips_opts.noreorder && prev_nop_frag != NULL)
11419 {
11420 /* If we still have pending nops, we can discard them. The
11421 usual nop handling will insert any that are still
11422 needed. */
11423 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11424 * (mips_opts.mips16 ? 2 : 4));
11425 prev_nop_frag = NULL;
11426 }
11427 mips_opts.noreorder = 0;
11428 }
11429 else if (strcmp (name, "noreorder") == 0)
11430 {
11431 mips_emit_delays (TRUE);
11432 mips_opts.noreorder = 1;
11433 mips_any_noreorder = 1;
11434 }
11435 else if (strcmp (name, "at") == 0)
11436 {
11437 mips_opts.noat = 0;
11438 }
11439 else if (strcmp (name, "noat") == 0)
11440 {
11441 mips_opts.noat = 1;
11442 }
11443 else if (strcmp (name, "macro") == 0)
11444 {
11445 mips_opts.warn_about_macros = 0;
11446 }
11447 else if (strcmp (name, "nomacro") == 0)
11448 {
11449 if (mips_opts.noreorder == 0)
11450 as_bad (_("`noreorder' must be set before `nomacro'"));
11451 mips_opts.warn_about_macros = 1;
11452 }
11453 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11454 {
11455 mips_opts.nomove = 0;
11456 }
11457 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11458 {
11459 mips_opts.nomove = 1;
11460 }
11461 else if (strcmp (name, "bopt") == 0)
11462 {
11463 mips_opts.nobopt = 0;
11464 }
11465 else if (strcmp (name, "nobopt") == 0)
11466 {
11467 mips_opts.nobopt = 1;
11468 }
11469 else if (strcmp (name, "mips16") == 0
11470 || strcmp (name, "MIPS-16") == 0)
11471 mips_opts.mips16 = 1;
11472 else if (strcmp (name, "nomips16") == 0
11473 || strcmp (name, "noMIPS-16") == 0)
11474 mips_opts.mips16 = 0;
11475 else if (strcmp (name, "mips3d") == 0)
11476 mips_opts.ase_mips3d = 1;
11477 else if (strcmp (name, "nomips3d") == 0)
11478 mips_opts.ase_mips3d = 0;
11479 else if (strcmp (name, "mdmx") == 0)
11480 mips_opts.ase_mdmx = 1;
11481 else if (strcmp (name, "nomdmx") == 0)
11482 mips_opts.ase_mdmx = 0;
11483 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11484 {
11485 int reset = 0;
11486
11487 /* Permit the user to change the ISA and architecture on the fly.
11488 Needless to say, misuse can cause serious problems. */
11489 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11490 {
11491 reset = 1;
11492 mips_opts.isa = file_mips_isa;
11493 mips_opts.arch = file_mips_arch;
11494 }
11495 else if (strncmp (name, "arch=", 5) == 0)
11496 {
11497 const struct mips_cpu_info *p;
11498
11499 p = mips_parse_cpu("internal use", name + 5);
11500 if (!p)
11501 as_bad (_("unknown architecture %s"), name + 5);
11502 else
11503 {
11504 mips_opts.arch = p->cpu;
11505 mips_opts.isa = p->isa;
11506 }
11507 }
11508 else if (strncmp (name, "mips", 4) == 0)
11509 {
11510 const struct mips_cpu_info *p;
11511
11512 p = mips_parse_cpu("internal use", name);
11513 if (!p)
11514 as_bad (_("unknown ISA level %s"), name + 4);
11515 else
11516 {
11517 mips_opts.arch = p->cpu;
11518 mips_opts.isa = p->isa;
11519 }
11520 }
11521 else
11522 as_bad (_("unknown ISA or architecture %s"), name);
11523
11524 switch (mips_opts.isa)
11525 {
11526 case 0:
11527 break;
11528 case ISA_MIPS1:
11529 case ISA_MIPS2:
11530 case ISA_MIPS32:
11531 case ISA_MIPS32R2:
11532 mips_opts.gp32 = 1;
11533 mips_opts.fp32 = 1;
11534 break;
11535 case ISA_MIPS3:
11536 case ISA_MIPS4:
11537 case ISA_MIPS5:
11538 case ISA_MIPS64:
11539 case ISA_MIPS64R2:
11540 mips_opts.gp32 = 0;
11541 mips_opts.fp32 = 0;
11542 break;
11543 default:
11544 as_bad (_("unknown ISA level %s"), name + 4);
11545 break;
11546 }
11547 if (reset)
11548 {
11549 mips_opts.gp32 = file_mips_gp32;
11550 mips_opts.fp32 = file_mips_fp32;
11551 }
11552 }
11553 else if (strcmp (name, "autoextend") == 0)
11554 mips_opts.noautoextend = 0;
11555 else if (strcmp (name, "noautoextend") == 0)
11556 mips_opts.noautoextend = 1;
11557 else if (strcmp (name, "push") == 0)
11558 {
11559 struct mips_option_stack *s;
11560
11561 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11562 s->next = mips_opts_stack;
11563 s->options = mips_opts;
11564 mips_opts_stack = s;
11565 }
11566 else if (strcmp (name, "pop") == 0)
11567 {
11568 struct mips_option_stack *s;
11569
11570 s = mips_opts_stack;
11571 if (s == NULL)
11572 as_bad (_(".set pop with no .set push"));
11573 else
11574 {
11575 /* If we're changing the reorder mode we need to handle
11576 delay slots correctly. */
11577 if (s->options.noreorder && ! mips_opts.noreorder)
11578 mips_emit_delays (TRUE);
11579 else if (! s->options.noreorder && mips_opts.noreorder)
11580 {
11581 if (prev_nop_frag != NULL)
11582 {
11583 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11584 * (mips_opts.mips16 ? 2 : 4));
11585 prev_nop_frag = NULL;
11586 }
11587 }
11588
11589 mips_opts = s->options;
11590 mips_opts_stack = s->next;
11591 free (s);
11592 }
11593 }
11594 else if (strcmp (name, "sym32") == 0)
11595 mips_opts.sym32 = TRUE;
11596 else if (strcmp (name, "nosym32") == 0)
11597 mips_opts.sym32 = FALSE;
11598 else
11599 {
11600 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11601 }
11602 *input_line_pointer = ch;
11603 demand_empty_rest_of_line ();
11604 }
11605
11606 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11607 .option pic2. It means to generate SVR4 PIC calls. */
11608
11609 static void
11610 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11611 {
11612 mips_pic = SVR4_PIC;
11613 mips_abicalls = TRUE;
11614
11615 if (g_switch_seen && g_switch_value != 0)
11616 as_warn (_("-G may not be used with SVR4 PIC code"));
11617 g_switch_value = 0;
11618
11619 bfd_set_gp_size (stdoutput, 0);
11620 demand_empty_rest_of_line ();
11621 }
11622
11623 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11624 PIC code. It sets the $gp register for the function based on the
11625 function address, which is in the register named in the argument.
11626 This uses a relocation against _gp_disp, which is handled specially
11627 by the linker. The result is:
11628 lui $gp,%hi(_gp_disp)
11629 addiu $gp,$gp,%lo(_gp_disp)
11630 addu $gp,$gp,.cpload argument
11631 The .cpload argument is normally $25 == $t9.
11632
11633 The -mno-shared option changes this to:
11634 lui $gp,%hi(__gnu_local_gp)
11635 addiu $gp,$gp,%lo(__gnu_local_gp)
11636 and the argument is ignored. This saves an instruction, but the
11637 resulting code is not position independent; it uses an absolute
11638 address for __gnu_local_gp. Thus code assembled with -mno-shared
11639 can go into an ordinary executable, but not into a shared library. */
11640
11641 static void
11642 s_cpload (int ignore ATTRIBUTE_UNUSED)
11643 {
11644 expressionS ex;
11645 int reg;
11646 int in_shared;
11647
11648 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11649 .cpload is ignored. */
11650 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11651 {
11652 s_ignore (0);
11653 return;
11654 }
11655
11656 /* .cpload should be in a .set noreorder section. */
11657 if (mips_opts.noreorder == 0)
11658 as_warn (_(".cpload not in noreorder section"));
11659
11660 reg = tc_get_register (0);
11661
11662 /* If we need to produce a 64-bit address, we are better off using
11663 the default instruction sequence. */
11664 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11665
11666 ex.X_op = O_symbol;
11667 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11668 "__gnu_local_gp");
11669 ex.X_op_symbol = NULL;
11670 ex.X_add_number = 0;
11671
11672 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11673 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11674
11675 macro_start ();
11676 macro_build_lui (&ex, mips_gp_register);
11677 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11678 mips_gp_register, BFD_RELOC_LO16);
11679 if (in_shared)
11680 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11681 mips_gp_register, reg);
11682 macro_end ();
11683
11684 demand_empty_rest_of_line ();
11685 }
11686
11687 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11688 .cpsetup $reg1, offset|$reg2, label
11689
11690 If offset is given, this results in:
11691 sd $gp, offset($sp)
11692 lui $gp, %hi(%neg(%gp_rel(label)))
11693 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11694 daddu $gp, $gp, $reg1
11695
11696 If $reg2 is given, this results in:
11697 daddu $reg2, $gp, $0
11698 lui $gp, %hi(%neg(%gp_rel(label)))
11699 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11700 daddu $gp, $gp, $reg1
11701 $reg1 is normally $25 == $t9.
11702
11703 The -mno-shared option replaces the last three instructions with
11704 lui $gp,%hi(_gp)
11705 addiu $gp,$gp,%lo(_gp)
11706 */
11707
11708 static void
11709 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11710 {
11711 expressionS ex_off;
11712 expressionS ex_sym;
11713 int reg1;
11714
11715 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11716 We also need NewABI support. */
11717 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11718 {
11719 s_ignore (0);
11720 return;
11721 }
11722
11723 reg1 = tc_get_register (0);
11724 SKIP_WHITESPACE ();
11725 if (*input_line_pointer != ',')
11726 {
11727 as_bad (_("missing argument separator ',' for .cpsetup"));
11728 return;
11729 }
11730 else
11731 ++input_line_pointer;
11732 SKIP_WHITESPACE ();
11733 if (*input_line_pointer == '$')
11734 {
11735 mips_cpreturn_register = tc_get_register (0);
11736 mips_cpreturn_offset = -1;
11737 }
11738 else
11739 {
11740 mips_cpreturn_offset = get_absolute_expression ();
11741 mips_cpreturn_register = -1;
11742 }
11743 SKIP_WHITESPACE ();
11744 if (*input_line_pointer != ',')
11745 {
11746 as_bad (_("missing argument separator ',' for .cpsetup"));
11747 return;
11748 }
11749 else
11750 ++input_line_pointer;
11751 SKIP_WHITESPACE ();
11752 expression (&ex_sym);
11753
11754 macro_start ();
11755 if (mips_cpreturn_register == -1)
11756 {
11757 ex_off.X_op = O_constant;
11758 ex_off.X_add_symbol = NULL;
11759 ex_off.X_op_symbol = NULL;
11760 ex_off.X_add_number = mips_cpreturn_offset;
11761
11762 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11763 BFD_RELOC_LO16, SP);
11764 }
11765 else
11766 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11767 mips_gp_register, 0);
11768
11769 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11770 {
11771 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11772 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11773 BFD_RELOC_HI16_S);
11774
11775 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11776 mips_gp_register, -1, BFD_RELOC_GPREL16,
11777 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11778
11779 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11780 mips_gp_register, reg1);
11781 }
11782 else
11783 {
11784 expressionS ex;
11785
11786 ex.X_op = O_symbol;
11787 ex.X_add_symbol = symbol_find_or_make ("_gp");
11788 ex.X_op_symbol = NULL;
11789 ex.X_add_number = 0;
11790
11791 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11792 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11793
11794 macro_build_lui (&ex, mips_gp_register);
11795 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11796 mips_gp_register, BFD_RELOC_LO16);
11797 }
11798
11799 macro_end ();
11800
11801 demand_empty_rest_of_line ();
11802 }
11803
11804 static void
11805 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11806 {
11807 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11808 .cplocal is ignored. */
11809 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11810 {
11811 s_ignore (0);
11812 return;
11813 }
11814
11815 mips_gp_register = tc_get_register (0);
11816 demand_empty_rest_of_line ();
11817 }
11818
11819 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11820 offset from $sp. The offset is remembered, and after making a PIC
11821 call $gp is restored from that location. */
11822
11823 static void
11824 s_cprestore (int ignore ATTRIBUTE_UNUSED)
11825 {
11826 expressionS ex;
11827
11828 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11829 .cprestore is ignored. */
11830 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11831 {
11832 s_ignore (0);
11833 return;
11834 }
11835
11836 mips_cprestore_offset = get_absolute_expression ();
11837 mips_cprestore_valid = 1;
11838
11839 ex.X_op = O_constant;
11840 ex.X_add_symbol = NULL;
11841 ex.X_op_symbol = NULL;
11842 ex.X_add_number = mips_cprestore_offset;
11843
11844 macro_start ();
11845 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
11846 SP, HAVE_64BIT_ADDRESSES);
11847 macro_end ();
11848
11849 demand_empty_rest_of_line ();
11850 }
11851
11852 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11853 was given in the preceding .cpsetup, it results in:
11854 ld $gp, offset($sp)
11855
11856 If a register $reg2 was given there, it results in:
11857 daddu $gp, $reg2, $0
11858 */
11859 static void
11860 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
11861 {
11862 expressionS ex;
11863
11864 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11865 We also need NewABI support. */
11866 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11867 {
11868 s_ignore (0);
11869 return;
11870 }
11871
11872 macro_start ();
11873 if (mips_cpreturn_register == -1)
11874 {
11875 ex.X_op = O_constant;
11876 ex.X_add_symbol = NULL;
11877 ex.X_op_symbol = NULL;
11878 ex.X_add_number = mips_cpreturn_offset;
11879
11880 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
11881 }
11882 else
11883 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
11884 mips_cpreturn_register, 0);
11885 macro_end ();
11886
11887 demand_empty_rest_of_line ();
11888 }
11889
11890 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11891 code. It sets the offset to use in gp_rel relocations. */
11892
11893 static void
11894 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
11895 {
11896 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11897 We also need NewABI support. */
11898 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11899 {
11900 s_ignore (0);
11901 return;
11902 }
11903
11904 mips_gprel_offset = get_absolute_expression ();
11905
11906 demand_empty_rest_of_line ();
11907 }
11908
11909 /* Handle the .gpword pseudo-op. This is used when generating PIC
11910 code. It generates a 32 bit GP relative reloc. */
11911
11912 static void
11913 s_gpword (int ignore ATTRIBUTE_UNUSED)
11914 {
11915 symbolS *label;
11916 expressionS ex;
11917 char *p;
11918
11919 /* When not generating PIC code, this is treated as .word. */
11920 if (mips_pic != SVR4_PIC)
11921 {
11922 s_cons (2);
11923 return;
11924 }
11925
11926 label = insn_labels != NULL ? insn_labels->label : NULL;
11927 mips_emit_delays (TRUE);
11928 if (auto_align)
11929 mips_align (2, 0, label);
11930 mips_clear_insn_labels ();
11931
11932 expression (&ex);
11933
11934 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11935 {
11936 as_bad (_("Unsupported use of .gpword"));
11937 ignore_rest_of_line ();
11938 }
11939
11940 p = frag_more (4);
11941 md_number_to_chars (p, 0, 4);
11942 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
11943 BFD_RELOC_GPREL32);
11944
11945 demand_empty_rest_of_line ();
11946 }
11947
11948 static void
11949 s_gpdword (int ignore ATTRIBUTE_UNUSED)
11950 {
11951 symbolS *label;
11952 expressionS ex;
11953 char *p;
11954
11955 /* When not generating PIC code, this is treated as .dword. */
11956 if (mips_pic != SVR4_PIC)
11957 {
11958 s_cons (3);
11959 return;
11960 }
11961
11962 label = insn_labels != NULL ? insn_labels->label : NULL;
11963 mips_emit_delays (TRUE);
11964 if (auto_align)
11965 mips_align (3, 0, label);
11966 mips_clear_insn_labels ();
11967
11968 expression (&ex);
11969
11970 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11971 {
11972 as_bad (_("Unsupported use of .gpdword"));
11973 ignore_rest_of_line ();
11974 }
11975
11976 p = frag_more (8);
11977 md_number_to_chars (p, 0, 8);
11978 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
11979 BFD_RELOC_GPREL32)->fx_tcbit = 1;
11980
11981 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
11982 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
11983 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
11984
11985 demand_empty_rest_of_line ();
11986 }
11987
11988 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11989 tables in SVR4 PIC code. */
11990
11991 static void
11992 s_cpadd (int ignore ATTRIBUTE_UNUSED)
11993 {
11994 int reg;
11995
11996 /* This is ignored when not generating SVR4 PIC code. */
11997 if (mips_pic != SVR4_PIC)
11998 {
11999 s_ignore (0);
12000 return;
12001 }
12002
12003 /* Add $gp to the register named as an argument. */
12004 macro_start ();
12005 reg = tc_get_register (0);
12006 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12007 macro_end ();
12008
12009 demand_empty_rest_of_line ();
12010 }
12011
12012 /* Handle the .insn pseudo-op. This marks instruction labels in
12013 mips16 mode. This permits the linker to handle them specially,
12014 such as generating jalx instructions when needed. We also make
12015 them odd for the duration of the assembly, in order to generate the
12016 right sort of code. We will make them even in the adjust_symtab
12017 routine, while leaving them marked. This is convenient for the
12018 debugger and the disassembler. The linker knows to make them odd
12019 again. */
12020
12021 static void
12022 s_insn (int ignore ATTRIBUTE_UNUSED)
12023 {
12024 mips16_mark_labels ();
12025
12026 demand_empty_rest_of_line ();
12027 }
12028
12029 /* Handle a .stabn directive. We need these in order to mark a label
12030 as being a mips16 text label correctly. Sometimes the compiler
12031 will emit a label, followed by a .stabn, and then switch sections.
12032 If the label and .stabn are in mips16 mode, then the label is
12033 really a mips16 text label. */
12034
12035 static void
12036 s_mips_stab (int type)
12037 {
12038 if (type == 'n')
12039 mips16_mark_labels ();
12040
12041 s_stab (type);
12042 }
12043
12044 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12045 */
12046
12047 static void
12048 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12049 {
12050 char *name;
12051 int c;
12052 symbolS *symbolP;
12053 expressionS exp;
12054
12055 name = input_line_pointer;
12056 c = get_symbol_end ();
12057 symbolP = symbol_find_or_make (name);
12058 S_SET_WEAK (symbolP);
12059 *input_line_pointer = c;
12060
12061 SKIP_WHITESPACE ();
12062
12063 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12064 {
12065 if (S_IS_DEFINED (symbolP))
12066 {
12067 as_bad ("ignoring attempt to redefine symbol %s",
12068 S_GET_NAME (symbolP));
12069 ignore_rest_of_line ();
12070 return;
12071 }
12072
12073 if (*input_line_pointer == ',')
12074 {
12075 ++input_line_pointer;
12076 SKIP_WHITESPACE ();
12077 }
12078
12079 expression (&exp);
12080 if (exp.X_op != O_symbol)
12081 {
12082 as_bad ("bad .weakext directive");
12083 ignore_rest_of_line ();
12084 return;
12085 }
12086 symbol_set_value_expression (symbolP, &exp);
12087 }
12088
12089 demand_empty_rest_of_line ();
12090 }
12091
12092 /* Parse a register string into a number. Called from the ECOFF code
12093 to parse .frame. The argument is non-zero if this is the frame
12094 register, so that we can record it in mips_frame_reg. */
12095
12096 int
12097 tc_get_register (int frame)
12098 {
12099 int reg;
12100
12101 SKIP_WHITESPACE ();
12102 if (*input_line_pointer++ != '$')
12103 {
12104 as_warn (_("expected `$'"));
12105 reg = ZERO;
12106 }
12107 else if (ISDIGIT (*input_line_pointer))
12108 {
12109 reg = get_absolute_expression ();
12110 if (reg < 0 || reg >= 32)
12111 {
12112 as_warn (_("Bad register number"));
12113 reg = ZERO;
12114 }
12115 }
12116 else
12117 {
12118 if (strncmp (input_line_pointer, "ra", 2) == 0)
12119 {
12120 reg = RA;
12121 input_line_pointer += 2;
12122 }
12123 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12124 {
12125 reg = FP;
12126 input_line_pointer += 2;
12127 }
12128 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12129 {
12130 reg = SP;
12131 input_line_pointer += 2;
12132 }
12133 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12134 {
12135 reg = GP;
12136 input_line_pointer += 2;
12137 }
12138 else if (strncmp (input_line_pointer, "at", 2) == 0)
12139 {
12140 reg = AT;
12141 input_line_pointer += 2;
12142 }
12143 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12144 {
12145 reg = KT0;
12146 input_line_pointer += 3;
12147 }
12148 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12149 {
12150 reg = KT1;
12151 input_line_pointer += 3;
12152 }
12153 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12154 {
12155 reg = ZERO;
12156 input_line_pointer += 4;
12157 }
12158 else
12159 {
12160 as_warn (_("Unrecognized register name"));
12161 reg = ZERO;
12162 while (ISALNUM(*input_line_pointer))
12163 input_line_pointer++;
12164 }
12165 }
12166 if (frame)
12167 {
12168 mips_frame_reg = reg != 0 ? reg : SP;
12169 mips_frame_reg_valid = 1;
12170 mips_cprestore_valid = 0;
12171 }
12172 return reg;
12173 }
12174
12175 valueT
12176 md_section_align (asection *seg, valueT addr)
12177 {
12178 int align = bfd_get_section_alignment (stdoutput, seg);
12179
12180 #ifdef OBJ_ELF
12181 /* We don't need to align ELF sections to the full alignment.
12182 However, Irix 5 may prefer that we align them at least to a 16
12183 byte boundary. We don't bother to align the sections if we are
12184 targeted for an embedded system. */
12185 if (strcmp (TARGET_OS, "elf") == 0)
12186 return addr;
12187 if (align > 4)
12188 align = 4;
12189 #endif
12190
12191 return ((addr + (1 << align) - 1) & (-1 << align));
12192 }
12193
12194 /* Utility routine, called from above as well. If called while the
12195 input file is still being read, it's only an approximation. (For
12196 example, a symbol may later become defined which appeared to be
12197 undefined earlier.) */
12198
12199 static int
12200 nopic_need_relax (symbolS *sym, int before_relaxing)
12201 {
12202 if (sym == 0)
12203 return 0;
12204
12205 if (g_switch_value > 0)
12206 {
12207 const char *symname;
12208 int change;
12209
12210 /* Find out whether this symbol can be referenced off the $gp
12211 register. It can be if it is smaller than the -G size or if
12212 it is in the .sdata or .sbss section. Certain symbols can
12213 not be referenced off the $gp, although it appears as though
12214 they can. */
12215 symname = S_GET_NAME (sym);
12216 if (symname != (const char *) NULL
12217 && (strcmp (symname, "eprol") == 0
12218 || strcmp (symname, "etext") == 0
12219 || strcmp (symname, "_gp") == 0
12220 || strcmp (symname, "edata") == 0
12221 || strcmp (symname, "_fbss") == 0
12222 || strcmp (symname, "_fdata") == 0
12223 || strcmp (symname, "_ftext") == 0
12224 || strcmp (symname, "end") == 0
12225 || strcmp (symname, "_gp_disp") == 0))
12226 change = 1;
12227 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12228 && (0
12229 #ifndef NO_ECOFF_DEBUGGING
12230 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12231 && (symbol_get_obj (sym)->ecoff_extern_size
12232 <= g_switch_value))
12233 #endif
12234 /* We must defer this decision until after the whole
12235 file has been read, since there might be a .extern
12236 after the first use of this symbol. */
12237 || (before_relaxing
12238 #ifndef NO_ECOFF_DEBUGGING
12239 && symbol_get_obj (sym)->ecoff_extern_size == 0
12240 #endif
12241 && S_GET_VALUE (sym) == 0)
12242 || (S_GET_VALUE (sym) != 0
12243 && S_GET_VALUE (sym) <= g_switch_value)))
12244 change = 0;
12245 else
12246 {
12247 const char *segname;
12248
12249 segname = segment_name (S_GET_SEGMENT (sym));
12250 assert (strcmp (segname, ".lit8") != 0
12251 && strcmp (segname, ".lit4") != 0);
12252 change = (strcmp (segname, ".sdata") != 0
12253 && strcmp (segname, ".sbss") != 0
12254 && strncmp (segname, ".sdata.", 7) != 0
12255 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12256 }
12257 return change;
12258 }
12259 else
12260 /* We are not optimizing for the $gp register. */
12261 return 1;
12262 }
12263
12264
12265 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12266
12267 static bfd_boolean
12268 pic_need_relax (symbolS *sym, asection *segtype)
12269 {
12270 asection *symsec;
12271 bfd_boolean linkonce;
12272
12273 /* Handle the case of a symbol equated to another symbol. */
12274 while (symbol_equated_reloc_p (sym))
12275 {
12276 symbolS *n;
12277
12278 /* It's possible to get a loop here in a badly written
12279 program. */
12280 n = symbol_get_value_expression (sym)->X_add_symbol;
12281 if (n == sym)
12282 break;
12283 sym = n;
12284 }
12285
12286 symsec = S_GET_SEGMENT (sym);
12287
12288 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12289 linkonce = FALSE;
12290 if (symsec != segtype && ! S_IS_LOCAL (sym))
12291 {
12292 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12293 != 0)
12294 linkonce = TRUE;
12295
12296 /* The GNU toolchain uses an extension for ELF: a section
12297 beginning with the magic string .gnu.linkonce is a linkonce
12298 section. */
12299 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12300 sizeof ".gnu.linkonce" - 1) == 0)
12301 linkonce = TRUE;
12302 }
12303
12304 /* This must duplicate the test in adjust_reloc_syms. */
12305 return (symsec != &bfd_und_section
12306 && symsec != &bfd_abs_section
12307 && ! bfd_is_com_section (symsec)
12308 && !linkonce
12309 #ifdef OBJ_ELF
12310 /* A global or weak symbol is treated as external. */
12311 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12312 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12313 #endif
12314 );
12315 }
12316
12317
12318 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12319 extended opcode. SEC is the section the frag is in. */
12320
12321 static int
12322 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12323 {
12324 int type;
12325 register const struct mips16_immed_operand *op;
12326 offsetT val;
12327 int mintiny, maxtiny;
12328 segT symsec;
12329 fragS *sym_frag;
12330
12331 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12332 return 0;
12333 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12334 return 1;
12335
12336 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12337 op = mips16_immed_operands;
12338 while (op->type != type)
12339 {
12340 ++op;
12341 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12342 }
12343
12344 if (op->unsp)
12345 {
12346 if (type == '<' || type == '>' || type == '[' || type == ']')
12347 {
12348 mintiny = 1;
12349 maxtiny = 1 << op->nbits;
12350 }
12351 else
12352 {
12353 mintiny = 0;
12354 maxtiny = (1 << op->nbits) - 1;
12355 }
12356 }
12357 else
12358 {
12359 mintiny = - (1 << (op->nbits - 1));
12360 maxtiny = (1 << (op->nbits - 1)) - 1;
12361 }
12362
12363 sym_frag = symbol_get_frag (fragp->fr_symbol);
12364 val = S_GET_VALUE (fragp->fr_symbol);
12365 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12366
12367 if (op->pcrel)
12368 {
12369 addressT addr;
12370
12371 /* We won't have the section when we are called from
12372 mips_relax_frag. However, we will always have been called
12373 from md_estimate_size_before_relax first. If this is a
12374 branch to a different section, we mark it as such. If SEC is
12375 NULL, and the frag is not marked, then it must be a branch to
12376 the same section. */
12377 if (sec == NULL)
12378 {
12379 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12380 return 1;
12381 }
12382 else
12383 {
12384 /* Must have been called from md_estimate_size_before_relax. */
12385 if (symsec != sec)
12386 {
12387 fragp->fr_subtype =
12388 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12389
12390 /* FIXME: We should support this, and let the linker
12391 catch branches and loads that are out of range. */
12392 as_bad_where (fragp->fr_file, fragp->fr_line,
12393 _("unsupported PC relative reference to different section"));
12394
12395 return 1;
12396 }
12397 if (fragp != sym_frag && sym_frag->fr_address == 0)
12398 /* Assume non-extended on the first relaxation pass.
12399 The address we have calculated will be bogus if this is
12400 a forward branch to another frag, as the forward frag
12401 will have fr_address == 0. */
12402 return 0;
12403 }
12404
12405 /* In this case, we know for sure that the symbol fragment is in
12406 the same section. If the relax_marker of the symbol fragment
12407 differs from the relax_marker of this fragment, we have not
12408 yet adjusted the symbol fragment fr_address. We want to add
12409 in STRETCH in order to get a better estimate of the address.
12410 This particularly matters because of the shift bits. */
12411 if (stretch != 0
12412 && sym_frag->relax_marker != fragp->relax_marker)
12413 {
12414 fragS *f;
12415
12416 /* Adjust stretch for any alignment frag. Note that if have
12417 been expanding the earlier code, the symbol may be
12418 defined in what appears to be an earlier frag. FIXME:
12419 This doesn't handle the fr_subtype field, which specifies
12420 a maximum number of bytes to skip when doing an
12421 alignment. */
12422 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12423 {
12424 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12425 {
12426 if (stretch < 0)
12427 stretch = - ((- stretch)
12428 & ~ ((1 << (int) f->fr_offset) - 1));
12429 else
12430 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12431 if (stretch == 0)
12432 break;
12433 }
12434 }
12435 if (f != NULL)
12436 val += stretch;
12437 }
12438
12439 addr = fragp->fr_address + fragp->fr_fix;
12440
12441 /* The base address rules are complicated. The base address of
12442 a branch is the following instruction. The base address of a
12443 PC relative load or add is the instruction itself, but if it
12444 is in a delay slot (in which case it can not be extended) use
12445 the address of the instruction whose delay slot it is in. */
12446 if (type == 'p' || type == 'q')
12447 {
12448 addr += 2;
12449
12450 /* If we are currently assuming that this frag should be
12451 extended, then, the current address is two bytes
12452 higher. */
12453 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12454 addr += 2;
12455
12456 /* Ignore the low bit in the target, since it will be set
12457 for a text label. */
12458 if ((val & 1) != 0)
12459 --val;
12460 }
12461 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12462 addr -= 4;
12463 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12464 addr -= 2;
12465
12466 val -= addr & ~ ((1 << op->shift) - 1);
12467
12468 /* Branch offsets have an implicit 0 in the lowest bit. */
12469 if (type == 'p' || type == 'q')
12470 val /= 2;
12471
12472 /* If any of the shifted bits are set, we must use an extended
12473 opcode. If the address depends on the size of this
12474 instruction, this can lead to a loop, so we arrange to always
12475 use an extended opcode. We only check this when we are in
12476 the main relaxation loop, when SEC is NULL. */
12477 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12478 {
12479 fragp->fr_subtype =
12480 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12481 return 1;
12482 }
12483
12484 /* If we are about to mark a frag as extended because the value
12485 is precisely maxtiny + 1, then there is a chance of an
12486 infinite loop as in the following code:
12487 la $4,foo
12488 .skip 1020
12489 .align 2
12490 foo:
12491 In this case when the la is extended, foo is 0x3fc bytes
12492 away, so the la can be shrunk, but then foo is 0x400 away, so
12493 the la must be extended. To avoid this loop, we mark the
12494 frag as extended if it was small, and is about to become
12495 extended with a value of maxtiny + 1. */
12496 if (val == ((maxtiny + 1) << op->shift)
12497 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12498 && sec == NULL)
12499 {
12500 fragp->fr_subtype =
12501 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12502 return 1;
12503 }
12504 }
12505 else if (symsec != absolute_section && sec != NULL)
12506 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12507
12508 if ((val & ((1 << op->shift) - 1)) != 0
12509 || val < (mintiny << op->shift)
12510 || val > (maxtiny << op->shift))
12511 return 1;
12512 else
12513 return 0;
12514 }
12515
12516 /* Compute the length of a branch sequence, and adjust the
12517 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12518 worst-case length is computed, with UPDATE being used to indicate
12519 whether an unconditional (-1), branch-likely (+1) or regular (0)
12520 branch is to be computed. */
12521 static int
12522 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12523 {
12524 bfd_boolean toofar;
12525 int length;
12526
12527 if (fragp
12528 && S_IS_DEFINED (fragp->fr_symbol)
12529 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12530 {
12531 addressT addr;
12532 offsetT val;
12533
12534 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12535
12536 addr = fragp->fr_address + fragp->fr_fix + 4;
12537
12538 val -= addr;
12539
12540 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12541 }
12542 else if (fragp)
12543 /* If the symbol is not defined or it's in a different segment,
12544 assume the user knows what's going on and emit a short
12545 branch. */
12546 toofar = FALSE;
12547 else
12548 toofar = TRUE;
12549
12550 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12551 fragp->fr_subtype
12552 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12553 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12554 RELAX_BRANCH_LINK (fragp->fr_subtype),
12555 toofar);
12556
12557 length = 4;
12558 if (toofar)
12559 {
12560 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12561 length += 8;
12562
12563 if (mips_pic != NO_PIC)
12564 {
12565 /* Additional space for PIC loading of target address. */
12566 length += 8;
12567 if (mips_opts.isa == ISA_MIPS1)
12568 /* Additional space for $at-stabilizing nop. */
12569 length += 4;
12570 }
12571
12572 /* If branch is conditional. */
12573 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12574 length += 8;
12575 }
12576
12577 return length;
12578 }
12579
12580 /* Estimate the size of a frag before relaxing. Unless this is the
12581 mips16, we are not really relaxing here, and the final size is
12582 encoded in the subtype information. For the mips16, we have to
12583 decide whether we are using an extended opcode or not. */
12584
12585 int
12586 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12587 {
12588 int change;
12589
12590 if (RELAX_BRANCH_P (fragp->fr_subtype))
12591 {
12592
12593 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12594
12595 return fragp->fr_var;
12596 }
12597
12598 if (RELAX_MIPS16_P (fragp->fr_subtype))
12599 /* We don't want to modify the EXTENDED bit here; it might get us
12600 into infinite loops. We change it only in mips_relax_frag(). */
12601 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12602
12603 if (mips_pic == NO_PIC)
12604 change = nopic_need_relax (fragp->fr_symbol, 0);
12605 else if (mips_pic == SVR4_PIC)
12606 change = pic_need_relax (fragp->fr_symbol, segtype);
12607 else
12608 abort ();
12609
12610 if (change)
12611 {
12612 fragp->fr_subtype |= RELAX_USE_SECOND;
12613 return -RELAX_FIRST (fragp->fr_subtype);
12614 }
12615 else
12616 return -RELAX_SECOND (fragp->fr_subtype);
12617 }
12618
12619 /* This is called to see whether a reloc against a defined symbol
12620 should be converted into a reloc against a section. */
12621
12622 int
12623 mips_fix_adjustable (fixS *fixp)
12624 {
12625 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12626 about the format of the offset in the .o file. */
12627 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12628 return 0;
12629
12630 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12631 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12632 return 0;
12633
12634 if (fixp->fx_addsy == NULL)
12635 return 1;
12636
12637 /* If symbol SYM is in a mergeable section, relocations of the form
12638 SYM + 0 can usually be made section-relative. The mergeable data
12639 is then identified by the section offset rather than by the symbol.
12640
12641 However, if we're generating REL LO16 relocations, the offset is split
12642 between the LO16 and parterning high part relocation. The linker will
12643 need to recalculate the complete offset in order to correctly identify
12644 the merge data.
12645
12646 The linker has traditionally not looked for the parterning high part
12647 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12648 placed anywhere. Rather than break backwards compatibility by changing
12649 this, it seems better not to force the issue, and instead keep the
12650 original symbol. This will work with either linker behavior. */
12651 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12652 && HAVE_IN_PLACE_ADDENDS
12653 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12654 return 0;
12655
12656 #ifdef OBJ_ELF
12657 /* Don't adjust relocations against mips16 symbols, so that the linker
12658 can find them if it needs to set up a stub. */
12659 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12660 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12661 && fixp->fx_subsy == NULL)
12662 return 0;
12663 #endif
12664
12665 return 1;
12666 }
12667
12668 /* Translate internal representation of relocation info to BFD target
12669 format. */
12670
12671 arelent **
12672 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12673 {
12674 static arelent *retval[4];
12675 arelent *reloc;
12676 bfd_reloc_code_real_type code;
12677
12678 memset (retval, 0, sizeof(retval));
12679 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12680 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12681 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12682 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12683
12684 assert (! fixp->fx_pcrel);
12685 reloc->addend = fixp->fx_addnumber;
12686
12687 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12688 entry to be used in the relocation's section offset. */
12689 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12690 {
12691 reloc->address = reloc->addend;
12692 reloc->addend = 0;
12693 }
12694
12695 code = fixp->fx_r_type;
12696
12697 /* To support a PC relative reloc, we used a Cygnus extension.
12698 We check for that here to make sure that we don't let such a
12699 reloc escape normally. (FIXME: This was formerly used by
12700 embedded-PIC support, but is now used by branch handling in
12701 general. That probably should be fixed.) */
12702 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12703 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12704 && code == BFD_RELOC_16_PCREL_S2)
12705 reloc->howto = NULL;
12706 else
12707 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12708
12709 if (reloc->howto == NULL)
12710 {
12711 as_bad_where (fixp->fx_file, fixp->fx_line,
12712 _("Can not represent %s relocation in this object file format"),
12713 bfd_get_reloc_code_name (code));
12714 retval[0] = NULL;
12715 }
12716
12717 return retval;
12718 }
12719
12720 /* Relax a machine dependent frag. This returns the amount by which
12721 the current size of the frag should change. */
12722
12723 int
12724 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12725 {
12726 if (RELAX_BRANCH_P (fragp->fr_subtype))
12727 {
12728 offsetT old_var = fragp->fr_var;
12729
12730 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12731
12732 return fragp->fr_var - old_var;
12733 }
12734
12735 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12736 return 0;
12737
12738 if (mips16_extended_frag (fragp, NULL, stretch))
12739 {
12740 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12741 return 0;
12742 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12743 return 2;
12744 }
12745 else
12746 {
12747 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12748 return 0;
12749 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12750 return -2;
12751 }
12752
12753 return 0;
12754 }
12755
12756 /* Convert a machine dependent frag. */
12757
12758 void
12759 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12760 {
12761 if (RELAX_BRANCH_P (fragp->fr_subtype))
12762 {
12763 bfd_byte *buf;
12764 unsigned long insn;
12765 expressionS exp;
12766 fixS *fixp;
12767
12768 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12769
12770 if (target_big_endian)
12771 insn = bfd_getb32 (buf);
12772 else
12773 insn = bfd_getl32 (buf);
12774
12775 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12776 {
12777 /* We generate a fixup instead of applying it right now
12778 because, if there are linker relaxations, we're going to
12779 need the relocations. */
12780 exp.X_op = O_symbol;
12781 exp.X_add_symbol = fragp->fr_symbol;
12782 exp.X_add_number = fragp->fr_offset;
12783
12784 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12785 4, &exp, 1,
12786 BFD_RELOC_16_PCREL_S2);
12787 fixp->fx_file = fragp->fr_file;
12788 fixp->fx_line = fragp->fr_line;
12789
12790 md_number_to_chars ((char *) buf, insn, 4);
12791 buf += 4;
12792 }
12793 else
12794 {
12795 int i;
12796
12797 as_warn_where (fragp->fr_file, fragp->fr_line,
12798 _("relaxed out-of-range branch into a jump"));
12799
12800 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12801 goto uncond;
12802
12803 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12804 {
12805 /* Reverse the branch. */
12806 switch ((insn >> 28) & 0xf)
12807 {
12808 case 4:
12809 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12810 have the condition reversed by tweaking a single
12811 bit, and their opcodes all have 0x4???????. */
12812 assert ((insn & 0xf1000000) == 0x41000000);
12813 insn ^= 0x00010000;
12814 break;
12815
12816 case 0:
12817 /* bltz 0x04000000 bgez 0x04010000
12818 bltzal 0x04100000 bgezal 0x04110000 */
12819 assert ((insn & 0xfc0e0000) == 0x04000000);
12820 insn ^= 0x00010000;
12821 break;
12822
12823 case 1:
12824 /* beq 0x10000000 bne 0x14000000
12825 blez 0x18000000 bgtz 0x1c000000 */
12826 insn ^= 0x04000000;
12827 break;
12828
12829 default:
12830 abort ();
12831 }
12832 }
12833
12834 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12835 {
12836 /* Clear the and-link bit. */
12837 assert ((insn & 0xfc1c0000) == 0x04100000);
12838
12839 /* bltzal 0x04100000 bgezal 0x04110000
12840 bltzall 0x04120000 bgezall 0x04130000 */
12841 insn &= ~0x00100000;
12842 }
12843
12844 /* Branch over the branch (if the branch was likely) or the
12845 full jump (not likely case). Compute the offset from the
12846 current instruction to branch to. */
12847 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12848 i = 16;
12849 else
12850 {
12851 /* How many bytes in instructions we've already emitted? */
12852 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12853 /* How many bytes in instructions from here to the end? */
12854 i = fragp->fr_var - i;
12855 }
12856 /* Convert to instruction count. */
12857 i >>= 2;
12858 /* Branch counts from the next instruction. */
12859 i--;
12860 insn |= i;
12861 /* Branch over the jump. */
12862 md_number_to_chars ((char *) buf, insn, 4);
12863 buf += 4;
12864
12865 /* Nop */
12866 md_number_to_chars ((char *) buf, 0, 4);
12867 buf += 4;
12868
12869 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12870 {
12871 /* beql $0, $0, 2f */
12872 insn = 0x50000000;
12873 /* Compute the PC offset from the current instruction to
12874 the end of the variable frag. */
12875 /* How many bytes in instructions we've already emitted? */
12876 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12877 /* How many bytes in instructions from here to the end? */
12878 i = fragp->fr_var - i;
12879 /* Convert to instruction count. */
12880 i >>= 2;
12881 /* Don't decrement i, because we want to branch over the
12882 delay slot. */
12883
12884 insn |= i;
12885 md_number_to_chars ((char *) buf, insn, 4);
12886 buf += 4;
12887
12888 md_number_to_chars ((char *) buf, 0, 4);
12889 buf += 4;
12890 }
12891
12892 uncond:
12893 if (mips_pic == NO_PIC)
12894 {
12895 /* j or jal. */
12896 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
12897 ? 0x0c000000 : 0x08000000);
12898 exp.X_op = O_symbol;
12899 exp.X_add_symbol = fragp->fr_symbol;
12900 exp.X_add_number = fragp->fr_offset;
12901
12902 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12903 4, &exp, 0, BFD_RELOC_MIPS_JMP);
12904 fixp->fx_file = fragp->fr_file;
12905 fixp->fx_line = fragp->fr_line;
12906
12907 md_number_to_chars ((char *) buf, insn, 4);
12908 buf += 4;
12909 }
12910 else
12911 {
12912 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
12913 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
12914 exp.X_op = O_symbol;
12915 exp.X_add_symbol = fragp->fr_symbol;
12916 exp.X_add_number = fragp->fr_offset;
12917
12918 if (fragp->fr_offset)
12919 {
12920 exp.X_add_symbol = make_expr_symbol (&exp);
12921 exp.X_add_number = 0;
12922 }
12923
12924 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12925 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
12926 fixp->fx_file = fragp->fr_file;
12927 fixp->fx_line = fragp->fr_line;
12928
12929 md_number_to_chars ((char *) buf, insn, 4);
12930 buf += 4;
12931
12932 if (mips_opts.isa == ISA_MIPS1)
12933 {
12934 /* nop */
12935 md_number_to_chars ((char *) buf, 0, 4);
12936 buf += 4;
12937 }
12938
12939 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
12940 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
12941
12942 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12943 4, &exp, 0, BFD_RELOC_LO16);
12944 fixp->fx_file = fragp->fr_file;
12945 fixp->fx_line = fragp->fr_line;
12946
12947 md_number_to_chars ((char *) buf, insn, 4);
12948 buf += 4;
12949
12950 /* j(al)r $at. */
12951 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12952 insn = 0x0020f809;
12953 else
12954 insn = 0x00200008;
12955
12956 md_number_to_chars ((char *) buf, insn, 4);
12957 buf += 4;
12958 }
12959 }
12960
12961 assert (buf == (bfd_byte *)fragp->fr_literal
12962 + fragp->fr_fix + fragp->fr_var);
12963
12964 fragp->fr_fix += fragp->fr_var;
12965
12966 return;
12967 }
12968
12969 if (RELAX_MIPS16_P (fragp->fr_subtype))
12970 {
12971 int type;
12972 register const struct mips16_immed_operand *op;
12973 bfd_boolean small, ext;
12974 offsetT val;
12975 bfd_byte *buf;
12976 unsigned long insn;
12977 bfd_boolean use_extend;
12978 unsigned short extend;
12979
12980 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12981 op = mips16_immed_operands;
12982 while (op->type != type)
12983 ++op;
12984
12985 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12986 {
12987 small = FALSE;
12988 ext = TRUE;
12989 }
12990 else
12991 {
12992 small = TRUE;
12993 ext = FALSE;
12994 }
12995
12996 resolve_symbol_value (fragp->fr_symbol);
12997 val = S_GET_VALUE (fragp->fr_symbol);
12998 if (op->pcrel)
12999 {
13000 addressT addr;
13001
13002 addr = fragp->fr_address + fragp->fr_fix;
13003
13004 /* The rules for the base address of a PC relative reloc are
13005 complicated; see mips16_extended_frag. */
13006 if (type == 'p' || type == 'q')
13007 {
13008 addr += 2;
13009 if (ext)
13010 addr += 2;
13011 /* Ignore the low bit in the target, since it will be
13012 set for a text label. */
13013 if ((val & 1) != 0)
13014 --val;
13015 }
13016 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13017 addr -= 4;
13018 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13019 addr -= 2;
13020
13021 addr &= ~ (addressT) ((1 << op->shift) - 1);
13022 val -= addr;
13023
13024 /* Make sure the section winds up with the alignment we have
13025 assumed. */
13026 if (op->shift > 0)
13027 record_alignment (asec, op->shift);
13028 }
13029
13030 if (ext
13031 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13032 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13033 as_warn_where (fragp->fr_file, fragp->fr_line,
13034 _("extended instruction in delay slot"));
13035
13036 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13037
13038 if (target_big_endian)
13039 insn = bfd_getb16 (buf);
13040 else
13041 insn = bfd_getl16 (buf);
13042
13043 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13044 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13045 small, ext, &insn, &use_extend, &extend);
13046
13047 if (use_extend)
13048 {
13049 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13050 fragp->fr_fix += 2;
13051 buf += 2;
13052 }
13053
13054 md_number_to_chars ((char *) buf, insn, 2);
13055 fragp->fr_fix += 2;
13056 buf += 2;
13057 }
13058 else
13059 {
13060 int first, second;
13061 fixS *fixp;
13062
13063 first = RELAX_FIRST (fragp->fr_subtype);
13064 second = RELAX_SECOND (fragp->fr_subtype);
13065 fixp = (fixS *) fragp->fr_opcode;
13066
13067 /* Possibly emit a warning if we've chosen the longer option. */
13068 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13069 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13070 {
13071 const char *msg = macro_warning (fragp->fr_subtype);
13072 if (msg != 0)
13073 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13074 }
13075
13076 /* Go through all the fixups for the first sequence. Disable them
13077 (by marking them as done) if we're going to use the second
13078 sequence instead. */
13079 while (fixp
13080 && fixp->fx_frag == fragp
13081 && fixp->fx_where < fragp->fr_fix - second)
13082 {
13083 if (fragp->fr_subtype & RELAX_USE_SECOND)
13084 fixp->fx_done = 1;
13085 fixp = fixp->fx_next;
13086 }
13087
13088 /* Go through the fixups for the second sequence. Disable them if
13089 we're going to use the first sequence, otherwise adjust their
13090 addresses to account for the relaxation. */
13091 while (fixp && fixp->fx_frag == fragp)
13092 {
13093 if (fragp->fr_subtype & RELAX_USE_SECOND)
13094 fixp->fx_where -= first;
13095 else
13096 fixp->fx_done = 1;
13097 fixp = fixp->fx_next;
13098 }
13099
13100 /* Now modify the frag contents. */
13101 if (fragp->fr_subtype & RELAX_USE_SECOND)
13102 {
13103 char *start;
13104
13105 start = fragp->fr_literal + fragp->fr_fix - first - second;
13106 memmove (start, start + first, second);
13107 fragp->fr_fix -= first;
13108 }
13109 else
13110 fragp->fr_fix -= second;
13111 }
13112 }
13113
13114 #ifdef OBJ_ELF
13115
13116 /* This function is called after the relocs have been generated.
13117 We've been storing mips16 text labels as odd. Here we convert them
13118 back to even for the convenience of the debugger. */
13119
13120 void
13121 mips_frob_file_after_relocs (void)
13122 {
13123 asymbol **syms;
13124 unsigned int count, i;
13125
13126 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13127 return;
13128
13129 syms = bfd_get_outsymbols (stdoutput);
13130 count = bfd_get_symcount (stdoutput);
13131 for (i = 0; i < count; i++, syms++)
13132 {
13133 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13134 && ((*syms)->value & 1) != 0)
13135 {
13136 (*syms)->value &= ~1;
13137 /* If the symbol has an odd size, it was probably computed
13138 incorrectly, so adjust that as well. */
13139 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13140 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13141 }
13142 }
13143 }
13144
13145 #endif
13146
13147 /* This function is called whenever a label is defined. It is used
13148 when handling branch delays; if a branch has a label, we assume we
13149 can not move it. */
13150
13151 void
13152 mips_define_label (symbolS *sym)
13153 {
13154 struct insn_label_list *l;
13155
13156 if (free_insn_labels == NULL)
13157 l = (struct insn_label_list *) xmalloc (sizeof *l);
13158 else
13159 {
13160 l = free_insn_labels;
13161 free_insn_labels = l->next;
13162 }
13163
13164 l->label = sym;
13165 l->next = insn_labels;
13166 insn_labels = l;
13167 }
13168 \f
13169 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13170
13171 /* Some special processing for a MIPS ELF file. */
13172
13173 void
13174 mips_elf_final_processing (void)
13175 {
13176 /* Write out the register information. */
13177 if (mips_abi != N64_ABI)
13178 {
13179 Elf32_RegInfo s;
13180
13181 s.ri_gprmask = mips_gprmask;
13182 s.ri_cprmask[0] = mips_cprmask[0];
13183 s.ri_cprmask[1] = mips_cprmask[1];
13184 s.ri_cprmask[2] = mips_cprmask[2];
13185 s.ri_cprmask[3] = mips_cprmask[3];
13186 /* The gp_value field is set by the MIPS ELF backend. */
13187
13188 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13189 ((Elf32_External_RegInfo *)
13190 mips_regmask_frag));
13191 }
13192 else
13193 {
13194 Elf64_Internal_RegInfo s;
13195
13196 s.ri_gprmask = mips_gprmask;
13197 s.ri_pad = 0;
13198 s.ri_cprmask[0] = mips_cprmask[0];
13199 s.ri_cprmask[1] = mips_cprmask[1];
13200 s.ri_cprmask[2] = mips_cprmask[2];
13201 s.ri_cprmask[3] = mips_cprmask[3];
13202 /* The gp_value field is set by the MIPS ELF backend. */
13203
13204 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13205 ((Elf64_External_RegInfo *)
13206 mips_regmask_frag));
13207 }
13208
13209 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13210 sort of BFD interface for this. */
13211 if (mips_any_noreorder)
13212 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13213 if (mips_pic != NO_PIC)
13214 {
13215 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13216 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13217 }
13218 if (mips_abicalls)
13219 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13220
13221 /* Set MIPS ELF flags for ASEs. */
13222 if (file_ase_mips16)
13223 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13224 #if 0 /* XXX FIXME */
13225 if (file_ase_mips3d)
13226 elf_elfheader (stdoutput)->e_flags |= ???;
13227 #endif
13228 if (file_ase_mdmx)
13229 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13230
13231 /* Set the MIPS ELF ABI flags. */
13232 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13233 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13234 else if (mips_abi == O64_ABI)
13235 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13236 else if (mips_abi == EABI_ABI)
13237 {
13238 if (!file_mips_gp32)
13239 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13240 else
13241 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13242 }
13243 else if (mips_abi == N32_ABI)
13244 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13245
13246 /* Nothing to do for N64_ABI. */
13247
13248 if (mips_32bitmode)
13249 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13250 }
13251
13252 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13253 \f
13254 typedef struct proc {
13255 symbolS *func_sym;
13256 symbolS *func_end_sym;
13257 unsigned long reg_mask;
13258 unsigned long reg_offset;
13259 unsigned long fpreg_mask;
13260 unsigned long fpreg_offset;
13261 unsigned long frame_offset;
13262 unsigned long frame_reg;
13263 unsigned long pc_reg;
13264 } procS;
13265
13266 static procS cur_proc;
13267 static procS *cur_proc_ptr;
13268 static int numprocs;
13269
13270 /* Fill in an rs_align_code fragment. */
13271
13272 void
13273 mips_handle_align (fragS *fragp)
13274 {
13275 if (fragp->fr_type != rs_align_code)
13276 return;
13277
13278 if (mips_opts.mips16)
13279 {
13280 static const unsigned char be_nop[] = { 0x65, 0x00 };
13281 static const unsigned char le_nop[] = { 0x00, 0x65 };
13282
13283 int bytes;
13284 char *p;
13285
13286 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13287 p = fragp->fr_literal + fragp->fr_fix;
13288
13289 if (bytes & 1)
13290 {
13291 *p++ = 0;
13292 fragp->fr_fix++;
13293 }
13294
13295 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13296 fragp->fr_var = 2;
13297 }
13298
13299 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13300 }
13301
13302 static void
13303 md_obj_begin (void)
13304 {
13305 }
13306
13307 static void
13308 md_obj_end (void)
13309 {
13310 /* check for premature end, nesting errors, etc */
13311 if (cur_proc_ptr)
13312 as_warn (_("missing .end at end of assembly"));
13313 }
13314
13315 static long
13316 get_number (void)
13317 {
13318 int negative = 0;
13319 long val = 0;
13320
13321 if (*input_line_pointer == '-')
13322 {
13323 ++input_line_pointer;
13324 negative = 1;
13325 }
13326 if (!ISDIGIT (*input_line_pointer))
13327 as_bad (_("expected simple number"));
13328 if (input_line_pointer[0] == '0')
13329 {
13330 if (input_line_pointer[1] == 'x')
13331 {
13332 input_line_pointer += 2;
13333 while (ISXDIGIT (*input_line_pointer))
13334 {
13335 val <<= 4;
13336 val |= hex_value (*input_line_pointer++);
13337 }
13338 return negative ? -val : val;
13339 }
13340 else
13341 {
13342 ++input_line_pointer;
13343 while (ISDIGIT (*input_line_pointer))
13344 {
13345 val <<= 3;
13346 val |= *input_line_pointer++ - '0';
13347 }
13348 return negative ? -val : val;
13349 }
13350 }
13351 if (!ISDIGIT (*input_line_pointer))
13352 {
13353 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13354 *input_line_pointer, *input_line_pointer);
13355 as_warn (_("invalid number"));
13356 return -1;
13357 }
13358 while (ISDIGIT (*input_line_pointer))
13359 {
13360 val *= 10;
13361 val += *input_line_pointer++ - '0';
13362 }
13363 return negative ? -val : val;
13364 }
13365
13366 /* The .file directive; just like the usual .file directive, but there
13367 is an initial number which is the ECOFF file index. In the non-ECOFF
13368 case .file implies DWARF-2. */
13369
13370 static void
13371 s_mips_file (int x ATTRIBUTE_UNUSED)
13372 {
13373 static int first_file_directive = 0;
13374
13375 if (ECOFF_DEBUGGING)
13376 {
13377 get_number ();
13378 s_app_file (0);
13379 }
13380 else
13381 {
13382 char *filename;
13383
13384 filename = dwarf2_directive_file (0);
13385
13386 /* Versions of GCC up to 3.1 start files with a ".file"
13387 directive even for stabs output. Make sure that this
13388 ".file" is handled. Note that you need a version of GCC
13389 after 3.1 in order to support DWARF-2 on MIPS. */
13390 if (filename != NULL && ! first_file_directive)
13391 {
13392 (void) new_logical_line (filename, -1);
13393 s_app_file_string (filename, 0);
13394 }
13395 first_file_directive = 1;
13396 }
13397 }
13398
13399 /* The .loc directive, implying DWARF-2. */
13400
13401 static void
13402 s_mips_loc (int x ATTRIBUTE_UNUSED)
13403 {
13404 if (!ECOFF_DEBUGGING)
13405 dwarf2_directive_loc (0);
13406 }
13407
13408 /* The .end directive. */
13409
13410 static void
13411 s_mips_end (int x ATTRIBUTE_UNUSED)
13412 {
13413 symbolS *p;
13414
13415 /* Following functions need their own .frame and .cprestore directives. */
13416 mips_frame_reg_valid = 0;
13417 mips_cprestore_valid = 0;
13418
13419 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13420 {
13421 p = get_symbol ();
13422 demand_empty_rest_of_line ();
13423 }
13424 else
13425 p = NULL;
13426
13427 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13428 as_warn (_(".end not in text section"));
13429
13430 if (!cur_proc_ptr)
13431 {
13432 as_warn (_(".end directive without a preceding .ent directive."));
13433 demand_empty_rest_of_line ();
13434 return;
13435 }
13436
13437 if (p != NULL)
13438 {
13439 assert (S_GET_NAME (p));
13440 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13441 as_warn (_(".end symbol does not match .ent symbol."));
13442
13443 if (debug_type == DEBUG_STABS)
13444 stabs_generate_asm_endfunc (S_GET_NAME (p),
13445 S_GET_NAME (p));
13446 }
13447 else
13448 as_warn (_(".end directive missing or unknown symbol"));
13449
13450 #ifdef OBJ_ELF
13451 /* Create an expression to calculate the size of the function. */
13452 if (p && cur_proc_ptr)
13453 {
13454 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13455 expressionS *exp = xmalloc (sizeof (expressionS));
13456
13457 obj->size = exp;
13458 exp->X_op = O_subtract;
13459 exp->X_add_symbol = symbol_temp_new_now ();
13460 exp->X_op_symbol = p;
13461 exp->X_add_number = 0;
13462
13463 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13464 }
13465
13466 /* Generate a .pdr section. */
13467 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13468 && mips_flag_pdr)
13469 {
13470 segT saved_seg = now_seg;
13471 subsegT saved_subseg = now_subseg;
13472 valueT dot;
13473 expressionS exp;
13474 char *fragp;
13475
13476 dot = frag_now_fix ();
13477
13478 #ifdef md_flush_pending_output
13479 md_flush_pending_output ();
13480 #endif
13481
13482 assert (pdr_seg);
13483 subseg_set (pdr_seg, 0);
13484
13485 /* Write the symbol. */
13486 exp.X_op = O_symbol;
13487 exp.X_add_symbol = p;
13488 exp.X_add_number = 0;
13489 emit_expr (&exp, 4);
13490
13491 fragp = frag_more (7 * 4);
13492
13493 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13494 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13495 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13496 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13497 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13498 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13499 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13500
13501 subseg_set (saved_seg, saved_subseg);
13502 }
13503 #endif /* OBJ_ELF */
13504
13505 cur_proc_ptr = NULL;
13506 }
13507
13508 /* The .aent and .ent directives. */
13509
13510 static void
13511 s_mips_ent (int aent)
13512 {
13513 symbolS *symbolP;
13514
13515 symbolP = get_symbol ();
13516 if (*input_line_pointer == ',')
13517 ++input_line_pointer;
13518 SKIP_WHITESPACE ();
13519 if (ISDIGIT (*input_line_pointer)
13520 || *input_line_pointer == '-')
13521 get_number ();
13522
13523 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13524 as_warn (_(".ent or .aent not in text section."));
13525
13526 if (!aent && cur_proc_ptr)
13527 as_warn (_("missing .end"));
13528
13529 if (!aent)
13530 {
13531 /* This function needs its own .frame and .cprestore directives. */
13532 mips_frame_reg_valid = 0;
13533 mips_cprestore_valid = 0;
13534
13535 cur_proc_ptr = &cur_proc;
13536 memset (cur_proc_ptr, '\0', sizeof (procS));
13537
13538 cur_proc_ptr->func_sym = symbolP;
13539
13540 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13541
13542 ++numprocs;
13543
13544 if (debug_type == DEBUG_STABS)
13545 stabs_generate_asm_func (S_GET_NAME (symbolP),
13546 S_GET_NAME (symbolP));
13547 }
13548
13549 demand_empty_rest_of_line ();
13550 }
13551
13552 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13553 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13554 s_mips_frame is used so that we can set the PDR information correctly.
13555 We can't use the ecoff routines because they make reference to the ecoff
13556 symbol table (in the mdebug section). */
13557
13558 static void
13559 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13560 {
13561 #ifdef OBJ_ELF
13562 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13563 {
13564 long val;
13565
13566 if (cur_proc_ptr == (procS *) NULL)
13567 {
13568 as_warn (_(".frame outside of .ent"));
13569 demand_empty_rest_of_line ();
13570 return;
13571 }
13572
13573 cur_proc_ptr->frame_reg = tc_get_register (1);
13574
13575 SKIP_WHITESPACE ();
13576 if (*input_line_pointer++ != ','
13577 || get_absolute_expression_and_terminator (&val) != ',')
13578 {
13579 as_warn (_("Bad .frame directive"));
13580 --input_line_pointer;
13581 demand_empty_rest_of_line ();
13582 return;
13583 }
13584
13585 cur_proc_ptr->frame_offset = val;
13586 cur_proc_ptr->pc_reg = tc_get_register (0);
13587
13588 demand_empty_rest_of_line ();
13589 }
13590 else
13591 #endif /* OBJ_ELF */
13592 s_ignore (ignore);
13593 }
13594
13595 /* The .fmask and .mask directives. If the mdebug section is present
13596 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13597 embedded targets, s_mips_mask is used so that we can set the PDR
13598 information correctly. We can't use the ecoff routines because they
13599 make reference to the ecoff symbol table (in the mdebug section). */
13600
13601 static void
13602 s_mips_mask (int reg_type)
13603 {
13604 #ifdef OBJ_ELF
13605 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13606 {
13607 long mask, off;
13608
13609 if (cur_proc_ptr == (procS *) NULL)
13610 {
13611 as_warn (_(".mask/.fmask outside of .ent"));
13612 demand_empty_rest_of_line ();
13613 return;
13614 }
13615
13616 if (get_absolute_expression_and_terminator (&mask) != ',')
13617 {
13618 as_warn (_("Bad .mask/.fmask directive"));
13619 --input_line_pointer;
13620 demand_empty_rest_of_line ();
13621 return;
13622 }
13623
13624 off = get_absolute_expression ();
13625
13626 if (reg_type == 'F')
13627 {
13628 cur_proc_ptr->fpreg_mask = mask;
13629 cur_proc_ptr->fpreg_offset = off;
13630 }
13631 else
13632 {
13633 cur_proc_ptr->reg_mask = mask;
13634 cur_proc_ptr->reg_offset = off;
13635 }
13636
13637 demand_empty_rest_of_line ();
13638 }
13639 else
13640 #endif /* OBJ_ELF */
13641 s_ignore (reg_type);
13642 }
13643
13644 /* A table describing all the processors gas knows about. Names are
13645 matched in the order listed.
13646
13647 To ease comparison, please keep this table in the same order as
13648 gcc's mips_cpu_info_table[]. */
13649 static const struct mips_cpu_info mips_cpu_info_table[] =
13650 {
13651 /* Entries for generic ISAs */
13652 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13653 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13654 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13655 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13656 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13657 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13658 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13659 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13660 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13661
13662 /* MIPS I */
13663 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13664 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13665 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13666
13667 /* MIPS II */
13668 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13669
13670 /* MIPS III */
13671 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13672 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13673 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13674 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13675 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13676 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13677 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13678 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13679 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13680 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13681 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13682 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13683
13684 /* MIPS IV */
13685 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13686 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13687 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13688 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13689 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13690 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13691 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13692 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13693 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13694 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13695 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13696 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13697 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13698
13699 /* MIPS 32 */
13700 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13701 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13702 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13703
13704 /* MIPS 64 */
13705 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13706 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13707
13708 /* Broadcom SB-1 CPU core */
13709 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13710
13711 /* End marker */
13712 { NULL, 0, 0, 0 }
13713 };
13714
13715
13716 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13717 with a final "000" replaced by "k". Ignore case.
13718
13719 Note: this function is shared between GCC and GAS. */
13720
13721 static bfd_boolean
13722 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13723 {
13724 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13725 given++, canonical++;
13726
13727 return ((*given == 0 && *canonical == 0)
13728 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13729 }
13730
13731
13732 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13733 CPU name. We've traditionally allowed a lot of variation here.
13734
13735 Note: this function is shared between GCC and GAS. */
13736
13737 static bfd_boolean
13738 mips_matching_cpu_name_p (const char *canonical, const char *given)
13739 {
13740 /* First see if the name matches exactly, or with a final "000"
13741 turned into "k". */
13742 if (mips_strict_matching_cpu_name_p (canonical, given))
13743 return TRUE;
13744
13745 /* If not, try comparing based on numerical designation alone.
13746 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13747 if (TOLOWER (*given) == 'r')
13748 given++;
13749 if (!ISDIGIT (*given))
13750 return FALSE;
13751
13752 /* Skip over some well-known prefixes in the canonical name,
13753 hoping to find a number there too. */
13754 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13755 canonical += 2;
13756 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13757 canonical += 2;
13758 else if (TOLOWER (canonical[0]) == 'r')
13759 canonical += 1;
13760
13761 return mips_strict_matching_cpu_name_p (canonical, given);
13762 }
13763
13764
13765 /* Parse an option that takes the name of a processor as its argument.
13766 OPTION is the name of the option and CPU_STRING is the argument.
13767 Return the corresponding processor enumeration if the CPU_STRING is
13768 recognized, otherwise report an error and return null.
13769
13770 A similar function exists in GCC. */
13771
13772 static const struct mips_cpu_info *
13773 mips_parse_cpu (const char *option, const char *cpu_string)
13774 {
13775 const struct mips_cpu_info *p;
13776
13777 /* 'from-abi' selects the most compatible architecture for the given
13778 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13779 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13780 version. Look first at the -mgp options, if given, otherwise base
13781 the choice on MIPS_DEFAULT_64BIT.
13782
13783 Treat NO_ABI like the EABIs. One reason to do this is that the
13784 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13785 architecture. This code picks MIPS I for 'mips' and MIPS III for
13786 'mips64', just as we did in the days before 'from-abi'. */
13787 if (strcasecmp (cpu_string, "from-abi") == 0)
13788 {
13789 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13790 return mips_cpu_info_from_isa (ISA_MIPS1);
13791
13792 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13793 return mips_cpu_info_from_isa (ISA_MIPS3);
13794
13795 if (file_mips_gp32 >= 0)
13796 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13797
13798 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13799 ? ISA_MIPS3
13800 : ISA_MIPS1);
13801 }
13802
13803 /* 'default' has traditionally been a no-op. Probably not very useful. */
13804 if (strcasecmp (cpu_string, "default") == 0)
13805 return 0;
13806
13807 for (p = mips_cpu_info_table; p->name != 0; p++)
13808 if (mips_matching_cpu_name_p (p->name, cpu_string))
13809 return p;
13810
13811 as_bad ("Bad value (%s) for %s", cpu_string, option);
13812 return 0;
13813 }
13814
13815 /* Return the canonical processor information for ISA (a member of the
13816 ISA_MIPS* enumeration). */
13817
13818 static const struct mips_cpu_info *
13819 mips_cpu_info_from_isa (int isa)
13820 {
13821 int i;
13822
13823 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13824 if (mips_cpu_info_table[i].is_isa
13825 && isa == mips_cpu_info_table[i].isa)
13826 return (&mips_cpu_info_table[i]);
13827
13828 return NULL;
13829 }
13830
13831 static const struct mips_cpu_info *
13832 mips_cpu_info_from_arch (int arch)
13833 {
13834 int i;
13835
13836 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13837 if (arch == mips_cpu_info_table[i].cpu)
13838 return (&mips_cpu_info_table[i]);
13839
13840 return NULL;
13841 }
13842 \f
13843 static void
13844 show (FILE *stream, const char *string, int *col_p, int *first_p)
13845 {
13846 if (*first_p)
13847 {
13848 fprintf (stream, "%24s", "");
13849 *col_p = 24;
13850 }
13851 else
13852 {
13853 fprintf (stream, ", ");
13854 *col_p += 2;
13855 }
13856
13857 if (*col_p + strlen (string) > 72)
13858 {
13859 fprintf (stream, "\n%24s", "");
13860 *col_p = 24;
13861 }
13862
13863 fprintf (stream, "%s", string);
13864 *col_p += strlen (string);
13865
13866 *first_p = 0;
13867 }
13868
13869 void
13870 md_show_usage (FILE *stream)
13871 {
13872 int column, first;
13873 size_t i;
13874
13875 fprintf (stream, _("\
13876 MIPS options:\n\
13877 -EB generate big endian output\n\
13878 -EL generate little endian output\n\
13879 -g, -g2 do not remove unneeded NOPs or swap branches\n\
13880 -G NUM allow referencing objects up to NUM bytes\n\
13881 implicitly with the gp register [default 8]\n"));
13882 fprintf (stream, _("\
13883 -mips1 generate MIPS ISA I instructions\n\
13884 -mips2 generate MIPS ISA II instructions\n\
13885 -mips3 generate MIPS ISA III instructions\n\
13886 -mips4 generate MIPS ISA IV instructions\n\
13887 -mips5 generate MIPS ISA V instructions\n\
13888 -mips32 generate MIPS32 ISA instructions\n\
13889 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
13890 -mips64 generate MIPS64 ISA instructions\n\
13891 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
13892 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13893
13894 first = 1;
13895
13896 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13897 show (stream, mips_cpu_info_table[i].name, &column, &first);
13898 show (stream, "from-abi", &column, &first);
13899 fputc ('\n', stream);
13900
13901 fprintf (stream, _("\
13902 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13903 -no-mCPU don't generate code specific to CPU.\n\
13904 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13905
13906 first = 1;
13907
13908 show (stream, "3900", &column, &first);
13909 show (stream, "4010", &column, &first);
13910 show (stream, "4100", &column, &first);
13911 show (stream, "4650", &column, &first);
13912 fputc ('\n', stream);
13913
13914 fprintf (stream, _("\
13915 -mips16 generate mips16 instructions\n\
13916 -no-mips16 do not generate mips16 instructions\n"));
13917 fprintf (stream, _("\
13918 -mfix-vr4120 work around certain VR4120 errata\n\
13919 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13920 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13921 -mno-shared optimize output for executables\n\
13922 -msym32 assume all symbols have 32-bit values\n\
13923 -O0 remove unneeded NOPs, do not swap branches\n\
13924 -O remove unneeded NOPs and swap branches\n\
13925 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13926 --trap, --no-break trap exception on div by 0 and mult overflow\n\
13927 --break, --no-trap break exception on div by 0 and mult overflow\n"));
13928 #ifdef OBJ_ELF
13929 fprintf (stream, _("\
13930 -KPIC, -call_shared generate SVR4 position independent code\n\
13931 -non_shared do not generate position independent code\n\
13932 -xgot assume a 32 bit GOT\n\
13933 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
13934 -mshared, -mno-shared disable/enable .cpload optimization for\n\
13935 non-shared code\n\
13936 -mabi=ABI create ABI conformant object file for:\n"));
13937
13938 first = 1;
13939
13940 show (stream, "32", &column, &first);
13941 show (stream, "o64", &column, &first);
13942 show (stream, "n32", &column, &first);
13943 show (stream, "64", &column, &first);
13944 show (stream, "eabi", &column, &first);
13945
13946 fputc ('\n', stream);
13947
13948 fprintf (stream, _("\
13949 -32 create o32 ABI object file (default)\n\
13950 -n32 create n32 ABI object file\n\
13951 -64 create 64 ABI object file\n"));
13952 #endif
13953 }
13954
13955 enum dwarf2_format
13956 mips_dwarf2_format (void)
13957 {
13958 if (mips_abi == N64_ABI)
13959 {
13960 #ifdef TE_IRIX
13961 return dwarf2_format_64bit_irix;
13962 #else
13963 return dwarf2_format_64bit;
13964 #endif
13965 }
13966 else
13967 return dwarf2_format_32bit;
13968 }
13969
13970 int
13971 mips_dwarf2_addr_size (void)
13972 {
13973 if (mips_abi == N64_ABI)
13974 return 8;
13975 else
13976 return 4;
13977 }
This page took 0.316355 seconds and 5 git commands to generate.