* gas/elf/section5.e, gas/elf/section5.l: Replace [:digit:],
[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 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 /* The ABI to use. */
123 enum mips_abi_level
124 {
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131 };
132
133 /* MIPS ABI we are using for this output file. */
134 static enum mips_abi_level mips_abi = NO_ABI;
135
136 /* Whether or not we have code that can call pic code. */
137 int mips_abicalls = FALSE;
138
139 /* Whether or not we have code which can be put into a shared
140 library. */
141 static bfd_boolean mips_in_shared = TRUE;
142
143 /* This is the set of options which may be modified by the .set
144 pseudo-op. We use a struct so that .set push and .set pop are more
145 reliable. */
146
147 struct mips_set_options
148 {
149 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
150 if it has not been initialized. Changed by `.set mipsN', and the
151 -mipsN command line option, and the default CPU. */
152 int isa;
153 /* Enabled Application Specific Extensions (ASEs). These are set to -1
154 if they have not been initialized. Changed by `.set <asename>', by
155 command line options, and based on the default architecture. */
156 int ase_mips3d;
157 int ase_mdmx;
158 /* Whether we are assembling for the mips16 processor. 0 if we are
159 not, 1 if we are, and -1 if the value has not been initialized.
160 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
161 -nomips16 command line options, and the default CPU. */
162 int mips16;
163 /* Non-zero if we should not reorder instructions. Changed by `.set
164 reorder' and `.set noreorder'. */
165 int noreorder;
166 /* Non-zero if we should not permit the $at ($1) register to be used
167 in instructions. Changed by `.set at' and `.set noat'. */
168 int noat;
169 /* Non-zero if we should warn when a macro instruction expands into
170 more than one machine instruction. Changed by `.set nomacro' and
171 `.set macro'. */
172 int warn_about_macros;
173 /* Non-zero if we should not move instructions. Changed by `.set
174 move', `.set volatile', `.set nomove', and `.set novolatile'. */
175 int nomove;
176 /* Non-zero if we should not optimize branches by moving the target
177 of the branch into the delay slot. Actually, we don't perform
178 this optimization anyhow. Changed by `.set bopt' and `.set
179 nobopt'. */
180 int nobopt;
181 /* Non-zero if we should not autoextend mips16 instructions.
182 Changed by `.set autoextend' and `.set noautoextend'. */
183 int noautoextend;
184 /* Restrict general purpose registers and floating point registers
185 to 32 bit. This is initially determined when -mgp32 or -mfp32
186 is passed but can changed if the assembler code uses .set mipsN. */
187 int gp32;
188 int fp32;
189 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
190 command line option, and the default CPU. */
191 int arch;
192 };
193
194 /* True if -mgp32 was passed. */
195 static int file_mips_gp32 = -1;
196
197 /* True if -mfp32 was passed. */
198 static int file_mips_fp32 = -1;
199
200 /* This is the struct we use to hold the current set of options. Note
201 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
202 -1 to indicate that they have not been initialized. */
203
204 static struct mips_set_options mips_opts =
205 {
206 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
207 };
208
209 /* These variables are filled in with the masks of registers used.
210 The object format code reads them and puts them in the appropriate
211 place. */
212 unsigned long mips_gprmask;
213 unsigned long mips_cprmask[4];
214
215 /* MIPS ISA we are using for this output file. */
216 static int file_mips_isa = ISA_UNKNOWN;
217
218 /* True if -mips16 was passed or implied by arguments passed on the
219 command line (e.g., by -march). */
220 static int file_ase_mips16;
221
222 /* True if -mips3d was passed or implied by arguments passed on the
223 command line (e.g., by -march). */
224 static int file_ase_mips3d;
225
226 /* True if -mdmx was passed or implied by arguments passed on the
227 command line (e.g., by -march). */
228 static int file_ase_mdmx;
229
230 /* The argument of the -march= flag. The architecture we are assembling. */
231 static int file_mips_arch = CPU_UNKNOWN;
232 static const char *mips_arch_string;
233
234 /* The argument of the -mtune= flag. The architecture for which we
235 are optimizing. */
236 static int mips_tune = CPU_UNKNOWN;
237 static const char *mips_tune_string;
238
239 /* True when generating 32-bit code for a 64-bit processor. */
240 static int mips_32bitmode = 0;
241
242 /* True if the given ABI requires 32-bit registers. */
243 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
244
245 /* Likewise 64-bit registers. */
246 #define ABI_NEEDS_64BIT_REGS(ABI) \
247 ((ABI) == N32_ABI \
248 || (ABI) == N64_ABI \
249 || (ABI) == O64_ABI)
250
251 /* Return true if ISA supports 64 bit gp register instructions. */
252 #define ISA_HAS_64BIT_REGS(ISA) ( \
253 (ISA) == ISA_MIPS3 \
254 || (ISA) == ISA_MIPS4 \
255 || (ISA) == ISA_MIPS5 \
256 || (ISA) == ISA_MIPS64 \
257 || (ISA) == ISA_MIPS64R2 \
258 )
259
260 /* Return true if ISA supports 64-bit right rotate (dror et al.)
261 instructions. */
262 #define ISA_HAS_DROR(ISA) ( \
263 (ISA) == ISA_MIPS64R2 \
264 )
265
266 /* Return true if ISA supports 32-bit right rotate (ror et al.)
267 instructions. */
268 #define ISA_HAS_ROR(ISA) ( \
269 (ISA) == ISA_MIPS32R2 \
270 || (ISA) == ISA_MIPS64R2 \
271 )
272
273 #define HAVE_32BIT_GPRS \
274 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
275
276 #define HAVE_32BIT_FPRS \
277 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
278
279 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
280 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
281
282 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
283
284 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
285
286 /* True if relocations are stored in-place. */
287 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
288
289 /* We can only have 64bit addresses if the object file format supports it. */
290 #define HAVE_32BIT_ADDRESSES \
291 (HAVE_32BIT_GPRS \
292 || (bfd_arch_bits_per_address (stdoutput) == 32 \
293 || ! HAVE_64BIT_OBJECTS)) \
294
295 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
296
297 /* Addresses are loaded in different ways, depending on the address size
298 in use. The n32 ABI Documentation also mandates the use of additions
299 with overflow checking, but existing implementations don't follow it. */
300 #define ADDRESS_ADD_INSN \
301 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
302
303 #define ADDRESS_ADDI_INSN \
304 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
305
306 #define ADDRESS_LOAD_INSN \
307 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
308
309 #define ADDRESS_STORE_INSN \
310 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
311
312 /* Return true if the given CPU supports the MIPS16 ASE. */
313 #define CPU_HAS_MIPS16(cpu) \
314 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
315 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
316
317 /* Return true if the given CPU supports the MIPS3D ASE. */
318 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
319 )
320
321 /* Return true if the given CPU supports the MDMX ASE. */
322 #define CPU_HAS_MDMX(cpu) (FALSE \
323 )
324
325 /* True if CPU has a dror instruction. */
326 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
327
328 /* True if CPU has a ror instruction. */
329 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
330
331 /* True if mflo and mfhi can be immediately followed by instructions
332 which write to the HI and LO registers.
333
334 According to MIPS specifications, MIPS ISAs I, II, and III need
335 (at least) two instructions between the reads of HI/LO and
336 instructions which write them, and later ISAs do not. Contradicting
337 the MIPS specifications, some MIPS IV processor user manuals (e.g.
338 the UM for the NEC Vr5000) document needing the instructions between
339 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
340 MIPS64 and later ISAs to have the interlocks, plus any specific
341 earlier-ISA CPUs for which CPU documentation declares that the
342 instructions are really interlocked. */
343 #define hilo_interlocks \
344 (mips_opts.isa == ISA_MIPS32 \
345 || mips_opts.isa == ISA_MIPS32R2 \
346 || mips_opts.isa == ISA_MIPS64 \
347 || mips_opts.isa == ISA_MIPS64R2 \
348 || mips_opts.arch == CPU_R4010 \
349 || mips_opts.arch == CPU_R10000 \
350 || mips_opts.arch == CPU_R12000 \
351 || mips_opts.arch == CPU_RM7000 \
352 || mips_opts.arch == CPU_VR5500 \
353 )
354
355 /* Whether the processor uses hardware interlocks to protect reads
356 from the GPRs after they are loaded from memory, and thus does not
357 require nops to be inserted. This applies to instructions marked
358 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
359 level I. */
360 #define gpr_interlocks \
361 (mips_opts.isa != ISA_MIPS1 \
362 || mips_opts.arch == CPU_R3900)
363
364 /* Whether the processor uses hardware interlocks to avoid delays
365 required by coprocessor instructions, and thus does not require
366 nops to be inserted. This applies to instructions marked
367 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
368 between instructions marked INSN_WRITE_COND_CODE and ones marked
369 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
370 levels I, II, and III. */
371 /* Itbl support may require additional care here. */
372 #define cop_interlocks \
373 ((mips_opts.isa != ISA_MIPS1 \
374 && mips_opts.isa != ISA_MIPS2 \
375 && mips_opts.isa != ISA_MIPS3) \
376 || mips_opts.arch == CPU_R4300 \
377 )
378
379 /* Whether the processor uses hardware interlocks to protect reads
380 from coprocessor registers after they are loaded from memory, and
381 thus does not require nops to be inserted. This applies to
382 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
383 requires at MIPS ISA level I. */
384 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
385
386 /* Is this a mfhi or mflo instruction? */
387 #define MF_HILO_INSN(PINFO) \
388 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
389
390 /* MIPS PIC level. */
391
392 enum mips_pic_level mips_pic;
393
394 /* 1 if we should generate 32 bit offsets from the $gp register in
395 SVR4_PIC mode. Currently has no meaning in other modes. */
396 static int mips_big_got = 0;
397
398 /* 1 if trap instructions should used for overflow rather than break
399 instructions. */
400 static int mips_trap = 0;
401
402 /* 1 if double width floating point constants should not be constructed
403 by assembling two single width halves into two single width floating
404 point registers which just happen to alias the double width destination
405 register. On some architectures this aliasing can be disabled by a bit
406 in the status register, and the setting of this bit cannot be determined
407 automatically at assemble time. */
408 static int mips_disable_float_construction;
409
410 /* Non-zero if any .set noreorder directives were used. */
411
412 static int mips_any_noreorder;
413
414 /* Non-zero if nops should be inserted when the register referenced in
415 an mfhi/mflo instruction is read in the next two instructions. */
416 static int mips_7000_hilo_fix;
417
418 /* The size of the small data section. */
419 static unsigned int g_switch_value = 8;
420 /* Whether the -G option was used. */
421 static int g_switch_seen = 0;
422
423 #define N_RMASK 0xc4
424 #define N_VFP 0xd4
425
426 /* If we can determine in advance that GP optimization won't be
427 possible, we can skip the relaxation stuff that tries to produce
428 GP-relative references. This makes delay slot optimization work
429 better.
430
431 This function can only provide a guess, but it seems to work for
432 gcc output. It needs to guess right for gcc, otherwise gcc
433 will put what it thinks is a GP-relative instruction in a branch
434 delay slot.
435
436 I don't know if a fix is needed for the SVR4_PIC mode. I've only
437 fixed it for the non-PIC mode. KR 95/04/07 */
438 static int nopic_need_relax (symbolS *, int);
439
440 /* handle of the OPCODE hash table */
441 static struct hash_control *op_hash = NULL;
442
443 /* The opcode hash table we use for the mips16. */
444 static struct hash_control *mips16_op_hash = NULL;
445
446 /* This array holds the chars that always start a comment. If the
447 pre-processor is disabled, these aren't very useful */
448 const char comment_chars[] = "#";
449
450 /* This array holds the chars that only start a comment at the beginning of
451 a line. If the line seems to have the form '# 123 filename'
452 .line and .file directives will appear in the pre-processed output */
453 /* Note that input_file.c hand checks for '#' at the beginning of the
454 first line of the input file. This is because the compiler outputs
455 #NO_APP at the beginning of its output. */
456 /* Also note that C style comments are always supported. */
457 const char line_comment_chars[] = "#";
458
459 /* This array holds machine specific line separator characters. */
460 const char line_separator_chars[] = ";";
461
462 /* Chars that can be used to separate mant from exp in floating point nums */
463 const char EXP_CHARS[] = "eE";
464
465 /* Chars that mean this number is a floating point constant */
466 /* As in 0f12.456 */
467 /* or 0d1.2345e12 */
468 const char FLT_CHARS[] = "rRsSfFdDxXpP";
469
470 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
471 changed in read.c . Ideally it shouldn't have to know about it at all,
472 but nothing is ideal around here.
473 */
474
475 static char *insn_error;
476
477 static int auto_align = 1;
478
479 /* When outputting SVR4 PIC code, the assembler needs to know the
480 offset in the stack frame from which to restore the $gp register.
481 This is set by the .cprestore pseudo-op, and saved in this
482 variable. */
483 static offsetT mips_cprestore_offset = -1;
484
485 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
486 more optimizations, it can use a register value instead of a memory-saved
487 offset and even an other register than $gp as global pointer. */
488 static offsetT mips_cpreturn_offset = -1;
489 static int mips_cpreturn_register = -1;
490 static int mips_gp_register = GP;
491 static int mips_gprel_offset = 0;
492
493 /* Whether mips_cprestore_offset has been set in the current function
494 (or whether it has already been warned about, if not). */
495 static int mips_cprestore_valid = 0;
496
497 /* This is the register which holds the stack frame, as set by the
498 .frame pseudo-op. This is needed to implement .cprestore. */
499 static int mips_frame_reg = SP;
500
501 /* Whether mips_frame_reg has been set in the current function
502 (or whether it has already been warned about, if not). */
503 static int mips_frame_reg_valid = 0;
504
505 /* To output NOP instructions correctly, we need to keep information
506 about the previous two instructions. */
507
508 /* Whether we are optimizing. The default value of 2 means to remove
509 unneeded NOPs and swap branch instructions when possible. A value
510 of 1 means to not swap branches. A value of 0 means to always
511 insert NOPs. */
512 static int mips_optimize = 2;
513
514 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
515 equivalent to seeing no -g option at all. */
516 static int mips_debug = 0;
517
518 /* The previous instruction. */
519 static struct mips_cl_insn prev_insn;
520
521 /* The instruction before prev_insn. */
522 static struct mips_cl_insn prev_prev_insn;
523
524 /* If we don't want information for prev_insn or prev_prev_insn, we
525 point the insn_mo field at this dummy integer. */
526 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
527
528 /* Non-zero if prev_insn is valid. */
529 static int prev_insn_valid;
530
531 /* The frag for the previous instruction. */
532 static struct frag *prev_insn_frag;
533
534 /* The offset into prev_insn_frag for the previous instruction. */
535 static long prev_insn_where;
536
537 /* The reloc type for the previous instruction, if any. */
538 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
539
540 /* The reloc for the previous instruction, if any. */
541 static fixS *prev_insn_fixp[3];
542
543 /* Non-zero if the previous instruction was in a delay slot. */
544 static int prev_insn_is_delay_slot;
545
546 /* Non-zero if the previous instruction was in a .set noreorder. */
547 static int prev_insn_unreordered;
548
549 /* Non-zero if the previous instruction uses an extend opcode (if
550 mips16). */
551 static int prev_insn_extended;
552
553 /* Non-zero if the previous previous instruction was in a .set
554 noreorder. */
555 static int prev_prev_insn_unreordered;
556
557 /* If this is set, it points to a frag holding nop instructions which
558 were inserted before the start of a noreorder section. If those
559 nops turn out to be unnecessary, the size of the frag can be
560 decreased. */
561 static fragS *prev_nop_frag;
562
563 /* The number of nop instructions we created in prev_nop_frag. */
564 static int prev_nop_frag_holds;
565
566 /* The number of nop instructions that we know we need in
567 prev_nop_frag. */
568 static int prev_nop_frag_required;
569
570 /* The number of instructions we've seen since prev_nop_frag. */
571 static int prev_nop_frag_since;
572
573 /* For ECOFF and ELF, relocations against symbols are done in two
574 parts, with a HI relocation and a LO relocation. Each relocation
575 has only 16 bits of space to store an addend. This means that in
576 order for the linker to handle carries correctly, it must be able
577 to locate both the HI and the LO relocation. This means that the
578 relocations must appear in order in the relocation table.
579
580 In order to implement this, we keep track of each unmatched HI
581 relocation. We then sort them so that they immediately precede the
582 corresponding LO relocation. */
583
584 struct mips_hi_fixup
585 {
586 /* Next HI fixup. */
587 struct mips_hi_fixup *next;
588 /* This fixup. */
589 fixS *fixp;
590 /* The section this fixup is in. */
591 segT seg;
592 };
593
594 /* The list of unmatched HI relocs. */
595
596 static struct mips_hi_fixup *mips_hi_fixup_list;
597
598 /* The frag containing the last explicit relocation operator.
599 Null if explicit relocations have not been used. */
600
601 static fragS *prev_reloc_op_frag;
602
603 /* Map normal MIPS register numbers to mips16 register numbers. */
604
605 #define X ILLEGAL_REG
606 static const int mips32_to_16_reg_map[] =
607 {
608 X, X, 2, 3, 4, 5, 6, 7,
609 X, X, X, X, X, X, X, X,
610 0, 1, X, X, X, X, X, X,
611 X, X, X, X, X, X, X, X
612 };
613 #undef X
614
615 /* Map mips16 register numbers to normal MIPS register numbers. */
616
617 static const unsigned int mips16_to_32_reg_map[] =
618 {
619 16, 17, 2, 3, 4, 5, 6, 7
620 };
621
622 static int mips_fix_vr4120;
623
624 /* We don't relax branches by default, since this causes us to expand
625 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
626 fail to compute the offset before expanding the macro to the most
627 efficient expansion. */
628
629 static int mips_relax_branch;
630 \f
631 /* The expansion of many macros depends on the type of symbol that
632 they refer to. For example, when generating position-dependent code,
633 a macro that refers to a symbol may have two different expansions,
634 one which uses GP-relative addresses and one which uses absolute
635 addresses. When generating SVR4-style PIC, a macro may have
636 different expansions for local and global symbols.
637
638 We handle these situations by generating both sequences and putting
639 them in variant frags. In position-dependent code, the first sequence
640 will be the GP-relative one and the second sequence will be the
641 absolute one. In SVR4 PIC, the first sequence will be for global
642 symbols and the second will be for local symbols.
643
644 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
645 SECOND are the lengths of the two sequences in bytes. These fields
646 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
647 the subtype has the following flags:
648
649 RELAX_USE_SECOND
650 Set if it has been decided that we should use the second
651 sequence instead of the first.
652
653 RELAX_SECOND_LONGER
654 Set in the first variant frag if the macro's second implementation
655 is longer than its first. This refers to the macro as a whole,
656 not an individual relaxation.
657
658 RELAX_NOMACRO
659 Set in the first variant frag if the macro appeared in a .set nomacro
660 block and if one alternative requires a warning but the other does not.
661
662 RELAX_DELAY_SLOT
663 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
664 delay slot.
665
666 The frag's "opcode" points to the first fixup for relaxable code.
667
668 Relaxable macros are generated using a sequence such as:
669
670 relax_start (SYMBOL);
671 ... generate first expansion ...
672 relax_switch ();
673 ... generate second expansion ...
674 relax_end ();
675
676 The code and fixups for the unwanted alternative are discarded
677 by md_convert_frag. */
678 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
679
680 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
681 #define RELAX_SECOND(X) ((X) & 0xff)
682 #define RELAX_USE_SECOND 0x10000
683 #define RELAX_SECOND_LONGER 0x20000
684 #define RELAX_NOMACRO 0x40000
685 #define RELAX_DELAY_SLOT 0x80000
686
687 /* Branch without likely bit. If label is out of range, we turn:
688
689 beq reg1, reg2, label
690 delay slot
691
692 into
693
694 bne reg1, reg2, 0f
695 nop
696 j label
697 0: delay slot
698
699 with the following opcode replacements:
700
701 beq <-> bne
702 blez <-> bgtz
703 bltz <-> bgez
704 bc1f <-> bc1t
705
706 bltzal <-> bgezal (with jal label instead of j label)
707
708 Even though keeping the delay slot instruction in the delay slot of
709 the branch would be more efficient, it would be very tricky to do
710 correctly, because we'd have to introduce a variable frag *after*
711 the delay slot instruction, and expand that instead. Let's do it
712 the easy way for now, even if the branch-not-taken case now costs
713 one additional instruction. Out-of-range branches are not supposed
714 to be common, anyway.
715
716 Branch likely. If label is out of range, we turn:
717
718 beql reg1, reg2, label
719 delay slot (annulled if branch not taken)
720
721 into
722
723 beql reg1, reg2, 1f
724 nop
725 beql $0, $0, 2f
726 nop
727 1: j[al] label
728 delay slot (executed only if branch taken)
729 2:
730
731 It would be possible to generate a shorter sequence by losing the
732 likely bit, generating something like:
733
734 bne reg1, reg2, 0f
735 nop
736 j[al] label
737 delay slot (executed only if branch taken)
738 0:
739
740 beql -> bne
741 bnel -> beq
742 blezl -> bgtz
743 bgtzl -> blez
744 bltzl -> bgez
745 bgezl -> bltz
746 bc1fl -> bc1t
747 bc1tl -> bc1f
748
749 bltzall -> bgezal (with jal label instead of j label)
750 bgezall -> bltzal (ditto)
751
752
753 but it's not clear that it would actually improve performance. */
754 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
755 ((relax_substateT) \
756 (0xc0000000 \
757 | ((toofar) ? 1 : 0) \
758 | ((link) ? 2 : 0) \
759 | ((likely) ? 4 : 0) \
760 | ((uncond) ? 8 : 0)))
761 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
762 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
763 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
764 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
765 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
766
767 /* For mips16 code, we use an entirely different form of relaxation.
768 mips16 supports two versions of most instructions which take
769 immediate values: a small one which takes some small value, and a
770 larger one which takes a 16 bit value. Since branches also follow
771 this pattern, relaxing these values is required.
772
773 We can assemble both mips16 and normal MIPS code in a single
774 object. Therefore, we need to support this type of relaxation at
775 the same time that we support the relaxation described above. We
776 use the high bit of the subtype field to distinguish these cases.
777
778 The information we store for this type of relaxation is the
779 argument code found in the opcode file for this relocation, whether
780 the user explicitly requested a small or extended form, and whether
781 the relocation is in a jump or jal delay slot. That tells us the
782 size of the value, and how it should be stored. We also store
783 whether the fragment is considered to be extended or not. We also
784 store whether this is known to be a branch to a different section,
785 whether we have tried to relax this frag yet, and whether we have
786 ever extended a PC relative fragment because of a shift count. */
787 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
788 (0x80000000 \
789 | ((type) & 0xff) \
790 | ((small) ? 0x100 : 0) \
791 | ((ext) ? 0x200 : 0) \
792 | ((dslot) ? 0x400 : 0) \
793 | ((jal_dslot) ? 0x800 : 0))
794 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
795 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
796 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
797 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
798 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
799 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
800 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
801 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
802 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
803 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
804 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
805 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
806
807 /* Is the given value a sign-extended 32-bit value? */
808 #define IS_SEXT_32BIT_NUM(x) \
809 (((x) &~ (offsetT) 0x7fffffff) == 0 \
810 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
811
812 /* Is the given value a sign-extended 16-bit value? */
813 #define IS_SEXT_16BIT_NUM(x) \
814 (((x) &~ (offsetT) 0x7fff) == 0 \
815 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
816
817 \f
818 /* Global variables used when generating relaxable macros. See the
819 comment above RELAX_ENCODE for more details about how relaxation
820 is used. */
821 static struct {
822 /* 0 if we're not emitting a relaxable macro.
823 1 if we're emitting the first of the two relaxation alternatives.
824 2 if we're emitting the second alternative. */
825 int sequence;
826
827 /* The first relaxable fixup in the current frag. (In other words,
828 the first fixup that refers to relaxable code.) */
829 fixS *first_fixup;
830
831 /* sizes[0] says how many bytes of the first alternative are stored in
832 the current frag. Likewise sizes[1] for the second alternative. */
833 unsigned int sizes[2];
834
835 /* The symbol on which the choice of sequence depends. */
836 symbolS *symbol;
837 } mips_relax;
838 \f
839 /* Global variables used to decide whether a macro needs a warning. */
840 static struct {
841 /* True if the macro is in a branch delay slot. */
842 bfd_boolean delay_slot_p;
843
844 /* For relaxable macros, sizes[0] is the length of the first alternative
845 in bytes and sizes[1] is the length of the second alternative.
846 For non-relaxable macros, both elements give the length of the
847 macro in bytes. */
848 unsigned int sizes[2];
849
850 /* The first variant frag for this macro. */
851 fragS *first_frag;
852 } mips_macro_warning;
853 \f
854 /* Prototypes for static functions. */
855
856 #define internalError() \
857 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
858
859 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
860
861 static void append_insn
862 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
863 static void mips_no_prev_insn (int);
864 static void mips16_macro_build
865 (expressionS *, const char *, const char *, va_list);
866 static void load_register (int, expressionS *, int);
867 static void macro_start (void);
868 static void macro_end (void);
869 static void macro (struct mips_cl_insn * ip);
870 static void mips16_macro (struct mips_cl_insn * ip);
871 #ifdef LOSING_COMPILER
872 static void macro2 (struct mips_cl_insn * ip);
873 #endif
874 static void mips_ip (char *str, struct mips_cl_insn * ip);
875 static void mips16_ip (char *str, struct mips_cl_insn * ip);
876 static void mips16_immed
877 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
878 unsigned long *, bfd_boolean *, unsigned short *);
879 static size_t my_getSmallExpression
880 (expressionS *, bfd_reloc_code_real_type *, char *);
881 static void my_getExpression (expressionS *, char *);
882 static void s_align (int);
883 static void s_change_sec (int);
884 static void s_change_section (int);
885 static void s_cons (int);
886 static void s_float_cons (int);
887 static void s_mips_globl (int);
888 static void s_option (int);
889 static void s_mipsset (int);
890 static void s_abicalls (int);
891 static void s_cpload (int);
892 static void s_cpsetup (int);
893 static void s_cplocal (int);
894 static void s_cprestore (int);
895 static void s_cpreturn (int);
896 static void s_gpvalue (int);
897 static void s_gpword (int);
898 static void s_gpdword (int);
899 static void s_cpadd (int);
900 static void s_insn (int);
901 static void md_obj_begin (void);
902 static void md_obj_end (void);
903 static void s_mips_ent (int);
904 static void s_mips_end (int);
905 static void s_mips_frame (int);
906 static void s_mips_mask (int reg_type);
907 static void s_mips_stab (int);
908 static void s_mips_weakext (int);
909 static void s_mips_file (int);
910 static void s_mips_loc (int);
911 static bfd_boolean pic_need_relax (symbolS *, asection *);
912 static int relaxed_branch_length (fragS *, asection *, int);
913 static int validate_mips_insn (const struct mips_opcode *);
914
915 /* Table and functions used to map between CPU/ISA names, and
916 ISA levels, and CPU numbers. */
917
918 struct mips_cpu_info
919 {
920 const char *name; /* CPU or ISA name. */
921 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
922 int isa; /* ISA level. */
923 int cpu; /* CPU number (default CPU if ISA). */
924 };
925
926 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
927 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
928 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
929 \f
930 /* Pseudo-op table.
931
932 The following pseudo-ops from the Kane and Heinrich MIPS book
933 should be defined here, but are currently unsupported: .alias,
934 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
935
936 The following pseudo-ops from the Kane and Heinrich MIPS book are
937 specific to the type of debugging information being generated, and
938 should be defined by the object format: .aent, .begin, .bend,
939 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
940 .vreg.
941
942 The following pseudo-ops from the Kane and Heinrich MIPS book are
943 not MIPS CPU specific, but are also not specific to the object file
944 format. This file is probably the best place to define them, but
945 they are not currently supported: .asm0, .endr, .lab, .repeat,
946 .struct. */
947
948 static const pseudo_typeS mips_pseudo_table[] =
949 {
950 /* MIPS specific pseudo-ops. */
951 {"option", s_option, 0},
952 {"set", s_mipsset, 0},
953 {"rdata", s_change_sec, 'r'},
954 {"sdata", s_change_sec, 's'},
955 {"livereg", s_ignore, 0},
956 {"abicalls", s_abicalls, 0},
957 {"cpload", s_cpload, 0},
958 {"cpsetup", s_cpsetup, 0},
959 {"cplocal", s_cplocal, 0},
960 {"cprestore", s_cprestore, 0},
961 {"cpreturn", s_cpreturn, 0},
962 {"gpvalue", s_gpvalue, 0},
963 {"gpword", s_gpword, 0},
964 {"gpdword", s_gpdword, 0},
965 {"cpadd", s_cpadd, 0},
966 {"insn", s_insn, 0},
967
968 /* Relatively generic pseudo-ops that happen to be used on MIPS
969 chips. */
970 {"asciiz", stringer, 1},
971 {"bss", s_change_sec, 'b'},
972 {"err", s_err, 0},
973 {"half", s_cons, 1},
974 {"dword", s_cons, 3},
975 {"weakext", s_mips_weakext, 0},
976
977 /* These pseudo-ops are defined in read.c, but must be overridden
978 here for one reason or another. */
979 {"align", s_align, 0},
980 {"byte", s_cons, 0},
981 {"data", s_change_sec, 'd'},
982 {"double", s_float_cons, 'd'},
983 {"float", s_float_cons, 'f'},
984 {"globl", s_mips_globl, 0},
985 {"global", s_mips_globl, 0},
986 {"hword", s_cons, 1},
987 {"int", s_cons, 2},
988 {"long", s_cons, 2},
989 {"octa", s_cons, 4},
990 {"quad", s_cons, 3},
991 {"section", s_change_section, 0},
992 {"short", s_cons, 1},
993 {"single", s_float_cons, 'f'},
994 {"stabn", s_mips_stab, 'n'},
995 {"text", s_change_sec, 't'},
996 {"word", s_cons, 2},
997
998 { "extern", ecoff_directive_extern, 0},
999
1000 { NULL, NULL, 0 },
1001 };
1002
1003 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1004 {
1005 /* These pseudo-ops should be defined by the object file format.
1006 However, a.out doesn't support them, so we have versions here. */
1007 {"aent", s_mips_ent, 1},
1008 {"bgnb", s_ignore, 0},
1009 {"end", s_mips_end, 0},
1010 {"endb", s_ignore, 0},
1011 {"ent", s_mips_ent, 0},
1012 {"file", s_mips_file, 0},
1013 {"fmask", s_mips_mask, 'F'},
1014 {"frame", s_mips_frame, 0},
1015 {"loc", s_mips_loc, 0},
1016 {"mask", s_mips_mask, 'R'},
1017 {"verstamp", s_ignore, 0},
1018 { NULL, NULL, 0 },
1019 };
1020
1021 extern void pop_insert (const pseudo_typeS *);
1022
1023 void
1024 mips_pop_insert (void)
1025 {
1026 pop_insert (mips_pseudo_table);
1027 if (! ECOFF_DEBUGGING)
1028 pop_insert (mips_nonecoff_pseudo_table);
1029 }
1030 \f
1031 /* Symbols labelling the current insn. */
1032
1033 struct insn_label_list
1034 {
1035 struct insn_label_list *next;
1036 symbolS *label;
1037 };
1038
1039 static struct insn_label_list *insn_labels;
1040 static struct insn_label_list *free_insn_labels;
1041
1042 static void mips_clear_insn_labels (void);
1043
1044 static inline void
1045 mips_clear_insn_labels (void)
1046 {
1047 register struct insn_label_list **pl;
1048
1049 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1050 ;
1051 *pl = insn_labels;
1052 insn_labels = NULL;
1053 }
1054 \f
1055 static char *expr_end;
1056
1057 /* Expressions which appear in instructions. These are set by
1058 mips_ip. */
1059
1060 static expressionS imm_expr;
1061 static expressionS imm2_expr;
1062 static expressionS offset_expr;
1063
1064 /* Relocs associated with imm_expr and offset_expr. */
1065
1066 static bfd_reloc_code_real_type imm_reloc[3]
1067 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068 static bfd_reloc_code_real_type offset_reloc[3]
1069 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1070
1071 /* These are set by mips16_ip if an explicit extension is used. */
1072
1073 static bfd_boolean mips16_small, mips16_ext;
1074
1075 #ifdef OBJ_ELF
1076 /* The pdr segment for per procedure frame/regmask info. Not used for
1077 ECOFF debugging. */
1078
1079 static segT pdr_seg;
1080 #endif
1081
1082 /* The default target format to use. */
1083
1084 const char *
1085 mips_target_format (void)
1086 {
1087 switch (OUTPUT_FLAVOR)
1088 {
1089 case bfd_target_ecoff_flavour:
1090 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1091 case bfd_target_coff_flavour:
1092 return "pe-mips";
1093 case bfd_target_elf_flavour:
1094 #ifdef TE_TMIPS
1095 /* This is traditional mips. */
1096 return (target_big_endian
1097 ? (HAVE_64BIT_OBJECTS
1098 ? "elf64-tradbigmips"
1099 : (HAVE_NEWABI
1100 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1101 : (HAVE_64BIT_OBJECTS
1102 ? "elf64-tradlittlemips"
1103 : (HAVE_NEWABI
1104 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1105 #else
1106 return (target_big_endian
1107 ? (HAVE_64BIT_OBJECTS
1108 ? "elf64-bigmips"
1109 : (HAVE_NEWABI
1110 ? "elf32-nbigmips" : "elf32-bigmips"))
1111 : (HAVE_64BIT_OBJECTS
1112 ? "elf64-littlemips"
1113 : (HAVE_NEWABI
1114 ? "elf32-nlittlemips" : "elf32-littlemips")));
1115 #endif
1116 default:
1117 abort ();
1118 return NULL;
1119 }
1120 }
1121
1122 /* This function is called once, at assembler startup time. It should
1123 set up all the tables, etc. that the MD part of the assembler will need. */
1124
1125 void
1126 md_begin (void)
1127 {
1128 register const char *retval = NULL;
1129 int i = 0;
1130 int broken = 0;
1131
1132 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1133 as_warn (_("Could not set architecture and machine"));
1134
1135 op_hash = hash_new ();
1136
1137 for (i = 0; i < NUMOPCODES;)
1138 {
1139 const char *name = mips_opcodes[i].name;
1140
1141 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1142 if (retval != NULL)
1143 {
1144 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1145 mips_opcodes[i].name, retval);
1146 /* Probably a memory allocation problem? Give up now. */
1147 as_fatal (_("Broken assembler. No assembly attempted."));
1148 }
1149 do
1150 {
1151 if (mips_opcodes[i].pinfo != INSN_MACRO)
1152 {
1153 if (!validate_mips_insn (&mips_opcodes[i]))
1154 broken = 1;
1155 }
1156 ++i;
1157 }
1158 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1159 }
1160
1161 mips16_op_hash = hash_new ();
1162
1163 i = 0;
1164 while (i < bfd_mips16_num_opcodes)
1165 {
1166 const char *name = mips16_opcodes[i].name;
1167
1168 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1169 if (retval != NULL)
1170 as_fatal (_("internal: can't hash `%s': %s"),
1171 mips16_opcodes[i].name, retval);
1172 do
1173 {
1174 if (mips16_opcodes[i].pinfo != INSN_MACRO
1175 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1176 != mips16_opcodes[i].match))
1177 {
1178 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1179 mips16_opcodes[i].name, mips16_opcodes[i].args);
1180 broken = 1;
1181 }
1182 ++i;
1183 }
1184 while (i < bfd_mips16_num_opcodes
1185 && strcmp (mips16_opcodes[i].name, name) == 0);
1186 }
1187
1188 if (broken)
1189 as_fatal (_("Broken assembler. No assembly attempted."));
1190
1191 /* We add all the general register names to the symbol table. This
1192 helps us detect invalid uses of them. */
1193 for (i = 0; i < 32; i++)
1194 {
1195 char buf[5];
1196
1197 sprintf (buf, "$%d", i);
1198 symbol_table_insert (symbol_new (buf, reg_section, i,
1199 &zero_address_frag));
1200 }
1201 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1202 &zero_address_frag));
1203 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1204 &zero_address_frag));
1205 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1206 &zero_address_frag));
1207 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1208 &zero_address_frag));
1209 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1210 &zero_address_frag));
1211 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1212 &zero_address_frag));
1213 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1214 &zero_address_frag));
1215 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1216 &zero_address_frag));
1217 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1218 &zero_address_frag));
1219
1220 /* If we don't add these register names to the symbol table, they
1221 may end up being added as regular symbols by operand(), and then
1222 make it to the object file as undefined in case they're not
1223 regarded as local symbols. They're local in o32, since `$' is a
1224 local symbol prefix, but not in n32 or n64. */
1225 for (i = 0; i < 8; i++)
1226 {
1227 char buf[6];
1228
1229 sprintf (buf, "$fcc%i", i);
1230 symbol_table_insert (symbol_new (buf, reg_section, -1,
1231 &zero_address_frag));
1232 }
1233
1234 mips_no_prev_insn (FALSE);
1235
1236 mips_gprmask = 0;
1237 mips_cprmask[0] = 0;
1238 mips_cprmask[1] = 0;
1239 mips_cprmask[2] = 0;
1240 mips_cprmask[3] = 0;
1241
1242 /* set the default alignment for the text section (2**2) */
1243 record_alignment (text_section, 2);
1244
1245 bfd_set_gp_size (stdoutput, g_switch_value);
1246
1247 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1248 {
1249 /* On a native system, sections must be aligned to 16 byte
1250 boundaries. When configured for an embedded ELF target, we
1251 don't bother. */
1252 if (strcmp (TARGET_OS, "elf") != 0)
1253 {
1254 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1255 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1256 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1257 }
1258
1259 /* Create a .reginfo section for register masks and a .mdebug
1260 section for debugging information. */
1261 {
1262 segT seg;
1263 subsegT subseg;
1264 flagword flags;
1265 segT sec;
1266
1267 seg = now_seg;
1268 subseg = now_subseg;
1269
1270 /* The ABI says this section should be loaded so that the
1271 running program can access it. However, we don't load it
1272 if we are configured for an embedded target */
1273 flags = SEC_READONLY | SEC_DATA;
1274 if (strcmp (TARGET_OS, "elf") != 0)
1275 flags |= SEC_ALLOC | SEC_LOAD;
1276
1277 if (mips_abi != N64_ABI)
1278 {
1279 sec = subseg_new (".reginfo", (subsegT) 0);
1280
1281 bfd_set_section_flags (stdoutput, sec, flags);
1282 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1283
1284 #ifdef OBJ_ELF
1285 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1286 #endif
1287 }
1288 else
1289 {
1290 /* The 64-bit ABI uses a .MIPS.options section rather than
1291 .reginfo section. */
1292 sec = subseg_new (".MIPS.options", (subsegT) 0);
1293 bfd_set_section_flags (stdoutput, sec, flags);
1294 bfd_set_section_alignment (stdoutput, sec, 3);
1295
1296 #ifdef OBJ_ELF
1297 /* Set up the option header. */
1298 {
1299 Elf_Internal_Options opthdr;
1300 char *f;
1301
1302 opthdr.kind = ODK_REGINFO;
1303 opthdr.size = (sizeof (Elf_External_Options)
1304 + sizeof (Elf64_External_RegInfo));
1305 opthdr.section = 0;
1306 opthdr.info = 0;
1307 f = frag_more (sizeof (Elf_External_Options));
1308 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1309 (Elf_External_Options *) f);
1310
1311 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1312 }
1313 #endif
1314 }
1315
1316 if (ECOFF_DEBUGGING)
1317 {
1318 sec = subseg_new (".mdebug", (subsegT) 0);
1319 (void) bfd_set_section_flags (stdoutput, sec,
1320 SEC_HAS_CONTENTS | SEC_READONLY);
1321 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1322 }
1323 #ifdef OBJ_ELF
1324 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1325 {
1326 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1327 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1328 SEC_READONLY | SEC_RELOC
1329 | SEC_DEBUGGING);
1330 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1331 }
1332 #endif
1333
1334 subseg_set (seg, subseg);
1335 }
1336 }
1337
1338 if (! ECOFF_DEBUGGING)
1339 md_obj_begin ();
1340 }
1341
1342 void
1343 md_mips_end (void)
1344 {
1345 if (! ECOFF_DEBUGGING)
1346 md_obj_end ();
1347 }
1348
1349 void
1350 md_assemble (char *str)
1351 {
1352 struct mips_cl_insn insn;
1353 bfd_reloc_code_real_type unused_reloc[3]
1354 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1355
1356 imm_expr.X_op = O_absent;
1357 imm2_expr.X_op = O_absent;
1358 offset_expr.X_op = O_absent;
1359 imm_reloc[0] = BFD_RELOC_UNUSED;
1360 imm_reloc[1] = BFD_RELOC_UNUSED;
1361 imm_reloc[2] = BFD_RELOC_UNUSED;
1362 offset_reloc[0] = BFD_RELOC_UNUSED;
1363 offset_reloc[1] = BFD_RELOC_UNUSED;
1364 offset_reloc[2] = BFD_RELOC_UNUSED;
1365
1366 if (mips_opts.mips16)
1367 mips16_ip (str, &insn);
1368 else
1369 {
1370 mips_ip (str, &insn);
1371 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1372 str, insn.insn_opcode));
1373 }
1374
1375 if (insn_error)
1376 {
1377 as_bad ("%s `%s'", insn_error, str);
1378 return;
1379 }
1380
1381 if (insn.insn_mo->pinfo == INSN_MACRO)
1382 {
1383 macro_start ();
1384 if (mips_opts.mips16)
1385 mips16_macro (&insn);
1386 else
1387 macro (&insn);
1388 macro_end ();
1389 }
1390 else
1391 {
1392 if (imm_expr.X_op != O_absent)
1393 append_insn (&insn, &imm_expr, imm_reloc);
1394 else if (offset_expr.X_op != O_absent)
1395 append_insn (&insn, &offset_expr, offset_reloc);
1396 else
1397 append_insn (&insn, NULL, unused_reloc);
1398 }
1399 }
1400
1401 /* Return true if the given relocation might need a matching %lo().
1402 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1403 applied to local symbols. */
1404
1405 static inline bfd_boolean
1406 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1407 {
1408 return (HAVE_IN_PLACE_ADDENDS
1409 && (reloc == BFD_RELOC_HI16_S
1410 || reloc == BFD_RELOC_MIPS_GOT16));
1411 }
1412
1413 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1414 relocation. */
1415
1416 static inline bfd_boolean
1417 fixup_has_matching_lo_p (fixS *fixp)
1418 {
1419 return (fixp->fx_next != NULL
1420 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1421 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1422 && fixp->fx_offset == fixp->fx_next->fx_offset);
1423 }
1424
1425 /* See whether instruction IP reads register REG. CLASS is the type
1426 of register. */
1427
1428 static int
1429 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1430 enum mips_regclass class)
1431 {
1432 if (class == MIPS16_REG)
1433 {
1434 assert (mips_opts.mips16);
1435 reg = mips16_to_32_reg_map[reg];
1436 class = MIPS_GR_REG;
1437 }
1438
1439 /* Don't report on general register ZERO, since it never changes. */
1440 if (class == MIPS_GR_REG && reg == ZERO)
1441 return 0;
1442
1443 if (class == MIPS_FP_REG)
1444 {
1445 assert (! mips_opts.mips16);
1446 /* If we are called with either $f0 or $f1, we must check $f0.
1447 This is not optimal, because it will introduce an unnecessary
1448 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1449 need to distinguish reading both $f0 and $f1 or just one of
1450 them. Note that we don't have to check the other way,
1451 because there is no instruction that sets both $f0 and $f1
1452 and requires a delay. */
1453 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1454 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1455 == (reg &~ (unsigned) 1)))
1456 return 1;
1457 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1458 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1459 == (reg &~ (unsigned) 1)))
1460 return 1;
1461 }
1462 else if (! mips_opts.mips16)
1463 {
1464 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1465 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1466 return 1;
1467 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1468 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1469 return 1;
1470 }
1471 else
1472 {
1473 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1474 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1475 & MIPS16OP_MASK_RX)]
1476 == reg))
1477 return 1;
1478 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1479 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1480 & MIPS16OP_MASK_RY)]
1481 == reg))
1482 return 1;
1483 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1484 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1485 & MIPS16OP_MASK_MOVE32Z)]
1486 == reg))
1487 return 1;
1488 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1489 return 1;
1490 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1491 return 1;
1492 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1493 return 1;
1494 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1495 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1496 & MIPS16OP_MASK_REGR32) == reg)
1497 return 1;
1498 }
1499
1500 return 0;
1501 }
1502
1503 /* This function returns true if modifying a register requires a
1504 delay. */
1505
1506 static int
1507 reg_needs_delay (unsigned int reg)
1508 {
1509 unsigned long prev_pinfo;
1510
1511 prev_pinfo = prev_insn.insn_mo->pinfo;
1512 if (! mips_opts.noreorder
1513 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1514 && ! gpr_interlocks)
1515 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1516 && ! cop_interlocks)))
1517 {
1518 /* A load from a coprocessor or from memory. All load delays
1519 delay the use of general register rt for one instruction. */
1520 /* Itbl support may require additional care here. */
1521 know (prev_pinfo & INSN_WRITE_GPR_T);
1522 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1523 return 1;
1524 }
1525
1526 return 0;
1527 }
1528
1529 /* Mark instruction labels in mips16 mode. This permits the linker to
1530 handle them specially, such as generating jalx instructions when
1531 needed. We also make them odd for the duration of the assembly, in
1532 order to generate the right sort of code. We will make them even
1533 in the adjust_symtab routine, while leaving them marked. This is
1534 convenient for the debugger and the disassembler. The linker knows
1535 to make them odd again. */
1536
1537 static void
1538 mips16_mark_labels (void)
1539 {
1540 if (mips_opts.mips16)
1541 {
1542 struct insn_label_list *l;
1543 valueT val;
1544
1545 for (l = insn_labels; l != NULL; l = l->next)
1546 {
1547 #ifdef OBJ_ELF
1548 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1549 S_SET_OTHER (l->label, STO_MIPS16);
1550 #endif
1551 val = S_GET_VALUE (l->label);
1552 if ((val & 1) == 0)
1553 S_SET_VALUE (l->label, val + 1);
1554 }
1555 }
1556 }
1557
1558 /* End the current frag. Make it a variant frag and record the
1559 relaxation info. */
1560
1561 static void
1562 relax_close_frag (void)
1563 {
1564 mips_macro_warning.first_frag = frag_now;
1565 frag_var (rs_machine_dependent, 0, 0,
1566 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1567 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1568
1569 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1570 mips_relax.first_fixup = 0;
1571 }
1572
1573 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1574 See the comment above RELAX_ENCODE for more details. */
1575
1576 static void
1577 relax_start (symbolS *symbol)
1578 {
1579 assert (mips_relax.sequence == 0);
1580 mips_relax.sequence = 1;
1581 mips_relax.symbol = symbol;
1582 }
1583
1584 /* Start generating the second version of a relaxable sequence.
1585 See the comment above RELAX_ENCODE for more details. */
1586
1587 static void
1588 relax_switch (void)
1589 {
1590 assert (mips_relax.sequence == 1);
1591 mips_relax.sequence = 2;
1592 }
1593
1594 /* End the current relaxable sequence. */
1595
1596 static void
1597 relax_end (void)
1598 {
1599 assert (mips_relax.sequence == 2);
1600 relax_close_frag ();
1601 mips_relax.sequence = 0;
1602 }
1603
1604 /* Output an instruction. IP is the instruction information.
1605 ADDRESS_EXPR is an operand of the instruction to be used with
1606 RELOC_TYPE. */
1607
1608 static void
1609 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
1610 bfd_reloc_code_real_type *reloc_type)
1611 {
1612 register unsigned long prev_pinfo, pinfo;
1613 char *f;
1614 fixS *fixp[3];
1615 int nops = 0;
1616 relax_stateT prev_insn_frag_type = 0;
1617 bfd_boolean relaxed_branch = FALSE;
1618 bfd_boolean force_new_frag = FALSE;
1619
1620 /* Mark instruction labels in mips16 mode. */
1621 mips16_mark_labels ();
1622
1623 prev_pinfo = prev_insn.insn_mo->pinfo;
1624 pinfo = ip->insn_mo->pinfo;
1625
1626 if (mips_relax.sequence != 2
1627 && (!mips_opts.noreorder || prev_nop_frag != NULL))
1628 {
1629 int prev_prev_nop;
1630
1631 /* If the previous insn required any delay slots, see if we need
1632 to insert a NOP or two. There are eight kinds of possible
1633 hazards, of which an instruction can have at most one type.
1634 (1) a load from memory delay
1635 (2) a load from a coprocessor delay
1636 (3) an unconditional branch delay
1637 (4) a conditional branch delay
1638 (5) a move to coprocessor register delay
1639 (6) a load coprocessor register from memory delay
1640 (7) a coprocessor condition code delay
1641 (8) a HI/LO special register delay
1642
1643 There are a lot of optimizations we could do that we don't.
1644 In particular, we do not, in general, reorder instructions.
1645 If you use gcc with optimization, it will reorder
1646 instructions and generally do much more optimization then we
1647 do here; repeating all that work in the assembler would only
1648 benefit hand written assembly code, and does not seem worth
1649 it. */
1650
1651 /* This is how a NOP is emitted. */
1652 #define emit_nop() \
1653 (mips_opts.mips16 \
1654 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1655 : md_number_to_chars (frag_more (4), 0, 4))
1656
1657 /* The previous insn might require a delay slot, depending upon
1658 the contents of the current insn. */
1659 if (! mips_opts.mips16
1660 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1661 && ! gpr_interlocks)
1662 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1663 && ! cop_interlocks)))
1664 {
1665 /* A load from a coprocessor or from memory. All load
1666 delays delay the use of general register rt for one
1667 instruction. */
1668 /* Itbl support may require additional care here. */
1669 know (prev_pinfo & INSN_WRITE_GPR_T);
1670 if (mips_optimize == 0
1671 || insn_uses_reg (ip,
1672 ((prev_insn.insn_opcode >> OP_SH_RT)
1673 & OP_MASK_RT),
1674 MIPS_GR_REG))
1675 ++nops;
1676 }
1677 else if (! mips_opts.mips16
1678 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1679 && ! cop_interlocks)
1680 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1681 && ! cop_mem_interlocks)))
1682 {
1683 /* A generic coprocessor delay. The previous instruction
1684 modified a coprocessor general or control register. If
1685 it modified a control register, we need to avoid any
1686 coprocessor instruction (this is probably not always
1687 required, but it sometimes is). If it modified a general
1688 register, we avoid using that register.
1689
1690 This case is not handled very well. There is no special
1691 knowledge of CP0 handling, and the coprocessors other
1692 than the floating point unit are not distinguished at
1693 all. */
1694 /* Itbl support may require additional care here. FIXME!
1695 Need to modify this to include knowledge about
1696 user specified delays! */
1697 if (prev_pinfo & INSN_WRITE_FPR_T)
1698 {
1699 if (mips_optimize == 0
1700 || insn_uses_reg (ip,
1701 ((prev_insn.insn_opcode >> OP_SH_FT)
1702 & OP_MASK_FT),
1703 MIPS_FP_REG))
1704 ++nops;
1705 }
1706 else if (prev_pinfo & INSN_WRITE_FPR_S)
1707 {
1708 if (mips_optimize == 0
1709 || insn_uses_reg (ip,
1710 ((prev_insn.insn_opcode >> OP_SH_FS)
1711 & OP_MASK_FS),
1712 MIPS_FP_REG))
1713 ++nops;
1714 }
1715 else
1716 {
1717 /* We don't know exactly what the previous instruction
1718 does. If the current instruction uses a coprocessor
1719 register, we must insert a NOP. If previous
1720 instruction may set the condition codes, and the
1721 current instruction uses them, we must insert two
1722 NOPS. */
1723 /* Itbl support may require additional care here. */
1724 if (mips_optimize == 0
1725 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1726 && (pinfo & INSN_READ_COND_CODE)))
1727 nops += 2;
1728 else if (pinfo & INSN_COP)
1729 ++nops;
1730 }
1731 }
1732 else if (! mips_opts.mips16
1733 && (prev_pinfo & INSN_WRITE_COND_CODE)
1734 && ! cop_interlocks)
1735 {
1736 /* The previous instruction sets the coprocessor condition
1737 codes, but does not require a general coprocessor delay
1738 (this means it is a floating point comparison
1739 instruction). If this instruction uses the condition
1740 codes, we need to insert a single NOP. */
1741 /* Itbl support may require additional care here. */
1742 if (mips_optimize == 0
1743 || (pinfo & INSN_READ_COND_CODE))
1744 ++nops;
1745 }
1746
1747 /* If we're fixing up mfhi/mflo for the r7000 and the
1748 previous insn was an mfhi/mflo and the current insn
1749 reads the register that the mfhi/mflo wrote to, then
1750 insert two nops. */
1751
1752 else if (mips_7000_hilo_fix
1753 && MF_HILO_INSN (prev_pinfo)
1754 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1755 & OP_MASK_RD),
1756 MIPS_GR_REG))
1757 {
1758 nops += 2;
1759 }
1760
1761 /* If we're fixing up mfhi/mflo for the r7000 and the
1762 2nd previous insn was an mfhi/mflo and the current insn
1763 reads the register that the mfhi/mflo wrote to, then
1764 insert one nop. */
1765
1766 else if (mips_7000_hilo_fix
1767 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1768 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1769 & OP_MASK_RD),
1770 MIPS_GR_REG))
1771
1772 {
1773 ++nops;
1774 }
1775
1776 else if (prev_pinfo & INSN_READ_LO)
1777 {
1778 /* The previous instruction reads the LO register; if the
1779 current instruction writes to the LO register, we must
1780 insert two NOPS. Some newer processors have interlocks.
1781 Also the tx39's multiply instructions can be executed
1782 immediately after a read from HI/LO (without the delay),
1783 though the tx39's divide insns still do require the
1784 delay. */
1785 if (! (hilo_interlocks
1786 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1787 && (mips_optimize == 0
1788 || (pinfo & INSN_WRITE_LO)))
1789 nops += 2;
1790 /* Most mips16 branch insns don't have a delay slot.
1791 If a read from LO is immediately followed by a branch
1792 to a write to LO we have a read followed by a write
1793 less than 2 insns away. We assume the target of
1794 a branch might be a write to LO, and insert a nop
1795 between a read and an immediately following branch. */
1796 else if (mips_opts.mips16
1797 && (mips_optimize == 0
1798 || (pinfo & MIPS16_INSN_BRANCH)))
1799 ++nops;
1800 }
1801 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1802 {
1803 /* The previous instruction reads the HI register; if the
1804 current instruction writes to the HI register, we must
1805 insert a NOP. Some newer processors have interlocks.
1806 Also the note tx39's multiply above. */
1807 if (! (hilo_interlocks
1808 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1809 && (mips_optimize == 0
1810 || (pinfo & INSN_WRITE_HI)))
1811 nops += 2;
1812 /* Most mips16 branch insns don't have a delay slot.
1813 If a read from HI is immediately followed by a branch
1814 to a write to HI we have a read followed by a write
1815 less than 2 insns away. We assume the target of
1816 a branch might be a write to HI, and insert a nop
1817 between a read and an immediately following branch. */
1818 else if (mips_opts.mips16
1819 && (mips_optimize == 0
1820 || (pinfo & MIPS16_INSN_BRANCH)))
1821 ++nops;
1822 }
1823
1824 /* If the previous instruction was in a noreorder section, then
1825 we don't want to insert the nop after all. */
1826 /* Itbl support may require additional care here. */
1827 if (prev_insn_unreordered)
1828 nops = 0;
1829
1830 /* There are two cases which require two intervening
1831 instructions: 1) setting the condition codes using a move to
1832 coprocessor instruction which requires a general coprocessor
1833 delay and then reading the condition codes 2) reading the HI
1834 or LO register and then writing to it (except on processors
1835 which have interlocks). If we are not already emitting a NOP
1836 instruction, we must check for these cases compared to the
1837 instruction previous to the previous instruction. */
1838 if ((! mips_opts.mips16
1839 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1840 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1841 && (pinfo & INSN_READ_COND_CODE)
1842 && ! cop_interlocks)
1843 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1844 && (pinfo & INSN_WRITE_LO)
1845 && ! (hilo_interlocks
1846 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
1847 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1848 && (pinfo & INSN_WRITE_HI)
1849 && ! (hilo_interlocks
1850 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
1851 prev_prev_nop = 1;
1852 else
1853 prev_prev_nop = 0;
1854
1855 if (prev_prev_insn_unreordered)
1856 prev_prev_nop = 0;
1857
1858 if (prev_prev_nop && nops == 0)
1859 ++nops;
1860
1861 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
1862 {
1863 /* We're out of bits in pinfo, so we must resort to string
1864 ops here. Shortcuts are selected based on opcodes being
1865 limited to the VR4120 instruction set. */
1866 int min_nops = 0;
1867 const char *pn = prev_insn.insn_mo->name;
1868 const char *tn = ip->insn_mo->name;
1869 if (strncmp (pn, "macc", 4) == 0
1870 || strncmp (pn, "dmacc", 5) == 0)
1871 {
1872 /* Errata 21 - [D]DIV[U] after [D]MACC */
1873 if (strstr (tn, "div"))
1874 min_nops = 1;
1875
1876 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1877 instruction is executed immediately after a MACC or
1878 DMACC instruction, the result of [either instruction]
1879 is incorrect." */
1880 if (strncmp (tn, "mult", 4) == 0
1881 || strncmp (tn, "dmult", 5) == 0)
1882 min_nops = 1;
1883
1884 /* Errata 23 - Continuous DMULT[U]/DMACC instructions.
1885 Applies on top of VR4181A MD(1) errata. */
1886 if (pn[0] == 'd' && strncmp (tn, "dmacc", 5) == 0)
1887 min_nops = 1;
1888
1889 /* Errata 24 - MT{LO,HI} after [D]MACC */
1890 if (strcmp (tn, "mtlo") == 0
1891 || strcmp (tn, "mthi") == 0)
1892 min_nops = 1;
1893 }
1894 else if (strncmp (pn, "dmult", 5) == 0
1895 && (strncmp (tn, "dmult", 5) == 0
1896 || strncmp (tn, "dmacc", 5) == 0))
1897 {
1898 /* Here is the rest of errata 23. */
1899 min_nops = 1;
1900 }
1901 else if ((strncmp (pn, "dmult", 5) == 0 || strstr (pn, "div"))
1902 && (strncmp (tn, "macc", 4) == 0
1903 || strncmp (tn, "dmacc", 5) == 0))
1904 {
1905 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1906 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1907 DDIV or DDIVU instruction, the result of the MACC or
1908 DMACC instruction is incorrect.". This partly overlaps
1909 the workaround for errata 23. */
1910 min_nops = 1;
1911 }
1912 if (nops < min_nops)
1913 nops = min_nops;
1914 }
1915
1916 /* If we are being given a nop instruction, don't bother with
1917 one of the nops we would otherwise output. This will only
1918 happen when a nop instruction is used with mips_optimize set
1919 to 0. */
1920 if (nops > 0
1921 && ! mips_opts.noreorder
1922 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1923 --nops;
1924
1925 /* Now emit the right number of NOP instructions. */
1926 if (nops > 0 && ! mips_opts.noreorder)
1927 {
1928 fragS *old_frag;
1929 unsigned long old_frag_offset;
1930 int i;
1931 struct insn_label_list *l;
1932
1933 old_frag = frag_now;
1934 old_frag_offset = frag_now_fix ();
1935
1936 for (i = 0; i < nops; i++)
1937 emit_nop ();
1938
1939 if (listing)
1940 {
1941 listing_prev_line ();
1942 /* We may be at the start of a variant frag. In case we
1943 are, make sure there is enough space for the frag
1944 after the frags created by listing_prev_line. The
1945 argument to frag_grow here must be at least as large
1946 as the argument to all other calls to frag_grow in
1947 this file. We don't have to worry about being in the
1948 middle of a variant frag, because the variants insert
1949 all needed nop instructions themselves. */
1950 frag_grow (40);
1951 }
1952
1953 for (l = insn_labels; l != NULL; l = l->next)
1954 {
1955 valueT val;
1956
1957 assert (S_GET_SEGMENT (l->label) == now_seg);
1958 symbol_set_frag (l->label, frag_now);
1959 val = (valueT) frag_now_fix ();
1960 /* mips16 text labels are stored as odd. */
1961 if (mips_opts.mips16)
1962 ++val;
1963 S_SET_VALUE (l->label, val);
1964 }
1965
1966 #ifndef NO_ECOFF_DEBUGGING
1967 if (ECOFF_DEBUGGING)
1968 ecoff_fix_loc (old_frag, old_frag_offset);
1969 #endif
1970 }
1971 else if (prev_nop_frag != NULL)
1972 {
1973 /* We have a frag holding nops we may be able to remove. If
1974 we don't need any nops, we can decrease the size of
1975 prev_nop_frag by the size of one instruction. If we do
1976 need some nops, we count them in prev_nops_required. */
1977 if (prev_nop_frag_since == 0)
1978 {
1979 if (nops == 0)
1980 {
1981 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1982 --prev_nop_frag_holds;
1983 }
1984 else
1985 prev_nop_frag_required += nops;
1986 }
1987 else
1988 {
1989 if (prev_prev_nop == 0)
1990 {
1991 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1992 --prev_nop_frag_holds;
1993 }
1994 else
1995 ++prev_nop_frag_required;
1996 }
1997
1998 if (prev_nop_frag_holds <= prev_nop_frag_required)
1999 prev_nop_frag = NULL;
2000
2001 ++prev_nop_frag_since;
2002
2003 /* Sanity check: by the time we reach the second instruction
2004 after prev_nop_frag, we should have used up all the nops
2005 one way or another. */
2006 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2007 }
2008 }
2009
2010 /* Record the frag type before frag_var. */
2011 if (prev_insn_frag)
2012 prev_insn_frag_type = prev_insn_frag->fr_type;
2013
2014 if (address_expr
2015 && *reloc_type == BFD_RELOC_16_PCREL_S2
2016 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2017 || pinfo & INSN_COND_BRANCH_LIKELY)
2018 && mips_relax_branch
2019 /* Don't try branch relaxation within .set nomacro, or within
2020 .set noat if we use $at for PIC computations. If it turns
2021 out that the branch was out-of-range, we'll get an error. */
2022 && !mips_opts.warn_about_macros
2023 && !(mips_opts.noat && mips_pic != NO_PIC)
2024 && !mips_opts.mips16)
2025 {
2026 relaxed_branch = TRUE;
2027 f = frag_var (rs_machine_dependent,
2028 relaxed_branch_length
2029 (NULL, NULL,
2030 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2031 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2032 RELAX_BRANCH_ENCODE
2033 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2034 pinfo & INSN_COND_BRANCH_LIKELY,
2035 pinfo & INSN_WRITE_GPR_31,
2036 0),
2037 address_expr->X_add_symbol,
2038 address_expr->X_add_number,
2039 0);
2040 *reloc_type = BFD_RELOC_UNUSED;
2041 }
2042 else if (*reloc_type > BFD_RELOC_UNUSED)
2043 {
2044 /* We need to set up a variant frag. */
2045 assert (mips_opts.mips16 && address_expr != NULL);
2046 f = frag_var (rs_machine_dependent, 4, 0,
2047 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2048 mips16_small, mips16_ext,
2049 (prev_pinfo
2050 & INSN_UNCOND_BRANCH_DELAY),
2051 (*prev_insn_reloc_type
2052 == BFD_RELOC_MIPS16_JMP)),
2053 make_expr_symbol (address_expr), 0, NULL);
2054 }
2055 else if (mips_opts.mips16
2056 && ! ip->use_extend
2057 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2058 {
2059 /* Make sure there is enough room to swap this instruction with
2060 a following jump instruction. */
2061 frag_grow (6);
2062 f = frag_more (2);
2063 }
2064 else
2065 {
2066 if (mips_opts.mips16
2067 && mips_opts.noreorder
2068 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2069 as_warn (_("extended instruction in delay slot"));
2070
2071 if (mips_relax.sequence)
2072 {
2073 /* If we've reached the end of this frag, turn it into a variant
2074 frag and record the information for the instructions we've
2075 written so far. */
2076 if (frag_room () < 4)
2077 relax_close_frag ();
2078 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2079 }
2080
2081 if (mips_relax.sequence != 2)
2082 mips_macro_warning.sizes[0] += 4;
2083 if (mips_relax.sequence != 1)
2084 mips_macro_warning.sizes[1] += 4;
2085
2086 f = frag_more (4);
2087 }
2088
2089 fixp[0] = fixp[1] = fixp[2] = NULL;
2090 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2091 {
2092 if (address_expr->X_op == O_constant)
2093 {
2094 valueT tmp;
2095
2096 switch (*reloc_type)
2097 {
2098 case BFD_RELOC_32:
2099 ip->insn_opcode |= address_expr->X_add_number;
2100 break;
2101
2102 case BFD_RELOC_MIPS_HIGHEST:
2103 tmp = (address_expr->X_add_number
2104 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2105 tmp >>= 16;
2106 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2107 break;
2108
2109 case BFD_RELOC_MIPS_HIGHER:
2110 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2111 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2112 break;
2113
2114 case BFD_RELOC_HI16_S:
2115 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2116 >> 16) & 0xffff;
2117 break;
2118
2119 case BFD_RELOC_HI16:
2120 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2121 break;
2122
2123 case BFD_RELOC_UNUSED:
2124 case BFD_RELOC_LO16:
2125 case BFD_RELOC_MIPS_GOT_DISP:
2126 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2127 break;
2128
2129 case BFD_RELOC_MIPS_JMP:
2130 if ((address_expr->X_add_number & 3) != 0)
2131 as_bad (_("jump to misaligned address (0x%lx)"),
2132 (unsigned long) address_expr->X_add_number);
2133 if (address_expr->X_add_number & ~0xfffffff)
2134 as_bad (_("jump address range overflow (0x%lx)"),
2135 (unsigned long) address_expr->X_add_number);
2136 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2137 break;
2138
2139 case BFD_RELOC_MIPS16_JMP:
2140 if ((address_expr->X_add_number & 3) != 0)
2141 as_bad (_("jump to misaligned address (0x%lx)"),
2142 (unsigned long) address_expr->X_add_number);
2143 if (address_expr->X_add_number & ~0xfffffff)
2144 as_bad (_("jump address range overflow (0x%lx)"),
2145 (unsigned long) address_expr->X_add_number);
2146 ip->insn_opcode |=
2147 (((address_expr->X_add_number & 0x7c0000) << 3)
2148 | ((address_expr->X_add_number & 0xf800000) >> 7)
2149 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2150 break;
2151
2152 case BFD_RELOC_16_PCREL_S2:
2153 goto need_reloc;
2154
2155 default:
2156 internalError ();
2157 }
2158 }
2159 else if (*reloc_type < BFD_RELOC_UNUSED)
2160 need_reloc:
2161 {
2162 reloc_howto_type *howto;
2163 int i;
2164
2165 /* In a compound relocation, it is the final (outermost)
2166 operator that determines the relocated field. */
2167 for (i = 1; i < 3; i++)
2168 if (reloc_type[i] == BFD_RELOC_UNUSED)
2169 break;
2170
2171 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2172 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2173 bfd_get_reloc_size(howto),
2174 address_expr,
2175 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2176 reloc_type[0]);
2177
2178 /* These relocations can have an addend that won't fit in
2179 4 octets for 64bit assembly. */
2180 if (HAVE_64BIT_GPRS
2181 && ! howto->partial_inplace
2182 && (reloc_type[0] == BFD_RELOC_16
2183 || reloc_type[0] == BFD_RELOC_32
2184 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2185 || reloc_type[0] == BFD_RELOC_HI16_S
2186 || reloc_type[0] == BFD_RELOC_LO16
2187 || reloc_type[0] == BFD_RELOC_GPREL16
2188 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2189 || reloc_type[0] == BFD_RELOC_GPREL32
2190 || reloc_type[0] == BFD_RELOC_64
2191 || reloc_type[0] == BFD_RELOC_CTOR
2192 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2193 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2194 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2195 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2196 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2197 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2198 fixp[0]->fx_no_overflow = 1;
2199
2200 if (mips_relax.sequence)
2201 {
2202 if (mips_relax.first_fixup == 0)
2203 mips_relax.first_fixup = fixp[0];
2204 }
2205 else if (reloc_needs_lo_p (*reloc_type))
2206 {
2207 struct mips_hi_fixup *hi_fixup;
2208
2209 /* Reuse the last entry if it already has a matching %lo. */
2210 hi_fixup = mips_hi_fixup_list;
2211 if (hi_fixup == 0
2212 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2213 {
2214 hi_fixup = ((struct mips_hi_fixup *)
2215 xmalloc (sizeof (struct mips_hi_fixup)));
2216 hi_fixup->next = mips_hi_fixup_list;
2217 mips_hi_fixup_list = hi_fixup;
2218 }
2219 hi_fixup->fixp = fixp[0];
2220 hi_fixup->seg = now_seg;
2221 }
2222
2223 /* Add fixups for the second and third relocations, if given.
2224 Note that the ABI allows the second relocation to be
2225 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2226 moment we only use RSS_UNDEF, but we could add support
2227 for the others if it ever becomes necessary. */
2228 for (i = 1; i < 3; i++)
2229 if (reloc_type[i] != BFD_RELOC_UNUSED)
2230 {
2231 fixp[i] = fix_new (frag_now, fixp[0]->fx_where,
2232 fixp[0]->fx_size, NULL, 0,
2233 FALSE, reloc_type[i]);
2234
2235 /* Use fx_tcbit to mark compound relocs. */
2236 fixp[0]->fx_tcbit = 1;
2237 fixp[i]->fx_tcbit = 1;
2238 }
2239 }
2240 }
2241
2242 if (! mips_opts.mips16)
2243 {
2244 md_number_to_chars (f, ip->insn_opcode, 4);
2245 #ifdef OBJ_ELF
2246 dwarf2_emit_insn (4);
2247 #endif
2248 }
2249 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2250 {
2251 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2252 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2253 #ifdef OBJ_ELF
2254 /* The value passed to dwarf2_emit_insn is the distance between
2255 the end of the current instruction and the address that should
2256 be recorded in the debug tables. Since we want to use ISA-encoded
2257 addresses in MIPS16 debug info, the value is one byte less than
2258 the real instruction length. */
2259 dwarf2_emit_insn (3);
2260 #endif
2261 }
2262 else
2263 {
2264 if (ip->use_extend)
2265 {
2266 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2267 f += 2;
2268 }
2269 md_number_to_chars (f, ip->insn_opcode, 2);
2270 #ifdef OBJ_ELF
2271 dwarf2_emit_insn (ip->use_extend ? 3 : 1);
2272 #endif
2273 }
2274
2275 /* Update the register mask information. */
2276 if (! mips_opts.mips16)
2277 {
2278 if (pinfo & INSN_WRITE_GPR_D)
2279 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2280 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2281 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2282 if (pinfo & INSN_READ_GPR_S)
2283 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2284 if (pinfo & INSN_WRITE_GPR_31)
2285 mips_gprmask |= 1 << RA;
2286 if (pinfo & INSN_WRITE_FPR_D)
2287 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2288 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2289 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2290 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2291 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2292 if ((pinfo & INSN_READ_FPR_R) != 0)
2293 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2294 if (pinfo & INSN_COP)
2295 {
2296 /* We don't keep enough information to sort these cases out.
2297 The itbl support does keep this information however, although
2298 we currently don't support itbl fprmats as part of the cop
2299 instruction. May want to add this support in the future. */
2300 }
2301 /* Never set the bit for $0, which is always zero. */
2302 mips_gprmask &= ~1 << 0;
2303 }
2304 else
2305 {
2306 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2307 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2308 & MIPS16OP_MASK_RX);
2309 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2310 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2311 & MIPS16OP_MASK_RY);
2312 if (pinfo & MIPS16_INSN_WRITE_Z)
2313 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2314 & MIPS16OP_MASK_RZ);
2315 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2316 mips_gprmask |= 1 << TREG;
2317 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2318 mips_gprmask |= 1 << SP;
2319 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2320 mips_gprmask |= 1 << RA;
2321 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2322 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2323 if (pinfo & MIPS16_INSN_READ_Z)
2324 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2325 & MIPS16OP_MASK_MOVE32Z);
2326 if (pinfo & MIPS16_INSN_READ_GPR_X)
2327 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2328 & MIPS16OP_MASK_REGR32);
2329 }
2330
2331 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2332 {
2333 /* Filling the branch delay slot is more complex. We try to
2334 switch the branch with the previous instruction, which we can
2335 do if the previous instruction does not set up a condition
2336 that the branch tests and if the branch is not itself the
2337 target of any branch. */
2338 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2339 || (pinfo & INSN_COND_BRANCH_DELAY))
2340 {
2341 if (mips_optimize < 2
2342 /* If we have seen .set volatile or .set nomove, don't
2343 optimize. */
2344 || mips_opts.nomove != 0
2345 /* If we had to emit any NOP instructions, then we
2346 already know we can not swap. */
2347 || nops != 0
2348 /* If we don't even know the previous insn, we can not
2349 swap. */
2350 || ! prev_insn_valid
2351 /* If the previous insn is already in a branch delay
2352 slot, then we can not swap. */
2353 || prev_insn_is_delay_slot
2354 /* If the previous previous insn was in a .set
2355 noreorder, we can't swap. Actually, the MIPS
2356 assembler will swap in this situation. However, gcc
2357 configured -with-gnu-as will generate code like
2358 .set noreorder
2359 lw $4,XXX
2360 .set reorder
2361 INSN
2362 bne $4,$0,foo
2363 in which we can not swap the bne and INSN. If gcc is
2364 not configured -with-gnu-as, it does not output the
2365 .set pseudo-ops. We don't have to check
2366 prev_insn_unreordered, because prev_insn_valid will
2367 be 0 in that case. We don't want to use
2368 prev_prev_insn_valid, because we do want to be able
2369 to swap at the start of a function. */
2370 || prev_prev_insn_unreordered
2371 /* If the branch is itself the target of a branch, we
2372 can not swap. We cheat on this; all we check for is
2373 whether there is a label on this instruction. If
2374 there are any branches to anything other than a
2375 label, users must use .set noreorder. */
2376 || insn_labels != NULL
2377 /* If the previous instruction is in a variant frag
2378 other than this branch's one, we cannot do the swap.
2379 This does not apply to the mips16, which uses variant
2380 frags for different purposes. */
2381 || (! mips_opts.mips16
2382 && prev_insn_frag_type == rs_machine_dependent)
2383 /* If the branch reads the condition codes, we don't
2384 even try to swap, because in the sequence
2385 ctc1 $X,$31
2386 INSN
2387 INSN
2388 bc1t LABEL
2389 we can not swap, and I don't feel like handling that
2390 case. */
2391 || (! mips_opts.mips16
2392 && (pinfo & INSN_READ_COND_CODE)
2393 && ! cop_interlocks)
2394 /* We can not swap with an instruction that requires a
2395 delay slot, because the target of the branch might
2396 interfere with that instruction. */
2397 || (! mips_opts.mips16
2398 && (prev_pinfo
2399 /* Itbl support may require additional care here. */
2400 & (INSN_LOAD_COPROC_DELAY
2401 | INSN_COPROC_MOVE_DELAY
2402 | INSN_WRITE_COND_CODE))
2403 && ! cop_interlocks)
2404 || (! (hilo_interlocks
2405 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
2406 && (prev_pinfo
2407 & (INSN_READ_LO
2408 | INSN_READ_HI)))
2409 || (! mips_opts.mips16
2410 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2411 && ! gpr_interlocks)
2412 || (! mips_opts.mips16
2413 /* Itbl support may require additional care here. */
2414 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2415 && ! cop_mem_interlocks)
2416 /* We can not swap with a branch instruction. */
2417 || (prev_pinfo
2418 & (INSN_UNCOND_BRANCH_DELAY
2419 | INSN_COND_BRANCH_DELAY
2420 | INSN_COND_BRANCH_LIKELY))
2421 /* We do not swap with a trap instruction, since it
2422 complicates trap handlers to have the trap
2423 instruction be in a delay slot. */
2424 || (prev_pinfo & INSN_TRAP)
2425 /* If the branch reads a register that the previous
2426 instruction sets, we can not swap. */
2427 || (! mips_opts.mips16
2428 && (prev_pinfo & INSN_WRITE_GPR_T)
2429 && insn_uses_reg (ip,
2430 ((prev_insn.insn_opcode >> OP_SH_RT)
2431 & OP_MASK_RT),
2432 MIPS_GR_REG))
2433 || (! mips_opts.mips16
2434 && (prev_pinfo & INSN_WRITE_GPR_D)
2435 && insn_uses_reg (ip,
2436 ((prev_insn.insn_opcode >> OP_SH_RD)
2437 & OP_MASK_RD),
2438 MIPS_GR_REG))
2439 || (mips_opts.mips16
2440 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2441 && insn_uses_reg (ip,
2442 ((prev_insn.insn_opcode
2443 >> MIPS16OP_SH_RX)
2444 & MIPS16OP_MASK_RX),
2445 MIPS16_REG))
2446 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2447 && insn_uses_reg (ip,
2448 ((prev_insn.insn_opcode
2449 >> MIPS16OP_SH_RY)
2450 & MIPS16OP_MASK_RY),
2451 MIPS16_REG))
2452 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2453 && insn_uses_reg (ip,
2454 ((prev_insn.insn_opcode
2455 >> MIPS16OP_SH_RZ)
2456 & MIPS16OP_MASK_RZ),
2457 MIPS16_REG))
2458 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2459 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2460 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2461 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2462 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2463 && insn_uses_reg (ip,
2464 MIPS16OP_EXTRACT_REG32R (prev_insn.
2465 insn_opcode),
2466 MIPS_GR_REG))))
2467 /* If the branch writes a register that the previous
2468 instruction sets, we can not swap (we know that
2469 branches write only to RD or to $31). */
2470 || (! mips_opts.mips16
2471 && (prev_pinfo & INSN_WRITE_GPR_T)
2472 && (((pinfo & INSN_WRITE_GPR_D)
2473 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2474 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2475 || ((pinfo & INSN_WRITE_GPR_31)
2476 && (((prev_insn.insn_opcode >> OP_SH_RT)
2477 & OP_MASK_RT)
2478 == RA))))
2479 || (! mips_opts.mips16
2480 && (prev_pinfo & INSN_WRITE_GPR_D)
2481 && (((pinfo & INSN_WRITE_GPR_D)
2482 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2483 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2484 || ((pinfo & INSN_WRITE_GPR_31)
2485 && (((prev_insn.insn_opcode >> OP_SH_RD)
2486 & OP_MASK_RD)
2487 == RA))))
2488 || (mips_opts.mips16
2489 && (pinfo & MIPS16_INSN_WRITE_31)
2490 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2491 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2492 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2493 == RA))))
2494 /* If the branch writes a register that the previous
2495 instruction reads, we can not swap (we know that
2496 branches only write to RD or to $31). */
2497 || (! mips_opts.mips16
2498 && (pinfo & INSN_WRITE_GPR_D)
2499 && insn_uses_reg (&prev_insn,
2500 ((ip->insn_opcode >> OP_SH_RD)
2501 & OP_MASK_RD),
2502 MIPS_GR_REG))
2503 || (! mips_opts.mips16
2504 && (pinfo & INSN_WRITE_GPR_31)
2505 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2506 || (mips_opts.mips16
2507 && (pinfo & MIPS16_INSN_WRITE_31)
2508 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2509 /* If the previous previous instruction has a load
2510 delay, and sets a register that the branch reads, we
2511 can not swap. */
2512 || (! mips_opts.mips16
2513 /* Itbl support may require additional care here. */
2514 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2515 && ! cop_interlocks)
2516 || ((prev_prev_insn.insn_mo->pinfo
2517 & INSN_LOAD_MEMORY_DELAY)
2518 && ! gpr_interlocks))
2519 && insn_uses_reg (ip,
2520 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2521 & OP_MASK_RT),
2522 MIPS_GR_REG))
2523 /* If one instruction sets a condition code and the
2524 other one uses a condition code, we can not swap. */
2525 || ((pinfo & INSN_READ_COND_CODE)
2526 && (prev_pinfo & INSN_WRITE_COND_CODE))
2527 || ((pinfo & INSN_WRITE_COND_CODE)
2528 && (prev_pinfo & INSN_READ_COND_CODE))
2529 /* If the previous instruction uses the PC, we can not
2530 swap. */
2531 || (mips_opts.mips16
2532 && (prev_pinfo & MIPS16_INSN_READ_PC))
2533 /* If the previous instruction was extended, we can not
2534 swap. */
2535 || (mips_opts.mips16 && prev_insn_extended)
2536 /* If the previous instruction had a fixup in mips16
2537 mode, we can not swap. This normally means that the
2538 previous instruction was a 4 byte branch anyhow. */
2539 || (mips_opts.mips16 && prev_insn_fixp[0])
2540 /* If the previous instruction is a sync, sync.l, or
2541 sync.p, we can not swap. */
2542 || (prev_pinfo & INSN_SYNC))
2543 {
2544 /* We could do even better for unconditional branches to
2545 portions of this object file; we could pick up the
2546 instruction at the destination, put it in the delay
2547 slot, and bump the destination address. */
2548 emit_nop ();
2549 if (mips_relax.sequence)
2550 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2551 /* Update the previous insn information. */
2552 prev_prev_insn = *ip;
2553 prev_insn.insn_mo = &dummy_opcode;
2554 }
2555 else
2556 {
2557 /* It looks like we can actually do the swap. */
2558 if (! mips_opts.mips16)
2559 {
2560 char *prev_f;
2561 char temp[4];
2562
2563 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2564 if (!relaxed_branch)
2565 {
2566 /* If this is not a relaxed branch, then just
2567 swap the instructions. */
2568 memcpy (temp, prev_f, 4);
2569 memcpy (prev_f, f, 4);
2570 memcpy (f, temp, 4);
2571 }
2572 else
2573 {
2574 /* If this is a relaxed branch, then we move the
2575 instruction to be placed in the delay slot to
2576 the current frag, shrinking the fixed part of
2577 the originating frag. If the branch occupies
2578 the tail of the latter, we move it backwards,
2579 into the space freed by the moved instruction. */
2580 f = frag_more (4);
2581 memcpy (f, prev_f, 4);
2582 prev_insn_frag->fr_fix -= 4;
2583 if (prev_insn_frag->fr_type == rs_machine_dependent)
2584 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2585 }
2586
2587 if (prev_insn_fixp[0])
2588 {
2589 prev_insn_fixp[0]->fx_frag = frag_now;
2590 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2591 }
2592 if (prev_insn_fixp[1])
2593 {
2594 prev_insn_fixp[1]->fx_frag = frag_now;
2595 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2596 }
2597 if (prev_insn_fixp[2])
2598 {
2599 prev_insn_fixp[2]->fx_frag = frag_now;
2600 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2601 }
2602 if (prev_insn_fixp[0] && HAVE_NEWABI
2603 && prev_insn_frag != frag_now
2604 && (prev_insn_fixp[0]->fx_r_type
2605 == BFD_RELOC_MIPS_GOT_DISP
2606 || (prev_insn_fixp[0]->fx_r_type
2607 == BFD_RELOC_MIPS_CALL16)))
2608 {
2609 /* To avoid confusion in tc_gen_reloc, we must
2610 ensure that this does not become a variant
2611 frag. */
2612 force_new_frag = TRUE;
2613 }
2614
2615 if (!relaxed_branch)
2616 {
2617 if (fixp[0])
2618 {
2619 fixp[0]->fx_frag = prev_insn_frag;
2620 fixp[0]->fx_where = prev_insn_where;
2621 }
2622 if (fixp[1])
2623 {
2624 fixp[1]->fx_frag = prev_insn_frag;
2625 fixp[1]->fx_where = prev_insn_where;
2626 }
2627 if (fixp[2])
2628 {
2629 fixp[2]->fx_frag = prev_insn_frag;
2630 fixp[2]->fx_where = prev_insn_where;
2631 }
2632 }
2633 else if (prev_insn_frag->fr_type == rs_machine_dependent)
2634 {
2635 if (fixp[0])
2636 fixp[0]->fx_where -= 4;
2637 if (fixp[1])
2638 fixp[1]->fx_where -= 4;
2639 if (fixp[2])
2640 fixp[2]->fx_where -= 4;
2641 }
2642 }
2643 else
2644 {
2645 char *prev_f;
2646 char temp[2];
2647
2648 assert (prev_insn_fixp[0] == NULL);
2649 assert (prev_insn_fixp[1] == NULL);
2650 assert (prev_insn_fixp[2] == NULL);
2651 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2652 memcpy (temp, prev_f, 2);
2653 memcpy (prev_f, f, 2);
2654 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2655 {
2656 assert (*reloc_type == BFD_RELOC_UNUSED);
2657 memcpy (f, temp, 2);
2658 }
2659 else
2660 {
2661 memcpy (f, f + 2, 2);
2662 memcpy (f + 2, temp, 2);
2663 }
2664 if (fixp[0])
2665 {
2666 fixp[0]->fx_frag = prev_insn_frag;
2667 fixp[0]->fx_where = prev_insn_where;
2668 }
2669 if (fixp[1])
2670 {
2671 fixp[1]->fx_frag = prev_insn_frag;
2672 fixp[1]->fx_where = prev_insn_where;
2673 }
2674 if (fixp[2])
2675 {
2676 fixp[2]->fx_frag = prev_insn_frag;
2677 fixp[2]->fx_where = prev_insn_where;
2678 }
2679 }
2680
2681 /* Update the previous insn information; leave prev_insn
2682 unchanged. */
2683 prev_prev_insn = *ip;
2684 }
2685 prev_insn_is_delay_slot = 1;
2686
2687 /* If that was an unconditional branch, forget the previous
2688 insn information. */
2689 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2690 {
2691 prev_prev_insn.insn_mo = &dummy_opcode;
2692 prev_insn.insn_mo = &dummy_opcode;
2693 }
2694
2695 prev_insn_fixp[0] = NULL;
2696 prev_insn_fixp[1] = NULL;
2697 prev_insn_fixp[2] = NULL;
2698 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2699 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2700 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2701 prev_insn_extended = 0;
2702 }
2703 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2704 {
2705 /* We don't yet optimize a branch likely. What we should do
2706 is look at the target, copy the instruction found there
2707 into the delay slot, and increment the branch to jump to
2708 the next instruction. */
2709 emit_nop ();
2710 /* Update the previous insn information. */
2711 prev_prev_insn = *ip;
2712 prev_insn.insn_mo = &dummy_opcode;
2713 prev_insn_fixp[0] = NULL;
2714 prev_insn_fixp[1] = NULL;
2715 prev_insn_fixp[2] = NULL;
2716 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2717 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2718 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2719 prev_insn_extended = 0;
2720 prev_insn_is_delay_slot = 1;
2721 }
2722 else
2723 {
2724 /* Update the previous insn information. */
2725 if (nops > 0)
2726 prev_prev_insn.insn_mo = &dummy_opcode;
2727 else
2728 prev_prev_insn = prev_insn;
2729 prev_insn = *ip;
2730
2731 /* Any time we see a branch, we always fill the delay slot
2732 immediately; since this insn is not a branch, we know it
2733 is not in a delay slot. */
2734 prev_insn_is_delay_slot = 0;
2735
2736 prev_insn_fixp[0] = fixp[0];
2737 prev_insn_fixp[1] = fixp[1];
2738 prev_insn_fixp[2] = fixp[2];
2739 prev_insn_reloc_type[0] = reloc_type[0];
2740 prev_insn_reloc_type[1] = reloc_type[1];
2741 prev_insn_reloc_type[2] = reloc_type[2];
2742 if (mips_opts.mips16)
2743 prev_insn_extended = (ip->use_extend
2744 || *reloc_type > BFD_RELOC_UNUSED);
2745 }
2746
2747 prev_prev_insn_unreordered = prev_insn_unreordered;
2748 prev_insn_unreordered = 0;
2749 prev_insn_frag = frag_now;
2750 prev_insn_where = f - frag_now->fr_literal;
2751 prev_insn_valid = 1;
2752 }
2753 else if (mips_relax.sequence != 2)
2754 {
2755 /* We need to record a bit of information even when we are not
2756 reordering, in order to determine the base address for mips16
2757 PC relative relocs. */
2758 prev_prev_insn = prev_insn;
2759 prev_insn = *ip;
2760 prev_insn_reloc_type[0] = reloc_type[0];
2761 prev_insn_reloc_type[1] = reloc_type[1];
2762 prev_insn_reloc_type[2] = reloc_type[2];
2763 prev_prev_insn_unreordered = prev_insn_unreordered;
2764 prev_insn_unreordered = 1;
2765 }
2766
2767 /* We just output an insn, so the next one doesn't have a label. */
2768 mips_clear_insn_labels ();
2769 }
2770
2771 /* This function forgets that there was any previous instruction or
2772 label. If PRESERVE is non-zero, it remembers enough information to
2773 know whether nops are needed before a noreorder section. */
2774
2775 static void
2776 mips_no_prev_insn (int preserve)
2777 {
2778 if (! preserve)
2779 {
2780 prev_insn.insn_mo = &dummy_opcode;
2781 prev_prev_insn.insn_mo = &dummy_opcode;
2782 prev_nop_frag = NULL;
2783 prev_nop_frag_holds = 0;
2784 prev_nop_frag_required = 0;
2785 prev_nop_frag_since = 0;
2786 }
2787 prev_insn_valid = 0;
2788 prev_insn_is_delay_slot = 0;
2789 prev_insn_unreordered = 0;
2790 prev_insn_extended = 0;
2791 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2792 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2793 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2794 prev_prev_insn_unreordered = 0;
2795 mips_clear_insn_labels ();
2796 }
2797
2798 /* This function must be called whenever we turn on noreorder or emit
2799 something other than instructions. It inserts any NOPS which might
2800 be needed by the previous instruction, and clears the information
2801 kept for the previous instructions. The INSNS parameter is true if
2802 instructions are to follow. */
2803
2804 static void
2805 mips_emit_delays (bfd_boolean insns)
2806 {
2807 if (! mips_opts.noreorder)
2808 {
2809 int nops;
2810
2811 nops = 0;
2812 if ((! mips_opts.mips16
2813 && ((prev_insn.insn_mo->pinfo
2814 & (INSN_LOAD_COPROC_DELAY
2815 | INSN_COPROC_MOVE_DELAY
2816 | INSN_WRITE_COND_CODE))
2817 && ! cop_interlocks))
2818 || (! hilo_interlocks
2819 && (prev_insn.insn_mo->pinfo
2820 & (INSN_READ_LO
2821 | INSN_READ_HI)))
2822 || (! mips_opts.mips16
2823 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2824 && ! gpr_interlocks)
2825 || (! mips_opts.mips16
2826 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2827 && ! cop_mem_interlocks))
2828 {
2829 /* Itbl support may require additional care here. */
2830 ++nops;
2831 if ((! mips_opts.mips16
2832 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2833 && ! cop_interlocks))
2834 || (! hilo_interlocks
2835 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2836 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2837 ++nops;
2838
2839 if (prev_insn_unreordered)
2840 nops = 0;
2841 }
2842 else if ((! mips_opts.mips16
2843 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2844 && ! cop_interlocks))
2845 || (! hilo_interlocks
2846 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2847 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2848 {
2849 /* Itbl support may require additional care here. */
2850 if (! prev_prev_insn_unreordered)
2851 ++nops;
2852 }
2853
2854 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
2855 {
2856 int min_nops = 0;
2857 const char *pn = prev_insn.insn_mo->name;
2858 if (strncmp (pn, "macc", 4) == 0
2859 || strncmp (pn, "dmacc", 5) == 0
2860 || strncmp (pn, "dmult", 5) == 0
2861 || strstr (pn, "div"))
2862 min_nops = 1;
2863 if (nops < min_nops)
2864 nops = min_nops;
2865 }
2866
2867 if (nops > 0)
2868 {
2869 struct insn_label_list *l;
2870
2871 if (insns)
2872 {
2873 /* Record the frag which holds the nop instructions, so
2874 that we can remove them if we don't need them. */
2875 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2876 prev_nop_frag = frag_now;
2877 prev_nop_frag_holds = nops;
2878 prev_nop_frag_required = 0;
2879 prev_nop_frag_since = 0;
2880 }
2881
2882 for (; nops > 0; --nops)
2883 emit_nop ();
2884
2885 if (insns)
2886 {
2887 /* Move on to a new frag, so that it is safe to simply
2888 decrease the size of prev_nop_frag. */
2889 frag_wane (frag_now);
2890 frag_new (0);
2891 }
2892
2893 for (l = insn_labels; l != NULL; l = l->next)
2894 {
2895 valueT val;
2896
2897 assert (S_GET_SEGMENT (l->label) == now_seg);
2898 symbol_set_frag (l->label, frag_now);
2899 val = (valueT) frag_now_fix ();
2900 /* mips16 text labels are stored as odd. */
2901 if (mips_opts.mips16)
2902 ++val;
2903 S_SET_VALUE (l->label, val);
2904 }
2905 }
2906 }
2907
2908 /* Mark instruction labels in mips16 mode. */
2909 if (insns)
2910 mips16_mark_labels ();
2911
2912 mips_no_prev_insn (insns);
2913 }
2914
2915 /* Set up global variables for the start of a new macro. */
2916
2917 static void
2918 macro_start (void)
2919 {
2920 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2921 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2922 && (prev_insn.insn_mo->pinfo
2923 & (INSN_UNCOND_BRANCH_DELAY
2924 | INSN_COND_BRANCH_DELAY
2925 | INSN_COND_BRANCH_LIKELY)) != 0);
2926 }
2927
2928 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2929 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2930 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2931
2932 static const char *
2933 macro_warning (relax_substateT subtype)
2934 {
2935 if (subtype & RELAX_DELAY_SLOT)
2936 return _("Macro instruction expanded into multiple instructions"
2937 " in a branch delay slot");
2938 else if (subtype & RELAX_NOMACRO)
2939 return _("Macro instruction expanded into multiple instructions");
2940 else
2941 return 0;
2942 }
2943
2944 /* Finish up a macro. Emit warnings as appropriate. */
2945
2946 static void
2947 macro_end (void)
2948 {
2949 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2950 {
2951 relax_substateT subtype;
2952
2953 /* Set up the relaxation warning flags. */
2954 subtype = 0;
2955 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2956 subtype |= RELAX_SECOND_LONGER;
2957 if (mips_opts.warn_about_macros)
2958 subtype |= RELAX_NOMACRO;
2959 if (mips_macro_warning.delay_slot_p)
2960 subtype |= RELAX_DELAY_SLOT;
2961
2962 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2963 {
2964 /* Either the macro has a single implementation or both
2965 implementations are longer than 4 bytes. Emit the
2966 warning now. */
2967 const char *msg = macro_warning (subtype);
2968 if (msg != 0)
2969 as_warn (msg);
2970 }
2971 else
2972 {
2973 /* One implementation might need a warning but the other
2974 definitely doesn't. */
2975 mips_macro_warning.first_frag->fr_subtype |= subtype;
2976 }
2977 }
2978 }
2979
2980 /* Read a macro's relocation codes from *ARGS and store them in *R.
2981 The first argument in *ARGS will be either the code for a single
2982 relocation or -1 followed by the three codes that make up a
2983 composite relocation. */
2984
2985 static void
2986 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2987 {
2988 int i, next;
2989
2990 next = va_arg (*args, int);
2991 if (next >= 0)
2992 r[0] = (bfd_reloc_code_real_type) next;
2993 else
2994 for (i = 0; i < 3; i++)
2995 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2996 }
2997
2998 /* Build an instruction created by a macro expansion. This is passed
2999 a pointer to the count of instructions created so far, an
3000 expression, the name of the instruction to build, an operand format
3001 string, and corresponding arguments. */
3002
3003 static void
3004 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3005 {
3006 struct mips_cl_insn insn;
3007 bfd_reloc_code_real_type r[3];
3008 va_list args;
3009
3010 va_start (args, fmt);
3011
3012 if (mips_opts.mips16)
3013 {
3014 mips16_macro_build (ep, name, fmt, args);
3015 va_end (args);
3016 return;
3017 }
3018
3019 r[0] = BFD_RELOC_UNUSED;
3020 r[1] = BFD_RELOC_UNUSED;
3021 r[2] = BFD_RELOC_UNUSED;
3022 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3023 assert (insn.insn_mo);
3024 assert (strcmp (name, insn.insn_mo->name) == 0);
3025
3026 /* Search until we get a match for NAME. */
3027 while (1)
3028 {
3029 /* It is assumed here that macros will never generate
3030 MDMX or MIPS-3D instructions. */
3031 if (strcmp (fmt, insn.insn_mo->args) == 0
3032 && insn.insn_mo->pinfo != INSN_MACRO
3033 && OPCODE_IS_MEMBER (insn.insn_mo,
3034 (mips_opts.isa
3035 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3036 mips_opts.arch)
3037 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3038 break;
3039
3040 ++insn.insn_mo;
3041 assert (insn.insn_mo->name);
3042 assert (strcmp (name, insn.insn_mo->name) == 0);
3043 }
3044
3045 insn.insn_opcode = insn.insn_mo->match;
3046 for (;;)
3047 {
3048 switch (*fmt++)
3049 {
3050 case '\0':
3051 break;
3052
3053 case ',':
3054 case '(':
3055 case ')':
3056 continue;
3057
3058 case '+':
3059 switch (*fmt++)
3060 {
3061 case 'A':
3062 case 'E':
3063 insn.insn_opcode |= (va_arg (args, int)
3064 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3065 continue;
3066
3067 case 'B':
3068 case 'F':
3069 /* Note that in the macro case, these arguments are already
3070 in MSB form. (When handling the instruction in the
3071 non-macro case, these arguments are sizes from which
3072 MSB values must be calculated.) */
3073 insn.insn_opcode |= (va_arg (args, int)
3074 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3075 continue;
3076
3077 case 'C':
3078 case 'G':
3079 case 'H':
3080 /* Note that in the macro case, these arguments are already
3081 in MSBD form. (When handling the instruction in the
3082 non-macro case, these arguments are sizes from which
3083 MSBD values must be calculated.) */
3084 insn.insn_opcode |= (va_arg (args, int)
3085 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3086 continue;
3087
3088 default:
3089 internalError ();
3090 }
3091 continue;
3092
3093 case 't':
3094 case 'w':
3095 case 'E':
3096 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3097 continue;
3098
3099 case 'c':
3100 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3101 continue;
3102
3103 case 'T':
3104 case 'W':
3105 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3106 continue;
3107
3108 case 'd':
3109 case 'G':
3110 case 'K':
3111 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3112 continue;
3113
3114 case 'U':
3115 {
3116 int tmp = va_arg (args, int);
3117
3118 insn.insn_opcode |= tmp << OP_SH_RT;
3119 insn.insn_opcode |= tmp << OP_SH_RD;
3120 continue;
3121 }
3122
3123 case 'V':
3124 case 'S':
3125 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3126 continue;
3127
3128 case 'z':
3129 continue;
3130
3131 case '<':
3132 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3133 continue;
3134
3135 case 'D':
3136 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3137 continue;
3138
3139 case 'B':
3140 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3141 continue;
3142
3143 case 'J':
3144 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3145 continue;
3146
3147 case 'q':
3148 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3149 continue;
3150
3151 case 'b':
3152 case 's':
3153 case 'r':
3154 case 'v':
3155 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3156 continue;
3157
3158 case 'i':
3159 case 'j':
3160 case 'o':
3161 macro_read_relocs (&args, r);
3162 assert (*r == BFD_RELOC_GPREL16
3163 || *r == BFD_RELOC_MIPS_LITERAL
3164 || *r == BFD_RELOC_MIPS_HIGHER
3165 || *r == BFD_RELOC_HI16_S
3166 || *r == BFD_RELOC_LO16
3167 || *r == BFD_RELOC_MIPS_GOT16
3168 || *r == BFD_RELOC_MIPS_CALL16
3169 || *r == BFD_RELOC_MIPS_GOT_DISP
3170 || *r == BFD_RELOC_MIPS_GOT_PAGE
3171 || *r == BFD_RELOC_MIPS_GOT_OFST
3172 || *r == BFD_RELOC_MIPS_GOT_LO16
3173 || *r == BFD_RELOC_MIPS_CALL_LO16);
3174 continue;
3175
3176 case 'u':
3177 macro_read_relocs (&args, r);
3178 assert (ep != NULL
3179 && (ep->X_op == O_constant
3180 || (ep->X_op == O_symbol
3181 && (*r == BFD_RELOC_MIPS_HIGHEST
3182 || *r == BFD_RELOC_HI16_S
3183 || *r == BFD_RELOC_HI16
3184 || *r == BFD_RELOC_GPREL16
3185 || *r == BFD_RELOC_MIPS_GOT_HI16
3186 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3187 continue;
3188
3189 case 'p':
3190 assert (ep != NULL);
3191 /*
3192 * This allows macro() to pass an immediate expression for
3193 * creating short branches without creating a symbol.
3194 * Note that the expression still might come from the assembly
3195 * input, in which case the value is not checked for range nor
3196 * is a relocation entry generated (yuck).
3197 */
3198 if (ep->X_op == O_constant)
3199 {
3200 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3201 ep = NULL;
3202 }
3203 else
3204 *r = BFD_RELOC_16_PCREL_S2;
3205 continue;
3206
3207 case 'a':
3208 assert (ep != NULL);
3209 *r = BFD_RELOC_MIPS_JMP;
3210 continue;
3211
3212 case 'C':
3213 insn.insn_opcode |= va_arg (args, unsigned long);
3214 continue;
3215
3216 default:
3217 internalError ();
3218 }
3219 break;
3220 }
3221 va_end (args);
3222 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3223
3224 append_insn (&insn, ep, r);
3225 }
3226
3227 static void
3228 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3229 va_list args)
3230 {
3231 struct mips_cl_insn insn;
3232 bfd_reloc_code_real_type r[3]
3233 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3234
3235 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3236 assert (insn.insn_mo);
3237 assert (strcmp (name, insn.insn_mo->name) == 0);
3238
3239 while (strcmp (fmt, insn.insn_mo->args) != 0
3240 || insn.insn_mo->pinfo == INSN_MACRO)
3241 {
3242 ++insn.insn_mo;
3243 assert (insn.insn_mo->name);
3244 assert (strcmp (name, insn.insn_mo->name) == 0);
3245 }
3246
3247 insn.insn_opcode = insn.insn_mo->match;
3248 insn.use_extend = FALSE;
3249
3250 for (;;)
3251 {
3252 int c;
3253
3254 c = *fmt++;
3255 switch (c)
3256 {
3257 case '\0':
3258 break;
3259
3260 case ',':
3261 case '(':
3262 case ')':
3263 continue;
3264
3265 case 'y':
3266 case 'w':
3267 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3268 continue;
3269
3270 case 'x':
3271 case 'v':
3272 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3273 continue;
3274
3275 case 'z':
3276 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3277 continue;
3278
3279 case 'Z':
3280 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3281 continue;
3282
3283 case '0':
3284 case 'S':
3285 case 'P':
3286 case 'R':
3287 continue;
3288
3289 case 'X':
3290 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3291 continue;
3292
3293 case 'Y':
3294 {
3295 int regno;
3296
3297 regno = va_arg (args, int);
3298 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3299 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3300 }
3301 continue;
3302
3303 case '<':
3304 case '>':
3305 case '4':
3306 case '5':
3307 case 'H':
3308 case 'W':
3309 case 'D':
3310 case 'j':
3311 case '8':
3312 case 'V':
3313 case 'C':
3314 case 'U':
3315 case 'k':
3316 case 'K':
3317 case 'p':
3318 case 'q':
3319 {
3320 assert (ep != NULL);
3321
3322 if (ep->X_op != O_constant)
3323 *r = (int) BFD_RELOC_UNUSED + c;
3324 else
3325 {
3326 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3327 FALSE, &insn.insn_opcode, &insn.use_extend,
3328 &insn.extend);
3329 ep = NULL;
3330 *r = BFD_RELOC_UNUSED;
3331 }
3332 }
3333 continue;
3334
3335 case '6':
3336 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3337 continue;
3338 }
3339
3340 break;
3341 }
3342
3343 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3344
3345 append_insn (&insn, ep, r);
3346 }
3347
3348 /*
3349 * Generate a "jalr" instruction with a relocation hint to the called
3350 * function. This occurs in NewABI PIC code.
3351 */
3352 static void
3353 macro_build_jalr (expressionS *ep)
3354 {
3355 char *f = NULL;
3356
3357 if (HAVE_NEWABI)
3358 {
3359 frag_grow (8);
3360 f = frag_more (0);
3361 }
3362 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3363 if (HAVE_NEWABI)
3364 fix_new_exp (frag_now, f - frag_now->fr_literal,
3365 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3366 }
3367
3368 /*
3369 * Generate a "lui" instruction.
3370 */
3371 static void
3372 macro_build_lui (expressionS *ep, int regnum)
3373 {
3374 expressionS high_expr;
3375 struct mips_cl_insn insn;
3376 bfd_reloc_code_real_type r[3]
3377 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3378 const char *name = "lui";
3379 const char *fmt = "t,u";
3380
3381 assert (! mips_opts.mips16);
3382
3383 high_expr = *ep;
3384
3385 if (high_expr.X_op == O_constant)
3386 {
3387 /* we can compute the instruction now without a relocation entry */
3388 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3389 >> 16) & 0xffff;
3390 *r = BFD_RELOC_UNUSED;
3391 }
3392 else
3393 {
3394 assert (ep->X_op == O_symbol);
3395 /* _gp_disp is a special case, used from s_cpload. _gp is used
3396 if mips_no_shared. */
3397 assert (mips_pic == NO_PIC
3398 || (! HAVE_NEWABI
3399 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3400 || (! mips_in_shared
3401 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp") == 0));
3402 *r = BFD_RELOC_HI16_S;
3403 }
3404
3405 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3406 assert (insn.insn_mo);
3407 assert (strcmp (name, insn.insn_mo->name) == 0);
3408 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3409
3410 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3411 if (*r == BFD_RELOC_UNUSED)
3412 {
3413 insn.insn_opcode |= high_expr.X_add_number;
3414 append_insn (&insn, NULL, r);
3415 }
3416 else
3417 append_insn (&insn, &high_expr, r);
3418 }
3419
3420 /* Generate a sequence of instructions to do a load or store from a constant
3421 offset off of a base register (breg) into/from a target register (treg),
3422 using AT if necessary. */
3423 static void
3424 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3425 int treg, int breg, int dbl)
3426 {
3427 assert (ep->X_op == O_constant);
3428
3429 /* Sign-extending 32-bit constants makes their handling easier. */
3430 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3431 == ~((bfd_vma) 0x7fffffff)))
3432 {
3433 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3434 as_bad (_("constant too large"));
3435
3436 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3437 - 0x80000000);
3438 }
3439
3440 /* Right now, this routine can only handle signed 32-bit constants. */
3441 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3442 as_warn (_("operand overflow"));
3443
3444 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3445 {
3446 /* Signed 16-bit offset will fit in the op. Easy! */
3447 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3448 }
3449 else
3450 {
3451 /* 32-bit offset, need multiple instructions and AT, like:
3452 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3453 addu $tempreg,$tempreg,$breg
3454 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3455 to handle the complete offset. */
3456 macro_build_lui (ep, AT);
3457 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3458 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3459
3460 if (mips_opts.noat)
3461 as_warn (_("Macro used $at after \".set noat\""));
3462 }
3463 }
3464
3465 /* set_at()
3466 * Generates code to set the $at register to true (one)
3467 * if reg is less than the immediate expression.
3468 */
3469 static void
3470 set_at (int reg, int unsignedp)
3471 {
3472 if (imm_expr.X_op == O_constant
3473 && imm_expr.X_add_number >= -0x8000
3474 && imm_expr.X_add_number < 0x8000)
3475 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3476 AT, reg, BFD_RELOC_LO16);
3477 else
3478 {
3479 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3480 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3481 }
3482 }
3483
3484 static void
3485 normalize_constant_expr (expressionS *ex)
3486 {
3487 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3488 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3489 - 0x80000000);
3490 }
3491
3492 /* Warn if an expression is not a constant. */
3493
3494 static void
3495 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3496 {
3497 if (ex->X_op == O_big)
3498 as_bad (_("unsupported large constant"));
3499 else if (ex->X_op != O_constant)
3500 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3501
3502 normalize_constant_expr (ex);
3503 }
3504
3505 /* Count the leading zeroes by performing a binary chop. This is a
3506 bulky bit of source, but performance is a LOT better for the
3507 majority of values than a simple loop to count the bits:
3508 for (lcnt = 0; (lcnt < 32); lcnt++)
3509 if ((v) & (1 << (31 - lcnt)))
3510 break;
3511 However it is not code size friendly, and the gain will drop a bit
3512 on certain cached systems.
3513 */
3514 #define COUNT_TOP_ZEROES(v) \
3515 (((v) & ~0xffff) == 0 \
3516 ? ((v) & ~0xff) == 0 \
3517 ? ((v) & ~0xf) == 0 \
3518 ? ((v) & ~0x3) == 0 \
3519 ? ((v) & ~0x1) == 0 \
3520 ? !(v) \
3521 ? 32 \
3522 : 31 \
3523 : 30 \
3524 : ((v) & ~0x7) == 0 \
3525 ? 29 \
3526 : 28 \
3527 : ((v) & ~0x3f) == 0 \
3528 ? ((v) & ~0x1f) == 0 \
3529 ? 27 \
3530 : 26 \
3531 : ((v) & ~0x7f) == 0 \
3532 ? 25 \
3533 : 24 \
3534 : ((v) & ~0xfff) == 0 \
3535 ? ((v) & ~0x3ff) == 0 \
3536 ? ((v) & ~0x1ff) == 0 \
3537 ? 23 \
3538 : 22 \
3539 : ((v) & ~0x7ff) == 0 \
3540 ? 21 \
3541 : 20 \
3542 : ((v) & ~0x3fff) == 0 \
3543 ? ((v) & ~0x1fff) == 0 \
3544 ? 19 \
3545 : 18 \
3546 : ((v) & ~0x7fff) == 0 \
3547 ? 17 \
3548 : 16 \
3549 : ((v) & ~0xffffff) == 0 \
3550 ? ((v) & ~0xfffff) == 0 \
3551 ? ((v) & ~0x3ffff) == 0 \
3552 ? ((v) & ~0x1ffff) == 0 \
3553 ? 15 \
3554 : 14 \
3555 : ((v) & ~0x7ffff) == 0 \
3556 ? 13 \
3557 : 12 \
3558 : ((v) & ~0x3fffff) == 0 \
3559 ? ((v) & ~0x1fffff) == 0 \
3560 ? 11 \
3561 : 10 \
3562 : ((v) & ~0x7fffff) == 0 \
3563 ? 9 \
3564 : 8 \
3565 : ((v) & ~0xfffffff) == 0 \
3566 ? ((v) & ~0x3ffffff) == 0 \
3567 ? ((v) & ~0x1ffffff) == 0 \
3568 ? 7 \
3569 : 6 \
3570 : ((v) & ~0x7ffffff) == 0 \
3571 ? 5 \
3572 : 4 \
3573 : ((v) & ~0x3fffffff) == 0 \
3574 ? ((v) & ~0x1fffffff) == 0 \
3575 ? 3 \
3576 : 2 \
3577 : ((v) & ~0x7fffffff) == 0 \
3578 ? 1 \
3579 : 0)
3580
3581 /* load_register()
3582 * This routine generates the least number of instructions necessary to load
3583 * an absolute expression value into a register.
3584 */
3585 static void
3586 load_register (int reg, expressionS *ep, int dbl)
3587 {
3588 int freg;
3589 expressionS hi32, lo32;
3590
3591 if (ep->X_op != O_big)
3592 {
3593 assert (ep->X_op == O_constant);
3594
3595 /* Sign-extending 32-bit constants makes their handling easier. */
3596 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3597 == ~((bfd_vma) 0x7fffffff)))
3598 {
3599 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3600 as_bad (_("constant too large"));
3601
3602 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3603 - 0x80000000);
3604 }
3605
3606 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3607 {
3608 /* We can handle 16 bit signed values with an addiu to
3609 $zero. No need to ever use daddiu here, since $zero and
3610 the result are always correct in 32 bit mode. */
3611 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3612 return;
3613 }
3614 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3615 {
3616 /* We can handle 16 bit unsigned values with an ori to
3617 $zero. */
3618 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3619 return;
3620 }
3621 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3622 {
3623 /* 32 bit values require an lui. */
3624 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3625 if ((ep->X_add_number & 0xffff) != 0)
3626 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3627 return;
3628 }
3629 }
3630
3631 /* The value is larger than 32 bits. */
3632
3633 if (HAVE_32BIT_GPRS)
3634 {
3635 as_bad (_("Number (0x%lx) larger than 32 bits"),
3636 (unsigned long) ep->X_add_number);
3637 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3638 return;
3639 }
3640
3641 if (ep->X_op != O_big)
3642 {
3643 hi32 = *ep;
3644 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3645 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3646 hi32.X_add_number &= 0xffffffff;
3647 lo32 = *ep;
3648 lo32.X_add_number &= 0xffffffff;
3649 }
3650 else
3651 {
3652 assert (ep->X_add_number > 2);
3653 if (ep->X_add_number == 3)
3654 generic_bignum[3] = 0;
3655 else if (ep->X_add_number > 4)
3656 as_bad (_("Number larger than 64 bits"));
3657 lo32.X_op = O_constant;
3658 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3659 hi32.X_op = O_constant;
3660 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3661 }
3662
3663 if (hi32.X_add_number == 0)
3664 freg = 0;
3665 else
3666 {
3667 int shift, bit;
3668 unsigned long hi, lo;
3669
3670 if (hi32.X_add_number == (offsetT) 0xffffffff)
3671 {
3672 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3673 {
3674 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3675 return;
3676 }
3677 if (lo32.X_add_number & 0x80000000)
3678 {
3679 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3680 if (lo32.X_add_number & 0xffff)
3681 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3682 return;
3683 }
3684 }
3685
3686 /* Check for 16bit shifted constant. We know that hi32 is
3687 non-zero, so start the mask on the first bit of the hi32
3688 value. */
3689 shift = 17;
3690 do
3691 {
3692 unsigned long himask, lomask;
3693
3694 if (shift < 32)
3695 {
3696 himask = 0xffff >> (32 - shift);
3697 lomask = (0xffff << shift) & 0xffffffff;
3698 }
3699 else
3700 {
3701 himask = 0xffff << (shift - 32);
3702 lomask = 0;
3703 }
3704 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3705 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3706 {
3707 expressionS tmp;
3708
3709 tmp.X_op = O_constant;
3710 if (shift < 32)
3711 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3712 | (lo32.X_add_number >> shift));
3713 else
3714 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3715 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3716 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3717 reg, reg, (shift >= 32) ? shift - 32 : shift);
3718 return;
3719 }
3720 ++shift;
3721 }
3722 while (shift <= (64 - 16));
3723
3724 /* Find the bit number of the lowest one bit, and store the
3725 shifted value in hi/lo. */
3726 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3727 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3728 if (lo != 0)
3729 {
3730 bit = 0;
3731 while ((lo & 1) == 0)
3732 {
3733 lo >>= 1;
3734 ++bit;
3735 }
3736 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3737 hi >>= bit;
3738 }
3739 else
3740 {
3741 bit = 32;
3742 while ((hi & 1) == 0)
3743 {
3744 hi >>= 1;
3745 ++bit;
3746 }
3747 lo = hi;
3748 hi = 0;
3749 }
3750
3751 /* Optimize if the shifted value is a (power of 2) - 1. */
3752 if ((hi == 0 && ((lo + 1) & lo) == 0)
3753 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3754 {
3755 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3756 if (shift != 0)
3757 {
3758 expressionS tmp;
3759
3760 /* This instruction will set the register to be all
3761 ones. */
3762 tmp.X_op = O_constant;
3763 tmp.X_add_number = (offsetT) -1;
3764 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3765 if (bit != 0)
3766 {
3767 bit += shift;
3768 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3769 reg, reg, (bit >= 32) ? bit - 32 : bit);
3770 }
3771 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3772 reg, reg, (shift >= 32) ? shift - 32 : shift);
3773 return;
3774 }
3775 }
3776
3777 /* Sign extend hi32 before calling load_register, because we can
3778 generally get better code when we load a sign extended value. */
3779 if ((hi32.X_add_number & 0x80000000) != 0)
3780 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3781 load_register (reg, &hi32, 0);
3782 freg = reg;
3783 }
3784 if ((lo32.X_add_number & 0xffff0000) == 0)
3785 {
3786 if (freg != 0)
3787 {
3788 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3789 freg = reg;
3790 }
3791 }
3792 else
3793 {
3794 expressionS mid16;
3795
3796 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3797 {
3798 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3799 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3800 return;
3801 }
3802
3803 if (freg != 0)
3804 {
3805 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3806 freg = reg;
3807 }
3808 mid16 = lo32;
3809 mid16.X_add_number >>= 16;
3810 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3811 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3812 freg = reg;
3813 }
3814 if ((lo32.X_add_number & 0xffff) != 0)
3815 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3816 }
3817
3818 static inline void
3819 load_delay_nop (void)
3820 {
3821 if (!gpr_interlocks)
3822 macro_build (NULL, "nop", "");
3823 }
3824
3825 /* Load an address into a register. */
3826
3827 static void
3828 load_address (int reg, expressionS *ep, int *used_at)
3829 {
3830 if (ep->X_op != O_constant
3831 && ep->X_op != O_symbol)
3832 {
3833 as_bad (_("expression too complex"));
3834 ep->X_op = O_constant;
3835 }
3836
3837 if (ep->X_op == O_constant)
3838 {
3839 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3840 return;
3841 }
3842
3843 if (mips_pic == NO_PIC)
3844 {
3845 /* If this is a reference to a GP relative symbol, we want
3846 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3847 Otherwise we want
3848 lui $reg,<sym> (BFD_RELOC_HI16_S)
3849 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3850 If we have an addend, we always use the latter form.
3851
3852 With 64bit address space and a usable $at we want
3853 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3854 lui $at,<sym> (BFD_RELOC_HI16_S)
3855 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3856 daddiu $at,<sym> (BFD_RELOC_LO16)
3857 dsll32 $reg,0
3858 daddu $reg,$reg,$at
3859
3860 If $at is already in use, we use a path which is suboptimal
3861 on superscalar processors.
3862 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3863 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3864 dsll $reg,16
3865 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3866 dsll $reg,16
3867 daddiu $reg,<sym> (BFD_RELOC_LO16)
3868 */
3869 if (HAVE_64BIT_ADDRESSES)
3870 {
3871 /* ??? We don't provide a GP-relative alternative for these macros.
3872 It used not to be possible with the original relaxation code,
3873 but it could be done now. */
3874
3875 if (*used_at == 0 && ! mips_opts.noat)
3876 {
3877 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3878 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3879 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3880 BFD_RELOC_MIPS_HIGHER);
3881 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3882 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3883 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3884 *used_at = 1;
3885 }
3886 else
3887 {
3888 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3889 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3890 BFD_RELOC_MIPS_HIGHER);
3891 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3892 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3893 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3894 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3895 }
3896 }
3897 else
3898 {
3899 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3900 && ! nopic_need_relax (ep->X_add_symbol, 1))
3901 {
3902 relax_start (ep->X_add_symbol);
3903 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3904 mips_gp_register, BFD_RELOC_GPREL16);
3905 relax_switch ();
3906 }
3907 macro_build_lui (ep, reg);
3908 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3909 reg, reg, BFD_RELOC_LO16);
3910 if (mips_relax.sequence)
3911 relax_end ();
3912 }
3913 }
3914 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3915 {
3916 expressionS ex;
3917
3918 /* If this is a reference to an external symbol, we want
3919 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3920 Otherwise we want
3921 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3922 nop
3923 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3924 If there is a constant, it must be added in after.
3925
3926 If we have NewABI, we want
3927 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3928 unless we're referencing a global symbol with a non-zero
3929 offset, in which case cst must be added separately. */
3930 if (HAVE_NEWABI)
3931 {
3932 if (ep->X_add_number)
3933 {
3934 ex.X_add_number = ep->X_add_number;
3935 ep->X_add_number = 0;
3936 relax_start (ep->X_add_symbol);
3937 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3938 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3939 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941 ex.X_op = O_constant;
3942 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3943 reg, reg, BFD_RELOC_LO16);
3944 ep->X_add_number = ex.X_add_number;
3945 relax_switch ();
3946 }
3947 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3948 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3949 if (mips_relax.sequence)
3950 relax_end ();
3951 }
3952 else
3953 {
3954 ex.X_add_number = ep->X_add_number;
3955 ep->X_add_number = 0;
3956 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3957 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3958 load_delay_nop ();
3959 relax_start (ep->X_add_symbol);
3960 relax_switch ();
3961 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3962 BFD_RELOC_LO16);
3963 relax_end ();
3964
3965 if (ex.X_add_number != 0)
3966 {
3967 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3968 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3969 ex.X_op = O_constant;
3970 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3971 reg, reg, BFD_RELOC_LO16);
3972 }
3973 }
3974 }
3975 else if (mips_pic == SVR4_PIC)
3976 {
3977 expressionS ex;
3978
3979 /* This is the large GOT case. If this is a reference to an
3980 external symbol, we want
3981 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3982 addu $reg,$reg,$gp
3983 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3984
3985 Otherwise, for a reference to a local symbol in old ABI, we want
3986 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3987 nop
3988 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3989 If there is a constant, it must be added in after.
3990
3991 In the NewABI, for local symbols, with or without offsets, we want:
3992 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3993 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3994 */
3995 if (HAVE_NEWABI)
3996 {
3997 ex.X_add_number = ep->X_add_number;
3998 ep->X_add_number = 0;
3999 relax_start (ep->X_add_symbol);
4000 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4001 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4002 reg, reg, mips_gp_register);
4003 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4004 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4005 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4006 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4007 else if (ex.X_add_number)
4008 {
4009 ex.X_op = O_constant;
4010 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4011 BFD_RELOC_LO16);
4012 }
4013
4014 ep->X_add_number = ex.X_add_number;
4015 relax_switch ();
4016 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4017 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4018 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4019 BFD_RELOC_MIPS_GOT_OFST);
4020 relax_end ();
4021 }
4022 else
4023 {
4024 ex.X_add_number = ep->X_add_number;
4025 ep->X_add_number = 0;
4026 relax_start (ep->X_add_symbol);
4027 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4028 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4029 reg, reg, mips_gp_register);
4030 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4031 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4032 relax_switch ();
4033 if (reg_needs_delay (mips_gp_register))
4034 {
4035 /* We need a nop before loading from $gp. This special
4036 check is required because the lui which starts the main
4037 instruction stream does not refer to $gp, and so will not
4038 insert the nop which may be required. */
4039 macro_build (NULL, "nop", "");
4040 }
4041 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4042 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4043 load_delay_nop ();
4044 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4045 BFD_RELOC_LO16);
4046 relax_end ();
4047
4048 if (ex.X_add_number != 0)
4049 {
4050 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4051 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4052 ex.X_op = O_constant;
4053 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4054 BFD_RELOC_LO16);
4055 }
4056 }
4057 }
4058 else
4059 abort ();
4060 }
4061
4062 /* Move the contents of register SOURCE into register DEST. */
4063
4064 static void
4065 move_register (int dest, int source)
4066 {
4067 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4068 dest, source, 0);
4069 }
4070
4071 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4072 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4073 The two alternatives are:
4074
4075 Global symbol Local sybmol
4076 ------------- ------------
4077 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4078 ... ...
4079 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4080
4081 load_got_offset emits the first instruction and add_got_offset
4082 emits the second for a 16-bit offset or add_got_offset_hilo emits
4083 a sequence to add a 32-bit offset using a scratch register. */
4084
4085 static void
4086 load_got_offset (int dest, expressionS *local)
4087 {
4088 expressionS global;
4089
4090 global = *local;
4091 global.X_add_number = 0;
4092
4093 relax_start (local->X_add_symbol);
4094 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4095 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4096 relax_switch ();
4097 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4098 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4099 relax_end ();
4100 }
4101
4102 static void
4103 add_got_offset (int dest, expressionS *local)
4104 {
4105 expressionS global;
4106
4107 global.X_op = O_constant;
4108 global.X_op_symbol = NULL;
4109 global.X_add_symbol = NULL;
4110 global.X_add_number = local->X_add_number;
4111
4112 relax_start (local->X_add_symbol);
4113 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4114 dest, dest, BFD_RELOC_LO16);
4115 relax_switch ();
4116 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4117 relax_end ();
4118 }
4119
4120 static void
4121 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4122 {
4123 expressionS global;
4124 int hold_mips_optimize;
4125
4126 global.X_op = O_constant;
4127 global.X_op_symbol = NULL;
4128 global.X_add_symbol = NULL;
4129 global.X_add_number = local->X_add_number;
4130
4131 relax_start (local->X_add_symbol);
4132 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4133 relax_switch ();
4134 /* Set mips_optimize around the lui instruction to avoid
4135 inserting an unnecessary nop after the lw. */
4136 hold_mips_optimize = mips_optimize;
4137 mips_optimize = 2;
4138 macro_build_lui (&global, tmp);
4139 mips_optimize = hold_mips_optimize;
4140 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4141 relax_end ();
4142
4143 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4144 }
4145
4146 /*
4147 * Build macros
4148 * This routine implements the seemingly endless macro or synthesized
4149 * instructions and addressing modes in the mips assembly language. Many
4150 * of these macros are simple and are similar to each other. These could
4151 * probably be handled by some kind of table or grammar approach instead of
4152 * this verbose method. Others are not simple macros but are more like
4153 * optimizing code generation.
4154 * One interesting optimization is when several store macros appear
4155 * consecutively that would load AT with the upper half of the same address.
4156 * The ensuing load upper instructions are ommited. This implies some kind
4157 * of global optimization. We currently only optimize within a single macro.
4158 * For many of the load and store macros if the address is specified as a
4159 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4160 * first load register 'at' with zero and use it as the base register. The
4161 * mips assembler simply uses register $zero. Just one tiny optimization
4162 * we're missing.
4163 */
4164 static void
4165 macro (struct mips_cl_insn *ip)
4166 {
4167 register int treg, sreg, dreg, breg;
4168 int tempreg;
4169 int mask;
4170 int used_at = 0;
4171 expressionS expr1;
4172 const char *s;
4173 const char *s2;
4174 const char *fmt;
4175 int likely = 0;
4176 int dbl = 0;
4177 int coproc = 0;
4178 int lr = 0;
4179 int imm = 0;
4180 int call = 0;
4181 int off;
4182 offsetT maxnum;
4183 bfd_reloc_code_real_type r;
4184 int hold_mips_optimize;
4185
4186 assert (! mips_opts.mips16);
4187
4188 treg = (ip->insn_opcode >> 16) & 0x1f;
4189 dreg = (ip->insn_opcode >> 11) & 0x1f;
4190 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4191 mask = ip->insn_mo->mask;
4192
4193 expr1.X_op = O_constant;
4194 expr1.X_op_symbol = NULL;
4195 expr1.X_add_symbol = NULL;
4196 expr1.X_add_number = 1;
4197
4198 switch (mask)
4199 {
4200 case M_DABS:
4201 dbl = 1;
4202 case M_ABS:
4203 /* bgez $a0,.+12
4204 move v0,$a0
4205 sub v0,$zero,$a0
4206 */
4207
4208 mips_emit_delays (TRUE);
4209 ++mips_opts.noreorder;
4210 mips_any_noreorder = 1;
4211
4212 expr1.X_add_number = 8;
4213 macro_build (&expr1, "bgez", "s,p", sreg);
4214 if (dreg == sreg)
4215 macro_build (NULL, "nop", "", 0);
4216 else
4217 move_register (dreg, sreg);
4218 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4219
4220 --mips_opts.noreorder;
4221 return;
4222
4223 case M_ADD_I:
4224 s = "addi";
4225 s2 = "add";
4226 goto do_addi;
4227 case M_ADDU_I:
4228 s = "addiu";
4229 s2 = "addu";
4230 goto do_addi;
4231 case M_DADD_I:
4232 dbl = 1;
4233 s = "daddi";
4234 s2 = "dadd";
4235 goto do_addi;
4236 case M_DADDU_I:
4237 dbl = 1;
4238 s = "daddiu";
4239 s2 = "daddu";
4240 do_addi:
4241 if (imm_expr.X_op == O_constant
4242 && imm_expr.X_add_number >= -0x8000
4243 && imm_expr.X_add_number < 0x8000)
4244 {
4245 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4246 return;
4247 }
4248 load_register (AT, &imm_expr, dbl);
4249 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4250 break;
4251
4252 case M_AND_I:
4253 s = "andi";
4254 s2 = "and";
4255 goto do_bit;
4256 case M_OR_I:
4257 s = "ori";
4258 s2 = "or";
4259 goto do_bit;
4260 case M_NOR_I:
4261 s = "";
4262 s2 = "nor";
4263 goto do_bit;
4264 case M_XOR_I:
4265 s = "xori";
4266 s2 = "xor";
4267 do_bit:
4268 if (imm_expr.X_op == O_constant
4269 && imm_expr.X_add_number >= 0
4270 && imm_expr.X_add_number < 0x10000)
4271 {
4272 if (mask != M_NOR_I)
4273 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4274 else
4275 {
4276 macro_build (&imm_expr, "ori", "t,r,i",
4277 treg, sreg, BFD_RELOC_LO16);
4278 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4279 }
4280 return;
4281 }
4282
4283 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4284 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4285 break;
4286
4287 case M_BEQ_I:
4288 s = "beq";
4289 goto beq_i;
4290 case M_BEQL_I:
4291 s = "beql";
4292 likely = 1;
4293 goto beq_i;
4294 case M_BNE_I:
4295 s = "bne";
4296 goto beq_i;
4297 case M_BNEL_I:
4298 s = "bnel";
4299 likely = 1;
4300 beq_i:
4301 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4302 {
4303 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4304 return;
4305 }
4306 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4307 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4308 break;
4309
4310 case M_BGEL:
4311 likely = 1;
4312 case M_BGE:
4313 if (treg == 0)
4314 {
4315 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4316 return;
4317 }
4318 if (sreg == 0)
4319 {
4320 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4321 return;
4322 }
4323 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4324 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4325 break;
4326
4327 case M_BGTL_I:
4328 likely = 1;
4329 case M_BGT_I:
4330 /* check for > max integer */
4331 maxnum = 0x7fffffff;
4332 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4333 {
4334 maxnum <<= 16;
4335 maxnum |= 0xffff;
4336 maxnum <<= 16;
4337 maxnum |= 0xffff;
4338 }
4339 if (imm_expr.X_op == O_constant
4340 && imm_expr.X_add_number >= maxnum
4341 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4342 {
4343 do_false:
4344 /* result is always false */
4345 if (! likely)
4346 macro_build (NULL, "nop", "", 0);
4347 else
4348 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4349 return;
4350 }
4351 if (imm_expr.X_op != O_constant)
4352 as_bad (_("Unsupported large constant"));
4353 ++imm_expr.X_add_number;
4354 /* FALLTHROUGH */
4355 case M_BGE_I:
4356 case M_BGEL_I:
4357 if (mask == M_BGEL_I)
4358 likely = 1;
4359 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4360 {
4361 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4362 return;
4363 }
4364 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4365 {
4366 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4367 return;
4368 }
4369 maxnum = 0x7fffffff;
4370 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4371 {
4372 maxnum <<= 16;
4373 maxnum |= 0xffff;
4374 maxnum <<= 16;
4375 maxnum |= 0xffff;
4376 }
4377 maxnum = - maxnum - 1;
4378 if (imm_expr.X_op == O_constant
4379 && imm_expr.X_add_number <= maxnum
4380 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4381 {
4382 do_true:
4383 /* result is always true */
4384 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4385 macro_build (&offset_expr, "b", "p");
4386 return;
4387 }
4388 set_at (sreg, 0);
4389 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4390 break;
4391
4392 case M_BGEUL:
4393 likely = 1;
4394 case M_BGEU:
4395 if (treg == 0)
4396 goto do_true;
4397 if (sreg == 0)
4398 {
4399 macro_build (&offset_expr, likely ? "beql" : "beq",
4400 "s,t,p", 0, treg);
4401 return;
4402 }
4403 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4404 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4405 break;
4406
4407 case M_BGTUL_I:
4408 likely = 1;
4409 case M_BGTU_I:
4410 if (sreg == 0
4411 || (HAVE_32BIT_GPRS
4412 && imm_expr.X_op == O_constant
4413 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4414 goto do_false;
4415 if (imm_expr.X_op != O_constant)
4416 as_bad (_("Unsupported large constant"));
4417 ++imm_expr.X_add_number;
4418 /* FALLTHROUGH */
4419 case M_BGEU_I:
4420 case M_BGEUL_I:
4421 if (mask == M_BGEUL_I)
4422 likely = 1;
4423 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4424 goto do_true;
4425 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4426 {
4427 macro_build (&offset_expr, likely ? "bnel" : "bne",
4428 "s,t,p", sreg, 0);
4429 return;
4430 }
4431 set_at (sreg, 1);
4432 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4433 break;
4434
4435 case M_BGTL:
4436 likely = 1;
4437 case M_BGT:
4438 if (treg == 0)
4439 {
4440 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4441 return;
4442 }
4443 if (sreg == 0)
4444 {
4445 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4446 return;
4447 }
4448 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4449 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4450 break;
4451
4452 case M_BGTUL:
4453 likely = 1;
4454 case M_BGTU:
4455 if (treg == 0)
4456 {
4457 macro_build (&offset_expr, likely ? "bnel" : "bne",
4458 "s,t,p", sreg, 0);
4459 return;
4460 }
4461 if (sreg == 0)
4462 goto do_false;
4463 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4464 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4465 break;
4466
4467 case M_BLEL:
4468 likely = 1;
4469 case M_BLE:
4470 if (treg == 0)
4471 {
4472 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4473 return;
4474 }
4475 if (sreg == 0)
4476 {
4477 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4478 return;
4479 }
4480 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4481 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4482 break;
4483
4484 case M_BLEL_I:
4485 likely = 1;
4486 case M_BLE_I:
4487 maxnum = 0x7fffffff;
4488 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4489 {
4490 maxnum <<= 16;
4491 maxnum |= 0xffff;
4492 maxnum <<= 16;
4493 maxnum |= 0xffff;
4494 }
4495 if (imm_expr.X_op == O_constant
4496 && imm_expr.X_add_number >= maxnum
4497 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4498 goto do_true;
4499 if (imm_expr.X_op != O_constant)
4500 as_bad (_("Unsupported large constant"));
4501 ++imm_expr.X_add_number;
4502 /* FALLTHROUGH */
4503 case M_BLT_I:
4504 case M_BLTL_I:
4505 if (mask == M_BLTL_I)
4506 likely = 1;
4507 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4508 {
4509 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4510 return;
4511 }
4512 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4513 {
4514 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4515 return;
4516 }
4517 set_at (sreg, 0);
4518 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4519 break;
4520
4521 case M_BLEUL:
4522 likely = 1;
4523 case M_BLEU:
4524 if (treg == 0)
4525 {
4526 macro_build (&offset_expr, likely ? "beql" : "beq",
4527 "s,t,p", sreg, 0);
4528 return;
4529 }
4530 if (sreg == 0)
4531 goto do_true;
4532 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4533 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4534 break;
4535
4536 case M_BLEUL_I:
4537 likely = 1;
4538 case M_BLEU_I:
4539 if (sreg == 0
4540 || (HAVE_32BIT_GPRS
4541 && imm_expr.X_op == O_constant
4542 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4543 goto do_true;
4544 if (imm_expr.X_op != O_constant)
4545 as_bad (_("Unsupported large constant"));
4546 ++imm_expr.X_add_number;
4547 /* FALLTHROUGH */
4548 case M_BLTU_I:
4549 case M_BLTUL_I:
4550 if (mask == M_BLTUL_I)
4551 likely = 1;
4552 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4553 goto do_false;
4554 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4555 {
4556 macro_build (&offset_expr, likely ? "beql" : "beq",
4557 "s,t,p", sreg, 0);
4558 return;
4559 }
4560 set_at (sreg, 1);
4561 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4562 break;
4563
4564 case M_BLTL:
4565 likely = 1;
4566 case M_BLT:
4567 if (treg == 0)
4568 {
4569 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4570 return;
4571 }
4572 if (sreg == 0)
4573 {
4574 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4575 return;
4576 }
4577 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4578 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4579 break;
4580
4581 case M_BLTUL:
4582 likely = 1;
4583 case M_BLTU:
4584 if (treg == 0)
4585 goto do_false;
4586 if (sreg == 0)
4587 {
4588 macro_build (&offset_expr, likely ? "bnel" : "bne",
4589 "s,t,p", 0, treg);
4590 return;
4591 }
4592 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4593 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4594 break;
4595
4596 case M_DEXT:
4597 {
4598 unsigned long pos;
4599 unsigned long size;
4600
4601 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4602 {
4603 as_bad (_("Unsupported large constant"));
4604 pos = size = 1;
4605 }
4606 else
4607 {
4608 pos = (unsigned long) imm_expr.X_add_number;
4609 size = (unsigned long) imm2_expr.X_add_number;
4610 }
4611
4612 if (pos > 63)
4613 {
4614 as_bad (_("Improper position (%lu)"), pos);
4615 pos = 1;
4616 }
4617 if (size == 0 || size > 64
4618 || (pos + size - 1) > 63)
4619 {
4620 as_bad (_("Improper extract size (%lu, position %lu)"),
4621 size, pos);
4622 size = 1;
4623 }
4624
4625 if (size <= 32 && pos < 32)
4626 {
4627 s = "dext";
4628 fmt = "t,r,+A,+C";
4629 }
4630 else if (size <= 32)
4631 {
4632 s = "dextu";
4633 fmt = "t,r,+E,+H";
4634 }
4635 else
4636 {
4637 s = "dextm";
4638 fmt = "t,r,+A,+G";
4639 }
4640 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4641 }
4642 return;
4643
4644 case M_DINS:
4645 {
4646 unsigned long pos;
4647 unsigned long size;
4648
4649 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4650 {
4651 as_bad (_("Unsupported large constant"));
4652 pos = size = 1;
4653 }
4654 else
4655 {
4656 pos = (unsigned long) imm_expr.X_add_number;
4657 size = (unsigned long) imm2_expr.X_add_number;
4658 }
4659
4660 if (pos > 63)
4661 {
4662 as_bad (_("Improper position (%lu)"), pos);
4663 pos = 1;
4664 }
4665 if (size == 0 || size > 64
4666 || (pos + size - 1) > 63)
4667 {
4668 as_bad (_("Improper insert size (%lu, position %lu)"),
4669 size, pos);
4670 size = 1;
4671 }
4672
4673 if (pos < 32 && (pos + size - 1) < 32)
4674 {
4675 s = "dins";
4676 fmt = "t,r,+A,+B";
4677 }
4678 else if (pos >= 32)
4679 {
4680 s = "dinsu";
4681 fmt = "t,r,+E,+F";
4682 }
4683 else
4684 {
4685 s = "dinsm";
4686 fmt = "t,r,+A,+F";
4687 }
4688 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4689 pos + size - 1);
4690 }
4691 return;
4692
4693 case M_DDIV_3:
4694 dbl = 1;
4695 case M_DIV_3:
4696 s = "mflo";
4697 goto do_div3;
4698 case M_DREM_3:
4699 dbl = 1;
4700 case M_REM_3:
4701 s = "mfhi";
4702 do_div3:
4703 if (treg == 0)
4704 {
4705 as_warn (_("Divide by zero."));
4706 if (mips_trap)
4707 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4708 else
4709 macro_build (NULL, "break", "c", 7);
4710 return;
4711 }
4712
4713 mips_emit_delays (TRUE);
4714 ++mips_opts.noreorder;
4715 mips_any_noreorder = 1;
4716 if (mips_trap)
4717 {
4718 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4719 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4720 }
4721 else
4722 {
4723 expr1.X_add_number = 8;
4724 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4725 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4726 macro_build (NULL, "break", "c", 7);
4727 }
4728 expr1.X_add_number = -1;
4729 load_register (AT, &expr1, dbl);
4730 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4731 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4732 if (dbl)
4733 {
4734 expr1.X_add_number = 1;
4735 load_register (AT, &expr1, dbl);
4736 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4737 }
4738 else
4739 {
4740 expr1.X_add_number = 0x80000000;
4741 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4742 }
4743 if (mips_trap)
4744 {
4745 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4746 /* We want to close the noreorder block as soon as possible, so
4747 that later insns are available for delay slot filling. */
4748 --mips_opts.noreorder;
4749 }
4750 else
4751 {
4752 expr1.X_add_number = 8;
4753 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4754 macro_build (NULL, "nop", "", 0);
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 --mips_opts.noreorder;
4759
4760 macro_build (NULL, "break", "c", 6);
4761 }
4762 macro_build (NULL, s, "d", dreg);
4763 break;
4764
4765 case M_DIV_3I:
4766 s = "div";
4767 s2 = "mflo";
4768 goto do_divi;
4769 case M_DIVU_3I:
4770 s = "divu";
4771 s2 = "mflo";
4772 goto do_divi;
4773 case M_REM_3I:
4774 s = "div";
4775 s2 = "mfhi";
4776 goto do_divi;
4777 case M_REMU_3I:
4778 s = "divu";
4779 s2 = "mfhi";
4780 goto do_divi;
4781 case M_DDIV_3I:
4782 dbl = 1;
4783 s = "ddiv";
4784 s2 = "mflo";
4785 goto do_divi;
4786 case M_DDIVU_3I:
4787 dbl = 1;
4788 s = "ddivu";
4789 s2 = "mflo";
4790 goto do_divi;
4791 case M_DREM_3I:
4792 dbl = 1;
4793 s = "ddiv";
4794 s2 = "mfhi";
4795 goto do_divi;
4796 case M_DREMU_3I:
4797 dbl = 1;
4798 s = "ddivu";
4799 s2 = "mfhi";
4800 do_divi:
4801 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4802 {
4803 as_warn (_("Divide by zero."));
4804 if (mips_trap)
4805 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4806 else
4807 macro_build (NULL, "break", "c", 7);
4808 return;
4809 }
4810 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4811 {
4812 if (strcmp (s2, "mflo") == 0)
4813 move_register (dreg, sreg);
4814 else
4815 move_register (dreg, 0);
4816 return;
4817 }
4818 if (imm_expr.X_op == O_constant
4819 && imm_expr.X_add_number == -1
4820 && s[strlen (s) - 1] != 'u')
4821 {
4822 if (strcmp (s2, "mflo") == 0)
4823 {
4824 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4825 }
4826 else
4827 move_register (dreg, 0);
4828 return;
4829 }
4830
4831 load_register (AT, &imm_expr, dbl);
4832 macro_build (NULL, s, "z,s,t", sreg, AT);
4833 macro_build (NULL, s2, "d", dreg);
4834 break;
4835
4836 case M_DIVU_3:
4837 s = "divu";
4838 s2 = "mflo";
4839 goto do_divu3;
4840 case M_REMU_3:
4841 s = "divu";
4842 s2 = "mfhi";
4843 goto do_divu3;
4844 case M_DDIVU_3:
4845 s = "ddivu";
4846 s2 = "mflo";
4847 goto do_divu3;
4848 case M_DREMU_3:
4849 s = "ddivu";
4850 s2 = "mfhi";
4851 do_divu3:
4852 mips_emit_delays (TRUE);
4853 ++mips_opts.noreorder;
4854 mips_any_noreorder = 1;
4855 if (mips_trap)
4856 {
4857 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4858 macro_build (NULL, s, "z,s,t", sreg, treg);
4859 /* We want to close the noreorder block as soon as possible, so
4860 that later insns are available for delay slot filling. */
4861 --mips_opts.noreorder;
4862 }
4863 else
4864 {
4865 expr1.X_add_number = 8;
4866 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4867 macro_build (NULL, s, "z,s,t", sreg, treg);
4868
4869 /* We want to close the noreorder block as soon as possible, so
4870 that later insns are available for delay slot filling. */
4871 --mips_opts.noreorder;
4872 macro_build (NULL, "break", "c", 7);
4873 }
4874 macro_build (NULL, s2, "d", dreg);
4875 return;
4876
4877 case M_DLCA_AB:
4878 dbl = 1;
4879 case M_LCA_AB:
4880 call = 1;
4881 goto do_la;
4882 case M_DLA_AB:
4883 dbl = 1;
4884 case M_LA_AB:
4885 do_la:
4886 /* Load the address of a symbol into a register. If breg is not
4887 zero, we then add a base register to it. */
4888
4889 if (dbl && HAVE_32BIT_GPRS)
4890 as_warn (_("dla used to load 32-bit register"));
4891
4892 if (! dbl && HAVE_64BIT_OBJECTS)
4893 as_warn (_("la used to load 64-bit address"));
4894
4895 if (offset_expr.X_op == O_constant
4896 && offset_expr.X_add_number >= -0x8000
4897 && offset_expr.X_add_number < 0x8000)
4898 {
4899 macro_build (&offset_expr,
4900 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4901 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4902 return;
4903 }
4904
4905 if (treg == breg)
4906 {
4907 tempreg = AT;
4908 used_at = 1;
4909 }
4910 else
4911 {
4912 tempreg = treg;
4913 used_at = 0;
4914 }
4915
4916 if (offset_expr.X_op != O_symbol
4917 && offset_expr.X_op != O_constant)
4918 {
4919 as_bad (_("expression too complex"));
4920 offset_expr.X_op = O_constant;
4921 }
4922
4923 if (offset_expr.X_op == O_constant)
4924 load_register (tempreg, &offset_expr,
4925 (mips_pic == NO_PIC
4926 ? (dbl || HAVE_64BIT_ADDRESSES)
4927 : HAVE_64BIT_ADDRESSES));
4928 else if (mips_pic == NO_PIC)
4929 {
4930 /* If this is a reference to a GP relative symbol, we want
4931 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4932 Otherwise we want
4933 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4934 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4935 If we have a constant, we need two instructions anyhow,
4936 so we may as well always use the latter form.
4937
4938 With 64bit address space and a usable $at we want
4939 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4940 lui $at,<sym> (BFD_RELOC_HI16_S)
4941 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4942 daddiu $at,<sym> (BFD_RELOC_LO16)
4943 dsll32 $tempreg,0
4944 daddu $tempreg,$tempreg,$at
4945
4946 If $at is already in use, we use a path which is suboptimal
4947 on superscalar processors.
4948 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4949 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4950 dsll $tempreg,16
4951 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4952 dsll $tempreg,16
4953 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4954 */
4955 if (HAVE_64BIT_ADDRESSES)
4956 {
4957 /* ??? We don't provide a GP-relative alternative for
4958 these macros. It used not to be possible with the
4959 original relaxation code, but it could be done now. */
4960
4961 if (used_at == 0 && ! mips_opts.noat)
4962 {
4963 macro_build (&offset_expr, "lui", "t,u",
4964 tempreg, BFD_RELOC_MIPS_HIGHEST);
4965 macro_build (&offset_expr, "lui", "t,u",
4966 AT, BFD_RELOC_HI16_S);
4967 macro_build (&offset_expr, "daddiu", "t,r,j",
4968 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4969 macro_build (&offset_expr, "daddiu", "t,r,j",
4970 AT, AT, BFD_RELOC_LO16);
4971 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4972 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4973 used_at = 1;
4974 }
4975 else
4976 {
4977 macro_build (&offset_expr, "lui", "t,u",
4978 tempreg, BFD_RELOC_MIPS_HIGHEST);
4979 macro_build (&offset_expr, "daddiu", "t,r,j",
4980 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4981 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4982 macro_build (&offset_expr, "daddiu", "t,r,j",
4983 tempreg, tempreg, BFD_RELOC_HI16_S);
4984 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4985 macro_build (&offset_expr, "daddiu", "t,r,j",
4986 tempreg, tempreg, BFD_RELOC_LO16);
4987 }
4988 }
4989 else
4990 {
4991 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4992 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4993 {
4994 relax_start (offset_expr.X_add_symbol);
4995 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4996 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4997 relax_switch ();
4998 }
4999 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5000 as_bad (_("offset too large"));
5001 macro_build_lui (&offset_expr, tempreg);
5002 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5003 tempreg, tempreg, BFD_RELOC_LO16);
5004 if (mips_relax.sequence)
5005 relax_end ();
5006 }
5007 }
5008 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5009 {
5010 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5011
5012 /* If this is a reference to an external symbol, and there
5013 is no constant, we want
5014 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5015 or for lca or if tempreg is PIC_CALL_REG
5016 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5017 For a local symbol, we want
5018 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5019 nop
5020 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5021
5022 If we have a small constant, and this is a reference to
5023 an external symbol, we want
5024 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5025 nop
5026 addiu $tempreg,$tempreg,<constant>
5027 For a local symbol, we want the same instruction
5028 sequence, but we output a BFD_RELOC_LO16 reloc on the
5029 addiu instruction.
5030
5031 If we have a large constant, and this is a reference to
5032 an external symbol, we want
5033 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5034 lui $at,<hiconstant>
5035 addiu $at,$at,<loconstant>
5036 addu $tempreg,$tempreg,$at
5037 For a local symbol, we want the same instruction
5038 sequence, but we output a BFD_RELOC_LO16 reloc on the
5039 addiu instruction.
5040 */
5041
5042 if (offset_expr.X_add_number == 0)
5043 {
5044 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5045 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5046
5047 relax_start (offset_expr.X_add_symbol);
5048 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5049 lw_reloc_type, mips_gp_register);
5050 if (breg != 0)
5051 {
5052 /* We're going to put in an addu instruction using
5053 tempreg, so we may as well insert the nop right
5054 now. */
5055 load_delay_nop ();
5056 }
5057 relax_switch ();
5058 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5059 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5060 load_delay_nop ();
5061 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5062 tempreg, tempreg, BFD_RELOC_LO16);
5063 relax_end ();
5064 /* FIXME: If breg == 0, and the next instruction uses
5065 $tempreg, then if this variant case is used an extra
5066 nop will be generated. */
5067 }
5068 else if (offset_expr.X_add_number >= -0x8000
5069 && offset_expr.X_add_number < 0x8000)
5070 {
5071 load_got_offset (tempreg, &offset_expr);
5072 load_delay_nop ();
5073 add_got_offset (tempreg, &offset_expr);
5074 }
5075 else
5076 {
5077 expr1.X_add_number = offset_expr.X_add_number;
5078 offset_expr.X_add_number =
5079 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5080 load_got_offset (tempreg, &offset_expr);
5081 offset_expr.X_add_number = expr1.X_add_number;
5082 /* If we are going to add in a base register, and the
5083 target register and the base register are the same,
5084 then we are using AT as a temporary register. Since
5085 we want to load the constant into AT, we add our
5086 current AT (from the global offset table) and the
5087 register into the register now, and pretend we were
5088 not using a base register. */
5089 if (breg == treg)
5090 {
5091 load_delay_nop ();
5092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5093 treg, AT, breg);
5094 breg = 0;
5095 tempreg = treg;
5096 }
5097 add_got_offset_hilo (tempreg, &offset_expr, AT);
5098 used_at = 1;
5099 }
5100 }
5101 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5102 {
5103 int add_breg_early = 0;
5104
5105 /* If this is a reference to an external, and there is no
5106 constant, or local symbol (*), with or without a
5107 constant, we want
5108 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5109 or for lca or if tempreg is PIC_CALL_REG
5110 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5111
5112 If we have a small constant, and this is a reference to
5113 an external symbol, we want
5114 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5115 addiu $tempreg,$tempreg,<constant>
5116
5117 If we have a large constant, and this is a reference to
5118 an external symbol, we want
5119 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5120 lui $at,<hiconstant>
5121 addiu $at,$at,<loconstant>
5122 addu $tempreg,$tempreg,$at
5123
5124 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5125 local symbols, even though it introduces an additional
5126 instruction. */
5127
5128 if (offset_expr.X_add_number)
5129 {
5130 expr1.X_add_number = offset_expr.X_add_number;
5131 offset_expr.X_add_number = 0;
5132
5133 relax_start (offset_expr.X_add_symbol);
5134 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5135 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5136
5137 if (expr1.X_add_number >= -0x8000
5138 && expr1.X_add_number < 0x8000)
5139 {
5140 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5141 tempreg, tempreg, BFD_RELOC_LO16);
5142 }
5143 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5144 {
5145 int dreg;
5146
5147 /* If we are going to add in a base register, and the
5148 target register and the base register are the same,
5149 then we are using AT as a temporary register. Since
5150 we want to load the constant into AT, we add our
5151 current AT (from the global offset table) and the
5152 register into the register now, and pretend we were
5153 not using a base register. */
5154 if (breg != treg)
5155 dreg = tempreg;
5156 else
5157 {
5158 assert (tempreg == AT);
5159 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5160 treg, AT, breg);
5161 dreg = treg;
5162 add_breg_early = 1;
5163 }
5164
5165 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5166 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5167 dreg, dreg, AT);
5168
5169 used_at = 1;
5170 }
5171 else
5172 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5173
5174 relax_switch ();
5175 offset_expr.X_add_number = expr1.X_add_number;
5176
5177 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5178 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5179 if (add_breg_early)
5180 {
5181 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5182 treg, tempreg, breg);
5183 breg = 0;
5184 tempreg = treg;
5185 }
5186 relax_end ();
5187 }
5188 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5189 {
5190 relax_start (offset_expr.X_add_symbol);
5191 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5192 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5193 relax_switch ();
5194 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5195 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5196 relax_end ();
5197 }
5198 else
5199 {
5200 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5201 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5202 }
5203 }
5204 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5205 {
5206 int gpdelay;
5207 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5208 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5209 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5210
5211 /* This is the large GOT case. If this is a reference to an
5212 external symbol, and there is no constant, we want
5213 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5214 addu $tempreg,$tempreg,$gp
5215 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5216 or for lca or if tempreg is PIC_CALL_REG
5217 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5218 addu $tempreg,$tempreg,$gp
5219 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5220 For a local symbol, we want
5221 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5222 nop
5223 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5224
5225 If we have a small constant, and this is a reference to
5226 an external symbol, we want
5227 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5228 addu $tempreg,$tempreg,$gp
5229 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5230 nop
5231 addiu $tempreg,$tempreg,<constant>
5232 For a local symbol, we want
5233 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5234 nop
5235 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5236
5237 If we have a large constant, and this is a reference to
5238 an external symbol, we want
5239 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5240 addu $tempreg,$tempreg,$gp
5241 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5242 lui $at,<hiconstant>
5243 addiu $at,$at,<loconstant>
5244 addu $tempreg,$tempreg,$at
5245 For a local symbol, we want
5246 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5247 lui $at,<hiconstant>
5248 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5249 addu $tempreg,$tempreg,$at
5250 */
5251
5252 expr1.X_add_number = offset_expr.X_add_number;
5253 offset_expr.X_add_number = 0;
5254 relax_start (offset_expr.X_add_symbol);
5255 gpdelay = reg_needs_delay (mips_gp_register);
5256 if (expr1.X_add_number == 0 && breg == 0
5257 && (call || tempreg == PIC_CALL_REG))
5258 {
5259 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5260 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5261 }
5262 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5263 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5264 tempreg, tempreg, mips_gp_register);
5265 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5266 tempreg, lw_reloc_type, tempreg);
5267 if (expr1.X_add_number == 0)
5268 {
5269 if (breg != 0)
5270 {
5271 /* We're going to put in an addu instruction using
5272 tempreg, so we may as well insert the nop right
5273 now. */
5274 load_delay_nop ();
5275 }
5276 }
5277 else if (expr1.X_add_number >= -0x8000
5278 && expr1.X_add_number < 0x8000)
5279 {
5280 load_delay_nop ();
5281 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5282 tempreg, tempreg, BFD_RELOC_LO16);
5283 }
5284 else
5285 {
5286 int dreg;
5287
5288 /* If we are going to add in a base register, and the
5289 target register and the base register are the same,
5290 then we are using AT as a temporary register. Since
5291 we want to load the constant into AT, we add our
5292 current AT (from the global offset table) and the
5293 register into the register now, and pretend we were
5294 not using a base register. */
5295 if (breg != treg)
5296 dreg = tempreg;
5297 else
5298 {
5299 assert (tempreg == AT);
5300 load_delay_nop ();
5301 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5302 treg, AT, breg);
5303 dreg = treg;
5304 }
5305
5306 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5307 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5308
5309 used_at = 1;
5310 }
5311 offset_expr.X_add_number =
5312 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5313 relax_switch ();
5314
5315 if (gpdelay)
5316 {
5317 /* This is needed because this instruction uses $gp, but
5318 the first instruction on the main stream does not. */
5319 macro_build (NULL, "nop", "");
5320 }
5321
5322 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5323 local_reloc_type, mips_gp_register);
5324 if (expr1.X_add_number >= -0x8000
5325 && expr1.X_add_number < 0x8000)
5326 {
5327 load_delay_nop ();
5328 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5329 tempreg, tempreg, BFD_RELOC_LO16);
5330 /* FIXME: If add_number is 0, and there was no base
5331 register, the external symbol case ended with a load,
5332 so if the symbol turns out to not be external, and
5333 the next instruction uses tempreg, an unnecessary nop
5334 will be inserted. */
5335 }
5336 else
5337 {
5338 if (breg == treg)
5339 {
5340 /* We must add in the base register now, as in the
5341 external symbol case. */
5342 assert (tempreg == AT);
5343 load_delay_nop ();
5344 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5345 treg, AT, breg);
5346 tempreg = treg;
5347 /* We set breg to 0 because we have arranged to add
5348 it in in both cases. */
5349 breg = 0;
5350 }
5351
5352 macro_build_lui (&expr1, AT);
5353 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5354 AT, AT, BFD_RELOC_LO16);
5355 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5356 tempreg, tempreg, AT);
5357 }
5358 relax_end ();
5359 }
5360 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5361 {
5362 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5363 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5364 int add_breg_early = 0;
5365
5366 /* This is the large GOT case. If this is a reference to an
5367 external symbol, and there is no constant, we want
5368 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5369 add $tempreg,$tempreg,$gp
5370 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5371 or for lca or if tempreg is PIC_CALL_REG
5372 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5373 add $tempreg,$tempreg,$gp
5374 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5375
5376 If we have a small constant, and this is a reference to
5377 an external symbol, we want
5378 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5379 add $tempreg,$tempreg,$gp
5380 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5381 addi $tempreg,$tempreg,<constant>
5382
5383 If we have a large constant, and this is a reference to
5384 an external symbol, we want
5385 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5386 addu $tempreg,$tempreg,$gp
5387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5388 lui $at,<hiconstant>
5389 addi $at,$at,<loconstant>
5390 add $tempreg,$tempreg,$at
5391
5392 If we have NewABI, and we know it's a local symbol, we want
5393 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5394 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5395 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5396
5397 relax_start (offset_expr.X_add_symbol);
5398
5399 expr1.X_add_number = offset_expr.X_add_number;
5400 offset_expr.X_add_number = 0;
5401
5402 if (expr1.X_add_number == 0 && breg == 0
5403 && (call || tempreg == PIC_CALL_REG))
5404 {
5405 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5406 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5407 }
5408 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5409 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5410 tempreg, tempreg, mips_gp_register);
5411 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5412 tempreg, lw_reloc_type, tempreg);
5413
5414 if (expr1.X_add_number == 0)
5415 ;
5416 else if (expr1.X_add_number >= -0x8000
5417 && expr1.X_add_number < 0x8000)
5418 {
5419 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5420 tempreg, tempreg, BFD_RELOC_LO16);
5421 }
5422 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5423 {
5424 int dreg;
5425
5426 /* If we are going to add in a base register, and the
5427 target register and the base register are the same,
5428 then we are using AT as a temporary register. Since
5429 we want to load the constant into AT, we add our
5430 current AT (from the global offset table) and the
5431 register into the register now, and pretend we were
5432 not using a base register. */
5433 if (breg != treg)
5434 dreg = tempreg;
5435 else
5436 {
5437 assert (tempreg == AT);
5438 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5439 treg, AT, breg);
5440 dreg = treg;
5441 add_breg_early = 1;
5442 }
5443
5444 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5445 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5446
5447 used_at = 1;
5448 }
5449 else
5450 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5451
5452 relax_switch ();
5453 offset_expr.X_add_number = expr1.X_add_number;
5454 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5455 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5456 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5457 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5458 if (add_breg_early)
5459 {
5460 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5461 treg, tempreg, breg);
5462 breg = 0;
5463 tempreg = treg;
5464 }
5465 relax_end ();
5466 }
5467 else
5468 abort ();
5469
5470 if (breg != 0)
5471 {
5472 char *s;
5473
5474 if (mips_pic == NO_PIC)
5475 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5476 else
5477 s = ADDRESS_ADD_INSN;
5478
5479 macro_build (NULL, s, "d,v,t", treg, tempreg, breg);
5480 }
5481
5482 if (! used_at)
5483 return;
5484
5485 break;
5486
5487 case M_J_A:
5488 /* The j instruction may not be used in PIC code, since it
5489 requires an absolute address. We convert it to a b
5490 instruction. */
5491 if (mips_pic == NO_PIC)
5492 macro_build (&offset_expr, "j", "a");
5493 else
5494 macro_build (&offset_expr, "b", "p");
5495 return;
5496
5497 /* The jal instructions must be handled as macros because when
5498 generating PIC code they expand to multi-instruction
5499 sequences. Normally they are simple instructions. */
5500 case M_JAL_1:
5501 dreg = RA;
5502 /* Fall through. */
5503 case M_JAL_2:
5504 if (mips_pic == NO_PIC)
5505 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5506 else if (mips_pic == SVR4_PIC)
5507 {
5508 if (sreg != PIC_CALL_REG)
5509 as_warn (_("MIPS PIC call to register other than $25"));
5510
5511 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5512 if (! HAVE_NEWABI)
5513 {
5514 if (mips_cprestore_offset < 0)
5515 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5516 else
5517 {
5518 if (! mips_frame_reg_valid)
5519 {
5520 as_warn (_("No .frame pseudo-op used in PIC code"));
5521 /* Quiet this warning. */
5522 mips_frame_reg_valid = 1;
5523 }
5524 if (! mips_cprestore_valid)
5525 {
5526 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5527 /* Quiet this warning. */
5528 mips_cprestore_valid = 1;
5529 }
5530 expr1.X_add_number = mips_cprestore_offset;
5531 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5532 mips_gp_register,
5533 mips_frame_reg,
5534 HAVE_64BIT_ADDRESSES);
5535 }
5536 }
5537 }
5538 else
5539 abort ();
5540
5541 return;
5542
5543 case M_JAL_A:
5544 if (mips_pic == NO_PIC)
5545 macro_build (&offset_expr, "jal", "a");
5546 else if (mips_pic == SVR4_PIC)
5547 {
5548 /* If this is a reference to an external symbol, and we are
5549 using a small GOT, we want
5550 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5551 nop
5552 jalr $ra,$25
5553 nop
5554 lw $gp,cprestore($sp)
5555 The cprestore value is set using the .cprestore
5556 pseudo-op. If we are using a big GOT, we want
5557 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5558 addu $25,$25,$gp
5559 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5560 nop
5561 jalr $ra,$25
5562 nop
5563 lw $gp,cprestore($sp)
5564 If the symbol is not external, we want
5565 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5566 nop
5567 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5568 jalr $ra,$25
5569 nop
5570 lw $gp,cprestore($sp)
5571
5572 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5573 sequences above, minus nops, unless the symbol is local,
5574 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5575 GOT_DISP. */
5576 if (HAVE_NEWABI)
5577 {
5578 if (! mips_big_got)
5579 {
5580 relax_start (offset_expr.X_add_symbol);
5581 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5582 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5583 mips_gp_register);
5584 relax_switch ();
5585 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5586 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5587 mips_gp_register);
5588 relax_end ();
5589 }
5590 else
5591 {
5592 relax_start (offset_expr.X_add_symbol);
5593 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5594 BFD_RELOC_MIPS_CALL_HI16);
5595 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5596 PIC_CALL_REG, mips_gp_register);
5597 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5598 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5599 PIC_CALL_REG);
5600 relax_switch ();
5601 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5602 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5603 mips_gp_register);
5604 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5605 PIC_CALL_REG, PIC_CALL_REG,
5606 BFD_RELOC_MIPS_GOT_OFST);
5607 relax_end ();
5608 }
5609
5610 macro_build_jalr (&offset_expr);
5611 }
5612 else
5613 {
5614 relax_start (offset_expr.X_add_symbol);
5615 if (! mips_big_got)
5616 {
5617 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5618 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5619 mips_gp_register);
5620 load_delay_nop ();
5621 relax_switch ();
5622 }
5623 else
5624 {
5625 int gpdelay;
5626
5627 gpdelay = reg_needs_delay (mips_gp_register);
5628 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5629 BFD_RELOC_MIPS_CALL_HI16);
5630 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5631 PIC_CALL_REG, mips_gp_register);
5632 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5633 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5634 PIC_CALL_REG);
5635 load_delay_nop ();
5636 relax_switch ();
5637 if (gpdelay)
5638 macro_build (NULL, "nop", "");
5639 }
5640 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5641 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5642 mips_gp_register);
5643 load_delay_nop ();
5644 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5645 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5646 relax_end ();
5647 macro_build_jalr (&offset_expr);
5648
5649 if (mips_cprestore_offset < 0)
5650 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5651 else
5652 {
5653 if (! mips_frame_reg_valid)
5654 {
5655 as_warn (_("No .frame pseudo-op used in PIC code"));
5656 /* Quiet this warning. */
5657 mips_frame_reg_valid = 1;
5658 }
5659 if (! mips_cprestore_valid)
5660 {
5661 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5662 /* Quiet this warning. */
5663 mips_cprestore_valid = 1;
5664 }
5665 if (mips_opts.noreorder)
5666 macro_build (NULL, "nop", "");
5667 expr1.X_add_number = mips_cprestore_offset;
5668 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5669 mips_gp_register,
5670 mips_frame_reg,
5671 HAVE_64BIT_ADDRESSES);
5672 }
5673 }
5674 }
5675 else
5676 abort ();
5677
5678 return;
5679
5680 case M_LB_AB:
5681 s = "lb";
5682 goto ld;
5683 case M_LBU_AB:
5684 s = "lbu";
5685 goto ld;
5686 case M_LH_AB:
5687 s = "lh";
5688 goto ld;
5689 case M_LHU_AB:
5690 s = "lhu";
5691 goto ld;
5692 case M_LW_AB:
5693 s = "lw";
5694 goto ld;
5695 case M_LWC0_AB:
5696 s = "lwc0";
5697 /* Itbl support may require additional care here. */
5698 coproc = 1;
5699 goto ld;
5700 case M_LWC1_AB:
5701 s = "lwc1";
5702 /* Itbl support may require additional care here. */
5703 coproc = 1;
5704 goto ld;
5705 case M_LWC2_AB:
5706 s = "lwc2";
5707 /* Itbl support may require additional care here. */
5708 coproc = 1;
5709 goto ld;
5710 case M_LWC3_AB:
5711 s = "lwc3";
5712 /* Itbl support may require additional care here. */
5713 coproc = 1;
5714 goto ld;
5715 case M_LWL_AB:
5716 s = "lwl";
5717 lr = 1;
5718 goto ld;
5719 case M_LWR_AB:
5720 s = "lwr";
5721 lr = 1;
5722 goto ld;
5723 case M_LDC1_AB:
5724 if (mips_opts.arch == CPU_R4650)
5725 {
5726 as_bad (_("opcode not supported on this processor"));
5727 return;
5728 }
5729 s = "ldc1";
5730 /* Itbl support may require additional care here. */
5731 coproc = 1;
5732 goto ld;
5733 case M_LDC2_AB:
5734 s = "ldc2";
5735 /* Itbl support may require additional care here. */
5736 coproc = 1;
5737 goto ld;
5738 case M_LDC3_AB:
5739 s = "ldc3";
5740 /* Itbl support may require additional care here. */
5741 coproc = 1;
5742 goto ld;
5743 case M_LDL_AB:
5744 s = "ldl";
5745 lr = 1;
5746 goto ld;
5747 case M_LDR_AB:
5748 s = "ldr";
5749 lr = 1;
5750 goto ld;
5751 case M_LL_AB:
5752 s = "ll";
5753 goto ld;
5754 case M_LLD_AB:
5755 s = "lld";
5756 goto ld;
5757 case M_LWU_AB:
5758 s = "lwu";
5759 ld:
5760 if (breg == treg || coproc || lr)
5761 {
5762 tempreg = AT;
5763 used_at = 1;
5764 }
5765 else
5766 {
5767 tempreg = treg;
5768 used_at = 0;
5769 }
5770 goto ld_st;
5771 case M_SB_AB:
5772 s = "sb";
5773 goto st;
5774 case M_SH_AB:
5775 s = "sh";
5776 goto st;
5777 case M_SW_AB:
5778 s = "sw";
5779 goto st;
5780 case M_SWC0_AB:
5781 s = "swc0";
5782 /* Itbl support may require additional care here. */
5783 coproc = 1;
5784 goto st;
5785 case M_SWC1_AB:
5786 s = "swc1";
5787 /* Itbl support may require additional care here. */
5788 coproc = 1;
5789 goto st;
5790 case M_SWC2_AB:
5791 s = "swc2";
5792 /* Itbl support may require additional care here. */
5793 coproc = 1;
5794 goto st;
5795 case M_SWC3_AB:
5796 s = "swc3";
5797 /* Itbl support may require additional care here. */
5798 coproc = 1;
5799 goto st;
5800 case M_SWL_AB:
5801 s = "swl";
5802 goto st;
5803 case M_SWR_AB:
5804 s = "swr";
5805 goto st;
5806 case M_SC_AB:
5807 s = "sc";
5808 goto st;
5809 case M_SCD_AB:
5810 s = "scd";
5811 goto st;
5812 case M_SDC1_AB:
5813 if (mips_opts.arch == CPU_R4650)
5814 {
5815 as_bad (_("opcode not supported on this processor"));
5816 return;
5817 }
5818 s = "sdc1";
5819 coproc = 1;
5820 /* Itbl support may require additional care here. */
5821 goto st;
5822 case M_SDC2_AB:
5823 s = "sdc2";
5824 /* Itbl support may require additional care here. */
5825 coproc = 1;
5826 goto st;
5827 case M_SDC3_AB:
5828 s = "sdc3";
5829 /* Itbl support may require additional care here. */
5830 coproc = 1;
5831 goto st;
5832 case M_SDL_AB:
5833 s = "sdl";
5834 goto st;
5835 case M_SDR_AB:
5836 s = "sdr";
5837 st:
5838 tempreg = AT;
5839 used_at = 1;
5840 ld_st:
5841 /* Itbl support may require additional care here. */
5842 if (mask == M_LWC1_AB
5843 || mask == M_SWC1_AB
5844 || mask == M_LDC1_AB
5845 || mask == M_SDC1_AB
5846 || mask == M_L_DAB
5847 || mask == M_S_DAB)
5848 fmt = "T,o(b)";
5849 else if (coproc)
5850 fmt = "E,o(b)";
5851 else
5852 fmt = "t,o(b)";
5853
5854 /* Sign-extending 32-bit constants makes their handling easier.
5855 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5856 described below. */
5857 if ((! HAVE_64BIT_ADDRESSES
5858 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
5859 && (offset_expr.X_op == O_constant)
5860 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5861 == ~((bfd_vma) 0x7fffffff)))
5862 {
5863 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
5864 as_bad (_("constant too large"));
5865
5866 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5867 ^ 0x80000000) - 0x80000000);
5868 }
5869
5870 if (offset_expr.X_op != O_constant
5871 && offset_expr.X_op != O_symbol)
5872 {
5873 as_bad (_("expression too complex"));
5874 offset_expr.X_op = O_constant;
5875 }
5876
5877 /* A constant expression in PIC code can be handled just as it
5878 is in non PIC code. */
5879 if (mips_pic == NO_PIC
5880 || offset_expr.X_op == O_constant)
5881 {
5882 /* If this is a reference to a GP relative symbol, and there
5883 is no base register, we want
5884 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5885 Otherwise, if there is no base register, we want
5886 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5887 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5888 If we have a constant, we need two instructions anyhow,
5889 so we always use the latter form.
5890
5891 If we have a base register, and this is a reference to a
5892 GP relative symbol, we want
5893 addu $tempreg,$breg,$gp
5894 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5895 Otherwise we want
5896 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5897 addu $tempreg,$tempreg,$breg
5898 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5899 With a constant we always use the latter case.
5900
5901 With 64bit address space and no base register and $at usable,
5902 we want
5903 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5904 lui $at,<sym> (BFD_RELOC_HI16_S)
5905 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5906 dsll32 $tempreg,0
5907 daddu $tempreg,$at
5908 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5909 If we have a base register, we want
5910 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5911 lui $at,<sym> (BFD_RELOC_HI16_S)
5912 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5913 daddu $at,$breg
5914 dsll32 $tempreg,0
5915 daddu $tempreg,$at
5916 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5917
5918 Without $at we can't generate the optimal path for superscalar
5919 processors here since this would require two temporary registers.
5920 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5921 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5922 dsll $tempreg,16
5923 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5924 dsll $tempreg,16
5925 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5926 If we have a base register, we want
5927 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5928 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5929 dsll $tempreg,16
5930 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5931 dsll $tempreg,16
5932 daddu $tempreg,$tempreg,$breg
5933 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5934
5935 If we have 64-bit addresses, as an optimization, for
5936 addresses which are 32-bit constants (e.g. kseg0/kseg1
5937 addresses) we fall back to the 32-bit address generation
5938 mechanism since it is more efficient. Note that due to
5939 the signed offset used by memory operations, the 32-bit
5940 range is shifted down by 32768 here. This code should
5941 probably attempt to generate 64-bit constants more
5942 efficiently in general.
5943
5944 As an extension for architectures with 64-bit registers,
5945 we don't truncate 64-bit addresses given as literal
5946 constants down to 32 bits, to support existing practice
5947 in the mips64 Linux (the kernel), that compiles source
5948 files with -mabi=64, assembling them as o32 or n32 (with
5949 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
5950 the whole kernel is loaded into a memory region that is
5951 addressable with sign-extended 32-bit addresses, it is
5952 wasteful to compute the upper 32 bits of every
5953 non-literal address, that takes more space and time.
5954 Some day this should probably be implemented as an
5955 assembler option, such that the kernel doesn't have to
5956 use such ugly hacks, even though it will still have to
5957 end up converting the binary to ELF32 for a number of
5958 platforms whose boot loaders don't support ELF64
5959 binaries. */
5960 if ((HAVE_64BIT_ADDRESSES
5961 && ! (offset_expr.X_op == O_constant
5962 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5963 || (HAVE_64BIT_GPRS
5964 && offset_expr.X_op == O_constant
5965 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5966 {
5967 /* ??? We don't provide a GP-relative alternative for
5968 these macros. It used not to be possible with the
5969 original relaxation code, but it could be done now. */
5970
5971 if (used_at == 0 && ! mips_opts.noat)
5972 {
5973 macro_build (&offset_expr, "lui", "t,u", tempreg,
5974 BFD_RELOC_MIPS_HIGHEST);
5975 macro_build (&offset_expr, "lui", "t,u", AT,
5976 BFD_RELOC_HI16_S);
5977 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5978 tempreg, BFD_RELOC_MIPS_HIGHER);
5979 if (breg != 0)
5980 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5981 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5982 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5983 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5984 tempreg);
5985 used_at = 1;
5986 }
5987 else
5988 {
5989 macro_build (&offset_expr, "lui", "t,u", tempreg,
5990 BFD_RELOC_MIPS_HIGHEST);
5991 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5992 tempreg, BFD_RELOC_MIPS_HIGHER);
5993 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5994 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5995 tempreg, BFD_RELOC_HI16_S);
5996 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5997 if (breg != 0)
5998 macro_build (NULL, "daddu", "d,v,t",
5999 tempreg, tempreg, breg);
6000 macro_build (&offset_expr, s, fmt, treg,
6001 BFD_RELOC_LO16, tempreg);
6002 }
6003
6004 return;
6005 }
6006
6007 if (offset_expr.X_op == O_constant
6008 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
6009 as_bad (_("load/store address overflow (max 32 bits)"));
6010
6011 if (breg == 0)
6012 {
6013 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6014 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
6015 {
6016 relax_start (offset_expr.X_add_symbol);
6017 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6018 mips_gp_register);
6019 relax_switch ();
6020 used_at = 0;
6021 }
6022 macro_build_lui (&offset_expr, tempreg);
6023 macro_build (&offset_expr, s, fmt, treg,
6024 BFD_RELOC_LO16, tempreg);
6025 if (mips_relax.sequence)
6026 relax_end ();
6027 }
6028 else
6029 {
6030 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6031 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
6032 {
6033 relax_start (offset_expr.X_add_symbol);
6034 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6035 tempreg, breg, mips_gp_register);
6036 macro_build (&offset_expr, s, fmt, treg,
6037 BFD_RELOC_GPREL16, tempreg);
6038 relax_switch ();
6039 }
6040 macro_build_lui (&offset_expr, tempreg);
6041 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6042 tempreg, tempreg, breg);
6043 macro_build (&offset_expr, s, fmt, treg,
6044 BFD_RELOC_LO16, tempreg);
6045 if (mips_relax.sequence)
6046 relax_end ();
6047 }
6048 }
6049 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6050 {
6051 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6052
6053 /* If this is a reference to an external symbol, we want
6054 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6055 nop
6056 <op> $treg,0($tempreg)
6057 Otherwise we want
6058 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6059 nop
6060 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6061 <op> $treg,0($tempreg)
6062
6063 For NewABI, we want
6064 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6065 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6066
6067 If there is a base register, we add it to $tempreg before
6068 the <op>. If there is a constant, we stick it in the
6069 <op> instruction. We don't handle constants larger than
6070 16 bits, because we have no way to load the upper 16 bits
6071 (actually, we could handle them for the subset of cases
6072 in which we are not using $at). */
6073 assert (offset_expr.X_op == O_symbol);
6074 if (HAVE_NEWABI)
6075 {
6076 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6077 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6078 if (breg != 0)
6079 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6080 tempreg, tempreg, breg);
6081 macro_build (&offset_expr, s, fmt, treg,
6082 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6083
6084 if (! used_at)
6085 return;
6086
6087 break;
6088 }
6089 expr1.X_add_number = offset_expr.X_add_number;
6090 offset_expr.X_add_number = 0;
6091 if (expr1.X_add_number < -0x8000
6092 || expr1.X_add_number >= 0x8000)
6093 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6094 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6095 lw_reloc_type, mips_gp_register);
6096 load_delay_nop ();
6097 relax_start (offset_expr.X_add_symbol);
6098 relax_switch ();
6099 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6100 tempreg, BFD_RELOC_LO16);
6101 relax_end ();
6102 if (breg != 0)
6103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6104 tempreg, tempreg, breg);
6105 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6106 }
6107 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6108 {
6109 int gpdelay;
6110
6111 /* If this is a reference to an external symbol, we want
6112 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6113 addu $tempreg,$tempreg,$gp
6114 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6115 <op> $treg,0($tempreg)
6116 Otherwise we want
6117 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6118 nop
6119 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6120 <op> $treg,0($tempreg)
6121 If there is a base register, we add it to $tempreg before
6122 the <op>. If there is a constant, we stick it in the
6123 <op> instruction. We don't handle constants larger than
6124 16 bits, because we have no way to load the upper 16 bits
6125 (actually, we could handle them for the subset of cases
6126 in which we are not using $at). */
6127 assert (offset_expr.X_op == O_symbol);
6128 expr1.X_add_number = offset_expr.X_add_number;
6129 offset_expr.X_add_number = 0;
6130 if (expr1.X_add_number < -0x8000
6131 || expr1.X_add_number >= 0x8000)
6132 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6133 gpdelay = reg_needs_delay (mips_gp_register);
6134 relax_start (offset_expr.X_add_symbol);
6135 macro_build (&offset_expr, "lui", "t,u", tempreg,
6136 BFD_RELOC_MIPS_GOT_HI16);
6137 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6138 mips_gp_register);
6139 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6140 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6141 relax_switch ();
6142 if (gpdelay)
6143 macro_build (NULL, "nop", "");
6144 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6145 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6146 load_delay_nop ();
6147 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6148 tempreg, BFD_RELOC_LO16);
6149 relax_end ();
6150
6151 if (breg != 0)
6152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6153 tempreg, tempreg, breg);
6154 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6155 }
6156 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6157 {
6158 /* If this is a reference to an external symbol, we want
6159 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6160 add $tempreg,$tempreg,$gp
6161 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6162 <op> $treg,<ofst>($tempreg)
6163 Otherwise, for local symbols, we want:
6164 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6165 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6166 assert (offset_expr.X_op == O_symbol);
6167 expr1.X_add_number = offset_expr.X_add_number;
6168 offset_expr.X_add_number = 0;
6169 if (expr1.X_add_number < -0x8000
6170 || expr1.X_add_number >= 0x8000)
6171 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6172 relax_start (offset_expr.X_add_symbol);
6173 macro_build (&offset_expr, "lui", "t,u", tempreg,
6174 BFD_RELOC_MIPS_GOT_HI16);
6175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6176 mips_gp_register);
6177 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6178 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6179 if (breg != 0)
6180 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6181 tempreg, tempreg, breg);
6182 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6183
6184 relax_switch ();
6185 offset_expr.X_add_number = expr1.X_add_number;
6186 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6187 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6188 if (breg != 0)
6189 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6190 tempreg, tempreg, breg);
6191 macro_build (&offset_expr, s, fmt, treg,
6192 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6193 relax_end ();
6194 }
6195 else
6196 abort ();
6197
6198 if (! used_at)
6199 return;
6200
6201 break;
6202
6203 case M_LI:
6204 case M_LI_S:
6205 load_register (treg, &imm_expr, 0);
6206 return;
6207
6208 case M_DLI:
6209 load_register (treg, &imm_expr, 1);
6210 return;
6211
6212 case M_LI_SS:
6213 if (imm_expr.X_op == O_constant)
6214 {
6215 load_register (AT, &imm_expr, 0);
6216 macro_build (NULL, "mtc1", "t,G", AT, treg);
6217 break;
6218 }
6219 else
6220 {
6221 assert (offset_expr.X_op == O_symbol
6222 && strcmp (segment_name (S_GET_SEGMENT
6223 (offset_expr.X_add_symbol)),
6224 ".lit4") == 0
6225 && offset_expr.X_add_number == 0);
6226 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6227 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6228 return;
6229 }
6230
6231 case M_LI_D:
6232 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6233 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6234 order 32 bits of the value and the low order 32 bits are either
6235 zero or in OFFSET_EXPR. */
6236 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6237 {
6238 if (HAVE_64BIT_GPRS)
6239 load_register (treg, &imm_expr, 1);
6240 else
6241 {
6242 int hreg, lreg;
6243
6244 if (target_big_endian)
6245 {
6246 hreg = treg;
6247 lreg = treg + 1;
6248 }
6249 else
6250 {
6251 hreg = treg + 1;
6252 lreg = treg;
6253 }
6254
6255 if (hreg <= 31)
6256 load_register (hreg, &imm_expr, 0);
6257 if (lreg <= 31)
6258 {
6259 if (offset_expr.X_op == O_absent)
6260 move_register (lreg, 0);
6261 else
6262 {
6263 assert (offset_expr.X_op == O_constant);
6264 load_register (lreg, &offset_expr, 0);
6265 }
6266 }
6267 }
6268 return;
6269 }
6270
6271 /* We know that sym is in the .rdata section. First we get the
6272 upper 16 bits of the address. */
6273 if (mips_pic == NO_PIC)
6274 {
6275 macro_build_lui (&offset_expr, AT);
6276 }
6277 else if (mips_pic == SVR4_PIC)
6278 {
6279 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6280 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6281 }
6282 else
6283 abort ();
6284
6285 /* Now we load the register(s). */
6286 if (HAVE_64BIT_GPRS)
6287 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6288 else
6289 {
6290 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6291 if (treg != RA)
6292 {
6293 /* FIXME: How in the world do we deal with the possible
6294 overflow here? */
6295 offset_expr.X_add_number += 4;
6296 macro_build (&offset_expr, "lw", "t,o(b)",
6297 treg + 1, BFD_RELOC_LO16, AT);
6298 }
6299 }
6300 break;
6301
6302 case M_LI_DD:
6303 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6304 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6305 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6306 the value and the low order 32 bits are either zero or in
6307 OFFSET_EXPR. */
6308 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6309 {
6310 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6311 if (HAVE_64BIT_FPRS)
6312 {
6313 assert (HAVE_64BIT_GPRS);
6314 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6315 }
6316 else
6317 {
6318 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6319 if (offset_expr.X_op == O_absent)
6320 macro_build (NULL, "mtc1", "t,G", 0, treg);
6321 else
6322 {
6323 assert (offset_expr.X_op == O_constant);
6324 load_register (AT, &offset_expr, 0);
6325 macro_build (NULL, "mtc1", "t,G", AT, treg);
6326 }
6327 }
6328 break;
6329 }
6330
6331 assert (offset_expr.X_op == O_symbol
6332 && offset_expr.X_add_number == 0);
6333 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6334 if (strcmp (s, ".lit8") == 0)
6335 {
6336 if (mips_opts.isa != ISA_MIPS1)
6337 {
6338 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6339 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6340 return;
6341 }
6342 breg = mips_gp_register;
6343 r = BFD_RELOC_MIPS_LITERAL;
6344 goto dob;
6345 }
6346 else
6347 {
6348 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6349 if (mips_pic == SVR4_PIC)
6350 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6351 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6352 else
6353 {
6354 /* FIXME: This won't work for a 64 bit address. */
6355 macro_build_lui (&offset_expr, AT);
6356 }
6357
6358 if (mips_opts.isa != ISA_MIPS1)
6359 {
6360 macro_build (&offset_expr, "ldc1", "T,o(b)",
6361 treg, BFD_RELOC_LO16, AT);
6362 break;
6363 }
6364 breg = AT;
6365 r = BFD_RELOC_LO16;
6366 goto dob;
6367 }
6368
6369 case M_L_DOB:
6370 if (mips_opts.arch == CPU_R4650)
6371 {
6372 as_bad (_("opcode not supported on this processor"));
6373 return;
6374 }
6375 /* Even on a big endian machine $fn comes before $fn+1. We have
6376 to adjust when loading from memory. */
6377 r = BFD_RELOC_LO16;
6378 dob:
6379 assert (mips_opts.isa == ISA_MIPS1);
6380 macro_build (&offset_expr, "lwc1", "T,o(b)",
6381 target_big_endian ? treg + 1 : treg, r, breg);
6382 /* FIXME: A possible overflow which I don't know how to deal
6383 with. */
6384 offset_expr.X_add_number += 4;
6385 macro_build (&offset_expr, "lwc1", "T,o(b)",
6386 target_big_endian ? treg : treg + 1, r, breg);
6387
6388 if (breg != AT)
6389 return;
6390 break;
6391
6392 case M_L_DAB:
6393 /*
6394 * The MIPS assembler seems to check for X_add_number not
6395 * being double aligned and generating:
6396 * lui at,%hi(foo+1)
6397 * addu at,at,v1
6398 * addiu at,at,%lo(foo+1)
6399 * lwc1 f2,0(at)
6400 * lwc1 f3,4(at)
6401 * But, the resulting address is the same after relocation so why
6402 * generate the extra instruction?
6403 */
6404 if (mips_opts.arch == CPU_R4650)
6405 {
6406 as_bad (_("opcode not supported on this processor"));
6407 return;
6408 }
6409 /* Itbl support may require additional care here. */
6410 coproc = 1;
6411 if (mips_opts.isa != ISA_MIPS1)
6412 {
6413 s = "ldc1";
6414 goto ld;
6415 }
6416
6417 s = "lwc1";
6418 fmt = "T,o(b)";
6419 goto ldd_std;
6420
6421 case M_S_DAB:
6422 if (mips_opts.arch == CPU_R4650)
6423 {
6424 as_bad (_("opcode not supported on this processor"));
6425 return;
6426 }
6427
6428 if (mips_opts.isa != ISA_MIPS1)
6429 {
6430 s = "sdc1";
6431 goto st;
6432 }
6433
6434 s = "swc1";
6435 fmt = "T,o(b)";
6436 /* Itbl support may require additional care here. */
6437 coproc = 1;
6438 goto ldd_std;
6439
6440 case M_LD_AB:
6441 if (HAVE_64BIT_GPRS)
6442 {
6443 s = "ld";
6444 goto ld;
6445 }
6446
6447 s = "lw";
6448 fmt = "t,o(b)";
6449 goto ldd_std;
6450
6451 case M_SD_AB:
6452 if (HAVE_64BIT_GPRS)
6453 {
6454 s = "sd";
6455 goto st;
6456 }
6457
6458 s = "sw";
6459 fmt = "t,o(b)";
6460
6461 ldd_std:
6462 if (offset_expr.X_op != O_symbol
6463 && offset_expr.X_op != O_constant)
6464 {
6465 as_bad (_("expression too complex"));
6466 offset_expr.X_op = O_constant;
6467 }
6468
6469 /* Even on a big endian machine $fn comes before $fn+1. We have
6470 to adjust when loading from memory. We set coproc if we must
6471 load $fn+1 first. */
6472 /* Itbl support may require additional care here. */
6473 if (! target_big_endian)
6474 coproc = 0;
6475
6476 if (mips_pic == NO_PIC
6477 || offset_expr.X_op == O_constant)
6478 {
6479 /* If this is a reference to a GP relative symbol, we want
6480 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6481 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6482 If we have a base register, we use this
6483 addu $at,$breg,$gp
6484 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6485 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6486 If this is not a GP relative symbol, we want
6487 lui $at,<sym> (BFD_RELOC_HI16_S)
6488 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6489 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6490 If there is a base register, we add it to $at after the
6491 lui instruction. If there is a constant, we always use
6492 the last case. */
6493 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6494 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6495 used_at = 1;
6496 else
6497 {
6498 relax_start (offset_expr.X_add_symbol);
6499 if (breg == 0)
6500 {
6501 tempreg = mips_gp_register;
6502 used_at = 0;
6503 }
6504 else
6505 {
6506 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6507 AT, breg, mips_gp_register);
6508 tempreg = AT;
6509 used_at = 1;
6510 }
6511
6512 /* Itbl support may require additional care here. */
6513 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6514 BFD_RELOC_GPREL16, tempreg);
6515 offset_expr.X_add_number += 4;
6516
6517 /* Set mips_optimize to 2 to avoid inserting an
6518 undesired nop. */
6519 hold_mips_optimize = mips_optimize;
6520 mips_optimize = 2;
6521 /* Itbl support may require additional care here. */
6522 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6523 BFD_RELOC_GPREL16, tempreg);
6524 mips_optimize = hold_mips_optimize;
6525
6526 relax_switch ();
6527
6528 /* We just generated two relocs. When tc_gen_reloc
6529 handles this case, it will skip the first reloc and
6530 handle the second. The second reloc already has an
6531 extra addend of 4, which we added above. We must
6532 subtract it out, and then subtract another 4 to make
6533 the first reloc come out right. The second reloc
6534 will come out right because we are going to add 4 to
6535 offset_expr when we build its instruction below.
6536
6537 If we have a symbol, then we don't want to include
6538 the offset, because it will wind up being included
6539 when we generate the reloc. */
6540
6541 if (offset_expr.X_op == O_constant)
6542 offset_expr.X_add_number -= 8;
6543 else
6544 {
6545 offset_expr.X_add_number = -4;
6546 offset_expr.X_op = O_constant;
6547 }
6548 }
6549 macro_build_lui (&offset_expr, AT);
6550 if (breg != 0)
6551 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6552 /* Itbl support may require additional care here. */
6553 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6554 BFD_RELOC_LO16, AT);
6555 /* FIXME: How do we handle overflow here? */
6556 offset_expr.X_add_number += 4;
6557 /* Itbl support may require additional care here. */
6558 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6559 BFD_RELOC_LO16, AT);
6560 if (mips_relax.sequence)
6561 relax_end ();
6562 }
6563 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6564 {
6565 /* If this is a reference to an external symbol, we want
6566 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6567 nop
6568 <op> $treg,0($at)
6569 <op> $treg+1,4($at)
6570 Otherwise we want
6571 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6572 nop
6573 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6574 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6575 If there is a base register we add it to $at before the
6576 lwc1 instructions. If there is a constant we include it
6577 in the lwc1 instructions. */
6578 used_at = 1;
6579 expr1.X_add_number = offset_expr.X_add_number;
6580 if (expr1.X_add_number < -0x8000
6581 || expr1.X_add_number >= 0x8000 - 4)
6582 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6583 load_got_offset (AT, &offset_expr);
6584 load_delay_nop ();
6585 if (breg != 0)
6586 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6587
6588 /* Set mips_optimize to 2 to avoid inserting an undesired
6589 nop. */
6590 hold_mips_optimize = mips_optimize;
6591 mips_optimize = 2;
6592
6593 /* Itbl support may require additional care here. */
6594 relax_start (offset_expr.X_add_symbol);
6595 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6596 BFD_RELOC_LO16, AT);
6597 expr1.X_add_number += 4;
6598 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6599 BFD_RELOC_LO16, AT);
6600 relax_switch ();
6601 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6602 BFD_RELOC_LO16, AT);
6603 offset_expr.X_add_number += 4;
6604 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6605 BFD_RELOC_LO16, AT);
6606 relax_end ();
6607
6608 mips_optimize = hold_mips_optimize;
6609 }
6610 else if (mips_pic == SVR4_PIC)
6611 {
6612 int gpdelay;
6613
6614 /* If this is a reference to an external symbol, we want
6615 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6616 addu $at,$at,$gp
6617 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6618 nop
6619 <op> $treg,0($at)
6620 <op> $treg+1,4($at)
6621 Otherwise we want
6622 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6623 nop
6624 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6625 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6626 If there is a base register we add it to $at before the
6627 lwc1 instructions. If there is a constant we include it
6628 in the lwc1 instructions. */
6629 used_at = 1;
6630 expr1.X_add_number = offset_expr.X_add_number;
6631 offset_expr.X_add_number = 0;
6632 if (expr1.X_add_number < -0x8000
6633 || expr1.X_add_number >= 0x8000 - 4)
6634 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6635 gpdelay = reg_needs_delay (mips_gp_register);
6636 relax_start (offset_expr.X_add_symbol);
6637 macro_build (&offset_expr, "lui", "t,u",
6638 AT, BFD_RELOC_MIPS_GOT_HI16);
6639 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6640 AT, AT, mips_gp_register);
6641 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6642 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6643 load_delay_nop ();
6644 if (breg != 0)
6645 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6646 /* Itbl support may require additional care here. */
6647 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6648 BFD_RELOC_LO16, AT);
6649 expr1.X_add_number += 4;
6650
6651 /* Set mips_optimize to 2 to avoid inserting an undesired
6652 nop. */
6653 hold_mips_optimize = mips_optimize;
6654 mips_optimize = 2;
6655 /* Itbl support may require additional care here. */
6656 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6657 BFD_RELOC_LO16, AT);
6658 mips_optimize = hold_mips_optimize;
6659 expr1.X_add_number -= 4;
6660
6661 relax_switch ();
6662 offset_expr.X_add_number = expr1.X_add_number;
6663 if (gpdelay)
6664 macro_build (NULL, "nop", "");
6665 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6666 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6667 load_delay_nop ();
6668 if (breg != 0)
6669 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6670 /* Itbl support may require additional care here. */
6671 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6672 BFD_RELOC_LO16, AT);
6673 offset_expr.X_add_number += 4;
6674
6675 /* Set mips_optimize to 2 to avoid inserting an undesired
6676 nop. */
6677 hold_mips_optimize = mips_optimize;
6678 mips_optimize = 2;
6679 /* Itbl support may require additional care here. */
6680 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6681 BFD_RELOC_LO16, AT);
6682 mips_optimize = hold_mips_optimize;
6683 relax_end ();
6684 }
6685 else
6686 abort ();
6687
6688 if (! used_at)
6689 return;
6690
6691 break;
6692
6693 case M_LD_OB:
6694 s = "lw";
6695 goto sd_ob;
6696 case M_SD_OB:
6697 s = "sw";
6698 sd_ob:
6699 assert (HAVE_32BIT_ADDRESSES);
6700 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6701 offset_expr.X_add_number += 4;
6702 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6703 return;
6704
6705 /* New code added to support COPZ instructions.
6706 This code builds table entries out of the macros in mip_opcodes.
6707 R4000 uses interlocks to handle coproc delays.
6708 Other chips (like the R3000) require nops to be inserted for delays.
6709
6710 FIXME: Currently, we require that the user handle delays.
6711 In order to fill delay slots for non-interlocked chips,
6712 we must have a way to specify delays based on the coprocessor.
6713 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6714 What are the side-effects of the cop instruction?
6715 What cache support might we have and what are its effects?
6716 Both coprocessor & memory require delays. how long???
6717 What registers are read/set/modified?
6718
6719 If an itbl is provided to interpret cop instructions,
6720 this knowledge can be encoded in the itbl spec. */
6721
6722 case M_COP0:
6723 s = "c0";
6724 goto copz;
6725 case M_COP1:
6726 s = "c1";
6727 goto copz;
6728 case M_COP2:
6729 s = "c2";
6730 goto copz;
6731 case M_COP3:
6732 s = "c3";
6733 copz:
6734 /* For now we just do C (same as Cz). The parameter will be
6735 stored in insn_opcode by mips_ip. */
6736 macro_build (NULL, s, "C", ip->insn_opcode);
6737 return;
6738
6739 case M_MOVE:
6740 move_register (dreg, sreg);
6741 return;
6742
6743 #ifdef LOSING_COMPILER
6744 default:
6745 /* Try and see if this is a new itbl instruction.
6746 This code builds table entries out of the macros in mip_opcodes.
6747 FIXME: For now we just assemble the expression and pass it's
6748 value along as a 32-bit immediate.
6749 We may want to have the assembler assemble this value,
6750 so that we gain the assembler's knowledge of delay slots,
6751 symbols, etc.
6752 Would it be more efficient to use mask (id) here? */
6753 if (itbl_have_entries
6754 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6755 {
6756 s = ip->insn_mo->name;
6757 s2 = "cop3";
6758 coproc = ITBL_DECODE_PNUM (immed_expr);;
6759 macro_build (&immed_expr, s, "C");
6760 return;
6761 }
6762 macro2 (ip);
6763 return;
6764 }
6765 if (mips_opts.noat)
6766 as_warn (_("Macro used $at after \".set noat\""));
6767 }
6768
6769 static void
6770 macro2 (struct mips_cl_insn *ip)
6771 {
6772 register int treg, sreg, dreg, breg;
6773 int tempreg;
6774 int mask;
6775 int used_at;
6776 expressionS expr1;
6777 const char *s;
6778 const char *s2;
6779 const char *fmt;
6780 int likely = 0;
6781 int dbl = 0;
6782 int coproc = 0;
6783 int lr = 0;
6784 int imm = 0;
6785 int off;
6786 offsetT maxnum;
6787 bfd_reloc_code_real_type r;
6788
6789 treg = (ip->insn_opcode >> 16) & 0x1f;
6790 dreg = (ip->insn_opcode >> 11) & 0x1f;
6791 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6792 mask = ip->insn_mo->mask;
6793
6794 expr1.X_op = O_constant;
6795 expr1.X_op_symbol = NULL;
6796 expr1.X_add_symbol = NULL;
6797 expr1.X_add_number = 1;
6798
6799 switch (mask)
6800 {
6801 #endif /* LOSING_COMPILER */
6802
6803 case M_DMUL:
6804 dbl = 1;
6805 case M_MUL:
6806 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6807 macro_build (NULL, "mflo", "d", dreg);
6808 return;
6809
6810 case M_DMUL_I:
6811 dbl = 1;
6812 case M_MUL_I:
6813 /* The MIPS assembler some times generates shifts and adds. I'm
6814 not trying to be that fancy. GCC should do this for us
6815 anyway. */
6816 load_register (AT, &imm_expr, dbl);
6817 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6818 macro_build (NULL, "mflo", "d", dreg);
6819 break;
6820
6821 case M_DMULO_I:
6822 dbl = 1;
6823 case M_MULO_I:
6824 imm = 1;
6825 goto do_mulo;
6826
6827 case M_DMULO:
6828 dbl = 1;
6829 case M_MULO:
6830 do_mulo:
6831 mips_emit_delays (TRUE);
6832 ++mips_opts.noreorder;
6833 mips_any_noreorder = 1;
6834 if (imm)
6835 load_register (AT, &imm_expr, dbl);
6836 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6837 macro_build (NULL, "mflo", "d", dreg);
6838 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6839 macro_build (NULL, "mfhi", "d", AT);
6840 if (mips_trap)
6841 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6842 else
6843 {
6844 expr1.X_add_number = 8;
6845 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6846 macro_build (NULL, "nop", "", 0);
6847 macro_build (NULL, "break", "c", 6);
6848 }
6849 --mips_opts.noreorder;
6850 macro_build (NULL, "mflo", "d", dreg);
6851 break;
6852
6853 case M_DMULOU_I:
6854 dbl = 1;
6855 case M_MULOU_I:
6856 imm = 1;
6857 goto do_mulou;
6858
6859 case M_DMULOU:
6860 dbl = 1;
6861 case M_MULOU:
6862 do_mulou:
6863 mips_emit_delays (TRUE);
6864 ++mips_opts.noreorder;
6865 mips_any_noreorder = 1;
6866 if (imm)
6867 load_register (AT, &imm_expr, dbl);
6868 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6869 sreg, imm ? AT : treg);
6870 macro_build (NULL, "mfhi", "d", AT);
6871 macro_build (NULL, "mflo", "d", dreg);
6872 if (mips_trap)
6873 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6874 else
6875 {
6876 expr1.X_add_number = 8;
6877 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6878 macro_build (NULL, "nop", "", 0);
6879 macro_build (NULL, "break", "c", 6);
6880 }
6881 --mips_opts.noreorder;
6882 break;
6883
6884 case M_DROL:
6885 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6886 {
6887 if (dreg == sreg)
6888 {
6889 tempreg = AT;
6890 used_at = 1;
6891 }
6892 else
6893 {
6894 tempreg = dreg;
6895 used_at = 0;
6896 }
6897 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6898 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6899 if (used_at)
6900 break;
6901 return;
6902 }
6903 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6904 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6905 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6906 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6907 break;
6908
6909 case M_ROL:
6910 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6911 {
6912 if (dreg == sreg)
6913 {
6914 tempreg = AT;
6915 used_at = 1;
6916 }
6917 else
6918 {
6919 tempreg = dreg;
6920 used_at = 0;
6921 }
6922 macro_build (NULL, "negu", "d,w", tempreg, treg);
6923 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6924 if (used_at)
6925 break;
6926 return;
6927 }
6928 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6929 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6930 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6931 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6932 break;
6933
6934 case M_DROL_I:
6935 {
6936 unsigned int rot;
6937 char *l, *r;
6938
6939 if (imm_expr.X_op != O_constant)
6940 as_bad (_("Improper rotate count"));
6941 rot = imm_expr.X_add_number & 0x3f;
6942 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6943 {
6944 rot = (64 - rot) & 0x3f;
6945 if (rot >= 32)
6946 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6947 else
6948 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6949 return;
6950 }
6951 if (rot == 0)
6952 {
6953 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6954 return;
6955 }
6956 l = (rot < 0x20) ? "dsll" : "dsll32";
6957 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6958 rot &= 0x1f;
6959 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6960 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6961 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6962 }
6963 break;
6964
6965 case M_ROL_I:
6966 {
6967 unsigned int rot;
6968
6969 if (imm_expr.X_op != O_constant)
6970 as_bad (_("Improper rotate count"));
6971 rot = imm_expr.X_add_number & 0x1f;
6972 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6973 {
6974 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6975 return;
6976 }
6977 if (rot == 0)
6978 {
6979 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6980 return;
6981 }
6982 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6983 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6984 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6985 }
6986 break;
6987
6988 case M_DROR:
6989 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6990 {
6991 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6992 return;
6993 }
6994 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6995 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6996 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6997 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6998 break;
6999
7000 case M_ROR:
7001 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7002 {
7003 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7004 return;
7005 }
7006 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7007 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7008 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7009 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7010 break;
7011
7012 case M_DROR_I:
7013 {
7014 unsigned int rot;
7015 char *l, *r;
7016
7017 if (imm_expr.X_op != O_constant)
7018 as_bad (_("Improper rotate count"));
7019 rot = imm_expr.X_add_number & 0x3f;
7020 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7021 {
7022 if (rot >= 32)
7023 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7024 else
7025 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7026 return;
7027 }
7028 if (rot == 0)
7029 {
7030 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7031 return;
7032 }
7033 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7034 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7035 rot &= 0x1f;
7036 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7037 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7038 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7039 }
7040 break;
7041
7042 case M_ROR_I:
7043 {
7044 unsigned int rot;
7045
7046 if (imm_expr.X_op != O_constant)
7047 as_bad (_("Improper rotate count"));
7048 rot = imm_expr.X_add_number & 0x1f;
7049 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7050 {
7051 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7052 return;
7053 }
7054 if (rot == 0)
7055 {
7056 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7057 return;
7058 }
7059 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7060 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7061 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7062 }
7063 break;
7064
7065 case M_S_DOB:
7066 if (mips_opts.arch == CPU_R4650)
7067 {
7068 as_bad (_("opcode not supported on this processor"));
7069 return;
7070 }
7071 assert (mips_opts.isa == ISA_MIPS1);
7072 /* Even on a big endian machine $fn comes before $fn+1. We have
7073 to adjust when storing to memory. */
7074 macro_build (&offset_expr, "swc1", "T,o(b)",
7075 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7076 offset_expr.X_add_number += 4;
7077 macro_build (&offset_expr, "swc1", "T,o(b)",
7078 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7079 return;
7080
7081 case M_SEQ:
7082 if (sreg == 0)
7083 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7084 else if (treg == 0)
7085 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7086 else
7087 {
7088 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7089 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7090 }
7091 return;
7092
7093 case M_SEQ_I:
7094 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7095 {
7096 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7097 return;
7098 }
7099 if (sreg == 0)
7100 {
7101 as_warn (_("Instruction %s: result is always false"),
7102 ip->insn_mo->name);
7103 move_register (dreg, 0);
7104 return;
7105 }
7106 if (imm_expr.X_op == O_constant
7107 && imm_expr.X_add_number >= 0
7108 && imm_expr.X_add_number < 0x10000)
7109 {
7110 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7111 used_at = 0;
7112 }
7113 else if (imm_expr.X_op == O_constant
7114 && imm_expr.X_add_number > -0x8000
7115 && imm_expr.X_add_number < 0)
7116 {
7117 imm_expr.X_add_number = -imm_expr.X_add_number;
7118 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7119 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7120 used_at = 0;
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 (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7129 if (used_at)
7130 break;
7131 return;
7132
7133 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7134 s = "slt";
7135 goto sge;
7136 case M_SGEU:
7137 s = "sltu";
7138 sge:
7139 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7140 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7141 return;
7142
7143 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7144 case M_SGEU_I:
7145 if (imm_expr.X_op == O_constant
7146 && imm_expr.X_add_number >= -0x8000
7147 && imm_expr.X_add_number < 0x8000)
7148 {
7149 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7150 dreg, sreg, BFD_RELOC_LO16);
7151 used_at = 0;
7152 }
7153 else
7154 {
7155 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7156 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7157 dreg, sreg, AT);
7158 used_at = 1;
7159 }
7160 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7161 if (used_at)
7162 break;
7163 return;
7164
7165 case M_SGT: /* sreg > treg <==> treg < sreg */
7166 s = "slt";
7167 goto sgt;
7168 case M_SGTU:
7169 s = "sltu";
7170 sgt:
7171 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7172 return;
7173
7174 case M_SGT_I: /* sreg > I <==> I < sreg */
7175 s = "slt";
7176 goto sgti;
7177 case M_SGTU_I:
7178 s = "sltu";
7179 sgti:
7180 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7181 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7182 break;
7183
7184 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7185 s = "slt";
7186 goto sle;
7187 case M_SLEU:
7188 s = "sltu";
7189 sle:
7190 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7191 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7192 return;
7193
7194 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7195 s = "slt";
7196 goto slei;
7197 case M_SLEU_I:
7198 s = "sltu";
7199 slei:
7200 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7201 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7202 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7203 break;
7204
7205 case M_SLT_I:
7206 if (imm_expr.X_op == O_constant
7207 && imm_expr.X_add_number >= -0x8000
7208 && imm_expr.X_add_number < 0x8000)
7209 {
7210 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7211 return;
7212 }
7213 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7214 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7215 break;
7216
7217 case M_SLTU_I:
7218 if (imm_expr.X_op == O_constant
7219 && imm_expr.X_add_number >= -0x8000
7220 && imm_expr.X_add_number < 0x8000)
7221 {
7222 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7223 BFD_RELOC_LO16);
7224 return;
7225 }
7226 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7227 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7228 break;
7229
7230 case M_SNE:
7231 if (sreg == 0)
7232 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7233 else if (treg == 0)
7234 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7235 else
7236 {
7237 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7238 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7239 }
7240 return;
7241
7242 case M_SNE_I:
7243 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7244 {
7245 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7246 return;
7247 }
7248 if (sreg == 0)
7249 {
7250 as_warn (_("Instruction %s: result is always true"),
7251 ip->insn_mo->name);
7252 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7253 dreg, 0, BFD_RELOC_LO16);
7254 return;
7255 }
7256 if (imm_expr.X_op == O_constant
7257 && imm_expr.X_add_number >= 0
7258 && imm_expr.X_add_number < 0x10000)
7259 {
7260 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7261 used_at = 0;
7262 }
7263 else if (imm_expr.X_op == O_constant
7264 && imm_expr.X_add_number > -0x8000
7265 && imm_expr.X_add_number < 0)
7266 {
7267 imm_expr.X_add_number = -imm_expr.X_add_number;
7268 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7269 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7270 used_at = 0;
7271 }
7272 else
7273 {
7274 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7275 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7276 used_at = 1;
7277 }
7278 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7279 if (used_at)
7280 break;
7281 return;
7282
7283 case M_DSUB_I:
7284 dbl = 1;
7285 case M_SUB_I:
7286 if (imm_expr.X_op == O_constant
7287 && imm_expr.X_add_number > -0x8000
7288 && imm_expr.X_add_number <= 0x8000)
7289 {
7290 imm_expr.X_add_number = -imm_expr.X_add_number;
7291 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7292 dreg, sreg, BFD_RELOC_LO16);
7293 return;
7294 }
7295 load_register (AT, &imm_expr, dbl);
7296 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7297 break;
7298
7299 case M_DSUBU_I:
7300 dbl = 1;
7301 case M_SUBU_I:
7302 if (imm_expr.X_op == O_constant
7303 && imm_expr.X_add_number > -0x8000
7304 && imm_expr.X_add_number <= 0x8000)
7305 {
7306 imm_expr.X_add_number = -imm_expr.X_add_number;
7307 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7308 dreg, sreg, BFD_RELOC_LO16);
7309 return;
7310 }
7311 load_register (AT, &imm_expr, dbl);
7312 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7313 break;
7314
7315 case M_TEQ_I:
7316 s = "teq";
7317 goto trap;
7318 case M_TGE_I:
7319 s = "tge";
7320 goto trap;
7321 case M_TGEU_I:
7322 s = "tgeu";
7323 goto trap;
7324 case M_TLT_I:
7325 s = "tlt";
7326 goto trap;
7327 case M_TLTU_I:
7328 s = "tltu";
7329 goto trap;
7330 case M_TNE_I:
7331 s = "tne";
7332 trap:
7333 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7334 macro_build (NULL, s, "s,t", sreg, AT);
7335 break;
7336
7337 case M_TRUNCWS:
7338 case M_TRUNCWD:
7339 assert (mips_opts.isa == ISA_MIPS1);
7340 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7341 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7342
7343 /*
7344 * Is the double cfc1 instruction a bug in the mips assembler;
7345 * or is there a reason for it?
7346 */
7347 mips_emit_delays (TRUE);
7348 ++mips_opts.noreorder;
7349 mips_any_noreorder = 1;
7350 macro_build (NULL, "cfc1", "t,G", treg, RA);
7351 macro_build (NULL, "cfc1", "t,G", treg, RA);
7352 macro_build (NULL, "nop", "");
7353 expr1.X_add_number = 3;
7354 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7355 expr1.X_add_number = 2;
7356 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7357 macro_build (NULL, "ctc1", "t,G", AT, RA);
7358 macro_build (NULL, "nop", "");
7359 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7360 dreg, sreg);
7361 macro_build (NULL, "ctc1", "t,G", treg, RA);
7362 macro_build (NULL, "nop", "");
7363 --mips_opts.noreorder;
7364 break;
7365
7366 case M_ULH:
7367 s = "lb";
7368 goto ulh;
7369 case M_ULHU:
7370 s = "lbu";
7371 ulh:
7372 if (offset_expr.X_add_number >= 0x7fff)
7373 as_bad (_("operand overflow"));
7374 if (! target_big_endian)
7375 ++offset_expr.X_add_number;
7376 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7377 if (! target_big_endian)
7378 --offset_expr.X_add_number;
7379 else
7380 ++offset_expr.X_add_number;
7381 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7382 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7383 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7384 break;
7385
7386 case M_ULD:
7387 s = "ldl";
7388 s2 = "ldr";
7389 off = 7;
7390 goto ulw;
7391 case M_ULW:
7392 s = "lwl";
7393 s2 = "lwr";
7394 off = 3;
7395 ulw:
7396 if (offset_expr.X_add_number >= 0x8000 - off)
7397 as_bad (_("operand overflow"));
7398 if (treg != breg)
7399 tempreg = treg;
7400 else
7401 tempreg = AT;
7402 if (! target_big_endian)
7403 offset_expr.X_add_number += off;
7404 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7405 if (! target_big_endian)
7406 offset_expr.X_add_number -= off;
7407 else
7408 offset_expr.X_add_number += off;
7409 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7410
7411 /* If necessary, move the result in tempreg the final destination. */
7412 if (treg == tempreg)
7413 return;
7414 /* Protect second load's delay slot. */
7415 load_delay_nop ();
7416 move_register (treg, tempreg);
7417 break;
7418
7419 case M_ULD_A:
7420 s = "ldl";
7421 s2 = "ldr";
7422 off = 7;
7423 goto ulwa;
7424 case M_ULW_A:
7425 s = "lwl";
7426 s2 = "lwr";
7427 off = 3;
7428 ulwa:
7429 used_at = 1;
7430 load_address (AT, &offset_expr, &used_at);
7431 if (breg != 0)
7432 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7433 if (! target_big_endian)
7434 expr1.X_add_number = off;
7435 else
7436 expr1.X_add_number = 0;
7437 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7438 if (! target_big_endian)
7439 expr1.X_add_number = 0;
7440 else
7441 expr1.X_add_number = off;
7442 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7443 break;
7444
7445 case M_ULH_A:
7446 case M_ULHU_A:
7447 used_at = 1;
7448 load_address (AT, &offset_expr, &used_at);
7449 if (breg != 0)
7450 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7451 if (target_big_endian)
7452 expr1.X_add_number = 0;
7453 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7454 treg, BFD_RELOC_LO16, AT);
7455 if (target_big_endian)
7456 expr1.X_add_number = 1;
7457 else
7458 expr1.X_add_number = 0;
7459 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7460 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7461 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7462 break;
7463
7464 case M_USH:
7465 if (offset_expr.X_add_number >= 0x7fff)
7466 as_bad (_("operand overflow"));
7467 if (target_big_endian)
7468 ++offset_expr.X_add_number;
7469 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7470 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7471 if (target_big_endian)
7472 --offset_expr.X_add_number;
7473 else
7474 ++offset_expr.X_add_number;
7475 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7476 break;
7477
7478 case M_USD:
7479 s = "sdl";
7480 s2 = "sdr";
7481 off = 7;
7482 goto usw;
7483 case M_USW:
7484 s = "swl";
7485 s2 = "swr";
7486 off = 3;
7487 usw:
7488 if (offset_expr.X_add_number >= 0x8000 - off)
7489 as_bad (_("operand overflow"));
7490 if (! target_big_endian)
7491 offset_expr.X_add_number += off;
7492 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7493 if (! target_big_endian)
7494 offset_expr.X_add_number -= off;
7495 else
7496 offset_expr.X_add_number += off;
7497 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7498 return;
7499
7500 case M_USD_A:
7501 s = "sdl";
7502 s2 = "sdr";
7503 off = 7;
7504 goto uswa;
7505 case M_USW_A:
7506 s = "swl";
7507 s2 = "swr";
7508 off = 3;
7509 uswa:
7510 used_at = 1;
7511 load_address (AT, &offset_expr, &used_at);
7512 if (breg != 0)
7513 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7514 if (! target_big_endian)
7515 expr1.X_add_number = off;
7516 else
7517 expr1.X_add_number = 0;
7518 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7519 if (! target_big_endian)
7520 expr1.X_add_number = 0;
7521 else
7522 expr1.X_add_number = off;
7523 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7524 break;
7525
7526 case M_USH_A:
7527 used_at = 1;
7528 load_address (AT, &offset_expr, &used_at);
7529 if (breg != 0)
7530 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7531 if (! target_big_endian)
7532 expr1.X_add_number = 0;
7533 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7534 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7535 if (! target_big_endian)
7536 expr1.X_add_number = 1;
7537 else
7538 expr1.X_add_number = 0;
7539 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7540 if (! target_big_endian)
7541 expr1.X_add_number = 0;
7542 else
7543 expr1.X_add_number = 1;
7544 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7545 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7546 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7547 break;
7548
7549 default:
7550 /* FIXME: Check if this is one of the itbl macros, since they
7551 are added dynamically. */
7552 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7553 break;
7554 }
7555 if (mips_opts.noat)
7556 as_warn (_("Macro used $at after \".set noat\""));
7557 }
7558
7559 /* Implement macros in mips16 mode. */
7560
7561 static void
7562 mips16_macro (struct mips_cl_insn *ip)
7563 {
7564 int mask;
7565 int xreg, yreg, zreg, tmp;
7566 expressionS expr1;
7567 int dbl;
7568 const char *s, *s2, *s3;
7569
7570 mask = ip->insn_mo->mask;
7571
7572 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7573 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7574 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7575
7576 expr1.X_op = O_constant;
7577 expr1.X_op_symbol = NULL;
7578 expr1.X_add_symbol = NULL;
7579 expr1.X_add_number = 1;
7580
7581 dbl = 0;
7582
7583 switch (mask)
7584 {
7585 default:
7586 internalError ();
7587
7588 case M_DDIV_3:
7589 dbl = 1;
7590 case M_DIV_3:
7591 s = "mflo";
7592 goto do_div3;
7593 case M_DREM_3:
7594 dbl = 1;
7595 case M_REM_3:
7596 s = "mfhi";
7597 do_div3:
7598 mips_emit_delays (TRUE);
7599 ++mips_opts.noreorder;
7600 mips_any_noreorder = 1;
7601 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7602 expr1.X_add_number = 2;
7603 macro_build (&expr1, "bnez", "x,p", yreg);
7604 macro_build (NULL, "break", "6", 7);
7605
7606 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7607 since that causes an overflow. We should do that as well,
7608 but I don't see how to do the comparisons without a temporary
7609 register. */
7610 --mips_opts.noreorder;
7611 macro_build (NULL, s, "x", zreg);
7612 break;
7613
7614 case M_DIVU_3:
7615 s = "divu";
7616 s2 = "mflo";
7617 goto do_divu3;
7618 case M_REMU_3:
7619 s = "divu";
7620 s2 = "mfhi";
7621 goto do_divu3;
7622 case M_DDIVU_3:
7623 s = "ddivu";
7624 s2 = "mflo";
7625 goto do_divu3;
7626 case M_DREMU_3:
7627 s = "ddivu";
7628 s2 = "mfhi";
7629 do_divu3:
7630 mips_emit_delays (TRUE);
7631 ++mips_opts.noreorder;
7632 mips_any_noreorder = 1;
7633 macro_build (NULL, s, "0,x,y", xreg, yreg);
7634 expr1.X_add_number = 2;
7635 macro_build (&expr1, "bnez", "x,p", yreg);
7636 macro_build (NULL, "break", "6", 7);
7637 --mips_opts.noreorder;
7638 macro_build (NULL, s2, "x", zreg);
7639 break;
7640
7641 case M_DMUL:
7642 dbl = 1;
7643 case M_MUL:
7644 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7645 macro_build (NULL, "mflo", "x", zreg);
7646 return;
7647
7648 case M_DSUBU_I:
7649 dbl = 1;
7650 goto do_subu;
7651 case M_SUBU_I:
7652 do_subu:
7653 if (imm_expr.X_op != O_constant)
7654 as_bad (_("Unsupported large constant"));
7655 imm_expr.X_add_number = -imm_expr.X_add_number;
7656 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7657 break;
7658
7659 case M_SUBU_I_2:
7660 if (imm_expr.X_op != O_constant)
7661 as_bad (_("Unsupported large constant"));
7662 imm_expr.X_add_number = -imm_expr.X_add_number;
7663 macro_build (&imm_expr, "addiu", "x,k", xreg);
7664 break;
7665
7666 case M_DSUBU_I_2:
7667 if (imm_expr.X_op != O_constant)
7668 as_bad (_("Unsupported large constant"));
7669 imm_expr.X_add_number = -imm_expr.X_add_number;
7670 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7671 break;
7672
7673 case M_BEQ:
7674 s = "cmp";
7675 s2 = "bteqz";
7676 goto do_branch;
7677 case M_BNE:
7678 s = "cmp";
7679 s2 = "btnez";
7680 goto do_branch;
7681 case M_BLT:
7682 s = "slt";
7683 s2 = "btnez";
7684 goto do_branch;
7685 case M_BLTU:
7686 s = "sltu";
7687 s2 = "btnez";
7688 goto do_branch;
7689 case M_BLE:
7690 s = "slt";
7691 s2 = "bteqz";
7692 goto do_reverse_branch;
7693 case M_BLEU:
7694 s = "sltu";
7695 s2 = "bteqz";
7696 goto do_reverse_branch;
7697 case M_BGE:
7698 s = "slt";
7699 s2 = "bteqz";
7700 goto do_branch;
7701 case M_BGEU:
7702 s = "sltu";
7703 s2 = "bteqz";
7704 goto do_branch;
7705 case M_BGT:
7706 s = "slt";
7707 s2 = "btnez";
7708 goto do_reverse_branch;
7709 case M_BGTU:
7710 s = "sltu";
7711 s2 = "btnez";
7712
7713 do_reverse_branch:
7714 tmp = xreg;
7715 xreg = yreg;
7716 yreg = tmp;
7717
7718 do_branch:
7719 macro_build (NULL, s, "x,y", xreg, yreg);
7720 macro_build (&offset_expr, s2, "p");
7721 break;
7722
7723 case M_BEQ_I:
7724 s = "cmpi";
7725 s2 = "bteqz";
7726 s3 = "x,U";
7727 goto do_branch_i;
7728 case M_BNE_I:
7729 s = "cmpi";
7730 s2 = "btnez";
7731 s3 = "x,U";
7732 goto do_branch_i;
7733 case M_BLT_I:
7734 s = "slti";
7735 s2 = "btnez";
7736 s3 = "x,8";
7737 goto do_branch_i;
7738 case M_BLTU_I:
7739 s = "sltiu";
7740 s2 = "btnez";
7741 s3 = "x,8";
7742 goto do_branch_i;
7743 case M_BLE_I:
7744 s = "slti";
7745 s2 = "btnez";
7746 s3 = "x,8";
7747 goto do_addone_branch_i;
7748 case M_BLEU_I:
7749 s = "sltiu";
7750 s2 = "btnez";
7751 s3 = "x,8";
7752 goto do_addone_branch_i;
7753 case M_BGE_I:
7754 s = "slti";
7755 s2 = "bteqz";
7756 s3 = "x,8";
7757 goto do_branch_i;
7758 case M_BGEU_I:
7759 s = "sltiu";
7760 s2 = "bteqz";
7761 s3 = "x,8";
7762 goto do_branch_i;
7763 case M_BGT_I:
7764 s = "slti";
7765 s2 = "bteqz";
7766 s3 = "x,8";
7767 goto do_addone_branch_i;
7768 case M_BGTU_I:
7769 s = "sltiu";
7770 s2 = "bteqz";
7771 s3 = "x,8";
7772
7773 do_addone_branch_i:
7774 if (imm_expr.X_op != O_constant)
7775 as_bad (_("Unsupported large constant"));
7776 ++imm_expr.X_add_number;
7777
7778 do_branch_i:
7779 macro_build (&imm_expr, s, s3, xreg);
7780 macro_build (&offset_expr, s2, "p");
7781 break;
7782
7783 case M_ABS:
7784 expr1.X_add_number = 0;
7785 macro_build (&expr1, "slti", "x,8", yreg);
7786 if (xreg != yreg)
7787 move_register (xreg, yreg);
7788 expr1.X_add_number = 2;
7789 macro_build (&expr1, "bteqz", "p");
7790 macro_build (NULL, "neg", "x,w", xreg, xreg);
7791 }
7792 }
7793
7794 /* For consistency checking, verify that all bits are specified either
7795 by the match/mask part of the instruction definition, or by the
7796 operand list. */
7797 static int
7798 validate_mips_insn (const struct mips_opcode *opc)
7799 {
7800 const char *p = opc->args;
7801 char c;
7802 unsigned long used_bits = opc->mask;
7803
7804 if ((used_bits & opc->match) != opc->match)
7805 {
7806 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7807 opc->name, opc->args);
7808 return 0;
7809 }
7810 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7811 while (*p)
7812 switch (c = *p++)
7813 {
7814 case ',': break;
7815 case '(': break;
7816 case ')': break;
7817 case '+':
7818 switch (c = *p++)
7819 {
7820 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7821 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7822 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7823 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7824 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7825 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7826 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7827 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7828 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7829 case 'I': break;
7830 default:
7831 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7832 c, opc->name, opc->args);
7833 return 0;
7834 }
7835 break;
7836 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7837 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7838 case 'A': break;
7839 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7840 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7841 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7842 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7843 case 'F': break;
7844 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7845 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7846 case 'I': break;
7847 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7848 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7849 case 'L': break;
7850 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7851 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7852 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7853 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7854 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7855 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7856 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7857 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7858 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7859 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7860 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7861 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7862 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7863 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7864 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7865 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7866 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7867 case 'f': break;
7868 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7869 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7870 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7871 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7872 case 'l': break;
7873 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7874 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7875 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7876 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7877 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7878 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7879 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7880 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7881 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7882 case 'x': break;
7883 case 'z': break;
7884 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7885 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7886 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7887 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7888 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7889 case '[': break;
7890 case ']': break;
7891 default:
7892 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7893 c, opc->name, opc->args);
7894 return 0;
7895 }
7896 #undef USE_BITS
7897 if (used_bits != 0xffffffff)
7898 {
7899 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7900 ~used_bits & 0xffffffff, opc->name, opc->args);
7901 return 0;
7902 }
7903 return 1;
7904 }
7905
7906 /* This routine assembles an instruction into its binary format. As a
7907 side effect, it sets one of the global variables imm_reloc or
7908 offset_reloc to the type of relocation to do if one of the operands
7909 is an address expression. */
7910
7911 static void
7912 mips_ip (char *str, struct mips_cl_insn *ip)
7913 {
7914 char *s;
7915 const char *args;
7916 char c = 0;
7917 struct mips_opcode *insn;
7918 char *argsStart;
7919 unsigned int regno;
7920 unsigned int lastregno = 0;
7921 unsigned int lastpos = 0;
7922 unsigned int limlo, limhi;
7923 char *s_reset;
7924 char save_c = 0;
7925
7926 insn_error = NULL;
7927
7928 /* If the instruction contains a '.', we first try to match an instruction
7929 including the '.'. Then we try again without the '.'. */
7930 insn = NULL;
7931 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7932 continue;
7933
7934 /* If we stopped on whitespace, then replace the whitespace with null for
7935 the call to hash_find. Save the character we replaced just in case we
7936 have to re-parse the instruction. */
7937 if (ISSPACE (*s))
7938 {
7939 save_c = *s;
7940 *s++ = '\0';
7941 }
7942
7943 insn = (struct mips_opcode *) hash_find (op_hash, str);
7944
7945 /* If we didn't find the instruction in the opcode table, try again, but
7946 this time with just the instruction up to, but not including the
7947 first '.'. */
7948 if (insn == NULL)
7949 {
7950 /* Restore the character we overwrite above (if any). */
7951 if (save_c)
7952 *(--s) = save_c;
7953
7954 /* Scan up to the first '.' or whitespace. */
7955 for (s = str;
7956 *s != '\0' && *s != '.' && !ISSPACE (*s);
7957 ++s)
7958 continue;
7959
7960 /* If we did not find a '.', then we can quit now. */
7961 if (*s != '.')
7962 {
7963 insn_error = "unrecognized opcode";
7964 return;
7965 }
7966
7967 /* Lookup the instruction in the hash table. */
7968 *s++ = '\0';
7969 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7970 {
7971 insn_error = "unrecognized opcode";
7972 return;
7973 }
7974 }
7975
7976 argsStart = s;
7977 for (;;)
7978 {
7979 bfd_boolean ok;
7980
7981 assert (strcmp (insn->name, str) == 0);
7982
7983 if (OPCODE_IS_MEMBER (insn,
7984 (mips_opts.isa
7985 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7986 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7987 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7988 mips_opts.arch))
7989 ok = TRUE;
7990 else
7991 ok = FALSE;
7992
7993 if (insn->pinfo != INSN_MACRO)
7994 {
7995 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7996 ok = FALSE;
7997 }
7998
7999 if (! ok)
8000 {
8001 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8002 && strcmp (insn->name, insn[1].name) == 0)
8003 {
8004 ++insn;
8005 continue;
8006 }
8007 else
8008 {
8009 if (!insn_error)
8010 {
8011 static char buf[100];
8012 sprintf (buf,
8013 _("opcode not supported on this processor: %s (%s)"),
8014 mips_cpu_info_from_arch (mips_opts.arch)->name,
8015 mips_cpu_info_from_isa (mips_opts.isa)->name);
8016 insn_error = buf;
8017 }
8018 if (save_c)
8019 *(--s) = save_c;
8020 return;
8021 }
8022 }
8023
8024 ip->insn_mo = insn;
8025 ip->insn_opcode = insn->match;
8026 insn_error = NULL;
8027 for (args = insn->args;; ++args)
8028 {
8029 int is_mdmx;
8030
8031 s += strspn (s, " \t");
8032 is_mdmx = 0;
8033 switch (*args)
8034 {
8035 case '\0': /* end of args */
8036 if (*s == '\0')
8037 return;
8038 break;
8039
8040 case ',':
8041 if (*s++ == *args)
8042 continue;
8043 s--;
8044 switch (*++args)
8045 {
8046 case 'r':
8047 case 'v':
8048 ip->insn_opcode |= lastregno << OP_SH_RS;
8049 continue;
8050
8051 case 'w':
8052 ip->insn_opcode |= lastregno << OP_SH_RT;
8053 continue;
8054
8055 case 'W':
8056 ip->insn_opcode |= lastregno << OP_SH_FT;
8057 continue;
8058
8059 case 'V':
8060 ip->insn_opcode |= lastregno << OP_SH_FS;
8061 continue;
8062 }
8063 break;
8064
8065 case '(':
8066 /* Handle optional base register.
8067 Either the base register is omitted or
8068 we must have a left paren. */
8069 /* This is dependent on the next operand specifier
8070 is a base register specification. */
8071 assert (args[1] == 'b' || args[1] == '5'
8072 || args[1] == '-' || args[1] == '4');
8073 if (*s == '\0')
8074 return;
8075
8076 case ')': /* these must match exactly */
8077 case '[':
8078 case ']':
8079 if (*s++ == *args)
8080 continue;
8081 break;
8082
8083 case '+': /* Opcode extension character. */
8084 switch (*++args)
8085 {
8086 case 'A': /* ins/ext position, becomes LSB. */
8087 limlo = 0;
8088 limhi = 31;
8089 goto do_lsb;
8090 case 'E':
8091 limlo = 32;
8092 limhi = 63;
8093 goto do_lsb;
8094 do_lsb:
8095 my_getExpression (&imm_expr, s);
8096 check_absolute_expr (ip, &imm_expr);
8097 if ((unsigned long) imm_expr.X_add_number < limlo
8098 || (unsigned long) imm_expr.X_add_number > limhi)
8099 {
8100 as_bad (_("Improper position (%lu)"),
8101 (unsigned long) imm_expr.X_add_number);
8102 imm_expr.X_add_number = limlo;
8103 }
8104 lastpos = imm_expr.X_add_number;
8105 ip->insn_opcode |= (imm_expr.X_add_number
8106 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8107 imm_expr.X_op = O_absent;
8108 s = expr_end;
8109 continue;
8110
8111 case 'B': /* ins size, becomes MSB. */
8112 limlo = 1;
8113 limhi = 32;
8114 goto do_msb;
8115 case 'F':
8116 limlo = 33;
8117 limhi = 64;
8118 goto do_msb;
8119 do_msb:
8120 my_getExpression (&imm_expr, s);
8121 check_absolute_expr (ip, &imm_expr);
8122 /* Check for negative input so that small negative numbers
8123 will not succeed incorrectly. The checks against
8124 (pos+size) transitively check "size" itself,
8125 assuming that "pos" is reasonable. */
8126 if ((long) imm_expr.X_add_number < 0
8127 || ((unsigned long) imm_expr.X_add_number
8128 + lastpos) < limlo
8129 || ((unsigned long) imm_expr.X_add_number
8130 + lastpos) > limhi)
8131 {
8132 as_bad (_("Improper insert size (%lu, position %lu)"),
8133 (unsigned long) imm_expr.X_add_number,
8134 (unsigned long) lastpos);
8135 imm_expr.X_add_number = limlo - lastpos;
8136 }
8137 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8138 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8139 imm_expr.X_op = O_absent;
8140 s = expr_end;
8141 continue;
8142
8143 case 'C': /* ext size, becomes MSBD. */
8144 limlo = 1;
8145 limhi = 32;
8146 goto do_msbd;
8147 case 'G':
8148 limlo = 33;
8149 limhi = 64;
8150 goto do_msbd;
8151 case 'H':
8152 limlo = 33;
8153 limhi = 64;
8154 goto do_msbd;
8155 do_msbd:
8156 my_getExpression (&imm_expr, s);
8157 check_absolute_expr (ip, &imm_expr);
8158 /* Check for negative input so that small negative numbers
8159 will not succeed incorrectly. The checks against
8160 (pos+size) transitively check "size" itself,
8161 assuming that "pos" is reasonable. */
8162 if ((long) imm_expr.X_add_number < 0
8163 || ((unsigned long) imm_expr.X_add_number
8164 + lastpos) < limlo
8165 || ((unsigned long) imm_expr.X_add_number
8166 + lastpos) > limhi)
8167 {
8168 as_bad (_("Improper extract size (%lu, position %lu)"),
8169 (unsigned long) imm_expr.X_add_number,
8170 (unsigned long) lastpos);
8171 imm_expr.X_add_number = limlo - lastpos;
8172 }
8173 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8174 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8175 imm_expr.X_op = O_absent;
8176 s = expr_end;
8177 continue;
8178
8179 case 'D':
8180 /* +D is for disassembly only; never match. */
8181 break;
8182
8183 case 'I':
8184 /* "+I" is like "I", except that imm2_expr is used. */
8185 my_getExpression (&imm2_expr, s);
8186 if (imm2_expr.X_op != O_big
8187 && imm2_expr.X_op != O_constant)
8188 insn_error = _("absolute expression required");
8189 normalize_constant_expr (&imm2_expr);
8190 s = expr_end;
8191 continue;
8192
8193 default:
8194 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8195 *args, insn->name, insn->args);
8196 /* Further processing is fruitless. */
8197 return;
8198 }
8199 break;
8200
8201 case '<': /* must be at least one digit */
8202 /*
8203 * According to the manual, if the shift amount is greater
8204 * than 31 or less than 0, then the shift amount should be
8205 * mod 32. In reality the mips assembler issues an error.
8206 * We issue a warning and mask out all but the low 5 bits.
8207 */
8208 my_getExpression (&imm_expr, s);
8209 check_absolute_expr (ip, &imm_expr);
8210 if ((unsigned long) imm_expr.X_add_number > 31)
8211 {
8212 as_warn (_("Improper shift amount (%lu)"),
8213 (unsigned long) imm_expr.X_add_number);
8214 imm_expr.X_add_number &= OP_MASK_SHAMT;
8215 }
8216 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8217 imm_expr.X_op = O_absent;
8218 s = expr_end;
8219 continue;
8220
8221 case '>': /* shift amount minus 32 */
8222 my_getExpression (&imm_expr, s);
8223 check_absolute_expr (ip, &imm_expr);
8224 if ((unsigned long) imm_expr.X_add_number < 32
8225 || (unsigned long) imm_expr.X_add_number > 63)
8226 break;
8227 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8228 imm_expr.X_op = O_absent;
8229 s = expr_end;
8230 continue;
8231
8232 case 'k': /* cache code */
8233 case 'h': /* prefx code */
8234 my_getExpression (&imm_expr, s);
8235 check_absolute_expr (ip, &imm_expr);
8236 if ((unsigned long) imm_expr.X_add_number > 31)
8237 {
8238 as_warn (_("Invalid value for `%s' (%lu)"),
8239 ip->insn_mo->name,
8240 (unsigned long) imm_expr.X_add_number);
8241 imm_expr.X_add_number &= 0x1f;
8242 }
8243 if (*args == 'k')
8244 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8245 else
8246 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8247 imm_expr.X_op = O_absent;
8248 s = expr_end;
8249 continue;
8250
8251 case 'c': /* break code */
8252 my_getExpression (&imm_expr, s);
8253 check_absolute_expr (ip, &imm_expr);
8254 if ((unsigned long) imm_expr.X_add_number > 1023)
8255 {
8256 as_warn (_("Illegal break code (%lu)"),
8257 (unsigned long) imm_expr.X_add_number);
8258 imm_expr.X_add_number &= OP_MASK_CODE;
8259 }
8260 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8261 imm_expr.X_op = O_absent;
8262 s = expr_end;
8263 continue;
8264
8265 case 'q': /* lower break code */
8266 my_getExpression (&imm_expr, s);
8267 check_absolute_expr (ip, &imm_expr);
8268 if ((unsigned long) imm_expr.X_add_number > 1023)
8269 {
8270 as_warn (_("Illegal lower break code (%lu)"),
8271 (unsigned long) imm_expr.X_add_number);
8272 imm_expr.X_add_number &= OP_MASK_CODE2;
8273 }
8274 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8275 imm_expr.X_op = O_absent;
8276 s = expr_end;
8277 continue;
8278
8279 case 'B': /* 20-bit syscall/break code. */
8280 my_getExpression (&imm_expr, s);
8281 check_absolute_expr (ip, &imm_expr);
8282 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8283 as_warn (_("Illegal 20-bit code (%lu)"),
8284 (unsigned long) imm_expr.X_add_number);
8285 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8286 imm_expr.X_op = O_absent;
8287 s = expr_end;
8288 continue;
8289
8290 case 'C': /* Coprocessor code */
8291 my_getExpression (&imm_expr, s);
8292 check_absolute_expr (ip, &imm_expr);
8293 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8294 {
8295 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8296 (unsigned long) imm_expr.X_add_number);
8297 imm_expr.X_add_number &= ((1 << 25) - 1);
8298 }
8299 ip->insn_opcode |= imm_expr.X_add_number;
8300 imm_expr.X_op = O_absent;
8301 s = expr_end;
8302 continue;
8303
8304 case 'J': /* 19-bit wait code. */
8305 my_getExpression (&imm_expr, s);
8306 check_absolute_expr (ip, &imm_expr);
8307 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8308 as_warn (_("Illegal 19-bit code (%lu)"),
8309 (unsigned long) imm_expr.X_add_number);
8310 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8311 imm_expr.X_op = O_absent;
8312 s = expr_end;
8313 continue;
8314
8315 case 'P': /* Performance register */
8316 my_getExpression (&imm_expr, s);
8317 check_absolute_expr (ip, &imm_expr);
8318 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8319 {
8320 as_warn (_("Invalid performance register (%lu)"),
8321 (unsigned long) imm_expr.X_add_number);
8322 imm_expr.X_add_number &= OP_MASK_PERFREG;
8323 }
8324 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8325 imm_expr.X_op = O_absent;
8326 s = expr_end;
8327 continue;
8328
8329 case 'b': /* base register */
8330 case 'd': /* destination register */
8331 case 's': /* source register */
8332 case 't': /* target register */
8333 case 'r': /* both target and source */
8334 case 'v': /* both dest and source */
8335 case 'w': /* both dest and target */
8336 case 'E': /* coprocessor target register */
8337 case 'G': /* coprocessor destination register */
8338 case 'K': /* 'rdhwr' destination register */
8339 case 'x': /* ignore register name */
8340 case 'z': /* must be zero register */
8341 case 'U': /* destination register (clo/clz). */
8342 s_reset = s;
8343 if (s[0] == '$')
8344 {
8345
8346 if (ISDIGIT (s[1]))
8347 {
8348 ++s;
8349 regno = 0;
8350 do
8351 {
8352 regno *= 10;
8353 regno += *s - '0';
8354 ++s;
8355 }
8356 while (ISDIGIT (*s));
8357 if (regno > 31)
8358 as_bad (_("Invalid register number (%d)"), regno);
8359 }
8360 else if (*args == 'E' || *args == 'G' || *args == 'K')
8361 goto notreg;
8362 else
8363 {
8364 if (s[1] == 'r' && s[2] == 'a')
8365 {
8366 s += 3;
8367 regno = RA;
8368 }
8369 else if (s[1] == 'f' && s[2] == 'p')
8370 {
8371 s += 3;
8372 regno = FP;
8373 }
8374 else if (s[1] == 's' && s[2] == 'p')
8375 {
8376 s += 3;
8377 regno = SP;
8378 }
8379 else if (s[1] == 'g' && s[2] == 'p')
8380 {
8381 s += 3;
8382 regno = GP;
8383 }
8384 else if (s[1] == 'a' && s[2] == 't')
8385 {
8386 s += 3;
8387 regno = AT;
8388 }
8389 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8390 {
8391 s += 4;
8392 regno = KT0;
8393 }
8394 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8395 {
8396 s += 4;
8397 regno = KT1;
8398 }
8399 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8400 {
8401 s += 5;
8402 regno = ZERO;
8403 }
8404 else if (itbl_have_entries)
8405 {
8406 char *p, *n;
8407 unsigned long r;
8408
8409 p = s + 1; /* advance past '$' */
8410 n = itbl_get_field (&p); /* n is name */
8411
8412 /* See if this is a register defined in an
8413 itbl entry. */
8414 if (itbl_get_reg_val (n, &r))
8415 {
8416 /* Get_field advances to the start of
8417 the next field, so we need to back
8418 rack to the end of the last field. */
8419 if (p)
8420 s = p - 1;
8421 else
8422 s = strchr (s, '\0');
8423 regno = r;
8424 }
8425 else
8426 goto notreg;
8427 }
8428 else
8429 goto notreg;
8430 }
8431 if (regno == AT
8432 && ! mips_opts.noat
8433 && *args != 'E'
8434 && *args != 'G'
8435 && *args != 'K')
8436 as_warn (_("Used $at without \".set noat\""));
8437 c = *args;
8438 if (*s == ' ')
8439 ++s;
8440 if (args[1] != *s)
8441 {
8442 if (c == 'r' || c == 'v' || c == 'w')
8443 {
8444 regno = lastregno;
8445 s = s_reset;
8446 ++args;
8447 }
8448 }
8449 /* 'z' only matches $0. */
8450 if (c == 'z' && regno != 0)
8451 break;
8452
8453 /* Now that we have assembled one operand, we use the args string
8454 * to figure out where it goes in the instruction. */
8455 switch (c)
8456 {
8457 case 'r':
8458 case 's':
8459 case 'v':
8460 case 'b':
8461 ip->insn_opcode |= regno << OP_SH_RS;
8462 break;
8463 case 'd':
8464 case 'G':
8465 case 'K':
8466 ip->insn_opcode |= regno << OP_SH_RD;
8467 break;
8468 case 'U':
8469 ip->insn_opcode |= regno << OP_SH_RD;
8470 ip->insn_opcode |= regno << OP_SH_RT;
8471 break;
8472 case 'w':
8473 case 't':
8474 case 'E':
8475 ip->insn_opcode |= regno << OP_SH_RT;
8476 break;
8477 case 'x':
8478 /* This case exists because on the r3000 trunc
8479 expands into a macro which requires a gp
8480 register. On the r6000 or r4000 it is
8481 assembled into a single instruction which
8482 ignores the register. Thus the insn version
8483 is MIPS_ISA2 and uses 'x', and the macro
8484 version is MIPS_ISA1 and uses 't'. */
8485 break;
8486 case 'z':
8487 /* This case is for the div instruction, which
8488 acts differently if the destination argument
8489 is $0. This only matches $0, and is checked
8490 outside the switch. */
8491 break;
8492 case 'D':
8493 /* Itbl operand; not yet implemented. FIXME ?? */
8494 break;
8495 /* What about all other operands like 'i', which
8496 can be specified in the opcode table? */
8497 }
8498 lastregno = regno;
8499 continue;
8500 }
8501 notreg:
8502 switch (*args++)
8503 {
8504 case 'r':
8505 case 'v':
8506 ip->insn_opcode |= lastregno << OP_SH_RS;
8507 continue;
8508 case 'w':
8509 ip->insn_opcode |= lastregno << OP_SH_RT;
8510 continue;
8511 }
8512 break;
8513
8514 case 'O': /* MDMX alignment immediate constant. */
8515 my_getExpression (&imm_expr, s);
8516 check_absolute_expr (ip, &imm_expr);
8517 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8518 {
8519 as_warn ("Improper align amount (%ld), using low bits",
8520 (long) imm_expr.X_add_number);
8521 imm_expr.X_add_number &= OP_MASK_ALN;
8522 }
8523 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8524 imm_expr.X_op = O_absent;
8525 s = expr_end;
8526 continue;
8527
8528 case 'Q': /* MDMX vector, element sel, or const. */
8529 if (s[0] != '$')
8530 {
8531 /* MDMX Immediate. */
8532 my_getExpression (&imm_expr, s);
8533 check_absolute_expr (ip, &imm_expr);
8534 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8535 {
8536 as_warn (_("Invalid MDMX Immediate (%ld)"),
8537 (long) imm_expr.X_add_number);
8538 imm_expr.X_add_number &= OP_MASK_FT;
8539 }
8540 imm_expr.X_add_number &= OP_MASK_FT;
8541 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8542 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8543 else
8544 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8545 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8546 imm_expr.X_op = O_absent;
8547 s = expr_end;
8548 continue;
8549 }
8550 /* Not MDMX Immediate. Fall through. */
8551 case 'X': /* MDMX destination register. */
8552 case 'Y': /* MDMX source register. */
8553 case 'Z': /* MDMX target register. */
8554 is_mdmx = 1;
8555 case 'D': /* floating point destination register */
8556 case 'S': /* floating point source register */
8557 case 'T': /* floating point target register */
8558 case 'R': /* floating point source register */
8559 case 'V':
8560 case 'W':
8561 s_reset = s;
8562 /* Accept $fN for FP and MDMX register numbers, and in
8563 addition accept $vN for MDMX register numbers. */
8564 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8565 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8566 && ISDIGIT (s[2])))
8567 {
8568 s += 2;
8569 regno = 0;
8570 do
8571 {
8572 regno *= 10;
8573 regno += *s - '0';
8574 ++s;
8575 }
8576 while (ISDIGIT (*s));
8577
8578 if (regno > 31)
8579 as_bad (_("Invalid float register number (%d)"), regno);
8580
8581 if ((regno & 1) != 0
8582 && HAVE_32BIT_FPRS
8583 && ! (strcmp (str, "mtc1") == 0
8584 || strcmp (str, "mfc1") == 0
8585 || strcmp (str, "lwc1") == 0
8586 || strcmp (str, "swc1") == 0
8587 || strcmp (str, "l.s") == 0
8588 || strcmp (str, "s.s") == 0))
8589 as_warn (_("Float register should be even, was %d"),
8590 regno);
8591
8592 c = *args;
8593 if (*s == ' ')
8594 ++s;
8595 if (args[1] != *s)
8596 {
8597 if (c == 'V' || c == 'W')
8598 {
8599 regno = lastregno;
8600 s = s_reset;
8601 ++args;
8602 }
8603 }
8604 switch (c)
8605 {
8606 case 'D':
8607 case 'X':
8608 ip->insn_opcode |= regno << OP_SH_FD;
8609 break;
8610 case 'V':
8611 case 'S':
8612 case 'Y':
8613 ip->insn_opcode |= regno << OP_SH_FS;
8614 break;
8615 case 'Q':
8616 /* This is like 'Z', but also needs to fix the MDMX
8617 vector/scalar select bits. Note that the
8618 scalar immediate case is handled above. */
8619 if (*s == '[')
8620 {
8621 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8622 int max_el = (is_qh ? 3 : 7);
8623 s++;
8624 my_getExpression(&imm_expr, s);
8625 check_absolute_expr (ip, &imm_expr);
8626 s = expr_end;
8627 if (imm_expr.X_add_number > max_el)
8628 as_bad(_("Bad element selector %ld"),
8629 (long) imm_expr.X_add_number);
8630 imm_expr.X_add_number &= max_el;
8631 ip->insn_opcode |= (imm_expr.X_add_number
8632 << (OP_SH_VSEL +
8633 (is_qh ? 2 : 1)));
8634 imm_expr.X_op = O_absent;
8635 if (*s != ']')
8636 as_warn(_("Expecting ']' found '%s'"), s);
8637 else
8638 s++;
8639 }
8640 else
8641 {
8642 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8643 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8644 << OP_SH_VSEL);
8645 else
8646 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8647 OP_SH_VSEL);
8648 }
8649 /* Fall through */
8650 case 'W':
8651 case 'T':
8652 case 'Z':
8653 ip->insn_opcode |= regno << OP_SH_FT;
8654 break;
8655 case 'R':
8656 ip->insn_opcode |= regno << OP_SH_FR;
8657 break;
8658 }
8659 lastregno = regno;
8660 continue;
8661 }
8662
8663 switch (*args++)
8664 {
8665 case 'V':
8666 ip->insn_opcode |= lastregno << OP_SH_FS;
8667 continue;
8668 case 'W':
8669 ip->insn_opcode |= lastregno << OP_SH_FT;
8670 continue;
8671 }
8672 break;
8673
8674 case 'I':
8675 my_getExpression (&imm_expr, s);
8676 if (imm_expr.X_op != O_big
8677 && imm_expr.X_op != O_constant)
8678 insn_error = _("absolute expression required");
8679 normalize_constant_expr (&imm_expr);
8680 s = expr_end;
8681 continue;
8682
8683 case 'A':
8684 my_getExpression (&offset_expr, s);
8685 *imm_reloc = BFD_RELOC_32;
8686 s = expr_end;
8687 continue;
8688
8689 case 'F':
8690 case 'L':
8691 case 'f':
8692 case 'l':
8693 {
8694 int f64;
8695 int using_gprs;
8696 char *save_in;
8697 char *err;
8698 unsigned char temp[8];
8699 int len;
8700 unsigned int length;
8701 segT seg;
8702 subsegT subseg;
8703 char *p;
8704
8705 /* These only appear as the last operand in an
8706 instruction, and every instruction that accepts
8707 them in any variant accepts them in all variants.
8708 This means we don't have to worry about backing out
8709 any changes if the instruction does not match.
8710
8711 The difference between them is the size of the
8712 floating point constant and where it goes. For 'F'
8713 and 'L' the constant is 64 bits; for 'f' and 'l' it
8714 is 32 bits. Where the constant is placed is based
8715 on how the MIPS assembler does things:
8716 F -- .rdata
8717 L -- .lit8
8718 f -- immediate value
8719 l -- .lit4
8720
8721 The .lit4 and .lit8 sections are only used if
8722 permitted by the -G argument.
8723
8724 The code below needs to know whether the target register
8725 is 32 or 64 bits wide. It relies on the fact 'f' and
8726 'F' are used with GPR-based instructions and 'l' and
8727 'L' are used with FPR-based instructions. */
8728
8729 f64 = *args == 'F' || *args == 'L';
8730 using_gprs = *args == 'F' || *args == 'f';
8731
8732 save_in = input_line_pointer;
8733 input_line_pointer = s;
8734 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8735 length = len;
8736 s = input_line_pointer;
8737 input_line_pointer = save_in;
8738 if (err != NULL && *err != '\0')
8739 {
8740 as_bad (_("Bad floating point constant: %s"), err);
8741 memset (temp, '\0', sizeof temp);
8742 length = f64 ? 8 : 4;
8743 }
8744
8745 assert (length == (unsigned) (f64 ? 8 : 4));
8746
8747 if (*args == 'f'
8748 || (*args == 'l'
8749 && (g_switch_value < 4
8750 || (temp[0] == 0 && temp[1] == 0)
8751 || (temp[2] == 0 && temp[3] == 0))))
8752 {
8753 imm_expr.X_op = O_constant;
8754 if (! target_big_endian)
8755 imm_expr.X_add_number = bfd_getl32 (temp);
8756 else
8757 imm_expr.X_add_number = bfd_getb32 (temp);
8758 }
8759 else if (length > 4
8760 && ! mips_disable_float_construction
8761 /* Constants can only be constructed in GPRs and
8762 copied to FPRs if the GPRs are at least as wide
8763 as the FPRs. Force the constant into memory if
8764 we are using 64-bit FPRs but the GPRs are only
8765 32 bits wide. */
8766 && (using_gprs
8767 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8768 && ((temp[0] == 0 && temp[1] == 0)
8769 || (temp[2] == 0 && temp[3] == 0))
8770 && ((temp[4] == 0 && temp[5] == 0)
8771 || (temp[6] == 0 && temp[7] == 0)))
8772 {
8773 /* The value is simple enough to load with a couple of
8774 instructions. If using 32-bit registers, set
8775 imm_expr to the high order 32 bits and offset_expr to
8776 the low order 32 bits. Otherwise, set imm_expr to
8777 the entire 64 bit constant. */
8778 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8779 {
8780 imm_expr.X_op = O_constant;
8781 offset_expr.X_op = O_constant;
8782 if (! target_big_endian)
8783 {
8784 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8785 offset_expr.X_add_number = bfd_getl32 (temp);
8786 }
8787 else
8788 {
8789 imm_expr.X_add_number = bfd_getb32 (temp);
8790 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8791 }
8792 if (offset_expr.X_add_number == 0)
8793 offset_expr.X_op = O_absent;
8794 }
8795 else if (sizeof (imm_expr.X_add_number) > 4)
8796 {
8797 imm_expr.X_op = O_constant;
8798 if (! target_big_endian)
8799 imm_expr.X_add_number = bfd_getl64 (temp);
8800 else
8801 imm_expr.X_add_number = bfd_getb64 (temp);
8802 }
8803 else
8804 {
8805 imm_expr.X_op = O_big;
8806 imm_expr.X_add_number = 4;
8807 if (! target_big_endian)
8808 {
8809 generic_bignum[0] = bfd_getl16 (temp);
8810 generic_bignum[1] = bfd_getl16 (temp + 2);
8811 generic_bignum[2] = bfd_getl16 (temp + 4);
8812 generic_bignum[3] = bfd_getl16 (temp + 6);
8813 }
8814 else
8815 {
8816 generic_bignum[0] = bfd_getb16 (temp + 6);
8817 generic_bignum[1] = bfd_getb16 (temp + 4);
8818 generic_bignum[2] = bfd_getb16 (temp + 2);
8819 generic_bignum[3] = bfd_getb16 (temp);
8820 }
8821 }
8822 }
8823 else
8824 {
8825 const char *newname;
8826 segT new_seg;
8827
8828 /* Switch to the right section. */
8829 seg = now_seg;
8830 subseg = now_subseg;
8831 switch (*args)
8832 {
8833 default: /* unused default case avoids warnings. */
8834 case 'L':
8835 newname = RDATA_SECTION_NAME;
8836 if (g_switch_value >= 8)
8837 newname = ".lit8";
8838 break;
8839 case 'F':
8840 newname = RDATA_SECTION_NAME;
8841 break;
8842 case 'l':
8843 assert (g_switch_value >= 4);
8844 newname = ".lit4";
8845 break;
8846 }
8847 new_seg = subseg_new (newname, (subsegT) 0);
8848 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8849 bfd_set_section_flags (stdoutput, new_seg,
8850 (SEC_ALLOC
8851 | SEC_LOAD
8852 | SEC_READONLY
8853 | SEC_DATA));
8854 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8855 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8856 && strcmp (TARGET_OS, "elf") != 0)
8857 record_alignment (new_seg, 4);
8858 else
8859 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8860 if (seg == now_seg)
8861 as_bad (_("Can't use floating point insn in this section"));
8862
8863 /* Set the argument to the current address in the
8864 section. */
8865 offset_expr.X_op = O_symbol;
8866 offset_expr.X_add_symbol =
8867 symbol_new ("L0\001", now_seg,
8868 (valueT) frag_now_fix (), frag_now);
8869 offset_expr.X_add_number = 0;
8870
8871 /* Put the floating point number into the section. */
8872 p = frag_more ((int) length);
8873 memcpy (p, temp, length);
8874
8875 /* Switch back to the original section. */
8876 subseg_set (seg, subseg);
8877 }
8878 }
8879 continue;
8880
8881 case 'i': /* 16 bit unsigned immediate */
8882 case 'j': /* 16 bit signed immediate */
8883 *imm_reloc = BFD_RELOC_LO16;
8884 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8885 {
8886 int more;
8887 offsetT minval, maxval;
8888
8889 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8890 && strcmp (insn->name, insn[1].name) == 0);
8891
8892 /* If the expression was written as an unsigned number,
8893 only treat it as signed if there are no more
8894 alternatives. */
8895 if (more
8896 && *args == 'j'
8897 && sizeof (imm_expr.X_add_number) <= 4
8898 && imm_expr.X_op == O_constant
8899 && imm_expr.X_add_number < 0
8900 && imm_expr.X_unsigned
8901 && HAVE_64BIT_GPRS)
8902 break;
8903
8904 /* For compatibility with older assemblers, we accept
8905 0x8000-0xffff as signed 16-bit numbers when only
8906 signed numbers are allowed. */
8907 if (*args == 'i')
8908 minval = 0, maxval = 0xffff;
8909 else if (more)
8910 minval = -0x8000, maxval = 0x7fff;
8911 else
8912 minval = -0x8000, maxval = 0xffff;
8913
8914 if (imm_expr.X_op != O_constant
8915 || imm_expr.X_add_number < minval
8916 || imm_expr.X_add_number > maxval)
8917 {
8918 if (more)
8919 break;
8920 if (imm_expr.X_op == O_constant
8921 || imm_expr.X_op == O_big)
8922 as_bad (_("expression out of range"));
8923 }
8924 }
8925 s = expr_end;
8926 continue;
8927
8928 case 'o': /* 16 bit offset */
8929 /* Check whether there is only a single bracketed expression
8930 left. If so, it must be the base register and the
8931 constant must be zero. */
8932 if (*s == '(' && strchr (s + 1, '(') == 0)
8933 {
8934 offset_expr.X_op = O_constant;
8935 offset_expr.X_add_number = 0;
8936 continue;
8937 }
8938
8939 /* If this value won't fit into a 16 bit offset, then go
8940 find a macro that will generate the 32 bit offset
8941 code pattern. */
8942 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8943 && (offset_expr.X_op != O_constant
8944 || offset_expr.X_add_number >= 0x8000
8945 || offset_expr.X_add_number < -0x8000))
8946 break;
8947
8948 s = expr_end;
8949 continue;
8950
8951 case 'p': /* pc relative offset */
8952 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8953 my_getExpression (&offset_expr, s);
8954 s = expr_end;
8955 continue;
8956
8957 case 'u': /* upper 16 bits */
8958 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8959 && imm_expr.X_op == O_constant
8960 && (imm_expr.X_add_number < 0
8961 || imm_expr.X_add_number >= 0x10000))
8962 as_bad (_("lui expression not in range 0..65535"));
8963 s = expr_end;
8964 continue;
8965
8966 case 'a': /* 26 bit address */
8967 my_getExpression (&offset_expr, s);
8968 s = expr_end;
8969 *offset_reloc = BFD_RELOC_MIPS_JMP;
8970 continue;
8971
8972 case 'N': /* 3 bit branch condition code */
8973 case 'M': /* 3 bit compare condition code */
8974 if (strncmp (s, "$fcc", 4) != 0)
8975 break;
8976 s += 4;
8977 regno = 0;
8978 do
8979 {
8980 regno *= 10;
8981 regno += *s - '0';
8982 ++s;
8983 }
8984 while (ISDIGIT (*s));
8985 if (regno > 7)
8986 as_bad (_("Invalid condition code register $fcc%d"), regno);
8987 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8988 || strcmp(str + strlen(str) - 5, "any2f") == 0
8989 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8990 && (regno & 1) != 0)
8991 as_warn(_("Condition code register should be even for %s, was %d"),
8992 str, regno);
8993 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8994 || strcmp(str + strlen(str) - 5, "any4t") == 0)
8995 && (regno & 3) != 0)
8996 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
8997 str, regno);
8998 if (*args == 'N')
8999 ip->insn_opcode |= regno << OP_SH_BCC;
9000 else
9001 ip->insn_opcode |= regno << OP_SH_CCC;
9002 continue;
9003
9004 case 'H':
9005 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9006 s += 2;
9007 if (ISDIGIT (*s))
9008 {
9009 c = 0;
9010 do
9011 {
9012 c *= 10;
9013 c += *s - '0';
9014 ++s;
9015 }
9016 while (ISDIGIT (*s));
9017 }
9018 else
9019 c = 8; /* Invalid sel value. */
9020
9021 if (c > 7)
9022 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9023 ip->insn_opcode |= c;
9024 continue;
9025
9026 case 'e':
9027 /* Must be at least one digit. */
9028 my_getExpression (&imm_expr, s);
9029 check_absolute_expr (ip, &imm_expr);
9030
9031 if ((unsigned long) imm_expr.X_add_number
9032 > (unsigned long) OP_MASK_VECBYTE)
9033 {
9034 as_bad (_("bad byte vector index (%ld)"),
9035 (long) imm_expr.X_add_number);
9036 imm_expr.X_add_number = 0;
9037 }
9038
9039 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9040 imm_expr.X_op = O_absent;
9041 s = expr_end;
9042 continue;
9043
9044 case '%':
9045 my_getExpression (&imm_expr, s);
9046 check_absolute_expr (ip, &imm_expr);
9047
9048 if ((unsigned long) imm_expr.X_add_number
9049 > (unsigned long) OP_MASK_VECALIGN)
9050 {
9051 as_bad (_("bad byte vector index (%ld)"),
9052 (long) imm_expr.X_add_number);
9053 imm_expr.X_add_number = 0;
9054 }
9055
9056 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9057 imm_expr.X_op = O_absent;
9058 s = expr_end;
9059 continue;
9060
9061 default:
9062 as_bad (_("bad char = '%c'\n"), *args);
9063 internalError ();
9064 }
9065 break;
9066 }
9067 /* Args don't match. */
9068 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9069 !strcmp (insn->name, insn[1].name))
9070 {
9071 ++insn;
9072 s = argsStart;
9073 insn_error = _("illegal operands");
9074 continue;
9075 }
9076 if (save_c)
9077 *(--s) = save_c;
9078 insn_error = _("illegal operands");
9079 return;
9080 }
9081 }
9082
9083 /* This routine assembles an instruction into its binary format when
9084 assembling for the mips16. As a side effect, it sets one of the
9085 global variables imm_reloc or offset_reloc to the type of
9086 relocation to do if one of the operands is an address expression.
9087 It also sets mips16_small and mips16_ext if the user explicitly
9088 requested a small or extended instruction. */
9089
9090 static void
9091 mips16_ip (char *str, struct mips_cl_insn *ip)
9092 {
9093 char *s;
9094 const char *args;
9095 struct mips_opcode *insn;
9096 char *argsstart;
9097 unsigned int regno;
9098 unsigned int lastregno = 0;
9099 char *s_reset;
9100
9101 insn_error = NULL;
9102
9103 mips16_small = FALSE;
9104 mips16_ext = FALSE;
9105
9106 for (s = str; ISLOWER (*s); ++s)
9107 ;
9108 switch (*s)
9109 {
9110 case '\0':
9111 break;
9112
9113 case ' ':
9114 *s++ = '\0';
9115 break;
9116
9117 case '.':
9118 if (s[1] == 't' && s[2] == ' ')
9119 {
9120 *s = '\0';
9121 mips16_small = TRUE;
9122 s += 3;
9123 break;
9124 }
9125 else if (s[1] == 'e' && s[2] == ' ')
9126 {
9127 *s = '\0';
9128 mips16_ext = TRUE;
9129 s += 3;
9130 break;
9131 }
9132 /* Fall through. */
9133 default:
9134 insn_error = _("unknown opcode");
9135 return;
9136 }
9137
9138 if (mips_opts.noautoextend && ! mips16_ext)
9139 mips16_small = TRUE;
9140
9141 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9142 {
9143 insn_error = _("unrecognized opcode");
9144 return;
9145 }
9146
9147 argsstart = s;
9148 for (;;)
9149 {
9150 assert (strcmp (insn->name, str) == 0);
9151
9152 ip->insn_mo = insn;
9153 ip->insn_opcode = insn->match;
9154 ip->use_extend = FALSE;
9155 imm_expr.X_op = O_absent;
9156 imm_reloc[0] = BFD_RELOC_UNUSED;
9157 imm_reloc[1] = BFD_RELOC_UNUSED;
9158 imm_reloc[2] = BFD_RELOC_UNUSED;
9159 imm2_expr.X_op = O_absent;
9160 offset_expr.X_op = O_absent;
9161 offset_reloc[0] = BFD_RELOC_UNUSED;
9162 offset_reloc[1] = BFD_RELOC_UNUSED;
9163 offset_reloc[2] = BFD_RELOC_UNUSED;
9164 for (args = insn->args; 1; ++args)
9165 {
9166 int c;
9167
9168 if (*s == ' ')
9169 ++s;
9170
9171 /* In this switch statement we call break if we did not find
9172 a match, continue if we did find a match, or return if we
9173 are done. */
9174
9175 c = *args;
9176 switch (c)
9177 {
9178 case '\0':
9179 if (*s == '\0')
9180 {
9181 /* Stuff the immediate value in now, if we can. */
9182 if (imm_expr.X_op == O_constant
9183 && *imm_reloc > BFD_RELOC_UNUSED
9184 && insn->pinfo != INSN_MACRO)
9185 {
9186 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9187 imm_expr.X_add_number, TRUE, mips16_small,
9188 mips16_ext, &ip->insn_opcode,
9189 &ip->use_extend, &ip->extend);
9190 imm_expr.X_op = O_absent;
9191 *imm_reloc = BFD_RELOC_UNUSED;
9192 }
9193
9194 return;
9195 }
9196 break;
9197
9198 case ',':
9199 if (*s++ == c)
9200 continue;
9201 s--;
9202 switch (*++args)
9203 {
9204 case 'v':
9205 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9206 continue;
9207 case 'w':
9208 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9209 continue;
9210 }
9211 break;
9212
9213 case '(':
9214 case ')':
9215 if (*s++ == c)
9216 continue;
9217 break;
9218
9219 case 'v':
9220 case 'w':
9221 if (s[0] != '$')
9222 {
9223 if (c == 'v')
9224 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9225 else
9226 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9227 ++args;
9228 continue;
9229 }
9230 /* Fall through. */
9231 case 'x':
9232 case 'y':
9233 case 'z':
9234 case 'Z':
9235 case '0':
9236 case 'S':
9237 case 'R':
9238 case 'X':
9239 case 'Y':
9240 if (s[0] != '$')
9241 break;
9242 s_reset = s;
9243 if (ISDIGIT (s[1]))
9244 {
9245 ++s;
9246 regno = 0;
9247 do
9248 {
9249 regno *= 10;
9250 regno += *s - '0';
9251 ++s;
9252 }
9253 while (ISDIGIT (*s));
9254 if (regno > 31)
9255 {
9256 as_bad (_("invalid register number (%d)"), regno);
9257 regno = 2;
9258 }
9259 }
9260 else
9261 {
9262 if (s[1] == 'r' && s[2] == 'a')
9263 {
9264 s += 3;
9265 regno = RA;
9266 }
9267 else if (s[1] == 'f' && s[2] == 'p')
9268 {
9269 s += 3;
9270 regno = FP;
9271 }
9272 else if (s[1] == 's' && s[2] == 'p')
9273 {
9274 s += 3;
9275 regno = SP;
9276 }
9277 else if (s[1] == 'g' && s[2] == 'p')
9278 {
9279 s += 3;
9280 regno = GP;
9281 }
9282 else if (s[1] == 'a' && s[2] == 't')
9283 {
9284 s += 3;
9285 regno = AT;
9286 }
9287 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9288 {
9289 s += 4;
9290 regno = KT0;
9291 }
9292 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9293 {
9294 s += 4;
9295 regno = KT1;
9296 }
9297 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9298 {
9299 s += 5;
9300 regno = ZERO;
9301 }
9302 else
9303 break;
9304 }
9305
9306 if (*s == ' ')
9307 ++s;
9308 if (args[1] != *s)
9309 {
9310 if (c == 'v' || c == 'w')
9311 {
9312 regno = mips16_to_32_reg_map[lastregno];
9313 s = s_reset;
9314 ++args;
9315 }
9316 }
9317
9318 switch (c)
9319 {
9320 case 'x':
9321 case 'y':
9322 case 'z':
9323 case 'v':
9324 case 'w':
9325 case 'Z':
9326 regno = mips32_to_16_reg_map[regno];
9327 break;
9328
9329 case '0':
9330 if (regno != 0)
9331 regno = ILLEGAL_REG;
9332 break;
9333
9334 case 'S':
9335 if (regno != SP)
9336 regno = ILLEGAL_REG;
9337 break;
9338
9339 case 'R':
9340 if (regno != RA)
9341 regno = ILLEGAL_REG;
9342 break;
9343
9344 case 'X':
9345 case 'Y':
9346 if (regno == AT && ! mips_opts.noat)
9347 as_warn (_("used $at without \".set noat\""));
9348 break;
9349
9350 default:
9351 internalError ();
9352 }
9353
9354 if (regno == ILLEGAL_REG)
9355 break;
9356
9357 switch (c)
9358 {
9359 case 'x':
9360 case 'v':
9361 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9362 break;
9363 case 'y':
9364 case 'w':
9365 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9366 break;
9367 case 'z':
9368 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9369 break;
9370 case 'Z':
9371 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9372 case '0':
9373 case 'S':
9374 case 'R':
9375 break;
9376 case 'X':
9377 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9378 break;
9379 case 'Y':
9380 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9381 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9382 break;
9383 default:
9384 internalError ();
9385 }
9386
9387 lastregno = regno;
9388 continue;
9389
9390 case 'P':
9391 if (strncmp (s, "$pc", 3) == 0)
9392 {
9393 s += 3;
9394 continue;
9395 }
9396 break;
9397
9398 case '<':
9399 case '>':
9400 case '[':
9401 case ']':
9402 case '4':
9403 case '5':
9404 case 'H':
9405 case 'W':
9406 case 'D':
9407 case 'j':
9408 case '8':
9409 case 'V':
9410 case 'C':
9411 case 'U':
9412 case 'k':
9413 case 'K':
9414 if (s[0] == '%'
9415 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9416 {
9417 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9418 and generate the appropriate reloc. If the text
9419 inside %gprel is not a symbol name with an
9420 optional offset, then we generate a normal reloc
9421 and will probably fail later. */
9422 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9423 if (imm_expr.X_op == O_symbol)
9424 {
9425 mips16_ext = TRUE;
9426 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9427 s = expr_end;
9428 ip->use_extend = TRUE;
9429 ip->extend = 0;
9430 continue;
9431 }
9432 }
9433 else
9434 {
9435 /* Just pick up a normal expression. */
9436 my_getExpression (&imm_expr, s);
9437 }
9438
9439 if (imm_expr.X_op == O_register)
9440 {
9441 /* What we thought was an expression turned out to
9442 be a register. */
9443
9444 if (s[0] == '(' && args[1] == '(')
9445 {
9446 /* It looks like the expression was omitted
9447 before a register indirection, which means
9448 that the expression is implicitly zero. We
9449 still set up imm_expr, so that we handle
9450 explicit extensions correctly. */
9451 imm_expr.X_op = O_constant;
9452 imm_expr.X_add_number = 0;
9453 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9454 continue;
9455 }
9456
9457 break;
9458 }
9459
9460 /* We need to relax this instruction. */
9461 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9462 s = expr_end;
9463 continue;
9464
9465 case 'p':
9466 case 'q':
9467 case 'A':
9468 case 'B':
9469 case 'E':
9470 /* We use offset_reloc rather than imm_reloc for the PC
9471 relative operands. This lets macros with both
9472 immediate and address operands work correctly. */
9473 my_getExpression (&offset_expr, s);
9474
9475 if (offset_expr.X_op == O_register)
9476 break;
9477
9478 /* We need to relax this instruction. */
9479 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9480 s = expr_end;
9481 continue;
9482
9483 case '6': /* break code */
9484 my_getExpression (&imm_expr, s);
9485 check_absolute_expr (ip, &imm_expr);
9486 if ((unsigned long) imm_expr.X_add_number > 63)
9487 {
9488 as_warn (_("Invalid value for `%s' (%lu)"),
9489 ip->insn_mo->name,
9490 (unsigned long) imm_expr.X_add_number);
9491 imm_expr.X_add_number &= 0x3f;
9492 }
9493 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9494 imm_expr.X_op = O_absent;
9495 s = expr_end;
9496 continue;
9497
9498 case 'a': /* 26 bit address */
9499 my_getExpression (&offset_expr, s);
9500 s = expr_end;
9501 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9502 ip->insn_opcode <<= 16;
9503 continue;
9504
9505 case 'l': /* register list for entry macro */
9506 case 'L': /* register list for exit macro */
9507 {
9508 int mask;
9509
9510 if (c == 'l')
9511 mask = 0;
9512 else
9513 mask = 7 << 3;
9514 while (*s != '\0')
9515 {
9516 int freg, reg1, reg2;
9517
9518 while (*s == ' ' || *s == ',')
9519 ++s;
9520 if (*s != '$')
9521 {
9522 as_bad (_("can't parse register list"));
9523 break;
9524 }
9525 ++s;
9526 if (*s != 'f')
9527 freg = 0;
9528 else
9529 {
9530 freg = 1;
9531 ++s;
9532 }
9533 reg1 = 0;
9534 while (ISDIGIT (*s))
9535 {
9536 reg1 *= 10;
9537 reg1 += *s - '0';
9538 ++s;
9539 }
9540 if (*s == ' ')
9541 ++s;
9542 if (*s != '-')
9543 reg2 = reg1;
9544 else
9545 {
9546 ++s;
9547 if (*s != '$')
9548 break;
9549 ++s;
9550 if (freg)
9551 {
9552 if (*s == 'f')
9553 ++s;
9554 else
9555 {
9556 as_bad (_("invalid register list"));
9557 break;
9558 }
9559 }
9560 reg2 = 0;
9561 while (ISDIGIT (*s))
9562 {
9563 reg2 *= 10;
9564 reg2 += *s - '0';
9565 ++s;
9566 }
9567 }
9568 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9569 {
9570 mask &= ~ (7 << 3);
9571 mask |= 5 << 3;
9572 }
9573 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9574 {
9575 mask &= ~ (7 << 3);
9576 mask |= 6 << 3;
9577 }
9578 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9579 mask |= (reg2 - 3) << 3;
9580 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9581 mask |= (reg2 - 15) << 1;
9582 else if (reg1 == RA && reg2 == RA)
9583 mask |= 1;
9584 else
9585 {
9586 as_bad (_("invalid register list"));
9587 break;
9588 }
9589 }
9590 /* The mask is filled in in the opcode table for the
9591 benefit of the disassembler. We remove it before
9592 applying the actual mask. */
9593 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9594 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9595 }
9596 continue;
9597
9598 case 'e': /* extend code */
9599 my_getExpression (&imm_expr, s);
9600 check_absolute_expr (ip, &imm_expr);
9601 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9602 {
9603 as_warn (_("Invalid value for `%s' (%lu)"),
9604 ip->insn_mo->name,
9605 (unsigned long) imm_expr.X_add_number);
9606 imm_expr.X_add_number &= 0x7ff;
9607 }
9608 ip->insn_opcode |= imm_expr.X_add_number;
9609 imm_expr.X_op = O_absent;
9610 s = expr_end;
9611 continue;
9612
9613 default:
9614 internalError ();
9615 }
9616 break;
9617 }
9618
9619 /* Args don't match. */
9620 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9621 strcmp (insn->name, insn[1].name) == 0)
9622 {
9623 ++insn;
9624 s = argsstart;
9625 continue;
9626 }
9627
9628 insn_error = _("illegal operands");
9629
9630 return;
9631 }
9632 }
9633
9634 /* This structure holds information we know about a mips16 immediate
9635 argument type. */
9636
9637 struct mips16_immed_operand
9638 {
9639 /* The type code used in the argument string in the opcode table. */
9640 int type;
9641 /* The number of bits in the short form of the opcode. */
9642 int nbits;
9643 /* The number of bits in the extended form of the opcode. */
9644 int extbits;
9645 /* The amount by which the short form is shifted when it is used;
9646 for example, the sw instruction has a shift count of 2. */
9647 int shift;
9648 /* The amount by which the short form is shifted when it is stored
9649 into the instruction code. */
9650 int op_shift;
9651 /* Non-zero if the short form is unsigned. */
9652 int unsp;
9653 /* Non-zero if the extended form is unsigned. */
9654 int extu;
9655 /* Non-zero if the value is PC relative. */
9656 int pcrel;
9657 };
9658
9659 /* The mips16 immediate operand types. */
9660
9661 static const struct mips16_immed_operand mips16_immed_operands[] =
9662 {
9663 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9664 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9665 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9666 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9667 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9668 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9669 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9670 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9671 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9672 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9673 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9674 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9675 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9676 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9677 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9678 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9679 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9680 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9681 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9682 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9683 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9684 };
9685
9686 #define MIPS16_NUM_IMMED \
9687 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9688
9689 /* Handle a mips16 instruction with an immediate value. This or's the
9690 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9691 whether an extended value is needed; if one is needed, it sets
9692 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9693 If SMALL is true, an unextended opcode was explicitly requested.
9694 If EXT is true, an extended opcode was explicitly requested. If
9695 WARN is true, warn if EXT does not match reality. */
9696
9697 static void
9698 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9699 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9700 unsigned long *insn, bfd_boolean *use_extend,
9701 unsigned short *extend)
9702 {
9703 register const struct mips16_immed_operand *op;
9704 int mintiny, maxtiny;
9705 bfd_boolean needext;
9706
9707 op = mips16_immed_operands;
9708 while (op->type != type)
9709 {
9710 ++op;
9711 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9712 }
9713
9714 if (op->unsp)
9715 {
9716 if (type == '<' || type == '>' || type == '[' || type == ']')
9717 {
9718 mintiny = 1;
9719 maxtiny = 1 << op->nbits;
9720 }
9721 else
9722 {
9723 mintiny = 0;
9724 maxtiny = (1 << op->nbits) - 1;
9725 }
9726 }
9727 else
9728 {
9729 mintiny = - (1 << (op->nbits - 1));
9730 maxtiny = (1 << (op->nbits - 1)) - 1;
9731 }
9732
9733 /* Branch offsets have an implicit 0 in the lowest bit. */
9734 if (type == 'p' || type == 'q')
9735 val /= 2;
9736
9737 if ((val & ((1 << op->shift) - 1)) != 0
9738 || val < (mintiny << op->shift)
9739 || val > (maxtiny << op->shift))
9740 needext = TRUE;
9741 else
9742 needext = FALSE;
9743
9744 if (warn && ext && ! needext)
9745 as_warn_where (file, line,
9746 _("extended operand requested but not required"));
9747 if (small && needext)
9748 as_bad_where (file, line, _("invalid unextended operand value"));
9749
9750 if (small || (! ext && ! needext))
9751 {
9752 int insnval;
9753
9754 *use_extend = FALSE;
9755 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9756 insnval <<= op->op_shift;
9757 *insn |= insnval;
9758 }
9759 else
9760 {
9761 long minext, maxext;
9762 int extval;
9763
9764 if (op->extu)
9765 {
9766 minext = 0;
9767 maxext = (1 << op->extbits) - 1;
9768 }
9769 else
9770 {
9771 minext = - (1 << (op->extbits - 1));
9772 maxext = (1 << (op->extbits - 1)) - 1;
9773 }
9774 if (val < minext || val > maxext)
9775 as_bad_where (file, line,
9776 _("operand value out of range for instruction"));
9777
9778 *use_extend = TRUE;
9779 if (op->extbits == 16)
9780 {
9781 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9782 val &= 0x1f;
9783 }
9784 else if (op->extbits == 15)
9785 {
9786 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9787 val &= 0xf;
9788 }
9789 else
9790 {
9791 extval = ((val & 0x1f) << 6) | (val & 0x20);
9792 val = 0;
9793 }
9794
9795 *extend = (unsigned short) extval;
9796 *insn |= val;
9797 }
9798 }
9799 \f
9800 static const struct percent_op_match
9801 {
9802 const char *str;
9803 bfd_reloc_code_real_type reloc;
9804 } percent_op[] =
9805 {
9806 {"%lo", BFD_RELOC_LO16},
9807 #ifdef OBJ_ELF
9808 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9809 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9810 {"%call16", BFD_RELOC_MIPS_CALL16},
9811 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9812 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9813 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9814 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9815 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9816 {"%got", BFD_RELOC_MIPS_GOT16},
9817 {"%gp_rel", BFD_RELOC_GPREL16},
9818 {"%half", BFD_RELOC_16},
9819 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9820 {"%higher", BFD_RELOC_MIPS_HIGHER},
9821 {"%neg", BFD_RELOC_MIPS_SUB},
9822 #endif
9823 {"%hi", BFD_RELOC_HI16_S}
9824 };
9825
9826
9827 /* Return true if *STR points to a relocation operator. When returning true,
9828 move *STR over the operator and store its relocation code in *RELOC.
9829 Leave both *STR and *RELOC alone when returning false. */
9830
9831 static bfd_boolean
9832 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9833 {
9834 size_t i;
9835
9836 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9837 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9838 {
9839 *str += strlen (percent_op[i].str);
9840 *reloc = percent_op[i].reloc;
9841
9842 /* Check whether the output BFD supports this relocation.
9843 If not, issue an error and fall back on something safe. */
9844 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9845 {
9846 as_bad ("relocation %s isn't supported by the current ABI",
9847 percent_op[i].str);
9848 *reloc = BFD_RELOC_UNUSED;
9849 }
9850 return TRUE;
9851 }
9852 return FALSE;
9853 }
9854
9855
9856 /* Parse string STR as a 16-bit relocatable operand. Store the
9857 expression in *EP and the relocations in the array starting
9858 at RELOC. Return the number of relocation operators used.
9859
9860 On exit, EXPR_END points to the first character after the expression. */
9861
9862 static size_t
9863 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9864 char *str)
9865 {
9866 bfd_reloc_code_real_type reversed_reloc[3];
9867 size_t reloc_index, i;
9868 int crux_depth, str_depth;
9869 char *crux;
9870
9871 /* Search for the start of the main expression, recoding relocations
9872 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9873 of the main expression and with CRUX_DEPTH containing the number
9874 of open brackets at that point. */
9875 reloc_index = -1;
9876 str_depth = 0;
9877 do
9878 {
9879 reloc_index++;
9880 crux = str;
9881 crux_depth = str_depth;
9882
9883 /* Skip over whitespace and brackets, keeping count of the number
9884 of brackets. */
9885 while (*str == ' ' || *str == '\t' || *str == '(')
9886 if (*str++ == '(')
9887 str_depth++;
9888 }
9889 while (*str == '%'
9890 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9891 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9892
9893 my_getExpression (ep, crux);
9894 str = expr_end;
9895
9896 /* Match every open bracket. */
9897 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9898 if (*str++ == ')')
9899 crux_depth--;
9900
9901 if (crux_depth > 0)
9902 as_bad ("unclosed '('");
9903
9904 expr_end = str;
9905
9906 if (reloc_index != 0)
9907 {
9908 prev_reloc_op_frag = frag_now;
9909 for (i = 0; i < reloc_index; i++)
9910 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9911 }
9912
9913 return reloc_index;
9914 }
9915
9916 static void
9917 my_getExpression (expressionS *ep, char *str)
9918 {
9919 char *save_in;
9920 valueT val;
9921
9922 save_in = input_line_pointer;
9923 input_line_pointer = str;
9924 expression (ep);
9925 expr_end = input_line_pointer;
9926 input_line_pointer = save_in;
9927
9928 /* If we are in mips16 mode, and this is an expression based on `.',
9929 then we bump the value of the symbol by 1 since that is how other
9930 text symbols are handled. We don't bother to handle complex
9931 expressions, just `.' plus or minus a constant. */
9932 if (mips_opts.mips16
9933 && ep->X_op == O_symbol
9934 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9935 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9936 && symbol_get_frag (ep->X_add_symbol) == frag_now
9937 && symbol_constant_p (ep->X_add_symbol)
9938 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9939 S_SET_VALUE (ep->X_add_symbol, val + 1);
9940 }
9941
9942 /* Turn a string in input_line_pointer into a floating point constant
9943 of type TYPE, and store the appropriate bytes in *LITP. The number
9944 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9945 returned, or NULL on OK. */
9946
9947 char *
9948 md_atof (int type, char *litP, int *sizeP)
9949 {
9950 int prec;
9951 LITTLENUM_TYPE words[4];
9952 char *t;
9953 int i;
9954
9955 switch (type)
9956 {
9957 case 'f':
9958 prec = 2;
9959 break;
9960
9961 case 'd':
9962 prec = 4;
9963 break;
9964
9965 default:
9966 *sizeP = 0;
9967 return _("bad call to md_atof");
9968 }
9969
9970 t = atof_ieee (input_line_pointer, type, words);
9971 if (t)
9972 input_line_pointer = t;
9973
9974 *sizeP = prec * 2;
9975
9976 if (! target_big_endian)
9977 {
9978 for (i = prec - 1; i >= 0; i--)
9979 {
9980 md_number_to_chars (litP, words[i], 2);
9981 litP += 2;
9982 }
9983 }
9984 else
9985 {
9986 for (i = 0; i < prec; i++)
9987 {
9988 md_number_to_chars (litP, words[i], 2);
9989 litP += 2;
9990 }
9991 }
9992
9993 return NULL;
9994 }
9995
9996 void
9997 md_number_to_chars (char *buf, valueT val, int n)
9998 {
9999 if (target_big_endian)
10000 number_to_chars_bigendian (buf, val, n);
10001 else
10002 number_to_chars_littleendian (buf, val, n);
10003 }
10004 \f
10005 #ifdef OBJ_ELF
10006 static int support_64bit_objects(void)
10007 {
10008 const char **list, **l;
10009 int yes;
10010
10011 list = bfd_target_list ();
10012 for (l = list; *l != NULL; l++)
10013 #ifdef TE_TMIPS
10014 /* This is traditional mips */
10015 if (strcmp (*l, "elf64-tradbigmips") == 0
10016 || strcmp (*l, "elf64-tradlittlemips") == 0)
10017 #else
10018 if (strcmp (*l, "elf64-bigmips") == 0
10019 || strcmp (*l, "elf64-littlemips") == 0)
10020 #endif
10021 break;
10022 yes = (*l != NULL);
10023 free (list);
10024 return yes;
10025 }
10026 #endif /* OBJ_ELF */
10027
10028 const char *md_shortopts = "O::g::G:";
10029
10030 struct option md_longopts[] =
10031 {
10032 /* Options which specify architecture. */
10033 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10034 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10035 {"march", required_argument, NULL, OPTION_MARCH},
10036 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10037 {"mtune", required_argument, NULL, OPTION_MTUNE},
10038 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10039 {"mips0", no_argument, NULL, OPTION_MIPS1},
10040 {"mips1", no_argument, NULL, OPTION_MIPS1},
10041 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10042 {"mips2", no_argument, NULL, OPTION_MIPS2},
10043 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10044 {"mips3", no_argument, NULL, OPTION_MIPS3},
10045 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10046 {"mips4", no_argument, NULL, OPTION_MIPS4},
10047 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10048 {"mips5", no_argument, NULL, OPTION_MIPS5},
10049 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10050 {"mips32", no_argument, NULL, OPTION_MIPS32},
10051 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10052 {"mips64", no_argument, NULL, OPTION_MIPS64},
10053 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10054 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10055 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10056 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10057
10058 /* Options which specify Application Specific Extensions (ASEs). */
10059 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10060 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10061 {"mips16", no_argument, NULL, OPTION_MIPS16},
10062 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10063 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10064 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10065 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10066 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10067 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10068 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10069 {"mdmx", no_argument, NULL, OPTION_MDMX},
10070 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10071 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10072
10073 /* Old-style architecture options. Don't add more of these. */
10074 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10075 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10076 {"m4650", no_argument, NULL, OPTION_M4650},
10077 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10078 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10079 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10080 {"m4010", no_argument, NULL, OPTION_M4010},
10081 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10082 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10083 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10084 {"m4100", no_argument, NULL, OPTION_M4100},
10085 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10086 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10087 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10088 {"m3900", no_argument, NULL, OPTION_M3900},
10089 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10090 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10091
10092 /* Options which enable bug fixes. */
10093 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10094 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10095 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10096 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10097 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10098 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10099 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10100 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10101 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10102 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10103
10104 /* Miscellaneous options. */
10105 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10106 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10107 {"trap", no_argument, NULL, OPTION_TRAP},
10108 {"no-break", no_argument, NULL, OPTION_TRAP},
10109 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10110 {"break", no_argument, NULL, OPTION_BREAK},
10111 {"no-trap", no_argument, NULL, OPTION_BREAK},
10112 #define OPTION_EB (OPTION_MISC_BASE + 2)
10113 {"EB", no_argument, NULL, OPTION_EB},
10114 #define OPTION_EL (OPTION_MISC_BASE + 3)
10115 {"EL", no_argument, NULL, OPTION_EL},
10116 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10117 {"mfp32", no_argument, NULL, OPTION_FP32},
10118 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10119 {"mgp32", no_argument, NULL, OPTION_GP32},
10120 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10121 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10122 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10123 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10124 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10125 {"mfp64", no_argument, NULL, OPTION_FP64},
10126 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10127 {"mgp64", no_argument, NULL, OPTION_GP64},
10128 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10129 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10130 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10131 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10132 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10133 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10134 {"mshared", no_argument, NULL, OPTION_MSHARED},
10135 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10136
10137 /* ELF-specific options. */
10138 #ifdef OBJ_ELF
10139 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 14)
10140 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10141 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10142 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10143 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10144 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10145 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10146 {"xgot", no_argument, NULL, OPTION_XGOT},
10147 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10148 {"mabi", required_argument, NULL, OPTION_MABI},
10149 #define OPTION_32 (OPTION_ELF_BASE + 4)
10150 {"32", no_argument, NULL, OPTION_32},
10151 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10152 {"n32", no_argument, NULL, OPTION_N32},
10153 #define OPTION_64 (OPTION_ELF_BASE + 6)
10154 {"64", no_argument, NULL, OPTION_64},
10155 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10156 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10157 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10158 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10159 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10160 {"mpdr", no_argument, NULL, OPTION_PDR},
10161 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10162 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10163 #endif /* OBJ_ELF */
10164
10165 {NULL, no_argument, NULL, 0}
10166 };
10167 size_t md_longopts_size = sizeof (md_longopts);
10168
10169 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10170 NEW_VALUE. Warn if another value was already specified. Note:
10171 we have to defer parsing the -march and -mtune arguments in order
10172 to handle 'from-abi' correctly, since the ABI might be specified
10173 in a later argument. */
10174
10175 static void
10176 mips_set_option_string (const char **string_ptr, const char *new_value)
10177 {
10178 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10179 as_warn (_("A different %s was already specified, is now %s"),
10180 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10181 new_value);
10182
10183 *string_ptr = new_value;
10184 }
10185
10186 int
10187 md_parse_option (int c, char *arg)
10188 {
10189 switch (c)
10190 {
10191 case OPTION_CONSTRUCT_FLOATS:
10192 mips_disable_float_construction = 0;
10193 break;
10194
10195 case OPTION_NO_CONSTRUCT_FLOATS:
10196 mips_disable_float_construction = 1;
10197 break;
10198
10199 case OPTION_TRAP:
10200 mips_trap = 1;
10201 break;
10202
10203 case OPTION_BREAK:
10204 mips_trap = 0;
10205 break;
10206
10207 case OPTION_EB:
10208 target_big_endian = 1;
10209 break;
10210
10211 case OPTION_EL:
10212 target_big_endian = 0;
10213 break;
10214
10215 case 'O':
10216 if (arg && arg[1] == '0')
10217 mips_optimize = 1;
10218 else
10219 mips_optimize = 2;
10220 break;
10221
10222 case 'g':
10223 if (arg == NULL)
10224 mips_debug = 2;
10225 else
10226 mips_debug = atoi (arg);
10227 /* When the MIPS assembler sees -g or -g2, it does not do
10228 optimizations which limit full symbolic debugging. We take
10229 that to be equivalent to -O0. */
10230 if (mips_debug == 2)
10231 mips_optimize = 1;
10232 break;
10233
10234 case OPTION_MIPS1:
10235 file_mips_isa = ISA_MIPS1;
10236 break;
10237
10238 case OPTION_MIPS2:
10239 file_mips_isa = ISA_MIPS2;
10240 break;
10241
10242 case OPTION_MIPS3:
10243 file_mips_isa = ISA_MIPS3;
10244 break;
10245
10246 case OPTION_MIPS4:
10247 file_mips_isa = ISA_MIPS4;
10248 break;
10249
10250 case OPTION_MIPS5:
10251 file_mips_isa = ISA_MIPS5;
10252 break;
10253
10254 case OPTION_MIPS32:
10255 file_mips_isa = ISA_MIPS32;
10256 break;
10257
10258 case OPTION_MIPS32R2:
10259 file_mips_isa = ISA_MIPS32R2;
10260 break;
10261
10262 case OPTION_MIPS64R2:
10263 file_mips_isa = ISA_MIPS64R2;
10264 break;
10265
10266 case OPTION_MIPS64:
10267 file_mips_isa = ISA_MIPS64;
10268 break;
10269
10270 case OPTION_MTUNE:
10271 mips_set_option_string (&mips_tune_string, arg);
10272 break;
10273
10274 case OPTION_MARCH:
10275 mips_set_option_string (&mips_arch_string, arg);
10276 break;
10277
10278 case OPTION_M4650:
10279 mips_set_option_string (&mips_arch_string, "4650");
10280 mips_set_option_string (&mips_tune_string, "4650");
10281 break;
10282
10283 case OPTION_NO_M4650:
10284 break;
10285
10286 case OPTION_M4010:
10287 mips_set_option_string (&mips_arch_string, "4010");
10288 mips_set_option_string (&mips_tune_string, "4010");
10289 break;
10290
10291 case OPTION_NO_M4010:
10292 break;
10293
10294 case OPTION_M4100:
10295 mips_set_option_string (&mips_arch_string, "4100");
10296 mips_set_option_string (&mips_tune_string, "4100");
10297 break;
10298
10299 case OPTION_NO_M4100:
10300 break;
10301
10302 case OPTION_M3900:
10303 mips_set_option_string (&mips_arch_string, "3900");
10304 mips_set_option_string (&mips_tune_string, "3900");
10305 break;
10306
10307 case OPTION_NO_M3900:
10308 break;
10309
10310 case OPTION_MDMX:
10311 mips_opts.ase_mdmx = 1;
10312 break;
10313
10314 case OPTION_NO_MDMX:
10315 mips_opts.ase_mdmx = 0;
10316 break;
10317
10318 case OPTION_MIPS16:
10319 mips_opts.mips16 = 1;
10320 mips_no_prev_insn (FALSE);
10321 break;
10322
10323 case OPTION_NO_MIPS16:
10324 mips_opts.mips16 = 0;
10325 mips_no_prev_insn (FALSE);
10326 break;
10327
10328 case OPTION_MIPS3D:
10329 mips_opts.ase_mips3d = 1;
10330 break;
10331
10332 case OPTION_NO_MIPS3D:
10333 mips_opts.ase_mips3d = 0;
10334 break;
10335
10336 case OPTION_FIX_VR4120:
10337 mips_fix_vr4120 = 1;
10338 break;
10339
10340 case OPTION_NO_FIX_VR4120:
10341 mips_fix_vr4120 = 0;
10342 break;
10343
10344 case OPTION_RELAX_BRANCH:
10345 mips_relax_branch = 1;
10346 break;
10347
10348 case OPTION_NO_RELAX_BRANCH:
10349 mips_relax_branch = 0;
10350 break;
10351
10352 case OPTION_MSHARED:
10353 mips_in_shared = TRUE;
10354 break;
10355
10356 case OPTION_MNO_SHARED:
10357 mips_in_shared = FALSE;
10358 break;
10359
10360 #ifdef OBJ_ELF
10361 /* When generating ELF code, we permit -KPIC and -call_shared to
10362 select SVR4_PIC, and -non_shared to select no PIC. This is
10363 intended to be compatible with Irix 5. */
10364 case OPTION_CALL_SHARED:
10365 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10366 {
10367 as_bad (_("-call_shared is supported only for ELF format"));
10368 return 0;
10369 }
10370 mips_pic = SVR4_PIC;
10371 mips_abicalls = TRUE;
10372 if (g_switch_seen && g_switch_value != 0)
10373 {
10374 as_bad (_("-G may not be used with SVR4 PIC code"));
10375 return 0;
10376 }
10377 g_switch_value = 0;
10378 break;
10379
10380 case OPTION_NON_SHARED:
10381 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10382 {
10383 as_bad (_("-non_shared is supported only for ELF format"));
10384 return 0;
10385 }
10386 mips_pic = NO_PIC;
10387 mips_abicalls = FALSE;
10388 break;
10389
10390 /* The -xgot option tells the assembler to use 32 offsets when
10391 accessing the got in SVR4_PIC mode. It is for Irix
10392 compatibility. */
10393 case OPTION_XGOT:
10394 mips_big_got = 1;
10395 break;
10396 #endif /* OBJ_ELF */
10397
10398 case 'G':
10399 if (mips_pic == SVR4_PIC)
10400 {
10401 as_bad (_("-G may not be used with SVR4 PIC code"));
10402 return 0;
10403 }
10404 else
10405 g_switch_value = atoi (arg);
10406 g_switch_seen = 1;
10407 break;
10408
10409 #ifdef OBJ_ELF
10410 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10411 and -mabi=64. */
10412 case OPTION_32:
10413 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10414 {
10415 as_bad (_("-32 is supported for ELF format only"));
10416 return 0;
10417 }
10418 mips_abi = O32_ABI;
10419 break;
10420
10421 case OPTION_N32:
10422 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10423 {
10424 as_bad (_("-n32 is supported for ELF format only"));
10425 return 0;
10426 }
10427 mips_abi = N32_ABI;
10428 break;
10429
10430 case OPTION_64:
10431 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10432 {
10433 as_bad (_("-64 is supported for ELF format only"));
10434 return 0;
10435 }
10436 mips_abi = N64_ABI;
10437 if (! support_64bit_objects())
10438 as_fatal (_("No compiled in support for 64 bit object file format"));
10439 break;
10440 #endif /* OBJ_ELF */
10441
10442 case OPTION_GP32:
10443 file_mips_gp32 = 1;
10444 break;
10445
10446 case OPTION_GP64:
10447 file_mips_gp32 = 0;
10448 break;
10449
10450 case OPTION_FP32:
10451 file_mips_fp32 = 1;
10452 break;
10453
10454 case OPTION_FP64:
10455 file_mips_fp32 = 0;
10456 break;
10457
10458 #ifdef OBJ_ELF
10459 case OPTION_MABI:
10460 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10461 {
10462 as_bad (_("-mabi is supported for ELF format only"));
10463 return 0;
10464 }
10465 if (strcmp (arg, "32") == 0)
10466 mips_abi = O32_ABI;
10467 else if (strcmp (arg, "o64") == 0)
10468 mips_abi = O64_ABI;
10469 else if (strcmp (arg, "n32") == 0)
10470 mips_abi = N32_ABI;
10471 else if (strcmp (arg, "64") == 0)
10472 {
10473 mips_abi = N64_ABI;
10474 if (! support_64bit_objects())
10475 as_fatal (_("No compiled in support for 64 bit object file "
10476 "format"));
10477 }
10478 else if (strcmp (arg, "eabi") == 0)
10479 mips_abi = EABI_ABI;
10480 else
10481 {
10482 as_fatal (_("invalid abi -mabi=%s"), arg);
10483 return 0;
10484 }
10485 break;
10486 #endif /* OBJ_ELF */
10487
10488 case OPTION_M7000_HILO_FIX:
10489 mips_7000_hilo_fix = TRUE;
10490 break;
10491
10492 case OPTION_MNO_7000_HILO_FIX:
10493 mips_7000_hilo_fix = FALSE;
10494 break;
10495
10496 #ifdef OBJ_ELF
10497 case OPTION_MDEBUG:
10498 mips_flag_mdebug = TRUE;
10499 break;
10500
10501 case OPTION_NO_MDEBUG:
10502 mips_flag_mdebug = FALSE;
10503 break;
10504
10505 case OPTION_PDR:
10506 mips_flag_pdr = TRUE;
10507 break;
10508
10509 case OPTION_NO_PDR:
10510 mips_flag_pdr = FALSE;
10511 break;
10512 #endif /* OBJ_ELF */
10513
10514 default:
10515 return 0;
10516 }
10517
10518 return 1;
10519 }
10520 \f
10521 /* Set up globals to generate code for the ISA or processor
10522 described by INFO. */
10523
10524 static void
10525 mips_set_architecture (const struct mips_cpu_info *info)
10526 {
10527 if (info != 0)
10528 {
10529 file_mips_arch = info->cpu;
10530 mips_opts.arch = info->cpu;
10531 mips_opts.isa = info->isa;
10532 }
10533 }
10534
10535
10536 /* Likewise for tuning. */
10537
10538 static void
10539 mips_set_tune (const struct mips_cpu_info *info)
10540 {
10541 if (info != 0)
10542 mips_tune = info->cpu;
10543 }
10544
10545
10546 void
10547 mips_after_parse_args (void)
10548 {
10549 const struct mips_cpu_info *arch_info = 0;
10550 const struct mips_cpu_info *tune_info = 0;
10551
10552 /* GP relative stuff not working for PE */
10553 if (strncmp (TARGET_OS, "pe", 2) == 0
10554 && g_switch_value != 0)
10555 {
10556 if (g_switch_seen)
10557 as_bad (_("-G not supported in this configuration."));
10558 g_switch_value = 0;
10559 }
10560
10561 if (mips_abi == NO_ABI)
10562 mips_abi = MIPS_DEFAULT_ABI;
10563
10564 /* The following code determines the architecture and register size.
10565 Similar code was added to GCC 3.3 (see override_options() in
10566 config/mips/mips.c). The GAS and GCC code should be kept in sync
10567 as much as possible. */
10568
10569 if (mips_arch_string != 0)
10570 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10571
10572 if (file_mips_isa != ISA_UNKNOWN)
10573 {
10574 /* Handle -mipsN. At this point, file_mips_isa contains the
10575 ISA level specified by -mipsN, while arch_info->isa contains
10576 the -march selection (if any). */
10577 if (arch_info != 0)
10578 {
10579 /* -march takes precedence over -mipsN, since it is more descriptive.
10580 There's no harm in specifying both as long as the ISA levels
10581 are the same. */
10582 if (file_mips_isa != arch_info->isa)
10583 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10584 mips_cpu_info_from_isa (file_mips_isa)->name,
10585 mips_cpu_info_from_isa (arch_info->isa)->name);
10586 }
10587 else
10588 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10589 }
10590
10591 if (arch_info == 0)
10592 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10593
10594 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10595 as_bad ("-march=%s is not compatible with the selected ABI",
10596 arch_info->name);
10597
10598 mips_set_architecture (arch_info);
10599
10600 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10601 if (mips_tune_string != 0)
10602 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10603
10604 if (tune_info == 0)
10605 mips_set_tune (arch_info);
10606 else
10607 mips_set_tune (tune_info);
10608
10609 if (file_mips_gp32 >= 0)
10610 {
10611 /* The user specified the size of the integer registers. Make sure
10612 it agrees with the ABI and ISA. */
10613 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10614 as_bad (_("-mgp64 used with a 32-bit processor"));
10615 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10616 as_bad (_("-mgp32 used with a 64-bit ABI"));
10617 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10618 as_bad (_("-mgp64 used with a 32-bit ABI"));
10619 }
10620 else
10621 {
10622 /* Infer the integer register size from the ABI and processor.
10623 Restrict ourselves to 32-bit registers if that's all the
10624 processor has, or if the ABI cannot handle 64-bit registers. */
10625 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10626 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10627 }
10628
10629 /* ??? GAS treats single-float processors as though they had 64-bit
10630 float registers (although it complains when double-precision
10631 instructions are used). As things stand, saying they have 32-bit
10632 registers would lead to spurious "register must be even" messages.
10633 So here we assume float registers are always the same size as
10634 integer ones, unless the user says otherwise. */
10635 if (file_mips_fp32 < 0)
10636 file_mips_fp32 = file_mips_gp32;
10637
10638 /* End of GCC-shared inference code. */
10639
10640 /* This flag is set when we have a 64-bit capable CPU but use only
10641 32-bit wide registers. Note that EABI does not use it. */
10642 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10643 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10644 || mips_abi == O32_ABI))
10645 mips_32bitmode = 1;
10646
10647 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10648 as_bad (_("trap exception not supported at ISA 1"));
10649
10650 /* If the selected architecture includes support for ASEs, enable
10651 generation of code for them. */
10652 if (mips_opts.mips16 == -1)
10653 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10654 if (mips_opts.ase_mips3d == -1)
10655 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10656 if (mips_opts.ase_mdmx == -1)
10657 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10658
10659 file_mips_isa = mips_opts.isa;
10660 file_ase_mips16 = mips_opts.mips16;
10661 file_ase_mips3d = mips_opts.ase_mips3d;
10662 file_ase_mdmx = mips_opts.ase_mdmx;
10663 mips_opts.gp32 = file_mips_gp32;
10664 mips_opts.fp32 = file_mips_fp32;
10665
10666 if (mips_flag_mdebug < 0)
10667 {
10668 #ifdef OBJ_MAYBE_ECOFF
10669 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10670 mips_flag_mdebug = 1;
10671 else
10672 #endif /* OBJ_MAYBE_ECOFF */
10673 mips_flag_mdebug = 0;
10674 }
10675 }
10676 \f
10677 void
10678 mips_init_after_args (void)
10679 {
10680 /* initialize opcodes */
10681 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10682 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10683 }
10684
10685 long
10686 md_pcrel_from (fixS *fixP)
10687 {
10688 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10689 switch (fixP->fx_r_type)
10690 {
10691 case BFD_RELOC_16_PCREL_S2:
10692 case BFD_RELOC_MIPS_JMP:
10693 /* Return the address of the delay slot. */
10694 return addr + 4;
10695 default:
10696 return addr;
10697 }
10698 }
10699
10700 /* This is called before the symbol table is processed. In order to
10701 work with gcc when using mips-tfile, we must keep all local labels.
10702 However, in other cases, we want to discard them. If we were
10703 called with -g, but we didn't see any debugging information, it may
10704 mean that gcc is smuggling debugging information through to
10705 mips-tfile, in which case we must generate all local labels. */
10706
10707 void
10708 mips_frob_file_before_adjust (void)
10709 {
10710 #ifndef NO_ECOFF_DEBUGGING
10711 if (ECOFF_DEBUGGING
10712 && mips_debug != 0
10713 && ! ecoff_debugging_seen)
10714 flag_keep_locals = 1;
10715 #endif
10716 }
10717
10718 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10719 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10720 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10721 relocation operators.
10722
10723 For our purposes, a %lo() expression matches a %got() or %hi()
10724 expression if:
10725
10726 (a) it refers to the same symbol; and
10727 (b) the offset applied in the %lo() expression is no lower than
10728 the offset applied in the %got() or %hi().
10729
10730 (b) allows us to cope with code like:
10731
10732 lui $4,%hi(foo)
10733 lh $4,%lo(foo+2)($4)
10734
10735 ...which is legal on RELA targets, and has a well-defined behaviour
10736 if the user knows that adding 2 to "foo" will not induce a carry to
10737 the high 16 bits.
10738
10739 When several %lo()s match a particular %got() or %hi(), we use the
10740 following rules to distinguish them:
10741
10742 (1) %lo()s with smaller offsets are a better match than %lo()s with
10743 higher offsets.
10744
10745 (2) %lo()s with no matching %got() or %hi() are better than those
10746 that already have a matching %got() or %hi().
10747
10748 (3) later %lo()s are better than earlier %lo()s.
10749
10750 These rules are applied in order.
10751
10752 (1) means, among other things, that %lo()s with identical offsets are
10753 chosen if they exist.
10754
10755 (2) means that we won't associate several high-part relocations with
10756 the same low-part relocation unless there's no alternative. Having
10757 several high parts for the same low part is a GNU extension; this rule
10758 allows careful users to avoid it.
10759
10760 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10761 with the last high-part relocation being at the front of the list.
10762 It therefore makes sense to choose the last matching low-part
10763 relocation, all other things being equal. It's also easier
10764 to code that way. */
10765
10766 void
10767 mips_frob_file (void)
10768 {
10769 struct mips_hi_fixup *l;
10770
10771 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10772 {
10773 segment_info_type *seginfo;
10774 bfd_boolean matched_lo_p;
10775 fixS **hi_pos, **lo_pos, **pos;
10776
10777 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10778
10779 /* If a GOT16 relocation turns out to be against a global symbol,
10780 there isn't supposed to be a matching LO. */
10781 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10782 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10783 continue;
10784
10785 /* Check quickly whether the next fixup happens to be a matching %lo. */
10786 if (fixup_has_matching_lo_p (l->fixp))
10787 continue;
10788
10789 seginfo = seg_info (l->seg);
10790
10791 /* Set HI_POS to the position of this relocation in the chain.
10792 Set LO_POS to the position of the chosen low-part relocation.
10793 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10794 relocation that matches an immediately-preceding high-part
10795 relocation. */
10796 hi_pos = NULL;
10797 lo_pos = NULL;
10798 matched_lo_p = FALSE;
10799 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10800 {
10801 if (*pos == l->fixp)
10802 hi_pos = pos;
10803
10804 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10805 && (*pos)->fx_addsy == l->fixp->fx_addsy
10806 && (*pos)->fx_offset >= l->fixp->fx_offset
10807 && (lo_pos == NULL
10808 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10809 || (!matched_lo_p
10810 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10811 lo_pos = pos;
10812
10813 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10814 && fixup_has_matching_lo_p (*pos));
10815 }
10816
10817 /* If we found a match, remove the high-part relocation from its
10818 current position and insert it before the low-part relocation.
10819 Make the offsets match so that fixup_has_matching_lo_p()
10820 will return true.
10821
10822 We don't warn about unmatched high-part relocations since some
10823 versions of gcc have been known to emit dead "lui ...%hi(...)"
10824 instructions. */
10825 if (lo_pos != NULL)
10826 {
10827 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10828 if (l->fixp->fx_next != *lo_pos)
10829 {
10830 *hi_pos = l->fixp->fx_next;
10831 l->fixp->fx_next = *lo_pos;
10832 *lo_pos = l->fixp;
10833 }
10834 }
10835 }
10836 }
10837
10838 /* We may have combined relocations without symbols in the N32/N64 ABI.
10839 We have to prevent gas from dropping them. */
10840
10841 int
10842 mips_force_relocation (fixS *fixp)
10843 {
10844 if (generic_force_reloc (fixp))
10845 return 1;
10846
10847 if (HAVE_NEWABI
10848 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10849 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10850 || fixp->fx_r_type == BFD_RELOC_HI16_S
10851 || fixp->fx_r_type == BFD_RELOC_LO16))
10852 return 1;
10853
10854 return 0;
10855 }
10856
10857 /* This hook is called before a fix is simplified. We don't really
10858 decide whether to skip a fix here. Rather, we turn global symbols
10859 used as branch targets into local symbols, such that they undergo
10860 simplification. We can only do this if the symbol is defined and
10861 it is in the same section as the branch. If this doesn't hold, we
10862 emit a better error message than just saying the relocation is not
10863 valid for the selected object format.
10864
10865 FIXP is the fix-up we're going to try to simplify, SEG is the
10866 segment in which the fix up occurs. The return value should be
10867 non-zero to indicate the fix-up is valid for further
10868 simplifications. */
10869
10870 int
10871 mips_validate_fix (struct fix *fixP, asection *seg)
10872 {
10873 /* There's a lot of discussion on whether it should be possible to
10874 use R_MIPS_PC16 to represent branch relocations. The outcome
10875 seems to be that it can, but gas/bfd are very broken in creating
10876 RELA relocations for this, so for now we only accept branches to
10877 symbols in the same section. Anything else is of dubious value,
10878 since there's no guarantee that at link time the symbol would be
10879 in range. Even for branches to local symbols this is arguably
10880 wrong, since it we assume the symbol is not going to be
10881 overridden, which should be possible per ELF library semantics,
10882 but then, there isn't a dynamic relocation that could be used to
10883 this effect, and the target would likely be out of range as well.
10884
10885 Unfortunately, it seems that there is too much code out there
10886 that relies on branches to symbols that are global to be resolved
10887 as if they were local, like the IRIX tools do, so we do it as
10888 well, but with a warning so that people are reminded to fix their
10889 code. If we ever get back to using R_MIPS_PC16 for branch
10890 targets, this entire block should go away (and probably the
10891 whole function). */
10892
10893 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10894 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10895 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10896 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10897 && fixP->fx_addsy)
10898 {
10899 if (! S_IS_DEFINED (fixP->fx_addsy))
10900 {
10901 as_bad_where (fixP->fx_file, fixP->fx_line,
10902 _("Cannot branch to undefined symbol."));
10903 /* Avoid any further errors about this fixup. */
10904 fixP->fx_done = 1;
10905 }
10906 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10907 {
10908 as_bad_where (fixP->fx_file, fixP->fx_line,
10909 _("Cannot branch to symbol in another section."));
10910 fixP->fx_done = 1;
10911 }
10912 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10913 {
10914 symbolS *sym = fixP->fx_addsy;
10915
10916 if (mips_pic == SVR4_PIC)
10917 as_warn_where (fixP->fx_file, fixP->fx_line,
10918 _("Pretending global symbol used as branch target is local."));
10919
10920 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10921 S_GET_SEGMENT (sym),
10922 S_GET_VALUE (sym),
10923 symbol_get_frag (sym));
10924 copy_symbol_attributes (fixP->fx_addsy, sym);
10925 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10926 assert (symbol_resolved_p (sym));
10927 symbol_mark_resolved (fixP->fx_addsy);
10928 }
10929 }
10930
10931 return 1;
10932 }
10933
10934 /* Apply a fixup to the object file. */
10935
10936 void
10937 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
10938 {
10939 bfd_byte *buf;
10940 long insn;
10941 reloc_howto_type *howto;
10942
10943 /* We ignore generic BFD relocations we don't know about. */
10944 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10945 if (! howto)
10946 return;
10947
10948 assert (fixP->fx_size == 4
10949 || fixP->fx_r_type == BFD_RELOC_16
10950 || fixP->fx_r_type == BFD_RELOC_64
10951 || fixP->fx_r_type == BFD_RELOC_CTOR
10952 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10953 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10954 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10955
10956 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
10957
10958 assert (! fixP->fx_pcrel);
10959
10960 /* Don't treat parts of a composite relocation as done. There are two
10961 reasons for this:
10962
10963 (1) The second and third parts will be against 0 (RSS_UNDEF) but
10964 should nevertheless be emitted if the first part is.
10965
10966 (2) In normal usage, composite relocations are never assembly-time
10967 constants. The easiest way of dealing with the pathological
10968 exceptions is to generate a relocation against STN_UNDEF and
10969 leave everything up to the linker. */
10970 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
10971 fixP->fx_done = 1;
10972
10973 switch (fixP->fx_r_type)
10974 {
10975 case BFD_RELOC_MIPS_JMP:
10976 case BFD_RELOC_MIPS_SHIFT5:
10977 case BFD_RELOC_MIPS_SHIFT6:
10978 case BFD_RELOC_MIPS_GOT_DISP:
10979 case BFD_RELOC_MIPS_GOT_PAGE:
10980 case BFD_RELOC_MIPS_GOT_OFST:
10981 case BFD_RELOC_MIPS_SUB:
10982 case BFD_RELOC_MIPS_INSERT_A:
10983 case BFD_RELOC_MIPS_INSERT_B:
10984 case BFD_RELOC_MIPS_DELETE:
10985 case BFD_RELOC_MIPS_HIGHEST:
10986 case BFD_RELOC_MIPS_HIGHER:
10987 case BFD_RELOC_MIPS_SCN_DISP:
10988 case BFD_RELOC_MIPS_REL16:
10989 case BFD_RELOC_MIPS_RELGOT:
10990 case BFD_RELOC_MIPS_JALR:
10991 case BFD_RELOC_HI16:
10992 case BFD_RELOC_HI16_S:
10993 case BFD_RELOC_GPREL16:
10994 case BFD_RELOC_MIPS_LITERAL:
10995 case BFD_RELOC_MIPS_CALL16:
10996 case BFD_RELOC_MIPS_GOT16:
10997 case BFD_RELOC_GPREL32:
10998 case BFD_RELOC_MIPS_GOT_HI16:
10999 case BFD_RELOC_MIPS_GOT_LO16:
11000 case BFD_RELOC_MIPS_CALL_HI16:
11001 case BFD_RELOC_MIPS_CALL_LO16:
11002 case BFD_RELOC_MIPS16_GPREL:
11003 assert (! fixP->fx_pcrel);
11004 /* Nothing needed to do. The value comes from the reloc entry */
11005 break;
11006
11007 case BFD_RELOC_MIPS16_JMP:
11008 /* We currently always generate a reloc against a symbol, which
11009 means that we don't want an addend even if the symbol is
11010 defined. */
11011 *valP = 0;
11012 break;
11013
11014 case BFD_RELOC_64:
11015 /* This is handled like BFD_RELOC_32, but we output a sign
11016 extended value if we are only 32 bits. */
11017 if (fixP->fx_done)
11018 {
11019 if (8 <= sizeof (valueT))
11020 md_number_to_chars (buf, *valP, 8);
11021 else
11022 {
11023 valueT hiv;
11024
11025 if ((*valP & 0x80000000) != 0)
11026 hiv = 0xffffffff;
11027 else
11028 hiv = 0;
11029 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11030 *valP, 4);
11031 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11032 hiv, 4);
11033 }
11034 }
11035 break;
11036
11037 case BFD_RELOC_RVA:
11038 case BFD_RELOC_32:
11039 /* If we are deleting this reloc entry, we must fill in the
11040 value now. This can happen if we have a .word which is not
11041 resolved when it appears but is later defined. */
11042 if (fixP->fx_done)
11043 md_number_to_chars (buf, *valP, 4);
11044 break;
11045
11046 case BFD_RELOC_16:
11047 /* If we are deleting this reloc entry, we must fill in the
11048 value now. */
11049 if (fixP->fx_done)
11050 md_number_to_chars (buf, *valP, 2);
11051 break;
11052
11053 case BFD_RELOC_LO16:
11054 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11055 may be safe to remove, but if so it's not obvious. */
11056 /* When handling an embedded PIC switch statement, we can wind
11057 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11058 if (fixP->fx_done)
11059 {
11060 if (*valP + 0x8000 > 0xffff)
11061 as_bad_where (fixP->fx_file, fixP->fx_line,
11062 _("relocation overflow"));
11063 if (target_big_endian)
11064 buf += 2;
11065 md_number_to_chars (buf, *valP, 2);
11066 }
11067 break;
11068
11069 case BFD_RELOC_16_PCREL_S2:
11070 if ((*valP & 0x3) != 0)
11071 as_bad_where (fixP->fx_file, fixP->fx_line,
11072 _("Branch to odd address (%lx)"), (long) *valP);
11073
11074 /*
11075 * We need to save the bits in the instruction since fixup_segment()
11076 * might be deleting the relocation entry (i.e., a branch within
11077 * the current segment).
11078 */
11079 if (! fixP->fx_done)
11080 break;
11081
11082 /* update old instruction data */
11083 if (target_big_endian)
11084 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11085 else
11086 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11087
11088 if (*valP + 0x20000 <= 0x3ffff)
11089 {
11090 insn |= (*valP >> 2) & 0xffff;
11091 md_number_to_chars (buf, insn, 4);
11092 }
11093 else if (mips_pic == NO_PIC
11094 && fixP->fx_done
11095 && fixP->fx_frag->fr_address >= text_section->vma
11096 && (fixP->fx_frag->fr_address
11097 < text_section->vma + bfd_get_section_size (text_section))
11098 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11099 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11100 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11101 {
11102 /* The branch offset is too large. If this is an
11103 unconditional branch, and we are not generating PIC code,
11104 we can convert it to an absolute jump instruction. */
11105 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11106 insn = 0x0c000000; /* jal */
11107 else
11108 insn = 0x08000000; /* j */
11109 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11110 fixP->fx_done = 0;
11111 fixP->fx_addsy = section_symbol (text_section);
11112 *valP += md_pcrel_from (fixP);
11113 md_number_to_chars (buf, insn, 4);
11114 }
11115 else
11116 {
11117 /* If we got here, we have branch-relaxation disabled,
11118 and there's nothing we can do to fix this instruction
11119 without turning it into a longer sequence. */
11120 as_bad_where (fixP->fx_file, fixP->fx_line,
11121 _("Branch out of range"));
11122 }
11123 break;
11124
11125 case BFD_RELOC_VTABLE_INHERIT:
11126 fixP->fx_done = 0;
11127 if (fixP->fx_addsy
11128 && !S_IS_DEFINED (fixP->fx_addsy)
11129 && !S_IS_WEAK (fixP->fx_addsy))
11130 S_SET_WEAK (fixP->fx_addsy);
11131 break;
11132
11133 case BFD_RELOC_VTABLE_ENTRY:
11134 fixP->fx_done = 0;
11135 break;
11136
11137 default:
11138 internalError ();
11139 }
11140
11141 /* Remember value for tc_gen_reloc. */
11142 fixP->fx_addnumber = *valP;
11143 }
11144
11145 #if 0
11146 void
11147 printInsn (unsigned long oc)
11148 {
11149 const struct mips_opcode *p;
11150 int treg, sreg, dreg, shamt;
11151 short imm;
11152 const char *args;
11153 int i;
11154
11155 for (i = 0; i < NUMOPCODES; ++i)
11156 {
11157 p = &mips_opcodes[i];
11158 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11159 {
11160 printf ("%08lx %s\t", oc, p->name);
11161 treg = (oc >> 16) & 0x1f;
11162 sreg = (oc >> 21) & 0x1f;
11163 dreg = (oc >> 11) & 0x1f;
11164 shamt = (oc >> 6) & 0x1f;
11165 imm = oc;
11166 for (args = p->args;; ++args)
11167 {
11168 switch (*args)
11169 {
11170 case '\0':
11171 printf ("\n");
11172 break;
11173
11174 case ',':
11175 case '(':
11176 case ')':
11177 printf ("%c", *args);
11178 continue;
11179
11180 case 'r':
11181 assert (treg == sreg);
11182 printf ("$%d,$%d", treg, sreg);
11183 continue;
11184
11185 case 'd':
11186 case 'G':
11187 printf ("$%d", dreg);
11188 continue;
11189
11190 case 't':
11191 case 'E':
11192 printf ("$%d", treg);
11193 continue;
11194
11195 case 'k':
11196 printf ("0x%x", treg);
11197 continue;
11198
11199 case 'b':
11200 case 's':
11201 printf ("$%d", sreg);
11202 continue;
11203
11204 case 'a':
11205 printf ("0x%08lx", oc & 0x1ffffff);
11206 continue;
11207
11208 case 'i':
11209 case 'j':
11210 case 'o':
11211 case 'u':
11212 printf ("%d", imm);
11213 continue;
11214
11215 case '<':
11216 case '>':
11217 printf ("$%d", shamt);
11218 continue;
11219
11220 default:
11221 internalError ();
11222 }
11223 break;
11224 }
11225 return;
11226 }
11227 }
11228 printf (_("%08lx UNDEFINED\n"), oc);
11229 }
11230 #endif
11231
11232 static symbolS *
11233 get_symbol (void)
11234 {
11235 int c;
11236 char *name;
11237 symbolS *p;
11238
11239 name = input_line_pointer;
11240 c = get_symbol_end ();
11241 p = (symbolS *) symbol_find_or_make (name);
11242 *input_line_pointer = c;
11243 return p;
11244 }
11245
11246 /* Align the current frag to a given power of two. The MIPS assembler
11247 also automatically adjusts any preceding label. */
11248
11249 static void
11250 mips_align (int to, int fill, symbolS *label)
11251 {
11252 mips_emit_delays (FALSE);
11253 frag_align (to, fill, 0);
11254 record_alignment (now_seg, to);
11255 if (label != NULL)
11256 {
11257 assert (S_GET_SEGMENT (label) == now_seg);
11258 symbol_set_frag (label, frag_now);
11259 S_SET_VALUE (label, (valueT) frag_now_fix ());
11260 }
11261 }
11262
11263 /* Align to a given power of two. .align 0 turns off the automatic
11264 alignment used by the data creating pseudo-ops. */
11265
11266 static void
11267 s_align (int x ATTRIBUTE_UNUSED)
11268 {
11269 register int temp;
11270 register long temp_fill;
11271 long max_alignment = 15;
11272
11273 /*
11274
11275 o Note that the assembler pulls down any immediately preceding label
11276 to the aligned address.
11277 o It's not documented but auto alignment is reinstated by
11278 a .align pseudo instruction.
11279 o Note also that after auto alignment is turned off the mips assembler
11280 issues an error on attempt to assemble an improperly aligned data item.
11281 We don't.
11282
11283 */
11284
11285 temp = get_absolute_expression ();
11286 if (temp > max_alignment)
11287 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11288 else if (temp < 0)
11289 {
11290 as_warn (_("Alignment negative: 0 assumed."));
11291 temp = 0;
11292 }
11293 if (*input_line_pointer == ',')
11294 {
11295 ++input_line_pointer;
11296 temp_fill = get_absolute_expression ();
11297 }
11298 else
11299 temp_fill = 0;
11300 if (temp)
11301 {
11302 auto_align = 1;
11303 mips_align (temp, (int) temp_fill,
11304 insn_labels != NULL ? insn_labels->label : NULL);
11305 }
11306 else
11307 {
11308 auto_align = 0;
11309 }
11310
11311 demand_empty_rest_of_line ();
11312 }
11313
11314 void
11315 mips_flush_pending_output (void)
11316 {
11317 mips_emit_delays (FALSE);
11318 mips_clear_insn_labels ();
11319 }
11320
11321 static void
11322 s_change_sec (int sec)
11323 {
11324 segT seg;
11325
11326 #ifdef OBJ_ELF
11327 /* The ELF backend needs to know that we are changing sections, so
11328 that .previous works correctly. We could do something like check
11329 for an obj_section_change_hook macro, but that might be confusing
11330 as it would not be appropriate to use it in the section changing
11331 functions in read.c, since obj-elf.c intercepts those. FIXME:
11332 This should be cleaner, somehow. */
11333 obj_elf_section_change_hook ();
11334 #endif
11335
11336 mips_emit_delays (FALSE);
11337 switch (sec)
11338 {
11339 case 't':
11340 s_text (0);
11341 break;
11342 case 'd':
11343 s_data (0);
11344 break;
11345 case 'b':
11346 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11347 demand_empty_rest_of_line ();
11348 break;
11349
11350 case 'r':
11351 seg = subseg_new (RDATA_SECTION_NAME,
11352 (subsegT) get_absolute_expression ());
11353 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11354 {
11355 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11356 | SEC_READONLY | SEC_RELOC
11357 | SEC_DATA));
11358 if (strcmp (TARGET_OS, "elf") != 0)
11359 record_alignment (seg, 4);
11360 }
11361 demand_empty_rest_of_line ();
11362 break;
11363
11364 case 's':
11365 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11366 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11367 {
11368 bfd_set_section_flags (stdoutput, seg,
11369 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11370 if (strcmp (TARGET_OS, "elf") != 0)
11371 record_alignment (seg, 4);
11372 }
11373 demand_empty_rest_of_line ();
11374 break;
11375 }
11376
11377 auto_align = 1;
11378 }
11379
11380 void
11381 s_change_section (int ignore ATTRIBUTE_UNUSED)
11382 {
11383 #ifdef OBJ_ELF
11384 char *section_name;
11385 char c;
11386 char next_c = 0;
11387 int section_type;
11388 int section_flag;
11389 int section_entry_size;
11390 int section_alignment;
11391
11392 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11393 return;
11394
11395 section_name = input_line_pointer;
11396 c = get_symbol_end ();
11397 if (c)
11398 next_c = *(input_line_pointer + 1);
11399
11400 /* Do we have .section Name<,"flags">? */
11401 if (c != ',' || (c == ',' && next_c == '"'))
11402 {
11403 /* just after name is now '\0'. */
11404 *input_line_pointer = c;
11405 input_line_pointer = section_name;
11406 obj_elf_section (ignore);
11407 return;
11408 }
11409 input_line_pointer++;
11410
11411 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11412 if (c == ',')
11413 section_type = get_absolute_expression ();
11414 else
11415 section_type = 0;
11416 if (*input_line_pointer++ == ',')
11417 section_flag = get_absolute_expression ();
11418 else
11419 section_flag = 0;
11420 if (*input_line_pointer++ == ',')
11421 section_entry_size = get_absolute_expression ();
11422 else
11423 section_entry_size = 0;
11424 if (*input_line_pointer++ == ',')
11425 section_alignment = get_absolute_expression ();
11426 else
11427 section_alignment = 0;
11428
11429 section_name = xstrdup (section_name);
11430
11431 /* When using the generic form of .section (as implemented by obj-elf.c),
11432 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11433 traditionally had to fall back on the more common @progbits instead.
11434
11435 There's nothing really harmful in this, since bfd will correct
11436 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11437 means that, for backwards compatibiltiy, the special_section entries
11438 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11439
11440 Even so, we shouldn't force users of the MIPS .section syntax to
11441 incorrectly label the sections as SHT_PROGBITS. The best compromise
11442 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11443 generic type-checking code. */
11444 if (section_type == SHT_MIPS_DWARF)
11445 section_type = SHT_PROGBITS;
11446
11447 obj_elf_change_section (section_name, section_type, section_flag,
11448 section_entry_size, 0, 0, 0);
11449
11450 if (now_seg->name != section_name)
11451 free (section_name);
11452 #endif /* OBJ_ELF */
11453 }
11454
11455 void
11456 mips_enable_auto_align (void)
11457 {
11458 auto_align = 1;
11459 }
11460
11461 static void
11462 s_cons (int log_size)
11463 {
11464 symbolS *label;
11465
11466 label = insn_labels != NULL ? insn_labels->label : NULL;
11467 mips_emit_delays (FALSE);
11468 if (log_size > 0 && auto_align)
11469 mips_align (log_size, 0, label);
11470 mips_clear_insn_labels ();
11471 cons (1 << log_size);
11472 }
11473
11474 static void
11475 s_float_cons (int type)
11476 {
11477 symbolS *label;
11478
11479 label = insn_labels != NULL ? insn_labels->label : NULL;
11480
11481 mips_emit_delays (FALSE);
11482
11483 if (auto_align)
11484 {
11485 if (type == 'd')
11486 mips_align (3, 0, label);
11487 else
11488 mips_align (2, 0, label);
11489 }
11490
11491 mips_clear_insn_labels ();
11492
11493 float_cons (type);
11494 }
11495
11496 /* Handle .globl. We need to override it because on Irix 5 you are
11497 permitted to say
11498 .globl foo .text
11499 where foo is an undefined symbol, to mean that foo should be
11500 considered to be the address of a function. */
11501
11502 static void
11503 s_mips_globl (int x ATTRIBUTE_UNUSED)
11504 {
11505 char *name;
11506 int c;
11507 symbolS *symbolP;
11508 flagword flag;
11509
11510 name = input_line_pointer;
11511 c = get_symbol_end ();
11512 symbolP = symbol_find_or_make (name);
11513 *input_line_pointer = c;
11514 SKIP_WHITESPACE ();
11515
11516 /* On Irix 5, every global symbol that is not explicitly labelled as
11517 being a function is apparently labelled as being an object. */
11518 flag = BSF_OBJECT;
11519
11520 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11521 {
11522 char *secname;
11523 asection *sec;
11524
11525 secname = input_line_pointer;
11526 c = get_symbol_end ();
11527 sec = bfd_get_section_by_name (stdoutput, secname);
11528 if (sec == NULL)
11529 as_bad (_("%s: no such section"), secname);
11530 *input_line_pointer = c;
11531
11532 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11533 flag = BSF_FUNCTION;
11534 }
11535
11536 symbol_get_bfdsym (symbolP)->flags |= flag;
11537
11538 S_SET_EXTERNAL (symbolP);
11539 demand_empty_rest_of_line ();
11540 }
11541
11542 static void
11543 s_option (int x ATTRIBUTE_UNUSED)
11544 {
11545 char *opt;
11546 char c;
11547
11548 opt = input_line_pointer;
11549 c = get_symbol_end ();
11550
11551 if (*opt == 'O')
11552 {
11553 /* FIXME: What does this mean? */
11554 }
11555 else if (strncmp (opt, "pic", 3) == 0)
11556 {
11557 int i;
11558
11559 i = atoi (opt + 3);
11560 if (i == 0)
11561 mips_pic = NO_PIC;
11562 else if (i == 2)
11563 {
11564 mips_pic = SVR4_PIC;
11565 mips_abicalls = TRUE;
11566 }
11567 else
11568 as_bad (_(".option pic%d not supported"), i);
11569
11570 if (mips_pic == SVR4_PIC)
11571 {
11572 if (g_switch_seen && g_switch_value != 0)
11573 as_warn (_("-G may not be used with SVR4 PIC code"));
11574 g_switch_value = 0;
11575 bfd_set_gp_size (stdoutput, 0);
11576 }
11577 }
11578 else
11579 as_warn (_("Unrecognized option \"%s\""), opt);
11580
11581 *input_line_pointer = c;
11582 demand_empty_rest_of_line ();
11583 }
11584
11585 /* This structure is used to hold a stack of .set values. */
11586
11587 struct mips_option_stack
11588 {
11589 struct mips_option_stack *next;
11590 struct mips_set_options options;
11591 };
11592
11593 static struct mips_option_stack *mips_opts_stack;
11594
11595 /* Handle the .set pseudo-op. */
11596
11597 static void
11598 s_mipsset (int x ATTRIBUTE_UNUSED)
11599 {
11600 char *name = input_line_pointer, ch;
11601
11602 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11603 ++input_line_pointer;
11604 ch = *input_line_pointer;
11605 *input_line_pointer = '\0';
11606
11607 if (strcmp (name, "reorder") == 0)
11608 {
11609 if (mips_opts.noreorder && prev_nop_frag != NULL)
11610 {
11611 /* If we still have pending nops, we can discard them. The
11612 usual nop handling will insert any that are still
11613 needed. */
11614 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11615 * (mips_opts.mips16 ? 2 : 4));
11616 prev_nop_frag = NULL;
11617 }
11618 mips_opts.noreorder = 0;
11619 }
11620 else if (strcmp (name, "noreorder") == 0)
11621 {
11622 mips_emit_delays (TRUE);
11623 mips_opts.noreorder = 1;
11624 mips_any_noreorder = 1;
11625 }
11626 else if (strcmp (name, "at") == 0)
11627 {
11628 mips_opts.noat = 0;
11629 }
11630 else if (strcmp (name, "noat") == 0)
11631 {
11632 mips_opts.noat = 1;
11633 }
11634 else if (strcmp (name, "macro") == 0)
11635 {
11636 mips_opts.warn_about_macros = 0;
11637 }
11638 else if (strcmp (name, "nomacro") == 0)
11639 {
11640 if (mips_opts.noreorder == 0)
11641 as_bad (_("`noreorder' must be set before `nomacro'"));
11642 mips_opts.warn_about_macros = 1;
11643 }
11644 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11645 {
11646 mips_opts.nomove = 0;
11647 }
11648 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11649 {
11650 mips_opts.nomove = 1;
11651 }
11652 else if (strcmp (name, "bopt") == 0)
11653 {
11654 mips_opts.nobopt = 0;
11655 }
11656 else if (strcmp (name, "nobopt") == 0)
11657 {
11658 mips_opts.nobopt = 1;
11659 }
11660 else if (strcmp (name, "mips16") == 0
11661 || strcmp (name, "MIPS-16") == 0)
11662 mips_opts.mips16 = 1;
11663 else if (strcmp (name, "nomips16") == 0
11664 || strcmp (name, "noMIPS-16") == 0)
11665 mips_opts.mips16 = 0;
11666 else if (strcmp (name, "mips3d") == 0)
11667 mips_opts.ase_mips3d = 1;
11668 else if (strcmp (name, "nomips3d") == 0)
11669 mips_opts.ase_mips3d = 0;
11670 else if (strcmp (name, "mdmx") == 0)
11671 mips_opts.ase_mdmx = 1;
11672 else if (strcmp (name, "nomdmx") == 0)
11673 mips_opts.ase_mdmx = 0;
11674 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11675 {
11676 int reset = 0;
11677
11678 /* Permit the user to change the ISA and architecture on the fly.
11679 Needless to say, misuse can cause serious problems. */
11680 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11681 {
11682 reset = 1;
11683 mips_opts.isa = file_mips_isa;
11684 mips_opts.arch = file_mips_arch;
11685 }
11686 else if (strncmp (name, "arch=", 5) == 0)
11687 {
11688 const struct mips_cpu_info *p;
11689
11690 p = mips_parse_cpu("internal use", name + 5);
11691 if (!p)
11692 as_bad (_("unknown architecture %s"), name + 5);
11693 else
11694 {
11695 mips_opts.arch = p->cpu;
11696 mips_opts.isa = p->isa;
11697 }
11698 }
11699 else if (strncmp (name, "mips", 4) == 0)
11700 {
11701 const struct mips_cpu_info *p;
11702
11703 p = mips_parse_cpu("internal use", name);
11704 if (!p)
11705 as_bad (_("unknown ISA level %s"), name + 4);
11706 else
11707 {
11708 mips_opts.arch = p->cpu;
11709 mips_opts.isa = p->isa;
11710 }
11711 }
11712 else
11713 as_bad (_("unknown ISA or architecture %s"), name);
11714
11715 switch (mips_opts.isa)
11716 {
11717 case 0:
11718 break;
11719 case ISA_MIPS1:
11720 case ISA_MIPS2:
11721 case ISA_MIPS32:
11722 case ISA_MIPS32R2:
11723 mips_opts.gp32 = 1;
11724 mips_opts.fp32 = 1;
11725 break;
11726 case ISA_MIPS3:
11727 case ISA_MIPS4:
11728 case ISA_MIPS5:
11729 case ISA_MIPS64:
11730 case ISA_MIPS64R2:
11731 mips_opts.gp32 = 0;
11732 mips_opts.fp32 = 0;
11733 break;
11734 default:
11735 as_bad (_("unknown ISA level %s"), name + 4);
11736 break;
11737 }
11738 if (reset)
11739 {
11740 mips_opts.gp32 = file_mips_gp32;
11741 mips_opts.fp32 = file_mips_fp32;
11742 }
11743 }
11744 else if (strcmp (name, "autoextend") == 0)
11745 mips_opts.noautoextend = 0;
11746 else if (strcmp (name, "noautoextend") == 0)
11747 mips_opts.noautoextend = 1;
11748 else if (strcmp (name, "push") == 0)
11749 {
11750 struct mips_option_stack *s;
11751
11752 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11753 s->next = mips_opts_stack;
11754 s->options = mips_opts;
11755 mips_opts_stack = s;
11756 }
11757 else if (strcmp (name, "pop") == 0)
11758 {
11759 struct mips_option_stack *s;
11760
11761 s = mips_opts_stack;
11762 if (s == NULL)
11763 as_bad (_(".set pop with no .set push"));
11764 else
11765 {
11766 /* If we're changing the reorder mode we need to handle
11767 delay slots correctly. */
11768 if (s->options.noreorder && ! mips_opts.noreorder)
11769 mips_emit_delays (TRUE);
11770 else if (! s->options.noreorder && mips_opts.noreorder)
11771 {
11772 if (prev_nop_frag != NULL)
11773 {
11774 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11775 * (mips_opts.mips16 ? 2 : 4));
11776 prev_nop_frag = NULL;
11777 }
11778 }
11779
11780 mips_opts = s->options;
11781 mips_opts_stack = s->next;
11782 free (s);
11783 }
11784 }
11785 else
11786 {
11787 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11788 }
11789 *input_line_pointer = ch;
11790 demand_empty_rest_of_line ();
11791 }
11792
11793 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11794 .option pic2. It means to generate SVR4 PIC calls. */
11795
11796 static void
11797 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11798 {
11799 mips_pic = SVR4_PIC;
11800 mips_abicalls = TRUE;
11801
11802 if (g_switch_seen && g_switch_value != 0)
11803 as_warn (_("-G may not be used with SVR4 PIC code"));
11804 g_switch_value = 0;
11805
11806 bfd_set_gp_size (stdoutput, 0);
11807 demand_empty_rest_of_line ();
11808 }
11809
11810 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11811 PIC code. It sets the $gp register for the function based on the
11812 function address, which is in the register named in the argument.
11813 This uses a relocation against _gp_disp, which is handled specially
11814 by the linker. The result is:
11815 lui $gp,%hi(_gp_disp)
11816 addiu $gp,$gp,%lo(_gp_disp)
11817 addu $gp,$gp,.cpload argument
11818 The .cpload argument is normally $25 == $t9.
11819
11820 The -mno-shared option changes this to:
11821 lui $gp,%hi(_gp)
11822 addiu $gp,$gp,%lo(_gp)
11823 and the argument is ignored. This saves an instruction, but the
11824 resulting code is not position independent; it uses an absolute
11825 address for _gp. Thus code assembled with -mno-shared can go into
11826 an ordinary executable, but not into a shared library. */
11827
11828 static void
11829 s_cpload (int ignore ATTRIBUTE_UNUSED)
11830 {
11831 expressionS ex;
11832 int reg;
11833 int in_shared;
11834
11835 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11836 .cpload is ignored. */
11837 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11838 {
11839 s_ignore (0);
11840 return;
11841 }
11842
11843 /* .cpload should be in a .set noreorder section. */
11844 if (mips_opts.noreorder == 0)
11845 as_warn (_(".cpload not in noreorder section"));
11846
11847 reg = tc_get_register (0);
11848
11849 /* If we need to produce a 64-bit address, we are better off using
11850 the default instruction sequence. */
11851 in_shared = mips_in_shared || HAVE_64BIT_ADDRESSES;
11852
11853 ex.X_op = O_symbol;
11854 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" : "_gp");
11855 ex.X_op_symbol = NULL;
11856 ex.X_add_number = 0;
11857
11858 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11859 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11860
11861 macro_start ();
11862 macro_build_lui (&ex, mips_gp_register);
11863 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11864 mips_gp_register, BFD_RELOC_LO16);
11865 if (in_shared)
11866 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11867 mips_gp_register, reg);
11868 macro_end ();
11869
11870 demand_empty_rest_of_line ();
11871 }
11872
11873 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11874 .cpsetup $reg1, offset|$reg2, label
11875
11876 If offset is given, this results in:
11877 sd $gp, offset($sp)
11878 lui $gp, %hi(%neg(%gp_rel(label)))
11879 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11880 daddu $gp, $gp, $reg1
11881
11882 If $reg2 is given, this results in:
11883 daddu $reg2, $gp, $0
11884 lui $gp, %hi(%neg(%gp_rel(label)))
11885 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11886 daddu $gp, $gp, $reg1
11887 $reg1 is normally $25 == $t9.
11888
11889 The -mno-shared option replaces the last three instructions with
11890 lui $gp,%hi(_gp)
11891 addiu $gp,$gp,%lo(_gp)
11892 */
11893
11894 static void
11895 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11896 {
11897 expressionS ex_off;
11898 expressionS ex_sym;
11899 int reg1;
11900
11901 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11902 We also need NewABI support. */
11903 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11904 {
11905 s_ignore (0);
11906 return;
11907 }
11908
11909 reg1 = tc_get_register (0);
11910 SKIP_WHITESPACE ();
11911 if (*input_line_pointer != ',')
11912 {
11913 as_bad (_("missing argument separator ',' for .cpsetup"));
11914 return;
11915 }
11916 else
11917 ++input_line_pointer;
11918 SKIP_WHITESPACE ();
11919 if (*input_line_pointer == '$')
11920 {
11921 mips_cpreturn_register = tc_get_register (0);
11922 mips_cpreturn_offset = -1;
11923 }
11924 else
11925 {
11926 mips_cpreturn_offset = get_absolute_expression ();
11927 mips_cpreturn_register = -1;
11928 }
11929 SKIP_WHITESPACE ();
11930 if (*input_line_pointer != ',')
11931 {
11932 as_bad (_("missing argument separator ',' for .cpsetup"));
11933 return;
11934 }
11935 else
11936 ++input_line_pointer;
11937 SKIP_WHITESPACE ();
11938 expression (&ex_sym);
11939
11940 macro_start ();
11941 if (mips_cpreturn_register == -1)
11942 {
11943 ex_off.X_op = O_constant;
11944 ex_off.X_add_symbol = NULL;
11945 ex_off.X_op_symbol = NULL;
11946 ex_off.X_add_number = mips_cpreturn_offset;
11947
11948 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11949 BFD_RELOC_LO16, SP);
11950 }
11951 else
11952 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11953 mips_gp_register, 0);
11954
11955 if (mips_in_shared || HAVE_64BIT_ADDRESSES)
11956 {
11957 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11958 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11959 BFD_RELOC_HI16_S);
11960
11961 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11962 mips_gp_register, -1, BFD_RELOC_GPREL16,
11963 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11964
11965 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11966 mips_gp_register, reg1);
11967 }
11968 else
11969 {
11970 expressionS ex;
11971
11972 ex.X_op = O_symbol;
11973 ex.X_add_symbol = symbol_find_or_make ("_gp");
11974 ex.X_op_symbol = NULL;
11975 ex.X_add_number = 0;
11976
11977 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11978 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11979
11980 macro_build_lui (&ex, mips_gp_register);
11981 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11982 mips_gp_register, BFD_RELOC_LO16);
11983 }
11984
11985 macro_end ();
11986
11987 demand_empty_rest_of_line ();
11988 }
11989
11990 static void
11991 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11992 {
11993 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11994 .cplocal is ignored. */
11995 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11996 {
11997 s_ignore (0);
11998 return;
11999 }
12000
12001 mips_gp_register = tc_get_register (0);
12002 demand_empty_rest_of_line ();
12003 }
12004
12005 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12006 offset from $sp. The offset is remembered, and after making a PIC
12007 call $gp is restored from that location. */
12008
12009 static void
12010 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12011 {
12012 expressionS ex;
12013
12014 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12015 .cprestore is ignored. */
12016 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12017 {
12018 s_ignore (0);
12019 return;
12020 }
12021
12022 mips_cprestore_offset = get_absolute_expression ();
12023 mips_cprestore_valid = 1;
12024
12025 ex.X_op = O_constant;
12026 ex.X_add_symbol = NULL;
12027 ex.X_op_symbol = NULL;
12028 ex.X_add_number = mips_cprestore_offset;
12029
12030 macro_start ();
12031 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12032 SP, HAVE_64BIT_ADDRESSES);
12033 macro_end ();
12034
12035 demand_empty_rest_of_line ();
12036 }
12037
12038 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12039 was given in the preceding .cpsetup, it results in:
12040 ld $gp, offset($sp)
12041
12042 If a register $reg2 was given there, it results in:
12043 daddu $gp, $reg2, $0
12044 */
12045 static void
12046 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12047 {
12048 expressionS ex;
12049
12050 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12051 We also need NewABI support. */
12052 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12053 {
12054 s_ignore (0);
12055 return;
12056 }
12057
12058 macro_start ();
12059 if (mips_cpreturn_register == -1)
12060 {
12061 ex.X_op = O_constant;
12062 ex.X_add_symbol = NULL;
12063 ex.X_op_symbol = NULL;
12064 ex.X_add_number = mips_cpreturn_offset;
12065
12066 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12067 }
12068 else
12069 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12070 mips_cpreturn_register, 0);
12071 macro_end ();
12072
12073 demand_empty_rest_of_line ();
12074 }
12075
12076 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12077 code. It sets the offset to use in gp_rel relocations. */
12078
12079 static void
12080 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12081 {
12082 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12083 We also need NewABI support. */
12084 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12085 {
12086 s_ignore (0);
12087 return;
12088 }
12089
12090 mips_gprel_offset = get_absolute_expression ();
12091
12092 demand_empty_rest_of_line ();
12093 }
12094
12095 /* Handle the .gpword pseudo-op. This is used when generating PIC
12096 code. It generates a 32 bit GP relative reloc. */
12097
12098 static void
12099 s_gpword (int ignore ATTRIBUTE_UNUSED)
12100 {
12101 symbolS *label;
12102 expressionS ex;
12103 char *p;
12104
12105 /* When not generating PIC code, this is treated as .word. */
12106 if (mips_pic != SVR4_PIC)
12107 {
12108 s_cons (2);
12109 return;
12110 }
12111
12112 label = insn_labels != NULL ? insn_labels->label : NULL;
12113 mips_emit_delays (TRUE);
12114 if (auto_align)
12115 mips_align (2, 0, label);
12116 mips_clear_insn_labels ();
12117
12118 expression (&ex);
12119
12120 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12121 {
12122 as_bad (_("Unsupported use of .gpword"));
12123 ignore_rest_of_line ();
12124 }
12125
12126 p = frag_more (4);
12127 md_number_to_chars (p, 0, 4);
12128 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12129 BFD_RELOC_GPREL32);
12130
12131 demand_empty_rest_of_line ();
12132 }
12133
12134 static void
12135 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12136 {
12137 symbolS *label;
12138 expressionS ex;
12139 char *p;
12140
12141 /* When not generating PIC code, this is treated as .dword. */
12142 if (mips_pic != SVR4_PIC)
12143 {
12144 s_cons (3);
12145 return;
12146 }
12147
12148 label = insn_labels != NULL ? insn_labels->label : NULL;
12149 mips_emit_delays (TRUE);
12150 if (auto_align)
12151 mips_align (3, 0, label);
12152 mips_clear_insn_labels ();
12153
12154 expression (&ex);
12155
12156 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12157 {
12158 as_bad (_("Unsupported use of .gpdword"));
12159 ignore_rest_of_line ();
12160 }
12161
12162 p = frag_more (8);
12163 md_number_to_chars (p, 0, 8);
12164 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12165 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12166
12167 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12168 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12169 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12170
12171 demand_empty_rest_of_line ();
12172 }
12173
12174 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12175 tables in SVR4 PIC code. */
12176
12177 static void
12178 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12179 {
12180 int reg;
12181
12182 /* This is ignored when not generating SVR4 PIC code. */
12183 if (mips_pic != SVR4_PIC)
12184 {
12185 s_ignore (0);
12186 return;
12187 }
12188
12189 /* Add $gp to the register named as an argument. */
12190 macro_start ();
12191 reg = tc_get_register (0);
12192 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12193 macro_end ();
12194
12195 demand_empty_rest_of_line ();
12196 }
12197
12198 /* Handle the .insn pseudo-op. This marks instruction labels in
12199 mips16 mode. This permits the linker to handle them specially,
12200 such as generating jalx instructions when needed. We also make
12201 them odd for the duration of the assembly, in order to generate the
12202 right sort of code. We will make them even in the adjust_symtab
12203 routine, while leaving them marked. This is convenient for the
12204 debugger and the disassembler. The linker knows to make them odd
12205 again. */
12206
12207 static void
12208 s_insn (int ignore ATTRIBUTE_UNUSED)
12209 {
12210 mips16_mark_labels ();
12211
12212 demand_empty_rest_of_line ();
12213 }
12214
12215 /* Handle a .stabn directive. We need these in order to mark a label
12216 as being a mips16 text label correctly. Sometimes the compiler
12217 will emit a label, followed by a .stabn, and then switch sections.
12218 If the label and .stabn are in mips16 mode, then the label is
12219 really a mips16 text label. */
12220
12221 static void
12222 s_mips_stab (int type)
12223 {
12224 if (type == 'n')
12225 mips16_mark_labels ();
12226
12227 s_stab (type);
12228 }
12229
12230 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12231 */
12232
12233 static void
12234 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12235 {
12236 char *name;
12237 int c;
12238 symbolS *symbolP;
12239 expressionS exp;
12240
12241 name = input_line_pointer;
12242 c = get_symbol_end ();
12243 symbolP = symbol_find_or_make (name);
12244 S_SET_WEAK (symbolP);
12245 *input_line_pointer = c;
12246
12247 SKIP_WHITESPACE ();
12248
12249 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12250 {
12251 if (S_IS_DEFINED (symbolP))
12252 {
12253 as_bad ("ignoring attempt to redefine symbol %s",
12254 S_GET_NAME (symbolP));
12255 ignore_rest_of_line ();
12256 return;
12257 }
12258
12259 if (*input_line_pointer == ',')
12260 {
12261 ++input_line_pointer;
12262 SKIP_WHITESPACE ();
12263 }
12264
12265 expression (&exp);
12266 if (exp.X_op != O_symbol)
12267 {
12268 as_bad ("bad .weakext directive");
12269 ignore_rest_of_line ();
12270 return;
12271 }
12272 symbol_set_value_expression (symbolP, &exp);
12273 }
12274
12275 demand_empty_rest_of_line ();
12276 }
12277
12278 /* Parse a register string into a number. Called from the ECOFF code
12279 to parse .frame. The argument is non-zero if this is the frame
12280 register, so that we can record it in mips_frame_reg. */
12281
12282 int
12283 tc_get_register (int frame)
12284 {
12285 int reg;
12286
12287 SKIP_WHITESPACE ();
12288 if (*input_line_pointer++ != '$')
12289 {
12290 as_warn (_("expected `$'"));
12291 reg = ZERO;
12292 }
12293 else if (ISDIGIT (*input_line_pointer))
12294 {
12295 reg = get_absolute_expression ();
12296 if (reg < 0 || reg >= 32)
12297 {
12298 as_warn (_("Bad register number"));
12299 reg = ZERO;
12300 }
12301 }
12302 else
12303 {
12304 if (strncmp (input_line_pointer, "ra", 2) == 0)
12305 {
12306 reg = RA;
12307 input_line_pointer += 2;
12308 }
12309 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12310 {
12311 reg = FP;
12312 input_line_pointer += 2;
12313 }
12314 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12315 {
12316 reg = SP;
12317 input_line_pointer += 2;
12318 }
12319 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12320 {
12321 reg = GP;
12322 input_line_pointer += 2;
12323 }
12324 else if (strncmp (input_line_pointer, "at", 2) == 0)
12325 {
12326 reg = AT;
12327 input_line_pointer += 2;
12328 }
12329 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12330 {
12331 reg = KT0;
12332 input_line_pointer += 3;
12333 }
12334 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12335 {
12336 reg = KT1;
12337 input_line_pointer += 3;
12338 }
12339 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12340 {
12341 reg = ZERO;
12342 input_line_pointer += 4;
12343 }
12344 else
12345 {
12346 as_warn (_("Unrecognized register name"));
12347 reg = ZERO;
12348 while (ISALNUM(*input_line_pointer))
12349 input_line_pointer++;
12350 }
12351 }
12352 if (frame)
12353 {
12354 mips_frame_reg = reg != 0 ? reg : SP;
12355 mips_frame_reg_valid = 1;
12356 mips_cprestore_valid = 0;
12357 }
12358 return reg;
12359 }
12360
12361 valueT
12362 md_section_align (asection *seg, valueT addr)
12363 {
12364 int align = bfd_get_section_alignment (stdoutput, seg);
12365
12366 #ifdef OBJ_ELF
12367 /* We don't need to align ELF sections to the full alignment.
12368 However, Irix 5 may prefer that we align them at least to a 16
12369 byte boundary. We don't bother to align the sections if we are
12370 targeted for an embedded system. */
12371 if (strcmp (TARGET_OS, "elf") == 0)
12372 return addr;
12373 if (align > 4)
12374 align = 4;
12375 #endif
12376
12377 return ((addr + (1 << align) - 1) & (-1 << align));
12378 }
12379
12380 /* Utility routine, called from above as well. If called while the
12381 input file is still being read, it's only an approximation. (For
12382 example, a symbol may later become defined which appeared to be
12383 undefined earlier.) */
12384
12385 static int
12386 nopic_need_relax (symbolS *sym, int before_relaxing)
12387 {
12388 if (sym == 0)
12389 return 0;
12390
12391 if (g_switch_value > 0)
12392 {
12393 const char *symname;
12394 int change;
12395
12396 /* Find out whether this symbol can be referenced off the $gp
12397 register. It can be if it is smaller than the -G size or if
12398 it is in the .sdata or .sbss section. Certain symbols can
12399 not be referenced off the $gp, although it appears as though
12400 they can. */
12401 symname = S_GET_NAME (sym);
12402 if (symname != (const char *) NULL
12403 && (strcmp (symname, "eprol") == 0
12404 || strcmp (symname, "etext") == 0
12405 || strcmp (symname, "_gp") == 0
12406 || strcmp (symname, "edata") == 0
12407 || strcmp (symname, "_fbss") == 0
12408 || strcmp (symname, "_fdata") == 0
12409 || strcmp (symname, "_ftext") == 0
12410 || strcmp (symname, "end") == 0
12411 || strcmp (symname, "_gp_disp") == 0))
12412 change = 1;
12413 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12414 && (0
12415 #ifndef NO_ECOFF_DEBUGGING
12416 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12417 && (symbol_get_obj (sym)->ecoff_extern_size
12418 <= g_switch_value))
12419 #endif
12420 /* We must defer this decision until after the whole
12421 file has been read, since there might be a .extern
12422 after the first use of this symbol. */
12423 || (before_relaxing
12424 #ifndef NO_ECOFF_DEBUGGING
12425 && symbol_get_obj (sym)->ecoff_extern_size == 0
12426 #endif
12427 && S_GET_VALUE (sym) == 0)
12428 || (S_GET_VALUE (sym) != 0
12429 && S_GET_VALUE (sym) <= g_switch_value)))
12430 change = 0;
12431 else
12432 {
12433 const char *segname;
12434
12435 segname = segment_name (S_GET_SEGMENT (sym));
12436 assert (strcmp (segname, ".lit8") != 0
12437 && strcmp (segname, ".lit4") != 0);
12438 change = (strcmp (segname, ".sdata") != 0
12439 && strcmp (segname, ".sbss") != 0
12440 && strncmp (segname, ".sdata.", 7) != 0
12441 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12442 }
12443 return change;
12444 }
12445 else
12446 /* We are not optimizing for the $gp register. */
12447 return 1;
12448 }
12449
12450
12451 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12452
12453 static bfd_boolean
12454 pic_need_relax (symbolS *sym, asection *segtype)
12455 {
12456 asection *symsec;
12457 bfd_boolean linkonce;
12458
12459 /* Handle the case of a symbol equated to another symbol. */
12460 while (symbol_equated_reloc_p (sym))
12461 {
12462 symbolS *n;
12463
12464 /* It's possible to get a loop here in a badly written
12465 program. */
12466 n = symbol_get_value_expression (sym)->X_add_symbol;
12467 if (n == sym)
12468 break;
12469 sym = n;
12470 }
12471
12472 symsec = S_GET_SEGMENT (sym);
12473
12474 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12475 linkonce = FALSE;
12476 if (symsec != segtype && ! S_IS_LOCAL (sym))
12477 {
12478 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12479 != 0)
12480 linkonce = TRUE;
12481
12482 /* The GNU toolchain uses an extension for ELF: a section
12483 beginning with the magic string .gnu.linkonce is a linkonce
12484 section. */
12485 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12486 sizeof ".gnu.linkonce" - 1) == 0)
12487 linkonce = TRUE;
12488 }
12489
12490 /* This must duplicate the test in adjust_reloc_syms. */
12491 return (symsec != &bfd_und_section
12492 && symsec != &bfd_abs_section
12493 && ! bfd_is_com_section (symsec)
12494 && !linkonce
12495 #ifdef OBJ_ELF
12496 /* A global or weak symbol is treated as external. */
12497 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12498 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12499 #endif
12500 );
12501 }
12502
12503
12504 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12505 extended opcode. SEC is the section the frag is in. */
12506
12507 static int
12508 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12509 {
12510 int type;
12511 register const struct mips16_immed_operand *op;
12512 offsetT val;
12513 int mintiny, maxtiny;
12514 segT symsec;
12515 fragS *sym_frag;
12516
12517 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12518 return 0;
12519 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12520 return 1;
12521
12522 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12523 op = mips16_immed_operands;
12524 while (op->type != type)
12525 {
12526 ++op;
12527 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12528 }
12529
12530 if (op->unsp)
12531 {
12532 if (type == '<' || type == '>' || type == '[' || type == ']')
12533 {
12534 mintiny = 1;
12535 maxtiny = 1 << op->nbits;
12536 }
12537 else
12538 {
12539 mintiny = 0;
12540 maxtiny = (1 << op->nbits) - 1;
12541 }
12542 }
12543 else
12544 {
12545 mintiny = - (1 << (op->nbits - 1));
12546 maxtiny = (1 << (op->nbits - 1)) - 1;
12547 }
12548
12549 sym_frag = symbol_get_frag (fragp->fr_symbol);
12550 val = S_GET_VALUE (fragp->fr_symbol);
12551 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12552
12553 if (op->pcrel)
12554 {
12555 addressT addr;
12556
12557 /* We won't have the section when we are called from
12558 mips_relax_frag. However, we will always have been called
12559 from md_estimate_size_before_relax first. If this is a
12560 branch to a different section, we mark it as such. If SEC is
12561 NULL, and the frag is not marked, then it must be a branch to
12562 the same section. */
12563 if (sec == NULL)
12564 {
12565 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12566 return 1;
12567 }
12568 else
12569 {
12570 /* Must have been called from md_estimate_size_before_relax. */
12571 if (symsec != sec)
12572 {
12573 fragp->fr_subtype =
12574 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12575
12576 /* FIXME: We should support this, and let the linker
12577 catch branches and loads that are out of range. */
12578 as_bad_where (fragp->fr_file, fragp->fr_line,
12579 _("unsupported PC relative reference to different section"));
12580
12581 return 1;
12582 }
12583 if (fragp != sym_frag && sym_frag->fr_address == 0)
12584 /* Assume non-extended on the first relaxation pass.
12585 The address we have calculated will be bogus if this is
12586 a forward branch to another frag, as the forward frag
12587 will have fr_address == 0. */
12588 return 0;
12589 }
12590
12591 /* In this case, we know for sure that the symbol fragment is in
12592 the same section. If the relax_marker of the symbol fragment
12593 differs from the relax_marker of this fragment, we have not
12594 yet adjusted the symbol fragment fr_address. We want to add
12595 in STRETCH in order to get a better estimate of the address.
12596 This particularly matters because of the shift bits. */
12597 if (stretch != 0
12598 && sym_frag->relax_marker != fragp->relax_marker)
12599 {
12600 fragS *f;
12601
12602 /* Adjust stretch for any alignment frag. Note that if have
12603 been expanding the earlier code, the symbol may be
12604 defined in what appears to be an earlier frag. FIXME:
12605 This doesn't handle the fr_subtype field, which specifies
12606 a maximum number of bytes to skip when doing an
12607 alignment. */
12608 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12609 {
12610 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12611 {
12612 if (stretch < 0)
12613 stretch = - ((- stretch)
12614 & ~ ((1 << (int) f->fr_offset) - 1));
12615 else
12616 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12617 if (stretch == 0)
12618 break;
12619 }
12620 }
12621 if (f != NULL)
12622 val += stretch;
12623 }
12624
12625 addr = fragp->fr_address + fragp->fr_fix;
12626
12627 /* The base address rules are complicated. The base address of
12628 a branch is the following instruction. The base address of a
12629 PC relative load or add is the instruction itself, but if it
12630 is in a delay slot (in which case it can not be extended) use
12631 the address of the instruction whose delay slot it is in. */
12632 if (type == 'p' || type == 'q')
12633 {
12634 addr += 2;
12635
12636 /* If we are currently assuming that this frag should be
12637 extended, then, the current address is two bytes
12638 higher. */
12639 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12640 addr += 2;
12641
12642 /* Ignore the low bit in the target, since it will be set
12643 for a text label. */
12644 if ((val & 1) != 0)
12645 --val;
12646 }
12647 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12648 addr -= 4;
12649 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12650 addr -= 2;
12651
12652 val -= addr & ~ ((1 << op->shift) - 1);
12653
12654 /* Branch offsets have an implicit 0 in the lowest bit. */
12655 if (type == 'p' || type == 'q')
12656 val /= 2;
12657
12658 /* If any of the shifted bits are set, we must use an extended
12659 opcode. If the address depends on the size of this
12660 instruction, this can lead to a loop, so we arrange to always
12661 use an extended opcode. We only check this when we are in
12662 the main relaxation loop, when SEC is NULL. */
12663 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12664 {
12665 fragp->fr_subtype =
12666 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12667 return 1;
12668 }
12669
12670 /* If we are about to mark a frag as extended because the value
12671 is precisely maxtiny + 1, then there is a chance of an
12672 infinite loop as in the following code:
12673 la $4,foo
12674 .skip 1020
12675 .align 2
12676 foo:
12677 In this case when the la is extended, foo is 0x3fc bytes
12678 away, so the la can be shrunk, but then foo is 0x400 away, so
12679 the la must be extended. To avoid this loop, we mark the
12680 frag as extended if it was small, and is about to become
12681 extended with a value of maxtiny + 1. */
12682 if (val == ((maxtiny + 1) << op->shift)
12683 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12684 && sec == NULL)
12685 {
12686 fragp->fr_subtype =
12687 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12688 return 1;
12689 }
12690 }
12691 else if (symsec != absolute_section && sec != NULL)
12692 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12693
12694 if ((val & ((1 << op->shift) - 1)) != 0
12695 || val < (mintiny << op->shift)
12696 || val > (maxtiny << op->shift))
12697 return 1;
12698 else
12699 return 0;
12700 }
12701
12702 /* Compute the length of a branch sequence, and adjust the
12703 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12704 worst-case length is computed, with UPDATE being used to indicate
12705 whether an unconditional (-1), branch-likely (+1) or regular (0)
12706 branch is to be computed. */
12707 static int
12708 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12709 {
12710 bfd_boolean toofar;
12711 int length;
12712
12713 if (fragp
12714 && S_IS_DEFINED (fragp->fr_symbol)
12715 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12716 {
12717 addressT addr;
12718 offsetT val;
12719
12720 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12721
12722 addr = fragp->fr_address + fragp->fr_fix + 4;
12723
12724 val -= addr;
12725
12726 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12727 }
12728 else if (fragp)
12729 /* If the symbol is not defined or it's in a different segment,
12730 assume the user knows what's going on and emit a short
12731 branch. */
12732 toofar = FALSE;
12733 else
12734 toofar = TRUE;
12735
12736 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12737 fragp->fr_subtype
12738 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12739 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12740 RELAX_BRANCH_LINK (fragp->fr_subtype),
12741 toofar);
12742
12743 length = 4;
12744 if (toofar)
12745 {
12746 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12747 length += 8;
12748
12749 if (mips_pic != NO_PIC)
12750 {
12751 /* Additional space for PIC loading of target address. */
12752 length += 8;
12753 if (mips_opts.isa == ISA_MIPS1)
12754 /* Additional space for $at-stabilizing nop. */
12755 length += 4;
12756 }
12757
12758 /* If branch is conditional. */
12759 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12760 length += 8;
12761 }
12762
12763 return length;
12764 }
12765
12766 /* Estimate the size of a frag before relaxing. Unless this is the
12767 mips16, we are not really relaxing here, and the final size is
12768 encoded in the subtype information. For the mips16, we have to
12769 decide whether we are using an extended opcode or not. */
12770
12771 int
12772 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12773 {
12774 int change;
12775
12776 if (RELAX_BRANCH_P (fragp->fr_subtype))
12777 {
12778
12779 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12780
12781 return fragp->fr_var;
12782 }
12783
12784 if (RELAX_MIPS16_P (fragp->fr_subtype))
12785 /* We don't want to modify the EXTENDED bit here; it might get us
12786 into infinite loops. We change it only in mips_relax_frag(). */
12787 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12788
12789 if (mips_pic == NO_PIC)
12790 change = nopic_need_relax (fragp->fr_symbol, 0);
12791 else if (mips_pic == SVR4_PIC)
12792 change = pic_need_relax (fragp->fr_symbol, segtype);
12793 else
12794 abort ();
12795
12796 if (change)
12797 {
12798 fragp->fr_subtype |= RELAX_USE_SECOND;
12799 return -RELAX_FIRST (fragp->fr_subtype);
12800 }
12801 else
12802 return -RELAX_SECOND (fragp->fr_subtype);
12803 }
12804
12805 /* This is called to see whether a reloc against a defined symbol
12806 should be converted into a reloc against a section. */
12807
12808 int
12809 mips_fix_adjustable (fixS *fixp)
12810 {
12811 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12812 about the format of the offset in the .o file. */
12813 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12814 return 0;
12815
12816 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12817 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12818 return 0;
12819
12820 if (fixp->fx_addsy == NULL)
12821 return 1;
12822
12823 /* If symbol SYM is in a mergeable section, relocations of the form
12824 SYM + 0 can usually be made section-relative. The mergeable data
12825 is then identified by the section offset rather than by the symbol.
12826
12827 However, if we're generating REL LO16 relocations, the offset is split
12828 between the LO16 and parterning high part relocation. The linker will
12829 need to recalculate the complete offset in order to correctly identify
12830 the merge data.
12831
12832 The linker has traditionally not looked for the parterning high part
12833 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12834 placed anywhere. Rather than break backwards compatibility by changing
12835 this, it seems better not to force the issue, and instead keep the
12836 original symbol. This will work with either linker behavior. */
12837 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12838 && HAVE_IN_PLACE_ADDENDS
12839 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12840 return 0;
12841
12842 #ifdef OBJ_ELF
12843 /* Don't adjust relocations against mips16 symbols, so that the linker
12844 can find them if it needs to set up a stub. */
12845 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12846 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12847 && fixp->fx_subsy == NULL)
12848 return 0;
12849 #endif
12850
12851 return 1;
12852 }
12853
12854 /* Translate internal representation of relocation info to BFD target
12855 format. */
12856
12857 arelent **
12858 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12859 {
12860 static arelent *retval[4];
12861 arelent *reloc;
12862 bfd_reloc_code_real_type code;
12863
12864 memset (retval, 0, sizeof(retval));
12865 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12866 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12867 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12868 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12869
12870 assert (! fixp->fx_pcrel);
12871 reloc->addend = fixp->fx_addnumber;
12872
12873 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12874 entry to be used in the relocation's section offset. */
12875 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12876 {
12877 reloc->address = reloc->addend;
12878 reloc->addend = 0;
12879 }
12880
12881 code = fixp->fx_r_type;
12882
12883 /* To support a PC relative reloc, we used a Cygnus extension.
12884 We check for that here to make sure that we don't let such a
12885 reloc escape normally. (FIXME: This was formerly used by
12886 embedded-PIC support, but is now used by branch handling in
12887 general. That probably should be fixed.) */
12888 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12889 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12890 && code == BFD_RELOC_16_PCREL_S2)
12891 reloc->howto = NULL;
12892 else
12893 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12894
12895 if (reloc->howto == NULL)
12896 {
12897 as_bad_where (fixp->fx_file, fixp->fx_line,
12898 _("Can not represent %s relocation in this object file format"),
12899 bfd_get_reloc_code_name (code));
12900 retval[0] = NULL;
12901 }
12902
12903 return retval;
12904 }
12905
12906 /* Relax a machine dependent frag. This returns the amount by which
12907 the current size of the frag should change. */
12908
12909 int
12910 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12911 {
12912 if (RELAX_BRANCH_P (fragp->fr_subtype))
12913 {
12914 offsetT old_var = fragp->fr_var;
12915
12916 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12917
12918 return fragp->fr_var - old_var;
12919 }
12920
12921 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12922 return 0;
12923
12924 if (mips16_extended_frag (fragp, NULL, stretch))
12925 {
12926 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12927 return 0;
12928 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12929 return 2;
12930 }
12931 else
12932 {
12933 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12934 return 0;
12935 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12936 return -2;
12937 }
12938
12939 return 0;
12940 }
12941
12942 /* Convert a machine dependent frag. */
12943
12944 void
12945 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12946 {
12947 if (RELAX_BRANCH_P (fragp->fr_subtype))
12948 {
12949 bfd_byte *buf;
12950 unsigned long insn;
12951 expressionS exp;
12952 fixS *fixp;
12953
12954 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12955
12956 if (target_big_endian)
12957 insn = bfd_getb32 (buf);
12958 else
12959 insn = bfd_getl32 (buf);
12960
12961 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12962 {
12963 /* We generate a fixup instead of applying it right now
12964 because, if there are linker relaxations, we're going to
12965 need the relocations. */
12966 exp.X_op = O_symbol;
12967 exp.X_add_symbol = fragp->fr_symbol;
12968 exp.X_add_number = fragp->fr_offset;
12969
12970 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12971 4, &exp, 1,
12972 BFD_RELOC_16_PCREL_S2);
12973 fixp->fx_file = fragp->fr_file;
12974 fixp->fx_line = fragp->fr_line;
12975
12976 md_number_to_chars (buf, insn, 4);
12977 buf += 4;
12978 }
12979 else
12980 {
12981 int i;
12982
12983 as_warn_where (fragp->fr_file, fragp->fr_line,
12984 _("relaxed out-of-range branch into a jump"));
12985
12986 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12987 goto uncond;
12988
12989 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12990 {
12991 /* Reverse the branch. */
12992 switch ((insn >> 28) & 0xf)
12993 {
12994 case 4:
12995 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12996 have the condition reversed by tweaking a single
12997 bit, and their opcodes all have 0x4???????. */
12998 assert ((insn & 0xf1000000) == 0x41000000);
12999 insn ^= 0x00010000;
13000 break;
13001
13002 case 0:
13003 /* bltz 0x04000000 bgez 0x04010000
13004 bltzal 0x04100000 bgezal 0x04110000 */
13005 assert ((insn & 0xfc0e0000) == 0x04000000);
13006 insn ^= 0x00010000;
13007 break;
13008
13009 case 1:
13010 /* beq 0x10000000 bne 0x14000000
13011 blez 0x18000000 bgtz 0x1c000000 */
13012 insn ^= 0x04000000;
13013 break;
13014
13015 default:
13016 abort ();
13017 }
13018 }
13019
13020 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13021 {
13022 /* Clear the and-link bit. */
13023 assert ((insn & 0xfc1c0000) == 0x04100000);
13024
13025 /* bltzal 0x04100000 bgezal 0x04110000
13026 bltzall 0x04120000 bgezall 0x04130000 */
13027 insn &= ~0x00100000;
13028 }
13029
13030 /* Branch over the branch (if the branch was likely) or the
13031 full jump (not likely case). Compute the offset from the
13032 current instruction to branch to. */
13033 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13034 i = 16;
13035 else
13036 {
13037 /* How many bytes in instructions we've already emitted? */
13038 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13039 /* How many bytes in instructions from here to the end? */
13040 i = fragp->fr_var - i;
13041 }
13042 /* Convert to instruction count. */
13043 i >>= 2;
13044 /* Branch counts from the next instruction. */
13045 i--;
13046 insn |= i;
13047 /* Branch over the jump. */
13048 md_number_to_chars (buf, insn, 4);
13049 buf += 4;
13050
13051 /* Nop */
13052 md_number_to_chars (buf, 0, 4);
13053 buf += 4;
13054
13055 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13056 {
13057 /* beql $0, $0, 2f */
13058 insn = 0x50000000;
13059 /* Compute the PC offset from the current instruction to
13060 the end of the variable frag. */
13061 /* How many bytes in instructions we've already emitted? */
13062 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13063 /* How many bytes in instructions from here to the end? */
13064 i = fragp->fr_var - i;
13065 /* Convert to instruction count. */
13066 i >>= 2;
13067 /* Don't decrement i, because we want to branch over the
13068 delay slot. */
13069
13070 insn |= i;
13071 md_number_to_chars (buf, insn, 4);
13072 buf += 4;
13073
13074 md_number_to_chars (buf, 0, 4);
13075 buf += 4;
13076 }
13077
13078 uncond:
13079 if (mips_pic == NO_PIC)
13080 {
13081 /* j or jal. */
13082 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13083 ? 0x0c000000 : 0x08000000);
13084 exp.X_op = O_symbol;
13085 exp.X_add_symbol = fragp->fr_symbol;
13086 exp.X_add_number = fragp->fr_offset;
13087
13088 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13089 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13090 fixp->fx_file = fragp->fr_file;
13091 fixp->fx_line = fragp->fr_line;
13092
13093 md_number_to_chars (buf, insn, 4);
13094 buf += 4;
13095 }
13096 else
13097 {
13098 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13099 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13100 exp.X_op = O_symbol;
13101 exp.X_add_symbol = fragp->fr_symbol;
13102 exp.X_add_number = fragp->fr_offset;
13103
13104 if (fragp->fr_offset)
13105 {
13106 exp.X_add_symbol = make_expr_symbol (&exp);
13107 exp.X_add_number = 0;
13108 }
13109
13110 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13111 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13112 fixp->fx_file = fragp->fr_file;
13113 fixp->fx_line = fragp->fr_line;
13114
13115 md_number_to_chars (buf, insn, 4);
13116 buf += 4;
13117
13118 if (mips_opts.isa == ISA_MIPS1)
13119 {
13120 /* nop */
13121 md_number_to_chars (buf, 0, 4);
13122 buf += 4;
13123 }
13124
13125 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13126 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13127
13128 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13129 4, &exp, 0, BFD_RELOC_LO16);
13130 fixp->fx_file = fragp->fr_file;
13131 fixp->fx_line = fragp->fr_line;
13132
13133 md_number_to_chars (buf, insn, 4);
13134 buf += 4;
13135
13136 /* j(al)r $at. */
13137 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13138 insn = 0x0020f809;
13139 else
13140 insn = 0x00200008;
13141
13142 md_number_to_chars (buf, insn, 4);
13143 buf += 4;
13144 }
13145 }
13146
13147 assert (buf == (bfd_byte *)fragp->fr_literal
13148 + fragp->fr_fix + fragp->fr_var);
13149
13150 fragp->fr_fix += fragp->fr_var;
13151
13152 return;
13153 }
13154
13155 if (RELAX_MIPS16_P (fragp->fr_subtype))
13156 {
13157 int type;
13158 register const struct mips16_immed_operand *op;
13159 bfd_boolean small, ext;
13160 offsetT val;
13161 bfd_byte *buf;
13162 unsigned long insn;
13163 bfd_boolean use_extend;
13164 unsigned short extend;
13165
13166 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13167 op = mips16_immed_operands;
13168 while (op->type != type)
13169 ++op;
13170
13171 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13172 {
13173 small = FALSE;
13174 ext = TRUE;
13175 }
13176 else
13177 {
13178 small = TRUE;
13179 ext = FALSE;
13180 }
13181
13182 resolve_symbol_value (fragp->fr_symbol);
13183 val = S_GET_VALUE (fragp->fr_symbol);
13184 if (op->pcrel)
13185 {
13186 addressT addr;
13187
13188 addr = fragp->fr_address + fragp->fr_fix;
13189
13190 /* The rules for the base address of a PC relative reloc are
13191 complicated; see mips16_extended_frag. */
13192 if (type == 'p' || type == 'q')
13193 {
13194 addr += 2;
13195 if (ext)
13196 addr += 2;
13197 /* Ignore the low bit in the target, since it will be
13198 set for a text label. */
13199 if ((val & 1) != 0)
13200 --val;
13201 }
13202 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13203 addr -= 4;
13204 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13205 addr -= 2;
13206
13207 addr &= ~ (addressT) ((1 << op->shift) - 1);
13208 val -= addr;
13209
13210 /* Make sure the section winds up with the alignment we have
13211 assumed. */
13212 if (op->shift > 0)
13213 record_alignment (asec, op->shift);
13214 }
13215
13216 if (ext
13217 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13218 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13219 as_warn_where (fragp->fr_file, fragp->fr_line,
13220 _("extended instruction in delay slot"));
13221
13222 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13223
13224 if (target_big_endian)
13225 insn = bfd_getb16 (buf);
13226 else
13227 insn = bfd_getl16 (buf);
13228
13229 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13230 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13231 small, ext, &insn, &use_extend, &extend);
13232
13233 if (use_extend)
13234 {
13235 md_number_to_chars (buf, 0xf000 | extend, 2);
13236 fragp->fr_fix += 2;
13237 buf += 2;
13238 }
13239
13240 md_number_to_chars (buf, insn, 2);
13241 fragp->fr_fix += 2;
13242 buf += 2;
13243 }
13244 else
13245 {
13246 int first, second;
13247 fixS *fixp;
13248
13249 first = RELAX_FIRST (fragp->fr_subtype);
13250 second = RELAX_SECOND (fragp->fr_subtype);
13251 fixp = (fixS *) fragp->fr_opcode;
13252
13253 /* Possibly emit a warning if we've chosen the longer option. */
13254 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13255 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13256 {
13257 const char *msg = macro_warning (fragp->fr_subtype);
13258 if (msg != 0)
13259 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13260 }
13261
13262 /* Go through all the fixups for the first sequence. Disable them
13263 (by marking them as done) if we're going to use the second
13264 sequence instead. */
13265 while (fixp
13266 && fixp->fx_frag == fragp
13267 && fixp->fx_where < fragp->fr_fix - second)
13268 {
13269 if (fragp->fr_subtype & RELAX_USE_SECOND)
13270 fixp->fx_done = 1;
13271 fixp = fixp->fx_next;
13272 }
13273
13274 /* Go through the fixups for the second sequence. Disable them if
13275 we're going to use the first sequence, otherwise adjust their
13276 addresses to account for the relaxation. */
13277 while (fixp && fixp->fx_frag == fragp)
13278 {
13279 if (fragp->fr_subtype & RELAX_USE_SECOND)
13280 fixp->fx_where -= first;
13281 else
13282 fixp->fx_done = 1;
13283 fixp = fixp->fx_next;
13284 }
13285
13286 /* Now modify the frag contents. */
13287 if (fragp->fr_subtype & RELAX_USE_SECOND)
13288 {
13289 char *start;
13290
13291 start = fragp->fr_literal + fragp->fr_fix - first - second;
13292 memmove (start, start + first, second);
13293 fragp->fr_fix -= first;
13294 }
13295 else
13296 fragp->fr_fix -= second;
13297 }
13298 }
13299
13300 #ifdef OBJ_ELF
13301
13302 /* This function is called after the relocs have been generated.
13303 We've been storing mips16 text labels as odd. Here we convert them
13304 back to even for the convenience of the debugger. */
13305
13306 void
13307 mips_frob_file_after_relocs (void)
13308 {
13309 asymbol **syms;
13310 unsigned int count, i;
13311
13312 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13313 return;
13314
13315 syms = bfd_get_outsymbols (stdoutput);
13316 count = bfd_get_symcount (stdoutput);
13317 for (i = 0; i < count; i++, syms++)
13318 {
13319 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13320 && ((*syms)->value & 1) != 0)
13321 {
13322 (*syms)->value &= ~1;
13323 /* If the symbol has an odd size, it was probably computed
13324 incorrectly, so adjust that as well. */
13325 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13326 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13327 }
13328 }
13329 }
13330
13331 #endif
13332
13333 /* This function is called whenever a label is defined. It is used
13334 when handling branch delays; if a branch has a label, we assume we
13335 can not move it. */
13336
13337 void
13338 mips_define_label (symbolS *sym)
13339 {
13340 struct insn_label_list *l;
13341
13342 if (free_insn_labels == NULL)
13343 l = (struct insn_label_list *) xmalloc (sizeof *l);
13344 else
13345 {
13346 l = free_insn_labels;
13347 free_insn_labels = l->next;
13348 }
13349
13350 l->label = sym;
13351 l->next = insn_labels;
13352 insn_labels = l;
13353 }
13354 \f
13355 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13356
13357 /* Some special processing for a MIPS ELF file. */
13358
13359 void
13360 mips_elf_final_processing (void)
13361 {
13362 /* Write out the register information. */
13363 if (mips_abi != N64_ABI)
13364 {
13365 Elf32_RegInfo s;
13366
13367 s.ri_gprmask = mips_gprmask;
13368 s.ri_cprmask[0] = mips_cprmask[0];
13369 s.ri_cprmask[1] = mips_cprmask[1];
13370 s.ri_cprmask[2] = mips_cprmask[2];
13371 s.ri_cprmask[3] = mips_cprmask[3];
13372 /* The gp_value field is set by the MIPS ELF backend. */
13373
13374 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13375 ((Elf32_External_RegInfo *)
13376 mips_regmask_frag));
13377 }
13378 else
13379 {
13380 Elf64_Internal_RegInfo s;
13381
13382 s.ri_gprmask = mips_gprmask;
13383 s.ri_pad = 0;
13384 s.ri_cprmask[0] = mips_cprmask[0];
13385 s.ri_cprmask[1] = mips_cprmask[1];
13386 s.ri_cprmask[2] = mips_cprmask[2];
13387 s.ri_cprmask[3] = mips_cprmask[3];
13388 /* The gp_value field is set by the MIPS ELF backend. */
13389
13390 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13391 ((Elf64_External_RegInfo *)
13392 mips_regmask_frag));
13393 }
13394
13395 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13396 sort of BFD interface for this. */
13397 if (mips_any_noreorder)
13398 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13399 if (mips_pic != NO_PIC)
13400 {
13401 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13402 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13403 }
13404 if (mips_abicalls)
13405 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13406
13407 /* Set MIPS ELF flags for ASEs. */
13408 if (file_ase_mips16)
13409 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13410 #if 0 /* XXX FIXME */
13411 if (file_ase_mips3d)
13412 elf_elfheader (stdoutput)->e_flags |= ???;
13413 #endif
13414 if (file_ase_mdmx)
13415 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13416
13417 /* Set the MIPS ELF ABI flags. */
13418 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13419 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13420 else if (mips_abi == O64_ABI)
13421 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13422 else if (mips_abi == EABI_ABI)
13423 {
13424 if (!file_mips_gp32)
13425 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13426 else
13427 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13428 }
13429 else if (mips_abi == N32_ABI)
13430 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13431
13432 /* Nothing to do for N64_ABI. */
13433
13434 if (mips_32bitmode)
13435 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13436 }
13437
13438 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13439 \f
13440 typedef struct proc {
13441 symbolS *isym;
13442 unsigned long reg_mask;
13443 unsigned long reg_offset;
13444 unsigned long fpreg_mask;
13445 unsigned long fpreg_offset;
13446 unsigned long frame_offset;
13447 unsigned long frame_reg;
13448 unsigned long pc_reg;
13449 } procS;
13450
13451 static procS cur_proc;
13452 static procS *cur_proc_ptr;
13453 static int numprocs;
13454
13455 /* Fill in an rs_align_code fragment. */
13456
13457 void
13458 mips_handle_align (fragS *fragp)
13459 {
13460 if (fragp->fr_type != rs_align_code)
13461 return;
13462
13463 if (mips_opts.mips16)
13464 {
13465 static const unsigned char be_nop[] = { 0x65, 0x00 };
13466 static const unsigned char le_nop[] = { 0x00, 0x65 };
13467
13468 int bytes;
13469 char *p;
13470
13471 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13472 p = fragp->fr_literal + fragp->fr_fix;
13473
13474 if (bytes & 1)
13475 {
13476 *p++ = 0;
13477 fragp->fr_fix++;
13478 }
13479
13480 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13481 fragp->fr_var = 2;
13482 }
13483
13484 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13485 }
13486
13487 static void
13488 md_obj_begin (void)
13489 {
13490 }
13491
13492 static void
13493 md_obj_end (void)
13494 {
13495 /* check for premature end, nesting errors, etc */
13496 if (cur_proc_ptr)
13497 as_warn (_("missing .end at end of assembly"));
13498 }
13499
13500 static long
13501 get_number (void)
13502 {
13503 int negative = 0;
13504 long val = 0;
13505
13506 if (*input_line_pointer == '-')
13507 {
13508 ++input_line_pointer;
13509 negative = 1;
13510 }
13511 if (!ISDIGIT (*input_line_pointer))
13512 as_bad (_("expected simple number"));
13513 if (input_line_pointer[0] == '0')
13514 {
13515 if (input_line_pointer[1] == 'x')
13516 {
13517 input_line_pointer += 2;
13518 while (ISXDIGIT (*input_line_pointer))
13519 {
13520 val <<= 4;
13521 val |= hex_value (*input_line_pointer++);
13522 }
13523 return negative ? -val : val;
13524 }
13525 else
13526 {
13527 ++input_line_pointer;
13528 while (ISDIGIT (*input_line_pointer))
13529 {
13530 val <<= 3;
13531 val |= *input_line_pointer++ - '0';
13532 }
13533 return negative ? -val : val;
13534 }
13535 }
13536 if (!ISDIGIT (*input_line_pointer))
13537 {
13538 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13539 *input_line_pointer, *input_line_pointer);
13540 as_warn (_("invalid number"));
13541 return -1;
13542 }
13543 while (ISDIGIT (*input_line_pointer))
13544 {
13545 val *= 10;
13546 val += *input_line_pointer++ - '0';
13547 }
13548 return negative ? -val : val;
13549 }
13550
13551 /* The .file directive; just like the usual .file directive, but there
13552 is an initial number which is the ECOFF file index. In the non-ECOFF
13553 case .file implies DWARF-2. */
13554
13555 static void
13556 s_mips_file (int x ATTRIBUTE_UNUSED)
13557 {
13558 static int first_file_directive = 0;
13559
13560 if (ECOFF_DEBUGGING)
13561 {
13562 get_number ();
13563 s_app_file (0);
13564 }
13565 else
13566 {
13567 char *filename;
13568
13569 filename = dwarf2_directive_file (0);
13570
13571 /* Versions of GCC up to 3.1 start files with a ".file"
13572 directive even for stabs output. Make sure that this
13573 ".file" is handled. Note that you need a version of GCC
13574 after 3.1 in order to support DWARF-2 on MIPS. */
13575 if (filename != NULL && ! first_file_directive)
13576 {
13577 (void) new_logical_line (filename, -1);
13578 s_app_file_string (filename, 0);
13579 }
13580 first_file_directive = 1;
13581 }
13582 }
13583
13584 /* The .loc directive, implying DWARF-2. */
13585
13586 static void
13587 s_mips_loc (int x ATTRIBUTE_UNUSED)
13588 {
13589 if (!ECOFF_DEBUGGING)
13590 dwarf2_directive_loc (0);
13591 }
13592
13593 /* The .end directive. */
13594
13595 static void
13596 s_mips_end (int x ATTRIBUTE_UNUSED)
13597 {
13598 symbolS *p;
13599
13600 /* Following functions need their own .frame and .cprestore directives. */
13601 mips_frame_reg_valid = 0;
13602 mips_cprestore_valid = 0;
13603
13604 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13605 {
13606 p = get_symbol ();
13607 demand_empty_rest_of_line ();
13608 }
13609 else
13610 p = NULL;
13611
13612 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13613 as_warn (_(".end not in text section"));
13614
13615 if (!cur_proc_ptr)
13616 {
13617 as_warn (_(".end directive without a preceding .ent directive."));
13618 demand_empty_rest_of_line ();
13619 return;
13620 }
13621
13622 if (p != NULL)
13623 {
13624 assert (S_GET_NAME (p));
13625 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13626 as_warn (_(".end symbol does not match .ent symbol."));
13627
13628 if (debug_type == DEBUG_STABS)
13629 stabs_generate_asm_endfunc (S_GET_NAME (p),
13630 S_GET_NAME (p));
13631 }
13632 else
13633 as_warn (_(".end directive missing or unknown symbol"));
13634
13635 #ifdef OBJ_ELF
13636 /* Generate a .pdr section. */
13637 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13638 && mips_flag_pdr)
13639 {
13640 segT saved_seg = now_seg;
13641 subsegT saved_subseg = now_subseg;
13642 valueT dot;
13643 expressionS exp;
13644 char *fragp;
13645
13646 dot = frag_now_fix ();
13647
13648 #ifdef md_flush_pending_output
13649 md_flush_pending_output ();
13650 #endif
13651
13652 assert (pdr_seg);
13653 subseg_set (pdr_seg, 0);
13654
13655 /* Write the symbol. */
13656 exp.X_op = O_symbol;
13657 exp.X_add_symbol = p;
13658 exp.X_add_number = 0;
13659 emit_expr (&exp, 4);
13660
13661 fragp = frag_more (7 * 4);
13662
13663 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13664 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13665 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13666 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13667 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13668 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13669 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13670
13671 subseg_set (saved_seg, saved_subseg);
13672 }
13673 #endif /* OBJ_ELF */
13674
13675 cur_proc_ptr = NULL;
13676 }
13677
13678 /* The .aent and .ent directives. */
13679
13680 static void
13681 s_mips_ent (int aent)
13682 {
13683 symbolS *symbolP;
13684
13685 symbolP = get_symbol ();
13686 if (*input_line_pointer == ',')
13687 ++input_line_pointer;
13688 SKIP_WHITESPACE ();
13689 if (ISDIGIT (*input_line_pointer)
13690 || *input_line_pointer == '-')
13691 get_number ();
13692
13693 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13694 as_warn (_(".ent or .aent not in text section."));
13695
13696 if (!aent && cur_proc_ptr)
13697 as_warn (_("missing .end"));
13698
13699 if (!aent)
13700 {
13701 /* This function needs its own .frame and .cprestore directives. */
13702 mips_frame_reg_valid = 0;
13703 mips_cprestore_valid = 0;
13704
13705 cur_proc_ptr = &cur_proc;
13706 memset (cur_proc_ptr, '\0', sizeof (procS));
13707
13708 cur_proc_ptr->isym = symbolP;
13709
13710 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13711
13712 ++numprocs;
13713
13714 if (debug_type == DEBUG_STABS)
13715 stabs_generate_asm_func (S_GET_NAME (symbolP),
13716 S_GET_NAME (symbolP));
13717 }
13718
13719 demand_empty_rest_of_line ();
13720 }
13721
13722 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13723 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13724 s_mips_frame is used so that we can set the PDR information correctly.
13725 We can't use the ecoff routines because they make reference to the ecoff
13726 symbol table (in the mdebug section). */
13727
13728 static void
13729 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13730 {
13731 #ifdef OBJ_ELF
13732 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13733 {
13734 long val;
13735
13736 if (cur_proc_ptr == (procS *) NULL)
13737 {
13738 as_warn (_(".frame outside of .ent"));
13739 demand_empty_rest_of_line ();
13740 return;
13741 }
13742
13743 cur_proc_ptr->frame_reg = tc_get_register (1);
13744
13745 SKIP_WHITESPACE ();
13746 if (*input_line_pointer++ != ','
13747 || get_absolute_expression_and_terminator (&val) != ',')
13748 {
13749 as_warn (_("Bad .frame directive"));
13750 --input_line_pointer;
13751 demand_empty_rest_of_line ();
13752 return;
13753 }
13754
13755 cur_proc_ptr->frame_offset = val;
13756 cur_proc_ptr->pc_reg = tc_get_register (0);
13757
13758 demand_empty_rest_of_line ();
13759 }
13760 else
13761 #endif /* OBJ_ELF */
13762 s_ignore (ignore);
13763 }
13764
13765 /* The .fmask and .mask directives. If the mdebug section is present
13766 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13767 embedded targets, s_mips_mask is used so that we can set the PDR
13768 information correctly. We can't use the ecoff routines because they
13769 make reference to the ecoff symbol table (in the mdebug section). */
13770
13771 static void
13772 s_mips_mask (int reg_type)
13773 {
13774 #ifdef OBJ_ELF
13775 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13776 {
13777 long mask, off;
13778
13779 if (cur_proc_ptr == (procS *) NULL)
13780 {
13781 as_warn (_(".mask/.fmask outside of .ent"));
13782 demand_empty_rest_of_line ();
13783 return;
13784 }
13785
13786 if (get_absolute_expression_and_terminator (&mask) != ',')
13787 {
13788 as_warn (_("Bad .mask/.fmask directive"));
13789 --input_line_pointer;
13790 demand_empty_rest_of_line ();
13791 return;
13792 }
13793
13794 off = get_absolute_expression ();
13795
13796 if (reg_type == 'F')
13797 {
13798 cur_proc_ptr->fpreg_mask = mask;
13799 cur_proc_ptr->fpreg_offset = off;
13800 }
13801 else
13802 {
13803 cur_proc_ptr->reg_mask = mask;
13804 cur_proc_ptr->reg_offset = off;
13805 }
13806
13807 demand_empty_rest_of_line ();
13808 }
13809 else
13810 #endif /* OBJ_ELF */
13811 s_ignore (reg_type);
13812 }
13813
13814 /* The .loc directive. */
13815
13816 #if 0
13817 static void
13818 s_loc (int x)
13819 {
13820 symbolS *symbolP;
13821 int lineno;
13822 int addroff;
13823
13824 assert (now_seg == text_section);
13825
13826 lineno = get_number ();
13827 addroff = frag_now_fix ();
13828
13829 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13830 S_SET_TYPE (symbolP, N_SLINE);
13831 S_SET_OTHER (symbolP, 0);
13832 S_SET_DESC (symbolP, lineno);
13833 symbolP->sy_segment = now_seg;
13834 }
13835 #endif
13836
13837 /* A table describing all the processors gas knows about. Names are
13838 matched in the order listed.
13839
13840 To ease comparison, please keep this table in the same order as
13841 gcc's mips_cpu_info_table[]. */
13842 static const struct mips_cpu_info mips_cpu_info_table[] =
13843 {
13844 /* Entries for generic ISAs */
13845 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13846 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13847 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13848 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13849 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13850 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13851 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13852 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13853 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13854
13855 /* MIPS I */
13856 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13857 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13858 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13859
13860 /* MIPS II */
13861 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13862
13863 /* MIPS III */
13864 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13865 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13866 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13867 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13868 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13869 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13870 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13871 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13872 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13873 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13874 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13875 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13876
13877 /* MIPS IV */
13878 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13879 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13880 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13881 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13882 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13883 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13884 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13885 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13886 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13887 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13888 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13889 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13890 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13891
13892 /* MIPS 32 */
13893 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13894 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13895 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13896
13897 /* MIPS 64 */
13898 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13899 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13900
13901 /* Broadcom SB-1 CPU core */
13902 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13903
13904 /* End marker */
13905 { NULL, 0, 0, 0 }
13906 };
13907
13908
13909 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13910 with a final "000" replaced by "k". Ignore case.
13911
13912 Note: this function is shared between GCC and GAS. */
13913
13914 static bfd_boolean
13915 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13916 {
13917 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13918 given++, canonical++;
13919
13920 return ((*given == 0 && *canonical == 0)
13921 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13922 }
13923
13924
13925 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13926 CPU name. We've traditionally allowed a lot of variation here.
13927
13928 Note: this function is shared between GCC and GAS. */
13929
13930 static bfd_boolean
13931 mips_matching_cpu_name_p (const char *canonical, const char *given)
13932 {
13933 /* First see if the name matches exactly, or with a final "000"
13934 turned into "k". */
13935 if (mips_strict_matching_cpu_name_p (canonical, given))
13936 return TRUE;
13937
13938 /* If not, try comparing based on numerical designation alone.
13939 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13940 if (TOLOWER (*given) == 'r')
13941 given++;
13942 if (!ISDIGIT (*given))
13943 return FALSE;
13944
13945 /* Skip over some well-known prefixes in the canonical name,
13946 hoping to find a number there too. */
13947 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13948 canonical += 2;
13949 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13950 canonical += 2;
13951 else if (TOLOWER (canonical[0]) == 'r')
13952 canonical += 1;
13953
13954 return mips_strict_matching_cpu_name_p (canonical, given);
13955 }
13956
13957
13958 /* Parse an option that takes the name of a processor as its argument.
13959 OPTION is the name of the option and CPU_STRING is the argument.
13960 Return the corresponding processor enumeration if the CPU_STRING is
13961 recognized, otherwise report an error and return null.
13962
13963 A similar function exists in GCC. */
13964
13965 static const struct mips_cpu_info *
13966 mips_parse_cpu (const char *option, const char *cpu_string)
13967 {
13968 const struct mips_cpu_info *p;
13969
13970 /* 'from-abi' selects the most compatible architecture for the given
13971 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13972 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13973 version. Look first at the -mgp options, if given, otherwise base
13974 the choice on MIPS_DEFAULT_64BIT.
13975
13976 Treat NO_ABI like the EABIs. One reason to do this is that the
13977 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13978 architecture. This code picks MIPS I for 'mips' and MIPS III for
13979 'mips64', just as we did in the days before 'from-abi'. */
13980 if (strcasecmp (cpu_string, "from-abi") == 0)
13981 {
13982 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13983 return mips_cpu_info_from_isa (ISA_MIPS1);
13984
13985 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13986 return mips_cpu_info_from_isa (ISA_MIPS3);
13987
13988 if (file_mips_gp32 >= 0)
13989 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13990
13991 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13992 ? ISA_MIPS3
13993 : ISA_MIPS1);
13994 }
13995
13996 /* 'default' has traditionally been a no-op. Probably not very useful. */
13997 if (strcasecmp (cpu_string, "default") == 0)
13998 return 0;
13999
14000 for (p = mips_cpu_info_table; p->name != 0; p++)
14001 if (mips_matching_cpu_name_p (p->name, cpu_string))
14002 return p;
14003
14004 as_bad ("Bad value (%s) for %s", cpu_string, option);
14005 return 0;
14006 }
14007
14008 /* Return the canonical processor information for ISA (a member of the
14009 ISA_MIPS* enumeration). */
14010
14011 static const struct mips_cpu_info *
14012 mips_cpu_info_from_isa (int isa)
14013 {
14014 int i;
14015
14016 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14017 if (mips_cpu_info_table[i].is_isa
14018 && isa == mips_cpu_info_table[i].isa)
14019 return (&mips_cpu_info_table[i]);
14020
14021 return NULL;
14022 }
14023
14024 static const struct mips_cpu_info *
14025 mips_cpu_info_from_arch (int arch)
14026 {
14027 int i;
14028
14029 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14030 if (arch == mips_cpu_info_table[i].cpu)
14031 return (&mips_cpu_info_table[i]);
14032
14033 return NULL;
14034 }
14035 \f
14036 static void
14037 show (FILE *stream, const char *string, int *col_p, int *first_p)
14038 {
14039 if (*first_p)
14040 {
14041 fprintf (stream, "%24s", "");
14042 *col_p = 24;
14043 }
14044 else
14045 {
14046 fprintf (stream, ", ");
14047 *col_p += 2;
14048 }
14049
14050 if (*col_p + strlen (string) > 72)
14051 {
14052 fprintf (stream, "\n%24s", "");
14053 *col_p = 24;
14054 }
14055
14056 fprintf (stream, "%s", string);
14057 *col_p += strlen (string);
14058
14059 *first_p = 0;
14060 }
14061
14062 void
14063 md_show_usage (FILE *stream)
14064 {
14065 int column, first;
14066 size_t i;
14067
14068 fprintf (stream, _("\
14069 MIPS options:\n\
14070 -EB generate big endian output\n\
14071 -EL generate little endian output\n\
14072 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14073 -G NUM allow referencing objects up to NUM bytes\n\
14074 implicitly with the gp register [default 8]\n"));
14075 fprintf (stream, _("\
14076 -mips1 generate MIPS ISA I instructions\n\
14077 -mips2 generate MIPS ISA II instructions\n\
14078 -mips3 generate MIPS ISA III instructions\n\
14079 -mips4 generate MIPS ISA IV instructions\n\
14080 -mips5 generate MIPS ISA V instructions\n\
14081 -mips32 generate MIPS32 ISA instructions\n\
14082 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14083 -mips64 generate MIPS64 ISA instructions\n\
14084 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14085 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14086
14087 first = 1;
14088
14089 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14090 show (stream, mips_cpu_info_table[i].name, &column, &first);
14091 show (stream, "from-abi", &column, &first);
14092 fputc ('\n', stream);
14093
14094 fprintf (stream, _("\
14095 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14096 -no-mCPU don't generate code specific to CPU.\n\
14097 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14098
14099 first = 1;
14100
14101 show (stream, "3900", &column, &first);
14102 show (stream, "4010", &column, &first);
14103 show (stream, "4100", &column, &first);
14104 show (stream, "4650", &column, &first);
14105 fputc ('\n', stream);
14106
14107 fprintf (stream, _("\
14108 -mips16 generate mips16 instructions\n\
14109 -no-mips16 do not generate mips16 instructions\n"));
14110 fprintf (stream, _("\
14111 -mfix-vr4120 work around certain VR4120 errata\n\
14112 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14113 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14114 -O0 remove unneeded NOPs, do not swap branches\n\
14115 -O remove unneeded NOPs and swap branches\n\
14116 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14117 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14118 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14119 #ifdef OBJ_ELF
14120 fprintf (stream, _("\
14121 -KPIC, -call_shared generate SVR4 position independent code\n\
14122 -non_shared do not generate position independent code\n\
14123 -xgot assume a 32 bit GOT\n\
14124 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14125 -mabi=ABI create ABI conformant object file for:\n"));
14126
14127 first = 1;
14128
14129 show (stream, "32", &column, &first);
14130 show (stream, "o64", &column, &first);
14131 show (stream, "n32", &column, &first);
14132 show (stream, "64", &column, &first);
14133 show (stream, "eabi", &column, &first);
14134
14135 fputc ('\n', stream);
14136
14137 fprintf (stream, _("\
14138 -32 create o32 ABI object file (default)\n\
14139 -n32 create n32 ABI object file\n\
14140 -64 create 64 ABI object file\n"));
14141 #endif
14142 }
14143
14144 enum dwarf2_format
14145 mips_dwarf2_format (void)
14146 {
14147 if (mips_abi == N64_ABI)
14148 {
14149 #ifdef TE_IRIX
14150 return dwarf2_format_64bit_irix;
14151 #else
14152 return dwarf2_format_64bit;
14153 #endif
14154 }
14155 else
14156 return dwarf2_format_32bit;
14157 }
14158
14159 int
14160 mips_dwarf2_addr_size (void)
14161 {
14162 if (mips_abi == N64_ABI)
14163 return 8;
14164 else
14165 return 4;
14166 }