2003-06-30 Chris Demetriou <cgd@broadcom.com>
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 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 #include "ecoff.h"
78
79 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
80 static char *mips_regmask_frag;
81 #endif
82
83 #define ZERO 0
84 #define AT 1
85 #define TREG 24
86 #define PIC_CALL_REG 25
87 #define KT0 26
88 #define KT1 27
89 #define GP 28
90 #define SP 29
91 #define FP 30
92 #define RA 31
93
94 #define ILLEGAL_REG (32)
95
96 /* Allow override of standard little-endian ECOFF format. */
97
98 #ifndef ECOFF_LITTLE_FORMAT
99 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
100 #endif
101
102 extern int target_big_endian;
103
104 /* The name of the readonly data section. */
105 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
106 ? ".data" \
107 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
108 ? ".rdata" \
109 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
110 ? ".rdata" \
111 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
112 ? ".rodata" \
113 : (abort (), ""))
114
115 /* The ABI to use. */
116 enum mips_abi_level
117 {
118 NO_ABI = 0,
119 O32_ABI,
120 O64_ABI,
121 N32_ABI,
122 N64_ABI,
123 EABI_ABI
124 };
125
126 /* MIPS ABI we are using for this output file. */
127 static enum mips_abi_level mips_abi = NO_ABI;
128
129 /* Whether or not we have code that can call pic code. */
130 int mips_abicalls = FALSE;
131
132 /* This is the set of options which may be modified by the .set
133 pseudo-op. We use a struct so that .set push and .set pop are more
134 reliable. */
135
136 struct mips_set_options
137 {
138 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
139 if it has not been initialized. Changed by `.set mipsN', and the
140 -mipsN command line option, and the default CPU. */
141 int isa;
142 /* Enabled Application Specific Extensions (ASEs). These are set to -1
143 if they have not been initialized. Changed by `.set <asename>', by
144 command line options, and based on the default architecture. */
145 int ase_mips3d;
146 int ase_mdmx;
147 /* Whether we are assembling for the mips16 processor. 0 if we are
148 not, 1 if we are, and -1 if the value has not been initialized.
149 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
150 -nomips16 command line options, and the default CPU. */
151 int mips16;
152 /* Non-zero if we should not reorder instructions. Changed by `.set
153 reorder' and `.set noreorder'. */
154 int noreorder;
155 /* Non-zero if we should not permit the $at ($1) register to be used
156 in instructions. Changed by `.set at' and `.set noat'. */
157 int noat;
158 /* Non-zero if we should warn when a macro instruction expands into
159 more than one machine instruction. Changed by `.set nomacro' and
160 `.set macro'. */
161 int warn_about_macros;
162 /* Non-zero if we should not move instructions. Changed by `.set
163 move', `.set volatile', `.set nomove', and `.set novolatile'. */
164 int nomove;
165 /* Non-zero if we should not optimize branches by moving the target
166 of the branch into the delay slot. Actually, we don't perform
167 this optimization anyhow. Changed by `.set bopt' and `.set
168 nobopt'. */
169 int nobopt;
170 /* Non-zero if we should not autoextend mips16 instructions.
171 Changed by `.set autoextend' and `.set noautoextend'. */
172 int noautoextend;
173 /* Restrict general purpose registers and floating point registers
174 to 32 bit. This is initially determined when -mgp32 or -mfp32
175 is passed but can changed if the assembler code uses .set mipsN. */
176 int gp32;
177 int fp32;
178 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
179 command line option, and the default CPU. */
180 int arch;
181 };
182
183 /* True if -mgp32 was passed. */
184 static int file_mips_gp32 = -1;
185
186 /* True if -mfp32 was passed. */
187 static int file_mips_fp32 = -1;
188
189 /* This is the struct we use to hold the current set of options. Note
190 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
191 -1 to indicate that they have not been initialized. */
192
193 static struct mips_set_options mips_opts =
194 {
195 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
196 };
197
198 /* These variables are filled in with the masks of registers used.
199 The object format code reads them and puts them in the appropriate
200 place. */
201 unsigned long mips_gprmask;
202 unsigned long mips_cprmask[4];
203
204 /* MIPS ISA we are using for this output file. */
205 static int file_mips_isa = ISA_UNKNOWN;
206
207 /* True if -mips16 was passed or implied by arguments passed on the
208 command line (e.g., by -march). */
209 static int file_ase_mips16;
210
211 /* True if -mips3d was passed or implied by arguments passed on the
212 command line (e.g., by -march). */
213 static int file_ase_mips3d;
214
215 /* True if -mdmx was passed or implied by arguments passed on the
216 command line (e.g., by -march). */
217 static int file_ase_mdmx;
218
219 /* The argument of the -march= flag. The architecture we are assembling. */
220 static int file_mips_arch = CPU_UNKNOWN;
221 static const char *mips_arch_string;
222
223 /* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225 static int mips_tune = CPU_UNKNOWN;
226 static const char *mips_tune_string;
227
228 /* True when generating 32-bit code for a 64-bit processor. */
229 static int mips_32bitmode = 0;
230
231 /* Some ISA's have delay slots for instructions which read or write
232 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
233 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
234 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
235 delay slot in this ISA. The uses of this macro assume that any
236 ISA that has delay slots for one of these, has them for all. They
237 also assume that ISAs which don't have delays for these insns, don't
238 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
239 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
240 (ISA) == ISA_MIPS1 \
241 || (ISA) == ISA_MIPS2 \
242 || (ISA) == ISA_MIPS3 \
243 )
244
245 /* True if the given ABI requires 32-bit registers. */
246 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
247
248 /* Likewise 64-bit registers. */
249 #define ABI_NEEDS_64BIT_REGS(ABI) \
250 ((ABI) == N32_ABI \
251 || (ABI) == N64_ABI \
252 || (ABI) == O64_ABI)
253
254 /* Return true if ISA supports 64 bit gp register instructions. */
255 #define ISA_HAS_64BIT_REGS(ISA) ( \
256 (ISA) == ISA_MIPS3 \
257 || (ISA) == ISA_MIPS4 \
258 || (ISA) == ISA_MIPS5 \
259 || (ISA) == ISA_MIPS64 \
260 )
261
262 /* Return true if ISA supports 64-bit right rotate (dror et al.)
263 instructions. */
264 #define ISA_HAS_DROR(ISA) ( \
265 0 \
266 )
267
268 /* Return true if ISA supports 32-bit right rotate (ror et al.)
269 instructions. */
270 #define ISA_HAS_ROR(ISA) ( \
271 (ISA) == ISA_MIPS32R2 \
272 )
273
274 #define HAVE_32BIT_GPRS \
275 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
276
277 #define HAVE_32BIT_FPRS \
278 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
279
280 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
281 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
282
283 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
284
285 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
286
287 /* We can only have 64bit addresses if the object file format
288 supports it. */
289 #define HAVE_32BIT_ADDRESSES \
290 (HAVE_32BIT_GPRS \
291 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
292 || ! HAVE_64BIT_OBJECTS) \
293 && mips_pic != EMBEDDED_PIC))
294
295 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
296 #define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
297 || HAVE_64BIT_GPRS)
298
299 /* Addresses are loaded in different ways, depending on the address size
300 in use. The n32 ABI Documentation also mandates the use of additions
301 with overflow checking, but existing implementations don't follow it. */
302 #define ADDRESS_ADD_INSN \
303 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
304
305 #define ADDRESS_ADDI_INSN \
306 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
307
308 #define ADDRESS_LOAD_INSN \
309 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
310
311 #define ADDRESS_STORE_INSN \
312 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
313
314 /* Return true if the given CPU supports the MIPS16 ASE. */
315 #define CPU_HAS_MIPS16(cpu) \
316 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
317 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
318
319 /* Return true if the given CPU supports the MIPS3D ASE. */
320 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
321 )
322
323 /* Return true if the given CPU supports the MDMX ASE. */
324 #define CPU_HAS_MDMX(cpu) (FALSE \
325 )
326
327 /* True if CPU has a dror instruction. */
328 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
329
330 /* True if CPU has a ror instruction. */
331 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
332
333 /* Whether the processor uses hardware interlocks to protect
334 reads from the HI and LO registers, and thus does not
335 require nops to be inserted. */
336
337 #define hilo_interlocks (mips_opts.arch == CPU_R4010 \
338 || mips_opts.arch == CPU_VR5500 \
339 || mips_opts.arch == CPU_SB1 \
340 )
341
342 /* Whether the processor uses hardware interlocks to protect reads
343 from the GPRs, and thus does not require nops to be inserted. */
344 #define gpr_interlocks \
345 (mips_opts.isa != ISA_MIPS1 \
346 || mips_opts.arch == CPU_VR5400 \
347 || mips_opts.arch == CPU_VR5500 \
348 || mips_opts.arch == CPU_R3900)
349
350 /* As with other "interlocks" this is used by hardware that has FP
351 (co-processor) interlocks. */
352 /* Itbl support may require additional care here. */
353 #define cop_interlocks (mips_opts.arch == CPU_R4300 \
354 || mips_opts.arch == CPU_VR5400 \
355 || mips_opts.arch == CPU_VR5500 \
356 || mips_opts.arch == CPU_SB1 \
357 )
358
359 /* Is this a mfhi or mflo instruction? */
360 #define MF_HILO_INSN(PINFO) \
361 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
362
363 /* MIPS PIC level. */
364
365 enum mips_pic_level mips_pic;
366
367 /* Warn about all NOPS that the assembler generates. */
368 static int warn_nops = 0;
369
370 /* 1 if we should generate 32 bit offsets from the $gp register in
371 SVR4_PIC mode. Currently has no meaning in other modes. */
372 static int mips_big_got = 0;
373
374 /* 1 if trap instructions should used for overflow rather than break
375 instructions. */
376 static int mips_trap = 0;
377
378 /* 1 if double width floating point constants should not be constructed
379 by assembling two single width halves into two single width floating
380 point registers which just happen to alias the double width destination
381 register. On some architectures this aliasing can be disabled by a bit
382 in the status register, and the setting of this bit cannot be determined
383 automatically at assemble time. */
384 static int mips_disable_float_construction;
385
386 /* Non-zero if any .set noreorder directives were used. */
387
388 static int mips_any_noreorder;
389
390 /* Non-zero if nops should be inserted when the register referenced in
391 an mfhi/mflo instruction is read in the next two instructions. */
392 static int mips_7000_hilo_fix;
393
394 /* The size of the small data section. */
395 static unsigned int g_switch_value = 8;
396 /* Whether the -G option was used. */
397 static int g_switch_seen = 0;
398
399 #define N_RMASK 0xc4
400 #define N_VFP 0xd4
401
402 /* If we can determine in advance that GP optimization won't be
403 possible, we can skip the relaxation stuff that tries to produce
404 GP-relative references. This makes delay slot optimization work
405 better.
406
407 This function can only provide a guess, but it seems to work for
408 gcc output. It needs to guess right for gcc, otherwise gcc
409 will put what it thinks is a GP-relative instruction in a branch
410 delay slot.
411
412 I don't know if a fix is needed for the SVR4_PIC mode. I've only
413 fixed it for the non-PIC mode. KR 95/04/07 */
414 static int nopic_need_relax (symbolS *, int);
415
416 /* handle of the OPCODE hash table */
417 static struct hash_control *op_hash = NULL;
418
419 /* The opcode hash table we use for the mips16. */
420 static struct hash_control *mips16_op_hash = NULL;
421
422 /* This array holds the chars that always start a comment. If the
423 pre-processor is disabled, these aren't very useful */
424 const char comment_chars[] = "#";
425
426 /* This array holds the chars that only start a comment at the beginning of
427 a line. If the line seems to have the form '# 123 filename'
428 .line and .file directives will appear in the pre-processed output */
429 /* Note that input_file.c hand checks for '#' at the beginning of the
430 first line of the input file. This is because the compiler outputs
431 #NO_APP at the beginning of its output. */
432 /* Also note that C style comments are always supported. */
433 const char line_comment_chars[] = "#";
434
435 /* This array holds machine specific line separator characters. */
436 const char line_separator_chars[] = ";";
437
438 /* Chars that can be used to separate mant from exp in floating point nums */
439 const char EXP_CHARS[] = "eE";
440
441 /* Chars that mean this number is a floating point constant */
442 /* As in 0f12.456 */
443 /* or 0d1.2345e12 */
444 const char FLT_CHARS[] = "rRsSfFdDxXpP";
445
446 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
447 changed in read.c . Ideally it shouldn't have to know about it at all,
448 but nothing is ideal around here.
449 */
450
451 static char *insn_error;
452
453 static int auto_align = 1;
454
455 /* When outputting SVR4 PIC code, the assembler needs to know the
456 offset in the stack frame from which to restore the $gp register.
457 This is set by the .cprestore pseudo-op, and saved in this
458 variable. */
459 static offsetT mips_cprestore_offset = -1;
460
461 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
462 more optimizations, it can use a register value instead of a memory-saved
463 offset and even an other register than $gp as global pointer. */
464 static offsetT mips_cpreturn_offset = -1;
465 static int mips_cpreturn_register = -1;
466 static int mips_gp_register = GP;
467 static int mips_gprel_offset = 0;
468
469 /* Whether mips_cprestore_offset has been set in the current function
470 (or whether it has already been warned about, if not). */
471 static int mips_cprestore_valid = 0;
472
473 /* This is the register which holds the stack frame, as set by the
474 .frame pseudo-op. This is needed to implement .cprestore. */
475 static int mips_frame_reg = SP;
476
477 /* Whether mips_frame_reg has been set in the current function
478 (or whether it has already been warned about, if not). */
479 static int mips_frame_reg_valid = 0;
480
481 /* To output NOP instructions correctly, we need to keep information
482 about the previous two instructions. */
483
484 /* Whether we are optimizing. The default value of 2 means to remove
485 unneeded NOPs and swap branch instructions when possible. A value
486 of 1 means to not swap branches. A value of 0 means to always
487 insert NOPs. */
488 static int mips_optimize = 2;
489
490 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
491 equivalent to seeing no -g option at all. */
492 static int mips_debug = 0;
493
494 /* The previous instruction. */
495 static struct mips_cl_insn prev_insn;
496
497 /* The instruction before prev_insn. */
498 static struct mips_cl_insn prev_prev_insn;
499
500 /* If we don't want information for prev_insn or prev_prev_insn, we
501 point the insn_mo field at this dummy integer. */
502 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
503
504 /* Non-zero if prev_insn is valid. */
505 static int prev_insn_valid;
506
507 /* The frag for the previous instruction. */
508 static struct frag *prev_insn_frag;
509
510 /* The offset into prev_insn_frag for the previous instruction. */
511 static long prev_insn_where;
512
513 /* The reloc type for the previous instruction, if any. */
514 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
515
516 /* The reloc for the previous instruction, if any. */
517 static fixS *prev_insn_fixp[3];
518
519 /* Non-zero if the previous instruction was in a delay slot. */
520 static int prev_insn_is_delay_slot;
521
522 /* Non-zero if the previous instruction was in a .set noreorder. */
523 static int prev_insn_unreordered;
524
525 /* Non-zero if the previous instruction uses an extend opcode (if
526 mips16). */
527 static int prev_insn_extended;
528
529 /* Non-zero if the previous previous instruction was in a .set
530 noreorder. */
531 static int prev_prev_insn_unreordered;
532
533 /* If this is set, it points to a frag holding nop instructions which
534 were inserted before the start of a noreorder section. If those
535 nops turn out to be unnecessary, the size of the frag can be
536 decreased. */
537 static fragS *prev_nop_frag;
538
539 /* The number of nop instructions we created in prev_nop_frag. */
540 static int prev_nop_frag_holds;
541
542 /* The number of nop instructions that we know we need in
543 prev_nop_frag. */
544 static int prev_nop_frag_required;
545
546 /* The number of instructions we've seen since prev_nop_frag. */
547 static int prev_nop_frag_since;
548
549 /* For ECOFF and ELF, relocations against symbols are done in two
550 parts, with a HI relocation and a LO relocation. Each relocation
551 has only 16 bits of space to store an addend. This means that in
552 order for the linker to handle carries correctly, it must be able
553 to locate both the HI and the LO relocation. This means that the
554 relocations must appear in order in the relocation table.
555
556 In order to implement this, we keep track of each unmatched HI
557 relocation. We then sort them so that they immediately precede the
558 corresponding LO relocation. */
559
560 struct mips_hi_fixup
561 {
562 /* Next HI fixup. */
563 struct mips_hi_fixup *next;
564 /* This fixup. */
565 fixS *fixp;
566 /* The section this fixup is in. */
567 segT seg;
568 };
569
570 /* The list of unmatched HI relocs. */
571
572 static struct mips_hi_fixup *mips_hi_fixup_list;
573
574 /* The frag containing the last explicit relocation operator.
575 Null if explicit relocations have not been used. */
576
577 static fragS *prev_reloc_op_frag;
578
579 /* Map normal MIPS register numbers to mips16 register numbers. */
580
581 #define X ILLEGAL_REG
582 static const int mips32_to_16_reg_map[] =
583 {
584 X, X, 2, 3, 4, 5, 6, 7,
585 X, X, X, X, X, X, X, X,
586 0, 1, X, X, X, X, X, X,
587 X, X, X, X, X, X, X, X
588 };
589 #undef X
590
591 /* Map mips16 register numbers to normal MIPS register numbers. */
592
593 static const unsigned int mips16_to_32_reg_map[] =
594 {
595 16, 17, 2, 3, 4, 5, 6, 7
596 };
597
598 static int mips_fix_4122_bugs;
599
600 /* We don't relax branches by default, since this causes us to expand
601 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
602 fail to compute the offset before expanding the macro to the most
603 efficient expansion. */
604
605 static int mips_relax_branch;
606 \f
607 /* Since the MIPS does not have multiple forms of PC relative
608 instructions, we do not have to do relaxing as is done on other
609 platforms. However, we do have to handle GP relative addressing
610 correctly, which turns out to be a similar problem.
611
612 Every macro that refers to a symbol can occur in (at least) two
613 forms, one with GP relative addressing and one without. For
614 example, loading a global variable into a register generally uses
615 a macro instruction like this:
616 lw $4,i
617 If i can be addressed off the GP register (this is true if it is in
618 the .sbss or .sdata section, or if it is known to be smaller than
619 the -G argument) this will generate the following instruction:
620 lw $4,i($gp)
621 This instruction will use a GPREL reloc. If i can not be addressed
622 off the GP register, the following instruction sequence will be used:
623 lui $at,i
624 lw $4,i($at)
625 In this case the first instruction will have a HI16 reloc, and the
626 second reloc will have a LO16 reloc. Both relocs will be against
627 the symbol i.
628
629 The issue here is that we may not know whether i is GP addressable
630 until after we see the instruction that uses it. Therefore, we
631 want to be able to choose the final instruction sequence only at
632 the end of the assembly. This is similar to the way other
633 platforms choose the size of a PC relative instruction only at the
634 end of assembly.
635
636 When generating position independent code we do not use GP
637 addressing in quite the same way, but the issue still arises as
638 external symbols and local symbols must be handled differently.
639
640 We handle these issues by actually generating both possible
641 instruction sequences. The longer one is put in a frag_var with
642 type rs_machine_dependent. We encode what to do with the frag in
643 the subtype field. We encode (1) the number of existing bytes to
644 replace, (2) the number of new bytes to use, (3) the offset from
645 the start of the existing bytes to the first reloc we must generate
646 (that is, the offset is applied from the start of the existing
647 bytes after they are replaced by the new bytes, if any), (4) the
648 offset from the start of the existing bytes to the second reloc,
649 (5) whether a third reloc is needed (the third reloc is always four
650 bytes after the second reloc), and (6) whether to warn if this
651 variant is used (this is sometimes needed if .set nomacro or .set
652 noat is in effect). All these numbers are reasonably small.
653
654 Generating two instruction sequences must be handled carefully to
655 ensure that delay slots are handled correctly. Fortunately, there
656 are a limited number of cases. When the second instruction
657 sequence is generated, append_insn is directed to maintain the
658 existing delay slot information, so it continues to apply to any
659 code after the second instruction sequence. This means that the
660 second instruction sequence must not impose any requirements not
661 required by the first instruction sequence.
662
663 These variant frags are then handled in functions called by the
664 machine independent code. md_estimate_size_before_relax returns
665 the final size of the frag. md_convert_frag sets up the final form
666 of the frag. tc_gen_reloc adjust the first reloc and adds a second
667 one if needed. */
668 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
669 ((relax_substateT) \
670 (((old) << 23) \
671 | ((new) << 16) \
672 | (((reloc1) + 64) << 9) \
673 | (((reloc2) + 64) << 2) \
674 | ((reloc3) ? (1 << 1) : 0) \
675 | ((warn) ? 1 : 0)))
676 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
677 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
678 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
679 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
680 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
681 #define RELAX_WARN(i) ((i) & 1)
682
683 /* Branch without likely bit. If label is out of range, we turn:
684
685 beq reg1, reg2, label
686 delay slot
687
688 into
689
690 bne reg1, reg2, 0f
691 nop
692 j label
693 0: delay slot
694
695 with the following opcode replacements:
696
697 beq <-> bne
698 blez <-> bgtz
699 bltz <-> bgez
700 bc1f <-> bc1t
701
702 bltzal <-> bgezal (with jal label instead of j label)
703
704 Even though keeping the delay slot instruction in the delay slot of
705 the branch would be more efficient, it would be very tricky to do
706 correctly, because we'd have to introduce a variable frag *after*
707 the delay slot instruction, and expand that instead. Let's do it
708 the easy way for now, even if the branch-not-taken case now costs
709 one additional instruction. Out-of-range branches are not supposed
710 to be common, anyway.
711
712 Branch likely. If label is out of range, we turn:
713
714 beql reg1, reg2, label
715 delay slot (annulled if branch not taken)
716
717 into
718
719 beql reg1, reg2, 1f
720 nop
721 beql $0, $0, 2f
722 nop
723 1: j[al] label
724 delay slot (executed only if branch taken)
725 2:
726
727 It would be possible to generate a shorter sequence by losing the
728 likely bit, generating something like:
729
730 bne reg1, reg2, 0f
731 nop
732 j[al] label
733 delay slot (executed only if branch taken)
734 0:
735
736 beql -> bne
737 bnel -> beq
738 blezl -> bgtz
739 bgtzl -> blez
740 bltzl -> bgez
741 bgezl -> bltz
742 bc1fl -> bc1t
743 bc1tl -> bc1f
744
745 bltzall -> bgezal (with jal label instead of j label)
746 bgezall -> bltzal (ditto)
747
748
749 but it's not clear that it would actually improve performance. */
750 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
751 ((relax_substateT) \
752 (0xc0000000 \
753 | ((toofar) ? 1 : 0) \
754 | ((link) ? 2 : 0) \
755 | ((likely) ? 4 : 0) \
756 | ((uncond) ? 8 : 0)))
757 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
758 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
759 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
760 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
761 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
762
763 /* For mips16 code, we use an entirely different form of relaxation.
764 mips16 supports two versions of most instructions which take
765 immediate values: a small one which takes some small value, and a
766 larger one which takes a 16 bit value. Since branches also follow
767 this pattern, relaxing these values is required.
768
769 We can assemble both mips16 and normal MIPS code in a single
770 object. Therefore, we need to support this type of relaxation at
771 the same time that we support the relaxation described above. We
772 use the high bit of the subtype field to distinguish these cases.
773
774 The information we store for this type of relaxation is the
775 argument code found in the opcode file for this relocation, whether
776 the user explicitly requested a small or extended form, and whether
777 the relocation is in a jump or jal delay slot. That tells us the
778 size of the value, and how it should be stored. We also store
779 whether the fragment is considered to be extended or not. We also
780 store whether this is known to be a branch to a different section,
781 whether we have tried to relax this frag yet, and whether we have
782 ever extended a PC relative fragment because of a shift count. */
783 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
784 (0x80000000 \
785 | ((type) & 0xff) \
786 | ((small) ? 0x100 : 0) \
787 | ((ext) ? 0x200 : 0) \
788 | ((dslot) ? 0x400 : 0) \
789 | ((jal_dslot) ? 0x800 : 0))
790 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
791 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
792 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
793 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
794 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
795 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
796 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
797 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
798 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
799 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
800 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
801 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
802
803 /* Is the given value a sign-extended 32-bit value? */
804 #define IS_SEXT_32BIT_NUM(x) \
805 (((x) &~ (offsetT) 0x7fffffff) == 0 \
806 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
807
808 /* Is the given value a sign-extended 16-bit value? */
809 #define IS_SEXT_16BIT_NUM(x) \
810 (((x) &~ (offsetT) 0x7fff) == 0 \
811 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
812
813 \f
814 /* Prototypes for static functions. */
815
816 #define internalError() \
817 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
818
819 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
820
821 static void append_insn
822 (char *place, struct mips_cl_insn *ip, expressionS *p,
823 bfd_reloc_code_real_type *r);
824 static void mips_no_prev_insn (int);
825 static void mips16_macro_build
826 (char *, int *, expressionS *, const char *, const char *, va_list);
827 static void load_register (int *, int, expressionS *, int);
828 static void macro (struct mips_cl_insn * ip);
829 static void mips16_macro (struct mips_cl_insn * ip);
830 #ifdef LOSING_COMPILER
831 static void macro2 (struct mips_cl_insn * ip);
832 #endif
833 static void mips_ip (char *str, struct mips_cl_insn * ip);
834 static void mips16_ip (char *str, struct mips_cl_insn * ip);
835 static void mips16_immed
836 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
837 unsigned long *, bfd_boolean *, unsigned short *);
838 static size_t my_getSmallExpression
839 (expressionS *, bfd_reloc_code_real_type *, char *);
840 static void my_getExpression (expressionS *, char *);
841 static void s_align (int);
842 static void s_change_sec (int);
843 static void s_change_section (int);
844 static void s_cons (int);
845 static void s_float_cons (int);
846 static void s_mips_globl (int);
847 static void s_option (int);
848 static void s_mipsset (int);
849 static void s_abicalls (int);
850 static void s_cpload (int);
851 static void s_cpsetup (int);
852 static void s_cplocal (int);
853 static void s_cprestore (int);
854 static void s_cpreturn (int);
855 static void s_gpvalue (int);
856 static void s_gpword (int);
857 static void s_gpdword (int);
858 static void s_cpadd (int);
859 static void s_insn (int);
860 static void md_obj_begin (void);
861 static void md_obj_end (void);
862 static void s_mips_ent (int);
863 static void s_mips_end (int);
864 static void s_mips_frame (int);
865 static void s_mips_mask (int reg_type);
866 static void s_mips_stab (int);
867 static void s_mips_weakext (int);
868 static void s_mips_file (int);
869 static void s_mips_loc (int);
870 static bfd_boolean pic_need_relax (symbolS *, asection *);
871 static int relaxed_branch_length (fragS *, asection *, int);
872 static int validate_mips_insn (const struct mips_opcode *);
873
874 /* Table and functions used to map between CPU/ISA names, and
875 ISA levels, and CPU numbers. */
876
877 struct mips_cpu_info
878 {
879 const char *name; /* CPU or ISA name. */
880 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
881 int isa; /* ISA level. */
882 int cpu; /* CPU number (default CPU if ISA). */
883 };
884
885 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
886 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
887 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
888 \f
889 /* Pseudo-op table.
890
891 The following pseudo-ops from the Kane and Heinrich MIPS book
892 should be defined here, but are currently unsupported: .alias,
893 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
894
895 The following pseudo-ops from the Kane and Heinrich MIPS book are
896 specific to the type of debugging information being generated, and
897 should be defined by the object format: .aent, .begin, .bend,
898 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
899 .vreg.
900
901 The following pseudo-ops from the Kane and Heinrich MIPS book are
902 not MIPS CPU specific, but are also not specific to the object file
903 format. This file is probably the best place to define them, but
904 they are not currently supported: .asm0, .endr, .lab, .repeat,
905 .struct. */
906
907 static const pseudo_typeS mips_pseudo_table[] =
908 {
909 /* MIPS specific pseudo-ops. */
910 {"option", s_option, 0},
911 {"set", s_mipsset, 0},
912 {"rdata", s_change_sec, 'r'},
913 {"sdata", s_change_sec, 's'},
914 {"livereg", s_ignore, 0},
915 {"abicalls", s_abicalls, 0},
916 {"cpload", s_cpload, 0},
917 {"cpsetup", s_cpsetup, 0},
918 {"cplocal", s_cplocal, 0},
919 {"cprestore", s_cprestore, 0},
920 {"cpreturn", s_cpreturn, 0},
921 {"gpvalue", s_gpvalue, 0},
922 {"gpword", s_gpword, 0},
923 {"gpdword", s_gpdword, 0},
924 {"cpadd", s_cpadd, 0},
925 {"insn", s_insn, 0},
926
927 /* Relatively generic pseudo-ops that happen to be used on MIPS
928 chips. */
929 {"asciiz", stringer, 1},
930 {"bss", s_change_sec, 'b'},
931 {"err", s_err, 0},
932 {"half", s_cons, 1},
933 {"dword", s_cons, 3},
934 {"weakext", s_mips_weakext, 0},
935
936 /* These pseudo-ops are defined in read.c, but must be overridden
937 here for one reason or another. */
938 {"align", s_align, 0},
939 {"byte", s_cons, 0},
940 {"data", s_change_sec, 'd'},
941 {"double", s_float_cons, 'd'},
942 {"float", s_float_cons, 'f'},
943 {"globl", s_mips_globl, 0},
944 {"global", s_mips_globl, 0},
945 {"hword", s_cons, 1},
946 {"int", s_cons, 2},
947 {"long", s_cons, 2},
948 {"octa", s_cons, 4},
949 {"quad", s_cons, 3},
950 {"section", s_change_section, 0},
951 {"short", s_cons, 1},
952 {"single", s_float_cons, 'f'},
953 {"stabn", s_mips_stab, 'n'},
954 {"text", s_change_sec, 't'},
955 {"word", s_cons, 2},
956
957 { "extern", ecoff_directive_extern, 0},
958
959 { NULL, NULL, 0 },
960 };
961
962 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
963 {
964 /* These pseudo-ops should be defined by the object file format.
965 However, a.out doesn't support them, so we have versions here. */
966 {"aent", s_mips_ent, 1},
967 {"bgnb", s_ignore, 0},
968 {"end", s_mips_end, 0},
969 {"endb", s_ignore, 0},
970 {"ent", s_mips_ent, 0},
971 {"file", s_mips_file, 0},
972 {"fmask", s_mips_mask, 'F'},
973 {"frame", s_mips_frame, 0},
974 {"loc", s_mips_loc, 0},
975 {"mask", s_mips_mask, 'R'},
976 {"verstamp", s_ignore, 0},
977 { NULL, NULL, 0 },
978 };
979
980 extern void pop_insert (const pseudo_typeS *);
981
982 void
983 mips_pop_insert (void)
984 {
985 pop_insert (mips_pseudo_table);
986 if (! ECOFF_DEBUGGING)
987 pop_insert (mips_nonecoff_pseudo_table);
988 }
989 \f
990 /* Symbols labelling the current insn. */
991
992 struct insn_label_list
993 {
994 struct insn_label_list *next;
995 symbolS *label;
996 };
997
998 static struct insn_label_list *insn_labels;
999 static struct insn_label_list *free_insn_labels;
1000
1001 static void mips_clear_insn_labels (void);
1002
1003 static inline void
1004 mips_clear_insn_labels (void)
1005 {
1006 register struct insn_label_list **pl;
1007
1008 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1009 ;
1010 *pl = insn_labels;
1011 insn_labels = NULL;
1012 }
1013 \f
1014 static char *expr_end;
1015
1016 /* Expressions which appear in instructions. These are set by
1017 mips_ip. */
1018
1019 static expressionS imm_expr;
1020 static expressionS offset_expr;
1021
1022 /* Relocs associated with imm_expr and offset_expr. */
1023
1024 static bfd_reloc_code_real_type imm_reloc[3]
1025 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1026 static bfd_reloc_code_real_type offset_reloc[3]
1027 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1028
1029 /* These are set by mips16_ip if an explicit extension is used. */
1030
1031 static bfd_boolean mips16_small, mips16_ext;
1032
1033 #ifdef OBJ_ELF
1034 /* The pdr segment for per procedure frame/regmask info. Not used for
1035 ECOFF debugging. */
1036
1037 static segT pdr_seg;
1038 #endif
1039
1040 /* The default target format to use. */
1041
1042 const char *
1043 mips_target_format (void)
1044 {
1045 switch (OUTPUT_FLAVOR)
1046 {
1047 case bfd_target_aout_flavour:
1048 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1049 case bfd_target_ecoff_flavour:
1050 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1051 case bfd_target_coff_flavour:
1052 return "pe-mips";
1053 case bfd_target_elf_flavour:
1054 #ifdef TE_TMIPS
1055 /* This is traditional mips. */
1056 return (target_big_endian
1057 ? (HAVE_64BIT_OBJECTS
1058 ? "elf64-tradbigmips"
1059 : (HAVE_NEWABI
1060 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1061 : (HAVE_64BIT_OBJECTS
1062 ? "elf64-tradlittlemips"
1063 : (HAVE_NEWABI
1064 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1065 #else
1066 return (target_big_endian
1067 ? (HAVE_64BIT_OBJECTS
1068 ? "elf64-bigmips"
1069 : (HAVE_NEWABI
1070 ? "elf32-nbigmips" : "elf32-bigmips"))
1071 : (HAVE_64BIT_OBJECTS
1072 ? "elf64-littlemips"
1073 : (HAVE_NEWABI
1074 ? "elf32-nlittlemips" : "elf32-littlemips")));
1075 #endif
1076 default:
1077 abort ();
1078 return NULL;
1079 }
1080 }
1081
1082 /* This function is called once, at assembler startup time. It should
1083 set up all the tables, etc. that the MD part of the assembler will need. */
1084
1085 void
1086 md_begin (void)
1087 {
1088 register const char *retval = NULL;
1089 int i = 0;
1090 int broken = 0;
1091
1092 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1093 as_warn (_("Could not set architecture and machine"));
1094
1095 op_hash = hash_new ();
1096
1097 for (i = 0; i < NUMOPCODES;)
1098 {
1099 const char *name = mips_opcodes[i].name;
1100
1101 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1102 if (retval != NULL)
1103 {
1104 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1105 mips_opcodes[i].name, retval);
1106 /* Probably a memory allocation problem? Give up now. */
1107 as_fatal (_("Broken assembler. No assembly attempted."));
1108 }
1109 do
1110 {
1111 if (mips_opcodes[i].pinfo != INSN_MACRO)
1112 {
1113 if (!validate_mips_insn (&mips_opcodes[i]))
1114 broken = 1;
1115 }
1116 ++i;
1117 }
1118 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1119 }
1120
1121 mips16_op_hash = hash_new ();
1122
1123 i = 0;
1124 while (i < bfd_mips16_num_opcodes)
1125 {
1126 const char *name = mips16_opcodes[i].name;
1127
1128 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1129 if (retval != NULL)
1130 as_fatal (_("internal: can't hash `%s': %s"),
1131 mips16_opcodes[i].name, retval);
1132 do
1133 {
1134 if (mips16_opcodes[i].pinfo != INSN_MACRO
1135 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1136 != mips16_opcodes[i].match))
1137 {
1138 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1139 mips16_opcodes[i].name, mips16_opcodes[i].args);
1140 broken = 1;
1141 }
1142 ++i;
1143 }
1144 while (i < bfd_mips16_num_opcodes
1145 && strcmp (mips16_opcodes[i].name, name) == 0);
1146 }
1147
1148 if (broken)
1149 as_fatal (_("Broken assembler. No assembly attempted."));
1150
1151 /* We add all the general register names to the symbol table. This
1152 helps us detect invalid uses of them. */
1153 for (i = 0; i < 32; i++)
1154 {
1155 char buf[5];
1156
1157 sprintf (buf, "$%d", i);
1158 symbol_table_insert (symbol_new (buf, reg_section, i,
1159 &zero_address_frag));
1160 }
1161 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1162 &zero_address_frag));
1163 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1164 &zero_address_frag));
1165 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1166 &zero_address_frag));
1167 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1168 &zero_address_frag));
1169 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1170 &zero_address_frag));
1171 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1172 &zero_address_frag));
1173 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1174 &zero_address_frag));
1175 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1176 &zero_address_frag));
1177 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1178 &zero_address_frag));
1179
1180 /* If we don't add these register names to the symbol table, they
1181 may end up being added as regular symbols by operand(), and then
1182 make it to the object file as undefined in case they're not
1183 regarded as local symbols. They're local in o32, since `$' is a
1184 local symbol prefix, but not in n32 or n64. */
1185 for (i = 0; i < 8; i++)
1186 {
1187 char buf[6];
1188
1189 sprintf (buf, "$fcc%i", i);
1190 symbol_table_insert (symbol_new (buf, reg_section, -1,
1191 &zero_address_frag));
1192 }
1193
1194 mips_no_prev_insn (FALSE);
1195
1196 mips_gprmask = 0;
1197 mips_cprmask[0] = 0;
1198 mips_cprmask[1] = 0;
1199 mips_cprmask[2] = 0;
1200 mips_cprmask[3] = 0;
1201
1202 /* set the default alignment for the text section (2**2) */
1203 record_alignment (text_section, 2);
1204
1205 if (USE_GLOBAL_POINTER_OPT)
1206 bfd_set_gp_size (stdoutput, g_switch_value);
1207
1208 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1209 {
1210 /* On a native system, sections must be aligned to 16 byte
1211 boundaries. When configured for an embedded ELF target, we
1212 don't bother. */
1213 if (strcmp (TARGET_OS, "elf") != 0)
1214 {
1215 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1216 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1217 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1218 }
1219
1220 /* Create a .reginfo section for register masks and a .mdebug
1221 section for debugging information. */
1222 {
1223 segT seg;
1224 subsegT subseg;
1225 flagword flags;
1226 segT sec;
1227
1228 seg = now_seg;
1229 subseg = now_subseg;
1230
1231 /* The ABI says this section should be loaded so that the
1232 running program can access it. However, we don't load it
1233 if we are configured for an embedded target */
1234 flags = SEC_READONLY | SEC_DATA;
1235 if (strcmp (TARGET_OS, "elf") != 0)
1236 flags |= SEC_ALLOC | SEC_LOAD;
1237
1238 if (mips_abi != N64_ABI)
1239 {
1240 sec = subseg_new (".reginfo", (subsegT) 0);
1241
1242 bfd_set_section_flags (stdoutput, sec, flags);
1243 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1244
1245 #ifdef OBJ_ELF
1246 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1247 #endif
1248 }
1249 else
1250 {
1251 /* The 64-bit ABI uses a .MIPS.options section rather than
1252 .reginfo section. */
1253 sec = subseg_new (".MIPS.options", (subsegT) 0);
1254 bfd_set_section_flags (stdoutput, sec, flags);
1255 bfd_set_section_alignment (stdoutput, sec, 3);
1256
1257 #ifdef OBJ_ELF
1258 /* Set up the option header. */
1259 {
1260 Elf_Internal_Options opthdr;
1261 char *f;
1262
1263 opthdr.kind = ODK_REGINFO;
1264 opthdr.size = (sizeof (Elf_External_Options)
1265 + sizeof (Elf64_External_RegInfo));
1266 opthdr.section = 0;
1267 opthdr.info = 0;
1268 f = frag_more (sizeof (Elf_External_Options));
1269 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1270 (Elf_External_Options *) f);
1271
1272 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1273 }
1274 #endif
1275 }
1276
1277 if (ECOFF_DEBUGGING)
1278 {
1279 sec = subseg_new (".mdebug", (subsegT) 0);
1280 (void) bfd_set_section_flags (stdoutput, sec,
1281 SEC_HAS_CONTENTS | SEC_READONLY);
1282 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1283 }
1284 #ifdef OBJ_ELF
1285 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1286 {
1287 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1288 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1289 SEC_READONLY | SEC_RELOC
1290 | SEC_DEBUGGING);
1291 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1292 }
1293 #endif
1294
1295 subseg_set (seg, subseg);
1296 }
1297 }
1298
1299 if (! ECOFF_DEBUGGING)
1300 md_obj_begin ();
1301 }
1302
1303 void
1304 md_mips_end (void)
1305 {
1306 if (! ECOFF_DEBUGGING)
1307 md_obj_end ();
1308 }
1309
1310 void
1311 md_assemble (char *str)
1312 {
1313 struct mips_cl_insn insn;
1314 bfd_reloc_code_real_type unused_reloc[3]
1315 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1316
1317 imm_expr.X_op = O_absent;
1318 offset_expr.X_op = O_absent;
1319 imm_reloc[0] = BFD_RELOC_UNUSED;
1320 imm_reloc[1] = BFD_RELOC_UNUSED;
1321 imm_reloc[2] = BFD_RELOC_UNUSED;
1322 offset_reloc[0] = BFD_RELOC_UNUSED;
1323 offset_reloc[1] = BFD_RELOC_UNUSED;
1324 offset_reloc[2] = BFD_RELOC_UNUSED;
1325
1326 if (mips_opts.mips16)
1327 mips16_ip (str, &insn);
1328 else
1329 {
1330 mips_ip (str, &insn);
1331 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1332 str, insn.insn_opcode));
1333 }
1334
1335 if (insn_error)
1336 {
1337 as_bad ("%s `%s'", insn_error, str);
1338 return;
1339 }
1340
1341 if (insn.insn_mo->pinfo == INSN_MACRO)
1342 {
1343 if (mips_opts.mips16)
1344 mips16_macro (&insn);
1345 else
1346 macro (&insn);
1347 }
1348 else
1349 {
1350 if (imm_expr.X_op != O_absent)
1351 append_insn (NULL, &insn, &imm_expr, imm_reloc);
1352 else if (offset_expr.X_op != O_absent)
1353 append_insn (NULL, &insn, &offset_expr, offset_reloc);
1354 else
1355 append_insn (NULL, &insn, NULL, unused_reloc);
1356 }
1357 }
1358
1359 /* Return true if the given relocation might need a matching %lo().
1360 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1361 applied to local symbols. */
1362
1363 static inline bfd_boolean
1364 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1365 {
1366 return (reloc == BFD_RELOC_HI16_S
1367 || reloc == BFD_RELOC_MIPS_GOT16);
1368 }
1369
1370 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1371 relocation. */
1372
1373 static inline bfd_boolean
1374 fixup_has_matching_lo_p (fixS *fixp)
1375 {
1376 return (fixp->fx_next != NULL
1377 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1378 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1379 && fixp->fx_offset == fixp->fx_next->fx_offset);
1380 }
1381
1382 /* See whether instruction IP reads register REG. CLASS is the type
1383 of register. */
1384
1385 static int
1386 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1387 enum mips_regclass class)
1388 {
1389 if (class == MIPS16_REG)
1390 {
1391 assert (mips_opts.mips16);
1392 reg = mips16_to_32_reg_map[reg];
1393 class = MIPS_GR_REG;
1394 }
1395
1396 /* Don't report on general register ZERO, since it never changes. */
1397 if (class == MIPS_GR_REG && reg == ZERO)
1398 return 0;
1399
1400 if (class == MIPS_FP_REG)
1401 {
1402 assert (! mips_opts.mips16);
1403 /* If we are called with either $f0 or $f1, we must check $f0.
1404 This is not optimal, because it will introduce an unnecessary
1405 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1406 need to distinguish reading both $f0 and $f1 or just one of
1407 them. Note that we don't have to check the other way,
1408 because there is no instruction that sets both $f0 and $f1
1409 and requires a delay. */
1410 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1411 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1412 == (reg &~ (unsigned) 1)))
1413 return 1;
1414 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1415 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1416 == (reg &~ (unsigned) 1)))
1417 return 1;
1418 }
1419 else if (! mips_opts.mips16)
1420 {
1421 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1422 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1423 return 1;
1424 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1425 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1426 return 1;
1427 }
1428 else
1429 {
1430 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1431 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1432 & MIPS16OP_MASK_RX)]
1433 == reg))
1434 return 1;
1435 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1436 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1437 & MIPS16OP_MASK_RY)]
1438 == reg))
1439 return 1;
1440 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1441 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1442 & MIPS16OP_MASK_MOVE32Z)]
1443 == reg))
1444 return 1;
1445 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1446 return 1;
1447 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1448 return 1;
1449 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1450 return 1;
1451 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1452 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1453 & MIPS16OP_MASK_REGR32) == reg)
1454 return 1;
1455 }
1456
1457 return 0;
1458 }
1459
1460 /* This function returns true if modifying a register requires a
1461 delay. */
1462
1463 static int
1464 reg_needs_delay (unsigned int reg)
1465 {
1466 unsigned long prev_pinfo;
1467
1468 prev_pinfo = prev_insn.insn_mo->pinfo;
1469 if (! mips_opts.noreorder
1470 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1471 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1472 || (! gpr_interlocks
1473 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1474 {
1475 /* A load from a coprocessor or from memory. All load
1476 delays delay the use of general register rt for one
1477 instruction on the r3000. The r6000 and r4000 use
1478 interlocks. */
1479 /* Itbl support may require additional care here. */
1480 know (prev_pinfo & INSN_WRITE_GPR_T);
1481 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1482 return 1;
1483 }
1484
1485 return 0;
1486 }
1487
1488 /* Mark instruction labels in mips16 mode. This permits the linker to
1489 handle them specially, such as generating jalx instructions when
1490 needed. We also make them odd for the duration of the assembly, in
1491 order to generate the right sort of code. We will make them even
1492 in the adjust_symtab routine, while leaving them marked. This is
1493 convenient for the debugger and the disassembler. The linker knows
1494 to make them odd again. */
1495
1496 static void
1497 mips16_mark_labels (void)
1498 {
1499 if (mips_opts.mips16)
1500 {
1501 struct insn_label_list *l;
1502 valueT val;
1503
1504 for (l = insn_labels; l != NULL; l = l->next)
1505 {
1506 #ifdef OBJ_ELF
1507 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1508 S_SET_OTHER (l->label, STO_MIPS16);
1509 #endif
1510 val = S_GET_VALUE (l->label);
1511 if ((val & 1) == 0)
1512 S_SET_VALUE (l->label, val + 1);
1513 }
1514 }
1515 }
1516
1517 /* Output an instruction. PLACE is where to put the instruction; if
1518 it is NULL, this uses frag_more to get room. IP is the instruction
1519 information. ADDRESS_EXPR is an operand of the instruction to be
1520 used with RELOC_TYPE. */
1521
1522 static void
1523 append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1524 bfd_reloc_code_real_type *reloc_type)
1525 {
1526 register unsigned long prev_pinfo, pinfo;
1527 char *f;
1528 fixS *fixp[3];
1529 int nops = 0;
1530 bfd_boolean force_new_frag = FALSE;
1531
1532 /* Mark instruction labels in mips16 mode. */
1533 mips16_mark_labels ();
1534
1535 prev_pinfo = prev_insn.insn_mo->pinfo;
1536 pinfo = ip->insn_mo->pinfo;
1537
1538 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1539 {
1540 int prev_prev_nop;
1541
1542 /* If the previous insn required any delay slots, see if we need
1543 to insert a NOP or two. There are eight kinds of possible
1544 hazards, of which an instruction can have at most one type.
1545 (1) a load from memory delay
1546 (2) a load from a coprocessor delay
1547 (3) an unconditional branch delay
1548 (4) a conditional branch delay
1549 (5) a move to coprocessor register delay
1550 (6) a load coprocessor register from memory delay
1551 (7) a coprocessor condition code delay
1552 (8) a HI/LO special register delay
1553
1554 There are a lot of optimizations we could do that we don't.
1555 In particular, we do not, in general, reorder instructions.
1556 If you use gcc with optimization, it will reorder
1557 instructions and generally do much more optimization then we
1558 do here; repeating all that work in the assembler would only
1559 benefit hand written assembly code, and does not seem worth
1560 it. */
1561
1562 /* This is how a NOP is emitted. */
1563 #define emit_nop() \
1564 (mips_opts.mips16 \
1565 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1566 : md_number_to_chars (frag_more (4), 0, 4))
1567
1568 /* The previous insn might require a delay slot, depending upon
1569 the contents of the current insn. */
1570 if (! mips_opts.mips16
1571 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1572 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1573 && ! cop_interlocks)
1574 || (! gpr_interlocks
1575 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1576 {
1577 /* A load from a coprocessor or from memory. All load
1578 delays delay the use of general register rt for one
1579 instruction on the r3000. The r6000 and r4000 use
1580 interlocks. */
1581 /* Itbl support may require additional care here. */
1582 know (prev_pinfo & INSN_WRITE_GPR_T);
1583 if (mips_optimize == 0
1584 || insn_uses_reg (ip,
1585 ((prev_insn.insn_opcode >> OP_SH_RT)
1586 & OP_MASK_RT),
1587 MIPS_GR_REG))
1588 ++nops;
1589 }
1590 else if (! mips_opts.mips16
1591 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1592 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1593 && ! cop_interlocks)
1594 || (mips_opts.isa == ISA_MIPS1
1595 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1596 {
1597 /* A generic coprocessor delay. The previous instruction
1598 modified a coprocessor general or control register. If
1599 it modified a control register, we need to avoid any
1600 coprocessor instruction (this is probably not always
1601 required, but it sometimes is). If it modified a general
1602 register, we avoid using that register.
1603
1604 On the r6000 and r4000 loading a coprocessor register
1605 from memory is interlocked, and does not require a delay.
1606
1607 This case is not handled very well. There is no special
1608 knowledge of CP0 handling, and the coprocessors other
1609 than the floating point unit are not distinguished at
1610 all. */
1611 /* Itbl support may require additional care here. FIXME!
1612 Need to modify this to include knowledge about
1613 user specified delays! */
1614 if (prev_pinfo & INSN_WRITE_FPR_T)
1615 {
1616 if (mips_optimize == 0
1617 || insn_uses_reg (ip,
1618 ((prev_insn.insn_opcode >> OP_SH_FT)
1619 & OP_MASK_FT),
1620 MIPS_FP_REG))
1621 ++nops;
1622 }
1623 else if (prev_pinfo & INSN_WRITE_FPR_S)
1624 {
1625 if (mips_optimize == 0
1626 || insn_uses_reg (ip,
1627 ((prev_insn.insn_opcode >> OP_SH_FS)
1628 & OP_MASK_FS),
1629 MIPS_FP_REG))
1630 ++nops;
1631 }
1632 else
1633 {
1634 /* We don't know exactly what the previous instruction
1635 does. If the current instruction uses a coprocessor
1636 register, we must insert a NOP. If previous
1637 instruction may set the condition codes, and the
1638 current instruction uses them, we must insert two
1639 NOPS. */
1640 /* Itbl support may require additional care here. */
1641 if (mips_optimize == 0
1642 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1643 && (pinfo & INSN_READ_COND_CODE)))
1644 nops += 2;
1645 else if (pinfo & INSN_COP)
1646 ++nops;
1647 }
1648 }
1649 else if (! mips_opts.mips16
1650 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1651 && (prev_pinfo & INSN_WRITE_COND_CODE)
1652 && ! cop_interlocks)
1653 {
1654 /* The previous instruction sets the coprocessor condition
1655 codes, but does not require a general coprocessor delay
1656 (this means it is a floating point comparison
1657 instruction). If this instruction uses the condition
1658 codes, we need to insert a single NOP. */
1659 /* Itbl support may require additional care here. */
1660 if (mips_optimize == 0
1661 || (pinfo & INSN_READ_COND_CODE))
1662 ++nops;
1663 }
1664
1665 /* If we're fixing up mfhi/mflo for the r7000 and the
1666 previous insn was an mfhi/mflo and the current insn
1667 reads the register that the mfhi/mflo wrote to, then
1668 insert two nops. */
1669
1670 else if (mips_7000_hilo_fix
1671 && MF_HILO_INSN (prev_pinfo)
1672 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1673 & OP_MASK_RD),
1674 MIPS_GR_REG))
1675 {
1676 nops += 2;
1677 }
1678
1679 /* If we're fixing up mfhi/mflo for the r7000 and the
1680 2nd previous insn was an mfhi/mflo and the current insn
1681 reads the register that the mfhi/mflo wrote to, then
1682 insert one nop. */
1683
1684 else if (mips_7000_hilo_fix
1685 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1686 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1687 & OP_MASK_RD),
1688 MIPS_GR_REG))
1689
1690 {
1691 ++nops;
1692 }
1693
1694 else if (prev_pinfo & INSN_READ_LO)
1695 {
1696 /* The previous instruction reads the LO register; if the
1697 current instruction writes to the LO register, we must
1698 insert two NOPS. Some newer processors have interlocks.
1699 Also the tx39's multiply instructions can be exectuted
1700 immediatly after a read from HI/LO (without the delay),
1701 though the tx39's divide insns still do require the
1702 delay. */
1703 if (! (hilo_interlocks
1704 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1705 && (mips_optimize == 0
1706 || (pinfo & INSN_WRITE_LO)))
1707 nops += 2;
1708 /* Most mips16 branch insns don't have a delay slot.
1709 If a read from LO is immediately followed by a branch
1710 to a write to LO we have a read followed by a write
1711 less than 2 insns away. We assume the target of
1712 a branch might be a write to LO, and insert a nop
1713 between a read and an immediately following branch. */
1714 else if (mips_opts.mips16
1715 && (mips_optimize == 0
1716 || (pinfo & MIPS16_INSN_BRANCH)))
1717 ++nops;
1718 }
1719 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1720 {
1721 /* The previous instruction reads the HI register; if the
1722 current instruction writes to the HI register, we must
1723 insert a NOP. Some newer processors have interlocks.
1724 Also the note tx39's multiply above. */
1725 if (! (hilo_interlocks
1726 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1727 && (mips_optimize == 0
1728 || (pinfo & INSN_WRITE_HI)))
1729 nops += 2;
1730 /* Most mips16 branch insns don't have a delay slot.
1731 If a read from HI is immediately followed by a branch
1732 to a write to HI we have a read followed by a write
1733 less than 2 insns away. We assume the target of
1734 a branch might be a write to HI, and insert a nop
1735 between a read and an immediately following branch. */
1736 else if (mips_opts.mips16
1737 && (mips_optimize == 0
1738 || (pinfo & MIPS16_INSN_BRANCH)))
1739 ++nops;
1740 }
1741
1742 /* If the previous instruction was in a noreorder section, then
1743 we don't want to insert the nop after all. */
1744 /* Itbl support may require additional care here. */
1745 if (prev_insn_unreordered)
1746 nops = 0;
1747
1748 /* There are two cases which require two intervening
1749 instructions: 1) setting the condition codes using a move to
1750 coprocessor instruction which requires a general coprocessor
1751 delay and then reading the condition codes 2) reading the HI
1752 or LO register and then writing to it (except on processors
1753 which have interlocks). If we are not already emitting a NOP
1754 instruction, we must check for these cases compared to the
1755 instruction previous to the previous instruction. */
1756 if ((! mips_opts.mips16
1757 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1758 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1759 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1760 && (pinfo & INSN_READ_COND_CODE)
1761 && ! cop_interlocks)
1762 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1763 && (pinfo & INSN_WRITE_LO)
1764 && ! (hilo_interlocks
1765 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1766 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1767 && (pinfo & INSN_WRITE_HI)
1768 && ! (hilo_interlocks
1769 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1770 prev_prev_nop = 1;
1771 else
1772 prev_prev_nop = 0;
1773
1774 if (prev_prev_insn_unreordered)
1775 prev_prev_nop = 0;
1776
1777 if (prev_prev_nop && nops == 0)
1778 ++nops;
1779
1780 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1781 {
1782 /* We're out of bits in pinfo, so we must resort to string
1783 ops here. Shortcuts are selected based on opcodes being
1784 limited to the VR4122 instruction set. */
1785 int min_nops = 0;
1786 const char *pn = prev_insn.insn_mo->name;
1787 const char *tn = ip->insn_mo->name;
1788 if (strncmp(pn, "macc", 4) == 0
1789 || strncmp(pn, "dmacc", 5) == 0)
1790 {
1791 /* Errata 21 - [D]DIV[U] after [D]MACC */
1792 if (strstr (tn, "div"))
1793 {
1794 min_nops = 1;
1795 }
1796
1797 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1798 if (pn[0] == 'd' /* dmacc */
1799 && (strncmp(tn, "dmult", 5) == 0
1800 || strncmp(tn, "dmacc", 5) == 0))
1801 {
1802 min_nops = 1;
1803 }
1804
1805 /* Errata 24 - MT{LO,HI} after [D]MACC */
1806 if (strcmp (tn, "mtlo") == 0
1807 || strcmp (tn, "mthi") == 0)
1808 {
1809 min_nops = 1;
1810 }
1811
1812 }
1813 else if (strncmp(pn, "dmult", 5) == 0
1814 && (strncmp(tn, "dmult", 5) == 0
1815 || strncmp(tn, "dmacc", 5) == 0))
1816 {
1817 /* Here is the rest of errata 23. */
1818 min_nops = 1;
1819 }
1820 if (nops < min_nops)
1821 nops = min_nops;
1822 }
1823
1824 /* If we are being given a nop instruction, don't bother with
1825 one of the nops we would otherwise output. This will only
1826 happen when a nop instruction is used with mips_optimize set
1827 to 0. */
1828 if (nops > 0
1829 && ! mips_opts.noreorder
1830 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1831 --nops;
1832
1833 /* Now emit the right number of NOP instructions. */
1834 if (nops > 0 && ! mips_opts.noreorder)
1835 {
1836 fragS *old_frag;
1837 unsigned long old_frag_offset;
1838 int i;
1839 struct insn_label_list *l;
1840
1841 old_frag = frag_now;
1842 old_frag_offset = frag_now_fix ();
1843
1844 for (i = 0; i < nops; i++)
1845 emit_nop ();
1846
1847 if (listing)
1848 {
1849 listing_prev_line ();
1850 /* We may be at the start of a variant frag. In case we
1851 are, make sure there is enough space for the frag
1852 after the frags created by listing_prev_line. The
1853 argument to frag_grow here must be at least as large
1854 as the argument to all other calls to frag_grow in
1855 this file. We don't have to worry about being in the
1856 middle of a variant frag, because the variants insert
1857 all needed nop instructions themselves. */
1858 frag_grow (40);
1859 }
1860
1861 for (l = insn_labels; l != NULL; l = l->next)
1862 {
1863 valueT val;
1864
1865 assert (S_GET_SEGMENT (l->label) == now_seg);
1866 symbol_set_frag (l->label, frag_now);
1867 val = (valueT) frag_now_fix ();
1868 /* mips16 text labels are stored as odd. */
1869 if (mips_opts.mips16)
1870 ++val;
1871 S_SET_VALUE (l->label, val);
1872 }
1873
1874 #ifndef NO_ECOFF_DEBUGGING
1875 if (ECOFF_DEBUGGING)
1876 ecoff_fix_loc (old_frag, old_frag_offset);
1877 #endif
1878 }
1879 else if (prev_nop_frag != NULL)
1880 {
1881 /* We have a frag holding nops we may be able to remove. If
1882 we don't need any nops, we can decrease the size of
1883 prev_nop_frag by the size of one instruction. If we do
1884 need some nops, we count them in prev_nops_required. */
1885 if (prev_nop_frag_since == 0)
1886 {
1887 if (nops == 0)
1888 {
1889 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1890 --prev_nop_frag_holds;
1891 }
1892 else
1893 prev_nop_frag_required += nops;
1894 }
1895 else
1896 {
1897 if (prev_prev_nop == 0)
1898 {
1899 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1900 --prev_nop_frag_holds;
1901 }
1902 else
1903 ++prev_nop_frag_required;
1904 }
1905
1906 if (prev_nop_frag_holds <= prev_nop_frag_required)
1907 prev_nop_frag = NULL;
1908
1909 ++prev_nop_frag_since;
1910
1911 /* Sanity check: by the time we reach the second instruction
1912 after prev_nop_frag, we should have used up all the nops
1913 one way or another. */
1914 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1915 }
1916 }
1917
1918 if (place == NULL
1919 && address_expr
1920 && *reloc_type == BFD_RELOC_16_PCREL_S2
1921 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1922 || pinfo & INSN_COND_BRANCH_LIKELY)
1923 && mips_relax_branch
1924 /* Don't try branch relaxation within .set nomacro, or within
1925 .set noat if we use $at for PIC computations. If it turns
1926 out that the branch was out-of-range, we'll get an error. */
1927 && !mips_opts.warn_about_macros
1928 && !(mips_opts.noat && mips_pic != NO_PIC)
1929 && !mips_opts.mips16)
1930 {
1931 f = frag_var (rs_machine_dependent,
1932 relaxed_branch_length
1933 (NULL, NULL,
1934 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1935 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1936 RELAX_BRANCH_ENCODE
1937 (pinfo & INSN_UNCOND_BRANCH_DELAY,
1938 pinfo & INSN_COND_BRANCH_LIKELY,
1939 pinfo & INSN_WRITE_GPR_31,
1940 0),
1941 address_expr->X_add_symbol,
1942 address_expr->X_add_number,
1943 0);
1944 *reloc_type = BFD_RELOC_UNUSED;
1945 }
1946 else if (*reloc_type > BFD_RELOC_UNUSED)
1947 {
1948 /* We need to set up a variant frag. */
1949 assert (mips_opts.mips16 && address_expr != NULL);
1950 f = frag_var (rs_machine_dependent, 4, 0,
1951 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1952 mips16_small, mips16_ext,
1953 (prev_pinfo
1954 & INSN_UNCOND_BRANCH_DELAY),
1955 (*prev_insn_reloc_type
1956 == BFD_RELOC_MIPS16_JMP)),
1957 make_expr_symbol (address_expr), 0, NULL);
1958 }
1959 else if (place != NULL)
1960 f = place;
1961 else if (mips_opts.mips16
1962 && ! ip->use_extend
1963 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1964 {
1965 /* Make sure there is enough room to swap this instruction with
1966 a following jump instruction. */
1967 frag_grow (6);
1968 f = frag_more (2);
1969 }
1970 else
1971 {
1972 if (mips_opts.mips16
1973 && mips_opts.noreorder
1974 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1975 as_warn (_("extended instruction in delay slot"));
1976
1977 f = frag_more (4);
1978 }
1979
1980 fixp[0] = fixp[1] = fixp[2] = NULL;
1981 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1982 {
1983 if (address_expr->X_op == O_constant)
1984 {
1985 valueT tmp;
1986
1987 switch (*reloc_type)
1988 {
1989 case BFD_RELOC_32:
1990 ip->insn_opcode |= address_expr->X_add_number;
1991 break;
1992
1993 case BFD_RELOC_MIPS_HIGHEST:
1994 tmp = (address_expr->X_add_number
1995 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
1996 tmp >>= 16;
1997 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1998 break;
1999
2000 case BFD_RELOC_MIPS_HIGHER:
2001 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2002 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2003 break;
2004
2005 case BFD_RELOC_HI16_S:
2006 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2007 >> 16) & 0xffff;
2008 break;
2009
2010 case BFD_RELOC_HI16:
2011 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2012 break;
2013
2014 case BFD_RELOC_LO16:
2015 case BFD_RELOC_MIPS_GOT_DISP:
2016 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2017 break;
2018
2019 case BFD_RELOC_MIPS_JMP:
2020 if ((address_expr->X_add_number & 3) != 0)
2021 as_bad (_("jump to misaligned address (0x%lx)"),
2022 (unsigned long) address_expr->X_add_number);
2023 if (address_expr->X_add_number & ~0xfffffff)
2024 as_bad (_("jump address range overflow (0x%lx)"),
2025 (unsigned long) address_expr->X_add_number);
2026 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2027 break;
2028
2029 case BFD_RELOC_MIPS16_JMP:
2030 if ((address_expr->X_add_number & 3) != 0)
2031 as_bad (_("jump to misaligned address (0x%lx)"),
2032 (unsigned long) address_expr->X_add_number);
2033 if (address_expr->X_add_number & ~0xfffffff)
2034 as_bad (_("jump address range overflow (0x%lx)"),
2035 (unsigned long) address_expr->X_add_number);
2036 ip->insn_opcode |=
2037 (((address_expr->X_add_number & 0x7c0000) << 3)
2038 | ((address_expr->X_add_number & 0xf800000) >> 7)
2039 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2040 break;
2041
2042 case BFD_RELOC_16_PCREL_S2:
2043 goto need_reloc;
2044
2045 default:
2046 internalError ();
2047 }
2048 }
2049 else
2050 {
2051 need_reloc:
2052 /* Don't generate a reloc if we are writing into a variant frag. */
2053 if (place == NULL)
2054 {
2055 reloc_howto_type *howto;
2056 int i;
2057
2058 /* In a compound relocation, it is the final (outermost)
2059 operator that determines the relocated field. */
2060 for (i = 1; i < 3; i++)
2061 if (reloc_type[i] == BFD_RELOC_UNUSED)
2062 break;
2063
2064 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2065 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2066 bfd_get_reloc_size(howto),
2067 address_expr,
2068 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2069 reloc_type[0]);
2070
2071 /* These relocations can have an addend that won't fit in
2072 4 octets for 64bit assembly. */
2073 if (HAVE_64BIT_GPRS
2074 && ! howto->partial_inplace
2075 && (reloc_type[0] == BFD_RELOC_16
2076 || reloc_type[0] == BFD_RELOC_32
2077 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2078 || reloc_type[0] == BFD_RELOC_HI16_S
2079 || reloc_type[0] == BFD_RELOC_LO16
2080 || reloc_type[0] == BFD_RELOC_GPREL16
2081 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2082 || reloc_type[0] == BFD_RELOC_GPREL32
2083 || reloc_type[0] == BFD_RELOC_64
2084 || reloc_type[0] == BFD_RELOC_CTOR
2085 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2086 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2087 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2088 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2089 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2090 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2091 fixp[0]->fx_no_overflow = 1;
2092
2093 if (reloc_needs_lo_p (*reloc_type))
2094 {
2095 struct mips_hi_fixup *hi_fixup;
2096
2097 /* Reuse the last entry if it already has a matching %lo. */
2098 hi_fixup = mips_hi_fixup_list;
2099 if (hi_fixup == 0
2100 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2101 {
2102 hi_fixup = ((struct mips_hi_fixup *)
2103 xmalloc (sizeof (struct mips_hi_fixup)));
2104 hi_fixup->next = mips_hi_fixup_list;
2105 mips_hi_fixup_list = hi_fixup;
2106 }
2107 hi_fixup->fixp = fixp[0];
2108 hi_fixup->seg = now_seg;
2109 }
2110
2111 /* Add fixups for the second and third relocations, if given.
2112 Note that the ABI allows the second relocation to be
2113 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2114 moment we only use RSS_UNDEF, but we could add support
2115 for the others if it ever becomes necessary. */
2116 for (i = 1; i < 3; i++)
2117 if (reloc_type[i] != BFD_RELOC_UNUSED)
2118 {
2119 address_expr->X_op = O_absent;
2120 address_expr->X_add_symbol = 0;
2121 address_expr->X_add_number = 0;
2122
2123 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2124 fixp[0]->fx_size, address_expr,
2125 FALSE, reloc_type[i]);
2126 }
2127 }
2128 }
2129 }
2130
2131 if (! mips_opts.mips16)
2132 {
2133 md_number_to_chars (f, ip->insn_opcode, 4);
2134 #ifdef OBJ_ELF
2135 dwarf2_emit_insn (4);
2136 #endif
2137 }
2138 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2139 {
2140 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2141 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2142 #ifdef OBJ_ELF
2143 dwarf2_emit_insn (4);
2144 #endif
2145 }
2146 else
2147 {
2148 if (ip->use_extend)
2149 {
2150 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2151 f += 2;
2152 }
2153 md_number_to_chars (f, ip->insn_opcode, 2);
2154 #ifdef OBJ_ELF
2155 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2156 #endif
2157 }
2158
2159 /* Update the register mask information. */
2160 if (! mips_opts.mips16)
2161 {
2162 if (pinfo & INSN_WRITE_GPR_D)
2163 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2164 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2165 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2166 if (pinfo & INSN_READ_GPR_S)
2167 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2168 if (pinfo & INSN_WRITE_GPR_31)
2169 mips_gprmask |= 1 << RA;
2170 if (pinfo & INSN_WRITE_FPR_D)
2171 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2172 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2173 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2174 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2175 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2176 if ((pinfo & INSN_READ_FPR_R) != 0)
2177 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2178 if (pinfo & INSN_COP)
2179 {
2180 /* We don't keep enough information to sort these cases out.
2181 The itbl support does keep this information however, although
2182 we currently don't support itbl fprmats as part of the cop
2183 instruction. May want to add this support in the future. */
2184 }
2185 /* Never set the bit for $0, which is always zero. */
2186 mips_gprmask &= ~1 << 0;
2187 }
2188 else
2189 {
2190 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2191 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2192 & MIPS16OP_MASK_RX);
2193 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2194 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2195 & MIPS16OP_MASK_RY);
2196 if (pinfo & MIPS16_INSN_WRITE_Z)
2197 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2198 & MIPS16OP_MASK_RZ);
2199 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2200 mips_gprmask |= 1 << TREG;
2201 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2202 mips_gprmask |= 1 << SP;
2203 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2204 mips_gprmask |= 1 << RA;
2205 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2206 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2207 if (pinfo & MIPS16_INSN_READ_Z)
2208 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2209 & MIPS16OP_MASK_MOVE32Z);
2210 if (pinfo & MIPS16_INSN_READ_GPR_X)
2211 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2212 & MIPS16OP_MASK_REGR32);
2213 }
2214
2215 if (place == NULL && ! mips_opts.noreorder)
2216 {
2217 /* Filling the branch delay slot is more complex. We try to
2218 switch the branch with the previous instruction, which we can
2219 do if the previous instruction does not set up a condition
2220 that the branch tests and if the branch is not itself the
2221 target of any branch. */
2222 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2223 || (pinfo & INSN_COND_BRANCH_DELAY))
2224 {
2225 if (mips_optimize < 2
2226 /* If we have seen .set volatile or .set nomove, don't
2227 optimize. */
2228 || mips_opts.nomove != 0
2229 /* If we had to emit any NOP instructions, then we
2230 already know we can not swap. */
2231 || nops != 0
2232 /* If we don't even know the previous insn, we can not
2233 swap. */
2234 || ! prev_insn_valid
2235 /* If the previous insn is already in a branch delay
2236 slot, then we can not swap. */
2237 || prev_insn_is_delay_slot
2238 /* If the previous previous insn was in a .set
2239 noreorder, we can't swap. Actually, the MIPS
2240 assembler will swap in this situation. However, gcc
2241 configured -with-gnu-as will generate code like
2242 .set noreorder
2243 lw $4,XXX
2244 .set reorder
2245 INSN
2246 bne $4,$0,foo
2247 in which we can not swap the bne and INSN. If gcc is
2248 not configured -with-gnu-as, it does not output the
2249 .set pseudo-ops. We don't have to check
2250 prev_insn_unreordered, because prev_insn_valid will
2251 be 0 in that case. We don't want to use
2252 prev_prev_insn_valid, because we do want to be able
2253 to swap at the start of a function. */
2254 || prev_prev_insn_unreordered
2255 /* If the branch is itself the target of a branch, we
2256 can not swap. We cheat on this; all we check for is
2257 whether there is a label on this instruction. If
2258 there are any branches to anything other than a
2259 label, users must use .set noreorder. */
2260 || insn_labels != NULL
2261 /* If the previous instruction is in a variant frag, we
2262 can not do the swap. This does not apply to the
2263 mips16, which uses variant frags for different
2264 purposes. */
2265 || (! mips_opts.mips16
2266 && prev_insn_frag->fr_type == rs_machine_dependent)
2267 /* If the branch reads the condition codes, we don't
2268 even try to swap, because in the sequence
2269 ctc1 $X,$31
2270 INSN
2271 INSN
2272 bc1t LABEL
2273 we can not swap, and I don't feel like handling that
2274 case. */
2275 || (! mips_opts.mips16
2276 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2277 && (pinfo & INSN_READ_COND_CODE))
2278 /* We can not swap with an instruction that requires a
2279 delay slot, becase the target of the branch might
2280 interfere with that instruction. */
2281 || (! mips_opts.mips16
2282 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2283 && (prev_pinfo
2284 /* Itbl support may require additional care here. */
2285 & (INSN_LOAD_COPROC_DELAY
2286 | INSN_COPROC_MOVE_DELAY
2287 | INSN_WRITE_COND_CODE)))
2288 || (! (hilo_interlocks
2289 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2290 && (prev_pinfo
2291 & (INSN_READ_LO
2292 | INSN_READ_HI)))
2293 || (! mips_opts.mips16
2294 && ! gpr_interlocks
2295 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2296 || (! mips_opts.mips16
2297 && mips_opts.isa == ISA_MIPS1
2298 /* Itbl support may require additional care here. */
2299 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2300 /* We can not swap with a branch instruction. */
2301 || (prev_pinfo
2302 & (INSN_UNCOND_BRANCH_DELAY
2303 | INSN_COND_BRANCH_DELAY
2304 | INSN_COND_BRANCH_LIKELY))
2305 /* We do not swap with a trap instruction, since it
2306 complicates trap handlers to have the trap
2307 instruction be in a delay slot. */
2308 || (prev_pinfo & INSN_TRAP)
2309 /* If the branch reads a register that the previous
2310 instruction sets, we can not swap. */
2311 || (! mips_opts.mips16
2312 && (prev_pinfo & INSN_WRITE_GPR_T)
2313 && insn_uses_reg (ip,
2314 ((prev_insn.insn_opcode >> OP_SH_RT)
2315 & OP_MASK_RT),
2316 MIPS_GR_REG))
2317 || (! mips_opts.mips16
2318 && (prev_pinfo & INSN_WRITE_GPR_D)
2319 && insn_uses_reg (ip,
2320 ((prev_insn.insn_opcode >> OP_SH_RD)
2321 & OP_MASK_RD),
2322 MIPS_GR_REG))
2323 || (mips_opts.mips16
2324 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2325 && insn_uses_reg (ip,
2326 ((prev_insn.insn_opcode
2327 >> MIPS16OP_SH_RX)
2328 & MIPS16OP_MASK_RX),
2329 MIPS16_REG))
2330 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2331 && insn_uses_reg (ip,
2332 ((prev_insn.insn_opcode
2333 >> MIPS16OP_SH_RY)
2334 & MIPS16OP_MASK_RY),
2335 MIPS16_REG))
2336 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2337 && insn_uses_reg (ip,
2338 ((prev_insn.insn_opcode
2339 >> MIPS16OP_SH_RZ)
2340 & MIPS16OP_MASK_RZ),
2341 MIPS16_REG))
2342 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2343 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2344 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2345 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2346 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2347 && insn_uses_reg (ip,
2348 MIPS16OP_EXTRACT_REG32R (prev_insn.
2349 insn_opcode),
2350 MIPS_GR_REG))))
2351 /* If the branch writes a register that the previous
2352 instruction sets, we can not swap (we know that
2353 branches write only to RD or to $31). */
2354 || (! mips_opts.mips16
2355 && (prev_pinfo & INSN_WRITE_GPR_T)
2356 && (((pinfo & INSN_WRITE_GPR_D)
2357 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2358 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2359 || ((pinfo & INSN_WRITE_GPR_31)
2360 && (((prev_insn.insn_opcode >> OP_SH_RT)
2361 & OP_MASK_RT)
2362 == RA))))
2363 || (! mips_opts.mips16
2364 && (prev_pinfo & INSN_WRITE_GPR_D)
2365 && (((pinfo & INSN_WRITE_GPR_D)
2366 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2367 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2368 || ((pinfo & INSN_WRITE_GPR_31)
2369 && (((prev_insn.insn_opcode >> OP_SH_RD)
2370 & OP_MASK_RD)
2371 == RA))))
2372 || (mips_opts.mips16
2373 && (pinfo & MIPS16_INSN_WRITE_31)
2374 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2375 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2376 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2377 == RA))))
2378 /* If the branch writes a register that the previous
2379 instruction reads, we can not swap (we know that
2380 branches only write to RD or to $31). */
2381 || (! mips_opts.mips16
2382 && (pinfo & INSN_WRITE_GPR_D)
2383 && insn_uses_reg (&prev_insn,
2384 ((ip->insn_opcode >> OP_SH_RD)
2385 & OP_MASK_RD),
2386 MIPS_GR_REG))
2387 || (! mips_opts.mips16
2388 && (pinfo & INSN_WRITE_GPR_31)
2389 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2390 || (mips_opts.mips16
2391 && (pinfo & MIPS16_INSN_WRITE_31)
2392 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2393 /* If we are generating embedded PIC code, the branch
2394 might be expanded into a sequence which uses $at, so
2395 we can't swap with an instruction which reads it. */
2396 || (mips_pic == EMBEDDED_PIC
2397 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2398 /* If the previous previous instruction has a load
2399 delay, and sets a register that the branch reads, we
2400 can not swap. */
2401 || (! mips_opts.mips16
2402 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2403 /* Itbl support may require additional care here. */
2404 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2405 || (! gpr_interlocks
2406 && (prev_prev_insn.insn_mo->pinfo
2407 & INSN_LOAD_MEMORY_DELAY)))
2408 && insn_uses_reg (ip,
2409 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2410 & OP_MASK_RT),
2411 MIPS_GR_REG))
2412 /* If one instruction sets a condition code and the
2413 other one uses a condition code, we can not swap. */
2414 || ((pinfo & INSN_READ_COND_CODE)
2415 && (prev_pinfo & INSN_WRITE_COND_CODE))
2416 || ((pinfo & INSN_WRITE_COND_CODE)
2417 && (prev_pinfo & INSN_READ_COND_CODE))
2418 /* If the previous instruction uses the PC, we can not
2419 swap. */
2420 || (mips_opts.mips16
2421 && (prev_pinfo & MIPS16_INSN_READ_PC))
2422 /* If the previous instruction was extended, we can not
2423 swap. */
2424 || (mips_opts.mips16 && prev_insn_extended)
2425 /* If the previous instruction had a fixup in mips16
2426 mode, we can not swap. This normally means that the
2427 previous instruction was a 4 byte branch anyhow. */
2428 || (mips_opts.mips16 && prev_insn_fixp[0])
2429 /* If the previous instruction is a sync, sync.l, or
2430 sync.p, we can not swap. */
2431 || (prev_pinfo & INSN_SYNC))
2432 {
2433 /* We could do even better for unconditional branches to
2434 portions of this object file; we could pick up the
2435 instruction at the destination, put it in the delay
2436 slot, and bump the destination address. */
2437 emit_nop ();
2438 /* Update the previous insn information. */
2439 prev_prev_insn = *ip;
2440 prev_insn.insn_mo = &dummy_opcode;
2441 }
2442 else
2443 {
2444 /* It looks like we can actually do the swap. */
2445 if (! mips_opts.mips16)
2446 {
2447 char *prev_f;
2448 char temp[4];
2449
2450 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2451 memcpy (temp, prev_f, 4);
2452 memcpy (prev_f, f, 4);
2453 memcpy (f, temp, 4);
2454 if (prev_insn_fixp[0])
2455 {
2456 prev_insn_fixp[0]->fx_frag = frag_now;
2457 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2458 }
2459 if (prev_insn_fixp[1])
2460 {
2461 prev_insn_fixp[1]->fx_frag = frag_now;
2462 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2463 }
2464 if (prev_insn_fixp[2])
2465 {
2466 prev_insn_fixp[2]->fx_frag = frag_now;
2467 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2468 }
2469 if (prev_insn_fixp[0] && HAVE_NEWABI
2470 && prev_insn_frag != frag_now
2471 && (prev_insn_fixp[0]->fx_r_type
2472 == BFD_RELOC_MIPS_GOT_DISP
2473 || (prev_insn_fixp[0]->fx_r_type
2474 == BFD_RELOC_MIPS_CALL16)))
2475 {
2476 /* To avoid confusion in tc_gen_reloc, we must
2477 ensure that this does not become a variant
2478 frag. */
2479 force_new_frag = TRUE;
2480 }
2481 if (fixp[0])
2482 {
2483 fixp[0]->fx_frag = prev_insn_frag;
2484 fixp[0]->fx_where = prev_insn_where;
2485 }
2486 if (fixp[1])
2487 {
2488 fixp[1]->fx_frag = prev_insn_frag;
2489 fixp[1]->fx_where = prev_insn_where;
2490 }
2491 if (fixp[2])
2492 {
2493 fixp[2]->fx_frag = prev_insn_frag;
2494 fixp[2]->fx_where = prev_insn_where;
2495 }
2496 }
2497 else
2498 {
2499 char *prev_f;
2500 char temp[2];
2501
2502 assert (prev_insn_fixp[0] == NULL);
2503 assert (prev_insn_fixp[1] == NULL);
2504 assert (prev_insn_fixp[2] == NULL);
2505 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2506 memcpy (temp, prev_f, 2);
2507 memcpy (prev_f, f, 2);
2508 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2509 {
2510 assert (*reloc_type == BFD_RELOC_UNUSED);
2511 memcpy (f, temp, 2);
2512 }
2513 else
2514 {
2515 memcpy (f, f + 2, 2);
2516 memcpy (f + 2, temp, 2);
2517 }
2518 if (fixp[0])
2519 {
2520 fixp[0]->fx_frag = prev_insn_frag;
2521 fixp[0]->fx_where = prev_insn_where;
2522 }
2523 if (fixp[1])
2524 {
2525 fixp[1]->fx_frag = prev_insn_frag;
2526 fixp[1]->fx_where = prev_insn_where;
2527 }
2528 if (fixp[2])
2529 {
2530 fixp[2]->fx_frag = prev_insn_frag;
2531 fixp[2]->fx_where = prev_insn_where;
2532 }
2533 }
2534
2535 /* Update the previous insn information; leave prev_insn
2536 unchanged. */
2537 prev_prev_insn = *ip;
2538 }
2539 prev_insn_is_delay_slot = 1;
2540
2541 /* If that was an unconditional branch, forget the previous
2542 insn information. */
2543 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2544 {
2545 prev_prev_insn.insn_mo = &dummy_opcode;
2546 prev_insn.insn_mo = &dummy_opcode;
2547 }
2548
2549 prev_insn_fixp[0] = NULL;
2550 prev_insn_fixp[1] = NULL;
2551 prev_insn_fixp[2] = NULL;
2552 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2553 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2554 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2555 prev_insn_extended = 0;
2556 }
2557 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2558 {
2559 /* We don't yet optimize a branch likely. What we should do
2560 is look at the target, copy the instruction found there
2561 into the delay slot, and increment the branch to jump to
2562 the next instruction. */
2563 emit_nop ();
2564 /* Update the previous insn information. */
2565 prev_prev_insn = *ip;
2566 prev_insn.insn_mo = &dummy_opcode;
2567 prev_insn_fixp[0] = NULL;
2568 prev_insn_fixp[1] = NULL;
2569 prev_insn_fixp[2] = NULL;
2570 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2571 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2572 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2573 prev_insn_extended = 0;
2574 }
2575 else
2576 {
2577 /* Update the previous insn information. */
2578 if (nops > 0)
2579 prev_prev_insn.insn_mo = &dummy_opcode;
2580 else
2581 prev_prev_insn = prev_insn;
2582 prev_insn = *ip;
2583
2584 /* Any time we see a branch, we always fill the delay slot
2585 immediately; since this insn is not a branch, we know it
2586 is not in a delay slot. */
2587 prev_insn_is_delay_slot = 0;
2588
2589 prev_insn_fixp[0] = fixp[0];
2590 prev_insn_fixp[1] = fixp[1];
2591 prev_insn_fixp[2] = fixp[2];
2592 prev_insn_reloc_type[0] = reloc_type[0];
2593 prev_insn_reloc_type[1] = reloc_type[1];
2594 prev_insn_reloc_type[2] = reloc_type[2];
2595 if (mips_opts.mips16)
2596 prev_insn_extended = (ip->use_extend
2597 || *reloc_type > BFD_RELOC_UNUSED);
2598 }
2599
2600 prev_prev_insn_unreordered = prev_insn_unreordered;
2601 prev_insn_unreordered = 0;
2602 prev_insn_frag = frag_now;
2603 prev_insn_where = f - frag_now->fr_literal;
2604 prev_insn_valid = 1;
2605 }
2606 else if (place == NULL)
2607 {
2608 /* We need to record a bit of information even when we are not
2609 reordering, in order to determine the base address for mips16
2610 PC relative relocs. */
2611 prev_prev_insn = prev_insn;
2612 prev_insn = *ip;
2613 prev_insn_reloc_type[0] = reloc_type[0];
2614 prev_insn_reloc_type[1] = reloc_type[1];
2615 prev_insn_reloc_type[2] = reloc_type[2];
2616 prev_prev_insn_unreordered = prev_insn_unreordered;
2617 prev_insn_unreordered = 1;
2618 }
2619
2620 /* We just output an insn, so the next one doesn't have a label. */
2621 mips_clear_insn_labels ();
2622
2623 /* We must ensure that the frag to which an instruction that was
2624 moved from a non-variant frag doesn't become a variant frag,
2625 otherwise tc_gen_reloc may get confused. */
2626 if (force_new_frag)
2627 {
2628 frag_wane (frag_now);
2629 frag_new (0);
2630 }
2631 }
2632
2633 /* This function forgets that there was any previous instruction or
2634 label. If PRESERVE is non-zero, it remembers enough information to
2635 know whether nops are needed before a noreorder section. */
2636
2637 static void
2638 mips_no_prev_insn (int preserve)
2639 {
2640 if (! preserve)
2641 {
2642 prev_insn.insn_mo = &dummy_opcode;
2643 prev_prev_insn.insn_mo = &dummy_opcode;
2644 prev_nop_frag = NULL;
2645 prev_nop_frag_holds = 0;
2646 prev_nop_frag_required = 0;
2647 prev_nop_frag_since = 0;
2648 }
2649 prev_insn_valid = 0;
2650 prev_insn_is_delay_slot = 0;
2651 prev_insn_unreordered = 0;
2652 prev_insn_extended = 0;
2653 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2654 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2655 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2656 prev_prev_insn_unreordered = 0;
2657 mips_clear_insn_labels ();
2658 }
2659
2660 /* This function must be called whenever we turn on noreorder or emit
2661 something other than instructions. It inserts any NOPS which might
2662 be needed by the previous instruction, and clears the information
2663 kept for the previous instructions. The INSNS parameter is true if
2664 instructions are to follow. */
2665
2666 static void
2667 mips_emit_delays (bfd_boolean insns)
2668 {
2669 if (! mips_opts.noreorder)
2670 {
2671 int nops;
2672
2673 nops = 0;
2674 if ((! mips_opts.mips16
2675 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2676 && (! cop_interlocks
2677 && (prev_insn.insn_mo->pinfo
2678 & (INSN_LOAD_COPROC_DELAY
2679 | INSN_COPROC_MOVE_DELAY
2680 | INSN_WRITE_COND_CODE))))
2681 || (! hilo_interlocks
2682 && (prev_insn.insn_mo->pinfo
2683 & (INSN_READ_LO
2684 | INSN_READ_HI)))
2685 || (! mips_opts.mips16
2686 && ! gpr_interlocks
2687 && (prev_insn.insn_mo->pinfo
2688 & INSN_LOAD_MEMORY_DELAY))
2689 || (! mips_opts.mips16
2690 && mips_opts.isa == ISA_MIPS1
2691 && (prev_insn.insn_mo->pinfo
2692 & INSN_COPROC_MEMORY_DELAY)))
2693 {
2694 /* Itbl support may require additional care here. */
2695 ++nops;
2696 if ((! mips_opts.mips16
2697 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2698 && (! cop_interlocks
2699 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2700 || (! hilo_interlocks
2701 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2702 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2703 ++nops;
2704
2705 if (prev_insn_unreordered)
2706 nops = 0;
2707 }
2708 else if ((! mips_opts.mips16
2709 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2710 && (! cop_interlocks
2711 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2712 || (! hilo_interlocks
2713 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2714 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2715 {
2716 /* Itbl support may require additional care here. */
2717 if (! prev_prev_insn_unreordered)
2718 ++nops;
2719 }
2720
2721 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2722 {
2723 int min_nops = 0;
2724 const char *pn = prev_insn.insn_mo->name;
2725 if (strncmp(pn, "macc", 4) == 0
2726 || strncmp(pn, "dmacc", 5) == 0
2727 || strncmp(pn, "dmult", 5) == 0)
2728 {
2729 min_nops = 1;
2730 }
2731 if (nops < min_nops)
2732 nops = min_nops;
2733 }
2734
2735 if (nops > 0)
2736 {
2737 struct insn_label_list *l;
2738
2739 if (insns)
2740 {
2741 /* Record the frag which holds the nop instructions, so
2742 that we can remove them if we don't need them. */
2743 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2744 prev_nop_frag = frag_now;
2745 prev_nop_frag_holds = nops;
2746 prev_nop_frag_required = 0;
2747 prev_nop_frag_since = 0;
2748 }
2749
2750 for (; nops > 0; --nops)
2751 emit_nop ();
2752
2753 if (insns)
2754 {
2755 /* Move on to a new frag, so that it is safe to simply
2756 decrease the size of prev_nop_frag. */
2757 frag_wane (frag_now);
2758 frag_new (0);
2759 }
2760
2761 for (l = insn_labels; l != NULL; l = l->next)
2762 {
2763 valueT val;
2764
2765 assert (S_GET_SEGMENT (l->label) == now_seg);
2766 symbol_set_frag (l->label, frag_now);
2767 val = (valueT) frag_now_fix ();
2768 /* mips16 text labels are stored as odd. */
2769 if (mips_opts.mips16)
2770 ++val;
2771 S_SET_VALUE (l->label, val);
2772 }
2773 }
2774 }
2775
2776 /* Mark instruction labels in mips16 mode. */
2777 if (insns)
2778 mips16_mark_labels ();
2779
2780 mips_no_prev_insn (insns);
2781 }
2782
2783 /* Build an instruction created by a macro expansion. This is passed
2784 a pointer to the count of instructions created so far, an
2785 expression, the name of the instruction to build, an operand format
2786 string, and corresponding arguments. */
2787
2788 static void
2789 macro_build (char *place, int *counter, expressionS *ep, const char *name,
2790 const char *fmt, ...)
2791 {
2792 struct mips_cl_insn insn;
2793 bfd_reloc_code_real_type r[3];
2794 va_list args;
2795
2796 va_start (args, fmt);
2797
2798 /*
2799 * If the macro is about to expand into a second instruction,
2800 * print a warning if needed. We need to pass ip as a parameter
2801 * to generate a better warning message here...
2802 */
2803 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2804 as_warn (_("Macro instruction expanded into multiple instructions"));
2805
2806 /*
2807 * If the macro is about to expand into a second instruction,
2808 * and it is in a delay slot, print a warning.
2809 */
2810 if (place == NULL
2811 && *counter == 1
2812 && mips_opts.noreorder
2813 && (prev_prev_insn.insn_mo->pinfo
2814 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2815 | INSN_COND_BRANCH_LIKELY)) != 0)
2816 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2817
2818 if (place == NULL)
2819 ++*counter; /* bump instruction counter */
2820
2821 if (mips_opts.mips16)
2822 {
2823 mips16_macro_build (place, counter, ep, name, fmt, args);
2824 va_end (args);
2825 return;
2826 }
2827
2828 r[0] = BFD_RELOC_UNUSED;
2829 r[1] = BFD_RELOC_UNUSED;
2830 r[2] = BFD_RELOC_UNUSED;
2831 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2832 assert (insn.insn_mo);
2833 assert (strcmp (name, insn.insn_mo->name) == 0);
2834
2835 /* Search until we get a match for NAME. */
2836 while (1)
2837 {
2838 /* It is assumed here that macros will never generate
2839 MDMX or MIPS-3D instructions. */
2840 if (strcmp (fmt, insn.insn_mo->args) == 0
2841 && insn.insn_mo->pinfo != INSN_MACRO
2842 && OPCODE_IS_MEMBER (insn.insn_mo,
2843 (mips_opts.isa
2844 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2845 mips_opts.arch)
2846 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2847 break;
2848
2849 ++insn.insn_mo;
2850 assert (insn.insn_mo->name);
2851 assert (strcmp (name, insn.insn_mo->name) == 0);
2852 }
2853
2854 insn.insn_opcode = insn.insn_mo->match;
2855 for (;;)
2856 {
2857 switch (*fmt++)
2858 {
2859 case '\0':
2860 break;
2861
2862 case ',':
2863 case '(':
2864 case ')':
2865 continue;
2866
2867 case 't':
2868 case 'w':
2869 case 'E':
2870 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2871 continue;
2872
2873 case 'c':
2874 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2875 continue;
2876
2877 case 'T':
2878 case 'W':
2879 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2880 continue;
2881
2882 case 'd':
2883 case 'G':
2884 case 'K':
2885 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2886 continue;
2887
2888 case 'U':
2889 {
2890 int tmp = va_arg (args, int);
2891
2892 insn.insn_opcode |= tmp << OP_SH_RT;
2893 insn.insn_opcode |= tmp << OP_SH_RD;
2894 continue;
2895 }
2896
2897 case 'V':
2898 case 'S':
2899 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2900 continue;
2901
2902 case 'z':
2903 continue;
2904
2905 case '<':
2906 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2907 continue;
2908
2909 case 'D':
2910 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2911 continue;
2912
2913 case 'B':
2914 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2915 continue;
2916
2917 case 'J':
2918 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2919 continue;
2920
2921 case 'q':
2922 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2923 continue;
2924
2925 case 'b':
2926 case 's':
2927 case 'r':
2928 case 'v':
2929 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2930 continue;
2931
2932 case 'i':
2933 case 'j':
2934 case 'o':
2935 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2936 assert (*r == BFD_RELOC_GPREL16
2937 || *r == BFD_RELOC_MIPS_LITERAL
2938 || *r == BFD_RELOC_MIPS_HIGHER
2939 || *r == BFD_RELOC_HI16_S
2940 || *r == BFD_RELOC_LO16
2941 || *r == BFD_RELOC_MIPS_GOT16
2942 || *r == BFD_RELOC_MIPS_CALL16
2943 || *r == BFD_RELOC_MIPS_GOT_DISP
2944 || *r == BFD_RELOC_MIPS_GOT_PAGE
2945 || *r == BFD_RELOC_MIPS_GOT_OFST
2946 || *r == BFD_RELOC_MIPS_GOT_LO16
2947 || *r == BFD_RELOC_MIPS_CALL_LO16
2948 || (ep->X_op == O_subtract
2949 && *r == BFD_RELOC_PCREL_LO16));
2950 continue;
2951
2952 case 'u':
2953 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2954 assert (ep != NULL
2955 && (ep->X_op == O_constant
2956 || (ep->X_op == O_symbol
2957 && (*r == BFD_RELOC_MIPS_HIGHEST
2958 || *r == BFD_RELOC_HI16_S
2959 || *r == BFD_RELOC_HI16
2960 || *r == BFD_RELOC_GPREL16
2961 || *r == BFD_RELOC_MIPS_GOT_HI16
2962 || *r == BFD_RELOC_MIPS_CALL_HI16))
2963 || (ep->X_op == O_subtract
2964 && *r == BFD_RELOC_PCREL_HI16_S)));
2965 continue;
2966
2967 case 'p':
2968 assert (ep != NULL);
2969 /*
2970 * This allows macro() to pass an immediate expression for
2971 * creating short branches without creating a symbol.
2972 * Note that the expression still might come from the assembly
2973 * input, in which case the value is not checked for range nor
2974 * is a relocation entry generated (yuck).
2975 */
2976 if (ep->X_op == O_constant)
2977 {
2978 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2979 ep = NULL;
2980 }
2981 else
2982 *r = BFD_RELOC_16_PCREL_S2;
2983 continue;
2984
2985 case 'a':
2986 assert (ep != NULL);
2987 *r = BFD_RELOC_MIPS_JMP;
2988 continue;
2989
2990 case 'C':
2991 insn.insn_opcode |= va_arg (args, unsigned long);
2992 continue;
2993
2994 default:
2995 internalError ();
2996 }
2997 break;
2998 }
2999 va_end (args);
3000 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3001
3002 append_insn (place, &insn, ep, r);
3003 }
3004
3005 static void
3006 mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3007 expressionS *ep, const char *name, const char *fmt,
3008 va_list args)
3009 {
3010 struct mips_cl_insn insn;
3011 bfd_reloc_code_real_type r[3]
3012 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3013
3014 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3015 assert (insn.insn_mo);
3016 assert (strcmp (name, insn.insn_mo->name) == 0);
3017
3018 while (strcmp (fmt, insn.insn_mo->args) != 0
3019 || insn.insn_mo->pinfo == INSN_MACRO)
3020 {
3021 ++insn.insn_mo;
3022 assert (insn.insn_mo->name);
3023 assert (strcmp (name, insn.insn_mo->name) == 0);
3024 }
3025
3026 insn.insn_opcode = insn.insn_mo->match;
3027 insn.use_extend = FALSE;
3028
3029 for (;;)
3030 {
3031 int c;
3032
3033 c = *fmt++;
3034 switch (c)
3035 {
3036 case '\0':
3037 break;
3038
3039 case ',':
3040 case '(':
3041 case ')':
3042 continue;
3043
3044 case 'y':
3045 case 'w':
3046 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3047 continue;
3048
3049 case 'x':
3050 case 'v':
3051 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3052 continue;
3053
3054 case 'z':
3055 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3056 continue;
3057
3058 case 'Z':
3059 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3060 continue;
3061
3062 case '0':
3063 case 'S':
3064 case 'P':
3065 case 'R':
3066 continue;
3067
3068 case 'X':
3069 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3070 continue;
3071
3072 case 'Y':
3073 {
3074 int regno;
3075
3076 regno = va_arg (args, int);
3077 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3078 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3079 }
3080 continue;
3081
3082 case '<':
3083 case '>':
3084 case '4':
3085 case '5':
3086 case 'H':
3087 case 'W':
3088 case 'D':
3089 case 'j':
3090 case '8':
3091 case 'V':
3092 case 'C':
3093 case 'U':
3094 case 'k':
3095 case 'K':
3096 case 'p':
3097 case 'q':
3098 {
3099 assert (ep != NULL);
3100
3101 if (ep->X_op != O_constant)
3102 *r = (int) BFD_RELOC_UNUSED + c;
3103 else
3104 {
3105 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3106 FALSE, &insn.insn_opcode, &insn.use_extend,
3107 &insn.extend);
3108 ep = NULL;
3109 *r = BFD_RELOC_UNUSED;
3110 }
3111 }
3112 continue;
3113
3114 case '6':
3115 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3116 continue;
3117 }
3118
3119 break;
3120 }
3121
3122 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3123
3124 append_insn (place, &insn, ep, r);
3125 }
3126
3127 /*
3128 * Generate a "jalr" instruction with a relocation hint to the called
3129 * function. This occurs in NewABI PIC code.
3130 */
3131 static void
3132 macro_build_jalr (int icnt, expressionS *ep)
3133 {
3134 char *f = NULL;
3135
3136 if (HAVE_NEWABI)
3137 {
3138 frag_grow (4);
3139 f = frag_more (0);
3140 }
3141 macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3142 if (HAVE_NEWABI)
3143 fix_new_exp (frag_now, f - frag_now->fr_literal,
3144 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3145 }
3146
3147 /*
3148 * Generate a "lui" instruction.
3149 */
3150 static void
3151 macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
3152 {
3153 expressionS high_expr;
3154 struct mips_cl_insn insn;
3155 bfd_reloc_code_real_type r[3]
3156 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3157 const char *name = "lui";
3158 const char *fmt = "t,u";
3159
3160 assert (! mips_opts.mips16);
3161
3162 if (place == NULL)
3163 high_expr = *ep;
3164 else
3165 {
3166 high_expr.X_op = O_constant;
3167 high_expr.X_add_number = ep->X_add_number;
3168 }
3169
3170 if (high_expr.X_op == O_constant)
3171 {
3172 /* we can compute the instruction now without a relocation entry */
3173 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3174 >> 16) & 0xffff;
3175 *r = BFD_RELOC_UNUSED;
3176 }
3177 else
3178 {
3179 assert (ep->X_op == O_symbol);
3180 /* _gp_disp is a special case, used from s_cpload. */
3181 assert (mips_pic == NO_PIC
3182 || (! HAVE_NEWABI
3183 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3184 *r = BFD_RELOC_HI16_S;
3185 }
3186
3187 /*
3188 * If the macro is about to expand into a second instruction,
3189 * print a warning if needed. We need to pass ip as a parameter
3190 * to generate a better warning message here...
3191 */
3192 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3193 as_warn (_("Macro instruction expanded into multiple instructions"));
3194
3195 if (place == NULL)
3196 ++*counter; /* bump instruction counter */
3197
3198 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3199 assert (insn.insn_mo);
3200 assert (strcmp (name, insn.insn_mo->name) == 0);
3201 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3202
3203 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3204 if (*r == BFD_RELOC_UNUSED)
3205 {
3206 insn.insn_opcode |= high_expr.X_add_number;
3207 append_insn (place, &insn, NULL, r);
3208 }
3209 else
3210 append_insn (place, &insn, &high_expr, r);
3211 }
3212
3213 /* Generate a sequence of instructions to do a load or store from a constant
3214 offset off of a base register (breg) into/from a target register (treg),
3215 using AT if necessary. */
3216 static void
3217 macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
3218 const char *op, int treg, int breg)
3219 {
3220 assert (ep->X_op == O_constant);
3221
3222 /* Right now, this routine can only handle signed 32-bit contants. */
3223 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3224 as_warn (_("operand overflow"));
3225
3226 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3227 {
3228 /* Signed 16-bit offset will fit in the op. Easy! */
3229 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3230 breg);
3231 }
3232 else
3233 {
3234 /* 32-bit offset, need multiple instructions and AT, like:
3235 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3236 addu $tempreg,$tempreg,$breg
3237 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3238 to handle the complete offset. */
3239 macro_build_lui (place, counter, ep, AT);
3240 if (place != NULL)
3241 place += 4;
3242 macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3243 breg);
3244 if (place != NULL)
3245 place += 4;
3246 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3247 AT);
3248
3249 if (mips_opts.noat)
3250 as_warn (_("Macro used $at after \".set noat\""));
3251 }
3252 }
3253
3254 /* set_at()
3255 * Generates code to set the $at register to true (one)
3256 * if reg is less than the immediate expression.
3257 */
3258 static void
3259 set_at (int *counter, int reg, int unsignedp)
3260 {
3261 if (imm_expr.X_op == O_constant
3262 && imm_expr.X_add_number >= -0x8000
3263 && imm_expr.X_add_number < 0x8000)
3264 macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3265 "t,r,j", AT, reg, BFD_RELOC_LO16);
3266 else
3267 {
3268 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3269 macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
3270 "d,v,t", AT, reg, AT);
3271 }
3272 }
3273
3274 /* Warn if an expression is not a constant. */
3275
3276 static void
3277 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3278 {
3279 if (ex->X_op == O_big)
3280 as_bad (_("unsupported large constant"));
3281 else if (ex->X_op != O_constant)
3282 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3283 }
3284
3285 /* Count the leading zeroes by performing a binary chop. This is a
3286 bulky bit of source, but performance is a LOT better for the
3287 majority of values than a simple loop to count the bits:
3288 for (lcnt = 0; (lcnt < 32); lcnt++)
3289 if ((v) & (1 << (31 - lcnt)))
3290 break;
3291 However it is not code size friendly, and the gain will drop a bit
3292 on certain cached systems.
3293 */
3294 #define COUNT_TOP_ZEROES(v) \
3295 (((v) & ~0xffff) == 0 \
3296 ? ((v) & ~0xff) == 0 \
3297 ? ((v) & ~0xf) == 0 \
3298 ? ((v) & ~0x3) == 0 \
3299 ? ((v) & ~0x1) == 0 \
3300 ? !(v) \
3301 ? 32 \
3302 : 31 \
3303 : 30 \
3304 : ((v) & ~0x7) == 0 \
3305 ? 29 \
3306 : 28 \
3307 : ((v) & ~0x3f) == 0 \
3308 ? ((v) & ~0x1f) == 0 \
3309 ? 27 \
3310 : 26 \
3311 : ((v) & ~0x7f) == 0 \
3312 ? 25 \
3313 : 24 \
3314 : ((v) & ~0xfff) == 0 \
3315 ? ((v) & ~0x3ff) == 0 \
3316 ? ((v) & ~0x1ff) == 0 \
3317 ? 23 \
3318 : 22 \
3319 : ((v) & ~0x7ff) == 0 \
3320 ? 21 \
3321 : 20 \
3322 : ((v) & ~0x3fff) == 0 \
3323 ? ((v) & ~0x1fff) == 0 \
3324 ? 19 \
3325 : 18 \
3326 : ((v) & ~0x7fff) == 0 \
3327 ? 17 \
3328 : 16 \
3329 : ((v) & ~0xffffff) == 0 \
3330 ? ((v) & ~0xfffff) == 0 \
3331 ? ((v) & ~0x3ffff) == 0 \
3332 ? ((v) & ~0x1ffff) == 0 \
3333 ? 15 \
3334 : 14 \
3335 : ((v) & ~0x7ffff) == 0 \
3336 ? 13 \
3337 : 12 \
3338 : ((v) & ~0x3fffff) == 0 \
3339 ? ((v) & ~0x1fffff) == 0 \
3340 ? 11 \
3341 : 10 \
3342 : ((v) & ~0x7fffff) == 0 \
3343 ? 9 \
3344 : 8 \
3345 : ((v) & ~0xfffffff) == 0 \
3346 ? ((v) & ~0x3ffffff) == 0 \
3347 ? ((v) & ~0x1ffffff) == 0 \
3348 ? 7 \
3349 : 6 \
3350 : ((v) & ~0x7ffffff) == 0 \
3351 ? 5 \
3352 : 4 \
3353 : ((v) & ~0x3fffffff) == 0 \
3354 ? ((v) & ~0x1fffffff) == 0 \
3355 ? 3 \
3356 : 2 \
3357 : ((v) & ~0x7fffffff) == 0 \
3358 ? 1 \
3359 : 0)
3360
3361 /* load_register()
3362 * This routine generates the least number of instructions neccessary to load
3363 * an absolute expression value into a register.
3364 */
3365 static void
3366 load_register (int *counter, int reg, expressionS *ep, int dbl)
3367 {
3368 int freg;
3369 expressionS hi32, lo32;
3370
3371 if (ep->X_op != O_big)
3372 {
3373 assert (ep->X_op == O_constant);
3374 if (ep->X_add_number < 0x8000
3375 && (ep->X_add_number >= 0
3376 || (ep->X_add_number >= -0x8000
3377 && (! dbl
3378 || ! ep->X_unsigned
3379 || sizeof (ep->X_add_number) > 4))))
3380 {
3381 /* We can handle 16 bit signed values with an addiu to
3382 $zero. No need to ever use daddiu here, since $zero and
3383 the result are always correct in 32 bit mode. */
3384 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3385 BFD_RELOC_LO16);
3386 return;
3387 }
3388 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3389 {
3390 /* We can handle 16 bit unsigned values with an ori to
3391 $zero. */
3392 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3393 BFD_RELOC_LO16);
3394 return;
3395 }
3396 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3397 && (! dbl
3398 || ! ep->X_unsigned
3399 || sizeof (ep->X_add_number) > 4
3400 || (ep->X_add_number & 0x80000000) == 0))
3401 || ((HAVE_32BIT_GPRS || ! dbl)
3402 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3403 || (HAVE_32BIT_GPRS
3404 && ! dbl
3405 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3406 == ~ (offsetT) 0xffffffff)))
3407 {
3408 /* 32 bit values require an lui. */
3409 macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3410 if ((ep->X_add_number & 0xffff) != 0)
3411 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3412 BFD_RELOC_LO16);
3413 return;
3414 }
3415 }
3416
3417 /* The value is larger than 32 bits. */
3418
3419 if (HAVE_32BIT_GPRS)
3420 {
3421 as_bad (_("Number (0x%lx) larger than 32 bits"),
3422 (unsigned long) ep->X_add_number);
3423 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3424 BFD_RELOC_LO16);
3425 return;
3426 }
3427
3428 if (ep->X_op != O_big)
3429 {
3430 hi32 = *ep;
3431 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3432 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3433 hi32.X_add_number &= 0xffffffff;
3434 lo32 = *ep;
3435 lo32.X_add_number &= 0xffffffff;
3436 }
3437 else
3438 {
3439 assert (ep->X_add_number > 2);
3440 if (ep->X_add_number == 3)
3441 generic_bignum[3] = 0;
3442 else if (ep->X_add_number > 4)
3443 as_bad (_("Number larger than 64 bits"));
3444 lo32.X_op = O_constant;
3445 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3446 hi32.X_op = O_constant;
3447 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3448 }
3449
3450 if (hi32.X_add_number == 0)
3451 freg = 0;
3452 else
3453 {
3454 int shift, bit;
3455 unsigned long hi, lo;
3456
3457 if (hi32.X_add_number == (offsetT) 0xffffffff)
3458 {
3459 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3460 {
3461 macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3462 BFD_RELOC_LO16);
3463 return;
3464 }
3465 if (lo32.X_add_number & 0x80000000)
3466 {
3467 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3468 BFD_RELOC_HI16);
3469 if (lo32.X_add_number & 0xffff)
3470 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3471 BFD_RELOC_LO16);
3472 return;
3473 }
3474 }
3475
3476 /* Check for 16bit shifted constant. We know that hi32 is
3477 non-zero, so start the mask on the first bit of the hi32
3478 value. */
3479 shift = 17;
3480 do
3481 {
3482 unsigned long himask, lomask;
3483
3484 if (shift < 32)
3485 {
3486 himask = 0xffff >> (32 - shift);
3487 lomask = (0xffff << shift) & 0xffffffff;
3488 }
3489 else
3490 {
3491 himask = 0xffff << (shift - 32);
3492 lomask = 0;
3493 }
3494 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3495 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3496 {
3497 expressionS tmp;
3498
3499 tmp.X_op = O_constant;
3500 if (shift < 32)
3501 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3502 | (lo32.X_add_number >> shift));
3503 else
3504 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3505 macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3506 BFD_RELOC_LO16);
3507 macro_build (NULL, counter, NULL,
3508 (shift >= 32) ? "dsll32" : "dsll",
3509 "d,w,<", reg, reg,
3510 (shift >= 32) ? shift - 32 : shift);
3511 return;
3512 }
3513 ++shift;
3514 }
3515 while (shift <= (64 - 16));
3516
3517 /* Find the bit number of the lowest one bit, and store the
3518 shifted value in hi/lo. */
3519 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3520 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3521 if (lo != 0)
3522 {
3523 bit = 0;
3524 while ((lo & 1) == 0)
3525 {
3526 lo >>= 1;
3527 ++bit;
3528 }
3529 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3530 hi >>= bit;
3531 }
3532 else
3533 {
3534 bit = 32;
3535 while ((hi & 1) == 0)
3536 {
3537 hi >>= 1;
3538 ++bit;
3539 }
3540 lo = hi;
3541 hi = 0;
3542 }
3543
3544 /* Optimize if the shifted value is a (power of 2) - 1. */
3545 if ((hi == 0 && ((lo + 1) & lo) == 0)
3546 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3547 {
3548 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3549 if (shift != 0)
3550 {
3551 expressionS tmp;
3552
3553 /* This instruction will set the register to be all
3554 ones. */
3555 tmp.X_op = O_constant;
3556 tmp.X_add_number = (offsetT) -1;
3557 macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3558 BFD_RELOC_LO16);
3559 if (bit != 0)
3560 {
3561 bit += shift;
3562 macro_build (NULL, counter, NULL,
3563 (bit >= 32) ? "dsll32" : "dsll",
3564 "d,w,<", reg, reg,
3565 (bit >= 32) ? bit - 32 : bit);
3566 }
3567 macro_build (NULL, counter, NULL,
3568 (shift >= 32) ? "dsrl32" : "dsrl",
3569 "d,w,<", reg, reg,
3570 (shift >= 32) ? shift - 32 : shift);
3571 return;
3572 }
3573 }
3574
3575 /* Sign extend hi32 before calling load_register, because we can
3576 generally get better code when we load a sign extended value. */
3577 if ((hi32.X_add_number & 0x80000000) != 0)
3578 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3579 load_register (counter, reg, &hi32, 0);
3580 freg = reg;
3581 }
3582 if ((lo32.X_add_number & 0xffff0000) == 0)
3583 {
3584 if (freg != 0)
3585 {
3586 macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
3587 freg = reg;
3588 }
3589 }
3590 else
3591 {
3592 expressionS mid16;
3593
3594 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3595 {
3596 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3597 BFD_RELOC_HI16);
3598 macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
3599 return;
3600 }
3601
3602 if (freg != 0)
3603 {
3604 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
3605 freg = reg;
3606 }
3607 mid16 = lo32;
3608 mid16.X_add_number >>= 16;
3609 macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3610 BFD_RELOC_LO16);
3611 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3612 freg = reg;
3613 }
3614 if ((lo32.X_add_number & 0xffff) != 0)
3615 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3616 BFD_RELOC_LO16);
3617 }
3618
3619 /* Load an address into a register. */
3620
3621 static void
3622 load_address (int *counter, int reg, expressionS *ep, int *used_at)
3623 {
3624 char *p = NULL;
3625
3626 if (ep->X_op != O_constant
3627 && ep->X_op != O_symbol)
3628 {
3629 as_bad (_("expression too complex"));
3630 ep->X_op = O_constant;
3631 }
3632
3633 if (ep->X_op == O_constant)
3634 {
3635 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3636 return;
3637 }
3638
3639 if (mips_pic == NO_PIC)
3640 {
3641 /* If this is a reference to a GP relative symbol, we want
3642 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3643 Otherwise we want
3644 lui $reg,<sym> (BFD_RELOC_HI16_S)
3645 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3646 If we have an addend, we always use the latter form.
3647
3648 With 64bit address space and a usable $at we want
3649 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3650 lui $at,<sym> (BFD_RELOC_HI16_S)
3651 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3652 daddiu $at,<sym> (BFD_RELOC_LO16)
3653 dsll32 $reg,0
3654 daddu $reg,$reg,$at
3655
3656 If $at is already in use, we use a path which is suboptimal
3657 on superscalar processors.
3658 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3659 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3660 dsll $reg,16
3661 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3662 dsll $reg,16
3663 daddiu $reg,<sym> (BFD_RELOC_LO16)
3664 */
3665 if (HAVE_64BIT_ADDRESSES)
3666 {
3667 /* We don't do GP optimization for now because RELAX_ENCODE can't
3668 hold the data for such large chunks. */
3669
3670 if (*used_at == 0 && ! mips_opts.noat)
3671 {
3672 macro_build (p, counter, ep, "lui", "t,u",
3673 reg, BFD_RELOC_MIPS_HIGHEST);
3674 macro_build (p, counter, ep, "lui", "t,u",
3675 AT, BFD_RELOC_HI16_S);
3676 macro_build (p, counter, ep, "daddiu", "t,r,j",
3677 reg, reg, BFD_RELOC_MIPS_HIGHER);
3678 macro_build (p, counter, ep, "daddiu", "t,r,j",
3679 AT, AT, BFD_RELOC_LO16);
3680 macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3681 macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
3682 *used_at = 1;
3683 }
3684 else
3685 {
3686 macro_build (p, counter, ep, "lui", "t,u",
3687 reg, BFD_RELOC_MIPS_HIGHEST);
3688 macro_build (p, counter, ep, "daddiu", "t,r,j",
3689 reg, reg, BFD_RELOC_MIPS_HIGHER);
3690 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3691 macro_build (p, counter, ep, "daddiu", "t,r,j",
3692 reg, reg, BFD_RELOC_HI16_S);
3693 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
3694 macro_build (p, counter, ep, "daddiu", "t,r,j",
3695 reg, reg, BFD_RELOC_LO16);
3696 }
3697 }
3698 else
3699 {
3700 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3701 && ! nopic_need_relax (ep->X_add_symbol, 1))
3702 {
3703 frag_grow (20);
3704 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3705 mips_gp_register, BFD_RELOC_GPREL16);
3706 p = frag_var (rs_machine_dependent, 8, 0,
3707 RELAX_ENCODE (4, 8, 0, 4, 0,
3708 mips_opts.warn_about_macros),
3709 ep->X_add_symbol, 0, NULL);
3710 }
3711 macro_build_lui (p, counter, ep, reg);
3712 if (p != NULL)
3713 p += 4;
3714 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3715 BFD_RELOC_LO16);
3716 }
3717 }
3718 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3719 {
3720 expressionS ex;
3721
3722 /* If this is a reference to an external symbol, we want
3723 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3724 Otherwise we want
3725 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3726 nop
3727 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3728 If there is a constant, it must be added in after.
3729
3730 If we have NewABI, we want
3731 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3732 unless we're referencing a global symbol with a non-zero
3733 offset, in which case cst must be added separately. */
3734 if (HAVE_NEWABI)
3735 {
3736 frag_grow (12);
3737
3738 if (ep->X_add_number)
3739 {
3740 frag_now->tc_frag_data.tc_fr_offset =
3741 ex.X_add_number = ep->X_add_number;
3742 ep->X_add_number = 0;
3743 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3744 reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3745 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3746 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3747 ex.X_op = O_constant;
3748 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3749 reg, reg, BFD_RELOC_LO16);
3750 p = frag_var (rs_machine_dependent, 8, 0,
3751 RELAX_ENCODE (8, 4, 0, 0, 0,
3752 mips_opts.warn_about_macros),
3753 ep->X_add_symbol, 0, NULL);
3754 ep->X_add_number = ex.X_add_number;
3755 }
3756
3757 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3758 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3759
3760 if (! p)
3761 {
3762 /* To avoid confusion in tc_gen_reloc, we must ensure
3763 that this does not become a variant frag. */
3764 frag_wane (frag_now);
3765 frag_new (0);
3766 }
3767 }
3768 else
3769 {
3770 ex.X_add_number = ep->X_add_number;
3771 ep->X_add_number = 0;
3772 frag_grow (20);
3773 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3774 BFD_RELOC_MIPS_GOT16,
3775 mips_gp_register);
3776 macro_build (NULL, counter, NULL, "nop", "");
3777 p = frag_var (rs_machine_dependent, 4, 0,
3778 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3779 ep->X_add_symbol, 0, NULL);
3780 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3781 BFD_RELOC_LO16);
3782
3783 if (ex.X_add_number != 0)
3784 {
3785 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3786 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3787 ex.X_op = O_constant;
3788 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3789 reg, reg, BFD_RELOC_LO16);
3790 }
3791 }
3792 }
3793 else if (mips_pic == SVR4_PIC)
3794 {
3795 expressionS ex;
3796 int off;
3797
3798 /* This is the large GOT case. If this is a reference to an
3799 external symbol, we want
3800 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3801 addu $reg,$reg,$gp
3802 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3803
3804 Otherwise, for a reference to a local symbol in old ABI, we want
3805 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3806 nop
3807 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3808 If there is a constant, it must be added in after.
3809
3810 In the NewABI, for local symbols, with or without offsets, we want:
3811 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3812 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3813 */
3814 if (HAVE_NEWABI)
3815 {
3816 frag_grow (24);
3817
3818 frag_now->tc_frag_data.tc_fr_offset =
3819 ex.X_add_number = ep->X_add_number;
3820 ep->X_add_number = 0;
3821 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3822 BFD_RELOC_MIPS_GOT_HI16);
3823 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3824 reg, mips_gp_register);
3825 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3826 BFD_RELOC_MIPS_GOT_LO16, reg);
3827 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3828 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3829 else if (ex.X_add_number)
3830 {
3831 ex.X_op = O_constant;
3832 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3833 reg, reg, BFD_RELOC_LO16);
3834 }
3835
3836 ep->X_add_number = ex.X_add_number;
3837 p = frag_var (rs_machine_dependent, 8, 0,
3838 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3839 mips_opts.warn_about_macros),
3840 ep->X_add_symbol, 0, NULL);
3841 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3842 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3843 macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3844 reg, BFD_RELOC_MIPS_GOT_OFST);
3845 }
3846 else
3847 {
3848 ex.X_add_number = ep->X_add_number;
3849 ep->X_add_number = 0;
3850 if (reg_needs_delay (mips_gp_register))
3851 off = 4;
3852 else
3853 off = 0;
3854 frag_grow (32);
3855 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3856 BFD_RELOC_MIPS_GOT_HI16);
3857 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3858 reg, mips_gp_register);
3859 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3860 BFD_RELOC_MIPS_GOT_LO16, reg);
3861 p = frag_var (rs_machine_dependent, 12 + off, 0,
3862 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3863 mips_opts.warn_about_macros),
3864 ep->X_add_symbol, 0, NULL);
3865 if (off > 0)
3866 {
3867 /* We need a nop before loading from $gp. This special
3868 check is required because the lui which starts the main
3869 instruction stream does not refer to $gp, and so will not
3870 insert the nop which may be required. */
3871 macro_build (p, counter, NULL, "nop", "");
3872 p += 4;
3873 }
3874 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3875 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3876 p += 4;
3877 macro_build (p, counter, NULL, "nop", "");
3878 p += 4;
3879 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3880 BFD_RELOC_LO16);
3881
3882 if (ex.X_add_number != 0)
3883 {
3884 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3885 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3886 ex.X_op = O_constant;
3887 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3888 reg, reg, BFD_RELOC_LO16);
3889 }
3890 }
3891 }
3892 else if (mips_pic == EMBEDDED_PIC)
3893 {
3894 /* We always do
3895 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3896 */
3897 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3898 mips_gp_register, BFD_RELOC_GPREL16);
3899 }
3900 else
3901 abort ();
3902 }
3903
3904 /* Move the contents of register SOURCE into register DEST. */
3905
3906 static void
3907 move_register (int *counter, int dest, int source)
3908 {
3909 macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
3910 "d,v,t", dest, source, 0);
3911 }
3912
3913 /*
3914 * Build macros
3915 * This routine implements the seemingly endless macro or synthesized
3916 * instructions and addressing modes in the mips assembly language. Many
3917 * of these macros are simple and are similar to each other. These could
3918 * probably be handled by some kind of table or grammer aproach instead of
3919 * this verbose method. Others are not simple macros but are more like
3920 * optimizing code generation.
3921 * One interesting optimization is when several store macros appear
3922 * consecutivly that would load AT with the upper half of the same address.
3923 * The ensuing load upper instructions are ommited. This implies some kind
3924 * of global optimization. We currently only optimize within a single macro.
3925 * For many of the load and store macros if the address is specified as a
3926 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3927 * first load register 'at' with zero and use it as the base register. The
3928 * mips assembler simply uses register $zero. Just one tiny optimization
3929 * we're missing.
3930 */
3931 static void
3932 macro (struct mips_cl_insn *ip)
3933 {
3934 register int treg, sreg, dreg, breg;
3935 int tempreg;
3936 int mask;
3937 int icnt = 0;
3938 int used_at = 0;
3939 expressionS expr1;
3940 const char *s;
3941 const char *s2;
3942 const char *fmt;
3943 int likely = 0;
3944 int dbl = 0;
3945 int coproc = 0;
3946 int lr = 0;
3947 int imm = 0;
3948 offsetT maxnum;
3949 int off;
3950 bfd_reloc_code_real_type r;
3951 int hold_mips_optimize;
3952
3953 assert (! mips_opts.mips16);
3954
3955 treg = (ip->insn_opcode >> 16) & 0x1f;
3956 dreg = (ip->insn_opcode >> 11) & 0x1f;
3957 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3958 mask = ip->insn_mo->mask;
3959
3960 expr1.X_op = O_constant;
3961 expr1.X_op_symbol = NULL;
3962 expr1.X_add_symbol = NULL;
3963 expr1.X_add_number = 1;
3964
3965 /* Umatched fixups should not be put in the same frag as a relaxable
3966 macro. For example, suppose we have:
3967
3968 lui $4,%hi(l1) # 1
3969 la $5,l2 # 2
3970 addiu $4,$4,%lo(l1) # 3
3971
3972 If instructions 1 and 2 were put in the same frag, md_frob_file would
3973 move the fixup for #1 after the fixups for the "unrelaxed" version of
3974 #2. This would confuse tc_gen_reloc, which expects the relocations
3975 for #2 to be the last for that frag.
3976
3977 Also, if tc_gen_reloc sees certain relocations in a variant frag,
3978 it assumes that they belong to a relaxable macro. We mustn't put
3979 other uses of such relocations into a variant frag.
3980
3981 To avoid both problems, finish the current frag it contains a
3982 %reloc() operator. The macro then goes into a new frag. */
3983 if (prev_reloc_op_frag == frag_now)
3984 {
3985 frag_wane (frag_now);
3986 frag_new (0);
3987 }
3988
3989 switch (mask)
3990 {
3991 case M_DABS:
3992 dbl = 1;
3993 case M_ABS:
3994 /* bgez $a0,.+12
3995 move v0,$a0
3996 sub v0,$zero,$a0
3997 */
3998
3999 mips_emit_delays (TRUE);
4000 ++mips_opts.noreorder;
4001 mips_any_noreorder = 1;
4002
4003 expr1.X_add_number = 8;
4004 macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4005 if (dreg == sreg)
4006 macro_build (NULL, &icnt, NULL, "nop", "", 0);
4007 else
4008 move_register (&icnt, dreg, sreg);
4009 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4010 sreg);
4011
4012 --mips_opts.noreorder;
4013 return;
4014
4015 case M_ADD_I:
4016 s = "addi";
4017 s2 = "add";
4018 goto do_addi;
4019 case M_ADDU_I:
4020 s = "addiu";
4021 s2 = "addu";
4022 goto do_addi;
4023 case M_DADD_I:
4024 dbl = 1;
4025 s = "daddi";
4026 s2 = "dadd";
4027 goto do_addi;
4028 case M_DADDU_I:
4029 dbl = 1;
4030 s = "daddiu";
4031 s2 = "daddu";
4032 do_addi:
4033 if (imm_expr.X_op == O_constant
4034 && imm_expr.X_add_number >= -0x8000
4035 && imm_expr.X_add_number < 0x8000)
4036 {
4037 macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4038 BFD_RELOC_LO16);
4039 return;
4040 }
4041 load_register (&icnt, AT, &imm_expr, dbl);
4042 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4043 break;
4044
4045 case M_AND_I:
4046 s = "andi";
4047 s2 = "and";
4048 goto do_bit;
4049 case M_OR_I:
4050 s = "ori";
4051 s2 = "or";
4052 goto do_bit;
4053 case M_NOR_I:
4054 s = "";
4055 s2 = "nor";
4056 goto do_bit;
4057 case M_XOR_I:
4058 s = "xori";
4059 s2 = "xor";
4060 do_bit:
4061 if (imm_expr.X_op == O_constant
4062 && imm_expr.X_add_number >= 0
4063 && imm_expr.X_add_number < 0x10000)
4064 {
4065 if (mask != M_NOR_I)
4066 macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4067 BFD_RELOC_LO16);
4068 else
4069 {
4070 macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4071 BFD_RELOC_LO16);
4072 macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
4073 }
4074 return;
4075 }
4076
4077 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4078 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
4079 break;
4080
4081 case M_BEQ_I:
4082 s = "beq";
4083 goto beq_i;
4084 case M_BEQL_I:
4085 s = "beql";
4086 likely = 1;
4087 goto beq_i;
4088 case M_BNE_I:
4089 s = "bne";
4090 goto beq_i;
4091 case M_BNEL_I:
4092 s = "bnel";
4093 likely = 1;
4094 beq_i:
4095 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4096 {
4097 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
4098 return;
4099 }
4100 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4101 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4102 break;
4103
4104 case M_BGEL:
4105 likely = 1;
4106 case M_BGE:
4107 if (treg == 0)
4108 {
4109 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4110 "s,p", sreg);
4111 return;
4112 }
4113 if (sreg == 0)
4114 {
4115 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4116 "s,p", treg);
4117 return;
4118 }
4119 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4120 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4121 "s,t,p", AT, 0);
4122 break;
4123
4124 case M_BGTL_I:
4125 likely = 1;
4126 case M_BGT_I:
4127 /* check for > max integer */
4128 maxnum = 0x7fffffff;
4129 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4130 {
4131 maxnum <<= 16;
4132 maxnum |= 0xffff;
4133 maxnum <<= 16;
4134 maxnum |= 0xffff;
4135 }
4136 if (imm_expr.X_op == O_constant
4137 && imm_expr.X_add_number >= maxnum
4138 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4139 {
4140 do_false:
4141 /* result is always false */
4142 if (! likely)
4143 {
4144 if (warn_nops)
4145 as_warn (_("Branch %s is always false (nop)"),
4146 ip->insn_mo->name);
4147 macro_build (NULL, &icnt, NULL, "nop", "", 0);
4148 }
4149 else
4150 {
4151 if (warn_nops)
4152 as_warn (_("Branch likely %s is always false"),
4153 ip->insn_mo->name);
4154 macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
4155 }
4156 return;
4157 }
4158 if (imm_expr.X_op != O_constant)
4159 as_bad (_("Unsupported large constant"));
4160 ++imm_expr.X_add_number;
4161 /* FALLTHROUGH */
4162 case M_BGE_I:
4163 case M_BGEL_I:
4164 if (mask == M_BGEL_I)
4165 likely = 1;
4166 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4167 {
4168 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4169 "s,p", sreg);
4170 return;
4171 }
4172 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4173 {
4174 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4175 "s,p", sreg);
4176 return;
4177 }
4178 maxnum = 0x7fffffff;
4179 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4180 {
4181 maxnum <<= 16;
4182 maxnum |= 0xffff;
4183 maxnum <<= 16;
4184 maxnum |= 0xffff;
4185 }
4186 maxnum = - maxnum - 1;
4187 if (imm_expr.X_op == O_constant
4188 && imm_expr.X_add_number <= maxnum
4189 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4190 {
4191 do_true:
4192 /* result is always true */
4193 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4194 macro_build (NULL, &icnt, &offset_expr, "b", "p");
4195 return;
4196 }
4197 set_at (&icnt, sreg, 0);
4198 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4199 "s,t,p", AT, 0);
4200 break;
4201
4202 case M_BGEUL:
4203 likely = 1;
4204 case M_BGEU:
4205 if (treg == 0)
4206 goto do_true;
4207 if (sreg == 0)
4208 {
4209 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4210 "s,t,p", 0, treg);
4211 return;
4212 }
4213 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4214 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4215 "s,t,p", AT, 0);
4216 break;
4217
4218 case M_BGTUL_I:
4219 likely = 1;
4220 case M_BGTU_I:
4221 if (sreg == 0
4222 || (HAVE_32BIT_GPRS
4223 && imm_expr.X_op == O_constant
4224 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4225 goto do_false;
4226 if (imm_expr.X_op != O_constant)
4227 as_bad (_("Unsupported large constant"));
4228 ++imm_expr.X_add_number;
4229 /* FALLTHROUGH */
4230 case M_BGEU_I:
4231 case M_BGEUL_I:
4232 if (mask == M_BGEUL_I)
4233 likely = 1;
4234 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4235 goto do_true;
4236 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4237 {
4238 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4239 "s,t,p", sreg, 0);
4240 return;
4241 }
4242 set_at (&icnt, sreg, 1);
4243 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4244 "s,t,p", AT, 0);
4245 break;
4246
4247 case M_BGTL:
4248 likely = 1;
4249 case M_BGT:
4250 if (treg == 0)
4251 {
4252 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4253 "s,p", sreg);
4254 return;
4255 }
4256 if (sreg == 0)
4257 {
4258 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4259 "s,p", treg);
4260 return;
4261 }
4262 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4263 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4264 "s,t,p", AT, 0);
4265 break;
4266
4267 case M_BGTUL:
4268 likely = 1;
4269 case M_BGTU:
4270 if (treg == 0)
4271 {
4272 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4273 "s,t,p", sreg, 0);
4274 return;
4275 }
4276 if (sreg == 0)
4277 goto do_false;
4278 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4279 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4280 "s,t,p", AT, 0);
4281 break;
4282
4283 case M_BLEL:
4284 likely = 1;
4285 case M_BLE:
4286 if (treg == 0)
4287 {
4288 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4289 "s,p", sreg);
4290 return;
4291 }
4292 if (sreg == 0)
4293 {
4294 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4295 "s,p", treg);
4296 return;
4297 }
4298 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4299 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4300 "s,t,p", AT, 0);
4301 break;
4302
4303 case M_BLEL_I:
4304 likely = 1;
4305 case M_BLE_I:
4306 maxnum = 0x7fffffff;
4307 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4308 {
4309 maxnum <<= 16;
4310 maxnum |= 0xffff;
4311 maxnum <<= 16;
4312 maxnum |= 0xffff;
4313 }
4314 if (imm_expr.X_op == O_constant
4315 && imm_expr.X_add_number >= maxnum
4316 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4317 goto do_true;
4318 if (imm_expr.X_op != O_constant)
4319 as_bad (_("Unsupported large constant"));
4320 ++imm_expr.X_add_number;
4321 /* FALLTHROUGH */
4322 case M_BLT_I:
4323 case M_BLTL_I:
4324 if (mask == M_BLTL_I)
4325 likely = 1;
4326 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4327 {
4328 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4329 "s,p", sreg);
4330 return;
4331 }
4332 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4333 {
4334 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4335 "s,p", sreg);
4336 return;
4337 }
4338 set_at (&icnt, sreg, 0);
4339 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4340 "s,t,p", AT, 0);
4341 break;
4342
4343 case M_BLEUL:
4344 likely = 1;
4345 case M_BLEU:
4346 if (treg == 0)
4347 {
4348 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4349 "s,t,p", sreg, 0);
4350 return;
4351 }
4352 if (sreg == 0)
4353 goto do_true;
4354 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4355 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4356 "s,t,p", AT, 0);
4357 break;
4358
4359 case M_BLEUL_I:
4360 likely = 1;
4361 case M_BLEU_I:
4362 if (sreg == 0
4363 || (HAVE_32BIT_GPRS
4364 && imm_expr.X_op == O_constant
4365 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4366 goto do_true;
4367 if (imm_expr.X_op != O_constant)
4368 as_bad (_("Unsupported large constant"));
4369 ++imm_expr.X_add_number;
4370 /* FALLTHROUGH */
4371 case M_BLTU_I:
4372 case M_BLTUL_I:
4373 if (mask == M_BLTUL_I)
4374 likely = 1;
4375 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4376 goto do_false;
4377 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4378 {
4379 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4380 "s,t,p", sreg, 0);
4381 return;
4382 }
4383 set_at (&icnt, sreg, 1);
4384 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4385 "s,t,p", AT, 0);
4386 break;
4387
4388 case M_BLTL:
4389 likely = 1;
4390 case M_BLT:
4391 if (treg == 0)
4392 {
4393 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4394 "s,p", sreg);
4395 return;
4396 }
4397 if (sreg == 0)
4398 {
4399 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4400 "s,p", treg);
4401 return;
4402 }
4403 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4404 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4405 "s,t,p", AT, 0);
4406 break;
4407
4408 case M_BLTUL:
4409 likely = 1;
4410 case M_BLTU:
4411 if (treg == 0)
4412 goto do_false;
4413 if (sreg == 0)
4414 {
4415 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4416 "s,t,p", 0, treg);
4417 return;
4418 }
4419 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4420 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4421 "s,t,p", AT, 0);
4422 break;
4423
4424 case M_DDIV_3:
4425 dbl = 1;
4426 case M_DIV_3:
4427 s = "mflo";
4428 goto do_div3;
4429 case M_DREM_3:
4430 dbl = 1;
4431 case M_REM_3:
4432 s = "mfhi";
4433 do_div3:
4434 if (treg == 0)
4435 {
4436 as_warn (_("Divide by zero."));
4437 if (mips_trap)
4438 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4439 else
4440 macro_build (NULL, &icnt, NULL, "break", "c", 7);
4441 return;
4442 }
4443
4444 mips_emit_delays (TRUE);
4445 ++mips_opts.noreorder;
4446 mips_any_noreorder = 1;
4447 if (mips_trap)
4448 {
4449 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4450 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4451 sreg, treg);
4452 }
4453 else
4454 {
4455 expr1.X_add_number = 8;
4456 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4457 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4458 sreg, treg);
4459 macro_build (NULL, &icnt,NULL, "break", "c", 7);
4460 }
4461 expr1.X_add_number = -1;
4462 macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4463 AT, 0, BFD_RELOC_LO16);
4464 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4465 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4466 if (dbl)
4467 {
4468 expr1.X_add_number = 1;
4469 macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4470 BFD_RELOC_LO16);
4471 macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
4472 }
4473 else
4474 {
4475 expr1.X_add_number = 0x80000000;
4476 macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4477 BFD_RELOC_HI16);
4478 }
4479 if (mips_trap)
4480 {
4481 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
4482 /* We want to close the noreorder block as soon as possible, so
4483 that later insns are available for delay slot filling. */
4484 --mips_opts.noreorder;
4485 }
4486 else
4487 {
4488 expr1.X_add_number = 8;
4489 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4490 macro_build (NULL, &icnt, NULL, "nop", "", 0);
4491
4492 /* We want to close the noreorder block as soon as possible, so
4493 that later insns are available for delay slot filling. */
4494 --mips_opts.noreorder;
4495
4496 macro_build (NULL, &icnt, NULL, "break", "c", 6);
4497 }
4498 macro_build (NULL, &icnt, NULL, s, "d", dreg);
4499 break;
4500
4501 case M_DIV_3I:
4502 s = "div";
4503 s2 = "mflo";
4504 goto do_divi;
4505 case M_DIVU_3I:
4506 s = "divu";
4507 s2 = "mflo";
4508 goto do_divi;
4509 case M_REM_3I:
4510 s = "div";
4511 s2 = "mfhi";
4512 goto do_divi;
4513 case M_REMU_3I:
4514 s = "divu";
4515 s2 = "mfhi";
4516 goto do_divi;
4517 case M_DDIV_3I:
4518 dbl = 1;
4519 s = "ddiv";
4520 s2 = "mflo";
4521 goto do_divi;
4522 case M_DDIVU_3I:
4523 dbl = 1;
4524 s = "ddivu";
4525 s2 = "mflo";
4526 goto do_divi;
4527 case M_DREM_3I:
4528 dbl = 1;
4529 s = "ddiv";
4530 s2 = "mfhi";
4531 goto do_divi;
4532 case M_DREMU_3I:
4533 dbl = 1;
4534 s = "ddivu";
4535 s2 = "mfhi";
4536 do_divi:
4537 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4538 {
4539 as_warn (_("Divide by zero."));
4540 if (mips_trap)
4541 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
4542 else
4543 macro_build (NULL, &icnt, NULL, "break", "c", 7);
4544 return;
4545 }
4546 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4547 {
4548 if (strcmp (s2, "mflo") == 0)
4549 move_register (&icnt, dreg, sreg);
4550 else
4551 move_register (&icnt, dreg, 0);
4552 return;
4553 }
4554 if (imm_expr.X_op == O_constant
4555 && imm_expr.X_add_number == -1
4556 && s[strlen (s) - 1] != 'u')
4557 {
4558 if (strcmp (s2, "mflo") == 0)
4559 {
4560 macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4561 dreg, sreg);
4562 }
4563 else
4564 move_register (&icnt, dreg, 0);
4565 return;
4566 }
4567
4568 load_register (&icnt, AT, &imm_expr, dbl);
4569 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4570 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4571 break;
4572
4573 case M_DIVU_3:
4574 s = "divu";
4575 s2 = "mflo";
4576 goto do_divu3;
4577 case M_REMU_3:
4578 s = "divu";
4579 s2 = "mfhi";
4580 goto do_divu3;
4581 case M_DDIVU_3:
4582 s = "ddivu";
4583 s2 = "mflo";
4584 goto do_divu3;
4585 case M_DREMU_3:
4586 s = "ddivu";
4587 s2 = "mfhi";
4588 do_divu3:
4589 mips_emit_delays (TRUE);
4590 ++mips_opts.noreorder;
4591 mips_any_noreorder = 1;
4592 if (mips_trap)
4593 {
4594 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4595 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4596 /* We want to close the noreorder block as soon as possible, so
4597 that later insns are available for delay slot filling. */
4598 --mips_opts.noreorder;
4599 }
4600 else
4601 {
4602 expr1.X_add_number = 8;
4603 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4604 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4605
4606 /* We want to close the noreorder block as soon as possible, so
4607 that later insns are available for delay slot filling. */
4608 --mips_opts.noreorder;
4609 macro_build (NULL, &icnt, NULL, "break", "c", 7);
4610 }
4611 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
4612 return;
4613
4614 case M_DLA_AB:
4615 dbl = 1;
4616 case M_LA_AB:
4617 /* Load the address of a symbol into a register. If breg is not
4618 zero, we then add a base register to it. */
4619
4620 if (dbl && HAVE_32BIT_GPRS)
4621 as_warn (_("dla used to load 32-bit register"));
4622
4623 if (! dbl && HAVE_64BIT_OBJECTS)
4624 as_warn (_("la used to load 64-bit address"));
4625
4626 if (offset_expr.X_op == O_constant
4627 && offset_expr.X_add_number >= -0x8000
4628 && offset_expr.X_add_number < 0x8000)
4629 {
4630 macro_build (NULL, &icnt, &offset_expr,
4631 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4632 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4633 return;
4634 }
4635
4636 if (treg == breg)
4637 {
4638 tempreg = AT;
4639 used_at = 1;
4640 }
4641 else
4642 {
4643 tempreg = treg;
4644 used_at = 0;
4645 }
4646
4647 /* When generating embedded PIC code, we permit expressions of
4648 the form
4649 la $treg,foo-bar
4650 la $treg,foo-bar($breg)
4651 where bar is an address in the current section. These are used
4652 when getting the addresses of functions. We don't permit
4653 X_add_number to be non-zero, because if the symbol is
4654 external the relaxing code needs to know that any addend is
4655 purely the offset to X_op_symbol. */
4656 if (mips_pic == EMBEDDED_PIC
4657 && offset_expr.X_op == O_subtract
4658 && (symbol_constant_p (offset_expr.X_op_symbol)
4659 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4660 : (symbol_equated_p (offset_expr.X_op_symbol)
4661 && (S_GET_SEGMENT
4662 (symbol_get_value_expression (offset_expr.X_op_symbol)
4663 ->X_add_symbol)
4664 == now_seg)))
4665 && (offset_expr.X_add_number == 0
4666 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4667 {
4668 if (breg == 0)
4669 {
4670 tempreg = treg;
4671 used_at = 0;
4672 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4673 BFD_RELOC_PCREL_HI16_S);
4674 }
4675 else
4676 {
4677 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4678 BFD_RELOC_PCREL_HI16_S);
4679 macro_build (NULL, &icnt, NULL,
4680 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4681 "d,v,t", tempreg, tempreg, breg);
4682 }
4683 macro_build (NULL, &icnt, &offset_expr,
4684 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4685 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
4686 if (! used_at)
4687 return;
4688 break;
4689 }
4690
4691 if (offset_expr.X_op != O_symbol
4692 && offset_expr.X_op != O_constant)
4693 {
4694 as_bad (_("expression too complex"));
4695 offset_expr.X_op = O_constant;
4696 }
4697
4698 if (offset_expr.X_op == O_constant)
4699 load_register (&icnt, tempreg, &offset_expr,
4700 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4701 ? (dbl || HAVE_64BIT_ADDRESSES)
4702 : HAVE_64BIT_ADDRESSES));
4703 else if (mips_pic == NO_PIC)
4704 {
4705 /* If this is a reference to a GP relative symbol, we want
4706 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4707 Otherwise we want
4708 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4709 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4710 If we have a constant, we need two instructions anyhow,
4711 so we may as well always use the latter form.
4712
4713 With 64bit address space and a usable $at we want
4714 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4715 lui $at,<sym> (BFD_RELOC_HI16_S)
4716 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4717 daddiu $at,<sym> (BFD_RELOC_LO16)
4718 dsll32 $tempreg,0
4719 daddu $tempreg,$tempreg,$at
4720
4721 If $at is already in use, we use a path which is suboptimal
4722 on superscalar processors.
4723 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4724 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4725 dsll $tempreg,16
4726 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4727 dsll $tempreg,16
4728 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4729 */
4730 char *p = NULL;
4731 if (HAVE_64BIT_ADDRESSES)
4732 {
4733 /* We don't do GP optimization for now because RELAX_ENCODE can't
4734 hold the data for such large chunks. */
4735
4736 if (used_at == 0 && ! mips_opts.noat)
4737 {
4738 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4739 tempreg, BFD_RELOC_MIPS_HIGHEST);
4740 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4741 AT, BFD_RELOC_HI16_S);
4742 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4743 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4744 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4745 AT, AT, BFD_RELOC_LO16);
4746 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4747 tempreg, tempreg, 0);
4748 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4749 tempreg, tempreg, AT);
4750 used_at = 1;
4751 }
4752 else
4753 {
4754 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4755 tempreg, BFD_RELOC_MIPS_HIGHEST);
4756 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4757 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4758 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4759 tempreg, tempreg, 16);
4760 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4761 tempreg, tempreg, BFD_RELOC_HI16_S);
4762 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
4763 tempreg, tempreg, 16);
4764 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4765 tempreg, tempreg, BFD_RELOC_LO16);
4766 }
4767 }
4768 else
4769 {
4770 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4771 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4772 {
4773 frag_grow (20);
4774 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4775 "t,r,j", tempreg, mips_gp_register,
4776 BFD_RELOC_GPREL16);
4777 p = frag_var (rs_machine_dependent, 8, 0,
4778 RELAX_ENCODE (4, 8, 0, 4, 0,
4779 mips_opts.warn_about_macros),
4780 offset_expr.X_add_symbol, 0, NULL);
4781 }
4782 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4783 if (p != NULL)
4784 p += 4;
4785 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4786 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4787 }
4788 }
4789 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
4790 {
4791 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4792
4793 /* If this is a reference to an external symbol, and there
4794 is no constant, we want
4795 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4796 or if tempreg is PIC_CALL_REG
4797 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4798 For a local symbol, we want
4799 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4800 nop
4801 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4802
4803 If we have a small constant, and this is a reference to
4804 an external symbol, we want
4805 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4806 nop
4807 addiu $tempreg,$tempreg,<constant>
4808 For a local symbol, we want the same instruction
4809 sequence, but we output a BFD_RELOC_LO16 reloc on the
4810 addiu instruction.
4811
4812 If we have a large constant, and this is a reference to
4813 an external symbol, we want
4814 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4815 lui $at,<hiconstant>
4816 addiu $at,$at,<loconstant>
4817 addu $tempreg,$tempreg,$at
4818 For a local symbol, we want the same instruction
4819 sequence, but we output a BFD_RELOC_LO16 reloc on the
4820 addiu instruction.
4821 */
4822
4823 expr1.X_add_number = offset_expr.X_add_number;
4824 offset_expr.X_add_number = 0;
4825 frag_grow (32);
4826 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4827 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4828 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4829 tempreg, lw_reloc_type, mips_gp_register);
4830 if (expr1.X_add_number == 0)
4831 {
4832 int off;
4833 char *p;
4834
4835 if (breg == 0)
4836 off = 0;
4837 else
4838 {
4839 /* We're going to put in an addu instruction using
4840 tempreg, so we may as well insert the nop right
4841 now. */
4842 macro_build (NULL, &icnt, NULL, "nop", "");
4843 off = 4;
4844 }
4845 p = frag_var (rs_machine_dependent, 8 - off, 0,
4846 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4847 (breg == 0
4848 ? mips_opts.warn_about_macros
4849 : 0)),
4850 offset_expr.X_add_symbol, 0, NULL);
4851 if (breg == 0)
4852 {
4853 macro_build (p, &icnt, NULL, "nop", "");
4854 p += 4;
4855 }
4856 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
4857 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4858 /* FIXME: If breg == 0, and the next instruction uses
4859 $tempreg, then if this variant case is used an extra
4860 nop will be generated. */
4861 }
4862 else if (expr1.X_add_number >= -0x8000
4863 && expr1.X_add_number < 0x8000)
4864 {
4865 macro_build (NULL, &icnt, NULL, "nop", "");
4866 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
4867 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4868 frag_var (rs_machine_dependent, 0, 0,
4869 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4870 offset_expr.X_add_symbol, 0, NULL);
4871 }
4872 else
4873 {
4874 int off1;
4875
4876 /* If we are going to add in a base register, and the
4877 target register and the base register are the same,
4878 then we are using AT as a temporary register. Since
4879 we want to load the constant into AT, we add our
4880 current AT (from the global offset table) and the
4881 register into the register now, and pretend we were
4882 not using a base register. */
4883 if (breg != treg)
4884 off1 = 0;
4885 else
4886 {
4887 macro_build (NULL, &icnt, NULL, "nop", "");
4888 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
4889 treg, AT, breg);
4890 breg = 0;
4891 tempreg = treg;
4892 off1 = -8;
4893 }
4894
4895 /* Set mips_optimize around the lui instruction to avoid
4896 inserting an unnecessary nop after the lw. */
4897 hold_mips_optimize = mips_optimize;
4898 mips_optimize = 2;
4899 macro_build_lui (NULL, &icnt, &expr1, AT);
4900 mips_optimize = hold_mips_optimize;
4901
4902 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
4903 AT, AT, BFD_RELOC_LO16);
4904 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
4905 tempreg, tempreg, AT);
4906 frag_var (rs_machine_dependent, 0, 0,
4907 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4908 offset_expr.X_add_symbol, 0, NULL);
4909 used_at = 1;
4910 }
4911 }
4912 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
4913 {
4914 char *p = NULL;
4915 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4916 int adj = 0;
4917
4918 /* If this is a reference to an external, and there is no
4919 constant, or local symbol (*), with or without a
4920 constant, we want
4921 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4922 or if tempreg is PIC_CALL_REG
4923 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4924
4925 If we have a small constant, and this is a reference to
4926 an external symbol, we want
4927 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4928 addiu $tempreg,$tempreg,<constant>
4929
4930 If we have a large constant, and this is a reference to
4931 an external symbol, we want
4932 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4933 lui $at,<hiconstant>
4934 addiu $at,$at,<loconstant>
4935 addu $tempreg,$tempreg,$at
4936
4937 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
4938 local symbols, even though it introduces an additional
4939 instruction. */
4940
4941 frag_grow (28);
4942 if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
4943 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4944 if (offset_expr.X_add_number)
4945 {
4946 frag_now->tc_frag_data.tc_fr_offset =
4947 expr1.X_add_number = offset_expr.X_add_number;
4948 offset_expr.X_add_number = 0;
4949
4950 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
4951 "t,o(b)", tempreg, lw_reloc_type,
4952 mips_gp_register);
4953
4954 if (expr1.X_add_number >= -0x8000
4955 && expr1.X_add_number < 0x8000)
4956 {
4957 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
4958 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
4959 p = frag_var (rs_machine_dependent, 4, 0,
4960 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
4961 offset_expr.X_add_symbol, 0, NULL);
4962 }
4963 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
4964 {
4965 int dreg;
4966
4967 /* If we are going to add in a base register, and the
4968 target register and the base register are the same,
4969 then we are using AT as a temporary register. Since
4970 we want to load the constant into AT, we add our
4971 current AT (from the global offset table) and the
4972 register into the register now, and pretend we were
4973 not using a base register. */
4974 if (breg != treg)
4975 dreg = tempreg;
4976 else
4977 {
4978 assert (tempreg == AT);
4979 macro_build (NULL, &icnt,NULL, ADDRESS_ADD_INSN,
4980 "d,v,t", treg, AT, breg);
4981 dreg = treg;
4982 adj = 4;
4983 }
4984
4985 macro_build_lui (NULL, &icnt, &expr1, AT);
4986 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
4987 "t,r,j", AT, AT, BFD_RELOC_LO16);
4988 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
4989 dreg, dreg, AT);
4990
4991 p = frag_var (rs_machine_dependent, 4 + adj, 0,
4992 RELAX_ENCODE (16 + adj, 4 + adj,
4993 0, 0, 0, 0),
4994 offset_expr.X_add_symbol, 0, NULL);
4995
4996 used_at = 1;
4997 }
4998 else
4999 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5000
5001 offset_expr.X_add_number = expr1.X_add_number;
5002
5003 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5004 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
5005 mips_gp_register);
5006 if (adj)
5007 {
5008 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5009 treg, tempreg, breg);
5010 breg = 0;
5011 tempreg = treg;
5012 }
5013 }
5014 else
5015 {
5016 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5017 "t,o(b)", tempreg, lw_reloc_type,
5018 mips_gp_register);
5019 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5020 p = frag_var (rs_machine_dependent, 0, 0,
5021 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5022 offset_expr.X_add_symbol, 0, NULL);
5023 }
5024
5025 if (! p)
5026 {
5027 /* To avoid confusion in tc_gen_reloc, we must ensure
5028 that this does not become a variant frag. */
5029 frag_wane (frag_now);
5030 frag_new (0);
5031 }
5032 }
5033 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5034 {
5035 int gpdel;
5036 char *p;
5037 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5038 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5039 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5040
5041 /* This is the large GOT case. If this is a reference to an
5042 external symbol, and there is no constant, we want
5043 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5044 addu $tempreg,$tempreg,$gp
5045 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5046 or if tempreg is PIC_CALL_REG
5047 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5048 addu $tempreg,$tempreg,$gp
5049 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5050 For a local symbol, we want
5051 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5052 nop
5053 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5054
5055 If we have a small constant, and this is a reference to
5056 an external symbol, we want
5057 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5058 addu $tempreg,$tempreg,$gp
5059 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5060 nop
5061 addiu $tempreg,$tempreg,<constant>
5062 For a local symbol, we want
5063 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5064 nop
5065 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5066
5067 If we have a large constant, and this is a reference to
5068 an external symbol, we want
5069 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5070 addu $tempreg,$tempreg,$gp
5071 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5072 lui $at,<hiconstant>
5073 addiu $at,$at,<loconstant>
5074 addu $tempreg,$tempreg,$at
5075 For a local symbol, we want
5076 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5077 lui $at,<hiconstant>
5078 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5079 addu $tempreg,$tempreg,$at
5080 */
5081
5082 expr1.X_add_number = offset_expr.X_add_number;
5083 offset_expr.X_add_number = 0;
5084 frag_grow (52);
5085 if (reg_needs_delay (mips_gp_register))
5086 gpdel = 4;
5087 else
5088 gpdel = 0;
5089 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5090 {
5091 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5092 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5093 }
5094 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5095 tempreg, lui_reloc_type);
5096 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5097 tempreg, tempreg, mips_gp_register);
5098 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5099 tempreg, lw_reloc_type, tempreg);
5100 if (expr1.X_add_number == 0)
5101 {
5102 int off;
5103
5104 if (breg == 0)
5105 off = 0;
5106 else
5107 {
5108 /* We're going to put in an addu instruction using
5109 tempreg, so we may as well insert the nop right
5110 now. */
5111 macro_build (NULL, &icnt, NULL, "nop", "");
5112 off = 4;
5113 }
5114
5115 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5116 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5117 8 + gpdel, 0,
5118 (breg == 0
5119 ? mips_opts.warn_about_macros
5120 : 0)),
5121 offset_expr.X_add_symbol, 0, NULL);
5122 }
5123 else if (expr1.X_add_number >= -0x8000
5124 && expr1.X_add_number < 0x8000)
5125 {
5126 macro_build (NULL, &icnt, NULL, "nop", "");
5127 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5128 tempreg, tempreg, BFD_RELOC_LO16);
5129
5130 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5131 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5132 (breg == 0
5133 ? mips_opts.warn_about_macros
5134 : 0)),
5135 offset_expr.X_add_symbol, 0, NULL);
5136 }
5137 else
5138 {
5139 int adj, dreg;
5140
5141 /* If we are going to add in a base register, and the
5142 target register and the base register are the same,
5143 then we are using AT as a temporary register. Since
5144 we want to load the constant into AT, we add our
5145 current AT (from the global offset table) and the
5146 register into the register now, and pretend we were
5147 not using a base register. */
5148 if (breg != treg)
5149 {
5150 adj = 0;
5151 dreg = tempreg;
5152 }
5153 else
5154 {
5155 assert (tempreg == AT);
5156 macro_build (NULL, &icnt, NULL, "nop", "");
5157 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5158 treg, AT, breg);
5159 dreg = treg;
5160 adj = 8;
5161 }
5162
5163 /* Set mips_optimize around the lui instruction to avoid
5164 inserting an unnecessary nop after the lw. */
5165 hold_mips_optimize = mips_optimize;
5166 mips_optimize = 2;
5167 macro_build_lui (NULL, &icnt, &expr1, AT);
5168 mips_optimize = hold_mips_optimize;
5169
5170 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5171 AT, AT, BFD_RELOC_LO16);
5172 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5173 dreg, dreg, AT);
5174
5175 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5176 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5177 8 + gpdel, 0,
5178 (breg == 0
5179 ? mips_opts.warn_about_macros
5180 : 0)),
5181 offset_expr.X_add_symbol, 0, NULL);
5182
5183 used_at = 1;
5184 }
5185
5186 if (gpdel > 0)
5187 {
5188 /* This is needed because this instruction uses $gp, but
5189 the first instruction on the main stream does not. */
5190 macro_build (p, &icnt, NULL, "nop", "");
5191 p += 4;
5192 }
5193
5194 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5195 tempreg, local_reloc_type, mips_gp_register);
5196 p += 4;
5197 if (expr1.X_add_number >= -0x8000
5198 && expr1.X_add_number < 0x8000)
5199 {
5200 macro_build (p, &icnt, NULL, "nop", "");
5201 p += 4;
5202 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
5203 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
5204 /* FIXME: If add_number is 0, and there was no base
5205 register, the external symbol case ended with a load,
5206 so if the symbol turns out to not be external, and
5207 the next instruction uses tempreg, an unnecessary nop
5208 will be inserted. */
5209 }
5210 else
5211 {
5212 if (breg == treg)
5213 {
5214 /* We must add in the base register now, as in the
5215 external symbol case. */
5216 assert (tempreg == AT);
5217 macro_build (p, &icnt, NULL, "nop", "");
5218 p += 4;
5219 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5220 treg, AT, breg);
5221 p += 4;
5222 tempreg = treg;
5223 /* We set breg to 0 because we have arranged to add
5224 it in in both cases. */
5225 breg = 0;
5226 }
5227
5228 macro_build_lui (p, &icnt, &expr1, AT);
5229 p += 4;
5230 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5231 AT, AT, BFD_RELOC_LO16);
5232 p += 4;
5233 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5234 tempreg, tempreg, AT);
5235 p += 4;
5236 }
5237 }
5238 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5239 {
5240 char *p = NULL;
5241 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5242 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5243 int adj = 0;
5244
5245 /* This is the large GOT case. If this is a reference to an
5246 external symbol, and there is no constant, we want
5247 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5248 add $tempreg,$tempreg,$gp
5249 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5250 or if tempreg is PIC_CALL_REG
5251 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5252 add $tempreg,$tempreg,$gp
5253 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5254
5255 If we have a small constant, and this is a reference to
5256 an external symbol, we want
5257 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5258 add $tempreg,$tempreg,$gp
5259 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5260 addi $tempreg,$tempreg,<constant>
5261
5262 If we have a large constant, and this is a reference to
5263 an external symbol, we want
5264 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5265 addu $tempreg,$tempreg,$gp
5266 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5267 lui $at,<hiconstant>
5268 addi $at,$at,<loconstant>
5269 add $tempreg,$tempreg,$at
5270
5271 If we have NewABI, and we know it's a local symbol, we want
5272 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5273 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5274 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5275
5276 frag_grow (40);
5277
5278 frag_now->tc_frag_data.tc_fr_offset =
5279 expr1.X_add_number = offset_expr.X_add_number;
5280 offset_expr.X_add_number = 0;
5281
5282 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5283 {
5284 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5285 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5286 }
5287 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5288 tempreg, lui_reloc_type);
5289 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5290 tempreg, tempreg, mips_gp_register);
5291 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5292 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5293
5294 if (expr1.X_add_number == 0)
5295 {
5296 p = frag_var (rs_machine_dependent, 8, 0,
5297 RELAX_ENCODE (12, 8, 0, 4, 0,
5298 mips_opts.warn_about_macros),
5299 offset_expr.X_add_symbol, 0, NULL);
5300 }
5301 else if (expr1.X_add_number >= -0x8000
5302 && expr1.X_add_number < 0x8000)
5303 {
5304 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5305 tempreg, tempreg, BFD_RELOC_LO16);
5306 p = frag_var (rs_machine_dependent, 8, 0,
5307 RELAX_ENCODE (16, 8, 0, 4, 0,
5308 mips_opts.warn_about_macros),
5309 offset_expr.X_add_symbol, 0, NULL);
5310 }
5311 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5312 {
5313 int dreg;
5314
5315 /* If we are going to add in a base register, and the
5316 target register and the base register are the same,
5317 then we are using AT as a temporary register. Since
5318 we want to load the constant into AT, we add our
5319 current AT (from the global offset table) and the
5320 register into the register now, and pretend we were
5321 not using a base register. */
5322 if (breg != treg)
5323 dreg = tempreg;
5324 else
5325 {
5326 assert (tempreg == AT);
5327 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5328 treg, AT, breg);
5329 dreg = treg;
5330 adj = 4;
5331 }
5332
5333 /* Set mips_optimize around the lui instruction to avoid
5334 inserting an unnecessary nop after the lw. */
5335 macro_build_lui (NULL, &icnt, &expr1, AT);
5336 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5337 "t,r,j", AT, AT, BFD_RELOC_LO16);
5338 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5339 dreg, dreg, AT);
5340
5341 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5342 RELAX_ENCODE (24 + adj, 8 + adj,
5343 0, 4, 0,
5344 (breg == 0
5345 ? mips_opts.warn_about_macros
5346 : 0)),
5347 offset_expr.X_add_symbol, 0, NULL);
5348
5349 used_at = 1;
5350 }
5351 else
5352 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5353
5354 offset_expr.X_add_number = expr1.X_add_number;
5355 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5356 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5357 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5358 tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
5359 if (adj)
5360 {
5361 macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5362 treg, tempreg, breg);
5363 breg = 0;
5364 tempreg = treg;
5365 }
5366 }
5367 else if (mips_pic == EMBEDDED_PIC)
5368 {
5369 /* We use
5370 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5371 */
5372 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5373 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5374 }
5375 else
5376 abort ();
5377
5378 if (breg != 0)
5379 {
5380 char *s;
5381
5382 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5383 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5384 else
5385 s = ADDRESS_ADD_INSN;
5386
5387 macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
5388 }
5389
5390 if (! used_at)
5391 return;
5392
5393 break;
5394
5395 case M_J_A:
5396 /* The j instruction may not be used in PIC code, since it
5397 requires an absolute address. We convert it to a b
5398 instruction. */
5399 if (mips_pic == NO_PIC)
5400 macro_build (NULL, &icnt, &offset_expr, "j", "a");
5401 else
5402 macro_build (NULL, &icnt, &offset_expr, "b", "p");
5403 return;
5404
5405 /* The jal instructions must be handled as macros because when
5406 generating PIC code they expand to multi-instruction
5407 sequences. Normally they are simple instructions. */
5408 case M_JAL_1:
5409 dreg = RA;
5410 /* Fall through. */
5411 case M_JAL_2:
5412 if (mips_pic == NO_PIC
5413 || mips_pic == EMBEDDED_PIC)
5414 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5415 else if (mips_pic == SVR4_PIC)
5416 {
5417 if (sreg != PIC_CALL_REG)
5418 as_warn (_("MIPS PIC call to register other than $25"));
5419
5420 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
5421 if (! HAVE_NEWABI)
5422 {
5423 if (mips_cprestore_offset < 0)
5424 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5425 else
5426 {
5427 if (! mips_frame_reg_valid)
5428 {
5429 as_warn (_("No .frame pseudo-op used in PIC code"));
5430 /* Quiet this warning. */
5431 mips_frame_reg_valid = 1;
5432 }
5433 if (! mips_cprestore_valid)
5434 {
5435 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5436 /* Quiet this warning. */
5437 mips_cprestore_valid = 1;
5438 }
5439 expr1.X_add_number = mips_cprestore_offset;
5440 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5441 ADDRESS_LOAD_INSN,
5442 mips_gp_register,
5443 mips_frame_reg);
5444 }
5445 }
5446 }
5447 else
5448 abort ();
5449
5450 return;
5451
5452 case M_JAL_A:
5453 if (mips_pic == NO_PIC)
5454 macro_build (NULL, &icnt, &offset_expr, "jal", "a");
5455 else if (mips_pic == SVR4_PIC)
5456 {
5457 char *p;
5458
5459 /* If this is a reference to an external symbol, and we are
5460 using a small GOT, we want
5461 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5462 nop
5463 jalr $ra,$25
5464 nop
5465 lw $gp,cprestore($sp)
5466 The cprestore value is set using the .cprestore
5467 pseudo-op. If we are using a big GOT, we want
5468 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5469 addu $25,$25,$gp
5470 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5471 nop
5472 jalr $ra,$25
5473 nop
5474 lw $gp,cprestore($sp)
5475 If the symbol is not external, we want
5476 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5477 nop
5478 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5479 jalr $ra,$25
5480 nop
5481 lw $gp,cprestore($sp)
5482
5483 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5484 sequences above, minus nops, unless the symbol is local,
5485 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5486 GOT_DISP. */
5487 if (HAVE_NEWABI)
5488 {
5489 if (! mips_big_got)
5490 {
5491 frag_grow (4);
5492 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5493 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5494 mips_gp_register);
5495 frag_var (rs_machine_dependent, 0, 0,
5496 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5497 offset_expr.X_add_symbol, 0, NULL);
5498 }
5499 else
5500 {
5501 frag_grow (20);
5502 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5503 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5504 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5505 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5506 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5507 "t,o(b)", PIC_CALL_REG,
5508 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5509 p = frag_var (rs_machine_dependent, 8, 0,
5510 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5511 offset_expr.X_add_symbol, 0, NULL);
5512 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5513 "t,o(b)", PIC_CALL_REG,
5514 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5515 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5516 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5517 BFD_RELOC_MIPS_GOT_OFST);
5518 }
5519
5520 macro_build_jalr (icnt, &offset_expr);
5521 }
5522 else
5523 {
5524 frag_grow (40);
5525 if (! mips_big_got)
5526 {
5527 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5528 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5529 mips_gp_register);
5530 macro_build (NULL, &icnt, NULL, "nop", "");
5531 p = frag_var (rs_machine_dependent, 4, 0,
5532 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5533 offset_expr.X_add_symbol, 0, NULL);
5534 }
5535 else
5536 {
5537 int gpdel;
5538
5539 if (reg_needs_delay (mips_gp_register))
5540 gpdel = 4;
5541 else
5542 gpdel = 0;
5543 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5544 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5545 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5546 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5547 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5548 "t,o(b)", PIC_CALL_REG,
5549 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5550 macro_build (NULL, &icnt, NULL, "nop", "");
5551 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5552 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5553 8 + gpdel, 0, 0),
5554 offset_expr.X_add_symbol, 0, NULL);
5555 if (gpdel > 0)
5556 {
5557 macro_build (p, &icnt, NULL, "nop", "");
5558 p += 4;
5559 }
5560 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5561 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5562 mips_gp_register);
5563 p += 4;
5564 macro_build (p, &icnt, NULL, "nop", "");
5565 p += 4;
5566 }
5567 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
5568 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5569 BFD_RELOC_LO16);
5570 macro_build_jalr (icnt, &offset_expr);
5571
5572 if (mips_cprestore_offset < 0)
5573 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5574 else
5575 {
5576 if (! mips_frame_reg_valid)
5577 {
5578 as_warn (_("No .frame pseudo-op used in PIC code"));
5579 /* Quiet this warning. */
5580 mips_frame_reg_valid = 1;
5581 }
5582 if (! mips_cprestore_valid)
5583 {
5584 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5585 /* Quiet this warning. */
5586 mips_cprestore_valid = 1;
5587 }
5588 if (mips_opts.noreorder)
5589 macro_build (NULL, &icnt, NULL, "nop", "");
5590 expr1.X_add_number = mips_cprestore_offset;
5591 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
5592 ADDRESS_LOAD_INSN,
5593 mips_gp_register,
5594 mips_frame_reg);
5595 }
5596 }
5597 }
5598 else if (mips_pic == EMBEDDED_PIC)
5599 {
5600 macro_build (NULL, &icnt, &offset_expr, "bal", "p");
5601 /* The linker may expand the call to a longer sequence which
5602 uses $at, so we must break rather than return. */
5603 break;
5604 }
5605 else
5606 abort ();
5607
5608 return;
5609
5610 case M_LB_AB:
5611 s = "lb";
5612 goto ld;
5613 case M_LBU_AB:
5614 s = "lbu";
5615 goto ld;
5616 case M_LH_AB:
5617 s = "lh";
5618 goto ld;
5619 case M_LHU_AB:
5620 s = "lhu";
5621 goto ld;
5622 case M_LW_AB:
5623 s = "lw";
5624 goto ld;
5625 case M_LWC0_AB:
5626 s = "lwc0";
5627 /* Itbl support may require additional care here. */
5628 coproc = 1;
5629 goto ld;
5630 case M_LWC1_AB:
5631 s = "lwc1";
5632 /* Itbl support may require additional care here. */
5633 coproc = 1;
5634 goto ld;
5635 case M_LWC2_AB:
5636 s = "lwc2";
5637 /* Itbl support may require additional care here. */
5638 coproc = 1;
5639 goto ld;
5640 case M_LWC3_AB:
5641 s = "lwc3";
5642 /* Itbl support may require additional care here. */
5643 coproc = 1;
5644 goto ld;
5645 case M_LWL_AB:
5646 s = "lwl";
5647 lr = 1;
5648 goto ld;
5649 case M_LWR_AB:
5650 s = "lwr";
5651 lr = 1;
5652 goto ld;
5653 case M_LDC1_AB:
5654 if (mips_opts.arch == CPU_R4650)
5655 {
5656 as_bad (_("opcode not supported on this processor"));
5657 return;
5658 }
5659 s = "ldc1";
5660 /* Itbl support may require additional care here. */
5661 coproc = 1;
5662 goto ld;
5663 case M_LDC2_AB:
5664 s = "ldc2";
5665 /* Itbl support may require additional care here. */
5666 coproc = 1;
5667 goto ld;
5668 case M_LDC3_AB:
5669 s = "ldc3";
5670 /* Itbl support may require additional care here. */
5671 coproc = 1;
5672 goto ld;
5673 case M_LDL_AB:
5674 s = "ldl";
5675 lr = 1;
5676 goto ld;
5677 case M_LDR_AB:
5678 s = "ldr";
5679 lr = 1;
5680 goto ld;
5681 case M_LL_AB:
5682 s = "ll";
5683 goto ld;
5684 case M_LLD_AB:
5685 s = "lld";
5686 goto ld;
5687 case M_LWU_AB:
5688 s = "lwu";
5689 ld:
5690 if (breg == treg || coproc || lr)
5691 {
5692 tempreg = AT;
5693 used_at = 1;
5694 }
5695 else
5696 {
5697 tempreg = treg;
5698 used_at = 0;
5699 }
5700 goto ld_st;
5701 case M_SB_AB:
5702 s = "sb";
5703 goto st;
5704 case M_SH_AB:
5705 s = "sh";
5706 goto st;
5707 case M_SW_AB:
5708 s = "sw";
5709 goto st;
5710 case M_SWC0_AB:
5711 s = "swc0";
5712 /* Itbl support may require additional care here. */
5713 coproc = 1;
5714 goto st;
5715 case M_SWC1_AB:
5716 s = "swc1";
5717 /* Itbl support may require additional care here. */
5718 coproc = 1;
5719 goto st;
5720 case M_SWC2_AB:
5721 s = "swc2";
5722 /* Itbl support may require additional care here. */
5723 coproc = 1;
5724 goto st;
5725 case M_SWC3_AB:
5726 s = "swc3";
5727 /* Itbl support may require additional care here. */
5728 coproc = 1;
5729 goto st;
5730 case M_SWL_AB:
5731 s = "swl";
5732 goto st;
5733 case M_SWR_AB:
5734 s = "swr";
5735 goto st;
5736 case M_SC_AB:
5737 s = "sc";
5738 goto st;
5739 case M_SCD_AB:
5740 s = "scd";
5741 goto st;
5742 case M_SDC1_AB:
5743 if (mips_opts.arch == CPU_R4650)
5744 {
5745 as_bad (_("opcode not supported on this processor"));
5746 return;
5747 }
5748 s = "sdc1";
5749 coproc = 1;
5750 /* Itbl support may require additional care here. */
5751 goto st;
5752 case M_SDC2_AB:
5753 s = "sdc2";
5754 /* Itbl support may require additional care here. */
5755 coproc = 1;
5756 goto st;
5757 case M_SDC3_AB:
5758 s = "sdc3";
5759 /* Itbl support may require additional care here. */
5760 coproc = 1;
5761 goto st;
5762 case M_SDL_AB:
5763 s = "sdl";
5764 goto st;
5765 case M_SDR_AB:
5766 s = "sdr";
5767 st:
5768 tempreg = AT;
5769 used_at = 1;
5770 ld_st:
5771 /* Itbl support may require additional care here. */
5772 if (mask == M_LWC1_AB
5773 || mask == M_SWC1_AB
5774 || mask == M_LDC1_AB
5775 || mask == M_SDC1_AB
5776 || mask == M_L_DAB
5777 || mask == M_S_DAB)
5778 fmt = "T,o(b)";
5779 else if (coproc)
5780 fmt = "E,o(b)";
5781 else
5782 fmt = "t,o(b)";
5783
5784 /* For embedded PIC, we allow loads where the offset is calculated
5785 by subtracting a symbol in the current segment from an unknown
5786 symbol, relative to a base register, e.g.:
5787 <op> $treg, <sym>-<localsym>($breg)
5788 This is used by the compiler for switch statements. */
5789 if (mips_pic == EMBEDDED_PIC
5790 && offset_expr.X_op == O_subtract
5791 && (symbol_constant_p (offset_expr.X_op_symbol)
5792 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5793 : (symbol_equated_p (offset_expr.X_op_symbol)
5794 && (S_GET_SEGMENT
5795 (symbol_get_value_expression (offset_expr.X_op_symbol)
5796 ->X_add_symbol)
5797 == now_seg)))
5798 && breg != 0
5799 && (offset_expr.X_add_number == 0
5800 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5801 {
5802 /* For this case, we output the instructions:
5803 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5804 addiu $tempreg,$tempreg,$breg
5805 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5806 If the relocation would fit entirely in 16 bits, it would be
5807 nice to emit:
5808 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5809 instead, but that seems quite difficult. */
5810 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
5811 BFD_RELOC_PCREL_HI16_S);
5812 macro_build (NULL, &icnt, NULL,
5813 ((bfd_arch_bits_per_address (stdoutput) == 32
5814 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5815 ? "addu" : "daddu"),
5816 "d,v,t", tempreg, tempreg, breg);
5817 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5818 BFD_RELOC_PCREL_LO16, tempreg);
5819 if (! used_at)
5820 return;
5821 break;
5822 }
5823
5824 if (offset_expr.X_op != O_constant
5825 && offset_expr.X_op != O_symbol)
5826 {
5827 as_bad (_("expression too complex"));
5828 offset_expr.X_op = O_constant;
5829 }
5830
5831 /* A constant expression in PIC code can be handled just as it
5832 is in non PIC code. */
5833 if (mips_pic == NO_PIC
5834 || offset_expr.X_op == O_constant)
5835 {
5836 char *p;
5837
5838 /* If this is a reference to a GP relative symbol, and there
5839 is no base register, we want
5840 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5841 Otherwise, if there is no base register, we want
5842 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5843 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5844 If we have a constant, we need two instructions anyhow,
5845 so we always use the latter form.
5846
5847 If we have a base register, and this is a reference to a
5848 GP relative symbol, we want
5849 addu $tempreg,$breg,$gp
5850 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5851 Otherwise we want
5852 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5853 addu $tempreg,$tempreg,$breg
5854 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5855 With a constant we always use the latter case.
5856
5857 With 64bit address space and no base register and $at usable,
5858 we want
5859 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5860 lui $at,<sym> (BFD_RELOC_HI16_S)
5861 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5862 dsll32 $tempreg,0
5863 daddu $tempreg,$at
5864 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5865 If we have a base register, we want
5866 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5867 lui $at,<sym> (BFD_RELOC_HI16_S)
5868 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5869 daddu $at,$breg
5870 dsll32 $tempreg,0
5871 daddu $tempreg,$at
5872 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5873
5874 Without $at we can't generate the optimal path for superscalar
5875 processors here since this would require two temporary registers.
5876 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5877 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5878 dsll $tempreg,16
5879 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5880 dsll $tempreg,16
5881 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5882 If we have a base register, we want
5883 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5884 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5885 dsll $tempreg,16
5886 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5887 dsll $tempreg,16
5888 daddu $tempreg,$tempreg,$breg
5889 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5890
5891 If we have 64-bit addresses, as an optimization, for
5892 addresses which are 32-bit constants (e.g. kseg0/kseg1
5893 addresses) we fall back to the 32-bit address generation
5894 mechanism since it is more efficient. Note that due to
5895 the signed offset used by memory operations, the 32-bit
5896 range is shifted down by 32768 here. This code should
5897 probably attempt to generate 64-bit constants more
5898 efficiently in general.
5899
5900 As an extension for architectures with 64-bit registers,
5901 we don't truncate 64-bit addresses given as literal
5902 constants down to 32 bits, to support existing practice
5903 in the mips64 Linux (the kernel), that compiles source
5904 files with -mabi=64, assembling them as o32 or n32 (with
5905 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
5906 the whole kernel is loaded into a memory region that is
5907 addressible with sign-extended 32-bit addresses, it is
5908 wasteful to compute the upper 32 bits of every
5909 non-literal address, that takes more space and time.
5910 Some day this should probably be implemented as an
5911 assembler option, such that the kernel doesn't have to
5912 use such ugly hacks, even though it will still have to
5913 end up converting the binary to ELF32 for a number of
5914 platforms whose boot loaders don't support ELF64
5915 binaries. */
5916 if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
5917 || (offset_expr.X_op == O_constant
5918 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
5919 && HAVE_64BIT_ADDRESS_CONSTANTS))
5920 {
5921 p = NULL;
5922
5923 /* We don't do GP optimization for now because RELAX_ENCODE can't
5924 hold the data for such large chunks. */
5925
5926 if (used_at == 0 && ! mips_opts.noat)
5927 {
5928 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5929 tempreg, BFD_RELOC_MIPS_HIGHEST);
5930 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5931 AT, BFD_RELOC_HI16_S);
5932 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5933 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5934 if (breg != 0)
5935 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5936 AT, AT, breg);
5937 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
5938 tempreg, tempreg, 0);
5939 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5940 tempreg, tempreg, AT);
5941 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5942 BFD_RELOC_LO16, tempreg);
5943 used_at = 1;
5944 }
5945 else
5946 {
5947 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5948 tempreg, BFD_RELOC_MIPS_HIGHEST);
5949 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5950 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5951 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
5952 tempreg, tempreg, 16);
5953 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5954 tempreg, tempreg, BFD_RELOC_HI16_S);
5955 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
5956 tempreg, tempreg, 16);
5957 if (breg != 0)
5958 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
5959 tempreg, tempreg, breg);
5960 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5961 BFD_RELOC_LO16, tempreg);
5962 }
5963
5964 return;
5965 }
5966 else if (offset_expr.X_op == O_constant
5967 && !HAVE_64BIT_ADDRESS_CONSTANTS
5968 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5969 as_bad (_("load/store address overflow (max 32 bits)"));
5970
5971 if (breg == 0)
5972 {
5973 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5974 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5975 p = NULL;
5976 else
5977 {
5978 frag_grow (20);
5979 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5980 BFD_RELOC_GPREL16, mips_gp_register);
5981 p = frag_var (rs_machine_dependent, 8, 0,
5982 RELAX_ENCODE (4, 8, 0, 4, 0,
5983 (mips_opts.warn_about_macros
5984 || (used_at
5985 && mips_opts.noat))),
5986 offset_expr.X_add_symbol, 0, NULL);
5987 used_at = 0;
5988 }
5989 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5990 if (p != NULL)
5991 p += 4;
5992 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5993 BFD_RELOC_LO16, tempreg);
5994 }
5995 else
5996 {
5997 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5998 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5999 p = NULL;
6000 else
6001 {
6002 frag_grow (28);
6003 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6004 tempreg, breg, mips_gp_register);
6005 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6006 BFD_RELOC_GPREL16, tempreg);
6007 p = frag_var (rs_machine_dependent, 12, 0,
6008 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6009 offset_expr.X_add_symbol, 0, NULL);
6010 }
6011 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6012 if (p != NULL)
6013 p += 4;
6014 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6015 tempreg, tempreg, breg);
6016 if (p != NULL)
6017 p += 4;
6018 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6019 BFD_RELOC_LO16, tempreg);
6020 }
6021 }
6022 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6023 {
6024 char *p;
6025 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6026
6027 /* If this is a reference to an external symbol, we want
6028 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6029 nop
6030 <op> $treg,0($tempreg)
6031 Otherwise we want
6032 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6033 nop
6034 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6035 <op> $treg,0($tempreg)
6036
6037 For NewABI, we want
6038 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6039 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6040
6041 If there is a base register, we add it to $tempreg before
6042 the <op>. If there is a constant, we stick it in the
6043 <op> instruction. We don't handle constants larger than
6044 16 bits, because we have no way to load the upper 16 bits
6045 (actually, we could handle them for the subset of cases
6046 in which we are not using $at). */
6047 assert (offset_expr.X_op == O_symbol);
6048 if (HAVE_NEWABI)
6049 {
6050 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6051 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6052 mips_gp_register);
6053 if (breg != 0)
6054 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6055 tempreg, tempreg, breg);
6056 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6057 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6058
6059 if (! used_at)
6060 return;
6061
6062 break;
6063 }
6064 expr1.X_add_number = offset_expr.X_add_number;
6065 offset_expr.X_add_number = 0;
6066 if (expr1.X_add_number < -0x8000
6067 || expr1.X_add_number >= 0x8000)
6068 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6069 frag_grow (20);
6070 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6071 tempreg, lw_reloc_type, mips_gp_register);
6072 macro_build (NULL, &icnt, NULL, "nop", "");
6073 p = frag_var (rs_machine_dependent, 4, 0,
6074 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6075 offset_expr.X_add_symbol, 0, NULL);
6076 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
6077 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
6078 if (breg != 0)
6079 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6080 tempreg, tempreg, breg);
6081 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6082 tempreg);
6083 }
6084 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6085 {
6086 int gpdel;
6087 char *p;
6088
6089 /* If this is a reference to an external symbol, we want
6090 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6091 addu $tempreg,$tempreg,$gp
6092 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6093 <op> $treg,0($tempreg)
6094 Otherwise we want
6095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6096 nop
6097 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6098 <op> $treg,0($tempreg)
6099 If there is a base register, we add it to $tempreg before
6100 the <op>. If there is a constant, we stick it in the
6101 <op> instruction. We don't handle constants larger than
6102 16 bits, because we have no way to load the upper 16 bits
6103 (actually, we could handle them for the subset of cases
6104 in which we are not using $at). */
6105 assert (offset_expr.X_op == O_symbol);
6106 expr1.X_add_number = offset_expr.X_add_number;
6107 offset_expr.X_add_number = 0;
6108 if (expr1.X_add_number < -0x8000
6109 || expr1.X_add_number >= 0x8000)
6110 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6111 if (reg_needs_delay (mips_gp_register))
6112 gpdel = 4;
6113 else
6114 gpdel = 0;
6115 frag_grow (36);
6116 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6117 BFD_RELOC_MIPS_GOT_HI16);
6118 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6119 tempreg, tempreg, mips_gp_register);
6120 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6121 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6122 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6123 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6124 offset_expr.X_add_symbol, 0, NULL);
6125 if (gpdel > 0)
6126 {
6127 macro_build (p, &icnt, NULL, "nop", "");
6128 p += 4;
6129 }
6130 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6131 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6132 p += 4;
6133 macro_build (p, &icnt, NULL, "nop", "");
6134 p += 4;
6135 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6136 tempreg, tempreg, BFD_RELOC_LO16);
6137 if (breg != 0)
6138 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6139 tempreg, tempreg, breg);
6140 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6141 tempreg);
6142 }
6143 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6144 {
6145 char *p;
6146 int bregsz = breg != 0 ? 4 : 0;
6147
6148 /* If this is a reference to an external symbol, we want
6149 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6150 add $tempreg,$tempreg,$gp
6151 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6152 <op> $treg,<ofst>($tempreg)
6153 Otherwise, for local symbols, we want:
6154 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6155 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6156 assert (offset_expr.X_op == O_symbol);
6157 frag_now->tc_frag_data.tc_fr_offset =
6158 expr1.X_add_number = offset_expr.X_add_number;
6159 offset_expr.X_add_number = 0;
6160 if (expr1.X_add_number < -0x8000
6161 || expr1.X_add_number >= 0x8000)
6162 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6163 frag_grow (36);
6164 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6165 BFD_RELOC_MIPS_GOT_HI16);
6166 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6167 tempreg, tempreg, mips_gp_register);
6168 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6169 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
6170 if (breg != 0)
6171 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6172 tempreg, tempreg, breg);
6173 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6174 tempreg);
6175
6176 offset_expr.X_add_number = expr1.X_add_number;
6177 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6178 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6179 0, 4 + bregsz, 0, 0),
6180 offset_expr.X_add_symbol, 0, NULL);
6181 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6182 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6183 if (breg != 0)
6184 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6185 tempreg, tempreg, breg);
6186 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6187 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6188 }
6189 else if (mips_pic == EMBEDDED_PIC)
6190 {
6191 /* If there is no base register, we want
6192 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6193 If there is a base register, we want
6194 addu $tempreg,$breg,$gp
6195 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6196 */
6197 assert (offset_expr.X_op == O_symbol);
6198 if (breg == 0)
6199 {
6200 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6201 BFD_RELOC_GPREL16, mips_gp_register);
6202 used_at = 0;
6203 }
6204 else
6205 {
6206 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6207 tempreg, breg, mips_gp_register);
6208 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6209 BFD_RELOC_GPREL16, tempreg);
6210 }
6211 }
6212 else
6213 abort ();
6214
6215 if (! used_at)
6216 return;
6217
6218 break;
6219
6220 case M_LI:
6221 case M_LI_S:
6222 load_register (&icnt, treg, &imm_expr, 0);
6223 return;
6224
6225 case M_DLI:
6226 load_register (&icnt, treg, &imm_expr, 1);
6227 return;
6228
6229 case M_LI_SS:
6230 if (imm_expr.X_op == O_constant)
6231 {
6232 load_register (&icnt, AT, &imm_expr, 0);
6233 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6234 break;
6235 }
6236 else
6237 {
6238 assert (offset_expr.X_op == O_symbol
6239 && strcmp (segment_name (S_GET_SEGMENT
6240 (offset_expr.X_add_symbol)),
6241 ".lit4") == 0
6242 && offset_expr.X_add_number == 0);
6243 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6244 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6245 return;
6246 }
6247
6248 case M_LI_D:
6249 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6250 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6251 order 32 bits of the value and the low order 32 bits are either
6252 zero or in OFFSET_EXPR. */
6253 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6254 {
6255 if (HAVE_64BIT_GPRS)
6256 load_register (&icnt, treg, &imm_expr, 1);
6257 else
6258 {
6259 int hreg, lreg;
6260
6261 if (target_big_endian)
6262 {
6263 hreg = treg;
6264 lreg = treg + 1;
6265 }
6266 else
6267 {
6268 hreg = treg + 1;
6269 lreg = treg;
6270 }
6271
6272 if (hreg <= 31)
6273 load_register (&icnt, hreg, &imm_expr, 0);
6274 if (lreg <= 31)
6275 {
6276 if (offset_expr.X_op == O_absent)
6277 move_register (&icnt, lreg, 0);
6278 else
6279 {
6280 assert (offset_expr.X_op == O_constant);
6281 load_register (&icnt, lreg, &offset_expr, 0);
6282 }
6283 }
6284 }
6285 return;
6286 }
6287
6288 /* We know that sym is in the .rdata section. First we get the
6289 upper 16 bits of the address. */
6290 if (mips_pic == NO_PIC)
6291 {
6292 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6293 }
6294 else if (mips_pic == SVR4_PIC)
6295 {
6296 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6297 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6298 }
6299 else if (mips_pic == EMBEDDED_PIC)
6300 {
6301 /* For embedded PIC we pick up the entire address off $gp in
6302 a single instruction. */
6303 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6304 AT, mips_gp_register, BFD_RELOC_GPREL16);
6305 offset_expr.X_op = O_constant;
6306 offset_expr.X_add_number = 0;
6307 }
6308 else
6309 abort ();
6310
6311 /* Now we load the register(s). */
6312 if (HAVE_64BIT_GPRS)
6313 macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6314 BFD_RELOC_LO16, AT);
6315 else
6316 {
6317 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6318 BFD_RELOC_LO16, AT);
6319 if (treg != RA)
6320 {
6321 /* FIXME: How in the world do we deal with the possible
6322 overflow here? */
6323 offset_expr.X_add_number += 4;
6324 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6325 treg + 1, BFD_RELOC_LO16, AT);
6326 }
6327 }
6328
6329 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6330 does not become a variant frag. */
6331 frag_wane (frag_now);
6332 frag_new (0);
6333
6334 break;
6335
6336 case M_LI_DD:
6337 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6338 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6339 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6340 the value and the low order 32 bits are either zero or in
6341 OFFSET_EXPR. */
6342 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6343 {
6344 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6345 if (HAVE_64BIT_FPRS)
6346 {
6347 assert (HAVE_64BIT_GPRS);
6348 macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
6349 }
6350 else
6351 {
6352 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
6353 if (offset_expr.X_op == O_absent)
6354 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
6355 else
6356 {
6357 assert (offset_expr.X_op == O_constant);
6358 load_register (&icnt, AT, &offset_expr, 0);
6359 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
6360 }
6361 }
6362 break;
6363 }
6364
6365 assert (offset_expr.X_op == O_symbol
6366 && offset_expr.X_add_number == 0);
6367 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6368 if (strcmp (s, ".lit8") == 0)
6369 {
6370 if (mips_opts.isa != ISA_MIPS1)
6371 {
6372 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6373 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6374 return;
6375 }
6376 breg = mips_gp_register;
6377 r = BFD_RELOC_MIPS_LITERAL;
6378 goto dob;
6379 }
6380 else
6381 {
6382 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6383 if (mips_pic == SVR4_PIC)
6384 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6385 "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6386 mips_gp_register);
6387 else
6388 {
6389 /* FIXME: This won't work for a 64 bit address. */
6390 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6391 }
6392
6393 if (mips_opts.isa != ISA_MIPS1)
6394 {
6395 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6396 BFD_RELOC_LO16, AT);
6397
6398 /* To avoid confusion in tc_gen_reloc, we must ensure
6399 that this does not become a variant frag. */
6400 frag_wane (frag_now);
6401 frag_new (0);
6402
6403 break;
6404 }
6405 breg = AT;
6406 r = BFD_RELOC_LO16;
6407 goto dob;
6408 }
6409
6410 case M_L_DOB:
6411 if (mips_opts.arch == CPU_R4650)
6412 {
6413 as_bad (_("opcode not supported on this processor"));
6414 return;
6415 }
6416 /* Even on a big endian machine $fn comes before $fn+1. We have
6417 to adjust when loading from memory. */
6418 r = BFD_RELOC_LO16;
6419 dob:
6420 assert (mips_opts.isa == ISA_MIPS1);
6421 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6422 target_big_endian ? treg + 1 : treg, r, breg);
6423 /* FIXME: A possible overflow which I don't know how to deal
6424 with. */
6425 offset_expr.X_add_number += 4;
6426 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6427 target_big_endian ? treg : treg + 1, r, breg);
6428
6429 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6430 does not become a variant frag. */
6431 frag_wane (frag_now);
6432 frag_new (0);
6433
6434 if (breg != AT)
6435 return;
6436 break;
6437
6438 case M_L_DAB:
6439 /*
6440 * The MIPS assembler seems to check for X_add_number not
6441 * being double aligned and generating:
6442 * lui at,%hi(foo+1)
6443 * addu at,at,v1
6444 * addiu at,at,%lo(foo+1)
6445 * lwc1 f2,0(at)
6446 * lwc1 f3,4(at)
6447 * But, the resulting address is the same after relocation so why
6448 * generate the extra instruction?
6449 */
6450 if (mips_opts.arch == CPU_R4650)
6451 {
6452 as_bad (_("opcode not supported on this processor"));
6453 return;
6454 }
6455 /* Itbl support may require additional care here. */
6456 coproc = 1;
6457 if (mips_opts.isa != ISA_MIPS1)
6458 {
6459 s = "ldc1";
6460 goto ld;
6461 }
6462
6463 s = "lwc1";
6464 fmt = "T,o(b)";
6465 goto ldd_std;
6466
6467 case M_S_DAB:
6468 if (mips_opts.arch == CPU_R4650)
6469 {
6470 as_bad (_("opcode not supported on this processor"));
6471 return;
6472 }
6473
6474 if (mips_opts.isa != ISA_MIPS1)
6475 {
6476 s = "sdc1";
6477 goto st;
6478 }
6479
6480 s = "swc1";
6481 fmt = "T,o(b)";
6482 /* Itbl support may require additional care here. */
6483 coproc = 1;
6484 goto ldd_std;
6485
6486 case M_LD_AB:
6487 if (HAVE_64BIT_GPRS)
6488 {
6489 s = "ld";
6490 goto ld;
6491 }
6492
6493 s = "lw";
6494 fmt = "t,o(b)";
6495 goto ldd_std;
6496
6497 case M_SD_AB:
6498 if (HAVE_64BIT_GPRS)
6499 {
6500 s = "sd";
6501 goto st;
6502 }
6503
6504 s = "sw";
6505 fmt = "t,o(b)";
6506
6507 ldd_std:
6508 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6509 loads for the case of doing a pair of loads to simulate an 'ld'.
6510 This is not currently done by the compiler, and assembly coders
6511 writing embedded-pic code can cope. */
6512
6513 if (offset_expr.X_op != O_symbol
6514 && offset_expr.X_op != O_constant)
6515 {
6516 as_bad (_("expression too complex"));
6517 offset_expr.X_op = O_constant;
6518 }
6519
6520 /* Even on a big endian machine $fn comes before $fn+1. We have
6521 to adjust when loading from memory. We set coproc if we must
6522 load $fn+1 first. */
6523 /* Itbl support may require additional care here. */
6524 if (! target_big_endian)
6525 coproc = 0;
6526
6527 if (mips_pic == NO_PIC
6528 || offset_expr.X_op == O_constant)
6529 {
6530 char *p;
6531
6532 /* If this is a reference to a GP relative symbol, we want
6533 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6534 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6535 If we have a base register, we use this
6536 addu $at,$breg,$gp
6537 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6538 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6539 If this is not a GP relative symbol, we want
6540 lui $at,<sym> (BFD_RELOC_HI16_S)
6541 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6542 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6543 If there is a base register, we add it to $at after the
6544 lui instruction. If there is a constant, we always use
6545 the last case. */
6546 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6547 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6548 {
6549 p = NULL;
6550 used_at = 1;
6551 }
6552 else
6553 {
6554 int off;
6555
6556 if (breg == 0)
6557 {
6558 frag_grow (28);
6559 tempreg = mips_gp_register;
6560 off = 0;
6561 used_at = 0;
6562 }
6563 else
6564 {
6565 frag_grow (36);
6566 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6567 AT, breg, mips_gp_register);
6568 tempreg = AT;
6569 off = 4;
6570 used_at = 1;
6571 }
6572
6573 /* Itbl support may require additional care here. */
6574 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6575 coproc ? treg + 1 : treg,
6576 BFD_RELOC_GPREL16, tempreg);
6577 offset_expr.X_add_number += 4;
6578
6579 /* Set mips_optimize to 2 to avoid inserting an
6580 undesired nop. */
6581 hold_mips_optimize = mips_optimize;
6582 mips_optimize = 2;
6583 /* Itbl support may require additional care here. */
6584 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6585 coproc ? treg : treg + 1,
6586 BFD_RELOC_GPREL16, tempreg);
6587 mips_optimize = hold_mips_optimize;
6588
6589 p = frag_var (rs_machine_dependent, 12 + off, 0,
6590 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6591 used_at && mips_opts.noat),
6592 offset_expr.X_add_symbol, 0, NULL);
6593
6594 /* We just generated two relocs. When tc_gen_reloc
6595 handles this case, it will skip the first reloc and
6596 handle the second. The second reloc already has an
6597 extra addend of 4, which we added above. We must
6598 subtract it out, and then subtract another 4 to make
6599 the first reloc come out right. The second reloc
6600 will come out right because we are going to add 4 to
6601 offset_expr when we build its instruction below.
6602
6603 If we have a symbol, then we don't want to include
6604 the offset, because it will wind up being included
6605 when we generate the reloc. */
6606
6607 if (offset_expr.X_op == O_constant)
6608 offset_expr.X_add_number -= 8;
6609 else
6610 {
6611 offset_expr.X_add_number = -4;
6612 offset_expr.X_op = O_constant;
6613 }
6614 }
6615 macro_build_lui (p, &icnt, &offset_expr, AT);
6616 if (p != NULL)
6617 p += 4;
6618 if (breg != 0)
6619 {
6620 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6621 AT, breg, AT);
6622 if (p != NULL)
6623 p += 4;
6624 }
6625 /* Itbl support may require additional care here. */
6626 macro_build (p, &icnt, &offset_expr, s, fmt,
6627 coproc ? treg + 1 : treg,
6628 BFD_RELOC_LO16, AT);
6629 if (p != NULL)
6630 p += 4;
6631 /* FIXME: How do we handle overflow here? */
6632 offset_expr.X_add_number += 4;
6633 /* Itbl support may require additional care here. */
6634 macro_build (p, &icnt, &offset_expr, s, fmt,
6635 coproc ? treg : treg + 1,
6636 BFD_RELOC_LO16, AT);
6637 }
6638 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6639 {
6640 int off;
6641
6642 /* If this is a reference to an external symbol, we want
6643 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6644 nop
6645 <op> $treg,0($at)
6646 <op> $treg+1,4($at)
6647 Otherwise we want
6648 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6649 nop
6650 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6651 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6652 If there is a base register we add it to $at before the
6653 lwc1 instructions. If there is a constant we include it
6654 in the lwc1 instructions. */
6655 used_at = 1;
6656 expr1.X_add_number = offset_expr.X_add_number;
6657 offset_expr.X_add_number = 0;
6658 if (expr1.X_add_number < -0x8000
6659 || expr1.X_add_number >= 0x8000 - 4)
6660 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6661 if (breg == 0)
6662 off = 0;
6663 else
6664 off = 4;
6665 frag_grow (24 + off);
6666 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6667 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6668 macro_build (NULL, &icnt, NULL, "nop", "");
6669 if (breg != 0)
6670 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6671 AT, breg, AT);
6672 /* Itbl support may require additional care here. */
6673 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6674 BFD_RELOC_LO16, AT);
6675 expr1.X_add_number += 4;
6676
6677 /* Set mips_optimize to 2 to avoid inserting an undesired
6678 nop. */
6679 hold_mips_optimize = mips_optimize;
6680 mips_optimize = 2;
6681 /* Itbl support may require additional care here. */
6682 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6683 BFD_RELOC_LO16, AT);
6684 mips_optimize = hold_mips_optimize;
6685
6686 (void) frag_var (rs_machine_dependent, 0, 0,
6687 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6688 offset_expr.X_add_symbol, 0, NULL);
6689 }
6690 else if (mips_pic == SVR4_PIC)
6691 {
6692 int gpdel, off;
6693 char *p;
6694
6695 /* If this is a reference to an external symbol, we want
6696 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6697 addu $at,$at,$gp
6698 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6699 nop
6700 <op> $treg,0($at)
6701 <op> $treg+1,4($at)
6702 Otherwise we want
6703 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6704 nop
6705 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6706 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6707 If there is a base register we add it to $at before the
6708 lwc1 instructions. If there is a constant we include it
6709 in the lwc1 instructions. */
6710 used_at = 1;
6711 expr1.X_add_number = offset_expr.X_add_number;
6712 offset_expr.X_add_number = 0;
6713 if (expr1.X_add_number < -0x8000
6714 || expr1.X_add_number >= 0x8000 - 4)
6715 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6716 if (reg_needs_delay (mips_gp_register))
6717 gpdel = 4;
6718 else
6719 gpdel = 0;
6720 if (breg == 0)
6721 off = 0;
6722 else
6723 off = 4;
6724 frag_grow (56);
6725 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6726 BFD_RELOC_MIPS_GOT_HI16);
6727 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6728 AT, AT, mips_gp_register);
6729 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6730 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6731 macro_build (NULL, &icnt, NULL, "nop", "");
6732 if (breg != 0)
6733 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6734 AT, breg, AT);
6735 /* Itbl support may require additional care here. */
6736 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6737 BFD_RELOC_LO16, AT);
6738 expr1.X_add_number += 4;
6739
6740 /* Set mips_optimize to 2 to avoid inserting an undesired
6741 nop. */
6742 hold_mips_optimize = mips_optimize;
6743 mips_optimize = 2;
6744 /* Itbl support may require additional care here. */
6745 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6746 BFD_RELOC_LO16, AT);
6747 mips_optimize = hold_mips_optimize;
6748 expr1.X_add_number -= 4;
6749
6750 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6751 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6752 8 + gpdel + off, 1, 0),
6753 offset_expr.X_add_symbol, 0, NULL);
6754 if (gpdel > 0)
6755 {
6756 macro_build (p, &icnt, NULL, "nop", "");
6757 p += 4;
6758 }
6759 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6760 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6761 p += 4;
6762 macro_build (p, &icnt, NULL, "nop", "");
6763 p += 4;
6764 if (breg != 0)
6765 {
6766 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6767 AT, breg, AT);
6768 p += 4;
6769 }
6770 /* Itbl support may require additional care here. */
6771 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6772 BFD_RELOC_LO16, AT);
6773 p += 4;
6774 expr1.X_add_number += 4;
6775
6776 /* Set mips_optimize to 2 to avoid inserting an undesired
6777 nop. */
6778 hold_mips_optimize = mips_optimize;
6779 mips_optimize = 2;
6780 /* Itbl support may require additional care here. */
6781 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6782 BFD_RELOC_LO16, AT);
6783 mips_optimize = hold_mips_optimize;
6784 }
6785 else if (mips_pic == EMBEDDED_PIC)
6786 {
6787 /* If there is no base register, we use
6788 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6789 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6790 If we have a base register, we use
6791 addu $at,$breg,$gp
6792 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6793 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6794 */
6795 if (breg == 0)
6796 {
6797 tempreg = mips_gp_register;
6798 used_at = 0;
6799 }
6800 else
6801 {
6802 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6803 AT, breg, mips_gp_register);
6804 tempreg = AT;
6805 used_at = 1;
6806 }
6807
6808 /* Itbl support may require additional care here. */
6809 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6810 coproc ? treg + 1 : treg,
6811 BFD_RELOC_GPREL16, tempreg);
6812 offset_expr.X_add_number += 4;
6813 /* Itbl support may require additional care here. */
6814 macro_build (NULL, &icnt, &offset_expr, s, fmt,
6815 coproc ? treg : treg + 1,
6816 BFD_RELOC_GPREL16, tempreg);
6817 }
6818 else
6819 abort ();
6820
6821 if (! used_at)
6822 return;
6823
6824 break;
6825
6826 case M_LD_OB:
6827 s = "lw";
6828 goto sd_ob;
6829 case M_SD_OB:
6830 s = "sw";
6831 sd_ob:
6832 assert (HAVE_32BIT_ADDRESSES);
6833 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6834 BFD_RELOC_LO16, breg);
6835 offset_expr.X_add_number += 4;
6836 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6837 BFD_RELOC_LO16, breg);
6838 return;
6839
6840 /* New code added to support COPZ instructions.
6841 This code builds table entries out of the macros in mip_opcodes.
6842 R4000 uses interlocks to handle coproc delays.
6843 Other chips (like the R3000) require nops to be inserted for delays.
6844
6845 FIXME: Currently, we require that the user handle delays.
6846 In order to fill delay slots for non-interlocked chips,
6847 we must have a way to specify delays based on the coprocessor.
6848 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6849 What are the side-effects of the cop instruction?
6850 What cache support might we have and what are its effects?
6851 Both coprocessor & memory require delays. how long???
6852 What registers are read/set/modified?
6853
6854 If an itbl is provided to interpret cop instructions,
6855 this knowledge can be encoded in the itbl spec. */
6856
6857 case M_COP0:
6858 s = "c0";
6859 goto copz;
6860 case M_COP1:
6861 s = "c1";
6862 goto copz;
6863 case M_COP2:
6864 s = "c2";
6865 goto copz;
6866 case M_COP3:
6867 s = "c3";
6868 copz:
6869 /* For now we just do C (same as Cz). The parameter will be
6870 stored in insn_opcode by mips_ip. */
6871 macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
6872 return;
6873
6874 case M_MOVE:
6875 move_register (&icnt, dreg, sreg);
6876 return;
6877
6878 #ifdef LOSING_COMPILER
6879 default:
6880 /* Try and see if this is a new itbl instruction.
6881 This code builds table entries out of the macros in mip_opcodes.
6882 FIXME: For now we just assemble the expression and pass it's
6883 value along as a 32-bit immediate.
6884 We may want to have the assembler assemble this value,
6885 so that we gain the assembler's knowledge of delay slots,
6886 symbols, etc.
6887 Would it be more efficient to use mask (id) here? */
6888 if (itbl_have_entries
6889 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6890 {
6891 s = ip->insn_mo->name;
6892 s2 = "cop3";
6893 coproc = ITBL_DECODE_PNUM (immed_expr);;
6894 macro_build (NULL, &icnt, &immed_expr, s, "C");
6895 return;
6896 }
6897 macro2 (ip);
6898 return;
6899 }
6900 if (mips_opts.noat)
6901 as_warn (_("Macro used $at after \".set noat\""));
6902 }
6903
6904 static void
6905 macro2 (struct mips_cl_insn *ip)
6906 {
6907 register int treg, sreg, dreg, breg;
6908 int tempreg;
6909 int mask;
6910 int icnt = 0;
6911 int used_at;
6912 expressionS expr1;
6913 const char *s;
6914 const char *s2;
6915 const char *fmt;
6916 int likely = 0;
6917 int dbl = 0;
6918 int coproc = 0;
6919 int lr = 0;
6920 int imm = 0;
6921 int off;
6922 offsetT maxnum;
6923 bfd_reloc_code_real_type r;
6924 char *p;
6925
6926 treg = (ip->insn_opcode >> 16) & 0x1f;
6927 dreg = (ip->insn_opcode >> 11) & 0x1f;
6928 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6929 mask = ip->insn_mo->mask;
6930
6931 expr1.X_op = O_constant;
6932 expr1.X_op_symbol = NULL;
6933 expr1.X_add_symbol = NULL;
6934 expr1.X_add_number = 1;
6935
6936 switch (mask)
6937 {
6938 #endif /* LOSING_COMPILER */
6939
6940 case M_DMUL:
6941 dbl = 1;
6942 case M_MUL:
6943 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
6944 sreg, treg);
6945 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6946 return;
6947
6948 case M_DMUL_I:
6949 dbl = 1;
6950 case M_MUL_I:
6951 /* The MIPS assembler some times generates shifts and adds. I'm
6952 not trying to be that fancy. GCC should do this for us
6953 anyway. */
6954 load_register (&icnt, AT, &imm_expr, dbl);
6955 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
6956 sreg, AT);
6957 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6958 break;
6959
6960 case M_DMULO_I:
6961 dbl = 1;
6962 case M_MULO_I:
6963 imm = 1;
6964 goto do_mulo;
6965
6966 case M_DMULO:
6967 dbl = 1;
6968 case M_MULO:
6969 do_mulo:
6970 mips_emit_delays (TRUE);
6971 ++mips_opts.noreorder;
6972 mips_any_noreorder = 1;
6973 if (imm)
6974 load_register (&icnt, AT, &imm_expr, dbl);
6975 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
6976 sreg, imm ? AT : treg);
6977 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6978 macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
6979 dreg, dreg, RA);
6980 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
6981 if (mips_trap)
6982 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
6983 else
6984 {
6985 expr1.X_add_number = 8;
6986 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
6987 macro_build (NULL, &icnt, NULL, "nop", "", 0);
6988 macro_build (NULL, &icnt, NULL, "break", "c", 6);
6989 }
6990 --mips_opts.noreorder;
6991 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
6992 break;
6993
6994 case M_DMULOU_I:
6995 dbl = 1;
6996 case M_MULOU_I:
6997 imm = 1;
6998 goto do_mulou;
6999
7000 case M_DMULOU:
7001 dbl = 1;
7002 case M_MULOU:
7003 do_mulou:
7004 mips_emit_delays (TRUE);
7005 ++mips_opts.noreorder;
7006 mips_any_noreorder = 1;
7007 if (imm)
7008 load_register (&icnt, AT, &imm_expr, dbl);
7009 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7010 sreg, imm ? AT : treg);
7011 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7012 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7013 if (mips_trap)
7014 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
7015 else
7016 {
7017 expr1.X_add_number = 8;
7018 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7019 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7020 macro_build (NULL, &icnt, NULL, "break", "c", 6);
7021 }
7022 --mips_opts.noreorder;
7023 break;
7024
7025 case M_DROL:
7026 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7027 {
7028 if (dreg == sreg)
7029 {
7030 tempreg = AT;
7031 used_at = 1;
7032 }
7033 else
7034 {
7035 tempreg = dreg;
7036 used_at = 0;
7037 }
7038 macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7039 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7040 tempreg);
7041 if (used_at)
7042 break;
7043 return;
7044 }
7045 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7046 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7047 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7048 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7049 break;
7050
7051 case M_ROL:
7052 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7053 {
7054 if (dreg == sreg)
7055 {
7056 tempreg = AT;
7057 used_at = 1;
7058 }
7059 else
7060 {
7061 tempreg = dreg;
7062 used_at = 0;
7063 }
7064 macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7065 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7066 tempreg);
7067 if (used_at)
7068 break;
7069 return;
7070 }
7071 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7072 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7073 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7074 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7075 break;
7076
7077 case M_DROL_I:
7078 {
7079 unsigned int rot;
7080 char *l, *r;
7081
7082 if (imm_expr.X_op != O_constant)
7083 as_bad (_("Improper rotate count"));
7084 rot = imm_expr.X_add_number & 0x3f;
7085 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7086 {
7087 rot = (64 - rot) & 0x3f;
7088 if (rot >= 32)
7089 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7090 dreg, sreg, rot - 32);
7091 else
7092 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7093 dreg, sreg, rot);
7094 return;
7095 }
7096 if (rot == 0)
7097 {
7098 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7099 return;
7100 }
7101 l = (rot < 0x20) ? "dsll" : "dsll32";
7102 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7103 rot &= 0x1f;
7104 macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7105 macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7106 (0x20 - rot) & 0x1f);
7107 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7108 }
7109 break;
7110
7111 case M_ROL_I:
7112 {
7113 unsigned int rot;
7114
7115 if (imm_expr.X_op != O_constant)
7116 as_bad (_("Improper rotate count"));
7117 rot = imm_expr.X_add_number & 0x1f;
7118 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7119 {
7120 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7121 (32 - rot) & 0x1f);
7122 return;
7123 }
7124 if (rot == 0)
7125 {
7126 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7127 return;
7128 }
7129 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7130 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7131 (0x20 - rot) & 0x1f);
7132 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7133 }
7134 break;
7135
7136 case M_DROR:
7137 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7138 {
7139 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
7140 return;
7141 }
7142 macro_build (NULL, &icnt,NULL, "dsubu", "d,v,t", AT, 0, treg);
7143 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7144 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7145 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7146 break;
7147
7148 case M_ROR:
7149 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7150 {
7151 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
7152 return;
7153 }
7154 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7155 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7156 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7157 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7158 break;
7159
7160 case M_DROR_I:
7161 {
7162 unsigned int rot;
7163 char *l, *r;
7164
7165 if (imm_expr.X_op != O_constant)
7166 as_bad (_("Improper rotate count"));
7167 rot = imm_expr.X_add_number & 0x3f;
7168 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7169 {
7170 if (rot >= 32)
7171 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7172 dreg, sreg, rot - 32);
7173 else
7174 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7175 dreg, sreg, rot);
7176 return;
7177 }
7178 if (rot == 0)
7179 {
7180 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7181 return;
7182 }
7183 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7184 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7185 rot &= 0x1f;
7186 macro_build ( NULL, &icnt,NULL, r, "d,w,<", AT, sreg, rot);
7187 macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7188 (0x20 - rot) & 0x1f);
7189 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7190 }
7191 break;
7192
7193 case M_ROR_I:
7194 {
7195 unsigned int rot;
7196
7197 if (imm_expr.X_op != O_constant)
7198 as_bad (_("Improper rotate count"));
7199 rot = imm_expr.X_add_number & 0x1f;
7200 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7201 {
7202 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
7203 return;
7204 }
7205 if (rot == 0)
7206 {
7207 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
7208 return;
7209 }
7210 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7211 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7212 (0x20 - rot) & 0x1f);
7213 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
7214 }
7215 break;
7216
7217 case M_S_DOB:
7218 if (mips_opts.arch == CPU_R4650)
7219 {
7220 as_bad (_("opcode not supported on this processor"));
7221 return;
7222 }
7223 assert (mips_opts.isa == ISA_MIPS1);
7224 /* Even on a big endian machine $fn comes before $fn+1. We have
7225 to adjust when storing to memory. */
7226 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7227 target_big_endian ? treg + 1 : treg,
7228 BFD_RELOC_LO16, breg);
7229 offset_expr.X_add_number += 4;
7230 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7231 target_big_endian ? treg : treg + 1,
7232 BFD_RELOC_LO16, breg);
7233 return;
7234
7235 case M_SEQ:
7236 if (sreg == 0)
7237 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7238 BFD_RELOC_LO16);
7239 else if (treg == 0)
7240 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7241 BFD_RELOC_LO16);
7242 else
7243 {
7244 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7245 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7246 BFD_RELOC_LO16);
7247 }
7248 return;
7249
7250 case M_SEQ_I:
7251 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7252 {
7253 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7254 BFD_RELOC_LO16);
7255 return;
7256 }
7257 if (sreg == 0)
7258 {
7259 as_warn (_("Instruction %s: result is always false"),
7260 ip->insn_mo->name);
7261 move_register (&icnt, dreg, 0);
7262 return;
7263 }
7264 if (imm_expr.X_op == O_constant
7265 && imm_expr.X_add_number >= 0
7266 && imm_expr.X_add_number < 0x10000)
7267 {
7268 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7269 BFD_RELOC_LO16);
7270 used_at = 0;
7271 }
7272 else if (imm_expr.X_op == O_constant
7273 && imm_expr.X_add_number > -0x8000
7274 && imm_expr.X_add_number < 0)
7275 {
7276 imm_expr.X_add_number = -imm_expr.X_add_number;
7277 macro_build (NULL, &icnt, &imm_expr,
7278 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7279 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7280 used_at = 0;
7281 }
7282 else
7283 {
7284 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7285 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7286 used_at = 1;
7287 }
7288 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7289 BFD_RELOC_LO16);
7290 if (used_at)
7291 break;
7292 return;
7293
7294 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7295 s = "slt";
7296 goto sge;
7297 case M_SGEU:
7298 s = "sltu";
7299 sge:
7300 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7301 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7302 BFD_RELOC_LO16);
7303 return;
7304
7305 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7306 case M_SGEU_I:
7307 if (imm_expr.X_op == O_constant
7308 && imm_expr.X_add_number >= -0x8000
7309 && imm_expr.X_add_number < 0x8000)
7310 {
7311 macro_build (NULL, &icnt, &imm_expr,
7312 mask == M_SGE_I ? "slti" : "sltiu",
7313 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7314 used_at = 0;
7315 }
7316 else
7317 {
7318 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7319 macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7320 "d,v,t", dreg, sreg, AT);
7321 used_at = 1;
7322 }
7323 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7324 BFD_RELOC_LO16);
7325 if (used_at)
7326 break;
7327 return;
7328
7329 case M_SGT: /* sreg > treg <==> treg < sreg */
7330 s = "slt";
7331 goto sgt;
7332 case M_SGTU:
7333 s = "sltu";
7334 sgt:
7335 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7336 return;
7337
7338 case M_SGT_I: /* sreg > I <==> I < sreg */
7339 s = "slt";
7340 goto sgti;
7341 case M_SGTU_I:
7342 s = "sltu";
7343 sgti:
7344 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7345 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7346 break;
7347
7348 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7349 s = "slt";
7350 goto sle;
7351 case M_SLEU:
7352 s = "sltu";
7353 sle:
7354 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7355 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7356 BFD_RELOC_LO16);
7357 return;
7358
7359 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7360 s = "slt";
7361 goto slei;
7362 case M_SLEU_I:
7363 s = "sltu";
7364 slei:
7365 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7366 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7367 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7368 BFD_RELOC_LO16);
7369 break;
7370
7371 case M_SLT_I:
7372 if (imm_expr.X_op == O_constant
7373 && imm_expr.X_add_number >= -0x8000
7374 && imm_expr.X_add_number < 0x8000)
7375 {
7376 macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7377 BFD_RELOC_LO16);
7378 return;
7379 }
7380 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7381 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
7382 break;
7383
7384 case M_SLTU_I:
7385 if (imm_expr.X_op == O_constant
7386 && imm_expr.X_add_number >= -0x8000
7387 && imm_expr.X_add_number < 0x8000)
7388 {
7389 macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7390 BFD_RELOC_LO16);
7391 return;
7392 }
7393 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7394 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
7395 break;
7396
7397 case M_SNE:
7398 if (sreg == 0)
7399 macro_build (NULL, &icnt,NULL, "sltu","d,v,t", dreg, 0, treg);
7400 else if (treg == 0)
7401 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7402 else
7403 {
7404 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7405 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7406 }
7407 return;
7408
7409 case M_SNE_I:
7410 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7411 {
7412 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
7413 return;
7414 }
7415 if (sreg == 0)
7416 {
7417 as_warn (_("Instruction %s: result is always true"),
7418 ip->insn_mo->name);
7419 macro_build (NULL, &icnt, &expr1,
7420 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7421 "t,r,j", dreg, 0, BFD_RELOC_LO16);
7422 return;
7423 }
7424 if (imm_expr.X_op == O_constant
7425 && imm_expr.X_add_number >= 0
7426 && imm_expr.X_add_number < 0x10000)
7427 {
7428 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7429 BFD_RELOC_LO16);
7430 used_at = 0;
7431 }
7432 else if (imm_expr.X_op == O_constant
7433 && imm_expr.X_add_number > -0x8000
7434 && imm_expr.X_add_number < 0)
7435 {
7436 imm_expr.X_add_number = -imm_expr.X_add_number;
7437 macro_build (NULL, &icnt, &imm_expr,
7438 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7439 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7440 used_at = 0;
7441 }
7442 else
7443 {
7444 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7445 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
7446 used_at = 1;
7447 }
7448 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
7449 if (used_at)
7450 break;
7451 return;
7452
7453 case M_DSUB_I:
7454 dbl = 1;
7455 case M_SUB_I:
7456 if (imm_expr.X_op == O_constant
7457 && imm_expr.X_add_number > -0x8000
7458 && imm_expr.X_add_number <= 0x8000)
7459 {
7460 imm_expr.X_add_number = -imm_expr.X_add_number;
7461 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7462 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7463 return;
7464 }
7465 load_register (&icnt, AT, &imm_expr, dbl);
7466 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7467 dreg, sreg, AT);
7468 break;
7469
7470 case M_DSUBU_I:
7471 dbl = 1;
7472 case M_SUBU_I:
7473 if (imm_expr.X_op == O_constant
7474 && imm_expr.X_add_number > -0x8000
7475 && imm_expr.X_add_number <= 0x8000)
7476 {
7477 imm_expr.X_add_number = -imm_expr.X_add_number;
7478 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7479 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7480 return;
7481 }
7482 load_register (&icnt, AT, &imm_expr, dbl);
7483 macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7484 dreg, sreg, AT);
7485 break;
7486
7487 case M_TEQ_I:
7488 s = "teq";
7489 goto trap;
7490 case M_TGE_I:
7491 s = "tge";
7492 goto trap;
7493 case M_TGEU_I:
7494 s = "tgeu";
7495 goto trap;
7496 case M_TLT_I:
7497 s = "tlt";
7498 goto trap;
7499 case M_TLTU_I:
7500 s = "tltu";
7501 goto trap;
7502 case M_TNE_I:
7503 s = "tne";
7504 trap:
7505 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7506 macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
7507 break;
7508
7509 case M_TRUNCWS:
7510 case M_TRUNCWD:
7511 assert (mips_opts.isa == ISA_MIPS1);
7512 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7513 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7514
7515 /*
7516 * Is the double cfc1 instruction a bug in the mips assembler;
7517 * or is there a reason for it?
7518 */
7519 mips_emit_delays (TRUE);
7520 ++mips_opts.noreorder;
7521 mips_any_noreorder = 1;
7522 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7523 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7524 macro_build (NULL, &icnt, NULL, "nop", "");
7525 expr1.X_add_number = 3;
7526 macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7527 BFD_RELOC_LO16);
7528 expr1.X_add_number = 2;
7529 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7530 BFD_RELOC_LO16);
7531 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7532 macro_build (NULL, &icnt, NULL, "nop", "");
7533 macro_build (NULL, &icnt, NULL,
7534 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7535 "D,S", dreg, sreg);
7536 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7537 macro_build (NULL, &icnt, NULL, "nop", "");
7538 --mips_opts.noreorder;
7539 break;
7540
7541 case M_ULH:
7542 s = "lb";
7543 goto ulh;
7544 case M_ULHU:
7545 s = "lbu";
7546 ulh:
7547 if (offset_expr.X_add_number >= 0x7fff)
7548 as_bad (_("operand overflow"));
7549 if (! target_big_endian)
7550 ++offset_expr.X_add_number;
7551 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7552 BFD_RELOC_LO16, breg);
7553 if (! target_big_endian)
7554 --offset_expr.X_add_number;
7555 else
7556 ++offset_expr.X_add_number;
7557 macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7558 BFD_RELOC_LO16, breg);
7559 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7560 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7561 break;
7562
7563 case M_ULD:
7564 s = "ldl";
7565 s2 = "ldr";
7566 off = 7;
7567 goto ulw;
7568 case M_ULW:
7569 s = "lwl";
7570 s2 = "lwr";
7571 off = 3;
7572 ulw:
7573 if (offset_expr.X_add_number >= 0x8000 - off)
7574 as_bad (_("operand overflow"));
7575 if (treg != breg)
7576 tempreg = treg;
7577 else
7578 tempreg = AT;
7579 if (! target_big_endian)
7580 offset_expr.X_add_number += off;
7581 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7582 BFD_RELOC_LO16, breg);
7583 if (! target_big_endian)
7584 offset_expr.X_add_number -= off;
7585 else
7586 offset_expr.X_add_number += off;
7587 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7588 BFD_RELOC_LO16, breg);
7589
7590 /* If necessary, move the result in tempreg the final destination. */
7591 if (treg == tempreg)
7592 return;
7593 /* Protect second load's delay slot. */
7594 if (!gpr_interlocks)
7595 macro_build (NULL, &icnt, NULL, "nop", "");
7596 move_register (&icnt, treg, tempreg);
7597 break;
7598
7599 case M_ULD_A:
7600 s = "ldl";
7601 s2 = "ldr";
7602 off = 7;
7603 goto ulwa;
7604 case M_ULW_A:
7605 s = "lwl";
7606 s2 = "lwr";
7607 off = 3;
7608 ulwa:
7609 used_at = 1;
7610 load_address (&icnt, AT, &offset_expr, &used_at);
7611 if (breg != 0)
7612 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7613 AT, AT, breg);
7614 if (! target_big_endian)
7615 expr1.X_add_number = off;
7616 else
7617 expr1.X_add_number = 0;
7618 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7619 BFD_RELOC_LO16, AT);
7620 if (! target_big_endian)
7621 expr1.X_add_number = 0;
7622 else
7623 expr1.X_add_number = off;
7624 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7625 BFD_RELOC_LO16, AT);
7626 break;
7627
7628 case M_ULH_A:
7629 case M_ULHU_A:
7630 used_at = 1;
7631 load_address (&icnt, AT, &offset_expr, &used_at);
7632 if (breg != 0)
7633 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7634 AT, AT, breg);
7635 if (target_big_endian)
7636 expr1.X_add_number = 0;
7637 macro_build (NULL, &icnt, &expr1,
7638 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7639 treg, BFD_RELOC_LO16, AT);
7640 if (target_big_endian)
7641 expr1.X_add_number = 1;
7642 else
7643 expr1.X_add_number = 0;
7644 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7645 AT, BFD_RELOC_LO16, AT);
7646 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7647 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7648 break;
7649
7650 case M_USH:
7651 if (offset_expr.X_add_number >= 0x7fff)
7652 as_bad (_("operand overflow"));
7653 if (target_big_endian)
7654 ++offset_expr.X_add_number;
7655 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7656 BFD_RELOC_LO16, breg);
7657 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
7658 if (target_big_endian)
7659 --offset_expr.X_add_number;
7660 else
7661 ++offset_expr.X_add_number;
7662 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7663 BFD_RELOC_LO16, breg);
7664 break;
7665
7666 case M_USD:
7667 s = "sdl";
7668 s2 = "sdr";
7669 off = 7;
7670 goto usw;
7671 case M_USW:
7672 s = "swl";
7673 s2 = "swr";
7674 off = 3;
7675 usw:
7676 if (offset_expr.X_add_number >= 0x8000 - off)
7677 as_bad (_("operand overflow"));
7678 if (! target_big_endian)
7679 offset_expr.X_add_number += off;
7680 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7681 BFD_RELOC_LO16, breg);
7682 if (! target_big_endian)
7683 offset_expr.X_add_number -= off;
7684 else
7685 offset_expr.X_add_number += off;
7686 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7687 BFD_RELOC_LO16, breg);
7688 return;
7689
7690 case M_USD_A:
7691 s = "sdl";
7692 s2 = "sdr";
7693 off = 7;
7694 goto uswa;
7695 case M_USW_A:
7696 s = "swl";
7697 s2 = "swr";
7698 off = 3;
7699 uswa:
7700 used_at = 1;
7701 load_address (&icnt, AT, &offset_expr, &used_at);
7702 if (breg != 0)
7703 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7704 AT, AT, breg);
7705 if (! target_big_endian)
7706 expr1.X_add_number = off;
7707 else
7708 expr1.X_add_number = 0;
7709 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7710 BFD_RELOC_LO16, AT);
7711 if (! target_big_endian)
7712 expr1.X_add_number = 0;
7713 else
7714 expr1.X_add_number = off;
7715 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7716 BFD_RELOC_LO16, AT);
7717 break;
7718
7719 case M_USH_A:
7720 used_at = 1;
7721 load_address (&icnt, AT, &offset_expr, &used_at);
7722 if (breg != 0)
7723 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7724 AT, AT, breg);
7725 if (! target_big_endian)
7726 expr1.X_add_number = 0;
7727 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7728 BFD_RELOC_LO16, AT);
7729 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
7730 if (! target_big_endian)
7731 expr1.X_add_number = 1;
7732 else
7733 expr1.X_add_number = 0;
7734 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7735 BFD_RELOC_LO16, AT);
7736 if (! target_big_endian)
7737 expr1.X_add_number = 0;
7738 else
7739 expr1.X_add_number = 1;
7740 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7741 BFD_RELOC_LO16, AT);
7742 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7743 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
7744 break;
7745
7746 default:
7747 /* FIXME: Check if this is one of the itbl macros, since they
7748 are added dynamically. */
7749 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7750 break;
7751 }
7752 if (mips_opts.noat)
7753 as_warn (_("Macro used $at after \".set noat\""));
7754 }
7755
7756 /* Implement macros in mips16 mode. */
7757
7758 static void
7759 mips16_macro (struct mips_cl_insn *ip)
7760 {
7761 int mask;
7762 int xreg, yreg, zreg, tmp;
7763 int icnt;
7764 expressionS expr1;
7765 int dbl;
7766 const char *s, *s2, *s3;
7767
7768 mask = ip->insn_mo->mask;
7769
7770 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7771 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7772 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7773
7774 icnt = 0;
7775
7776 expr1.X_op = O_constant;
7777 expr1.X_op_symbol = NULL;
7778 expr1.X_add_symbol = NULL;
7779 expr1.X_add_number = 1;
7780
7781 dbl = 0;
7782
7783 switch (mask)
7784 {
7785 default:
7786 internalError ();
7787
7788 case M_DDIV_3:
7789 dbl = 1;
7790 case M_DIV_3:
7791 s = "mflo";
7792 goto do_div3;
7793 case M_DREM_3:
7794 dbl = 1;
7795 case M_REM_3:
7796 s = "mfhi";
7797 do_div3:
7798 mips_emit_delays (TRUE);
7799 ++mips_opts.noreorder;
7800 mips_any_noreorder = 1;
7801 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7802 xreg, yreg);
7803 expr1.X_add_number = 2;
7804 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7805 macro_build (NULL, &icnt, NULL, "break", "6", 7);
7806
7807 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7808 since that causes an overflow. We should do that as well,
7809 but I don't see how to do the comparisons without a temporary
7810 register. */
7811 --mips_opts.noreorder;
7812 macro_build (NULL, &icnt, NULL, s, "x", zreg);
7813 break;
7814
7815 case M_DIVU_3:
7816 s = "divu";
7817 s2 = "mflo";
7818 goto do_divu3;
7819 case M_REMU_3:
7820 s = "divu";
7821 s2 = "mfhi";
7822 goto do_divu3;
7823 case M_DDIVU_3:
7824 s = "ddivu";
7825 s2 = "mflo";
7826 goto do_divu3;
7827 case M_DREMU_3:
7828 s = "ddivu";
7829 s2 = "mfhi";
7830 do_divu3:
7831 mips_emit_delays (TRUE);
7832 ++mips_opts.noreorder;
7833 mips_any_noreorder = 1;
7834 macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
7835 expr1.X_add_number = 2;
7836 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7837 macro_build (NULL, &icnt, NULL, "break", "6", 7);
7838 --mips_opts.noreorder;
7839 macro_build (NULL, &icnt, NULL, s2, "x", zreg);
7840 break;
7841
7842 case M_DMUL:
7843 dbl = 1;
7844 case M_MUL:
7845 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
7846 xreg, yreg);
7847 macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
7848 return;
7849
7850 case M_DSUBU_I:
7851 dbl = 1;
7852 goto do_subu;
7853 case M_SUBU_I:
7854 do_subu:
7855 if (imm_expr.X_op != O_constant)
7856 as_bad (_("Unsupported large constant"));
7857 imm_expr.X_add_number = -imm_expr.X_add_number;
7858 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
7859 yreg, xreg);
7860 break;
7861
7862 case M_SUBU_I_2:
7863 if (imm_expr.X_op != O_constant)
7864 as_bad (_("Unsupported large constant"));
7865 imm_expr.X_add_number = -imm_expr.X_add_number;
7866 macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
7867 break;
7868
7869 case M_DSUBU_I_2:
7870 if (imm_expr.X_op != O_constant)
7871 as_bad (_("Unsupported large constant"));
7872 imm_expr.X_add_number = -imm_expr.X_add_number;
7873 macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
7874 break;
7875
7876 case M_BEQ:
7877 s = "cmp";
7878 s2 = "bteqz";
7879 goto do_branch;
7880 case M_BNE:
7881 s = "cmp";
7882 s2 = "btnez";
7883 goto do_branch;
7884 case M_BLT:
7885 s = "slt";
7886 s2 = "btnez";
7887 goto do_branch;
7888 case M_BLTU:
7889 s = "sltu";
7890 s2 = "btnez";
7891 goto do_branch;
7892 case M_BLE:
7893 s = "slt";
7894 s2 = "bteqz";
7895 goto do_reverse_branch;
7896 case M_BLEU:
7897 s = "sltu";
7898 s2 = "bteqz";
7899 goto do_reverse_branch;
7900 case M_BGE:
7901 s = "slt";
7902 s2 = "bteqz";
7903 goto do_branch;
7904 case M_BGEU:
7905 s = "sltu";
7906 s2 = "bteqz";
7907 goto do_branch;
7908 case M_BGT:
7909 s = "slt";
7910 s2 = "btnez";
7911 goto do_reverse_branch;
7912 case M_BGTU:
7913 s = "sltu";
7914 s2 = "btnez";
7915
7916 do_reverse_branch:
7917 tmp = xreg;
7918 xreg = yreg;
7919 yreg = tmp;
7920
7921 do_branch:
7922 macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
7923 macro_build (NULL, &icnt, &offset_expr, s2, "p");
7924 break;
7925
7926 case M_BEQ_I:
7927 s = "cmpi";
7928 s2 = "bteqz";
7929 s3 = "x,U";
7930 goto do_branch_i;
7931 case M_BNE_I:
7932 s = "cmpi";
7933 s2 = "btnez";
7934 s3 = "x,U";
7935 goto do_branch_i;
7936 case M_BLT_I:
7937 s = "slti";
7938 s2 = "btnez";
7939 s3 = "x,8";
7940 goto do_branch_i;
7941 case M_BLTU_I:
7942 s = "sltiu";
7943 s2 = "btnez";
7944 s3 = "x,8";
7945 goto do_branch_i;
7946 case M_BLE_I:
7947 s = "slti";
7948 s2 = "btnez";
7949 s3 = "x,8";
7950 goto do_addone_branch_i;
7951 case M_BLEU_I:
7952 s = "sltiu";
7953 s2 = "btnez";
7954 s3 = "x,8";
7955 goto do_addone_branch_i;
7956 case M_BGE_I:
7957 s = "slti";
7958 s2 = "bteqz";
7959 s3 = "x,8";
7960 goto do_branch_i;
7961 case M_BGEU_I:
7962 s = "sltiu";
7963 s2 = "bteqz";
7964 s3 = "x,8";
7965 goto do_branch_i;
7966 case M_BGT_I:
7967 s = "slti";
7968 s2 = "bteqz";
7969 s3 = "x,8";
7970 goto do_addone_branch_i;
7971 case M_BGTU_I:
7972 s = "sltiu";
7973 s2 = "bteqz";
7974 s3 = "x,8";
7975
7976 do_addone_branch_i:
7977 if (imm_expr.X_op != O_constant)
7978 as_bad (_("Unsupported large constant"));
7979 ++imm_expr.X_add_number;
7980
7981 do_branch_i:
7982 macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
7983 macro_build (NULL, &icnt, &offset_expr, s2, "p");
7984 break;
7985
7986 case M_ABS:
7987 expr1.X_add_number = 0;
7988 macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
7989 if (xreg != yreg)
7990 move_register (&icnt, xreg, yreg);
7991 expr1.X_add_number = 2;
7992 macro_build (NULL, &icnt, &expr1, "bteqz", "p");
7993 macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
7994 }
7995 }
7996
7997 /* For consistency checking, verify that all bits are specified either
7998 by the match/mask part of the instruction definition, or by the
7999 operand list. */
8000 static int
8001 validate_mips_insn (const struct mips_opcode *opc)
8002 {
8003 const char *p = opc->args;
8004 char c;
8005 unsigned long used_bits = opc->mask;
8006
8007 if ((used_bits & opc->match) != opc->match)
8008 {
8009 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8010 opc->name, opc->args);
8011 return 0;
8012 }
8013 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8014 while (*p)
8015 switch (c = *p++)
8016 {
8017 case ',': break;
8018 case '(': break;
8019 case ')': break;
8020 case '+':
8021 switch (c = *p++)
8022 {
8023 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8024 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8025 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8026 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8027 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8028 default:
8029 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8030 c, opc->name, opc->args);
8031 return 0;
8032 }
8033 break;
8034 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8035 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8036 case 'A': break;
8037 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
8038 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8039 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8040 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8041 case 'F': break;
8042 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8043 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8044 case 'I': break;
8045 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
8046 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8047 case 'L': break;
8048 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8049 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
8050 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8051 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8052 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8053 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8054 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8055 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8056 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8057 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8058 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8059 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8060 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8061 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8062 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8063 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8064 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8065 case 'f': break;
8066 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8067 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8068 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8069 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8070 case 'l': break;
8071 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8072 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8073 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8074 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8075 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8076 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8077 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8078 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8079 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8080 case 'x': break;
8081 case 'z': break;
8082 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8083 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8084 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8085 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8086 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8087 case '[': break;
8088 case ']': break;
8089 default:
8090 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8091 c, opc->name, opc->args);
8092 return 0;
8093 }
8094 #undef USE_BITS
8095 if (used_bits != 0xffffffff)
8096 {
8097 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8098 ~used_bits & 0xffffffff, opc->name, opc->args);
8099 return 0;
8100 }
8101 return 1;
8102 }
8103
8104 /* This routine assembles an instruction into its binary format. As a
8105 side effect, it sets one of the global variables imm_reloc or
8106 offset_reloc to the type of relocation to do if one of the operands
8107 is an address expression. */
8108
8109 static void
8110 mips_ip (char *str, struct mips_cl_insn *ip)
8111 {
8112 char *s;
8113 const char *args;
8114 char c = 0;
8115 struct mips_opcode *insn;
8116 char *argsStart;
8117 unsigned int regno;
8118 unsigned int lastregno = 0;
8119 unsigned int lastpos = 0;
8120 unsigned int limlo, limhi;
8121 char *s_reset;
8122 char save_c = 0;
8123
8124 insn_error = NULL;
8125
8126 /* If the instruction contains a '.', we first try to match an instruction
8127 including the '.'. Then we try again without the '.'. */
8128 insn = NULL;
8129 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8130 continue;
8131
8132 /* If we stopped on whitespace, then replace the whitespace with null for
8133 the call to hash_find. Save the character we replaced just in case we
8134 have to re-parse the instruction. */
8135 if (ISSPACE (*s))
8136 {
8137 save_c = *s;
8138 *s++ = '\0';
8139 }
8140
8141 insn = (struct mips_opcode *) hash_find (op_hash, str);
8142
8143 /* If we didn't find the instruction in the opcode table, try again, but
8144 this time with just the instruction up to, but not including the
8145 first '.'. */
8146 if (insn == NULL)
8147 {
8148 /* Restore the character we overwrite above (if any). */
8149 if (save_c)
8150 *(--s) = save_c;
8151
8152 /* Scan up to the first '.' or whitespace. */
8153 for (s = str;
8154 *s != '\0' && *s != '.' && !ISSPACE (*s);
8155 ++s)
8156 continue;
8157
8158 /* If we did not find a '.', then we can quit now. */
8159 if (*s != '.')
8160 {
8161 insn_error = "unrecognized opcode";
8162 return;
8163 }
8164
8165 /* Lookup the instruction in the hash table. */
8166 *s++ = '\0';
8167 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8168 {
8169 insn_error = "unrecognized opcode";
8170 return;
8171 }
8172 }
8173
8174 argsStart = s;
8175 for (;;)
8176 {
8177 bfd_boolean ok;
8178
8179 assert (strcmp (insn->name, str) == 0);
8180
8181 if (OPCODE_IS_MEMBER (insn,
8182 (mips_opts.isa
8183 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8184 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8185 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8186 mips_opts.arch))
8187 ok = TRUE;
8188 else
8189 ok = FALSE;
8190
8191 if (insn->pinfo != INSN_MACRO)
8192 {
8193 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8194 ok = FALSE;
8195 }
8196
8197 if (! ok)
8198 {
8199 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8200 && strcmp (insn->name, insn[1].name) == 0)
8201 {
8202 ++insn;
8203 continue;
8204 }
8205 else
8206 {
8207 if (!insn_error)
8208 {
8209 static char buf[100];
8210 sprintf (buf,
8211 _("opcode not supported on this processor: %s (%s)"),
8212 mips_cpu_info_from_arch (mips_opts.arch)->name,
8213 mips_cpu_info_from_isa (mips_opts.isa)->name);
8214 insn_error = buf;
8215 }
8216 if (save_c)
8217 *(--s) = save_c;
8218 return;
8219 }
8220 }
8221
8222 ip->insn_mo = insn;
8223 ip->insn_opcode = insn->match;
8224 insn_error = NULL;
8225 for (args = insn->args;; ++args)
8226 {
8227 int is_mdmx;
8228
8229 s += strspn (s, " \t");
8230 is_mdmx = 0;
8231 switch (*args)
8232 {
8233 case '\0': /* end of args */
8234 if (*s == '\0')
8235 return;
8236 break;
8237
8238 case ',':
8239 if (*s++ == *args)
8240 continue;
8241 s--;
8242 switch (*++args)
8243 {
8244 case 'r':
8245 case 'v':
8246 ip->insn_opcode |= lastregno << OP_SH_RS;
8247 continue;
8248
8249 case 'w':
8250 ip->insn_opcode |= lastregno << OP_SH_RT;
8251 continue;
8252
8253 case 'W':
8254 ip->insn_opcode |= lastregno << OP_SH_FT;
8255 continue;
8256
8257 case 'V':
8258 ip->insn_opcode |= lastregno << OP_SH_FS;
8259 continue;
8260 }
8261 break;
8262
8263 case '(':
8264 /* Handle optional base register.
8265 Either the base register is omitted or
8266 we must have a left paren. */
8267 /* This is dependent on the next operand specifier
8268 is a base register specification. */
8269 assert (args[1] == 'b' || args[1] == '5'
8270 || args[1] == '-' || args[1] == '4');
8271 if (*s == '\0')
8272 return;
8273
8274 case ')': /* these must match exactly */
8275 case '[':
8276 case ']':
8277 if (*s++ == *args)
8278 continue;
8279 break;
8280
8281 case '+': /* Opcode extension character. */
8282 switch (*++args)
8283 {
8284 case 'A': /* ins/ext position, becomes LSB. */
8285 limlo = 0;
8286 limhi = 31;
8287 my_getExpression (&imm_expr, s);
8288 check_absolute_expr (ip, &imm_expr);
8289 if ((unsigned long) imm_expr.X_add_number < limlo
8290 || (unsigned long) imm_expr.X_add_number > limhi)
8291 {
8292 as_bad (_("Improper position (%lu)"),
8293 (unsigned long) imm_expr.X_add_number);
8294 imm_expr.X_add_number = limlo;
8295 }
8296 lastpos = imm_expr.X_add_number;
8297 ip->insn_opcode |= (imm_expr.X_add_number
8298 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8299 imm_expr.X_op = O_absent;
8300 s = expr_end;
8301 continue;
8302
8303 case 'B': /* ins size, becomes MSB. */
8304 limlo = 1;
8305 limhi = 32;
8306 my_getExpression (&imm_expr, s);
8307 check_absolute_expr (ip, &imm_expr);
8308 /* Check for negative input so that small negative numbers
8309 will not succeed incorrectly. The checks against
8310 (pos+size) transitively check "size" itself,
8311 assuming that "pos" is reasonable. */
8312 if ((long) imm_expr.X_add_number < 0
8313 || ((unsigned long) imm_expr.X_add_number
8314 + lastpos) < limlo
8315 || ((unsigned long) imm_expr.X_add_number
8316 + lastpos) > limhi)
8317 {
8318 as_bad (_("Improper insert size (%lu, position %lu)"),
8319 (unsigned long) imm_expr.X_add_number,
8320 (unsigned long) lastpos);
8321 imm_expr.X_add_number = limlo - lastpos;
8322 }
8323 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8324 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8325 imm_expr.X_op = O_absent;
8326 s = expr_end;
8327 continue;
8328
8329 case 'C': /* ext size, becomes MSBD. */
8330 limlo = 1;
8331 limhi = 32;
8332 my_getExpression (&imm_expr, s);
8333 check_absolute_expr (ip, &imm_expr);
8334 /* Check for negative input so that small negative numbers
8335 will not succeed incorrectly. The checks against
8336 (pos+size) transitively check "size" itself,
8337 assuming that "pos" is reasonable. */
8338 if ((long) imm_expr.X_add_number < 0
8339 || ((unsigned long) imm_expr.X_add_number
8340 + lastpos) < limlo
8341 || ((unsigned long) imm_expr.X_add_number
8342 + lastpos) > limhi)
8343 {
8344 as_bad (_("Improper extract size (%lu, position %lu)"),
8345 (unsigned long) imm_expr.X_add_number,
8346 (unsigned long) lastpos);
8347 imm_expr.X_add_number = limlo - lastpos;
8348 }
8349 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8350 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8351 imm_expr.X_op = O_absent;
8352 s = expr_end;
8353 continue;
8354
8355 case 'D':
8356 /* +D is for disassembly only; never match. */
8357 break;
8358
8359 default:
8360 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8361 *args, insn->name, insn->args);
8362 /* Further processing is fruitless. */
8363 return;
8364 }
8365 break;
8366
8367 case '<': /* must be at least one digit */
8368 /*
8369 * According to the manual, if the shift amount is greater
8370 * than 31 or less than 0, then the shift amount should be
8371 * mod 32. In reality the mips assembler issues an error.
8372 * We issue a warning and mask out all but the low 5 bits.
8373 */
8374 my_getExpression (&imm_expr, s);
8375 check_absolute_expr (ip, &imm_expr);
8376 if ((unsigned long) imm_expr.X_add_number > 31)
8377 {
8378 as_warn (_("Improper shift amount (%lu)"),
8379 (unsigned long) imm_expr.X_add_number);
8380 imm_expr.X_add_number &= OP_MASK_SHAMT;
8381 }
8382 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8383 imm_expr.X_op = O_absent;
8384 s = expr_end;
8385 continue;
8386
8387 case '>': /* shift amount minus 32 */
8388 my_getExpression (&imm_expr, s);
8389 check_absolute_expr (ip, &imm_expr);
8390 if ((unsigned long) imm_expr.X_add_number < 32
8391 || (unsigned long) imm_expr.X_add_number > 63)
8392 break;
8393 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8394 imm_expr.X_op = O_absent;
8395 s = expr_end;
8396 continue;
8397
8398 case 'k': /* cache code */
8399 case 'h': /* prefx code */
8400 my_getExpression (&imm_expr, s);
8401 check_absolute_expr (ip, &imm_expr);
8402 if ((unsigned long) imm_expr.X_add_number > 31)
8403 {
8404 as_warn (_("Invalid value for `%s' (%lu)"),
8405 ip->insn_mo->name,
8406 (unsigned long) imm_expr.X_add_number);
8407 imm_expr.X_add_number &= 0x1f;
8408 }
8409 if (*args == 'k')
8410 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8411 else
8412 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8413 imm_expr.X_op = O_absent;
8414 s = expr_end;
8415 continue;
8416
8417 case 'c': /* break code */
8418 my_getExpression (&imm_expr, s);
8419 check_absolute_expr (ip, &imm_expr);
8420 if ((unsigned long) imm_expr.X_add_number > 1023)
8421 {
8422 as_warn (_("Illegal break code (%lu)"),
8423 (unsigned long) imm_expr.X_add_number);
8424 imm_expr.X_add_number &= OP_MASK_CODE;
8425 }
8426 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8427 imm_expr.X_op = O_absent;
8428 s = expr_end;
8429 continue;
8430
8431 case 'q': /* lower break code */
8432 my_getExpression (&imm_expr, s);
8433 check_absolute_expr (ip, &imm_expr);
8434 if ((unsigned long) imm_expr.X_add_number > 1023)
8435 {
8436 as_warn (_("Illegal lower break code (%lu)"),
8437 (unsigned long) imm_expr.X_add_number);
8438 imm_expr.X_add_number &= OP_MASK_CODE2;
8439 }
8440 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8441 imm_expr.X_op = O_absent;
8442 s = expr_end;
8443 continue;
8444
8445 case 'B': /* 20-bit syscall/break code. */
8446 my_getExpression (&imm_expr, s);
8447 check_absolute_expr (ip, &imm_expr);
8448 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8449 as_warn (_("Illegal 20-bit code (%lu)"),
8450 (unsigned long) imm_expr.X_add_number);
8451 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8452 imm_expr.X_op = O_absent;
8453 s = expr_end;
8454 continue;
8455
8456 case 'C': /* Coprocessor code */
8457 my_getExpression (&imm_expr, s);
8458 check_absolute_expr (ip, &imm_expr);
8459 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8460 {
8461 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8462 (unsigned long) imm_expr.X_add_number);
8463 imm_expr.X_add_number &= ((1 << 25) - 1);
8464 }
8465 ip->insn_opcode |= imm_expr.X_add_number;
8466 imm_expr.X_op = O_absent;
8467 s = expr_end;
8468 continue;
8469
8470 case 'J': /* 19-bit wait code. */
8471 my_getExpression (&imm_expr, s);
8472 check_absolute_expr (ip, &imm_expr);
8473 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8474 as_warn (_("Illegal 19-bit code (%lu)"),
8475 (unsigned long) imm_expr.X_add_number);
8476 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8477 imm_expr.X_op = O_absent;
8478 s = expr_end;
8479 continue;
8480
8481 case 'P': /* Performance register */
8482 my_getExpression (&imm_expr, s);
8483 check_absolute_expr (ip, &imm_expr);
8484 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8485 {
8486 as_warn (_("Invalid performance register (%lu)"),
8487 (unsigned long) imm_expr.X_add_number);
8488 imm_expr.X_add_number &= OP_MASK_PERFREG;
8489 }
8490 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8491 imm_expr.X_op = O_absent;
8492 s = expr_end;
8493 continue;
8494
8495 case 'b': /* base register */
8496 case 'd': /* destination register */
8497 case 's': /* source register */
8498 case 't': /* target register */
8499 case 'r': /* both target and source */
8500 case 'v': /* both dest and source */
8501 case 'w': /* both dest and target */
8502 case 'E': /* coprocessor target register */
8503 case 'G': /* coprocessor destination register */
8504 case 'K': /* 'rdhwr' destination register */
8505 case 'x': /* ignore register name */
8506 case 'z': /* must be zero register */
8507 case 'U': /* destination register (clo/clz). */
8508 s_reset = s;
8509 if (s[0] == '$')
8510 {
8511
8512 if (ISDIGIT (s[1]))
8513 {
8514 ++s;
8515 regno = 0;
8516 do
8517 {
8518 regno *= 10;
8519 regno += *s - '0';
8520 ++s;
8521 }
8522 while (ISDIGIT (*s));
8523 if (regno > 31)
8524 as_bad (_("Invalid register number (%d)"), regno);
8525 }
8526 else if (*args == 'E' || *args == 'G' || *args == 'K')
8527 goto notreg;
8528 else
8529 {
8530 if (s[1] == 'r' && s[2] == 'a')
8531 {
8532 s += 3;
8533 regno = RA;
8534 }
8535 else if (s[1] == 'f' && s[2] == 'p')
8536 {
8537 s += 3;
8538 regno = FP;
8539 }
8540 else if (s[1] == 's' && s[2] == 'p')
8541 {
8542 s += 3;
8543 regno = SP;
8544 }
8545 else if (s[1] == 'g' && s[2] == 'p')
8546 {
8547 s += 3;
8548 regno = GP;
8549 }
8550 else if (s[1] == 'a' && s[2] == 't')
8551 {
8552 s += 3;
8553 regno = AT;
8554 }
8555 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8556 {
8557 s += 4;
8558 regno = KT0;
8559 }
8560 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8561 {
8562 s += 4;
8563 regno = KT1;
8564 }
8565 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8566 {
8567 s += 5;
8568 regno = ZERO;
8569 }
8570 else if (itbl_have_entries)
8571 {
8572 char *p, *n;
8573 unsigned long r;
8574
8575 p = s + 1; /* advance past '$' */
8576 n = itbl_get_field (&p); /* n is name */
8577
8578 /* See if this is a register defined in an
8579 itbl entry. */
8580 if (itbl_get_reg_val (n, &r))
8581 {
8582 /* Get_field advances to the start of
8583 the next field, so we need to back
8584 rack to the end of the last field. */
8585 if (p)
8586 s = p - 1;
8587 else
8588 s = strchr (s, '\0');
8589 regno = r;
8590 }
8591 else
8592 goto notreg;
8593 }
8594 else
8595 goto notreg;
8596 }
8597 if (regno == AT
8598 && ! mips_opts.noat
8599 && *args != 'E'
8600 && *args != 'G'
8601 && *args != 'K')
8602 as_warn (_("Used $at without \".set noat\""));
8603 c = *args;
8604 if (*s == ' ')
8605 ++s;
8606 if (args[1] != *s)
8607 {
8608 if (c == 'r' || c == 'v' || c == 'w')
8609 {
8610 regno = lastregno;
8611 s = s_reset;
8612 ++args;
8613 }
8614 }
8615 /* 'z' only matches $0. */
8616 if (c == 'z' && regno != 0)
8617 break;
8618
8619 /* Now that we have assembled one operand, we use the args string
8620 * to figure out where it goes in the instruction. */
8621 switch (c)
8622 {
8623 case 'r':
8624 case 's':
8625 case 'v':
8626 case 'b':
8627 ip->insn_opcode |= regno << OP_SH_RS;
8628 break;
8629 case 'd':
8630 case 'G':
8631 case 'K':
8632 ip->insn_opcode |= regno << OP_SH_RD;
8633 break;
8634 case 'U':
8635 ip->insn_opcode |= regno << OP_SH_RD;
8636 ip->insn_opcode |= regno << OP_SH_RT;
8637 break;
8638 case 'w':
8639 case 't':
8640 case 'E':
8641 ip->insn_opcode |= regno << OP_SH_RT;
8642 break;
8643 case 'x':
8644 /* This case exists because on the r3000 trunc
8645 expands into a macro which requires a gp
8646 register. On the r6000 or r4000 it is
8647 assembled into a single instruction which
8648 ignores the register. Thus the insn version
8649 is MIPS_ISA2 and uses 'x', and the macro
8650 version is MIPS_ISA1 and uses 't'. */
8651 break;
8652 case 'z':
8653 /* This case is for the div instruction, which
8654 acts differently if the destination argument
8655 is $0. This only matches $0, and is checked
8656 outside the switch. */
8657 break;
8658 case 'D':
8659 /* Itbl operand; not yet implemented. FIXME ?? */
8660 break;
8661 /* What about all other operands like 'i', which
8662 can be specified in the opcode table? */
8663 }
8664 lastregno = regno;
8665 continue;
8666 }
8667 notreg:
8668 switch (*args++)
8669 {
8670 case 'r':
8671 case 'v':
8672 ip->insn_opcode |= lastregno << OP_SH_RS;
8673 continue;
8674 case 'w':
8675 ip->insn_opcode |= lastregno << OP_SH_RT;
8676 continue;
8677 }
8678 break;
8679
8680 case 'O': /* MDMX alignment immediate constant. */
8681 my_getExpression (&imm_expr, s);
8682 check_absolute_expr (ip, &imm_expr);
8683 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8684 {
8685 as_warn ("Improper align amount (%ld), using low bits",
8686 (long) imm_expr.X_add_number);
8687 imm_expr.X_add_number &= OP_MASK_ALN;
8688 }
8689 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8690 imm_expr.X_op = O_absent;
8691 s = expr_end;
8692 continue;
8693
8694 case 'Q': /* MDMX vector, element sel, or const. */
8695 if (s[0] != '$')
8696 {
8697 /* MDMX Immediate. */
8698 my_getExpression (&imm_expr, s);
8699 check_absolute_expr (ip, &imm_expr);
8700 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8701 {
8702 as_warn (_("Invalid MDMX Immediate (%ld)"),
8703 (long) imm_expr.X_add_number);
8704 imm_expr.X_add_number &= OP_MASK_FT;
8705 }
8706 imm_expr.X_add_number &= OP_MASK_FT;
8707 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8708 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8709 else
8710 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8711 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8712 imm_expr.X_op = O_absent;
8713 s = expr_end;
8714 continue;
8715 }
8716 /* Not MDMX Immediate. Fall through. */
8717 case 'X': /* MDMX destination register. */
8718 case 'Y': /* MDMX source register. */
8719 case 'Z': /* MDMX target register. */
8720 is_mdmx = 1;
8721 case 'D': /* floating point destination register */
8722 case 'S': /* floating point source register */
8723 case 'T': /* floating point target register */
8724 case 'R': /* floating point source register */
8725 case 'V':
8726 case 'W':
8727 s_reset = s;
8728 /* Accept $fN for FP and MDMX register numbers, and in
8729 addition accept $vN for MDMX register numbers. */
8730 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8731 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8732 && ISDIGIT (s[2])))
8733 {
8734 s += 2;
8735 regno = 0;
8736 do
8737 {
8738 regno *= 10;
8739 regno += *s - '0';
8740 ++s;
8741 }
8742 while (ISDIGIT (*s));
8743
8744 if (regno > 31)
8745 as_bad (_("Invalid float register number (%d)"), regno);
8746
8747 if ((regno & 1) != 0
8748 && HAVE_32BIT_FPRS
8749 && ! (strcmp (str, "mtc1") == 0
8750 || strcmp (str, "mfc1") == 0
8751 || strcmp (str, "lwc1") == 0
8752 || strcmp (str, "swc1") == 0
8753 || strcmp (str, "l.s") == 0
8754 || strcmp (str, "s.s") == 0))
8755 as_warn (_("Float register should be even, was %d"),
8756 regno);
8757
8758 c = *args;
8759 if (*s == ' ')
8760 ++s;
8761 if (args[1] != *s)
8762 {
8763 if (c == 'V' || c == 'W')
8764 {
8765 regno = lastregno;
8766 s = s_reset;
8767 ++args;
8768 }
8769 }
8770 switch (c)
8771 {
8772 case 'D':
8773 case 'X':
8774 ip->insn_opcode |= regno << OP_SH_FD;
8775 break;
8776 case 'V':
8777 case 'S':
8778 case 'Y':
8779 ip->insn_opcode |= regno << OP_SH_FS;
8780 break;
8781 case 'Q':
8782 /* This is like 'Z', but also needs to fix the MDMX
8783 vector/scalar select bits. Note that the
8784 scalar immediate case is handled above. */
8785 if (*s == '[')
8786 {
8787 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8788 int max_el = (is_qh ? 3 : 7);
8789 s++;
8790 my_getExpression(&imm_expr, s);
8791 check_absolute_expr (ip, &imm_expr);
8792 s = expr_end;
8793 if (imm_expr.X_add_number > max_el)
8794 as_bad(_("Bad element selector %ld"),
8795 (long) imm_expr.X_add_number);
8796 imm_expr.X_add_number &= max_el;
8797 ip->insn_opcode |= (imm_expr.X_add_number
8798 << (OP_SH_VSEL +
8799 (is_qh ? 2 : 1)));
8800 if (*s != ']')
8801 as_warn(_("Expecting ']' found '%s'"), s);
8802 else
8803 s++;
8804 }
8805 else
8806 {
8807 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8808 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8809 << OP_SH_VSEL);
8810 else
8811 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8812 OP_SH_VSEL);
8813 }
8814 /* Fall through */
8815 case 'W':
8816 case 'T':
8817 case 'Z':
8818 ip->insn_opcode |= regno << OP_SH_FT;
8819 break;
8820 case 'R':
8821 ip->insn_opcode |= regno << OP_SH_FR;
8822 break;
8823 }
8824 lastregno = regno;
8825 continue;
8826 }
8827
8828 switch (*args++)
8829 {
8830 case 'V':
8831 ip->insn_opcode |= lastregno << OP_SH_FS;
8832 continue;
8833 case 'W':
8834 ip->insn_opcode |= lastregno << OP_SH_FT;
8835 continue;
8836 }
8837 break;
8838
8839 case 'I':
8840 my_getExpression (&imm_expr, s);
8841 if (imm_expr.X_op != O_big
8842 && imm_expr.X_op != O_constant)
8843 insn_error = _("absolute expression required");
8844 s = expr_end;
8845 continue;
8846
8847 case 'A':
8848 my_getExpression (&offset_expr, s);
8849 *imm_reloc = BFD_RELOC_32;
8850 s = expr_end;
8851 continue;
8852
8853 case 'F':
8854 case 'L':
8855 case 'f':
8856 case 'l':
8857 {
8858 int f64;
8859 int using_gprs;
8860 char *save_in;
8861 char *err;
8862 unsigned char temp[8];
8863 int len;
8864 unsigned int length;
8865 segT seg;
8866 subsegT subseg;
8867 char *p;
8868
8869 /* These only appear as the last operand in an
8870 instruction, and every instruction that accepts
8871 them in any variant accepts them in all variants.
8872 This means we don't have to worry about backing out
8873 any changes if the instruction does not match.
8874
8875 The difference between them is the size of the
8876 floating point constant and where it goes. For 'F'
8877 and 'L' the constant is 64 bits; for 'f' and 'l' it
8878 is 32 bits. Where the constant is placed is based
8879 on how the MIPS assembler does things:
8880 F -- .rdata
8881 L -- .lit8
8882 f -- immediate value
8883 l -- .lit4
8884
8885 The .lit4 and .lit8 sections are only used if
8886 permitted by the -G argument.
8887
8888 When generating embedded PIC code, we use the
8889 .lit8 section but not the .lit4 section (we can do
8890 .lit4 inline easily; we need to put .lit8
8891 somewhere in the data segment, and using .lit8
8892 permits the linker to eventually combine identical
8893 .lit8 entries).
8894
8895 The code below needs to know whether the target register
8896 is 32 or 64 bits wide. It relies on the fact 'f' and
8897 'F' are used with GPR-based instructions and 'l' and
8898 'L' are used with FPR-based instructions. */
8899
8900 f64 = *args == 'F' || *args == 'L';
8901 using_gprs = *args == 'F' || *args == 'f';
8902
8903 save_in = input_line_pointer;
8904 input_line_pointer = s;
8905 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8906 length = len;
8907 s = input_line_pointer;
8908 input_line_pointer = save_in;
8909 if (err != NULL && *err != '\0')
8910 {
8911 as_bad (_("Bad floating point constant: %s"), err);
8912 memset (temp, '\0', sizeof temp);
8913 length = f64 ? 8 : 4;
8914 }
8915
8916 assert (length == (unsigned) (f64 ? 8 : 4));
8917
8918 if (*args == 'f'
8919 || (*args == 'l'
8920 && (! USE_GLOBAL_POINTER_OPT
8921 || mips_pic == EMBEDDED_PIC
8922 || g_switch_value < 4
8923 || (temp[0] == 0 && temp[1] == 0)
8924 || (temp[2] == 0 && temp[3] == 0))))
8925 {
8926 imm_expr.X_op = O_constant;
8927 if (! target_big_endian)
8928 imm_expr.X_add_number = bfd_getl32 (temp);
8929 else
8930 imm_expr.X_add_number = bfd_getb32 (temp);
8931 }
8932 else if (length > 4
8933 && ! mips_disable_float_construction
8934 /* Constants can only be constructed in GPRs and
8935 copied to FPRs if the GPRs are at least as wide
8936 as the FPRs. Force the constant into memory if
8937 we are using 64-bit FPRs but the GPRs are only
8938 32 bits wide. */
8939 && (using_gprs
8940 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8941 && ((temp[0] == 0 && temp[1] == 0)
8942 || (temp[2] == 0 && temp[3] == 0))
8943 && ((temp[4] == 0 && temp[5] == 0)
8944 || (temp[6] == 0 && temp[7] == 0)))
8945 {
8946 /* The value is simple enough to load with a couple of
8947 instructions. If using 32-bit registers, set
8948 imm_expr to the high order 32 bits and offset_expr to
8949 the low order 32 bits. Otherwise, set imm_expr to
8950 the entire 64 bit constant. */
8951 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8952 {
8953 imm_expr.X_op = O_constant;
8954 offset_expr.X_op = O_constant;
8955 if (! target_big_endian)
8956 {
8957 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8958 offset_expr.X_add_number = bfd_getl32 (temp);
8959 }
8960 else
8961 {
8962 imm_expr.X_add_number = bfd_getb32 (temp);
8963 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8964 }
8965 if (offset_expr.X_add_number == 0)
8966 offset_expr.X_op = O_absent;
8967 }
8968 else if (sizeof (imm_expr.X_add_number) > 4)
8969 {
8970 imm_expr.X_op = O_constant;
8971 if (! target_big_endian)
8972 imm_expr.X_add_number = bfd_getl64 (temp);
8973 else
8974 imm_expr.X_add_number = bfd_getb64 (temp);
8975 }
8976 else
8977 {
8978 imm_expr.X_op = O_big;
8979 imm_expr.X_add_number = 4;
8980 if (! target_big_endian)
8981 {
8982 generic_bignum[0] = bfd_getl16 (temp);
8983 generic_bignum[1] = bfd_getl16 (temp + 2);
8984 generic_bignum[2] = bfd_getl16 (temp + 4);
8985 generic_bignum[3] = bfd_getl16 (temp + 6);
8986 }
8987 else
8988 {
8989 generic_bignum[0] = bfd_getb16 (temp + 6);
8990 generic_bignum[1] = bfd_getb16 (temp + 4);
8991 generic_bignum[2] = bfd_getb16 (temp + 2);
8992 generic_bignum[3] = bfd_getb16 (temp);
8993 }
8994 }
8995 }
8996 else
8997 {
8998 const char *newname;
8999 segT new_seg;
9000
9001 /* Switch to the right section. */
9002 seg = now_seg;
9003 subseg = now_subseg;
9004 switch (*args)
9005 {
9006 default: /* unused default case avoids warnings. */
9007 case 'L':
9008 newname = RDATA_SECTION_NAME;
9009 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9010 || mips_pic == EMBEDDED_PIC)
9011 newname = ".lit8";
9012 break;
9013 case 'F':
9014 if (mips_pic == EMBEDDED_PIC)
9015 newname = ".lit8";
9016 else
9017 newname = RDATA_SECTION_NAME;
9018 break;
9019 case 'l':
9020 assert (!USE_GLOBAL_POINTER_OPT
9021 || g_switch_value >= 4);
9022 newname = ".lit4";
9023 break;
9024 }
9025 new_seg = subseg_new (newname, (subsegT) 0);
9026 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9027 bfd_set_section_flags (stdoutput, new_seg,
9028 (SEC_ALLOC
9029 | SEC_LOAD
9030 | SEC_READONLY
9031 | SEC_DATA));
9032 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9033 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9034 && strcmp (TARGET_OS, "elf") != 0)
9035 record_alignment (new_seg, 4);
9036 else
9037 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9038 if (seg == now_seg)
9039 as_bad (_("Can't use floating point insn in this section"));
9040
9041 /* Set the argument to the current address in the
9042 section. */
9043 offset_expr.X_op = O_symbol;
9044 offset_expr.X_add_symbol =
9045 symbol_new ("L0\001", now_seg,
9046 (valueT) frag_now_fix (), frag_now);
9047 offset_expr.X_add_number = 0;
9048
9049 /* Put the floating point number into the section. */
9050 p = frag_more ((int) length);
9051 memcpy (p, temp, length);
9052
9053 /* Switch back to the original section. */
9054 subseg_set (seg, subseg);
9055 }
9056 }
9057 continue;
9058
9059 case 'i': /* 16 bit unsigned immediate */
9060 case 'j': /* 16 bit signed immediate */
9061 *imm_reloc = BFD_RELOC_LO16;
9062 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9063 {
9064 int more;
9065 offsetT minval, maxval;
9066
9067 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9068 && strcmp (insn->name, insn[1].name) == 0);
9069
9070 /* If the expression was written as an unsigned number,
9071 only treat it as signed if there are no more
9072 alternatives. */
9073 if (more
9074 && *args == 'j'
9075 && sizeof (imm_expr.X_add_number) <= 4
9076 && imm_expr.X_op == O_constant
9077 && imm_expr.X_add_number < 0
9078 && imm_expr.X_unsigned
9079 && HAVE_64BIT_GPRS)
9080 break;
9081
9082 /* For compatibility with older assemblers, we accept
9083 0x8000-0xffff as signed 16-bit numbers when only
9084 signed numbers are allowed. */
9085 if (*args == 'i')
9086 minval = 0, maxval = 0xffff;
9087 else if (more)
9088 minval = -0x8000, maxval = 0x7fff;
9089 else
9090 minval = -0x8000, maxval = 0xffff;
9091
9092 if (imm_expr.X_op != O_constant
9093 || imm_expr.X_add_number < minval
9094 || imm_expr.X_add_number > maxval)
9095 {
9096 if (more)
9097 break;
9098 if (imm_expr.X_op == O_constant
9099 || imm_expr.X_op == O_big)
9100 as_bad (_("expression out of range"));
9101 }
9102 }
9103 s = expr_end;
9104 continue;
9105
9106 case 'o': /* 16 bit offset */
9107 /* Check whether there is only a single bracketed expression
9108 left. If so, it must be the base register and the
9109 constant must be zero. */
9110 if (*s == '(' && strchr (s + 1, '(') == 0)
9111 {
9112 offset_expr.X_op = O_constant;
9113 offset_expr.X_add_number = 0;
9114 continue;
9115 }
9116
9117 /* If this value won't fit into a 16 bit offset, then go
9118 find a macro that will generate the 32 bit offset
9119 code pattern. */
9120 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9121 && (offset_expr.X_op != O_constant
9122 || offset_expr.X_add_number >= 0x8000
9123 || offset_expr.X_add_number < -0x8000))
9124 break;
9125
9126 s = expr_end;
9127 continue;
9128
9129 case 'p': /* pc relative offset */
9130 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9131 my_getExpression (&offset_expr, s);
9132 s = expr_end;
9133 continue;
9134
9135 case 'u': /* upper 16 bits */
9136 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9137 && imm_expr.X_op == O_constant
9138 && (imm_expr.X_add_number < 0
9139 || imm_expr.X_add_number >= 0x10000))
9140 as_bad (_("lui expression not in range 0..65535"));
9141 s = expr_end;
9142 continue;
9143
9144 case 'a': /* 26 bit address */
9145 my_getExpression (&offset_expr, s);
9146 s = expr_end;
9147 *offset_reloc = BFD_RELOC_MIPS_JMP;
9148 continue;
9149
9150 case 'N': /* 3 bit branch condition code */
9151 case 'M': /* 3 bit compare condition code */
9152 if (strncmp (s, "$fcc", 4) != 0)
9153 break;
9154 s += 4;
9155 regno = 0;
9156 do
9157 {
9158 regno *= 10;
9159 regno += *s - '0';
9160 ++s;
9161 }
9162 while (ISDIGIT (*s));
9163 if (regno > 7)
9164 as_bad (_("invalid condition code register $fcc%d"), regno);
9165 if (*args == 'N')
9166 ip->insn_opcode |= regno << OP_SH_BCC;
9167 else
9168 ip->insn_opcode |= regno << OP_SH_CCC;
9169 continue;
9170
9171 case 'H':
9172 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9173 s += 2;
9174 if (ISDIGIT (*s))
9175 {
9176 c = 0;
9177 do
9178 {
9179 c *= 10;
9180 c += *s - '0';
9181 ++s;
9182 }
9183 while (ISDIGIT (*s));
9184 }
9185 else
9186 c = 8; /* Invalid sel value. */
9187
9188 if (c > 7)
9189 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9190 ip->insn_opcode |= c;
9191 continue;
9192
9193 case 'e':
9194 /* Must be at least one digit. */
9195 my_getExpression (&imm_expr, s);
9196 check_absolute_expr (ip, &imm_expr);
9197
9198 if ((unsigned long) imm_expr.X_add_number
9199 > (unsigned long) OP_MASK_VECBYTE)
9200 {
9201 as_bad (_("bad byte vector index (%ld)"),
9202 (long) imm_expr.X_add_number);
9203 imm_expr.X_add_number = 0;
9204 }
9205
9206 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9207 imm_expr.X_op = O_absent;
9208 s = expr_end;
9209 continue;
9210
9211 case '%':
9212 my_getExpression (&imm_expr, s);
9213 check_absolute_expr (ip, &imm_expr);
9214
9215 if ((unsigned long) imm_expr.X_add_number
9216 > (unsigned long) OP_MASK_VECALIGN)
9217 {
9218 as_bad (_("bad byte vector index (%ld)"),
9219 (long) imm_expr.X_add_number);
9220 imm_expr.X_add_number = 0;
9221 }
9222
9223 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9224 imm_expr.X_op = O_absent;
9225 s = expr_end;
9226 continue;
9227
9228 default:
9229 as_bad (_("bad char = '%c'\n"), *args);
9230 internalError ();
9231 }
9232 break;
9233 }
9234 /* Args don't match. */
9235 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9236 !strcmp (insn->name, insn[1].name))
9237 {
9238 ++insn;
9239 s = argsStart;
9240 insn_error = _("illegal operands");
9241 continue;
9242 }
9243 if (save_c)
9244 *(--s) = save_c;
9245 insn_error = _("illegal operands");
9246 return;
9247 }
9248 }
9249
9250 /* This routine assembles an instruction into its binary format when
9251 assembling for the mips16. As a side effect, it sets one of the
9252 global variables imm_reloc or offset_reloc to the type of
9253 relocation to do if one of the operands is an address expression.
9254 It also sets mips16_small and mips16_ext if the user explicitly
9255 requested a small or extended instruction. */
9256
9257 static void
9258 mips16_ip (char *str, struct mips_cl_insn *ip)
9259 {
9260 char *s;
9261 const char *args;
9262 struct mips_opcode *insn;
9263 char *argsstart;
9264 unsigned int regno;
9265 unsigned int lastregno = 0;
9266 char *s_reset;
9267
9268 insn_error = NULL;
9269
9270 mips16_small = FALSE;
9271 mips16_ext = FALSE;
9272
9273 for (s = str; ISLOWER (*s); ++s)
9274 ;
9275 switch (*s)
9276 {
9277 case '\0':
9278 break;
9279
9280 case ' ':
9281 *s++ = '\0';
9282 break;
9283
9284 case '.':
9285 if (s[1] == 't' && s[2] == ' ')
9286 {
9287 *s = '\0';
9288 mips16_small = TRUE;
9289 s += 3;
9290 break;
9291 }
9292 else if (s[1] == 'e' && s[2] == ' ')
9293 {
9294 *s = '\0';
9295 mips16_ext = TRUE;
9296 s += 3;
9297 break;
9298 }
9299 /* Fall through. */
9300 default:
9301 insn_error = _("unknown opcode");
9302 return;
9303 }
9304
9305 if (mips_opts.noautoextend && ! mips16_ext)
9306 mips16_small = TRUE;
9307
9308 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9309 {
9310 insn_error = _("unrecognized opcode");
9311 return;
9312 }
9313
9314 argsstart = s;
9315 for (;;)
9316 {
9317 assert (strcmp (insn->name, str) == 0);
9318
9319 ip->insn_mo = insn;
9320 ip->insn_opcode = insn->match;
9321 ip->use_extend = FALSE;
9322 imm_expr.X_op = O_absent;
9323 imm_reloc[0] = BFD_RELOC_UNUSED;
9324 imm_reloc[1] = BFD_RELOC_UNUSED;
9325 imm_reloc[2] = BFD_RELOC_UNUSED;
9326 offset_expr.X_op = O_absent;
9327 offset_reloc[0] = BFD_RELOC_UNUSED;
9328 offset_reloc[1] = BFD_RELOC_UNUSED;
9329 offset_reloc[2] = BFD_RELOC_UNUSED;
9330 for (args = insn->args; 1; ++args)
9331 {
9332 int c;
9333
9334 if (*s == ' ')
9335 ++s;
9336
9337 /* In this switch statement we call break if we did not find
9338 a match, continue if we did find a match, or return if we
9339 are done. */
9340
9341 c = *args;
9342 switch (c)
9343 {
9344 case '\0':
9345 if (*s == '\0')
9346 {
9347 /* Stuff the immediate value in now, if we can. */
9348 if (imm_expr.X_op == O_constant
9349 && *imm_reloc > BFD_RELOC_UNUSED
9350 && insn->pinfo != INSN_MACRO)
9351 {
9352 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9353 imm_expr.X_add_number, TRUE, mips16_small,
9354 mips16_ext, &ip->insn_opcode,
9355 &ip->use_extend, &ip->extend);
9356 imm_expr.X_op = O_absent;
9357 *imm_reloc = BFD_RELOC_UNUSED;
9358 }
9359
9360 return;
9361 }
9362 break;
9363
9364 case ',':
9365 if (*s++ == c)
9366 continue;
9367 s--;
9368 switch (*++args)
9369 {
9370 case 'v':
9371 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9372 continue;
9373 case 'w':
9374 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9375 continue;
9376 }
9377 break;
9378
9379 case '(':
9380 case ')':
9381 if (*s++ == c)
9382 continue;
9383 break;
9384
9385 case 'v':
9386 case 'w':
9387 if (s[0] != '$')
9388 {
9389 if (c == 'v')
9390 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9391 else
9392 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9393 ++args;
9394 continue;
9395 }
9396 /* Fall through. */
9397 case 'x':
9398 case 'y':
9399 case 'z':
9400 case 'Z':
9401 case '0':
9402 case 'S':
9403 case 'R':
9404 case 'X':
9405 case 'Y':
9406 if (s[0] != '$')
9407 break;
9408 s_reset = s;
9409 if (ISDIGIT (s[1]))
9410 {
9411 ++s;
9412 regno = 0;
9413 do
9414 {
9415 regno *= 10;
9416 regno += *s - '0';
9417 ++s;
9418 }
9419 while (ISDIGIT (*s));
9420 if (regno > 31)
9421 {
9422 as_bad (_("invalid register number (%d)"), regno);
9423 regno = 2;
9424 }
9425 }
9426 else
9427 {
9428 if (s[1] == 'r' && s[2] == 'a')
9429 {
9430 s += 3;
9431 regno = RA;
9432 }
9433 else if (s[1] == 'f' && s[2] == 'p')
9434 {
9435 s += 3;
9436 regno = FP;
9437 }
9438 else if (s[1] == 's' && s[2] == 'p')
9439 {
9440 s += 3;
9441 regno = SP;
9442 }
9443 else if (s[1] == 'g' && s[2] == 'p')
9444 {
9445 s += 3;
9446 regno = GP;
9447 }
9448 else if (s[1] == 'a' && s[2] == 't')
9449 {
9450 s += 3;
9451 regno = AT;
9452 }
9453 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9454 {
9455 s += 4;
9456 regno = KT0;
9457 }
9458 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9459 {
9460 s += 4;
9461 regno = KT1;
9462 }
9463 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9464 {
9465 s += 5;
9466 regno = ZERO;
9467 }
9468 else
9469 break;
9470 }
9471
9472 if (*s == ' ')
9473 ++s;
9474 if (args[1] != *s)
9475 {
9476 if (c == 'v' || c == 'w')
9477 {
9478 regno = mips16_to_32_reg_map[lastregno];
9479 s = s_reset;
9480 ++args;
9481 }
9482 }
9483
9484 switch (c)
9485 {
9486 case 'x':
9487 case 'y':
9488 case 'z':
9489 case 'v':
9490 case 'w':
9491 case 'Z':
9492 regno = mips32_to_16_reg_map[regno];
9493 break;
9494
9495 case '0':
9496 if (regno != 0)
9497 regno = ILLEGAL_REG;
9498 break;
9499
9500 case 'S':
9501 if (regno != SP)
9502 regno = ILLEGAL_REG;
9503 break;
9504
9505 case 'R':
9506 if (regno != RA)
9507 regno = ILLEGAL_REG;
9508 break;
9509
9510 case 'X':
9511 case 'Y':
9512 if (regno == AT && ! mips_opts.noat)
9513 as_warn (_("used $at without \".set noat\""));
9514 break;
9515
9516 default:
9517 internalError ();
9518 }
9519
9520 if (regno == ILLEGAL_REG)
9521 break;
9522
9523 switch (c)
9524 {
9525 case 'x':
9526 case 'v':
9527 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9528 break;
9529 case 'y':
9530 case 'w':
9531 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9532 break;
9533 case 'z':
9534 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9535 break;
9536 case 'Z':
9537 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9538 case '0':
9539 case 'S':
9540 case 'R':
9541 break;
9542 case 'X':
9543 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9544 break;
9545 case 'Y':
9546 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9547 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9548 break;
9549 default:
9550 internalError ();
9551 }
9552
9553 lastregno = regno;
9554 continue;
9555
9556 case 'P':
9557 if (strncmp (s, "$pc", 3) == 0)
9558 {
9559 s += 3;
9560 continue;
9561 }
9562 break;
9563
9564 case '<':
9565 case '>':
9566 case '[':
9567 case ']':
9568 case '4':
9569 case '5':
9570 case 'H':
9571 case 'W':
9572 case 'D':
9573 case 'j':
9574 case '8':
9575 case 'V':
9576 case 'C':
9577 case 'U':
9578 case 'k':
9579 case 'K':
9580 if (s[0] == '%'
9581 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9582 {
9583 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9584 and generate the appropriate reloc. If the text
9585 inside %gprel is not a symbol name with an
9586 optional offset, then we generate a normal reloc
9587 and will probably fail later. */
9588 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9589 if (imm_expr.X_op == O_symbol)
9590 {
9591 mips16_ext = TRUE;
9592 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9593 s = expr_end;
9594 ip->use_extend = TRUE;
9595 ip->extend = 0;
9596 continue;
9597 }
9598 }
9599 else
9600 {
9601 /* Just pick up a normal expression. */
9602 my_getExpression (&imm_expr, s);
9603 }
9604
9605 if (imm_expr.X_op == O_register)
9606 {
9607 /* What we thought was an expression turned out to
9608 be a register. */
9609
9610 if (s[0] == '(' && args[1] == '(')
9611 {
9612 /* It looks like the expression was omitted
9613 before a register indirection, which means
9614 that the expression is implicitly zero. We
9615 still set up imm_expr, so that we handle
9616 explicit extensions correctly. */
9617 imm_expr.X_op = O_constant;
9618 imm_expr.X_add_number = 0;
9619 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9620 continue;
9621 }
9622
9623 break;
9624 }
9625
9626 /* We need to relax this instruction. */
9627 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9628 s = expr_end;
9629 continue;
9630
9631 case 'p':
9632 case 'q':
9633 case 'A':
9634 case 'B':
9635 case 'E':
9636 /* We use offset_reloc rather than imm_reloc for the PC
9637 relative operands. This lets macros with both
9638 immediate and address operands work correctly. */
9639 my_getExpression (&offset_expr, s);
9640
9641 if (offset_expr.X_op == O_register)
9642 break;
9643
9644 /* We need to relax this instruction. */
9645 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9646 s = expr_end;
9647 continue;
9648
9649 case '6': /* break code */
9650 my_getExpression (&imm_expr, s);
9651 check_absolute_expr (ip, &imm_expr);
9652 if ((unsigned long) imm_expr.X_add_number > 63)
9653 {
9654 as_warn (_("Invalid value for `%s' (%lu)"),
9655 ip->insn_mo->name,
9656 (unsigned long) imm_expr.X_add_number);
9657 imm_expr.X_add_number &= 0x3f;
9658 }
9659 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9660 imm_expr.X_op = O_absent;
9661 s = expr_end;
9662 continue;
9663
9664 case 'a': /* 26 bit address */
9665 my_getExpression (&offset_expr, s);
9666 s = expr_end;
9667 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9668 ip->insn_opcode <<= 16;
9669 continue;
9670
9671 case 'l': /* register list for entry macro */
9672 case 'L': /* register list for exit macro */
9673 {
9674 int mask;
9675
9676 if (c == 'l')
9677 mask = 0;
9678 else
9679 mask = 7 << 3;
9680 while (*s != '\0')
9681 {
9682 int freg, reg1, reg2;
9683
9684 while (*s == ' ' || *s == ',')
9685 ++s;
9686 if (*s != '$')
9687 {
9688 as_bad (_("can't parse register list"));
9689 break;
9690 }
9691 ++s;
9692 if (*s != 'f')
9693 freg = 0;
9694 else
9695 {
9696 freg = 1;
9697 ++s;
9698 }
9699 reg1 = 0;
9700 while (ISDIGIT (*s))
9701 {
9702 reg1 *= 10;
9703 reg1 += *s - '0';
9704 ++s;
9705 }
9706 if (*s == ' ')
9707 ++s;
9708 if (*s != '-')
9709 reg2 = reg1;
9710 else
9711 {
9712 ++s;
9713 if (*s != '$')
9714 break;
9715 ++s;
9716 if (freg)
9717 {
9718 if (*s == 'f')
9719 ++s;
9720 else
9721 {
9722 as_bad (_("invalid register list"));
9723 break;
9724 }
9725 }
9726 reg2 = 0;
9727 while (ISDIGIT (*s))
9728 {
9729 reg2 *= 10;
9730 reg2 += *s - '0';
9731 ++s;
9732 }
9733 }
9734 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9735 {
9736 mask &= ~ (7 << 3);
9737 mask |= 5 << 3;
9738 }
9739 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9740 {
9741 mask &= ~ (7 << 3);
9742 mask |= 6 << 3;
9743 }
9744 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9745 mask |= (reg2 - 3) << 3;
9746 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9747 mask |= (reg2 - 15) << 1;
9748 else if (reg1 == RA && reg2 == RA)
9749 mask |= 1;
9750 else
9751 {
9752 as_bad (_("invalid register list"));
9753 break;
9754 }
9755 }
9756 /* The mask is filled in in the opcode table for the
9757 benefit of the disassembler. We remove it before
9758 applying the actual mask. */
9759 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9760 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9761 }
9762 continue;
9763
9764 case 'e': /* extend code */
9765 my_getExpression (&imm_expr, s);
9766 check_absolute_expr (ip, &imm_expr);
9767 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9768 {
9769 as_warn (_("Invalid value for `%s' (%lu)"),
9770 ip->insn_mo->name,
9771 (unsigned long) imm_expr.X_add_number);
9772 imm_expr.X_add_number &= 0x7ff;
9773 }
9774 ip->insn_opcode |= imm_expr.X_add_number;
9775 imm_expr.X_op = O_absent;
9776 s = expr_end;
9777 continue;
9778
9779 default:
9780 internalError ();
9781 }
9782 break;
9783 }
9784
9785 /* Args don't match. */
9786 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9787 strcmp (insn->name, insn[1].name) == 0)
9788 {
9789 ++insn;
9790 s = argsstart;
9791 continue;
9792 }
9793
9794 insn_error = _("illegal operands");
9795
9796 return;
9797 }
9798 }
9799
9800 /* This structure holds information we know about a mips16 immediate
9801 argument type. */
9802
9803 struct mips16_immed_operand
9804 {
9805 /* The type code used in the argument string in the opcode table. */
9806 int type;
9807 /* The number of bits in the short form of the opcode. */
9808 int nbits;
9809 /* The number of bits in the extended form of the opcode. */
9810 int extbits;
9811 /* The amount by which the short form is shifted when it is used;
9812 for example, the sw instruction has a shift count of 2. */
9813 int shift;
9814 /* The amount by which the short form is shifted when it is stored
9815 into the instruction code. */
9816 int op_shift;
9817 /* Non-zero if the short form is unsigned. */
9818 int unsp;
9819 /* Non-zero if the extended form is unsigned. */
9820 int extu;
9821 /* Non-zero if the value is PC relative. */
9822 int pcrel;
9823 };
9824
9825 /* The mips16 immediate operand types. */
9826
9827 static const struct mips16_immed_operand mips16_immed_operands[] =
9828 {
9829 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9830 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9831 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9832 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9833 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9834 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9835 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9836 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9837 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9838 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9839 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9840 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9841 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9842 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9843 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9844 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9845 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9846 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9847 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9848 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9849 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9850 };
9851
9852 #define MIPS16_NUM_IMMED \
9853 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9854
9855 /* Handle a mips16 instruction with an immediate value. This or's the
9856 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9857 whether an extended value is needed; if one is needed, it sets
9858 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9859 If SMALL is true, an unextended opcode was explicitly requested.
9860 If EXT is true, an extended opcode was explicitly requested. If
9861 WARN is true, warn if EXT does not match reality. */
9862
9863 static void
9864 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9865 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9866 unsigned long *insn, bfd_boolean *use_extend,
9867 unsigned short *extend)
9868 {
9869 register const struct mips16_immed_operand *op;
9870 int mintiny, maxtiny;
9871 bfd_boolean needext;
9872
9873 op = mips16_immed_operands;
9874 while (op->type != type)
9875 {
9876 ++op;
9877 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9878 }
9879
9880 if (op->unsp)
9881 {
9882 if (type == '<' || type == '>' || type == '[' || type == ']')
9883 {
9884 mintiny = 1;
9885 maxtiny = 1 << op->nbits;
9886 }
9887 else
9888 {
9889 mintiny = 0;
9890 maxtiny = (1 << op->nbits) - 1;
9891 }
9892 }
9893 else
9894 {
9895 mintiny = - (1 << (op->nbits - 1));
9896 maxtiny = (1 << (op->nbits - 1)) - 1;
9897 }
9898
9899 /* Branch offsets have an implicit 0 in the lowest bit. */
9900 if (type == 'p' || type == 'q')
9901 val /= 2;
9902
9903 if ((val & ((1 << op->shift) - 1)) != 0
9904 || val < (mintiny << op->shift)
9905 || val > (maxtiny << op->shift))
9906 needext = TRUE;
9907 else
9908 needext = FALSE;
9909
9910 if (warn && ext && ! needext)
9911 as_warn_where (file, line,
9912 _("extended operand requested but not required"));
9913 if (small && needext)
9914 as_bad_where (file, line, _("invalid unextended operand value"));
9915
9916 if (small || (! ext && ! needext))
9917 {
9918 int insnval;
9919
9920 *use_extend = FALSE;
9921 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9922 insnval <<= op->op_shift;
9923 *insn |= insnval;
9924 }
9925 else
9926 {
9927 long minext, maxext;
9928 int extval;
9929
9930 if (op->extu)
9931 {
9932 minext = 0;
9933 maxext = (1 << op->extbits) - 1;
9934 }
9935 else
9936 {
9937 minext = - (1 << (op->extbits - 1));
9938 maxext = (1 << (op->extbits - 1)) - 1;
9939 }
9940 if (val < minext || val > maxext)
9941 as_bad_where (file, line,
9942 _("operand value out of range for instruction"));
9943
9944 *use_extend = TRUE;
9945 if (op->extbits == 16)
9946 {
9947 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9948 val &= 0x1f;
9949 }
9950 else if (op->extbits == 15)
9951 {
9952 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9953 val &= 0xf;
9954 }
9955 else
9956 {
9957 extval = ((val & 0x1f) << 6) | (val & 0x20);
9958 val = 0;
9959 }
9960
9961 *extend = (unsigned short) extval;
9962 *insn |= val;
9963 }
9964 }
9965 \f
9966 static const struct percent_op_match
9967 {
9968 const char *str;
9969 bfd_reloc_code_real_type reloc;
9970 } percent_op[] =
9971 {
9972 {"%lo", BFD_RELOC_LO16},
9973 #ifdef OBJ_ELF
9974 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9975 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9976 {"%call16", BFD_RELOC_MIPS_CALL16},
9977 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9978 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9979 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9980 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9981 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9982 {"%got", BFD_RELOC_MIPS_GOT16},
9983 {"%gp_rel", BFD_RELOC_GPREL16},
9984 {"%half", BFD_RELOC_16},
9985 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9986 {"%higher", BFD_RELOC_MIPS_HIGHER},
9987 {"%neg", BFD_RELOC_MIPS_SUB},
9988 #endif
9989 {"%hi", BFD_RELOC_HI16_S}
9990 };
9991
9992
9993 /* Return true if *STR points to a relocation operator. When returning true,
9994 move *STR over the operator and store its relocation code in *RELOC.
9995 Leave both *STR and *RELOC alone when returning false. */
9996
9997 static bfd_boolean
9998 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9999 {
10000 size_t i;
10001
10002 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10003 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10004 {
10005 *str += strlen (percent_op[i].str);
10006 *reloc = percent_op[i].reloc;
10007
10008 /* Check whether the output BFD supports this relocation.
10009 If not, issue an error and fall back on something safe. */
10010 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10011 {
10012 as_bad ("relocation %s isn't supported by the current ABI",
10013 percent_op[i].str);
10014 *reloc = BFD_RELOC_LO16;
10015 }
10016 return TRUE;
10017 }
10018 return FALSE;
10019 }
10020
10021
10022 /* Parse string STR as a 16-bit relocatable operand. Store the
10023 expression in *EP and the relocations in the array starting
10024 at RELOC. Return the number of relocation operators used.
10025
10026 On exit, EXPR_END points to the first character after the expression.
10027 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
10028
10029 static size_t
10030 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10031 char *str)
10032 {
10033 bfd_reloc_code_real_type reversed_reloc[3];
10034 size_t reloc_index, i;
10035 int crux_depth, str_depth;
10036 char *crux;
10037
10038 /* Search for the start of the main expression, recoding relocations
10039 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10040 of the main expression and with CRUX_DEPTH containing the number
10041 of open brackets at that point. */
10042 reloc_index = -1;
10043 str_depth = 0;
10044 do
10045 {
10046 reloc_index++;
10047 crux = str;
10048 crux_depth = str_depth;
10049
10050 /* Skip over whitespace and brackets, keeping count of the number
10051 of brackets. */
10052 while (*str == ' ' || *str == '\t' || *str == '(')
10053 if (*str++ == '(')
10054 str_depth++;
10055 }
10056 while (*str == '%'
10057 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10058 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10059
10060 my_getExpression (ep, crux);
10061 str = expr_end;
10062
10063 /* Match every open bracket. */
10064 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10065 if (*str++ == ')')
10066 crux_depth--;
10067
10068 if (crux_depth > 0)
10069 as_bad ("unclosed '('");
10070
10071 expr_end = str;
10072
10073 if (reloc_index == 0)
10074 reloc[0] = BFD_RELOC_LO16;
10075 else
10076 {
10077 prev_reloc_op_frag = frag_now;
10078 for (i = 0; i < reloc_index; i++)
10079 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10080 }
10081
10082 return reloc_index;
10083 }
10084
10085 static void
10086 my_getExpression (expressionS *ep, char *str)
10087 {
10088 char *save_in;
10089 valueT val;
10090
10091 save_in = input_line_pointer;
10092 input_line_pointer = str;
10093 expression (ep);
10094 expr_end = input_line_pointer;
10095 input_line_pointer = save_in;
10096
10097 /* If we are in mips16 mode, and this is an expression based on `.',
10098 then we bump the value of the symbol by 1 since that is how other
10099 text symbols are handled. We don't bother to handle complex
10100 expressions, just `.' plus or minus a constant. */
10101 if (mips_opts.mips16
10102 && ep->X_op == O_symbol
10103 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10104 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10105 && symbol_get_frag (ep->X_add_symbol) == frag_now
10106 && symbol_constant_p (ep->X_add_symbol)
10107 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10108 S_SET_VALUE (ep->X_add_symbol, val + 1);
10109 }
10110
10111 /* Turn a string in input_line_pointer into a floating point constant
10112 of type TYPE, and store the appropriate bytes in *LITP. The number
10113 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10114 returned, or NULL on OK. */
10115
10116 char *
10117 md_atof (int type, char *litP, int *sizeP)
10118 {
10119 int prec;
10120 LITTLENUM_TYPE words[4];
10121 char *t;
10122 int i;
10123
10124 switch (type)
10125 {
10126 case 'f':
10127 prec = 2;
10128 break;
10129
10130 case 'd':
10131 prec = 4;
10132 break;
10133
10134 default:
10135 *sizeP = 0;
10136 return _("bad call to md_atof");
10137 }
10138
10139 t = atof_ieee (input_line_pointer, type, words);
10140 if (t)
10141 input_line_pointer = t;
10142
10143 *sizeP = prec * 2;
10144
10145 if (! target_big_endian)
10146 {
10147 for (i = prec - 1; i >= 0; i--)
10148 {
10149 md_number_to_chars (litP, words[i], 2);
10150 litP += 2;
10151 }
10152 }
10153 else
10154 {
10155 for (i = 0; i < prec; i++)
10156 {
10157 md_number_to_chars (litP, words[i], 2);
10158 litP += 2;
10159 }
10160 }
10161
10162 return NULL;
10163 }
10164
10165 void
10166 md_number_to_chars (char *buf, valueT val, int n)
10167 {
10168 if (target_big_endian)
10169 number_to_chars_bigendian (buf, val, n);
10170 else
10171 number_to_chars_littleendian (buf, val, n);
10172 }
10173 \f
10174 #ifdef OBJ_ELF
10175 static int support_64bit_objects(void)
10176 {
10177 const char **list, **l;
10178 int yes;
10179
10180 list = bfd_target_list ();
10181 for (l = list; *l != NULL; l++)
10182 #ifdef TE_TMIPS
10183 /* This is traditional mips */
10184 if (strcmp (*l, "elf64-tradbigmips") == 0
10185 || strcmp (*l, "elf64-tradlittlemips") == 0)
10186 #else
10187 if (strcmp (*l, "elf64-bigmips") == 0
10188 || strcmp (*l, "elf64-littlemips") == 0)
10189 #endif
10190 break;
10191 yes = (*l != NULL);
10192 free (list);
10193 return yes;
10194 }
10195 #endif /* OBJ_ELF */
10196
10197 const char *md_shortopts = "nO::g::G:";
10198
10199 struct option md_longopts[] =
10200 {
10201 /* Options which specify architecture. */
10202 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10203 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10204 {"march", required_argument, NULL, OPTION_MARCH},
10205 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10206 {"mtune", required_argument, NULL, OPTION_MTUNE},
10207 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10208 {"mips0", no_argument, NULL, OPTION_MIPS1},
10209 {"mips1", no_argument, NULL, OPTION_MIPS1},
10210 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10211 {"mips2", no_argument, NULL, OPTION_MIPS2},
10212 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10213 {"mips3", no_argument, NULL, OPTION_MIPS3},
10214 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10215 {"mips4", no_argument, NULL, OPTION_MIPS4},
10216 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10217 {"mips5", no_argument, NULL, OPTION_MIPS5},
10218 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10219 {"mips32", no_argument, NULL, OPTION_MIPS32},
10220 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10221 {"mips64", no_argument, NULL, OPTION_MIPS64},
10222 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10223 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10224
10225 /* Options which specify Application Specific Extensions (ASEs). */
10226 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 10)
10227 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10228 {"mips16", no_argument, NULL, OPTION_MIPS16},
10229 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10230 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10231 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10232 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10233 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10234 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10235 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10236 {"mdmx", no_argument, NULL, OPTION_MDMX},
10237 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10238 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10239
10240 /* Old-style architecture options. Don't add more of these. */
10241 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10242 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10243 {"m4650", no_argument, NULL, OPTION_M4650},
10244 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10245 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10246 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10247 {"m4010", no_argument, NULL, OPTION_M4010},
10248 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10249 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10250 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10251 {"m4100", no_argument, NULL, OPTION_M4100},
10252 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10253 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10254 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10255 {"m3900", no_argument, NULL, OPTION_M3900},
10256 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10257 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10258
10259 /* Options which enable bug fixes. */
10260 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10261 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10262 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10263 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10264 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10265 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10266 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10267 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10268 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10269 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10270
10271 /* Miscellaneous options. */
10272 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10273 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10274 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10275 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10276 {"trap", no_argument, NULL, OPTION_TRAP},
10277 {"no-break", no_argument, NULL, OPTION_TRAP},
10278 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10279 {"break", no_argument, NULL, OPTION_BREAK},
10280 {"no-trap", no_argument, NULL, OPTION_BREAK},
10281 #define OPTION_EB (OPTION_MISC_BASE + 3)
10282 {"EB", no_argument, NULL, OPTION_EB},
10283 #define OPTION_EL (OPTION_MISC_BASE + 4)
10284 {"EL", no_argument, NULL, OPTION_EL},
10285 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10286 {"mfp32", no_argument, NULL, OPTION_FP32},
10287 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10288 {"mgp32", no_argument, NULL, OPTION_GP32},
10289 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10290 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10291 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10292 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10293 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10294 {"mfp64", no_argument, NULL, OPTION_FP64},
10295 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10296 {"mgp64", no_argument, NULL, OPTION_GP64},
10297 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10298 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10299 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10300 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10301
10302 /* ELF-specific options. */
10303 #ifdef OBJ_ELF
10304 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
10305 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10306 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10307 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10308 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10309 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10310 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10311 {"xgot", no_argument, NULL, OPTION_XGOT},
10312 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10313 {"mabi", required_argument, NULL, OPTION_MABI},
10314 #define OPTION_32 (OPTION_ELF_BASE + 4)
10315 {"32", no_argument, NULL, OPTION_32},
10316 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10317 {"n32", no_argument, NULL, OPTION_N32},
10318 #define OPTION_64 (OPTION_ELF_BASE + 6)
10319 {"64", no_argument, NULL, OPTION_64},
10320 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10321 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10322 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10323 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10324 #endif /* OBJ_ELF */
10325
10326 {NULL, no_argument, NULL, 0}
10327 };
10328 size_t md_longopts_size = sizeof (md_longopts);
10329
10330 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10331 NEW_VALUE. Warn if another value was already specified. Note:
10332 we have to defer parsing the -march and -mtune arguments in order
10333 to handle 'from-abi' correctly, since the ABI might be specified
10334 in a later argument. */
10335
10336 static void
10337 mips_set_option_string (const char **string_ptr, const char *new_value)
10338 {
10339 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10340 as_warn (_("A different %s was already specified, is now %s"),
10341 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10342 new_value);
10343
10344 *string_ptr = new_value;
10345 }
10346
10347 int
10348 md_parse_option (int c, char *arg)
10349 {
10350 switch (c)
10351 {
10352 case OPTION_CONSTRUCT_FLOATS:
10353 mips_disable_float_construction = 0;
10354 break;
10355
10356 case OPTION_NO_CONSTRUCT_FLOATS:
10357 mips_disable_float_construction = 1;
10358 break;
10359
10360 case OPTION_TRAP:
10361 mips_trap = 1;
10362 break;
10363
10364 case OPTION_BREAK:
10365 mips_trap = 0;
10366 break;
10367
10368 case OPTION_EB:
10369 target_big_endian = 1;
10370 break;
10371
10372 case OPTION_EL:
10373 target_big_endian = 0;
10374 break;
10375
10376 case 'n':
10377 warn_nops = 1;
10378 break;
10379
10380 case 'O':
10381 if (arg && arg[1] == '0')
10382 mips_optimize = 1;
10383 else
10384 mips_optimize = 2;
10385 break;
10386
10387 case 'g':
10388 if (arg == NULL)
10389 mips_debug = 2;
10390 else
10391 mips_debug = atoi (arg);
10392 /* When the MIPS assembler sees -g or -g2, it does not do
10393 optimizations which limit full symbolic debugging. We take
10394 that to be equivalent to -O0. */
10395 if (mips_debug == 2)
10396 mips_optimize = 1;
10397 break;
10398
10399 case OPTION_MIPS1:
10400 file_mips_isa = ISA_MIPS1;
10401 break;
10402
10403 case OPTION_MIPS2:
10404 file_mips_isa = ISA_MIPS2;
10405 break;
10406
10407 case OPTION_MIPS3:
10408 file_mips_isa = ISA_MIPS3;
10409 break;
10410
10411 case OPTION_MIPS4:
10412 file_mips_isa = ISA_MIPS4;
10413 break;
10414
10415 case OPTION_MIPS5:
10416 file_mips_isa = ISA_MIPS5;
10417 break;
10418
10419 case OPTION_MIPS32:
10420 file_mips_isa = ISA_MIPS32;
10421 break;
10422
10423 case OPTION_MIPS32R2:
10424 file_mips_isa = ISA_MIPS32R2;
10425 break;
10426
10427 case OPTION_MIPS64:
10428 file_mips_isa = ISA_MIPS64;
10429 break;
10430
10431 case OPTION_MTUNE:
10432 mips_set_option_string (&mips_tune_string, arg);
10433 break;
10434
10435 case OPTION_MARCH:
10436 mips_set_option_string (&mips_arch_string, arg);
10437 break;
10438
10439 case OPTION_M4650:
10440 mips_set_option_string (&mips_arch_string, "4650");
10441 mips_set_option_string (&mips_tune_string, "4650");
10442 break;
10443
10444 case OPTION_NO_M4650:
10445 break;
10446
10447 case OPTION_M4010:
10448 mips_set_option_string (&mips_arch_string, "4010");
10449 mips_set_option_string (&mips_tune_string, "4010");
10450 break;
10451
10452 case OPTION_NO_M4010:
10453 break;
10454
10455 case OPTION_M4100:
10456 mips_set_option_string (&mips_arch_string, "4100");
10457 mips_set_option_string (&mips_tune_string, "4100");
10458 break;
10459
10460 case OPTION_NO_M4100:
10461 break;
10462
10463 case OPTION_M3900:
10464 mips_set_option_string (&mips_arch_string, "3900");
10465 mips_set_option_string (&mips_tune_string, "3900");
10466 break;
10467
10468 case OPTION_NO_M3900:
10469 break;
10470
10471 case OPTION_MDMX:
10472 mips_opts.ase_mdmx = 1;
10473 break;
10474
10475 case OPTION_NO_MDMX:
10476 mips_opts.ase_mdmx = 0;
10477 break;
10478
10479 case OPTION_MIPS16:
10480 mips_opts.mips16 = 1;
10481 mips_no_prev_insn (FALSE);
10482 break;
10483
10484 case OPTION_NO_MIPS16:
10485 mips_opts.mips16 = 0;
10486 mips_no_prev_insn (FALSE);
10487 break;
10488
10489 case OPTION_MIPS3D:
10490 mips_opts.ase_mips3d = 1;
10491 break;
10492
10493 case OPTION_NO_MIPS3D:
10494 mips_opts.ase_mips3d = 0;
10495 break;
10496
10497 case OPTION_MEMBEDDED_PIC:
10498 mips_pic = EMBEDDED_PIC;
10499 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10500 {
10501 as_bad (_("-G may not be used with embedded PIC code"));
10502 return 0;
10503 }
10504 g_switch_value = 0x7fffffff;
10505 break;
10506
10507 case OPTION_FIX_VR4122:
10508 mips_fix_4122_bugs = 1;
10509 break;
10510
10511 case OPTION_NO_FIX_VR4122:
10512 mips_fix_4122_bugs = 0;
10513 break;
10514
10515 case OPTION_RELAX_BRANCH:
10516 mips_relax_branch = 1;
10517 break;
10518
10519 case OPTION_NO_RELAX_BRANCH:
10520 mips_relax_branch = 0;
10521 break;
10522
10523 #ifdef OBJ_ELF
10524 /* When generating ELF code, we permit -KPIC and -call_shared to
10525 select SVR4_PIC, and -non_shared to select no PIC. This is
10526 intended to be compatible with Irix 5. */
10527 case OPTION_CALL_SHARED:
10528 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10529 {
10530 as_bad (_("-call_shared is supported only for ELF format"));
10531 return 0;
10532 }
10533 mips_pic = SVR4_PIC;
10534 mips_abicalls = TRUE;
10535 if (g_switch_seen && g_switch_value != 0)
10536 {
10537 as_bad (_("-G may not be used with SVR4 PIC code"));
10538 return 0;
10539 }
10540 g_switch_value = 0;
10541 break;
10542
10543 case OPTION_NON_SHARED:
10544 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10545 {
10546 as_bad (_("-non_shared is supported only for ELF format"));
10547 return 0;
10548 }
10549 mips_pic = NO_PIC;
10550 mips_abicalls = FALSE;
10551 break;
10552
10553 /* The -xgot option tells the assembler to use 32 offsets when
10554 accessing the got in SVR4_PIC mode. It is for Irix
10555 compatibility. */
10556 case OPTION_XGOT:
10557 mips_big_got = 1;
10558 break;
10559 #endif /* OBJ_ELF */
10560
10561 case 'G':
10562 if (! USE_GLOBAL_POINTER_OPT)
10563 {
10564 as_bad (_("-G is not supported for this configuration"));
10565 return 0;
10566 }
10567 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10568 {
10569 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10570 return 0;
10571 }
10572 else
10573 g_switch_value = atoi (arg);
10574 g_switch_seen = 1;
10575 break;
10576
10577 #ifdef OBJ_ELF
10578 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10579 and -mabi=64. */
10580 case OPTION_32:
10581 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10582 {
10583 as_bad (_("-32 is supported for ELF format only"));
10584 return 0;
10585 }
10586 mips_abi = O32_ABI;
10587 break;
10588
10589 case OPTION_N32:
10590 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10591 {
10592 as_bad (_("-n32 is supported for ELF format only"));
10593 return 0;
10594 }
10595 mips_abi = N32_ABI;
10596 break;
10597
10598 case OPTION_64:
10599 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10600 {
10601 as_bad (_("-64 is supported for ELF format only"));
10602 return 0;
10603 }
10604 mips_abi = N64_ABI;
10605 if (! support_64bit_objects())
10606 as_fatal (_("No compiled in support for 64 bit object file format"));
10607 break;
10608 #endif /* OBJ_ELF */
10609
10610 case OPTION_GP32:
10611 file_mips_gp32 = 1;
10612 break;
10613
10614 case OPTION_GP64:
10615 file_mips_gp32 = 0;
10616 break;
10617
10618 case OPTION_FP32:
10619 file_mips_fp32 = 1;
10620 break;
10621
10622 case OPTION_FP64:
10623 file_mips_fp32 = 0;
10624 break;
10625
10626 #ifdef OBJ_ELF
10627 case OPTION_MABI:
10628 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10629 {
10630 as_bad (_("-mabi is supported for ELF format only"));
10631 return 0;
10632 }
10633 if (strcmp (arg, "32") == 0)
10634 mips_abi = O32_ABI;
10635 else if (strcmp (arg, "o64") == 0)
10636 mips_abi = O64_ABI;
10637 else if (strcmp (arg, "n32") == 0)
10638 mips_abi = N32_ABI;
10639 else if (strcmp (arg, "64") == 0)
10640 {
10641 mips_abi = N64_ABI;
10642 if (! support_64bit_objects())
10643 as_fatal (_("No compiled in support for 64 bit object file "
10644 "format"));
10645 }
10646 else if (strcmp (arg, "eabi") == 0)
10647 mips_abi = EABI_ABI;
10648 else
10649 {
10650 as_fatal (_("invalid abi -mabi=%s"), arg);
10651 return 0;
10652 }
10653 break;
10654 #endif /* OBJ_ELF */
10655
10656 case OPTION_M7000_HILO_FIX:
10657 mips_7000_hilo_fix = TRUE;
10658 break;
10659
10660 case OPTION_MNO_7000_HILO_FIX:
10661 mips_7000_hilo_fix = FALSE;
10662 break;
10663
10664 #ifdef OBJ_ELF
10665 case OPTION_MDEBUG:
10666 mips_flag_mdebug = TRUE;
10667 break;
10668
10669 case OPTION_NO_MDEBUG:
10670 mips_flag_mdebug = FALSE;
10671 break;
10672 #endif /* OBJ_ELF */
10673
10674 default:
10675 return 0;
10676 }
10677
10678 return 1;
10679 }
10680 \f
10681 /* Set up globals to generate code for the ISA or processor
10682 described by INFO. */
10683
10684 static void
10685 mips_set_architecture (const struct mips_cpu_info *info)
10686 {
10687 if (info != 0)
10688 {
10689 file_mips_arch = info->cpu;
10690 mips_opts.arch = info->cpu;
10691 mips_opts.isa = info->isa;
10692 }
10693 }
10694
10695
10696 /* Likewise for tuning. */
10697
10698 static void
10699 mips_set_tune (const struct mips_cpu_info *info)
10700 {
10701 if (info != 0)
10702 mips_tune = info->cpu;
10703 }
10704
10705
10706 void
10707 mips_after_parse_args (void)
10708 {
10709 const struct mips_cpu_info *arch_info = 0;
10710 const struct mips_cpu_info *tune_info = 0;
10711
10712 /* GP relative stuff not working for PE */
10713 if (strncmp (TARGET_OS, "pe", 2) == 0
10714 && g_switch_value != 0)
10715 {
10716 if (g_switch_seen)
10717 as_bad (_("-G not supported in this configuration."));
10718 g_switch_value = 0;
10719 }
10720
10721 if (mips_abi == NO_ABI)
10722 mips_abi = MIPS_DEFAULT_ABI;
10723
10724 /* The following code determines the architecture and register size.
10725 Similar code was added to GCC 3.3 (see override_options() in
10726 config/mips/mips.c). The GAS and GCC code should be kept in sync
10727 as much as possible. */
10728
10729 if (mips_arch_string != 0)
10730 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10731
10732 if (file_mips_isa != ISA_UNKNOWN)
10733 {
10734 /* Handle -mipsN. At this point, file_mips_isa contains the
10735 ISA level specified by -mipsN, while arch_info->isa contains
10736 the -march selection (if any). */
10737 if (arch_info != 0)
10738 {
10739 /* -march takes precedence over -mipsN, since it is more descriptive.
10740 There's no harm in specifying both as long as the ISA levels
10741 are the same. */
10742 if (file_mips_isa != arch_info->isa)
10743 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10744 mips_cpu_info_from_isa (file_mips_isa)->name,
10745 mips_cpu_info_from_isa (arch_info->isa)->name);
10746 }
10747 else
10748 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10749 }
10750
10751 if (arch_info == 0)
10752 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10753
10754 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10755 as_bad ("-march=%s is not compatible with the selected ABI",
10756 arch_info->name);
10757
10758 mips_set_architecture (arch_info);
10759
10760 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10761 if (mips_tune_string != 0)
10762 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10763
10764 if (tune_info == 0)
10765 mips_set_tune (arch_info);
10766 else
10767 mips_set_tune (tune_info);
10768
10769 if (file_mips_gp32 >= 0)
10770 {
10771 /* The user specified the size of the integer registers. Make sure
10772 it agrees with the ABI and ISA. */
10773 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10774 as_bad (_("-mgp64 used with a 32-bit processor"));
10775 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10776 as_bad (_("-mgp32 used with a 64-bit ABI"));
10777 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10778 as_bad (_("-mgp64 used with a 32-bit ABI"));
10779 }
10780 else
10781 {
10782 /* Infer the integer register size from the ABI and processor.
10783 Restrict ourselves to 32-bit registers if that's all the
10784 processor has, or if the ABI cannot handle 64-bit registers. */
10785 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10786 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10787 }
10788
10789 /* ??? GAS treats single-float processors as though they had 64-bit
10790 float registers (although it complains when double-precision
10791 instructions are used). As things stand, saying they have 32-bit
10792 registers would lead to spurious "register must be even" messages.
10793 So here we assume float registers are always the same size as
10794 integer ones, unless the user says otherwise. */
10795 if (file_mips_fp32 < 0)
10796 file_mips_fp32 = file_mips_gp32;
10797
10798 /* End of GCC-shared inference code. */
10799
10800 /* This flag is set when we have a 64-bit capable CPU but use only
10801 32-bit wide registers. Note that EABI does not use it. */
10802 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10803 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10804 || mips_abi == O32_ABI))
10805 mips_32bitmode = 1;
10806
10807 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10808 as_bad (_("trap exception not supported at ISA 1"));
10809
10810 /* If the selected architecture includes support for ASEs, enable
10811 generation of code for them. */
10812 if (mips_opts.mips16 == -1)
10813 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10814 if (mips_opts.ase_mips3d == -1)
10815 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10816 if (mips_opts.ase_mdmx == -1)
10817 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10818
10819 file_mips_isa = mips_opts.isa;
10820 file_ase_mips16 = mips_opts.mips16;
10821 file_ase_mips3d = mips_opts.ase_mips3d;
10822 file_ase_mdmx = mips_opts.ase_mdmx;
10823 mips_opts.gp32 = file_mips_gp32;
10824 mips_opts.fp32 = file_mips_fp32;
10825
10826 if (mips_flag_mdebug < 0)
10827 {
10828 #ifdef OBJ_MAYBE_ECOFF
10829 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10830 mips_flag_mdebug = 1;
10831 else
10832 #endif /* OBJ_MAYBE_ECOFF */
10833 mips_flag_mdebug = 0;
10834 }
10835 }
10836 \f
10837 void
10838 mips_init_after_args (void)
10839 {
10840 /* initialize opcodes */
10841 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10842 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10843 }
10844
10845 long
10846 md_pcrel_from (fixS *fixP)
10847 {
10848 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10849 switch (fixP->fx_r_type)
10850 {
10851 case BFD_RELOC_16_PCREL_S2:
10852 case BFD_RELOC_MIPS_JMP:
10853 /* Return the address of the delay slot. */
10854 return addr + 4;
10855 default:
10856 return addr;
10857 }
10858 }
10859
10860 /* This is called before the symbol table is processed. In order to
10861 work with gcc when using mips-tfile, we must keep all local labels.
10862 However, in other cases, we want to discard them. If we were
10863 called with -g, but we didn't see any debugging information, it may
10864 mean that gcc is smuggling debugging information through to
10865 mips-tfile, in which case we must generate all local labels. */
10866
10867 void
10868 mips_frob_file_before_adjust (void)
10869 {
10870 #ifndef NO_ECOFF_DEBUGGING
10871 if (ECOFF_DEBUGGING
10872 && mips_debug != 0
10873 && ! ecoff_debugging_seen)
10874 flag_keep_locals = 1;
10875 #endif
10876 }
10877
10878 /* Sort any unmatched HI16_S relocs so that they immediately precede
10879 the corresponding LO reloc. This is called before md_apply_fix3 and
10880 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10881 explicit use of the %hi modifier. */
10882
10883 void
10884 mips_frob_file (void)
10885 {
10886 struct mips_hi_fixup *l;
10887
10888 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10889 {
10890 segment_info_type *seginfo;
10891 int pass;
10892
10893 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10894
10895 /* If a GOT16 relocation turns out to be against a global symbol,
10896 there isn't supposed to be a matching LO. */
10897 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10898 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10899 continue;
10900
10901 /* Check quickly whether the next fixup happens to be a matching %lo. */
10902 if (fixup_has_matching_lo_p (l->fixp))
10903 continue;
10904
10905 /* Look through the fixups for this segment for a matching %lo.
10906 When we find one, move the %hi just in front of it. We do
10907 this in two passes. In the first pass, we try to find a
10908 unique %lo. In the second pass, we permit multiple %hi
10909 relocs for a single %lo (this is a GNU extension). */
10910 seginfo = seg_info (l->seg);
10911 for (pass = 0; pass < 2; pass++)
10912 {
10913 fixS *f, *prev;
10914
10915 prev = NULL;
10916 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10917 {
10918 /* Check whether this is a %lo fixup which matches l->fixp. */
10919 if (f->fx_r_type == BFD_RELOC_LO16
10920 && f->fx_addsy == l->fixp->fx_addsy
10921 && f->fx_offset == l->fixp->fx_offset
10922 && (pass == 1
10923 || prev == NULL
10924 || !reloc_needs_lo_p (prev->fx_r_type)
10925 || !fixup_has_matching_lo_p (prev)))
10926 {
10927 fixS **pf;
10928
10929 /* Move l->fixp before f. */
10930 for (pf = &seginfo->fix_root;
10931 *pf != l->fixp;
10932 pf = &(*pf)->fx_next)
10933 assert (*pf != NULL);
10934
10935 *pf = l->fixp->fx_next;
10936
10937 l->fixp->fx_next = f;
10938 if (prev == NULL)
10939 seginfo->fix_root = l->fixp;
10940 else
10941 prev->fx_next = l->fixp;
10942
10943 break;
10944 }
10945
10946 prev = f;
10947 }
10948
10949 if (f != NULL)
10950 break;
10951
10952 #if 0 /* GCC code motion plus incomplete dead code elimination
10953 can leave a %hi without a %lo. */
10954 if (pass == 1)
10955 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10956 _("Unmatched %%hi reloc"));
10957 #endif
10958 }
10959 }
10960 }
10961
10962 /* When generating embedded PIC code we need to use a special
10963 relocation to represent the difference of two symbols in the .text
10964 section (switch tables use a difference of this sort). See
10965 include/coff/mips.h for details. This macro checks whether this
10966 fixup requires the special reloc. */
10967 #define SWITCH_TABLE(fixp) \
10968 ((fixp)->fx_r_type == BFD_RELOC_32 \
10969 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10970 && (fixp)->fx_addsy != NULL \
10971 && (fixp)->fx_subsy != NULL \
10972 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10973 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10974
10975 /* When generating embedded PIC code we must keep all PC relative
10976 relocations, in case the linker has to relax a call. We also need
10977 to keep relocations for switch table entries.
10978
10979 We may have combined relocations without symbols in the N32/N64 ABI.
10980 We have to prevent gas from dropping them. */
10981
10982 int
10983 mips_force_relocation (fixS *fixp)
10984 {
10985 if (generic_force_reloc (fixp))
10986 return 1;
10987
10988 if (HAVE_NEWABI
10989 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10990 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10991 || fixp->fx_r_type == BFD_RELOC_HI16_S
10992 || fixp->fx_r_type == BFD_RELOC_LO16))
10993 return 1;
10994
10995 return (mips_pic == EMBEDDED_PIC
10996 && (fixp->fx_pcrel
10997 || SWITCH_TABLE (fixp)
10998 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10999 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11000 }
11001
11002 /* This hook is called before a fix is simplified. We don't really
11003 decide whether to skip a fix here. Rather, we turn global symbols
11004 used as branch targets into local symbols, such that they undergo
11005 simplification. We can only do this if the symbol is defined and
11006 it is in the same section as the branch. If this doesn't hold, we
11007 emit a better error message than just saying the relocation is not
11008 valid for the selected object format.
11009
11010 FIXP is the fix-up we're going to try to simplify, SEG is the
11011 segment in which the fix up occurs. The return value should be
11012 non-zero to indicate the fix-up is valid for further
11013 simplifications. */
11014
11015 int
11016 mips_validate_fix (struct fix *fixP, asection *seg)
11017 {
11018 /* There's a lot of discussion on whether it should be possible to
11019 use R_MIPS_PC16 to represent branch relocations. The outcome
11020 seems to be that it can, but gas/bfd are very broken in creating
11021 RELA relocations for this, so for now we only accept branches to
11022 symbols in the same section. Anything else is of dubious value,
11023 since there's no guarantee that at link time the symbol would be
11024 in range. Even for branches to local symbols this is arguably
11025 wrong, since it we assume the symbol is not going to be
11026 overridden, which should be possible per ELF library semantics,
11027 but then, there isn't a dynamic relocation that could be used to
11028 this effect, and the target would likely be out of range as well.
11029
11030 Unfortunately, it seems that there is too much code out there
11031 that relies on branches to symbols that are global to be resolved
11032 as if they were local, like the IRIX tools do, so we do it as
11033 well, but with a warning so that people are reminded to fix their
11034 code. If we ever get back to using R_MIPS_PC16 for branch
11035 targets, this entire block should go away (and probably the
11036 whole function). */
11037
11038 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11039 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11040 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11041 && mips_pic != EMBEDDED_PIC)
11042 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11043 && fixP->fx_addsy)
11044 {
11045 if (! S_IS_DEFINED (fixP->fx_addsy))
11046 {
11047 as_bad_where (fixP->fx_file, fixP->fx_line,
11048 _("Cannot branch to undefined symbol."));
11049 /* Avoid any further errors about this fixup. */
11050 fixP->fx_done = 1;
11051 }
11052 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11053 {
11054 as_bad_where (fixP->fx_file, fixP->fx_line,
11055 _("Cannot branch to symbol in another section."));
11056 fixP->fx_done = 1;
11057 }
11058 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11059 {
11060 symbolS *sym = fixP->fx_addsy;
11061
11062 as_warn_where (fixP->fx_file, fixP->fx_line,
11063 _("Pretending global symbol used as branch target is local."));
11064
11065 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11066 S_GET_SEGMENT (sym),
11067 S_GET_VALUE (sym),
11068 symbol_get_frag (sym));
11069 copy_symbol_attributes (fixP->fx_addsy, sym);
11070 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11071 assert (symbol_resolved_p (sym));
11072 symbol_mark_resolved (fixP->fx_addsy);
11073 }
11074 }
11075
11076 return 1;
11077 }
11078
11079 #ifdef OBJ_ELF
11080 static int
11081 mips_need_elf_addend_fixup (fixS *fixP)
11082 {
11083 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11084 return 1;
11085 if (mips_pic == EMBEDDED_PIC
11086 && S_IS_WEAK (fixP->fx_addsy))
11087 return 1;
11088 if (mips_pic != EMBEDDED_PIC
11089 && (S_IS_WEAK (fixP->fx_addsy)
11090 || S_IS_EXTERNAL (fixP->fx_addsy))
11091 && !S_IS_COMMON (fixP->fx_addsy))
11092 return 1;
11093 if (((bfd_get_section_flags (stdoutput,
11094 S_GET_SEGMENT (fixP->fx_addsy))
11095 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11096 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11097 ".gnu.linkonce",
11098 sizeof (".gnu.linkonce") - 1))
11099 return 1;
11100 return 0;
11101 }
11102 #endif
11103
11104 /* Apply a fixup to the object file. */
11105
11106 void
11107 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11108 {
11109 bfd_byte *buf;
11110 long insn;
11111 static int previous_fx_r_type = 0;
11112 reloc_howto_type *howto;
11113
11114 /* We ignore generic BFD relocations we don't know about. */
11115 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11116 if (! howto)
11117 return;
11118
11119 assert (fixP->fx_size == 4
11120 || fixP->fx_r_type == BFD_RELOC_16
11121 || fixP->fx_r_type == BFD_RELOC_64
11122 || fixP->fx_r_type == BFD_RELOC_CTOR
11123 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11124 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11125 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11126
11127 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11128
11129 /* If we aren't adjusting this fixup to be against the section
11130 symbol, we need to adjust the value. */
11131 #ifdef OBJ_ELF
11132 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11133 {
11134 if (mips_need_elf_addend_fixup (fixP)
11135 && howto->partial_inplace
11136 && fixP->fx_r_type != BFD_RELOC_GPREL16
11137 && fixP->fx_r_type != BFD_RELOC_GPREL32
11138 && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11139 {
11140 /* In this case, the bfd_install_relocation routine will
11141 incorrectly add the symbol value back in. We just want
11142 the addend to appear in the object file.
11143
11144 The condition above used to include
11145 "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11146
11147 However, howto can't be trusted here, because we
11148 might change the reloc type in tc_gen_reloc. We can
11149 check howto->partial_inplace because that conversion
11150 happens to preserve howto->partial_inplace; but it
11151 does not preserve howto->pcrel_offset. I've just
11152 eliminated the check, because all MIPS PC-relative
11153 relocations are marked howto->pcrel_offset.
11154
11155 howto->pcrel_offset was originally added for
11156 R_MIPS_PC16, which is generated for code like
11157
11158 globl g1 .text
11159 .text
11160 .space 20
11161 g1:
11162 x:
11163 bal g1
11164 */
11165 *valP -= S_GET_VALUE (fixP->fx_addsy);
11166 }
11167
11168 /* This code was generated using trial and error and so is
11169 fragile and not trustworthy. If you change it, you should
11170 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11171 they still pass. */
11172 if (fixP->fx_pcrel)
11173 {
11174 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11175
11176 /* BFD's REL handling, for MIPS, is _very_ weird.
11177 This gives the right results, but it can't possibly
11178 be the way things are supposed to work. */
11179 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11180 }
11181 }
11182 #endif
11183
11184 /* We are not done if this is a composite relocation to set up gp. */
11185 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11186 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11187 || (fixP->fx_r_type == BFD_RELOC_64
11188 && (previous_fx_r_type == BFD_RELOC_GPREL32
11189 || previous_fx_r_type == BFD_RELOC_GPREL16))
11190 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11191 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11192 || fixP->fx_r_type == BFD_RELOC_LO16))))
11193 fixP->fx_done = 1;
11194 previous_fx_r_type = fixP->fx_r_type;
11195
11196 switch (fixP->fx_r_type)
11197 {
11198 case BFD_RELOC_MIPS_JMP:
11199 case BFD_RELOC_MIPS_SHIFT5:
11200 case BFD_RELOC_MIPS_SHIFT6:
11201 case BFD_RELOC_MIPS_GOT_DISP:
11202 case BFD_RELOC_MIPS_GOT_PAGE:
11203 case BFD_RELOC_MIPS_GOT_OFST:
11204 case BFD_RELOC_MIPS_SUB:
11205 case BFD_RELOC_MIPS_INSERT_A:
11206 case BFD_RELOC_MIPS_INSERT_B:
11207 case BFD_RELOC_MIPS_DELETE:
11208 case BFD_RELOC_MIPS_HIGHEST:
11209 case BFD_RELOC_MIPS_HIGHER:
11210 case BFD_RELOC_MIPS_SCN_DISP:
11211 case BFD_RELOC_MIPS_REL16:
11212 case BFD_RELOC_MIPS_RELGOT:
11213 case BFD_RELOC_MIPS_JALR:
11214 case BFD_RELOC_HI16:
11215 case BFD_RELOC_HI16_S:
11216 case BFD_RELOC_GPREL16:
11217 case BFD_RELOC_MIPS_LITERAL:
11218 case BFD_RELOC_MIPS_CALL16:
11219 case BFD_RELOC_MIPS_GOT16:
11220 case BFD_RELOC_GPREL32:
11221 case BFD_RELOC_MIPS_GOT_HI16:
11222 case BFD_RELOC_MIPS_GOT_LO16:
11223 case BFD_RELOC_MIPS_CALL_HI16:
11224 case BFD_RELOC_MIPS_CALL_LO16:
11225 case BFD_RELOC_MIPS16_GPREL:
11226 if (fixP->fx_pcrel)
11227 as_bad_where (fixP->fx_file, fixP->fx_line,
11228 _("Invalid PC relative reloc"));
11229 /* Nothing needed to do. The value comes from the reloc entry */
11230 break;
11231
11232 case BFD_RELOC_MIPS16_JMP:
11233 /* We currently always generate a reloc against a symbol, which
11234 means that we don't want an addend even if the symbol is
11235 defined. */
11236 *valP = 0;
11237 break;
11238
11239 case BFD_RELOC_PCREL_HI16_S:
11240 /* The addend for this is tricky if it is internal, so we just
11241 do everything here rather than in bfd_install_relocation. */
11242 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11243 break;
11244 if (fixP->fx_addsy
11245 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11246 {
11247 /* For an external symbol adjust by the address to make it
11248 pcrel_offset. We use the address of the RELLO reloc
11249 which follows this one. */
11250 *valP += (fixP->fx_next->fx_frag->fr_address
11251 + fixP->fx_next->fx_where);
11252 }
11253 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11254 if (target_big_endian)
11255 buf += 2;
11256 md_number_to_chars (buf, *valP, 2);
11257 break;
11258
11259 case BFD_RELOC_PCREL_LO16:
11260 /* The addend for this is tricky if it is internal, so we just
11261 do everything here rather than in bfd_install_relocation. */
11262 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11263 break;
11264 if (fixP->fx_addsy
11265 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11266 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11267 if (target_big_endian)
11268 buf += 2;
11269 md_number_to_chars (buf, *valP, 2);
11270 break;
11271
11272 case BFD_RELOC_64:
11273 /* This is handled like BFD_RELOC_32, but we output a sign
11274 extended value if we are only 32 bits. */
11275 if (fixP->fx_done
11276 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11277 {
11278 if (8 <= sizeof (valueT))
11279 md_number_to_chars (buf, *valP, 8);
11280 else
11281 {
11282 valueT hiv;
11283
11284 if ((*valP & 0x80000000) != 0)
11285 hiv = 0xffffffff;
11286 else
11287 hiv = 0;
11288 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11289 *valP, 4);
11290 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11291 hiv, 4);
11292 }
11293 }
11294 break;
11295
11296 case BFD_RELOC_RVA:
11297 case BFD_RELOC_32:
11298 /* If we are deleting this reloc entry, we must fill in the
11299 value now. This can happen if we have a .word which is not
11300 resolved when it appears but is later defined. We also need
11301 to fill in the value if this is an embedded PIC switch table
11302 entry. */
11303 if (fixP->fx_done
11304 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11305 md_number_to_chars (buf, *valP, 4);
11306 break;
11307
11308 case BFD_RELOC_16:
11309 /* If we are deleting this reloc entry, we must fill in the
11310 value now. */
11311 assert (fixP->fx_size == 2);
11312 if (fixP->fx_done)
11313 md_number_to_chars (buf, *valP, 2);
11314 break;
11315
11316 case BFD_RELOC_LO16:
11317 /* When handling an embedded PIC switch statement, we can wind
11318 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11319 if (fixP->fx_done)
11320 {
11321 if (*valP + 0x8000 > 0xffff)
11322 as_bad_where (fixP->fx_file, fixP->fx_line,
11323 _("relocation overflow"));
11324 if (target_big_endian)
11325 buf += 2;
11326 md_number_to_chars (buf, *valP, 2);
11327 }
11328 break;
11329
11330 case BFD_RELOC_16_PCREL_S2:
11331 if ((*valP & 0x3) != 0)
11332 as_bad_where (fixP->fx_file, fixP->fx_line,
11333 _("Branch to odd address (%lx)"), (long) *valP);
11334
11335 /*
11336 * We need to save the bits in the instruction since fixup_segment()
11337 * might be deleting the relocation entry (i.e., a branch within
11338 * the current segment).
11339 */
11340 if (! fixP->fx_done)
11341 break;
11342
11343 /* update old instruction data */
11344 if (target_big_endian)
11345 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11346 else
11347 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11348
11349 if (*valP + 0x20000 <= 0x3ffff)
11350 {
11351 insn |= (*valP >> 2) & 0xffff;
11352 md_number_to_chars (buf, insn, 4);
11353 }
11354 else if (mips_pic == NO_PIC
11355 && fixP->fx_done
11356 && fixP->fx_frag->fr_address >= text_section->vma
11357 && (fixP->fx_frag->fr_address
11358 < text_section->vma + text_section->_raw_size)
11359 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11360 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11361 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11362 {
11363 /* The branch offset is too large. If this is an
11364 unconditional branch, and we are not generating PIC code,
11365 we can convert it to an absolute jump instruction. */
11366 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11367 insn = 0x0c000000; /* jal */
11368 else
11369 insn = 0x08000000; /* j */
11370 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11371 fixP->fx_done = 0;
11372 fixP->fx_addsy = section_symbol (text_section);
11373 *valP += md_pcrel_from (fixP);
11374 md_number_to_chars (buf, insn, 4);
11375 }
11376 else
11377 {
11378 /* If we got here, we have branch-relaxation disabled,
11379 and there's nothing we can do to fix this instruction
11380 without turning it into a longer sequence. */
11381 as_bad_where (fixP->fx_file, fixP->fx_line,
11382 _("Branch out of range"));
11383 }
11384 break;
11385
11386 case BFD_RELOC_VTABLE_INHERIT:
11387 fixP->fx_done = 0;
11388 if (fixP->fx_addsy
11389 && !S_IS_DEFINED (fixP->fx_addsy)
11390 && !S_IS_WEAK (fixP->fx_addsy))
11391 S_SET_WEAK (fixP->fx_addsy);
11392 break;
11393
11394 case BFD_RELOC_VTABLE_ENTRY:
11395 fixP->fx_done = 0;
11396 break;
11397
11398 default:
11399 internalError ();
11400 }
11401
11402 /* Remember value for tc_gen_reloc. */
11403 fixP->fx_addnumber = *valP;
11404 }
11405
11406 #if 0
11407 void
11408 printInsn (unsigned long oc)
11409 {
11410 const struct mips_opcode *p;
11411 int treg, sreg, dreg, shamt;
11412 short imm;
11413 const char *args;
11414 int i;
11415
11416 for (i = 0; i < NUMOPCODES; ++i)
11417 {
11418 p = &mips_opcodes[i];
11419 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11420 {
11421 printf ("%08lx %s\t", oc, p->name);
11422 treg = (oc >> 16) & 0x1f;
11423 sreg = (oc >> 21) & 0x1f;
11424 dreg = (oc >> 11) & 0x1f;
11425 shamt = (oc >> 6) & 0x1f;
11426 imm = oc;
11427 for (args = p->args;; ++args)
11428 {
11429 switch (*args)
11430 {
11431 case '\0':
11432 printf ("\n");
11433 break;
11434
11435 case ',':
11436 case '(':
11437 case ')':
11438 printf ("%c", *args);
11439 continue;
11440
11441 case 'r':
11442 assert (treg == sreg);
11443 printf ("$%d,$%d", treg, sreg);
11444 continue;
11445
11446 case 'd':
11447 case 'G':
11448 printf ("$%d", dreg);
11449 continue;
11450
11451 case 't':
11452 case 'E':
11453 printf ("$%d", treg);
11454 continue;
11455
11456 case 'k':
11457 printf ("0x%x", treg);
11458 continue;
11459
11460 case 'b':
11461 case 's':
11462 printf ("$%d", sreg);
11463 continue;
11464
11465 case 'a':
11466 printf ("0x%08lx", oc & 0x1ffffff);
11467 continue;
11468
11469 case 'i':
11470 case 'j':
11471 case 'o':
11472 case 'u':
11473 printf ("%d", imm);
11474 continue;
11475
11476 case '<':
11477 case '>':
11478 printf ("$%d", shamt);
11479 continue;
11480
11481 default:
11482 internalError ();
11483 }
11484 break;
11485 }
11486 return;
11487 }
11488 }
11489 printf (_("%08lx UNDEFINED\n"), oc);
11490 }
11491 #endif
11492
11493 static symbolS *
11494 get_symbol (void)
11495 {
11496 int c;
11497 char *name;
11498 symbolS *p;
11499
11500 name = input_line_pointer;
11501 c = get_symbol_end ();
11502 p = (symbolS *) symbol_find_or_make (name);
11503 *input_line_pointer = c;
11504 return p;
11505 }
11506
11507 /* Align the current frag to a given power of two. The MIPS assembler
11508 also automatically adjusts any preceding label. */
11509
11510 static void
11511 mips_align (int to, int fill, symbolS *label)
11512 {
11513 mips_emit_delays (FALSE);
11514 frag_align (to, fill, 0);
11515 record_alignment (now_seg, to);
11516 if (label != NULL)
11517 {
11518 assert (S_GET_SEGMENT (label) == now_seg);
11519 symbol_set_frag (label, frag_now);
11520 S_SET_VALUE (label, (valueT) frag_now_fix ());
11521 }
11522 }
11523
11524 /* Align to a given power of two. .align 0 turns off the automatic
11525 alignment used by the data creating pseudo-ops. */
11526
11527 static void
11528 s_align (int x ATTRIBUTE_UNUSED)
11529 {
11530 register int temp;
11531 register long temp_fill;
11532 long max_alignment = 15;
11533
11534 /*
11535
11536 o Note that the assembler pulls down any immediately preceeding label
11537 to the aligned address.
11538 o It's not documented but auto alignment is reinstated by
11539 a .align pseudo instruction.
11540 o Note also that after auto alignment is turned off the mips assembler
11541 issues an error on attempt to assemble an improperly aligned data item.
11542 We don't.
11543
11544 */
11545
11546 temp = get_absolute_expression ();
11547 if (temp > max_alignment)
11548 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11549 else if (temp < 0)
11550 {
11551 as_warn (_("Alignment negative: 0 assumed."));
11552 temp = 0;
11553 }
11554 if (*input_line_pointer == ',')
11555 {
11556 ++input_line_pointer;
11557 temp_fill = get_absolute_expression ();
11558 }
11559 else
11560 temp_fill = 0;
11561 if (temp)
11562 {
11563 auto_align = 1;
11564 mips_align (temp, (int) temp_fill,
11565 insn_labels != NULL ? insn_labels->label : NULL);
11566 }
11567 else
11568 {
11569 auto_align = 0;
11570 }
11571
11572 demand_empty_rest_of_line ();
11573 }
11574
11575 void
11576 mips_flush_pending_output (void)
11577 {
11578 mips_emit_delays (FALSE);
11579 mips_clear_insn_labels ();
11580 }
11581
11582 static void
11583 s_change_sec (int sec)
11584 {
11585 segT seg;
11586
11587 /* When generating embedded PIC code, we only use the .text, .lit8,
11588 .sdata and .sbss sections. We change the .data and .rdata
11589 pseudo-ops to use .sdata. */
11590 if (mips_pic == EMBEDDED_PIC
11591 && (sec == 'd' || sec == 'r'))
11592 sec = 's';
11593
11594 #ifdef OBJ_ELF
11595 /* The ELF backend needs to know that we are changing sections, so
11596 that .previous works correctly. We could do something like check
11597 for an obj_section_change_hook macro, but that might be confusing
11598 as it would not be appropriate to use it in the section changing
11599 functions in read.c, since obj-elf.c intercepts those. FIXME:
11600 This should be cleaner, somehow. */
11601 obj_elf_section_change_hook ();
11602 #endif
11603
11604 mips_emit_delays (FALSE);
11605 switch (sec)
11606 {
11607 case 't':
11608 s_text (0);
11609 break;
11610 case 'd':
11611 s_data (0);
11612 break;
11613 case 'b':
11614 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11615 demand_empty_rest_of_line ();
11616 break;
11617
11618 case 'r':
11619 if (USE_GLOBAL_POINTER_OPT)
11620 {
11621 seg = subseg_new (RDATA_SECTION_NAME,
11622 (subsegT) get_absolute_expression ());
11623 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11624 {
11625 bfd_set_section_flags (stdoutput, seg,
11626 (SEC_ALLOC
11627 | SEC_LOAD
11628 | SEC_READONLY
11629 | SEC_RELOC
11630 | SEC_DATA));
11631 if (strcmp (TARGET_OS, "elf") != 0)
11632 record_alignment (seg, 4);
11633 }
11634 demand_empty_rest_of_line ();
11635 }
11636 else
11637 {
11638 as_bad (_("No read only data section in this object file format"));
11639 demand_empty_rest_of_line ();
11640 return;
11641 }
11642 break;
11643
11644 case 's':
11645 if (USE_GLOBAL_POINTER_OPT)
11646 {
11647 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11648 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11649 {
11650 bfd_set_section_flags (stdoutput, seg,
11651 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11652 | SEC_DATA);
11653 if (strcmp (TARGET_OS, "elf") != 0)
11654 record_alignment (seg, 4);
11655 }
11656 demand_empty_rest_of_line ();
11657 break;
11658 }
11659 else
11660 {
11661 as_bad (_("Global pointers not supported; recompile -G 0"));
11662 demand_empty_rest_of_line ();
11663 return;
11664 }
11665 }
11666
11667 auto_align = 1;
11668 }
11669
11670 void
11671 s_change_section (int ignore ATTRIBUTE_UNUSED)
11672 {
11673 #ifdef OBJ_ELF
11674 char *section_name;
11675 char c;
11676 char next_c = 0;
11677 int section_type;
11678 int section_flag;
11679 int section_entry_size;
11680 int section_alignment;
11681
11682 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11683 return;
11684
11685 section_name = input_line_pointer;
11686 c = get_symbol_end ();
11687 if (c)
11688 next_c = *(input_line_pointer + 1);
11689
11690 /* Do we have .section Name<,"flags">? */
11691 if (c != ',' || (c == ',' && next_c == '"'))
11692 {
11693 /* just after name is now '\0'. */
11694 *input_line_pointer = c;
11695 input_line_pointer = section_name;
11696 obj_elf_section (ignore);
11697 return;
11698 }
11699 input_line_pointer++;
11700
11701 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11702 if (c == ',')
11703 section_type = get_absolute_expression ();
11704 else
11705 section_type = 0;
11706 if (*input_line_pointer++ == ',')
11707 section_flag = get_absolute_expression ();
11708 else
11709 section_flag = 0;
11710 if (*input_line_pointer++ == ',')
11711 section_entry_size = get_absolute_expression ();
11712 else
11713 section_entry_size = 0;
11714 if (*input_line_pointer++ == ',')
11715 section_alignment = get_absolute_expression ();
11716 else
11717 section_alignment = 0;
11718
11719 section_name = xstrdup (section_name);
11720
11721 obj_elf_change_section (section_name, section_type, section_flag,
11722 section_entry_size, 0, 0, 0);
11723
11724 if (now_seg->name != section_name)
11725 free (section_name);
11726 #endif /* OBJ_ELF */
11727 }
11728
11729 void
11730 mips_enable_auto_align (void)
11731 {
11732 auto_align = 1;
11733 }
11734
11735 static void
11736 s_cons (int log_size)
11737 {
11738 symbolS *label;
11739
11740 label = insn_labels != NULL ? insn_labels->label : NULL;
11741 mips_emit_delays (FALSE);
11742 if (log_size > 0 && auto_align)
11743 mips_align (log_size, 0, label);
11744 mips_clear_insn_labels ();
11745 cons (1 << log_size);
11746 }
11747
11748 static void
11749 s_float_cons (int type)
11750 {
11751 symbolS *label;
11752
11753 label = insn_labels != NULL ? insn_labels->label : NULL;
11754
11755 mips_emit_delays (FALSE);
11756
11757 if (auto_align)
11758 {
11759 if (type == 'd')
11760 mips_align (3, 0, label);
11761 else
11762 mips_align (2, 0, label);
11763 }
11764
11765 mips_clear_insn_labels ();
11766
11767 float_cons (type);
11768 }
11769
11770 /* Handle .globl. We need to override it because on Irix 5 you are
11771 permitted to say
11772 .globl foo .text
11773 where foo is an undefined symbol, to mean that foo should be
11774 considered to be the address of a function. */
11775
11776 static void
11777 s_mips_globl (int x ATTRIBUTE_UNUSED)
11778 {
11779 char *name;
11780 int c;
11781 symbolS *symbolP;
11782 flagword flag;
11783
11784 name = input_line_pointer;
11785 c = get_symbol_end ();
11786 symbolP = symbol_find_or_make (name);
11787 *input_line_pointer = c;
11788 SKIP_WHITESPACE ();
11789
11790 /* On Irix 5, every global symbol that is not explicitly labelled as
11791 being a function is apparently labelled as being an object. */
11792 flag = BSF_OBJECT;
11793
11794 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11795 {
11796 char *secname;
11797 asection *sec;
11798
11799 secname = input_line_pointer;
11800 c = get_symbol_end ();
11801 sec = bfd_get_section_by_name (stdoutput, secname);
11802 if (sec == NULL)
11803 as_bad (_("%s: no such section"), secname);
11804 *input_line_pointer = c;
11805
11806 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11807 flag = BSF_FUNCTION;
11808 }
11809
11810 symbol_get_bfdsym (symbolP)->flags |= flag;
11811
11812 S_SET_EXTERNAL (symbolP);
11813 demand_empty_rest_of_line ();
11814 }
11815
11816 static void
11817 s_option (int x ATTRIBUTE_UNUSED)
11818 {
11819 char *opt;
11820 char c;
11821
11822 opt = input_line_pointer;
11823 c = get_symbol_end ();
11824
11825 if (*opt == 'O')
11826 {
11827 /* FIXME: What does this mean? */
11828 }
11829 else if (strncmp (opt, "pic", 3) == 0)
11830 {
11831 int i;
11832
11833 i = atoi (opt + 3);
11834 if (i == 0)
11835 mips_pic = NO_PIC;
11836 else if (i == 2)
11837 {
11838 mips_pic = SVR4_PIC;
11839 mips_abicalls = TRUE;
11840 }
11841 else
11842 as_bad (_(".option pic%d not supported"), i);
11843
11844 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11845 {
11846 if (g_switch_seen && g_switch_value != 0)
11847 as_warn (_("-G may not be used with SVR4 PIC code"));
11848 g_switch_value = 0;
11849 bfd_set_gp_size (stdoutput, 0);
11850 }
11851 }
11852 else
11853 as_warn (_("Unrecognized option \"%s\""), opt);
11854
11855 *input_line_pointer = c;
11856 demand_empty_rest_of_line ();
11857 }
11858
11859 /* This structure is used to hold a stack of .set values. */
11860
11861 struct mips_option_stack
11862 {
11863 struct mips_option_stack *next;
11864 struct mips_set_options options;
11865 };
11866
11867 static struct mips_option_stack *mips_opts_stack;
11868
11869 /* Handle the .set pseudo-op. */
11870
11871 static void
11872 s_mipsset (int x ATTRIBUTE_UNUSED)
11873 {
11874 char *name = input_line_pointer, ch;
11875
11876 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11877 ++input_line_pointer;
11878 ch = *input_line_pointer;
11879 *input_line_pointer = '\0';
11880
11881 if (strcmp (name, "reorder") == 0)
11882 {
11883 if (mips_opts.noreorder && prev_nop_frag != NULL)
11884 {
11885 /* If we still have pending nops, we can discard them. The
11886 usual nop handling will insert any that are still
11887 needed. */
11888 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11889 * (mips_opts.mips16 ? 2 : 4));
11890 prev_nop_frag = NULL;
11891 }
11892 mips_opts.noreorder = 0;
11893 }
11894 else if (strcmp (name, "noreorder") == 0)
11895 {
11896 mips_emit_delays (TRUE);
11897 mips_opts.noreorder = 1;
11898 mips_any_noreorder = 1;
11899 }
11900 else if (strcmp (name, "at") == 0)
11901 {
11902 mips_opts.noat = 0;
11903 }
11904 else if (strcmp (name, "noat") == 0)
11905 {
11906 mips_opts.noat = 1;
11907 }
11908 else if (strcmp (name, "macro") == 0)
11909 {
11910 mips_opts.warn_about_macros = 0;
11911 }
11912 else if (strcmp (name, "nomacro") == 0)
11913 {
11914 if (mips_opts.noreorder == 0)
11915 as_bad (_("`noreorder' must be set before `nomacro'"));
11916 mips_opts.warn_about_macros = 1;
11917 }
11918 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11919 {
11920 mips_opts.nomove = 0;
11921 }
11922 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11923 {
11924 mips_opts.nomove = 1;
11925 }
11926 else if (strcmp (name, "bopt") == 0)
11927 {
11928 mips_opts.nobopt = 0;
11929 }
11930 else if (strcmp (name, "nobopt") == 0)
11931 {
11932 mips_opts.nobopt = 1;
11933 }
11934 else if (strcmp (name, "mips16") == 0
11935 || strcmp (name, "MIPS-16") == 0)
11936 mips_opts.mips16 = 1;
11937 else if (strcmp (name, "nomips16") == 0
11938 || strcmp (name, "noMIPS-16") == 0)
11939 mips_opts.mips16 = 0;
11940 else if (strcmp (name, "mips3d") == 0)
11941 mips_opts.ase_mips3d = 1;
11942 else if (strcmp (name, "nomips3d") == 0)
11943 mips_opts.ase_mips3d = 0;
11944 else if (strcmp (name, "mdmx") == 0)
11945 mips_opts.ase_mdmx = 1;
11946 else if (strcmp (name, "nomdmx") == 0)
11947 mips_opts.ase_mdmx = 0;
11948 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11949 {
11950 int reset = 0;
11951
11952 /* Permit the user to change the ISA and architecture on the fly.
11953 Needless to say, misuse can cause serious problems. */
11954 if (strcmp (name, "mips0") == 0)
11955 {
11956 reset = 1;
11957 mips_opts.isa = file_mips_isa;
11958 }
11959 else if (strcmp (name, "mips1") == 0)
11960 mips_opts.isa = ISA_MIPS1;
11961 else if (strcmp (name, "mips2") == 0)
11962 mips_opts.isa = ISA_MIPS2;
11963 else if (strcmp (name, "mips3") == 0)
11964 mips_opts.isa = ISA_MIPS3;
11965 else if (strcmp (name, "mips4") == 0)
11966 mips_opts.isa = ISA_MIPS4;
11967 else if (strcmp (name, "mips5") == 0)
11968 mips_opts.isa = ISA_MIPS5;
11969 else if (strcmp (name, "mips32") == 0)
11970 mips_opts.isa = ISA_MIPS32;
11971 else if (strcmp (name, "mips32r2") == 0)
11972 mips_opts.isa = ISA_MIPS32R2;
11973 else if (strcmp (name, "mips64") == 0)
11974 mips_opts.isa = ISA_MIPS64;
11975 else if (strcmp (name, "arch=default") == 0)
11976 {
11977 reset = 1;
11978 mips_opts.arch = file_mips_arch;
11979 mips_opts.isa = file_mips_isa;
11980 }
11981 else if (strncmp (name, "arch=", 5) == 0)
11982 {
11983 const struct mips_cpu_info *p;
11984
11985 p = mips_parse_cpu("internal use", name + 5);
11986 if (!p)
11987 as_bad (_("unknown architecture %s"), name + 5);
11988 else
11989 {
11990 mips_opts.arch = p->cpu;
11991 mips_opts.isa = p->isa;
11992 }
11993 }
11994 else
11995 as_bad (_("unknown ISA level %s"), name + 4);
11996
11997 switch (mips_opts.isa)
11998 {
11999 case 0:
12000 break;
12001 case ISA_MIPS1:
12002 case ISA_MIPS2:
12003 case ISA_MIPS32:
12004 case ISA_MIPS32R2:
12005 mips_opts.gp32 = 1;
12006 mips_opts.fp32 = 1;
12007 break;
12008 case ISA_MIPS3:
12009 case ISA_MIPS4:
12010 case ISA_MIPS5:
12011 case ISA_MIPS64:
12012 mips_opts.gp32 = 0;
12013 mips_opts.fp32 = 0;
12014 break;
12015 default:
12016 as_bad (_("unknown ISA level %s"), name + 4);
12017 break;
12018 }
12019 if (reset)
12020 {
12021 mips_opts.gp32 = file_mips_gp32;
12022 mips_opts.fp32 = file_mips_fp32;
12023 }
12024 }
12025 else if (strcmp (name, "autoextend") == 0)
12026 mips_opts.noautoextend = 0;
12027 else if (strcmp (name, "noautoextend") == 0)
12028 mips_opts.noautoextend = 1;
12029 else if (strcmp (name, "push") == 0)
12030 {
12031 struct mips_option_stack *s;
12032
12033 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12034 s->next = mips_opts_stack;
12035 s->options = mips_opts;
12036 mips_opts_stack = s;
12037 }
12038 else if (strcmp (name, "pop") == 0)
12039 {
12040 struct mips_option_stack *s;
12041
12042 s = mips_opts_stack;
12043 if (s == NULL)
12044 as_bad (_(".set pop with no .set push"));
12045 else
12046 {
12047 /* If we're changing the reorder mode we need to handle
12048 delay slots correctly. */
12049 if (s->options.noreorder && ! mips_opts.noreorder)
12050 mips_emit_delays (TRUE);
12051 else if (! s->options.noreorder && mips_opts.noreorder)
12052 {
12053 if (prev_nop_frag != NULL)
12054 {
12055 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12056 * (mips_opts.mips16 ? 2 : 4));
12057 prev_nop_frag = NULL;
12058 }
12059 }
12060
12061 mips_opts = s->options;
12062 mips_opts_stack = s->next;
12063 free (s);
12064 }
12065 }
12066 else
12067 {
12068 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12069 }
12070 *input_line_pointer = ch;
12071 demand_empty_rest_of_line ();
12072 }
12073
12074 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12075 .option pic2. It means to generate SVR4 PIC calls. */
12076
12077 static void
12078 s_abicalls (int ignore ATTRIBUTE_UNUSED)
12079 {
12080 mips_pic = SVR4_PIC;
12081 mips_abicalls = TRUE;
12082 if (USE_GLOBAL_POINTER_OPT)
12083 {
12084 if (g_switch_seen && g_switch_value != 0)
12085 as_warn (_("-G may not be used with SVR4 PIC code"));
12086 g_switch_value = 0;
12087 }
12088 bfd_set_gp_size (stdoutput, 0);
12089 demand_empty_rest_of_line ();
12090 }
12091
12092 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12093 PIC code. It sets the $gp register for the function based on the
12094 function address, which is in the register named in the argument.
12095 This uses a relocation against _gp_disp, which is handled specially
12096 by the linker. The result is:
12097 lui $gp,%hi(_gp_disp)
12098 addiu $gp,$gp,%lo(_gp_disp)
12099 addu $gp,$gp,.cpload argument
12100 The .cpload argument is normally $25 == $t9. */
12101
12102 static void
12103 s_cpload (int ignore ATTRIBUTE_UNUSED)
12104 {
12105 expressionS ex;
12106 int icnt = 0;
12107
12108 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12109 .cpload is ignored. */
12110 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12111 {
12112 s_ignore (0);
12113 return;
12114 }
12115
12116 /* .cpload should be in a .set noreorder section. */
12117 if (mips_opts.noreorder == 0)
12118 as_warn (_(".cpload not in noreorder section"));
12119
12120 ex.X_op = O_symbol;
12121 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12122 ex.X_op_symbol = NULL;
12123 ex.X_add_number = 0;
12124
12125 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12126 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12127
12128 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12129 macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12130 mips_gp_register, BFD_RELOC_LO16);
12131
12132 macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12133 mips_gp_register, tc_get_register (0));
12134
12135 demand_empty_rest_of_line ();
12136 }
12137
12138 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12139 .cpsetup $reg1, offset|$reg2, label
12140
12141 If offset is given, this results in:
12142 sd $gp, offset($sp)
12143 lui $gp, %hi(%neg(%gp_rel(label)))
12144 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12145 daddu $gp, $gp, $reg1
12146
12147 If $reg2 is given, this results in:
12148 daddu $reg2, $gp, $0
12149 lui $gp, %hi(%neg(%gp_rel(label)))
12150 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12151 daddu $gp, $gp, $reg1
12152 $reg1 is normally $25 == $t9. */
12153 static void
12154 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12155 {
12156 expressionS ex_off;
12157 expressionS ex_sym;
12158 int reg1;
12159 int icnt = 0;
12160 char *f;
12161
12162 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12163 We also need NewABI support. */
12164 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12165 {
12166 s_ignore (0);
12167 return;
12168 }
12169
12170 reg1 = tc_get_register (0);
12171 SKIP_WHITESPACE ();
12172 if (*input_line_pointer != ',')
12173 {
12174 as_bad (_("missing argument separator ',' for .cpsetup"));
12175 return;
12176 }
12177 else
12178 ++input_line_pointer;
12179 SKIP_WHITESPACE ();
12180 if (*input_line_pointer == '$')
12181 {
12182 mips_cpreturn_register = tc_get_register (0);
12183 mips_cpreturn_offset = -1;
12184 }
12185 else
12186 {
12187 mips_cpreturn_offset = get_absolute_expression ();
12188 mips_cpreturn_register = -1;
12189 }
12190 SKIP_WHITESPACE ();
12191 if (*input_line_pointer != ',')
12192 {
12193 as_bad (_("missing argument separator ',' for .cpsetup"));
12194 return;
12195 }
12196 else
12197 ++input_line_pointer;
12198 SKIP_WHITESPACE ();
12199 expression (&ex_sym);
12200
12201 if (mips_cpreturn_register == -1)
12202 {
12203 ex_off.X_op = O_constant;
12204 ex_off.X_add_symbol = NULL;
12205 ex_off.X_op_symbol = NULL;
12206 ex_off.X_add_number = mips_cpreturn_offset;
12207
12208 macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12209 BFD_RELOC_LO16, SP);
12210 }
12211 else
12212 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12213 mips_gp_register, 0);
12214
12215 /* Ensure there's room for the next two instructions, so that `f'
12216 doesn't end up with an address in the wrong frag. */
12217 frag_grow (8);
12218 f = frag_more (0);
12219 macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12220 BFD_RELOC_GPREL16);
12221 fix_new (frag_now, f - frag_now->fr_literal,
12222 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12223 fix_new (frag_now, f - frag_now->fr_literal,
12224 4, NULL, 0, 0, BFD_RELOC_HI16_S);
12225
12226 f = frag_more (0);
12227 macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12228 mips_gp_register, BFD_RELOC_GPREL16);
12229 fix_new (frag_now, f - frag_now->fr_literal,
12230 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12231 fix_new (frag_now, f - frag_now->fr_literal,
12232 4, NULL, 0, 0, BFD_RELOC_LO16);
12233
12234 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12235 mips_gp_register, reg1);
12236
12237 demand_empty_rest_of_line ();
12238 }
12239
12240 static void
12241 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12242 {
12243 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12244 .cplocal is ignored. */
12245 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12246 {
12247 s_ignore (0);
12248 return;
12249 }
12250
12251 mips_gp_register = tc_get_register (0);
12252 demand_empty_rest_of_line ();
12253 }
12254
12255 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12256 offset from $sp. The offset is remembered, and after making a PIC
12257 call $gp is restored from that location. */
12258
12259 static void
12260 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12261 {
12262 expressionS ex;
12263 int icnt = 0;
12264
12265 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12266 .cprestore is ignored. */
12267 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12268 {
12269 s_ignore (0);
12270 return;
12271 }
12272
12273 mips_cprestore_offset = get_absolute_expression ();
12274 mips_cprestore_valid = 1;
12275
12276 ex.X_op = O_constant;
12277 ex.X_add_symbol = NULL;
12278 ex.X_op_symbol = NULL;
12279 ex.X_add_number = mips_cprestore_offset;
12280
12281 macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
12282 mips_gp_register, SP);
12283
12284 demand_empty_rest_of_line ();
12285 }
12286
12287 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12288 was given in the preceeding .gpsetup, it results in:
12289 ld $gp, offset($sp)
12290
12291 If a register $reg2 was given there, it results in:
12292 daddiu $gp, $gp, $reg2
12293 */
12294 static void
12295 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12296 {
12297 expressionS ex;
12298 int icnt = 0;
12299
12300 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12301 We also need NewABI support. */
12302 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12303 {
12304 s_ignore (0);
12305 return;
12306 }
12307
12308 if (mips_cpreturn_register == -1)
12309 {
12310 ex.X_op = O_constant;
12311 ex.X_add_symbol = NULL;
12312 ex.X_op_symbol = NULL;
12313 ex.X_add_number = mips_cpreturn_offset;
12314
12315 macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12316 BFD_RELOC_LO16, SP);
12317 }
12318 else
12319 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12320 mips_cpreturn_register, 0);
12321
12322 demand_empty_rest_of_line ();
12323 }
12324
12325 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12326 code. It sets the offset to use in gp_rel relocations. */
12327
12328 static void
12329 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12330 {
12331 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12332 We also need NewABI support. */
12333 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12334 {
12335 s_ignore (0);
12336 return;
12337 }
12338
12339 mips_gprel_offset = get_absolute_expression ();
12340
12341 demand_empty_rest_of_line ();
12342 }
12343
12344 /* Handle the .gpword pseudo-op. This is used when generating PIC
12345 code. It generates a 32 bit GP relative reloc. */
12346
12347 static void
12348 s_gpword (int ignore ATTRIBUTE_UNUSED)
12349 {
12350 symbolS *label;
12351 expressionS ex;
12352 char *p;
12353
12354 /* When not generating PIC code, this is treated as .word. */
12355 if (mips_pic != SVR4_PIC)
12356 {
12357 s_cons (2);
12358 return;
12359 }
12360
12361 label = insn_labels != NULL ? insn_labels->label : NULL;
12362 mips_emit_delays (TRUE);
12363 if (auto_align)
12364 mips_align (2, 0, label);
12365 mips_clear_insn_labels ();
12366
12367 expression (&ex);
12368
12369 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12370 {
12371 as_bad (_("Unsupported use of .gpword"));
12372 ignore_rest_of_line ();
12373 }
12374
12375 p = frag_more (4);
12376 md_number_to_chars (p, 0, 4);
12377 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12378 BFD_RELOC_GPREL32);
12379
12380 demand_empty_rest_of_line ();
12381 }
12382
12383 static void
12384 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12385 {
12386 symbolS *label;
12387 expressionS ex;
12388 char *p;
12389
12390 /* When not generating PIC code, this is treated as .dword. */
12391 if (mips_pic != SVR4_PIC)
12392 {
12393 s_cons (3);
12394 return;
12395 }
12396
12397 label = insn_labels != NULL ? insn_labels->label : NULL;
12398 mips_emit_delays (TRUE);
12399 if (auto_align)
12400 mips_align (3, 0, label);
12401 mips_clear_insn_labels ();
12402
12403 expression (&ex);
12404
12405 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12406 {
12407 as_bad (_("Unsupported use of .gpdword"));
12408 ignore_rest_of_line ();
12409 }
12410
12411 p = frag_more (8);
12412 md_number_to_chars (p, 0, 8);
12413 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12414 BFD_RELOC_GPREL32);
12415
12416 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12417 ex.X_op = O_absent;
12418 ex.X_add_symbol = 0;
12419 ex.X_add_number = 0;
12420 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12421 BFD_RELOC_64);
12422
12423 demand_empty_rest_of_line ();
12424 }
12425
12426 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12427 tables in SVR4 PIC code. */
12428
12429 static void
12430 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12431 {
12432 int icnt = 0;
12433 int reg;
12434
12435 /* This is ignored when not generating SVR4 PIC code. */
12436 if (mips_pic != SVR4_PIC)
12437 {
12438 s_ignore (0);
12439 return;
12440 }
12441
12442 /* Add $gp to the register named as an argument. */
12443 reg = tc_get_register (0);
12444 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12445 reg, reg, mips_gp_register);
12446
12447 demand_empty_rest_of_line ();
12448 }
12449
12450 /* Handle the .insn pseudo-op. This marks instruction labels in
12451 mips16 mode. This permits the linker to handle them specially,
12452 such as generating jalx instructions when needed. We also make
12453 them odd for the duration of the assembly, in order to generate the
12454 right sort of code. We will make them even in the adjust_symtab
12455 routine, while leaving them marked. This is convenient for the
12456 debugger and the disassembler. The linker knows to make them odd
12457 again. */
12458
12459 static void
12460 s_insn (int ignore ATTRIBUTE_UNUSED)
12461 {
12462 mips16_mark_labels ();
12463
12464 demand_empty_rest_of_line ();
12465 }
12466
12467 /* Handle a .stabn directive. We need these in order to mark a label
12468 as being a mips16 text label correctly. Sometimes the compiler
12469 will emit a label, followed by a .stabn, and then switch sections.
12470 If the label and .stabn are in mips16 mode, then the label is
12471 really a mips16 text label. */
12472
12473 static void
12474 s_mips_stab (int type)
12475 {
12476 if (type == 'n')
12477 mips16_mark_labels ();
12478
12479 s_stab (type);
12480 }
12481
12482 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12483 */
12484
12485 static void
12486 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12487 {
12488 char *name;
12489 int c;
12490 symbolS *symbolP;
12491 expressionS exp;
12492
12493 name = input_line_pointer;
12494 c = get_symbol_end ();
12495 symbolP = symbol_find_or_make (name);
12496 S_SET_WEAK (symbolP);
12497 *input_line_pointer = c;
12498
12499 SKIP_WHITESPACE ();
12500
12501 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12502 {
12503 if (S_IS_DEFINED (symbolP))
12504 {
12505 as_bad ("ignoring attempt to redefine symbol %s",
12506 S_GET_NAME (symbolP));
12507 ignore_rest_of_line ();
12508 return;
12509 }
12510
12511 if (*input_line_pointer == ',')
12512 {
12513 ++input_line_pointer;
12514 SKIP_WHITESPACE ();
12515 }
12516
12517 expression (&exp);
12518 if (exp.X_op != O_symbol)
12519 {
12520 as_bad ("bad .weakext directive");
12521 ignore_rest_of_line ();
12522 return;
12523 }
12524 symbol_set_value_expression (symbolP, &exp);
12525 }
12526
12527 demand_empty_rest_of_line ();
12528 }
12529
12530 /* Parse a register string into a number. Called from the ECOFF code
12531 to parse .frame. The argument is non-zero if this is the frame
12532 register, so that we can record it in mips_frame_reg. */
12533
12534 int
12535 tc_get_register (int frame)
12536 {
12537 int reg;
12538
12539 SKIP_WHITESPACE ();
12540 if (*input_line_pointer++ != '$')
12541 {
12542 as_warn (_("expected `$'"));
12543 reg = ZERO;
12544 }
12545 else if (ISDIGIT (*input_line_pointer))
12546 {
12547 reg = get_absolute_expression ();
12548 if (reg < 0 || reg >= 32)
12549 {
12550 as_warn (_("Bad register number"));
12551 reg = ZERO;
12552 }
12553 }
12554 else
12555 {
12556 if (strncmp (input_line_pointer, "ra", 2) == 0)
12557 {
12558 reg = RA;
12559 input_line_pointer += 2;
12560 }
12561 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12562 {
12563 reg = FP;
12564 input_line_pointer += 2;
12565 }
12566 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12567 {
12568 reg = SP;
12569 input_line_pointer += 2;
12570 }
12571 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12572 {
12573 reg = GP;
12574 input_line_pointer += 2;
12575 }
12576 else if (strncmp (input_line_pointer, "at", 2) == 0)
12577 {
12578 reg = AT;
12579 input_line_pointer += 2;
12580 }
12581 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12582 {
12583 reg = KT0;
12584 input_line_pointer += 3;
12585 }
12586 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12587 {
12588 reg = KT1;
12589 input_line_pointer += 3;
12590 }
12591 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12592 {
12593 reg = ZERO;
12594 input_line_pointer += 4;
12595 }
12596 else
12597 {
12598 as_warn (_("Unrecognized register name"));
12599 reg = ZERO;
12600 while (ISALNUM(*input_line_pointer))
12601 input_line_pointer++;
12602 }
12603 }
12604 if (frame)
12605 {
12606 mips_frame_reg = reg != 0 ? reg : SP;
12607 mips_frame_reg_valid = 1;
12608 mips_cprestore_valid = 0;
12609 }
12610 return reg;
12611 }
12612
12613 valueT
12614 md_section_align (asection *seg, valueT addr)
12615 {
12616 int align = bfd_get_section_alignment (stdoutput, seg);
12617
12618 #ifdef OBJ_ELF
12619 /* We don't need to align ELF sections to the full alignment.
12620 However, Irix 5 may prefer that we align them at least to a 16
12621 byte boundary. We don't bother to align the sections if we are
12622 targeted for an embedded system. */
12623 if (strcmp (TARGET_OS, "elf") == 0)
12624 return addr;
12625 if (align > 4)
12626 align = 4;
12627 #endif
12628
12629 return ((addr + (1 << align) - 1) & (-1 << align));
12630 }
12631
12632 /* Utility routine, called from above as well. If called while the
12633 input file is still being read, it's only an approximation. (For
12634 example, a symbol may later become defined which appeared to be
12635 undefined earlier.) */
12636
12637 static int
12638 nopic_need_relax (symbolS *sym, int before_relaxing)
12639 {
12640 if (sym == 0)
12641 return 0;
12642
12643 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
12644 {
12645 const char *symname;
12646 int change;
12647
12648 /* Find out whether this symbol can be referenced off the $gp
12649 register. It can be if it is smaller than the -G size or if
12650 it is in the .sdata or .sbss section. Certain symbols can
12651 not be referenced off the $gp, although it appears as though
12652 they can. */
12653 symname = S_GET_NAME (sym);
12654 if (symname != (const char *) NULL
12655 && (strcmp (symname, "eprol") == 0
12656 || strcmp (symname, "etext") == 0
12657 || strcmp (symname, "_gp") == 0
12658 || strcmp (symname, "edata") == 0
12659 || strcmp (symname, "_fbss") == 0
12660 || strcmp (symname, "_fdata") == 0
12661 || strcmp (symname, "_ftext") == 0
12662 || strcmp (symname, "end") == 0
12663 || strcmp (symname, "_gp_disp") == 0))
12664 change = 1;
12665 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12666 && (0
12667 #ifndef NO_ECOFF_DEBUGGING
12668 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12669 && (symbol_get_obj (sym)->ecoff_extern_size
12670 <= g_switch_value))
12671 #endif
12672 /* We must defer this decision until after the whole
12673 file has been read, since there might be a .extern
12674 after the first use of this symbol. */
12675 || (before_relaxing
12676 #ifndef NO_ECOFF_DEBUGGING
12677 && symbol_get_obj (sym)->ecoff_extern_size == 0
12678 #endif
12679 && S_GET_VALUE (sym) == 0)
12680 || (S_GET_VALUE (sym) != 0
12681 && S_GET_VALUE (sym) <= g_switch_value)))
12682 change = 0;
12683 else
12684 {
12685 const char *segname;
12686
12687 segname = segment_name (S_GET_SEGMENT (sym));
12688 assert (strcmp (segname, ".lit8") != 0
12689 && strcmp (segname, ".lit4") != 0);
12690 change = (strcmp (segname, ".sdata") != 0
12691 && strcmp (segname, ".sbss") != 0
12692 && strncmp (segname, ".sdata.", 7) != 0
12693 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12694 }
12695 return change;
12696 }
12697 else
12698 /* We are not optimizing for the $gp register. */
12699 return 1;
12700 }
12701
12702
12703 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12704
12705 static bfd_boolean
12706 pic_need_relax (symbolS *sym, asection *segtype)
12707 {
12708 asection *symsec;
12709 bfd_boolean linkonce;
12710
12711 /* Handle the case of a symbol equated to another symbol. */
12712 while (symbol_equated_reloc_p (sym))
12713 {
12714 symbolS *n;
12715
12716 /* It's possible to get a loop here in a badly written
12717 program. */
12718 n = symbol_get_value_expression (sym)->X_add_symbol;
12719 if (n == sym)
12720 break;
12721 sym = n;
12722 }
12723
12724 symsec = S_GET_SEGMENT (sym);
12725
12726 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12727 linkonce = FALSE;
12728 if (symsec != segtype && ! S_IS_LOCAL (sym))
12729 {
12730 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12731 != 0)
12732 linkonce = TRUE;
12733
12734 /* The GNU toolchain uses an extension for ELF: a section
12735 beginning with the magic string .gnu.linkonce is a linkonce
12736 section. */
12737 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12738 sizeof ".gnu.linkonce" - 1) == 0)
12739 linkonce = TRUE;
12740 }
12741
12742 /* This must duplicate the test in adjust_reloc_syms. */
12743 return (symsec != &bfd_und_section
12744 && symsec != &bfd_abs_section
12745 && ! bfd_is_com_section (symsec)
12746 && !linkonce
12747 #ifdef OBJ_ELF
12748 /* A global or weak symbol is treated as external. */
12749 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12750 || (! S_IS_WEAK (sym)
12751 && (! S_IS_EXTERNAL (sym)
12752 || mips_pic == EMBEDDED_PIC)))
12753 #endif
12754 );
12755 }
12756
12757
12758 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12759 extended opcode. SEC is the section the frag is in. */
12760
12761 static int
12762 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12763 {
12764 int type;
12765 register const struct mips16_immed_operand *op;
12766 offsetT val;
12767 int mintiny, maxtiny;
12768 segT symsec;
12769 fragS *sym_frag;
12770
12771 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12772 return 0;
12773 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12774 return 1;
12775
12776 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12777 op = mips16_immed_operands;
12778 while (op->type != type)
12779 {
12780 ++op;
12781 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12782 }
12783
12784 if (op->unsp)
12785 {
12786 if (type == '<' || type == '>' || type == '[' || type == ']')
12787 {
12788 mintiny = 1;
12789 maxtiny = 1 << op->nbits;
12790 }
12791 else
12792 {
12793 mintiny = 0;
12794 maxtiny = (1 << op->nbits) - 1;
12795 }
12796 }
12797 else
12798 {
12799 mintiny = - (1 << (op->nbits - 1));
12800 maxtiny = (1 << (op->nbits - 1)) - 1;
12801 }
12802
12803 sym_frag = symbol_get_frag (fragp->fr_symbol);
12804 val = S_GET_VALUE (fragp->fr_symbol);
12805 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12806
12807 if (op->pcrel)
12808 {
12809 addressT addr;
12810
12811 /* We won't have the section when we are called from
12812 mips_relax_frag. However, we will always have been called
12813 from md_estimate_size_before_relax first. If this is a
12814 branch to a different section, we mark it as such. If SEC is
12815 NULL, and the frag is not marked, then it must be a branch to
12816 the same section. */
12817 if (sec == NULL)
12818 {
12819 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12820 return 1;
12821 }
12822 else
12823 {
12824 /* Must have been called from md_estimate_size_before_relax. */
12825 if (symsec != sec)
12826 {
12827 fragp->fr_subtype =
12828 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12829
12830 /* FIXME: We should support this, and let the linker
12831 catch branches and loads that are out of range. */
12832 as_bad_where (fragp->fr_file, fragp->fr_line,
12833 _("unsupported PC relative reference to different section"));
12834
12835 return 1;
12836 }
12837 if (fragp != sym_frag && sym_frag->fr_address == 0)
12838 /* Assume non-extended on the first relaxation pass.
12839 The address we have calculated will be bogus if this is
12840 a forward branch to another frag, as the forward frag
12841 will have fr_address == 0. */
12842 return 0;
12843 }
12844
12845 /* In this case, we know for sure that the symbol fragment is in
12846 the same section. If the relax_marker of the symbol fragment
12847 differs from the relax_marker of this fragment, we have not
12848 yet adjusted the symbol fragment fr_address. We want to add
12849 in STRETCH in order to get a better estimate of the address.
12850 This particularly matters because of the shift bits. */
12851 if (stretch != 0
12852 && sym_frag->relax_marker != fragp->relax_marker)
12853 {
12854 fragS *f;
12855
12856 /* Adjust stretch for any alignment frag. Note that if have
12857 been expanding the earlier code, the symbol may be
12858 defined in what appears to be an earlier frag. FIXME:
12859 This doesn't handle the fr_subtype field, which specifies
12860 a maximum number of bytes to skip when doing an
12861 alignment. */
12862 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12863 {
12864 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12865 {
12866 if (stretch < 0)
12867 stretch = - ((- stretch)
12868 & ~ ((1 << (int) f->fr_offset) - 1));
12869 else
12870 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12871 if (stretch == 0)
12872 break;
12873 }
12874 }
12875 if (f != NULL)
12876 val += stretch;
12877 }
12878
12879 addr = fragp->fr_address + fragp->fr_fix;
12880
12881 /* The base address rules are complicated. The base address of
12882 a branch is the following instruction. The base address of a
12883 PC relative load or add is the instruction itself, but if it
12884 is in a delay slot (in which case it can not be extended) use
12885 the address of the instruction whose delay slot it is in. */
12886 if (type == 'p' || type == 'q')
12887 {
12888 addr += 2;
12889
12890 /* If we are currently assuming that this frag should be
12891 extended, then, the current address is two bytes
12892 higher. */
12893 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12894 addr += 2;
12895
12896 /* Ignore the low bit in the target, since it will be set
12897 for a text label. */
12898 if ((val & 1) != 0)
12899 --val;
12900 }
12901 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12902 addr -= 4;
12903 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12904 addr -= 2;
12905
12906 val -= addr & ~ ((1 << op->shift) - 1);
12907
12908 /* Branch offsets have an implicit 0 in the lowest bit. */
12909 if (type == 'p' || type == 'q')
12910 val /= 2;
12911
12912 /* If any of the shifted bits are set, we must use an extended
12913 opcode. If the address depends on the size of this
12914 instruction, this can lead to a loop, so we arrange to always
12915 use an extended opcode. We only check this when we are in
12916 the main relaxation loop, when SEC is NULL. */
12917 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12918 {
12919 fragp->fr_subtype =
12920 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12921 return 1;
12922 }
12923
12924 /* If we are about to mark a frag as extended because the value
12925 is precisely maxtiny + 1, then there is a chance of an
12926 infinite loop as in the following code:
12927 la $4,foo
12928 .skip 1020
12929 .align 2
12930 foo:
12931 In this case when the la is extended, foo is 0x3fc bytes
12932 away, so the la can be shrunk, but then foo is 0x400 away, so
12933 the la must be extended. To avoid this loop, we mark the
12934 frag as extended if it was small, and is about to become
12935 extended with a value of maxtiny + 1. */
12936 if (val == ((maxtiny + 1) << op->shift)
12937 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12938 && sec == NULL)
12939 {
12940 fragp->fr_subtype =
12941 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12942 return 1;
12943 }
12944 }
12945 else if (symsec != absolute_section && sec != NULL)
12946 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12947
12948 if ((val & ((1 << op->shift) - 1)) != 0
12949 || val < (mintiny << op->shift)
12950 || val > (maxtiny << op->shift))
12951 return 1;
12952 else
12953 return 0;
12954 }
12955
12956 /* Compute the length of a branch sequence, and adjust the
12957 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12958 worst-case length is computed, with UPDATE being used to indicate
12959 whether an unconditional (-1), branch-likely (+1) or regular (0)
12960 branch is to be computed. */
12961 static int
12962 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12963 {
12964 bfd_boolean toofar;
12965 int length;
12966
12967 if (fragp
12968 && S_IS_DEFINED (fragp->fr_symbol)
12969 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12970 {
12971 addressT addr;
12972 offsetT val;
12973
12974 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12975
12976 addr = fragp->fr_address + fragp->fr_fix + 4;
12977
12978 val -= addr;
12979
12980 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12981 }
12982 else if (fragp)
12983 /* If the symbol is not defined or it's in a different segment,
12984 assume the user knows what's going on and emit a short
12985 branch. */
12986 toofar = FALSE;
12987 else
12988 toofar = TRUE;
12989
12990 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12991 fragp->fr_subtype
12992 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12993 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12994 RELAX_BRANCH_LINK (fragp->fr_subtype),
12995 toofar);
12996
12997 length = 4;
12998 if (toofar)
12999 {
13000 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13001 length += 8;
13002
13003 if (mips_pic != NO_PIC)
13004 {
13005 /* Additional space for PIC loading of target address. */
13006 length += 8;
13007 if (mips_opts.isa == ISA_MIPS1)
13008 /* Additional space for $at-stabilizing nop. */
13009 length += 4;
13010 }
13011
13012 /* If branch is conditional. */
13013 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13014 length += 8;
13015 }
13016
13017 return length;
13018 }
13019
13020 /* Estimate the size of a frag before relaxing. Unless this is the
13021 mips16, we are not really relaxing here, and the final size is
13022 encoded in the subtype information. For the mips16, we have to
13023 decide whether we are using an extended opcode or not. */
13024
13025 int
13026 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
13027 {
13028 int change;
13029
13030 if (RELAX_BRANCH_P (fragp->fr_subtype))
13031 {
13032
13033 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13034
13035 return fragp->fr_var;
13036 }
13037
13038 if (RELAX_MIPS16_P (fragp->fr_subtype))
13039 /* We don't want to modify the EXTENDED bit here; it might get us
13040 into infinite loops. We change it only in mips_relax_frag(). */
13041 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13042
13043 if (mips_pic == NO_PIC)
13044 change = nopic_need_relax (fragp->fr_symbol, 0);
13045 else if (mips_pic == SVR4_PIC)
13046 change = pic_need_relax (fragp->fr_symbol, segtype);
13047 else
13048 abort ();
13049
13050 if (change)
13051 {
13052 /* Record the offset to the first reloc in the fr_opcode field.
13053 This lets md_convert_frag and tc_gen_reloc know that the code
13054 must be expanded. */
13055 fragp->fr_opcode = (fragp->fr_literal
13056 + fragp->fr_fix
13057 - RELAX_OLD (fragp->fr_subtype)
13058 + RELAX_RELOC1 (fragp->fr_subtype));
13059 /* FIXME: This really needs as_warn_where. */
13060 if (RELAX_WARN (fragp->fr_subtype))
13061 as_warn (_("AT used after \".set noat\" or macro used after "
13062 "\".set nomacro\""));
13063
13064 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13065 }
13066
13067 return 0;
13068 }
13069
13070 /* This is called to see whether a reloc against a defined symbol
13071 should be converted into a reloc against a section. Don't adjust
13072 MIPS16 jump relocations, so we don't have to worry about the format
13073 of the offset in the .o file. Don't adjust relocations against
13074 mips16 symbols, so that the linker can find them if it needs to set
13075 up a stub. */
13076
13077 int
13078 mips_fix_adjustable (fixS *fixp)
13079 {
13080 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13081 return 0;
13082
13083 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13084 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13085 return 0;
13086
13087 if (fixp->fx_addsy == NULL)
13088 return 1;
13089
13090 #ifdef OBJ_ELF
13091 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13092 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13093 && fixp->fx_subsy == NULL)
13094 return 0;
13095 #endif
13096
13097 return 1;
13098 }
13099
13100 /* Translate internal representation of relocation info to BFD target
13101 format. */
13102
13103 arelent **
13104 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
13105 {
13106 static arelent *retval[4];
13107 arelent *reloc;
13108 bfd_reloc_code_real_type code;
13109
13110 memset (retval, 0, sizeof(retval));
13111 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
13112 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13113 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13114 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13115
13116 if (mips_pic == EMBEDDED_PIC
13117 && SWITCH_TABLE (fixp))
13118 {
13119 /* For a switch table entry we use a special reloc. The addend
13120 is actually the difference between the reloc address and the
13121 subtrahend. */
13122 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13123 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13124 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13125 fixp->fx_r_type = BFD_RELOC_GPREL32;
13126 }
13127 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13128 {
13129 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13130 reloc->addend = fixp->fx_addnumber;
13131 else
13132 {
13133 /* We use a special addend for an internal RELLO reloc. */
13134 if (symbol_section_p (fixp->fx_addsy))
13135 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13136 else
13137 reloc->addend = fixp->fx_addnumber + reloc->address;
13138 }
13139 }
13140 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13141 {
13142 assert (fixp->fx_next != NULL
13143 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13144
13145 /* The reloc is relative to the RELLO; adjust the addend
13146 accordingly. */
13147 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13148 reloc->addend = fixp->fx_next->fx_addnumber;
13149 else
13150 {
13151 /* We use a special addend for an internal RELHI reloc. */
13152 if (symbol_section_p (fixp->fx_addsy))
13153 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13154 + fixp->fx_next->fx_where
13155 - S_GET_VALUE (fixp->fx_subsy));
13156 else
13157 reloc->addend = (fixp->fx_addnumber
13158 + fixp->fx_next->fx_frag->fr_address
13159 + fixp->fx_next->fx_where);
13160 }
13161 }
13162 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13163 reloc->addend = fixp->fx_addnumber;
13164 else
13165 {
13166 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13167 /* A gruesome hack which is a result of the gruesome gas reloc
13168 handling. */
13169 reloc->addend = reloc->address;
13170 else
13171 reloc->addend = -reloc->address;
13172 }
13173
13174 /* If this is a variant frag, we may need to adjust the existing
13175 reloc and generate a new one. */
13176 if (fixp->fx_frag->fr_opcode != NULL
13177 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13178 && ! HAVE_NEWABI)
13179 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13180 && HAVE_NEWABI)
13181 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13182 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13183 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13184 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13185 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13186 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13187 )
13188 {
13189 arelent *reloc2;
13190
13191 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13192
13193 /* If this is not the last reloc in this frag, then we have two
13194 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13195 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13196 the second one handle all of them. */
13197 if (fixp->fx_next != NULL
13198 && fixp->fx_frag == fixp->fx_next->fx_frag)
13199 {
13200 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13201 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13202 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13203 && (fixp->fx_next->fx_r_type
13204 == BFD_RELOC_MIPS_GOT_LO16))
13205 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13206 && (fixp->fx_next->fx_r_type
13207 == BFD_RELOC_MIPS_CALL_LO16)));
13208 retval[0] = NULL;
13209 return retval;
13210 }
13211
13212 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13213 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13214 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13215 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13216 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13217 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13218 reloc2->address = (reloc->address
13219 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13220 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13221 reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13222 + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13223 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13224 assert (reloc2->howto != NULL);
13225
13226 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13227 {
13228 arelent *reloc3;
13229
13230 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13231 *reloc3 = *reloc2;
13232 reloc3->address += 4;
13233 }
13234
13235 if (mips_pic == NO_PIC)
13236 {
13237 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13238 fixp->fx_r_type = BFD_RELOC_HI16_S;
13239 }
13240 else if (mips_pic == SVR4_PIC)
13241 {
13242 switch (fixp->fx_r_type)
13243 {
13244 default:
13245 abort ();
13246 case BFD_RELOC_MIPS_GOT16:
13247 break;
13248 case BFD_RELOC_MIPS_GOT_LO16:
13249 case BFD_RELOC_MIPS_CALL_LO16:
13250 if (HAVE_NEWABI)
13251 {
13252 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13253 reloc2->howto = bfd_reloc_type_lookup
13254 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13255 }
13256 else
13257 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13258 break;
13259 case BFD_RELOC_MIPS_CALL16:
13260 case BFD_RELOC_MIPS_GOT_OFST:
13261 case BFD_RELOC_MIPS_GOT_DISP:
13262 if (HAVE_NEWABI)
13263 {
13264 /* It may seem nonsensical to relax GOT_DISP to
13265 GOT_DISP, but we're actually turning a GOT_DISP
13266 without offset into a GOT_DISP with an offset,
13267 getting rid of the separate addition, which we can
13268 do when the symbol is found to be local. */
13269 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13270 retval[1] = NULL;
13271 }
13272 else
13273 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13274 break;
13275 }
13276 }
13277 else
13278 abort ();
13279 }
13280
13281 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13282 entry to be used in the relocation's section offset. */
13283 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13284 {
13285 reloc->address = reloc->addend;
13286 reloc->addend = 0;
13287 }
13288
13289 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13290 fixup_segment converted a non-PC relative reloc into a PC
13291 relative reloc. In such a case, we need to convert the reloc
13292 code. */
13293 code = fixp->fx_r_type;
13294 if (fixp->fx_pcrel)
13295 {
13296 switch (code)
13297 {
13298 case BFD_RELOC_8:
13299 code = BFD_RELOC_8_PCREL;
13300 break;
13301 case BFD_RELOC_16:
13302 code = BFD_RELOC_16_PCREL;
13303 break;
13304 case BFD_RELOC_32:
13305 code = BFD_RELOC_32_PCREL;
13306 break;
13307 case BFD_RELOC_64:
13308 code = BFD_RELOC_64_PCREL;
13309 break;
13310 case BFD_RELOC_8_PCREL:
13311 case BFD_RELOC_16_PCREL:
13312 case BFD_RELOC_32_PCREL:
13313 case BFD_RELOC_64_PCREL:
13314 case BFD_RELOC_16_PCREL_S2:
13315 case BFD_RELOC_PCREL_HI16_S:
13316 case BFD_RELOC_PCREL_LO16:
13317 break;
13318 default:
13319 as_bad_where (fixp->fx_file, fixp->fx_line,
13320 _("Cannot make %s relocation PC relative"),
13321 bfd_get_reloc_code_name (code));
13322 }
13323 }
13324
13325 /* To support a PC relative reloc when generating embedded PIC code
13326 for ECOFF, we use a Cygnus extension. We check for that here to
13327 make sure that we don't let such a reloc escape normally. */
13328 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13329 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13330 && code == BFD_RELOC_16_PCREL_S2
13331 && mips_pic != EMBEDDED_PIC)
13332 reloc->howto = NULL;
13333 else
13334 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13335
13336 if (reloc->howto == NULL)
13337 {
13338 as_bad_where (fixp->fx_file, fixp->fx_line,
13339 _("Can not represent %s relocation in this object file format"),
13340 bfd_get_reloc_code_name (code));
13341 retval[0] = NULL;
13342 }
13343
13344 return retval;
13345 }
13346
13347 /* Relax a machine dependent frag. This returns the amount by which
13348 the current size of the frag should change. */
13349
13350 int
13351 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13352 {
13353 if (RELAX_BRANCH_P (fragp->fr_subtype))
13354 {
13355 offsetT old_var = fragp->fr_var;
13356
13357 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13358
13359 return fragp->fr_var - old_var;
13360 }
13361
13362 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13363 return 0;
13364
13365 if (mips16_extended_frag (fragp, NULL, stretch))
13366 {
13367 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13368 return 0;
13369 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13370 return 2;
13371 }
13372 else
13373 {
13374 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13375 return 0;
13376 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13377 return -2;
13378 }
13379
13380 return 0;
13381 }
13382
13383 /* Convert a machine dependent frag. */
13384
13385 void
13386 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13387 {
13388 int old, new;
13389 char *fixptr;
13390
13391 if (RELAX_BRANCH_P (fragp->fr_subtype))
13392 {
13393 bfd_byte *buf;
13394 unsigned long insn;
13395 expressionS exp;
13396 fixS *fixp;
13397
13398 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13399
13400 if (target_big_endian)
13401 insn = bfd_getb32 (buf);
13402 else
13403 insn = bfd_getl32 (buf);
13404
13405 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13406 {
13407 /* We generate a fixup instead of applying it right now
13408 because, if there are linker relaxations, we're going to
13409 need the relocations. */
13410 exp.X_op = O_symbol;
13411 exp.X_add_symbol = fragp->fr_symbol;
13412 exp.X_add_number = fragp->fr_offset;
13413
13414 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13415 4, &exp, 1,
13416 BFD_RELOC_16_PCREL_S2);
13417 fixp->fx_file = fragp->fr_file;
13418 fixp->fx_line = fragp->fr_line;
13419
13420 md_number_to_chars (buf, insn, 4);
13421 buf += 4;
13422 }
13423 else
13424 {
13425 int i;
13426
13427 as_warn_where (fragp->fr_file, fragp->fr_line,
13428 _("relaxed out-of-range branch into a jump"));
13429
13430 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13431 goto uncond;
13432
13433 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13434 {
13435 /* Reverse the branch. */
13436 switch ((insn >> 28) & 0xf)
13437 {
13438 case 4:
13439 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13440 have the condition reversed by tweaking a single
13441 bit, and their opcodes all have 0x4???????. */
13442 assert ((insn & 0xf1000000) == 0x41000000);
13443 insn ^= 0x00010000;
13444 break;
13445
13446 case 0:
13447 /* bltz 0x04000000 bgez 0x04010000
13448 bltzal 0x04100000 bgezal 0x04110000 */
13449 assert ((insn & 0xfc0e0000) == 0x04000000);
13450 insn ^= 0x00010000;
13451 break;
13452
13453 case 1:
13454 /* beq 0x10000000 bne 0x14000000
13455 blez 0x18000000 bgtz 0x1c000000 */
13456 insn ^= 0x04000000;
13457 break;
13458
13459 default:
13460 abort ();
13461 }
13462 }
13463
13464 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13465 {
13466 /* Clear the and-link bit. */
13467 assert ((insn & 0xfc1c0000) == 0x04100000);
13468
13469 /* bltzal 0x04100000 bgezal 0x04110000
13470 bltzall 0x04120000 bgezall 0x04130000 */
13471 insn &= ~0x00100000;
13472 }
13473
13474 /* Branch over the branch (if the branch was likely) or the
13475 full jump (not likely case). Compute the offset from the
13476 current instruction to branch to. */
13477 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13478 i = 16;
13479 else
13480 {
13481 /* How many bytes in instructions we've already emitted? */
13482 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13483 /* How many bytes in instructions from here to the end? */
13484 i = fragp->fr_var - i;
13485 }
13486 /* Convert to instruction count. */
13487 i >>= 2;
13488 /* Branch counts from the next instruction. */
13489 i--;
13490 insn |= i;
13491 /* Branch over the jump. */
13492 md_number_to_chars (buf, insn, 4);
13493 buf += 4;
13494
13495 /* Nop */
13496 md_number_to_chars (buf, 0, 4);
13497 buf += 4;
13498
13499 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13500 {
13501 /* beql $0, $0, 2f */
13502 insn = 0x50000000;
13503 /* Compute the PC offset from the current instruction to
13504 the end of the variable frag. */
13505 /* How many bytes in instructions we've already emitted? */
13506 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13507 /* How many bytes in instructions from here to the end? */
13508 i = fragp->fr_var - i;
13509 /* Convert to instruction count. */
13510 i >>= 2;
13511 /* Don't decrement i, because we want to branch over the
13512 delay slot. */
13513
13514 insn |= i;
13515 md_number_to_chars (buf, insn, 4);
13516 buf += 4;
13517
13518 md_number_to_chars (buf, 0, 4);
13519 buf += 4;
13520 }
13521
13522 uncond:
13523 if (mips_pic == NO_PIC)
13524 {
13525 /* j or jal. */
13526 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13527 ? 0x0c000000 : 0x08000000);
13528 exp.X_op = O_symbol;
13529 exp.X_add_symbol = fragp->fr_symbol;
13530 exp.X_add_number = fragp->fr_offset;
13531
13532 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13533 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13534 fixp->fx_file = fragp->fr_file;
13535 fixp->fx_line = fragp->fr_line;
13536
13537 md_number_to_chars (buf, insn, 4);
13538 buf += 4;
13539 }
13540 else
13541 {
13542 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13543 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13544 exp.X_op = O_symbol;
13545 exp.X_add_symbol = fragp->fr_symbol;
13546 exp.X_add_number = fragp->fr_offset;
13547
13548 if (fragp->fr_offset)
13549 {
13550 exp.X_add_symbol = make_expr_symbol (&exp);
13551 exp.X_add_number = 0;
13552 }
13553
13554 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13555 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13556 fixp->fx_file = fragp->fr_file;
13557 fixp->fx_line = fragp->fr_line;
13558
13559 md_number_to_chars (buf, insn, 4);
13560 buf += 4;
13561
13562 if (mips_opts.isa == ISA_MIPS1)
13563 {
13564 /* nop */
13565 md_number_to_chars (buf, 0, 4);
13566 buf += 4;
13567 }
13568
13569 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13570 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13571
13572 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13573 4, &exp, 0, BFD_RELOC_LO16);
13574 fixp->fx_file = fragp->fr_file;
13575 fixp->fx_line = fragp->fr_line;
13576
13577 md_number_to_chars (buf, insn, 4);
13578 buf += 4;
13579
13580 /* j(al)r $at. */
13581 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13582 insn = 0x0020f809;
13583 else
13584 insn = 0x00200008;
13585
13586 md_number_to_chars (buf, insn, 4);
13587 buf += 4;
13588 }
13589 }
13590
13591 assert (buf == (bfd_byte *)fragp->fr_literal
13592 + fragp->fr_fix + fragp->fr_var);
13593
13594 fragp->fr_fix += fragp->fr_var;
13595
13596 return;
13597 }
13598
13599 if (RELAX_MIPS16_P (fragp->fr_subtype))
13600 {
13601 int type;
13602 register const struct mips16_immed_operand *op;
13603 bfd_boolean small, ext;
13604 offsetT val;
13605 bfd_byte *buf;
13606 unsigned long insn;
13607 bfd_boolean use_extend;
13608 unsigned short extend;
13609
13610 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13611 op = mips16_immed_operands;
13612 while (op->type != type)
13613 ++op;
13614
13615 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13616 {
13617 small = FALSE;
13618 ext = TRUE;
13619 }
13620 else
13621 {
13622 small = TRUE;
13623 ext = FALSE;
13624 }
13625
13626 resolve_symbol_value (fragp->fr_symbol);
13627 val = S_GET_VALUE (fragp->fr_symbol);
13628 if (op->pcrel)
13629 {
13630 addressT addr;
13631
13632 addr = fragp->fr_address + fragp->fr_fix;
13633
13634 /* The rules for the base address of a PC relative reloc are
13635 complicated; see mips16_extended_frag. */
13636 if (type == 'p' || type == 'q')
13637 {
13638 addr += 2;
13639 if (ext)
13640 addr += 2;
13641 /* Ignore the low bit in the target, since it will be
13642 set for a text label. */
13643 if ((val & 1) != 0)
13644 --val;
13645 }
13646 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13647 addr -= 4;
13648 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13649 addr -= 2;
13650
13651 addr &= ~ (addressT) ((1 << op->shift) - 1);
13652 val -= addr;
13653
13654 /* Make sure the section winds up with the alignment we have
13655 assumed. */
13656 if (op->shift > 0)
13657 record_alignment (asec, op->shift);
13658 }
13659
13660 if (ext
13661 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13662 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13663 as_warn_where (fragp->fr_file, fragp->fr_line,
13664 _("extended instruction in delay slot"));
13665
13666 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13667
13668 if (target_big_endian)
13669 insn = bfd_getb16 (buf);
13670 else
13671 insn = bfd_getl16 (buf);
13672
13673 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13674 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13675 small, ext, &insn, &use_extend, &extend);
13676
13677 if (use_extend)
13678 {
13679 md_number_to_chars (buf, 0xf000 | extend, 2);
13680 fragp->fr_fix += 2;
13681 buf += 2;
13682 }
13683
13684 md_number_to_chars (buf, insn, 2);
13685 fragp->fr_fix += 2;
13686 buf += 2;
13687 }
13688 else
13689 {
13690 if (fragp->fr_opcode == NULL)
13691 return;
13692
13693 old = RELAX_OLD (fragp->fr_subtype);
13694 new = RELAX_NEW (fragp->fr_subtype);
13695 fixptr = fragp->fr_literal + fragp->fr_fix;
13696
13697 if (new > 0)
13698 memmove (fixptr - old, fixptr, new);
13699
13700 fragp->fr_fix += new - old;
13701 }
13702 }
13703
13704 #ifdef OBJ_ELF
13705
13706 /* This function is called after the relocs have been generated.
13707 We've been storing mips16 text labels as odd. Here we convert them
13708 back to even for the convenience of the debugger. */
13709
13710 void
13711 mips_frob_file_after_relocs (void)
13712 {
13713 asymbol **syms;
13714 unsigned int count, i;
13715
13716 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13717 return;
13718
13719 syms = bfd_get_outsymbols (stdoutput);
13720 count = bfd_get_symcount (stdoutput);
13721 for (i = 0; i < count; i++, syms++)
13722 {
13723 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13724 && ((*syms)->value & 1) != 0)
13725 {
13726 (*syms)->value &= ~1;
13727 /* If the symbol has an odd size, it was probably computed
13728 incorrectly, so adjust that as well. */
13729 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13730 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13731 }
13732 }
13733 }
13734
13735 #endif
13736
13737 /* This function is called whenever a label is defined. It is used
13738 when handling branch delays; if a branch has a label, we assume we
13739 can not move it. */
13740
13741 void
13742 mips_define_label (symbolS *sym)
13743 {
13744 struct insn_label_list *l;
13745
13746 if (free_insn_labels == NULL)
13747 l = (struct insn_label_list *) xmalloc (sizeof *l);
13748 else
13749 {
13750 l = free_insn_labels;
13751 free_insn_labels = l->next;
13752 }
13753
13754 l->label = sym;
13755 l->next = insn_labels;
13756 insn_labels = l;
13757 }
13758 \f
13759 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13760
13761 /* Some special processing for a MIPS ELF file. */
13762
13763 void
13764 mips_elf_final_processing (void)
13765 {
13766 /* Write out the register information. */
13767 if (mips_abi != N64_ABI)
13768 {
13769 Elf32_RegInfo s;
13770
13771 s.ri_gprmask = mips_gprmask;
13772 s.ri_cprmask[0] = mips_cprmask[0];
13773 s.ri_cprmask[1] = mips_cprmask[1];
13774 s.ri_cprmask[2] = mips_cprmask[2];
13775 s.ri_cprmask[3] = mips_cprmask[3];
13776 /* The gp_value field is set by the MIPS ELF backend. */
13777
13778 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13779 ((Elf32_External_RegInfo *)
13780 mips_regmask_frag));
13781 }
13782 else
13783 {
13784 Elf64_Internal_RegInfo s;
13785
13786 s.ri_gprmask = mips_gprmask;
13787 s.ri_pad = 0;
13788 s.ri_cprmask[0] = mips_cprmask[0];
13789 s.ri_cprmask[1] = mips_cprmask[1];
13790 s.ri_cprmask[2] = mips_cprmask[2];
13791 s.ri_cprmask[3] = mips_cprmask[3];
13792 /* The gp_value field is set by the MIPS ELF backend. */
13793
13794 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13795 ((Elf64_External_RegInfo *)
13796 mips_regmask_frag));
13797 }
13798
13799 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13800 sort of BFD interface for this. */
13801 if (mips_any_noreorder)
13802 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13803 if (mips_pic != NO_PIC)
13804 {
13805 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13806 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13807 }
13808 if (mips_abicalls)
13809 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13810
13811 /* Set MIPS ELF flags for ASEs. */
13812 if (file_ase_mips16)
13813 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13814 #if 0 /* XXX FIXME */
13815 if (file_ase_mips3d)
13816 elf_elfheader (stdoutput)->e_flags |= ???;
13817 #endif
13818 if (file_ase_mdmx)
13819 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13820
13821 /* Set the MIPS ELF ABI flags. */
13822 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13823 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13824 else if (mips_abi == O64_ABI)
13825 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13826 else if (mips_abi == EABI_ABI)
13827 {
13828 if (!file_mips_gp32)
13829 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13830 else
13831 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13832 }
13833 else if (mips_abi == N32_ABI)
13834 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13835
13836 /* Nothing to do for N64_ABI. */
13837
13838 if (mips_32bitmode)
13839 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13840 }
13841
13842 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13843 \f
13844 typedef struct proc {
13845 symbolS *isym;
13846 unsigned long reg_mask;
13847 unsigned long reg_offset;
13848 unsigned long fpreg_mask;
13849 unsigned long fpreg_offset;
13850 unsigned long frame_offset;
13851 unsigned long frame_reg;
13852 unsigned long pc_reg;
13853 } procS;
13854
13855 static procS cur_proc;
13856 static procS *cur_proc_ptr;
13857 static int numprocs;
13858
13859 /* Fill in an rs_align_code fragment. */
13860
13861 void
13862 mips_handle_align (fragS *fragp)
13863 {
13864 if (fragp->fr_type != rs_align_code)
13865 return;
13866
13867 if (mips_opts.mips16)
13868 {
13869 static const unsigned char be_nop[] = { 0x65, 0x00 };
13870 static const unsigned char le_nop[] = { 0x00, 0x65 };
13871
13872 int bytes;
13873 char *p;
13874
13875 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13876 p = fragp->fr_literal + fragp->fr_fix;
13877
13878 if (bytes & 1)
13879 {
13880 *p++ = 0;
13881 fragp->fr_fix++;
13882 }
13883
13884 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13885 fragp->fr_var = 2;
13886 }
13887
13888 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13889 }
13890
13891 static void
13892 md_obj_begin (void)
13893 {
13894 }
13895
13896 static void
13897 md_obj_end (void)
13898 {
13899 /* check for premature end, nesting errors, etc */
13900 if (cur_proc_ptr)
13901 as_warn (_("missing .end at end of assembly"));
13902 }
13903
13904 static long
13905 get_number (void)
13906 {
13907 int negative = 0;
13908 long val = 0;
13909
13910 if (*input_line_pointer == '-')
13911 {
13912 ++input_line_pointer;
13913 negative = 1;
13914 }
13915 if (!ISDIGIT (*input_line_pointer))
13916 as_bad (_("expected simple number"));
13917 if (input_line_pointer[0] == '0')
13918 {
13919 if (input_line_pointer[1] == 'x')
13920 {
13921 input_line_pointer += 2;
13922 while (ISXDIGIT (*input_line_pointer))
13923 {
13924 val <<= 4;
13925 val |= hex_value (*input_line_pointer++);
13926 }
13927 return negative ? -val : val;
13928 }
13929 else
13930 {
13931 ++input_line_pointer;
13932 while (ISDIGIT (*input_line_pointer))
13933 {
13934 val <<= 3;
13935 val |= *input_line_pointer++ - '0';
13936 }
13937 return negative ? -val : val;
13938 }
13939 }
13940 if (!ISDIGIT (*input_line_pointer))
13941 {
13942 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13943 *input_line_pointer, *input_line_pointer);
13944 as_warn (_("invalid number"));
13945 return -1;
13946 }
13947 while (ISDIGIT (*input_line_pointer))
13948 {
13949 val *= 10;
13950 val += *input_line_pointer++ - '0';
13951 }
13952 return negative ? -val : val;
13953 }
13954
13955 /* The .file directive; just like the usual .file directive, but there
13956 is an initial number which is the ECOFF file index. In the non-ECOFF
13957 case .file implies DWARF-2. */
13958
13959 static void
13960 s_mips_file (int x ATTRIBUTE_UNUSED)
13961 {
13962 static int first_file_directive = 0;
13963
13964 if (ECOFF_DEBUGGING)
13965 {
13966 get_number ();
13967 s_app_file (0);
13968 }
13969 else
13970 {
13971 char *filename;
13972
13973 filename = dwarf2_directive_file (0);
13974
13975 /* Versions of GCC up to 3.1 start files with a ".file"
13976 directive even for stabs output. Make sure that this
13977 ".file" is handled. Note that you need a version of GCC
13978 after 3.1 in order to support DWARF-2 on MIPS. */
13979 if (filename != NULL && ! first_file_directive)
13980 {
13981 (void) new_logical_line (filename, -1);
13982 s_app_file_string (filename);
13983 }
13984 first_file_directive = 1;
13985 }
13986 }
13987
13988 /* The .loc directive, implying DWARF-2. */
13989
13990 static void
13991 s_mips_loc (int x ATTRIBUTE_UNUSED)
13992 {
13993 if (!ECOFF_DEBUGGING)
13994 dwarf2_directive_loc (0);
13995 }
13996
13997 /* The .end directive. */
13998
13999 static void
14000 s_mips_end (int x ATTRIBUTE_UNUSED)
14001 {
14002 symbolS *p;
14003
14004 /* Following functions need their own .frame and .cprestore directives. */
14005 mips_frame_reg_valid = 0;
14006 mips_cprestore_valid = 0;
14007
14008 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14009 {
14010 p = get_symbol ();
14011 demand_empty_rest_of_line ();
14012 }
14013 else
14014 p = NULL;
14015
14016 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14017 as_warn (_(".end not in text section"));
14018
14019 if (!cur_proc_ptr)
14020 {
14021 as_warn (_(".end directive without a preceding .ent directive."));
14022 demand_empty_rest_of_line ();
14023 return;
14024 }
14025
14026 if (p != NULL)
14027 {
14028 assert (S_GET_NAME (p));
14029 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14030 as_warn (_(".end symbol does not match .ent symbol."));
14031
14032 if (debug_type == DEBUG_STABS)
14033 stabs_generate_asm_endfunc (S_GET_NAME (p),
14034 S_GET_NAME (p));
14035 }
14036 else
14037 as_warn (_(".end directive missing or unknown symbol"));
14038
14039 #ifdef OBJ_ELF
14040 /* Generate a .pdr section. */
14041 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14042 {
14043 segT saved_seg = now_seg;
14044 subsegT saved_subseg = now_subseg;
14045 valueT dot;
14046 expressionS exp;
14047 char *fragp;
14048
14049 dot = frag_now_fix ();
14050
14051 #ifdef md_flush_pending_output
14052 md_flush_pending_output ();
14053 #endif
14054
14055 assert (pdr_seg);
14056 subseg_set (pdr_seg, 0);
14057
14058 /* Write the symbol. */
14059 exp.X_op = O_symbol;
14060 exp.X_add_symbol = p;
14061 exp.X_add_number = 0;
14062 emit_expr (&exp, 4);
14063
14064 fragp = frag_more (7 * 4);
14065
14066 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14067 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14068 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14069 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14070 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14071 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14072 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
14073
14074 subseg_set (saved_seg, saved_subseg);
14075 }
14076 #endif /* OBJ_ELF */
14077
14078 cur_proc_ptr = NULL;
14079 }
14080
14081 /* The .aent and .ent directives. */
14082
14083 static void
14084 s_mips_ent (int aent)
14085 {
14086 symbolS *symbolP;
14087
14088 symbolP = get_symbol ();
14089 if (*input_line_pointer == ',')
14090 ++input_line_pointer;
14091 SKIP_WHITESPACE ();
14092 if (ISDIGIT (*input_line_pointer)
14093 || *input_line_pointer == '-')
14094 get_number ();
14095
14096 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14097 as_warn (_(".ent or .aent not in text section."));
14098
14099 if (!aent && cur_proc_ptr)
14100 as_warn (_("missing .end"));
14101
14102 if (!aent)
14103 {
14104 /* This function needs its own .frame and .cprestore directives. */
14105 mips_frame_reg_valid = 0;
14106 mips_cprestore_valid = 0;
14107
14108 cur_proc_ptr = &cur_proc;
14109 memset (cur_proc_ptr, '\0', sizeof (procS));
14110
14111 cur_proc_ptr->isym = symbolP;
14112
14113 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14114
14115 ++numprocs;
14116
14117 if (debug_type == DEBUG_STABS)
14118 stabs_generate_asm_func (S_GET_NAME (symbolP),
14119 S_GET_NAME (symbolP));
14120 }
14121
14122 demand_empty_rest_of_line ();
14123 }
14124
14125 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14126 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14127 s_mips_frame is used so that we can set the PDR information correctly.
14128 We can't use the ecoff routines because they make reference to the ecoff
14129 symbol table (in the mdebug section). */
14130
14131 static void
14132 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
14133 {
14134 #ifdef OBJ_ELF
14135 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14136 {
14137 long val;
14138
14139 if (cur_proc_ptr == (procS *) NULL)
14140 {
14141 as_warn (_(".frame outside of .ent"));
14142 demand_empty_rest_of_line ();
14143 return;
14144 }
14145
14146 cur_proc_ptr->frame_reg = tc_get_register (1);
14147
14148 SKIP_WHITESPACE ();
14149 if (*input_line_pointer++ != ','
14150 || get_absolute_expression_and_terminator (&val) != ',')
14151 {
14152 as_warn (_("Bad .frame directive"));
14153 --input_line_pointer;
14154 demand_empty_rest_of_line ();
14155 return;
14156 }
14157
14158 cur_proc_ptr->frame_offset = val;
14159 cur_proc_ptr->pc_reg = tc_get_register (0);
14160
14161 demand_empty_rest_of_line ();
14162 }
14163 else
14164 #endif /* OBJ_ELF */
14165 s_ignore (ignore);
14166 }
14167
14168 /* The .fmask and .mask directives. If the mdebug section is present
14169 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14170 embedded targets, s_mips_mask is used so that we can set the PDR
14171 information correctly. We can't use the ecoff routines because they
14172 make reference to the ecoff symbol table (in the mdebug section). */
14173
14174 static void
14175 s_mips_mask (int reg_type)
14176 {
14177 #ifdef OBJ_ELF
14178 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14179 {
14180 long mask, off;
14181
14182 if (cur_proc_ptr == (procS *) NULL)
14183 {
14184 as_warn (_(".mask/.fmask outside of .ent"));
14185 demand_empty_rest_of_line ();
14186 return;
14187 }
14188
14189 if (get_absolute_expression_and_terminator (&mask) != ',')
14190 {
14191 as_warn (_("Bad .mask/.fmask directive"));
14192 --input_line_pointer;
14193 demand_empty_rest_of_line ();
14194 return;
14195 }
14196
14197 off = get_absolute_expression ();
14198
14199 if (reg_type == 'F')
14200 {
14201 cur_proc_ptr->fpreg_mask = mask;
14202 cur_proc_ptr->fpreg_offset = off;
14203 }
14204 else
14205 {
14206 cur_proc_ptr->reg_mask = mask;
14207 cur_proc_ptr->reg_offset = off;
14208 }
14209
14210 demand_empty_rest_of_line ();
14211 }
14212 else
14213 #endif /* OBJ_ELF */
14214 s_ignore (reg_type);
14215 }
14216
14217 /* The .loc directive. */
14218
14219 #if 0
14220 static void
14221 s_loc (int x)
14222 {
14223 symbolS *symbolP;
14224 int lineno;
14225 int addroff;
14226
14227 assert (now_seg == text_section);
14228
14229 lineno = get_number ();
14230 addroff = frag_now_fix ();
14231
14232 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14233 S_SET_TYPE (symbolP, N_SLINE);
14234 S_SET_OTHER (symbolP, 0);
14235 S_SET_DESC (symbolP, lineno);
14236 symbolP->sy_segment = now_seg;
14237 }
14238 #endif
14239
14240 /* A table describing all the processors gas knows about. Names are
14241 matched in the order listed.
14242
14243 To ease comparison, please keep this table in the same order as
14244 gcc's mips_cpu_info_table[]. */
14245 static const struct mips_cpu_info mips_cpu_info_table[] =
14246 {
14247 /* Entries for generic ISAs */
14248 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14249 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14250 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14251 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14252 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14253 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14254 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
14255 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14256
14257 /* MIPS I */
14258 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14259 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14260 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14261
14262 /* MIPS II */
14263 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14264
14265 /* MIPS III */
14266 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14267 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14268 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14269 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14270 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14271 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14272 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14273 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14274 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14275 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14276 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14277 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14278
14279 /* MIPS IV */
14280 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14281 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14282 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14283 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14284 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14285 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14286 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14287 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14288 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14289 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14290 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14291 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14292
14293 /* MIPS 32 */
14294 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14295 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14296 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14297
14298 /* MIPS 64 */
14299 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14300 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14301
14302 /* Broadcom SB-1 CPU core */
14303 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14304
14305 /* End marker */
14306 { NULL, 0, 0, 0 }
14307 };
14308
14309
14310 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14311 with a final "000" replaced by "k". Ignore case.
14312
14313 Note: this function is shared between GCC and GAS. */
14314
14315 static bfd_boolean
14316 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14317 {
14318 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14319 given++, canonical++;
14320
14321 return ((*given == 0 && *canonical == 0)
14322 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14323 }
14324
14325
14326 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14327 CPU name. We've traditionally allowed a lot of variation here.
14328
14329 Note: this function is shared between GCC and GAS. */
14330
14331 static bfd_boolean
14332 mips_matching_cpu_name_p (const char *canonical, const char *given)
14333 {
14334 /* First see if the name matches exactly, or with a final "000"
14335 turned into "k". */
14336 if (mips_strict_matching_cpu_name_p (canonical, given))
14337 return TRUE;
14338
14339 /* If not, try comparing based on numerical designation alone.
14340 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14341 if (TOLOWER (*given) == 'r')
14342 given++;
14343 if (!ISDIGIT (*given))
14344 return FALSE;
14345
14346 /* Skip over some well-known prefixes in the canonical name,
14347 hoping to find a number there too. */
14348 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14349 canonical += 2;
14350 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14351 canonical += 2;
14352 else if (TOLOWER (canonical[0]) == 'r')
14353 canonical += 1;
14354
14355 return mips_strict_matching_cpu_name_p (canonical, given);
14356 }
14357
14358
14359 /* Parse an option that takes the name of a processor as its argument.
14360 OPTION is the name of the option and CPU_STRING is the argument.
14361 Return the corresponding processor enumeration if the CPU_STRING is
14362 recognized, otherwise report an error and return null.
14363
14364 A similar function exists in GCC. */
14365
14366 static const struct mips_cpu_info *
14367 mips_parse_cpu (const char *option, const char *cpu_string)
14368 {
14369 const struct mips_cpu_info *p;
14370
14371 /* 'from-abi' selects the most compatible architecture for the given
14372 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14373 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14374 version. Look first at the -mgp options, if given, otherwise base
14375 the choice on MIPS_DEFAULT_64BIT.
14376
14377 Treat NO_ABI like the EABIs. One reason to do this is that the
14378 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14379 architecture. This code picks MIPS I for 'mips' and MIPS III for
14380 'mips64', just as we did in the days before 'from-abi'. */
14381 if (strcasecmp (cpu_string, "from-abi") == 0)
14382 {
14383 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14384 return mips_cpu_info_from_isa (ISA_MIPS1);
14385
14386 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14387 return mips_cpu_info_from_isa (ISA_MIPS3);
14388
14389 if (file_mips_gp32 >= 0)
14390 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14391
14392 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14393 ? ISA_MIPS3
14394 : ISA_MIPS1);
14395 }
14396
14397 /* 'default' has traditionally been a no-op. Probably not very useful. */
14398 if (strcasecmp (cpu_string, "default") == 0)
14399 return 0;
14400
14401 for (p = mips_cpu_info_table; p->name != 0; p++)
14402 if (mips_matching_cpu_name_p (p->name, cpu_string))
14403 return p;
14404
14405 as_bad ("Bad value (%s) for %s", cpu_string, option);
14406 return 0;
14407 }
14408
14409 /* Return the canonical processor information for ISA (a member of the
14410 ISA_MIPS* enumeration). */
14411
14412 static const struct mips_cpu_info *
14413 mips_cpu_info_from_isa (int isa)
14414 {
14415 int i;
14416
14417 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14418 if (mips_cpu_info_table[i].is_isa
14419 && isa == mips_cpu_info_table[i].isa)
14420 return (&mips_cpu_info_table[i]);
14421
14422 return NULL;
14423 }
14424
14425 static const struct mips_cpu_info *
14426 mips_cpu_info_from_arch (int arch)
14427 {
14428 int i;
14429
14430 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14431 if (arch == mips_cpu_info_table[i].cpu)
14432 return (&mips_cpu_info_table[i]);
14433
14434 return NULL;
14435 }
14436 \f
14437 static void
14438 show (FILE *stream, const char *string, int *col_p, int *first_p)
14439 {
14440 if (*first_p)
14441 {
14442 fprintf (stream, "%24s", "");
14443 *col_p = 24;
14444 }
14445 else
14446 {
14447 fprintf (stream, ", ");
14448 *col_p += 2;
14449 }
14450
14451 if (*col_p + strlen (string) > 72)
14452 {
14453 fprintf (stream, "\n%24s", "");
14454 *col_p = 24;
14455 }
14456
14457 fprintf (stream, "%s", string);
14458 *col_p += strlen (string);
14459
14460 *first_p = 0;
14461 }
14462
14463 void
14464 md_show_usage (FILE *stream)
14465 {
14466 int column, first;
14467 size_t i;
14468
14469 fprintf (stream, _("\
14470 MIPS options:\n\
14471 -membedded-pic generate embedded position independent code\n\
14472 -EB generate big endian output\n\
14473 -EL generate little endian output\n\
14474 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14475 -G NUM allow referencing objects up to NUM bytes\n\
14476 implicitly with the gp register [default 8]\n"));
14477 fprintf (stream, _("\
14478 -mips1 generate MIPS ISA I instructions\n\
14479 -mips2 generate MIPS ISA II instructions\n\
14480 -mips3 generate MIPS ISA III instructions\n\
14481 -mips4 generate MIPS ISA IV instructions\n\
14482 -mips5 generate MIPS ISA V instructions\n\
14483 -mips32 generate MIPS32 ISA instructions\n\
14484 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14485 -mips64 generate MIPS64 ISA instructions\n\
14486 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14487
14488 first = 1;
14489
14490 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14491 show (stream, mips_cpu_info_table[i].name, &column, &first);
14492 show (stream, "from-abi", &column, &first);
14493 fputc ('\n', stream);
14494
14495 fprintf (stream, _("\
14496 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14497 -no-mCPU don't generate code specific to CPU.\n\
14498 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14499
14500 first = 1;
14501
14502 show (stream, "3900", &column, &first);
14503 show (stream, "4010", &column, &first);
14504 show (stream, "4100", &column, &first);
14505 show (stream, "4650", &column, &first);
14506 fputc ('\n', stream);
14507
14508 fprintf (stream, _("\
14509 -mips16 generate mips16 instructions\n\
14510 -no-mips16 do not generate mips16 instructions\n"));
14511 fprintf (stream, _("\
14512 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14513 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14514 -O0 remove unneeded NOPs, do not swap branches\n\
14515 -O remove unneeded NOPs and swap branches\n\
14516 -n warn about NOPs generated from macros\n\
14517 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14518 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14519 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14520 #ifdef OBJ_ELF
14521 fprintf (stream, _("\
14522 -KPIC, -call_shared generate SVR4 position independent code\n\
14523 -non_shared do not generate position independent code\n\
14524 -xgot assume a 32 bit GOT\n\
14525 -mabi=ABI create ABI conformant object file for:\n"));
14526
14527 first = 1;
14528
14529 show (stream, "32", &column, &first);
14530 show (stream, "o64", &column, &first);
14531 show (stream, "n32", &column, &first);
14532 show (stream, "64", &column, &first);
14533 show (stream, "eabi", &column, &first);
14534
14535 fputc ('\n', stream);
14536
14537 fprintf (stream, _("\
14538 -32 create o32 ABI object file (default)\n\
14539 -n32 create n32 ABI object file\n\
14540 -64 create 64 ABI object file\n"));
14541 #endif
14542 }
14543
14544 enum dwarf2_format
14545 mips_dwarf2_format (void)
14546 {
14547 if (mips_abi == N64_ABI)
14548 {
14549 #ifdef TE_IRIX
14550 return dwarf2_format_64bit_irix;
14551 #else
14552 return dwarf2_format_64bit;
14553 #endif
14554 }
14555 else
14556 return dwarf2_format_32bit;
14557 }
This page took 0.364454 seconds and 4 git commands to generate.