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