* config/tc-mips.c (append_insn): Remove cop_interlocks test from
[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 (void);
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 ();
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 /* Check for conflicts between the branch and the instructions
2517 before the candidate delay slot. */
2518 || nops_for_insn (history + 1, ip) > 0
2519 /* Check for conflicts between the swapped sequence and the
2520 target of the branch. */
2521 || nops_for_sequence (2, history + 1, ip, history) > 0
2522 /* We do not swap with a trap instruction, since it
2523 complicates trap handlers to have the trap
2524 instruction be in a delay slot. */
2525 || (prev_pinfo & INSN_TRAP)
2526 /* If the branch reads a register that the previous
2527 instruction sets, we can not swap. */
2528 || (! mips_opts.mips16
2529 && (prev_pinfo & INSN_WRITE_GPR_T)
2530 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
2531 MIPS_GR_REG))
2532 || (! mips_opts.mips16
2533 && (prev_pinfo & INSN_WRITE_GPR_D)
2534 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
2535 MIPS_GR_REG))
2536 || (mips_opts.mips16
2537 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2538 && (insn_uses_reg
2539 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2540 MIPS16_REG)))
2541 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2542 && (insn_uses_reg
2543 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2544 MIPS16_REG)))
2545 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2546 && (insn_uses_reg
2547 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2548 MIPS16_REG)))
2549 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2550 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2551 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2552 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2553 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2554 && insn_uses_reg (ip,
2555 MIPS16OP_EXTRACT_REG32R
2556 (history[0].insn_opcode),
2557 MIPS_GR_REG))))
2558 /* If the branch writes a register that the previous
2559 instruction sets, we can not swap (we know that
2560 branches write only to RD or to $31). */
2561 || (! mips_opts.mips16
2562 && (prev_pinfo & INSN_WRITE_GPR_T)
2563 && (((pinfo & INSN_WRITE_GPR_D)
2564 && (EXTRACT_OPERAND (RT, history[0])
2565 == EXTRACT_OPERAND (RD, *ip)))
2566 || ((pinfo & INSN_WRITE_GPR_31)
2567 && EXTRACT_OPERAND (RT, history[0]) == RA)))
2568 || (! mips_opts.mips16
2569 && (prev_pinfo & INSN_WRITE_GPR_D)
2570 && (((pinfo & INSN_WRITE_GPR_D)
2571 && (EXTRACT_OPERAND (RD, history[0])
2572 == EXTRACT_OPERAND (RD, *ip)))
2573 || ((pinfo & INSN_WRITE_GPR_31)
2574 && EXTRACT_OPERAND (RD, history[0]) == RA)))
2575 || (mips_opts.mips16
2576 && (pinfo & MIPS16_INSN_WRITE_31)
2577 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2578 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2579 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2580 == RA))))
2581 /* If the branch writes a register that the previous
2582 instruction reads, we can not swap (we know that
2583 branches only write to RD or to $31). */
2584 || (! mips_opts.mips16
2585 && (pinfo & INSN_WRITE_GPR_D)
2586 && insn_uses_reg (&history[0],
2587 EXTRACT_OPERAND (RD, *ip),
2588 MIPS_GR_REG))
2589 || (! mips_opts.mips16
2590 && (pinfo & INSN_WRITE_GPR_31)
2591 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2592 || (mips_opts.mips16
2593 && (pinfo & MIPS16_INSN_WRITE_31)
2594 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2595 /* If one instruction sets a condition code and the
2596 other one uses a condition code, we can not swap. */
2597 || ((pinfo & INSN_READ_COND_CODE)
2598 && (prev_pinfo & INSN_WRITE_COND_CODE))
2599 || ((pinfo & INSN_WRITE_COND_CODE)
2600 && (prev_pinfo & INSN_READ_COND_CODE))
2601 /* If the previous instruction uses the PC, we can not
2602 swap. */
2603 || (mips_opts.mips16
2604 && (prev_pinfo & MIPS16_INSN_READ_PC))
2605 /* If the previous instruction had a fixup in mips16
2606 mode, we can not swap. This normally means that the
2607 previous instruction was a 4 byte branch anyhow. */
2608 || (mips_opts.mips16 && history[0].fixp[0])
2609 /* If the previous instruction is a sync, sync.l, or
2610 sync.p, we can not swap. */
2611 || (prev_pinfo & INSN_SYNC))
2612 {
2613 /* We could do even better for unconditional branches to
2614 portions of this object file; we could pick up the
2615 instruction at the destination, put it in the delay
2616 slot, and bump the destination address. */
2617 insert_into_history (0, 1, ip);
2618 emit_nop ();
2619 if (mips_relax.sequence)
2620 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2621 }
2622 else
2623 {
2624 /* It looks like we can actually do the swap. */
2625 struct mips_cl_insn delay = history[0];
2626 if (mips_opts.mips16)
2627 {
2628 know (delay.frag == ip->frag);
2629 move_insn (ip, delay.frag, delay.where);
2630 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
2631 }
2632 else if (relaxed_branch)
2633 {
2634 /* Add the delay slot instruction to the end of the
2635 current frag and shrink the fixed part of the
2636 original frag. If the branch occupies the tail of
2637 the latter, move it backwards to cover the gap. */
2638 delay.frag->fr_fix -= 4;
2639 if (delay.frag == ip->frag)
2640 move_insn (ip, ip->frag, ip->where - 4);
2641 add_fixed_insn (&delay);
2642 }
2643 else
2644 {
2645 move_insn (&delay, ip->frag, ip->where);
2646 move_insn (ip, history[0].frag, history[0].where);
2647 }
2648 history[0] = *ip;
2649 delay.fixed_p = 1;
2650 insert_into_history (0, 1, &delay);
2651 }
2652
2653 /* If that was an unconditional branch, forget the previous
2654 insn information. */
2655 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2656 mips_no_prev_insn ();
2657 }
2658 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2659 {
2660 /* We don't yet optimize a branch likely. What we should do
2661 is look at the target, copy the instruction found there
2662 into the delay slot, and increment the branch to jump to
2663 the next instruction. */
2664 insert_into_history (0, 1, ip);
2665 emit_nop ();
2666 }
2667 else
2668 insert_into_history (0, 1, ip);
2669 }
2670 else
2671 insert_into_history (0, 1, ip);
2672
2673 /* We just output an insn, so the next one doesn't have a label. */
2674 mips_clear_insn_labels ();
2675 }
2676
2677 /* Forget that there was any previous instruction or label. */
2678
2679 static void
2680 mips_no_prev_insn (void)
2681 {
2682 prev_nop_frag = NULL;
2683 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
2684 mips_clear_insn_labels ();
2685 }
2686
2687 /* This function must be called before we emit something other than
2688 instructions. It is like mips_no_prev_insn except that it inserts
2689 any NOPS that might be needed by previous instructions. */
2690
2691 void
2692 mips_emit_delays (void)
2693 {
2694 if (! mips_opts.noreorder)
2695 {
2696 int nops = nops_for_insn (history, NULL);
2697 if (nops > 0)
2698 {
2699 while (nops-- > 0)
2700 add_fixed_insn (NOP_INSN);
2701 mips_move_labels ();
2702 }
2703 }
2704 mips_no_prev_insn ();
2705 }
2706
2707 /* Start a (possibly nested) noreorder block. */
2708
2709 static void
2710 start_noreorder (void)
2711 {
2712 if (mips_opts.noreorder == 0)
2713 {
2714 unsigned int i;
2715 int nops;
2716
2717 /* None of the instructions before the .set noreorder can be moved. */
2718 for (i = 0; i < ARRAY_SIZE (history); i++)
2719 history[i].fixed_p = 1;
2720
2721 /* Insert any nops that might be needed between the .set noreorder
2722 block and the previous instructions. We will later remove any
2723 nops that turn out not to be needed. */
2724 nops = nops_for_insn (history, NULL);
2725 if (nops > 0)
2726 {
2727 if (mips_optimize != 0)
2728 {
2729 /* Record the frag which holds the nop instructions, so
2730 that we can remove them if we don't need them. */
2731 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2732 prev_nop_frag = frag_now;
2733 prev_nop_frag_holds = nops;
2734 prev_nop_frag_required = 0;
2735 prev_nop_frag_since = 0;
2736 }
2737
2738 for (; nops > 0; --nops)
2739 add_fixed_insn (NOP_INSN);
2740
2741 /* Move on to a new frag, so that it is safe to simply
2742 decrease the size of prev_nop_frag. */
2743 frag_wane (frag_now);
2744 frag_new (0);
2745 mips_move_labels ();
2746 }
2747 mips16_mark_labels ();
2748 mips_clear_insn_labels ();
2749 }
2750 mips_opts.noreorder++;
2751 mips_any_noreorder = 1;
2752 }
2753
2754 /* End a nested noreorder block. */
2755
2756 static void
2757 end_noreorder (void)
2758 {
2759 mips_opts.noreorder--;
2760 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2761 {
2762 /* Commit to inserting prev_nop_frag_required nops and go back to
2763 handling nop insertion the .set reorder way. */
2764 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2765 * (mips_opts.mips16 ? 2 : 4));
2766 insert_into_history (prev_nop_frag_since,
2767 prev_nop_frag_required, NOP_INSN);
2768 prev_nop_frag = NULL;
2769 }
2770 }
2771
2772 /* Set up global variables for the start of a new macro. */
2773
2774 static void
2775 macro_start (void)
2776 {
2777 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2778 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2779 && (history[0].insn_mo->pinfo
2780 & (INSN_UNCOND_BRANCH_DELAY
2781 | INSN_COND_BRANCH_DELAY
2782 | INSN_COND_BRANCH_LIKELY)) != 0);
2783 }
2784
2785 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2786 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2787 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2788
2789 static const char *
2790 macro_warning (relax_substateT subtype)
2791 {
2792 if (subtype & RELAX_DELAY_SLOT)
2793 return _("Macro instruction expanded into multiple instructions"
2794 " in a branch delay slot");
2795 else if (subtype & RELAX_NOMACRO)
2796 return _("Macro instruction expanded into multiple instructions");
2797 else
2798 return 0;
2799 }
2800
2801 /* Finish up a macro. Emit warnings as appropriate. */
2802
2803 static void
2804 macro_end (void)
2805 {
2806 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2807 {
2808 relax_substateT subtype;
2809
2810 /* Set up the relaxation warning flags. */
2811 subtype = 0;
2812 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2813 subtype |= RELAX_SECOND_LONGER;
2814 if (mips_opts.warn_about_macros)
2815 subtype |= RELAX_NOMACRO;
2816 if (mips_macro_warning.delay_slot_p)
2817 subtype |= RELAX_DELAY_SLOT;
2818
2819 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2820 {
2821 /* Either the macro has a single implementation or both
2822 implementations are longer than 4 bytes. Emit the
2823 warning now. */
2824 const char *msg = macro_warning (subtype);
2825 if (msg != 0)
2826 as_warn (msg);
2827 }
2828 else
2829 {
2830 /* One implementation might need a warning but the other
2831 definitely doesn't. */
2832 mips_macro_warning.first_frag->fr_subtype |= subtype;
2833 }
2834 }
2835 }
2836
2837 /* Read a macro's relocation codes from *ARGS and store them in *R.
2838 The first argument in *ARGS will be either the code for a single
2839 relocation or -1 followed by the three codes that make up a
2840 composite relocation. */
2841
2842 static void
2843 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2844 {
2845 int i, next;
2846
2847 next = va_arg (*args, int);
2848 if (next >= 0)
2849 r[0] = (bfd_reloc_code_real_type) next;
2850 else
2851 for (i = 0; i < 3; i++)
2852 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2853 }
2854
2855 /* Build an instruction created by a macro expansion. This is passed
2856 a pointer to the count of instructions created so far, an
2857 expression, the name of the instruction to build, an operand format
2858 string, and corresponding arguments. */
2859
2860 static void
2861 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2862 {
2863 const struct mips_opcode *mo;
2864 struct mips_cl_insn insn;
2865 bfd_reloc_code_real_type r[3];
2866 va_list args;
2867
2868 va_start (args, fmt);
2869
2870 if (mips_opts.mips16)
2871 {
2872 mips16_macro_build (ep, name, fmt, args);
2873 va_end (args);
2874 return;
2875 }
2876
2877 r[0] = BFD_RELOC_UNUSED;
2878 r[1] = BFD_RELOC_UNUSED;
2879 r[2] = BFD_RELOC_UNUSED;
2880 mo = (struct mips_opcode *) hash_find (op_hash, name);
2881 assert (mo);
2882 assert (strcmp (name, mo->name) == 0);
2883
2884 /* Search until we get a match for NAME. It is assumed here that
2885 macros will never generate MDMX or MIPS-3D instructions. */
2886 while (strcmp (fmt, mo->args) != 0
2887 || mo->pinfo == INSN_MACRO
2888 || !OPCODE_IS_MEMBER (mo,
2889 (mips_opts.isa
2890 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2891 mips_opts.arch)
2892 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
2893 {
2894 ++mo;
2895 assert (mo->name);
2896 assert (strcmp (name, mo->name) == 0);
2897 }
2898
2899 create_insn (&insn, mo);
2900 for (;;)
2901 {
2902 switch (*fmt++)
2903 {
2904 case '\0':
2905 break;
2906
2907 case ',':
2908 case '(':
2909 case ')':
2910 continue;
2911
2912 case '+':
2913 switch (*fmt++)
2914 {
2915 case 'A':
2916 case 'E':
2917 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
2918 continue;
2919
2920 case 'B':
2921 case 'F':
2922 /* Note that in the macro case, these arguments are already
2923 in MSB form. (When handling the instruction in the
2924 non-macro case, these arguments are sizes from which
2925 MSB values must be calculated.) */
2926 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
2927 continue;
2928
2929 case 'C':
2930 case 'G':
2931 case 'H':
2932 /* Note that in the macro case, these arguments are already
2933 in MSBD form. (When handling the instruction in the
2934 non-macro case, these arguments are sizes from which
2935 MSBD values must be calculated.) */
2936 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
2937 continue;
2938
2939 default:
2940 internalError ();
2941 }
2942 continue;
2943
2944 case 't':
2945 case 'w':
2946 case 'E':
2947 INSERT_OPERAND (RT, insn, va_arg (args, int));
2948 continue;
2949
2950 case 'c':
2951 INSERT_OPERAND (CODE, insn, va_arg (args, int));
2952 continue;
2953
2954 case 'T':
2955 case 'W':
2956 INSERT_OPERAND (FT, insn, va_arg (args, int));
2957 continue;
2958
2959 case 'd':
2960 case 'G':
2961 case 'K':
2962 INSERT_OPERAND (RD, insn, va_arg (args, int));
2963 continue;
2964
2965 case 'U':
2966 {
2967 int tmp = va_arg (args, int);
2968
2969 INSERT_OPERAND (RT, insn, tmp);
2970 INSERT_OPERAND (RD, insn, tmp);
2971 continue;
2972 }
2973
2974 case 'V':
2975 case 'S':
2976 INSERT_OPERAND (FS, insn, va_arg (args, int));
2977 continue;
2978
2979 case 'z':
2980 continue;
2981
2982 case '<':
2983 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
2984 continue;
2985
2986 case 'D':
2987 INSERT_OPERAND (FD, insn, va_arg (args, int));
2988 continue;
2989
2990 case 'B':
2991 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
2992 continue;
2993
2994 case 'J':
2995 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
2996 continue;
2997
2998 case 'q':
2999 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
3000 continue;
3001
3002 case 'b':
3003 case 's':
3004 case 'r':
3005 case 'v':
3006 INSERT_OPERAND (RS, insn, va_arg (args, int));
3007 continue;
3008
3009 case 'i':
3010 case 'j':
3011 case 'o':
3012 macro_read_relocs (&args, r);
3013 assert (*r == BFD_RELOC_GPREL16
3014 || *r == BFD_RELOC_MIPS_LITERAL
3015 || *r == BFD_RELOC_MIPS_HIGHER
3016 || *r == BFD_RELOC_HI16_S
3017 || *r == BFD_RELOC_LO16
3018 || *r == BFD_RELOC_MIPS_GOT16
3019 || *r == BFD_RELOC_MIPS_CALL16
3020 || *r == BFD_RELOC_MIPS_GOT_DISP
3021 || *r == BFD_RELOC_MIPS_GOT_PAGE
3022 || *r == BFD_RELOC_MIPS_GOT_OFST
3023 || *r == BFD_RELOC_MIPS_GOT_LO16
3024 || *r == BFD_RELOC_MIPS_CALL_LO16);
3025 continue;
3026
3027 case 'u':
3028 macro_read_relocs (&args, r);
3029 assert (ep != NULL
3030 && (ep->X_op == O_constant
3031 || (ep->X_op == O_symbol
3032 && (*r == BFD_RELOC_MIPS_HIGHEST
3033 || *r == BFD_RELOC_HI16_S
3034 || *r == BFD_RELOC_HI16
3035 || *r == BFD_RELOC_GPREL16
3036 || *r == BFD_RELOC_MIPS_GOT_HI16
3037 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3038 continue;
3039
3040 case 'p':
3041 assert (ep != NULL);
3042 /*
3043 * This allows macro() to pass an immediate expression for
3044 * creating short branches without creating a symbol.
3045 * Note that the expression still might come from the assembly
3046 * input, in which case the value is not checked for range nor
3047 * is a relocation entry generated (yuck).
3048 */
3049 if (ep->X_op == O_constant)
3050 {
3051 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3052 ep = NULL;
3053 }
3054 else
3055 *r = BFD_RELOC_16_PCREL_S2;
3056 continue;
3057
3058 case 'a':
3059 assert (ep != NULL);
3060 *r = BFD_RELOC_MIPS_JMP;
3061 continue;
3062
3063 case 'C':
3064 insn.insn_opcode |= va_arg (args, unsigned long);
3065 continue;
3066
3067 default:
3068 internalError ();
3069 }
3070 break;
3071 }
3072 va_end (args);
3073 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3074
3075 append_insn (&insn, ep, r);
3076 }
3077
3078 static void
3079 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3080 va_list args)
3081 {
3082 struct mips_opcode *mo;
3083 struct mips_cl_insn insn;
3084 bfd_reloc_code_real_type r[3]
3085 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3086
3087 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3088 assert (mo);
3089 assert (strcmp (name, mo->name) == 0);
3090
3091 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
3092 {
3093 ++mo;
3094 assert (mo->name);
3095 assert (strcmp (name, mo->name) == 0);
3096 }
3097
3098 create_insn (&insn, mo);
3099 for (;;)
3100 {
3101 int c;
3102
3103 c = *fmt++;
3104 switch (c)
3105 {
3106 case '\0':
3107 break;
3108
3109 case ',':
3110 case '(':
3111 case ')':
3112 continue;
3113
3114 case 'y':
3115 case 'w':
3116 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
3117 continue;
3118
3119 case 'x':
3120 case 'v':
3121 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
3122 continue;
3123
3124 case 'z':
3125 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
3126 continue;
3127
3128 case 'Z':
3129 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
3130 continue;
3131
3132 case '0':
3133 case 'S':
3134 case 'P':
3135 case 'R':
3136 continue;
3137
3138 case 'X':
3139 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
3140 continue;
3141
3142 case 'Y':
3143 {
3144 int regno;
3145
3146 regno = va_arg (args, int);
3147 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3148 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3149 }
3150 continue;
3151
3152 case '<':
3153 case '>':
3154 case '4':
3155 case '5':
3156 case 'H':
3157 case 'W':
3158 case 'D':
3159 case 'j':
3160 case '8':
3161 case 'V':
3162 case 'C':
3163 case 'U':
3164 case 'k':
3165 case 'K':
3166 case 'p':
3167 case 'q':
3168 {
3169 assert (ep != NULL);
3170
3171 if (ep->X_op != O_constant)
3172 *r = (int) BFD_RELOC_UNUSED + c;
3173 else
3174 {
3175 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3176 FALSE, &insn.insn_opcode, &insn.use_extend,
3177 &insn.extend);
3178 ep = NULL;
3179 *r = BFD_RELOC_UNUSED;
3180 }
3181 }
3182 continue;
3183
3184 case '6':
3185 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
3186 continue;
3187 }
3188
3189 break;
3190 }
3191
3192 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3193
3194 append_insn (&insn, ep, r);
3195 }
3196
3197 /*
3198 * Generate a "jalr" instruction with a relocation hint to the called
3199 * function. This occurs in NewABI PIC code.
3200 */
3201 static void
3202 macro_build_jalr (expressionS *ep)
3203 {
3204 char *f = NULL;
3205
3206 if (HAVE_NEWABI)
3207 {
3208 frag_grow (8);
3209 f = frag_more (0);
3210 }
3211 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3212 if (HAVE_NEWABI)
3213 fix_new_exp (frag_now, f - frag_now->fr_literal,
3214 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3215 }
3216
3217 /*
3218 * Generate a "lui" instruction.
3219 */
3220 static void
3221 macro_build_lui (expressionS *ep, int regnum)
3222 {
3223 expressionS high_expr;
3224 const struct mips_opcode *mo;
3225 struct mips_cl_insn insn;
3226 bfd_reloc_code_real_type r[3]
3227 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3228 const char *name = "lui";
3229 const char *fmt = "t,u";
3230
3231 assert (! mips_opts.mips16);
3232
3233 high_expr = *ep;
3234
3235 if (high_expr.X_op == O_constant)
3236 {
3237 /* we can compute the instruction now without a relocation entry */
3238 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3239 >> 16) & 0xffff;
3240 *r = BFD_RELOC_UNUSED;
3241 }
3242 else
3243 {
3244 assert (ep->X_op == O_symbol);
3245 /* _gp_disp is a special case, used from s_cpload.
3246 __gnu_local_gp is used if mips_no_shared. */
3247 assert (mips_pic == NO_PIC
3248 || (! HAVE_NEWABI
3249 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3250 || (! mips_in_shared
3251 && strcmp (S_GET_NAME (ep->X_add_symbol),
3252 "__gnu_local_gp") == 0));
3253 *r = BFD_RELOC_HI16_S;
3254 }
3255
3256 mo = hash_find (op_hash, name);
3257 assert (strcmp (name, mo->name) == 0);
3258 assert (strcmp (fmt, mo->args) == 0);
3259 create_insn (&insn, mo);
3260
3261 insn.insn_opcode = insn.insn_mo->match;
3262 INSERT_OPERAND (RT, insn, regnum);
3263 if (*r == BFD_RELOC_UNUSED)
3264 {
3265 insn.insn_opcode |= high_expr.X_add_number;
3266 append_insn (&insn, NULL, r);
3267 }
3268 else
3269 append_insn (&insn, &high_expr, r);
3270 }
3271
3272 /* Generate a sequence of instructions to do a load or store from a constant
3273 offset off of a base register (breg) into/from a target register (treg),
3274 using AT if necessary. */
3275 static void
3276 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3277 int treg, int breg, int dbl)
3278 {
3279 assert (ep->X_op == O_constant);
3280
3281 /* Sign-extending 32-bit constants makes their handling easier. */
3282 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3283 == ~((bfd_vma) 0x7fffffff)))
3284 {
3285 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3286 as_bad (_("constant too large"));
3287
3288 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3289 - 0x80000000);
3290 }
3291
3292 /* Right now, this routine can only handle signed 32-bit constants. */
3293 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3294 as_warn (_("operand overflow"));
3295
3296 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3297 {
3298 /* Signed 16-bit offset will fit in the op. Easy! */
3299 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3300 }
3301 else
3302 {
3303 /* 32-bit offset, need multiple instructions and AT, like:
3304 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3305 addu $tempreg,$tempreg,$breg
3306 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3307 to handle the complete offset. */
3308 macro_build_lui (ep, AT);
3309 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3310 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3311
3312 if (mips_opts.noat)
3313 as_bad (_("Macro used $at after \".set noat\""));
3314 }
3315 }
3316
3317 /* set_at()
3318 * Generates code to set the $at register to true (one)
3319 * if reg is less than the immediate expression.
3320 */
3321 static void
3322 set_at (int reg, int unsignedp)
3323 {
3324 if (imm_expr.X_op == O_constant
3325 && imm_expr.X_add_number >= -0x8000
3326 && imm_expr.X_add_number < 0x8000)
3327 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3328 AT, reg, BFD_RELOC_LO16);
3329 else
3330 {
3331 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3332 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3333 }
3334 }
3335
3336 static void
3337 normalize_constant_expr (expressionS *ex)
3338 {
3339 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3340 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3341 - 0x80000000);
3342 }
3343
3344 /* Warn if an expression is not a constant. */
3345
3346 static void
3347 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3348 {
3349 if (ex->X_op == O_big)
3350 as_bad (_("unsupported large constant"));
3351 else if (ex->X_op != O_constant)
3352 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3353
3354 normalize_constant_expr (ex);
3355 }
3356
3357 /* Count the leading zeroes by performing a binary chop. This is a
3358 bulky bit of source, but performance is a LOT better for the
3359 majority of values than a simple loop to count the bits:
3360 for (lcnt = 0; (lcnt < 32); lcnt++)
3361 if ((v) & (1 << (31 - lcnt)))
3362 break;
3363 However it is not code size friendly, and the gain will drop a bit
3364 on certain cached systems.
3365 */
3366 #define COUNT_TOP_ZEROES(v) \
3367 (((v) & ~0xffff) == 0 \
3368 ? ((v) & ~0xff) == 0 \
3369 ? ((v) & ~0xf) == 0 \
3370 ? ((v) & ~0x3) == 0 \
3371 ? ((v) & ~0x1) == 0 \
3372 ? !(v) \
3373 ? 32 \
3374 : 31 \
3375 : 30 \
3376 : ((v) & ~0x7) == 0 \
3377 ? 29 \
3378 : 28 \
3379 : ((v) & ~0x3f) == 0 \
3380 ? ((v) & ~0x1f) == 0 \
3381 ? 27 \
3382 : 26 \
3383 : ((v) & ~0x7f) == 0 \
3384 ? 25 \
3385 : 24 \
3386 : ((v) & ~0xfff) == 0 \
3387 ? ((v) & ~0x3ff) == 0 \
3388 ? ((v) & ~0x1ff) == 0 \
3389 ? 23 \
3390 : 22 \
3391 : ((v) & ~0x7ff) == 0 \
3392 ? 21 \
3393 : 20 \
3394 : ((v) & ~0x3fff) == 0 \
3395 ? ((v) & ~0x1fff) == 0 \
3396 ? 19 \
3397 : 18 \
3398 : ((v) & ~0x7fff) == 0 \
3399 ? 17 \
3400 : 16 \
3401 : ((v) & ~0xffffff) == 0 \
3402 ? ((v) & ~0xfffff) == 0 \
3403 ? ((v) & ~0x3ffff) == 0 \
3404 ? ((v) & ~0x1ffff) == 0 \
3405 ? 15 \
3406 : 14 \
3407 : ((v) & ~0x7ffff) == 0 \
3408 ? 13 \
3409 : 12 \
3410 : ((v) & ~0x3fffff) == 0 \
3411 ? ((v) & ~0x1fffff) == 0 \
3412 ? 11 \
3413 : 10 \
3414 : ((v) & ~0x7fffff) == 0 \
3415 ? 9 \
3416 : 8 \
3417 : ((v) & ~0xfffffff) == 0 \
3418 ? ((v) & ~0x3ffffff) == 0 \
3419 ? ((v) & ~0x1ffffff) == 0 \
3420 ? 7 \
3421 : 6 \
3422 : ((v) & ~0x7ffffff) == 0 \
3423 ? 5 \
3424 : 4 \
3425 : ((v) & ~0x3fffffff) == 0 \
3426 ? ((v) & ~0x1fffffff) == 0 \
3427 ? 3 \
3428 : 2 \
3429 : ((v) & ~0x7fffffff) == 0 \
3430 ? 1 \
3431 : 0)
3432
3433 /* load_register()
3434 * This routine generates the least number of instructions necessary to load
3435 * an absolute expression value into a register.
3436 */
3437 static void
3438 load_register (int reg, expressionS *ep, int dbl)
3439 {
3440 int freg;
3441 expressionS hi32, lo32;
3442
3443 if (ep->X_op != O_big)
3444 {
3445 assert (ep->X_op == O_constant);
3446
3447 /* Sign-extending 32-bit constants makes their handling easier. */
3448 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3449 == ~((bfd_vma) 0x7fffffff)))
3450 {
3451 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3452 as_bad (_("constant too large"));
3453
3454 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3455 - 0x80000000);
3456 }
3457
3458 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3459 {
3460 /* We can handle 16 bit signed values with an addiu to
3461 $zero. No need to ever use daddiu here, since $zero and
3462 the result are always correct in 32 bit mode. */
3463 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3464 return;
3465 }
3466 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3467 {
3468 /* We can handle 16 bit unsigned values with an ori to
3469 $zero. */
3470 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3471 return;
3472 }
3473 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3474 {
3475 /* 32 bit values require an lui. */
3476 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3477 if ((ep->X_add_number & 0xffff) != 0)
3478 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3479 return;
3480 }
3481 }
3482
3483 /* The value is larger than 32 bits. */
3484
3485 if (HAVE_32BIT_GPRS)
3486 {
3487 as_bad (_("Number (0x%lx) larger than 32 bits"),
3488 (unsigned long) ep->X_add_number);
3489 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3490 return;
3491 }
3492
3493 if (ep->X_op != O_big)
3494 {
3495 hi32 = *ep;
3496 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3497 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3498 hi32.X_add_number &= 0xffffffff;
3499 lo32 = *ep;
3500 lo32.X_add_number &= 0xffffffff;
3501 }
3502 else
3503 {
3504 assert (ep->X_add_number > 2);
3505 if (ep->X_add_number == 3)
3506 generic_bignum[3] = 0;
3507 else if (ep->X_add_number > 4)
3508 as_bad (_("Number larger than 64 bits"));
3509 lo32.X_op = O_constant;
3510 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3511 hi32.X_op = O_constant;
3512 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3513 }
3514
3515 if (hi32.X_add_number == 0)
3516 freg = 0;
3517 else
3518 {
3519 int shift, bit;
3520 unsigned long hi, lo;
3521
3522 if (hi32.X_add_number == (offsetT) 0xffffffff)
3523 {
3524 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3525 {
3526 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3527 return;
3528 }
3529 if (lo32.X_add_number & 0x80000000)
3530 {
3531 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3532 if (lo32.X_add_number & 0xffff)
3533 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3534 return;
3535 }
3536 }
3537
3538 /* Check for 16bit shifted constant. We know that hi32 is
3539 non-zero, so start the mask on the first bit of the hi32
3540 value. */
3541 shift = 17;
3542 do
3543 {
3544 unsigned long himask, lomask;
3545
3546 if (shift < 32)
3547 {
3548 himask = 0xffff >> (32 - shift);
3549 lomask = (0xffff << shift) & 0xffffffff;
3550 }
3551 else
3552 {
3553 himask = 0xffff << (shift - 32);
3554 lomask = 0;
3555 }
3556 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3557 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3558 {
3559 expressionS tmp;
3560
3561 tmp.X_op = O_constant;
3562 if (shift < 32)
3563 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3564 | (lo32.X_add_number >> shift));
3565 else
3566 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3567 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3568 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3569 reg, reg, (shift >= 32) ? shift - 32 : shift);
3570 return;
3571 }
3572 ++shift;
3573 }
3574 while (shift <= (64 - 16));
3575
3576 /* Find the bit number of the lowest one bit, and store the
3577 shifted value in hi/lo. */
3578 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3579 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3580 if (lo != 0)
3581 {
3582 bit = 0;
3583 while ((lo & 1) == 0)
3584 {
3585 lo >>= 1;
3586 ++bit;
3587 }
3588 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3589 hi >>= bit;
3590 }
3591 else
3592 {
3593 bit = 32;
3594 while ((hi & 1) == 0)
3595 {
3596 hi >>= 1;
3597 ++bit;
3598 }
3599 lo = hi;
3600 hi = 0;
3601 }
3602
3603 /* Optimize if the shifted value is a (power of 2) - 1. */
3604 if ((hi == 0 && ((lo + 1) & lo) == 0)
3605 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3606 {
3607 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3608 if (shift != 0)
3609 {
3610 expressionS tmp;
3611
3612 /* This instruction will set the register to be all
3613 ones. */
3614 tmp.X_op = O_constant;
3615 tmp.X_add_number = (offsetT) -1;
3616 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3617 if (bit != 0)
3618 {
3619 bit += shift;
3620 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3621 reg, reg, (bit >= 32) ? bit - 32 : bit);
3622 }
3623 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3624 reg, reg, (shift >= 32) ? shift - 32 : shift);
3625 return;
3626 }
3627 }
3628
3629 /* Sign extend hi32 before calling load_register, because we can
3630 generally get better code when we load a sign extended value. */
3631 if ((hi32.X_add_number & 0x80000000) != 0)
3632 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3633 load_register (reg, &hi32, 0);
3634 freg = reg;
3635 }
3636 if ((lo32.X_add_number & 0xffff0000) == 0)
3637 {
3638 if (freg != 0)
3639 {
3640 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3641 freg = reg;
3642 }
3643 }
3644 else
3645 {
3646 expressionS mid16;
3647
3648 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3649 {
3650 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3651 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3652 return;
3653 }
3654
3655 if (freg != 0)
3656 {
3657 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3658 freg = reg;
3659 }
3660 mid16 = lo32;
3661 mid16.X_add_number >>= 16;
3662 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3663 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3664 freg = reg;
3665 }
3666 if ((lo32.X_add_number & 0xffff) != 0)
3667 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3668 }
3669
3670 static inline void
3671 load_delay_nop (void)
3672 {
3673 if (!gpr_interlocks)
3674 macro_build (NULL, "nop", "");
3675 }
3676
3677 /* Load an address into a register. */
3678
3679 static void
3680 load_address (int reg, expressionS *ep, int *used_at)
3681 {
3682 if (ep->X_op != O_constant
3683 && ep->X_op != O_symbol)
3684 {
3685 as_bad (_("expression too complex"));
3686 ep->X_op = O_constant;
3687 }
3688
3689 if (ep->X_op == O_constant)
3690 {
3691 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3692 return;
3693 }
3694
3695 if (mips_pic == NO_PIC)
3696 {
3697 /* If this is a reference to a GP relative symbol, we want
3698 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3699 Otherwise we want
3700 lui $reg,<sym> (BFD_RELOC_HI16_S)
3701 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3702 If we have an addend, we always use the latter form.
3703
3704 With 64bit address space and a usable $at we want
3705 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3706 lui $at,<sym> (BFD_RELOC_HI16_S)
3707 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3708 daddiu $at,<sym> (BFD_RELOC_LO16)
3709 dsll32 $reg,0
3710 daddu $reg,$reg,$at
3711
3712 If $at is already in use, we use a path which is suboptimal
3713 on superscalar processors.
3714 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3715 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3716 dsll $reg,16
3717 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3718 dsll $reg,16
3719 daddiu $reg,<sym> (BFD_RELOC_LO16)
3720
3721 For GP relative symbols in 64bit address space we can use
3722 the same sequence as in 32bit address space. */
3723 if (HAVE_64BIT_SYMBOLS)
3724 {
3725 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3726 && !nopic_need_relax (ep->X_add_symbol, 1))
3727 {
3728 relax_start (ep->X_add_symbol);
3729 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3730 mips_gp_register, BFD_RELOC_GPREL16);
3731 relax_switch ();
3732 }
3733
3734 if (*used_at == 0 && !mips_opts.noat)
3735 {
3736 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3737 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3738 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3739 BFD_RELOC_MIPS_HIGHER);
3740 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3741 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3742 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3743 *used_at = 1;
3744 }
3745 else
3746 {
3747 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3748 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3749 BFD_RELOC_MIPS_HIGHER);
3750 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3751 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3752 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3753 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3754 }
3755
3756 if (mips_relax.sequence)
3757 relax_end ();
3758 }
3759 else
3760 {
3761 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3762 && !nopic_need_relax (ep->X_add_symbol, 1))
3763 {
3764 relax_start (ep->X_add_symbol);
3765 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3766 mips_gp_register, BFD_RELOC_GPREL16);
3767 relax_switch ();
3768 }
3769 macro_build_lui (ep, reg);
3770 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3771 reg, reg, BFD_RELOC_LO16);
3772 if (mips_relax.sequence)
3773 relax_end ();
3774 }
3775 }
3776 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3777 {
3778 expressionS ex;
3779
3780 /* If this is a reference to an external symbol, we want
3781 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3782 Otherwise we want
3783 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3784 nop
3785 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3786 If there is a constant, it must be added in after.
3787
3788 If we have NewABI, we want
3789 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3790 unless we're referencing a global symbol with a non-zero
3791 offset, in which case cst must be added separately. */
3792 if (HAVE_NEWABI)
3793 {
3794 if (ep->X_add_number)
3795 {
3796 ex.X_add_number = ep->X_add_number;
3797 ep->X_add_number = 0;
3798 relax_start (ep->X_add_symbol);
3799 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3800 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3801 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3802 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3803 ex.X_op = O_constant;
3804 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3805 reg, reg, BFD_RELOC_LO16);
3806 ep->X_add_number = ex.X_add_number;
3807 relax_switch ();
3808 }
3809 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3810 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3811 if (mips_relax.sequence)
3812 relax_end ();
3813 }
3814 else
3815 {
3816 ex.X_add_number = ep->X_add_number;
3817 ep->X_add_number = 0;
3818 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3819 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3820 load_delay_nop ();
3821 relax_start (ep->X_add_symbol);
3822 relax_switch ();
3823 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3824 BFD_RELOC_LO16);
3825 relax_end ();
3826
3827 if (ex.X_add_number != 0)
3828 {
3829 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3830 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3831 ex.X_op = O_constant;
3832 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3833 reg, reg, BFD_RELOC_LO16);
3834 }
3835 }
3836 }
3837 else if (mips_pic == SVR4_PIC)
3838 {
3839 expressionS ex;
3840
3841 /* This is the large GOT case. If this is a reference to an
3842 external symbol, we want
3843 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3844 addu $reg,$reg,$gp
3845 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3846
3847 Otherwise, for a reference to a local symbol in old ABI, we want
3848 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3849 nop
3850 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3851 If there is a constant, it must be added in after.
3852
3853 In the NewABI, for local symbols, with or without offsets, we want:
3854 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3855 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3856 */
3857 if (HAVE_NEWABI)
3858 {
3859 ex.X_add_number = ep->X_add_number;
3860 ep->X_add_number = 0;
3861 relax_start (ep->X_add_symbol);
3862 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3863 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3864 reg, reg, mips_gp_register);
3865 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3866 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3867 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3868 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3869 else if (ex.X_add_number)
3870 {
3871 ex.X_op = O_constant;
3872 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3873 BFD_RELOC_LO16);
3874 }
3875
3876 ep->X_add_number = ex.X_add_number;
3877 relax_switch ();
3878 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3879 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3880 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3881 BFD_RELOC_MIPS_GOT_OFST);
3882 relax_end ();
3883 }
3884 else
3885 {
3886 ex.X_add_number = ep->X_add_number;
3887 ep->X_add_number = 0;
3888 relax_start (ep->X_add_symbol);
3889 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3890 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3891 reg, reg, mips_gp_register);
3892 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3893 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3894 relax_switch ();
3895 if (reg_needs_delay (mips_gp_register))
3896 {
3897 /* We need a nop before loading from $gp. This special
3898 check is required because the lui which starts the main
3899 instruction stream does not refer to $gp, and so will not
3900 insert the nop which may be required. */
3901 macro_build (NULL, "nop", "");
3902 }
3903 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3904 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3905 load_delay_nop ();
3906 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3907 BFD_RELOC_LO16);
3908 relax_end ();
3909
3910 if (ex.X_add_number != 0)
3911 {
3912 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3913 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3914 ex.X_op = O_constant;
3915 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3916 BFD_RELOC_LO16);
3917 }
3918 }
3919 }
3920 else
3921 abort ();
3922
3923 if (mips_opts.noat && *used_at == 1)
3924 as_bad (_("Macro used $at after \".set noat\""));
3925 }
3926
3927 /* Move the contents of register SOURCE into register DEST. */
3928
3929 static void
3930 move_register (int dest, int source)
3931 {
3932 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
3933 dest, source, 0);
3934 }
3935
3936 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
3937 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
3938 The two alternatives are:
3939
3940 Global symbol Local sybmol
3941 ------------- ------------
3942 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
3943 ... ...
3944 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
3945
3946 load_got_offset emits the first instruction and add_got_offset
3947 emits the second for a 16-bit offset or add_got_offset_hilo emits
3948 a sequence to add a 32-bit offset using a scratch register. */
3949
3950 static void
3951 load_got_offset (int dest, expressionS *local)
3952 {
3953 expressionS global;
3954
3955 global = *local;
3956 global.X_add_number = 0;
3957
3958 relax_start (local->X_add_symbol);
3959 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
3960 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3961 relax_switch ();
3962 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
3963 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3964 relax_end ();
3965 }
3966
3967 static void
3968 add_got_offset (int dest, expressionS *local)
3969 {
3970 expressionS global;
3971
3972 global.X_op = O_constant;
3973 global.X_op_symbol = NULL;
3974 global.X_add_symbol = NULL;
3975 global.X_add_number = local->X_add_number;
3976
3977 relax_start (local->X_add_symbol);
3978 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
3979 dest, dest, BFD_RELOC_LO16);
3980 relax_switch ();
3981 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
3982 relax_end ();
3983 }
3984
3985 static void
3986 add_got_offset_hilo (int dest, expressionS *local, int tmp)
3987 {
3988 expressionS global;
3989 int hold_mips_optimize;
3990
3991 global.X_op = O_constant;
3992 global.X_op_symbol = NULL;
3993 global.X_add_symbol = NULL;
3994 global.X_add_number = local->X_add_number;
3995
3996 relax_start (local->X_add_symbol);
3997 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
3998 relax_switch ();
3999 /* Set mips_optimize around the lui instruction to avoid
4000 inserting an unnecessary nop after the lw. */
4001 hold_mips_optimize = mips_optimize;
4002 mips_optimize = 2;
4003 macro_build_lui (&global, tmp);
4004 mips_optimize = hold_mips_optimize;
4005 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4006 relax_end ();
4007
4008 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4009 }
4010
4011 /*
4012 * Build macros
4013 * This routine implements the seemingly endless macro or synthesized
4014 * instructions and addressing modes in the mips assembly language. Many
4015 * of these macros are simple and are similar to each other. These could
4016 * probably be handled by some kind of table or grammar approach instead of
4017 * this verbose method. Others are not simple macros but are more like
4018 * optimizing code generation.
4019 * One interesting optimization is when several store macros appear
4020 * consecutively that would load AT with the upper half of the same address.
4021 * The ensuing load upper instructions are ommited. This implies some kind
4022 * of global optimization. We currently only optimize within a single macro.
4023 * For many of the load and store macros if the address is specified as a
4024 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4025 * first load register 'at' with zero and use it as the base register. The
4026 * mips assembler simply uses register $zero. Just one tiny optimization
4027 * we're missing.
4028 */
4029 static void
4030 macro (struct mips_cl_insn *ip)
4031 {
4032 register int treg, sreg, dreg, breg;
4033 int tempreg;
4034 int mask;
4035 int used_at = 0;
4036 expressionS expr1;
4037 const char *s;
4038 const char *s2;
4039 const char *fmt;
4040 int likely = 0;
4041 int dbl = 0;
4042 int coproc = 0;
4043 int lr = 0;
4044 int imm = 0;
4045 int call = 0;
4046 int off;
4047 offsetT maxnum;
4048 bfd_reloc_code_real_type r;
4049 int hold_mips_optimize;
4050
4051 assert (! mips_opts.mips16);
4052
4053 treg = (ip->insn_opcode >> 16) & 0x1f;
4054 dreg = (ip->insn_opcode >> 11) & 0x1f;
4055 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4056 mask = ip->insn_mo->mask;
4057
4058 expr1.X_op = O_constant;
4059 expr1.X_op_symbol = NULL;
4060 expr1.X_add_symbol = NULL;
4061 expr1.X_add_number = 1;
4062
4063 switch (mask)
4064 {
4065 case M_DABS:
4066 dbl = 1;
4067 case M_ABS:
4068 /* bgez $a0,.+12
4069 move v0,$a0
4070 sub v0,$zero,$a0
4071 */
4072
4073 start_noreorder ();
4074
4075 expr1.X_add_number = 8;
4076 macro_build (&expr1, "bgez", "s,p", sreg);
4077 if (dreg == sreg)
4078 macro_build (NULL, "nop", "", 0);
4079 else
4080 move_register (dreg, sreg);
4081 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4082
4083 end_noreorder ();
4084 break;
4085
4086 case M_ADD_I:
4087 s = "addi";
4088 s2 = "add";
4089 goto do_addi;
4090 case M_ADDU_I:
4091 s = "addiu";
4092 s2 = "addu";
4093 goto do_addi;
4094 case M_DADD_I:
4095 dbl = 1;
4096 s = "daddi";
4097 s2 = "dadd";
4098 goto do_addi;
4099 case M_DADDU_I:
4100 dbl = 1;
4101 s = "daddiu";
4102 s2 = "daddu";
4103 do_addi:
4104 if (imm_expr.X_op == O_constant
4105 && imm_expr.X_add_number >= -0x8000
4106 && imm_expr.X_add_number < 0x8000)
4107 {
4108 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4109 break;
4110 }
4111 used_at = 1;
4112 load_register (AT, &imm_expr, dbl);
4113 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4114 break;
4115
4116 case M_AND_I:
4117 s = "andi";
4118 s2 = "and";
4119 goto do_bit;
4120 case M_OR_I:
4121 s = "ori";
4122 s2 = "or";
4123 goto do_bit;
4124 case M_NOR_I:
4125 s = "";
4126 s2 = "nor";
4127 goto do_bit;
4128 case M_XOR_I:
4129 s = "xori";
4130 s2 = "xor";
4131 do_bit:
4132 if (imm_expr.X_op == O_constant
4133 && imm_expr.X_add_number >= 0
4134 && imm_expr.X_add_number < 0x10000)
4135 {
4136 if (mask != M_NOR_I)
4137 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4138 else
4139 {
4140 macro_build (&imm_expr, "ori", "t,r,i",
4141 treg, sreg, BFD_RELOC_LO16);
4142 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4143 }
4144 break;
4145 }
4146
4147 used_at = 1;
4148 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4149 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4150 break;
4151
4152 case M_BEQ_I:
4153 s = "beq";
4154 goto beq_i;
4155 case M_BEQL_I:
4156 s = "beql";
4157 likely = 1;
4158 goto beq_i;
4159 case M_BNE_I:
4160 s = "bne";
4161 goto beq_i;
4162 case M_BNEL_I:
4163 s = "bnel";
4164 likely = 1;
4165 beq_i:
4166 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4167 {
4168 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4169 break;
4170 }
4171 used_at = 1;
4172 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4173 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4174 break;
4175
4176 case M_BGEL:
4177 likely = 1;
4178 case M_BGE:
4179 if (treg == 0)
4180 {
4181 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4182 break;
4183 }
4184 if (sreg == 0)
4185 {
4186 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4187 break;
4188 }
4189 used_at = 1;
4190 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4191 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4192 break;
4193
4194 case M_BGTL_I:
4195 likely = 1;
4196 case M_BGT_I:
4197 /* check for > max integer */
4198 maxnum = 0x7fffffff;
4199 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4200 {
4201 maxnum <<= 16;
4202 maxnum |= 0xffff;
4203 maxnum <<= 16;
4204 maxnum |= 0xffff;
4205 }
4206 if (imm_expr.X_op == O_constant
4207 && imm_expr.X_add_number >= maxnum
4208 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4209 {
4210 do_false:
4211 /* result is always false */
4212 if (! likely)
4213 macro_build (NULL, "nop", "", 0);
4214 else
4215 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4216 break;
4217 }
4218 if (imm_expr.X_op != O_constant)
4219 as_bad (_("Unsupported large constant"));
4220 ++imm_expr.X_add_number;
4221 /* FALLTHROUGH */
4222 case M_BGE_I:
4223 case M_BGEL_I:
4224 if (mask == M_BGEL_I)
4225 likely = 1;
4226 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4227 {
4228 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4229 break;
4230 }
4231 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4232 {
4233 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4234 break;
4235 }
4236 maxnum = 0x7fffffff;
4237 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4238 {
4239 maxnum <<= 16;
4240 maxnum |= 0xffff;
4241 maxnum <<= 16;
4242 maxnum |= 0xffff;
4243 }
4244 maxnum = - maxnum - 1;
4245 if (imm_expr.X_op == O_constant
4246 && imm_expr.X_add_number <= maxnum
4247 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4248 {
4249 do_true:
4250 /* result is always true */
4251 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4252 macro_build (&offset_expr, "b", "p");
4253 break;
4254 }
4255 used_at = 1;
4256 set_at (sreg, 0);
4257 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4258 break;
4259
4260 case M_BGEUL:
4261 likely = 1;
4262 case M_BGEU:
4263 if (treg == 0)
4264 goto do_true;
4265 if (sreg == 0)
4266 {
4267 macro_build (&offset_expr, likely ? "beql" : "beq",
4268 "s,t,p", 0, treg);
4269 break;
4270 }
4271 used_at = 1;
4272 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4273 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4274 break;
4275
4276 case M_BGTUL_I:
4277 likely = 1;
4278 case M_BGTU_I:
4279 if (sreg == 0
4280 || (HAVE_32BIT_GPRS
4281 && imm_expr.X_op == O_constant
4282 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4283 goto do_false;
4284 if (imm_expr.X_op != O_constant)
4285 as_bad (_("Unsupported large constant"));
4286 ++imm_expr.X_add_number;
4287 /* FALLTHROUGH */
4288 case M_BGEU_I:
4289 case M_BGEUL_I:
4290 if (mask == M_BGEUL_I)
4291 likely = 1;
4292 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4293 goto do_true;
4294 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4295 {
4296 macro_build (&offset_expr, likely ? "bnel" : "bne",
4297 "s,t,p", sreg, 0);
4298 break;
4299 }
4300 used_at = 1;
4301 set_at (sreg, 1);
4302 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4303 break;
4304
4305 case M_BGTL:
4306 likely = 1;
4307 case M_BGT:
4308 if (treg == 0)
4309 {
4310 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4311 break;
4312 }
4313 if (sreg == 0)
4314 {
4315 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4316 break;
4317 }
4318 used_at = 1;
4319 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4320 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4321 break;
4322
4323 case M_BGTUL:
4324 likely = 1;
4325 case M_BGTU:
4326 if (treg == 0)
4327 {
4328 macro_build (&offset_expr, likely ? "bnel" : "bne",
4329 "s,t,p", sreg, 0);
4330 break;
4331 }
4332 if (sreg == 0)
4333 goto do_false;
4334 used_at = 1;
4335 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4336 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4337 break;
4338
4339 case M_BLEL:
4340 likely = 1;
4341 case M_BLE:
4342 if (treg == 0)
4343 {
4344 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4345 break;
4346 }
4347 if (sreg == 0)
4348 {
4349 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4350 break;
4351 }
4352 used_at = 1;
4353 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4354 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4355 break;
4356
4357 case M_BLEL_I:
4358 likely = 1;
4359 case M_BLE_I:
4360 maxnum = 0x7fffffff;
4361 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4362 {
4363 maxnum <<= 16;
4364 maxnum |= 0xffff;
4365 maxnum <<= 16;
4366 maxnum |= 0xffff;
4367 }
4368 if (imm_expr.X_op == O_constant
4369 && imm_expr.X_add_number >= maxnum
4370 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4371 goto do_true;
4372 if (imm_expr.X_op != O_constant)
4373 as_bad (_("Unsupported large constant"));
4374 ++imm_expr.X_add_number;
4375 /* FALLTHROUGH */
4376 case M_BLT_I:
4377 case M_BLTL_I:
4378 if (mask == M_BLTL_I)
4379 likely = 1;
4380 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4381 {
4382 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4383 break;
4384 }
4385 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4386 {
4387 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4388 break;
4389 }
4390 used_at = 1;
4391 set_at (sreg, 0);
4392 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4393 break;
4394
4395 case M_BLEUL:
4396 likely = 1;
4397 case M_BLEU:
4398 if (treg == 0)
4399 {
4400 macro_build (&offset_expr, likely ? "beql" : "beq",
4401 "s,t,p", sreg, 0);
4402 break;
4403 }
4404 if (sreg == 0)
4405 goto do_true;
4406 used_at = 1;
4407 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4408 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4409 break;
4410
4411 case M_BLEUL_I:
4412 likely = 1;
4413 case M_BLEU_I:
4414 if (sreg == 0
4415 || (HAVE_32BIT_GPRS
4416 && imm_expr.X_op == O_constant
4417 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4418 goto do_true;
4419 if (imm_expr.X_op != O_constant)
4420 as_bad (_("Unsupported large constant"));
4421 ++imm_expr.X_add_number;
4422 /* FALLTHROUGH */
4423 case M_BLTU_I:
4424 case M_BLTUL_I:
4425 if (mask == M_BLTUL_I)
4426 likely = 1;
4427 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4428 goto do_false;
4429 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4430 {
4431 macro_build (&offset_expr, likely ? "beql" : "beq",
4432 "s,t,p", sreg, 0);
4433 break;
4434 }
4435 used_at = 1;
4436 set_at (sreg, 1);
4437 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4438 break;
4439
4440 case M_BLTL:
4441 likely = 1;
4442 case M_BLT:
4443 if (treg == 0)
4444 {
4445 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4446 break;
4447 }
4448 if (sreg == 0)
4449 {
4450 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4451 break;
4452 }
4453 used_at = 1;
4454 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4455 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4456 break;
4457
4458 case M_BLTUL:
4459 likely = 1;
4460 case M_BLTU:
4461 if (treg == 0)
4462 goto do_false;
4463 if (sreg == 0)
4464 {
4465 macro_build (&offset_expr, likely ? "bnel" : "bne",
4466 "s,t,p", 0, treg);
4467 break;
4468 }
4469 used_at = 1;
4470 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4471 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4472 break;
4473
4474 case M_DEXT:
4475 {
4476 unsigned long pos;
4477 unsigned long size;
4478
4479 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4480 {
4481 as_bad (_("Unsupported large constant"));
4482 pos = size = 1;
4483 }
4484 else
4485 {
4486 pos = (unsigned long) imm_expr.X_add_number;
4487 size = (unsigned long) imm2_expr.X_add_number;
4488 }
4489
4490 if (pos > 63)
4491 {
4492 as_bad (_("Improper position (%lu)"), pos);
4493 pos = 1;
4494 }
4495 if (size == 0 || size > 64
4496 || (pos + size - 1) > 63)
4497 {
4498 as_bad (_("Improper extract size (%lu, position %lu)"),
4499 size, pos);
4500 size = 1;
4501 }
4502
4503 if (size <= 32 && pos < 32)
4504 {
4505 s = "dext";
4506 fmt = "t,r,+A,+C";
4507 }
4508 else if (size <= 32)
4509 {
4510 s = "dextu";
4511 fmt = "t,r,+E,+H";
4512 }
4513 else
4514 {
4515 s = "dextm";
4516 fmt = "t,r,+A,+G";
4517 }
4518 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4519 }
4520 break;
4521
4522 case M_DINS:
4523 {
4524 unsigned long pos;
4525 unsigned long size;
4526
4527 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4528 {
4529 as_bad (_("Unsupported large constant"));
4530 pos = size = 1;
4531 }
4532 else
4533 {
4534 pos = (unsigned long) imm_expr.X_add_number;
4535 size = (unsigned long) imm2_expr.X_add_number;
4536 }
4537
4538 if (pos > 63)
4539 {
4540 as_bad (_("Improper position (%lu)"), pos);
4541 pos = 1;
4542 }
4543 if (size == 0 || size > 64
4544 || (pos + size - 1) > 63)
4545 {
4546 as_bad (_("Improper insert size (%lu, position %lu)"),
4547 size, pos);
4548 size = 1;
4549 }
4550
4551 if (pos < 32 && (pos + size - 1) < 32)
4552 {
4553 s = "dins";
4554 fmt = "t,r,+A,+B";
4555 }
4556 else if (pos >= 32)
4557 {
4558 s = "dinsu";
4559 fmt = "t,r,+E,+F";
4560 }
4561 else
4562 {
4563 s = "dinsm";
4564 fmt = "t,r,+A,+F";
4565 }
4566 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4567 pos + size - 1);
4568 }
4569 break;
4570
4571 case M_DDIV_3:
4572 dbl = 1;
4573 case M_DIV_3:
4574 s = "mflo";
4575 goto do_div3;
4576 case M_DREM_3:
4577 dbl = 1;
4578 case M_REM_3:
4579 s = "mfhi";
4580 do_div3:
4581 if (treg == 0)
4582 {
4583 as_warn (_("Divide by zero."));
4584 if (mips_trap)
4585 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4586 else
4587 macro_build (NULL, "break", "c", 7);
4588 break;
4589 }
4590
4591 start_noreorder ();
4592 if (mips_trap)
4593 {
4594 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4595 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4596 }
4597 else
4598 {
4599 expr1.X_add_number = 8;
4600 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4601 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4602 macro_build (NULL, "break", "c", 7);
4603 }
4604 expr1.X_add_number = -1;
4605 used_at = 1;
4606 load_register (AT, &expr1, dbl);
4607 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4608 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4609 if (dbl)
4610 {
4611 expr1.X_add_number = 1;
4612 load_register (AT, &expr1, dbl);
4613 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4614 }
4615 else
4616 {
4617 expr1.X_add_number = 0x80000000;
4618 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4619 }
4620 if (mips_trap)
4621 {
4622 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4623 /* We want to close the noreorder block as soon as possible, so
4624 that later insns are available for delay slot filling. */
4625 end_noreorder ();
4626 }
4627 else
4628 {
4629 expr1.X_add_number = 8;
4630 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4631 macro_build (NULL, "nop", "", 0);
4632
4633 /* We want to close the noreorder block as soon as possible, so
4634 that later insns are available for delay slot filling. */
4635 end_noreorder ();
4636
4637 macro_build (NULL, "break", "c", 6);
4638 }
4639 macro_build (NULL, s, "d", dreg);
4640 break;
4641
4642 case M_DIV_3I:
4643 s = "div";
4644 s2 = "mflo";
4645 goto do_divi;
4646 case M_DIVU_3I:
4647 s = "divu";
4648 s2 = "mflo";
4649 goto do_divi;
4650 case M_REM_3I:
4651 s = "div";
4652 s2 = "mfhi";
4653 goto do_divi;
4654 case M_REMU_3I:
4655 s = "divu";
4656 s2 = "mfhi";
4657 goto do_divi;
4658 case M_DDIV_3I:
4659 dbl = 1;
4660 s = "ddiv";
4661 s2 = "mflo";
4662 goto do_divi;
4663 case M_DDIVU_3I:
4664 dbl = 1;
4665 s = "ddivu";
4666 s2 = "mflo";
4667 goto do_divi;
4668 case M_DREM_3I:
4669 dbl = 1;
4670 s = "ddiv";
4671 s2 = "mfhi";
4672 goto do_divi;
4673 case M_DREMU_3I:
4674 dbl = 1;
4675 s = "ddivu";
4676 s2 = "mfhi";
4677 do_divi:
4678 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4679 {
4680 as_warn (_("Divide by zero."));
4681 if (mips_trap)
4682 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4683 else
4684 macro_build (NULL, "break", "c", 7);
4685 break;
4686 }
4687 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4688 {
4689 if (strcmp (s2, "mflo") == 0)
4690 move_register (dreg, sreg);
4691 else
4692 move_register (dreg, 0);
4693 break;
4694 }
4695 if (imm_expr.X_op == O_constant
4696 && imm_expr.X_add_number == -1
4697 && s[strlen (s) - 1] != 'u')
4698 {
4699 if (strcmp (s2, "mflo") == 0)
4700 {
4701 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4702 }
4703 else
4704 move_register (dreg, 0);
4705 break;
4706 }
4707
4708 used_at = 1;
4709 load_register (AT, &imm_expr, dbl);
4710 macro_build (NULL, s, "z,s,t", sreg, AT);
4711 macro_build (NULL, s2, "d", dreg);
4712 break;
4713
4714 case M_DIVU_3:
4715 s = "divu";
4716 s2 = "mflo";
4717 goto do_divu3;
4718 case M_REMU_3:
4719 s = "divu";
4720 s2 = "mfhi";
4721 goto do_divu3;
4722 case M_DDIVU_3:
4723 s = "ddivu";
4724 s2 = "mflo";
4725 goto do_divu3;
4726 case M_DREMU_3:
4727 s = "ddivu";
4728 s2 = "mfhi";
4729 do_divu3:
4730 start_noreorder ();
4731 if (mips_trap)
4732 {
4733 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4734 macro_build (NULL, s, "z,s,t", sreg, treg);
4735 /* We want to close the noreorder block as soon as possible, so
4736 that later insns are available for delay slot filling. */
4737 end_noreorder ();
4738 }
4739 else
4740 {
4741 expr1.X_add_number = 8;
4742 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4743 macro_build (NULL, s, "z,s,t", sreg, treg);
4744
4745 /* We want to close the noreorder block as soon as possible, so
4746 that later insns are available for delay slot filling. */
4747 end_noreorder ();
4748 macro_build (NULL, "break", "c", 7);
4749 }
4750 macro_build (NULL, s2, "d", dreg);
4751 break;
4752
4753 case M_DLCA_AB:
4754 dbl = 1;
4755 case M_LCA_AB:
4756 call = 1;
4757 goto do_la;
4758 case M_DLA_AB:
4759 dbl = 1;
4760 case M_LA_AB:
4761 do_la:
4762 /* Load the address of a symbol into a register. If breg is not
4763 zero, we then add a base register to it. */
4764
4765 if (dbl && HAVE_32BIT_GPRS)
4766 as_warn (_("dla used to load 32-bit register"));
4767
4768 if (! dbl && HAVE_64BIT_OBJECTS)
4769 as_warn (_("la used to load 64-bit address"));
4770
4771 if (offset_expr.X_op == O_constant
4772 && offset_expr.X_add_number >= -0x8000
4773 && offset_expr.X_add_number < 0x8000)
4774 {
4775 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4776 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4777 break;
4778 }
4779
4780 if (!mips_opts.noat && (treg == breg))
4781 {
4782 tempreg = AT;
4783 used_at = 1;
4784 }
4785 else
4786 {
4787 tempreg = treg;
4788 }
4789
4790 if (offset_expr.X_op != O_symbol
4791 && offset_expr.X_op != O_constant)
4792 {
4793 as_bad (_("expression too complex"));
4794 offset_expr.X_op = O_constant;
4795 }
4796
4797 if (offset_expr.X_op == O_constant)
4798 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4799 else if (mips_pic == NO_PIC)
4800 {
4801 /* If this is a reference to a GP relative symbol, we want
4802 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4803 Otherwise we want
4804 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4805 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4806 If we have a constant, we need two instructions anyhow,
4807 so we may as well always use the latter form.
4808
4809 With 64bit address space and a usable $at we want
4810 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4811 lui $at,<sym> (BFD_RELOC_HI16_S)
4812 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4813 daddiu $at,<sym> (BFD_RELOC_LO16)
4814 dsll32 $tempreg,0
4815 daddu $tempreg,$tempreg,$at
4816
4817 If $at is already in use, we use a path which is suboptimal
4818 on superscalar processors.
4819 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4820 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4821 dsll $tempreg,16
4822 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4823 dsll $tempreg,16
4824 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4825
4826 For GP relative symbols in 64bit address space we can use
4827 the same sequence as in 32bit address space. */
4828 if (HAVE_64BIT_SYMBOLS)
4829 {
4830 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4831 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4832 {
4833 relax_start (offset_expr.X_add_symbol);
4834 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4835 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4836 relax_switch ();
4837 }
4838
4839 if (used_at == 0 && !mips_opts.noat)
4840 {
4841 macro_build (&offset_expr, "lui", "t,u",
4842 tempreg, BFD_RELOC_MIPS_HIGHEST);
4843 macro_build (&offset_expr, "lui", "t,u",
4844 AT, BFD_RELOC_HI16_S);
4845 macro_build (&offset_expr, "daddiu", "t,r,j",
4846 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4847 macro_build (&offset_expr, "daddiu", "t,r,j",
4848 AT, AT, BFD_RELOC_LO16);
4849 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4850 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4851 used_at = 1;
4852 }
4853 else
4854 {
4855 macro_build (&offset_expr, "lui", "t,u",
4856 tempreg, BFD_RELOC_MIPS_HIGHEST);
4857 macro_build (&offset_expr, "daddiu", "t,r,j",
4858 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4859 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4860 macro_build (&offset_expr, "daddiu", "t,r,j",
4861 tempreg, tempreg, BFD_RELOC_HI16_S);
4862 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4863 macro_build (&offset_expr, "daddiu", "t,r,j",
4864 tempreg, tempreg, BFD_RELOC_LO16);
4865 }
4866
4867 if (mips_relax.sequence)
4868 relax_end ();
4869 }
4870 else
4871 {
4872 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4873 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4874 {
4875 relax_start (offset_expr.X_add_symbol);
4876 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4877 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4878 relax_switch ();
4879 }
4880 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
4881 as_bad (_("offset too large"));
4882 macro_build_lui (&offset_expr, tempreg);
4883 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4884 tempreg, tempreg, BFD_RELOC_LO16);
4885 if (mips_relax.sequence)
4886 relax_end ();
4887 }
4888 }
4889 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4890 {
4891 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4892
4893 /* If this is a reference to an external symbol, and there
4894 is no constant, we want
4895 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4896 or for lca or if tempreg is PIC_CALL_REG
4897 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4898 For a local symbol, we want
4899 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4900 nop
4901 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4902
4903 If we have a small constant, and this is a reference to
4904 an external symbol, we want
4905 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4906 nop
4907 addiu $tempreg,$tempreg,<constant>
4908 For a local symbol, we want the same instruction
4909 sequence, but we output a BFD_RELOC_LO16 reloc on the
4910 addiu instruction.
4911
4912 If we have a large constant, and this is a reference to
4913 an external symbol, we want
4914 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4915 lui $at,<hiconstant>
4916 addiu $at,$at,<loconstant>
4917 addu $tempreg,$tempreg,$at
4918 For a local symbol, we want the same instruction
4919 sequence, but we output a BFD_RELOC_LO16 reloc on the
4920 addiu instruction.
4921 */
4922
4923 if (offset_expr.X_add_number == 0)
4924 {
4925 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
4926 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4927
4928 relax_start (offset_expr.X_add_symbol);
4929 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
4930 lw_reloc_type, mips_gp_register);
4931 if (breg != 0)
4932 {
4933 /* We're going to put in an addu instruction using
4934 tempreg, so we may as well insert the nop right
4935 now. */
4936 load_delay_nop ();
4937 }
4938 relax_switch ();
4939 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4940 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
4941 load_delay_nop ();
4942 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4943 tempreg, tempreg, BFD_RELOC_LO16);
4944 relax_end ();
4945 /* FIXME: If breg == 0, and the next instruction uses
4946 $tempreg, then if this variant case is used an extra
4947 nop will be generated. */
4948 }
4949 else if (offset_expr.X_add_number >= -0x8000
4950 && offset_expr.X_add_number < 0x8000)
4951 {
4952 load_got_offset (tempreg, &offset_expr);
4953 load_delay_nop ();
4954 add_got_offset (tempreg, &offset_expr);
4955 }
4956 else
4957 {
4958 expr1.X_add_number = offset_expr.X_add_number;
4959 offset_expr.X_add_number =
4960 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
4961 load_got_offset (tempreg, &offset_expr);
4962 offset_expr.X_add_number = expr1.X_add_number;
4963 /* If we are going to add in a base register, and the
4964 target register and the base register are the same,
4965 then we are using AT as a temporary register. Since
4966 we want to load the constant into AT, we add our
4967 current AT (from the global offset table) and the
4968 register into the register now, and pretend we were
4969 not using a base register. */
4970 if (breg == treg)
4971 {
4972 load_delay_nop ();
4973 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4974 treg, AT, breg);
4975 breg = 0;
4976 tempreg = treg;
4977 }
4978 add_got_offset_hilo (tempreg, &offset_expr, AT);
4979 used_at = 1;
4980 }
4981 }
4982 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
4983 {
4984 int add_breg_early = 0;
4985
4986 /* If this is a reference to an external, and there is no
4987 constant, or local symbol (*), with or without a
4988 constant, we want
4989 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4990 or for lca or if tempreg is PIC_CALL_REG
4991 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4992
4993 If we have a small constant, and this is a reference to
4994 an external symbol, we want
4995 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4996 addiu $tempreg,$tempreg,<constant>
4997
4998 If we have a large constant, and this is a reference to
4999 an external symbol, we want
5000 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5001 lui $at,<hiconstant>
5002 addiu $at,$at,<loconstant>
5003 addu $tempreg,$tempreg,$at
5004
5005 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5006 local symbols, even though it introduces an additional
5007 instruction. */
5008
5009 if (offset_expr.X_add_number)
5010 {
5011 expr1.X_add_number = offset_expr.X_add_number;
5012 offset_expr.X_add_number = 0;
5013
5014 relax_start (offset_expr.X_add_symbol);
5015 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5016 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5017
5018 if (expr1.X_add_number >= -0x8000
5019 && expr1.X_add_number < 0x8000)
5020 {
5021 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5022 tempreg, tempreg, BFD_RELOC_LO16);
5023 }
5024 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5025 {
5026 int dreg;
5027
5028 /* If we are going to add in a base register, and the
5029 target register and the base register are the same,
5030 then we are using AT as a temporary register. Since
5031 we want to load the constant into AT, we add our
5032 current AT (from the global offset table) and the
5033 register into the register now, and pretend we were
5034 not using a base register. */
5035 if (breg != treg)
5036 dreg = tempreg;
5037 else
5038 {
5039 assert (tempreg == AT);
5040 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5041 treg, AT, breg);
5042 dreg = treg;
5043 add_breg_early = 1;
5044 }
5045
5046 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5047 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5048 dreg, dreg, AT);
5049
5050 used_at = 1;
5051 }
5052 else
5053 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5054
5055 relax_switch ();
5056 offset_expr.X_add_number = expr1.X_add_number;
5057
5058 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5059 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5060 if (add_breg_early)
5061 {
5062 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5063 treg, tempreg, breg);
5064 breg = 0;
5065 tempreg = treg;
5066 }
5067 relax_end ();
5068 }
5069 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5070 {
5071 relax_start (offset_expr.X_add_symbol);
5072 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5073 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5074 relax_switch ();
5075 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5076 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5077 relax_end ();
5078 }
5079 else
5080 {
5081 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5082 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5083 }
5084 }
5085 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5086 {
5087 int gpdelay;
5088 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5089 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5090 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5091
5092 /* This is the large GOT case. If this is a reference to an
5093 external symbol, and there is no constant, we want
5094 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5095 addu $tempreg,$tempreg,$gp
5096 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5097 or for lca or if tempreg is PIC_CALL_REG
5098 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5099 addu $tempreg,$tempreg,$gp
5100 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5101 For a local symbol, we want
5102 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5103 nop
5104 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5105
5106 If we have a small constant, and this is a reference to
5107 an external symbol, we want
5108 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5109 addu $tempreg,$tempreg,$gp
5110 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5111 nop
5112 addiu $tempreg,$tempreg,<constant>
5113 For a local symbol, we want
5114 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5115 nop
5116 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5117
5118 If we have a large constant, and this is a reference to
5119 an external symbol, we want
5120 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5121 addu $tempreg,$tempreg,$gp
5122 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5123 lui $at,<hiconstant>
5124 addiu $at,$at,<loconstant>
5125 addu $tempreg,$tempreg,$at
5126 For a local symbol, we want
5127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5128 lui $at,<hiconstant>
5129 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5130 addu $tempreg,$tempreg,$at
5131 */
5132
5133 expr1.X_add_number = offset_expr.X_add_number;
5134 offset_expr.X_add_number = 0;
5135 relax_start (offset_expr.X_add_symbol);
5136 gpdelay = reg_needs_delay (mips_gp_register);
5137 if (expr1.X_add_number == 0 && breg == 0
5138 && (call || tempreg == PIC_CALL_REG))
5139 {
5140 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5141 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5142 }
5143 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5144 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5145 tempreg, tempreg, mips_gp_register);
5146 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5147 tempreg, lw_reloc_type, tempreg);
5148 if (expr1.X_add_number == 0)
5149 {
5150 if (breg != 0)
5151 {
5152 /* We're going to put in an addu instruction using
5153 tempreg, so we may as well insert the nop right
5154 now. */
5155 load_delay_nop ();
5156 }
5157 }
5158 else if (expr1.X_add_number >= -0x8000
5159 && expr1.X_add_number < 0x8000)
5160 {
5161 load_delay_nop ();
5162 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5163 tempreg, tempreg, BFD_RELOC_LO16);
5164 }
5165 else
5166 {
5167 int dreg;
5168
5169 /* If we are going to add in a base register, and the
5170 target register and the base register are the same,
5171 then we are using AT as a temporary register. Since
5172 we want to load the constant into AT, we add our
5173 current AT (from the global offset table) and the
5174 register into the register now, and pretend we were
5175 not using a base register. */
5176 if (breg != treg)
5177 dreg = tempreg;
5178 else
5179 {
5180 assert (tempreg == AT);
5181 load_delay_nop ();
5182 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5183 treg, AT, breg);
5184 dreg = treg;
5185 }
5186
5187 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5188 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5189
5190 used_at = 1;
5191 }
5192 offset_expr.X_add_number =
5193 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5194 relax_switch ();
5195
5196 if (gpdelay)
5197 {
5198 /* This is needed because this instruction uses $gp, but
5199 the first instruction on the main stream does not. */
5200 macro_build (NULL, "nop", "");
5201 }
5202
5203 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5204 local_reloc_type, mips_gp_register);
5205 if (expr1.X_add_number >= -0x8000
5206 && expr1.X_add_number < 0x8000)
5207 {
5208 load_delay_nop ();
5209 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5210 tempreg, tempreg, BFD_RELOC_LO16);
5211 /* FIXME: If add_number is 0, and there was no base
5212 register, the external symbol case ended with a load,
5213 so if the symbol turns out to not be external, and
5214 the next instruction uses tempreg, an unnecessary nop
5215 will be inserted. */
5216 }
5217 else
5218 {
5219 if (breg == treg)
5220 {
5221 /* We must add in the base register now, as in the
5222 external symbol case. */
5223 assert (tempreg == AT);
5224 load_delay_nop ();
5225 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5226 treg, AT, breg);
5227 tempreg = treg;
5228 /* We set breg to 0 because we have arranged to add
5229 it in in both cases. */
5230 breg = 0;
5231 }
5232
5233 macro_build_lui (&expr1, AT);
5234 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5235 AT, AT, BFD_RELOC_LO16);
5236 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5237 tempreg, tempreg, AT);
5238 used_at = 1;
5239 }
5240 relax_end ();
5241 }
5242 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5243 {
5244 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5245 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5246 int add_breg_early = 0;
5247
5248 /* This is the large GOT case. If this is a reference to an
5249 external symbol, and there is no constant, we want
5250 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5251 add $tempreg,$tempreg,$gp
5252 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5253 or for lca or if tempreg is PIC_CALL_REG
5254 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5255 add $tempreg,$tempreg,$gp
5256 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5257
5258 If we have a small constant, and this is a reference to
5259 an external symbol, we want
5260 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5261 add $tempreg,$tempreg,$gp
5262 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5263 addi $tempreg,$tempreg,<constant>
5264
5265 If we have a large constant, and this is a reference to
5266 an external symbol, we want
5267 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5268 addu $tempreg,$tempreg,$gp
5269 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5270 lui $at,<hiconstant>
5271 addi $at,$at,<loconstant>
5272 add $tempreg,$tempreg,$at
5273
5274 If we have NewABI, and we know it's a local symbol, we want
5275 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5276 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5277 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5278
5279 relax_start (offset_expr.X_add_symbol);
5280
5281 expr1.X_add_number = offset_expr.X_add_number;
5282 offset_expr.X_add_number = 0;
5283
5284 if (expr1.X_add_number == 0 && breg == 0
5285 && (call || tempreg == PIC_CALL_REG))
5286 {
5287 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5288 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5289 }
5290 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5291 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5292 tempreg, tempreg, mips_gp_register);
5293 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5294 tempreg, lw_reloc_type, tempreg);
5295
5296 if (expr1.X_add_number == 0)
5297 ;
5298 else if (expr1.X_add_number >= -0x8000
5299 && expr1.X_add_number < 0x8000)
5300 {
5301 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5302 tempreg, tempreg, BFD_RELOC_LO16);
5303 }
5304 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5305 {
5306 int dreg;
5307
5308 /* If we are going to add in a base register, and the
5309 target register and the base register are the same,
5310 then we are using AT as a temporary register. Since
5311 we want to load the constant into AT, we add our
5312 current AT (from the global offset table) and the
5313 register into the register now, and pretend we were
5314 not using a base register. */
5315 if (breg != treg)
5316 dreg = tempreg;
5317 else
5318 {
5319 assert (tempreg == AT);
5320 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5321 treg, AT, breg);
5322 dreg = treg;
5323 add_breg_early = 1;
5324 }
5325
5326 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5327 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5328
5329 used_at = 1;
5330 }
5331 else
5332 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5333
5334 relax_switch ();
5335 offset_expr.X_add_number = expr1.X_add_number;
5336 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5337 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5338 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5339 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5340 if (add_breg_early)
5341 {
5342 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5343 treg, tempreg, breg);
5344 breg = 0;
5345 tempreg = treg;
5346 }
5347 relax_end ();
5348 }
5349 else
5350 abort ();
5351
5352 if (breg != 0)
5353 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5354 break;
5355
5356 case M_J_A:
5357 /* The j instruction may not be used in PIC code, since it
5358 requires an absolute address. We convert it to a b
5359 instruction. */
5360 if (mips_pic == NO_PIC)
5361 macro_build (&offset_expr, "j", "a");
5362 else
5363 macro_build (&offset_expr, "b", "p");
5364 break;
5365
5366 /* The jal instructions must be handled as macros because when
5367 generating PIC code they expand to multi-instruction
5368 sequences. Normally they are simple instructions. */
5369 case M_JAL_1:
5370 dreg = RA;
5371 /* Fall through. */
5372 case M_JAL_2:
5373 if (mips_pic == NO_PIC)
5374 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5375 else if (mips_pic == SVR4_PIC)
5376 {
5377 if (sreg != PIC_CALL_REG)
5378 as_warn (_("MIPS PIC call to register other than $25"));
5379
5380 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5381 if (! HAVE_NEWABI)
5382 {
5383 if (mips_cprestore_offset < 0)
5384 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5385 else
5386 {
5387 if (! mips_frame_reg_valid)
5388 {
5389 as_warn (_("No .frame pseudo-op used in PIC code"));
5390 /* Quiet this warning. */
5391 mips_frame_reg_valid = 1;
5392 }
5393 if (! mips_cprestore_valid)
5394 {
5395 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5396 /* Quiet this warning. */
5397 mips_cprestore_valid = 1;
5398 }
5399 expr1.X_add_number = mips_cprestore_offset;
5400 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5401 mips_gp_register,
5402 mips_frame_reg,
5403 HAVE_64BIT_ADDRESSES);
5404 }
5405 }
5406 }
5407 else
5408 abort ();
5409
5410 break;
5411
5412 case M_JAL_A:
5413 if (mips_pic == NO_PIC)
5414 macro_build (&offset_expr, "jal", "a");
5415 else if (mips_pic == SVR4_PIC)
5416 {
5417 /* If this is a reference to an external symbol, and we are
5418 using a small GOT, we want
5419 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5420 nop
5421 jalr $ra,$25
5422 nop
5423 lw $gp,cprestore($sp)
5424 The cprestore value is set using the .cprestore
5425 pseudo-op. If we are using a big GOT, we want
5426 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5427 addu $25,$25,$gp
5428 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5429 nop
5430 jalr $ra,$25
5431 nop
5432 lw $gp,cprestore($sp)
5433 If the symbol is not external, we want
5434 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5435 nop
5436 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5437 jalr $ra,$25
5438 nop
5439 lw $gp,cprestore($sp)
5440
5441 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5442 sequences above, minus nops, unless the symbol is local,
5443 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5444 GOT_DISP. */
5445 if (HAVE_NEWABI)
5446 {
5447 if (! mips_big_got)
5448 {
5449 relax_start (offset_expr.X_add_symbol);
5450 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5451 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5452 mips_gp_register);
5453 relax_switch ();
5454 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5455 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5456 mips_gp_register);
5457 relax_end ();
5458 }
5459 else
5460 {
5461 relax_start (offset_expr.X_add_symbol);
5462 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5463 BFD_RELOC_MIPS_CALL_HI16);
5464 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5465 PIC_CALL_REG, mips_gp_register);
5466 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5467 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5468 PIC_CALL_REG);
5469 relax_switch ();
5470 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5471 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5472 mips_gp_register);
5473 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5474 PIC_CALL_REG, PIC_CALL_REG,
5475 BFD_RELOC_MIPS_GOT_OFST);
5476 relax_end ();
5477 }
5478
5479 macro_build_jalr (&offset_expr);
5480 }
5481 else
5482 {
5483 relax_start (offset_expr.X_add_symbol);
5484 if (! mips_big_got)
5485 {
5486 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5487 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5488 mips_gp_register);
5489 load_delay_nop ();
5490 relax_switch ();
5491 }
5492 else
5493 {
5494 int gpdelay;
5495
5496 gpdelay = reg_needs_delay (mips_gp_register);
5497 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5498 BFD_RELOC_MIPS_CALL_HI16);
5499 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5500 PIC_CALL_REG, mips_gp_register);
5501 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5502 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5503 PIC_CALL_REG);
5504 load_delay_nop ();
5505 relax_switch ();
5506 if (gpdelay)
5507 macro_build (NULL, "nop", "");
5508 }
5509 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5510 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5511 mips_gp_register);
5512 load_delay_nop ();
5513 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5514 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5515 relax_end ();
5516 macro_build_jalr (&offset_expr);
5517
5518 if (mips_cprestore_offset < 0)
5519 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5520 else
5521 {
5522 if (! mips_frame_reg_valid)
5523 {
5524 as_warn (_("No .frame pseudo-op used in PIC code"));
5525 /* Quiet this warning. */
5526 mips_frame_reg_valid = 1;
5527 }
5528 if (! mips_cprestore_valid)
5529 {
5530 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5531 /* Quiet this warning. */
5532 mips_cprestore_valid = 1;
5533 }
5534 if (mips_opts.noreorder)
5535 macro_build (NULL, "nop", "");
5536 expr1.X_add_number = mips_cprestore_offset;
5537 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5538 mips_gp_register,
5539 mips_frame_reg,
5540 HAVE_64BIT_ADDRESSES);
5541 }
5542 }
5543 }
5544 else
5545 abort ();
5546
5547 break;
5548
5549 case M_LB_AB:
5550 s = "lb";
5551 goto ld;
5552 case M_LBU_AB:
5553 s = "lbu";
5554 goto ld;
5555 case M_LH_AB:
5556 s = "lh";
5557 goto ld;
5558 case M_LHU_AB:
5559 s = "lhu";
5560 goto ld;
5561 case M_LW_AB:
5562 s = "lw";
5563 goto ld;
5564 case M_LWC0_AB:
5565 s = "lwc0";
5566 /* Itbl support may require additional care here. */
5567 coproc = 1;
5568 goto ld;
5569 case M_LWC1_AB:
5570 s = "lwc1";
5571 /* Itbl support may require additional care here. */
5572 coproc = 1;
5573 goto ld;
5574 case M_LWC2_AB:
5575 s = "lwc2";
5576 /* Itbl support may require additional care here. */
5577 coproc = 1;
5578 goto ld;
5579 case M_LWC3_AB:
5580 s = "lwc3";
5581 /* Itbl support may require additional care here. */
5582 coproc = 1;
5583 goto ld;
5584 case M_LWL_AB:
5585 s = "lwl";
5586 lr = 1;
5587 goto ld;
5588 case M_LWR_AB:
5589 s = "lwr";
5590 lr = 1;
5591 goto ld;
5592 case M_LDC1_AB:
5593 if (mips_opts.arch == CPU_R4650)
5594 {
5595 as_bad (_("opcode not supported on this processor"));
5596 break;
5597 }
5598 s = "ldc1";
5599 /* Itbl support may require additional care here. */
5600 coproc = 1;
5601 goto ld;
5602 case M_LDC2_AB:
5603 s = "ldc2";
5604 /* Itbl support may require additional care here. */
5605 coproc = 1;
5606 goto ld;
5607 case M_LDC3_AB:
5608 s = "ldc3";
5609 /* Itbl support may require additional care here. */
5610 coproc = 1;
5611 goto ld;
5612 case M_LDL_AB:
5613 s = "ldl";
5614 lr = 1;
5615 goto ld;
5616 case M_LDR_AB:
5617 s = "ldr";
5618 lr = 1;
5619 goto ld;
5620 case M_LL_AB:
5621 s = "ll";
5622 goto ld;
5623 case M_LLD_AB:
5624 s = "lld";
5625 goto ld;
5626 case M_LWU_AB:
5627 s = "lwu";
5628 ld:
5629 if (breg == treg || coproc || lr)
5630 {
5631 tempreg = AT;
5632 used_at = 1;
5633 }
5634 else
5635 {
5636 tempreg = treg;
5637 }
5638 goto ld_st;
5639 case M_SB_AB:
5640 s = "sb";
5641 goto st;
5642 case M_SH_AB:
5643 s = "sh";
5644 goto st;
5645 case M_SW_AB:
5646 s = "sw";
5647 goto st;
5648 case M_SWC0_AB:
5649 s = "swc0";
5650 /* Itbl support may require additional care here. */
5651 coproc = 1;
5652 goto st;
5653 case M_SWC1_AB:
5654 s = "swc1";
5655 /* Itbl support may require additional care here. */
5656 coproc = 1;
5657 goto st;
5658 case M_SWC2_AB:
5659 s = "swc2";
5660 /* Itbl support may require additional care here. */
5661 coproc = 1;
5662 goto st;
5663 case M_SWC3_AB:
5664 s = "swc3";
5665 /* Itbl support may require additional care here. */
5666 coproc = 1;
5667 goto st;
5668 case M_SWL_AB:
5669 s = "swl";
5670 goto st;
5671 case M_SWR_AB:
5672 s = "swr";
5673 goto st;
5674 case M_SC_AB:
5675 s = "sc";
5676 goto st;
5677 case M_SCD_AB:
5678 s = "scd";
5679 goto st;
5680 case M_SDC1_AB:
5681 if (mips_opts.arch == CPU_R4650)
5682 {
5683 as_bad (_("opcode not supported on this processor"));
5684 break;
5685 }
5686 s = "sdc1";
5687 coproc = 1;
5688 /* Itbl support may require additional care here. */
5689 goto st;
5690 case M_SDC2_AB:
5691 s = "sdc2";
5692 /* Itbl support may require additional care here. */
5693 coproc = 1;
5694 goto st;
5695 case M_SDC3_AB:
5696 s = "sdc3";
5697 /* Itbl support may require additional care here. */
5698 coproc = 1;
5699 goto st;
5700 case M_SDL_AB:
5701 s = "sdl";
5702 goto st;
5703 case M_SDR_AB:
5704 s = "sdr";
5705 st:
5706 tempreg = AT;
5707 used_at = 1;
5708 ld_st:
5709 /* Itbl support may require additional care here. */
5710 if (mask == M_LWC1_AB
5711 || mask == M_SWC1_AB
5712 || mask == M_LDC1_AB
5713 || mask == M_SDC1_AB
5714 || mask == M_L_DAB
5715 || mask == M_S_DAB)
5716 fmt = "T,o(b)";
5717 else if (coproc)
5718 fmt = "E,o(b)";
5719 else
5720 fmt = "t,o(b)";
5721
5722 if (offset_expr.X_op != O_constant
5723 && offset_expr.X_op != O_symbol)
5724 {
5725 as_bad (_("expression too complex"));
5726 offset_expr.X_op = O_constant;
5727 }
5728
5729 /* A constant expression in PIC code can be handled just as it
5730 is in non PIC code. */
5731 if (offset_expr.X_op == O_constant)
5732 {
5733 if (HAVE_32BIT_ADDRESSES
5734 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5735 as_bad (_("constant too large"));
5736
5737 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5738 & ~(bfd_vma) 0xffff);
5739 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5740 if (breg != 0)
5741 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5742 tempreg, tempreg, breg);
5743 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5744 }
5745 else if (mips_pic == NO_PIC)
5746 {
5747 /* If this is a reference to a GP relative symbol, and there
5748 is no base register, we want
5749 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5750 Otherwise, if there is no base register, we want
5751 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5752 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5753 If we have a constant, we need two instructions anyhow,
5754 so we always use the latter form.
5755
5756 If we have a base register, and this is a reference to a
5757 GP relative symbol, we want
5758 addu $tempreg,$breg,$gp
5759 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5760 Otherwise we want
5761 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5762 addu $tempreg,$tempreg,$breg
5763 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5764 With a constant we always use the latter case.
5765
5766 With 64bit address space and no base register and $at usable,
5767 we want
5768 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5769 lui $at,<sym> (BFD_RELOC_HI16_S)
5770 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5771 dsll32 $tempreg,0
5772 daddu $tempreg,$at
5773 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5774 If we have a base register, we want
5775 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5776 lui $at,<sym> (BFD_RELOC_HI16_S)
5777 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5778 daddu $at,$breg
5779 dsll32 $tempreg,0
5780 daddu $tempreg,$at
5781 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5782
5783 Without $at we can't generate the optimal path for superscalar
5784 processors here since this would require two temporary registers.
5785 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5786 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5787 dsll $tempreg,16
5788 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5789 dsll $tempreg,16
5790 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5791 If we have a base register, we want
5792 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5793 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5794 dsll $tempreg,16
5795 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5796 dsll $tempreg,16
5797 daddu $tempreg,$tempreg,$breg
5798 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5799
5800 For GP relative symbols in 64bit address space we can use
5801 the same sequence as in 32bit address space. */
5802 if (HAVE_64BIT_SYMBOLS)
5803 {
5804 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5805 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5806 {
5807 relax_start (offset_expr.X_add_symbol);
5808 if (breg == 0)
5809 {
5810 macro_build (&offset_expr, s, fmt, treg,
5811 BFD_RELOC_GPREL16, mips_gp_register);
5812 }
5813 else
5814 {
5815 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5816 tempreg, breg, mips_gp_register);
5817 macro_build (&offset_expr, s, fmt, treg,
5818 BFD_RELOC_GPREL16, tempreg);
5819 }
5820 relax_switch ();
5821 }
5822
5823 if (used_at == 0 && !mips_opts.noat)
5824 {
5825 macro_build (&offset_expr, "lui", "t,u", tempreg,
5826 BFD_RELOC_MIPS_HIGHEST);
5827 macro_build (&offset_expr, "lui", "t,u", AT,
5828 BFD_RELOC_HI16_S);
5829 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5830 tempreg, BFD_RELOC_MIPS_HIGHER);
5831 if (breg != 0)
5832 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5833 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5834 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5835 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5836 tempreg);
5837 used_at = 1;
5838 }
5839 else
5840 {
5841 macro_build (&offset_expr, "lui", "t,u", tempreg,
5842 BFD_RELOC_MIPS_HIGHEST);
5843 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5844 tempreg, BFD_RELOC_MIPS_HIGHER);
5845 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5846 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5847 tempreg, BFD_RELOC_HI16_S);
5848 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5849 if (breg != 0)
5850 macro_build (NULL, "daddu", "d,v,t",
5851 tempreg, tempreg, breg);
5852 macro_build (&offset_expr, s, fmt, treg,
5853 BFD_RELOC_LO16, tempreg);
5854 }
5855
5856 if (mips_relax.sequence)
5857 relax_end ();
5858 break;
5859 }
5860
5861 if (breg == 0)
5862 {
5863 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5864 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5865 {
5866 relax_start (offset_expr.X_add_symbol);
5867 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5868 mips_gp_register);
5869 relax_switch ();
5870 }
5871 macro_build_lui (&offset_expr, tempreg);
5872 macro_build (&offset_expr, s, fmt, treg,
5873 BFD_RELOC_LO16, tempreg);
5874 if (mips_relax.sequence)
5875 relax_end ();
5876 }
5877 else
5878 {
5879 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5880 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5881 {
5882 relax_start (offset_expr.X_add_symbol);
5883 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5884 tempreg, breg, mips_gp_register);
5885 macro_build (&offset_expr, s, fmt, treg,
5886 BFD_RELOC_GPREL16, tempreg);
5887 relax_switch ();
5888 }
5889 macro_build_lui (&offset_expr, tempreg);
5890 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5891 tempreg, tempreg, breg);
5892 macro_build (&offset_expr, s, fmt, treg,
5893 BFD_RELOC_LO16, tempreg);
5894 if (mips_relax.sequence)
5895 relax_end ();
5896 }
5897 }
5898 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5899 {
5900 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5901
5902 /* If this is a reference to an external symbol, we want
5903 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5904 nop
5905 <op> $treg,0($tempreg)
5906 Otherwise we want
5907 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5908 nop
5909 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5910 <op> $treg,0($tempreg)
5911
5912 For NewABI, we want
5913 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5914 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
5915
5916 If there is a base register, we add it to $tempreg before
5917 the <op>. If there is a constant, we stick it in the
5918 <op> instruction. We don't handle constants larger than
5919 16 bits, because we have no way to load the upper 16 bits
5920 (actually, we could handle them for the subset of cases
5921 in which we are not using $at). */
5922 assert (offset_expr.X_op == O_symbol);
5923 if (HAVE_NEWABI)
5924 {
5925 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5926 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5927 if (breg != 0)
5928 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5929 tempreg, tempreg, breg);
5930 macro_build (&offset_expr, s, fmt, treg,
5931 BFD_RELOC_MIPS_GOT_OFST, tempreg);
5932 break;
5933 }
5934 expr1.X_add_number = offset_expr.X_add_number;
5935 offset_expr.X_add_number = 0;
5936 if (expr1.X_add_number < -0x8000
5937 || expr1.X_add_number >= 0x8000)
5938 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5939 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5940 lw_reloc_type, mips_gp_register);
5941 load_delay_nop ();
5942 relax_start (offset_expr.X_add_symbol);
5943 relax_switch ();
5944 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5945 tempreg, BFD_RELOC_LO16);
5946 relax_end ();
5947 if (breg != 0)
5948 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5949 tempreg, tempreg, breg);
5950 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5951 }
5952 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5953 {
5954 int gpdelay;
5955
5956 /* If this is a reference to an external symbol, we want
5957 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5958 addu $tempreg,$tempreg,$gp
5959 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5960 <op> $treg,0($tempreg)
5961 Otherwise we want
5962 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5963 nop
5964 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5965 <op> $treg,0($tempreg)
5966 If there is a base register, we add it to $tempreg before
5967 the <op>. If there is a constant, we stick it in the
5968 <op> instruction. We don't handle constants larger than
5969 16 bits, because we have no way to load the upper 16 bits
5970 (actually, we could handle them for the subset of cases
5971 in which we are not using $at). */
5972 assert (offset_expr.X_op == O_symbol);
5973 expr1.X_add_number = offset_expr.X_add_number;
5974 offset_expr.X_add_number = 0;
5975 if (expr1.X_add_number < -0x8000
5976 || expr1.X_add_number >= 0x8000)
5977 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5978 gpdelay = reg_needs_delay (mips_gp_register);
5979 relax_start (offset_expr.X_add_symbol);
5980 macro_build (&offset_expr, "lui", "t,u", tempreg,
5981 BFD_RELOC_MIPS_GOT_HI16);
5982 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
5983 mips_gp_register);
5984 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5985 BFD_RELOC_MIPS_GOT_LO16, tempreg);
5986 relax_switch ();
5987 if (gpdelay)
5988 macro_build (NULL, "nop", "");
5989 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5990 BFD_RELOC_MIPS_GOT16, mips_gp_register);
5991 load_delay_nop ();
5992 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5993 tempreg, BFD_RELOC_LO16);
5994 relax_end ();
5995
5996 if (breg != 0)
5997 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5998 tempreg, tempreg, breg);
5999 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6000 }
6001 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6002 {
6003 /* If this is a reference to an external symbol, we want
6004 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6005 add $tempreg,$tempreg,$gp
6006 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6007 <op> $treg,<ofst>($tempreg)
6008 Otherwise, for local symbols, we want:
6009 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6010 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6011 assert (offset_expr.X_op == O_symbol);
6012 expr1.X_add_number = offset_expr.X_add_number;
6013 offset_expr.X_add_number = 0;
6014 if (expr1.X_add_number < -0x8000
6015 || expr1.X_add_number >= 0x8000)
6016 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6017 relax_start (offset_expr.X_add_symbol);
6018 macro_build (&offset_expr, "lui", "t,u", tempreg,
6019 BFD_RELOC_MIPS_GOT_HI16);
6020 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6021 mips_gp_register);
6022 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6023 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6024 if (breg != 0)
6025 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6026 tempreg, tempreg, breg);
6027 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6028
6029 relax_switch ();
6030 offset_expr.X_add_number = expr1.X_add_number;
6031 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6032 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6033 if (breg != 0)
6034 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6035 tempreg, tempreg, breg);
6036 macro_build (&offset_expr, s, fmt, treg,
6037 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6038 relax_end ();
6039 }
6040 else
6041 abort ();
6042
6043 break;
6044
6045 case M_LI:
6046 case M_LI_S:
6047 load_register (treg, &imm_expr, 0);
6048 break;
6049
6050 case M_DLI:
6051 load_register (treg, &imm_expr, 1);
6052 break;
6053
6054 case M_LI_SS:
6055 if (imm_expr.X_op == O_constant)
6056 {
6057 used_at = 1;
6058 load_register (AT, &imm_expr, 0);
6059 macro_build (NULL, "mtc1", "t,G", AT, treg);
6060 break;
6061 }
6062 else
6063 {
6064 assert (offset_expr.X_op == O_symbol
6065 && strcmp (segment_name (S_GET_SEGMENT
6066 (offset_expr.X_add_symbol)),
6067 ".lit4") == 0
6068 && offset_expr.X_add_number == 0);
6069 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6070 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6071 break;
6072 }
6073
6074 case M_LI_D:
6075 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6076 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6077 order 32 bits of the value and the low order 32 bits are either
6078 zero or in OFFSET_EXPR. */
6079 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6080 {
6081 if (HAVE_64BIT_GPRS)
6082 load_register (treg, &imm_expr, 1);
6083 else
6084 {
6085 int hreg, lreg;
6086
6087 if (target_big_endian)
6088 {
6089 hreg = treg;
6090 lreg = treg + 1;
6091 }
6092 else
6093 {
6094 hreg = treg + 1;
6095 lreg = treg;
6096 }
6097
6098 if (hreg <= 31)
6099 load_register (hreg, &imm_expr, 0);
6100 if (lreg <= 31)
6101 {
6102 if (offset_expr.X_op == O_absent)
6103 move_register (lreg, 0);
6104 else
6105 {
6106 assert (offset_expr.X_op == O_constant);
6107 load_register (lreg, &offset_expr, 0);
6108 }
6109 }
6110 }
6111 break;
6112 }
6113
6114 /* We know that sym is in the .rdata section. First we get the
6115 upper 16 bits of the address. */
6116 if (mips_pic == NO_PIC)
6117 {
6118 macro_build_lui (&offset_expr, AT);
6119 used_at = 1;
6120 }
6121 else if (mips_pic == SVR4_PIC)
6122 {
6123 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6124 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6125 used_at = 1;
6126 }
6127 else
6128 abort ();
6129
6130 /* Now we load the register(s). */
6131 if (HAVE_64BIT_GPRS)
6132 {
6133 used_at = 1;
6134 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6135 }
6136 else
6137 {
6138 used_at = 1;
6139 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6140 if (treg != RA)
6141 {
6142 /* FIXME: How in the world do we deal with the possible
6143 overflow here? */
6144 offset_expr.X_add_number += 4;
6145 macro_build (&offset_expr, "lw", "t,o(b)",
6146 treg + 1, BFD_RELOC_LO16, AT);
6147 }
6148 }
6149 break;
6150
6151 case M_LI_DD:
6152 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6153 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6154 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6155 the value and the low order 32 bits are either zero or in
6156 OFFSET_EXPR. */
6157 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6158 {
6159 used_at = 1;
6160 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6161 if (HAVE_64BIT_FPRS)
6162 {
6163 assert (HAVE_64BIT_GPRS);
6164 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6165 }
6166 else
6167 {
6168 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6169 if (offset_expr.X_op == O_absent)
6170 macro_build (NULL, "mtc1", "t,G", 0, treg);
6171 else
6172 {
6173 assert (offset_expr.X_op == O_constant);
6174 load_register (AT, &offset_expr, 0);
6175 macro_build (NULL, "mtc1", "t,G", AT, treg);
6176 }
6177 }
6178 break;
6179 }
6180
6181 assert (offset_expr.X_op == O_symbol
6182 && offset_expr.X_add_number == 0);
6183 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6184 if (strcmp (s, ".lit8") == 0)
6185 {
6186 if (mips_opts.isa != ISA_MIPS1)
6187 {
6188 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6189 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6190 break;
6191 }
6192 breg = mips_gp_register;
6193 r = BFD_RELOC_MIPS_LITERAL;
6194 goto dob;
6195 }
6196 else
6197 {
6198 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6199 used_at = 1;
6200 if (mips_pic == SVR4_PIC)
6201 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6202 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6203 else
6204 {
6205 /* FIXME: This won't work for a 64 bit address. */
6206 macro_build_lui (&offset_expr, AT);
6207 }
6208
6209 if (mips_opts.isa != ISA_MIPS1)
6210 {
6211 macro_build (&offset_expr, "ldc1", "T,o(b)",
6212 treg, BFD_RELOC_LO16, AT);
6213 break;
6214 }
6215 breg = AT;
6216 r = BFD_RELOC_LO16;
6217 goto dob;
6218 }
6219
6220 case M_L_DOB:
6221 if (mips_opts.arch == CPU_R4650)
6222 {
6223 as_bad (_("opcode not supported on this processor"));
6224 break;
6225 }
6226 /* Even on a big endian machine $fn comes before $fn+1. We have
6227 to adjust when loading from memory. */
6228 r = BFD_RELOC_LO16;
6229 dob:
6230 assert (mips_opts.isa == ISA_MIPS1);
6231 macro_build (&offset_expr, "lwc1", "T,o(b)",
6232 target_big_endian ? treg + 1 : treg, r, breg);
6233 /* FIXME: A possible overflow which I don't know how to deal
6234 with. */
6235 offset_expr.X_add_number += 4;
6236 macro_build (&offset_expr, "lwc1", "T,o(b)",
6237 target_big_endian ? treg : treg + 1, r, breg);
6238 break;
6239
6240 case M_L_DAB:
6241 /*
6242 * The MIPS assembler seems to check for X_add_number not
6243 * being double aligned and generating:
6244 * lui at,%hi(foo+1)
6245 * addu at,at,v1
6246 * addiu at,at,%lo(foo+1)
6247 * lwc1 f2,0(at)
6248 * lwc1 f3,4(at)
6249 * But, the resulting address is the same after relocation so why
6250 * generate the extra instruction?
6251 */
6252 if (mips_opts.arch == CPU_R4650)
6253 {
6254 as_bad (_("opcode not supported on this processor"));
6255 break;
6256 }
6257 /* Itbl support may require additional care here. */
6258 coproc = 1;
6259 if (mips_opts.isa != ISA_MIPS1)
6260 {
6261 s = "ldc1";
6262 goto ld;
6263 }
6264
6265 s = "lwc1";
6266 fmt = "T,o(b)";
6267 goto ldd_std;
6268
6269 case M_S_DAB:
6270 if (mips_opts.arch == CPU_R4650)
6271 {
6272 as_bad (_("opcode not supported on this processor"));
6273 break;
6274 }
6275
6276 if (mips_opts.isa != ISA_MIPS1)
6277 {
6278 s = "sdc1";
6279 goto st;
6280 }
6281
6282 s = "swc1";
6283 fmt = "T,o(b)";
6284 /* Itbl support may require additional care here. */
6285 coproc = 1;
6286 goto ldd_std;
6287
6288 case M_LD_AB:
6289 if (HAVE_64BIT_GPRS)
6290 {
6291 s = "ld";
6292 goto ld;
6293 }
6294
6295 s = "lw";
6296 fmt = "t,o(b)";
6297 goto ldd_std;
6298
6299 case M_SD_AB:
6300 if (HAVE_64BIT_GPRS)
6301 {
6302 s = "sd";
6303 goto st;
6304 }
6305
6306 s = "sw";
6307 fmt = "t,o(b)";
6308
6309 ldd_std:
6310 if (offset_expr.X_op != O_symbol
6311 && offset_expr.X_op != O_constant)
6312 {
6313 as_bad (_("expression too complex"));
6314 offset_expr.X_op = O_constant;
6315 }
6316
6317 /* Even on a big endian machine $fn comes before $fn+1. We have
6318 to adjust when loading from memory. We set coproc if we must
6319 load $fn+1 first. */
6320 /* Itbl support may require additional care here. */
6321 if (! target_big_endian)
6322 coproc = 0;
6323
6324 if (mips_pic == NO_PIC
6325 || offset_expr.X_op == O_constant)
6326 {
6327 /* If this is a reference to a GP relative symbol, we want
6328 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6329 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6330 If we have a base register, we use this
6331 addu $at,$breg,$gp
6332 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6333 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6334 If this is not a GP relative symbol, we want
6335 lui $at,<sym> (BFD_RELOC_HI16_S)
6336 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6337 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6338 If there is a base register, we add it to $at after the
6339 lui instruction. If there is a constant, we always use
6340 the last case. */
6341 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6342 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6343 {
6344 relax_start (offset_expr.X_add_symbol);
6345 if (breg == 0)
6346 {
6347 tempreg = mips_gp_register;
6348 }
6349 else
6350 {
6351 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6352 AT, breg, mips_gp_register);
6353 tempreg = AT;
6354 used_at = 1;
6355 }
6356
6357 /* Itbl support may require additional care here. */
6358 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6359 BFD_RELOC_GPREL16, tempreg);
6360 offset_expr.X_add_number += 4;
6361
6362 /* Set mips_optimize to 2 to avoid inserting an
6363 undesired nop. */
6364 hold_mips_optimize = mips_optimize;
6365 mips_optimize = 2;
6366 /* Itbl support may require additional care here. */
6367 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6368 BFD_RELOC_GPREL16, tempreg);
6369 mips_optimize = hold_mips_optimize;
6370
6371 relax_switch ();
6372
6373 /* We just generated two relocs. When tc_gen_reloc
6374 handles this case, it will skip the first reloc and
6375 handle the second. The second reloc already has an
6376 extra addend of 4, which we added above. We must
6377 subtract it out, and then subtract another 4 to make
6378 the first reloc come out right. The second reloc
6379 will come out right because we are going to add 4 to
6380 offset_expr when we build its instruction below.
6381
6382 If we have a symbol, then we don't want to include
6383 the offset, because it will wind up being included
6384 when we generate the reloc. */
6385
6386 if (offset_expr.X_op == O_constant)
6387 offset_expr.X_add_number -= 8;
6388 else
6389 {
6390 offset_expr.X_add_number = -4;
6391 offset_expr.X_op = O_constant;
6392 }
6393 }
6394 used_at = 1;
6395 macro_build_lui (&offset_expr, AT);
6396 if (breg != 0)
6397 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6398 /* Itbl support may require additional care here. */
6399 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6400 BFD_RELOC_LO16, AT);
6401 /* FIXME: How do we handle overflow here? */
6402 offset_expr.X_add_number += 4;
6403 /* Itbl support may require additional care here. */
6404 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6405 BFD_RELOC_LO16, AT);
6406 if (mips_relax.sequence)
6407 relax_end ();
6408 }
6409 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6410 {
6411 /* If this is a reference to an external symbol, we want
6412 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6413 nop
6414 <op> $treg,0($at)
6415 <op> $treg+1,4($at)
6416 Otherwise we want
6417 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6418 nop
6419 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6420 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6421 If there is a base register we add it to $at before the
6422 lwc1 instructions. If there is a constant we include it
6423 in the lwc1 instructions. */
6424 used_at = 1;
6425 expr1.X_add_number = offset_expr.X_add_number;
6426 if (expr1.X_add_number < -0x8000
6427 || expr1.X_add_number >= 0x8000 - 4)
6428 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6429 load_got_offset (AT, &offset_expr);
6430 load_delay_nop ();
6431 if (breg != 0)
6432 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6433
6434 /* Set mips_optimize to 2 to avoid inserting an undesired
6435 nop. */
6436 hold_mips_optimize = mips_optimize;
6437 mips_optimize = 2;
6438
6439 /* Itbl support may require additional care here. */
6440 relax_start (offset_expr.X_add_symbol);
6441 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6442 BFD_RELOC_LO16, AT);
6443 expr1.X_add_number += 4;
6444 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6445 BFD_RELOC_LO16, AT);
6446 relax_switch ();
6447 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6448 BFD_RELOC_LO16, AT);
6449 offset_expr.X_add_number += 4;
6450 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6451 BFD_RELOC_LO16, AT);
6452 relax_end ();
6453
6454 mips_optimize = hold_mips_optimize;
6455 }
6456 else if (mips_pic == SVR4_PIC)
6457 {
6458 int gpdelay;
6459
6460 /* If this is a reference to an external symbol, we want
6461 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6462 addu $at,$at,$gp
6463 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6464 nop
6465 <op> $treg,0($at)
6466 <op> $treg+1,4($at)
6467 Otherwise we want
6468 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6469 nop
6470 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6471 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6472 If there is a base register we add it to $at before the
6473 lwc1 instructions. If there is a constant we include it
6474 in the lwc1 instructions. */
6475 used_at = 1;
6476 expr1.X_add_number = offset_expr.X_add_number;
6477 offset_expr.X_add_number = 0;
6478 if (expr1.X_add_number < -0x8000
6479 || expr1.X_add_number >= 0x8000 - 4)
6480 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6481 gpdelay = reg_needs_delay (mips_gp_register);
6482 relax_start (offset_expr.X_add_symbol);
6483 macro_build (&offset_expr, "lui", "t,u",
6484 AT, BFD_RELOC_MIPS_GOT_HI16);
6485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6486 AT, AT, mips_gp_register);
6487 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6488 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6489 load_delay_nop ();
6490 if (breg != 0)
6491 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6492 /* Itbl support may require additional care here. */
6493 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6494 BFD_RELOC_LO16, AT);
6495 expr1.X_add_number += 4;
6496
6497 /* Set mips_optimize to 2 to avoid inserting an undesired
6498 nop. */
6499 hold_mips_optimize = mips_optimize;
6500 mips_optimize = 2;
6501 /* Itbl support may require additional care here. */
6502 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6503 BFD_RELOC_LO16, AT);
6504 mips_optimize = hold_mips_optimize;
6505 expr1.X_add_number -= 4;
6506
6507 relax_switch ();
6508 offset_expr.X_add_number = expr1.X_add_number;
6509 if (gpdelay)
6510 macro_build (NULL, "nop", "");
6511 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6512 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6513 load_delay_nop ();
6514 if (breg != 0)
6515 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6516 /* Itbl support may require additional care here. */
6517 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6518 BFD_RELOC_LO16, AT);
6519 offset_expr.X_add_number += 4;
6520
6521 /* Set mips_optimize to 2 to avoid inserting an undesired
6522 nop. */
6523 hold_mips_optimize = mips_optimize;
6524 mips_optimize = 2;
6525 /* Itbl support may require additional care here. */
6526 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6527 BFD_RELOC_LO16, AT);
6528 mips_optimize = hold_mips_optimize;
6529 relax_end ();
6530 }
6531 else
6532 abort ();
6533
6534 break;
6535
6536 case M_LD_OB:
6537 s = "lw";
6538 goto sd_ob;
6539 case M_SD_OB:
6540 s = "sw";
6541 sd_ob:
6542 assert (HAVE_32BIT_ADDRESSES);
6543 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6544 offset_expr.X_add_number += 4;
6545 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6546 break;
6547
6548 /* New code added to support COPZ instructions.
6549 This code builds table entries out of the macros in mip_opcodes.
6550 R4000 uses interlocks to handle coproc delays.
6551 Other chips (like the R3000) require nops to be inserted for delays.
6552
6553 FIXME: Currently, we require that the user handle delays.
6554 In order to fill delay slots for non-interlocked chips,
6555 we must have a way to specify delays based on the coprocessor.
6556 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6557 What are the side-effects of the cop instruction?
6558 What cache support might we have and what are its effects?
6559 Both coprocessor & memory require delays. how long???
6560 What registers are read/set/modified?
6561
6562 If an itbl is provided to interpret cop instructions,
6563 this knowledge can be encoded in the itbl spec. */
6564
6565 case M_COP0:
6566 s = "c0";
6567 goto copz;
6568 case M_COP1:
6569 s = "c1";
6570 goto copz;
6571 case M_COP2:
6572 s = "c2";
6573 goto copz;
6574 case M_COP3:
6575 s = "c3";
6576 copz:
6577 /* For now we just do C (same as Cz). The parameter will be
6578 stored in insn_opcode by mips_ip. */
6579 macro_build (NULL, s, "C", ip->insn_opcode);
6580 break;
6581
6582 case M_MOVE:
6583 move_register (dreg, sreg);
6584 break;
6585
6586 #ifdef LOSING_COMPILER
6587 default:
6588 /* Try and see if this is a new itbl instruction.
6589 This code builds table entries out of the macros in mip_opcodes.
6590 FIXME: For now we just assemble the expression and pass it's
6591 value along as a 32-bit immediate.
6592 We may want to have the assembler assemble this value,
6593 so that we gain the assembler's knowledge of delay slots,
6594 symbols, etc.
6595 Would it be more efficient to use mask (id) here? */
6596 if (itbl_have_entries
6597 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6598 {
6599 s = ip->insn_mo->name;
6600 s2 = "cop3";
6601 coproc = ITBL_DECODE_PNUM (immed_expr);;
6602 macro_build (&immed_expr, s, "C");
6603 break;
6604 }
6605 macro2 (ip);
6606 break;
6607 }
6608 if (mips_opts.noat && used_at)
6609 as_bad (_("Macro used $at after \".set noat\""));
6610 }
6611
6612 static void
6613 macro2 (struct mips_cl_insn *ip)
6614 {
6615 register int treg, sreg, dreg, breg;
6616 int tempreg;
6617 int mask;
6618 int used_at;
6619 expressionS expr1;
6620 const char *s;
6621 const char *s2;
6622 const char *fmt;
6623 int likely = 0;
6624 int dbl = 0;
6625 int coproc = 0;
6626 int lr = 0;
6627 int imm = 0;
6628 int off;
6629 offsetT maxnum;
6630 bfd_reloc_code_real_type r;
6631
6632 treg = (ip->insn_opcode >> 16) & 0x1f;
6633 dreg = (ip->insn_opcode >> 11) & 0x1f;
6634 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6635 mask = ip->insn_mo->mask;
6636
6637 expr1.X_op = O_constant;
6638 expr1.X_op_symbol = NULL;
6639 expr1.X_add_symbol = NULL;
6640 expr1.X_add_number = 1;
6641
6642 switch (mask)
6643 {
6644 #endif /* LOSING_COMPILER */
6645
6646 case M_DMUL:
6647 dbl = 1;
6648 case M_MUL:
6649 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6650 macro_build (NULL, "mflo", "d", dreg);
6651 break;
6652
6653 case M_DMUL_I:
6654 dbl = 1;
6655 case M_MUL_I:
6656 /* The MIPS assembler some times generates shifts and adds. I'm
6657 not trying to be that fancy. GCC should do this for us
6658 anyway. */
6659 used_at = 1;
6660 load_register (AT, &imm_expr, dbl);
6661 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6662 macro_build (NULL, "mflo", "d", dreg);
6663 break;
6664
6665 case M_DMULO_I:
6666 dbl = 1;
6667 case M_MULO_I:
6668 imm = 1;
6669 goto do_mulo;
6670
6671 case M_DMULO:
6672 dbl = 1;
6673 case M_MULO:
6674 do_mulo:
6675 start_noreorder ();
6676 used_at = 1;
6677 if (imm)
6678 load_register (AT, &imm_expr, dbl);
6679 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6680 macro_build (NULL, "mflo", "d", dreg);
6681 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6682 macro_build (NULL, "mfhi", "d", AT);
6683 if (mips_trap)
6684 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6685 else
6686 {
6687 expr1.X_add_number = 8;
6688 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6689 macro_build (NULL, "nop", "", 0);
6690 macro_build (NULL, "break", "c", 6);
6691 }
6692 end_noreorder ();
6693 macro_build (NULL, "mflo", "d", dreg);
6694 break;
6695
6696 case M_DMULOU_I:
6697 dbl = 1;
6698 case M_MULOU_I:
6699 imm = 1;
6700 goto do_mulou;
6701
6702 case M_DMULOU:
6703 dbl = 1;
6704 case M_MULOU:
6705 do_mulou:
6706 start_noreorder ();
6707 used_at = 1;
6708 if (imm)
6709 load_register (AT, &imm_expr, dbl);
6710 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6711 sreg, imm ? AT : treg);
6712 macro_build (NULL, "mfhi", "d", AT);
6713 macro_build (NULL, "mflo", "d", dreg);
6714 if (mips_trap)
6715 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6716 else
6717 {
6718 expr1.X_add_number = 8;
6719 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6720 macro_build (NULL, "nop", "", 0);
6721 macro_build (NULL, "break", "c", 6);
6722 }
6723 end_noreorder ();
6724 break;
6725
6726 case M_DROL:
6727 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6728 {
6729 if (dreg == sreg)
6730 {
6731 tempreg = AT;
6732 used_at = 1;
6733 }
6734 else
6735 {
6736 tempreg = dreg;
6737 }
6738 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6739 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6740 break;
6741 }
6742 used_at = 1;
6743 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6744 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6745 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6746 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6747 break;
6748
6749 case M_ROL:
6750 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6751 {
6752 if (dreg == sreg)
6753 {
6754 tempreg = AT;
6755 used_at = 1;
6756 }
6757 else
6758 {
6759 tempreg = dreg;
6760 }
6761 macro_build (NULL, "negu", "d,w", tempreg, treg);
6762 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6763 break;
6764 }
6765 used_at = 1;
6766 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6767 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6768 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6769 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6770 break;
6771
6772 case M_DROL_I:
6773 {
6774 unsigned int rot;
6775 char *l, *r;
6776
6777 if (imm_expr.X_op != O_constant)
6778 as_bad (_("Improper rotate count"));
6779 rot = imm_expr.X_add_number & 0x3f;
6780 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6781 {
6782 rot = (64 - rot) & 0x3f;
6783 if (rot >= 32)
6784 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6785 else
6786 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6787 break;
6788 }
6789 if (rot == 0)
6790 {
6791 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6792 break;
6793 }
6794 l = (rot < 0x20) ? "dsll" : "dsll32";
6795 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6796 rot &= 0x1f;
6797 used_at = 1;
6798 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6799 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6800 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6801 }
6802 break;
6803
6804 case M_ROL_I:
6805 {
6806 unsigned int rot;
6807
6808 if (imm_expr.X_op != O_constant)
6809 as_bad (_("Improper rotate count"));
6810 rot = imm_expr.X_add_number & 0x1f;
6811 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6812 {
6813 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6814 break;
6815 }
6816 if (rot == 0)
6817 {
6818 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6819 break;
6820 }
6821 used_at = 1;
6822 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6823 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6824 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6825 }
6826 break;
6827
6828 case M_DROR:
6829 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6830 {
6831 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6832 break;
6833 }
6834 used_at = 1;
6835 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6836 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6837 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6838 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6839 break;
6840
6841 case M_ROR:
6842 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6843 {
6844 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
6845 break;
6846 }
6847 used_at = 1;
6848 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6849 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6850 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6851 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6852 break;
6853
6854 case M_DROR_I:
6855 {
6856 unsigned int rot;
6857 char *l, *r;
6858
6859 if (imm_expr.X_op != O_constant)
6860 as_bad (_("Improper rotate count"));
6861 rot = imm_expr.X_add_number & 0x3f;
6862 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6863 {
6864 if (rot >= 32)
6865 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6866 else
6867 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6868 break;
6869 }
6870 if (rot == 0)
6871 {
6872 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6873 break;
6874 }
6875 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6876 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6877 rot &= 0x1f;
6878 used_at = 1;
6879 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
6880 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6881 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6882 }
6883 break;
6884
6885 case M_ROR_I:
6886 {
6887 unsigned int rot;
6888
6889 if (imm_expr.X_op != O_constant)
6890 as_bad (_("Improper rotate count"));
6891 rot = imm_expr.X_add_number & 0x1f;
6892 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6893 {
6894 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
6895 break;
6896 }
6897 if (rot == 0)
6898 {
6899 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6900 break;
6901 }
6902 used_at = 1;
6903 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
6904 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6905 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6906 }
6907 break;
6908
6909 case M_S_DOB:
6910 if (mips_opts.arch == CPU_R4650)
6911 {
6912 as_bad (_("opcode not supported on this processor"));
6913 break;
6914 }
6915 assert (mips_opts.isa == ISA_MIPS1);
6916 /* Even on a big endian machine $fn comes before $fn+1. We have
6917 to adjust when storing to memory. */
6918 macro_build (&offset_expr, "swc1", "T,o(b)",
6919 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
6920 offset_expr.X_add_number += 4;
6921 macro_build (&offset_expr, "swc1", "T,o(b)",
6922 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
6923 break;
6924
6925 case M_SEQ:
6926 if (sreg == 0)
6927 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
6928 else if (treg == 0)
6929 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6930 else
6931 {
6932 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
6933 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
6934 }
6935 break;
6936
6937 case M_SEQ_I:
6938 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6939 {
6940 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6941 break;
6942 }
6943 if (sreg == 0)
6944 {
6945 as_warn (_("Instruction %s: result is always false"),
6946 ip->insn_mo->name);
6947 move_register (dreg, 0);
6948 break;
6949 }
6950 if (imm_expr.X_op == O_constant
6951 && imm_expr.X_add_number >= 0
6952 && imm_expr.X_add_number < 0x10000)
6953 {
6954 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
6955 }
6956 else if (imm_expr.X_op == O_constant
6957 && imm_expr.X_add_number > -0x8000
6958 && imm_expr.X_add_number < 0)
6959 {
6960 imm_expr.X_add_number = -imm_expr.X_add_number;
6961 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6962 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
6963 }
6964 else
6965 {
6966 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6967 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
6968 used_at = 1;
6969 }
6970 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
6971 break;
6972
6973 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6974 s = "slt";
6975 goto sge;
6976 case M_SGEU:
6977 s = "sltu";
6978 sge:
6979 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
6980 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
6981 break;
6982
6983 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6984 case M_SGEU_I:
6985 if (imm_expr.X_op == O_constant
6986 && imm_expr.X_add_number >= -0x8000
6987 && imm_expr.X_add_number < 0x8000)
6988 {
6989 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
6990 dreg, sreg, BFD_RELOC_LO16);
6991 }
6992 else
6993 {
6994 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6995 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
6996 dreg, sreg, AT);
6997 used_at = 1;
6998 }
6999 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7000 break;
7001
7002 case M_SGT: /* sreg > treg <==> treg < sreg */
7003 s = "slt";
7004 goto sgt;
7005 case M_SGTU:
7006 s = "sltu";
7007 sgt:
7008 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7009 break;
7010
7011 case M_SGT_I: /* sreg > I <==> I < sreg */
7012 s = "slt";
7013 goto sgti;
7014 case M_SGTU_I:
7015 s = "sltu";
7016 sgti:
7017 used_at = 1;
7018 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7019 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7020 break;
7021
7022 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7023 s = "slt";
7024 goto sle;
7025 case M_SLEU:
7026 s = "sltu";
7027 sle:
7028 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7029 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7030 break;
7031
7032 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7033 s = "slt";
7034 goto slei;
7035 case M_SLEU_I:
7036 s = "sltu";
7037 slei:
7038 used_at = 1;
7039 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7040 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7041 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7042 break;
7043
7044 case M_SLT_I:
7045 if (imm_expr.X_op == O_constant
7046 && imm_expr.X_add_number >= -0x8000
7047 && imm_expr.X_add_number < 0x8000)
7048 {
7049 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7050 break;
7051 }
7052 used_at = 1;
7053 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7054 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7055 break;
7056
7057 case M_SLTU_I:
7058 if (imm_expr.X_op == O_constant
7059 && imm_expr.X_add_number >= -0x8000
7060 && imm_expr.X_add_number < 0x8000)
7061 {
7062 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7063 BFD_RELOC_LO16);
7064 break;
7065 }
7066 used_at = 1;
7067 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7068 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7069 break;
7070
7071 case M_SNE:
7072 if (sreg == 0)
7073 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7074 else if (treg == 0)
7075 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7076 else
7077 {
7078 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7079 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7080 }
7081 break;
7082
7083 case M_SNE_I:
7084 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7085 {
7086 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7087 break;
7088 }
7089 if (sreg == 0)
7090 {
7091 as_warn (_("Instruction %s: result is always true"),
7092 ip->insn_mo->name);
7093 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7094 dreg, 0, BFD_RELOC_LO16);
7095 break;
7096 }
7097 if (imm_expr.X_op == O_constant
7098 && imm_expr.X_add_number >= 0
7099 && imm_expr.X_add_number < 0x10000)
7100 {
7101 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7102 }
7103 else if (imm_expr.X_op == O_constant
7104 && imm_expr.X_add_number > -0x8000
7105 && imm_expr.X_add_number < 0)
7106 {
7107 imm_expr.X_add_number = -imm_expr.X_add_number;
7108 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7109 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7110 }
7111 else
7112 {
7113 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7114 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7115 used_at = 1;
7116 }
7117 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7118 break;
7119
7120 case M_DSUB_I:
7121 dbl = 1;
7122 case M_SUB_I:
7123 if (imm_expr.X_op == O_constant
7124 && imm_expr.X_add_number > -0x8000
7125 && imm_expr.X_add_number <= 0x8000)
7126 {
7127 imm_expr.X_add_number = -imm_expr.X_add_number;
7128 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7129 dreg, sreg, BFD_RELOC_LO16);
7130 break;
7131 }
7132 used_at = 1;
7133 load_register (AT, &imm_expr, dbl);
7134 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7135 break;
7136
7137 case M_DSUBU_I:
7138 dbl = 1;
7139 case M_SUBU_I:
7140 if (imm_expr.X_op == O_constant
7141 && imm_expr.X_add_number > -0x8000
7142 && imm_expr.X_add_number <= 0x8000)
7143 {
7144 imm_expr.X_add_number = -imm_expr.X_add_number;
7145 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7146 dreg, sreg, BFD_RELOC_LO16);
7147 break;
7148 }
7149 used_at = 1;
7150 load_register (AT, &imm_expr, dbl);
7151 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7152 break;
7153
7154 case M_TEQ_I:
7155 s = "teq";
7156 goto trap;
7157 case M_TGE_I:
7158 s = "tge";
7159 goto trap;
7160 case M_TGEU_I:
7161 s = "tgeu";
7162 goto trap;
7163 case M_TLT_I:
7164 s = "tlt";
7165 goto trap;
7166 case M_TLTU_I:
7167 s = "tltu";
7168 goto trap;
7169 case M_TNE_I:
7170 s = "tne";
7171 trap:
7172 used_at = 1;
7173 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7174 macro_build (NULL, s, "s,t", sreg, AT);
7175 break;
7176
7177 case M_TRUNCWS:
7178 case M_TRUNCWD:
7179 assert (mips_opts.isa == ISA_MIPS1);
7180 used_at = 1;
7181 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7182 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7183
7184 /*
7185 * Is the double cfc1 instruction a bug in the mips assembler;
7186 * or is there a reason for it?
7187 */
7188 start_noreorder ();
7189 macro_build (NULL, "cfc1", "t,G", treg, RA);
7190 macro_build (NULL, "cfc1", "t,G", treg, RA);
7191 macro_build (NULL, "nop", "");
7192 expr1.X_add_number = 3;
7193 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7194 expr1.X_add_number = 2;
7195 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7196 macro_build (NULL, "ctc1", "t,G", AT, RA);
7197 macro_build (NULL, "nop", "");
7198 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7199 dreg, sreg);
7200 macro_build (NULL, "ctc1", "t,G", treg, RA);
7201 macro_build (NULL, "nop", "");
7202 end_noreorder ();
7203 break;
7204
7205 case M_ULH:
7206 s = "lb";
7207 goto ulh;
7208 case M_ULHU:
7209 s = "lbu";
7210 ulh:
7211 used_at = 1;
7212 if (offset_expr.X_add_number >= 0x7fff)
7213 as_bad (_("operand overflow"));
7214 if (! target_big_endian)
7215 ++offset_expr.X_add_number;
7216 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7217 if (! target_big_endian)
7218 --offset_expr.X_add_number;
7219 else
7220 ++offset_expr.X_add_number;
7221 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7222 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7223 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7224 break;
7225
7226 case M_ULD:
7227 s = "ldl";
7228 s2 = "ldr";
7229 off = 7;
7230 goto ulw;
7231 case M_ULW:
7232 s = "lwl";
7233 s2 = "lwr";
7234 off = 3;
7235 ulw:
7236 if (offset_expr.X_add_number >= 0x8000 - off)
7237 as_bad (_("operand overflow"));
7238 if (treg != breg)
7239 tempreg = treg;
7240 else
7241 {
7242 used_at = 1;
7243 tempreg = AT;
7244 }
7245 if (! target_big_endian)
7246 offset_expr.X_add_number += off;
7247 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7248 if (! target_big_endian)
7249 offset_expr.X_add_number -= off;
7250 else
7251 offset_expr.X_add_number += off;
7252 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7253
7254 /* If necessary, move the result in tempreg the final destination. */
7255 if (treg == tempreg)
7256 break;
7257 /* Protect second load's delay slot. */
7258 load_delay_nop ();
7259 move_register (treg, tempreg);
7260 break;
7261
7262 case M_ULD_A:
7263 s = "ldl";
7264 s2 = "ldr";
7265 off = 7;
7266 goto ulwa;
7267 case M_ULW_A:
7268 s = "lwl";
7269 s2 = "lwr";
7270 off = 3;
7271 ulwa:
7272 used_at = 1;
7273 load_address (AT, &offset_expr, &used_at);
7274 if (breg != 0)
7275 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7276 if (! target_big_endian)
7277 expr1.X_add_number = off;
7278 else
7279 expr1.X_add_number = 0;
7280 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7281 if (! target_big_endian)
7282 expr1.X_add_number = 0;
7283 else
7284 expr1.X_add_number = off;
7285 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7286 break;
7287
7288 case M_ULH_A:
7289 case M_ULHU_A:
7290 used_at = 1;
7291 load_address (AT, &offset_expr, &used_at);
7292 if (breg != 0)
7293 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7294 if (target_big_endian)
7295 expr1.X_add_number = 0;
7296 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7297 treg, BFD_RELOC_LO16, AT);
7298 if (target_big_endian)
7299 expr1.X_add_number = 1;
7300 else
7301 expr1.X_add_number = 0;
7302 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7303 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7304 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7305 break;
7306
7307 case M_USH:
7308 used_at = 1;
7309 if (offset_expr.X_add_number >= 0x7fff)
7310 as_bad (_("operand overflow"));
7311 if (target_big_endian)
7312 ++offset_expr.X_add_number;
7313 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7314 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7315 if (target_big_endian)
7316 --offset_expr.X_add_number;
7317 else
7318 ++offset_expr.X_add_number;
7319 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7320 break;
7321
7322 case M_USD:
7323 s = "sdl";
7324 s2 = "sdr";
7325 off = 7;
7326 goto usw;
7327 case M_USW:
7328 s = "swl";
7329 s2 = "swr";
7330 off = 3;
7331 usw:
7332 if (offset_expr.X_add_number >= 0x8000 - off)
7333 as_bad (_("operand overflow"));
7334 if (! target_big_endian)
7335 offset_expr.X_add_number += off;
7336 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7337 if (! target_big_endian)
7338 offset_expr.X_add_number -= off;
7339 else
7340 offset_expr.X_add_number += off;
7341 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7342 break;
7343
7344 case M_USD_A:
7345 s = "sdl";
7346 s2 = "sdr";
7347 off = 7;
7348 goto uswa;
7349 case M_USW_A:
7350 s = "swl";
7351 s2 = "swr";
7352 off = 3;
7353 uswa:
7354 used_at = 1;
7355 load_address (AT, &offset_expr, &used_at);
7356 if (breg != 0)
7357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7358 if (! target_big_endian)
7359 expr1.X_add_number = off;
7360 else
7361 expr1.X_add_number = 0;
7362 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7363 if (! target_big_endian)
7364 expr1.X_add_number = 0;
7365 else
7366 expr1.X_add_number = off;
7367 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7368 break;
7369
7370 case M_USH_A:
7371 used_at = 1;
7372 load_address (AT, &offset_expr, &used_at);
7373 if (breg != 0)
7374 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7375 if (! target_big_endian)
7376 expr1.X_add_number = 0;
7377 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7378 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7379 if (! target_big_endian)
7380 expr1.X_add_number = 1;
7381 else
7382 expr1.X_add_number = 0;
7383 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7384 if (! target_big_endian)
7385 expr1.X_add_number = 0;
7386 else
7387 expr1.X_add_number = 1;
7388 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7389 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7390 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7391 break;
7392
7393 default:
7394 /* FIXME: Check if this is one of the itbl macros, since they
7395 are added dynamically. */
7396 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7397 break;
7398 }
7399 if (mips_opts.noat && used_at)
7400 as_bad (_("Macro used $at after \".set noat\""));
7401 }
7402
7403 /* Implement macros in mips16 mode. */
7404
7405 static void
7406 mips16_macro (struct mips_cl_insn *ip)
7407 {
7408 int mask;
7409 int xreg, yreg, zreg, tmp;
7410 expressionS expr1;
7411 int dbl;
7412 const char *s, *s2, *s3;
7413
7414 mask = ip->insn_mo->mask;
7415
7416 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7417 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7418 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
7419
7420 expr1.X_op = O_constant;
7421 expr1.X_op_symbol = NULL;
7422 expr1.X_add_symbol = NULL;
7423 expr1.X_add_number = 1;
7424
7425 dbl = 0;
7426
7427 switch (mask)
7428 {
7429 default:
7430 internalError ();
7431
7432 case M_DDIV_3:
7433 dbl = 1;
7434 case M_DIV_3:
7435 s = "mflo";
7436 goto do_div3;
7437 case M_DREM_3:
7438 dbl = 1;
7439 case M_REM_3:
7440 s = "mfhi";
7441 do_div3:
7442 start_noreorder ();
7443 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7444 expr1.X_add_number = 2;
7445 macro_build (&expr1, "bnez", "x,p", yreg);
7446 macro_build (NULL, "break", "6", 7);
7447
7448 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7449 since that causes an overflow. We should do that as well,
7450 but I don't see how to do the comparisons without a temporary
7451 register. */
7452 end_noreorder ();
7453 macro_build (NULL, s, "x", zreg);
7454 break;
7455
7456 case M_DIVU_3:
7457 s = "divu";
7458 s2 = "mflo";
7459 goto do_divu3;
7460 case M_REMU_3:
7461 s = "divu";
7462 s2 = "mfhi";
7463 goto do_divu3;
7464 case M_DDIVU_3:
7465 s = "ddivu";
7466 s2 = "mflo";
7467 goto do_divu3;
7468 case M_DREMU_3:
7469 s = "ddivu";
7470 s2 = "mfhi";
7471 do_divu3:
7472 start_noreorder ();
7473 macro_build (NULL, s, "0,x,y", xreg, yreg);
7474 expr1.X_add_number = 2;
7475 macro_build (&expr1, "bnez", "x,p", yreg);
7476 macro_build (NULL, "break", "6", 7);
7477 end_noreorder ();
7478 macro_build (NULL, s2, "x", zreg);
7479 break;
7480
7481 case M_DMUL:
7482 dbl = 1;
7483 case M_MUL:
7484 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7485 macro_build (NULL, "mflo", "x", zreg);
7486 break;
7487
7488 case M_DSUBU_I:
7489 dbl = 1;
7490 goto do_subu;
7491 case M_SUBU_I:
7492 do_subu:
7493 if (imm_expr.X_op != O_constant)
7494 as_bad (_("Unsupported large constant"));
7495 imm_expr.X_add_number = -imm_expr.X_add_number;
7496 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7497 break;
7498
7499 case M_SUBU_I_2:
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, "addiu", "x,k", xreg);
7504 break;
7505
7506 case M_DSUBU_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, "daddiu", "y,j", yreg);
7511 break;
7512
7513 case M_BEQ:
7514 s = "cmp";
7515 s2 = "bteqz";
7516 goto do_branch;
7517 case M_BNE:
7518 s = "cmp";
7519 s2 = "btnez";
7520 goto do_branch;
7521 case M_BLT:
7522 s = "slt";
7523 s2 = "btnez";
7524 goto do_branch;
7525 case M_BLTU:
7526 s = "sltu";
7527 s2 = "btnez";
7528 goto do_branch;
7529 case M_BLE:
7530 s = "slt";
7531 s2 = "bteqz";
7532 goto do_reverse_branch;
7533 case M_BLEU:
7534 s = "sltu";
7535 s2 = "bteqz";
7536 goto do_reverse_branch;
7537 case M_BGE:
7538 s = "slt";
7539 s2 = "bteqz";
7540 goto do_branch;
7541 case M_BGEU:
7542 s = "sltu";
7543 s2 = "bteqz";
7544 goto do_branch;
7545 case M_BGT:
7546 s = "slt";
7547 s2 = "btnez";
7548 goto do_reverse_branch;
7549 case M_BGTU:
7550 s = "sltu";
7551 s2 = "btnez";
7552
7553 do_reverse_branch:
7554 tmp = xreg;
7555 xreg = yreg;
7556 yreg = tmp;
7557
7558 do_branch:
7559 macro_build (NULL, s, "x,y", xreg, yreg);
7560 macro_build (&offset_expr, s2, "p");
7561 break;
7562
7563 case M_BEQ_I:
7564 s = "cmpi";
7565 s2 = "bteqz";
7566 s3 = "x,U";
7567 goto do_branch_i;
7568 case M_BNE_I:
7569 s = "cmpi";
7570 s2 = "btnez";
7571 s3 = "x,U";
7572 goto do_branch_i;
7573 case M_BLT_I:
7574 s = "slti";
7575 s2 = "btnez";
7576 s3 = "x,8";
7577 goto do_branch_i;
7578 case M_BLTU_I:
7579 s = "sltiu";
7580 s2 = "btnez";
7581 s3 = "x,8";
7582 goto do_branch_i;
7583 case M_BLE_I:
7584 s = "slti";
7585 s2 = "btnez";
7586 s3 = "x,8";
7587 goto do_addone_branch_i;
7588 case M_BLEU_I:
7589 s = "sltiu";
7590 s2 = "btnez";
7591 s3 = "x,8";
7592 goto do_addone_branch_i;
7593 case M_BGE_I:
7594 s = "slti";
7595 s2 = "bteqz";
7596 s3 = "x,8";
7597 goto do_branch_i;
7598 case M_BGEU_I:
7599 s = "sltiu";
7600 s2 = "bteqz";
7601 s3 = "x,8";
7602 goto do_branch_i;
7603 case M_BGT_I:
7604 s = "slti";
7605 s2 = "bteqz";
7606 s3 = "x,8";
7607 goto do_addone_branch_i;
7608 case M_BGTU_I:
7609 s = "sltiu";
7610 s2 = "bteqz";
7611 s3 = "x,8";
7612
7613 do_addone_branch_i:
7614 if (imm_expr.X_op != O_constant)
7615 as_bad (_("Unsupported large constant"));
7616 ++imm_expr.X_add_number;
7617
7618 do_branch_i:
7619 macro_build (&imm_expr, s, s3, xreg);
7620 macro_build (&offset_expr, s2, "p");
7621 break;
7622
7623 case M_ABS:
7624 expr1.X_add_number = 0;
7625 macro_build (&expr1, "slti", "x,8", yreg);
7626 if (xreg != yreg)
7627 move_register (xreg, yreg);
7628 expr1.X_add_number = 2;
7629 macro_build (&expr1, "bteqz", "p");
7630 macro_build (NULL, "neg", "x,w", xreg, xreg);
7631 }
7632 }
7633
7634 /* For consistency checking, verify that all bits are specified either
7635 by the match/mask part of the instruction definition, or by the
7636 operand list. */
7637 static int
7638 validate_mips_insn (const struct mips_opcode *opc)
7639 {
7640 const char *p = opc->args;
7641 char c;
7642 unsigned long used_bits = opc->mask;
7643
7644 if ((used_bits & opc->match) != opc->match)
7645 {
7646 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7647 opc->name, opc->args);
7648 return 0;
7649 }
7650 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7651 while (*p)
7652 switch (c = *p++)
7653 {
7654 case ',': break;
7655 case '(': break;
7656 case ')': break;
7657 case '+':
7658 switch (c = *p++)
7659 {
7660 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7661 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7662 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7663 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7664 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7665 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7666 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7667 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7668 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7669 case 'I': break;
7670 default:
7671 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7672 c, opc->name, opc->args);
7673 return 0;
7674 }
7675 break;
7676 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7677 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7678 case 'A': break;
7679 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7680 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7681 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7682 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7683 case 'F': break;
7684 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7685 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7686 case 'I': break;
7687 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7688 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7689 case 'L': break;
7690 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7691 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7692 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7693 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7694 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7695 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7696 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7697 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7698 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7699 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7700 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7701 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7702 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7703 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7704 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7705 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7706 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7707 case 'f': break;
7708 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7709 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7710 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7711 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7712 case 'l': break;
7713 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7714 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7715 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7716 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7717 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7718 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7719 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7720 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7721 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7722 case 'x': break;
7723 case 'z': break;
7724 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7725 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7726 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7727 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7728 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7729 case '[': break;
7730 case ']': break;
7731 default:
7732 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7733 c, opc->name, opc->args);
7734 return 0;
7735 }
7736 #undef USE_BITS
7737 if (used_bits != 0xffffffff)
7738 {
7739 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7740 ~used_bits & 0xffffffff, opc->name, opc->args);
7741 return 0;
7742 }
7743 return 1;
7744 }
7745
7746 /* This routine assembles an instruction into its binary format. As a
7747 side effect, it sets one of the global variables imm_reloc or
7748 offset_reloc to the type of relocation to do if one of the operands
7749 is an address expression. */
7750
7751 static void
7752 mips_ip (char *str, struct mips_cl_insn *ip)
7753 {
7754 char *s;
7755 const char *args;
7756 char c = 0;
7757 struct mips_opcode *insn;
7758 char *argsStart;
7759 unsigned int regno;
7760 unsigned int lastregno = 0;
7761 unsigned int lastpos = 0;
7762 unsigned int limlo, limhi;
7763 char *s_reset;
7764 char save_c = 0;
7765
7766 insn_error = NULL;
7767
7768 /* If the instruction contains a '.', we first try to match an instruction
7769 including the '.'. Then we try again without the '.'. */
7770 insn = NULL;
7771 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7772 continue;
7773
7774 /* If we stopped on whitespace, then replace the whitespace with null for
7775 the call to hash_find. Save the character we replaced just in case we
7776 have to re-parse the instruction. */
7777 if (ISSPACE (*s))
7778 {
7779 save_c = *s;
7780 *s++ = '\0';
7781 }
7782
7783 insn = (struct mips_opcode *) hash_find (op_hash, str);
7784
7785 /* If we didn't find the instruction in the opcode table, try again, but
7786 this time with just the instruction up to, but not including the
7787 first '.'. */
7788 if (insn == NULL)
7789 {
7790 /* Restore the character we overwrite above (if any). */
7791 if (save_c)
7792 *(--s) = save_c;
7793
7794 /* Scan up to the first '.' or whitespace. */
7795 for (s = str;
7796 *s != '\0' && *s != '.' && !ISSPACE (*s);
7797 ++s)
7798 continue;
7799
7800 /* If we did not find a '.', then we can quit now. */
7801 if (*s != '.')
7802 {
7803 insn_error = "unrecognized opcode";
7804 return;
7805 }
7806
7807 /* Lookup the instruction in the hash table. */
7808 *s++ = '\0';
7809 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7810 {
7811 insn_error = "unrecognized opcode";
7812 return;
7813 }
7814 }
7815
7816 argsStart = s;
7817 for (;;)
7818 {
7819 bfd_boolean ok;
7820
7821 assert (strcmp (insn->name, str) == 0);
7822
7823 if (OPCODE_IS_MEMBER (insn,
7824 (mips_opts.isa
7825 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7826 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7827 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7828 mips_opts.arch))
7829 ok = TRUE;
7830 else
7831 ok = FALSE;
7832
7833 if (insn->pinfo != INSN_MACRO)
7834 {
7835 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7836 ok = FALSE;
7837 }
7838
7839 if (! ok)
7840 {
7841 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7842 && strcmp (insn->name, insn[1].name) == 0)
7843 {
7844 ++insn;
7845 continue;
7846 }
7847 else
7848 {
7849 if (!insn_error)
7850 {
7851 static char buf[100];
7852 sprintf (buf,
7853 _("opcode not supported on this processor: %s (%s)"),
7854 mips_cpu_info_from_arch (mips_opts.arch)->name,
7855 mips_cpu_info_from_isa (mips_opts.isa)->name);
7856 insn_error = buf;
7857 }
7858 if (save_c)
7859 *(--s) = save_c;
7860 return;
7861 }
7862 }
7863
7864 create_insn (ip, insn);
7865 insn_error = NULL;
7866 for (args = insn->args;; ++args)
7867 {
7868 int is_mdmx;
7869
7870 s += strspn (s, " \t");
7871 is_mdmx = 0;
7872 switch (*args)
7873 {
7874 case '\0': /* end of args */
7875 if (*s == '\0')
7876 return;
7877 break;
7878
7879 case ',':
7880 if (*s++ == *args)
7881 continue;
7882 s--;
7883 switch (*++args)
7884 {
7885 case 'r':
7886 case 'v':
7887 INSERT_OPERAND (RS, *ip, lastregno);
7888 continue;
7889
7890 case 'w':
7891 INSERT_OPERAND (RT, *ip, lastregno);
7892 continue;
7893
7894 case 'W':
7895 INSERT_OPERAND (FT, *ip, lastregno);
7896 continue;
7897
7898 case 'V':
7899 INSERT_OPERAND (FS, *ip, lastregno);
7900 continue;
7901 }
7902 break;
7903
7904 case '(':
7905 /* Handle optional base register.
7906 Either the base register is omitted or
7907 we must have a left paren. */
7908 /* This is dependent on the next operand specifier
7909 is a base register specification. */
7910 assert (args[1] == 'b' || args[1] == '5'
7911 || args[1] == '-' || args[1] == '4');
7912 if (*s == '\0')
7913 return;
7914
7915 case ')': /* these must match exactly */
7916 case '[':
7917 case ']':
7918 if (*s++ == *args)
7919 continue;
7920 break;
7921
7922 case '+': /* Opcode extension character. */
7923 switch (*++args)
7924 {
7925 case 'A': /* ins/ext position, becomes LSB. */
7926 limlo = 0;
7927 limhi = 31;
7928 goto do_lsb;
7929 case 'E':
7930 limlo = 32;
7931 limhi = 63;
7932 goto do_lsb;
7933 do_lsb:
7934 my_getExpression (&imm_expr, s);
7935 check_absolute_expr (ip, &imm_expr);
7936 if ((unsigned long) imm_expr.X_add_number < limlo
7937 || (unsigned long) imm_expr.X_add_number > limhi)
7938 {
7939 as_bad (_("Improper position (%lu)"),
7940 (unsigned long) imm_expr.X_add_number);
7941 imm_expr.X_add_number = limlo;
7942 }
7943 lastpos = imm_expr.X_add_number;
7944 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
7945 imm_expr.X_op = O_absent;
7946 s = expr_end;
7947 continue;
7948
7949 case 'B': /* ins size, becomes MSB. */
7950 limlo = 1;
7951 limhi = 32;
7952 goto do_msb;
7953 case 'F':
7954 limlo = 33;
7955 limhi = 64;
7956 goto do_msb;
7957 do_msb:
7958 my_getExpression (&imm_expr, s);
7959 check_absolute_expr (ip, &imm_expr);
7960 /* Check for negative input so that small negative numbers
7961 will not succeed incorrectly. The checks against
7962 (pos+size) transitively check "size" itself,
7963 assuming that "pos" is reasonable. */
7964 if ((long) imm_expr.X_add_number < 0
7965 || ((unsigned long) imm_expr.X_add_number
7966 + lastpos) < limlo
7967 || ((unsigned long) imm_expr.X_add_number
7968 + lastpos) > limhi)
7969 {
7970 as_bad (_("Improper insert size (%lu, position %lu)"),
7971 (unsigned long) imm_expr.X_add_number,
7972 (unsigned long) lastpos);
7973 imm_expr.X_add_number = limlo - lastpos;
7974 }
7975 INSERT_OPERAND (INSMSB, *ip,
7976 lastpos + imm_expr.X_add_number - 1);
7977 imm_expr.X_op = O_absent;
7978 s = expr_end;
7979 continue;
7980
7981 case 'C': /* ext size, becomes MSBD. */
7982 limlo = 1;
7983 limhi = 32;
7984 goto do_msbd;
7985 case 'G':
7986 limlo = 33;
7987 limhi = 64;
7988 goto do_msbd;
7989 case 'H':
7990 limlo = 33;
7991 limhi = 64;
7992 goto do_msbd;
7993 do_msbd:
7994 my_getExpression (&imm_expr, s);
7995 check_absolute_expr (ip, &imm_expr);
7996 /* Check for negative input so that small negative numbers
7997 will not succeed incorrectly. The checks against
7998 (pos+size) transitively check "size" itself,
7999 assuming that "pos" is reasonable. */
8000 if ((long) imm_expr.X_add_number < 0
8001 || ((unsigned long) imm_expr.X_add_number
8002 + lastpos) < limlo
8003 || ((unsigned long) imm_expr.X_add_number
8004 + lastpos) > limhi)
8005 {
8006 as_bad (_("Improper extract size (%lu, position %lu)"),
8007 (unsigned long) imm_expr.X_add_number,
8008 (unsigned long) lastpos);
8009 imm_expr.X_add_number = limlo - lastpos;
8010 }
8011 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
8012 imm_expr.X_op = O_absent;
8013 s = expr_end;
8014 continue;
8015
8016 case 'D':
8017 /* +D is for disassembly only; never match. */
8018 break;
8019
8020 case 'I':
8021 /* "+I" is like "I", except that imm2_expr is used. */
8022 my_getExpression (&imm2_expr, s);
8023 if (imm2_expr.X_op != O_big
8024 && imm2_expr.X_op != O_constant)
8025 insn_error = _("absolute expression required");
8026 normalize_constant_expr (&imm2_expr);
8027 s = expr_end;
8028 continue;
8029
8030 default:
8031 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8032 *args, insn->name, insn->args);
8033 /* Further processing is fruitless. */
8034 return;
8035 }
8036 break;
8037
8038 case '<': /* must be at least one digit */
8039 /*
8040 * According to the manual, if the shift amount is greater
8041 * than 31 or less than 0, then the shift amount should be
8042 * mod 32. In reality the mips assembler issues an error.
8043 * We issue a warning and mask out all but the low 5 bits.
8044 */
8045 my_getExpression (&imm_expr, s);
8046 check_absolute_expr (ip, &imm_expr);
8047 if ((unsigned long) imm_expr.X_add_number > 31)
8048 as_warn (_("Improper shift amount (%lu)"),
8049 (unsigned long) imm_expr.X_add_number);
8050 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
8051 imm_expr.X_op = O_absent;
8052 s = expr_end;
8053 continue;
8054
8055 case '>': /* shift amount minus 32 */
8056 my_getExpression (&imm_expr, s);
8057 check_absolute_expr (ip, &imm_expr);
8058 if ((unsigned long) imm_expr.X_add_number < 32
8059 || (unsigned long) imm_expr.X_add_number > 63)
8060 break;
8061 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
8062 imm_expr.X_op = O_absent;
8063 s = expr_end;
8064 continue;
8065
8066 case 'k': /* cache code */
8067 case 'h': /* prefx code */
8068 my_getExpression (&imm_expr, s);
8069 check_absolute_expr (ip, &imm_expr);
8070 if ((unsigned long) imm_expr.X_add_number > 31)
8071 as_warn (_("Invalid value for `%s' (%lu)"),
8072 ip->insn_mo->name,
8073 (unsigned long) imm_expr.X_add_number);
8074 if (*args == 'k')
8075 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
8076 else
8077 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
8078 imm_expr.X_op = O_absent;
8079 s = expr_end;
8080 continue;
8081
8082 case 'c': /* break code */
8083 my_getExpression (&imm_expr, s);
8084 check_absolute_expr (ip, &imm_expr);
8085 if ((unsigned long) imm_expr.X_add_number > 1023)
8086 as_warn (_("Illegal break code (%lu)"),
8087 (unsigned long) imm_expr.X_add_number);
8088 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
8089 imm_expr.X_op = O_absent;
8090 s = expr_end;
8091 continue;
8092
8093 case 'q': /* lower break code */
8094 my_getExpression (&imm_expr, s);
8095 check_absolute_expr (ip, &imm_expr);
8096 if ((unsigned long) imm_expr.X_add_number > 1023)
8097 as_warn (_("Illegal lower break code (%lu)"),
8098 (unsigned long) imm_expr.X_add_number);
8099 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
8100 imm_expr.X_op = O_absent;
8101 s = expr_end;
8102 continue;
8103
8104 case 'B': /* 20-bit syscall/break code. */
8105 my_getExpression (&imm_expr, s);
8106 check_absolute_expr (ip, &imm_expr);
8107 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8108 as_warn (_("Illegal 20-bit code (%lu)"),
8109 (unsigned long) imm_expr.X_add_number);
8110 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
8111 imm_expr.X_op = O_absent;
8112 s = expr_end;
8113 continue;
8114
8115 case 'C': /* Coprocessor code */
8116 my_getExpression (&imm_expr, s);
8117 check_absolute_expr (ip, &imm_expr);
8118 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8119 {
8120 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8121 (unsigned long) imm_expr.X_add_number);
8122 imm_expr.X_add_number &= ((1 << 25) - 1);
8123 }
8124 ip->insn_opcode |= imm_expr.X_add_number;
8125 imm_expr.X_op = O_absent;
8126 s = expr_end;
8127 continue;
8128
8129 case 'J': /* 19-bit wait code. */
8130 my_getExpression (&imm_expr, s);
8131 check_absolute_expr (ip, &imm_expr);
8132 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8133 as_warn (_("Illegal 19-bit code (%lu)"),
8134 (unsigned long) imm_expr.X_add_number);
8135 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
8136 imm_expr.X_op = O_absent;
8137 s = expr_end;
8138 continue;
8139
8140 case 'P': /* Performance register */
8141 my_getExpression (&imm_expr, s);
8142 check_absolute_expr (ip, &imm_expr);
8143 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8144 as_warn (_("Invalid performance register (%lu)"),
8145 (unsigned long) imm_expr.X_add_number);
8146 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
8147 imm_expr.X_op = O_absent;
8148 s = expr_end;
8149 continue;
8150
8151 case 'b': /* base register */
8152 case 'd': /* destination register */
8153 case 's': /* source register */
8154 case 't': /* target register */
8155 case 'r': /* both target and source */
8156 case 'v': /* both dest and source */
8157 case 'w': /* both dest and target */
8158 case 'E': /* coprocessor target register */
8159 case 'G': /* coprocessor destination register */
8160 case 'K': /* 'rdhwr' destination register */
8161 case 'x': /* ignore register name */
8162 case 'z': /* must be zero register */
8163 case 'U': /* destination register (clo/clz). */
8164 s_reset = s;
8165 if (s[0] == '$')
8166 {
8167
8168 if (ISDIGIT (s[1]))
8169 {
8170 ++s;
8171 regno = 0;
8172 do
8173 {
8174 regno *= 10;
8175 regno += *s - '0';
8176 ++s;
8177 }
8178 while (ISDIGIT (*s));
8179 if (regno > 31)
8180 as_bad (_("Invalid register number (%d)"), regno);
8181 }
8182 else if (*args == 'E' || *args == 'G' || *args == 'K')
8183 goto notreg;
8184 else
8185 {
8186 if (s[1] == 'r' && s[2] == 'a')
8187 {
8188 s += 3;
8189 regno = RA;
8190 }
8191 else if (s[1] == 'f' && s[2] == 'p')
8192 {
8193 s += 3;
8194 regno = FP;
8195 }
8196 else if (s[1] == 's' && s[2] == 'p')
8197 {
8198 s += 3;
8199 regno = SP;
8200 }
8201 else if (s[1] == 'g' && s[2] == 'p')
8202 {
8203 s += 3;
8204 regno = GP;
8205 }
8206 else if (s[1] == 'a' && s[2] == 't')
8207 {
8208 s += 3;
8209 regno = AT;
8210 }
8211 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8212 {
8213 s += 4;
8214 regno = KT0;
8215 }
8216 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8217 {
8218 s += 4;
8219 regno = KT1;
8220 }
8221 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8222 {
8223 s += 5;
8224 regno = ZERO;
8225 }
8226 else if (itbl_have_entries)
8227 {
8228 char *p, *n;
8229 unsigned long r;
8230
8231 p = s + 1; /* advance past '$' */
8232 n = itbl_get_field (&p); /* n is name */
8233
8234 /* See if this is a register defined in an
8235 itbl entry. */
8236 if (itbl_get_reg_val (n, &r))
8237 {
8238 /* Get_field advances to the start of
8239 the next field, so we need to back
8240 rack to the end of the last field. */
8241 if (p)
8242 s = p - 1;
8243 else
8244 s = strchr (s, '\0');
8245 regno = r;
8246 }
8247 else
8248 goto notreg;
8249 }
8250 else
8251 goto notreg;
8252 }
8253 if (regno == AT
8254 && ! mips_opts.noat
8255 && *args != 'E'
8256 && *args != 'G'
8257 && *args != 'K')
8258 as_warn (_("Used $at without \".set noat\""));
8259 c = *args;
8260 if (*s == ' ')
8261 ++s;
8262 if (args[1] != *s)
8263 {
8264 if (c == 'r' || c == 'v' || c == 'w')
8265 {
8266 regno = lastregno;
8267 s = s_reset;
8268 ++args;
8269 }
8270 }
8271 /* 'z' only matches $0. */
8272 if (c == 'z' && regno != 0)
8273 break;
8274
8275 /* Now that we have assembled one operand, we use the args string
8276 * to figure out where it goes in the instruction. */
8277 switch (c)
8278 {
8279 case 'r':
8280 case 's':
8281 case 'v':
8282 case 'b':
8283 INSERT_OPERAND (RS, *ip, regno);
8284 break;
8285 case 'd':
8286 case 'G':
8287 case 'K':
8288 INSERT_OPERAND (RD, *ip, regno);
8289 break;
8290 case 'U':
8291 INSERT_OPERAND (RD, *ip, regno);
8292 INSERT_OPERAND (RT, *ip, regno);
8293 break;
8294 case 'w':
8295 case 't':
8296 case 'E':
8297 INSERT_OPERAND (RT, *ip, regno);
8298 break;
8299 case 'x':
8300 /* This case exists because on the r3000 trunc
8301 expands into a macro which requires a gp
8302 register. On the r6000 or r4000 it is
8303 assembled into a single instruction which
8304 ignores the register. Thus the insn version
8305 is MIPS_ISA2 and uses 'x', and the macro
8306 version is MIPS_ISA1 and uses 't'. */
8307 break;
8308 case 'z':
8309 /* This case is for the div instruction, which
8310 acts differently if the destination argument
8311 is $0. This only matches $0, and is checked
8312 outside the switch. */
8313 break;
8314 case 'D':
8315 /* Itbl operand; not yet implemented. FIXME ?? */
8316 break;
8317 /* What about all other operands like 'i', which
8318 can be specified in the opcode table? */
8319 }
8320 lastregno = regno;
8321 continue;
8322 }
8323 notreg:
8324 switch (*args++)
8325 {
8326 case 'r':
8327 case 'v':
8328 INSERT_OPERAND (RS, *ip, lastregno);
8329 continue;
8330 case 'w':
8331 INSERT_OPERAND (RT, *ip, lastregno);
8332 continue;
8333 }
8334 break;
8335
8336 case 'O': /* MDMX alignment immediate constant. */
8337 my_getExpression (&imm_expr, s);
8338 check_absolute_expr (ip, &imm_expr);
8339 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8340 as_warn ("Improper align amount (%ld), using low bits",
8341 (long) imm_expr.X_add_number);
8342 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
8343 imm_expr.X_op = O_absent;
8344 s = expr_end;
8345 continue;
8346
8347 case 'Q': /* MDMX vector, element sel, or const. */
8348 if (s[0] != '$')
8349 {
8350 /* MDMX Immediate. */
8351 my_getExpression (&imm_expr, s);
8352 check_absolute_expr (ip, &imm_expr);
8353 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8354 as_warn (_("Invalid MDMX Immediate (%ld)"),
8355 (long) imm_expr.X_add_number);
8356 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
8357 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8358 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8359 else
8360 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8361 imm_expr.X_op = O_absent;
8362 s = expr_end;
8363 continue;
8364 }
8365 /* Not MDMX Immediate. Fall through. */
8366 case 'X': /* MDMX destination register. */
8367 case 'Y': /* MDMX source register. */
8368 case 'Z': /* MDMX target register. */
8369 is_mdmx = 1;
8370 case 'D': /* floating point destination register */
8371 case 'S': /* floating point source register */
8372 case 'T': /* floating point target register */
8373 case 'R': /* floating point source register */
8374 case 'V':
8375 case 'W':
8376 s_reset = s;
8377 /* Accept $fN for FP and MDMX register numbers, and in
8378 addition accept $vN for MDMX register numbers. */
8379 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8380 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8381 && ISDIGIT (s[2])))
8382 {
8383 s += 2;
8384 regno = 0;
8385 do
8386 {
8387 regno *= 10;
8388 regno += *s - '0';
8389 ++s;
8390 }
8391 while (ISDIGIT (*s));
8392
8393 if (regno > 31)
8394 as_bad (_("Invalid float register number (%d)"), regno);
8395
8396 if ((regno & 1) != 0
8397 && HAVE_32BIT_FPRS
8398 && ! (strcmp (str, "mtc1") == 0
8399 || strcmp (str, "mfc1") == 0
8400 || strcmp (str, "lwc1") == 0
8401 || strcmp (str, "swc1") == 0
8402 || strcmp (str, "l.s") == 0
8403 || strcmp (str, "s.s") == 0))
8404 as_warn (_("Float register should be even, was %d"),
8405 regno);
8406
8407 c = *args;
8408 if (*s == ' ')
8409 ++s;
8410 if (args[1] != *s)
8411 {
8412 if (c == 'V' || c == 'W')
8413 {
8414 regno = lastregno;
8415 s = s_reset;
8416 ++args;
8417 }
8418 }
8419 switch (c)
8420 {
8421 case 'D':
8422 case 'X':
8423 INSERT_OPERAND (FD, *ip, regno);
8424 break;
8425 case 'V':
8426 case 'S':
8427 case 'Y':
8428 INSERT_OPERAND (FS, *ip, regno);
8429 break;
8430 case 'Q':
8431 /* This is like 'Z', but also needs to fix the MDMX
8432 vector/scalar select bits. Note that the
8433 scalar immediate case is handled above. */
8434 if (*s == '[')
8435 {
8436 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8437 int max_el = (is_qh ? 3 : 7);
8438 s++;
8439 my_getExpression(&imm_expr, s);
8440 check_absolute_expr (ip, &imm_expr);
8441 s = expr_end;
8442 if (imm_expr.X_add_number > max_el)
8443 as_bad(_("Bad element selector %ld"),
8444 (long) imm_expr.X_add_number);
8445 imm_expr.X_add_number &= max_el;
8446 ip->insn_opcode |= (imm_expr.X_add_number
8447 << (OP_SH_VSEL +
8448 (is_qh ? 2 : 1)));
8449 imm_expr.X_op = O_absent;
8450 if (*s != ']')
8451 as_warn(_("Expecting ']' found '%s'"), s);
8452 else
8453 s++;
8454 }
8455 else
8456 {
8457 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8458 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8459 << OP_SH_VSEL);
8460 else
8461 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8462 OP_SH_VSEL);
8463 }
8464 /* Fall through */
8465 case 'W':
8466 case 'T':
8467 case 'Z':
8468 INSERT_OPERAND (FT, *ip, regno);
8469 break;
8470 case 'R':
8471 INSERT_OPERAND (FR, *ip, regno);
8472 break;
8473 }
8474 lastregno = regno;
8475 continue;
8476 }
8477
8478 switch (*args++)
8479 {
8480 case 'V':
8481 INSERT_OPERAND (FS, *ip, lastregno);
8482 continue;
8483 case 'W':
8484 INSERT_OPERAND (FT, *ip, lastregno);
8485 continue;
8486 }
8487 break;
8488
8489 case 'I':
8490 my_getExpression (&imm_expr, s);
8491 if (imm_expr.X_op != O_big
8492 && imm_expr.X_op != O_constant)
8493 insn_error = _("absolute expression required");
8494 normalize_constant_expr (&imm_expr);
8495 s = expr_end;
8496 continue;
8497
8498 case 'A':
8499 my_getExpression (&offset_expr, s);
8500 *imm_reloc = BFD_RELOC_32;
8501 s = expr_end;
8502 continue;
8503
8504 case 'F':
8505 case 'L':
8506 case 'f':
8507 case 'l':
8508 {
8509 int f64;
8510 int using_gprs;
8511 char *save_in;
8512 char *err;
8513 unsigned char temp[8];
8514 int len;
8515 unsigned int length;
8516 segT seg;
8517 subsegT subseg;
8518 char *p;
8519
8520 /* These only appear as the last operand in an
8521 instruction, and every instruction that accepts
8522 them in any variant accepts them in all variants.
8523 This means we don't have to worry about backing out
8524 any changes if the instruction does not match.
8525
8526 The difference between them is the size of the
8527 floating point constant and where it goes. For 'F'
8528 and 'L' the constant is 64 bits; for 'f' and 'l' it
8529 is 32 bits. Where the constant is placed is based
8530 on how the MIPS assembler does things:
8531 F -- .rdata
8532 L -- .lit8
8533 f -- immediate value
8534 l -- .lit4
8535
8536 The .lit4 and .lit8 sections are only used if
8537 permitted by the -G argument.
8538
8539 The code below needs to know whether the target register
8540 is 32 or 64 bits wide. It relies on the fact 'f' and
8541 'F' are used with GPR-based instructions and 'l' and
8542 'L' are used with FPR-based instructions. */
8543
8544 f64 = *args == 'F' || *args == 'L';
8545 using_gprs = *args == 'F' || *args == 'f';
8546
8547 save_in = input_line_pointer;
8548 input_line_pointer = s;
8549 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8550 length = len;
8551 s = input_line_pointer;
8552 input_line_pointer = save_in;
8553 if (err != NULL && *err != '\0')
8554 {
8555 as_bad (_("Bad floating point constant: %s"), err);
8556 memset (temp, '\0', sizeof temp);
8557 length = f64 ? 8 : 4;
8558 }
8559
8560 assert (length == (unsigned) (f64 ? 8 : 4));
8561
8562 if (*args == 'f'
8563 || (*args == 'l'
8564 && (g_switch_value < 4
8565 || (temp[0] == 0 && temp[1] == 0)
8566 || (temp[2] == 0 && temp[3] == 0))))
8567 {
8568 imm_expr.X_op = O_constant;
8569 if (! target_big_endian)
8570 imm_expr.X_add_number = bfd_getl32 (temp);
8571 else
8572 imm_expr.X_add_number = bfd_getb32 (temp);
8573 }
8574 else if (length > 4
8575 && ! mips_disable_float_construction
8576 /* Constants can only be constructed in GPRs and
8577 copied to FPRs if the GPRs are at least as wide
8578 as the FPRs. Force the constant into memory if
8579 we are using 64-bit FPRs but the GPRs are only
8580 32 bits wide. */
8581 && (using_gprs
8582 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8583 && ((temp[0] == 0 && temp[1] == 0)
8584 || (temp[2] == 0 && temp[3] == 0))
8585 && ((temp[4] == 0 && temp[5] == 0)
8586 || (temp[6] == 0 && temp[7] == 0)))
8587 {
8588 /* The value is simple enough to load with a couple of
8589 instructions. If using 32-bit registers, set
8590 imm_expr to the high order 32 bits and offset_expr to
8591 the low order 32 bits. Otherwise, set imm_expr to
8592 the entire 64 bit constant. */
8593 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8594 {
8595 imm_expr.X_op = O_constant;
8596 offset_expr.X_op = O_constant;
8597 if (! target_big_endian)
8598 {
8599 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8600 offset_expr.X_add_number = bfd_getl32 (temp);
8601 }
8602 else
8603 {
8604 imm_expr.X_add_number = bfd_getb32 (temp);
8605 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8606 }
8607 if (offset_expr.X_add_number == 0)
8608 offset_expr.X_op = O_absent;
8609 }
8610 else if (sizeof (imm_expr.X_add_number) > 4)
8611 {
8612 imm_expr.X_op = O_constant;
8613 if (! target_big_endian)
8614 imm_expr.X_add_number = bfd_getl64 (temp);
8615 else
8616 imm_expr.X_add_number = bfd_getb64 (temp);
8617 }
8618 else
8619 {
8620 imm_expr.X_op = O_big;
8621 imm_expr.X_add_number = 4;
8622 if (! target_big_endian)
8623 {
8624 generic_bignum[0] = bfd_getl16 (temp);
8625 generic_bignum[1] = bfd_getl16 (temp + 2);
8626 generic_bignum[2] = bfd_getl16 (temp + 4);
8627 generic_bignum[3] = bfd_getl16 (temp + 6);
8628 }
8629 else
8630 {
8631 generic_bignum[0] = bfd_getb16 (temp + 6);
8632 generic_bignum[1] = bfd_getb16 (temp + 4);
8633 generic_bignum[2] = bfd_getb16 (temp + 2);
8634 generic_bignum[3] = bfd_getb16 (temp);
8635 }
8636 }
8637 }
8638 else
8639 {
8640 const char *newname;
8641 segT new_seg;
8642
8643 /* Switch to the right section. */
8644 seg = now_seg;
8645 subseg = now_subseg;
8646 switch (*args)
8647 {
8648 default: /* unused default case avoids warnings. */
8649 case 'L':
8650 newname = RDATA_SECTION_NAME;
8651 if (g_switch_value >= 8)
8652 newname = ".lit8";
8653 break;
8654 case 'F':
8655 newname = RDATA_SECTION_NAME;
8656 break;
8657 case 'l':
8658 assert (g_switch_value >= 4);
8659 newname = ".lit4";
8660 break;
8661 }
8662 new_seg = subseg_new (newname, (subsegT) 0);
8663 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8664 bfd_set_section_flags (stdoutput, new_seg,
8665 (SEC_ALLOC
8666 | SEC_LOAD
8667 | SEC_READONLY
8668 | SEC_DATA));
8669 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8670 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8671 && strcmp (TARGET_OS, "elf") != 0)
8672 record_alignment (new_seg, 4);
8673 else
8674 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8675 if (seg == now_seg)
8676 as_bad (_("Can't use floating point insn in this section"));
8677
8678 /* Set the argument to the current address in the
8679 section. */
8680 offset_expr.X_op = O_symbol;
8681 offset_expr.X_add_symbol =
8682 symbol_new ("L0\001", now_seg,
8683 (valueT) frag_now_fix (), frag_now);
8684 offset_expr.X_add_number = 0;
8685
8686 /* Put the floating point number into the section. */
8687 p = frag_more ((int) length);
8688 memcpy (p, temp, length);
8689
8690 /* Switch back to the original section. */
8691 subseg_set (seg, subseg);
8692 }
8693 }
8694 continue;
8695
8696 case 'i': /* 16 bit unsigned immediate */
8697 case 'j': /* 16 bit signed immediate */
8698 *imm_reloc = BFD_RELOC_LO16;
8699 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8700 {
8701 int more;
8702 offsetT minval, maxval;
8703
8704 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8705 && strcmp (insn->name, insn[1].name) == 0);
8706
8707 /* If the expression was written as an unsigned number,
8708 only treat it as signed if there are no more
8709 alternatives. */
8710 if (more
8711 && *args == 'j'
8712 && sizeof (imm_expr.X_add_number) <= 4
8713 && imm_expr.X_op == O_constant
8714 && imm_expr.X_add_number < 0
8715 && imm_expr.X_unsigned
8716 && HAVE_64BIT_GPRS)
8717 break;
8718
8719 /* For compatibility with older assemblers, we accept
8720 0x8000-0xffff as signed 16-bit numbers when only
8721 signed numbers are allowed. */
8722 if (*args == 'i')
8723 minval = 0, maxval = 0xffff;
8724 else if (more)
8725 minval = -0x8000, maxval = 0x7fff;
8726 else
8727 minval = -0x8000, maxval = 0xffff;
8728
8729 if (imm_expr.X_op != O_constant
8730 || imm_expr.X_add_number < minval
8731 || imm_expr.X_add_number > maxval)
8732 {
8733 if (more)
8734 break;
8735 if (imm_expr.X_op == O_constant
8736 || imm_expr.X_op == O_big)
8737 as_bad (_("expression out of range"));
8738 }
8739 }
8740 s = expr_end;
8741 continue;
8742
8743 case 'o': /* 16 bit offset */
8744 /* Check whether there is only a single bracketed expression
8745 left. If so, it must be the base register and the
8746 constant must be zero. */
8747 if (*s == '(' && strchr (s + 1, '(') == 0)
8748 {
8749 offset_expr.X_op = O_constant;
8750 offset_expr.X_add_number = 0;
8751 continue;
8752 }
8753
8754 /* If this value won't fit into a 16 bit offset, then go
8755 find a macro that will generate the 32 bit offset
8756 code pattern. */
8757 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8758 && (offset_expr.X_op != O_constant
8759 || offset_expr.X_add_number >= 0x8000
8760 || offset_expr.X_add_number < -0x8000))
8761 break;
8762
8763 s = expr_end;
8764 continue;
8765
8766 case 'p': /* pc relative offset */
8767 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8768 my_getExpression (&offset_expr, s);
8769 s = expr_end;
8770 continue;
8771
8772 case 'u': /* upper 16 bits */
8773 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8774 && imm_expr.X_op == O_constant
8775 && (imm_expr.X_add_number < 0
8776 || imm_expr.X_add_number >= 0x10000))
8777 as_bad (_("lui expression not in range 0..65535"));
8778 s = expr_end;
8779 continue;
8780
8781 case 'a': /* 26 bit address */
8782 my_getExpression (&offset_expr, s);
8783 s = expr_end;
8784 *offset_reloc = BFD_RELOC_MIPS_JMP;
8785 continue;
8786
8787 case 'N': /* 3 bit branch condition code */
8788 case 'M': /* 3 bit compare condition code */
8789 if (strncmp (s, "$fcc", 4) != 0)
8790 break;
8791 s += 4;
8792 regno = 0;
8793 do
8794 {
8795 regno *= 10;
8796 regno += *s - '0';
8797 ++s;
8798 }
8799 while (ISDIGIT (*s));
8800 if (regno > 7)
8801 as_bad (_("Invalid condition code register $fcc%d"), regno);
8802 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8803 || strcmp(str + strlen(str) - 5, "any2f") == 0
8804 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8805 && (regno & 1) != 0)
8806 as_warn(_("Condition code register should be even for %s, was %d"),
8807 str, regno);
8808 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8809 || strcmp(str + strlen(str) - 5, "any4t") == 0)
8810 && (regno & 3) != 0)
8811 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
8812 str, regno);
8813 if (*args == 'N')
8814 INSERT_OPERAND (BCC, *ip, regno);
8815 else
8816 INSERT_OPERAND (CCC, *ip, regno);
8817 continue;
8818
8819 case 'H':
8820 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8821 s += 2;
8822 if (ISDIGIT (*s))
8823 {
8824 c = 0;
8825 do
8826 {
8827 c *= 10;
8828 c += *s - '0';
8829 ++s;
8830 }
8831 while (ISDIGIT (*s));
8832 }
8833 else
8834 c = 8; /* Invalid sel value. */
8835
8836 if (c > 7)
8837 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8838 ip->insn_opcode |= c;
8839 continue;
8840
8841 case 'e':
8842 /* Must be at least one digit. */
8843 my_getExpression (&imm_expr, s);
8844 check_absolute_expr (ip, &imm_expr);
8845
8846 if ((unsigned long) imm_expr.X_add_number
8847 > (unsigned long) OP_MASK_VECBYTE)
8848 {
8849 as_bad (_("bad byte vector index (%ld)"),
8850 (long) imm_expr.X_add_number);
8851 imm_expr.X_add_number = 0;
8852 }
8853
8854 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
8855 imm_expr.X_op = O_absent;
8856 s = expr_end;
8857 continue;
8858
8859 case '%':
8860 my_getExpression (&imm_expr, s);
8861 check_absolute_expr (ip, &imm_expr);
8862
8863 if ((unsigned long) imm_expr.X_add_number
8864 > (unsigned long) OP_MASK_VECALIGN)
8865 {
8866 as_bad (_("bad byte vector index (%ld)"),
8867 (long) imm_expr.X_add_number);
8868 imm_expr.X_add_number = 0;
8869 }
8870
8871 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
8872 imm_expr.X_op = O_absent;
8873 s = expr_end;
8874 continue;
8875
8876 default:
8877 as_bad (_("bad char = '%c'\n"), *args);
8878 internalError ();
8879 }
8880 break;
8881 }
8882 /* Args don't match. */
8883 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8884 !strcmp (insn->name, insn[1].name))
8885 {
8886 ++insn;
8887 s = argsStart;
8888 insn_error = _("illegal operands");
8889 continue;
8890 }
8891 if (save_c)
8892 *(--s) = save_c;
8893 insn_error = _("illegal operands");
8894 return;
8895 }
8896 }
8897
8898 /* This routine assembles an instruction into its binary format when
8899 assembling for the mips16. As a side effect, it sets one of the
8900 global variables imm_reloc or offset_reloc to the type of
8901 relocation to do if one of the operands is an address expression.
8902 It also sets mips16_small and mips16_ext if the user explicitly
8903 requested a small or extended instruction. */
8904
8905 static void
8906 mips16_ip (char *str, struct mips_cl_insn *ip)
8907 {
8908 char *s;
8909 const char *args;
8910 struct mips_opcode *insn;
8911 char *argsstart;
8912 unsigned int regno;
8913 unsigned int lastregno = 0;
8914 char *s_reset;
8915 size_t i;
8916
8917 insn_error = NULL;
8918
8919 mips16_small = FALSE;
8920 mips16_ext = FALSE;
8921
8922 for (s = str; ISLOWER (*s); ++s)
8923 ;
8924 switch (*s)
8925 {
8926 case '\0':
8927 break;
8928
8929 case ' ':
8930 *s++ = '\0';
8931 break;
8932
8933 case '.':
8934 if (s[1] == 't' && s[2] == ' ')
8935 {
8936 *s = '\0';
8937 mips16_small = TRUE;
8938 s += 3;
8939 break;
8940 }
8941 else if (s[1] == 'e' && s[2] == ' ')
8942 {
8943 *s = '\0';
8944 mips16_ext = TRUE;
8945 s += 3;
8946 break;
8947 }
8948 /* Fall through. */
8949 default:
8950 insn_error = _("unknown opcode");
8951 return;
8952 }
8953
8954 if (mips_opts.noautoextend && ! mips16_ext)
8955 mips16_small = TRUE;
8956
8957 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8958 {
8959 insn_error = _("unrecognized opcode");
8960 return;
8961 }
8962
8963 argsstart = s;
8964 for (;;)
8965 {
8966 assert (strcmp (insn->name, str) == 0);
8967
8968 create_insn (ip, insn);
8969 imm_expr.X_op = O_absent;
8970 imm_reloc[0] = BFD_RELOC_UNUSED;
8971 imm_reloc[1] = BFD_RELOC_UNUSED;
8972 imm_reloc[2] = BFD_RELOC_UNUSED;
8973 imm2_expr.X_op = O_absent;
8974 offset_expr.X_op = O_absent;
8975 offset_reloc[0] = BFD_RELOC_UNUSED;
8976 offset_reloc[1] = BFD_RELOC_UNUSED;
8977 offset_reloc[2] = BFD_RELOC_UNUSED;
8978 for (args = insn->args; 1; ++args)
8979 {
8980 int c;
8981
8982 if (*s == ' ')
8983 ++s;
8984
8985 /* In this switch statement we call break if we did not find
8986 a match, continue if we did find a match, or return if we
8987 are done. */
8988
8989 c = *args;
8990 switch (c)
8991 {
8992 case '\0':
8993 if (*s == '\0')
8994 {
8995 /* Stuff the immediate value in now, if we can. */
8996 if (imm_expr.X_op == O_constant
8997 && *imm_reloc > BFD_RELOC_UNUSED
8998 && insn->pinfo != INSN_MACRO)
8999 {
9000 valueT tmp;
9001
9002 switch (*offset_reloc)
9003 {
9004 case BFD_RELOC_MIPS16_HI16_S:
9005 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9006 break;
9007
9008 case BFD_RELOC_MIPS16_HI16:
9009 tmp = imm_expr.X_add_number >> 16;
9010 break;
9011
9012 case BFD_RELOC_MIPS16_LO16:
9013 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9014 - 0x8000;
9015 break;
9016
9017 case BFD_RELOC_UNUSED:
9018 tmp = imm_expr.X_add_number;
9019 break;
9020
9021 default:
9022 internalError ();
9023 }
9024 *offset_reloc = BFD_RELOC_UNUSED;
9025
9026 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9027 tmp, TRUE, mips16_small,
9028 mips16_ext, &ip->insn_opcode,
9029 &ip->use_extend, &ip->extend);
9030 imm_expr.X_op = O_absent;
9031 *imm_reloc = BFD_RELOC_UNUSED;
9032 }
9033
9034 return;
9035 }
9036 break;
9037
9038 case ',':
9039 if (*s++ == c)
9040 continue;
9041 s--;
9042 switch (*++args)
9043 {
9044 case 'v':
9045 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9046 continue;
9047 case 'w':
9048 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9049 continue;
9050 }
9051 break;
9052
9053 case '(':
9054 case ')':
9055 if (*s++ == c)
9056 continue;
9057 break;
9058
9059 case 'v':
9060 case 'w':
9061 if (s[0] != '$')
9062 {
9063 if (c == 'v')
9064 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
9065 else
9066 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
9067 ++args;
9068 continue;
9069 }
9070 /* Fall through. */
9071 case 'x':
9072 case 'y':
9073 case 'z':
9074 case 'Z':
9075 case '0':
9076 case 'S':
9077 case 'R':
9078 case 'X':
9079 case 'Y':
9080 if (s[0] != '$')
9081 break;
9082 s_reset = s;
9083 if (ISDIGIT (s[1]))
9084 {
9085 ++s;
9086 regno = 0;
9087 do
9088 {
9089 regno *= 10;
9090 regno += *s - '0';
9091 ++s;
9092 }
9093 while (ISDIGIT (*s));
9094 if (regno > 31)
9095 {
9096 as_bad (_("invalid register number (%d)"), regno);
9097 regno = 2;
9098 }
9099 }
9100 else
9101 {
9102 if (s[1] == 'r' && s[2] == 'a')
9103 {
9104 s += 3;
9105 regno = RA;
9106 }
9107 else if (s[1] == 'f' && s[2] == 'p')
9108 {
9109 s += 3;
9110 regno = FP;
9111 }
9112 else if (s[1] == 's' && s[2] == 'p')
9113 {
9114 s += 3;
9115 regno = SP;
9116 }
9117 else if (s[1] == 'g' && s[2] == 'p')
9118 {
9119 s += 3;
9120 regno = GP;
9121 }
9122 else if (s[1] == 'a' && s[2] == 't')
9123 {
9124 s += 3;
9125 regno = AT;
9126 }
9127 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9128 {
9129 s += 4;
9130 regno = KT0;
9131 }
9132 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9133 {
9134 s += 4;
9135 regno = KT1;
9136 }
9137 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9138 {
9139 s += 5;
9140 regno = ZERO;
9141 }
9142 else
9143 break;
9144 }
9145
9146 if (*s == ' ')
9147 ++s;
9148 if (args[1] != *s)
9149 {
9150 if (c == 'v' || c == 'w')
9151 {
9152 regno = mips16_to_32_reg_map[lastregno];
9153 s = s_reset;
9154 ++args;
9155 }
9156 }
9157
9158 switch (c)
9159 {
9160 case 'x':
9161 case 'y':
9162 case 'z':
9163 case 'v':
9164 case 'w':
9165 case 'Z':
9166 regno = mips32_to_16_reg_map[regno];
9167 break;
9168
9169 case '0':
9170 if (regno != 0)
9171 regno = ILLEGAL_REG;
9172 break;
9173
9174 case 'S':
9175 if (regno != SP)
9176 regno = ILLEGAL_REG;
9177 break;
9178
9179 case 'R':
9180 if (regno != RA)
9181 regno = ILLEGAL_REG;
9182 break;
9183
9184 case 'X':
9185 case 'Y':
9186 if (regno == AT && ! mips_opts.noat)
9187 as_warn (_("used $at without \".set noat\""));
9188 break;
9189
9190 default:
9191 internalError ();
9192 }
9193
9194 if (regno == ILLEGAL_REG)
9195 break;
9196
9197 switch (c)
9198 {
9199 case 'x':
9200 case 'v':
9201 MIPS16_INSERT_OPERAND (RX, *ip, regno);
9202 break;
9203 case 'y':
9204 case 'w':
9205 MIPS16_INSERT_OPERAND (RY, *ip, regno);
9206 break;
9207 case 'z':
9208 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
9209 break;
9210 case 'Z':
9211 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
9212 case '0':
9213 case 'S':
9214 case 'R':
9215 break;
9216 case 'X':
9217 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
9218 break;
9219 case 'Y':
9220 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9221 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
9222 break;
9223 default:
9224 internalError ();
9225 }
9226
9227 lastregno = regno;
9228 continue;
9229
9230 case 'P':
9231 if (strncmp (s, "$pc", 3) == 0)
9232 {
9233 s += 3;
9234 continue;
9235 }
9236 break;
9237
9238 case '5':
9239 case 'H':
9240 case 'W':
9241 case 'D':
9242 case 'j':
9243 case 'V':
9244 case 'C':
9245 case 'U':
9246 case 'k':
9247 case 'K':
9248 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9249 if (i > 0)
9250 {
9251 if (imm_expr.X_op != O_constant)
9252 {
9253 mips16_ext = TRUE;
9254 ip->use_extend = TRUE;
9255 ip->extend = 0;
9256 }
9257 else
9258 {
9259 /* We need to relax this instruction. */
9260 *offset_reloc = *imm_reloc;
9261 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9262 }
9263 s = expr_end;
9264 continue;
9265 }
9266 *imm_reloc = BFD_RELOC_UNUSED;
9267 /* Fall through. */
9268 case '<':
9269 case '>':
9270 case '[':
9271 case ']':
9272 case '4':
9273 case '8':
9274 my_getExpression (&imm_expr, s);
9275 if (imm_expr.X_op == O_register)
9276 {
9277 /* What we thought was an expression turned out to
9278 be a register. */
9279
9280 if (s[0] == '(' && args[1] == '(')
9281 {
9282 /* It looks like the expression was omitted
9283 before a register indirection, which means
9284 that the expression is implicitly zero. We
9285 still set up imm_expr, so that we handle
9286 explicit extensions correctly. */
9287 imm_expr.X_op = O_constant;
9288 imm_expr.X_add_number = 0;
9289 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9290 continue;
9291 }
9292
9293 break;
9294 }
9295
9296 /* We need to relax this instruction. */
9297 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9298 s = expr_end;
9299 continue;
9300
9301 case 'p':
9302 case 'q':
9303 case 'A':
9304 case 'B':
9305 case 'E':
9306 /* We use offset_reloc rather than imm_reloc for the PC
9307 relative operands. This lets macros with both
9308 immediate and address operands work correctly. */
9309 my_getExpression (&offset_expr, s);
9310
9311 if (offset_expr.X_op == O_register)
9312 break;
9313
9314 /* We need to relax this instruction. */
9315 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9316 s = expr_end;
9317 continue;
9318
9319 case '6': /* break code */
9320 my_getExpression (&imm_expr, s);
9321 check_absolute_expr (ip, &imm_expr);
9322 if ((unsigned long) imm_expr.X_add_number > 63)
9323 as_warn (_("Invalid value for `%s' (%lu)"),
9324 ip->insn_mo->name,
9325 (unsigned long) imm_expr.X_add_number);
9326 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
9327 imm_expr.X_op = O_absent;
9328 s = expr_end;
9329 continue;
9330
9331 case 'a': /* 26 bit address */
9332 my_getExpression (&offset_expr, s);
9333 s = expr_end;
9334 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9335 ip->insn_opcode <<= 16;
9336 continue;
9337
9338 case 'l': /* register list for entry macro */
9339 case 'L': /* register list for exit macro */
9340 {
9341 int mask;
9342
9343 if (c == 'l')
9344 mask = 0;
9345 else
9346 mask = 7 << 3;
9347 while (*s != '\0')
9348 {
9349 int freg, reg1, reg2;
9350
9351 while (*s == ' ' || *s == ',')
9352 ++s;
9353 if (*s != '$')
9354 {
9355 as_bad (_("can't parse register list"));
9356 break;
9357 }
9358 ++s;
9359 if (*s != 'f')
9360 freg = 0;
9361 else
9362 {
9363 freg = 1;
9364 ++s;
9365 }
9366 reg1 = 0;
9367 while (ISDIGIT (*s))
9368 {
9369 reg1 *= 10;
9370 reg1 += *s - '0';
9371 ++s;
9372 }
9373 if (*s == ' ')
9374 ++s;
9375 if (*s != '-')
9376 reg2 = reg1;
9377 else
9378 {
9379 ++s;
9380 if (*s != '$')
9381 break;
9382 ++s;
9383 if (freg)
9384 {
9385 if (*s == 'f')
9386 ++s;
9387 else
9388 {
9389 as_bad (_("invalid register list"));
9390 break;
9391 }
9392 }
9393 reg2 = 0;
9394 while (ISDIGIT (*s))
9395 {
9396 reg2 *= 10;
9397 reg2 += *s - '0';
9398 ++s;
9399 }
9400 }
9401 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9402 {
9403 mask &= ~ (7 << 3);
9404 mask |= 5 << 3;
9405 }
9406 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9407 {
9408 mask &= ~ (7 << 3);
9409 mask |= 6 << 3;
9410 }
9411 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9412 mask |= (reg2 - 3) << 3;
9413 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9414 mask |= (reg2 - 15) << 1;
9415 else if (reg1 == RA && reg2 == RA)
9416 mask |= 1;
9417 else
9418 {
9419 as_bad (_("invalid register list"));
9420 break;
9421 }
9422 }
9423 /* The mask is filled in in the opcode table for the
9424 benefit of the disassembler. We remove it before
9425 applying the actual mask. */
9426 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9427 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9428 }
9429 continue;
9430
9431 case 'e': /* extend code */
9432 my_getExpression (&imm_expr, s);
9433 check_absolute_expr (ip, &imm_expr);
9434 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9435 {
9436 as_warn (_("Invalid value for `%s' (%lu)"),
9437 ip->insn_mo->name,
9438 (unsigned long) imm_expr.X_add_number);
9439 imm_expr.X_add_number &= 0x7ff;
9440 }
9441 ip->insn_opcode |= imm_expr.X_add_number;
9442 imm_expr.X_op = O_absent;
9443 s = expr_end;
9444 continue;
9445
9446 default:
9447 internalError ();
9448 }
9449 break;
9450 }
9451
9452 /* Args don't match. */
9453 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9454 strcmp (insn->name, insn[1].name) == 0)
9455 {
9456 ++insn;
9457 s = argsstart;
9458 continue;
9459 }
9460
9461 insn_error = _("illegal operands");
9462
9463 return;
9464 }
9465 }
9466
9467 /* This structure holds information we know about a mips16 immediate
9468 argument type. */
9469
9470 struct mips16_immed_operand
9471 {
9472 /* The type code used in the argument string in the opcode table. */
9473 int type;
9474 /* The number of bits in the short form of the opcode. */
9475 int nbits;
9476 /* The number of bits in the extended form of the opcode. */
9477 int extbits;
9478 /* The amount by which the short form is shifted when it is used;
9479 for example, the sw instruction has a shift count of 2. */
9480 int shift;
9481 /* The amount by which the short form is shifted when it is stored
9482 into the instruction code. */
9483 int op_shift;
9484 /* Non-zero if the short form is unsigned. */
9485 int unsp;
9486 /* Non-zero if the extended form is unsigned. */
9487 int extu;
9488 /* Non-zero if the value is PC relative. */
9489 int pcrel;
9490 };
9491
9492 /* The mips16 immediate operand types. */
9493
9494 static const struct mips16_immed_operand mips16_immed_operands[] =
9495 {
9496 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9497 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9498 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9499 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9500 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9501 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9502 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9503 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9504 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9505 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9506 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9507 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9508 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9509 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9510 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9511 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9512 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9513 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9514 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9515 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9516 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9517 };
9518
9519 #define MIPS16_NUM_IMMED \
9520 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9521
9522 /* Handle a mips16 instruction with an immediate value. This or's the
9523 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9524 whether an extended value is needed; if one is needed, it sets
9525 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9526 If SMALL is true, an unextended opcode was explicitly requested.
9527 If EXT is true, an extended opcode was explicitly requested. If
9528 WARN is true, warn if EXT does not match reality. */
9529
9530 static void
9531 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9532 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9533 unsigned long *insn, bfd_boolean *use_extend,
9534 unsigned short *extend)
9535 {
9536 register const struct mips16_immed_operand *op;
9537 int mintiny, maxtiny;
9538 bfd_boolean needext;
9539
9540 op = mips16_immed_operands;
9541 while (op->type != type)
9542 {
9543 ++op;
9544 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9545 }
9546
9547 if (op->unsp)
9548 {
9549 if (type == '<' || type == '>' || type == '[' || type == ']')
9550 {
9551 mintiny = 1;
9552 maxtiny = 1 << op->nbits;
9553 }
9554 else
9555 {
9556 mintiny = 0;
9557 maxtiny = (1 << op->nbits) - 1;
9558 }
9559 }
9560 else
9561 {
9562 mintiny = - (1 << (op->nbits - 1));
9563 maxtiny = (1 << (op->nbits - 1)) - 1;
9564 }
9565
9566 /* Branch offsets have an implicit 0 in the lowest bit. */
9567 if (type == 'p' || type == 'q')
9568 val /= 2;
9569
9570 if ((val & ((1 << op->shift) - 1)) != 0
9571 || val < (mintiny << op->shift)
9572 || val > (maxtiny << op->shift))
9573 needext = TRUE;
9574 else
9575 needext = FALSE;
9576
9577 if (warn && ext && ! needext)
9578 as_warn_where (file, line,
9579 _("extended operand requested but not required"));
9580 if (small && needext)
9581 as_bad_where (file, line, _("invalid unextended operand value"));
9582
9583 if (small || (! ext && ! needext))
9584 {
9585 int insnval;
9586
9587 *use_extend = FALSE;
9588 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9589 insnval <<= op->op_shift;
9590 *insn |= insnval;
9591 }
9592 else
9593 {
9594 long minext, maxext;
9595 int extval;
9596
9597 if (op->extu)
9598 {
9599 minext = 0;
9600 maxext = (1 << op->extbits) - 1;
9601 }
9602 else
9603 {
9604 minext = - (1 << (op->extbits - 1));
9605 maxext = (1 << (op->extbits - 1)) - 1;
9606 }
9607 if (val < minext || val > maxext)
9608 as_bad_where (file, line,
9609 _("operand value out of range for instruction"));
9610
9611 *use_extend = TRUE;
9612 if (op->extbits == 16)
9613 {
9614 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9615 val &= 0x1f;
9616 }
9617 else if (op->extbits == 15)
9618 {
9619 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9620 val &= 0xf;
9621 }
9622 else
9623 {
9624 extval = ((val & 0x1f) << 6) | (val & 0x20);
9625 val = 0;
9626 }
9627
9628 *extend = (unsigned short) extval;
9629 *insn |= val;
9630 }
9631 }
9632 \f
9633 struct percent_op_match
9634 {
9635 const char *str;
9636 bfd_reloc_code_real_type reloc;
9637 };
9638
9639 static const struct percent_op_match mips_percent_op[] =
9640 {
9641 {"%lo", BFD_RELOC_LO16},
9642 #ifdef OBJ_ELF
9643 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9644 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9645 {"%call16", BFD_RELOC_MIPS_CALL16},
9646 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9647 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9648 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9649 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9650 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9651 {"%got", BFD_RELOC_MIPS_GOT16},
9652 {"%gp_rel", BFD_RELOC_GPREL16},
9653 {"%half", BFD_RELOC_16},
9654 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9655 {"%higher", BFD_RELOC_MIPS_HIGHER},
9656 {"%neg", BFD_RELOC_MIPS_SUB},
9657 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9658 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9659 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9660 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9661 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9662 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9663 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9664 #endif
9665 {"%hi", BFD_RELOC_HI16_S}
9666 };
9667
9668 static const struct percent_op_match mips16_percent_op[] =
9669 {
9670 {"%lo", BFD_RELOC_MIPS16_LO16},
9671 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9672 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9673 };
9674
9675
9676 /* Return true if *STR points to a relocation operator. When returning true,
9677 move *STR over the operator and store its relocation code in *RELOC.
9678 Leave both *STR and *RELOC alone when returning false. */
9679
9680 static bfd_boolean
9681 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9682 {
9683 const struct percent_op_match *percent_op;
9684 size_t limit, i;
9685
9686 if (mips_opts.mips16)
9687 {
9688 percent_op = mips16_percent_op;
9689 limit = ARRAY_SIZE (mips16_percent_op);
9690 }
9691 else
9692 {
9693 percent_op = mips_percent_op;
9694 limit = ARRAY_SIZE (mips_percent_op);
9695 }
9696
9697 for (i = 0; i < limit; i++)
9698 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9699 {
9700 int len = strlen (percent_op[i].str);
9701
9702 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9703 continue;
9704
9705 *str += strlen (percent_op[i].str);
9706 *reloc = percent_op[i].reloc;
9707
9708 /* Check whether the output BFD supports this relocation.
9709 If not, issue an error and fall back on something safe. */
9710 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9711 {
9712 as_bad ("relocation %s isn't supported by the current ABI",
9713 percent_op[i].str);
9714 *reloc = BFD_RELOC_UNUSED;
9715 }
9716 return TRUE;
9717 }
9718 return FALSE;
9719 }
9720
9721
9722 /* Parse string STR as a 16-bit relocatable operand. Store the
9723 expression in *EP and the relocations in the array starting
9724 at RELOC. Return the number of relocation operators used.
9725
9726 On exit, EXPR_END points to the first character after the expression. */
9727
9728 static size_t
9729 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9730 char *str)
9731 {
9732 bfd_reloc_code_real_type reversed_reloc[3];
9733 size_t reloc_index, i;
9734 int crux_depth, str_depth;
9735 char *crux;
9736
9737 /* Search for the start of the main expression, recoding relocations
9738 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9739 of the main expression and with CRUX_DEPTH containing the number
9740 of open brackets at that point. */
9741 reloc_index = -1;
9742 str_depth = 0;
9743 do
9744 {
9745 reloc_index++;
9746 crux = str;
9747 crux_depth = str_depth;
9748
9749 /* Skip over whitespace and brackets, keeping count of the number
9750 of brackets. */
9751 while (*str == ' ' || *str == '\t' || *str == '(')
9752 if (*str++ == '(')
9753 str_depth++;
9754 }
9755 while (*str == '%'
9756 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9757 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9758
9759 my_getExpression (ep, crux);
9760 str = expr_end;
9761
9762 /* Match every open bracket. */
9763 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9764 if (*str++ == ')')
9765 crux_depth--;
9766
9767 if (crux_depth > 0)
9768 as_bad ("unclosed '('");
9769
9770 expr_end = str;
9771
9772 if (reloc_index != 0)
9773 {
9774 prev_reloc_op_frag = frag_now;
9775 for (i = 0; i < reloc_index; i++)
9776 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9777 }
9778
9779 return reloc_index;
9780 }
9781
9782 static void
9783 my_getExpression (expressionS *ep, char *str)
9784 {
9785 char *save_in;
9786 valueT val;
9787
9788 save_in = input_line_pointer;
9789 input_line_pointer = str;
9790 expression (ep);
9791 expr_end = input_line_pointer;
9792 input_line_pointer = save_in;
9793
9794 /* If we are in mips16 mode, and this is an expression based on `.',
9795 then we bump the value of the symbol by 1 since that is how other
9796 text symbols are handled. We don't bother to handle complex
9797 expressions, just `.' plus or minus a constant. */
9798 if (mips_opts.mips16
9799 && ep->X_op == O_symbol
9800 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9801 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9802 && symbol_get_frag (ep->X_add_symbol) == frag_now
9803 && symbol_constant_p (ep->X_add_symbol)
9804 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9805 S_SET_VALUE (ep->X_add_symbol, val + 1);
9806 }
9807
9808 /* Turn a string in input_line_pointer into a floating point constant
9809 of type TYPE, and store the appropriate bytes in *LITP. The number
9810 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9811 returned, or NULL on OK. */
9812
9813 char *
9814 md_atof (int type, char *litP, int *sizeP)
9815 {
9816 int prec;
9817 LITTLENUM_TYPE words[4];
9818 char *t;
9819 int i;
9820
9821 switch (type)
9822 {
9823 case 'f':
9824 prec = 2;
9825 break;
9826
9827 case 'd':
9828 prec = 4;
9829 break;
9830
9831 default:
9832 *sizeP = 0;
9833 return _("bad call to md_atof");
9834 }
9835
9836 t = atof_ieee (input_line_pointer, type, words);
9837 if (t)
9838 input_line_pointer = t;
9839
9840 *sizeP = prec * 2;
9841
9842 if (! target_big_endian)
9843 {
9844 for (i = prec - 1; i >= 0; i--)
9845 {
9846 md_number_to_chars (litP, words[i], 2);
9847 litP += 2;
9848 }
9849 }
9850 else
9851 {
9852 for (i = 0; i < prec; i++)
9853 {
9854 md_number_to_chars (litP, words[i], 2);
9855 litP += 2;
9856 }
9857 }
9858
9859 return NULL;
9860 }
9861
9862 void
9863 md_number_to_chars (char *buf, valueT val, int n)
9864 {
9865 if (target_big_endian)
9866 number_to_chars_bigendian (buf, val, n);
9867 else
9868 number_to_chars_littleendian (buf, val, n);
9869 }
9870 \f
9871 #ifdef OBJ_ELF
9872 static int support_64bit_objects(void)
9873 {
9874 const char **list, **l;
9875 int yes;
9876
9877 list = bfd_target_list ();
9878 for (l = list; *l != NULL; l++)
9879 #ifdef TE_TMIPS
9880 /* This is traditional mips */
9881 if (strcmp (*l, "elf64-tradbigmips") == 0
9882 || strcmp (*l, "elf64-tradlittlemips") == 0)
9883 #else
9884 if (strcmp (*l, "elf64-bigmips") == 0
9885 || strcmp (*l, "elf64-littlemips") == 0)
9886 #endif
9887 break;
9888 yes = (*l != NULL);
9889 free (list);
9890 return yes;
9891 }
9892 #endif /* OBJ_ELF */
9893
9894 const char *md_shortopts = "O::g::G:";
9895
9896 struct option md_longopts[] =
9897 {
9898 /* Options which specify architecture. */
9899 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
9900 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
9901 {"march", required_argument, NULL, OPTION_MARCH},
9902 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
9903 {"mtune", required_argument, NULL, OPTION_MTUNE},
9904 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
9905 {"mips0", no_argument, NULL, OPTION_MIPS1},
9906 {"mips1", no_argument, NULL, OPTION_MIPS1},
9907 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
9908 {"mips2", no_argument, NULL, OPTION_MIPS2},
9909 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
9910 {"mips3", no_argument, NULL, OPTION_MIPS3},
9911 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
9912 {"mips4", no_argument, NULL, OPTION_MIPS4},
9913 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
9914 {"mips5", no_argument, NULL, OPTION_MIPS5},
9915 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
9916 {"mips32", no_argument, NULL, OPTION_MIPS32},
9917 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
9918 {"mips64", no_argument, NULL, OPTION_MIPS64},
9919 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
9920 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
9921 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
9922 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
9923
9924 /* Options which specify Application Specific Extensions (ASEs). */
9925 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
9926 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
9927 {"mips16", no_argument, NULL, OPTION_MIPS16},
9928 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
9929 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9930 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
9931 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9932 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
9933 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
9934 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
9935 {"mdmx", no_argument, NULL, OPTION_MDMX},
9936 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
9937 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
9938
9939 /* Old-style architecture options. Don't add more of these. */
9940 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
9941 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
9942 {"m4650", no_argument, NULL, OPTION_M4650},
9943 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
9944 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9945 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
9946 {"m4010", no_argument, NULL, OPTION_M4010},
9947 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
9948 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9949 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
9950 {"m4100", no_argument, NULL, OPTION_M4100},
9951 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
9952 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9953 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
9954 {"m3900", no_argument, NULL, OPTION_M3900},
9955 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
9956 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9957
9958 /* Options which enable bug fixes. */
9959 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
9960 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
9961 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9962 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
9963 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9964 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9965 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
9966 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
9967 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
9968 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
9969
9970 /* Miscellaneous options. */
9971 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
9972 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
9973 {"trap", no_argument, NULL, OPTION_TRAP},
9974 {"no-break", no_argument, NULL, OPTION_TRAP},
9975 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
9976 {"break", no_argument, NULL, OPTION_BREAK},
9977 {"no-trap", no_argument, NULL, OPTION_BREAK},
9978 #define OPTION_EB (OPTION_MISC_BASE + 2)
9979 {"EB", no_argument, NULL, OPTION_EB},
9980 #define OPTION_EL (OPTION_MISC_BASE + 3)
9981 {"EL", no_argument, NULL, OPTION_EL},
9982 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
9983 {"mfp32", no_argument, NULL, OPTION_FP32},
9984 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
9985 {"mgp32", no_argument, NULL, OPTION_GP32},
9986 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
9987 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9988 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
9989 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9990 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
9991 {"mfp64", no_argument, NULL, OPTION_FP64},
9992 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
9993 {"mgp64", no_argument, NULL, OPTION_GP64},
9994 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
9995 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
9996 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
9997 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
9998 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
9999 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10000 {"mshared", no_argument, NULL, OPTION_MSHARED},
10001 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10002 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10003 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10004 {"msym32", no_argument, NULL, OPTION_MSYM32},
10005 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10006
10007 /* ELF-specific options. */
10008 #ifdef OBJ_ELF
10009 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10010 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10011 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10012 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10013 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10014 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10015 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10016 {"xgot", no_argument, NULL, OPTION_XGOT},
10017 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10018 {"mabi", required_argument, NULL, OPTION_MABI},
10019 #define OPTION_32 (OPTION_ELF_BASE + 4)
10020 {"32", no_argument, NULL, OPTION_32},
10021 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10022 {"n32", no_argument, NULL, OPTION_N32},
10023 #define OPTION_64 (OPTION_ELF_BASE + 6)
10024 {"64", no_argument, NULL, OPTION_64},
10025 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10026 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10027 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10028 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10029 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10030 {"mpdr", no_argument, NULL, OPTION_PDR},
10031 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10032 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10033 #endif /* OBJ_ELF */
10034
10035 {NULL, no_argument, NULL, 0}
10036 };
10037 size_t md_longopts_size = sizeof (md_longopts);
10038
10039 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10040 NEW_VALUE. Warn if another value was already specified. Note:
10041 we have to defer parsing the -march and -mtune arguments in order
10042 to handle 'from-abi' correctly, since the ABI might be specified
10043 in a later argument. */
10044
10045 static void
10046 mips_set_option_string (const char **string_ptr, const char *new_value)
10047 {
10048 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10049 as_warn (_("A different %s was already specified, is now %s"),
10050 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10051 new_value);
10052
10053 *string_ptr = new_value;
10054 }
10055
10056 int
10057 md_parse_option (int c, char *arg)
10058 {
10059 switch (c)
10060 {
10061 case OPTION_CONSTRUCT_FLOATS:
10062 mips_disable_float_construction = 0;
10063 break;
10064
10065 case OPTION_NO_CONSTRUCT_FLOATS:
10066 mips_disable_float_construction = 1;
10067 break;
10068
10069 case OPTION_TRAP:
10070 mips_trap = 1;
10071 break;
10072
10073 case OPTION_BREAK:
10074 mips_trap = 0;
10075 break;
10076
10077 case OPTION_EB:
10078 target_big_endian = 1;
10079 break;
10080
10081 case OPTION_EL:
10082 target_big_endian = 0;
10083 break;
10084
10085 case 'O':
10086 if (arg && arg[1] == '0')
10087 mips_optimize = 1;
10088 else
10089 mips_optimize = 2;
10090 break;
10091
10092 case 'g':
10093 if (arg == NULL)
10094 mips_debug = 2;
10095 else
10096 mips_debug = atoi (arg);
10097 /* When the MIPS assembler sees -g or -g2, it does not do
10098 optimizations which limit full symbolic debugging. We take
10099 that to be equivalent to -O0. */
10100 if (mips_debug == 2)
10101 mips_optimize = 1;
10102 break;
10103
10104 case OPTION_MIPS1:
10105 file_mips_isa = ISA_MIPS1;
10106 break;
10107
10108 case OPTION_MIPS2:
10109 file_mips_isa = ISA_MIPS2;
10110 break;
10111
10112 case OPTION_MIPS3:
10113 file_mips_isa = ISA_MIPS3;
10114 break;
10115
10116 case OPTION_MIPS4:
10117 file_mips_isa = ISA_MIPS4;
10118 break;
10119
10120 case OPTION_MIPS5:
10121 file_mips_isa = ISA_MIPS5;
10122 break;
10123
10124 case OPTION_MIPS32:
10125 file_mips_isa = ISA_MIPS32;
10126 break;
10127
10128 case OPTION_MIPS32R2:
10129 file_mips_isa = ISA_MIPS32R2;
10130 break;
10131
10132 case OPTION_MIPS64R2:
10133 file_mips_isa = ISA_MIPS64R2;
10134 break;
10135
10136 case OPTION_MIPS64:
10137 file_mips_isa = ISA_MIPS64;
10138 break;
10139
10140 case OPTION_MTUNE:
10141 mips_set_option_string (&mips_tune_string, arg);
10142 break;
10143
10144 case OPTION_MARCH:
10145 mips_set_option_string (&mips_arch_string, arg);
10146 break;
10147
10148 case OPTION_M4650:
10149 mips_set_option_string (&mips_arch_string, "4650");
10150 mips_set_option_string (&mips_tune_string, "4650");
10151 break;
10152
10153 case OPTION_NO_M4650:
10154 break;
10155
10156 case OPTION_M4010:
10157 mips_set_option_string (&mips_arch_string, "4010");
10158 mips_set_option_string (&mips_tune_string, "4010");
10159 break;
10160
10161 case OPTION_NO_M4010:
10162 break;
10163
10164 case OPTION_M4100:
10165 mips_set_option_string (&mips_arch_string, "4100");
10166 mips_set_option_string (&mips_tune_string, "4100");
10167 break;
10168
10169 case OPTION_NO_M4100:
10170 break;
10171
10172 case OPTION_M3900:
10173 mips_set_option_string (&mips_arch_string, "3900");
10174 mips_set_option_string (&mips_tune_string, "3900");
10175 break;
10176
10177 case OPTION_NO_M3900:
10178 break;
10179
10180 case OPTION_MDMX:
10181 mips_opts.ase_mdmx = 1;
10182 break;
10183
10184 case OPTION_NO_MDMX:
10185 mips_opts.ase_mdmx = 0;
10186 break;
10187
10188 case OPTION_MIPS16:
10189 mips_opts.mips16 = 1;
10190 mips_no_prev_insn ();
10191 break;
10192
10193 case OPTION_NO_MIPS16:
10194 mips_opts.mips16 = 0;
10195 mips_no_prev_insn ();
10196 break;
10197
10198 case OPTION_MIPS3D:
10199 mips_opts.ase_mips3d = 1;
10200 break;
10201
10202 case OPTION_NO_MIPS3D:
10203 mips_opts.ase_mips3d = 0;
10204 break;
10205
10206 case OPTION_FIX_VR4120:
10207 mips_fix_vr4120 = 1;
10208 break;
10209
10210 case OPTION_NO_FIX_VR4120:
10211 mips_fix_vr4120 = 0;
10212 break;
10213
10214 case OPTION_RELAX_BRANCH:
10215 mips_relax_branch = 1;
10216 break;
10217
10218 case OPTION_NO_RELAX_BRANCH:
10219 mips_relax_branch = 0;
10220 break;
10221
10222 case OPTION_MSHARED:
10223 mips_in_shared = TRUE;
10224 break;
10225
10226 case OPTION_MNO_SHARED:
10227 mips_in_shared = FALSE;
10228 break;
10229
10230 case OPTION_MSYM32:
10231 mips_opts.sym32 = TRUE;
10232 break;
10233
10234 case OPTION_MNO_SYM32:
10235 mips_opts.sym32 = FALSE;
10236 break;
10237
10238 #ifdef OBJ_ELF
10239 /* When generating ELF code, we permit -KPIC and -call_shared to
10240 select SVR4_PIC, and -non_shared to select no PIC. This is
10241 intended to be compatible with Irix 5. */
10242 case OPTION_CALL_SHARED:
10243 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10244 {
10245 as_bad (_("-call_shared is supported only for ELF format"));
10246 return 0;
10247 }
10248 mips_pic = SVR4_PIC;
10249 mips_abicalls = TRUE;
10250 if (g_switch_seen && g_switch_value != 0)
10251 {
10252 as_bad (_("-G may not be used with SVR4 PIC code"));
10253 return 0;
10254 }
10255 g_switch_value = 0;
10256 break;
10257
10258 case OPTION_NON_SHARED:
10259 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10260 {
10261 as_bad (_("-non_shared is supported only for ELF format"));
10262 return 0;
10263 }
10264 mips_pic = NO_PIC;
10265 mips_abicalls = FALSE;
10266 break;
10267
10268 /* The -xgot option tells the assembler to use 32 offsets when
10269 accessing the got in SVR4_PIC mode. It is for Irix
10270 compatibility. */
10271 case OPTION_XGOT:
10272 mips_big_got = 1;
10273 break;
10274 #endif /* OBJ_ELF */
10275
10276 case 'G':
10277 g_switch_value = atoi (arg);
10278 g_switch_seen = 1;
10279 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10280 {
10281 as_bad (_("-G may not be used with SVR4 PIC code"));
10282 return 0;
10283 }
10284 break;
10285
10286 #ifdef OBJ_ELF
10287 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10288 and -mabi=64. */
10289 case OPTION_32:
10290 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10291 {
10292 as_bad (_("-32 is supported for ELF format only"));
10293 return 0;
10294 }
10295 mips_abi = O32_ABI;
10296 break;
10297
10298 case OPTION_N32:
10299 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10300 {
10301 as_bad (_("-n32 is supported for ELF format only"));
10302 return 0;
10303 }
10304 mips_abi = N32_ABI;
10305 break;
10306
10307 case OPTION_64:
10308 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10309 {
10310 as_bad (_("-64 is supported for ELF format only"));
10311 return 0;
10312 }
10313 mips_abi = N64_ABI;
10314 if (! support_64bit_objects())
10315 as_fatal (_("No compiled in support for 64 bit object file format"));
10316 break;
10317 #endif /* OBJ_ELF */
10318
10319 case OPTION_GP32:
10320 file_mips_gp32 = 1;
10321 break;
10322
10323 case OPTION_GP64:
10324 file_mips_gp32 = 0;
10325 break;
10326
10327 case OPTION_FP32:
10328 file_mips_fp32 = 1;
10329 break;
10330
10331 case OPTION_FP64:
10332 file_mips_fp32 = 0;
10333 break;
10334
10335 #ifdef OBJ_ELF
10336 case OPTION_MABI:
10337 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10338 {
10339 as_bad (_("-mabi is supported for ELF format only"));
10340 return 0;
10341 }
10342 if (strcmp (arg, "32") == 0)
10343 mips_abi = O32_ABI;
10344 else if (strcmp (arg, "o64") == 0)
10345 mips_abi = O64_ABI;
10346 else if (strcmp (arg, "n32") == 0)
10347 mips_abi = N32_ABI;
10348 else if (strcmp (arg, "64") == 0)
10349 {
10350 mips_abi = N64_ABI;
10351 if (! support_64bit_objects())
10352 as_fatal (_("No compiled in support for 64 bit object file "
10353 "format"));
10354 }
10355 else if (strcmp (arg, "eabi") == 0)
10356 mips_abi = EABI_ABI;
10357 else
10358 {
10359 as_fatal (_("invalid abi -mabi=%s"), arg);
10360 return 0;
10361 }
10362 break;
10363 #endif /* OBJ_ELF */
10364
10365 case OPTION_M7000_HILO_FIX:
10366 mips_7000_hilo_fix = TRUE;
10367 break;
10368
10369 case OPTION_MNO_7000_HILO_FIX:
10370 mips_7000_hilo_fix = FALSE;
10371 break;
10372
10373 #ifdef OBJ_ELF
10374 case OPTION_MDEBUG:
10375 mips_flag_mdebug = TRUE;
10376 break;
10377
10378 case OPTION_NO_MDEBUG:
10379 mips_flag_mdebug = FALSE;
10380 break;
10381
10382 case OPTION_PDR:
10383 mips_flag_pdr = TRUE;
10384 break;
10385
10386 case OPTION_NO_PDR:
10387 mips_flag_pdr = FALSE;
10388 break;
10389 #endif /* OBJ_ELF */
10390
10391 default:
10392 return 0;
10393 }
10394
10395 return 1;
10396 }
10397 \f
10398 /* Set up globals to generate code for the ISA or processor
10399 described by INFO. */
10400
10401 static void
10402 mips_set_architecture (const struct mips_cpu_info *info)
10403 {
10404 if (info != 0)
10405 {
10406 file_mips_arch = info->cpu;
10407 mips_opts.arch = info->cpu;
10408 mips_opts.isa = info->isa;
10409 }
10410 }
10411
10412
10413 /* Likewise for tuning. */
10414
10415 static void
10416 mips_set_tune (const struct mips_cpu_info *info)
10417 {
10418 if (info != 0)
10419 mips_tune = info->cpu;
10420 }
10421
10422
10423 void
10424 mips_after_parse_args (void)
10425 {
10426 const struct mips_cpu_info *arch_info = 0;
10427 const struct mips_cpu_info *tune_info = 0;
10428
10429 /* GP relative stuff not working for PE */
10430 if (strncmp (TARGET_OS, "pe", 2) == 0)
10431 {
10432 if (g_switch_seen && g_switch_value != 0)
10433 as_bad (_("-G not supported in this configuration."));
10434 g_switch_value = 0;
10435 }
10436
10437 if (mips_abi == NO_ABI)
10438 mips_abi = MIPS_DEFAULT_ABI;
10439
10440 /* The following code determines the architecture and register size.
10441 Similar code was added to GCC 3.3 (see override_options() in
10442 config/mips/mips.c). The GAS and GCC code should be kept in sync
10443 as much as possible. */
10444
10445 if (mips_arch_string != 0)
10446 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10447
10448 if (file_mips_isa != ISA_UNKNOWN)
10449 {
10450 /* Handle -mipsN. At this point, file_mips_isa contains the
10451 ISA level specified by -mipsN, while arch_info->isa contains
10452 the -march selection (if any). */
10453 if (arch_info != 0)
10454 {
10455 /* -march takes precedence over -mipsN, since it is more descriptive.
10456 There's no harm in specifying both as long as the ISA levels
10457 are the same. */
10458 if (file_mips_isa != arch_info->isa)
10459 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10460 mips_cpu_info_from_isa (file_mips_isa)->name,
10461 mips_cpu_info_from_isa (arch_info->isa)->name);
10462 }
10463 else
10464 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10465 }
10466
10467 if (arch_info == 0)
10468 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10469
10470 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10471 as_bad ("-march=%s is not compatible with the selected ABI",
10472 arch_info->name);
10473
10474 mips_set_architecture (arch_info);
10475
10476 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10477 if (mips_tune_string != 0)
10478 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10479
10480 if (tune_info == 0)
10481 mips_set_tune (arch_info);
10482 else
10483 mips_set_tune (tune_info);
10484
10485 if (file_mips_gp32 >= 0)
10486 {
10487 /* The user specified the size of the integer registers. Make sure
10488 it agrees with the ABI and ISA. */
10489 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10490 as_bad (_("-mgp64 used with a 32-bit processor"));
10491 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10492 as_bad (_("-mgp32 used with a 64-bit ABI"));
10493 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10494 as_bad (_("-mgp64 used with a 32-bit ABI"));
10495 }
10496 else
10497 {
10498 /* Infer the integer register size from the ABI and processor.
10499 Restrict ourselves to 32-bit registers if that's all the
10500 processor has, or if the ABI cannot handle 64-bit registers. */
10501 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10502 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10503 }
10504
10505 /* ??? GAS treats single-float processors as though they had 64-bit
10506 float registers (although it complains when double-precision
10507 instructions are used). As things stand, saying they have 32-bit
10508 registers would lead to spurious "register must be even" messages.
10509 So here we assume float registers are always the same size as
10510 integer ones, unless the user says otherwise. */
10511 if (file_mips_fp32 < 0)
10512 file_mips_fp32 = file_mips_gp32;
10513
10514 /* End of GCC-shared inference code. */
10515
10516 /* This flag is set when we have a 64-bit capable CPU but use only
10517 32-bit wide registers. Note that EABI does not use it. */
10518 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10519 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10520 || mips_abi == O32_ABI))
10521 mips_32bitmode = 1;
10522
10523 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10524 as_bad (_("trap exception not supported at ISA 1"));
10525
10526 /* If the selected architecture includes support for ASEs, enable
10527 generation of code for them. */
10528 if (mips_opts.mips16 == -1)
10529 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10530 if (mips_opts.ase_mips3d == -1)
10531 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10532 if (mips_opts.ase_mdmx == -1)
10533 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10534
10535 file_mips_isa = mips_opts.isa;
10536 file_ase_mips16 = mips_opts.mips16;
10537 file_ase_mips3d = mips_opts.ase_mips3d;
10538 file_ase_mdmx = mips_opts.ase_mdmx;
10539 mips_opts.gp32 = file_mips_gp32;
10540 mips_opts.fp32 = file_mips_fp32;
10541
10542 if (mips_flag_mdebug < 0)
10543 {
10544 #ifdef OBJ_MAYBE_ECOFF
10545 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10546 mips_flag_mdebug = 1;
10547 else
10548 #endif /* OBJ_MAYBE_ECOFF */
10549 mips_flag_mdebug = 0;
10550 }
10551 }
10552 \f
10553 void
10554 mips_init_after_args (void)
10555 {
10556 /* initialize opcodes */
10557 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10558 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10559 }
10560
10561 long
10562 md_pcrel_from (fixS *fixP)
10563 {
10564 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10565 switch (fixP->fx_r_type)
10566 {
10567 case BFD_RELOC_16_PCREL_S2:
10568 case BFD_RELOC_MIPS_JMP:
10569 /* Return the address of the delay slot. */
10570 return addr + 4;
10571 default:
10572 return addr;
10573 }
10574 }
10575
10576 /* This is called before the symbol table is processed. In order to
10577 work with gcc when using mips-tfile, we must keep all local labels.
10578 However, in other cases, we want to discard them. If we were
10579 called with -g, but we didn't see any debugging information, it may
10580 mean that gcc is smuggling debugging information through to
10581 mips-tfile, in which case we must generate all local labels. */
10582
10583 void
10584 mips_frob_file_before_adjust (void)
10585 {
10586 #ifndef NO_ECOFF_DEBUGGING
10587 if (ECOFF_DEBUGGING
10588 && mips_debug != 0
10589 && ! ecoff_debugging_seen)
10590 flag_keep_locals = 1;
10591 #endif
10592 }
10593
10594 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10595 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10596 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10597 relocation operators.
10598
10599 For our purposes, a %lo() expression matches a %got() or %hi()
10600 expression if:
10601
10602 (a) it refers to the same symbol; and
10603 (b) the offset applied in the %lo() expression is no lower than
10604 the offset applied in the %got() or %hi().
10605
10606 (b) allows us to cope with code like:
10607
10608 lui $4,%hi(foo)
10609 lh $4,%lo(foo+2)($4)
10610
10611 ...which is legal on RELA targets, and has a well-defined behaviour
10612 if the user knows that adding 2 to "foo" will not induce a carry to
10613 the high 16 bits.
10614
10615 When several %lo()s match a particular %got() or %hi(), we use the
10616 following rules to distinguish them:
10617
10618 (1) %lo()s with smaller offsets are a better match than %lo()s with
10619 higher offsets.
10620
10621 (2) %lo()s with no matching %got() or %hi() are better than those
10622 that already have a matching %got() or %hi().
10623
10624 (3) later %lo()s are better than earlier %lo()s.
10625
10626 These rules are applied in order.
10627
10628 (1) means, among other things, that %lo()s with identical offsets are
10629 chosen if they exist.
10630
10631 (2) means that we won't associate several high-part relocations with
10632 the same low-part relocation unless there's no alternative. Having
10633 several high parts for the same low part is a GNU extension; this rule
10634 allows careful users to avoid it.
10635
10636 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10637 with the last high-part relocation being at the front of the list.
10638 It therefore makes sense to choose the last matching low-part
10639 relocation, all other things being equal. It's also easier
10640 to code that way. */
10641
10642 void
10643 mips_frob_file (void)
10644 {
10645 struct mips_hi_fixup *l;
10646
10647 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10648 {
10649 segment_info_type *seginfo;
10650 bfd_boolean matched_lo_p;
10651 fixS **hi_pos, **lo_pos, **pos;
10652
10653 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10654
10655 /* If a GOT16 relocation turns out to be against a global symbol,
10656 there isn't supposed to be a matching LO. */
10657 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10658 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10659 continue;
10660
10661 /* Check quickly whether the next fixup happens to be a matching %lo. */
10662 if (fixup_has_matching_lo_p (l->fixp))
10663 continue;
10664
10665 seginfo = seg_info (l->seg);
10666
10667 /* Set HI_POS to the position of this relocation in the chain.
10668 Set LO_POS to the position of the chosen low-part relocation.
10669 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10670 relocation that matches an immediately-preceding high-part
10671 relocation. */
10672 hi_pos = NULL;
10673 lo_pos = NULL;
10674 matched_lo_p = FALSE;
10675 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10676 {
10677 if (*pos == l->fixp)
10678 hi_pos = pos;
10679
10680 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10681 && (*pos)->fx_addsy == l->fixp->fx_addsy
10682 && (*pos)->fx_offset >= l->fixp->fx_offset
10683 && (lo_pos == NULL
10684 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10685 || (!matched_lo_p
10686 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10687 lo_pos = pos;
10688
10689 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10690 && fixup_has_matching_lo_p (*pos));
10691 }
10692
10693 /* If we found a match, remove the high-part relocation from its
10694 current position and insert it before the low-part relocation.
10695 Make the offsets match so that fixup_has_matching_lo_p()
10696 will return true.
10697
10698 We don't warn about unmatched high-part relocations since some
10699 versions of gcc have been known to emit dead "lui ...%hi(...)"
10700 instructions. */
10701 if (lo_pos != NULL)
10702 {
10703 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10704 if (l->fixp->fx_next != *lo_pos)
10705 {
10706 *hi_pos = l->fixp->fx_next;
10707 l->fixp->fx_next = *lo_pos;
10708 *lo_pos = l->fixp;
10709 }
10710 }
10711 }
10712 }
10713
10714 /* We may have combined relocations without symbols in the N32/N64 ABI.
10715 We have to prevent gas from dropping them. */
10716
10717 int
10718 mips_force_relocation (fixS *fixp)
10719 {
10720 if (generic_force_reloc (fixp))
10721 return 1;
10722
10723 if (HAVE_NEWABI
10724 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10725 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10726 || fixp->fx_r_type == BFD_RELOC_HI16_S
10727 || fixp->fx_r_type == BFD_RELOC_LO16))
10728 return 1;
10729
10730 return 0;
10731 }
10732
10733 /* This hook is called before a fix is simplified. We don't really
10734 decide whether to skip a fix here. Rather, we turn global symbols
10735 used as branch targets into local symbols, such that they undergo
10736 simplification. We can only do this if the symbol is defined and
10737 it is in the same section as the branch. If this doesn't hold, we
10738 emit a better error message than just saying the relocation is not
10739 valid for the selected object format.
10740
10741 FIXP is the fix-up we're going to try to simplify, SEG is the
10742 segment in which the fix up occurs. The return value should be
10743 non-zero to indicate the fix-up is valid for further
10744 simplifications. */
10745
10746 int
10747 mips_validate_fix (struct fix *fixP, asection *seg)
10748 {
10749 /* There's a lot of discussion on whether it should be possible to
10750 use R_MIPS_PC16 to represent branch relocations. The outcome
10751 seems to be that it can, but gas/bfd are very broken in creating
10752 RELA relocations for this, so for now we only accept branches to
10753 symbols in the same section. Anything else is of dubious value,
10754 since there's no guarantee that at link time the symbol would be
10755 in range. Even for branches to local symbols this is arguably
10756 wrong, since it we assume the symbol is not going to be
10757 overridden, which should be possible per ELF library semantics,
10758 but then, there isn't a dynamic relocation that could be used to
10759 this effect, and the target would likely be out of range as well.
10760
10761 Unfortunately, it seems that there is too much code out there
10762 that relies on branches to symbols that are global to be resolved
10763 as if they were local, like the IRIX tools do, so we do it as
10764 well, but with a warning so that people are reminded to fix their
10765 code. If we ever get back to using R_MIPS_PC16 for branch
10766 targets, this entire block should go away (and probably the
10767 whole function). */
10768
10769 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10770 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10771 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10772 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10773 && fixP->fx_addsy)
10774 {
10775 if (! S_IS_DEFINED (fixP->fx_addsy))
10776 {
10777 as_bad_where (fixP->fx_file, fixP->fx_line,
10778 _("Cannot branch to undefined symbol."));
10779 /* Avoid any further errors about this fixup. */
10780 fixP->fx_done = 1;
10781 }
10782 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10783 {
10784 as_bad_where (fixP->fx_file, fixP->fx_line,
10785 _("Cannot branch to symbol in another section."));
10786 fixP->fx_done = 1;
10787 }
10788 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10789 {
10790 symbolS *sym = fixP->fx_addsy;
10791
10792 if (mips_pic == SVR4_PIC)
10793 as_warn_where (fixP->fx_file, fixP->fx_line,
10794 _("Pretending global symbol used as branch target is local."));
10795
10796 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10797 S_GET_SEGMENT (sym),
10798 S_GET_VALUE (sym),
10799 symbol_get_frag (sym));
10800 copy_symbol_attributes (fixP->fx_addsy, sym);
10801 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10802 assert (symbol_resolved_p (sym));
10803 symbol_mark_resolved (fixP->fx_addsy);
10804 }
10805 }
10806
10807 return 1;
10808 }
10809
10810 /* Apply a fixup to the object file. */
10811
10812 void
10813 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
10814 {
10815 bfd_byte *buf;
10816 long insn;
10817 reloc_howto_type *howto;
10818
10819 /* We ignore generic BFD relocations we don't know about. */
10820 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10821 if (! howto)
10822 return;
10823
10824 assert (fixP->fx_size == 4
10825 || fixP->fx_r_type == BFD_RELOC_16
10826 || fixP->fx_r_type == BFD_RELOC_64
10827 || fixP->fx_r_type == BFD_RELOC_CTOR
10828 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10829 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10830 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10831
10832 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
10833
10834 assert (! fixP->fx_pcrel);
10835
10836 /* Don't treat parts of a composite relocation as done. There are two
10837 reasons for this:
10838
10839 (1) The second and third parts will be against 0 (RSS_UNDEF) but
10840 should nevertheless be emitted if the first part is.
10841
10842 (2) In normal usage, composite relocations are never assembly-time
10843 constants. The easiest way of dealing with the pathological
10844 exceptions is to generate a relocation against STN_UNDEF and
10845 leave everything up to the linker. */
10846 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
10847 fixP->fx_done = 1;
10848
10849 switch (fixP->fx_r_type)
10850 {
10851 case BFD_RELOC_MIPS_TLS_GD:
10852 case BFD_RELOC_MIPS_TLS_LDM:
10853 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
10854 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
10855 case BFD_RELOC_MIPS_TLS_GOTTPREL:
10856 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
10857 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
10858 S_SET_THREAD_LOCAL (fixP->fx_addsy);
10859 /* fall through */
10860
10861 case BFD_RELOC_MIPS_JMP:
10862 case BFD_RELOC_MIPS_SHIFT5:
10863 case BFD_RELOC_MIPS_SHIFT6:
10864 case BFD_RELOC_MIPS_GOT_DISP:
10865 case BFD_RELOC_MIPS_GOT_PAGE:
10866 case BFD_RELOC_MIPS_GOT_OFST:
10867 case BFD_RELOC_MIPS_SUB:
10868 case BFD_RELOC_MIPS_INSERT_A:
10869 case BFD_RELOC_MIPS_INSERT_B:
10870 case BFD_RELOC_MIPS_DELETE:
10871 case BFD_RELOC_MIPS_HIGHEST:
10872 case BFD_RELOC_MIPS_HIGHER:
10873 case BFD_RELOC_MIPS_SCN_DISP:
10874 case BFD_RELOC_MIPS_REL16:
10875 case BFD_RELOC_MIPS_RELGOT:
10876 case BFD_RELOC_MIPS_JALR:
10877 case BFD_RELOC_HI16:
10878 case BFD_RELOC_HI16_S:
10879 case BFD_RELOC_GPREL16:
10880 case BFD_RELOC_MIPS_LITERAL:
10881 case BFD_RELOC_MIPS_CALL16:
10882 case BFD_RELOC_MIPS_GOT16:
10883 case BFD_RELOC_GPREL32:
10884 case BFD_RELOC_MIPS_GOT_HI16:
10885 case BFD_RELOC_MIPS_GOT_LO16:
10886 case BFD_RELOC_MIPS_CALL_HI16:
10887 case BFD_RELOC_MIPS_CALL_LO16:
10888 case BFD_RELOC_MIPS16_GPREL:
10889 case BFD_RELOC_MIPS16_HI16:
10890 case BFD_RELOC_MIPS16_HI16_S:
10891 assert (! fixP->fx_pcrel);
10892 /* Nothing needed to do. The value comes from the reloc entry */
10893 break;
10894
10895 case BFD_RELOC_MIPS16_JMP:
10896 /* We currently always generate a reloc against a symbol, which
10897 means that we don't want an addend even if the symbol is
10898 defined. */
10899 *valP = 0;
10900 break;
10901
10902 case BFD_RELOC_64:
10903 /* This is handled like BFD_RELOC_32, but we output a sign
10904 extended value if we are only 32 bits. */
10905 if (fixP->fx_done)
10906 {
10907 if (8 <= sizeof (valueT))
10908 md_number_to_chars ((char *) buf, *valP, 8);
10909 else
10910 {
10911 valueT hiv;
10912
10913 if ((*valP & 0x80000000) != 0)
10914 hiv = 0xffffffff;
10915 else
10916 hiv = 0;
10917 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
10918 *valP, 4);
10919 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
10920 hiv, 4);
10921 }
10922 }
10923 break;
10924
10925 case BFD_RELOC_RVA:
10926 case BFD_RELOC_32:
10927 /* If we are deleting this reloc entry, we must fill in the
10928 value now. This can happen if we have a .word which is not
10929 resolved when it appears but is later defined. */
10930 if (fixP->fx_done)
10931 md_number_to_chars ((char *) buf, *valP, 4);
10932 break;
10933
10934 case BFD_RELOC_16:
10935 /* If we are deleting this reloc entry, we must fill in the
10936 value now. */
10937 if (fixP->fx_done)
10938 md_number_to_chars ((char *) buf, *valP, 2);
10939 break;
10940
10941 case BFD_RELOC_LO16:
10942 case BFD_RELOC_MIPS16_LO16:
10943 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
10944 may be safe to remove, but if so it's not obvious. */
10945 /* When handling an embedded PIC switch statement, we can wind
10946 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10947 if (fixP->fx_done)
10948 {
10949 if (*valP + 0x8000 > 0xffff)
10950 as_bad_where (fixP->fx_file, fixP->fx_line,
10951 _("relocation overflow"));
10952 if (target_big_endian)
10953 buf += 2;
10954 md_number_to_chars ((char *) buf, *valP, 2);
10955 }
10956 break;
10957
10958 case BFD_RELOC_16_PCREL_S2:
10959 if ((*valP & 0x3) != 0)
10960 as_bad_where (fixP->fx_file, fixP->fx_line,
10961 _("Branch to odd address (%lx)"), (long) *valP);
10962
10963 /*
10964 * We need to save the bits in the instruction since fixup_segment()
10965 * might be deleting the relocation entry (i.e., a branch within
10966 * the current segment).
10967 */
10968 if (! fixP->fx_done)
10969 break;
10970
10971 /* update old instruction data */
10972 if (target_big_endian)
10973 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10974 else
10975 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10976
10977 if (*valP + 0x20000 <= 0x3ffff)
10978 {
10979 insn |= (*valP >> 2) & 0xffff;
10980 md_number_to_chars ((char *) buf, insn, 4);
10981 }
10982 else if (mips_pic == NO_PIC
10983 && fixP->fx_done
10984 && fixP->fx_frag->fr_address >= text_section->vma
10985 && (fixP->fx_frag->fr_address
10986 < text_section->vma + bfd_get_section_size (text_section))
10987 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10988 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10989 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10990 {
10991 /* The branch offset is too large. If this is an
10992 unconditional branch, and we are not generating PIC code,
10993 we can convert it to an absolute jump instruction. */
10994 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10995 insn = 0x0c000000; /* jal */
10996 else
10997 insn = 0x08000000; /* j */
10998 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10999 fixP->fx_done = 0;
11000 fixP->fx_addsy = section_symbol (text_section);
11001 *valP += md_pcrel_from (fixP);
11002 md_number_to_chars ((char *) buf, insn, 4);
11003 }
11004 else
11005 {
11006 /* If we got here, we have branch-relaxation disabled,
11007 and there's nothing we can do to fix this instruction
11008 without turning it into a longer sequence. */
11009 as_bad_where (fixP->fx_file, fixP->fx_line,
11010 _("Branch out of range"));
11011 }
11012 break;
11013
11014 case BFD_RELOC_VTABLE_INHERIT:
11015 fixP->fx_done = 0;
11016 if (fixP->fx_addsy
11017 && !S_IS_DEFINED (fixP->fx_addsy)
11018 && !S_IS_WEAK (fixP->fx_addsy))
11019 S_SET_WEAK (fixP->fx_addsy);
11020 break;
11021
11022 case BFD_RELOC_VTABLE_ENTRY:
11023 fixP->fx_done = 0;
11024 break;
11025
11026 default:
11027 internalError ();
11028 }
11029
11030 /* Remember value for tc_gen_reloc. */
11031 fixP->fx_addnumber = *valP;
11032 }
11033
11034 static symbolS *
11035 get_symbol (void)
11036 {
11037 int c;
11038 char *name;
11039 symbolS *p;
11040
11041 name = input_line_pointer;
11042 c = get_symbol_end ();
11043 p = (symbolS *) symbol_find_or_make (name);
11044 *input_line_pointer = c;
11045 return p;
11046 }
11047
11048 /* Align the current frag to a given power of two. The MIPS assembler
11049 also automatically adjusts any preceding label. */
11050
11051 static void
11052 mips_align (int to, int fill, symbolS *label)
11053 {
11054 mips_emit_delays ();
11055 frag_align (to, fill, 0);
11056 record_alignment (now_seg, to);
11057 if (label != NULL)
11058 {
11059 assert (S_GET_SEGMENT (label) == now_seg);
11060 symbol_set_frag (label, frag_now);
11061 S_SET_VALUE (label, (valueT) frag_now_fix ());
11062 }
11063 }
11064
11065 /* Align to a given power of two. .align 0 turns off the automatic
11066 alignment used by the data creating pseudo-ops. */
11067
11068 static void
11069 s_align (int x ATTRIBUTE_UNUSED)
11070 {
11071 register int temp;
11072 register long temp_fill;
11073 long max_alignment = 15;
11074
11075 /*
11076
11077 o Note that the assembler pulls down any immediately preceding label
11078 to the aligned address.
11079 o It's not documented but auto alignment is reinstated by
11080 a .align pseudo instruction.
11081 o Note also that after auto alignment is turned off the mips assembler
11082 issues an error on attempt to assemble an improperly aligned data item.
11083 We don't.
11084
11085 */
11086
11087 temp = get_absolute_expression ();
11088 if (temp > max_alignment)
11089 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11090 else if (temp < 0)
11091 {
11092 as_warn (_("Alignment negative: 0 assumed."));
11093 temp = 0;
11094 }
11095 if (*input_line_pointer == ',')
11096 {
11097 ++input_line_pointer;
11098 temp_fill = get_absolute_expression ();
11099 }
11100 else
11101 temp_fill = 0;
11102 if (temp)
11103 {
11104 auto_align = 1;
11105 mips_align (temp, (int) temp_fill,
11106 insn_labels != NULL ? insn_labels->label : NULL);
11107 }
11108 else
11109 {
11110 auto_align = 0;
11111 }
11112
11113 demand_empty_rest_of_line ();
11114 }
11115
11116 static void
11117 s_change_sec (int sec)
11118 {
11119 segT seg;
11120
11121 #ifdef OBJ_ELF
11122 /* The ELF backend needs to know that we are changing sections, so
11123 that .previous works correctly. We could do something like check
11124 for an obj_section_change_hook macro, but that might be confusing
11125 as it would not be appropriate to use it in the section changing
11126 functions in read.c, since obj-elf.c intercepts those. FIXME:
11127 This should be cleaner, somehow. */
11128 obj_elf_section_change_hook ();
11129 #endif
11130
11131 mips_emit_delays ();
11132 switch (sec)
11133 {
11134 case 't':
11135 s_text (0);
11136 break;
11137 case 'd':
11138 s_data (0);
11139 break;
11140 case 'b':
11141 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11142 demand_empty_rest_of_line ();
11143 break;
11144
11145 case 'r':
11146 seg = subseg_new (RDATA_SECTION_NAME,
11147 (subsegT) get_absolute_expression ());
11148 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11149 {
11150 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11151 | SEC_READONLY | SEC_RELOC
11152 | SEC_DATA));
11153 if (strcmp (TARGET_OS, "elf") != 0)
11154 record_alignment (seg, 4);
11155 }
11156 demand_empty_rest_of_line ();
11157 break;
11158
11159 case 's':
11160 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11161 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11162 {
11163 bfd_set_section_flags (stdoutput, seg,
11164 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11165 if (strcmp (TARGET_OS, "elf") != 0)
11166 record_alignment (seg, 4);
11167 }
11168 demand_empty_rest_of_line ();
11169 break;
11170 }
11171
11172 auto_align = 1;
11173 }
11174
11175 void
11176 s_change_section (int ignore ATTRIBUTE_UNUSED)
11177 {
11178 #ifdef OBJ_ELF
11179 char *section_name;
11180 char c;
11181 char next_c = 0;
11182 int section_type;
11183 int section_flag;
11184 int section_entry_size;
11185 int section_alignment;
11186
11187 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11188 return;
11189
11190 section_name = input_line_pointer;
11191 c = get_symbol_end ();
11192 if (c)
11193 next_c = *(input_line_pointer + 1);
11194
11195 /* Do we have .section Name<,"flags">? */
11196 if (c != ',' || (c == ',' && next_c == '"'))
11197 {
11198 /* just after name is now '\0'. */
11199 *input_line_pointer = c;
11200 input_line_pointer = section_name;
11201 obj_elf_section (ignore);
11202 return;
11203 }
11204 input_line_pointer++;
11205
11206 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11207 if (c == ',')
11208 section_type = get_absolute_expression ();
11209 else
11210 section_type = 0;
11211 if (*input_line_pointer++ == ',')
11212 section_flag = get_absolute_expression ();
11213 else
11214 section_flag = 0;
11215 if (*input_line_pointer++ == ',')
11216 section_entry_size = get_absolute_expression ();
11217 else
11218 section_entry_size = 0;
11219 if (*input_line_pointer++ == ',')
11220 section_alignment = get_absolute_expression ();
11221 else
11222 section_alignment = 0;
11223
11224 section_name = xstrdup (section_name);
11225
11226 /* When using the generic form of .section (as implemented by obj-elf.c),
11227 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11228 traditionally had to fall back on the more common @progbits instead.
11229
11230 There's nothing really harmful in this, since bfd will correct
11231 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11232 means that, for backwards compatibiltiy, the special_section entries
11233 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11234
11235 Even so, we shouldn't force users of the MIPS .section syntax to
11236 incorrectly label the sections as SHT_PROGBITS. The best compromise
11237 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11238 generic type-checking code. */
11239 if (section_type == SHT_MIPS_DWARF)
11240 section_type = SHT_PROGBITS;
11241
11242 obj_elf_change_section (section_name, section_type, section_flag,
11243 section_entry_size, 0, 0, 0);
11244
11245 if (now_seg->name != section_name)
11246 free (section_name);
11247 #endif /* OBJ_ELF */
11248 }
11249
11250 void
11251 mips_enable_auto_align (void)
11252 {
11253 auto_align = 1;
11254 }
11255
11256 static void
11257 s_cons (int log_size)
11258 {
11259 symbolS *label;
11260
11261 label = insn_labels != NULL ? insn_labels->label : NULL;
11262 mips_emit_delays ();
11263 if (log_size > 0 && auto_align)
11264 mips_align (log_size, 0, label);
11265 mips_clear_insn_labels ();
11266 cons (1 << log_size);
11267 }
11268
11269 static void
11270 s_float_cons (int type)
11271 {
11272 symbolS *label;
11273
11274 label = insn_labels != NULL ? insn_labels->label : NULL;
11275
11276 mips_emit_delays ();
11277
11278 if (auto_align)
11279 {
11280 if (type == 'd')
11281 mips_align (3, 0, label);
11282 else
11283 mips_align (2, 0, label);
11284 }
11285
11286 mips_clear_insn_labels ();
11287
11288 float_cons (type);
11289 }
11290
11291 /* Handle .globl. We need to override it because on Irix 5 you are
11292 permitted to say
11293 .globl foo .text
11294 where foo is an undefined symbol, to mean that foo should be
11295 considered to be the address of a function. */
11296
11297 static void
11298 s_mips_globl (int x ATTRIBUTE_UNUSED)
11299 {
11300 char *name;
11301 int c;
11302 symbolS *symbolP;
11303 flagword flag;
11304
11305 name = input_line_pointer;
11306 c = get_symbol_end ();
11307 symbolP = symbol_find_or_make (name);
11308 *input_line_pointer = c;
11309 SKIP_WHITESPACE ();
11310
11311 /* On Irix 5, every global symbol that is not explicitly labelled as
11312 being a function is apparently labelled as being an object. */
11313 flag = BSF_OBJECT;
11314
11315 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11316 {
11317 char *secname;
11318 asection *sec;
11319
11320 secname = input_line_pointer;
11321 c = get_symbol_end ();
11322 sec = bfd_get_section_by_name (stdoutput, secname);
11323 if (sec == NULL)
11324 as_bad (_("%s: no such section"), secname);
11325 *input_line_pointer = c;
11326
11327 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11328 flag = BSF_FUNCTION;
11329 }
11330
11331 symbol_get_bfdsym (symbolP)->flags |= flag;
11332
11333 S_SET_EXTERNAL (symbolP);
11334 demand_empty_rest_of_line ();
11335 }
11336
11337 static void
11338 s_option (int x ATTRIBUTE_UNUSED)
11339 {
11340 char *opt;
11341 char c;
11342
11343 opt = input_line_pointer;
11344 c = get_symbol_end ();
11345
11346 if (*opt == 'O')
11347 {
11348 /* FIXME: What does this mean? */
11349 }
11350 else if (strncmp (opt, "pic", 3) == 0)
11351 {
11352 int i;
11353
11354 i = atoi (opt + 3);
11355 if (i == 0)
11356 mips_pic = NO_PIC;
11357 else if (i == 2)
11358 {
11359 mips_pic = SVR4_PIC;
11360 mips_abicalls = TRUE;
11361 }
11362 else
11363 as_bad (_(".option pic%d not supported"), i);
11364
11365 if (mips_pic == SVR4_PIC)
11366 {
11367 if (g_switch_seen && g_switch_value != 0)
11368 as_warn (_("-G may not be used with SVR4 PIC code"));
11369 g_switch_value = 0;
11370 bfd_set_gp_size (stdoutput, 0);
11371 }
11372 }
11373 else
11374 as_warn (_("Unrecognized option \"%s\""), opt);
11375
11376 *input_line_pointer = c;
11377 demand_empty_rest_of_line ();
11378 }
11379
11380 /* This structure is used to hold a stack of .set values. */
11381
11382 struct mips_option_stack
11383 {
11384 struct mips_option_stack *next;
11385 struct mips_set_options options;
11386 };
11387
11388 static struct mips_option_stack *mips_opts_stack;
11389
11390 /* Handle the .set pseudo-op. */
11391
11392 static void
11393 s_mipsset (int x ATTRIBUTE_UNUSED)
11394 {
11395 char *name = input_line_pointer, ch;
11396
11397 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11398 ++input_line_pointer;
11399 ch = *input_line_pointer;
11400 *input_line_pointer = '\0';
11401
11402 if (strcmp (name, "reorder") == 0)
11403 {
11404 if (mips_opts.noreorder)
11405 end_noreorder ();
11406 }
11407 else if (strcmp (name, "noreorder") == 0)
11408 {
11409 if (!mips_opts.noreorder)
11410 start_noreorder ();
11411 }
11412 else if (strcmp (name, "at") == 0)
11413 {
11414 mips_opts.noat = 0;
11415 }
11416 else if (strcmp (name, "noat") == 0)
11417 {
11418 mips_opts.noat = 1;
11419 }
11420 else if (strcmp (name, "macro") == 0)
11421 {
11422 mips_opts.warn_about_macros = 0;
11423 }
11424 else if (strcmp (name, "nomacro") == 0)
11425 {
11426 if (mips_opts.noreorder == 0)
11427 as_bad (_("`noreorder' must be set before `nomacro'"));
11428 mips_opts.warn_about_macros = 1;
11429 }
11430 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11431 {
11432 mips_opts.nomove = 0;
11433 }
11434 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11435 {
11436 mips_opts.nomove = 1;
11437 }
11438 else if (strcmp (name, "bopt") == 0)
11439 {
11440 mips_opts.nobopt = 0;
11441 }
11442 else if (strcmp (name, "nobopt") == 0)
11443 {
11444 mips_opts.nobopt = 1;
11445 }
11446 else if (strcmp (name, "mips16") == 0
11447 || strcmp (name, "MIPS-16") == 0)
11448 mips_opts.mips16 = 1;
11449 else if (strcmp (name, "nomips16") == 0
11450 || strcmp (name, "noMIPS-16") == 0)
11451 mips_opts.mips16 = 0;
11452 else if (strcmp (name, "mips3d") == 0)
11453 mips_opts.ase_mips3d = 1;
11454 else if (strcmp (name, "nomips3d") == 0)
11455 mips_opts.ase_mips3d = 0;
11456 else if (strcmp (name, "mdmx") == 0)
11457 mips_opts.ase_mdmx = 1;
11458 else if (strcmp (name, "nomdmx") == 0)
11459 mips_opts.ase_mdmx = 0;
11460 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11461 {
11462 int reset = 0;
11463
11464 /* Permit the user to change the ISA and architecture on the fly.
11465 Needless to say, misuse can cause serious problems. */
11466 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11467 {
11468 reset = 1;
11469 mips_opts.isa = file_mips_isa;
11470 mips_opts.arch = file_mips_arch;
11471 }
11472 else if (strncmp (name, "arch=", 5) == 0)
11473 {
11474 const struct mips_cpu_info *p;
11475
11476 p = mips_parse_cpu("internal use", name + 5);
11477 if (!p)
11478 as_bad (_("unknown architecture %s"), name + 5);
11479 else
11480 {
11481 mips_opts.arch = p->cpu;
11482 mips_opts.isa = p->isa;
11483 }
11484 }
11485 else if (strncmp (name, "mips", 4) == 0)
11486 {
11487 const struct mips_cpu_info *p;
11488
11489 p = mips_parse_cpu("internal use", name);
11490 if (!p)
11491 as_bad (_("unknown ISA level %s"), name + 4);
11492 else
11493 {
11494 mips_opts.arch = p->cpu;
11495 mips_opts.isa = p->isa;
11496 }
11497 }
11498 else
11499 as_bad (_("unknown ISA or architecture %s"), name);
11500
11501 switch (mips_opts.isa)
11502 {
11503 case 0:
11504 break;
11505 case ISA_MIPS1:
11506 case ISA_MIPS2:
11507 case ISA_MIPS32:
11508 case ISA_MIPS32R2:
11509 mips_opts.gp32 = 1;
11510 mips_opts.fp32 = 1;
11511 break;
11512 case ISA_MIPS3:
11513 case ISA_MIPS4:
11514 case ISA_MIPS5:
11515 case ISA_MIPS64:
11516 case ISA_MIPS64R2:
11517 mips_opts.gp32 = 0;
11518 mips_opts.fp32 = 0;
11519 break;
11520 default:
11521 as_bad (_("unknown ISA level %s"), name + 4);
11522 break;
11523 }
11524 if (reset)
11525 {
11526 mips_opts.gp32 = file_mips_gp32;
11527 mips_opts.fp32 = file_mips_fp32;
11528 }
11529 }
11530 else if (strcmp (name, "autoextend") == 0)
11531 mips_opts.noautoextend = 0;
11532 else if (strcmp (name, "noautoextend") == 0)
11533 mips_opts.noautoextend = 1;
11534 else if (strcmp (name, "push") == 0)
11535 {
11536 struct mips_option_stack *s;
11537
11538 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11539 s->next = mips_opts_stack;
11540 s->options = mips_opts;
11541 mips_opts_stack = s;
11542 }
11543 else if (strcmp (name, "pop") == 0)
11544 {
11545 struct mips_option_stack *s;
11546
11547 s = mips_opts_stack;
11548 if (s == NULL)
11549 as_bad (_(".set pop with no .set push"));
11550 else
11551 {
11552 /* If we're changing the reorder mode we need to handle
11553 delay slots correctly. */
11554 if (s->options.noreorder && ! mips_opts.noreorder)
11555 start_noreorder ();
11556 else if (! s->options.noreorder && mips_opts.noreorder)
11557 end_noreorder ();
11558
11559 mips_opts = s->options;
11560 mips_opts_stack = s->next;
11561 free (s);
11562 }
11563 }
11564 else if (strcmp (name, "sym32") == 0)
11565 mips_opts.sym32 = TRUE;
11566 else if (strcmp (name, "nosym32") == 0)
11567 mips_opts.sym32 = FALSE;
11568 else
11569 {
11570 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11571 }
11572 *input_line_pointer = ch;
11573 demand_empty_rest_of_line ();
11574 }
11575
11576 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11577 .option pic2. It means to generate SVR4 PIC calls. */
11578
11579 static void
11580 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11581 {
11582 mips_pic = SVR4_PIC;
11583 mips_abicalls = TRUE;
11584
11585 if (g_switch_seen && g_switch_value != 0)
11586 as_warn (_("-G may not be used with SVR4 PIC code"));
11587 g_switch_value = 0;
11588
11589 bfd_set_gp_size (stdoutput, 0);
11590 demand_empty_rest_of_line ();
11591 }
11592
11593 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11594 PIC code. It sets the $gp register for the function based on the
11595 function address, which is in the register named in the argument.
11596 This uses a relocation against _gp_disp, which is handled specially
11597 by the linker. The result is:
11598 lui $gp,%hi(_gp_disp)
11599 addiu $gp,$gp,%lo(_gp_disp)
11600 addu $gp,$gp,.cpload argument
11601 The .cpload argument is normally $25 == $t9.
11602
11603 The -mno-shared option changes this to:
11604 lui $gp,%hi(__gnu_local_gp)
11605 addiu $gp,$gp,%lo(__gnu_local_gp)
11606 and the argument is ignored. This saves an instruction, but the
11607 resulting code is not position independent; it uses an absolute
11608 address for __gnu_local_gp. Thus code assembled with -mno-shared
11609 can go into an ordinary executable, but not into a shared library. */
11610
11611 static void
11612 s_cpload (int ignore ATTRIBUTE_UNUSED)
11613 {
11614 expressionS ex;
11615 int reg;
11616 int in_shared;
11617
11618 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11619 .cpload is ignored. */
11620 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11621 {
11622 s_ignore (0);
11623 return;
11624 }
11625
11626 /* .cpload should be in a .set noreorder section. */
11627 if (mips_opts.noreorder == 0)
11628 as_warn (_(".cpload not in noreorder section"));
11629
11630 reg = tc_get_register (0);
11631
11632 /* If we need to produce a 64-bit address, we are better off using
11633 the default instruction sequence. */
11634 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11635
11636 ex.X_op = O_symbol;
11637 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11638 "__gnu_local_gp");
11639 ex.X_op_symbol = NULL;
11640 ex.X_add_number = 0;
11641
11642 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11643 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11644
11645 macro_start ();
11646 macro_build_lui (&ex, mips_gp_register);
11647 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11648 mips_gp_register, BFD_RELOC_LO16);
11649 if (in_shared)
11650 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11651 mips_gp_register, reg);
11652 macro_end ();
11653
11654 demand_empty_rest_of_line ();
11655 }
11656
11657 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11658 .cpsetup $reg1, offset|$reg2, label
11659
11660 If offset is given, this results in:
11661 sd $gp, offset($sp)
11662 lui $gp, %hi(%neg(%gp_rel(label)))
11663 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11664 daddu $gp, $gp, $reg1
11665
11666 If $reg2 is given, this results in:
11667 daddu $reg2, $gp, $0
11668 lui $gp, %hi(%neg(%gp_rel(label)))
11669 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11670 daddu $gp, $gp, $reg1
11671 $reg1 is normally $25 == $t9.
11672
11673 The -mno-shared option replaces the last three instructions with
11674 lui $gp,%hi(_gp)
11675 addiu $gp,$gp,%lo(_gp)
11676 */
11677
11678 static void
11679 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11680 {
11681 expressionS ex_off;
11682 expressionS ex_sym;
11683 int reg1;
11684
11685 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11686 We also need NewABI support. */
11687 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11688 {
11689 s_ignore (0);
11690 return;
11691 }
11692
11693 reg1 = tc_get_register (0);
11694 SKIP_WHITESPACE ();
11695 if (*input_line_pointer != ',')
11696 {
11697 as_bad (_("missing argument separator ',' for .cpsetup"));
11698 return;
11699 }
11700 else
11701 ++input_line_pointer;
11702 SKIP_WHITESPACE ();
11703 if (*input_line_pointer == '$')
11704 {
11705 mips_cpreturn_register = tc_get_register (0);
11706 mips_cpreturn_offset = -1;
11707 }
11708 else
11709 {
11710 mips_cpreturn_offset = get_absolute_expression ();
11711 mips_cpreturn_register = -1;
11712 }
11713 SKIP_WHITESPACE ();
11714 if (*input_line_pointer != ',')
11715 {
11716 as_bad (_("missing argument separator ',' for .cpsetup"));
11717 return;
11718 }
11719 else
11720 ++input_line_pointer;
11721 SKIP_WHITESPACE ();
11722 expression (&ex_sym);
11723
11724 macro_start ();
11725 if (mips_cpreturn_register == -1)
11726 {
11727 ex_off.X_op = O_constant;
11728 ex_off.X_add_symbol = NULL;
11729 ex_off.X_op_symbol = NULL;
11730 ex_off.X_add_number = mips_cpreturn_offset;
11731
11732 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11733 BFD_RELOC_LO16, SP);
11734 }
11735 else
11736 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11737 mips_gp_register, 0);
11738
11739 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11740 {
11741 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11742 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11743 BFD_RELOC_HI16_S);
11744
11745 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11746 mips_gp_register, -1, BFD_RELOC_GPREL16,
11747 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11748
11749 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11750 mips_gp_register, reg1);
11751 }
11752 else
11753 {
11754 expressionS ex;
11755
11756 ex.X_op = O_symbol;
11757 ex.X_add_symbol = symbol_find_or_make ("_gp");
11758 ex.X_op_symbol = NULL;
11759 ex.X_add_number = 0;
11760
11761 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11762 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11763
11764 macro_build_lui (&ex, mips_gp_register);
11765 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11766 mips_gp_register, BFD_RELOC_LO16);
11767 }
11768
11769 macro_end ();
11770
11771 demand_empty_rest_of_line ();
11772 }
11773
11774 static void
11775 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11776 {
11777 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11778 .cplocal is ignored. */
11779 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11780 {
11781 s_ignore (0);
11782 return;
11783 }
11784
11785 mips_gp_register = tc_get_register (0);
11786 demand_empty_rest_of_line ();
11787 }
11788
11789 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11790 offset from $sp. The offset is remembered, and after making a PIC
11791 call $gp is restored from that location. */
11792
11793 static void
11794 s_cprestore (int ignore ATTRIBUTE_UNUSED)
11795 {
11796 expressionS ex;
11797
11798 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11799 .cprestore is ignored. */
11800 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11801 {
11802 s_ignore (0);
11803 return;
11804 }
11805
11806 mips_cprestore_offset = get_absolute_expression ();
11807 mips_cprestore_valid = 1;
11808
11809 ex.X_op = O_constant;
11810 ex.X_add_symbol = NULL;
11811 ex.X_op_symbol = NULL;
11812 ex.X_add_number = mips_cprestore_offset;
11813
11814 macro_start ();
11815 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
11816 SP, HAVE_64BIT_ADDRESSES);
11817 macro_end ();
11818
11819 demand_empty_rest_of_line ();
11820 }
11821
11822 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11823 was given in the preceding .cpsetup, it results in:
11824 ld $gp, offset($sp)
11825
11826 If a register $reg2 was given there, it results in:
11827 daddu $gp, $reg2, $0
11828 */
11829 static void
11830 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
11831 {
11832 expressionS ex;
11833
11834 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11835 We also need NewABI support. */
11836 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11837 {
11838 s_ignore (0);
11839 return;
11840 }
11841
11842 macro_start ();
11843 if (mips_cpreturn_register == -1)
11844 {
11845 ex.X_op = O_constant;
11846 ex.X_add_symbol = NULL;
11847 ex.X_op_symbol = NULL;
11848 ex.X_add_number = mips_cpreturn_offset;
11849
11850 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
11851 }
11852 else
11853 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
11854 mips_cpreturn_register, 0);
11855 macro_end ();
11856
11857 demand_empty_rest_of_line ();
11858 }
11859
11860 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11861 code. It sets the offset to use in gp_rel relocations. */
11862
11863 static void
11864 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
11865 {
11866 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11867 We also need NewABI support. */
11868 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11869 {
11870 s_ignore (0);
11871 return;
11872 }
11873
11874 mips_gprel_offset = get_absolute_expression ();
11875
11876 demand_empty_rest_of_line ();
11877 }
11878
11879 /* Handle the .gpword pseudo-op. This is used when generating PIC
11880 code. It generates a 32 bit GP relative reloc. */
11881
11882 static void
11883 s_gpword (int ignore ATTRIBUTE_UNUSED)
11884 {
11885 symbolS *label;
11886 expressionS ex;
11887 char *p;
11888
11889 /* When not generating PIC code, this is treated as .word. */
11890 if (mips_pic != SVR4_PIC)
11891 {
11892 s_cons (2);
11893 return;
11894 }
11895
11896 label = insn_labels != NULL ? insn_labels->label : NULL;
11897 mips_emit_delays ();
11898 if (auto_align)
11899 mips_align (2, 0, label);
11900 mips_clear_insn_labels ();
11901
11902 expression (&ex);
11903
11904 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11905 {
11906 as_bad (_("Unsupported use of .gpword"));
11907 ignore_rest_of_line ();
11908 }
11909
11910 p = frag_more (4);
11911 md_number_to_chars (p, 0, 4);
11912 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
11913 BFD_RELOC_GPREL32);
11914
11915 demand_empty_rest_of_line ();
11916 }
11917
11918 static void
11919 s_gpdword (int ignore ATTRIBUTE_UNUSED)
11920 {
11921 symbolS *label;
11922 expressionS ex;
11923 char *p;
11924
11925 /* When not generating PIC code, this is treated as .dword. */
11926 if (mips_pic != SVR4_PIC)
11927 {
11928 s_cons (3);
11929 return;
11930 }
11931
11932 label = insn_labels != NULL ? insn_labels->label : NULL;
11933 mips_emit_delays ();
11934 if (auto_align)
11935 mips_align (3, 0, label);
11936 mips_clear_insn_labels ();
11937
11938 expression (&ex);
11939
11940 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11941 {
11942 as_bad (_("Unsupported use of .gpdword"));
11943 ignore_rest_of_line ();
11944 }
11945
11946 p = frag_more (8);
11947 md_number_to_chars (p, 0, 8);
11948 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
11949 BFD_RELOC_GPREL32)->fx_tcbit = 1;
11950
11951 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
11952 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
11953 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
11954
11955 demand_empty_rest_of_line ();
11956 }
11957
11958 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11959 tables in SVR4 PIC code. */
11960
11961 static void
11962 s_cpadd (int ignore ATTRIBUTE_UNUSED)
11963 {
11964 int reg;
11965
11966 /* This is ignored when not generating SVR4 PIC code. */
11967 if (mips_pic != SVR4_PIC)
11968 {
11969 s_ignore (0);
11970 return;
11971 }
11972
11973 /* Add $gp to the register named as an argument. */
11974 macro_start ();
11975 reg = tc_get_register (0);
11976 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
11977 macro_end ();
11978
11979 demand_empty_rest_of_line ();
11980 }
11981
11982 /* Handle the .insn pseudo-op. This marks instruction labels in
11983 mips16 mode. This permits the linker to handle them specially,
11984 such as generating jalx instructions when needed. We also make
11985 them odd for the duration of the assembly, in order to generate the
11986 right sort of code. We will make them even in the adjust_symtab
11987 routine, while leaving them marked. This is convenient for the
11988 debugger and the disassembler. The linker knows to make them odd
11989 again. */
11990
11991 static void
11992 s_insn (int ignore ATTRIBUTE_UNUSED)
11993 {
11994 mips16_mark_labels ();
11995
11996 demand_empty_rest_of_line ();
11997 }
11998
11999 /* Handle a .stabn directive. We need these in order to mark a label
12000 as being a mips16 text label correctly. Sometimes the compiler
12001 will emit a label, followed by a .stabn, and then switch sections.
12002 If the label and .stabn are in mips16 mode, then the label is
12003 really a mips16 text label. */
12004
12005 static void
12006 s_mips_stab (int type)
12007 {
12008 if (type == 'n')
12009 mips16_mark_labels ();
12010
12011 s_stab (type);
12012 }
12013
12014 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12015 */
12016
12017 static void
12018 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12019 {
12020 char *name;
12021 int c;
12022 symbolS *symbolP;
12023 expressionS exp;
12024
12025 name = input_line_pointer;
12026 c = get_symbol_end ();
12027 symbolP = symbol_find_or_make (name);
12028 S_SET_WEAK (symbolP);
12029 *input_line_pointer = c;
12030
12031 SKIP_WHITESPACE ();
12032
12033 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12034 {
12035 if (S_IS_DEFINED (symbolP))
12036 {
12037 as_bad ("ignoring attempt to redefine symbol %s",
12038 S_GET_NAME (symbolP));
12039 ignore_rest_of_line ();
12040 return;
12041 }
12042
12043 if (*input_line_pointer == ',')
12044 {
12045 ++input_line_pointer;
12046 SKIP_WHITESPACE ();
12047 }
12048
12049 expression (&exp);
12050 if (exp.X_op != O_symbol)
12051 {
12052 as_bad ("bad .weakext directive");
12053 ignore_rest_of_line ();
12054 return;
12055 }
12056 symbol_set_value_expression (symbolP, &exp);
12057 }
12058
12059 demand_empty_rest_of_line ();
12060 }
12061
12062 /* Parse a register string into a number. Called from the ECOFF code
12063 to parse .frame. The argument is non-zero if this is the frame
12064 register, so that we can record it in mips_frame_reg. */
12065
12066 int
12067 tc_get_register (int frame)
12068 {
12069 int reg;
12070
12071 SKIP_WHITESPACE ();
12072 if (*input_line_pointer++ != '$')
12073 {
12074 as_warn (_("expected `$'"));
12075 reg = ZERO;
12076 }
12077 else if (ISDIGIT (*input_line_pointer))
12078 {
12079 reg = get_absolute_expression ();
12080 if (reg < 0 || reg >= 32)
12081 {
12082 as_warn (_("Bad register number"));
12083 reg = ZERO;
12084 }
12085 }
12086 else
12087 {
12088 if (strncmp (input_line_pointer, "ra", 2) == 0)
12089 {
12090 reg = RA;
12091 input_line_pointer += 2;
12092 }
12093 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12094 {
12095 reg = FP;
12096 input_line_pointer += 2;
12097 }
12098 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12099 {
12100 reg = SP;
12101 input_line_pointer += 2;
12102 }
12103 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12104 {
12105 reg = GP;
12106 input_line_pointer += 2;
12107 }
12108 else if (strncmp (input_line_pointer, "at", 2) == 0)
12109 {
12110 reg = AT;
12111 input_line_pointer += 2;
12112 }
12113 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12114 {
12115 reg = KT0;
12116 input_line_pointer += 3;
12117 }
12118 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12119 {
12120 reg = KT1;
12121 input_line_pointer += 3;
12122 }
12123 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12124 {
12125 reg = ZERO;
12126 input_line_pointer += 4;
12127 }
12128 else
12129 {
12130 as_warn (_("Unrecognized register name"));
12131 reg = ZERO;
12132 while (ISALNUM(*input_line_pointer))
12133 input_line_pointer++;
12134 }
12135 }
12136 if (frame)
12137 {
12138 mips_frame_reg = reg != 0 ? reg : SP;
12139 mips_frame_reg_valid = 1;
12140 mips_cprestore_valid = 0;
12141 }
12142 return reg;
12143 }
12144
12145 valueT
12146 md_section_align (asection *seg, valueT addr)
12147 {
12148 int align = bfd_get_section_alignment (stdoutput, seg);
12149
12150 #ifdef OBJ_ELF
12151 /* We don't need to align ELF sections to the full alignment.
12152 However, Irix 5 may prefer that we align them at least to a 16
12153 byte boundary. We don't bother to align the sections if we are
12154 targeted for an embedded system. */
12155 if (strcmp (TARGET_OS, "elf") == 0)
12156 return addr;
12157 if (align > 4)
12158 align = 4;
12159 #endif
12160
12161 return ((addr + (1 << align) - 1) & (-1 << align));
12162 }
12163
12164 /* Utility routine, called from above as well. If called while the
12165 input file is still being read, it's only an approximation. (For
12166 example, a symbol may later become defined which appeared to be
12167 undefined earlier.) */
12168
12169 static int
12170 nopic_need_relax (symbolS *sym, int before_relaxing)
12171 {
12172 if (sym == 0)
12173 return 0;
12174
12175 if (g_switch_value > 0)
12176 {
12177 const char *symname;
12178 int change;
12179
12180 /* Find out whether this symbol can be referenced off the $gp
12181 register. It can be if it is smaller than the -G size or if
12182 it is in the .sdata or .sbss section. Certain symbols can
12183 not be referenced off the $gp, although it appears as though
12184 they can. */
12185 symname = S_GET_NAME (sym);
12186 if (symname != (const char *) NULL
12187 && (strcmp (symname, "eprol") == 0
12188 || strcmp (symname, "etext") == 0
12189 || strcmp (symname, "_gp") == 0
12190 || strcmp (symname, "edata") == 0
12191 || strcmp (symname, "_fbss") == 0
12192 || strcmp (symname, "_fdata") == 0
12193 || strcmp (symname, "_ftext") == 0
12194 || strcmp (symname, "end") == 0
12195 || strcmp (symname, "_gp_disp") == 0))
12196 change = 1;
12197 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12198 && (0
12199 #ifndef NO_ECOFF_DEBUGGING
12200 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12201 && (symbol_get_obj (sym)->ecoff_extern_size
12202 <= g_switch_value))
12203 #endif
12204 /* We must defer this decision until after the whole
12205 file has been read, since there might be a .extern
12206 after the first use of this symbol. */
12207 || (before_relaxing
12208 #ifndef NO_ECOFF_DEBUGGING
12209 && symbol_get_obj (sym)->ecoff_extern_size == 0
12210 #endif
12211 && S_GET_VALUE (sym) == 0)
12212 || (S_GET_VALUE (sym) != 0
12213 && S_GET_VALUE (sym) <= g_switch_value)))
12214 change = 0;
12215 else
12216 {
12217 const char *segname;
12218
12219 segname = segment_name (S_GET_SEGMENT (sym));
12220 assert (strcmp (segname, ".lit8") != 0
12221 && strcmp (segname, ".lit4") != 0);
12222 change = (strcmp (segname, ".sdata") != 0
12223 && strcmp (segname, ".sbss") != 0
12224 && strncmp (segname, ".sdata.", 7) != 0
12225 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12226 }
12227 return change;
12228 }
12229 else
12230 /* We are not optimizing for the $gp register. */
12231 return 1;
12232 }
12233
12234
12235 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12236
12237 static bfd_boolean
12238 pic_need_relax (symbolS *sym, asection *segtype)
12239 {
12240 asection *symsec;
12241 bfd_boolean linkonce;
12242
12243 /* Handle the case of a symbol equated to another symbol. */
12244 while (symbol_equated_reloc_p (sym))
12245 {
12246 symbolS *n;
12247
12248 /* It's possible to get a loop here in a badly written
12249 program. */
12250 n = symbol_get_value_expression (sym)->X_add_symbol;
12251 if (n == sym)
12252 break;
12253 sym = n;
12254 }
12255
12256 symsec = S_GET_SEGMENT (sym);
12257
12258 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12259 linkonce = FALSE;
12260 if (symsec != segtype && ! S_IS_LOCAL (sym))
12261 {
12262 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12263 != 0)
12264 linkonce = TRUE;
12265
12266 /* The GNU toolchain uses an extension for ELF: a section
12267 beginning with the magic string .gnu.linkonce is a linkonce
12268 section. */
12269 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12270 sizeof ".gnu.linkonce" - 1) == 0)
12271 linkonce = TRUE;
12272 }
12273
12274 /* This must duplicate the test in adjust_reloc_syms. */
12275 return (symsec != &bfd_und_section
12276 && symsec != &bfd_abs_section
12277 && ! bfd_is_com_section (symsec)
12278 && !linkonce
12279 #ifdef OBJ_ELF
12280 /* A global or weak symbol is treated as external. */
12281 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12282 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12283 #endif
12284 );
12285 }
12286
12287
12288 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12289 extended opcode. SEC is the section the frag is in. */
12290
12291 static int
12292 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12293 {
12294 int type;
12295 register const struct mips16_immed_operand *op;
12296 offsetT val;
12297 int mintiny, maxtiny;
12298 segT symsec;
12299 fragS *sym_frag;
12300
12301 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12302 return 0;
12303 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12304 return 1;
12305
12306 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12307 op = mips16_immed_operands;
12308 while (op->type != type)
12309 {
12310 ++op;
12311 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12312 }
12313
12314 if (op->unsp)
12315 {
12316 if (type == '<' || type == '>' || type == '[' || type == ']')
12317 {
12318 mintiny = 1;
12319 maxtiny = 1 << op->nbits;
12320 }
12321 else
12322 {
12323 mintiny = 0;
12324 maxtiny = (1 << op->nbits) - 1;
12325 }
12326 }
12327 else
12328 {
12329 mintiny = - (1 << (op->nbits - 1));
12330 maxtiny = (1 << (op->nbits - 1)) - 1;
12331 }
12332
12333 sym_frag = symbol_get_frag (fragp->fr_symbol);
12334 val = S_GET_VALUE (fragp->fr_symbol);
12335 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12336
12337 if (op->pcrel)
12338 {
12339 addressT addr;
12340
12341 /* We won't have the section when we are called from
12342 mips_relax_frag. However, we will always have been called
12343 from md_estimate_size_before_relax first. If this is a
12344 branch to a different section, we mark it as such. If SEC is
12345 NULL, and the frag is not marked, then it must be a branch to
12346 the same section. */
12347 if (sec == NULL)
12348 {
12349 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12350 return 1;
12351 }
12352 else
12353 {
12354 /* Must have been called from md_estimate_size_before_relax. */
12355 if (symsec != sec)
12356 {
12357 fragp->fr_subtype =
12358 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12359
12360 /* FIXME: We should support this, and let the linker
12361 catch branches and loads that are out of range. */
12362 as_bad_where (fragp->fr_file, fragp->fr_line,
12363 _("unsupported PC relative reference to different section"));
12364
12365 return 1;
12366 }
12367 if (fragp != sym_frag && sym_frag->fr_address == 0)
12368 /* Assume non-extended on the first relaxation pass.
12369 The address we have calculated will be bogus if this is
12370 a forward branch to another frag, as the forward frag
12371 will have fr_address == 0. */
12372 return 0;
12373 }
12374
12375 /* In this case, we know for sure that the symbol fragment is in
12376 the same section. If the relax_marker of the symbol fragment
12377 differs from the relax_marker of this fragment, we have not
12378 yet adjusted the symbol fragment fr_address. We want to add
12379 in STRETCH in order to get a better estimate of the address.
12380 This particularly matters because of the shift bits. */
12381 if (stretch != 0
12382 && sym_frag->relax_marker != fragp->relax_marker)
12383 {
12384 fragS *f;
12385
12386 /* Adjust stretch for any alignment frag. Note that if have
12387 been expanding the earlier code, the symbol may be
12388 defined in what appears to be an earlier frag. FIXME:
12389 This doesn't handle the fr_subtype field, which specifies
12390 a maximum number of bytes to skip when doing an
12391 alignment. */
12392 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12393 {
12394 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12395 {
12396 if (stretch < 0)
12397 stretch = - ((- stretch)
12398 & ~ ((1 << (int) f->fr_offset) - 1));
12399 else
12400 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12401 if (stretch == 0)
12402 break;
12403 }
12404 }
12405 if (f != NULL)
12406 val += stretch;
12407 }
12408
12409 addr = fragp->fr_address + fragp->fr_fix;
12410
12411 /* The base address rules are complicated. The base address of
12412 a branch is the following instruction. The base address of a
12413 PC relative load or add is the instruction itself, but if it
12414 is in a delay slot (in which case it can not be extended) use
12415 the address of the instruction whose delay slot it is in. */
12416 if (type == 'p' || type == 'q')
12417 {
12418 addr += 2;
12419
12420 /* If we are currently assuming that this frag should be
12421 extended, then, the current address is two bytes
12422 higher. */
12423 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12424 addr += 2;
12425
12426 /* Ignore the low bit in the target, since it will be set
12427 for a text label. */
12428 if ((val & 1) != 0)
12429 --val;
12430 }
12431 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12432 addr -= 4;
12433 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12434 addr -= 2;
12435
12436 val -= addr & ~ ((1 << op->shift) - 1);
12437
12438 /* Branch offsets have an implicit 0 in the lowest bit. */
12439 if (type == 'p' || type == 'q')
12440 val /= 2;
12441
12442 /* If any of the shifted bits are set, we must use an extended
12443 opcode. If the address depends on the size of this
12444 instruction, this can lead to a loop, so we arrange to always
12445 use an extended opcode. We only check this when we are in
12446 the main relaxation loop, when SEC is NULL. */
12447 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12448 {
12449 fragp->fr_subtype =
12450 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12451 return 1;
12452 }
12453
12454 /* If we are about to mark a frag as extended because the value
12455 is precisely maxtiny + 1, then there is a chance of an
12456 infinite loop as in the following code:
12457 la $4,foo
12458 .skip 1020
12459 .align 2
12460 foo:
12461 In this case when the la is extended, foo is 0x3fc bytes
12462 away, so the la can be shrunk, but then foo is 0x400 away, so
12463 the la must be extended. To avoid this loop, we mark the
12464 frag as extended if it was small, and is about to become
12465 extended with a value of maxtiny + 1. */
12466 if (val == ((maxtiny + 1) << op->shift)
12467 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12468 && sec == NULL)
12469 {
12470 fragp->fr_subtype =
12471 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12472 return 1;
12473 }
12474 }
12475 else if (symsec != absolute_section && sec != NULL)
12476 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12477
12478 if ((val & ((1 << op->shift) - 1)) != 0
12479 || val < (mintiny << op->shift)
12480 || val > (maxtiny << op->shift))
12481 return 1;
12482 else
12483 return 0;
12484 }
12485
12486 /* Compute the length of a branch sequence, and adjust the
12487 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12488 worst-case length is computed, with UPDATE being used to indicate
12489 whether an unconditional (-1), branch-likely (+1) or regular (0)
12490 branch is to be computed. */
12491 static int
12492 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12493 {
12494 bfd_boolean toofar;
12495 int length;
12496
12497 if (fragp
12498 && S_IS_DEFINED (fragp->fr_symbol)
12499 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12500 {
12501 addressT addr;
12502 offsetT val;
12503
12504 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12505
12506 addr = fragp->fr_address + fragp->fr_fix + 4;
12507
12508 val -= addr;
12509
12510 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12511 }
12512 else if (fragp)
12513 /* If the symbol is not defined or it's in a different segment,
12514 assume the user knows what's going on and emit a short
12515 branch. */
12516 toofar = FALSE;
12517 else
12518 toofar = TRUE;
12519
12520 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12521 fragp->fr_subtype
12522 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12523 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12524 RELAX_BRANCH_LINK (fragp->fr_subtype),
12525 toofar);
12526
12527 length = 4;
12528 if (toofar)
12529 {
12530 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12531 length += 8;
12532
12533 if (mips_pic != NO_PIC)
12534 {
12535 /* Additional space for PIC loading of target address. */
12536 length += 8;
12537 if (mips_opts.isa == ISA_MIPS1)
12538 /* Additional space for $at-stabilizing nop. */
12539 length += 4;
12540 }
12541
12542 /* If branch is conditional. */
12543 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12544 length += 8;
12545 }
12546
12547 return length;
12548 }
12549
12550 /* Estimate the size of a frag before relaxing. Unless this is the
12551 mips16, we are not really relaxing here, and the final size is
12552 encoded in the subtype information. For the mips16, we have to
12553 decide whether we are using an extended opcode or not. */
12554
12555 int
12556 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12557 {
12558 int change;
12559
12560 if (RELAX_BRANCH_P (fragp->fr_subtype))
12561 {
12562
12563 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12564
12565 return fragp->fr_var;
12566 }
12567
12568 if (RELAX_MIPS16_P (fragp->fr_subtype))
12569 /* We don't want to modify the EXTENDED bit here; it might get us
12570 into infinite loops. We change it only in mips_relax_frag(). */
12571 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12572
12573 if (mips_pic == NO_PIC)
12574 change = nopic_need_relax (fragp->fr_symbol, 0);
12575 else if (mips_pic == SVR4_PIC)
12576 change = pic_need_relax (fragp->fr_symbol, segtype);
12577 else
12578 abort ();
12579
12580 if (change)
12581 {
12582 fragp->fr_subtype |= RELAX_USE_SECOND;
12583 return -RELAX_FIRST (fragp->fr_subtype);
12584 }
12585 else
12586 return -RELAX_SECOND (fragp->fr_subtype);
12587 }
12588
12589 /* This is called to see whether a reloc against a defined symbol
12590 should be converted into a reloc against a section. */
12591
12592 int
12593 mips_fix_adjustable (fixS *fixp)
12594 {
12595 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12596 about the format of the offset in the .o file. */
12597 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12598 return 0;
12599
12600 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12601 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12602 return 0;
12603
12604 if (fixp->fx_addsy == NULL)
12605 return 1;
12606
12607 /* If symbol SYM is in a mergeable section, relocations of the form
12608 SYM + 0 can usually be made section-relative. The mergeable data
12609 is then identified by the section offset rather than by the symbol.
12610
12611 However, if we're generating REL LO16 relocations, the offset is split
12612 between the LO16 and parterning high part relocation. The linker will
12613 need to recalculate the complete offset in order to correctly identify
12614 the merge data.
12615
12616 The linker has traditionally not looked for the parterning high part
12617 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12618 placed anywhere. Rather than break backwards compatibility by changing
12619 this, it seems better not to force the issue, and instead keep the
12620 original symbol. This will work with either linker behavior. */
12621 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12622 && HAVE_IN_PLACE_ADDENDS
12623 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12624 return 0;
12625
12626 #ifdef OBJ_ELF
12627 /* Don't adjust relocations against mips16 symbols, so that the linker
12628 can find them if it needs to set up a stub. */
12629 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12630 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12631 && fixp->fx_subsy == NULL)
12632 return 0;
12633 #endif
12634
12635 return 1;
12636 }
12637
12638 /* Translate internal representation of relocation info to BFD target
12639 format. */
12640
12641 arelent **
12642 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12643 {
12644 static arelent *retval[4];
12645 arelent *reloc;
12646 bfd_reloc_code_real_type code;
12647
12648 memset (retval, 0, sizeof(retval));
12649 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12650 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12651 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12652 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12653
12654 assert (! fixp->fx_pcrel);
12655 reloc->addend = fixp->fx_addnumber;
12656
12657 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12658 entry to be used in the relocation's section offset. */
12659 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12660 {
12661 reloc->address = reloc->addend;
12662 reloc->addend = 0;
12663 }
12664
12665 code = fixp->fx_r_type;
12666
12667 /* To support a PC relative reloc, we used a Cygnus extension.
12668 We check for that here to make sure that we don't let such a
12669 reloc escape normally. (FIXME: This was formerly used by
12670 embedded-PIC support, but is now used by branch handling in
12671 general. That probably should be fixed.) */
12672 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12673 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12674 && code == BFD_RELOC_16_PCREL_S2)
12675 reloc->howto = NULL;
12676 else
12677 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12678
12679 if (reloc->howto == NULL)
12680 {
12681 as_bad_where (fixp->fx_file, fixp->fx_line,
12682 _("Can not represent %s relocation in this object file format"),
12683 bfd_get_reloc_code_name (code));
12684 retval[0] = NULL;
12685 }
12686
12687 return retval;
12688 }
12689
12690 /* Relax a machine dependent frag. This returns the amount by which
12691 the current size of the frag should change. */
12692
12693 int
12694 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12695 {
12696 if (RELAX_BRANCH_P (fragp->fr_subtype))
12697 {
12698 offsetT old_var = fragp->fr_var;
12699
12700 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12701
12702 return fragp->fr_var - old_var;
12703 }
12704
12705 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12706 return 0;
12707
12708 if (mips16_extended_frag (fragp, NULL, stretch))
12709 {
12710 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12711 return 0;
12712 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12713 return 2;
12714 }
12715 else
12716 {
12717 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12718 return 0;
12719 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12720 return -2;
12721 }
12722
12723 return 0;
12724 }
12725
12726 /* Convert a machine dependent frag. */
12727
12728 void
12729 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12730 {
12731 if (RELAX_BRANCH_P (fragp->fr_subtype))
12732 {
12733 bfd_byte *buf;
12734 unsigned long insn;
12735 expressionS exp;
12736 fixS *fixp;
12737
12738 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12739
12740 if (target_big_endian)
12741 insn = bfd_getb32 (buf);
12742 else
12743 insn = bfd_getl32 (buf);
12744
12745 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12746 {
12747 /* We generate a fixup instead of applying it right now
12748 because, if there are linker relaxations, we're going to
12749 need the relocations. */
12750 exp.X_op = O_symbol;
12751 exp.X_add_symbol = fragp->fr_symbol;
12752 exp.X_add_number = fragp->fr_offset;
12753
12754 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12755 4, &exp, 1,
12756 BFD_RELOC_16_PCREL_S2);
12757 fixp->fx_file = fragp->fr_file;
12758 fixp->fx_line = fragp->fr_line;
12759
12760 md_number_to_chars ((char *) buf, insn, 4);
12761 buf += 4;
12762 }
12763 else
12764 {
12765 int i;
12766
12767 as_warn_where (fragp->fr_file, fragp->fr_line,
12768 _("relaxed out-of-range branch into a jump"));
12769
12770 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12771 goto uncond;
12772
12773 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12774 {
12775 /* Reverse the branch. */
12776 switch ((insn >> 28) & 0xf)
12777 {
12778 case 4:
12779 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12780 have the condition reversed by tweaking a single
12781 bit, and their opcodes all have 0x4???????. */
12782 assert ((insn & 0xf1000000) == 0x41000000);
12783 insn ^= 0x00010000;
12784 break;
12785
12786 case 0:
12787 /* bltz 0x04000000 bgez 0x04010000
12788 bltzal 0x04100000 bgezal 0x04110000 */
12789 assert ((insn & 0xfc0e0000) == 0x04000000);
12790 insn ^= 0x00010000;
12791 break;
12792
12793 case 1:
12794 /* beq 0x10000000 bne 0x14000000
12795 blez 0x18000000 bgtz 0x1c000000 */
12796 insn ^= 0x04000000;
12797 break;
12798
12799 default:
12800 abort ();
12801 }
12802 }
12803
12804 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12805 {
12806 /* Clear the and-link bit. */
12807 assert ((insn & 0xfc1c0000) == 0x04100000);
12808
12809 /* bltzal 0x04100000 bgezal 0x04110000
12810 bltzall 0x04120000 bgezall 0x04130000 */
12811 insn &= ~0x00100000;
12812 }
12813
12814 /* Branch over the branch (if the branch was likely) or the
12815 full jump (not likely case). Compute the offset from the
12816 current instruction to branch to. */
12817 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12818 i = 16;
12819 else
12820 {
12821 /* How many bytes in instructions we've already emitted? */
12822 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12823 /* How many bytes in instructions from here to the end? */
12824 i = fragp->fr_var - i;
12825 }
12826 /* Convert to instruction count. */
12827 i >>= 2;
12828 /* Branch counts from the next instruction. */
12829 i--;
12830 insn |= i;
12831 /* Branch over the jump. */
12832 md_number_to_chars ((char *) buf, insn, 4);
12833 buf += 4;
12834
12835 /* Nop */
12836 md_number_to_chars ((char *) buf, 0, 4);
12837 buf += 4;
12838
12839 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12840 {
12841 /* beql $0, $0, 2f */
12842 insn = 0x50000000;
12843 /* Compute the PC offset from the current instruction to
12844 the end of the variable frag. */
12845 /* How many bytes in instructions we've already emitted? */
12846 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
12847 /* How many bytes in instructions from here to the end? */
12848 i = fragp->fr_var - i;
12849 /* Convert to instruction count. */
12850 i >>= 2;
12851 /* Don't decrement i, because we want to branch over the
12852 delay slot. */
12853
12854 insn |= i;
12855 md_number_to_chars ((char *) buf, insn, 4);
12856 buf += 4;
12857
12858 md_number_to_chars ((char *) buf, 0, 4);
12859 buf += 4;
12860 }
12861
12862 uncond:
12863 if (mips_pic == NO_PIC)
12864 {
12865 /* j or jal. */
12866 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
12867 ? 0x0c000000 : 0x08000000);
12868 exp.X_op = O_symbol;
12869 exp.X_add_symbol = fragp->fr_symbol;
12870 exp.X_add_number = fragp->fr_offset;
12871
12872 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12873 4, &exp, 0, BFD_RELOC_MIPS_JMP);
12874 fixp->fx_file = fragp->fr_file;
12875 fixp->fx_line = fragp->fr_line;
12876
12877 md_number_to_chars ((char *) buf, insn, 4);
12878 buf += 4;
12879 }
12880 else
12881 {
12882 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
12883 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
12884 exp.X_op = O_symbol;
12885 exp.X_add_symbol = fragp->fr_symbol;
12886 exp.X_add_number = fragp->fr_offset;
12887
12888 if (fragp->fr_offset)
12889 {
12890 exp.X_add_symbol = make_expr_symbol (&exp);
12891 exp.X_add_number = 0;
12892 }
12893
12894 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12895 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
12896 fixp->fx_file = fragp->fr_file;
12897 fixp->fx_line = fragp->fr_line;
12898
12899 md_number_to_chars ((char *) buf, insn, 4);
12900 buf += 4;
12901
12902 if (mips_opts.isa == ISA_MIPS1)
12903 {
12904 /* nop */
12905 md_number_to_chars ((char *) buf, 0, 4);
12906 buf += 4;
12907 }
12908
12909 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
12910 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
12911
12912 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12913 4, &exp, 0, BFD_RELOC_LO16);
12914 fixp->fx_file = fragp->fr_file;
12915 fixp->fx_line = fragp->fr_line;
12916
12917 md_number_to_chars ((char *) buf, insn, 4);
12918 buf += 4;
12919
12920 /* j(al)r $at. */
12921 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
12922 insn = 0x0020f809;
12923 else
12924 insn = 0x00200008;
12925
12926 md_number_to_chars ((char *) buf, insn, 4);
12927 buf += 4;
12928 }
12929 }
12930
12931 assert (buf == (bfd_byte *)fragp->fr_literal
12932 + fragp->fr_fix + fragp->fr_var);
12933
12934 fragp->fr_fix += fragp->fr_var;
12935
12936 return;
12937 }
12938
12939 if (RELAX_MIPS16_P (fragp->fr_subtype))
12940 {
12941 int type;
12942 register const struct mips16_immed_operand *op;
12943 bfd_boolean small, ext;
12944 offsetT val;
12945 bfd_byte *buf;
12946 unsigned long insn;
12947 bfd_boolean use_extend;
12948 unsigned short extend;
12949
12950 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12951 op = mips16_immed_operands;
12952 while (op->type != type)
12953 ++op;
12954
12955 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12956 {
12957 small = FALSE;
12958 ext = TRUE;
12959 }
12960 else
12961 {
12962 small = TRUE;
12963 ext = FALSE;
12964 }
12965
12966 resolve_symbol_value (fragp->fr_symbol);
12967 val = S_GET_VALUE (fragp->fr_symbol);
12968 if (op->pcrel)
12969 {
12970 addressT addr;
12971
12972 addr = fragp->fr_address + fragp->fr_fix;
12973
12974 /* The rules for the base address of a PC relative reloc are
12975 complicated; see mips16_extended_frag. */
12976 if (type == 'p' || type == 'q')
12977 {
12978 addr += 2;
12979 if (ext)
12980 addr += 2;
12981 /* Ignore the low bit in the target, since it will be
12982 set for a text label. */
12983 if ((val & 1) != 0)
12984 --val;
12985 }
12986 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12987 addr -= 4;
12988 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12989 addr -= 2;
12990
12991 addr &= ~ (addressT) ((1 << op->shift) - 1);
12992 val -= addr;
12993
12994 /* Make sure the section winds up with the alignment we have
12995 assumed. */
12996 if (op->shift > 0)
12997 record_alignment (asec, op->shift);
12998 }
12999
13000 if (ext
13001 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13002 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13003 as_warn_where (fragp->fr_file, fragp->fr_line,
13004 _("extended instruction in delay slot"));
13005
13006 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13007
13008 if (target_big_endian)
13009 insn = bfd_getb16 (buf);
13010 else
13011 insn = bfd_getl16 (buf);
13012
13013 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13014 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13015 small, ext, &insn, &use_extend, &extend);
13016
13017 if (use_extend)
13018 {
13019 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13020 fragp->fr_fix += 2;
13021 buf += 2;
13022 }
13023
13024 md_number_to_chars ((char *) buf, insn, 2);
13025 fragp->fr_fix += 2;
13026 buf += 2;
13027 }
13028 else
13029 {
13030 int first, second;
13031 fixS *fixp;
13032
13033 first = RELAX_FIRST (fragp->fr_subtype);
13034 second = RELAX_SECOND (fragp->fr_subtype);
13035 fixp = (fixS *) fragp->fr_opcode;
13036
13037 /* Possibly emit a warning if we've chosen the longer option. */
13038 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13039 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13040 {
13041 const char *msg = macro_warning (fragp->fr_subtype);
13042 if (msg != 0)
13043 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13044 }
13045
13046 /* Go through all the fixups for the first sequence. Disable them
13047 (by marking them as done) if we're going to use the second
13048 sequence instead. */
13049 while (fixp
13050 && fixp->fx_frag == fragp
13051 && fixp->fx_where < fragp->fr_fix - second)
13052 {
13053 if (fragp->fr_subtype & RELAX_USE_SECOND)
13054 fixp->fx_done = 1;
13055 fixp = fixp->fx_next;
13056 }
13057
13058 /* Go through the fixups for the second sequence. Disable them if
13059 we're going to use the first sequence, otherwise adjust their
13060 addresses to account for the relaxation. */
13061 while (fixp && fixp->fx_frag == fragp)
13062 {
13063 if (fragp->fr_subtype & RELAX_USE_SECOND)
13064 fixp->fx_where -= first;
13065 else
13066 fixp->fx_done = 1;
13067 fixp = fixp->fx_next;
13068 }
13069
13070 /* Now modify the frag contents. */
13071 if (fragp->fr_subtype & RELAX_USE_SECOND)
13072 {
13073 char *start;
13074
13075 start = fragp->fr_literal + fragp->fr_fix - first - second;
13076 memmove (start, start + first, second);
13077 fragp->fr_fix -= first;
13078 }
13079 else
13080 fragp->fr_fix -= second;
13081 }
13082 }
13083
13084 #ifdef OBJ_ELF
13085
13086 /* This function is called after the relocs have been generated.
13087 We've been storing mips16 text labels as odd. Here we convert them
13088 back to even for the convenience of the debugger. */
13089
13090 void
13091 mips_frob_file_after_relocs (void)
13092 {
13093 asymbol **syms;
13094 unsigned int count, i;
13095
13096 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13097 return;
13098
13099 syms = bfd_get_outsymbols (stdoutput);
13100 count = bfd_get_symcount (stdoutput);
13101 for (i = 0; i < count; i++, syms++)
13102 {
13103 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13104 && ((*syms)->value & 1) != 0)
13105 {
13106 (*syms)->value &= ~1;
13107 /* If the symbol has an odd size, it was probably computed
13108 incorrectly, so adjust that as well. */
13109 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13110 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13111 }
13112 }
13113 }
13114
13115 #endif
13116
13117 /* This function is called whenever a label is defined. It is used
13118 when handling branch delays; if a branch has a label, we assume we
13119 can not move it. */
13120
13121 void
13122 mips_define_label (symbolS *sym)
13123 {
13124 struct insn_label_list *l;
13125
13126 if (free_insn_labels == NULL)
13127 l = (struct insn_label_list *) xmalloc (sizeof *l);
13128 else
13129 {
13130 l = free_insn_labels;
13131 free_insn_labels = l->next;
13132 }
13133
13134 l->label = sym;
13135 l->next = insn_labels;
13136 insn_labels = l;
13137 }
13138 \f
13139 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13140
13141 /* Some special processing for a MIPS ELF file. */
13142
13143 void
13144 mips_elf_final_processing (void)
13145 {
13146 /* Write out the register information. */
13147 if (mips_abi != N64_ABI)
13148 {
13149 Elf32_RegInfo s;
13150
13151 s.ri_gprmask = mips_gprmask;
13152 s.ri_cprmask[0] = mips_cprmask[0];
13153 s.ri_cprmask[1] = mips_cprmask[1];
13154 s.ri_cprmask[2] = mips_cprmask[2];
13155 s.ri_cprmask[3] = mips_cprmask[3];
13156 /* The gp_value field is set by the MIPS ELF backend. */
13157
13158 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13159 ((Elf32_External_RegInfo *)
13160 mips_regmask_frag));
13161 }
13162 else
13163 {
13164 Elf64_Internal_RegInfo s;
13165
13166 s.ri_gprmask = mips_gprmask;
13167 s.ri_pad = 0;
13168 s.ri_cprmask[0] = mips_cprmask[0];
13169 s.ri_cprmask[1] = mips_cprmask[1];
13170 s.ri_cprmask[2] = mips_cprmask[2];
13171 s.ri_cprmask[3] = mips_cprmask[3];
13172 /* The gp_value field is set by the MIPS ELF backend. */
13173
13174 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13175 ((Elf64_External_RegInfo *)
13176 mips_regmask_frag));
13177 }
13178
13179 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13180 sort of BFD interface for this. */
13181 if (mips_any_noreorder)
13182 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13183 if (mips_pic != NO_PIC)
13184 {
13185 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13186 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13187 }
13188 if (mips_abicalls)
13189 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13190
13191 /* Set MIPS ELF flags for ASEs. */
13192 if (file_ase_mips16)
13193 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13194 #if 0 /* XXX FIXME */
13195 if (file_ase_mips3d)
13196 elf_elfheader (stdoutput)->e_flags |= ???;
13197 #endif
13198 if (file_ase_mdmx)
13199 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13200
13201 /* Set the MIPS ELF ABI flags. */
13202 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13203 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13204 else if (mips_abi == O64_ABI)
13205 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13206 else if (mips_abi == EABI_ABI)
13207 {
13208 if (!file_mips_gp32)
13209 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13210 else
13211 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13212 }
13213 else if (mips_abi == N32_ABI)
13214 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13215
13216 /* Nothing to do for N64_ABI. */
13217
13218 if (mips_32bitmode)
13219 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13220 }
13221
13222 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13223 \f
13224 typedef struct proc {
13225 symbolS *func_sym;
13226 symbolS *func_end_sym;
13227 unsigned long reg_mask;
13228 unsigned long reg_offset;
13229 unsigned long fpreg_mask;
13230 unsigned long fpreg_offset;
13231 unsigned long frame_offset;
13232 unsigned long frame_reg;
13233 unsigned long pc_reg;
13234 } procS;
13235
13236 static procS cur_proc;
13237 static procS *cur_proc_ptr;
13238 static int numprocs;
13239
13240 /* Fill in an rs_align_code fragment. */
13241
13242 void
13243 mips_handle_align (fragS *fragp)
13244 {
13245 if (fragp->fr_type != rs_align_code)
13246 return;
13247
13248 if (mips_opts.mips16)
13249 {
13250 static const unsigned char be_nop[] = { 0x65, 0x00 };
13251 static const unsigned char le_nop[] = { 0x00, 0x65 };
13252
13253 int bytes;
13254 char *p;
13255
13256 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13257 p = fragp->fr_literal + fragp->fr_fix;
13258
13259 if (bytes & 1)
13260 {
13261 *p++ = 0;
13262 fragp->fr_fix++;
13263 }
13264
13265 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13266 fragp->fr_var = 2;
13267 }
13268
13269 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13270 }
13271
13272 static void
13273 md_obj_begin (void)
13274 {
13275 }
13276
13277 static void
13278 md_obj_end (void)
13279 {
13280 /* check for premature end, nesting errors, etc */
13281 if (cur_proc_ptr)
13282 as_warn (_("missing .end at end of assembly"));
13283 }
13284
13285 static long
13286 get_number (void)
13287 {
13288 int negative = 0;
13289 long val = 0;
13290
13291 if (*input_line_pointer == '-')
13292 {
13293 ++input_line_pointer;
13294 negative = 1;
13295 }
13296 if (!ISDIGIT (*input_line_pointer))
13297 as_bad (_("expected simple number"));
13298 if (input_line_pointer[0] == '0')
13299 {
13300 if (input_line_pointer[1] == 'x')
13301 {
13302 input_line_pointer += 2;
13303 while (ISXDIGIT (*input_line_pointer))
13304 {
13305 val <<= 4;
13306 val |= hex_value (*input_line_pointer++);
13307 }
13308 return negative ? -val : val;
13309 }
13310 else
13311 {
13312 ++input_line_pointer;
13313 while (ISDIGIT (*input_line_pointer))
13314 {
13315 val <<= 3;
13316 val |= *input_line_pointer++ - '0';
13317 }
13318 return negative ? -val : val;
13319 }
13320 }
13321 if (!ISDIGIT (*input_line_pointer))
13322 {
13323 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13324 *input_line_pointer, *input_line_pointer);
13325 as_warn (_("invalid number"));
13326 return -1;
13327 }
13328 while (ISDIGIT (*input_line_pointer))
13329 {
13330 val *= 10;
13331 val += *input_line_pointer++ - '0';
13332 }
13333 return negative ? -val : val;
13334 }
13335
13336 /* The .file directive; just like the usual .file directive, but there
13337 is an initial number which is the ECOFF file index. In the non-ECOFF
13338 case .file implies DWARF-2. */
13339
13340 static void
13341 s_mips_file (int x ATTRIBUTE_UNUSED)
13342 {
13343 static int first_file_directive = 0;
13344
13345 if (ECOFF_DEBUGGING)
13346 {
13347 get_number ();
13348 s_app_file (0);
13349 }
13350 else
13351 {
13352 char *filename;
13353
13354 filename = dwarf2_directive_file (0);
13355
13356 /* Versions of GCC up to 3.1 start files with a ".file"
13357 directive even for stabs output. Make sure that this
13358 ".file" is handled. Note that you need a version of GCC
13359 after 3.1 in order to support DWARF-2 on MIPS. */
13360 if (filename != NULL && ! first_file_directive)
13361 {
13362 (void) new_logical_line (filename, -1);
13363 s_app_file_string (filename, 0);
13364 }
13365 first_file_directive = 1;
13366 }
13367 }
13368
13369 /* The .loc directive, implying DWARF-2. */
13370
13371 static void
13372 s_mips_loc (int x ATTRIBUTE_UNUSED)
13373 {
13374 if (!ECOFF_DEBUGGING)
13375 dwarf2_directive_loc (0);
13376 }
13377
13378 /* The .end directive. */
13379
13380 static void
13381 s_mips_end (int x ATTRIBUTE_UNUSED)
13382 {
13383 symbolS *p;
13384
13385 /* Following functions need their own .frame and .cprestore directives. */
13386 mips_frame_reg_valid = 0;
13387 mips_cprestore_valid = 0;
13388
13389 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13390 {
13391 p = get_symbol ();
13392 demand_empty_rest_of_line ();
13393 }
13394 else
13395 p = NULL;
13396
13397 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13398 as_warn (_(".end not in text section"));
13399
13400 if (!cur_proc_ptr)
13401 {
13402 as_warn (_(".end directive without a preceding .ent directive."));
13403 demand_empty_rest_of_line ();
13404 return;
13405 }
13406
13407 if (p != NULL)
13408 {
13409 assert (S_GET_NAME (p));
13410 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13411 as_warn (_(".end symbol does not match .ent symbol."));
13412
13413 if (debug_type == DEBUG_STABS)
13414 stabs_generate_asm_endfunc (S_GET_NAME (p),
13415 S_GET_NAME (p));
13416 }
13417 else
13418 as_warn (_(".end directive missing or unknown symbol"));
13419
13420 #ifdef OBJ_ELF
13421 /* Create an expression to calculate the size of the function. */
13422 if (p && cur_proc_ptr)
13423 {
13424 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13425 expressionS *exp = xmalloc (sizeof (expressionS));
13426
13427 obj->size = exp;
13428 exp->X_op = O_subtract;
13429 exp->X_add_symbol = symbol_temp_new_now ();
13430 exp->X_op_symbol = p;
13431 exp->X_add_number = 0;
13432
13433 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13434 }
13435
13436 /* Generate a .pdr section. */
13437 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13438 && mips_flag_pdr)
13439 {
13440 segT saved_seg = now_seg;
13441 subsegT saved_subseg = now_subseg;
13442 valueT dot;
13443 expressionS exp;
13444 char *fragp;
13445
13446 dot = frag_now_fix ();
13447
13448 #ifdef md_flush_pending_output
13449 md_flush_pending_output ();
13450 #endif
13451
13452 assert (pdr_seg);
13453 subseg_set (pdr_seg, 0);
13454
13455 /* Write the symbol. */
13456 exp.X_op = O_symbol;
13457 exp.X_add_symbol = p;
13458 exp.X_add_number = 0;
13459 emit_expr (&exp, 4);
13460
13461 fragp = frag_more (7 * 4);
13462
13463 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13464 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13465 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13466 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13467 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13468 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13469 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13470
13471 subseg_set (saved_seg, saved_subseg);
13472 }
13473 #endif /* OBJ_ELF */
13474
13475 cur_proc_ptr = NULL;
13476 }
13477
13478 /* The .aent and .ent directives. */
13479
13480 static void
13481 s_mips_ent (int aent)
13482 {
13483 symbolS *symbolP;
13484
13485 symbolP = get_symbol ();
13486 if (*input_line_pointer == ',')
13487 ++input_line_pointer;
13488 SKIP_WHITESPACE ();
13489 if (ISDIGIT (*input_line_pointer)
13490 || *input_line_pointer == '-')
13491 get_number ();
13492
13493 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13494 as_warn (_(".ent or .aent not in text section."));
13495
13496 if (!aent && cur_proc_ptr)
13497 as_warn (_("missing .end"));
13498
13499 if (!aent)
13500 {
13501 /* This function needs its own .frame and .cprestore directives. */
13502 mips_frame_reg_valid = 0;
13503 mips_cprestore_valid = 0;
13504
13505 cur_proc_ptr = &cur_proc;
13506 memset (cur_proc_ptr, '\0', sizeof (procS));
13507
13508 cur_proc_ptr->func_sym = symbolP;
13509
13510 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13511
13512 ++numprocs;
13513
13514 if (debug_type == DEBUG_STABS)
13515 stabs_generate_asm_func (S_GET_NAME (symbolP),
13516 S_GET_NAME (symbolP));
13517 }
13518
13519 demand_empty_rest_of_line ();
13520 }
13521
13522 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13523 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13524 s_mips_frame is used so that we can set the PDR information correctly.
13525 We can't use the ecoff routines because they make reference to the ecoff
13526 symbol table (in the mdebug section). */
13527
13528 static void
13529 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13530 {
13531 #ifdef OBJ_ELF
13532 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13533 {
13534 long val;
13535
13536 if (cur_proc_ptr == (procS *) NULL)
13537 {
13538 as_warn (_(".frame outside of .ent"));
13539 demand_empty_rest_of_line ();
13540 return;
13541 }
13542
13543 cur_proc_ptr->frame_reg = tc_get_register (1);
13544
13545 SKIP_WHITESPACE ();
13546 if (*input_line_pointer++ != ','
13547 || get_absolute_expression_and_terminator (&val) != ',')
13548 {
13549 as_warn (_("Bad .frame directive"));
13550 --input_line_pointer;
13551 demand_empty_rest_of_line ();
13552 return;
13553 }
13554
13555 cur_proc_ptr->frame_offset = val;
13556 cur_proc_ptr->pc_reg = tc_get_register (0);
13557
13558 demand_empty_rest_of_line ();
13559 }
13560 else
13561 #endif /* OBJ_ELF */
13562 s_ignore (ignore);
13563 }
13564
13565 /* The .fmask and .mask directives. If the mdebug section is present
13566 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13567 embedded targets, s_mips_mask is used so that we can set the PDR
13568 information correctly. We can't use the ecoff routines because they
13569 make reference to the ecoff symbol table (in the mdebug section). */
13570
13571 static void
13572 s_mips_mask (int reg_type)
13573 {
13574 #ifdef OBJ_ELF
13575 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13576 {
13577 long mask, off;
13578
13579 if (cur_proc_ptr == (procS *) NULL)
13580 {
13581 as_warn (_(".mask/.fmask outside of .ent"));
13582 demand_empty_rest_of_line ();
13583 return;
13584 }
13585
13586 if (get_absolute_expression_and_terminator (&mask) != ',')
13587 {
13588 as_warn (_("Bad .mask/.fmask directive"));
13589 --input_line_pointer;
13590 demand_empty_rest_of_line ();
13591 return;
13592 }
13593
13594 off = get_absolute_expression ();
13595
13596 if (reg_type == 'F')
13597 {
13598 cur_proc_ptr->fpreg_mask = mask;
13599 cur_proc_ptr->fpreg_offset = off;
13600 }
13601 else
13602 {
13603 cur_proc_ptr->reg_mask = mask;
13604 cur_proc_ptr->reg_offset = off;
13605 }
13606
13607 demand_empty_rest_of_line ();
13608 }
13609 else
13610 #endif /* OBJ_ELF */
13611 s_ignore (reg_type);
13612 }
13613
13614 /* A table describing all the processors gas knows about. Names are
13615 matched in the order listed.
13616
13617 To ease comparison, please keep this table in the same order as
13618 gcc's mips_cpu_info_table[]. */
13619 static const struct mips_cpu_info mips_cpu_info_table[] =
13620 {
13621 /* Entries for generic ISAs */
13622 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13623 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13624 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13625 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13626 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13627 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13628 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13629 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13630 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13631
13632 /* MIPS I */
13633 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13634 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13635 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13636
13637 /* MIPS II */
13638 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13639
13640 /* MIPS III */
13641 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13642 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13643 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13644 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13645 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13646 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13647 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13648 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13649 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13650 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13651 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13652 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13653
13654 /* MIPS IV */
13655 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13656 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13657 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13658 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13659 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13660 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13661 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13662 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13663 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13664 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13665 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13666 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13667 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13668
13669 /* MIPS 32 */
13670 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13671 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13672 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13673
13674 /* MIPS 64 */
13675 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13676 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13677
13678 /* Broadcom SB-1 CPU core */
13679 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13680
13681 /* End marker */
13682 { NULL, 0, 0, 0 }
13683 };
13684
13685
13686 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13687 with a final "000" replaced by "k". Ignore case.
13688
13689 Note: this function is shared between GCC and GAS. */
13690
13691 static bfd_boolean
13692 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13693 {
13694 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13695 given++, canonical++;
13696
13697 return ((*given == 0 && *canonical == 0)
13698 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13699 }
13700
13701
13702 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13703 CPU name. We've traditionally allowed a lot of variation here.
13704
13705 Note: this function is shared between GCC and GAS. */
13706
13707 static bfd_boolean
13708 mips_matching_cpu_name_p (const char *canonical, const char *given)
13709 {
13710 /* First see if the name matches exactly, or with a final "000"
13711 turned into "k". */
13712 if (mips_strict_matching_cpu_name_p (canonical, given))
13713 return TRUE;
13714
13715 /* If not, try comparing based on numerical designation alone.
13716 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13717 if (TOLOWER (*given) == 'r')
13718 given++;
13719 if (!ISDIGIT (*given))
13720 return FALSE;
13721
13722 /* Skip over some well-known prefixes in the canonical name,
13723 hoping to find a number there too. */
13724 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13725 canonical += 2;
13726 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13727 canonical += 2;
13728 else if (TOLOWER (canonical[0]) == 'r')
13729 canonical += 1;
13730
13731 return mips_strict_matching_cpu_name_p (canonical, given);
13732 }
13733
13734
13735 /* Parse an option that takes the name of a processor as its argument.
13736 OPTION is the name of the option and CPU_STRING is the argument.
13737 Return the corresponding processor enumeration if the CPU_STRING is
13738 recognized, otherwise report an error and return null.
13739
13740 A similar function exists in GCC. */
13741
13742 static const struct mips_cpu_info *
13743 mips_parse_cpu (const char *option, const char *cpu_string)
13744 {
13745 const struct mips_cpu_info *p;
13746
13747 /* 'from-abi' selects the most compatible architecture for the given
13748 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13749 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13750 version. Look first at the -mgp options, if given, otherwise base
13751 the choice on MIPS_DEFAULT_64BIT.
13752
13753 Treat NO_ABI like the EABIs. One reason to do this is that the
13754 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13755 architecture. This code picks MIPS I for 'mips' and MIPS III for
13756 'mips64', just as we did in the days before 'from-abi'. */
13757 if (strcasecmp (cpu_string, "from-abi") == 0)
13758 {
13759 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13760 return mips_cpu_info_from_isa (ISA_MIPS1);
13761
13762 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13763 return mips_cpu_info_from_isa (ISA_MIPS3);
13764
13765 if (file_mips_gp32 >= 0)
13766 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13767
13768 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13769 ? ISA_MIPS3
13770 : ISA_MIPS1);
13771 }
13772
13773 /* 'default' has traditionally been a no-op. Probably not very useful. */
13774 if (strcasecmp (cpu_string, "default") == 0)
13775 return 0;
13776
13777 for (p = mips_cpu_info_table; p->name != 0; p++)
13778 if (mips_matching_cpu_name_p (p->name, cpu_string))
13779 return p;
13780
13781 as_bad ("Bad value (%s) for %s", cpu_string, option);
13782 return 0;
13783 }
13784
13785 /* Return the canonical processor information for ISA (a member of the
13786 ISA_MIPS* enumeration). */
13787
13788 static const struct mips_cpu_info *
13789 mips_cpu_info_from_isa (int isa)
13790 {
13791 int i;
13792
13793 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13794 if (mips_cpu_info_table[i].is_isa
13795 && isa == mips_cpu_info_table[i].isa)
13796 return (&mips_cpu_info_table[i]);
13797
13798 return NULL;
13799 }
13800
13801 static const struct mips_cpu_info *
13802 mips_cpu_info_from_arch (int arch)
13803 {
13804 int i;
13805
13806 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13807 if (arch == mips_cpu_info_table[i].cpu)
13808 return (&mips_cpu_info_table[i]);
13809
13810 return NULL;
13811 }
13812 \f
13813 static void
13814 show (FILE *stream, const char *string, int *col_p, int *first_p)
13815 {
13816 if (*first_p)
13817 {
13818 fprintf (stream, "%24s", "");
13819 *col_p = 24;
13820 }
13821 else
13822 {
13823 fprintf (stream, ", ");
13824 *col_p += 2;
13825 }
13826
13827 if (*col_p + strlen (string) > 72)
13828 {
13829 fprintf (stream, "\n%24s", "");
13830 *col_p = 24;
13831 }
13832
13833 fprintf (stream, "%s", string);
13834 *col_p += strlen (string);
13835
13836 *first_p = 0;
13837 }
13838
13839 void
13840 md_show_usage (FILE *stream)
13841 {
13842 int column, first;
13843 size_t i;
13844
13845 fprintf (stream, _("\
13846 MIPS options:\n\
13847 -EB generate big endian output\n\
13848 -EL generate little endian output\n\
13849 -g, -g2 do not remove unneeded NOPs or swap branches\n\
13850 -G NUM allow referencing objects up to NUM bytes\n\
13851 implicitly with the gp register [default 8]\n"));
13852 fprintf (stream, _("\
13853 -mips1 generate MIPS ISA I instructions\n\
13854 -mips2 generate MIPS ISA II instructions\n\
13855 -mips3 generate MIPS ISA III instructions\n\
13856 -mips4 generate MIPS ISA IV instructions\n\
13857 -mips5 generate MIPS ISA V instructions\n\
13858 -mips32 generate MIPS32 ISA instructions\n\
13859 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
13860 -mips64 generate MIPS64 ISA instructions\n\
13861 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
13862 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13863
13864 first = 1;
13865
13866 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13867 show (stream, mips_cpu_info_table[i].name, &column, &first);
13868 show (stream, "from-abi", &column, &first);
13869 fputc ('\n', stream);
13870
13871 fprintf (stream, _("\
13872 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13873 -no-mCPU don't generate code specific to CPU.\n\
13874 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13875
13876 first = 1;
13877
13878 show (stream, "3900", &column, &first);
13879 show (stream, "4010", &column, &first);
13880 show (stream, "4100", &column, &first);
13881 show (stream, "4650", &column, &first);
13882 fputc ('\n', stream);
13883
13884 fprintf (stream, _("\
13885 -mips16 generate mips16 instructions\n\
13886 -no-mips16 do not generate mips16 instructions\n"));
13887 fprintf (stream, _("\
13888 -mfix-vr4120 work around certain VR4120 errata\n\
13889 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13890 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13891 -mno-shared optimize output for executables\n\
13892 -msym32 assume all symbols have 32-bit values\n\
13893 -O0 remove unneeded NOPs, do not swap branches\n\
13894 -O remove unneeded NOPs and swap branches\n\
13895 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
13896 --trap, --no-break trap exception on div by 0 and mult overflow\n\
13897 --break, --no-trap break exception on div by 0 and mult overflow\n"));
13898 #ifdef OBJ_ELF
13899 fprintf (stream, _("\
13900 -KPIC, -call_shared generate SVR4 position independent code\n\
13901 -non_shared do not generate position independent code\n\
13902 -xgot assume a 32 bit GOT\n\
13903 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
13904 -mshared, -mno-shared disable/enable .cpload optimization for\n\
13905 non-shared code\n\
13906 -mabi=ABI create ABI conformant object file for:\n"));
13907
13908 first = 1;
13909
13910 show (stream, "32", &column, &first);
13911 show (stream, "o64", &column, &first);
13912 show (stream, "n32", &column, &first);
13913 show (stream, "64", &column, &first);
13914 show (stream, "eabi", &column, &first);
13915
13916 fputc ('\n', stream);
13917
13918 fprintf (stream, _("\
13919 -32 create o32 ABI object file (default)\n\
13920 -n32 create n32 ABI object file\n\
13921 -64 create 64 ABI object file\n"));
13922 #endif
13923 }
13924
13925 enum dwarf2_format
13926 mips_dwarf2_format (void)
13927 {
13928 if (mips_abi == N64_ABI)
13929 {
13930 #ifdef TE_IRIX
13931 return dwarf2_format_64bit_irix;
13932 #else
13933 return dwarf2_format_64bit;
13934 #endif
13935 }
13936 else
13937 return dwarf2_format_32bit;
13938 }
13939
13940 int
13941 mips_dwarf2_addr_size (void)
13942 {
13943 if (mips_abi == N64_ABI)
13944 return 8;
13945 else
13946 return 4;
13947 }