2004-04-22 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,
3 2003, 2004 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too. */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about. */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT 1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP 28
99 #define SP 29
100 #define FP 30
101 #define RA 31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format. */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
122 /* The ABI to use. */
123 enum mips_abi_level
124 {
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131 };
132
133 /* MIPS ABI we are using for this output file. */
134 static enum mips_abi_level mips_abi = NO_ABI;
135
136 /* Whether or not we have code that can call pic code. */
137 int mips_abicalls = FALSE;
138
139 /* This is the set of options which may be modified by the .set
140 pseudo-op. We use a struct so that .set push and .set pop are more
141 reliable. */
142
143 struct mips_set_options
144 {
145 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
146 if it has not been initialized. Changed by `.set mipsN', and the
147 -mipsN command line option, and the default CPU. */
148 int isa;
149 /* Enabled Application Specific Extensions (ASEs). These are set to -1
150 if they have not been initialized. Changed by `.set <asename>', by
151 command line options, and based on the default architecture. */
152 int ase_mips3d;
153 int ase_mdmx;
154 /* Whether we are assembling for the mips16 processor. 0 if we are
155 not, 1 if we are, and -1 if the value has not been initialized.
156 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
157 -nomips16 command line options, and the default CPU. */
158 int mips16;
159 /* Non-zero if we should not reorder instructions. Changed by `.set
160 reorder' and `.set noreorder'. */
161 int noreorder;
162 /* Non-zero if we should not permit the $at ($1) register to be used
163 in instructions. Changed by `.set at' and `.set noat'. */
164 int noat;
165 /* Non-zero if we should warn when a macro instruction expands into
166 more than one machine instruction. Changed by `.set nomacro' and
167 `.set macro'. */
168 int warn_about_macros;
169 /* Non-zero if we should not move instructions. Changed by `.set
170 move', `.set volatile', `.set nomove', and `.set novolatile'. */
171 int nomove;
172 /* Non-zero if we should not optimize branches by moving the target
173 of the branch into the delay slot. Actually, we don't perform
174 this optimization anyhow. Changed by `.set bopt' and `.set
175 nobopt'. */
176 int nobopt;
177 /* Non-zero if we should not autoextend mips16 instructions.
178 Changed by `.set autoextend' and `.set noautoextend'. */
179 int noautoextend;
180 /* Restrict general purpose registers and floating point registers
181 to 32 bit. This is initially determined when -mgp32 or -mfp32
182 is passed but can changed if the assembler code uses .set mipsN. */
183 int gp32;
184 int fp32;
185 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
186 command line option, and the default CPU. */
187 int arch;
188 };
189
190 /* True if -mgp32 was passed. */
191 static int file_mips_gp32 = -1;
192
193 /* True if -mfp32 was passed. */
194 static int file_mips_fp32 = -1;
195
196 /* This is the struct we use to hold the current set of options. Note
197 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
198 -1 to indicate that they have not been initialized. */
199
200 static struct mips_set_options mips_opts =
201 {
202 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
203 };
204
205 /* These variables are filled in with the masks of registers used.
206 The object format code reads them and puts them in the appropriate
207 place. */
208 unsigned long mips_gprmask;
209 unsigned long mips_cprmask[4];
210
211 /* MIPS ISA we are using for this output file. */
212 static int file_mips_isa = ISA_UNKNOWN;
213
214 /* True if -mips16 was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216 static int file_ase_mips16;
217
218 /* True if -mips3d was passed or implied by arguments passed on the
219 command line (e.g., by -march). */
220 static int file_ase_mips3d;
221
222 /* True if -mdmx was passed or implied by arguments passed on the
223 command line (e.g., by -march). */
224 static int file_ase_mdmx;
225
226 /* The argument of the -march= flag. The architecture we are assembling. */
227 static int file_mips_arch = CPU_UNKNOWN;
228 static const char *mips_arch_string;
229
230 /* The argument of the -mtune= flag. The architecture for which we
231 are optimizing. */
232 static int mips_tune = CPU_UNKNOWN;
233 static const char *mips_tune_string;
234
235 /* True when generating 32-bit code for a 64-bit processor. */
236 static int mips_32bitmode = 0;
237
238 /* True if the given ABI requires 32-bit registers. */
239 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
240
241 /* Likewise 64-bit registers. */
242 #define ABI_NEEDS_64BIT_REGS(ABI) \
243 ((ABI) == N32_ABI \
244 || (ABI) == N64_ABI \
245 || (ABI) == O64_ABI)
246
247 /* Return true if ISA supports 64 bit gp register instructions. */
248 #define ISA_HAS_64BIT_REGS(ISA) ( \
249 (ISA) == ISA_MIPS3 \
250 || (ISA) == ISA_MIPS4 \
251 || (ISA) == ISA_MIPS5 \
252 || (ISA) == ISA_MIPS64 \
253 || (ISA) == ISA_MIPS64R2 \
254 )
255
256 /* Return true if ISA supports 64-bit right rotate (dror et al.)
257 instructions. */
258 #define ISA_HAS_DROR(ISA) ( \
259 (ISA) == ISA_MIPS64R2 \
260 )
261
262 /* Return true if ISA supports 32-bit right rotate (ror et al.)
263 instructions. */
264 #define ISA_HAS_ROR(ISA) ( \
265 (ISA) == ISA_MIPS32R2 \
266 || (ISA) == ISA_MIPS64R2 \
267 )
268
269 #define HAVE_32BIT_GPRS \
270 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
271
272 #define HAVE_32BIT_FPRS \
273 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
274
275 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
276 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
277
278 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
279
280 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
281
282 /* We can only have 64bit addresses if the object file format
283 supports it. */
284 #define HAVE_32BIT_ADDRESSES \
285 (HAVE_32BIT_GPRS \
286 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
287 || ! HAVE_64BIT_OBJECTS) \
288 && mips_pic != EMBEDDED_PIC))
289
290 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
291
292 /* Addresses are loaded in different ways, depending on the address size
293 in use. The n32 ABI Documentation also mandates the use of additions
294 with overflow checking, but existing implementations don't follow it. */
295 #define ADDRESS_ADD_INSN \
296 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
297
298 #define ADDRESS_ADDI_INSN \
299 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
300
301 #define ADDRESS_LOAD_INSN \
302 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
303
304 #define ADDRESS_STORE_INSN \
305 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
306
307 /* Return true if the given CPU supports the MIPS16 ASE. */
308 #define CPU_HAS_MIPS16(cpu) \
309 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
310 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
311
312 /* Return true if the given CPU supports the MIPS3D ASE. */
313 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
314 )
315
316 /* Return true if the given CPU supports the MDMX ASE. */
317 #define CPU_HAS_MDMX(cpu) (FALSE \
318 )
319
320 /* True if CPU has a dror instruction. */
321 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
322
323 /* True if CPU has a ror instruction. */
324 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
325
326 /* True if mflo and mfhi can be immediately followed by instructions
327 which write to the HI and LO registers.
328
329 According to MIPS specifications, MIPS ISAs I, II, and III need
330 (at least) two instructions between the reads of HI/LO and
331 instructions which write them, and later ISAs do not. Contradicting
332 the MIPS specifications, some MIPS IV processor user manuals (e.g.
333 the UM for the NEC Vr5000) document needing the instructions between
334 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
335 MIPS64 and later ISAs to have the interlocks, plus any specific
336 earlier-ISA CPUs for which CPU documentation declares that the
337 instructions are really interlocked. */
338 #define hilo_interlocks \
339 (mips_opts.isa == ISA_MIPS32 \
340 || mips_opts.isa == ISA_MIPS32R2 \
341 || mips_opts.isa == ISA_MIPS64 \
342 || mips_opts.isa == ISA_MIPS64R2 \
343 || mips_opts.arch == CPU_R4010 \
344 || mips_opts.arch == CPU_R10000 \
345 || mips_opts.arch == CPU_R12000 \
346 || mips_opts.arch == CPU_RM7000 \
347 || mips_opts.arch == CPU_VR5500 \
348 )
349
350 /* Whether the processor uses hardware interlocks to protect reads
351 from the GPRs after they are loaded from memory, and thus does not
352 require nops to be inserted. This applies to instructions marked
353 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
354 level I. */
355 #define gpr_interlocks \
356 (mips_opts.isa != ISA_MIPS1 \
357 || mips_opts.arch == CPU_R3900)
358
359 /* Whether the processor uses hardware interlocks to avoid delays
360 required by coprocessor instructions, and thus does not require
361 nops to be inserted. This applies to instructions marked
362 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
363 between instructions marked INSN_WRITE_COND_CODE and ones marked
364 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
365 levels I, II, and III. */
366 /* Itbl support may require additional care here. */
367 #define cop_interlocks \
368 ((mips_opts.isa != ISA_MIPS1 \
369 && mips_opts.isa != ISA_MIPS2 \
370 && mips_opts.isa != ISA_MIPS3) \
371 || mips_opts.arch == CPU_R4300 \
372 )
373
374 /* Whether the processor uses hardware interlocks to protect reads
375 from coprocessor registers after they are loaded from memory, and
376 thus does not require nops to be inserted. This applies to
377 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
378 requires at MIPS ISA level I. */
379 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
380
381 /* Is this a mfhi or mflo instruction? */
382 #define MF_HILO_INSN(PINFO) \
383 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
384
385 /* MIPS PIC level. */
386
387 enum mips_pic_level mips_pic;
388
389 /* 1 if we should generate 32 bit offsets from the $gp register in
390 SVR4_PIC mode. Currently has no meaning in other modes. */
391 static int mips_big_got = 0;
392
393 /* 1 if trap instructions should used for overflow rather than break
394 instructions. */
395 static int mips_trap = 0;
396
397 /* 1 if double width floating point constants should not be constructed
398 by assembling two single width halves into two single width floating
399 point registers which just happen to alias the double width destination
400 register. On some architectures this aliasing can be disabled by a bit
401 in the status register, and the setting of this bit cannot be determined
402 automatically at assemble time. */
403 static int mips_disable_float_construction;
404
405 /* Non-zero if any .set noreorder directives were used. */
406
407 static int mips_any_noreorder;
408
409 /* Non-zero if nops should be inserted when the register referenced in
410 an mfhi/mflo instruction is read in the next two instructions. */
411 static int mips_7000_hilo_fix;
412
413 /* The size of the small data section. */
414 static unsigned int g_switch_value = 8;
415 /* Whether the -G option was used. */
416 static int g_switch_seen = 0;
417
418 #define N_RMASK 0xc4
419 #define N_VFP 0xd4
420
421 /* If we can determine in advance that GP optimization won't be
422 possible, we can skip the relaxation stuff that tries to produce
423 GP-relative references. This makes delay slot optimization work
424 better.
425
426 This function can only provide a guess, but it seems to work for
427 gcc output. It needs to guess right for gcc, otherwise gcc
428 will put what it thinks is a GP-relative instruction in a branch
429 delay slot.
430
431 I don't know if a fix is needed for the SVR4_PIC mode. I've only
432 fixed it for the non-PIC mode. KR 95/04/07 */
433 static int nopic_need_relax (symbolS *, int);
434
435 /* handle of the OPCODE hash table */
436 static struct hash_control *op_hash = NULL;
437
438 /* The opcode hash table we use for the mips16. */
439 static struct hash_control *mips16_op_hash = NULL;
440
441 /* This array holds the chars that always start a comment. If the
442 pre-processor is disabled, these aren't very useful */
443 const char comment_chars[] = "#";
444
445 /* This array holds the chars that only start a comment at the beginning of
446 a line. If the line seems to have the form '# 123 filename'
447 .line and .file directives will appear in the pre-processed output */
448 /* Note that input_file.c hand checks for '#' at the beginning of the
449 first line of the input file. This is because the compiler outputs
450 #NO_APP at the beginning of its output. */
451 /* Also note that C style comments are always supported. */
452 const char line_comment_chars[] = "#";
453
454 /* This array holds machine specific line separator characters. */
455 const char line_separator_chars[] = ";";
456
457 /* Chars that can be used to separate mant from exp in floating point nums */
458 const char EXP_CHARS[] = "eE";
459
460 /* Chars that mean this number is a floating point constant */
461 /* As in 0f12.456 */
462 /* or 0d1.2345e12 */
463 const char FLT_CHARS[] = "rRsSfFdDxXpP";
464
465 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
466 changed in read.c . Ideally it shouldn't have to know about it at all,
467 but nothing is ideal around here.
468 */
469
470 static char *insn_error;
471
472 static int auto_align = 1;
473
474 /* When outputting SVR4 PIC code, the assembler needs to know the
475 offset in the stack frame from which to restore the $gp register.
476 This is set by the .cprestore pseudo-op, and saved in this
477 variable. */
478 static offsetT mips_cprestore_offset = -1;
479
480 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
481 more optimizations, it can use a register value instead of a memory-saved
482 offset and even an other register than $gp as global pointer. */
483 static offsetT mips_cpreturn_offset = -1;
484 static int mips_cpreturn_register = -1;
485 static int mips_gp_register = GP;
486 static int mips_gprel_offset = 0;
487
488 /* Whether mips_cprestore_offset has been set in the current function
489 (or whether it has already been warned about, if not). */
490 static int mips_cprestore_valid = 0;
491
492 /* This is the register which holds the stack frame, as set by the
493 .frame pseudo-op. This is needed to implement .cprestore. */
494 static int mips_frame_reg = SP;
495
496 /* Whether mips_frame_reg has been set in the current function
497 (or whether it has already been warned about, if not). */
498 static int mips_frame_reg_valid = 0;
499
500 /* To output NOP instructions correctly, we need to keep information
501 about the previous two instructions. */
502
503 /* Whether we are optimizing. The default value of 2 means to remove
504 unneeded NOPs and swap branch instructions when possible. A value
505 of 1 means to not swap branches. A value of 0 means to always
506 insert NOPs. */
507 static int mips_optimize = 2;
508
509 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
510 equivalent to seeing no -g option at all. */
511 static int mips_debug = 0;
512
513 /* The previous instruction. */
514 static struct mips_cl_insn prev_insn;
515
516 /* The instruction before prev_insn. */
517 static struct mips_cl_insn prev_prev_insn;
518
519 /* If we don't want information for prev_insn or prev_prev_insn, we
520 point the insn_mo field at this dummy integer. */
521 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
522
523 /* Non-zero if prev_insn is valid. */
524 static int prev_insn_valid;
525
526 /* The frag for the previous instruction. */
527 static struct frag *prev_insn_frag;
528
529 /* The offset into prev_insn_frag for the previous instruction. */
530 static long prev_insn_where;
531
532 /* The reloc type for the previous instruction, if any. */
533 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
534
535 /* The reloc for the previous instruction, if any. */
536 static fixS *prev_insn_fixp[3];
537
538 /* Non-zero if the previous instruction was in a delay slot. */
539 static int prev_insn_is_delay_slot;
540
541 /* Non-zero if the previous instruction was in a .set noreorder. */
542 static int prev_insn_unreordered;
543
544 /* Non-zero if the previous instruction uses an extend opcode (if
545 mips16). */
546 static int prev_insn_extended;
547
548 /* Non-zero if the previous previous instruction was in a .set
549 noreorder. */
550 static int prev_prev_insn_unreordered;
551
552 /* If this is set, it points to a frag holding nop instructions which
553 were inserted before the start of a noreorder section. If those
554 nops turn out to be unnecessary, the size of the frag can be
555 decreased. */
556 static fragS *prev_nop_frag;
557
558 /* The number of nop instructions we created in prev_nop_frag. */
559 static int prev_nop_frag_holds;
560
561 /* The number of nop instructions that we know we need in
562 prev_nop_frag. */
563 static int prev_nop_frag_required;
564
565 /* The number of instructions we've seen since prev_nop_frag. */
566 static int prev_nop_frag_since;
567
568 /* For ECOFF and ELF, relocations against symbols are done in two
569 parts, with a HI relocation and a LO relocation. Each relocation
570 has only 16 bits of space to store an addend. This means that in
571 order for the linker to handle carries correctly, it must be able
572 to locate both the HI and the LO relocation. This means that the
573 relocations must appear in order in the relocation table.
574
575 In order to implement this, we keep track of each unmatched HI
576 relocation. We then sort them so that they immediately precede the
577 corresponding LO relocation. */
578
579 struct mips_hi_fixup
580 {
581 /* Next HI fixup. */
582 struct mips_hi_fixup *next;
583 /* This fixup. */
584 fixS *fixp;
585 /* The section this fixup is in. */
586 segT seg;
587 };
588
589 /* The list of unmatched HI relocs. */
590
591 static struct mips_hi_fixup *mips_hi_fixup_list;
592
593 /* The frag containing the last explicit relocation operator.
594 Null if explicit relocations have not been used. */
595
596 static fragS *prev_reloc_op_frag;
597
598 /* Map normal MIPS register numbers to mips16 register numbers. */
599
600 #define X ILLEGAL_REG
601 static const int mips32_to_16_reg_map[] =
602 {
603 X, X, 2, 3, 4, 5, 6, 7,
604 X, X, X, X, X, X, X, X,
605 0, 1, X, X, X, X, X, X,
606 X, X, X, X, X, X, X, X
607 };
608 #undef X
609
610 /* Map mips16 register numbers to normal MIPS register numbers. */
611
612 static const unsigned int mips16_to_32_reg_map[] =
613 {
614 16, 17, 2, 3, 4, 5, 6, 7
615 };
616
617 static int mips_fix_vr4120;
618
619 /* We don't relax branches by default, since this causes us to expand
620 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
621 fail to compute the offset before expanding the macro to the most
622 efficient expansion. */
623
624 static int mips_relax_branch;
625 \f
626 /* The expansion of many macros depends on the type of symbol that
627 they refer to. For example, when generating position-dependent code,
628 a macro that refers to a symbol may have two different expansions,
629 one which uses GP-relative addresses and one which uses absolute
630 addresses. When generating SVR4-style PIC, a macro may have
631 different expansions for local and global symbols.
632
633 We handle these situations by generating both sequences and putting
634 them in variant frags. In position-dependent code, the first sequence
635 will be the GP-relative one and the second sequence will be the
636 absolute one. In SVR4 PIC, the first sequence will be for global
637 symbols and the second will be for local symbols.
638
639 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
640 SECOND are the lengths of the two sequences in bytes. These fields
641 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
642 the subtype has the following flags:
643
644 RELAX_USE_SECOND
645 Set if it has been decided that we should use the second
646 sequence instead of the first.
647
648 RELAX_SECOND_LONGER
649 Set in the first variant frag if the macro's second implementation
650 is longer than its first. This refers to the macro as a whole,
651 not an individual relaxation.
652
653 RELAX_NOMACRO
654 Set in the first variant frag if the macro appeared in a .set nomacro
655 block and if one alternative requires a warning but the other does not.
656
657 RELAX_DELAY_SLOT
658 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
659 delay slot.
660
661 The frag's "opcode" points to the first fixup for relaxable code.
662
663 Relaxable macros are generated using a sequence such as:
664
665 relax_start (SYMBOL);
666 ... generate first expansion ...
667 relax_switch ();
668 ... generate second expansion ...
669 relax_end ();
670
671 The code and fixups for the unwanted alternative are discarded
672 by md_convert_frag. */
673 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
674
675 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
676 #define RELAX_SECOND(X) ((X) & 0xff)
677 #define RELAX_USE_SECOND 0x10000
678 #define RELAX_SECOND_LONGER 0x20000
679 #define RELAX_NOMACRO 0x40000
680 #define RELAX_DELAY_SLOT 0x80000
681
682 /* Branch without likely bit. If label is out of range, we turn:
683
684 beq reg1, reg2, label
685 delay slot
686
687 into
688
689 bne reg1, reg2, 0f
690 nop
691 j label
692 0: delay slot
693
694 with the following opcode replacements:
695
696 beq <-> bne
697 blez <-> bgtz
698 bltz <-> bgez
699 bc1f <-> bc1t
700
701 bltzal <-> bgezal (with jal label instead of j label)
702
703 Even though keeping the delay slot instruction in the delay slot of
704 the branch would be more efficient, it would be very tricky to do
705 correctly, because we'd have to introduce a variable frag *after*
706 the delay slot instruction, and expand that instead. Let's do it
707 the easy way for now, even if the branch-not-taken case now costs
708 one additional instruction. Out-of-range branches are not supposed
709 to be common, anyway.
710
711 Branch likely. If label is out of range, we turn:
712
713 beql reg1, reg2, label
714 delay slot (annulled if branch not taken)
715
716 into
717
718 beql reg1, reg2, 1f
719 nop
720 beql $0, $0, 2f
721 nop
722 1: j[al] label
723 delay slot (executed only if branch taken)
724 2:
725
726 It would be possible to generate a shorter sequence by losing the
727 likely bit, generating something like:
728
729 bne reg1, reg2, 0f
730 nop
731 j[al] label
732 delay slot (executed only if branch taken)
733 0:
734
735 beql -> bne
736 bnel -> beq
737 blezl -> bgtz
738 bgtzl -> blez
739 bltzl -> bgez
740 bgezl -> bltz
741 bc1fl -> bc1t
742 bc1tl -> bc1f
743
744 bltzall -> bgezal (with jal label instead of j label)
745 bgezall -> bltzal (ditto)
746
747
748 but it's not clear that it would actually improve performance. */
749 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
750 ((relax_substateT) \
751 (0xc0000000 \
752 | ((toofar) ? 1 : 0) \
753 | ((link) ? 2 : 0) \
754 | ((likely) ? 4 : 0) \
755 | ((uncond) ? 8 : 0)))
756 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
757 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
758 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
759 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
760 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
761
762 /* For mips16 code, we use an entirely different form of relaxation.
763 mips16 supports two versions of most instructions which take
764 immediate values: a small one which takes some small value, and a
765 larger one which takes a 16 bit value. Since branches also follow
766 this pattern, relaxing these values is required.
767
768 We can assemble both mips16 and normal MIPS code in a single
769 object. Therefore, we need to support this type of relaxation at
770 the same time that we support the relaxation described above. We
771 use the high bit of the subtype field to distinguish these cases.
772
773 The information we store for this type of relaxation is the
774 argument code found in the opcode file for this relocation, whether
775 the user explicitly requested a small or extended form, and whether
776 the relocation is in a jump or jal delay slot. That tells us the
777 size of the value, and how it should be stored. We also store
778 whether the fragment is considered to be extended or not. We also
779 store whether this is known to be a branch to a different section,
780 whether we have tried to relax this frag yet, and whether we have
781 ever extended a PC relative fragment because of a shift count. */
782 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
783 (0x80000000 \
784 | ((type) & 0xff) \
785 | ((small) ? 0x100 : 0) \
786 | ((ext) ? 0x200 : 0) \
787 | ((dslot) ? 0x400 : 0) \
788 | ((jal_dslot) ? 0x800 : 0))
789 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
790 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
791 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
792 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
793 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
794 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
795 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
796 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
797 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
798 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
799 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
800 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
801
802 /* Is the given value a sign-extended 32-bit value? */
803 #define IS_SEXT_32BIT_NUM(x) \
804 (((x) &~ (offsetT) 0x7fffffff) == 0 \
805 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
806
807 /* Is the given value a sign-extended 16-bit value? */
808 #define IS_SEXT_16BIT_NUM(x) \
809 (((x) &~ (offsetT) 0x7fff) == 0 \
810 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
811
812 \f
813 /* Global variables used when generating relaxable macros. See the
814 comment above RELAX_ENCODE for more details about how relaxation
815 is used. */
816 static struct {
817 /* 0 if we're not emitting a relaxable macro.
818 1 if we're emitting the first of the two relaxation alternatives.
819 2 if we're emitting the second alternative. */
820 int sequence;
821
822 /* The first relaxable fixup in the current frag. (In other words,
823 the first fixup that refers to relaxable code.) */
824 fixS *first_fixup;
825
826 /* sizes[0] says how many bytes of the first alternative are stored in
827 the current frag. Likewise sizes[1] for the second alternative. */
828 unsigned int sizes[2];
829
830 /* The symbol on which the choice of sequence depends. */
831 symbolS *symbol;
832 } mips_relax;
833 \f
834 /* Global variables used to decide whether a macro needs a warning. */
835 static struct {
836 /* True if the macro is in a branch delay slot. */
837 bfd_boolean delay_slot_p;
838
839 /* For relaxable macros, sizes[0] is the length of the first alternative
840 in bytes and sizes[1] is the length of the second alternative.
841 For non-relaxable macros, both elements give the length of the
842 macro in bytes. */
843 unsigned int sizes[2];
844
845 /* The first variant frag for this macro. */
846 fragS *first_frag;
847 } mips_macro_warning;
848 \f
849 /* Prototypes for static functions. */
850
851 #define internalError() \
852 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
853
854 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
855
856 static void append_insn
857 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
858 static void mips_no_prev_insn (int);
859 static void mips16_macro_build
860 (expressionS *, const char *, const char *, va_list);
861 static void load_register (int, expressionS *, int);
862 static void macro_start (void);
863 static void macro_end (void);
864 static void macro (struct mips_cl_insn * ip);
865 static void mips16_macro (struct mips_cl_insn * ip);
866 #ifdef LOSING_COMPILER
867 static void macro2 (struct mips_cl_insn * ip);
868 #endif
869 static void mips_ip (char *str, struct mips_cl_insn * ip);
870 static void mips16_ip (char *str, struct mips_cl_insn * ip);
871 static void mips16_immed
872 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
873 unsigned long *, bfd_boolean *, unsigned short *);
874 static size_t my_getSmallExpression
875 (expressionS *, bfd_reloc_code_real_type *, char *);
876 static void my_getExpression (expressionS *, char *);
877 static void s_align (int);
878 static void s_change_sec (int);
879 static void s_change_section (int);
880 static void s_cons (int);
881 static void s_float_cons (int);
882 static void s_mips_globl (int);
883 static void s_option (int);
884 static void s_mipsset (int);
885 static void s_abicalls (int);
886 static void s_cpload (int);
887 static void s_cpsetup (int);
888 static void s_cplocal (int);
889 static void s_cprestore (int);
890 static void s_cpreturn (int);
891 static void s_gpvalue (int);
892 static void s_gpword (int);
893 static void s_gpdword (int);
894 static void s_cpadd (int);
895 static void s_insn (int);
896 static void md_obj_begin (void);
897 static void md_obj_end (void);
898 static void s_mips_ent (int);
899 static void s_mips_end (int);
900 static void s_mips_frame (int);
901 static void s_mips_mask (int reg_type);
902 static void s_mips_stab (int);
903 static void s_mips_weakext (int);
904 static void s_mips_file (int);
905 static void s_mips_loc (int);
906 static bfd_boolean pic_need_relax (symbolS *, asection *);
907 static int relaxed_branch_length (fragS *, asection *, int);
908 static int validate_mips_insn (const struct mips_opcode *);
909
910 /* Table and functions used to map between CPU/ISA names, and
911 ISA levels, and CPU numbers. */
912
913 struct mips_cpu_info
914 {
915 const char *name; /* CPU or ISA name. */
916 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
917 int isa; /* ISA level. */
918 int cpu; /* CPU number (default CPU if ISA). */
919 };
920
921 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
922 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
923 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
924 \f
925 /* Pseudo-op table.
926
927 The following pseudo-ops from the Kane and Heinrich MIPS book
928 should be defined here, but are currently unsupported: .alias,
929 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
930
931 The following pseudo-ops from the Kane and Heinrich MIPS book are
932 specific to the type of debugging information being generated, and
933 should be defined by the object format: .aent, .begin, .bend,
934 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
935 .vreg.
936
937 The following pseudo-ops from the Kane and Heinrich MIPS book are
938 not MIPS CPU specific, but are also not specific to the object file
939 format. This file is probably the best place to define them, but
940 they are not currently supported: .asm0, .endr, .lab, .repeat,
941 .struct. */
942
943 static const pseudo_typeS mips_pseudo_table[] =
944 {
945 /* MIPS specific pseudo-ops. */
946 {"option", s_option, 0},
947 {"set", s_mipsset, 0},
948 {"rdata", s_change_sec, 'r'},
949 {"sdata", s_change_sec, 's'},
950 {"livereg", s_ignore, 0},
951 {"abicalls", s_abicalls, 0},
952 {"cpload", s_cpload, 0},
953 {"cpsetup", s_cpsetup, 0},
954 {"cplocal", s_cplocal, 0},
955 {"cprestore", s_cprestore, 0},
956 {"cpreturn", s_cpreturn, 0},
957 {"gpvalue", s_gpvalue, 0},
958 {"gpword", s_gpword, 0},
959 {"gpdword", s_gpdword, 0},
960 {"cpadd", s_cpadd, 0},
961 {"insn", s_insn, 0},
962
963 /* Relatively generic pseudo-ops that happen to be used on MIPS
964 chips. */
965 {"asciiz", stringer, 1},
966 {"bss", s_change_sec, 'b'},
967 {"err", s_err, 0},
968 {"half", s_cons, 1},
969 {"dword", s_cons, 3},
970 {"weakext", s_mips_weakext, 0},
971
972 /* These pseudo-ops are defined in read.c, but must be overridden
973 here for one reason or another. */
974 {"align", s_align, 0},
975 {"byte", s_cons, 0},
976 {"data", s_change_sec, 'd'},
977 {"double", s_float_cons, 'd'},
978 {"float", s_float_cons, 'f'},
979 {"globl", s_mips_globl, 0},
980 {"global", s_mips_globl, 0},
981 {"hword", s_cons, 1},
982 {"int", s_cons, 2},
983 {"long", s_cons, 2},
984 {"octa", s_cons, 4},
985 {"quad", s_cons, 3},
986 {"section", s_change_section, 0},
987 {"short", s_cons, 1},
988 {"single", s_float_cons, 'f'},
989 {"stabn", s_mips_stab, 'n'},
990 {"text", s_change_sec, 't'},
991 {"word", s_cons, 2},
992
993 { "extern", ecoff_directive_extern, 0},
994
995 { NULL, NULL, 0 },
996 };
997
998 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
999 {
1000 /* These pseudo-ops should be defined by the object file format.
1001 However, a.out doesn't support them, so we have versions here. */
1002 {"aent", s_mips_ent, 1},
1003 {"bgnb", s_ignore, 0},
1004 {"end", s_mips_end, 0},
1005 {"endb", s_ignore, 0},
1006 {"ent", s_mips_ent, 0},
1007 {"file", s_mips_file, 0},
1008 {"fmask", s_mips_mask, 'F'},
1009 {"frame", s_mips_frame, 0},
1010 {"loc", s_mips_loc, 0},
1011 {"mask", s_mips_mask, 'R'},
1012 {"verstamp", s_ignore, 0},
1013 { NULL, NULL, 0 },
1014 };
1015
1016 extern void pop_insert (const pseudo_typeS *);
1017
1018 void
1019 mips_pop_insert (void)
1020 {
1021 pop_insert (mips_pseudo_table);
1022 if (! ECOFF_DEBUGGING)
1023 pop_insert (mips_nonecoff_pseudo_table);
1024 }
1025 \f
1026 /* Symbols labelling the current insn. */
1027
1028 struct insn_label_list
1029 {
1030 struct insn_label_list *next;
1031 symbolS *label;
1032 };
1033
1034 static struct insn_label_list *insn_labels;
1035 static struct insn_label_list *free_insn_labels;
1036
1037 static void mips_clear_insn_labels (void);
1038
1039 static inline void
1040 mips_clear_insn_labels (void)
1041 {
1042 register struct insn_label_list **pl;
1043
1044 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1045 ;
1046 *pl = insn_labels;
1047 insn_labels = NULL;
1048 }
1049 \f
1050 static char *expr_end;
1051
1052 /* Expressions which appear in instructions. These are set by
1053 mips_ip. */
1054
1055 static expressionS imm_expr;
1056 static expressionS imm2_expr;
1057 static expressionS offset_expr;
1058
1059 /* Relocs associated with imm_expr and offset_expr. */
1060
1061 static bfd_reloc_code_real_type imm_reloc[3]
1062 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1063 static bfd_reloc_code_real_type offset_reloc[3]
1064 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1065
1066 /* These are set by mips16_ip if an explicit extension is used. */
1067
1068 static bfd_boolean mips16_small, mips16_ext;
1069
1070 #ifdef OBJ_ELF
1071 /* The pdr segment for per procedure frame/regmask info. Not used for
1072 ECOFF debugging. */
1073
1074 static segT pdr_seg;
1075 #endif
1076
1077 /* The default target format to use. */
1078
1079 const char *
1080 mips_target_format (void)
1081 {
1082 switch (OUTPUT_FLAVOR)
1083 {
1084 case bfd_target_ecoff_flavour:
1085 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1086 case bfd_target_coff_flavour:
1087 return "pe-mips";
1088 case bfd_target_elf_flavour:
1089 #ifdef TE_TMIPS
1090 /* This is traditional mips. */
1091 return (target_big_endian
1092 ? (HAVE_64BIT_OBJECTS
1093 ? "elf64-tradbigmips"
1094 : (HAVE_NEWABI
1095 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1096 : (HAVE_64BIT_OBJECTS
1097 ? "elf64-tradlittlemips"
1098 : (HAVE_NEWABI
1099 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1100 #else
1101 return (target_big_endian
1102 ? (HAVE_64BIT_OBJECTS
1103 ? "elf64-bigmips"
1104 : (HAVE_NEWABI
1105 ? "elf32-nbigmips" : "elf32-bigmips"))
1106 : (HAVE_64BIT_OBJECTS
1107 ? "elf64-littlemips"
1108 : (HAVE_NEWABI
1109 ? "elf32-nlittlemips" : "elf32-littlemips")));
1110 #endif
1111 default:
1112 abort ();
1113 return NULL;
1114 }
1115 }
1116
1117 /* This function is called once, at assembler startup time. It should
1118 set up all the tables, etc. that the MD part of the assembler will need. */
1119
1120 void
1121 md_begin (void)
1122 {
1123 register const char *retval = NULL;
1124 int i = 0;
1125 int broken = 0;
1126
1127 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1128 as_warn (_("Could not set architecture and machine"));
1129
1130 op_hash = hash_new ();
1131
1132 for (i = 0; i < NUMOPCODES;)
1133 {
1134 const char *name = mips_opcodes[i].name;
1135
1136 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1137 if (retval != NULL)
1138 {
1139 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1140 mips_opcodes[i].name, retval);
1141 /* Probably a memory allocation problem? Give up now. */
1142 as_fatal (_("Broken assembler. No assembly attempted."));
1143 }
1144 do
1145 {
1146 if (mips_opcodes[i].pinfo != INSN_MACRO)
1147 {
1148 if (!validate_mips_insn (&mips_opcodes[i]))
1149 broken = 1;
1150 }
1151 ++i;
1152 }
1153 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1154 }
1155
1156 mips16_op_hash = hash_new ();
1157
1158 i = 0;
1159 while (i < bfd_mips16_num_opcodes)
1160 {
1161 const char *name = mips16_opcodes[i].name;
1162
1163 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1164 if (retval != NULL)
1165 as_fatal (_("internal: can't hash `%s': %s"),
1166 mips16_opcodes[i].name, retval);
1167 do
1168 {
1169 if (mips16_opcodes[i].pinfo != INSN_MACRO
1170 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1171 != mips16_opcodes[i].match))
1172 {
1173 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1174 mips16_opcodes[i].name, mips16_opcodes[i].args);
1175 broken = 1;
1176 }
1177 ++i;
1178 }
1179 while (i < bfd_mips16_num_opcodes
1180 && strcmp (mips16_opcodes[i].name, name) == 0);
1181 }
1182
1183 if (broken)
1184 as_fatal (_("Broken assembler. No assembly attempted."));
1185
1186 /* We add all the general register names to the symbol table. This
1187 helps us detect invalid uses of them. */
1188 for (i = 0; i < 32; i++)
1189 {
1190 char buf[5];
1191
1192 sprintf (buf, "$%d", i);
1193 symbol_table_insert (symbol_new (buf, reg_section, i,
1194 &zero_address_frag));
1195 }
1196 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1197 &zero_address_frag));
1198 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1199 &zero_address_frag));
1200 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1201 &zero_address_frag));
1202 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1203 &zero_address_frag));
1204 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1205 &zero_address_frag));
1206 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1207 &zero_address_frag));
1208 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1209 &zero_address_frag));
1210 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1211 &zero_address_frag));
1212 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1213 &zero_address_frag));
1214
1215 /* If we don't add these register names to the symbol table, they
1216 may end up being added as regular symbols by operand(), and then
1217 make it to the object file as undefined in case they're not
1218 regarded as local symbols. They're local in o32, since `$' is a
1219 local symbol prefix, but not in n32 or n64. */
1220 for (i = 0; i < 8; i++)
1221 {
1222 char buf[6];
1223
1224 sprintf (buf, "$fcc%i", i);
1225 symbol_table_insert (symbol_new (buf, reg_section, -1,
1226 &zero_address_frag));
1227 }
1228
1229 mips_no_prev_insn (FALSE);
1230
1231 mips_gprmask = 0;
1232 mips_cprmask[0] = 0;
1233 mips_cprmask[1] = 0;
1234 mips_cprmask[2] = 0;
1235 mips_cprmask[3] = 0;
1236
1237 /* set the default alignment for the text section (2**2) */
1238 record_alignment (text_section, 2);
1239
1240 bfd_set_gp_size (stdoutput, g_switch_value);
1241
1242 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1243 {
1244 /* On a native system, sections must be aligned to 16 byte
1245 boundaries. When configured for an embedded ELF target, we
1246 don't bother. */
1247 if (strcmp (TARGET_OS, "elf") != 0)
1248 {
1249 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1250 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1251 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1252 }
1253
1254 /* Create a .reginfo section for register masks and a .mdebug
1255 section for debugging information. */
1256 {
1257 segT seg;
1258 subsegT subseg;
1259 flagword flags;
1260 segT sec;
1261
1262 seg = now_seg;
1263 subseg = now_subseg;
1264
1265 /* The ABI says this section should be loaded so that the
1266 running program can access it. However, we don't load it
1267 if we are configured for an embedded target */
1268 flags = SEC_READONLY | SEC_DATA;
1269 if (strcmp (TARGET_OS, "elf") != 0)
1270 flags |= SEC_ALLOC | SEC_LOAD;
1271
1272 if (mips_abi != N64_ABI)
1273 {
1274 sec = subseg_new (".reginfo", (subsegT) 0);
1275
1276 bfd_set_section_flags (stdoutput, sec, flags);
1277 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1278
1279 #ifdef OBJ_ELF
1280 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1281 #endif
1282 }
1283 else
1284 {
1285 /* The 64-bit ABI uses a .MIPS.options section rather than
1286 .reginfo section. */
1287 sec = subseg_new (".MIPS.options", (subsegT) 0);
1288 bfd_set_section_flags (stdoutput, sec, flags);
1289 bfd_set_section_alignment (stdoutput, sec, 3);
1290
1291 #ifdef OBJ_ELF
1292 /* Set up the option header. */
1293 {
1294 Elf_Internal_Options opthdr;
1295 char *f;
1296
1297 opthdr.kind = ODK_REGINFO;
1298 opthdr.size = (sizeof (Elf_External_Options)
1299 + sizeof (Elf64_External_RegInfo));
1300 opthdr.section = 0;
1301 opthdr.info = 0;
1302 f = frag_more (sizeof (Elf_External_Options));
1303 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1304 (Elf_External_Options *) f);
1305
1306 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1307 }
1308 #endif
1309 }
1310
1311 if (ECOFF_DEBUGGING)
1312 {
1313 sec = subseg_new (".mdebug", (subsegT) 0);
1314 (void) bfd_set_section_flags (stdoutput, sec,
1315 SEC_HAS_CONTENTS | SEC_READONLY);
1316 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1317 }
1318 #ifdef OBJ_ELF
1319 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1320 {
1321 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1322 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1323 SEC_READONLY | SEC_RELOC
1324 | SEC_DEBUGGING);
1325 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1326 }
1327 #endif
1328
1329 subseg_set (seg, subseg);
1330 }
1331 }
1332
1333 if (! ECOFF_DEBUGGING)
1334 md_obj_begin ();
1335 }
1336
1337 void
1338 md_mips_end (void)
1339 {
1340 if (! ECOFF_DEBUGGING)
1341 md_obj_end ();
1342 }
1343
1344 void
1345 md_assemble (char *str)
1346 {
1347 struct mips_cl_insn insn;
1348 bfd_reloc_code_real_type unused_reloc[3]
1349 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1350
1351 imm_expr.X_op = O_absent;
1352 imm2_expr.X_op = O_absent;
1353 offset_expr.X_op = O_absent;
1354 imm_reloc[0] = BFD_RELOC_UNUSED;
1355 imm_reloc[1] = BFD_RELOC_UNUSED;
1356 imm_reloc[2] = BFD_RELOC_UNUSED;
1357 offset_reloc[0] = BFD_RELOC_UNUSED;
1358 offset_reloc[1] = BFD_RELOC_UNUSED;
1359 offset_reloc[2] = BFD_RELOC_UNUSED;
1360
1361 if (mips_opts.mips16)
1362 mips16_ip (str, &insn);
1363 else
1364 {
1365 mips_ip (str, &insn);
1366 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1367 str, insn.insn_opcode));
1368 }
1369
1370 if (insn_error)
1371 {
1372 as_bad ("%s `%s'", insn_error, str);
1373 return;
1374 }
1375
1376 if (insn.insn_mo->pinfo == INSN_MACRO)
1377 {
1378 macro_start ();
1379 if (mips_opts.mips16)
1380 mips16_macro (&insn);
1381 else
1382 macro (&insn);
1383 macro_end ();
1384 }
1385 else
1386 {
1387 if (imm_expr.X_op != O_absent)
1388 append_insn (&insn, &imm_expr, imm_reloc);
1389 else if (offset_expr.X_op != O_absent)
1390 append_insn (&insn, &offset_expr, offset_reloc);
1391 else
1392 append_insn (&insn, NULL, unused_reloc);
1393 }
1394 }
1395
1396 /* Return true if the given relocation might need a matching %lo().
1397 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1398 applied to local symbols. */
1399
1400 static inline bfd_boolean
1401 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1402 {
1403 return (reloc == BFD_RELOC_HI16_S
1404 || reloc == BFD_RELOC_MIPS_GOT16);
1405 }
1406
1407 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1408 relocation. */
1409
1410 static inline bfd_boolean
1411 fixup_has_matching_lo_p (fixS *fixp)
1412 {
1413 return (fixp->fx_next != NULL
1414 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1415 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1416 && fixp->fx_offset == fixp->fx_next->fx_offset);
1417 }
1418
1419 /* See whether instruction IP reads register REG. CLASS is the type
1420 of register. */
1421
1422 static int
1423 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1424 enum mips_regclass class)
1425 {
1426 if (class == MIPS16_REG)
1427 {
1428 assert (mips_opts.mips16);
1429 reg = mips16_to_32_reg_map[reg];
1430 class = MIPS_GR_REG;
1431 }
1432
1433 /* Don't report on general register ZERO, since it never changes. */
1434 if (class == MIPS_GR_REG && reg == ZERO)
1435 return 0;
1436
1437 if (class == MIPS_FP_REG)
1438 {
1439 assert (! mips_opts.mips16);
1440 /* If we are called with either $f0 or $f1, we must check $f0.
1441 This is not optimal, because it will introduce an unnecessary
1442 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1443 need to distinguish reading both $f0 and $f1 or just one of
1444 them. Note that we don't have to check the other way,
1445 because there is no instruction that sets both $f0 and $f1
1446 and requires a delay. */
1447 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1448 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1449 == (reg &~ (unsigned) 1)))
1450 return 1;
1451 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1452 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1453 == (reg &~ (unsigned) 1)))
1454 return 1;
1455 }
1456 else if (! mips_opts.mips16)
1457 {
1458 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1459 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1460 return 1;
1461 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1462 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1463 return 1;
1464 }
1465 else
1466 {
1467 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1468 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1469 & MIPS16OP_MASK_RX)]
1470 == reg))
1471 return 1;
1472 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1473 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1474 & MIPS16OP_MASK_RY)]
1475 == reg))
1476 return 1;
1477 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1478 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1479 & MIPS16OP_MASK_MOVE32Z)]
1480 == reg))
1481 return 1;
1482 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1483 return 1;
1484 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1485 return 1;
1486 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1487 return 1;
1488 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1489 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1490 & MIPS16OP_MASK_REGR32) == reg)
1491 return 1;
1492 }
1493
1494 return 0;
1495 }
1496
1497 /* This function returns true if modifying a register requires a
1498 delay. */
1499
1500 static int
1501 reg_needs_delay (unsigned int reg)
1502 {
1503 unsigned long prev_pinfo;
1504
1505 prev_pinfo = prev_insn.insn_mo->pinfo;
1506 if (! mips_opts.noreorder
1507 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1508 && ! gpr_interlocks)
1509 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1510 && ! cop_interlocks)))
1511 {
1512 /* A load from a coprocessor or from memory. All load delays
1513 delay the use of general register rt for one instruction. */
1514 /* Itbl support may require additional care here. */
1515 know (prev_pinfo & INSN_WRITE_GPR_T);
1516 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1517 return 1;
1518 }
1519
1520 return 0;
1521 }
1522
1523 /* Mark instruction labels in mips16 mode. This permits the linker to
1524 handle them specially, such as generating jalx instructions when
1525 needed. We also make them odd for the duration of the assembly, in
1526 order to generate the right sort of code. We will make them even
1527 in the adjust_symtab routine, while leaving them marked. This is
1528 convenient for the debugger and the disassembler. The linker knows
1529 to make them odd again. */
1530
1531 static void
1532 mips16_mark_labels (void)
1533 {
1534 if (mips_opts.mips16)
1535 {
1536 struct insn_label_list *l;
1537 valueT val;
1538
1539 for (l = insn_labels; l != NULL; l = l->next)
1540 {
1541 #ifdef OBJ_ELF
1542 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1543 S_SET_OTHER (l->label, STO_MIPS16);
1544 #endif
1545 val = S_GET_VALUE (l->label);
1546 if ((val & 1) == 0)
1547 S_SET_VALUE (l->label, val + 1);
1548 }
1549 }
1550 }
1551
1552 /* End the current frag. Make it a variant frag and record the
1553 relaxation info. */
1554
1555 static void
1556 relax_close_frag (void)
1557 {
1558 mips_macro_warning.first_frag = frag_now;
1559 frag_var (rs_machine_dependent, 0, 0,
1560 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1561 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1562
1563 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1564 mips_relax.first_fixup = 0;
1565 }
1566
1567 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1568 See the comment above RELAX_ENCODE for more details. */
1569
1570 static void
1571 relax_start (symbolS *symbol)
1572 {
1573 assert (mips_relax.sequence == 0);
1574 mips_relax.sequence = 1;
1575 mips_relax.symbol = symbol;
1576 }
1577
1578 /* Start generating the second version of a relaxable sequence.
1579 See the comment above RELAX_ENCODE for more details. */
1580
1581 static void
1582 relax_switch (void)
1583 {
1584 assert (mips_relax.sequence == 1);
1585 mips_relax.sequence = 2;
1586 }
1587
1588 /* End the current relaxable sequence. */
1589
1590 static void
1591 relax_end (void)
1592 {
1593 assert (mips_relax.sequence == 2);
1594 relax_close_frag ();
1595 mips_relax.sequence = 0;
1596 }
1597
1598 /* Output an instruction. IP is the instruction information.
1599 ADDRESS_EXPR is an operand of the instruction to be used with
1600 RELOC_TYPE. */
1601
1602 static void
1603 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
1604 bfd_reloc_code_real_type *reloc_type)
1605 {
1606 register unsigned long prev_pinfo, pinfo;
1607 char *f;
1608 fixS *fixp[3];
1609 int nops = 0;
1610 relax_stateT prev_insn_frag_type = 0;
1611 bfd_boolean relaxed_branch = FALSE;
1612 bfd_boolean force_new_frag = FALSE;
1613
1614 /* Mark instruction labels in mips16 mode. */
1615 mips16_mark_labels ();
1616
1617 prev_pinfo = prev_insn.insn_mo->pinfo;
1618 pinfo = ip->insn_mo->pinfo;
1619
1620 if (mips_relax.sequence != 2
1621 && (!mips_opts.noreorder || prev_nop_frag != NULL))
1622 {
1623 int prev_prev_nop;
1624
1625 /* If the previous insn required any delay slots, see if we need
1626 to insert a NOP or two. There are eight kinds of possible
1627 hazards, of which an instruction can have at most one type.
1628 (1) a load from memory delay
1629 (2) a load from a coprocessor delay
1630 (3) an unconditional branch delay
1631 (4) a conditional branch delay
1632 (5) a move to coprocessor register delay
1633 (6) a load coprocessor register from memory delay
1634 (7) a coprocessor condition code delay
1635 (8) a HI/LO special register delay
1636
1637 There are a lot of optimizations we could do that we don't.
1638 In particular, we do not, in general, reorder instructions.
1639 If you use gcc with optimization, it will reorder
1640 instructions and generally do much more optimization then we
1641 do here; repeating all that work in the assembler would only
1642 benefit hand written assembly code, and does not seem worth
1643 it. */
1644
1645 /* This is how a NOP is emitted. */
1646 #define emit_nop() \
1647 (mips_opts.mips16 \
1648 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1649 : md_number_to_chars (frag_more (4), 0, 4))
1650
1651 /* The previous insn might require a delay slot, depending upon
1652 the contents of the current insn. */
1653 if (! mips_opts.mips16
1654 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1655 && ! gpr_interlocks)
1656 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1657 && ! cop_interlocks)))
1658 {
1659 /* A load from a coprocessor or from memory. All load
1660 delays delay the use of general register rt for one
1661 instruction. */
1662 /* Itbl support may require additional care here. */
1663 know (prev_pinfo & INSN_WRITE_GPR_T);
1664 if (mips_optimize == 0
1665 || insn_uses_reg (ip,
1666 ((prev_insn.insn_opcode >> OP_SH_RT)
1667 & OP_MASK_RT),
1668 MIPS_GR_REG))
1669 ++nops;
1670 }
1671 else if (! mips_opts.mips16
1672 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1673 && ! cop_interlocks)
1674 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1675 && ! cop_mem_interlocks)))
1676 {
1677 /* A generic coprocessor delay. The previous instruction
1678 modified a coprocessor general or control register. If
1679 it modified a control register, we need to avoid any
1680 coprocessor instruction (this is probably not always
1681 required, but it sometimes is). If it modified a general
1682 register, we avoid using that register.
1683
1684 This case is not handled very well. There is no special
1685 knowledge of CP0 handling, and the coprocessors other
1686 than the floating point unit are not distinguished at
1687 all. */
1688 /* Itbl support may require additional care here. FIXME!
1689 Need to modify this to include knowledge about
1690 user specified delays! */
1691 if (prev_pinfo & INSN_WRITE_FPR_T)
1692 {
1693 if (mips_optimize == 0
1694 || insn_uses_reg (ip,
1695 ((prev_insn.insn_opcode >> OP_SH_FT)
1696 & OP_MASK_FT),
1697 MIPS_FP_REG))
1698 ++nops;
1699 }
1700 else if (prev_pinfo & INSN_WRITE_FPR_S)
1701 {
1702 if (mips_optimize == 0
1703 || insn_uses_reg (ip,
1704 ((prev_insn.insn_opcode >> OP_SH_FS)
1705 & OP_MASK_FS),
1706 MIPS_FP_REG))
1707 ++nops;
1708 }
1709 else
1710 {
1711 /* We don't know exactly what the previous instruction
1712 does. If the current instruction uses a coprocessor
1713 register, we must insert a NOP. If previous
1714 instruction may set the condition codes, and the
1715 current instruction uses them, we must insert two
1716 NOPS. */
1717 /* Itbl support may require additional care here. */
1718 if (mips_optimize == 0
1719 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1720 && (pinfo & INSN_READ_COND_CODE)))
1721 nops += 2;
1722 else if (pinfo & INSN_COP)
1723 ++nops;
1724 }
1725 }
1726 else if (! mips_opts.mips16
1727 && (prev_pinfo & INSN_WRITE_COND_CODE)
1728 && ! cop_interlocks)
1729 {
1730 /* The previous instruction sets the coprocessor condition
1731 codes, but does not require a general coprocessor delay
1732 (this means it is a floating point comparison
1733 instruction). If this instruction uses the condition
1734 codes, we need to insert a single NOP. */
1735 /* Itbl support may require additional care here. */
1736 if (mips_optimize == 0
1737 || (pinfo & INSN_READ_COND_CODE))
1738 ++nops;
1739 }
1740
1741 /* If we're fixing up mfhi/mflo for the r7000 and the
1742 previous insn was an mfhi/mflo and the current insn
1743 reads the register that the mfhi/mflo wrote to, then
1744 insert two nops. */
1745
1746 else if (mips_7000_hilo_fix
1747 && MF_HILO_INSN (prev_pinfo)
1748 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1749 & OP_MASK_RD),
1750 MIPS_GR_REG))
1751 {
1752 nops += 2;
1753 }
1754
1755 /* If we're fixing up mfhi/mflo for the r7000 and the
1756 2nd previous insn was an mfhi/mflo and the current insn
1757 reads the register that the mfhi/mflo wrote to, then
1758 insert one nop. */
1759
1760 else if (mips_7000_hilo_fix
1761 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1762 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1763 & OP_MASK_RD),
1764 MIPS_GR_REG))
1765
1766 {
1767 ++nops;
1768 }
1769
1770 else if (prev_pinfo & INSN_READ_LO)
1771 {
1772 /* The previous instruction reads the LO register; if the
1773 current instruction writes to the LO register, we must
1774 insert two NOPS. Some newer processors have interlocks.
1775 Also the tx39's multiply instructions can be executed
1776 immediately after a read from HI/LO (without the delay),
1777 though the tx39's divide insns still do require the
1778 delay. */
1779 if (! (hilo_interlocks
1780 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1781 && (mips_optimize == 0
1782 || (pinfo & INSN_WRITE_LO)))
1783 nops += 2;
1784 /* Most mips16 branch insns don't have a delay slot.
1785 If a read from LO is immediately followed by a branch
1786 to a write to LO we have a read followed by a write
1787 less than 2 insns away. We assume the target of
1788 a branch might be a write to LO, and insert a nop
1789 between a read and an immediately following branch. */
1790 else if (mips_opts.mips16
1791 && (mips_optimize == 0
1792 || (pinfo & MIPS16_INSN_BRANCH)))
1793 ++nops;
1794 }
1795 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1796 {
1797 /* The previous instruction reads the HI register; if the
1798 current instruction writes to the HI register, we must
1799 insert a NOP. Some newer processors have interlocks.
1800 Also the note tx39's multiply above. */
1801 if (! (hilo_interlocks
1802 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1803 && (mips_optimize == 0
1804 || (pinfo & INSN_WRITE_HI)))
1805 nops += 2;
1806 /* Most mips16 branch insns don't have a delay slot.
1807 If a read from HI is immediately followed by a branch
1808 to a write to HI we have a read followed by a write
1809 less than 2 insns away. We assume the target of
1810 a branch might be a write to HI, and insert a nop
1811 between a read and an immediately following branch. */
1812 else if (mips_opts.mips16
1813 && (mips_optimize == 0
1814 || (pinfo & MIPS16_INSN_BRANCH)))
1815 ++nops;
1816 }
1817
1818 /* If the previous instruction was in a noreorder section, then
1819 we don't want to insert the nop after all. */
1820 /* Itbl support may require additional care here. */
1821 if (prev_insn_unreordered)
1822 nops = 0;
1823
1824 /* There are two cases which require two intervening
1825 instructions: 1) setting the condition codes using a move to
1826 coprocessor instruction which requires a general coprocessor
1827 delay and then reading the condition codes 2) reading the HI
1828 or LO register and then writing to it (except on processors
1829 which have interlocks). If we are not already emitting a NOP
1830 instruction, we must check for these cases compared to the
1831 instruction previous to the previous instruction. */
1832 if ((! mips_opts.mips16
1833 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1834 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1835 && (pinfo & INSN_READ_COND_CODE)
1836 && ! cop_interlocks)
1837 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1838 && (pinfo & INSN_WRITE_LO)
1839 && ! (hilo_interlocks
1840 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
1841 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1842 && (pinfo & INSN_WRITE_HI)
1843 && ! (hilo_interlocks
1844 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
1845 prev_prev_nop = 1;
1846 else
1847 prev_prev_nop = 0;
1848
1849 if (prev_prev_insn_unreordered)
1850 prev_prev_nop = 0;
1851
1852 if (prev_prev_nop && nops == 0)
1853 ++nops;
1854
1855 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
1856 {
1857 /* We're out of bits in pinfo, so we must resort to string
1858 ops here. Shortcuts are selected based on opcodes being
1859 limited to the VR4120 instruction set. */
1860 int min_nops = 0;
1861 const char *pn = prev_insn.insn_mo->name;
1862 const char *tn = ip->insn_mo->name;
1863 if (strncmp(pn, "macc", 4) == 0
1864 || strncmp(pn, "dmacc", 5) == 0)
1865 {
1866 /* Errata 21 - [D]DIV[U] after [D]MACC */
1867 if (strstr (tn, "div"))
1868 {
1869 min_nops = 1;
1870 }
1871
1872 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1873 if (pn[0] == 'd' /* dmacc */
1874 && (strncmp(tn, "dmult", 5) == 0
1875 || strncmp(tn, "dmacc", 5) == 0))
1876 {
1877 min_nops = 1;
1878 }
1879
1880 /* Errata 24 - MT{LO,HI} after [D]MACC */
1881 if (strcmp (tn, "mtlo") == 0
1882 || strcmp (tn, "mthi") == 0)
1883 {
1884 min_nops = 1;
1885 }
1886
1887 }
1888 else if (strncmp(pn, "dmult", 5) == 0
1889 && (strncmp(tn, "dmult", 5) == 0
1890 || strncmp(tn, "dmacc", 5) == 0))
1891 {
1892 /* Here is the rest of errata 23. */
1893 min_nops = 1;
1894 }
1895 if (nops < min_nops)
1896 nops = min_nops;
1897 }
1898
1899 /* If we are being given a nop instruction, don't bother with
1900 one of the nops we would otherwise output. This will only
1901 happen when a nop instruction is used with mips_optimize set
1902 to 0. */
1903 if (nops > 0
1904 && ! mips_opts.noreorder
1905 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1906 --nops;
1907
1908 /* Now emit the right number of NOP instructions. */
1909 if (nops > 0 && ! mips_opts.noreorder)
1910 {
1911 fragS *old_frag;
1912 unsigned long old_frag_offset;
1913 int i;
1914 struct insn_label_list *l;
1915
1916 old_frag = frag_now;
1917 old_frag_offset = frag_now_fix ();
1918
1919 for (i = 0; i < nops; i++)
1920 emit_nop ();
1921
1922 if (listing)
1923 {
1924 listing_prev_line ();
1925 /* We may be at the start of a variant frag. In case we
1926 are, make sure there is enough space for the frag
1927 after the frags created by listing_prev_line. The
1928 argument to frag_grow here must be at least as large
1929 as the argument to all other calls to frag_grow in
1930 this file. We don't have to worry about being in the
1931 middle of a variant frag, because the variants insert
1932 all needed nop instructions themselves. */
1933 frag_grow (40);
1934 }
1935
1936 for (l = insn_labels; l != NULL; l = l->next)
1937 {
1938 valueT val;
1939
1940 assert (S_GET_SEGMENT (l->label) == now_seg);
1941 symbol_set_frag (l->label, frag_now);
1942 val = (valueT) frag_now_fix ();
1943 /* mips16 text labels are stored as odd. */
1944 if (mips_opts.mips16)
1945 ++val;
1946 S_SET_VALUE (l->label, val);
1947 }
1948
1949 #ifndef NO_ECOFF_DEBUGGING
1950 if (ECOFF_DEBUGGING)
1951 ecoff_fix_loc (old_frag, old_frag_offset);
1952 #endif
1953 }
1954 else if (prev_nop_frag != NULL)
1955 {
1956 /* We have a frag holding nops we may be able to remove. If
1957 we don't need any nops, we can decrease the size of
1958 prev_nop_frag by the size of one instruction. If we do
1959 need some nops, we count them in prev_nops_required. */
1960 if (prev_nop_frag_since == 0)
1961 {
1962 if (nops == 0)
1963 {
1964 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1965 --prev_nop_frag_holds;
1966 }
1967 else
1968 prev_nop_frag_required += nops;
1969 }
1970 else
1971 {
1972 if (prev_prev_nop == 0)
1973 {
1974 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1975 --prev_nop_frag_holds;
1976 }
1977 else
1978 ++prev_nop_frag_required;
1979 }
1980
1981 if (prev_nop_frag_holds <= prev_nop_frag_required)
1982 prev_nop_frag = NULL;
1983
1984 ++prev_nop_frag_since;
1985
1986 /* Sanity check: by the time we reach the second instruction
1987 after prev_nop_frag, we should have used up all the nops
1988 one way or another. */
1989 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1990 }
1991 }
1992
1993 /* Record the frag type before frag_var. */
1994 if (prev_insn_frag)
1995 prev_insn_frag_type = prev_insn_frag->fr_type;
1996
1997 if (address_expr
1998 && *reloc_type == BFD_RELOC_16_PCREL_S2
1999 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2000 || pinfo & INSN_COND_BRANCH_LIKELY)
2001 && mips_relax_branch
2002 /* Don't try branch relaxation within .set nomacro, or within
2003 .set noat if we use $at for PIC computations. If it turns
2004 out that the branch was out-of-range, we'll get an error. */
2005 && !mips_opts.warn_about_macros
2006 && !(mips_opts.noat && mips_pic != NO_PIC)
2007 && !mips_opts.mips16)
2008 {
2009 relaxed_branch = TRUE;
2010 f = frag_var (rs_machine_dependent,
2011 relaxed_branch_length
2012 (NULL, NULL,
2013 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2014 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2015 RELAX_BRANCH_ENCODE
2016 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2017 pinfo & INSN_COND_BRANCH_LIKELY,
2018 pinfo & INSN_WRITE_GPR_31,
2019 0),
2020 address_expr->X_add_symbol,
2021 address_expr->X_add_number,
2022 0);
2023 *reloc_type = BFD_RELOC_UNUSED;
2024 }
2025 else if (*reloc_type > BFD_RELOC_UNUSED)
2026 {
2027 /* We need to set up a variant frag. */
2028 assert (mips_opts.mips16 && address_expr != NULL);
2029 f = frag_var (rs_machine_dependent, 4, 0,
2030 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2031 mips16_small, mips16_ext,
2032 (prev_pinfo
2033 & INSN_UNCOND_BRANCH_DELAY),
2034 (*prev_insn_reloc_type
2035 == BFD_RELOC_MIPS16_JMP)),
2036 make_expr_symbol (address_expr), 0, NULL);
2037 }
2038 else if (mips_opts.mips16
2039 && ! ip->use_extend
2040 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2041 {
2042 /* Make sure there is enough room to swap this instruction with
2043 a following jump instruction. */
2044 frag_grow (6);
2045 f = frag_more (2);
2046 }
2047 else
2048 {
2049 if (mips_opts.mips16
2050 && mips_opts.noreorder
2051 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2052 as_warn (_("extended instruction in delay slot"));
2053
2054 if (mips_relax.sequence)
2055 {
2056 /* If we've reached the end of this frag, turn it into a variant
2057 frag and record the information for the instructions we've
2058 written so far. */
2059 if (frag_room () < 4)
2060 relax_close_frag ();
2061 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2062 }
2063
2064 if (mips_relax.sequence != 2)
2065 mips_macro_warning.sizes[0] += 4;
2066 if (mips_relax.sequence != 1)
2067 mips_macro_warning.sizes[1] += 4;
2068
2069 f = frag_more (4);
2070 }
2071
2072 fixp[0] = fixp[1] = fixp[2] = NULL;
2073 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2074 {
2075 if (address_expr->X_op == O_constant)
2076 {
2077 valueT tmp;
2078
2079 switch (*reloc_type)
2080 {
2081 case BFD_RELOC_32:
2082 ip->insn_opcode |= address_expr->X_add_number;
2083 break;
2084
2085 case BFD_RELOC_MIPS_HIGHEST:
2086 tmp = (address_expr->X_add_number
2087 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
2088 tmp >>= 16;
2089 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2090 break;
2091
2092 case BFD_RELOC_MIPS_HIGHER:
2093 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2094 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2095 break;
2096
2097 case BFD_RELOC_HI16_S:
2098 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2099 >> 16) & 0xffff;
2100 break;
2101
2102 case BFD_RELOC_HI16:
2103 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2104 break;
2105
2106 case BFD_RELOC_LO16:
2107 case BFD_RELOC_MIPS_GOT_DISP:
2108 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2109 break;
2110
2111 case BFD_RELOC_MIPS_JMP:
2112 if ((address_expr->X_add_number & 3) != 0)
2113 as_bad (_("jump to misaligned address (0x%lx)"),
2114 (unsigned long) address_expr->X_add_number);
2115 if (address_expr->X_add_number & ~0xfffffff)
2116 as_bad (_("jump address range overflow (0x%lx)"),
2117 (unsigned long) address_expr->X_add_number);
2118 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2119 break;
2120
2121 case BFD_RELOC_MIPS16_JMP:
2122 if ((address_expr->X_add_number & 3) != 0)
2123 as_bad (_("jump to misaligned address (0x%lx)"),
2124 (unsigned long) address_expr->X_add_number);
2125 if (address_expr->X_add_number & ~0xfffffff)
2126 as_bad (_("jump address range overflow (0x%lx)"),
2127 (unsigned long) address_expr->X_add_number);
2128 ip->insn_opcode |=
2129 (((address_expr->X_add_number & 0x7c0000) << 3)
2130 | ((address_expr->X_add_number & 0xf800000) >> 7)
2131 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2132 break;
2133
2134 case BFD_RELOC_16_PCREL_S2:
2135 goto need_reloc;
2136
2137 default:
2138 internalError ();
2139 }
2140 }
2141 else
2142 need_reloc:
2143 {
2144 reloc_howto_type *howto;
2145 int i;
2146
2147 /* In a compound relocation, it is the final (outermost)
2148 operator that determines the relocated field. */
2149 for (i = 1; i < 3; i++)
2150 if (reloc_type[i] == BFD_RELOC_UNUSED)
2151 break;
2152
2153 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2154 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2155 bfd_get_reloc_size(howto),
2156 address_expr,
2157 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2158 reloc_type[0]);
2159
2160 /* These relocations can have an addend that won't fit in
2161 4 octets for 64bit assembly. */
2162 if (HAVE_64BIT_GPRS
2163 && ! howto->partial_inplace
2164 && (reloc_type[0] == BFD_RELOC_16
2165 || reloc_type[0] == BFD_RELOC_32
2166 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2167 || reloc_type[0] == BFD_RELOC_HI16_S
2168 || reloc_type[0] == BFD_RELOC_LO16
2169 || reloc_type[0] == BFD_RELOC_GPREL16
2170 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2171 || reloc_type[0] == BFD_RELOC_GPREL32
2172 || reloc_type[0] == BFD_RELOC_64
2173 || reloc_type[0] == BFD_RELOC_CTOR
2174 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2175 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2176 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2177 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2178 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2179 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2180 fixp[0]->fx_no_overflow = 1;
2181
2182 if (mips_relax.sequence)
2183 {
2184 if (mips_relax.first_fixup == 0)
2185 mips_relax.first_fixup = fixp[0];
2186 }
2187 else if (reloc_needs_lo_p (*reloc_type))
2188 {
2189 struct mips_hi_fixup *hi_fixup;
2190
2191 /* Reuse the last entry if it already has a matching %lo. */
2192 hi_fixup = mips_hi_fixup_list;
2193 if (hi_fixup == 0
2194 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2195 {
2196 hi_fixup = ((struct mips_hi_fixup *)
2197 xmalloc (sizeof (struct mips_hi_fixup)));
2198 hi_fixup->next = mips_hi_fixup_list;
2199 mips_hi_fixup_list = hi_fixup;
2200 }
2201 hi_fixup->fixp = fixp[0];
2202 hi_fixup->seg = now_seg;
2203 }
2204
2205 /* Add fixups for the second and third relocations, if given.
2206 Note that the ABI allows the second relocation to be
2207 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2208 moment we only use RSS_UNDEF, but we could add support
2209 for the others if it ever becomes necessary. */
2210 for (i = 1; i < 3; i++)
2211 if (reloc_type[i] != BFD_RELOC_UNUSED)
2212 {
2213 address_expr->X_op = O_absent;
2214 address_expr->X_add_symbol = 0;
2215 address_expr->X_add_number = 0;
2216
2217 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2218 fixp[0]->fx_size, address_expr,
2219 FALSE, reloc_type[i]);
2220 }
2221 }
2222 }
2223
2224 if (! mips_opts.mips16)
2225 {
2226 md_number_to_chars (f, ip->insn_opcode, 4);
2227 #ifdef OBJ_ELF
2228 dwarf2_emit_insn (4);
2229 #endif
2230 }
2231 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2232 {
2233 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2234 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2235 #ifdef OBJ_ELF
2236 dwarf2_emit_insn (4);
2237 #endif
2238 }
2239 else
2240 {
2241 if (ip->use_extend)
2242 {
2243 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2244 f += 2;
2245 }
2246 md_number_to_chars (f, ip->insn_opcode, 2);
2247 #ifdef OBJ_ELF
2248 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2249 #endif
2250 }
2251
2252 /* Update the register mask information. */
2253 if (! mips_opts.mips16)
2254 {
2255 if (pinfo & INSN_WRITE_GPR_D)
2256 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2257 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2258 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2259 if (pinfo & INSN_READ_GPR_S)
2260 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2261 if (pinfo & INSN_WRITE_GPR_31)
2262 mips_gprmask |= 1 << RA;
2263 if (pinfo & INSN_WRITE_FPR_D)
2264 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2265 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2266 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2267 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2268 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2269 if ((pinfo & INSN_READ_FPR_R) != 0)
2270 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2271 if (pinfo & INSN_COP)
2272 {
2273 /* We don't keep enough information to sort these cases out.
2274 The itbl support does keep this information however, although
2275 we currently don't support itbl fprmats as part of the cop
2276 instruction. May want to add this support in the future. */
2277 }
2278 /* Never set the bit for $0, which is always zero. */
2279 mips_gprmask &= ~1 << 0;
2280 }
2281 else
2282 {
2283 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2284 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2285 & MIPS16OP_MASK_RX);
2286 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2287 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2288 & MIPS16OP_MASK_RY);
2289 if (pinfo & MIPS16_INSN_WRITE_Z)
2290 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2291 & MIPS16OP_MASK_RZ);
2292 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2293 mips_gprmask |= 1 << TREG;
2294 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2295 mips_gprmask |= 1 << SP;
2296 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2297 mips_gprmask |= 1 << RA;
2298 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2299 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2300 if (pinfo & MIPS16_INSN_READ_Z)
2301 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2302 & MIPS16OP_MASK_MOVE32Z);
2303 if (pinfo & MIPS16_INSN_READ_GPR_X)
2304 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2305 & MIPS16OP_MASK_REGR32);
2306 }
2307
2308 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2309 {
2310 /* Filling the branch delay slot is more complex. We try to
2311 switch the branch with the previous instruction, which we can
2312 do if the previous instruction does not set up a condition
2313 that the branch tests and if the branch is not itself the
2314 target of any branch. */
2315 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2316 || (pinfo & INSN_COND_BRANCH_DELAY))
2317 {
2318 if (mips_optimize < 2
2319 /* If we have seen .set volatile or .set nomove, don't
2320 optimize. */
2321 || mips_opts.nomove != 0
2322 /* If we had to emit any NOP instructions, then we
2323 already know we can not swap. */
2324 || nops != 0
2325 /* If we don't even know the previous insn, we can not
2326 swap. */
2327 || ! prev_insn_valid
2328 /* If the previous insn is already in a branch delay
2329 slot, then we can not swap. */
2330 || prev_insn_is_delay_slot
2331 /* If the previous previous insn was in a .set
2332 noreorder, we can't swap. Actually, the MIPS
2333 assembler will swap in this situation. However, gcc
2334 configured -with-gnu-as will generate code like
2335 .set noreorder
2336 lw $4,XXX
2337 .set reorder
2338 INSN
2339 bne $4,$0,foo
2340 in which we can not swap the bne and INSN. If gcc is
2341 not configured -with-gnu-as, it does not output the
2342 .set pseudo-ops. We don't have to check
2343 prev_insn_unreordered, because prev_insn_valid will
2344 be 0 in that case. We don't want to use
2345 prev_prev_insn_valid, because we do want to be able
2346 to swap at the start of a function. */
2347 || prev_prev_insn_unreordered
2348 /* If the branch is itself the target of a branch, we
2349 can not swap. We cheat on this; all we check for is
2350 whether there is a label on this instruction. If
2351 there are any branches to anything other than a
2352 label, users must use .set noreorder. */
2353 || insn_labels != NULL
2354 /* If the previous instruction is in a variant frag
2355 other than this branch's one, we cannot do the swap.
2356 This does not apply to the mips16, which uses variant
2357 frags for different purposes. */
2358 || (! mips_opts.mips16
2359 && prev_insn_frag_type == rs_machine_dependent)
2360 /* If the branch reads the condition codes, we don't
2361 even try to swap, because in the sequence
2362 ctc1 $X,$31
2363 INSN
2364 INSN
2365 bc1t LABEL
2366 we can not swap, and I don't feel like handling that
2367 case. */
2368 || (! mips_opts.mips16
2369 && (pinfo & INSN_READ_COND_CODE)
2370 && ! cop_interlocks)
2371 /* We can not swap with an instruction that requires a
2372 delay slot, because the target of the branch might
2373 interfere with that instruction. */
2374 || (! mips_opts.mips16
2375 && (prev_pinfo
2376 /* Itbl support may require additional care here. */
2377 & (INSN_LOAD_COPROC_DELAY
2378 | INSN_COPROC_MOVE_DELAY
2379 | INSN_WRITE_COND_CODE))
2380 && ! cop_interlocks)
2381 || (! (hilo_interlocks
2382 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
2383 && (prev_pinfo
2384 & (INSN_READ_LO
2385 | INSN_READ_HI)))
2386 || (! mips_opts.mips16
2387 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2388 && ! gpr_interlocks)
2389 || (! mips_opts.mips16
2390 /* Itbl support may require additional care here. */
2391 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2392 && ! cop_mem_interlocks)
2393 /* We can not swap with a branch instruction. */
2394 || (prev_pinfo
2395 & (INSN_UNCOND_BRANCH_DELAY
2396 | INSN_COND_BRANCH_DELAY
2397 | INSN_COND_BRANCH_LIKELY))
2398 /* We do not swap with a trap instruction, since it
2399 complicates trap handlers to have the trap
2400 instruction be in a delay slot. */
2401 || (prev_pinfo & INSN_TRAP)
2402 /* If the branch reads a register that the previous
2403 instruction sets, we can not swap. */
2404 || (! mips_opts.mips16
2405 && (prev_pinfo & INSN_WRITE_GPR_T)
2406 && insn_uses_reg (ip,
2407 ((prev_insn.insn_opcode >> OP_SH_RT)
2408 & OP_MASK_RT),
2409 MIPS_GR_REG))
2410 || (! mips_opts.mips16
2411 && (prev_pinfo & INSN_WRITE_GPR_D)
2412 && insn_uses_reg (ip,
2413 ((prev_insn.insn_opcode >> OP_SH_RD)
2414 & OP_MASK_RD),
2415 MIPS_GR_REG))
2416 || (mips_opts.mips16
2417 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2418 && insn_uses_reg (ip,
2419 ((prev_insn.insn_opcode
2420 >> MIPS16OP_SH_RX)
2421 & MIPS16OP_MASK_RX),
2422 MIPS16_REG))
2423 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2424 && insn_uses_reg (ip,
2425 ((prev_insn.insn_opcode
2426 >> MIPS16OP_SH_RY)
2427 & MIPS16OP_MASK_RY),
2428 MIPS16_REG))
2429 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2430 && insn_uses_reg (ip,
2431 ((prev_insn.insn_opcode
2432 >> MIPS16OP_SH_RZ)
2433 & MIPS16OP_MASK_RZ),
2434 MIPS16_REG))
2435 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2436 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2437 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2438 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2439 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2440 && insn_uses_reg (ip,
2441 MIPS16OP_EXTRACT_REG32R (prev_insn.
2442 insn_opcode),
2443 MIPS_GR_REG))))
2444 /* If the branch writes a register that the previous
2445 instruction sets, we can not swap (we know that
2446 branches write only to RD or to $31). */
2447 || (! mips_opts.mips16
2448 && (prev_pinfo & INSN_WRITE_GPR_T)
2449 && (((pinfo & INSN_WRITE_GPR_D)
2450 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2451 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2452 || ((pinfo & INSN_WRITE_GPR_31)
2453 && (((prev_insn.insn_opcode >> OP_SH_RT)
2454 & OP_MASK_RT)
2455 == RA))))
2456 || (! mips_opts.mips16
2457 && (prev_pinfo & INSN_WRITE_GPR_D)
2458 && (((pinfo & INSN_WRITE_GPR_D)
2459 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2460 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2461 || ((pinfo & INSN_WRITE_GPR_31)
2462 && (((prev_insn.insn_opcode >> OP_SH_RD)
2463 & OP_MASK_RD)
2464 == RA))))
2465 || (mips_opts.mips16
2466 && (pinfo & MIPS16_INSN_WRITE_31)
2467 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2468 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2469 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2470 == RA))))
2471 /* If the branch writes a register that the previous
2472 instruction reads, we can not swap (we know that
2473 branches only write to RD or to $31). */
2474 || (! mips_opts.mips16
2475 && (pinfo & INSN_WRITE_GPR_D)
2476 && insn_uses_reg (&prev_insn,
2477 ((ip->insn_opcode >> OP_SH_RD)
2478 & OP_MASK_RD),
2479 MIPS_GR_REG))
2480 || (! mips_opts.mips16
2481 && (pinfo & INSN_WRITE_GPR_31)
2482 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2483 || (mips_opts.mips16
2484 && (pinfo & MIPS16_INSN_WRITE_31)
2485 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2486 /* If we are generating embedded PIC code, the branch
2487 might be expanded into a sequence which uses $at, so
2488 we can't swap with an instruction which reads it. */
2489 || (mips_pic == EMBEDDED_PIC
2490 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2491 /* If the previous previous instruction has a load
2492 delay, and sets a register that the branch reads, we
2493 can not swap. */
2494 || (! mips_opts.mips16
2495 /* Itbl support may require additional care here. */
2496 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2497 && ! cop_interlocks)
2498 || ((prev_prev_insn.insn_mo->pinfo
2499 & INSN_LOAD_MEMORY_DELAY)
2500 && ! gpr_interlocks))
2501 && insn_uses_reg (ip,
2502 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2503 & OP_MASK_RT),
2504 MIPS_GR_REG))
2505 /* If one instruction sets a condition code and the
2506 other one uses a condition code, we can not swap. */
2507 || ((pinfo & INSN_READ_COND_CODE)
2508 && (prev_pinfo & INSN_WRITE_COND_CODE))
2509 || ((pinfo & INSN_WRITE_COND_CODE)
2510 && (prev_pinfo & INSN_READ_COND_CODE))
2511 /* If the previous instruction uses the PC, we can not
2512 swap. */
2513 || (mips_opts.mips16
2514 && (prev_pinfo & MIPS16_INSN_READ_PC))
2515 /* If the previous instruction was extended, we can not
2516 swap. */
2517 || (mips_opts.mips16 && prev_insn_extended)
2518 /* If the previous instruction had a fixup in mips16
2519 mode, we can not swap. This normally means that the
2520 previous instruction was a 4 byte branch anyhow. */
2521 || (mips_opts.mips16 && prev_insn_fixp[0])
2522 /* If the previous instruction is a sync, sync.l, or
2523 sync.p, we can not swap. */
2524 || (prev_pinfo & INSN_SYNC))
2525 {
2526 /* We could do even better for unconditional branches to
2527 portions of this object file; we could pick up the
2528 instruction at the destination, put it in the delay
2529 slot, and bump the destination address. */
2530 emit_nop ();
2531 /* Update the previous insn information. */
2532 prev_prev_insn = *ip;
2533 prev_insn.insn_mo = &dummy_opcode;
2534 }
2535 else
2536 {
2537 /* It looks like we can actually do the swap. */
2538 if (! mips_opts.mips16)
2539 {
2540 char *prev_f;
2541 char temp[4];
2542
2543 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2544 if (!relaxed_branch)
2545 {
2546 /* If this is not a relaxed branch, then just
2547 swap the instructions. */
2548 memcpy (temp, prev_f, 4);
2549 memcpy (prev_f, f, 4);
2550 memcpy (f, temp, 4);
2551 }
2552 else
2553 {
2554 /* If this is a relaxed branch, then we move the
2555 instruction to be placed in the delay slot to
2556 the current frag, shrinking the fixed part of
2557 the originating frag. If the branch occupies
2558 the tail of the latter, we move it backwards,
2559 into the space freed by the moved instruction. */
2560 f = frag_more (4);
2561 memcpy (f, prev_f, 4);
2562 prev_insn_frag->fr_fix -= 4;
2563 if (prev_insn_frag->fr_type == rs_machine_dependent)
2564 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2565 }
2566
2567 if (prev_insn_fixp[0])
2568 {
2569 prev_insn_fixp[0]->fx_frag = frag_now;
2570 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2571 }
2572 if (prev_insn_fixp[1])
2573 {
2574 prev_insn_fixp[1]->fx_frag = frag_now;
2575 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2576 }
2577 if (prev_insn_fixp[2])
2578 {
2579 prev_insn_fixp[2]->fx_frag = frag_now;
2580 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2581 }
2582 if (prev_insn_fixp[0] && HAVE_NEWABI
2583 && prev_insn_frag != frag_now
2584 && (prev_insn_fixp[0]->fx_r_type
2585 == BFD_RELOC_MIPS_GOT_DISP
2586 || (prev_insn_fixp[0]->fx_r_type
2587 == BFD_RELOC_MIPS_CALL16)))
2588 {
2589 /* To avoid confusion in tc_gen_reloc, we must
2590 ensure that this does not become a variant
2591 frag. */
2592 force_new_frag = TRUE;
2593 }
2594
2595 if (!relaxed_branch)
2596 {
2597 if (fixp[0])
2598 {
2599 fixp[0]->fx_frag = prev_insn_frag;
2600 fixp[0]->fx_where = prev_insn_where;
2601 }
2602 if (fixp[1])
2603 {
2604 fixp[1]->fx_frag = prev_insn_frag;
2605 fixp[1]->fx_where = prev_insn_where;
2606 }
2607 if (fixp[2])
2608 {
2609 fixp[2]->fx_frag = prev_insn_frag;
2610 fixp[2]->fx_where = prev_insn_where;
2611 }
2612 }
2613 else if (prev_insn_frag->fr_type == rs_machine_dependent)
2614 {
2615 if (fixp[0])
2616 fixp[0]->fx_where -= 4;
2617 if (fixp[1])
2618 fixp[1]->fx_where -= 4;
2619 if (fixp[2])
2620 fixp[2]->fx_where -= 4;
2621 }
2622 }
2623 else
2624 {
2625 char *prev_f;
2626 char temp[2];
2627
2628 assert (prev_insn_fixp[0] == NULL);
2629 assert (prev_insn_fixp[1] == NULL);
2630 assert (prev_insn_fixp[2] == NULL);
2631 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2632 memcpy (temp, prev_f, 2);
2633 memcpy (prev_f, f, 2);
2634 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2635 {
2636 assert (*reloc_type == BFD_RELOC_UNUSED);
2637 memcpy (f, temp, 2);
2638 }
2639 else
2640 {
2641 memcpy (f, f + 2, 2);
2642 memcpy (f + 2, temp, 2);
2643 }
2644 if (fixp[0])
2645 {
2646 fixp[0]->fx_frag = prev_insn_frag;
2647 fixp[0]->fx_where = prev_insn_where;
2648 }
2649 if (fixp[1])
2650 {
2651 fixp[1]->fx_frag = prev_insn_frag;
2652 fixp[1]->fx_where = prev_insn_where;
2653 }
2654 if (fixp[2])
2655 {
2656 fixp[2]->fx_frag = prev_insn_frag;
2657 fixp[2]->fx_where = prev_insn_where;
2658 }
2659 }
2660
2661 /* Update the previous insn information; leave prev_insn
2662 unchanged. */
2663 prev_prev_insn = *ip;
2664 }
2665 prev_insn_is_delay_slot = 1;
2666
2667 /* If that was an unconditional branch, forget the previous
2668 insn information. */
2669 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2670 {
2671 prev_prev_insn.insn_mo = &dummy_opcode;
2672 prev_insn.insn_mo = &dummy_opcode;
2673 }
2674
2675 prev_insn_fixp[0] = NULL;
2676 prev_insn_fixp[1] = NULL;
2677 prev_insn_fixp[2] = NULL;
2678 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2679 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2680 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2681 prev_insn_extended = 0;
2682 }
2683 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2684 {
2685 /* We don't yet optimize a branch likely. What we should do
2686 is look at the target, copy the instruction found there
2687 into the delay slot, and increment the branch to jump to
2688 the next instruction. */
2689 emit_nop ();
2690 /* Update the previous insn information. */
2691 prev_prev_insn = *ip;
2692 prev_insn.insn_mo = &dummy_opcode;
2693 prev_insn_fixp[0] = NULL;
2694 prev_insn_fixp[1] = NULL;
2695 prev_insn_fixp[2] = NULL;
2696 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2697 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2698 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2699 prev_insn_extended = 0;
2700 }
2701 else
2702 {
2703 /* Update the previous insn information. */
2704 if (nops > 0)
2705 prev_prev_insn.insn_mo = &dummy_opcode;
2706 else
2707 prev_prev_insn = prev_insn;
2708 prev_insn = *ip;
2709
2710 /* Any time we see a branch, we always fill the delay slot
2711 immediately; since this insn is not a branch, we know it
2712 is not in a delay slot. */
2713 prev_insn_is_delay_slot = 0;
2714
2715 prev_insn_fixp[0] = fixp[0];
2716 prev_insn_fixp[1] = fixp[1];
2717 prev_insn_fixp[2] = fixp[2];
2718 prev_insn_reloc_type[0] = reloc_type[0];
2719 prev_insn_reloc_type[1] = reloc_type[1];
2720 prev_insn_reloc_type[2] = reloc_type[2];
2721 if (mips_opts.mips16)
2722 prev_insn_extended = (ip->use_extend
2723 || *reloc_type > BFD_RELOC_UNUSED);
2724 }
2725
2726 prev_prev_insn_unreordered = prev_insn_unreordered;
2727 prev_insn_unreordered = 0;
2728 prev_insn_frag = frag_now;
2729 prev_insn_where = f - frag_now->fr_literal;
2730 prev_insn_valid = 1;
2731 }
2732 else if (mips_relax.sequence != 2)
2733 {
2734 /* We need to record a bit of information even when we are not
2735 reordering, in order to determine the base address for mips16
2736 PC relative relocs. */
2737 prev_prev_insn = prev_insn;
2738 prev_insn = *ip;
2739 prev_insn_reloc_type[0] = reloc_type[0];
2740 prev_insn_reloc_type[1] = reloc_type[1];
2741 prev_insn_reloc_type[2] = reloc_type[2];
2742 prev_prev_insn_unreordered = prev_insn_unreordered;
2743 prev_insn_unreordered = 1;
2744 }
2745
2746 /* We just output an insn, so the next one doesn't have a label. */
2747 mips_clear_insn_labels ();
2748 }
2749
2750 /* This function forgets that there was any previous instruction or
2751 label. If PRESERVE is non-zero, it remembers enough information to
2752 know whether nops are needed before a noreorder section. */
2753
2754 static void
2755 mips_no_prev_insn (int preserve)
2756 {
2757 if (! preserve)
2758 {
2759 prev_insn.insn_mo = &dummy_opcode;
2760 prev_prev_insn.insn_mo = &dummy_opcode;
2761 prev_nop_frag = NULL;
2762 prev_nop_frag_holds = 0;
2763 prev_nop_frag_required = 0;
2764 prev_nop_frag_since = 0;
2765 }
2766 prev_insn_valid = 0;
2767 prev_insn_is_delay_slot = 0;
2768 prev_insn_unreordered = 0;
2769 prev_insn_extended = 0;
2770 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2771 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2772 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2773 prev_prev_insn_unreordered = 0;
2774 mips_clear_insn_labels ();
2775 }
2776
2777 /* This function must be called whenever we turn on noreorder or emit
2778 something other than instructions. It inserts any NOPS which might
2779 be needed by the previous instruction, and clears the information
2780 kept for the previous instructions. The INSNS parameter is true if
2781 instructions are to follow. */
2782
2783 static void
2784 mips_emit_delays (bfd_boolean insns)
2785 {
2786 if (! mips_opts.noreorder)
2787 {
2788 int nops;
2789
2790 nops = 0;
2791 if ((! mips_opts.mips16
2792 && ((prev_insn.insn_mo->pinfo
2793 & (INSN_LOAD_COPROC_DELAY
2794 | INSN_COPROC_MOVE_DELAY
2795 | INSN_WRITE_COND_CODE))
2796 && ! cop_interlocks))
2797 || (! hilo_interlocks
2798 && (prev_insn.insn_mo->pinfo
2799 & (INSN_READ_LO
2800 | INSN_READ_HI)))
2801 || (! mips_opts.mips16
2802 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2803 && ! gpr_interlocks)
2804 || (! mips_opts.mips16
2805 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2806 && ! cop_mem_interlocks))
2807 {
2808 /* Itbl support may require additional care here. */
2809 ++nops;
2810 if ((! mips_opts.mips16
2811 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2812 && ! cop_interlocks))
2813 || (! hilo_interlocks
2814 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2815 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2816 ++nops;
2817
2818 if (prev_insn_unreordered)
2819 nops = 0;
2820 }
2821 else if ((! mips_opts.mips16
2822 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2823 && ! cop_interlocks))
2824 || (! hilo_interlocks
2825 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2826 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2827 {
2828 /* Itbl support may require additional care here. */
2829 if (! prev_prev_insn_unreordered)
2830 ++nops;
2831 }
2832
2833 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
2834 {
2835 int min_nops = 0;
2836 const char *pn = prev_insn.insn_mo->name;
2837 if (strncmp(pn, "macc", 4) == 0
2838 || strncmp(pn, "dmacc", 5) == 0
2839 || strncmp(pn, "dmult", 5) == 0)
2840 {
2841 min_nops = 1;
2842 }
2843 if (nops < min_nops)
2844 nops = min_nops;
2845 }
2846
2847 if (nops > 0)
2848 {
2849 struct insn_label_list *l;
2850
2851 if (insns)
2852 {
2853 /* Record the frag which holds the nop instructions, so
2854 that we can remove them if we don't need them. */
2855 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2856 prev_nop_frag = frag_now;
2857 prev_nop_frag_holds = nops;
2858 prev_nop_frag_required = 0;
2859 prev_nop_frag_since = 0;
2860 }
2861
2862 for (; nops > 0; --nops)
2863 emit_nop ();
2864
2865 if (insns)
2866 {
2867 /* Move on to a new frag, so that it is safe to simply
2868 decrease the size of prev_nop_frag. */
2869 frag_wane (frag_now);
2870 frag_new (0);
2871 }
2872
2873 for (l = insn_labels; l != NULL; l = l->next)
2874 {
2875 valueT val;
2876
2877 assert (S_GET_SEGMENT (l->label) == now_seg);
2878 symbol_set_frag (l->label, frag_now);
2879 val = (valueT) frag_now_fix ();
2880 /* mips16 text labels are stored as odd. */
2881 if (mips_opts.mips16)
2882 ++val;
2883 S_SET_VALUE (l->label, val);
2884 }
2885 }
2886 }
2887
2888 /* Mark instruction labels in mips16 mode. */
2889 if (insns)
2890 mips16_mark_labels ();
2891
2892 mips_no_prev_insn (insns);
2893 }
2894
2895 /* Set up global variables for the start of a new macro. */
2896
2897 static void
2898 macro_start (void)
2899 {
2900 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2901 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2902 && (prev_insn.insn_mo->pinfo
2903 & (INSN_UNCOND_BRANCH_DELAY
2904 | INSN_COND_BRANCH_DELAY
2905 | INSN_COND_BRANCH_LIKELY)) != 0);
2906 }
2907
2908 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2909 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2910 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2911
2912 static const char *
2913 macro_warning (relax_substateT subtype)
2914 {
2915 if (subtype & RELAX_DELAY_SLOT)
2916 return _("Macro instruction expanded into multiple instructions"
2917 " in a branch delay slot");
2918 else if (subtype & RELAX_NOMACRO)
2919 return _("Macro instruction expanded into multiple instructions");
2920 else
2921 return 0;
2922 }
2923
2924 /* Finish up a macro. Emit warnings as appropriate. */
2925
2926 static void
2927 macro_end (void)
2928 {
2929 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2930 {
2931 relax_substateT subtype;
2932
2933 /* Set up the relaxation warning flags. */
2934 subtype = 0;
2935 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2936 subtype |= RELAX_SECOND_LONGER;
2937 if (mips_opts.warn_about_macros)
2938 subtype |= RELAX_NOMACRO;
2939 if (mips_macro_warning.delay_slot_p)
2940 subtype |= RELAX_DELAY_SLOT;
2941
2942 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2943 {
2944 /* Either the macro has a single implementation or both
2945 implementations are longer than 4 bytes. Emit the
2946 warning now. */
2947 const char *msg = macro_warning (subtype);
2948 if (msg != 0)
2949 as_warn (msg);
2950 }
2951 else
2952 {
2953 /* One implementation might need a warning but the other
2954 definitely doesn't. */
2955 mips_macro_warning.first_frag->fr_subtype |= subtype;
2956 }
2957 }
2958 }
2959
2960 /* Build an instruction created by a macro expansion. This is passed
2961 a pointer to the count of instructions created so far, an
2962 expression, the name of the instruction to build, an operand format
2963 string, and corresponding arguments. */
2964
2965 static void
2966 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2967 {
2968 struct mips_cl_insn insn;
2969 bfd_reloc_code_real_type r[3];
2970 va_list args;
2971
2972 va_start (args, fmt);
2973
2974 if (mips_opts.mips16)
2975 {
2976 mips16_macro_build (ep, name, fmt, args);
2977 va_end (args);
2978 return;
2979 }
2980
2981 r[0] = BFD_RELOC_UNUSED;
2982 r[1] = BFD_RELOC_UNUSED;
2983 r[2] = BFD_RELOC_UNUSED;
2984 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2985 assert (insn.insn_mo);
2986 assert (strcmp (name, insn.insn_mo->name) == 0);
2987
2988 /* Search until we get a match for NAME. */
2989 while (1)
2990 {
2991 /* It is assumed here that macros will never generate
2992 MDMX or MIPS-3D instructions. */
2993 if (strcmp (fmt, insn.insn_mo->args) == 0
2994 && insn.insn_mo->pinfo != INSN_MACRO
2995 && OPCODE_IS_MEMBER (insn.insn_mo,
2996 (mips_opts.isa
2997 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
2998 mips_opts.arch)
2999 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3000 break;
3001
3002 ++insn.insn_mo;
3003 assert (insn.insn_mo->name);
3004 assert (strcmp (name, insn.insn_mo->name) == 0);
3005 }
3006
3007 insn.insn_opcode = insn.insn_mo->match;
3008 for (;;)
3009 {
3010 switch (*fmt++)
3011 {
3012 case '\0':
3013 break;
3014
3015 case ',':
3016 case '(':
3017 case ')':
3018 continue;
3019
3020 case '+':
3021 switch (*fmt++)
3022 {
3023 case 'A':
3024 case 'E':
3025 insn.insn_opcode |= (va_arg (args, int)
3026 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3027 continue;
3028
3029 case 'B':
3030 case 'F':
3031 /* Note that in the macro case, these arguments are already
3032 in MSB form. (When handling the instruction in the
3033 non-macro case, these arguments are sizes from which
3034 MSB values must be calculated.) */
3035 insn.insn_opcode |= (va_arg (args, int)
3036 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3037 continue;
3038
3039 case 'C':
3040 case 'G':
3041 case 'H':
3042 /* Note that in the macro case, these arguments are already
3043 in MSBD form. (When handling the instruction in the
3044 non-macro case, these arguments are sizes from which
3045 MSBD values must be calculated.) */
3046 insn.insn_opcode |= (va_arg (args, int)
3047 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3048 continue;
3049
3050 default:
3051 internalError ();
3052 }
3053 continue;
3054
3055 case 't':
3056 case 'w':
3057 case 'E':
3058 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3059 continue;
3060
3061 case 'c':
3062 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3063 continue;
3064
3065 case 'T':
3066 case 'W':
3067 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3068 continue;
3069
3070 case 'd':
3071 case 'G':
3072 case 'K':
3073 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3074 continue;
3075
3076 case 'U':
3077 {
3078 int tmp = va_arg (args, int);
3079
3080 insn.insn_opcode |= tmp << OP_SH_RT;
3081 insn.insn_opcode |= tmp << OP_SH_RD;
3082 continue;
3083 }
3084
3085 case 'V':
3086 case 'S':
3087 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3088 continue;
3089
3090 case 'z':
3091 continue;
3092
3093 case '<':
3094 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3095 continue;
3096
3097 case 'D':
3098 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3099 continue;
3100
3101 case 'B':
3102 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3103 continue;
3104
3105 case 'J':
3106 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3107 continue;
3108
3109 case 'q':
3110 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3111 continue;
3112
3113 case 'b':
3114 case 's':
3115 case 'r':
3116 case 'v':
3117 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3118 continue;
3119
3120 case 'i':
3121 case 'j':
3122 case 'o':
3123 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3124 assert (*r == BFD_RELOC_GPREL16
3125 || *r == BFD_RELOC_MIPS_LITERAL
3126 || *r == BFD_RELOC_MIPS_HIGHER
3127 || *r == BFD_RELOC_HI16_S
3128 || *r == BFD_RELOC_LO16
3129 || *r == BFD_RELOC_MIPS_GOT16
3130 || *r == BFD_RELOC_MIPS_CALL16
3131 || *r == BFD_RELOC_MIPS_GOT_DISP
3132 || *r == BFD_RELOC_MIPS_GOT_PAGE
3133 || *r == BFD_RELOC_MIPS_GOT_OFST
3134 || *r == BFD_RELOC_MIPS_GOT_LO16
3135 || *r == BFD_RELOC_MIPS_CALL_LO16
3136 || (ep->X_op == O_subtract
3137 && *r == BFD_RELOC_PCREL_LO16));
3138 continue;
3139
3140 case 'u':
3141 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3142 assert (ep != NULL
3143 && (ep->X_op == O_constant
3144 || (ep->X_op == O_symbol
3145 && (*r == BFD_RELOC_MIPS_HIGHEST
3146 || *r == BFD_RELOC_HI16_S
3147 || *r == BFD_RELOC_HI16
3148 || *r == BFD_RELOC_GPREL16
3149 || *r == BFD_RELOC_MIPS_GOT_HI16
3150 || *r == BFD_RELOC_MIPS_CALL_HI16))
3151 || (ep->X_op == O_subtract
3152 && *r == BFD_RELOC_PCREL_HI16_S)));
3153 continue;
3154
3155 case 'p':
3156 assert (ep != NULL);
3157 /*
3158 * This allows macro() to pass an immediate expression for
3159 * creating short branches without creating a symbol.
3160 * Note that the expression still might come from the assembly
3161 * input, in which case the value is not checked for range nor
3162 * is a relocation entry generated (yuck).
3163 */
3164 if (ep->X_op == O_constant)
3165 {
3166 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3167 ep = NULL;
3168 }
3169 else
3170 *r = BFD_RELOC_16_PCREL_S2;
3171 continue;
3172
3173 case 'a':
3174 assert (ep != NULL);
3175 *r = BFD_RELOC_MIPS_JMP;
3176 continue;
3177
3178 case 'C':
3179 insn.insn_opcode |= va_arg (args, unsigned long);
3180 continue;
3181
3182 default:
3183 internalError ();
3184 }
3185 break;
3186 }
3187 va_end (args);
3188 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3189
3190 append_insn (&insn, ep, r);
3191 }
3192
3193 static void
3194 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3195 va_list args)
3196 {
3197 struct mips_cl_insn insn;
3198 bfd_reloc_code_real_type r[3]
3199 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3200
3201 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3202 assert (insn.insn_mo);
3203 assert (strcmp (name, insn.insn_mo->name) == 0);
3204
3205 while (strcmp (fmt, insn.insn_mo->args) != 0
3206 || insn.insn_mo->pinfo == INSN_MACRO)
3207 {
3208 ++insn.insn_mo;
3209 assert (insn.insn_mo->name);
3210 assert (strcmp (name, insn.insn_mo->name) == 0);
3211 }
3212
3213 insn.insn_opcode = insn.insn_mo->match;
3214 insn.use_extend = FALSE;
3215
3216 for (;;)
3217 {
3218 int c;
3219
3220 c = *fmt++;
3221 switch (c)
3222 {
3223 case '\0':
3224 break;
3225
3226 case ',':
3227 case '(':
3228 case ')':
3229 continue;
3230
3231 case 'y':
3232 case 'w':
3233 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3234 continue;
3235
3236 case 'x':
3237 case 'v':
3238 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3239 continue;
3240
3241 case 'z':
3242 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3243 continue;
3244
3245 case 'Z':
3246 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3247 continue;
3248
3249 case '0':
3250 case 'S':
3251 case 'P':
3252 case 'R':
3253 continue;
3254
3255 case 'X':
3256 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3257 continue;
3258
3259 case 'Y':
3260 {
3261 int regno;
3262
3263 regno = va_arg (args, int);
3264 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3265 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3266 }
3267 continue;
3268
3269 case '<':
3270 case '>':
3271 case '4':
3272 case '5':
3273 case 'H':
3274 case 'W':
3275 case 'D':
3276 case 'j':
3277 case '8':
3278 case 'V':
3279 case 'C':
3280 case 'U':
3281 case 'k':
3282 case 'K':
3283 case 'p':
3284 case 'q':
3285 {
3286 assert (ep != NULL);
3287
3288 if (ep->X_op != O_constant)
3289 *r = (int) BFD_RELOC_UNUSED + c;
3290 else
3291 {
3292 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3293 FALSE, &insn.insn_opcode, &insn.use_extend,
3294 &insn.extend);
3295 ep = NULL;
3296 *r = BFD_RELOC_UNUSED;
3297 }
3298 }
3299 continue;
3300
3301 case '6':
3302 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3303 continue;
3304 }
3305
3306 break;
3307 }
3308
3309 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3310
3311 append_insn (&insn, ep, r);
3312 }
3313
3314 /*
3315 * Generate a "jalr" instruction with a relocation hint to the called
3316 * function. This occurs in NewABI PIC code.
3317 */
3318 static void
3319 macro_build_jalr (expressionS *ep)
3320 {
3321 char *f = NULL;
3322
3323 if (HAVE_NEWABI)
3324 {
3325 frag_grow (8);
3326 f = frag_more (0);
3327 }
3328 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3329 if (HAVE_NEWABI)
3330 fix_new_exp (frag_now, f - frag_now->fr_literal,
3331 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3332 }
3333
3334 /*
3335 * Generate a "lui" instruction.
3336 */
3337 static void
3338 macro_build_lui (expressionS *ep, int regnum)
3339 {
3340 expressionS high_expr;
3341 struct mips_cl_insn insn;
3342 bfd_reloc_code_real_type r[3]
3343 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3344 const char *name = "lui";
3345 const char *fmt = "t,u";
3346
3347 assert (! mips_opts.mips16);
3348
3349 high_expr = *ep;
3350
3351 if (high_expr.X_op == O_constant)
3352 {
3353 /* we can compute the instruction now without a relocation entry */
3354 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3355 >> 16) & 0xffff;
3356 *r = BFD_RELOC_UNUSED;
3357 }
3358 else
3359 {
3360 assert (ep->X_op == O_symbol);
3361 /* _gp_disp is a special case, used from s_cpload. */
3362 assert (mips_pic == NO_PIC
3363 || (! HAVE_NEWABI
3364 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3365 *r = BFD_RELOC_HI16_S;
3366 }
3367
3368 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3369 assert (insn.insn_mo);
3370 assert (strcmp (name, insn.insn_mo->name) == 0);
3371 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3372
3373 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3374 if (*r == BFD_RELOC_UNUSED)
3375 {
3376 insn.insn_opcode |= high_expr.X_add_number;
3377 append_insn (&insn, NULL, r);
3378 }
3379 else
3380 append_insn (&insn, &high_expr, r);
3381 }
3382
3383 /* Generate a sequence of instructions to do a load or store from a constant
3384 offset off of a base register (breg) into/from a target register (treg),
3385 using AT if necessary. */
3386 static void
3387 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3388 int treg, int breg, int dbl)
3389 {
3390 assert (ep->X_op == O_constant);
3391
3392 /* Sign-extending 32-bit constants makes their handling easier. */
3393 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3394 == ~((bfd_vma) 0x7fffffff)))
3395 {
3396 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3397 as_bad (_("constant too large"));
3398
3399 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3400 - 0x80000000);
3401 }
3402
3403 /* Right now, this routine can only handle signed 32-bit constants. */
3404 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3405 as_warn (_("operand overflow"));
3406
3407 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3408 {
3409 /* Signed 16-bit offset will fit in the op. Easy! */
3410 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3411 }
3412 else
3413 {
3414 /* 32-bit offset, need multiple instructions and AT, like:
3415 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3416 addu $tempreg,$tempreg,$breg
3417 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3418 to handle the complete offset. */
3419 macro_build_lui (ep, AT);
3420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3421 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3422
3423 if (mips_opts.noat)
3424 as_warn (_("Macro used $at after \".set noat\""));
3425 }
3426 }
3427
3428 /* set_at()
3429 * Generates code to set the $at register to true (one)
3430 * if reg is less than the immediate expression.
3431 */
3432 static void
3433 set_at (int reg, int unsignedp)
3434 {
3435 if (imm_expr.X_op == O_constant
3436 && imm_expr.X_add_number >= -0x8000
3437 && imm_expr.X_add_number < 0x8000)
3438 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3439 AT, reg, BFD_RELOC_LO16);
3440 else
3441 {
3442 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3443 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3444 }
3445 }
3446
3447 static void
3448 normalize_constant_expr (expressionS *ex)
3449 {
3450 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3451 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3452 - 0x80000000);
3453 }
3454
3455 /* Warn if an expression is not a constant. */
3456
3457 static void
3458 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3459 {
3460 if (ex->X_op == O_big)
3461 as_bad (_("unsupported large constant"));
3462 else if (ex->X_op != O_constant)
3463 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3464
3465 normalize_constant_expr (ex);
3466 }
3467
3468 /* Count the leading zeroes by performing a binary chop. This is a
3469 bulky bit of source, but performance is a LOT better for the
3470 majority of values than a simple loop to count the bits:
3471 for (lcnt = 0; (lcnt < 32); lcnt++)
3472 if ((v) & (1 << (31 - lcnt)))
3473 break;
3474 However it is not code size friendly, and the gain will drop a bit
3475 on certain cached systems.
3476 */
3477 #define COUNT_TOP_ZEROES(v) \
3478 (((v) & ~0xffff) == 0 \
3479 ? ((v) & ~0xff) == 0 \
3480 ? ((v) & ~0xf) == 0 \
3481 ? ((v) & ~0x3) == 0 \
3482 ? ((v) & ~0x1) == 0 \
3483 ? !(v) \
3484 ? 32 \
3485 : 31 \
3486 : 30 \
3487 : ((v) & ~0x7) == 0 \
3488 ? 29 \
3489 : 28 \
3490 : ((v) & ~0x3f) == 0 \
3491 ? ((v) & ~0x1f) == 0 \
3492 ? 27 \
3493 : 26 \
3494 : ((v) & ~0x7f) == 0 \
3495 ? 25 \
3496 : 24 \
3497 : ((v) & ~0xfff) == 0 \
3498 ? ((v) & ~0x3ff) == 0 \
3499 ? ((v) & ~0x1ff) == 0 \
3500 ? 23 \
3501 : 22 \
3502 : ((v) & ~0x7ff) == 0 \
3503 ? 21 \
3504 : 20 \
3505 : ((v) & ~0x3fff) == 0 \
3506 ? ((v) & ~0x1fff) == 0 \
3507 ? 19 \
3508 : 18 \
3509 : ((v) & ~0x7fff) == 0 \
3510 ? 17 \
3511 : 16 \
3512 : ((v) & ~0xffffff) == 0 \
3513 ? ((v) & ~0xfffff) == 0 \
3514 ? ((v) & ~0x3ffff) == 0 \
3515 ? ((v) & ~0x1ffff) == 0 \
3516 ? 15 \
3517 : 14 \
3518 : ((v) & ~0x7ffff) == 0 \
3519 ? 13 \
3520 : 12 \
3521 : ((v) & ~0x3fffff) == 0 \
3522 ? ((v) & ~0x1fffff) == 0 \
3523 ? 11 \
3524 : 10 \
3525 : ((v) & ~0x7fffff) == 0 \
3526 ? 9 \
3527 : 8 \
3528 : ((v) & ~0xfffffff) == 0 \
3529 ? ((v) & ~0x3ffffff) == 0 \
3530 ? ((v) & ~0x1ffffff) == 0 \
3531 ? 7 \
3532 : 6 \
3533 : ((v) & ~0x7ffffff) == 0 \
3534 ? 5 \
3535 : 4 \
3536 : ((v) & ~0x3fffffff) == 0 \
3537 ? ((v) & ~0x1fffffff) == 0 \
3538 ? 3 \
3539 : 2 \
3540 : ((v) & ~0x7fffffff) == 0 \
3541 ? 1 \
3542 : 0)
3543
3544 /* load_register()
3545 * This routine generates the least number of instructions necessary to load
3546 * an absolute expression value into a register.
3547 */
3548 static void
3549 load_register (int reg, expressionS *ep, int dbl)
3550 {
3551 int freg;
3552 expressionS hi32, lo32;
3553
3554 if (ep->X_op != O_big)
3555 {
3556 assert (ep->X_op == O_constant);
3557
3558 /* Sign-extending 32-bit constants makes their handling easier. */
3559 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3560 == ~((bfd_vma) 0x7fffffff)))
3561 {
3562 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3563 as_bad (_("constant too large"));
3564
3565 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3566 - 0x80000000);
3567 }
3568
3569 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3570 {
3571 /* We can handle 16 bit signed values with an addiu to
3572 $zero. No need to ever use daddiu here, since $zero and
3573 the result are always correct in 32 bit mode. */
3574 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3575 return;
3576 }
3577 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3578 {
3579 /* We can handle 16 bit unsigned values with an ori to
3580 $zero. */
3581 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3582 return;
3583 }
3584 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3585 {
3586 /* 32 bit values require an lui. */
3587 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3588 if ((ep->X_add_number & 0xffff) != 0)
3589 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3590 return;
3591 }
3592 }
3593
3594 /* The value is larger than 32 bits. */
3595
3596 if (HAVE_32BIT_GPRS)
3597 {
3598 as_bad (_("Number (0x%lx) larger than 32 bits"),
3599 (unsigned long) ep->X_add_number);
3600 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3601 return;
3602 }
3603
3604 if (ep->X_op != O_big)
3605 {
3606 hi32 = *ep;
3607 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3608 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3609 hi32.X_add_number &= 0xffffffff;
3610 lo32 = *ep;
3611 lo32.X_add_number &= 0xffffffff;
3612 }
3613 else
3614 {
3615 assert (ep->X_add_number > 2);
3616 if (ep->X_add_number == 3)
3617 generic_bignum[3] = 0;
3618 else if (ep->X_add_number > 4)
3619 as_bad (_("Number larger than 64 bits"));
3620 lo32.X_op = O_constant;
3621 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3622 hi32.X_op = O_constant;
3623 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3624 }
3625
3626 if (hi32.X_add_number == 0)
3627 freg = 0;
3628 else
3629 {
3630 int shift, bit;
3631 unsigned long hi, lo;
3632
3633 if (hi32.X_add_number == (offsetT) 0xffffffff)
3634 {
3635 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3636 {
3637 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3638 return;
3639 }
3640 if (lo32.X_add_number & 0x80000000)
3641 {
3642 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3643 if (lo32.X_add_number & 0xffff)
3644 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3645 return;
3646 }
3647 }
3648
3649 /* Check for 16bit shifted constant. We know that hi32 is
3650 non-zero, so start the mask on the first bit of the hi32
3651 value. */
3652 shift = 17;
3653 do
3654 {
3655 unsigned long himask, lomask;
3656
3657 if (shift < 32)
3658 {
3659 himask = 0xffff >> (32 - shift);
3660 lomask = (0xffff << shift) & 0xffffffff;
3661 }
3662 else
3663 {
3664 himask = 0xffff << (shift - 32);
3665 lomask = 0;
3666 }
3667 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3668 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3669 {
3670 expressionS tmp;
3671
3672 tmp.X_op = O_constant;
3673 if (shift < 32)
3674 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3675 | (lo32.X_add_number >> shift));
3676 else
3677 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3678 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3679 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3680 reg, reg, (shift >= 32) ? shift - 32 : shift);
3681 return;
3682 }
3683 ++shift;
3684 }
3685 while (shift <= (64 - 16));
3686
3687 /* Find the bit number of the lowest one bit, and store the
3688 shifted value in hi/lo. */
3689 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3690 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3691 if (lo != 0)
3692 {
3693 bit = 0;
3694 while ((lo & 1) == 0)
3695 {
3696 lo >>= 1;
3697 ++bit;
3698 }
3699 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3700 hi >>= bit;
3701 }
3702 else
3703 {
3704 bit = 32;
3705 while ((hi & 1) == 0)
3706 {
3707 hi >>= 1;
3708 ++bit;
3709 }
3710 lo = hi;
3711 hi = 0;
3712 }
3713
3714 /* Optimize if the shifted value is a (power of 2) - 1. */
3715 if ((hi == 0 && ((lo + 1) & lo) == 0)
3716 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3717 {
3718 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3719 if (shift != 0)
3720 {
3721 expressionS tmp;
3722
3723 /* This instruction will set the register to be all
3724 ones. */
3725 tmp.X_op = O_constant;
3726 tmp.X_add_number = (offsetT) -1;
3727 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3728 if (bit != 0)
3729 {
3730 bit += shift;
3731 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3732 reg, reg, (bit >= 32) ? bit - 32 : bit);
3733 }
3734 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3735 reg, reg, (shift >= 32) ? shift - 32 : shift);
3736 return;
3737 }
3738 }
3739
3740 /* Sign extend hi32 before calling load_register, because we can
3741 generally get better code when we load a sign extended value. */
3742 if ((hi32.X_add_number & 0x80000000) != 0)
3743 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3744 load_register (reg, &hi32, 0);
3745 freg = reg;
3746 }
3747 if ((lo32.X_add_number & 0xffff0000) == 0)
3748 {
3749 if (freg != 0)
3750 {
3751 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3752 freg = reg;
3753 }
3754 }
3755 else
3756 {
3757 expressionS mid16;
3758
3759 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3760 {
3761 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3762 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3763 return;
3764 }
3765
3766 if (freg != 0)
3767 {
3768 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3769 freg = reg;
3770 }
3771 mid16 = lo32;
3772 mid16.X_add_number >>= 16;
3773 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3774 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3775 freg = reg;
3776 }
3777 if ((lo32.X_add_number & 0xffff) != 0)
3778 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3779 }
3780
3781 static inline void
3782 load_delay_nop (void)
3783 {
3784 if (!gpr_interlocks)
3785 macro_build (NULL, "nop", "");
3786 }
3787
3788 /* Load an address into a register. */
3789
3790 static void
3791 load_address (int reg, expressionS *ep, int *used_at)
3792 {
3793 if (ep->X_op != O_constant
3794 && ep->X_op != O_symbol)
3795 {
3796 as_bad (_("expression too complex"));
3797 ep->X_op = O_constant;
3798 }
3799
3800 if (ep->X_op == O_constant)
3801 {
3802 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3803 return;
3804 }
3805
3806 if (mips_pic == NO_PIC)
3807 {
3808 /* If this is a reference to a GP relative symbol, we want
3809 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3810 Otherwise we want
3811 lui $reg,<sym> (BFD_RELOC_HI16_S)
3812 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3813 If we have an addend, we always use the latter form.
3814
3815 With 64bit address space and a usable $at we want
3816 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3817 lui $at,<sym> (BFD_RELOC_HI16_S)
3818 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3819 daddiu $at,<sym> (BFD_RELOC_LO16)
3820 dsll32 $reg,0
3821 daddu $reg,$reg,$at
3822
3823 If $at is already in use, we use a path which is suboptimal
3824 on superscalar processors.
3825 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3826 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3827 dsll $reg,16
3828 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3829 dsll $reg,16
3830 daddiu $reg,<sym> (BFD_RELOC_LO16)
3831 */
3832 if (HAVE_64BIT_ADDRESSES)
3833 {
3834 /* ??? We don't provide a GP-relative alternative for these macros.
3835 It used not to be possible with the original relaxation code,
3836 but it could be done now. */
3837
3838 if (*used_at == 0 && ! mips_opts.noat)
3839 {
3840 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3841 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3842 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3843 BFD_RELOC_MIPS_HIGHER);
3844 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3845 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3846 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3847 *used_at = 1;
3848 }
3849 else
3850 {
3851 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3852 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3853 BFD_RELOC_MIPS_HIGHER);
3854 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3855 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3856 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3857 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3858 }
3859 }
3860 else
3861 {
3862 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3863 && ! nopic_need_relax (ep->X_add_symbol, 1))
3864 {
3865 relax_start (ep->X_add_symbol);
3866 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3867 mips_gp_register, BFD_RELOC_GPREL16);
3868 relax_switch ();
3869 }
3870 macro_build_lui (ep, reg);
3871 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3872 reg, reg, BFD_RELOC_LO16);
3873 if (mips_relax.sequence)
3874 relax_end ();
3875 }
3876 }
3877 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3878 {
3879 expressionS ex;
3880
3881 /* If this is a reference to an external symbol, we want
3882 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3883 Otherwise we want
3884 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3885 nop
3886 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3887 If there is a constant, it must be added in after.
3888
3889 If we have NewABI, we want
3890 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3891 unless we're referencing a global symbol with a non-zero
3892 offset, in which case cst must be added separately. */
3893 if (HAVE_NEWABI)
3894 {
3895 if (ep->X_add_number)
3896 {
3897 ex.X_add_number = ep->X_add_number;
3898 ep->X_add_number = 0;
3899 relax_start (ep->X_add_symbol);
3900 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3901 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3902 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3903 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3904 ex.X_op = O_constant;
3905 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3906 reg, reg, BFD_RELOC_LO16);
3907 ep->X_add_number = ex.X_add_number;
3908 relax_switch ();
3909 }
3910 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3911 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3912 if (mips_relax.sequence)
3913 relax_end ();
3914 }
3915 else
3916 {
3917 ex.X_add_number = ep->X_add_number;
3918 ep->X_add_number = 0;
3919 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3920 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3921 load_delay_nop ();
3922 relax_start (ep->X_add_symbol);
3923 relax_switch ();
3924 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3925 BFD_RELOC_LO16);
3926 relax_end ();
3927
3928 if (ex.X_add_number != 0)
3929 {
3930 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3931 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3932 ex.X_op = O_constant;
3933 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3934 reg, reg, BFD_RELOC_LO16);
3935 }
3936 }
3937 }
3938 else if (mips_pic == SVR4_PIC)
3939 {
3940 expressionS ex;
3941
3942 /* This is the large GOT case. If this is a reference to an
3943 external symbol, we want
3944 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3945 addu $reg,$reg,$gp
3946 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3947
3948 Otherwise, for a reference to a local symbol in old ABI, we want
3949 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3950 nop
3951 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3952 If there is a constant, it must be added in after.
3953
3954 In the NewABI, for local symbols, with or without offsets, we want:
3955 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3956 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
3957 */
3958 if (HAVE_NEWABI)
3959 {
3960 ex.X_add_number = ep->X_add_number;
3961 ep->X_add_number = 0;
3962 relax_start (ep->X_add_symbol);
3963 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3964 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3965 reg, reg, mips_gp_register);
3966 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3967 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3968 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3969 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3970 else if (ex.X_add_number)
3971 {
3972 ex.X_op = O_constant;
3973 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3974 BFD_RELOC_LO16);
3975 }
3976
3977 ep->X_add_number = ex.X_add_number;
3978 relax_switch ();
3979 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3980 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3981 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3982 BFD_RELOC_MIPS_GOT_OFST);
3983 relax_end ();
3984 }
3985 else
3986 {
3987 ex.X_add_number = ep->X_add_number;
3988 ep->X_add_number = 0;
3989 relax_start (ep->X_add_symbol);
3990 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3991 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3992 reg, reg, mips_gp_register);
3993 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3994 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
3995 relax_switch ();
3996 if (reg_needs_delay (mips_gp_register))
3997 {
3998 /* We need a nop before loading from $gp. This special
3999 check is required because the lui which starts the main
4000 instruction stream does not refer to $gp, and so will not
4001 insert the nop which may be required. */
4002 macro_build (NULL, "nop", "");
4003 }
4004 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4005 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4006 load_delay_nop ();
4007 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4008 BFD_RELOC_LO16);
4009 relax_end ();
4010
4011 if (ex.X_add_number != 0)
4012 {
4013 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4014 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4015 ex.X_op = O_constant;
4016 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4017 BFD_RELOC_LO16);
4018 }
4019 }
4020 }
4021 else if (mips_pic == EMBEDDED_PIC)
4022 {
4023 /* We always do
4024 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
4025 */
4026 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4027 reg, mips_gp_register, BFD_RELOC_GPREL16);
4028 }
4029 else
4030 abort ();
4031 }
4032
4033 /* Move the contents of register SOURCE into register DEST. */
4034
4035 static void
4036 move_register (int dest, int source)
4037 {
4038 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4039 dest, source, 0);
4040 }
4041
4042 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4043 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4044 The two alternatives are:
4045
4046 Global symbol Local sybmol
4047 ------------- ------------
4048 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4049 ... ...
4050 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4051
4052 load_got_offset emits the first instruction and add_got_offset
4053 emits the second for a 16-bit offset or add_got_offset_hilo emits
4054 a sequence to add a 32-bit offset using a scratch register. */
4055
4056 static void
4057 load_got_offset (int dest, expressionS *local)
4058 {
4059 expressionS global;
4060
4061 global = *local;
4062 global.X_add_number = 0;
4063
4064 relax_start (local->X_add_symbol);
4065 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4066 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4067 relax_switch ();
4068 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4069 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4070 relax_end ();
4071 }
4072
4073 static void
4074 add_got_offset (int dest, expressionS *local)
4075 {
4076 expressionS global;
4077
4078 global.X_op = O_constant;
4079 global.X_op_symbol = NULL;
4080 global.X_add_symbol = NULL;
4081 global.X_add_number = local->X_add_number;
4082
4083 relax_start (local->X_add_symbol);
4084 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4085 dest, dest, BFD_RELOC_LO16);
4086 relax_switch ();
4087 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4088 relax_end ();
4089 }
4090
4091 static void
4092 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4093 {
4094 expressionS global;
4095 int hold_mips_optimize;
4096
4097 global.X_op = O_constant;
4098 global.X_op_symbol = NULL;
4099 global.X_add_symbol = NULL;
4100 global.X_add_number = local->X_add_number;
4101
4102 relax_start (local->X_add_symbol);
4103 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4104 relax_switch ();
4105 /* Set mips_optimize around the lui instruction to avoid
4106 inserting an unnecessary nop after the lw. */
4107 hold_mips_optimize = mips_optimize;
4108 mips_optimize = 2;
4109 macro_build_lui (&global, tmp);
4110 mips_optimize = hold_mips_optimize;
4111 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4112 relax_end ();
4113
4114 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4115 }
4116
4117 /*
4118 * Build macros
4119 * This routine implements the seemingly endless macro or synthesized
4120 * instructions and addressing modes in the mips assembly language. Many
4121 * of these macros are simple and are similar to each other. These could
4122 * probably be handled by some kind of table or grammar approach instead of
4123 * this verbose method. Others are not simple macros but are more like
4124 * optimizing code generation.
4125 * One interesting optimization is when several store macros appear
4126 * consecutively that would load AT with the upper half of the same address.
4127 * The ensuing load upper instructions are ommited. This implies some kind
4128 * of global optimization. We currently only optimize within a single macro.
4129 * For many of the load and store macros if the address is specified as a
4130 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4131 * first load register 'at' with zero and use it as the base register. The
4132 * mips assembler simply uses register $zero. Just one tiny optimization
4133 * we're missing.
4134 */
4135 static void
4136 macro (struct mips_cl_insn *ip)
4137 {
4138 register int treg, sreg, dreg, breg;
4139 int tempreg;
4140 int mask;
4141 int used_at = 0;
4142 expressionS expr1;
4143 const char *s;
4144 const char *s2;
4145 const char *fmt;
4146 int likely = 0;
4147 int dbl = 0;
4148 int coproc = 0;
4149 int lr = 0;
4150 int imm = 0;
4151 int call = 0;
4152 int off;
4153 offsetT maxnum;
4154 bfd_reloc_code_real_type r;
4155 int hold_mips_optimize;
4156
4157 assert (! mips_opts.mips16);
4158
4159 treg = (ip->insn_opcode >> 16) & 0x1f;
4160 dreg = (ip->insn_opcode >> 11) & 0x1f;
4161 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4162 mask = ip->insn_mo->mask;
4163
4164 expr1.X_op = O_constant;
4165 expr1.X_op_symbol = NULL;
4166 expr1.X_add_symbol = NULL;
4167 expr1.X_add_number = 1;
4168
4169 switch (mask)
4170 {
4171 case M_DABS:
4172 dbl = 1;
4173 case M_ABS:
4174 /* bgez $a0,.+12
4175 move v0,$a0
4176 sub v0,$zero,$a0
4177 */
4178
4179 mips_emit_delays (TRUE);
4180 ++mips_opts.noreorder;
4181 mips_any_noreorder = 1;
4182
4183 expr1.X_add_number = 8;
4184 macro_build (&expr1, "bgez", "s,p", sreg);
4185 if (dreg == sreg)
4186 macro_build (NULL, "nop", "", 0);
4187 else
4188 move_register (dreg, sreg);
4189 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4190
4191 --mips_opts.noreorder;
4192 return;
4193
4194 case M_ADD_I:
4195 s = "addi";
4196 s2 = "add";
4197 goto do_addi;
4198 case M_ADDU_I:
4199 s = "addiu";
4200 s2 = "addu";
4201 goto do_addi;
4202 case M_DADD_I:
4203 dbl = 1;
4204 s = "daddi";
4205 s2 = "dadd";
4206 goto do_addi;
4207 case M_DADDU_I:
4208 dbl = 1;
4209 s = "daddiu";
4210 s2 = "daddu";
4211 do_addi:
4212 if (imm_expr.X_op == O_constant
4213 && imm_expr.X_add_number >= -0x8000
4214 && imm_expr.X_add_number < 0x8000)
4215 {
4216 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4217 return;
4218 }
4219 load_register (AT, &imm_expr, dbl);
4220 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4221 break;
4222
4223 case M_AND_I:
4224 s = "andi";
4225 s2 = "and";
4226 goto do_bit;
4227 case M_OR_I:
4228 s = "ori";
4229 s2 = "or";
4230 goto do_bit;
4231 case M_NOR_I:
4232 s = "";
4233 s2 = "nor";
4234 goto do_bit;
4235 case M_XOR_I:
4236 s = "xori";
4237 s2 = "xor";
4238 do_bit:
4239 if (imm_expr.X_op == O_constant
4240 && imm_expr.X_add_number >= 0
4241 && imm_expr.X_add_number < 0x10000)
4242 {
4243 if (mask != M_NOR_I)
4244 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4245 else
4246 {
4247 macro_build (&imm_expr, "ori", "t,r,i",
4248 treg, sreg, BFD_RELOC_LO16);
4249 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4250 }
4251 return;
4252 }
4253
4254 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4255 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4256 break;
4257
4258 case M_BEQ_I:
4259 s = "beq";
4260 goto beq_i;
4261 case M_BEQL_I:
4262 s = "beql";
4263 likely = 1;
4264 goto beq_i;
4265 case M_BNE_I:
4266 s = "bne";
4267 goto beq_i;
4268 case M_BNEL_I:
4269 s = "bnel";
4270 likely = 1;
4271 beq_i:
4272 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4273 {
4274 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4275 return;
4276 }
4277 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4278 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4279 break;
4280
4281 case M_BGEL:
4282 likely = 1;
4283 case M_BGE:
4284 if (treg == 0)
4285 {
4286 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4287 return;
4288 }
4289 if (sreg == 0)
4290 {
4291 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4292 return;
4293 }
4294 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4295 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4296 break;
4297
4298 case M_BGTL_I:
4299 likely = 1;
4300 case M_BGT_I:
4301 /* check for > max integer */
4302 maxnum = 0x7fffffff;
4303 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4304 {
4305 maxnum <<= 16;
4306 maxnum |= 0xffff;
4307 maxnum <<= 16;
4308 maxnum |= 0xffff;
4309 }
4310 if (imm_expr.X_op == O_constant
4311 && imm_expr.X_add_number >= maxnum
4312 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4313 {
4314 do_false:
4315 /* result is always false */
4316 if (! likely)
4317 macro_build (NULL, "nop", "", 0);
4318 else
4319 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4320 return;
4321 }
4322 if (imm_expr.X_op != O_constant)
4323 as_bad (_("Unsupported large constant"));
4324 ++imm_expr.X_add_number;
4325 /* FALLTHROUGH */
4326 case M_BGE_I:
4327 case M_BGEL_I:
4328 if (mask == M_BGEL_I)
4329 likely = 1;
4330 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4331 {
4332 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4333 return;
4334 }
4335 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4336 {
4337 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4338 return;
4339 }
4340 maxnum = 0x7fffffff;
4341 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4342 {
4343 maxnum <<= 16;
4344 maxnum |= 0xffff;
4345 maxnum <<= 16;
4346 maxnum |= 0xffff;
4347 }
4348 maxnum = - maxnum - 1;
4349 if (imm_expr.X_op == O_constant
4350 && imm_expr.X_add_number <= maxnum
4351 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4352 {
4353 do_true:
4354 /* result is always true */
4355 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4356 macro_build (&offset_expr, "b", "p");
4357 return;
4358 }
4359 set_at (sreg, 0);
4360 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4361 break;
4362
4363 case M_BGEUL:
4364 likely = 1;
4365 case M_BGEU:
4366 if (treg == 0)
4367 goto do_true;
4368 if (sreg == 0)
4369 {
4370 macro_build (&offset_expr, likely ? "beql" : "beq",
4371 "s,t,p", 0, treg);
4372 return;
4373 }
4374 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4375 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4376 break;
4377
4378 case M_BGTUL_I:
4379 likely = 1;
4380 case M_BGTU_I:
4381 if (sreg == 0
4382 || (HAVE_32BIT_GPRS
4383 && imm_expr.X_op == O_constant
4384 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4385 goto do_false;
4386 if (imm_expr.X_op != O_constant)
4387 as_bad (_("Unsupported large constant"));
4388 ++imm_expr.X_add_number;
4389 /* FALLTHROUGH */
4390 case M_BGEU_I:
4391 case M_BGEUL_I:
4392 if (mask == M_BGEUL_I)
4393 likely = 1;
4394 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4395 goto do_true;
4396 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4397 {
4398 macro_build (&offset_expr, likely ? "bnel" : "bne",
4399 "s,t,p", sreg, 0);
4400 return;
4401 }
4402 set_at (sreg, 1);
4403 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4404 break;
4405
4406 case M_BGTL:
4407 likely = 1;
4408 case M_BGT:
4409 if (treg == 0)
4410 {
4411 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4412 return;
4413 }
4414 if (sreg == 0)
4415 {
4416 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4417 return;
4418 }
4419 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4420 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4421 break;
4422
4423 case M_BGTUL:
4424 likely = 1;
4425 case M_BGTU:
4426 if (treg == 0)
4427 {
4428 macro_build (&offset_expr, likely ? "bnel" : "bne",
4429 "s,t,p", sreg, 0);
4430 return;
4431 }
4432 if (sreg == 0)
4433 goto do_false;
4434 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4435 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4436 break;
4437
4438 case M_BLEL:
4439 likely = 1;
4440 case M_BLE:
4441 if (treg == 0)
4442 {
4443 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4444 return;
4445 }
4446 if (sreg == 0)
4447 {
4448 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4449 return;
4450 }
4451 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4452 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4453 break;
4454
4455 case M_BLEL_I:
4456 likely = 1;
4457 case M_BLE_I:
4458 maxnum = 0x7fffffff;
4459 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4460 {
4461 maxnum <<= 16;
4462 maxnum |= 0xffff;
4463 maxnum <<= 16;
4464 maxnum |= 0xffff;
4465 }
4466 if (imm_expr.X_op == O_constant
4467 && imm_expr.X_add_number >= maxnum
4468 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4469 goto do_true;
4470 if (imm_expr.X_op != O_constant)
4471 as_bad (_("Unsupported large constant"));
4472 ++imm_expr.X_add_number;
4473 /* FALLTHROUGH */
4474 case M_BLT_I:
4475 case M_BLTL_I:
4476 if (mask == M_BLTL_I)
4477 likely = 1;
4478 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4479 {
4480 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4481 return;
4482 }
4483 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4484 {
4485 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4486 return;
4487 }
4488 set_at (sreg, 0);
4489 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4490 break;
4491
4492 case M_BLEUL:
4493 likely = 1;
4494 case M_BLEU:
4495 if (treg == 0)
4496 {
4497 macro_build (&offset_expr, likely ? "beql" : "beq",
4498 "s,t,p", sreg, 0);
4499 return;
4500 }
4501 if (sreg == 0)
4502 goto do_true;
4503 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4504 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4505 break;
4506
4507 case M_BLEUL_I:
4508 likely = 1;
4509 case M_BLEU_I:
4510 if (sreg == 0
4511 || (HAVE_32BIT_GPRS
4512 && imm_expr.X_op == O_constant
4513 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4514 goto do_true;
4515 if (imm_expr.X_op != O_constant)
4516 as_bad (_("Unsupported large constant"));
4517 ++imm_expr.X_add_number;
4518 /* FALLTHROUGH */
4519 case M_BLTU_I:
4520 case M_BLTUL_I:
4521 if (mask == M_BLTUL_I)
4522 likely = 1;
4523 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4524 goto do_false;
4525 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4526 {
4527 macro_build (&offset_expr, likely ? "beql" : "beq",
4528 "s,t,p", sreg, 0);
4529 return;
4530 }
4531 set_at (sreg, 1);
4532 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4533 break;
4534
4535 case M_BLTL:
4536 likely = 1;
4537 case M_BLT:
4538 if (treg == 0)
4539 {
4540 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4541 return;
4542 }
4543 if (sreg == 0)
4544 {
4545 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4546 return;
4547 }
4548 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4549 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4550 break;
4551
4552 case M_BLTUL:
4553 likely = 1;
4554 case M_BLTU:
4555 if (treg == 0)
4556 goto do_false;
4557 if (sreg == 0)
4558 {
4559 macro_build (&offset_expr, likely ? "bnel" : "bne",
4560 "s,t,p", 0, treg);
4561 return;
4562 }
4563 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4564 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4565 break;
4566
4567 case M_DEXT:
4568 {
4569 unsigned long pos;
4570 unsigned long size;
4571
4572 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4573 {
4574 as_bad (_("Unsupported large constant"));
4575 pos = size = 1;
4576 }
4577 else
4578 {
4579 pos = (unsigned long) imm_expr.X_add_number;
4580 size = (unsigned long) imm2_expr.X_add_number;
4581 }
4582
4583 if (pos > 63)
4584 {
4585 as_bad (_("Improper position (%lu)"), pos);
4586 pos = 1;
4587 }
4588 if (size == 0 || size > 64
4589 || (pos + size - 1) > 63)
4590 {
4591 as_bad (_("Improper extract size (%lu, position %lu)"),
4592 size, pos);
4593 size = 1;
4594 }
4595
4596 if (size <= 32 && pos < 32)
4597 {
4598 s = "dext";
4599 fmt = "t,r,+A,+C";
4600 }
4601 else if (size <= 32)
4602 {
4603 s = "dextu";
4604 fmt = "t,r,+E,+H";
4605 }
4606 else
4607 {
4608 s = "dextm";
4609 fmt = "t,r,+A,+G";
4610 }
4611 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4612 }
4613 return;
4614
4615 case M_DINS:
4616 {
4617 unsigned long pos;
4618 unsigned long size;
4619
4620 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4621 {
4622 as_bad (_("Unsupported large constant"));
4623 pos = size = 1;
4624 }
4625 else
4626 {
4627 pos = (unsigned long) imm_expr.X_add_number;
4628 size = (unsigned long) imm2_expr.X_add_number;
4629 }
4630
4631 if (pos > 63)
4632 {
4633 as_bad (_("Improper position (%lu)"), pos);
4634 pos = 1;
4635 }
4636 if (size == 0 || size > 64
4637 || (pos + size - 1) > 63)
4638 {
4639 as_bad (_("Improper insert size (%lu, position %lu)"),
4640 size, pos);
4641 size = 1;
4642 }
4643
4644 if (pos < 32 && (pos + size - 1) < 32)
4645 {
4646 s = "dins";
4647 fmt = "t,r,+A,+B";
4648 }
4649 else if (pos >= 32)
4650 {
4651 s = "dinsu";
4652 fmt = "t,r,+E,+F";
4653 }
4654 else
4655 {
4656 s = "dinsm";
4657 fmt = "t,r,+A,+F";
4658 }
4659 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4660 pos + size - 1);
4661 }
4662 return;
4663
4664 case M_DDIV_3:
4665 dbl = 1;
4666 case M_DIV_3:
4667 s = "mflo";
4668 goto do_div3;
4669 case M_DREM_3:
4670 dbl = 1;
4671 case M_REM_3:
4672 s = "mfhi";
4673 do_div3:
4674 if (treg == 0)
4675 {
4676 as_warn (_("Divide by zero."));
4677 if (mips_trap)
4678 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4679 else
4680 macro_build (NULL, "break", "c", 7);
4681 return;
4682 }
4683
4684 mips_emit_delays (TRUE);
4685 ++mips_opts.noreorder;
4686 mips_any_noreorder = 1;
4687 if (mips_trap)
4688 {
4689 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4690 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4691 }
4692 else
4693 {
4694 expr1.X_add_number = 8;
4695 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4696 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4697 macro_build (NULL, "break", "c", 7);
4698 }
4699 expr1.X_add_number = -1;
4700 load_register (AT, &expr1, dbl);
4701 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4702 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4703 if (dbl)
4704 {
4705 expr1.X_add_number = 1;
4706 load_register (AT, &expr1, dbl);
4707 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4708 }
4709 else
4710 {
4711 expr1.X_add_number = 0x80000000;
4712 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4713 }
4714 if (mips_trap)
4715 {
4716 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4717 /* We want to close the noreorder block as soon as possible, so
4718 that later insns are available for delay slot filling. */
4719 --mips_opts.noreorder;
4720 }
4721 else
4722 {
4723 expr1.X_add_number = 8;
4724 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4725 macro_build (NULL, "nop", "", 0);
4726
4727 /* We want to close the noreorder block as soon as possible, so
4728 that later insns are available for delay slot filling. */
4729 --mips_opts.noreorder;
4730
4731 macro_build (NULL, "break", "c", 6);
4732 }
4733 macro_build (NULL, s, "d", dreg);
4734 break;
4735
4736 case M_DIV_3I:
4737 s = "div";
4738 s2 = "mflo";
4739 goto do_divi;
4740 case M_DIVU_3I:
4741 s = "divu";
4742 s2 = "mflo";
4743 goto do_divi;
4744 case M_REM_3I:
4745 s = "div";
4746 s2 = "mfhi";
4747 goto do_divi;
4748 case M_REMU_3I:
4749 s = "divu";
4750 s2 = "mfhi";
4751 goto do_divi;
4752 case M_DDIV_3I:
4753 dbl = 1;
4754 s = "ddiv";
4755 s2 = "mflo";
4756 goto do_divi;
4757 case M_DDIVU_3I:
4758 dbl = 1;
4759 s = "ddivu";
4760 s2 = "mflo";
4761 goto do_divi;
4762 case M_DREM_3I:
4763 dbl = 1;
4764 s = "ddiv";
4765 s2 = "mfhi";
4766 goto do_divi;
4767 case M_DREMU_3I:
4768 dbl = 1;
4769 s = "ddivu";
4770 s2 = "mfhi";
4771 do_divi:
4772 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4773 {
4774 as_warn (_("Divide by zero."));
4775 if (mips_trap)
4776 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4777 else
4778 macro_build (NULL, "break", "c", 7);
4779 return;
4780 }
4781 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4782 {
4783 if (strcmp (s2, "mflo") == 0)
4784 move_register (dreg, sreg);
4785 else
4786 move_register (dreg, 0);
4787 return;
4788 }
4789 if (imm_expr.X_op == O_constant
4790 && imm_expr.X_add_number == -1
4791 && s[strlen (s) - 1] != 'u')
4792 {
4793 if (strcmp (s2, "mflo") == 0)
4794 {
4795 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4796 }
4797 else
4798 move_register (dreg, 0);
4799 return;
4800 }
4801
4802 load_register (AT, &imm_expr, dbl);
4803 macro_build (NULL, s, "z,s,t", sreg, AT);
4804 macro_build (NULL, s2, "d", dreg);
4805 break;
4806
4807 case M_DIVU_3:
4808 s = "divu";
4809 s2 = "mflo";
4810 goto do_divu3;
4811 case M_REMU_3:
4812 s = "divu";
4813 s2 = "mfhi";
4814 goto do_divu3;
4815 case M_DDIVU_3:
4816 s = "ddivu";
4817 s2 = "mflo";
4818 goto do_divu3;
4819 case M_DREMU_3:
4820 s = "ddivu";
4821 s2 = "mfhi";
4822 do_divu3:
4823 mips_emit_delays (TRUE);
4824 ++mips_opts.noreorder;
4825 mips_any_noreorder = 1;
4826 if (mips_trap)
4827 {
4828 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4829 macro_build (NULL, s, "z,s,t", sreg, treg);
4830 /* We want to close the noreorder block as soon as possible, so
4831 that later insns are available for delay slot filling. */
4832 --mips_opts.noreorder;
4833 }
4834 else
4835 {
4836 expr1.X_add_number = 8;
4837 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4838 macro_build (NULL, s, "z,s,t", sreg, treg);
4839
4840 /* We want to close the noreorder block as soon as possible, so
4841 that later insns are available for delay slot filling. */
4842 --mips_opts.noreorder;
4843 macro_build (NULL, "break", "c", 7);
4844 }
4845 macro_build (NULL, s2, "d", dreg);
4846 return;
4847
4848 case M_DLCA_AB:
4849 dbl = 1;
4850 case M_LCA_AB:
4851 call = 1;
4852 goto do_la;
4853 case M_DLA_AB:
4854 dbl = 1;
4855 case M_LA_AB:
4856 do_la:
4857 /* Load the address of a symbol into a register. If breg is not
4858 zero, we then add a base register to it. */
4859
4860 if (dbl && HAVE_32BIT_GPRS)
4861 as_warn (_("dla used to load 32-bit register"));
4862
4863 if (! dbl && HAVE_64BIT_OBJECTS)
4864 as_warn (_("la used to load 64-bit address"));
4865
4866 if (offset_expr.X_op == O_constant
4867 && offset_expr.X_add_number >= -0x8000
4868 && offset_expr.X_add_number < 0x8000)
4869 {
4870 macro_build (&offset_expr,
4871 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4872 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4873 return;
4874 }
4875
4876 if (treg == breg)
4877 {
4878 tempreg = AT;
4879 used_at = 1;
4880 }
4881 else
4882 {
4883 tempreg = treg;
4884 used_at = 0;
4885 }
4886
4887 /* When generating embedded PIC code, we permit expressions of
4888 the form
4889 la $treg,foo-bar
4890 la $treg,foo-bar($breg)
4891 where bar is an address in the current section. These are used
4892 when getting the addresses of functions. We don't permit
4893 X_add_number to be non-zero, because if the symbol is
4894 external the relaxing code needs to know that any addend is
4895 purely the offset to X_op_symbol. */
4896 if (mips_pic == EMBEDDED_PIC
4897 && offset_expr.X_op == O_subtract
4898 && (symbol_constant_p (offset_expr.X_op_symbol)
4899 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4900 : (symbol_equated_p (offset_expr.X_op_symbol)
4901 && (S_GET_SEGMENT
4902 (symbol_get_value_expression (offset_expr.X_op_symbol)
4903 ->X_add_symbol)
4904 == now_seg)))
4905 && (offset_expr.X_add_number == 0
4906 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4907 {
4908 if (breg == 0)
4909 {
4910 tempreg = treg;
4911 used_at = 0;
4912 macro_build (&offset_expr, "lui", "t,u",
4913 tempreg, BFD_RELOC_PCREL_HI16_S);
4914 }
4915 else
4916 {
4917 macro_build (&offset_expr, "lui", "t,u",
4918 tempreg, BFD_RELOC_PCREL_HI16_S);
4919 macro_build (NULL,
4920 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4921 "d,v,t", tempreg, tempreg, breg);
4922 }
4923 macro_build (&offset_expr,
4924 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4925 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
4926 if (! used_at)
4927 return;
4928 break;
4929 }
4930
4931 if (offset_expr.X_op != O_symbol
4932 && offset_expr.X_op != O_constant)
4933 {
4934 as_bad (_("expression too complex"));
4935 offset_expr.X_op = O_constant;
4936 }
4937
4938 if (offset_expr.X_op == O_constant)
4939 load_register (tempreg, &offset_expr,
4940 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4941 ? (dbl || HAVE_64BIT_ADDRESSES)
4942 : HAVE_64BIT_ADDRESSES));
4943 else if (mips_pic == NO_PIC)
4944 {
4945 /* If this is a reference to a GP relative symbol, we want
4946 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4947 Otherwise we want
4948 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4949 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4950 If we have a constant, we need two instructions anyhow,
4951 so we may as well always use the latter form.
4952
4953 With 64bit address space and a usable $at we want
4954 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4955 lui $at,<sym> (BFD_RELOC_HI16_S)
4956 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4957 daddiu $at,<sym> (BFD_RELOC_LO16)
4958 dsll32 $tempreg,0
4959 daddu $tempreg,$tempreg,$at
4960
4961 If $at is already in use, we use a path which is suboptimal
4962 on superscalar processors.
4963 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4964 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4965 dsll $tempreg,16
4966 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4967 dsll $tempreg,16
4968 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4969 */
4970 if (HAVE_64BIT_ADDRESSES)
4971 {
4972 /* ??? We don't provide a GP-relative alternative for
4973 these macros. It used not to be possible with the
4974 original relaxation code, but it could be done now. */
4975
4976 if (used_at == 0 && ! mips_opts.noat)
4977 {
4978 macro_build (&offset_expr, "lui", "t,u",
4979 tempreg, BFD_RELOC_MIPS_HIGHEST);
4980 macro_build (&offset_expr, "lui", "t,u",
4981 AT, BFD_RELOC_HI16_S);
4982 macro_build (&offset_expr, "daddiu", "t,r,j",
4983 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4984 macro_build (&offset_expr, "daddiu", "t,r,j",
4985 AT, AT, BFD_RELOC_LO16);
4986 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4987 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
4988 used_at = 1;
4989 }
4990 else
4991 {
4992 macro_build (&offset_expr, "lui", "t,u",
4993 tempreg, BFD_RELOC_MIPS_HIGHEST);
4994 macro_build (&offset_expr, "daddiu", "t,r,j",
4995 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4996 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4997 macro_build (&offset_expr, "daddiu", "t,r,j",
4998 tempreg, tempreg, BFD_RELOC_HI16_S);
4999 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5000 macro_build (&offset_expr, "daddiu", "t,r,j",
5001 tempreg, tempreg, BFD_RELOC_LO16);
5002 }
5003 }
5004 else
5005 {
5006 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5007 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5008 {
5009 relax_start (offset_expr.X_add_symbol);
5010 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5011 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5012 relax_switch ();
5013 }
5014 macro_build_lui (&offset_expr, tempreg);
5015 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5016 tempreg, tempreg, BFD_RELOC_LO16);
5017 if (mips_relax.sequence)
5018 relax_end ();
5019 }
5020 }
5021 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5022 {
5023 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5024
5025 /* If this is a reference to an external symbol, and there
5026 is no constant, we want
5027 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5028 or for lca or if tempreg is PIC_CALL_REG
5029 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5030 For a local symbol, we want
5031 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5032 nop
5033 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5034
5035 If we have a small constant, and this is a reference to
5036 an external symbol, we want
5037 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5038 nop
5039 addiu $tempreg,$tempreg,<constant>
5040 For a local symbol, we want the same instruction
5041 sequence, but we output a BFD_RELOC_LO16 reloc on the
5042 addiu instruction.
5043
5044 If we have a large constant, and this is a reference to
5045 an external symbol, we want
5046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5047 lui $at,<hiconstant>
5048 addiu $at,$at,<loconstant>
5049 addu $tempreg,$tempreg,$at
5050 For a local symbol, we want the same instruction
5051 sequence, but we output a BFD_RELOC_LO16 reloc on the
5052 addiu instruction.
5053 */
5054
5055 if (offset_expr.X_add_number == 0)
5056 {
5057 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5058 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5059
5060 relax_start (offset_expr.X_add_symbol);
5061 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5062 lw_reloc_type, mips_gp_register);
5063 if (breg != 0)
5064 {
5065 /* We're going to put in an addu instruction using
5066 tempreg, so we may as well insert the nop right
5067 now. */
5068 load_delay_nop ();
5069 }
5070 relax_switch ();
5071 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5072 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5073 load_delay_nop ();
5074 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5075 tempreg, tempreg, BFD_RELOC_LO16);
5076 relax_end ();
5077 /* FIXME: If breg == 0, and the next instruction uses
5078 $tempreg, then if this variant case is used an extra
5079 nop will be generated. */
5080 }
5081 else if (offset_expr.X_add_number >= -0x8000
5082 && offset_expr.X_add_number < 0x8000)
5083 {
5084 load_got_offset (tempreg, &offset_expr);
5085 load_delay_nop ();
5086 add_got_offset (tempreg, &offset_expr);
5087 }
5088 else
5089 {
5090 expr1.X_add_number = offset_expr.X_add_number;
5091 offset_expr.X_add_number =
5092 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5093 load_got_offset (tempreg, &offset_expr);
5094 offset_expr.X_add_number = expr1.X_add_number;
5095 /* If we are going to add in a base register, and the
5096 target register and the base register are the same,
5097 then we are using AT as a temporary register. Since
5098 we want to load the constant into AT, we add our
5099 current AT (from the global offset table) and the
5100 register into the register now, and pretend we were
5101 not using a base register. */
5102 if (breg == treg)
5103 {
5104 load_delay_nop ();
5105 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5106 treg, AT, breg);
5107 breg = 0;
5108 tempreg = treg;
5109 }
5110 add_got_offset_hilo (tempreg, &offset_expr, AT);
5111 used_at = 1;
5112 }
5113 }
5114 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5115 {
5116 int add_breg_early = 0;
5117
5118 /* If this is a reference to an external, and there is no
5119 constant, or local symbol (*), with or without a
5120 constant, we want
5121 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5122 or for lca or if tempreg is PIC_CALL_REG
5123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5124
5125 If we have a small constant, and this is a reference to
5126 an external symbol, we want
5127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5128 addiu $tempreg,$tempreg,<constant>
5129
5130 If we have a large constant, and this is a reference to
5131 an external symbol, we want
5132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5133 lui $at,<hiconstant>
5134 addiu $at,$at,<loconstant>
5135 addu $tempreg,$tempreg,$at
5136
5137 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5138 local symbols, even though it introduces an additional
5139 instruction. */
5140
5141 if (offset_expr.X_add_number)
5142 {
5143 expr1.X_add_number = offset_expr.X_add_number;
5144 offset_expr.X_add_number = 0;
5145
5146 relax_start (offset_expr.X_add_symbol);
5147 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5148 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5149
5150 if (expr1.X_add_number >= -0x8000
5151 && expr1.X_add_number < 0x8000)
5152 {
5153 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5154 tempreg, tempreg, BFD_RELOC_LO16);
5155 }
5156 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5157 {
5158 int dreg;
5159
5160 /* If we are going to add in a base register, and the
5161 target register and the base register are the same,
5162 then we are using AT as a temporary register. Since
5163 we want to load the constant into AT, we add our
5164 current AT (from the global offset table) and the
5165 register into the register now, and pretend we were
5166 not using a base register. */
5167 if (breg != treg)
5168 dreg = tempreg;
5169 else
5170 {
5171 assert (tempreg == AT);
5172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5173 treg, AT, breg);
5174 dreg = treg;
5175 add_breg_early = 1;
5176 }
5177
5178 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5180 dreg, dreg, AT);
5181
5182 used_at = 1;
5183 }
5184 else
5185 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5186
5187 relax_switch ();
5188 offset_expr.X_add_number = expr1.X_add_number;
5189
5190 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5191 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5192 if (add_breg_early)
5193 {
5194 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5195 treg, tempreg, breg);
5196 breg = 0;
5197 tempreg = treg;
5198 }
5199 relax_end ();
5200 }
5201 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5202 {
5203 relax_start (offset_expr.X_add_symbol);
5204 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5205 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5206 relax_switch ();
5207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5208 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5209 relax_end ();
5210 }
5211 else
5212 {
5213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5214 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5215 }
5216 }
5217 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5218 {
5219 int gpdelay;
5220 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5221 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5222 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5223
5224 /* This is the large GOT case. If this is a reference to an
5225 external symbol, and there is no constant, we want
5226 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5227 addu $tempreg,$tempreg,$gp
5228 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5229 or for lca or if tempreg is PIC_CALL_REG
5230 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5231 addu $tempreg,$tempreg,$gp
5232 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5233 For a local symbol, we want
5234 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5235 nop
5236 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5237
5238 If we have a small constant, and this is a reference to
5239 an external symbol, we want
5240 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5241 addu $tempreg,$tempreg,$gp
5242 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5243 nop
5244 addiu $tempreg,$tempreg,<constant>
5245 For a local symbol, we want
5246 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5247 nop
5248 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5249
5250 If we have a large constant, and this is a reference to
5251 an external symbol, we want
5252 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5253 addu $tempreg,$tempreg,$gp
5254 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5255 lui $at,<hiconstant>
5256 addiu $at,$at,<loconstant>
5257 addu $tempreg,$tempreg,$at
5258 For a local symbol, we want
5259 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5260 lui $at,<hiconstant>
5261 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5262 addu $tempreg,$tempreg,$at
5263 */
5264
5265 expr1.X_add_number = offset_expr.X_add_number;
5266 offset_expr.X_add_number = 0;
5267 relax_start (offset_expr.X_add_symbol);
5268 gpdelay = reg_needs_delay (mips_gp_register);
5269 if (expr1.X_add_number == 0 && breg == 0
5270 && (call || tempreg == PIC_CALL_REG))
5271 {
5272 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5273 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5274 }
5275 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5276 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5277 tempreg, tempreg, mips_gp_register);
5278 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5279 tempreg, lw_reloc_type, tempreg);
5280 if (expr1.X_add_number == 0)
5281 {
5282 if (breg != 0)
5283 {
5284 /* We're going to put in an addu instruction using
5285 tempreg, so we may as well insert the nop right
5286 now. */
5287 load_delay_nop ();
5288 }
5289 }
5290 else if (expr1.X_add_number >= -0x8000
5291 && expr1.X_add_number < 0x8000)
5292 {
5293 load_delay_nop ();
5294 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5295 tempreg, tempreg, BFD_RELOC_LO16);
5296 }
5297 else
5298 {
5299 int dreg;
5300
5301 /* If we are going to add in a base register, and the
5302 target register and the base register are the same,
5303 then we are using AT as a temporary register. Since
5304 we want to load the constant into AT, we add our
5305 current AT (from the global offset table) and the
5306 register into the register now, and pretend we were
5307 not using a base register. */
5308 if (breg != treg)
5309 dreg = tempreg;
5310 else
5311 {
5312 assert (tempreg == AT);
5313 load_delay_nop ();
5314 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5315 treg, AT, breg);
5316 dreg = treg;
5317 }
5318
5319 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5320 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5321
5322 used_at = 1;
5323 }
5324 offset_expr.X_add_number =
5325 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5326 relax_switch ();
5327
5328 if (gpdelay)
5329 {
5330 /* This is needed because this instruction uses $gp, but
5331 the first instruction on the main stream does not. */
5332 macro_build (NULL, "nop", "");
5333 }
5334
5335 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5336 local_reloc_type, mips_gp_register);
5337 if (expr1.X_add_number >= -0x8000
5338 && expr1.X_add_number < 0x8000)
5339 {
5340 load_delay_nop ();
5341 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5342 tempreg, tempreg, BFD_RELOC_LO16);
5343 /* FIXME: If add_number is 0, and there was no base
5344 register, the external symbol case ended with a load,
5345 so if the symbol turns out to not be external, and
5346 the next instruction uses tempreg, an unnecessary nop
5347 will be inserted. */
5348 }
5349 else
5350 {
5351 if (breg == treg)
5352 {
5353 /* We must add in the base register now, as in the
5354 external symbol case. */
5355 assert (tempreg == AT);
5356 load_delay_nop ();
5357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5358 treg, AT, breg);
5359 tempreg = treg;
5360 /* We set breg to 0 because we have arranged to add
5361 it in in both cases. */
5362 breg = 0;
5363 }
5364
5365 macro_build_lui (&expr1, AT);
5366 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5367 AT, AT, BFD_RELOC_LO16);
5368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5369 tempreg, tempreg, AT);
5370 }
5371 relax_end ();
5372 }
5373 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5374 {
5375 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5376 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5377 int add_breg_early = 0;
5378
5379 /* This is the large GOT case. If this is a reference to an
5380 external symbol, and there is no constant, we want
5381 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5382 add $tempreg,$tempreg,$gp
5383 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5384 or for lca or if tempreg is PIC_CALL_REG
5385 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5386 add $tempreg,$tempreg,$gp
5387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5388
5389 If we have a small constant, and this is a reference to
5390 an external symbol, we want
5391 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5392 add $tempreg,$tempreg,$gp
5393 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5394 addi $tempreg,$tempreg,<constant>
5395
5396 If we have a large constant, and this is a reference to
5397 an external symbol, we want
5398 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5399 addu $tempreg,$tempreg,$gp
5400 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5401 lui $at,<hiconstant>
5402 addi $at,$at,<loconstant>
5403 add $tempreg,$tempreg,$at
5404
5405 If we have NewABI, and we know it's a local symbol, we want
5406 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5407 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5408 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5409
5410 relax_start (offset_expr.X_add_symbol);
5411
5412 expr1.X_add_number = offset_expr.X_add_number;
5413 offset_expr.X_add_number = 0;
5414
5415 if (expr1.X_add_number == 0 && breg == 0
5416 && (call || tempreg == PIC_CALL_REG))
5417 {
5418 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5419 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5420 }
5421 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5422 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5423 tempreg, tempreg, mips_gp_register);
5424 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5425 tempreg, lw_reloc_type, tempreg);
5426
5427 if (expr1.X_add_number == 0)
5428 ;
5429 else if (expr1.X_add_number >= -0x8000
5430 && expr1.X_add_number < 0x8000)
5431 {
5432 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5433 tempreg, tempreg, BFD_RELOC_LO16);
5434 }
5435 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5436 {
5437 int dreg;
5438
5439 /* If we are going to add in a base register, and the
5440 target register and the base register are the same,
5441 then we are using AT as a temporary register. Since
5442 we want to load the constant into AT, we add our
5443 current AT (from the global offset table) and the
5444 register into the register now, and pretend we were
5445 not using a base register. */
5446 if (breg != treg)
5447 dreg = tempreg;
5448 else
5449 {
5450 assert (tempreg == AT);
5451 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5452 treg, AT, breg);
5453 dreg = treg;
5454 add_breg_early = 1;
5455 }
5456
5457 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5458 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5459
5460 used_at = 1;
5461 }
5462 else
5463 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5464
5465 relax_switch ();
5466 offset_expr.X_add_number = expr1.X_add_number;
5467 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5468 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5469 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5470 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5471 if (add_breg_early)
5472 {
5473 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5474 treg, tempreg, breg);
5475 breg = 0;
5476 tempreg = treg;
5477 }
5478 relax_end ();
5479 }
5480 else if (mips_pic == EMBEDDED_PIC)
5481 {
5482 /* We use
5483 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5484 */
5485 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5486 mips_gp_register, BFD_RELOC_GPREL16);
5487 }
5488 else
5489 abort ();
5490
5491 if (breg != 0)
5492 {
5493 char *s;
5494
5495 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5496 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5497 else
5498 s = ADDRESS_ADD_INSN;
5499
5500 macro_build (NULL, s, "d,v,t", treg, tempreg, breg);
5501 }
5502
5503 if (! used_at)
5504 return;
5505
5506 break;
5507
5508 case M_J_A:
5509 /* The j instruction may not be used in PIC code, since it
5510 requires an absolute address. We convert it to a b
5511 instruction. */
5512 if (mips_pic == NO_PIC)
5513 macro_build (&offset_expr, "j", "a");
5514 else
5515 macro_build (&offset_expr, "b", "p");
5516 return;
5517
5518 /* The jal instructions must be handled as macros because when
5519 generating PIC code they expand to multi-instruction
5520 sequences. Normally they are simple instructions. */
5521 case M_JAL_1:
5522 dreg = RA;
5523 /* Fall through. */
5524 case M_JAL_2:
5525 if (mips_pic == NO_PIC
5526 || mips_pic == EMBEDDED_PIC)
5527 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5528 else if (mips_pic == SVR4_PIC)
5529 {
5530 if (sreg != PIC_CALL_REG)
5531 as_warn (_("MIPS PIC call to register other than $25"));
5532
5533 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5534 if (! HAVE_NEWABI)
5535 {
5536 if (mips_cprestore_offset < 0)
5537 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5538 else
5539 {
5540 if (! mips_frame_reg_valid)
5541 {
5542 as_warn (_("No .frame pseudo-op used in PIC code"));
5543 /* Quiet this warning. */
5544 mips_frame_reg_valid = 1;
5545 }
5546 if (! mips_cprestore_valid)
5547 {
5548 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5549 /* Quiet this warning. */
5550 mips_cprestore_valid = 1;
5551 }
5552 expr1.X_add_number = mips_cprestore_offset;
5553 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5554 mips_gp_register,
5555 mips_frame_reg,
5556 HAVE_64BIT_ADDRESSES);
5557 }
5558 }
5559 }
5560 else
5561 abort ();
5562
5563 return;
5564
5565 case M_JAL_A:
5566 if (mips_pic == NO_PIC)
5567 macro_build (&offset_expr, "jal", "a");
5568 else if (mips_pic == SVR4_PIC)
5569 {
5570 /* If this is a reference to an external symbol, and we are
5571 using a small GOT, we want
5572 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5573 nop
5574 jalr $ra,$25
5575 nop
5576 lw $gp,cprestore($sp)
5577 The cprestore value is set using the .cprestore
5578 pseudo-op. If we are using a big GOT, we want
5579 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5580 addu $25,$25,$gp
5581 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5582 nop
5583 jalr $ra,$25
5584 nop
5585 lw $gp,cprestore($sp)
5586 If the symbol is not external, we want
5587 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5588 nop
5589 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5590 jalr $ra,$25
5591 nop
5592 lw $gp,cprestore($sp)
5593
5594 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5595 sequences above, minus nops, unless the symbol is local,
5596 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5597 GOT_DISP. */
5598 if (HAVE_NEWABI)
5599 {
5600 if (! mips_big_got)
5601 {
5602 relax_start (offset_expr.X_add_symbol);
5603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5604 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5605 mips_gp_register);
5606 relax_switch ();
5607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5608 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5609 mips_gp_register);
5610 relax_end ();
5611 }
5612 else
5613 {
5614 relax_start (offset_expr.X_add_symbol);
5615 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5616 BFD_RELOC_MIPS_CALL_HI16);
5617 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5618 PIC_CALL_REG, mips_gp_register);
5619 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5620 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5621 PIC_CALL_REG);
5622 relax_switch ();
5623 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5624 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5625 mips_gp_register);
5626 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5627 PIC_CALL_REG, PIC_CALL_REG,
5628 BFD_RELOC_MIPS_GOT_OFST);
5629 relax_end ();
5630 }
5631
5632 macro_build_jalr (&offset_expr);
5633 }
5634 else
5635 {
5636 relax_start (offset_expr.X_add_symbol);
5637 if (! mips_big_got)
5638 {
5639 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5640 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5641 mips_gp_register);
5642 load_delay_nop ();
5643 relax_switch ();
5644 }
5645 else
5646 {
5647 int gpdelay;
5648
5649 gpdelay = reg_needs_delay (mips_gp_register);
5650 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5651 BFD_RELOC_MIPS_CALL_HI16);
5652 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5653 PIC_CALL_REG, mips_gp_register);
5654 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5655 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5656 PIC_CALL_REG);
5657 load_delay_nop ();
5658 relax_switch ();
5659 if (gpdelay)
5660 macro_build (NULL, "nop", "");
5661 }
5662 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5663 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5664 mips_gp_register);
5665 load_delay_nop ();
5666 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5667 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5668 relax_end ();
5669 macro_build_jalr (&offset_expr);
5670
5671 if (mips_cprestore_offset < 0)
5672 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5673 else
5674 {
5675 if (! mips_frame_reg_valid)
5676 {
5677 as_warn (_("No .frame pseudo-op used in PIC code"));
5678 /* Quiet this warning. */
5679 mips_frame_reg_valid = 1;
5680 }
5681 if (! mips_cprestore_valid)
5682 {
5683 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5684 /* Quiet this warning. */
5685 mips_cprestore_valid = 1;
5686 }
5687 if (mips_opts.noreorder)
5688 macro_build (NULL, "nop", "");
5689 expr1.X_add_number = mips_cprestore_offset;
5690 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5691 mips_gp_register,
5692 mips_frame_reg,
5693 HAVE_64BIT_ADDRESSES);
5694 }
5695 }
5696 }
5697 else if (mips_pic == EMBEDDED_PIC)
5698 {
5699 macro_build (&offset_expr, "bal", "p");
5700 /* The linker may expand the call to a longer sequence which
5701 uses $at, so we must break rather than return. */
5702 break;
5703 }
5704 else
5705 abort ();
5706
5707 return;
5708
5709 case M_LB_AB:
5710 s = "lb";
5711 goto ld;
5712 case M_LBU_AB:
5713 s = "lbu";
5714 goto ld;
5715 case M_LH_AB:
5716 s = "lh";
5717 goto ld;
5718 case M_LHU_AB:
5719 s = "lhu";
5720 goto ld;
5721 case M_LW_AB:
5722 s = "lw";
5723 goto ld;
5724 case M_LWC0_AB:
5725 s = "lwc0";
5726 /* Itbl support may require additional care here. */
5727 coproc = 1;
5728 goto ld;
5729 case M_LWC1_AB:
5730 s = "lwc1";
5731 /* Itbl support may require additional care here. */
5732 coproc = 1;
5733 goto ld;
5734 case M_LWC2_AB:
5735 s = "lwc2";
5736 /* Itbl support may require additional care here. */
5737 coproc = 1;
5738 goto ld;
5739 case M_LWC3_AB:
5740 s = "lwc3";
5741 /* Itbl support may require additional care here. */
5742 coproc = 1;
5743 goto ld;
5744 case M_LWL_AB:
5745 s = "lwl";
5746 lr = 1;
5747 goto ld;
5748 case M_LWR_AB:
5749 s = "lwr";
5750 lr = 1;
5751 goto ld;
5752 case M_LDC1_AB:
5753 if (mips_opts.arch == CPU_R4650)
5754 {
5755 as_bad (_("opcode not supported on this processor"));
5756 return;
5757 }
5758 s = "ldc1";
5759 /* Itbl support may require additional care here. */
5760 coproc = 1;
5761 goto ld;
5762 case M_LDC2_AB:
5763 s = "ldc2";
5764 /* Itbl support may require additional care here. */
5765 coproc = 1;
5766 goto ld;
5767 case M_LDC3_AB:
5768 s = "ldc3";
5769 /* Itbl support may require additional care here. */
5770 coproc = 1;
5771 goto ld;
5772 case M_LDL_AB:
5773 s = "ldl";
5774 lr = 1;
5775 goto ld;
5776 case M_LDR_AB:
5777 s = "ldr";
5778 lr = 1;
5779 goto ld;
5780 case M_LL_AB:
5781 s = "ll";
5782 goto ld;
5783 case M_LLD_AB:
5784 s = "lld";
5785 goto ld;
5786 case M_LWU_AB:
5787 s = "lwu";
5788 ld:
5789 if (breg == treg || coproc || lr)
5790 {
5791 tempreg = AT;
5792 used_at = 1;
5793 }
5794 else
5795 {
5796 tempreg = treg;
5797 used_at = 0;
5798 }
5799 goto ld_st;
5800 case M_SB_AB:
5801 s = "sb";
5802 goto st;
5803 case M_SH_AB:
5804 s = "sh";
5805 goto st;
5806 case M_SW_AB:
5807 s = "sw";
5808 goto st;
5809 case M_SWC0_AB:
5810 s = "swc0";
5811 /* Itbl support may require additional care here. */
5812 coproc = 1;
5813 goto st;
5814 case M_SWC1_AB:
5815 s = "swc1";
5816 /* Itbl support may require additional care here. */
5817 coproc = 1;
5818 goto st;
5819 case M_SWC2_AB:
5820 s = "swc2";
5821 /* Itbl support may require additional care here. */
5822 coproc = 1;
5823 goto st;
5824 case M_SWC3_AB:
5825 s = "swc3";
5826 /* Itbl support may require additional care here. */
5827 coproc = 1;
5828 goto st;
5829 case M_SWL_AB:
5830 s = "swl";
5831 goto st;
5832 case M_SWR_AB:
5833 s = "swr";
5834 goto st;
5835 case M_SC_AB:
5836 s = "sc";
5837 goto st;
5838 case M_SCD_AB:
5839 s = "scd";
5840 goto st;
5841 case M_SDC1_AB:
5842 if (mips_opts.arch == CPU_R4650)
5843 {
5844 as_bad (_("opcode not supported on this processor"));
5845 return;
5846 }
5847 s = "sdc1";
5848 coproc = 1;
5849 /* Itbl support may require additional care here. */
5850 goto st;
5851 case M_SDC2_AB:
5852 s = "sdc2";
5853 /* Itbl support may require additional care here. */
5854 coproc = 1;
5855 goto st;
5856 case M_SDC3_AB:
5857 s = "sdc3";
5858 /* Itbl support may require additional care here. */
5859 coproc = 1;
5860 goto st;
5861 case M_SDL_AB:
5862 s = "sdl";
5863 goto st;
5864 case M_SDR_AB:
5865 s = "sdr";
5866 st:
5867 tempreg = AT;
5868 used_at = 1;
5869 ld_st:
5870 /* Itbl support may require additional care here. */
5871 if (mask == M_LWC1_AB
5872 || mask == M_SWC1_AB
5873 || mask == M_LDC1_AB
5874 || mask == M_SDC1_AB
5875 || mask == M_L_DAB
5876 || mask == M_S_DAB)
5877 fmt = "T,o(b)";
5878 else if (coproc)
5879 fmt = "E,o(b)";
5880 else
5881 fmt = "t,o(b)";
5882
5883 /* Sign-extending 32-bit constants makes their handling easier.
5884 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5885 described below. */
5886 if ((! HAVE_64BIT_ADDRESSES
5887 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
5888 && (offset_expr.X_op == O_constant)
5889 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5890 == ~((bfd_vma) 0x7fffffff)))
5891 {
5892 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
5893 as_bad (_("constant too large"));
5894
5895 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5896 ^ 0x80000000) - 0x80000000);
5897 }
5898
5899 /* For embedded PIC, we allow loads where the offset is calculated
5900 by subtracting a symbol in the current segment from an unknown
5901 symbol, relative to a base register, e.g.:
5902 <op> $treg, <sym>-<localsym>($breg)
5903 This is used by the compiler for switch statements. */
5904 if (mips_pic == EMBEDDED_PIC
5905 && offset_expr.X_op == O_subtract
5906 && (symbol_constant_p (offset_expr.X_op_symbol)
5907 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5908 : (symbol_equated_p (offset_expr.X_op_symbol)
5909 && (S_GET_SEGMENT
5910 (symbol_get_value_expression (offset_expr.X_op_symbol)
5911 ->X_add_symbol)
5912 == now_seg)))
5913 && breg != 0
5914 && (offset_expr.X_add_number == 0
5915 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5916 {
5917 /* For this case, we output the instructions:
5918 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5919 addiu $tempreg,$tempreg,$breg
5920 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5921 If the relocation would fit entirely in 16 bits, it would be
5922 nice to emit:
5923 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5924 instead, but that seems quite difficult. */
5925 macro_build (&offset_expr, "lui", "t,u", tempreg,
5926 BFD_RELOC_PCREL_HI16_S);
5927 macro_build (NULL,
5928 ((bfd_arch_bits_per_address (stdoutput) == 32
5929 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5930 ? "addu" : "daddu"),
5931 "d,v,t", tempreg, tempreg, breg);
5932 macro_build (&offset_expr, s, fmt, treg,
5933 BFD_RELOC_PCREL_LO16, tempreg);
5934 if (! used_at)
5935 return;
5936 break;
5937 }
5938
5939 if (offset_expr.X_op != O_constant
5940 && offset_expr.X_op != O_symbol)
5941 {
5942 as_bad (_("expression too complex"));
5943 offset_expr.X_op = O_constant;
5944 }
5945
5946 /* A constant expression in PIC code can be handled just as it
5947 is in non PIC code. */
5948 if (mips_pic == NO_PIC
5949 || offset_expr.X_op == O_constant)
5950 {
5951 /* If this is a reference to a GP relative symbol, and there
5952 is no base register, we want
5953 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5954 Otherwise, if there is no base register, we want
5955 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5956 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5957 If we have a constant, we need two instructions anyhow,
5958 so we always use the latter form.
5959
5960 If we have a base register, and this is a reference to a
5961 GP relative symbol, we want
5962 addu $tempreg,$breg,$gp
5963 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5964 Otherwise we want
5965 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5966 addu $tempreg,$tempreg,$breg
5967 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5968 With a constant we always use the latter case.
5969
5970 With 64bit address space and no base register and $at usable,
5971 we want
5972 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5973 lui $at,<sym> (BFD_RELOC_HI16_S)
5974 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5975 dsll32 $tempreg,0
5976 daddu $tempreg,$at
5977 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5978 If we have a base register, we want
5979 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5980 lui $at,<sym> (BFD_RELOC_HI16_S)
5981 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5982 daddu $at,$breg
5983 dsll32 $tempreg,0
5984 daddu $tempreg,$at
5985 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5986
5987 Without $at we can't generate the optimal path for superscalar
5988 processors here since this would require two temporary registers.
5989 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5990 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5991 dsll $tempreg,16
5992 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5993 dsll $tempreg,16
5994 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5995 If we have a base register, we want
5996 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5997 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5998 dsll $tempreg,16
5999 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6000 dsll $tempreg,16
6001 daddu $tempreg,$tempreg,$breg
6002 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6003
6004 If we have 64-bit addresses, as an optimization, for
6005 addresses which are 32-bit constants (e.g. kseg0/kseg1
6006 addresses) we fall back to the 32-bit address generation
6007 mechanism since it is more efficient. Note that due to
6008 the signed offset used by memory operations, the 32-bit
6009 range is shifted down by 32768 here. This code should
6010 probably attempt to generate 64-bit constants more
6011 efficiently in general.
6012
6013 As an extension for architectures with 64-bit registers,
6014 we don't truncate 64-bit addresses given as literal
6015 constants down to 32 bits, to support existing practice
6016 in the mips64 Linux (the kernel), that compiles source
6017 files with -mabi=64, assembling them as o32 or n32 (with
6018 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6019 the whole kernel is loaded into a memory region that is
6020 addressable with sign-extended 32-bit addresses, it is
6021 wasteful to compute the upper 32 bits of every
6022 non-literal address, that takes more space and time.
6023 Some day this should probably be implemented as an
6024 assembler option, such that the kernel doesn't have to
6025 use such ugly hacks, even though it will still have to
6026 end up converting the binary to ELF32 for a number of
6027 platforms whose boot loaders don't support ELF64
6028 binaries. */
6029 if ((HAVE_64BIT_ADDRESSES
6030 && ! (offset_expr.X_op == O_constant
6031 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6032 || (HAVE_64BIT_GPRS
6033 && offset_expr.X_op == O_constant
6034 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6035 {
6036 /* ??? We don't provide a GP-relative alternative for
6037 these macros. It used not to be possible with the
6038 original relaxation code, but it could be done now. */
6039
6040 if (used_at == 0 && ! mips_opts.noat)
6041 {
6042 macro_build (&offset_expr, "lui", "t,u", tempreg,
6043 BFD_RELOC_MIPS_HIGHEST);
6044 macro_build (&offset_expr, "lui", "t,u", AT,
6045 BFD_RELOC_HI16_S);
6046 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6047 tempreg, BFD_RELOC_MIPS_HIGHER);
6048 if (breg != 0)
6049 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6050 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6051 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6052 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6053 tempreg);
6054 used_at = 1;
6055 }
6056 else
6057 {
6058 macro_build (&offset_expr, "lui", "t,u", tempreg,
6059 BFD_RELOC_MIPS_HIGHEST);
6060 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6061 tempreg, BFD_RELOC_MIPS_HIGHER);
6062 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6063 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6064 tempreg, BFD_RELOC_HI16_S);
6065 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6066 if (breg != 0)
6067 macro_build (NULL, "daddu", "d,v,t",
6068 tempreg, tempreg, breg);
6069 macro_build (&offset_expr, s, fmt, treg,
6070 BFD_RELOC_LO16, tempreg);
6071 }
6072
6073 return;
6074 }
6075
6076 if (offset_expr.X_op == O_constant
6077 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
6078 as_bad (_("load/store address overflow (max 32 bits)"));
6079
6080 if (breg == 0)
6081 {
6082 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6083 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
6084 {
6085 relax_start (offset_expr.X_add_symbol);
6086 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6087 mips_gp_register);
6088 relax_switch ();
6089 used_at = 0;
6090 }
6091 macro_build_lui (&offset_expr, tempreg);
6092 macro_build (&offset_expr, s, fmt, treg,
6093 BFD_RELOC_LO16, tempreg);
6094 if (mips_relax.sequence)
6095 relax_end ();
6096 }
6097 else
6098 {
6099 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6100 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
6101 {
6102 relax_start (offset_expr.X_add_symbol);
6103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6104 tempreg, breg, mips_gp_register);
6105 macro_build (&offset_expr, s, fmt, treg,
6106 BFD_RELOC_GPREL16, tempreg);
6107 relax_switch ();
6108 }
6109 macro_build_lui (&offset_expr, tempreg);
6110 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6111 tempreg, tempreg, breg);
6112 macro_build (&offset_expr, s, fmt, treg,
6113 BFD_RELOC_LO16, tempreg);
6114 if (mips_relax.sequence)
6115 relax_end ();
6116 }
6117 }
6118 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6119 {
6120 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6121
6122 /* If this is a reference to an external symbol, we want
6123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6124 nop
6125 <op> $treg,0($tempreg)
6126 Otherwise we want
6127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6128 nop
6129 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6130 <op> $treg,0($tempreg)
6131
6132 For NewABI, we want
6133 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6134 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6135
6136 If there is a base register, we add it to $tempreg before
6137 the <op>. If there is a constant, we stick it in the
6138 <op> instruction. We don't handle constants larger than
6139 16 bits, because we have no way to load the upper 16 bits
6140 (actually, we could handle them for the subset of cases
6141 in which we are not using $at). */
6142 assert (offset_expr.X_op == O_symbol);
6143 if (HAVE_NEWABI)
6144 {
6145 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6146 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6147 if (breg != 0)
6148 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6149 tempreg, tempreg, breg);
6150 macro_build (&offset_expr, s, fmt, treg,
6151 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6152
6153 if (! used_at)
6154 return;
6155
6156 break;
6157 }
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 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6164 lw_reloc_type, mips_gp_register);
6165 load_delay_nop ();
6166 relax_start (offset_expr.X_add_symbol);
6167 relax_switch ();
6168 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6169 tempreg, BFD_RELOC_LO16);
6170 relax_end ();
6171 if (breg != 0)
6172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6173 tempreg, tempreg, breg);
6174 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6175 }
6176 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6177 {
6178 int gpdelay;
6179
6180 /* If this is a reference to an external symbol, we want
6181 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6182 addu $tempreg,$tempreg,$gp
6183 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6184 <op> $treg,0($tempreg)
6185 Otherwise we want
6186 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6187 nop
6188 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6189 <op> $treg,0($tempreg)
6190 If there is a base register, we add it to $tempreg before
6191 the <op>. If there is a constant, we stick it in the
6192 <op> instruction. We don't handle constants larger than
6193 16 bits, because we have no way to load the upper 16 bits
6194 (actually, we could handle them for the subset of cases
6195 in which we are not using $at). */
6196 assert (offset_expr.X_op == O_symbol);
6197 expr1.X_add_number = offset_expr.X_add_number;
6198 offset_expr.X_add_number = 0;
6199 if (expr1.X_add_number < -0x8000
6200 || expr1.X_add_number >= 0x8000)
6201 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6202 gpdelay = reg_needs_delay (mips_gp_register);
6203 relax_start (offset_expr.X_add_symbol);
6204 macro_build (&offset_expr, "lui", "t,u", tempreg,
6205 BFD_RELOC_MIPS_GOT_HI16);
6206 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6207 mips_gp_register);
6208 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6209 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6210 relax_switch ();
6211 if (gpdelay)
6212 macro_build (NULL, "nop", "");
6213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6214 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6215 load_delay_nop ();
6216 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6217 tempreg, BFD_RELOC_LO16);
6218 relax_end ();
6219
6220 if (breg != 0)
6221 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6222 tempreg, tempreg, breg);
6223 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6224 }
6225 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6226 {
6227 /* If this is a reference to an external symbol, we want
6228 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6229 add $tempreg,$tempreg,$gp
6230 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6231 <op> $treg,<ofst>($tempreg)
6232 Otherwise, for local symbols, we want:
6233 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6234 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6235 assert (offset_expr.X_op == O_symbol);
6236 expr1.X_add_number = offset_expr.X_add_number;
6237 offset_expr.X_add_number = 0;
6238 if (expr1.X_add_number < -0x8000
6239 || expr1.X_add_number >= 0x8000)
6240 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6241 relax_start (offset_expr.X_add_symbol);
6242 macro_build (&offset_expr, "lui", "t,u", tempreg,
6243 BFD_RELOC_MIPS_GOT_HI16);
6244 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6245 mips_gp_register);
6246 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6247 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6248 if (breg != 0)
6249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6250 tempreg, tempreg, breg);
6251 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6252
6253 relax_switch ();
6254 offset_expr.X_add_number = expr1.X_add_number;
6255 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6256 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6257 if (breg != 0)
6258 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6259 tempreg, tempreg, breg);
6260 macro_build (&offset_expr, s, fmt, treg,
6261 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6262 relax_end ();
6263 }
6264 else if (mips_pic == EMBEDDED_PIC)
6265 {
6266 /* If there is no base register, we want
6267 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6268 If there is a base register, we want
6269 addu $tempreg,$breg,$gp
6270 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6271 */
6272 assert (offset_expr.X_op == O_symbol);
6273 if (breg == 0)
6274 {
6275 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6276 mips_gp_register);
6277 used_at = 0;
6278 }
6279 else
6280 {
6281 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6282 tempreg, breg, mips_gp_register);
6283 macro_build (&offset_expr, s, fmt, treg,
6284 BFD_RELOC_GPREL16, tempreg);
6285 }
6286 }
6287 else
6288 abort ();
6289
6290 if (! used_at)
6291 return;
6292
6293 break;
6294
6295 case M_LI:
6296 case M_LI_S:
6297 load_register (treg, &imm_expr, 0);
6298 return;
6299
6300 case M_DLI:
6301 load_register (treg, &imm_expr, 1);
6302 return;
6303
6304 case M_LI_SS:
6305 if (imm_expr.X_op == O_constant)
6306 {
6307 load_register (AT, &imm_expr, 0);
6308 macro_build (NULL, "mtc1", "t,G", AT, treg);
6309 break;
6310 }
6311 else
6312 {
6313 assert (offset_expr.X_op == O_symbol
6314 && strcmp (segment_name (S_GET_SEGMENT
6315 (offset_expr.X_add_symbol)),
6316 ".lit4") == 0
6317 && offset_expr.X_add_number == 0);
6318 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6319 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6320 return;
6321 }
6322
6323 case M_LI_D:
6324 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6325 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6326 order 32 bits of the value and the low order 32 bits are either
6327 zero or in OFFSET_EXPR. */
6328 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6329 {
6330 if (HAVE_64BIT_GPRS)
6331 load_register (treg, &imm_expr, 1);
6332 else
6333 {
6334 int hreg, lreg;
6335
6336 if (target_big_endian)
6337 {
6338 hreg = treg;
6339 lreg = treg + 1;
6340 }
6341 else
6342 {
6343 hreg = treg + 1;
6344 lreg = treg;
6345 }
6346
6347 if (hreg <= 31)
6348 load_register (hreg, &imm_expr, 0);
6349 if (lreg <= 31)
6350 {
6351 if (offset_expr.X_op == O_absent)
6352 move_register (lreg, 0);
6353 else
6354 {
6355 assert (offset_expr.X_op == O_constant);
6356 load_register (lreg, &offset_expr, 0);
6357 }
6358 }
6359 }
6360 return;
6361 }
6362
6363 /* We know that sym is in the .rdata section. First we get the
6364 upper 16 bits of the address. */
6365 if (mips_pic == NO_PIC)
6366 {
6367 macro_build_lui (&offset_expr, AT);
6368 }
6369 else if (mips_pic == SVR4_PIC)
6370 {
6371 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6372 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6373 }
6374 else if (mips_pic == EMBEDDED_PIC)
6375 {
6376 /* For embedded PIC we pick up the entire address off $gp in
6377 a single instruction. */
6378 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT,
6379 mips_gp_register, BFD_RELOC_GPREL16);
6380 offset_expr.X_op = O_constant;
6381 offset_expr.X_add_number = 0;
6382 }
6383 else
6384 abort ();
6385
6386 /* Now we load the register(s). */
6387 if (HAVE_64BIT_GPRS)
6388 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6389 else
6390 {
6391 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6392 if (treg != RA)
6393 {
6394 /* FIXME: How in the world do we deal with the possible
6395 overflow here? */
6396 offset_expr.X_add_number += 4;
6397 macro_build (&offset_expr, "lw", "t,o(b)",
6398 treg + 1, BFD_RELOC_LO16, AT);
6399 }
6400 }
6401 break;
6402
6403 case M_LI_DD:
6404 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6405 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6406 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6407 the value and the low order 32 bits are either zero or in
6408 OFFSET_EXPR. */
6409 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6410 {
6411 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6412 if (HAVE_64BIT_FPRS)
6413 {
6414 assert (HAVE_64BIT_GPRS);
6415 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6416 }
6417 else
6418 {
6419 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6420 if (offset_expr.X_op == O_absent)
6421 macro_build (NULL, "mtc1", "t,G", 0, treg);
6422 else
6423 {
6424 assert (offset_expr.X_op == O_constant);
6425 load_register (AT, &offset_expr, 0);
6426 macro_build (NULL, "mtc1", "t,G", AT, treg);
6427 }
6428 }
6429 break;
6430 }
6431
6432 assert (offset_expr.X_op == O_symbol
6433 && offset_expr.X_add_number == 0);
6434 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6435 if (strcmp (s, ".lit8") == 0)
6436 {
6437 if (mips_opts.isa != ISA_MIPS1)
6438 {
6439 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6440 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6441 return;
6442 }
6443 breg = mips_gp_register;
6444 r = BFD_RELOC_MIPS_LITERAL;
6445 goto dob;
6446 }
6447 else
6448 {
6449 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6450 if (mips_pic == SVR4_PIC)
6451 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6452 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6453 else
6454 {
6455 /* FIXME: This won't work for a 64 bit address. */
6456 macro_build_lui (&offset_expr, AT);
6457 }
6458
6459 if (mips_opts.isa != ISA_MIPS1)
6460 {
6461 macro_build (&offset_expr, "ldc1", "T,o(b)",
6462 treg, BFD_RELOC_LO16, AT);
6463 break;
6464 }
6465 breg = AT;
6466 r = BFD_RELOC_LO16;
6467 goto dob;
6468 }
6469
6470 case M_L_DOB:
6471 if (mips_opts.arch == CPU_R4650)
6472 {
6473 as_bad (_("opcode not supported on this processor"));
6474 return;
6475 }
6476 /* Even on a big endian machine $fn comes before $fn+1. We have
6477 to adjust when loading from memory. */
6478 r = BFD_RELOC_LO16;
6479 dob:
6480 assert (mips_opts.isa == ISA_MIPS1);
6481 macro_build (&offset_expr, "lwc1", "T,o(b)",
6482 target_big_endian ? treg + 1 : treg, r, breg);
6483 /* FIXME: A possible overflow which I don't know how to deal
6484 with. */
6485 offset_expr.X_add_number += 4;
6486 macro_build (&offset_expr, "lwc1", "T,o(b)",
6487 target_big_endian ? treg : treg + 1, r, breg);
6488
6489 if (breg != AT)
6490 return;
6491 break;
6492
6493 case M_L_DAB:
6494 /*
6495 * The MIPS assembler seems to check for X_add_number not
6496 * being double aligned and generating:
6497 * lui at,%hi(foo+1)
6498 * addu at,at,v1
6499 * addiu at,at,%lo(foo+1)
6500 * lwc1 f2,0(at)
6501 * lwc1 f3,4(at)
6502 * But, the resulting address is the same after relocation so why
6503 * generate the extra instruction?
6504 */
6505 if (mips_opts.arch == CPU_R4650)
6506 {
6507 as_bad (_("opcode not supported on this processor"));
6508 return;
6509 }
6510 /* Itbl support may require additional care here. */
6511 coproc = 1;
6512 if (mips_opts.isa != ISA_MIPS1)
6513 {
6514 s = "ldc1";
6515 goto ld;
6516 }
6517
6518 s = "lwc1";
6519 fmt = "T,o(b)";
6520 goto ldd_std;
6521
6522 case M_S_DAB:
6523 if (mips_opts.arch == CPU_R4650)
6524 {
6525 as_bad (_("opcode not supported on this processor"));
6526 return;
6527 }
6528
6529 if (mips_opts.isa != ISA_MIPS1)
6530 {
6531 s = "sdc1";
6532 goto st;
6533 }
6534
6535 s = "swc1";
6536 fmt = "T,o(b)";
6537 /* Itbl support may require additional care here. */
6538 coproc = 1;
6539 goto ldd_std;
6540
6541 case M_LD_AB:
6542 if (HAVE_64BIT_GPRS)
6543 {
6544 s = "ld";
6545 goto ld;
6546 }
6547
6548 s = "lw";
6549 fmt = "t,o(b)";
6550 goto ldd_std;
6551
6552 case M_SD_AB:
6553 if (HAVE_64BIT_GPRS)
6554 {
6555 s = "sd";
6556 goto st;
6557 }
6558
6559 s = "sw";
6560 fmt = "t,o(b)";
6561
6562 ldd_std:
6563 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6564 loads for the case of doing a pair of loads to simulate an 'ld'.
6565 This is not currently done by the compiler, and assembly coders
6566 writing embedded-pic code can cope. */
6567
6568 if (offset_expr.X_op != O_symbol
6569 && offset_expr.X_op != O_constant)
6570 {
6571 as_bad (_("expression too complex"));
6572 offset_expr.X_op = O_constant;
6573 }
6574
6575 /* Even on a big endian machine $fn comes before $fn+1. We have
6576 to adjust when loading from memory. We set coproc if we must
6577 load $fn+1 first. */
6578 /* Itbl support may require additional care here. */
6579 if (! target_big_endian)
6580 coproc = 0;
6581
6582 if (mips_pic == NO_PIC
6583 || offset_expr.X_op == O_constant)
6584 {
6585 /* If this is a reference to a GP relative symbol, we want
6586 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6587 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6588 If we have a base register, we use this
6589 addu $at,$breg,$gp
6590 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6591 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6592 If this is not a GP relative symbol, we want
6593 lui $at,<sym> (BFD_RELOC_HI16_S)
6594 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6595 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6596 If there is a base register, we add it to $at after the
6597 lui instruction. If there is a constant, we always use
6598 the last case. */
6599 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6600 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6601 used_at = 1;
6602 else
6603 {
6604 relax_start (offset_expr.X_add_symbol);
6605 if (breg == 0)
6606 {
6607 tempreg = mips_gp_register;
6608 used_at = 0;
6609 }
6610 else
6611 {
6612 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6613 AT, breg, mips_gp_register);
6614 tempreg = AT;
6615 used_at = 1;
6616 }
6617
6618 /* Itbl support may require additional care here. */
6619 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6620 BFD_RELOC_GPREL16, tempreg);
6621 offset_expr.X_add_number += 4;
6622
6623 /* Set mips_optimize to 2 to avoid inserting an
6624 undesired nop. */
6625 hold_mips_optimize = mips_optimize;
6626 mips_optimize = 2;
6627 /* Itbl support may require additional care here. */
6628 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6629 BFD_RELOC_GPREL16, tempreg);
6630 mips_optimize = hold_mips_optimize;
6631
6632 relax_switch ();
6633
6634 /* We just generated two relocs. When tc_gen_reloc
6635 handles this case, it will skip the first reloc and
6636 handle the second. The second reloc already has an
6637 extra addend of 4, which we added above. We must
6638 subtract it out, and then subtract another 4 to make
6639 the first reloc come out right. The second reloc
6640 will come out right because we are going to add 4 to
6641 offset_expr when we build its instruction below.
6642
6643 If we have a symbol, then we don't want to include
6644 the offset, because it will wind up being included
6645 when we generate the reloc. */
6646
6647 if (offset_expr.X_op == O_constant)
6648 offset_expr.X_add_number -= 8;
6649 else
6650 {
6651 offset_expr.X_add_number = -4;
6652 offset_expr.X_op = O_constant;
6653 }
6654 }
6655 macro_build_lui (&offset_expr, AT);
6656 if (breg != 0)
6657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6658 /* Itbl support may require additional care here. */
6659 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6660 BFD_RELOC_LO16, AT);
6661 /* FIXME: How do we handle overflow here? */
6662 offset_expr.X_add_number += 4;
6663 /* Itbl support may require additional care here. */
6664 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6665 BFD_RELOC_LO16, AT);
6666 if (mips_relax.sequence)
6667 relax_end ();
6668 }
6669 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6670 {
6671 /* If this is a reference to an external symbol, we want
6672 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6673 nop
6674 <op> $treg,0($at)
6675 <op> $treg+1,4($at)
6676 Otherwise we want
6677 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6678 nop
6679 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6680 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6681 If there is a base register we add it to $at before the
6682 lwc1 instructions. If there is a constant we include it
6683 in the lwc1 instructions. */
6684 used_at = 1;
6685 expr1.X_add_number = offset_expr.X_add_number;
6686 if (expr1.X_add_number < -0x8000
6687 || expr1.X_add_number >= 0x8000 - 4)
6688 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6689 load_got_offset (AT, &offset_expr);
6690 load_delay_nop ();
6691 if (breg != 0)
6692 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6693
6694 /* Set mips_optimize to 2 to avoid inserting an undesired
6695 nop. */
6696 hold_mips_optimize = mips_optimize;
6697 mips_optimize = 2;
6698
6699 /* Itbl support may require additional care here. */
6700 relax_start (offset_expr.X_add_symbol);
6701 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6702 BFD_RELOC_LO16, AT);
6703 expr1.X_add_number += 4;
6704 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6705 BFD_RELOC_LO16, AT);
6706 relax_switch ();
6707 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6708 BFD_RELOC_LO16, AT);
6709 offset_expr.X_add_number += 4;
6710 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6711 BFD_RELOC_LO16, AT);
6712 relax_end ();
6713
6714 mips_optimize = hold_mips_optimize;
6715 }
6716 else if (mips_pic == SVR4_PIC)
6717 {
6718 int gpdelay;
6719
6720 /* If this is a reference to an external symbol, we want
6721 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6722 addu $at,$at,$gp
6723 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6724 nop
6725 <op> $treg,0($at)
6726 <op> $treg+1,4($at)
6727 Otherwise we want
6728 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6729 nop
6730 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6731 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6732 If there is a base register we add it to $at before the
6733 lwc1 instructions. If there is a constant we include it
6734 in the lwc1 instructions. */
6735 used_at = 1;
6736 expr1.X_add_number = offset_expr.X_add_number;
6737 offset_expr.X_add_number = 0;
6738 if (expr1.X_add_number < -0x8000
6739 || expr1.X_add_number >= 0x8000 - 4)
6740 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6741 gpdelay = reg_needs_delay (mips_gp_register);
6742 relax_start (offset_expr.X_add_symbol);
6743 macro_build (&offset_expr, "lui", "t,u",
6744 AT, BFD_RELOC_MIPS_GOT_HI16);
6745 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6746 AT, AT, mips_gp_register);
6747 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6748 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6749 load_delay_nop ();
6750 if (breg != 0)
6751 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6752 /* Itbl support may require additional care here. */
6753 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6754 BFD_RELOC_LO16, AT);
6755 expr1.X_add_number += 4;
6756
6757 /* Set mips_optimize to 2 to avoid inserting an undesired
6758 nop. */
6759 hold_mips_optimize = mips_optimize;
6760 mips_optimize = 2;
6761 /* Itbl support may require additional care here. */
6762 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6763 BFD_RELOC_LO16, AT);
6764 mips_optimize = hold_mips_optimize;
6765 expr1.X_add_number -= 4;
6766
6767 relax_switch ();
6768 offset_expr.X_add_number = expr1.X_add_number;
6769 if (gpdelay)
6770 macro_build (NULL, "nop", "");
6771 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6772 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6773 load_delay_nop ();
6774 if (breg != 0)
6775 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6776 /* Itbl support may require additional care here. */
6777 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6778 BFD_RELOC_LO16, AT);
6779 offset_expr.X_add_number += 4;
6780
6781 /* Set mips_optimize to 2 to avoid inserting an undesired
6782 nop. */
6783 hold_mips_optimize = mips_optimize;
6784 mips_optimize = 2;
6785 /* Itbl support may require additional care here. */
6786 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6787 BFD_RELOC_LO16, AT);
6788 mips_optimize = hold_mips_optimize;
6789 relax_end ();
6790 }
6791 else if (mips_pic == EMBEDDED_PIC)
6792 {
6793 /* If there is no base register, we use
6794 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6795 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6796 If we have a base register, we use
6797 addu $at,$breg,$gp
6798 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6799 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6800 */
6801 if (breg == 0)
6802 {
6803 tempreg = mips_gp_register;
6804 used_at = 0;
6805 }
6806 else
6807 {
6808 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6809 AT, breg, mips_gp_register);
6810 tempreg = AT;
6811 used_at = 1;
6812 }
6813
6814 /* Itbl support may require additional care here. */
6815 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6816 BFD_RELOC_GPREL16, tempreg);
6817 offset_expr.X_add_number += 4;
6818 /* Itbl support may require additional care here. */
6819 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6820 BFD_RELOC_GPREL16, tempreg);
6821 }
6822 else
6823 abort ();
6824
6825 if (! used_at)
6826 return;
6827
6828 break;
6829
6830 case M_LD_OB:
6831 s = "lw";
6832 goto sd_ob;
6833 case M_SD_OB:
6834 s = "sw";
6835 sd_ob:
6836 assert (HAVE_32BIT_ADDRESSES);
6837 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6838 offset_expr.X_add_number += 4;
6839 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6840 return;
6841
6842 /* New code added to support COPZ instructions.
6843 This code builds table entries out of the macros in mip_opcodes.
6844 R4000 uses interlocks to handle coproc delays.
6845 Other chips (like the R3000) require nops to be inserted for delays.
6846
6847 FIXME: Currently, we require that the user handle delays.
6848 In order to fill delay slots for non-interlocked chips,
6849 we must have a way to specify delays based on the coprocessor.
6850 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6851 What are the side-effects of the cop instruction?
6852 What cache support might we have and what are its effects?
6853 Both coprocessor & memory require delays. how long???
6854 What registers are read/set/modified?
6855
6856 If an itbl is provided to interpret cop instructions,
6857 this knowledge can be encoded in the itbl spec. */
6858
6859 case M_COP0:
6860 s = "c0";
6861 goto copz;
6862 case M_COP1:
6863 s = "c1";
6864 goto copz;
6865 case M_COP2:
6866 s = "c2";
6867 goto copz;
6868 case M_COP3:
6869 s = "c3";
6870 copz:
6871 /* For now we just do C (same as Cz). The parameter will be
6872 stored in insn_opcode by mips_ip. */
6873 macro_build (NULL, s, "C", ip->insn_opcode);
6874 return;
6875
6876 case M_MOVE:
6877 move_register (dreg, sreg);
6878 return;
6879
6880 #ifdef LOSING_COMPILER
6881 default:
6882 /* Try and see if this is a new itbl instruction.
6883 This code builds table entries out of the macros in mip_opcodes.
6884 FIXME: For now we just assemble the expression and pass it's
6885 value along as a 32-bit immediate.
6886 We may want to have the assembler assemble this value,
6887 so that we gain the assembler's knowledge of delay slots,
6888 symbols, etc.
6889 Would it be more efficient to use mask (id) here? */
6890 if (itbl_have_entries
6891 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6892 {
6893 s = ip->insn_mo->name;
6894 s2 = "cop3";
6895 coproc = ITBL_DECODE_PNUM (immed_expr);;
6896 macro_build (&immed_expr, s, "C");
6897 return;
6898 }
6899 macro2 (ip);
6900 return;
6901 }
6902 if (mips_opts.noat)
6903 as_warn (_("Macro used $at after \".set noat\""));
6904 }
6905
6906 static void
6907 macro2 (struct mips_cl_insn *ip)
6908 {
6909 register int treg, sreg, dreg, breg;
6910 int tempreg;
6911 int mask;
6912 int used_at;
6913 expressionS expr1;
6914 const char *s;
6915 const char *s2;
6916 const char *fmt;
6917 int likely = 0;
6918 int dbl = 0;
6919 int coproc = 0;
6920 int lr = 0;
6921 int imm = 0;
6922 int off;
6923 offsetT maxnum;
6924 bfd_reloc_code_real_type r;
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, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6944 macro_build (NULL, "mflo", "d", dreg);
6945 return;
6946
6947 case M_DMUL_I:
6948 dbl = 1;
6949 case M_MUL_I:
6950 /* The MIPS assembler some times generates shifts and adds. I'm
6951 not trying to be that fancy. GCC should do this for us
6952 anyway. */
6953 load_register (AT, &imm_expr, dbl);
6954 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6955 macro_build (NULL, "mflo", "d", dreg);
6956 break;
6957
6958 case M_DMULO_I:
6959 dbl = 1;
6960 case M_MULO_I:
6961 imm = 1;
6962 goto do_mulo;
6963
6964 case M_DMULO:
6965 dbl = 1;
6966 case M_MULO:
6967 do_mulo:
6968 mips_emit_delays (TRUE);
6969 ++mips_opts.noreorder;
6970 mips_any_noreorder = 1;
6971 if (imm)
6972 load_register (AT, &imm_expr, dbl);
6973 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6974 macro_build (NULL, "mflo", "d", dreg);
6975 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6976 macro_build (NULL, "mfhi", "d", AT);
6977 if (mips_trap)
6978 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6979 else
6980 {
6981 expr1.X_add_number = 8;
6982 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6983 macro_build (NULL, "nop", "", 0);
6984 macro_build (NULL, "break", "c", 6);
6985 }
6986 --mips_opts.noreorder;
6987 macro_build (NULL, "mflo", "d", dreg);
6988 break;
6989
6990 case M_DMULOU_I:
6991 dbl = 1;
6992 case M_MULOU_I:
6993 imm = 1;
6994 goto do_mulou;
6995
6996 case M_DMULOU:
6997 dbl = 1;
6998 case M_MULOU:
6999 do_mulou:
7000 mips_emit_delays (TRUE);
7001 ++mips_opts.noreorder;
7002 mips_any_noreorder = 1;
7003 if (imm)
7004 load_register (AT, &imm_expr, dbl);
7005 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
7006 sreg, imm ? AT : treg);
7007 macro_build (NULL, "mfhi", "d", AT);
7008 macro_build (NULL, "mflo", "d", dreg);
7009 if (mips_trap)
7010 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
7011 else
7012 {
7013 expr1.X_add_number = 8;
7014 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7015 macro_build (NULL, "nop", "", 0);
7016 macro_build (NULL, "break", "c", 6);
7017 }
7018 --mips_opts.noreorder;
7019 break;
7020
7021 case M_DROL:
7022 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7023 {
7024 if (dreg == sreg)
7025 {
7026 tempreg = AT;
7027 used_at = 1;
7028 }
7029 else
7030 {
7031 tempreg = dreg;
7032 used_at = 0;
7033 }
7034 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7035 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
7036 if (used_at)
7037 break;
7038 return;
7039 }
7040 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7041 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7042 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7043 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7044 break;
7045
7046 case M_ROL:
7047 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7048 {
7049 if (dreg == sreg)
7050 {
7051 tempreg = AT;
7052 used_at = 1;
7053 }
7054 else
7055 {
7056 tempreg = dreg;
7057 used_at = 0;
7058 }
7059 macro_build (NULL, "negu", "d,w", tempreg, treg);
7060 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
7061 if (used_at)
7062 break;
7063 return;
7064 }
7065 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7066 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7067 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7068 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7069 break;
7070
7071 case M_DROL_I:
7072 {
7073 unsigned int rot;
7074 char *l, *r;
7075
7076 if (imm_expr.X_op != O_constant)
7077 as_bad (_("Improper rotate count"));
7078 rot = imm_expr.X_add_number & 0x3f;
7079 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7080 {
7081 rot = (64 - rot) & 0x3f;
7082 if (rot >= 32)
7083 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7084 else
7085 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7086 return;
7087 }
7088 if (rot == 0)
7089 {
7090 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7091 return;
7092 }
7093 l = (rot < 0x20) ? "dsll" : "dsll32";
7094 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7095 rot &= 0x1f;
7096 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7097 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7098 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7099 }
7100 break;
7101
7102 case M_ROL_I:
7103 {
7104 unsigned int rot;
7105
7106 if (imm_expr.X_op != O_constant)
7107 as_bad (_("Improper rotate count"));
7108 rot = imm_expr.X_add_number & 0x1f;
7109 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7110 {
7111 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7112 return;
7113 }
7114 if (rot == 0)
7115 {
7116 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7117 return;
7118 }
7119 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7120 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7121 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7122 }
7123 break;
7124
7125 case M_DROR:
7126 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7127 {
7128 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
7129 return;
7130 }
7131 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7132 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7133 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7134 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7135 break;
7136
7137 case M_ROR:
7138 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7139 {
7140 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7141 return;
7142 }
7143 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7144 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7145 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7146 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7147 break;
7148
7149 case M_DROR_I:
7150 {
7151 unsigned int rot;
7152 char *l, *r;
7153
7154 if (imm_expr.X_op != O_constant)
7155 as_bad (_("Improper rotate count"));
7156 rot = imm_expr.X_add_number & 0x3f;
7157 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7158 {
7159 if (rot >= 32)
7160 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7161 else
7162 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7163 return;
7164 }
7165 if (rot == 0)
7166 {
7167 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7168 return;
7169 }
7170 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7171 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7172 rot &= 0x1f;
7173 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7174 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7175 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7176 }
7177 break;
7178
7179 case M_ROR_I:
7180 {
7181 unsigned int rot;
7182
7183 if (imm_expr.X_op != O_constant)
7184 as_bad (_("Improper rotate count"));
7185 rot = imm_expr.X_add_number & 0x1f;
7186 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7187 {
7188 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7189 return;
7190 }
7191 if (rot == 0)
7192 {
7193 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7194 return;
7195 }
7196 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7197 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7198 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7199 }
7200 break;
7201
7202 case M_S_DOB:
7203 if (mips_opts.arch == CPU_R4650)
7204 {
7205 as_bad (_("opcode not supported on this processor"));
7206 return;
7207 }
7208 assert (mips_opts.isa == ISA_MIPS1);
7209 /* Even on a big endian machine $fn comes before $fn+1. We have
7210 to adjust when storing to memory. */
7211 macro_build (&offset_expr, "swc1", "T,o(b)",
7212 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7213 offset_expr.X_add_number += 4;
7214 macro_build (&offset_expr, "swc1", "T,o(b)",
7215 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7216 return;
7217
7218 case M_SEQ:
7219 if (sreg == 0)
7220 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7221 else if (treg == 0)
7222 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7223 else
7224 {
7225 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7226 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7227 }
7228 return;
7229
7230 case M_SEQ_I:
7231 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7232 {
7233 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7234 return;
7235 }
7236 if (sreg == 0)
7237 {
7238 as_warn (_("Instruction %s: result is always false"),
7239 ip->insn_mo->name);
7240 move_register (dreg, 0);
7241 return;
7242 }
7243 if (imm_expr.X_op == O_constant
7244 && imm_expr.X_add_number >= 0
7245 && imm_expr.X_add_number < 0x10000)
7246 {
7247 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7248 used_at = 0;
7249 }
7250 else if (imm_expr.X_op == O_constant
7251 && imm_expr.X_add_number > -0x8000
7252 && imm_expr.X_add_number < 0)
7253 {
7254 imm_expr.X_add_number = -imm_expr.X_add_number;
7255 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7256 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7257 used_at = 0;
7258 }
7259 else
7260 {
7261 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7262 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7263 used_at = 1;
7264 }
7265 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7266 if (used_at)
7267 break;
7268 return;
7269
7270 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7271 s = "slt";
7272 goto sge;
7273 case M_SGEU:
7274 s = "sltu";
7275 sge:
7276 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7277 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7278 return;
7279
7280 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7281 case M_SGEU_I:
7282 if (imm_expr.X_op == O_constant
7283 && imm_expr.X_add_number >= -0x8000
7284 && imm_expr.X_add_number < 0x8000)
7285 {
7286 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7287 dreg, sreg, BFD_RELOC_LO16);
7288 used_at = 0;
7289 }
7290 else
7291 {
7292 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7293 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7294 dreg, sreg, AT);
7295 used_at = 1;
7296 }
7297 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7298 if (used_at)
7299 break;
7300 return;
7301
7302 case M_SGT: /* sreg > treg <==> treg < sreg */
7303 s = "slt";
7304 goto sgt;
7305 case M_SGTU:
7306 s = "sltu";
7307 sgt:
7308 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7309 return;
7310
7311 case M_SGT_I: /* sreg > I <==> I < sreg */
7312 s = "slt";
7313 goto sgti;
7314 case M_SGTU_I:
7315 s = "sltu";
7316 sgti:
7317 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7318 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7319 break;
7320
7321 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7322 s = "slt";
7323 goto sle;
7324 case M_SLEU:
7325 s = "sltu";
7326 sle:
7327 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7328 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7329 return;
7330
7331 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7332 s = "slt";
7333 goto slei;
7334 case M_SLEU_I:
7335 s = "sltu";
7336 slei:
7337 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7338 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7339 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7340 break;
7341
7342 case M_SLT_I:
7343 if (imm_expr.X_op == O_constant
7344 && imm_expr.X_add_number >= -0x8000
7345 && imm_expr.X_add_number < 0x8000)
7346 {
7347 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7348 return;
7349 }
7350 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7351 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7352 break;
7353
7354 case M_SLTU_I:
7355 if (imm_expr.X_op == O_constant
7356 && imm_expr.X_add_number >= -0x8000
7357 && imm_expr.X_add_number < 0x8000)
7358 {
7359 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7360 BFD_RELOC_LO16);
7361 return;
7362 }
7363 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7364 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7365 break;
7366
7367 case M_SNE:
7368 if (sreg == 0)
7369 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7370 else if (treg == 0)
7371 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7372 else
7373 {
7374 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7375 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7376 }
7377 return;
7378
7379 case M_SNE_I:
7380 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7381 {
7382 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7383 return;
7384 }
7385 if (sreg == 0)
7386 {
7387 as_warn (_("Instruction %s: result is always true"),
7388 ip->insn_mo->name);
7389 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7390 dreg, 0, BFD_RELOC_LO16);
7391 return;
7392 }
7393 if (imm_expr.X_op == O_constant
7394 && imm_expr.X_add_number >= 0
7395 && imm_expr.X_add_number < 0x10000)
7396 {
7397 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7398 used_at = 0;
7399 }
7400 else if (imm_expr.X_op == O_constant
7401 && imm_expr.X_add_number > -0x8000
7402 && imm_expr.X_add_number < 0)
7403 {
7404 imm_expr.X_add_number = -imm_expr.X_add_number;
7405 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7406 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7407 used_at = 0;
7408 }
7409 else
7410 {
7411 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7412 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7413 used_at = 1;
7414 }
7415 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7416 if (used_at)
7417 break;
7418 return;
7419
7420 case M_DSUB_I:
7421 dbl = 1;
7422 case M_SUB_I:
7423 if (imm_expr.X_op == O_constant
7424 && imm_expr.X_add_number > -0x8000
7425 && imm_expr.X_add_number <= 0x8000)
7426 {
7427 imm_expr.X_add_number = -imm_expr.X_add_number;
7428 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7429 dreg, sreg, BFD_RELOC_LO16);
7430 return;
7431 }
7432 load_register (AT, &imm_expr, dbl);
7433 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7434 break;
7435
7436 case M_DSUBU_I:
7437 dbl = 1;
7438 case M_SUBU_I:
7439 if (imm_expr.X_op == O_constant
7440 && imm_expr.X_add_number > -0x8000
7441 && imm_expr.X_add_number <= 0x8000)
7442 {
7443 imm_expr.X_add_number = -imm_expr.X_add_number;
7444 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7445 dreg, sreg, BFD_RELOC_LO16);
7446 return;
7447 }
7448 load_register (AT, &imm_expr, dbl);
7449 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7450 break;
7451
7452 case M_TEQ_I:
7453 s = "teq";
7454 goto trap;
7455 case M_TGE_I:
7456 s = "tge";
7457 goto trap;
7458 case M_TGEU_I:
7459 s = "tgeu";
7460 goto trap;
7461 case M_TLT_I:
7462 s = "tlt";
7463 goto trap;
7464 case M_TLTU_I:
7465 s = "tltu";
7466 goto trap;
7467 case M_TNE_I:
7468 s = "tne";
7469 trap:
7470 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7471 macro_build (NULL, s, "s,t", sreg, AT);
7472 break;
7473
7474 case M_TRUNCWS:
7475 case M_TRUNCWD:
7476 assert (mips_opts.isa == ISA_MIPS1);
7477 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7478 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7479
7480 /*
7481 * Is the double cfc1 instruction a bug in the mips assembler;
7482 * or is there a reason for it?
7483 */
7484 mips_emit_delays (TRUE);
7485 ++mips_opts.noreorder;
7486 mips_any_noreorder = 1;
7487 macro_build (NULL, "cfc1", "t,G", treg, RA);
7488 macro_build (NULL, "cfc1", "t,G", treg, RA);
7489 macro_build (NULL, "nop", "");
7490 expr1.X_add_number = 3;
7491 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7492 expr1.X_add_number = 2;
7493 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7494 macro_build (NULL, "ctc1", "t,G", AT, RA);
7495 macro_build (NULL, "nop", "");
7496 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7497 dreg, sreg);
7498 macro_build (NULL, "ctc1", "t,G", treg, RA);
7499 macro_build (NULL, "nop", "");
7500 --mips_opts.noreorder;
7501 break;
7502
7503 case M_ULH:
7504 s = "lb";
7505 goto ulh;
7506 case M_ULHU:
7507 s = "lbu";
7508 ulh:
7509 if (offset_expr.X_add_number >= 0x7fff)
7510 as_bad (_("operand overflow"));
7511 if (! target_big_endian)
7512 ++offset_expr.X_add_number;
7513 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7514 if (! target_big_endian)
7515 --offset_expr.X_add_number;
7516 else
7517 ++offset_expr.X_add_number;
7518 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7519 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7520 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7521 break;
7522
7523 case M_ULD:
7524 s = "ldl";
7525 s2 = "ldr";
7526 off = 7;
7527 goto ulw;
7528 case M_ULW:
7529 s = "lwl";
7530 s2 = "lwr";
7531 off = 3;
7532 ulw:
7533 if (offset_expr.X_add_number >= 0x8000 - off)
7534 as_bad (_("operand overflow"));
7535 if (treg != breg)
7536 tempreg = treg;
7537 else
7538 tempreg = AT;
7539 if (! target_big_endian)
7540 offset_expr.X_add_number += off;
7541 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7542 if (! target_big_endian)
7543 offset_expr.X_add_number -= off;
7544 else
7545 offset_expr.X_add_number += off;
7546 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7547
7548 /* If necessary, move the result in tempreg the final destination. */
7549 if (treg == tempreg)
7550 return;
7551 /* Protect second load's delay slot. */
7552 load_delay_nop ();
7553 move_register (treg, tempreg);
7554 break;
7555
7556 case M_ULD_A:
7557 s = "ldl";
7558 s2 = "ldr";
7559 off = 7;
7560 goto ulwa;
7561 case M_ULW_A:
7562 s = "lwl";
7563 s2 = "lwr";
7564 off = 3;
7565 ulwa:
7566 used_at = 1;
7567 load_address (AT, &offset_expr, &used_at);
7568 if (breg != 0)
7569 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7570 if (! target_big_endian)
7571 expr1.X_add_number = off;
7572 else
7573 expr1.X_add_number = 0;
7574 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7575 if (! target_big_endian)
7576 expr1.X_add_number = 0;
7577 else
7578 expr1.X_add_number = off;
7579 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7580 break;
7581
7582 case M_ULH_A:
7583 case M_ULHU_A:
7584 used_at = 1;
7585 load_address (AT, &offset_expr, &used_at);
7586 if (breg != 0)
7587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7588 if (target_big_endian)
7589 expr1.X_add_number = 0;
7590 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7591 treg, BFD_RELOC_LO16, AT);
7592 if (target_big_endian)
7593 expr1.X_add_number = 1;
7594 else
7595 expr1.X_add_number = 0;
7596 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7597 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7598 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7599 break;
7600
7601 case M_USH:
7602 if (offset_expr.X_add_number >= 0x7fff)
7603 as_bad (_("operand overflow"));
7604 if (target_big_endian)
7605 ++offset_expr.X_add_number;
7606 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7607 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7608 if (target_big_endian)
7609 --offset_expr.X_add_number;
7610 else
7611 ++offset_expr.X_add_number;
7612 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7613 break;
7614
7615 case M_USD:
7616 s = "sdl";
7617 s2 = "sdr";
7618 off = 7;
7619 goto usw;
7620 case M_USW:
7621 s = "swl";
7622 s2 = "swr";
7623 off = 3;
7624 usw:
7625 if (offset_expr.X_add_number >= 0x8000 - off)
7626 as_bad (_("operand overflow"));
7627 if (! target_big_endian)
7628 offset_expr.X_add_number += off;
7629 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7630 if (! target_big_endian)
7631 offset_expr.X_add_number -= off;
7632 else
7633 offset_expr.X_add_number += off;
7634 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7635 return;
7636
7637 case M_USD_A:
7638 s = "sdl";
7639 s2 = "sdr";
7640 off = 7;
7641 goto uswa;
7642 case M_USW_A:
7643 s = "swl";
7644 s2 = "swr";
7645 off = 3;
7646 uswa:
7647 used_at = 1;
7648 load_address (AT, &offset_expr, &used_at);
7649 if (breg != 0)
7650 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7651 if (! target_big_endian)
7652 expr1.X_add_number = off;
7653 else
7654 expr1.X_add_number = 0;
7655 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7656 if (! target_big_endian)
7657 expr1.X_add_number = 0;
7658 else
7659 expr1.X_add_number = off;
7660 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7661 break;
7662
7663 case M_USH_A:
7664 used_at = 1;
7665 load_address (AT, &offset_expr, &used_at);
7666 if (breg != 0)
7667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7668 if (! target_big_endian)
7669 expr1.X_add_number = 0;
7670 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7671 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7672 if (! target_big_endian)
7673 expr1.X_add_number = 1;
7674 else
7675 expr1.X_add_number = 0;
7676 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7677 if (! target_big_endian)
7678 expr1.X_add_number = 0;
7679 else
7680 expr1.X_add_number = 1;
7681 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7682 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7683 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7684 break;
7685
7686 default:
7687 /* FIXME: Check if this is one of the itbl macros, since they
7688 are added dynamically. */
7689 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7690 break;
7691 }
7692 if (mips_opts.noat)
7693 as_warn (_("Macro used $at after \".set noat\""));
7694 }
7695
7696 /* Implement macros in mips16 mode. */
7697
7698 static void
7699 mips16_macro (struct mips_cl_insn *ip)
7700 {
7701 int mask;
7702 int xreg, yreg, zreg, tmp;
7703 expressionS expr1;
7704 int dbl;
7705 const char *s, *s2, *s3;
7706
7707 mask = ip->insn_mo->mask;
7708
7709 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7710 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7711 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7712
7713 expr1.X_op = O_constant;
7714 expr1.X_op_symbol = NULL;
7715 expr1.X_add_symbol = NULL;
7716 expr1.X_add_number = 1;
7717
7718 dbl = 0;
7719
7720 switch (mask)
7721 {
7722 default:
7723 internalError ();
7724
7725 case M_DDIV_3:
7726 dbl = 1;
7727 case M_DIV_3:
7728 s = "mflo";
7729 goto do_div3;
7730 case M_DREM_3:
7731 dbl = 1;
7732 case M_REM_3:
7733 s = "mfhi";
7734 do_div3:
7735 mips_emit_delays (TRUE);
7736 ++mips_opts.noreorder;
7737 mips_any_noreorder = 1;
7738 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7739 expr1.X_add_number = 2;
7740 macro_build (&expr1, "bnez", "x,p", yreg);
7741 macro_build (NULL, "break", "6", 7);
7742
7743 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7744 since that causes an overflow. We should do that as well,
7745 but I don't see how to do the comparisons without a temporary
7746 register. */
7747 --mips_opts.noreorder;
7748 macro_build (NULL, s, "x", zreg);
7749 break;
7750
7751 case M_DIVU_3:
7752 s = "divu";
7753 s2 = "mflo";
7754 goto do_divu3;
7755 case M_REMU_3:
7756 s = "divu";
7757 s2 = "mfhi";
7758 goto do_divu3;
7759 case M_DDIVU_3:
7760 s = "ddivu";
7761 s2 = "mflo";
7762 goto do_divu3;
7763 case M_DREMU_3:
7764 s = "ddivu";
7765 s2 = "mfhi";
7766 do_divu3:
7767 mips_emit_delays (TRUE);
7768 ++mips_opts.noreorder;
7769 mips_any_noreorder = 1;
7770 macro_build (NULL, s, "0,x,y", xreg, yreg);
7771 expr1.X_add_number = 2;
7772 macro_build (&expr1, "bnez", "x,p", yreg);
7773 macro_build (NULL, "break", "6", 7);
7774 --mips_opts.noreorder;
7775 macro_build (NULL, s2, "x", zreg);
7776 break;
7777
7778 case M_DMUL:
7779 dbl = 1;
7780 case M_MUL:
7781 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7782 macro_build (NULL, "mflo", "x", zreg);
7783 return;
7784
7785 case M_DSUBU_I:
7786 dbl = 1;
7787 goto do_subu;
7788 case M_SUBU_I:
7789 do_subu:
7790 if (imm_expr.X_op != O_constant)
7791 as_bad (_("Unsupported large constant"));
7792 imm_expr.X_add_number = -imm_expr.X_add_number;
7793 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7794 break;
7795
7796 case M_SUBU_I_2:
7797 if (imm_expr.X_op != O_constant)
7798 as_bad (_("Unsupported large constant"));
7799 imm_expr.X_add_number = -imm_expr.X_add_number;
7800 macro_build (&imm_expr, "addiu", "x,k", xreg);
7801 break;
7802
7803 case M_DSUBU_I_2:
7804 if (imm_expr.X_op != O_constant)
7805 as_bad (_("Unsupported large constant"));
7806 imm_expr.X_add_number = -imm_expr.X_add_number;
7807 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7808 break;
7809
7810 case M_BEQ:
7811 s = "cmp";
7812 s2 = "bteqz";
7813 goto do_branch;
7814 case M_BNE:
7815 s = "cmp";
7816 s2 = "btnez";
7817 goto do_branch;
7818 case M_BLT:
7819 s = "slt";
7820 s2 = "btnez";
7821 goto do_branch;
7822 case M_BLTU:
7823 s = "sltu";
7824 s2 = "btnez";
7825 goto do_branch;
7826 case M_BLE:
7827 s = "slt";
7828 s2 = "bteqz";
7829 goto do_reverse_branch;
7830 case M_BLEU:
7831 s = "sltu";
7832 s2 = "bteqz";
7833 goto do_reverse_branch;
7834 case M_BGE:
7835 s = "slt";
7836 s2 = "bteqz";
7837 goto do_branch;
7838 case M_BGEU:
7839 s = "sltu";
7840 s2 = "bteqz";
7841 goto do_branch;
7842 case M_BGT:
7843 s = "slt";
7844 s2 = "btnez";
7845 goto do_reverse_branch;
7846 case M_BGTU:
7847 s = "sltu";
7848 s2 = "btnez";
7849
7850 do_reverse_branch:
7851 tmp = xreg;
7852 xreg = yreg;
7853 yreg = tmp;
7854
7855 do_branch:
7856 macro_build (NULL, s, "x,y", xreg, yreg);
7857 macro_build (&offset_expr, s2, "p");
7858 break;
7859
7860 case M_BEQ_I:
7861 s = "cmpi";
7862 s2 = "bteqz";
7863 s3 = "x,U";
7864 goto do_branch_i;
7865 case M_BNE_I:
7866 s = "cmpi";
7867 s2 = "btnez";
7868 s3 = "x,U";
7869 goto do_branch_i;
7870 case M_BLT_I:
7871 s = "slti";
7872 s2 = "btnez";
7873 s3 = "x,8";
7874 goto do_branch_i;
7875 case M_BLTU_I:
7876 s = "sltiu";
7877 s2 = "btnez";
7878 s3 = "x,8";
7879 goto do_branch_i;
7880 case M_BLE_I:
7881 s = "slti";
7882 s2 = "btnez";
7883 s3 = "x,8";
7884 goto do_addone_branch_i;
7885 case M_BLEU_I:
7886 s = "sltiu";
7887 s2 = "btnez";
7888 s3 = "x,8";
7889 goto do_addone_branch_i;
7890 case M_BGE_I:
7891 s = "slti";
7892 s2 = "bteqz";
7893 s3 = "x,8";
7894 goto do_branch_i;
7895 case M_BGEU_I:
7896 s = "sltiu";
7897 s2 = "bteqz";
7898 s3 = "x,8";
7899 goto do_branch_i;
7900 case M_BGT_I:
7901 s = "slti";
7902 s2 = "bteqz";
7903 s3 = "x,8";
7904 goto do_addone_branch_i;
7905 case M_BGTU_I:
7906 s = "sltiu";
7907 s2 = "bteqz";
7908 s3 = "x,8";
7909
7910 do_addone_branch_i:
7911 if (imm_expr.X_op != O_constant)
7912 as_bad (_("Unsupported large constant"));
7913 ++imm_expr.X_add_number;
7914
7915 do_branch_i:
7916 macro_build (&imm_expr, s, s3, xreg);
7917 macro_build (&offset_expr, s2, "p");
7918 break;
7919
7920 case M_ABS:
7921 expr1.X_add_number = 0;
7922 macro_build (&expr1, "slti", "x,8", yreg);
7923 if (xreg != yreg)
7924 move_register (xreg, yreg);
7925 expr1.X_add_number = 2;
7926 macro_build (&expr1, "bteqz", "p");
7927 macro_build (NULL, "neg", "x,w", xreg, xreg);
7928 }
7929 }
7930
7931 /* For consistency checking, verify that all bits are specified either
7932 by the match/mask part of the instruction definition, or by the
7933 operand list. */
7934 static int
7935 validate_mips_insn (const struct mips_opcode *opc)
7936 {
7937 const char *p = opc->args;
7938 char c;
7939 unsigned long used_bits = opc->mask;
7940
7941 if ((used_bits & opc->match) != opc->match)
7942 {
7943 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7944 opc->name, opc->args);
7945 return 0;
7946 }
7947 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7948 while (*p)
7949 switch (c = *p++)
7950 {
7951 case ',': break;
7952 case '(': break;
7953 case ')': break;
7954 case '+':
7955 switch (c = *p++)
7956 {
7957 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7958 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7959 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7960 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7961 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7962 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7963 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7964 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7965 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7966 case 'I': break;
7967 default:
7968 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7969 c, opc->name, opc->args);
7970 return 0;
7971 }
7972 break;
7973 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7974 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7975 case 'A': break;
7976 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7977 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7978 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7979 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7980 case 'F': break;
7981 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7982 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7983 case 'I': break;
7984 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7985 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7986 case 'L': break;
7987 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7988 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7989 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7990 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7991 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7992 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7993 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7994 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7995 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7996 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7997 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7998 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7999 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8000 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8001 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8002 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8003 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8004 case 'f': break;
8005 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8006 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8007 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8008 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8009 case 'l': break;
8010 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8011 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8012 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8013 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8014 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8015 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8016 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8017 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8018 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8019 case 'x': break;
8020 case 'z': break;
8021 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8022 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8023 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8024 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8025 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8026 case '[': break;
8027 case ']': break;
8028 default:
8029 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8030 c, opc->name, opc->args);
8031 return 0;
8032 }
8033 #undef USE_BITS
8034 if (used_bits != 0xffffffff)
8035 {
8036 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8037 ~used_bits & 0xffffffff, opc->name, opc->args);
8038 return 0;
8039 }
8040 return 1;
8041 }
8042
8043 /* This routine assembles an instruction into its binary format. As a
8044 side effect, it sets one of the global variables imm_reloc or
8045 offset_reloc to the type of relocation to do if one of the operands
8046 is an address expression. */
8047
8048 static void
8049 mips_ip (char *str, struct mips_cl_insn *ip)
8050 {
8051 char *s;
8052 const char *args;
8053 char c = 0;
8054 struct mips_opcode *insn;
8055 char *argsStart;
8056 unsigned int regno;
8057 unsigned int lastregno = 0;
8058 unsigned int lastpos = 0;
8059 unsigned int limlo, limhi;
8060 char *s_reset;
8061 char save_c = 0;
8062
8063 insn_error = NULL;
8064
8065 /* If the instruction contains a '.', we first try to match an instruction
8066 including the '.'. Then we try again without the '.'. */
8067 insn = NULL;
8068 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8069 continue;
8070
8071 /* If we stopped on whitespace, then replace the whitespace with null for
8072 the call to hash_find. Save the character we replaced just in case we
8073 have to re-parse the instruction. */
8074 if (ISSPACE (*s))
8075 {
8076 save_c = *s;
8077 *s++ = '\0';
8078 }
8079
8080 insn = (struct mips_opcode *) hash_find (op_hash, str);
8081
8082 /* If we didn't find the instruction in the opcode table, try again, but
8083 this time with just the instruction up to, but not including the
8084 first '.'. */
8085 if (insn == NULL)
8086 {
8087 /* Restore the character we overwrite above (if any). */
8088 if (save_c)
8089 *(--s) = save_c;
8090
8091 /* Scan up to the first '.' or whitespace. */
8092 for (s = str;
8093 *s != '\0' && *s != '.' && !ISSPACE (*s);
8094 ++s)
8095 continue;
8096
8097 /* If we did not find a '.', then we can quit now. */
8098 if (*s != '.')
8099 {
8100 insn_error = "unrecognized opcode";
8101 return;
8102 }
8103
8104 /* Lookup the instruction in the hash table. */
8105 *s++ = '\0';
8106 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8107 {
8108 insn_error = "unrecognized opcode";
8109 return;
8110 }
8111 }
8112
8113 argsStart = s;
8114 for (;;)
8115 {
8116 bfd_boolean ok;
8117
8118 assert (strcmp (insn->name, str) == 0);
8119
8120 if (OPCODE_IS_MEMBER (insn,
8121 (mips_opts.isa
8122 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8123 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8124 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8125 mips_opts.arch))
8126 ok = TRUE;
8127 else
8128 ok = FALSE;
8129
8130 if (insn->pinfo != INSN_MACRO)
8131 {
8132 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8133 ok = FALSE;
8134 }
8135
8136 if (! ok)
8137 {
8138 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8139 && strcmp (insn->name, insn[1].name) == 0)
8140 {
8141 ++insn;
8142 continue;
8143 }
8144 else
8145 {
8146 if (!insn_error)
8147 {
8148 static char buf[100];
8149 sprintf (buf,
8150 _("opcode not supported on this processor: %s (%s)"),
8151 mips_cpu_info_from_arch (mips_opts.arch)->name,
8152 mips_cpu_info_from_isa (mips_opts.isa)->name);
8153 insn_error = buf;
8154 }
8155 if (save_c)
8156 *(--s) = save_c;
8157 return;
8158 }
8159 }
8160
8161 ip->insn_mo = insn;
8162 ip->insn_opcode = insn->match;
8163 insn_error = NULL;
8164 for (args = insn->args;; ++args)
8165 {
8166 int is_mdmx;
8167
8168 s += strspn (s, " \t");
8169 is_mdmx = 0;
8170 switch (*args)
8171 {
8172 case '\0': /* end of args */
8173 if (*s == '\0')
8174 return;
8175 break;
8176
8177 case ',':
8178 if (*s++ == *args)
8179 continue;
8180 s--;
8181 switch (*++args)
8182 {
8183 case 'r':
8184 case 'v':
8185 ip->insn_opcode |= lastregno << OP_SH_RS;
8186 continue;
8187
8188 case 'w':
8189 ip->insn_opcode |= lastregno << OP_SH_RT;
8190 continue;
8191
8192 case 'W':
8193 ip->insn_opcode |= lastregno << OP_SH_FT;
8194 continue;
8195
8196 case 'V':
8197 ip->insn_opcode |= lastregno << OP_SH_FS;
8198 continue;
8199 }
8200 break;
8201
8202 case '(':
8203 /* Handle optional base register.
8204 Either the base register is omitted or
8205 we must have a left paren. */
8206 /* This is dependent on the next operand specifier
8207 is a base register specification. */
8208 assert (args[1] == 'b' || args[1] == '5'
8209 || args[1] == '-' || args[1] == '4');
8210 if (*s == '\0')
8211 return;
8212
8213 case ')': /* these must match exactly */
8214 case '[':
8215 case ']':
8216 if (*s++ == *args)
8217 continue;
8218 break;
8219
8220 case '+': /* Opcode extension character. */
8221 switch (*++args)
8222 {
8223 case 'A': /* ins/ext position, becomes LSB. */
8224 limlo = 0;
8225 limhi = 31;
8226 goto do_lsb;
8227 case 'E':
8228 limlo = 32;
8229 limhi = 63;
8230 goto do_lsb;
8231 do_lsb:
8232 my_getExpression (&imm_expr, s);
8233 check_absolute_expr (ip, &imm_expr);
8234 if ((unsigned long) imm_expr.X_add_number < limlo
8235 || (unsigned long) imm_expr.X_add_number > limhi)
8236 {
8237 as_bad (_("Improper position (%lu)"),
8238 (unsigned long) imm_expr.X_add_number);
8239 imm_expr.X_add_number = limlo;
8240 }
8241 lastpos = imm_expr.X_add_number;
8242 ip->insn_opcode |= (imm_expr.X_add_number
8243 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8244 imm_expr.X_op = O_absent;
8245 s = expr_end;
8246 continue;
8247
8248 case 'B': /* ins size, becomes MSB. */
8249 limlo = 1;
8250 limhi = 32;
8251 goto do_msb;
8252 case 'F':
8253 limlo = 33;
8254 limhi = 64;
8255 goto do_msb;
8256 do_msb:
8257 my_getExpression (&imm_expr, s);
8258 check_absolute_expr (ip, &imm_expr);
8259 /* Check for negative input so that small negative numbers
8260 will not succeed incorrectly. The checks against
8261 (pos+size) transitively check "size" itself,
8262 assuming that "pos" is reasonable. */
8263 if ((long) imm_expr.X_add_number < 0
8264 || ((unsigned long) imm_expr.X_add_number
8265 + lastpos) < limlo
8266 || ((unsigned long) imm_expr.X_add_number
8267 + lastpos) > limhi)
8268 {
8269 as_bad (_("Improper insert size (%lu, position %lu)"),
8270 (unsigned long) imm_expr.X_add_number,
8271 (unsigned long) lastpos);
8272 imm_expr.X_add_number = limlo - lastpos;
8273 }
8274 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8275 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8276 imm_expr.X_op = O_absent;
8277 s = expr_end;
8278 continue;
8279
8280 case 'C': /* ext size, becomes MSBD. */
8281 limlo = 1;
8282 limhi = 32;
8283 goto do_msbd;
8284 case 'G':
8285 limlo = 33;
8286 limhi = 64;
8287 goto do_msbd;
8288 case 'H':
8289 limlo = 33;
8290 limhi = 64;
8291 goto do_msbd;
8292 do_msbd:
8293 my_getExpression (&imm_expr, s);
8294 check_absolute_expr (ip, &imm_expr);
8295 /* Check for negative input so that small negative numbers
8296 will not succeed incorrectly. The checks against
8297 (pos+size) transitively check "size" itself,
8298 assuming that "pos" is reasonable. */
8299 if ((long) imm_expr.X_add_number < 0
8300 || ((unsigned long) imm_expr.X_add_number
8301 + lastpos) < limlo
8302 || ((unsigned long) imm_expr.X_add_number
8303 + lastpos) > limhi)
8304 {
8305 as_bad (_("Improper extract size (%lu, position %lu)"),
8306 (unsigned long) imm_expr.X_add_number,
8307 (unsigned long) lastpos);
8308 imm_expr.X_add_number = limlo - lastpos;
8309 }
8310 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8311 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8312 imm_expr.X_op = O_absent;
8313 s = expr_end;
8314 continue;
8315
8316 case 'D':
8317 /* +D is for disassembly only; never match. */
8318 break;
8319
8320 case 'I':
8321 /* "+I" is like "I", except that imm2_expr is used. */
8322 my_getExpression (&imm2_expr, s);
8323 if (imm2_expr.X_op != O_big
8324 && imm2_expr.X_op != O_constant)
8325 insn_error = _("absolute expression required");
8326 normalize_constant_expr (&imm2_expr);
8327 s = expr_end;
8328 continue;
8329
8330 default:
8331 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8332 *args, insn->name, insn->args);
8333 /* Further processing is fruitless. */
8334 return;
8335 }
8336 break;
8337
8338 case '<': /* must be at least one digit */
8339 /*
8340 * According to the manual, if the shift amount is greater
8341 * than 31 or less than 0, then the shift amount should be
8342 * mod 32. In reality the mips assembler issues an error.
8343 * We issue a warning and mask out all but the low 5 bits.
8344 */
8345 my_getExpression (&imm_expr, s);
8346 check_absolute_expr (ip, &imm_expr);
8347 if ((unsigned long) imm_expr.X_add_number > 31)
8348 {
8349 as_warn (_("Improper shift amount (%lu)"),
8350 (unsigned long) imm_expr.X_add_number);
8351 imm_expr.X_add_number &= OP_MASK_SHAMT;
8352 }
8353 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8354 imm_expr.X_op = O_absent;
8355 s = expr_end;
8356 continue;
8357
8358 case '>': /* shift amount minus 32 */
8359 my_getExpression (&imm_expr, s);
8360 check_absolute_expr (ip, &imm_expr);
8361 if ((unsigned long) imm_expr.X_add_number < 32
8362 || (unsigned long) imm_expr.X_add_number > 63)
8363 break;
8364 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8365 imm_expr.X_op = O_absent;
8366 s = expr_end;
8367 continue;
8368
8369 case 'k': /* cache code */
8370 case 'h': /* prefx code */
8371 my_getExpression (&imm_expr, s);
8372 check_absolute_expr (ip, &imm_expr);
8373 if ((unsigned long) imm_expr.X_add_number > 31)
8374 {
8375 as_warn (_("Invalid value for `%s' (%lu)"),
8376 ip->insn_mo->name,
8377 (unsigned long) imm_expr.X_add_number);
8378 imm_expr.X_add_number &= 0x1f;
8379 }
8380 if (*args == 'k')
8381 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8382 else
8383 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8384 imm_expr.X_op = O_absent;
8385 s = expr_end;
8386 continue;
8387
8388 case 'c': /* break code */
8389 my_getExpression (&imm_expr, s);
8390 check_absolute_expr (ip, &imm_expr);
8391 if ((unsigned long) imm_expr.X_add_number > 1023)
8392 {
8393 as_warn (_("Illegal break code (%lu)"),
8394 (unsigned long) imm_expr.X_add_number);
8395 imm_expr.X_add_number &= OP_MASK_CODE;
8396 }
8397 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8398 imm_expr.X_op = O_absent;
8399 s = expr_end;
8400 continue;
8401
8402 case 'q': /* lower break code */
8403 my_getExpression (&imm_expr, s);
8404 check_absolute_expr (ip, &imm_expr);
8405 if ((unsigned long) imm_expr.X_add_number > 1023)
8406 {
8407 as_warn (_("Illegal lower break code (%lu)"),
8408 (unsigned long) imm_expr.X_add_number);
8409 imm_expr.X_add_number &= OP_MASK_CODE2;
8410 }
8411 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8412 imm_expr.X_op = O_absent;
8413 s = expr_end;
8414 continue;
8415
8416 case 'B': /* 20-bit syscall/break code. */
8417 my_getExpression (&imm_expr, s);
8418 check_absolute_expr (ip, &imm_expr);
8419 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8420 as_warn (_("Illegal 20-bit code (%lu)"),
8421 (unsigned long) imm_expr.X_add_number);
8422 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8423 imm_expr.X_op = O_absent;
8424 s = expr_end;
8425 continue;
8426
8427 case 'C': /* Coprocessor code */
8428 my_getExpression (&imm_expr, s);
8429 check_absolute_expr (ip, &imm_expr);
8430 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8431 {
8432 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8433 (unsigned long) imm_expr.X_add_number);
8434 imm_expr.X_add_number &= ((1 << 25) - 1);
8435 }
8436 ip->insn_opcode |= imm_expr.X_add_number;
8437 imm_expr.X_op = O_absent;
8438 s = expr_end;
8439 continue;
8440
8441 case 'J': /* 19-bit wait code. */
8442 my_getExpression (&imm_expr, s);
8443 check_absolute_expr (ip, &imm_expr);
8444 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8445 as_warn (_("Illegal 19-bit code (%lu)"),
8446 (unsigned long) imm_expr.X_add_number);
8447 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8448 imm_expr.X_op = O_absent;
8449 s = expr_end;
8450 continue;
8451
8452 case 'P': /* Performance register */
8453 my_getExpression (&imm_expr, s);
8454 check_absolute_expr (ip, &imm_expr);
8455 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8456 {
8457 as_warn (_("Invalid performance register (%lu)"),
8458 (unsigned long) imm_expr.X_add_number);
8459 imm_expr.X_add_number &= OP_MASK_PERFREG;
8460 }
8461 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8462 imm_expr.X_op = O_absent;
8463 s = expr_end;
8464 continue;
8465
8466 case 'b': /* base register */
8467 case 'd': /* destination register */
8468 case 's': /* source register */
8469 case 't': /* target register */
8470 case 'r': /* both target and source */
8471 case 'v': /* both dest and source */
8472 case 'w': /* both dest and target */
8473 case 'E': /* coprocessor target register */
8474 case 'G': /* coprocessor destination register */
8475 case 'K': /* 'rdhwr' destination register */
8476 case 'x': /* ignore register name */
8477 case 'z': /* must be zero register */
8478 case 'U': /* destination register (clo/clz). */
8479 s_reset = s;
8480 if (s[0] == '$')
8481 {
8482
8483 if (ISDIGIT (s[1]))
8484 {
8485 ++s;
8486 regno = 0;
8487 do
8488 {
8489 regno *= 10;
8490 regno += *s - '0';
8491 ++s;
8492 }
8493 while (ISDIGIT (*s));
8494 if (regno > 31)
8495 as_bad (_("Invalid register number (%d)"), regno);
8496 }
8497 else if (*args == 'E' || *args == 'G' || *args == 'K')
8498 goto notreg;
8499 else
8500 {
8501 if (s[1] == 'r' && s[2] == 'a')
8502 {
8503 s += 3;
8504 regno = RA;
8505 }
8506 else if (s[1] == 'f' && s[2] == 'p')
8507 {
8508 s += 3;
8509 regno = FP;
8510 }
8511 else if (s[1] == 's' && s[2] == 'p')
8512 {
8513 s += 3;
8514 regno = SP;
8515 }
8516 else if (s[1] == 'g' && s[2] == 'p')
8517 {
8518 s += 3;
8519 regno = GP;
8520 }
8521 else if (s[1] == 'a' && s[2] == 't')
8522 {
8523 s += 3;
8524 regno = AT;
8525 }
8526 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8527 {
8528 s += 4;
8529 regno = KT0;
8530 }
8531 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8532 {
8533 s += 4;
8534 regno = KT1;
8535 }
8536 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8537 {
8538 s += 5;
8539 regno = ZERO;
8540 }
8541 else if (itbl_have_entries)
8542 {
8543 char *p, *n;
8544 unsigned long r;
8545
8546 p = s + 1; /* advance past '$' */
8547 n = itbl_get_field (&p); /* n is name */
8548
8549 /* See if this is a register defined in an
8550 itbl entry. */
8551 if (itbl_get_reg_val (n, &r))
8552 {
8553 /* Get_field advances to the start of
8554 the next field, so we need to back
8555 rack to the end of the last field. */
8556 if (p)
8557 s = p - 1;
8558 else
8559 s = strchr (s, '\0');
8560 regno = r;
8561 }
8562 else
8563 goto notreg;
8564 }
8565 else
8566 goto notreg;
8567 }
8568 if (regno == AT
8569 && ! mips_opts.noat
8570 && *args != 'E'
8571 && *args != 'G'
8572 && *args != 'K')
8573 as_warn (_("Used $at without \".set noat\""));
8574 c = *args;
8575 if (*s == ' ')
8576 ++s;
8577 if (args[1] != *s)
8578 {
8579 if (c == 'r' || c == 'v' || c == 'w')
8580 {
8581 regno = lastregno;
8582 s = s_reset;
8583 ++args;
8584 }
8585 }
8586 /* 'z' only matches $0. */
8587 if (c == 'z' && regno != 0)
8588 break;
8589
8590 /* Now that we have assembled one operand, we use the args string
8591 * to figure out where it goes in the instruction. */
8592 switch (c)
8593 {
8594 case 'r':
8595 case 's':
8596 case 'v':
8597 case 'b':
8598 ip->insn_opcode |= regno << OP_SH_RS;
8599 break;
8600 case 'd':
8601 case 'G':
8602 case 'K':
8603 ip->insn_opcode |= regno << OP_SH_RD;
8604 break;
8605 case 'U':
8606 ip->insn_opcode |= regno << OP_SH_RD;
8607 ip->insn_opcode |= regno << OP_SH_RT;
8608 break;
8609 case 'w':
8610 case 't':
8611 case 'E':
8612 ip->insn_opcode |= regno << OP_SH_RT;
8613 break;
8614 case 'x':
8615 /* This case exists because on the r3000 trunc
8616 expands into a macro which requires a gp
8617 register. On the r6000 or r4000 it is
8618 assembled into a single instruction which
8619 ignores the register. Thus the insn version
8620 is MIPS_ISA2 and uses 'x', and the macro
8621 version is MIPS_ISA1 and uses 't'. */
8622 break;
8623 case 'z':
8624 /* This case is for the div instruction, which
8625 acts differently if the destination argument
8626 is $0. This only matches $0, and is checked
8627 outside the switch. */
8628 break;
8629 case 'D':
8630 /* Itbl operand; not yet implemented. FIXME ?? */
8631 break;
8632 /* What about all other operands like 'i', which
8633 can be specified in the opcode table? */
8634 }
8635 lastregno = regno;
8636 continue;
8637 }
8638 notreg:
8639 switch (*args++)
8640 {
8641 case 'r':
8642 case 'v':
8643 ip->insn_opcode |= lastregno << OP_SH_RS;
8644 continue;
8645 case 'w':
8646 ip->insn_opcode |= lastregno << OP_SH_RT;
8647 continue;
8648 }
8649 break;
8650
8651 case 'O': /* MDMX alignment immediate constant. */
8652 my_getExpression (&imm_expr, s);
8653 check_absolute_expr (ip, &imm_expr);
8654 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8655 {
8656 as_warn ("Improper align amount (%ld), using low bits",
8657 (long) imm_expr.X_add_number);
8658 imm_expr.X_add_number &= OP_MASK_ALN;
8659 }
8660 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8661 imm_expr.X_op = O_absent;
8662 s = expr_end;
8663 continue;
8664
8665 case 'Q': /* MDMX vector, element sel, or const. */
8666 if (s[0] != '$')
8667 {
8668 /* MDMX Immediate. */
8669 my_getExpression (&imm_expr, s);
8670 check_absolute_expr (ip, &imm_expr);
8671 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8672 {
8673 as_warn (_("Invalid MDMX Immediate (%ld)"),
8674 (long) imm_expr.X_add_number);
8675 imm_expr.X_add_number &= OP_MASK_FT;
8676 }
8677 imm_expr.X_add_number &= OP_MASK_FT;
8678 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8679 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8680 else
8681 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8682 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8683 imm_expr.X_op = O_absent;
8684 s = expr_end;
8685 continue;
8686 }
8687 /* Not MDMX Immediate. Fall through. */
8688 case 'X': /* MDMX destination register. */
8689 case 'Y': /* MDMX source register. */
8690 case 'Z': /* MDMX target register. */
8691 is_mdmx = 1;
8692 case 'D': /* floating point destination register */
8693 case 'S': /* floating point source register */
8694 case 'T': /* floating point target register */
8695 case 'R': /* floating point source register */
8696 case 'V':
8697 case 'W':
8698 s_reset = s;
8699 /* Accept $fN for FP and MDMX register numbers, and in
8700 addition accept $vN for MDMX register numbers. */
8701 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8702 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8703 && ISDIGIT (s[2])))
8704 {
8705 s += 2;
8706 regno = 0;
8707 do
8708 {
8709 regno *= 10;
8710 regno += *s - '0';
8711 ++s;
8712 }
8713 while (ISDIGIT (*s));
8714
8715 if (regno > 31)
8716 as_bad (_("Invalid float register number (%d)"), regno);
8717
8718 if ((regno & 1) != 0
8719 && HAVE_32BIT_FPRS
8720 && ! (strcmp (str, "mtc1") == 0
8721 || strcmp (str, "mfc1") == 0
8722 || strcmp (str, "lwc1") == 0
8723 || strcmp (str, "swc1") == 0
8724 || strcmp (str, "l.s") == 0
8725 || strcmp (str, "s.s") == 0))
8726 as_warn (_("Float register should be even, was %d"),
8727 regno);
8728
8729 c = *args;
8730 if (*s == ' ')
8731 ++s;
8732 if (args[1] != *s)
8733 {
8734 if (c == 'V' || c == 'W')
8735 {
8736 regno = lastregno;
8737 s = s_reset;
8738 ++args;
8739 }
8740 }
8741 switch (c)
8742 {
8743 case 'D':
8744 case 'X':
8745 ip->insn_opcode |= regno << OP_SH_FD;
8746 break;
8747 case 'V':
8748 case 'S':
8749 case 'Y':
8750 ip->insn_opcode |= regno << OP_SH_FS;
8751 break;
8752 case 'Q':
8753 /* This is like 'Z', but also needs to fix the MDMX
8754 vector/scalar select bits. Note that the
8755 scalar immediate case is handled above. */
8756 if (*s == '[')
8757 {
8758 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8759 int max_el = (is_qh ? 3 : 7);
8760 s++;
8761 my_getExpression(&imm_expr, s);
8762 check_absolute_expr (ip, &imm_expr);
8763 s = expr_end;
8764 if (imm_expr.X_add_number > max_el)
8765 as_bad(_("Bad element selector %ld"),
8766 (long) imm_expr.X_add_number);
8767 imm_expr.X_add_number &= max_el;
8768 ip->insn_opcode |= (imm_expr.X_add_number
8769 << (OP_SH_VSEL +
8770 (is_qh ? 2 : 1)));
8771 if (*s != ']')
8772 as_warn(_("Expecting ']' found '%s'"), s);
8773 else
8774 s++;
8775 }
8776 else
8777 {
8778 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8779 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8780 << OP_SH_VSEL);
8781 else
8782 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8783 OP_SH_VSEL);
8784 }
8785 /* Fall through */
8786 case 'W':
8787 case 'T':
8788 case 'Z':
8789 ip->insn_opcode |= regno << OP_SH_FT;
8790 break;
8791 case 'R':
8792 ip->insn_opcode |= regno << OP_SH_FR;
8793 break;
8794 }
8795 lastregno = regno;
8796 continue;
8797 }
8798
8799 switch (*args++)
8800 {
8801 case 'V':
8802 ip->insn_opcode |= lastregno << OP_SH_FS;
8803 continue;
8804 case 'W':
8805 ip->insn_opcode |= lastregno << OP_SH_FT;
8806 continue;
8807 }
8808 break;
8809
8810 case 'I':
8811 my_getExpression (&imm_expr, s);
8812 if (imm_expr.X_op != O_big
8813 && imm_expr.X_op != O_constant)
8814 insn_error = _("absolute expression required");
8815 normalize_constant_expr (&imm_expr);
8816 s = expr_end;
8817 continue;
8818
8819 case 'A':
8820 my_getExpression (&offset_expr, s);
8821 *imm_reloc = BFD_RELOC_32;
8822 s = expr_end;
8823 continue;
8824
8825 case 'F':
8826 case 'L':
8827 case 'f':
8828 case 'l':
8829 {
8830 int f64;
8831 int using_gprs;
8832 char *save_in;
8833 char *err;
8834 unsigned char temp[8];
8835 int len;
8836 unsigned int length;
8837 segT seg;
8838 subsegT subseg;
8839 char *p;
8840
8841 /* These only appear as the last operand in an
8842 instruction, and every instruction that accepts
8843 them in any variant accepts them in all variants.
8844 This means we don't have to worry about backing out
8845 any changes if the instruction does not match.
8846
8847 The difference between them is the size of the
8848 floating point constant and where it goes. For 'F'
8849 and 'L' the constant is 64 bits; for 'f' and 'l' it
8850 is 32 bits. Where the constant is placed is based
8851 on how the MIPS assembler does things:
8852 F -- .rdata
8853 L -- .lit8
8854 f -- immediate value
8855 l -- .lit4
8856
8857 The .lit4 and .lit8 sections are only used if
8858 permitted by the -G argument.
8859
8860 When generating embedded PIC code, we use the
8861 .lit8 section but not the .lit4 section (we can do
8862 .lit4 inline easily; we need to put .lit8
8863 somewhere in the data segment, and using .lit8
8864 permits the linker to eventually combine identical
8865 .lit8 entries).
8866
8867 The code below needs to know whether the target register
8868 is 32 or 64 bits wide. It relies on the fact 'f' and
8869 'F' are used with GPR-based instructions and 'l' and
8870 'L' are used with FPR-based instructions. */
8871
8872 f64 = *args == 'F' || *args == 'L';
8873 using_gprs = *args == 'F' || *args == 'f';
8874
8875 save_in = input_line_pointer;
8876 input_line_pointer = s;
8877 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8878 length = len;
8879 s = input_line_pointer;
8880 input_line_pointer = save_in;
8881 if (err != NULL && *err != '\0')
8882 {
8883 as_bad (_("Bad floating point constant: %s"), err);
8884 memset (temp, '\0', sizeof temp);
8885 length = f64 ? 8 : 4;
8886 }
8887
8888 assert (length == (unsigned) (f64 ? 8 : 4));
8889
8890 if (*args == 'f'
8891 || (*args == 'l'
8892 && (mips_pic == EMBEDDED_PIC
8893 || g_switch_value < 4
8894 || (temp[0] == 0 && temp[1] == 0)
8895 || (temp[2] == 0 && temp[3] == 0))))
8896 {
8897 imm_expr.X_op = O_constant;
8898 if (! target_big_endian)
8899 imm_expr.X_add_number = bfd_getl32 (temp);
8900 else
8901 imm_expr.X_add_number = bfd_getb32 (temp);
8902 }
8903 else if (length > 4
8904 && ! mips_disable_float_construction
8905 /* Constants can only be constructed in GPRs and
8906 copied to FPRs if the GPRs are at least as wide
8907 as the FPRs. Force the constant into memory if
8908 we are using 64-bit FPRs but the GPRs are only
8909 32 bits wide. */
8910 && (using_gprs
8911 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8912 && ((temp[0] == 0 && temp[1] == 0)
8913 || (temp[2] == 0 && temp[3] == 0))
8914 && ((temp[4] == 0 && temp[5] == 0)
8915 || (temp[6] == 0 && temp[7] == 0)))
8916 {
8917 /* The value is simple enough to load with a couple of
8918 instructions. If using 32-bit registers, set
8919 imm_expr to the high order 32 bits and offset_expr to
8920 the low order 32 bits. Otherwise, set imm_expr to
8921 the entire 64 bit constant. */
8922 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8923 {
8924 imm_expr.X_op = O_constant;
8925 offset_expr.X_op = O_constant;
8926 if (! target_big_endian)
8927 {
8928 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8929 offset_expr.X_add_number = bfd_getl32 (temp);
8930 }
8931 else
8932 {
8933 imm_expr.X_add_number = bfd_getb32 (temp);
8934 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8935 }
8936 if (offset_expr.X_add_number == 0)
8937 offset_expr.X_op = O_absent;
8938 }
8939 else if (sizeof (imm_expr.X_add_number) > 4)
8940 {
8941 imm_expr.X_op = O_constant;
8942 if (! target_big_endian)
8943 imm_expr.X_add_number = bfd_getl64 (temp);
8944 else
8945 imm_expr.X_add_number = bfd_getb64 (temp);
8946 }
8947 else
8948 {
8949 imm_expr.X_op = O_big;
8950 imm_expr.X_add_number = 4;
8951 if (! target_big_endian)
8952 {
8953 generic_bignum[0] = bfd_getl16 (temp);
8954 generic_bignum[1] = bfd_getl16 (temp + 2);
8955 generic_bignum[2] = bfd_getl16 (temp + 4);
8956 generic_bignum[3] = bfd_getl16 (temp + 6);
8957 }
8958 else
8959 {
8960 generic_bignum[0] = bfd_getb16 (temp + 6);
8961 generic_bignum[1] = bfd_getb16 (temp + 4);
8962 generic_bignum[2] = bfd_getb16 (temp + 2);
8963 generic_bignum[3] = bfd_getb16 (temp);
8964 }
8965 }
8966 }
8967 else
8968 {
8969 const char *newname;
8970 segT new_seg;
8971
8972 /* Switch to the right section. */
8973 seg = now_seg;
8974 subseg = now_subseg;
8975 switch (*args)
8976 {
8977 default: /* unused default case avoids warnings. */
8978 case 'L':
8979 newname = RDATA_SECTION_NAME;
8980 if ((g_switch_value >= 8)
8981 || mips_pic == EMBEDDED_PIC)
8982 newname = ".lit8";
8983 break;
8984 case 'F':
8985 if (mips_pic == EMBEDDED_PIC)
8986 newname = ".lit8";
8987 else
8988 newname = RDATA_SECTION_NAME;
8989 break;
8990 case 'l':
8991 assert (g_switch_value >= 4);
8992 newname = ".lit4";
8993 break;
8994 }
8995 new_seg = subseg_new (newname, (subsegT) 0);
8996 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8997 bfd_set_section_flags (stdoutput, new_seg,
8998 (SEC_ALLOC
8999 | SEC_LOAD
9000 | SEC_READONLY
9001 | SEC_DATA));
9002 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9003 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9004 && strcmp (TARGET_OS, "elf") != 0)
9005 record_alignment (new_seg, 4);
9006 else
9007 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9008 if (seg == now_seg)
9009 as_bad (_("Can't use floating point insn in this section"));
9010
9011 /* Set the argument to the current address in the
9012 section. */
9013 offset_expr.X_op = O_symbol;
9014 offset_expr.X_add_symbol =
9015 symbol_new ("L0\001", now_seg,
9016 (valueT) frag_now_fix (), frag_now);
9017 offset_expr.X_add_number = 0;
9018
9019 /* Put the floating point number into the section. */
9020 p = frag_more ((int) length);
9021 memcpy (p, temp, length);
9022
9023 /* Switch back to the original section. */
9024 subseg_set (seg, subseg);
9025 }
9026 }
9027 continue;
9028
9029 case 'i': /* 16 bit unsigned immediate */
9030 case 'j': /* 16 bit signed immediate */
9031 *imm_reloc = BFD_RELOC_LO16;
9032 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9033 {
9034 int more;
9035 offsetT minval, maxval;
9036
9037 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9038 && strcmp (insn->name, insn[1].name) == 0);
9039
9040 /* If the expression was written as an unsigned number,
9041 only treat it as signed if there are no more
9042 alternatives. */
9043 if (more
9044 && *args == 'j'
9045 && sizeof (imm_expr.X_add_number) <= 4
9046 && imm_expr.X_op == O_constant
9047 && imm_expr.X_add_number < 0
9048 && imm_expr.X_unsigned
9049 && HAVE_64BIT_GPRS)
9050 break;
9051
9052 /* For compatibility with older assemblers, we accept
9053 0x8000-0xffff as signed 16-bit numbers when only
9054 signed numbers are allowed. */
9055 if (*args == 'i')
9056 minval = 0, maxval = 0xffff;
9057 else if (more)
9058 minval = -0x8000, maxval = 0x7fff;
9059 else
9060 minval = -0x8000, maxval = 0xffff;
9061
9062 if (imm_expr.X_op != O_constant
9063 || imm_expr.X_add_number < minval
9064 || imm_expr.X_add_number > maxval)
9065 {
9066 if (more)
9067 break;
9068 if (imm_expr.X_op == O_constant
9069 || imm_expr.X_op == O_big)
9070 as_bad (_("expression out of range"));
9071 }
9072 }
9073 s = expr_end;
9074 continue;
9075
9076 case 'o': /* 16 bit offset */
9077 /* Check whether there is only a single bracketed expression
9078 left. If so, it must be the base register and the
9079 constant must be zero. */
9080 if (*s == '(' && strchr (s + 1, '(') == 0)
9081 {
9082 offset_expr.X_op = O_constant;
9083 offset_expr.X_add_number = 0;
9084 continue;
9085 }
9086
9087 /* If this value won't fit into a 16 bit offset, then go
9088 find a macro that will generate the 32 bit offset
9089 code pattern. */
9090 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9091 && (offset_expr.X_op != O_constant
9092 || offset_expr.X_add_number >= 0x8000
9093 || offset_expr.X_add_number < -0x8000))
9094 break;
9095
9096 s = expr_end;
9097 continue;
9098
9099 case 'p': /* pc relative offset */
9100 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9101 my_getExpression (&offset_expr, s);
9102 s = expr_end;
9103 continue;
9104
9105 case 'u': /* upper 16 bits */
9106 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9107 && imm_expr.X_op == O_constant
9108 && (imm_expr.X_add_number < 0
9109 || imm_expr.X_add_number >= 0x10000))
9110 as_bad (_("lui expression not in range 0..65535"));
9111 s = expr_end;
9112 continue;
9113
9114 case 'a': /* 26 bit address */
9115 my_getExpression (&offset_expr, s);
9116 s = expr_end;
9117 *offset_reloc = BFD_RELOC_MIPS_JMP;
9118 continue;
9119
9120 case 'N': /* 3 bit branch condition code */
9121 case 'M': /* 3 bit compare condition code */
9122 if (strncmp (s, "$fcc", 4) != 0)
9123 break;
9124 s += 4;
9125 regno = 0;
9126 do
9127 {
9128 regno *= 10;
9129 regno += *s - '0';
9130 ++s;
9131 }
9132 while (ISDIGIT (*s));
9133 if (regno > 7)
9134 as_bad (_("Invalid condition code register $fcc%d"), regno);
9135 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9136 || strcmp(str + strlen(str) - 5, "any2f") == 0
9137 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9138 && (regno & 1) != 0)
9139 as_warn(_("Condition code register should be even for %s, was %d"),
9140 str, regno);
9141 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9142 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9143 && (regno & 3) != 0)
9144 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9145 str, regno);
9146 if (*args == 'N')
9147 ip->insn_opcode |= regno << OP_SH_BCC;
9148 else
9149 ip->insn_opcode |= regno << OP_SH_CCC;
9150 continue;
9151
9152 case 'H':
9153 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9154 s += 2;
9155 if (ISDIGIT (*s))
9156 {
9157 c = 0;
9158 do
9159 {
9160 c *= 10;
9161 c += *s - '0';
9162 ++s;
9163 }
9164 while (ISDIGIT (*s));
9165 }
9166 else
9167 c = 8; /* Invalid sel value. */
9168
9169 if (c > 7)
9170 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9171 ip->insn_opcode |= c;
9172 continue;
9173
9174 case 'e':
9175 /* Must be at least one digit. */
9176 my_getExpression (&imm_expr, s);
9177 check_absolute_expr (ip, &imm_expr);
9178
9179 if ((unsigned long) imm_expr.X_add_number
9180 > (unsigned long) OP_MASK_VECBYTE)
9181 {
9182 as_bad (_("bad byte vector index (%ld)"),
9183 (long) imm_expr.X_add_number);
9184 imm_expr.X_add_number = 0;
9185 }
9186
9187 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9188 imm_expr.X_op = O_absent;
9189 s = expr_end;
9190 continue;
9191
9192 case '%':
9193 my_getExpression (&imm_expr, s);
9194 check_absolute_expr (ip, &imm_expr);
9195
9196 if ((unsigned long) imm_expr.X_add_number
9197 > (unsigned long) OP_MASK_VECALIGN)
9198 {
9199 as_bad (_("bad byte vector index (%ld)"),
9200 (long) imm_expr.X_add_number);
9201 imm_expr.X_add_number = 0;
9202 }
9203
9204 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9205 imm_expr.X_op = O_absent;
9206 s = expr_end;
9207 continue;
9208
9209 default:
9210 as_bad (_("bad char = '%c'\n"), *args);
9211 internalError ();
9212 }
9213 break;
9214 }
9215 /* Args don't match. */
9216 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9217 !strcmp (insn->name, insn[1].name))
9218 {
9219 ++insn;
9220 s = argsStart;
9221 insn_error = _("illegal operands");
9222 continue;
9223 }
9224 if (save_c)
9225 *(--s) = save_c;
9226 insn_error = _("illegal operands");
9227 return;
9228 }
9229 }
9230
9231 /* This routine assembles an instruction into its binary format when
9232 assembling for the mips16. As a side effect, it sets one of the
9233 global variables imm_reloc or offset_reloc to the type of
9234 relocation to do if one of the operands is an address expression.
9235 It also sets mips16_small and mips16_ext if the user explicitly
9236 requested a small or extended instruction. */
9237
9238 static void
9239 mips16_ip (char *str, struct mips_cl_insn *ip)
9240 {
9241 char *s;
9242 const char *args;
9243 struct mips_opcode *insn;
9244 char *argsstart;
9245 unsigned int regno;
9246 unsigned int lastregno = 0;
9247 char *s_reset;
9248
9249 insn_error = NULL;
9250
9251 mips16_small = FALSE;
9252 mips16_ext = FALSE;
9253
9254 for (s = str; ISLOWER (*s); ++s)
9255 ;
9256 switch (*s)
9257 {
9258 case '\0':
9259 break;
9260
9261 case ' ':
9262 *s++ = '\0';
9263 break;
9264
9265 case '.':
9266 if (s[1] == 't' && s[2] == ' ')
9267 {
9268 *s = '\0';
9269 mips16_small = TRUE;
9270 s += 3;
9271 break;
9272 }
9273 else if (s[1] == 'e' && s[2] == ' ')
9274 {
9275 *s = '\0';
9276 mips16_ext = TRUE;
9277 s += 3;
9278 break;
9279 }
9280 /* Fall through. */
9281 default:
9282 insn_error = _("unknown opcode");
9283 return;
9284 }
9285
9286 if (mips_opts.noautoextend && ! mips16_ext)
9287 mips16_small = TRUE;
9288
9289 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9290 {
9291 insn_error = _("unrecognized opcode");
9292 return;
9293 }
9294
9295 argsstart = s;
9296 for (;;)
9297 {
9298 assert (strcmp (insn->name, str) == 0);
9299
9300 ip->insn_mo = insn;
9301 ip->insn_opcode = insn->match;
9302 ip->use_extend = FALSE;
9303 imm_expr.X_op = O_absent;
9304 imm_reloc[0] = BFD_RELOC_UNUSED;
9305 imm_reloc[1] = BFD_RELOC_UNUSED;
9306 imm_reloc[2] = BFD_RELOC_UNUSED;
9307 imm2_expr.X_op = O_absent;
9308 offset_expr.X_op = O_absent;
9309 offset_reloc[0] = BFD_RELOC_UNUSED;
9310 offset_reloc[1] = BFD_RELOC_UNUSED;
9311 offset_reloc[2] = BFD_RELOC_UNUSED;
9312 for (args = insn->args; 1; ++args)
9313 {
9314 int c;
9315
9316 if (*s == ' ')
9317 ++s;
9318
9319 /* In this switch statement we call break if we did not find
9320 a match, continue if we did find a match, or return if we
9321 are done. */
9322
9323 c = *args;
9324 switch (c)
9325 {
9326 case '\0':
9327 if (*s == '\0')
9328 {
9329 /* Stuff the immediate value in now, if we can. */
9330 if (imm_expr.X_op == O_constant
9331 && *imm_reloc > BFD_RELOC_UNUSED
9332 && insn->pinfo != INSN_MACRO)
9333 {
9334 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9335 imm_expr.X_add_number, TRUE, mips16_small,
9336 mips16_ext, &ip->insn_opcode,
9337 &ip->use_extend, &ip->extend);
9338 imm_expr.X_op = O_absent;
9339 *imm_reloc = BFD_RELOC_UNUSED;
9340 }
9341
9342 return;
9343 }
9344 break;
9345
9346 case ',':
9347 if (*s++ == c)
9348 continue;
9349 s--;
9350 switch (*++args)
9351 {
9352 case 'v':
9353 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9354 continue;
9355 case 'w':
9356 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9357 continue;
9358 }
9359 break;
9360
9361 case '(':
9362 case ')':
9363 if (*s++ == c)
9364 continue;
9365 break;
9366
9367 case 'v':
9368 case 'w':
9369 if (s[0] != '$')
9370 {
9371 if (c == 'v')
9372 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9373 else
9374 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9375 ++args;
9376 continue;
9377 }
9378 /* Fall through. */
9379 case 'x':
9380 case 'y':
9381 case 'z':
9382 case 'Z':
9383 case '0':
9384 case 'S':
9385 case 'R':
9386 case 'X':
9387 case 'Y':
9388 if (s[0] != '$')
9389 break;
9390 s_reset = s;
9391 if (ISDIGIT (s[1]))
9392 {
9393 ++s;
9394 regno = 0;
9395 do
9396 {
9397 regno *= 10;
9398 regno += *s - '0';
9399 ++s;
9400 }
9401 while (ISDIGIT (*s));
9402 if (regno > 31)
9403 {
9404 as_bad (_("invalid register number (%d)"), regno);
9405 regno = 2;
9406 }
9407 }
9408 else
9409 {
9410 if (s[1] == 'r' && s[2] == 'a')
9411 {
9412 s += 3;
9413 regno = RA;
9414 }
9415 else if (s[1] == 'f' && s[2] == 'p')
9416 {
9417 s += 3;
9418 regno = FP;
9419 }
9420 else if (s[1] == 's' && s[2] == 'p')
9421 {
9422 s += 3;
9423 regno = SP;
9424 }
9425 else if (s[1] == 'g' && s[2] == 'p')
9426 {
9427 s += 3;
9428 regno = GP;
9429 }
9430 else if (s[1] == 'a' && s[2] == 't')
9431 {
9432 s += 3;
9433 regno = AT;
9434 }
9435 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9436 {
9437 s += 4;
9438 regno = KT0;
9439 }
9440 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9441 {
9442 s += 4;
9443 regno = KT1;
9444 }
9445 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9446 {
9447 s += 5;
9448 regno = ZERO;
9449 }
9450 else
9451 break;
9452 }
9453
9454 if (*s == ' ')
9455 ++s;
9456 if (args[1] != *s)
9457 {
9458 if (c == 'v' || c == 'w')
9459 {
9460 regno = mips16_to_32_reg_map[lastregno];
9461 s = s_reset;
9462 ++args;
9463 }
9464 }
9465
9466 switch (c)
9467 {
9468 case 'x':
9469 case 'y':
9470 case 'z':
9471 case 'v':
9472 case 'w':
9473 case 'Z':
9474 regno = mips32_to_16_reg_map[regno];
9475 break;
9476
9477 case '0':
9478 if (regno != 0)
9479 regno = ILLEGAL_REG;
9480 break;
9481
9482 case 'S':
9483 if (regno != SP)
9484 regno = ILLEGAL_REG;
9485 break;
9486
9487 case 'R':
9488 if (regno != RA)
9489 regno = ILLEGAL_REG;
9490 break;
9491
9492 case 'X':
9493 case 'Y':
9494 if (regno == AT && ! mips_opts.noat)
9495 as_warn (_("used $at without \".set noat\""));
9496 break;
9497
9498 default:
9499 internalError ();
9500 }
9501
9502 if (regno == ILLEGAL_REG)
9503 break;
9504
9505 switch (c)
9506 {
9507 case 'x':
9508 case 'v':
9509 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9510 break;
9511 case 'y':
9512 case 'w':
9513 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9514 break;
9515 case 'z':
9516 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9517 break;
9518 case 'Z':
9519 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9520 case '0':
9521 case 'S':
9522 case 'R':
9523 break;
9524 case 'X':
9525 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9526 break;
9527 case 'Y':
9528 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9529 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9530 break;
9531 default:
9532 internalError ();
9533 }
9534
9535 lastregno = regno;
9536 continue;
9537
9538 case 'P':
9539 if (strncmp (s, "$pc", 3) == 0)
9540 {
9541 s += 3;
9542 continue;
9543 }
9544 break;
9545
9546 case '<':
9547 case '>':
9548 case '[':
9549 case ']':
9550 case '4':
9551 case '5':
9552 case 'H':
9553 case 'W':
9554 case 'D':
9555 case 'j':
9556 case '8':
9557 case 'V':
9558 case 'C':
9559 case 'U':
9560 case 'k':
9561 case 'K':
9562 if (s[0] == '%'
9563 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9564 {
9565 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9566 and generate the appropriate reloc. If the text
9567 inside %gprel is not a symbol name with an
9568 optional offset, then we generate a normal reloc
9569 and will probably fail later. */
9570 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9571 if (imm_expr.X_op == O_symbol)
9572 {
9573 mips16_ext = TRUE;
9574 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
9575 s = expr_end;
9576 ip->use_extend = TRUE;
9577 ip->extend = 0;
9578 continue;
9579 }
9580 }
9581 else
9582 {
9583 /* Just pick up a normal expression. */
9584 my_getExpression (&imm_expr, s);
9585 }
9586
9587 if (imm_expr.X_op == O_register)
9588 {
9589 /* What we thought was an expression turned out to
9590 be a register. */
9591
9592 if (s[0] == '(' && args[1] == '(')
9593 {
9594 /* It looks like the expression was omitted
9595 before a register indirection, which means
9596 that the expression is implicitly zero. We
9597 still set up imm_expr, so that we handle
9598 explicit extensions correctly. */
9599 imm_expr.X_op = O_constant;
9600 imm_expr.X_add_number = 0;
9601 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9602 continue;
9603 }
9604
9605 break;
9606 }
9607
9608 /* We need to relax this instruction. */
9609 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9610 s = expr_end;
9611 continue;
9612
9613 case 'p':
9614 case 'q':
9615 case 'A':
9616 case 'B':
9617 case 'E':
9618 /* We use offset_reloc rather than imm_reloc for the PC
9619 relative operands. This lets macros with both
9620 immediate and address operands work correctly. */
9621 my_getExpression (&offset_expr, s);
9622
9623 if (offset_expr.X_op == O_register)
9624 break;
9625
9626 /* We need to relax this instruction. */
9627 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9628 s = expr_end;
9629 continue;
9630
9631 case '6': /* break code */
9632 my_getExpression (&imm_expr, s);
9633 check_absolute_expr (ip, &imm_expr);
9634 if ((unsigned long) imm_expr.X_add_number > 63)
9635 {
9636 as_warn (_("Invalid value for `%s' (%lu)"),
9637 ip->insn_mo->name,
9638 (unsigned long) imm_expr.X_add_number);
9639 imm_expr.X_add_number &= 0x3f;
9640 }
9641 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9642 imm_expr.X_op = O_absent;
9643 s = expr_end;
9644 continue;
9645
9646 case 'a': /* 26 bit address */
9647 my_getExpression (&offset_expr, s);
9648 s = expr_end;
9649 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9650 ip->insn_opcode <<= 16;
9651 continue;
9652
9653 case 'l': /* register list for entry macro */
9654 case 'L': /* register list for exit macro */
9655 {
9656 int mask;
9657
9658 if (c == 'l')
9659 mask = 0;
9660 else
9661 mask = 7 << 3;
9662 while (*s != '\0')
9663 {
9664 int freg, reg1, reg2;
9665
9666 while (*s == ' ' || *s == ',')
9667 ++s;
9668 if (*s != '$')
9669 {
9670 as_bad (_("can't parse register list"));
9671 break;
9672 }
9673 ++s;
9674 if (*s != 'f')
9675 freg = 0;
9676 else
9677 {
9678 freg = 1;
9679 ++s;
9680 }
9681 reg1 = 0;
9682 while (ISDIGIT (*s))
9683 {
9684 reg1 *= 10;
9685 reg1 += *s - '0';
9686 ++s;
9687 }
9688 if (*s == ' ')
9689 ++s;
9690 if (*s != '-')
9691 reg2 = reg1;
9692 else
9693 {
9694 ++s;
9695 if (*s != '$')
9696 break;
9697 ++s;
9698 if (freg)
9699 {
9700 if (*s == 'f')
9701 ++s;
9702 else
9703 {
9704 as_bad (_("invalid register list"));
9705 break;
9706 }
9707 }
9708 reg2 = 0;
9709 while (ISDIGIT (*s))
9710 {
9711 reg2 *= 10;
9712 reg2 += *s - '0';
9713 ++s;
9714 }
9715 }
9716 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9717 {
9718 mask &= ~ (7 << 3);
9719 mask |= 5 << 3;
9720 }
9721 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9722 {
9723 mask &= ~ (7 << 3);
9724 mask |= 6 << 3;
9725 }
9726 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9727 mask |= (reg2 - 3) << 3;
9728 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9729 mask |= (reg2 - 15) << 1;
9730 else if (reg1 == RA && reg2 == RA)
9731 mask |= 1;
9732 else
9733 {
9734 as_bad (_("invalid register list"));
9735 break;
9736 }
9737 }
9738 /* The mask is filled in in the opcode table for the
9739 benefit of the disassembler. We remove it before
9740 applying the actual mask. */
9741 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9742 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9743 }
9744 continue;
9745
9746 case 'e': /* extend code */
9747 my_getExpression (&imm_expr, s);
9748 check_absolute_expr (ip, &imm_expr);
9749 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9750 {
9751 as_warn (_("Invalid value for `%s' (%lu)"),
9752 ip->insn_mo->name,
9753 (unsigned long) imm_expr.X_add_number);
9754 imm_expr.X_add_number &= 0x7ff;
9755 }
9756 ip->insn_opcode |= imm_expr.X_add_number;
9757 imm_expr.X_op = O_absent;
9758 s = expr_end;
9759 continue;
9760
9761 default:
9762 internalError ();
9763 }
9764 break;
9765 }
9766
9767 /* Args don't match. */
9768 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9769 strcmp (insn->name, insn[1].name) == 0)
9770 {
9771 ++insn;
9772 s = argsstart;
9773 continue;
9774 }
9775
9776 insn_error = _("illegal operands");
9777
9778 return;
9779 }
9780 }
9781
9782 /* This structure holds information we know about a mips16 immediate
9783 argument type. */
9784
9785 struct mips16_immed_operand
9786 {
9787 /* The type code used in the argument string in the opcode table. */
9788 int type;
9789 /* The number of bits in the short form of the opcode. */
9790 int nbits;
9791 /* The number of bits in the extended form of the opcode. */
9792 int extbits;
9793 /* The amount by which the short form is shifted when it is used;
9794 for example, the sw instruction has a shift count of 2. */
9795 int shift;
9796 /* The amount by which the short form is shifted when it is stored
9797 into the instruction code. */
9798 int op_shift;
9799 /* Non-zero if the short form is unsigned. */
9800 int unsp;
9801 /* Non-zero if the extended form is unsigned. */
9802 int extu;
9803 /* Non-zero if the value is PC relative. */
9804 int pcrel;
9805 };
9806
9807 /* The mips16 immediate operand types. */
9808
9809 static const struct mips16_immed_operand mips16_immed_operands[] =
9810 {
9811 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9812 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9813 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9814 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9815 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9816 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9817 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9818 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9819 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9820 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9821 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9822 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9823 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9824 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9825 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9826 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9827 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9828 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9829 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9830 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9831 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9832 };
9833
9834 #define MIPS16_NUM_IMMED \
9835 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9836
9837 /* Handle a mips16 instruction with an immediate value. This or's the
9838 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9839 whether an extended value is needed; if one is needed, it sets
9840 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9841 If SMALL is true, an unextended opcode was explicitly requested.
9842 If EXT is true, an extended opcode was explicitly requested. If
9843 WARN is true, warn if EXT does not match reality. */
9844
9845 static void
9846 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9847 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9848 unsigned long *insn, bfd_boolean *use_extend,
9849 unsigned short *extend)
9850 {
9851 register const struct mips16_immed_operand *op;
9852 int mintiny, maxtiny;
9853 bfd_boolean needext;
9854
9855 op = mips16_immed_operands;
9856 while (op->type != type)
9857 {
9858 ++op;
9859 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9860 }
9861
9862 if (op->unsp)
9863 {
9864 if (type == '<' || type == '>' || type == '[' || type == ']')
9865 {
9866 mintiny = 1;
9867 maxtiny = 1 << op->nbits;
9868 }
9869 else
9870 {
9871 mintiny = 0;
9872 maxtiny = (1 << op->nbits) - 1;
9873 }
9874 }
9875 else
9876 {
9877 mintiny = - (1 << (op->nbits - 1));
9878 maxtiny = (1 << (op->nbits - 1)) - 1;
9879 }
9880
9881 /* Branch offsets have an implicit 0 in the lowest bit. */
9882 if (type == 'p' || type == 'q')
9883 val /= 2;
9884
9885 if ((val & ((1 << op->shift) - 1)) != 0
9886 || val < (mintiny << op->shift)
9887 || val > (maxtiny << op->shift))
9888 needext = TRUE;
9889 else
9890 needext = FALSE;
9891
9892 if (warn && ext && ! needext)
9893 as_warn_where (file, line,
9894 _("extended operand requested but not required"));
9895 if (small && needext)
9896 as_bad_where (file, line, _("invalid unextended operand value"));
9897
9898 if (small || (! ext && ! needext))
9899 {
9900 int insnval;
9901
9902 *use_extend = FALSE;
9903 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9904 insnval <<= op->op_shift;
9905 *insn |= insnval;
9906 }
9907 else
9908 {
9909 long minext, maxext;
9910 int extval;
9911
9912 if (op->extu)
9913 {
9914 minext = 0;
9915 maxext = (1 << op->extbits) - 1;
9916 }
9917 else
9918 {
9919 minext = - (1 << (op->extbits - 1));
9920 maxext = (1 << (op->extbits - 1)) - 1;
9921 }
9922 if (val < minext || val > maxext)
9923 as_bad_where (file, line,
9924 _("operand value out of range for instruction"));
9925
9926 *use_extend = TRUE;
9927 if (op->extbits == 16)
9928 {
9929 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9930 val &= 0x1f;
9931 }
9932 else if (op->extbits == 15)
9933 {
9934 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9935 val &= 0xf;
9936 }
9937 else
9938 {
9939 extval = ((val & 0x1f) << 6) | (val & 0x20);
9940 val = 0;
9941 }
9942
9943 *extend = (unsigned short) extval;
9944 *insn |= val;
9945 }
9946 }
9947 \f
9948 static const struct percent_op_match
9949 {
9950 const char *str;
9951 bfd_reloc_code_real_type reloc;
9952 } percent_op[] =
9953 {
9954 {"%lo", BFD_RELOC_LO16},
9955 #ifdef OBJ_ELF
9956 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9957 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9958 {"%call16", BFD_RELOC_MIPS_CALL16},
9959 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9960 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9961 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9962 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9963 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9964 {"%got", BFD_RELOC_MIPS_GOT16},
9965 {"%gp_rel", BFD_RELOC_GPREL16},
9966 {"%half", BFD_RELOC_16},
9967 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9968 {"%higher", BFD_RELOC_MIPS_HIGHER},
9969 {"%neg", BFD_RELOC_MIPS_SUB},
9970 #endif
9971 {"%hi", BFD_RELOC_HI16_S}
9972 };
9973
9974
9975 /* Return true if *STR points to a relocation operator. When returning true,
9976 move *STR over the operator and store its relocation code in *RELOC.
9977 Leave both *STR and *RELOC alone when returning false. */
9978
9979 static bfd_boolean
9980 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9981 {
9982 size_t i;
9983
9984 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9985 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9986 {
9987 *str += strlen (percent_op[i].str);
9988 *reloc = percent_op[i].reloc;
9989
9990 /* Check whether the output BFD supports this relocation.
9991 If not, issue an error and fall back on something safe. */
9992 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9993 {
9994 as_bad ("relocation %s isn't supported by the current ABI",
9995 percent_op[i].str);
9996 *reloc = BFD_RELOC_LO16;
9997 }
9998 return TRUE;
9999 }
10000 return FALSE;
10001 }
10002
10003
10004 /* Parse string STR as a 16-bit relocatable operand. Store the
10005 expression in *EP and the relocations in the array starting
10006 at RELOC. Return the number of relocation operators used.
10007
10008 On exit, EXPR_END points to the first character after the expression.
10009 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
10010
10011 static size_t
10012 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10013 char *str)
10014 {
10015 bfd_reloc_code_real_type reversed_reloc[3];
10016 size_t reloc_index, i;
10017 int crux_depth, str_depth;
10018 char *crux;
10019
10020 /* Search for the start of the main expression, recoding relocations
10021 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10022 of the main expression and with CRUX_DEPTH containing the number
10023 of open brackets at that point. */
10024 reloc_index = -1;
10025 str_depth = 0;
10026 do
10027 {
10028 reloc_index++;
10029 crux = str;
10030 crux_depth = str_depth;
10031
10032 /* Skip over whitespace and brackets, keeping count of the number
10033 of brackets. */
10034 while (*str == ' ' || *str == '\t' || *str == '(')
10035 if (*str++ == '(')
10036 str_depth++;
10037 }
10038 while (*str == '%'
10039 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10040 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10041
10042 my_getExpression (ep, crux);
10043 str = expr_end;
10044
10045 /* Match every open bracket. */
10046 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10047 if (*str++ == ')')
10048 crux_depth--;
10049
10050 if (crux_depth > 0)
10051 as_bad ("unclosed '('");
10052
10053 expr_end = str;
10054
10055 if (reloc_index == 0)
10056 reloc[0] = BFD_RELOC_LO16;
10057 else
10058 {
10059 prev_reloc_op_frag = frag_now;
10060 for (i = 0; i < reloc_index; i++)
10061 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10062 }
10063
10064 return reloc_index;
10065 }
10066
10067 static void
10068 my_getExpression (expressionS *ep, char *str)
10069 {
10070 char *save_in;
10071 valueT val;
10072
10073 save_in = input_line_pointer;
10074 input_line_pointer = str;
10075 expression (ep);
10076 expr_end = input_line_pointer;
10077 input_line_pointer = save_in;
10078
10079 /* If we are in mips16 mode, and this is an expression based on `.',
10080 then we bump the value of the symbol by 1 since that is how other
10081 text symbols are handled. We don't bother to handle complex
10082 expressions, just `.' plus or minus a constant. */
10083 if (mips_opts.mips16
10084 && ep->X_op == O_symbol
10085 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10086 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10087 && symbol_get_frag (ep->X_add_symbol) == frag_now
10088 && symbol_constant_p (ep->X_add_symbol)
10089 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10090 S_SET_VALUE (ep->X_add_symbol, val + 1);
10091 }
10092
10093 /* Turn a string in input_line_pointer into a floating point constant
10094 of type TYPE, and store the appropriate bytes in *LITP. The number
10095 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10096 returned, or NULL on OK. */
10097
10098 char *
10099 md_atof (int type, char *litP, int *sizeP)
10100 {
10101 int prec;
10102 LITTLENUM_TYPE words[4];
10103 char *t;
10104 int i;
10105
10106 switch (type)
10107 {
10108 case 'f':
10109 prec = 2;
10110 break;
10111
10112 case 'd':
10113 prec = 4;
10114 break;
10115
10116 default:
10117 *sizeP = 0;
10118 return _("bad call to md_atof");
10119 }
10120
10121 t = atof_ieee (input_line_pointer, type, words);
10122 if (t)
10123 input_line_pointer = t;
10124
10125 *sizeP = prec * 2;
10126
10127 if (! target_big_endian)
10128 {
10129 for (i = prec - 1; i >= 0; i--)
10130 {
10131 md_number_to_chars (litP, words[i], 2);
10132 litP += 2;
10133 }
10134 }
10135 else
10136 {
10137 for (i = 0; i < prec; i++)
10138 {
10139 md_number_to_chars (litP, words[i], 2);
10140 litP += 2;
10141 }
10142 }
10143
10144 return NULL;
10145 }
10146
10147 void
10148 md_number_to_chars (char *buf, valueT val, int n)
10149 {
10150 if (target_big_endian)
10151 number_to_chars_bigendian (buf, val, n);
10152 else
10153 number_to_chars_littleendian (buf, val, n);
10154 }
10155 \f
10156 #ifdef OBJ_ELF
10157 static int support_64bit_objects(void)
10158 {
10159 const char **list, **l;
10160 int yes;
10161
10162 list = bfd_target_list ();
10163 for (l = list; *l != NULL; l++)
10164 #ifdef TE_TMIPS
10165 /* This is traditional mips */
10166 if (strcmp (*l, "elf64-tradbigmips") == 0
10167 || strcmp (*l, "elf64-tradlittlemips") == 0)
10168 #else
10169 if (strcmp (*l, "elf64-bigmips") == 0
10170 || strcmp (*l, "elf64-littlemips") == 0)
10171 #endif
10172 break;
10173 yes = (*l != NULL);
10174 free (list);
10175 return yes;
10176 }
10177 #endif /* OBJ_ELF */
10178
10179 const char *md_shortopts = "O::g::G:";
10180
10181 struct option md_longopts[] =
10182 {
10183 /* Options which specify architecture. */
10184 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10185 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10186 {"march", required_argument, NULL, OPTION_MARCH},
10187 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10188 {"mtune", required_argument, NULL, OPTION_MTUNE},
10189 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10190 {"mips0", no_argument, NULL, OPTION_MIPS1},
10191 {"mips1", no_argument, NULL, OPTION_MIPS1},
10192 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10193 {"mips2", no_argument, NULL, OPTION_MIPS2},
10194 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10195 {"mips3", no_argument, NULL, OPTION_MIPS3},
10196 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10197 {"mips4", no_argument, NULL, OPTION_MIPS4},
10198 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10199 {"mips5", no_argument, NULL, OPTION_MIPS5},
10200 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10201 {"mips32", no_argument, NULL, OPTION_MIPS32},
10202 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10203 {"mips64", no_argument, NULL, OPTION_MIPS64},
10204 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10205 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10206 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10207 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10208
10209 /* Options which specify Application Specific Extensions (ASEs). */
10210 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10211 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10212 {"mips16", no_argument, NULL, OPTION_MIPS16},
10213 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10214 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10215 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10216 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10217 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10218 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10219 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10220 {"mdmx", no_argument, NULL, OPTION_MDMX},
10221 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10222 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10223
10224 /* Old-style architecture options. Don't add more of these. */
10225 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10226 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10227 {"m4650", no_argument, NULL, OPTION_M4650},
10228 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10229 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10230 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10231 {"m4010", no_argument, NULL, OPTION_M4010},
10232 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10233 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10234 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10235 {"m4100", no_argument, NULL, OPTION_M4100},
10236 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10237 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10238 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10239 {"m3900", no_argument, NULL, OPTION_M3900},
10240 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10241 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10242
10243 /* Options which enable bug fixes. */
10244 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10245 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10246 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10247 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10248 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10249 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10250 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10251 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10252 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10253 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10254
10255 /* Miscellaneous options. */
10256 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10257 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10258 {"trap", no_argument, NULL, OPTION_TRAP},
10259 {"no-break", no_argument, NULL, OPTION_TRAP},
10260 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10261 {"break", no_argument, NULL, OPTION_BREAK},
10262 {"no-trap", no_argument, NULL, OPTION_BREAK},
10263 #define OPTION_EB (OPTION_MISC_BASE + 2)
10264 {"EB", no_argument, NULL, OPTION_EB},
10265 #define OPTION_EL (OPTION_MISC_BASE + 3)
10266 {"EL", no_argument, NULL, OPTION_EL},
10267 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10268 {"mfp32", no_argument, NULL, OPTION_FP32},
10269 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10270 {"mgp32", no_argument, NULL, OPTION_GP32},
10271 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10272 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10273 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10274 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10275 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10276 {"mfp64", no_argument, NULL, OPTION_FP64},
10277 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10278 {"mgp64", no_argument, NULL, OPTION_GP64},
10279 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10280 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10281 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10282 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10283
10284 /* ELF-specific options. */
10285 #ifdef OBJ_ELF
10286 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 12)
10287 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10288 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10289 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10290 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10291 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10292 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10293 {"xgot", no_argument, NULL, OPTION_XGOT},
10294 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10295 {"mabi", required_argument, NULL, OPTION_MABI},
10296 #define OPTION_32 (OPTION_ELF_BASE + 4)
10297 {"32", no_argument, NULL, OPTION_32},
10298 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10299 {"n32", no_argument, NULL, OPTION_N32},
10300 #define OPTION_64 (OPTION_ELF_BASE + 6)
10301 {"64", no_argument, NULL, OPTION_64},
10302 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10303 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10304 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10305 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10306 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10307 {"mpdr", no_argument, NULL, OPTION_PDR},
10308 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10309 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10310 #endif /* OBJ_ELF */
10311
10312 {NULL, no_argument, NULL, 0}
10313 };
10314 size_t md_longopts_size = sizeof (md_longopts);
10315
10316 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10317 NEW_VALUE. Warn if another value was already specified. Note:
10318 we have to defer parsing the -march and -mtune arguments in order
10319 to handle 'from-abi' correctly, since the ABI might be specified
10320 in a later argument. */
10321
10322 static void
10323 mips_set_option_string (const char **string_ptr, const char *new_value)
10324 {
10325 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10326 as_warn (_("A different %s was already specified, is now %s"),
10327 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10328 new_value);
10329
10330 *string_ptr = new_value;
10331 }
10332
10333 int
10334 md_parse_option (int c, char *arg)
10335 {
10336 switch (c)
10337 {
10338 case OPTION_CONSTRUCT_FLOATS:
10339 mips_disable_float_construction = 0;
10340 break;
10341
10342 case OPTION_NO_CONSTRUCT_FLOATS:
10343 mips_disable_float_construction = 1;
10344 break;
10345
10346 case OPTION_TRAP:
10347 mips_trap = 1;
10348 break;
10349
10350 case OPTION_BREAK:
10351 mips_trap = 0;
10352 break;
10353
10354 case OPTION_EB:
10355 target_big_endian = 1;
10356 break;
10357
10358 case OPTION_EL:
10359 target_big_endian = 0;
10360 break;
10361
10362 case 'O':
10363 if (arg && arg[1] == '0')
10364 mips_optimize = 1;
10365 else
10366 mips_optimize = 2;
10367 break;
10368
10369 case 'g':
10370 if (arg == NULL)
10371 mips_debug = 2;
10372 else
10373 mips_debug = atoi (arg);
10374 /* When the MIPS assembler sees -g or -g2, it does not do
10375 optimizations which limit full symbolic debugging. We take
10376 that to be equivalent to -O0. */
10377 if (mips_debug == 2)
10378 mips_optimize = 1;
10379 break;
10380
10381 case OPTION_MIPS1:
10382 file_mips_isa = ISA_MIPS1;
10383 break;
10384
10385 case OPTION_MIPS2:
10386 file_mips_isa = ISA_MIPS2;
10387 break;
10388
10389 case OPTION_MIPS3:
10390 file_mips_isa = ISA_MIPS3;
10391 break;
10392
10393 case OPTION_MIPS4:
10394 file_mips_isa = ISA_MIPS4;
10395 break;
10396
10397 case OPTION_MIPS5:
10398 file_mips_isa = ISA_MIPS5;
10399 break;
10400
10401 case OPTION_MIPS32:
10402 file_mips_isa = ISA_MIPS32;
10403 break;
10404
10405 case OPTION_MIPS32R2:
10406 file_mips_isa = ISA_MIPS32R2;
10407 break;
10408
10409 case OPTION_MIPS64R2:
10410 file_mips_isa = ISA_MIPS64R2;
10411 break;
10412
10413 case OPTION_MIPS64:
10414 file_mips_isa = ISA_MIPS64;
10415 break;
10416
10417 case OPTION_MTUNE:
10418 mips_set_option_string (&mips_tune_string, arg);
10419 break;
10420
10421 case OPTION_MARCH:
10422 mips_set_option_string (&mips_arch_string, arg);
10423 break;
10424
10425 case OPTION_M4650:
10426 mips_set_option_string (&mips_arch_string, "4650");
10427 mips_set_option_string (&mips_tune_string, "4650");
10428 break;
10429
10430 case OPTION_NO_M4650:
10431 break;
10432
10433 case OPTION_M4010:
10434 mips_set_option_string (&mips_arch_string, "4010");
10435 mips_set_option_string (&mips_tune_string, "4010");
10436 break;
10437
10438 case OPTION_NO_M4010:
10439 break;
10440
10441 case OPTION_M4100:
10442 mips_set_option_string (&mips_arch_string, "4100");
10443 mips_set_option_string (&mips_tune_string, "4100");
10444 break;
10445
10446 case OPTION_NO_M4100:
10447 break;
10448
10449 case OPTION_M3900:
10450 mips_set_option_string (&mips_arch_string, "3900");
10451 mips_set_option_string (&mips_tune_string, "3900");
10452 break;
10453
10454 case OPTION_NO_M3900:
10455 break;
10456
10457 case OPTION_MDMX:
10458 mips_opts.ase_mdmx = 1;
10459 break;
10460
10461 case OPTION_NO_MDMX:
10462 mips_opts.ase_mdmx = 0;
10463 break;
10464
10465 case OPTION_MIPS16:
10466 mips_opts.mips16 = 1;
10467 mips_no_prev_insn (FALSE);
10468 break;
10469
10470 case OPTION_NO_MIPS16:
10471 mips_opts.mips16 = 0;
10472 mips_no_prev_insn (FALSE);
10473 break;
10474
10475 case OPTION_MIPS3D:
10476 mips_opts.ase_mips3d = 1;
10477 break;
10478
10479 case OPTION_NO_MIPS3D:
10480 mips_opts.ase_mips3d = 0;
10481 break;
10482
10483 case OPTION_FIX_VR4120:
10484 mips_fix_vr4120 = 1;
10485 break;
10486
10487 case OPTION_NO_FIX_VR4120:
10488 mips_fix_vr4120 = 0;
10489 break;
10490
10491 case OPTION_RELAX_BRANCH:
10492 mips_relax_branch = 1;
10493 break;
10494
10495 case OPTION_NO_RELAX_BRANCH:
10496 mips_relax_branch = 0;
10497 break;
10498
10499 #ifdef OBJ_ELF
10500 /* When generating ELF code, we permit -KPIC and -call_shared to
10501 select SVR4_PIC, and -non_shared to select no PIC. This is
10502 intended to be compatible with Irix 5. */
10503 case OPTION_CALL_SHARED:
10504 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10505 {
10506 as_bad (_("-call_shared is supported only for ELF format"));
10507 return 0;
10508 }
10509 mips_pic = SVR4_PIC;
10510 mips_abicalls = TRUE;
10511 if (g_switch_seen && g_switch_value != 0)
10512 {
10513 as_bad (_("-G may not be used with SVR4 PIC code"));
10514 return 0;
10515 }
10516 g_switch_value = 0;
10517 break;
10518
10519 case OPTION_NON_SHARED:
10520 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10521 {
10522 as_bad (_("-non_shared is supported only for ELF format"));
10523 return 0;
10524 }
10525 mips_pic = NO_PIC;
10526 mips_abicalls = FALSE;
10527 break;
10528
10529 /* The -xgot option tells the assembler to use 32 offsets when
10530 accessing the got in SVR4_PIC mode. It is for Irix
10531 compatibility. */
10532 case OPTION_XGOT:
10533 mips_big_got = 1;
10534 break;
10535 #endif /* OBJ_ELF */
10536
10537 case 'G':
10538 if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10539 {
10540 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10541 return 0;
10542 }
10543 else
10544 g_switch_value = atoi (arg);
10545 g_switch_seen = 1;
10546 break;
10547
10548 #ifdef OBJ_ELF
10549 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10550 and -mabi=64. */
10551 case OPTION_32:
10552 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10553 {
10554 as_bad (_("-32 is supported for ELF format only"));
10555 return 0;
10556 }
10557 mips_abi = O32_ABI;
10558 break;
10559
10560 case OPTION_N32:
10561 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10562 {
10563 as_bad (_("-n32 is supported for ELF format only"));
10564 return 0;
10565 }
10566 mips_abi = N32_ABI;
10567 break;
10568
10569 case OPTION_64:
10570 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10571 {
10572 as_bad (_("-64 is supported for ELF format only"));
10573 return 0;
10574 }
10575 mips_abi = N64_ABI;
10576 if (! support_64bit_objects())
10577 as_fatal (_("No compiled in support for 64 bit object file format"));
10578 break;
10579 #endif /* OBJ_ELF */
10580
10581 case OPTION_GP32:
10582 file_mips_gp32 = 1;
10583 break;
10584
10585 case OPTION_GP64:
10586 file_mips_gp32 = 0;
10587 break;
10588
10589 case OPTION_FP32:
10590 file_mips_fp32 = 1;
10591 break;
10592
10593 case OPTION_FP64:
10594 file_mips_fp32 = 0;
10595 break;
10596
10597 #ifdef OBJ_ELF
10598 case OPTION_MABI:
10599 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10600 {
10601 as_bad (_("-mabi is supported for ELF format only"));
10602 return 0;
10603 }
10604 if (strcmp (arg, "32") == 0)
10605 mips_abi = O32_ABI;
10606 else if (strcmp (arg, "o64") == 0)
10607 mips_abi = O64_ABI;
10608 else if (strcmp (arg, "n32") == 0)
10609 mips_abi = N32_ABI;
10610 else if (strcmp (arg, "64") == 0)
10611 {
10612 mips_abi = N64_ABI;
10613 if (! support_64bit_objects())
10614 as_fatal (_("No compiled in support for 64 bit object file "
10615 "format"));
10616 }
10617 else if (strcmp (arg, "eabi") == 0)
10618 mips_abi = EABI_ABI;
10619 else
10620 {
10621 as_fatal (_("invalid abi -mabi=%s"), arg);
10622 return 0;
10623 }
10624 break;
10625 #endif /* OBJ_ELF */
10626
10627 case OPTION_M7000_HILO_FIX:
10628 mips_7000_hilo_fix = TRUE;
10629 break;
10630
10631 case OPTION_MNO_7000_HILO_FIX:
10632 mips_7000_hilo_fix = FALSE;
10633 break;
10634
10635 #ifdef OBJ_ELF
10636 case OPTION_MDEBUG:
10637 mips_flag_mdebug = TRUE;
10638 break;
10639
10640 case OPTION_NO_MDEBUG:
10641 mips_flag_mdebug = FALSE;
10642 break;
10643
10644 case OPTION_PDR:
10645 mips_flag_pdr = TRUE;
10646 break;
10647
10648 case OPTION_NO_PDR:
10649 mips_flag_pdr = FALSE;
10650 break;
10651 #endif /* OBJ_ELF */
10652
10653 default:
10654 return 0;
10655 }
10656
10657 return 1;
10658 }
10659 \f
10660 /* Set up globals to generate code for the ISA or processor
10661 described by INFO. */
10662
10663 static void
10664 mips_set_architecture (const struct mips_cpu_info *info)
10665 {
10666 if (info != 0)
10667 {
10668 file_mips_arch = info->cpu;
10669 mips_opts.arch = info->cpu;
10670 mips_opts.isa = info->isa;
10671 }
10672 }
10673
10674
10675 /* Likewise for tuning. */
10676
10677 static void
10678 mips_set_tune (const struct mips_cpu_info *info)
10679 {
10680 if (info != 0)
10681 mips_tune = info->cpu;
10682 }
10683
10684
10685 void
10686 mips_after_parse_args (void)
10687 {
10688 const struct mips_cpu_info *arch_info = 0;
10689 const struct mips_cpu_info *tune_info = 0;
10690
10691 /* GP relative stuff not working for PE */
10692 if (strncmp (TARGET_OS, "pe", 2) == 0
10693 && g_switch_value != 0)
10694 {
10695 if (g_switch_seen)
10696 as_bad (_("-G not supported in this configuration."));
10697 g_switch_value = 0;
10698 }
10699
10700 if (mips_abi == NO_ABI)
10701 mips_abi = MIPS_DEFAULT_ABI;
10702
10703 /* The following code determines the architecture and register size.
10704 Similar code was added to GCC 3.3 (see override_options() in
10705 config/mips/mips.c). The GAS and GCC code should be kept in sync
10706 as much as possible. */
10707
10708 if (mips_arch_string != 0)
10709 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10710
10711 if (file_mips_isa != ISA_UNKNOWN)
10712 {
10713 /* Handle -mipsN. At this point, file_mips_isa contains the
10714 ISA level specified by -mipsN, while arch_info->isa contains
10715 the -march selection (if any). */
10716 if (arch_info != 0)
10717 {
10718 /* -march takes precedence over -mipsN, since it is more descriptive.
10719 There's no harm in specifying both as long as the ISA levels
10720 are the same. */
10721 if (file_mips_isa != arch_info->isa)
10722 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10723 mips_cpu_info_from_isa (file_mips_isa)->name,
10724 mips_cpu_info_from_isa (arch_info->isa)->name);
10725 }
10726 else
10727 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10728 }
10729
10730 if (arch_info == 0)
10731 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10732
10733 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10734 as_bad ("-march=%s is not compatible with the selected ABI",
10735 arch_info->name);
10736
10737 mips_set_architecture (arch_info);
10738
10739 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10740 if (mips_tune_string != 0)
10741 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10742
10743 if (tune_info == 0)
10744 mips_set_tune (arch_info);
10745 else
10746 mips_set_tune (tune_info);
10747
10748 if (file_mips_gp32 >= 0)
10749 {
10750 /* The user specified the size of the integer registers. Make sure
10751 it agrees with the ABI and ISA. */
10752 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10753 as_bad (_("-mgp64 used with a 32-bit processor"));
10754 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10755 as_bad (_("-mgp32 used with a 64-bit ABI"));
10756 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10757 as_bad (_("-mgp64 used with a 32-bit ABI"));
10758 }
10759 else
10760 {
10761 /* Infer the integer register size from the ABI and processor.
10762 Restrict ourselves to 32-bit registers if that's all the
10763 processor has, or if the ABI cannot handle 64-bit registers. */
10764 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10765 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10766 }
10767
10768 /* ??? GAS treats single-float processors as though they had 64-bit
10769 float registers (although it complains when double-precision
10770 instructions are used). As things stand, saying they have 32-bit
10771 registers would lead to spurious "register must be even" messages.
10772 So here we assume float registers are always the same size as
10773 integer ones, unless the user says otherwise. */
10774 if (file_mips_fp32 < 0)
10775 file_mips_fp32 = file_mips_gp32;
10776
10777 /* End of GCC-shared inference code. */
10778
10779 /* This flag is set when we have a 64-bit capable CPU but use only
10780 32-bit wide registers. Note that EABI does not use it. */
10781 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10782 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10783 || mips_abi == O32_ABI))
10784 mips_32bitmode = 1;
10785
10786 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10787 as_bad (_("trap exception not supported at ISA 1"));
10788
10789 /* If the selected architecture includes support for ASEs, enable
10790 generation of code for them. */
10791 if (mips_opts.mips16 == -1)
10792 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10793 if (mips_opts.ase_mips3d == -1)
10794 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10795 if (mips_opts.ase_mdmx == -1)
10796 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10797
10798 file_mips_isa = mips_opts.isa;
10799 file_ase_mips16 = mips_opts.mips16;
10800 file_ase_mips3d = mips_opts.ase_mips3d;
10801 file_ase_mdmx = mips_opts.ase_mdmx;
10802 mips_opts.gp32 = file_mips_gp32;
10803 mips_opts.fp32 = file_mips_fp32;
10804
10805 if (mips_flag_mdebug < 0)
10806 {
10807 #ifdef OBJ_MAYBE_ECOFF
10808 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10809 mips_flag_mdebug = 1;
10810 else
10811 #endif /* OBJ_MAYBE_ECOFF */
10812 mips_flag_mdebug = 0;
10813 }
10814 }
10815 \f
10816 void
10817 mips_init_after_args (void)
10818 {
10819 /* initialize opcodes */
10820 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10821 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10822 }
10823
10824 long
10825 md_pcrel_from (fixS *fixP)
10826 {
10827 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10828 switch (fixP->fx_r_type)
10829 {
10830 case BFD_RELOC_16_PCREL_S2:
10831 case BFD_RELOC_MIPS_JMP:
10832 /* Return the address of the delay slot. */
10833 return addr + 4;
10834 default:
10835 return addr;
10836 }
10837 }
10838
10839 /* This is called before the symbol table is processed. In order to
10840 work with gcc when using mips-tfile, we must keep all local labels.
10841 However, in other cases, we want to discard them. If we were
10842 called with -g, but we didn't see any debugging information, it may
10843 mean that gcc is smuggling debugging information through to
10844 mips-tfile, in which case we must generate all local labels. */
10845
10846 void
10847 mips_frob_file_before_adjust (void)
10848 {
10849 #ifndef NO_ECOFF_DEBUGGING
10850 if (ECOFF_DEBUGGING
10851 && mips_debug != 0
10852 && ! ecoff_debugging_seen)
10853 flag_keep_locals = 1;
10854 #endif
10855 }
10856
10857 /* Sort any unmatched HI16_S relocs so that they immediately precede
10858 the corresponding LO reloc. This is called before md_apply_fix3 and
10859 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10860 explicit use of the %hi modifier. */
10861
10862 void
10863 mips_frob_file (void)
10864 {
10865 struct mips_hi_fixup *l;
10866
10867 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10868 {
10869 segment_info_type *seginfo;
10870 int pass;
10871
10872 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10873
10874 /* If a GOT16 relocation turns out to be against a global symbol,
10875 there isn't supposed to be a matching LO. */
10876 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10877 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10878 continue;
10879
10880 /* Check quickly whether the next fixup happens to be a matching %lo. */
10881 if (fixup_has_matching_lo_p (l->fixp))
10882 continue;
10883
10884 /* Look through the fixups for this segment for a matching %lo.
10885 When we find one, move the %hi just in front of it. We do
10886 this in two passes. In the first pass, we try to find a
10887 unique %lo. In the second pass, we permit multiple %hi
10888 relocs for a single %lo (this is a GNU extension). */
10889 seginfo = seg_info (l->seg);
10890 for (pass = 0; pass < 2; pass++)
10891 {
10892 fixS *f, *prev;
10893
10894 prev = NULL;
10895 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10896 {
10897 /* Check whether this is a %lo fixup which matches l->fixp. */
10898 if (f->fx_r_type == BFD_RELOC_LO16
10899 && f->fx_addsy == l->fixp->fx_addsy
10900 && f->fx_offset == l->fixp->fx_offset
10901 && (pass == 1
10902 || prev == NULL
10903 || !reloc_needs_lo_p (prev->fx_r_type)
10904 || !fixup_has_matching_lo_p (prev)))
10905 {
10906 fixS **pf;
10907
10908 /* Move l->fixp before f. */
10909 for (pf = &seginfo->fix_root;
10910 *pf != l->fixp;
10911 pf = &(*pf)->fx_next)
10912 assert (*pf != NULL);
10913
10914 *pf = l->fixp->fx_next;
10915
10916 l->fixp->fx_next = f;
10917 if (prev == NULL)
10918 seginfo->fix_root = l->fixp;
10919 else
10920 prev->fx_next = l->fixp;
10921
10922 break;
10923 }
10924
10925 prev = f;
10926 }
10927
10928 if (f != NULL)
10929 break;
10930
10931 #if 0 /* GCC code motion plus incomplete dead code elimination
10932 can leave a %hi without a %lo. */
10933 if (pass == 1)
10934 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10935 _("Unmatched %%hi reloc"));
10936 #endif
10937 }
10938 }
10939 }
10940
10941 /* When generating embedded PIC code we need to use a special
10942 relocation to represent the difference of two symbols in the .text
10943 section (switch tables use a difference of this sort). See
10944 include/coff/mips.h for details. This macro checks whether this
10945 fixup requires the special reloc. */
10946 #define SWITCH_TABLE(fixp) \
10947 ((fixp)->fx_r_type == BFD_RELOC_32 \
10948 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10949 && (fixp)->fx_addsy != NULL \
10950 && (fixp)->fx_subsy != NULL \
10951 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10952 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10953
10954 /* When generating embedded PIC code we must keep all PC relative
10955 relocations, in case the linker has to relax a call. We also need
10956 to keep relocations for switch table entries.
10957
10958 We may have combined relocations without symbols in the N32/N64 ABI.
10959 We have to prevent gas from dropping them. */
10960
10961 int
10962 mips_force_relocation (fixS *fixp)
10963 {
10964 if (generic_force_reloc (fixp))
10965 return 1;
10966
10967 if (HAVE_NEWABI
10968 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10969 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10970 || fixp->fx_r_type == BFD_RELOC_HI16_S
10971 || fixp->fx_r_type == BFD_RELOC_LO16))
10972 return 1;
10973
10974 return (mips_pic == EMBEDDED_PIC
10975 && (fixp->fx_pcrel
10976 || SWITCH_TABLE (fixp)
10977 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10978 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10979 }
10980
10981 /* This hook is called before a fix is simplified. We don't really
10982 decide whether to skip a fix here. Rather, we turn global symbols
10983 used as branch targets into local symbols, such that they undergo
10984 simplification. We can only do this if the symbol is defined and
10985 it is in the same section as the branch. If this doesn't hold, we
10986 emit a better error message than just saying the relocation is not
10987 valid for the selected object format.
10988
10989 FIXP is the fix-up we're going to try to simplify, SEG is the
10990 segment in which the fix up occurs. The return value should be
10991 non-zero to indicate the fix-up is valid for further
10992 simplifications. */
10993
10994 int
10995 mips_validate_fix (struct fix *fixP, asection *seg)
10996 {
10997 /* There's a lot of discussion on whether it should be possible to
10998 use R_MIPS_PC16 to represent branch relocations. The outcome
10999 seems to be that it can, but gas/bfd are very broken in creating
11000 RELA relocations for this, so for now we only accept branches to
11001 symbols in the same section. Anything else is of dubious value,
11002 since there's no guarantee that at link time the symbol would be
11003 in range. Even for branches to local symbols this is arguably
11004 wrong, since it we assume the symbol is not going to be
11005 overridden, which should be possible per ELF library semantics,
11006 but then, there isn't a dynamic relocation that could be used to
11007 this effect, and the target would likely be out of range as well.
11008
11009 Unfortunately, it seems that there is too much code out there
11010 that relies on branches to symbols that are global to be resolved
11011 as if they were local, like the IRIX tools do, so we do it as
11012 well, but with a warning so that people are reminded to fix their
11013 code. If we ever get back to using R_MIPS_PC16 for branch
11014 targets, this entire block should go away (and probably the
11015 whole function). */
11016
11017 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11018 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11019 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11020 && mips_pic != EMBEDDED_PIC)
11021 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11022 && fixP->fx_addsy)
11023 {
11024 if (! S_IS_DEFINED (fixP->fx_addsy))
11025 {
11026 as_bad_where (fixP->fx_file, fixP->fx_line,
11027 _("Cannot branch to undefined symbol."));
11028 /* Avoid any further errors about this fixup. */
11029 fixP->fx_done = 1;
11030 }
11031 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11032 {
11033 as_bad_where (fixP->fx_file, fixP->fx_line,
11034 _("Cannot branch to symbol in another section."));
11035 fixP->fx_done = 1;
11036 }
11037 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11038 {
11039 symbolS *sym = fixP->fx_addsy;
11040
11041 if (mips_pic == SVR4_PIC)
11042 as_warn_where (fixP->fx_file, fixP->fx_line,
11043 _("Pretending global symbol used as branch target is local."));
11044
11045 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11046 S_GET_SEGMENT (sym),
11047 S_GET_VALUE (sym),
11048 symbol_get_frag (sym));
11049 copy_symbol_attributes (fixP->fx_addsy, sym);
11050 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11051 assert (symbol_resolved_p (sym));
11052 symbol_mark_resolved (fixP->fx_addsy);
11053 }
11054 }
11055
11056 return 1;
11057 }
11058
11059 /* Apply a fixup to the object file. */
11060
11061 void
11062 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11063 {
11064 bfd_byte *buf;
11065 long insn;
11066 static int previous_fx_r_type = 0;
11067 reloc_howto_type *howto;
11068
11069 /* We ignore generic BFD relocations we don't know about. */
11070 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11071 if (! howto)
11072 return;
11073
11074 assert (fixP->fx_size == 4
11075 || fixP->fx_r_type == BFD_RELOC_16
11076 || fixP->fx_r_type == BFD_RELOC_64
11077 || fixP->fx_r_type == BFD_RELOC_CTOR
11078 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11079 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11080 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11081
11082 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11083
11084 /* We are not done if this is a composite relocation to set up gp. */
11085 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11086 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11087 || (fixP->fx_r_type == BFD_RELOC_64
11088 && (previous_fx_r_type == BFD_RELOC_GPREL32
11089 || previous_fx_r_type == BFD_RELOC_GPREL16))
11090 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11091 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11092 || fixP->fx_r_type == BFD_RELOC_LO16))))
11093 fixP->fx_done = 1;
11094 previous_fx_r_type = fixP->fx_r_type;
11095
11096 switch (fixP->fx_r_type)
11097 {
11098 case BFD_RELOC_MIPS_JMP:
11099 case BFD_RELOC_MIPS_SHIFT5:
11100 case BFD_RELOC_MIPS_SHIFT6:
11101 case BFD_RELOC_MIPS_GOT_DISP:
11102 case BFD_RELOC_MIPS_GOT_PAGE:
11103 case BFD_RELOC_MIPS_GOT_OFST:
11104 case BFD_RELOC_MIPS_SUB:
11105 case BFD_RELOC_MIPS_INSERT_A:
11106 case BFD_RELOC_MIPS_INSERT_B:
11107 case BFD_RELOC_MIPS_DELETE:
11108 case BFD_RELOC_MIPS_HIGHEST:
11109 case BFD_RELOC_MIPS_HIGHER:
11110 case BFD_RELOC_MIPS_SCN_DISP:
11111 case BFD_RELOC_MIPS_REL16:
11112 case BFD_RELOC_MIPS_RELGOT:
11113 case BFD_RELOC_MIPS_JALR:
11114 case BFD_RELOC_HI16:
11115 case BFD_RELOC_HI16_S:
11116 case BFD_RELOC_GPREL16:
11117 case BFD_RELOC_MIPS_LITERAL:
11118 case BFD_RELOC_MIPS_CALL16:
11119 case BFD_RELOC_MIPS_GOT16:
11120 case BFD_RELOC_GPREL32:
11121 case BFD_RELOC_MIPS_GOT_HI16:
11122 case BFD_RELOC_MIPS_GOT_LO16:
11123 case BFD_RELOC_MIPS_CALL_HI16:
11124 case BFD_RELOC_MIPS_CALL_LO16:
11125 case BFD_RELOC_MIPS16_GPREL:
11126 if (fixP->fx_pcrel)
11127 as_bad_where (fixP->fx_file, fixP->fx_line,
11128 _("Invalid PC relative reloc"));
11129 /* Nothing needed to do. The value comes from the reloc entry */
11130 break;
11131
11132 case BFD_RELOC_MIPS16_JMP:
11133 /* We currently always generate a reloc against a symbol, which
11134 means that we don't want an addend even if the symbol is
11135 defined. */
11136 *valP = 0;
11137 break;
11138
11139 case BFD_RELOC_PCREL_HI16_S:
11140 /* The addend for this is tricky if it is internal, so we just
11141 do everything here rather than in bfd_install_relocation. */
11142 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11143 break;
11144 if (fixP->fx_addsy
11145 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11146 {
11147 /* For an external symbol adjust by the address to make it
11148 pcrel_offset. We use the address of the RELLO reloc
11149 which follows this one. */
11150 *valP += (fixP->fx_next->fx_frag->fr_address
11151 + fixP->fx_next->fx_where);
11152 }
11153 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11154 if (target_big_endian)
11155 buf += 2;
11156 md_number_to_chars (buf, *valP, 2);
11157 break;
11158
11159 case BFD_RELOC_PCREL_LO16:
11160 /* The addend for this is tricky if it is internal, so we just
11161 do everything here rather than in bfd_install_relocation. */
11162 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11163 break;
11164 if (fixP->fx_addsy
11165 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11166 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11167 if (target_big_endian)
11168 buf += 2;
11169 md_number_to_chars (buf, *valP, 2);
11170 break;
11171
11172 case BFD_RELOC_64:
11173 /* This is handled like BFD_RELOC_32, but we output a sign
11174 extended value if we are only 32 bits. */
11175 if (fixP->fx_done
11176 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11177 {
11178 if (8 <= sizeof (valueT))
11179 md_number_to_chars (buf, *valP, 8);
11180 else
11181 {
11182 valueT hiv;
11183
11184 if ((*valP & 0x80000000) != 0)
11185 hiv = 0xffffffff;
11186 else
11187 hiv = 0;
11188 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11189 *valP, 4);
11190 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11191 hiv, 4);
11192 }
11193 }
11194 break;
11195
11196 case BFD_RELOC_RVA:
11197 case BFD_RELOC_32:
11198 /* If we are deleting this reloc entry, we must fill in the
11199 value now. This can happen if we have a .word which is not
11200 resolved when it appears but is later defined. We also need
11201 to fill in the value if this is an embedded PIC switch table
11202 entry. */
11203 if (fixP->fx_done
11204 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11205 md_number_to_chars (buf, *valP, 4);
11206 break;
11207
11208 case BFD_RELOC_16:
11209 /* If we are deleting this reloc entry, we must fill in the
11210 value now. */
11211 assert (fixP->fx_size == 2);
11212 if (fixP->fx_done)
11213 md_number_to_chars (buf, *valP, 2);
11214 break;
11215
11216 case BFD_RELOC_LO16:
11217 /* When handling an embedded PIC switch statement, we can wind
11218 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11219 if (fixP->fx_done)
11220 {
11221 if (*valP + 0x8000 > 0xffff)
11222 as_bad_where (fixP->fx_file, fixP->fx_line,
11223 _("relocation overflow"));
11224 if (target_big_endian)
11225 buf += 2;
11226 md_number_to_chars (buf, *valP, 2);
11227 }
11228 break;
11229
11230 case BFD_RELOC_16_PCREL_S2:
11231 if ((*valP & 0x3) != 0)
11232 as_bad_where (fixP->fx_file, fixP->fx_line,
11233 _("Branch to odd address (%lx)"), (long) *valP);
11234
11235 /*
11236 * We need to save the bits in the instruction since fixup_segment()
11237 * might be deleting the relocation entry (i.e., a branch within
11238 * the current segment).
11239 */
11240 if (! fixP->fx_done)
11241 break;
11242
11243 /* update old instruction data */
11244 if (target_big_endian)
11245 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11246 else
11247 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11248
11249 if (*valP + 0x20000 <= 0x3ffff)
11250 {
11251 insn |= (*valP >> 2) & 0xffff;
11252 md_number_to_chars (buf, insn, 4);
11253 }
11254 else if (mips_pic == NO_PIC
11255 && fixP->fx_done
11256 && fixP->fx_frag->fr_address >= text_section->vma
11257 && (fixP->fx_frag->fr_address
11258 < text_section->vma + text_section->_raw_size)
11259 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11260 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11261 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11262 {
11263 /* The branch offset is too large. If this is an
11264 unconditional branch, and we are not generating PIC code,
11265 we can convert it to an absolute jump instruction. */
11266 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11267 insn = 0x0c000000; /* jal */
11268 else
11269 insn = 0x08000000; /* j */
11270 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11271 fixP->fx_done = 0;
11272 fixP->fx_addsy = section_symbol (text_section);
11273 *valP += md_pcrel_from (fixP);
11274 md_number_to_chars (buf, insn, 4);
11275 }
11276 else
11277 {
11278 /* If we got here, we have branch-relaxation disabled,
11279 and there's nothing we can do to fix this instruction
11280 without turning it into a longer sequence. */
11281 as_bad_where (fixP->fx_file, fixP->fx_line,
11282 _("Branch out of range"));
11283 }
11284 break;
11285
11286 case BFD_RELOC_VTABLE_INHERIT:
11287 fixP->fx_done = 0;
11288 if (fixP->fx_addsy
11289 && !S_IS_DEFINED (fixP->fx_addsy)
11290 && !S_IS_WEAK (fixP->fx_addsy))
11291 S_SET_WEAK (fixP->fx_addsy);
11292 break;
11293
11294 case BFD_RELOC_VTABLE_ENTRY:
11295 fixP->fx_done = 0;
11296 break;
11297
11298 default:
11299 internalError ();
11300 }
11301
11302 /* Remember value for tc_gen_reloc. */
11303 fixP->fx_addnumber = *valP;
11304 }
11305
11306 #if 0
11307 void
11308 printInsn (unsigned long oc)
11309 {
11310 const struct mips_opcode *p;
11311 int treg, sreg, dreg, shamt;
11312 short imm;
11313 const char *args;
11314 int i;
11315
11316 for (i = 0; i < NUMOPCODES; ++i)
11317 {
11318 p = &mips_opcodes[i];
11319 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11320 {
11321 printf ("%08lx %s\t", oc, p->name);
11322 treg = (oc >> 16) & 0x1f;
11323 sreg = (oc >> 21) & 0x1f;
11324 dreg = (oc >> 11) & 0x1f;
11325 shamt = (oc >> 6) & 0x1f;
11326 imm = oc;
11327 for (args = p->args;; ++args)
11328 {
11329 switch (*args)
11330 {
11331 case '\0':
11332 printf ("\n");
11333 break;
11334
11335 case ',':
11336 case '(':
11337 case ')':
11338 printf ("%c", *args);
11339 continue;
11340
11341 case 'r':
11342 assert (treg == sreg);
11343 printf ("$%d,$%d", treg, sreg);
11344 continue;
11345
11346 case 'd':
11347 case 'G':
11348 printf ("$%d", dreg);
11349 continue;
11350
11351 case 't':
11352 case 'E':
11353 printf ("$%d", treg);
11354 continue;
11355
11356 case 'k':
11357 printf ("0x%x", treg);
11358 continue;
11359
11360 case 'b':
11361 case 's':
11362 printf ("$%d", sreg);
11363 continue;
11364
11365 case 'a':
11366 printf ("0x%08lx", oc & 0x1ffffff);
11367 continue;
11368
11369 case 'i':
11370 case 'j':
11371 case 'o':
11372 case 'u':
11373 printf ("%d", imm);
11374 continue;
11375
11376 case '<':
11377 case '>':
11378 printf ("$%d", shamt);
11379 continue;
11380
11381 default:
11382 internalError ();
11383 }
11384 break;
11385 }
11386 return;
11387 }
11388 }
11389 printf (_("%08lx UNDEFINED\n"), oc);
11390 }
11391 #endif
11392
11393 static symbolS *
11394 get_symbol (void)
11395 {
11396 int c;
11397 char *name;
11398 symbolS *p;
11399
11400 name = input_line_pointer;
11401 c = get_symbol_end ();
11402 p = (symbolS *) symbol_find_or_make (name);
11403 *input_line_pointer = c;
11404 return p;
11405 }
11406
11407 /* Align the current frag to a given power of two. The MIPS assembler
11408 also automatically adjusts any preceding label. */
11409
11410 static void
11411 mips_align (int to, int fill, symbolS *label)
11412 {
11413 mips_emit_delays (FALSE);
11414 frag_align (to, fill, 0);
11415 record_alignment (now_seg, to);
11416 if (label != NULL)
11417 {
11418 assert (S_GET_SEGMENT (label) == now_seg);
11419 symbol_set_frag (label, frag_now);
11420 S_SET_VALUE (label, (valueT) frag_now_fix ());
11421 }
11422 }
11423
11424 /* Align to a given power of two. .align 0 turns off the automatic
11425 alignment used by the data creating pseudo-ops. */
11426
11427 static void
11428 s_align (int x ATTRIBUTE_UNUSED)
11429 {
11430 register int temp;
11431 register long temp_fill;
11432 long max_alignment = 15;
11433
11434 /*
11435
11436 o Note that the assembler pulls down any immediately preceding label
11437 to the aligned address.
11438 o It's not documented but auto alignment is reinstated by
11439 a .align pseudo instruction.
11440 o Note also that after auto alignment is turned off the mips assembler
11441 issues an error on attempt to assemble an improperly aligned data item.
11442 We don't.
11443
11444 */
11445
11446 temp = get_absolute_expression ();
11447 if (temp > max_alignment)
11448 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11449 else if (temp < 0)
11450 {
11451 as_warn (_("Alignment negative: 0 assumed."));
11452 temp = 0;
11453 }
11454 if (*input_line_pointer == ',')
11455 {
11456 ++input_line_pointer;
11457 temp_fill = get_absolute_expression ();
11458 }
11459 else
11460 temp_fill = 0;
11461 if (temp)
11462 {
11463 auto_align = 1;
11464 mips_align (temp, (int) temp_fill,
11465 insn_labels != NULL ? insn_labels->label : NULL);
11466 }
11467 else
11468 {
11469 auto_align = 0;
11470 }
11471
11472 demand_empty_rest_of_line ();
11473 }
11474
11475 void
11476 mips_flush_pending_output (void)
11477 {
11478 mips_emit_delays (FALSE);
11479 mips_clear_insn_labels ();
11480 }
11481
11482 static void
11483 s_change_sec (int sec)
11484 {
11485 segT seg;
11486
11487 /* When generating embedded PIC code, we only use the .text, .lit8,
11488 .sdata and .sbss sections. We change the .data and .rdata
11489 pseudo-ops to use .sdata. */
11490 if (mips_pic == EMBEDDED_PIC
11491 && (sec == 'd' || sec == 'r'))
11492 sec = 's';
11493
11494 #ifdef OBJ_ELF
11495 /* The ELF backend needs to know that we are changing sections, so
11496 that .previous works correctly. We could do something like check
11497 for an obj_section_change_hook macro, but that might be confusing
11498 as it would not be appropriate to use it in the section changing
11499 functions in read.c, since obj-elf.c intercepts those. FIXME:
11500 This should be cleaner, somehow. */
11501 obj_elf_section_change_hook ();
11502 #endif
11503
11504 mips_emit_delays (FALSE);
11505 switch (sec)
11506 {
11507 case 't':
11508 s_text (0);
11509 break;
11510 case 'd':
11511 s_data (0);
11512 break;
11513 case 'b':
11514 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11515 demand_empty_rest_of_line ();
11516 break;
11517
11518 case 'r':
11519 seg = subseg_new (RDATA_SECTION_NAME,
11520 (subsegT) get_absolute_expression ());
11521 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11522 {
11523 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11524 | SEC_READONLY | SEC_RELOC
11525 | SEC_DATA));
11526 if (strcmp (TARGET_OS, "elf") != 0)
11527 record_alignment (seg, 4);
11528 }
11529 demand_empty_rest_of_line ();
11530 break;
11531
11532 case 's':
11533 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11534 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11535 {
11536 bfd_set_section_flags (stdoutput, seg,
11537 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11538 if (strcmp (TARGET_OS, "elf") != 0)
11539 record_alignment (seg, 4);
11540 }
11541 demand_empty_rest_of_line ();
11542 break;
11543 }
11544
11545 auto_align = 1;
11546 }
11547
11548 void
11549 s_change_section (int ignore ATTRIBUTE_UNUSED)
11550 {
11551 #ifdef OBJ_ELF
11552 char *section_name;
11553 char c;
11554 char next_c = 0;
11555 int section_type;
11556 int section_flag;
11557 int section_entry_size;
11558 int section_alignment;
11559
11560 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11561 return;
11562
11563 section_name = input_line_pointer;
11564 c = get_symbol_end ();
11565 if (c)
11566 next_c = *(input_line_pointer + 1);
11567
11568 /* Do we have .section Name<,"flags">? */
11569 if (c != ',' || (c == ',' && next_c == '"'))
11570 {
11571 /* just after name is now '\0'. */
11572 *input_line_pointer = c;
11573 input_line_pointer = section_name;
11574 obj_elf_section (ignore);
11575 return;
11576 }
11577 input_line_pointer++;
11578
11579 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11580 if (c == ',')
11581 section_type = get_absolute_expression ();
11582 else
11583 section_type = 0;
11584 if (*input_line_pointer++ == ',')
11585 section_flag = get_absolute_expression ();
11586 else
11587 section_flag = 0;
11588 if (*input_line_pointer++ == ',')
11589 section_entry_size = get_absolute_expression ();
11590 else
11591 section_entry_size = 0;
11592 if (*input_line_pointer++ == ',')
11593 section_alignment = get_absolute_expression ();
11594 else
11595 section_alignment = 0;
11596
11597 section_name = xstrdup (section_name);
11598
11599 /* When using the generic form of .section (as implemented by obj-elf.c),
11600 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11601 traditionally had to fall back on the more common @progbits instead.
11602
11603 There's nothing really harmful in this, since bfd will correct
11604 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11605 means that, for backwards compatibiltiy, the special_section entries
11606 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11607
11608 Even so, we shouldn't force users of the MIPS .section syntax to
11609 incorrectly label the sections as SHT_PROGBITS. The best compromise
11610 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11611 generic type-checking code. */
11612 if (section_type == SHT_MIPS_DWARF)
11613 section_type = SHT_PROGBITS;
11614
11615 obj_elf_change_section (section_name, section_type, section_flag,
11616 section_entry_size, 0, 0, 0);
11617
11618 if (now_seg->name != section_name)
11619 free (section_name);
11620 #endif /* OBJ_ELF */
11621 }
11622
11623 void
11624 mips_enable_auto_align (void)
11625 {
11626 auto_align = 1;
11627 }
11628
11629 static void
11630 s_cons (int log_size)
11631 {
11632 symbolS *label;
11633
11634 label = insn_labels != NULL ? insn_labels->label : NULL;
11635 mips_emit_delays (FALSE);
11636 if (log_size > 0 && auto_align)
11637 mips_align (log_size, 0, label);
11638 mips_clear_insn_labels ();
11639 cons (1 << log_size);
11640 }
11641
11642 static void
11643 s_float_cons (int type)
11644 {
11645 symbolS *label;
11646
11647 label = insn_labels != NULL ? insn_labels->label : NULL;
11648
11649 mips_emit_delays (FALSE);
11650
11651 if (auto_align)
11652 {
11653 if (type == 'd')
11654 mips_align (3, 0, label);
11655 else
11656 mips_align (2, 0, label);
11657 }
11658
11659 mips_clear_insn_labels ();
11660
11661 float_cons (type);
11662 }
11663
11664 /* Handle .globl. We need to override it because on Irix 5 you are
11665 permitted to say
11666 .globl foo .text
11667 where foo is an undefined symbol, to mean that foo should be
11668 considered to be the address of a function. */
11669
11670 static void
11671 s_mips_globl (int x ATTRIBUTE_UNUSED)
11672 {
11673 char *name;
11674 int c;
11675 symbolS *symbolP;
11676 flagword flag;
11677
11678 name = input_line_pointer;
11679 c = get_symbol_end ();
11680 symbolP = symbol_find_or_make (name);
11681 *input_line_pointer = c;
11682 SKIP_WHITESPACE ();
11683
11684 /* On Irix 5, every global symbol that is not explicitly labelled as
11685 being a function is apparently labelled as being an object. */
11686 flag = BSF_OBJECT;
11687
11688 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11689 {
11690 char *secname;
11691 asection *sec;
11692
11693 secname = input_line_pointer;
11694 c = get_symbol_end ();
11695 sec = bfd_get_section_by_name (stdoutput, secname);
11696 if (sec == NULL)
11697 as_bad (_("%s: no such section"), secname);
11698 *input_line_pointer = c;
11699
11700 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11701 flag = BSF_FUNCTION;
11702 }
11703
11704 symbol_get_bfdsym (symbolP)->flags |= flag;
11705
11706 S_SET_EXTERNAL (symbolP);
11707 demand_empty_rest_of_line ();
11708 }
11709
11710 static void
11711 s_option (int x ATTRIBUTE_UNUSED)
11712 {
11713 char *opt;
11714 char c;
11715
11716 opt = input_line_pointer;
11717 c = get_symbol_end ();
11718
11719 if (*opt == 'O')
11720 {
11721 /* FIXME: What does this mean? */
11722 }
11723 else if (strncmp (opt, "pic", 3) == 0)
11724 {
11725 int i;
11726
11727 i = atoi (opt + 3);
11728 if (i == 0)
11729 mips_pic = NO_PIC;
11730 else if (i == 2)
11731 {
11732 mips_pic = SVR4_PIC;
11733 mips_abicalls = TRUE;
11734 }
11735 else
11736 as_bad (_(".option pic%d not supported"), i);
11737
11738 if (mips_pic == SVR4_PIC)
11739 {
11740 if (g_switch_seen && g_switch_value != 0)
11741 as_warn (_("-G may not be used with SVR4 PIC code"));
11742 g_switch_value = 0;
11743 bfd_set_gp_size (stdoutput, 0);
11744 }
11745 }
11746 else
11747 as_warn (_("Unrecognized option \"%s\""), opt);
11748
11749 *input_line_pointer = c;
11750 demand_empty_rest_of_line ();
11751 }
11752
11753 /* This structure is used to hold a stack of .set values. */
11754
11755 struct mips_option_stack
11756 {
11757 struct mips_option_stack *next;
11758 struct mips_set_options options;
11759 };
11760
11761 static struct mips_option_stack *mips_opts_stack;
11762
11763 /* Handle the .set pseudo-op. */
11764
11765 static void
11766 s_mipsset (int x ATTRIBUTE_UNUSED)
11767 {
11768 char *name = input_line_pointer, ch;
11769
11770 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11771 ++input_line_pointer;
11772 ch = *input_line_pointer;
11773 *input_line_pointer = '\0';
11774
11775 if (strcmp (name, "reorder") == 0)
11776 {
11777 if (mips_opts.noreorder && prev_nop_frag != NULL)
11778 {
11779 /* If we still have pending nops, we can discard them. The
11780 usual nop handling will insert any that are still
11781 needed. */
11782 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11783 * (mips_opts.mips16 ? 2 : 4));
11784 prev_nop_frag = NULL;
11785 }
11786 mips_opts.noreorder = 0;
11787 }
11788 else if (strcmp (name, "noreorder") == 0)
11789 {
11790 mips_emit_delays (TRUE);
11791 mips_opts.noreorder = 1;
11792 mips_any_noreorder = 1;
11793 }
11794 else if (strcmp (name, "at") == 0)
11795 {
11796 mips_opts.noat = 0;
11797 }
11798 else if (strcmp (name, "noat") == 0)
11799 {
11800 mips_opts.noat = 1;
11801 }
11802 else if (strcmp (name, "macro") == 0)
11803 {
11804 mips_opts.warn_about_macros = 0;
11805 }
11806 else if (strcmp (name, "nomacro") == 0)
11807 {
11808 if (mips_opts.noreorder == 0)
11809 as_bad (_("`noreorder' must be set before `nomacro'"));
11810 mips_opts.warn_about_macros = 1;
11811 }
11812 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11813 {
11814 mips_opts.nomove = 0;
11815 }
11816 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11817 {
11818 mips_opts.nomove = 1;
11819 }
11820 else if (strcmp (name, "bopt") == 0)
11821 {
11822 mips_opts.nobopt = 0;
11823 }
11824 else if (strcmp (name, "nobopt") == 0)
11825 {
11826 mips_opts.nobopt = 1;
11827 }
11828 else if (strcmp (name, "mips16") == 0
11829 || strcmp (name, "MIPS-16") == 0)
11830 mips_opts.mips16 = 1;
11831 else if (strcmp (name, "nomips16") == 0
11832 || strcmp (name, "noMIPS-16") == 0)
11833 mips_opts.mips16 = 0;
11834 else if (strcmp (name, "mips3d") == 0)
11835 mips_opts.ase_mips3d = 1;
11836 else if (strcmp (name, "nomips3d") == 0)
11837 mips_opts.ase_mips3d = 0;
11838 else if (strcmp (name, "mdmx") == 0)
11839 mips_opts.ase_mdmx = 1;
11840 else if (strcmp (name, "nomdmx") == 0)
11841 mips_opts.ase_mdmx = 0;
11842 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11843 {
11844 int reset = 0;
11845
11846 /* Permit the user to change the ISA and architecture on the fly.
11847 Needless to say, misuse can cause serious problems. */
11848 if (strcmp (name, "mips0") == 0)
11849 {
11850 reset = 1;
11851 mips_opts.isa = file_mips_isa;
11852 }
11853 else if (strcmp (name, "mips1") == 0)
11854 mips_opts.isa = ISA_MIPS1;
11855 else if (strcmp (name, "mips2") == 0)
11856 mips_opts.isa = ISA_MIPS2;
11857 else if (strcmp (name, "mips3") == 0)
11858 mips_opts.isa = ISA_MIPS3;
11859 else if (strcmp (name, "mips4") == 0)
11860 mips_opts.isa = ISA_MIPS4;
11861 else if (strcmp (name, "mips5") == 0)
11862 mips_opts.isa = ISA_MIPS5;
11863 else if (strcmp (name, "mips32") == 0)
11864 mips_opts.isa = ISA_MIPS32;
11865 else if (strcmp (name, "mips32r2") == 0)
11866 mips_opts.isa = ISA_MIPS32R2;
11867 else if (strcmp (name, "mips64") == 0)
11868 mips_opts.isa = ISA_MIPS64;
11869 else if (strcmp (name, "mips64r2") == 0)
11870 mips_opts.isa = ISA_MIPS64R2;
11871 else if (strcmp (name, "arch=default") == 0)
11872 {
11873 reset = 1;
11874 mips_opts.arch = file_mips_arch;
11875 mips_opts.isa = file_mips_isa;
11876 }
11877 else if (strncmp (name, "arch=", 5) == 0)
11878 {
11879 const struct mips_cpu_info *p;
11880
11881 p = mips_parse_cpu("internal use", name + 5);
11882 if (!p)
11883 as_bad (_("unknown architecture %s"), name + 5);
11884 else
11885 {
11886 mips_opts.arch = p->cpu;
11887 mips_opts.isa = p->isa;
11888 }
11889 }
11890 else
11891 as_bad (_("unknown ISA level %s"), name + 4);
11892
11893 switch (mips_opts.isa)
11894 {
11895 case 0:
11896 break;
11897 case ISA_MIPS1:
11898 case ISA_MIPS2:
11899 case ISA_MIPS32:
11900 case ISA_MIPS32R2:
11901 mips_opts.gp32 = 1;
11902 mips_opts.fp32 = 1;
11903 break;
11904 case ISA_MIPS3:
11905 case ISA_MIPS4:
11906 case ISA_MIPS5:
11907 case ISA_MIPS64:
11908 case ISA_MIPS64R2:
11909 mips_opts.gp32 = 0;
11910 mips_opts.fp32 = 0;
11911 break;
11912 default:
11913 as_bad (_("unknown ISA level %s"), name + 4);
11914 break;
11915 }
11916 if (reset)
11917 {
11918 mips_opts.gp32 = file_mips_gp32;
11919 mips_opts.fp32 = file_mips_fp32;
11920 }
11921 }
11922 else if (strcmp (name, "autoextend") == 0)
11923 mips_opts.noautoextend = 0;
11924 else if (strcmp (name, "noautoextend") == 0)
11925 mips_opts.noautoextend = 1;
11926 else if (strcmp (name, "push") == 0)
11927 {
11928 struct mips_option_stack *s;
11929
11930 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11931 s->next = mips_opts_stack;
11932 s->options = mips_opts;
11933 mips_opts_stack = s;
11934 }
11935 else if (strcmp (name, "pop") == 0)
11936 {
11937 struct mips_option_stack *s;
11938
11939 s = mips_opts_stack;
11940 if (s == NULL)
11941 as_bad (_(".set pop with no .set push"));
11942 else
11943 {
11944 /* If we're changing the reorder mode we need to handle
11945 delay slots correctly. */
11946 if (s->options.noreorder && ! mips_opts.noreorder)
11947 mips_emit_delays (TRUE);
11948 else if (! s->options.noreorder && mips_opts.noreorder)
11949 {
11950 if (prev_nop_frag != NULL)
11951 {
11952 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11953 * (mips_opts.mips16 ? 2 : 4));
11954 prev_nop_frag = NULL;
11955 }
11956 }
11957
11958 mips_opts = s->options;
11959 mips_opts_stack = s->next;
11960 free (s);
11961 }
11962 }
11963 else
11964 {
11965 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11966 }
11967 *input_line_pointer = ch;
11968 demand_empty_rest_of_line ();
11969 }
11970
11971 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11972 .option pic2. It means to generate SVR4 PIC calls. */
11973
11974 static void
11975 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11976 {
11977 mips_pic = SVR4_PIC;
11978 mips_abicalls = TRUE;
11979
11980 if (g_switch_seen && g_switch_value != 0)
11981 as_warn (_("-G may not be used with SVR4 PIC code"));
11982 g_switch_value = 0;
11983
11984 bfd_set_gp_size (stdoutput, 0);
11985 demand_empty_rest_of_line ();
11986 }
11987
11988 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11989 PIC code. It sets the $gp register for the function based on the
11990 function address, which is in the register named in the argument.
11991 This uses a relocation against _gp_disp, which is handled specially
11992 by the linker. The result is:
11993 lui $gp,%hi(_gp_disp)
11994 addiu $gp,$gp,%lo(_gp_disp)
11995 addu $gp,$gp,.cpload argument
11996 The .cpload argument is normally $25 == $t9. */
11997
11998 static void
11999 s_cpload (int ignore ATTRIBUTE_UNUSED)
12000 {
12001 expressionS ex;
12002
12003 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12004 .cpload is ignored. */
12005 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12006 {
12007 s_ignore (0);
12008 return;
12009 }
12010
12011 /* .cpload should be in a .set noreorder section. */
12012 if (mips_opts.noreorder == 0)
12013 as_warn (_(".cpload not in noreorder section"));
12014
12015 ex.X_op = O_symbol;
12016 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12017 ex.X_op_symbol = NULL;
12018 ex.X_add_number = 0;
12019
12020 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12021 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12022
12023 macro_start ();
12024 macro_build_lui (&ex, mips_gp_register);
12025 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12026 mips_gp_register, BFD_RELOC_LO16);
12027 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12028 mips_gp_register, tc_get_register (0));
12029 macro_end ();
12030
12031 demand_empty_rest_of_line ();
12032 }
12033
12034 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12035 .cpsetup $reg1, offset|$reg2, label
12036
12037 If offset is given, this results in:
12038 sd $gp, offset($sp)
12039 lui $gp, %hi(%neg(%gp_rel(label)))
12040 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12041 daddu $gp, $gp, $reg1
12042
12043 If $reg2 is given, this results in:
12044 daddu $reg2, $gp, $0
12045 lui $gp, %hi(%neg(%gp_rel(label)))
12046 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12047 daddu $gp, $gp, $reg1
12048 $reg1 is normally $25 == $t9. */
12049 static void
12050 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
12051 {
12052 expressionS ex_off;
12053 expressionS ex_sym;
12054 int reg1;
12055 char *f;
12056
12057 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12058 We also need NewABI support. */
12059 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12060 {
12061 s_ignore (0);
12062 return;
12063 }
12064
12065 reg1 = tc_get_register (0);
12066 SKIP_WHITESPACE ();
12067 if (*input_line_pointer != ',')
12068 {
12069 as_bad (_("missing argument separator ',' for .cpsetup"));
12070 return;
12071 }
12072 else
12073 ++input_line_pointer;
12074 SKIP_WHITESPACE ();
12075 if (*input_line_pointer == '$')
12076 {
12077 mips_cpreturn_register = tc_get_register (0);
12078 mips_cpreturn_offset = -1;
12079 }
12080 else
12081 {
12082 mips_cpreturn_offset = get_absolute_expression ();
12083 mips_cpreturn_register = -1;
12084 }
12085 SKIP_WHITESPACE ();
12086 if (*input_line_pointer != ',')
12087 {
12088 as_bad (_("missing argument separator ',' for .cpsetup"));
12089 return;
12090 }
12091 else
12092 ++input_line_pointer;
12093 SKIP_WHITESPACE ();
12094 expression (&ex_sym);
12095
12096 macro_start ();
12097 if (mips_cpreturn_register == -1)
12098 {
12099 ex_off.X_op = O_constant;
12100 ex_off.X_add_symbol = NULL;
12101 ex_off.X_op_symbol = NULL;
12102 ex_off.X_add_number = mips_cpreturn_offset;
12103
12104 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
12105 BFD_RELOC_LO16, SP);
12106 }
12107 else
12108 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
12109 mips_gp_register, 0);
12110
12111 /* Ensure there's room for the next two instructions, so that `f'
12112 doesn't end up with an address in the wrong frag. */
12113 frag_grow (8);
12114 f = frag_more (0);
12115 macro_build (&ex_sym, "lui", "t,u", mips_gp_register, BFD_RELOC_GPREL16);
12116 fix_new (frag_now, f - frag_now->fr_literal,
12117 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12118 fix_new (frag_now, f - frag_now->fr_literal,
12119 4, NULL, 0, 0, BFD_RELOC_HI16_S);
12120
12121 f = frag_more (0);
12122 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12123 mips_gp_register, BFD_RELOC_GPREL16);
12124 fix_new (frag_now, f - frag_now->fr_literal,
12125 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12126 fix_new (frag_now, f - frag_now->fr_literal,
12127 4, NULL, 0, 0, BFD_RELOC_LO16);
12128
12129 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12130 mips_gp_register, reg1);
12131 macro_end ();
12132
12133 demand_empty_rest_of_line ();
12134 }
12135
12136 static void
12137 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12138 {
12139 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12140 .cplocal is ignored. */
12141 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12142 {
12143 s_ignore (0);
12144 return;
12145 }
12146
12147 mips_gp_register = tc_get_register (0);
12148 demand_empty_rest_of_line ();
12149 }
12150
12151 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12152 offset from $sp. The offset is remembered, and after making a PIC
12153 call $gp is restored from that location. */
12154
12155 static void
12156 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12157 {
12158 expressionS ex;
12159
12160 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12161 .cprestore is ignored. */
12162 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12163 {
12164 s_ignore (0);
12165 return;
12166 }
12167
12168 mips_cprestore_offset = get_absolute_expression ();
12169 mips_cprestore_valid = 1;
12170
12171 ex.X_op = O_constant;
12172 ex.X_add_symbol = NULL;
12173 ex.X_op_symbol = NULL;
12174 ex.X_add_number = mips_cprestore_offset;
12175
12176 macro_start ();
12177 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12178 SP, HAVE_64BIT_ADDRESSES);
12179 macro_end ();
12180
12181 demand_empty_rest_of_line ();
12182 }
12183
12184 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12185 was given in the preceding .cpsetup, it results in:
12186 ld $gp, offset($sp)
12187
12188 If a register $reg2 was given there, it results in:
12189 daddu $gp, $reg2, $0
12190 */
12191 static void
12192 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12193 {
12194 expressionS ex;
12195
12196 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12197 We also need NewABI support. */
12198 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12199 {
12200 s_ignore (0);
12201 return;
12202 }
12203
12204 macro_start ();
12205 if (mips_cpreturn_register == -1)
12206 {
12207 ex.X_op = O_constant;
12208 ex.X_add_symbol = NULL;
12209 ex.X_op_symbol = NULL;
12210 ex.X_add_number = mips_cpreturn_offset;
12211
12212 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12213 }
12214 else
12215 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12216 mips_cpreturn_register, 0);
12217 macro_end ();
12218
12219 demand_empty_rest_of_line ();
12220 }
12221
12222 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12223 code. It sets the offset to use in gp_rel relocations. */
12224
12225 static void
12226 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12227 {
12228 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12229 We also need NewABI support. */
12230 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12231 {
12232 s_ignore (0);
12233 return;
12234 }
12235
12236 mips_gprel_offset = get_absolute_expression ();
12237
12238 demand_empty_rest_of_line ();
12239 }
12240
12241 /* Handle the .gpword pseudo-op. This is used when generating PIC
12242 code. It generates a 32 bit GP relative reloc. */
12243
12244 static void
12245 s_gpword (int ignore ATTRIBUTE_UNUSED)
12246 {
12247 symbolS *label;
12248 expressionS ex;
12249 char *p;
12250
12251 /* When not generating PIC code, this is treated as .word. */
12252 if (mips_pic != SVR4_PIC)
12253 {
12254 s_cons (2);
12255 return;
12256 }
12257
12258 label = insn_labels != NULL ? insn_labels->label : NULL;
12259 mips_emit_delays (TRUE);
12260 if (auto_align)
12261 mips_align (2, 0, label);
12262 mips_clear_insn_labels ();
12263
12264 expression (&ex);
12265
12266 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12267 {
12268 as_bad (_("Unsupported use of .gpword"));
12269 ignore_rest_of_line ();
12270 }
12271
12272 p = frag_more (4);
12273 md_number_to_chars (p, 0, 4);
12274 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12275 BFD_RELOC_GPREL32);
12276
12277 demand_empty_rest_of_line ();
12278 }
12279
12280 static void
12281 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12282 {
12283 symbolS *label;
12284 expressionS ex;
12285 char *p;
12286
12287 /* When not generating PIC code, this is treated as .dword. */
12288 if (mips_pic != SVR4_PIC)
12289 {
12290 s_cons (3);
12291 return;
12292 }
12293
12294 label = insn_labels != NULL ? insn_labels->label : NULL;
12295 mips_emit_delays (TRUE);
12296 if (auto_align)
12297 mips_align (3, 0, label);
12298 mips_clear_insn_labels ();
12299
12300 expression (&ex);
12301
12302 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12303 {
12304 as_bad (_("Unsupported use of .gpdword"));
12305 ignore_rest_of_line ();
12306 }
12307
12308 p = frag_more (8);
12309 md_number_to_chars (p, 0, 8);
12310 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12311 BFD_RELOC_GPREL32);
12312
12313 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12314 ex.X_op = O_absent;
12315 ex.X_add_symbol = 0;
12316 ex.X_add_number = 0;
12317 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12318 BFD_RELOC_64);
12319
12320 demand_empty_rest_of_line ();
12321 }
12322
12323 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12324 tables in SVR4 PIC code. */
12325
12326 static void
12327 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12328 {
12329 int reg;
12330
12331 /* This is ignored when not generating SVR4 PIC code. */
12332 if (mips_pic != SVR4_PIC)
12333 {
12334 s_ignore (0);
12335 return;
12336 }
12337
12338 /* Add $gp to the register named as an argument. */
12339 macro_start ();
12340 reg = tc_get_register (0);
12341 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12342 macro_end ();
12343
12344 demand_empty_rest_of_line ();
12345 }
12346
12347 /* Handle the .insn pseudo-op. This marks instruction labels in
12348 mips16 mode. This permits the linker to handle them specially,
12349 such as generating jalx instructions when needed. We also make
12350 them odd for the duration of the assembly, in order to generate the
12351 right sort of code. We will make them even in the adjust_symtab
12352 routine, while leaving them marked. This is convenient for the
12353 debugger and the disassembler. The linker knows to make them odd
12354 again. */
12355
12356 static void
12357 s_insn (int ignore ATTRIBUTE_UNUSED)
12358 {
12359 mips16_mark_labels ();
12360
12361 demand_empty_rest_of_line ();
12362 }
12363
12364 /* Handle a .stabn directive. We need these in order to mark a label
12365 as being a mips16 text label correctly. Sometimes the compiler
12366 will emit a label, followed by a .stabn, and then switch sections.
12367 If the label and .stabn are in mips16 mode, then the label is
12368 really a mips16 text label. */
12369
12370 static void
12371 s_mips_stab (int type)
12372 {
12373 if (type == 'n')
12374 mips16_mark_labels ();
12375
12376 s_stab (type);
12377 }
12378
12379 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12380 */
12381
12382 static void
12383 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12384 {
12385 char *name;
12386 int c;
12387 symbolS *symbolP;
12388 expressionS exp;
12389
12390 name = input_line_pointer;
12391 c = get_symbol_end ();
12392 symbolP = symbol_find_or_make (name);
12393 S_SET_WEAK (symbolP);
12394 *input_line_pointer = c;
12395
12396 SKIP_WHITESPACE ();
12397
12398 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12399 {
12400 if (S_IS_DEFINED (symbolP))
12401 {
12402 as_bad ("ignoring attempt to redefine symbol %s",
12403 S_GET_NAME (symbolP));
12404 ignore_rest_of_line ();
12405 return;
12406 }
12407
12408 if (*input_line_pointer == ',')
12409 {
12410 ++input_line_pointer;
12411 SKIP_WHITESPACE ();
12412 }
12413
12414 expression (&exp);
12415 if (exp.X_op != O_symbol)
12416 {
12417 as_bad ("bad .weakext directive");
12418 ignore_rest_of_line ();
12419 return;
12420 }
12421 symbol_set_value_expression (symbolP, &exp);
12422 }
12423
12424 demand_empty_rest_of_line ();
12425 }
12426
12427 /* Parse a register string into a number. Called from the ECOFF code
12428 to parse .frame. The argument is non-zero if this is the frame
12429 register, so that we can record it in mips_frame_reg. */
12430
12431 int
12432 tc_get_register (int frame)
12433 {
12434 int reg;
12435
12436 SKIP_WHITESPACE ();
12437 if (*input_line_pointer++ != '$')
12438 {
12439 as_warn (_("expected `$'"));
12440 reg = ZERO;
12441 }
12442 else if (ISDIGIT (*input_line_pointer))
12443 {
12444 reg = get_absolute_expression ();
12445 if (reg < 0 || reg >= 32)
12446 {
12447 as_warn (_("Bad register number"));
12448 reg = ZERO;
12449 }
12450 }
12451 else
12452 {
12453 if (strncmp (input_line_pointer, "ra", 2) == 0)
12454 {
12455 reg = RA;
12456 input_line_pointer += 2;
12457 }
12458 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12459 {
12460 reg = FP;
12461 input_line_pointer += 2;
12462 }
12463 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12464 {
12465 reg = SP;
12466 input_line_pointer += 2;
12467 }
12468 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12469 {
12470 reg = GP;
12471 input_line_pointer += 2;
12472 }
12473 else if (strncmp (input_line_pointer, "at", 2) == 0)
12474 {
12475 reg = AT;
12476 input_line_pointer += 2;
12477 }
12478 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12479 {
12480 reg = KT0;
12481 input_line_pointer += 3;
12482 }
12483 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12484 {
12485 reg = KT1;
12486 input_line_pointer += 3;
12487 }
12488 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12489 {
12490 reg = ZERO;
12491 input_line_pointer += 4;
12492 }
12493 else
12494 {
12495 as_warn (_("Unrecognized register name"));
12496 reg = ZERO;
12497 while (ISALNUM(*input_line_pointer))
12498 input_line_pointer++;
12499 }
12500 }
12501 if (frame)
12502 {
12503 mips_frame_reg = reg != 0 ? reg : SP;
12504 mips_frame_reg_valid = 1;
12505 mips_cprestore_valid = 0;
12506 }
12507 return reg;
12508 }
12509
12510 valueT
12511 md_section_align (asection *seg, valueT addr)
12512 {
12513 int align = bfd_get_section_alignment (stdoutput, seg);
12514
12515 #ifdef OBJ_ELF
12516 /* We don't need to align ELF sections to the full alignment.
12517 However, Irix 5 may prefer that we align them at least to a 16
12518 byte boundary. We don't bother to align the sections if we are
12519 targeted for an embedded system. */
12520 if (strcmp (TARGET_OS, "elf") == 0)
12521 return addr;
12522 if (align > 4)
12523 align = 4;
12524 #endif
12525
12526 return ((addr + (1 << align) - 1) & (-1 << align));
12527 }
12528
12529 /* Utility routine, called from above as well. If called while the
12530 input file is still being read, it's only an approximation. (For
12531 example, a symbol may later become defined which appeared to be
12532 undefined earlier.) */
12533
12534 static int
12535 nopic_need_relax (symbolS *sym, int before_relaxing)
12536 {
12537 if (sym == 0)
12538 return 0;
12539
12540 if (g_switch_value > 0)
12541 {
12542 const char *symname;
12543 int change;
12544
12545 /* Find out whether this symbol can be referenced off the $gp
12546 register. It can be if it is smaller than the -G size or if
12547 it is in the .sdata or .sbss section. Certain symbols can
12548 not be referenced off the $gp, although it appears as though
12549 they can. */
12550 symname = S_GET_NAME (sym);
12551 if (symname != (const char *) NULL
12552 && (strcmp (symname, "eprol") == 0
12553 || strcmp (symname, "etext") == 0
12554 || strcmp (symname, "_gp") == 0
12555 || strcmp (symname, "edata") == 0
12556 || strcmp (symname, "_fbss") == 0
12557 || strcmp (symname, "_fdata") == 0
12558 || strcmp (symname, "_ftext") == 0
12559 || strcmp (symname, "end") == 0
12560 || strcmp (symname, "_gp_disp") == 0))
12561 change = 1;
12562 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12563 && (0
12564 #ifndef NO_ECOFF_DEBUGGING
12565 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12566 && (symbol_get_obj (sym)->ecoff_extern_size
12567 <= g_switch_value))
12568 #endif
12569 /* We must defer this decision until after the whole
12570 file has been read, since there might be a .extern
12571 after the first use of this symbol. */
12572 || (before_relaxing
12573 #ifndef NO_ECOFF_DEBUGGING
12574 && symbol_get_obj (sym)->ecoff_extern_size == 0
12575 #endif
12576 && S_GET_VALUE (sym) == 0)
12577 || (S_GET_VALUE (sym) != 0
12578 && S_GET_VALUE (sym) <= g_switch_value)))
12579 change = 0;
12580 else
12581 {
12582 const char *segname;
12583
12584 segname = segment_name (S_GET_SEGMENT (sym));
12585 assert (strcmp (segname, ".lit8") != 0
12586 && strcmp (segname, ".lit4") != 0);
12587 change = (strcmp (segname, ".sdata") != 0
12588 && strcmp (segname, ".sbss") != 0
12589 && strncmp (segname, ".sdata.", 7) != 0
12590 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12591 }
12592 return change;
12593 }
12594 else
12595 /* We are not optimizing for the $gp register. */
12596 return 1;
12597 }
12598
12599
12600 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12601
12602 static bfd_boolean
12603 pic_need_relax (symbolS *sym, asection *segtype)
12604 {
12605 asection *symsec;
12606 bfd_boolean linkonce;
12607
12608 /* Handle the case of a symbol equated to another symbol. */
12609 while (symbol_equated_reloc_p (sym))
12610 {
12611 symbolS *n;
12612
12613 /* It's possible to get a loop here in a badly written
12614 program. */
12615 n = symbol_get_value_expression (sym)->X_add_symbol;
12616 if (n == sym)
12617 break;
12618 sym = n;
12619 }
12620
12621 symsec = S_GET_SEGMENT (sym);
12622
12623 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12624 linkonce = FALSE;
12625 if (symsec != segtype && ! S_IS_LOCAL (sym))
12626 {
12627 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12628 != 0)
12629 linkonce = TRUE;
12630
12631 /* The GNU toolchain uses an extension for ELF: a section
12632 beginning with the magic string .gnu.linkonce is a linkonce
12633 section. */
12634 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12635 sizeof ".gnu.linkonce" - 1) == 0)
12636 linkonce = TRUE;
12637 }
12638
12639 /* This must duplicate the test in adjust_reloc_syms. */
12640 return (symsec != &bfd_und_section
12641 && symsec != &bfd_abs_section
12642 && ! bfd_is_com_section (symsec)
12643 && !linkonce
12644 #ifdef OBJ_ELF
12645 /* A global or weak symbol is treated as external. */
12646 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12647 || (! S_IS_WEAK (sym)
12648 && (! S_IS_EXTERNAL (sym)
12649 || mips_pic == EMBEDDED_PIC)))
12650 #endif
12651 );
12652 }
12653
12654
12655 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12656 extended opcode. SEC is the section the frag is in. */
12657
12658 static int
12659 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12660 {
12661 int type;
12662 register const struct mips16_immed_operand *op;
12663 offsetT val;
12664 int mintiny, maxtiny;
12665 segT symsec;
12666 fragS *sym_frag;
12667
12668 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12669 return 0;
12670 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12671 return 1;
12672
12673 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12674 op = mips16_immed_operands;
12675 while (op->type != type)
12676 {
12677 ++op;
12678 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12679 }
12680
12681 if (op->unsp)
12682 {
12683 if (type == '<' || type == '>' || type == '[' || type == ']')
12684 {
12685 mintiny = 1;
12686 maxtiny = 1 << op->nbits;
12687 }
12688 else
12689 {
12690 mintiny = 0;
12691 maxtiny = (1 << op->nbits) - 1;
12692 }
12693 }
12694 else
12695 {
12696 mintiny = - (1 << (op->nbits - 1));
12697 maxtiny = (1 << (op->nbits - 1)) - 1;
12698 }
12699
12700 sym_frag = symbol_get_frag (fragp->fr_symbol);
12701 val = S_GET_VALUE (fragp->fr_symbol);
12702 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12703
12704 if (op->pcrel)
12705 {
12706 addressT addr;
12707
12708 /* We won't have the section when we are called from
12709 mips_relax_frag. However, we will always have been called
12710 from md_estimate_size_before_relax first. If this is a
12711 branch to a different section, we mark it as such. If SEC is
12712 NULL, and the frag is not marked, then it must be a branch to
12713 the same section. */
12714 if (sec == NULL)
12715 {
12716 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12717 return 1;
12718 }
12719 else
12720 {
12721 /* Must have been called from md_estimate_size_before_relax. */
12722 if (symsec != sec)
12723 {
12724 fragp->fr_subtype =
12725 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12726
12727 /* FIXME: We should support this, and let the linker
12728 catch branches and loads that are out of range. */
12729 as_bad_where (fragp->fr_file, fragp->fr_line,
12730 _("unsupported PC relative reference to different section"));
12731
12732 return 1;
12733 }
12734 if (fragp != sym_frag && sym_frag->fr_address == 0)
12735 /* Assume non-extended on the first relaxation pass.
12736 The address we have calculated will be bogus if this is
12737 a forward branch to another frag, as the forward frag
12738 will have fr_address == 0. */
12739 return 0;
12740 }
12741
12742 /* In this case, we know for sure that the symbol fragment is in
12743 the same section. If the relax_marker of the symbol fragment
12744 differs from the relax_marker of this fragment, we have not
12745 yet adjusted the symbol fragment fr_address. We want to add
12746 in STRETCH in order to get a better estimate of the address.
12747 This particularly matters because of the shift bits. */
12748 if (stretch != 0
12749 && sym_frag->relax_marker != fragp->relax_marker)
12750 {
12751 fragS *f;
12752
12753 /* Adjust stretch for any alignment frag. Note that if have
12754 been expanding the earlier code, the symbol may be
12755 defined in what appears to be an earlier frag. FIXME:
12756 This doesn't handle the fr_subtype field, which specifies
12757 a maximum number of bytes to skip when doing an
12758 alignment. */
12759 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12760 {
12761 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12762 {
12763 if (stretch < 0)
12764 stretch = - ((- stretch)
12765 & ~ ((1 << (int) f->fr_offset) - 1));
12766 else
12767 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12768 if (stretch == 0)
12769 break;
12770 }
12771 }
12772 if (f != NULL)
12773 val += stretch;
12774 }
12775
12776 addr = fragp->fr_address + fragp->fr_fix;
12777
12778 /* The base address rules are complicated. The base address of
12779 a branch is the following instruction. The base address of a
12780 PC relative load or add is the instruction itself, but if it
12781 is in a delay slot (in which case it can not be extended) use
12782 the address of the instruction whose delay slot it is in. */
12783 if (type == 'p' || type == 'q')
12784 {
12785 addr += 2;
12786
12787 /* If we are currently assuming that this frag should be
12788 extended, then, the current address is two bytes
12789 higher. */
12790 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12791 addr += 2;
12792
12793 /* Ignore the low bit in the target, since it will be set
12794 for a text label. */
12795 if ((val & 1) != 0)
12796 --val;
12797 }
12798 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12799 addr -= 4;
12800 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12801 addr -= 2;
12802
12803 val -= addr & ~ ((1 << op->shift) - 1);
12804
12805 /* Branch offsets have an implicit 0 in the lowest bit. */
12806 if (type == 'p' || type == 'q')
12807 val /= 2;
12808
12809 /* If any of the shifted bits are set, we must use an extended
12810 opcode. If the address depends on the size of this
12811 instruction, this can lead to a loop, so we arrange to always
12812 use an extended opcode. We only check this when we are in
12813 the main relaxation loop, when SEC is NULL. */
12814 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12815 {
12816 fragp->fr_subtype =
12817 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12818 return 1;
12819 }
12820
12821 /* If we are about to mark a frag as extended because the value
12822 is precisely maxtiny + 1, then there is a chance of an
12823 infinite loop as in the following code:
12824 la $4,foo
12825 .skip 1020
12826 .align 2
12827 foo:
12828 In this case when the la is extended, foo is 0x3fc bytes
12829 away, so the la can be shrunk, but then foo is 0x400 away, so
12830 the la must be extended. To avoid this loop, we mark the
12831 frag as extended if it was small, and is about to become
12832 extended with a value of maxtiny + 1. */
12833 if (val == ((maxtiny + 1) << op->shift)
12834 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12835 && sec == NULL)
12836 {
12837 fragp->fr_subtype =
12838 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12839 return 1;
12840 }
12841 }
12842 else if (symsec != absolute_section && sec != NULL)
12843 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12844
12845 if ((val & ((1 << op->shift) - 1)) != 0
12846 || val < (mintiny << op->shift)
12847 || val > (maxtiny << op->shift))
12848 return 1;
12849 else
12850 return 0;
12851 }
12852
12853 /* Compute the length of a branch sequence, and adjust the
12854 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12855 worst-case length is computed, with UPDATE being used to indicate
12856 whether an unconditional (-1), branch-likely (+1) or regular (0)
12857 branch is to be computed. */
12858 static int
12859 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12860 {
12861 bfd_boolean toofar;
12862 int length;
12863
12864 if (fragp
12865 && S_IS_DEFINED (fragp->fr_symbol)
12866 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12867 {
12868 addressT addr;
12869 offsetT val;
12870
12871 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12872
12873 addr = fragp->fr_address + fragp->fr_fix + 4;
12874
12875 val -= addr;
12876
12877 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12878 }
12879 else if (fragp)
12880 /* If the symbol is not defined or it's in a different segment,
12881 assume the user knows what's going on and emit a short
12882 branch. */
12883 toofar = FALSE;
12884 else
12885 toofar = TRUE;
12886
12887 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12888 fragp->fr_subtype
12889 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12890 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12891 RELAX_BRANCH_LINK (fragp->fr_subtype),
12892 toofar);
12893
12894 length = 4;
12895 if (toofar)
12896 {
12897 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12898 length += 8;
12899
12900 if (mips_pic != NO_PIC)
12901 {
12902 /* Additional space for PIC loading of target address. */
12903 length += 8;
12904 if (mips_opts.isa == ISA_MIPS1)
12905 /* Additional space for $at-stabilizing nop. */
12906 length += 4;
12907 }
12908
12909 /* If branch is conditional. */
12910 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12911 length += 8;
12912 }
12913
12914 return length;
12915 }
12916
12917 /* Estimate the size of a frag before relaxing. Unless this is the
12918 mips16, we are not really relaxing here, and the final size is
12919 encoded in the subtype information. For the mips16, we have to
12920 decide whether we are using an extended opcode or not. */
12921
12922 int
12923 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12924 {
12925 int change;
12926
12927 if (RELAX_BRANCH_P (fragp->fr_subtype))
12928 {
12929
12930 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12931
12932 return fragp->fr_var;
12933 }
12934
12935 if (RELAX_MIPS16_P (fragp->fr_subtype))
12936 /* We don't want to modify the EXTENDED bit here; it might get us
12937 into infinite loops. We change it only in mips_relax_frag(). */
12938 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12939
12940 if (mips_pic == NO_PIC)
12941 change = nopic_need_relax (fragp->fr_symbol, 0);
12942 else if (mips_pic == SVR4_PIC)
12943 change = pic_need_relax (fragp->fr_symbol, segtype);
12944 else
12945 abort ();
12946
12947 if (change)
12948 {
12949 fragp->fr_subtype |= RELAX_USE_SECOND;
12950 return -RELAX_FIRST (fragp->fr_subtype);
12951 }
12952 else
12953 return -RELAX_SECOND (fragp->fr_subtype);
12954 }
12955
12956 /* This is called to see whether a reloc against a defined symbol
12957 should be converted into a reloc against a section. Don't adjust
12958 MIPS16 jump relocations, so we don't have to worry about the format
12959 of the offset in the .o file. Don't adjust relocations against
12960 mips16 symbols, so that the linker can find them if it needs to set
12961 up a stub. */
12962
12963 int
12964 mips_fix_adjustable (fixS *fixp)
12965 {
12966 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12967 return 0;
12968
12969 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12970 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12971 return 0;
12972
12973 if (fixp->fx_addsy == NULL)
12974 return 1;
12975
12976 #ifdef OBJ_ELF
12977 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12978 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12979 && fixp->fx_subsy == NULL)
12980 return 0;
12981 #endif
12982
12983 return 1;
12984 }
12985
12986 /* Translate internal representation of relocation info to BFD target
12987 format. */
12988
12989 arelent **
12990 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12991 {
12992 static arelent *retval[4];
12993 arelent *reloc;
12994 bfd_reloc_code_real_type code;
12995
12996 memset (retval, 0, sizeof(retval));
12997 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12998 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12999 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13000 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13001
13002 if (mips_pic == EMBEDDED_PIC
13003 && SWITCH_TABLE (fixp))
13004 {
13005 /* For a switch table entry we use a special reloc. The addend
13006 is actually the difference between the reloc address and the
13007 subtrahend. */
13008 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13009 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13010 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13011 fixp->fx_r_type = BFD_RELOC_GPREL32;
13012 }
13013 else if (fixp->fx_pcrel)
13014 {
13015 bfd_vma pcrel_address;
13016
13017 /* Set PCREL_ADDRESS to this relocation's "PC". The PC for high
13018 high-part relocs is the address of the low-part reloc. */
13019 if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13020 {
13021 assert (fixp->fx_next != NULL
13022 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13023 pcrel_address = (fixp->fx_next->fx_where
13024 + fixp->fx_next->fx_frag->fr_address);
13025 }
13026 else
13027 pcrel_address = reloc->address;
13028
13029 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13030 {
13031 /* At this point, fx_addnumber is "symbol offset - pcrel_address".
13032 Relocations want only the symbol offset. */
13033 reloc->addend = fixp->fx_addnumber + pcrel_address;
13034 }
13035 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16
13036 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13037 {
13038 /* We use a special addend for an internal RELLO or RELHI reloc. */
13039 if (symbol_section_p (fixp->fx_addsy))
13040 reloc->addend = pcrel_address - S_GET_VALUE (fixp->fx_subsy);
13041 else
13042 reloc->addend = fixp->fx_addnumber + pcrel_address;
13043 }
13044 else
13045 {
13046 /* A gruesome hack which is a result of the gruesome gas reloc
13047 handling. */
13048 reloc->addend = pcrel_address;
13049 }
13050 }
13051 else
13052 reloc->addend = fixp->fx_addnumber;
13053
13054 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13055 entry to be used in the relocation's section offset. */
13056 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13057 {
13058 reloc->address = reloc->addend;
13059 reloc->addend = 0;
13060 }
13061
13062 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13063 fixup_segment converted a non-PC relative reloc into a PC
13064 relative reloc. In such a case, we need to convert the reloc
13065 code. */
13066 code = fixp->fx_r_type;
13067 if (fixp->fx_pcrel)
13068 {
13069 switch (code)
13070 {
13071 case BFD_RELOC_8:
13072 code = BFD_RELOC_8_PCREL;
13073 break;
13074 case BFD_RELOC_16:
13075 code = BFD_RELOC_16_PCREL;
13076 break;
13077 case BFD_RELOC_32:
13078 code = BFD_RELOC_32_PCREL;
13079 break;
13080 case BFD_RELOC_64:
13081 code = BFD_RELOC_64_PCREL;
13082 break;
13083 case BFD_RELOC_8_PCREL:
13084 case BFD_RELOC_16_PCREL:
13085 case BFD_RELOC_32_PCREL:
13086 case BFD_RELOC_64_PCREL:
13087 case BFD_RELOC_16_PCREL_S2:
13088 case BFD_RELOC_PCREL_HI16_S:
13089 case BFD_RELOC_PCREL_LO16:
13090 break;
13091 default:
13092 as_bad_where (fixp->fx_file, fixp->fx_line,
13093 _("Cannot make %s relocation PC relative"),
13094 bfd_get_reloc_code_name (code));
13095 }
13096 }
13097
13098 /* To support a PC relative reloc when generating embedded PIC code
13099 for ECOFF, we use a Cygnus extension. We check for that here to
13100 make sure that we don't let such a reloc escape normally. */
13101 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13102 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13103 && code == BFD_RELOC_16_PCREL_S2
13104 && mips_pic != EMBEDDED_PIC)
13105 reloc->howto = NULL;
13106 else
13107 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13108
13109 if (reloc->howto == NULL)
13110 {
13111 as_bad_where (fixp->fx_file, fixp->fx_line,
13112 _("Can not represent %s relocation in this object file format"),
13113 bfd_get_reloc_code_name (code));
13114 retval[0] = NULL;
13115 }
13116
13117 return retval;
13118 }
13119
13120 /* Relax a machine dependent frag. This returns the amount by which
13121 the current size of the frag should change. */
13122
13123 int
13124 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
13125 {
13126 if (RELAX_BRANCH_P (fragp->fr_subtype))
13127 {
13128 offsetT old_var = fragp->fr_var;
13129
13130 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13131
13132 return fragp->fr_var - old_var;
13133 }
13134
13135 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13136 return 0;
13137
13138 if (mips16_extended_frag (fragp, NULL, stretch))
13139 {
13140 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13141 return 0;
13142 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13143 return 2;
13144 }
13145 else
13146 {
13147 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13148 return 0;
13149 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13150 return -2;
13151 }
13152
13153 return 0;
13154 }
13155
13156 /* Convert a machine dependent frag. */
13157
13158 void
13159 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
13160 {
13161 if (RELAX_BRANCH_P (fragp->fr_subtype))
13162 {
13163 bfd_byte *buf;
13164 unsigned long insn;
13165 expressionS exp;
13166 fixS *fixp;
13167
13168 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13169
13170 if (target_big_endian)
13171 insn = bfd_getb32 (buf);
13172 else
13173 insn = bfd_getl32 (buf);
13174
13175 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13176 {
13177 /* We generate a fixup instead of applying it right now
13178 because, if there are linker relaxations, we're going to
13179 need the relocations. */
13180 exp.X_op = O_symbol;
13181 exp.X_add_symbol = fragp->fr_symbol;
13182 exp.X_add_number = fragp->fr_offset;
13183
13184 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13185 4, &exp, 1,
13186 BFD_RELOC_16_PCREL_S2);
13187 fixp->fx_file = fragp->fr_file;
13188 fixp->fx_line = fragp->fr_line;
13189
13190 md_number_to_chars (buf, insn, 4);
13191 buf += 4;
13192 }
13193 else
13194 {
13195 int i;
13196
13197 as_warn_where (fragp->fr_file, fragp->fr_line,
13198 _("relaxed out-of-range branch into a jump"));
13199
13200 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13201 goto uncond;
13202
13203 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13204 {
13205 /* Reverse the branch. */
13206 switch ((insn >> 28) & 0xf)
13207 {
13208 case 4:
13209 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13210 have the condition reversed by tweaking a single
13211 bit, and their opcodes all have 0x4???????. */
13212 assert ((insn & 0xf1000000) == 0x41000000);
13213 insn ^= 0x00010000;
13214 break;
13215
13216 case 0:
13217 /* bltz 0x04000000 bgez 0x04010000
13218 bltzal 0x04100000 bgezal 0x04110000 */
13219 assert ((insn & 0xfc0e0000) == 0x04000000);
13220 insn ^= 0x00010000;
13221 break;
13222
13223 case 1:
13224 /* beq 0x10000000 bne 0x14000000
13225 blez 0x18000000 bgtz 0x1c000000 */
13226 insn ^= 0x04000000;
13227 break;
13228
13229 default:
13230 abort ();
13231 }
13232 }
13233
13234 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13235 {
13236 /* Clear the and-link bit. */
13237 assert ((insn & 0xfc1c0000) == 0x04100000);
13238
13239 /* bltzal 0x04100000 bgezal 0x04110000
13240 bltzall 0x04120000 bgezall 0x04130000 */
13241 insn &= ~0x00100000;
13242 }
13243
13244 /* Branch over the branch (if the branch was likely) or the
13245 full jump (not likely case). Compute the offset from the
13246 current instruction to branch to. */
13247 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13248 i = 16;
13249 else
13250 {
13251 /* How many bytes in instructions we've already emitted? */
13252 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13253 /* How many bytes in instructions from here to the end? */
13254 i = fragp->fr_var - i;
13255 }
13256 /* Convert to instruction count. */
13257 i >>= 2;
13258 /* Branch counts from the next instruction. */
13259 i--;
13260 insn |= i;
13261 /* Branch over the jump. */
13262 md_number_to_chars (buf, insn, 4);
13263 buf += 4;
13264
13265 /* Nop */
13266 md_number_to_chars (buf, 0, 4);
13267 buf += 4;
13268
13269 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13270 {
13271 /* beql $0, $0, 2f */
13272 insn = 0x50000000;
13273 /* Compute the PC offset from the current instruction to
13274 the end of the variable frag. */
13275 /* How many bytes in instructions we've already emitted? */
13276 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13277 /* How many bytes in instructions from here to the end? */
13278 i = fragp->fr_var - i;
13279 /* Convert to instruction count. */
13280 i >>= 2;
13281 /* Don't decrement i, because we want to branch over the
13282 delay slot. */
13283
13284 insn |= i;
13285 md_number_to_chars (buf, insn, 4);
13286 buf += 4;
13287
13288 md_number_to_chars (buf, 0, 4);
13289 buf += 4;
13290 }
13291
13292 uncond:
13293 if (mips_pic == NO_PIC)
13294 {
13295 /* j or jal. */
13296 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13297 ? 0x0c000000 : 0x08000000);
13298 exp.X_op = O_symbol;
13299 exp.X_add_symbol = fragp->fr_symbol;
13300 exp.X_add_number = fragp->fr_offset;
13301
13302 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13303 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13304 fixp->fx_file = fragp->fr_file;
13305 fixp->fx_line = fragp->fr_line;
13306
13307 md_number_to_chars (buf, insn, 4);
13308 buf += 4;
13309 }
13310 else
13311 {
13312 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13313 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13314 exp.X_op = O_symbol;
13315 exp.X_add_symbol = fragp->fr_symbol;
13316 exp.X_add_number = fragp->fr_offset;
13317
13318 if (fragp->fr_offset)
13319 {
13320 exp.X_add_symbol = make_expr_symbol (&exp);
13321 exp.X_add_number = 0;
13322 }
13323
13324 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13325 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13326 fixp->fx_file = fragp->fr_file;
13327 fixp->fx_line = fragp->fr_line;
13328
13329 md_number_to_chars (buf, insn, 4);
13330 buf += 4;
13331
13332 if (mips_opts.isa == ISA_MIPS1)
13333 {
13334 /* nop */
13335 md_number_to_chars (buf, 0, 4);
13336 buf += 4;
13337 }
13338
13339 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13340 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13341
13342 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13343 4, &exp, 0, BFD_RELOC_LO16);
13344 fixp->fx_file = fragp->fr_file;
13345 fixp->fx_line = fragp->fr_line;
13346
13347 md_number_to_chars (buf, insn, 4);
13348 buf += 4;
13349
13350 /* j(al)r $at. */
13351 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13352 insn = 0x0020f809;
13353 else
13354 insn = 0x00200008;
13355
13356 md_number_to_chars (buf, insn, 4);
13357 buf += 4;
13358 }
13359 }
13360
13361 assert (buf == (bfd_byte *)fragp->fr_literal
13362 + fragp->fr_fix + fragp->fr_var);
13363
13364 fragp->fr_fix += fragp->fr_var;
13365
13366 return;
13367 }
13368
13369 if (RELAX_MIPS16_P (fragp->fr_subtype))
13370 {
13371 int type;
13372 register const struct mips16_immed_operand *op;
13373 bfd_boolean small, ext;
13374 offsetT val;
13375 bfd_byte *buf;
13376 unsigned long insn;
13377 bfd_boolean use_extend;
13378 unsigned short extend;
13379
13380 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13381 op = mips16_immed_operands;
13382 while (op->type != type)
13383 ++op;
13384
13385 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13386 {
13387 small = FALSE;
13388 ext = TRUE;
13389 }
13390 else
13391 {
13392 small = TRUE;
13393 ext = FALSE;
13394 }
13395
13396 resolve_symbol_value (fragp->fr_symbol);
13397 val = S_GET_VALUE (fragp->fr_symbol);
13398 if (op->pcrel)
13399 {
13400 addressT addr;
13401
13402 addr = fragp->fr_address + fragp->fr_fix;
13403
13404 /* The rules for the base address of a PC relative reloc are
13405 complicated; see mips16_extended_frag. */
13406 if (type == 'p' || type == 'q')
13407 {
13408 addr += 2;
13409 if (ext)
13410 addr += 2;
13411 /* Ignore the low bit in the target, since it will be
13412 set for a text label. */
13413 if ((val & 1) != 0)
13414 --val;
13415 }
13416 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13417 addr -= 4;
13418 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13419 addr -= 2;
13420
13421 addr &= ~ (addressT) ((1 << op->shift) - 1);
13422 val -= addr;
13423
13424 /* Make sure the section winds up with the alignment we have
13425 assumed. */
13426 if (op->shift > 0)
13427 record_alignment (asec, op->shift);
13428 }
13429
13430 if (ext
13431 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13432 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13433 as_warn_where (fragp->fr_file, fragp->fr_line,
13434 _("extended instruction in delay slot"));
13435
13436 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13437
13438 if (target_big_endian)
13439 insn = bfd_getb16 (buf);
13440 else
13441 insn = bfd_getl16 (buf);
13442
13443 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13444 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13445 small, ext, &insn, &use_extend, &extend);
13446
13447 if (use_extend)
13448 {
13449 md_number_to_chars (buf, 0xf000 | extend, 2);
13450 fragp->fr_fix += 2;
13451 buf += 2;
13452 }
13453
13454 md_number_to_chars (buf, insn, 2);
13455 fragp->fr_fix += 2;
13456 buf += 2;
13457 }
13458 else
13459 {
13460 int first, second;
13461 fixS *fixp;
13462
13463 first = RELAX_FIRST (fragp->fr_subtype);
13464 second = RELAX_SECOND (fragp->fr_subtype);
13465 fixp = (fixS *) fragp->fr_opcode;
13466
13467 /* Possibly emit a warning if we've chosen the longer option. */
13468 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13469 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13470 {
13471 const char *msg = macro_warning (fragp->fr_subtype);
13472 if (msg != 0)
13473 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13474 }
13475
13476 /* Go through all the fixups for the first sequence. Disable them
13477 (by marking them as done) if we're going to use the second
13478 sequence instead. */
13479 while (fixp
13480 && fixp->fx_frag == fragp
13481 && fixp->fx_where < fragp->fr_fix - second)
13482 {
13483 if (fragp->fr_subtype & RELAX_USE_SECOND)
13484 fixp->fx_done = 1;
13485 fixp = fixp->fx_next;
13486 }
13487
13488 /* Go through the fixups for the second sequence. Disable them if
13489 we're going to use the first sequence, otherwise adjust their
13490 addresses to account for the relaxation. */
13491 while (fixp && fixp->fx_frag == fragp)
13492 {
13493 if (fragp->fr_subtype & RELAX_USE_SECOND)
13494 fixp->fx_where -= first;
13495 else
13496 fixp->fx_done = 1;
13497 fixp = fixp->fx_next;
13498 }
13499
13500 /* Now modify the frag contents. */
13501 if (fragp->fr_subtype & RELAX_USE_SECOND)
13502 {
13503 char *start;
13504
13505 start = fragp->fr_literal + fragp->fr_fix - first - second;
13506 memmove (start, start + first, second);
13507 fragp->fr_fix -= first;
13508 }
13509 else
13510 fragp->fr_fix -= second;
13511 }
13512 }
13513
13514 #ifdef OBJ_ELF
13515
13516 /* This function is called after the relocs have been generated.
13517 We've been storing mips16 text labels as odd. Here we convert them
13518 back to even for the convenience of the debugger. */
13519
13520 void
13521 mips_frob_file_after_relocs (void)
13522 {
13523 asymbol **syms;
13524 unsigned int count, i;
13525
13526 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13527 return;
13528
13529 syms = bfd_get_outsymbols (stdoutput);
13530 count = bfd_get_symcount (stdoutput);
13531 for (i = 0; i < count; i++, syms++)
13532 {
13533 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13534 && ((*syms)->value & 1) != 0)
13535 {
13536 (*syms)->value &= ~1;
13537 /* If the symbol has an odd size, it was probably computed
13538 incorrectly, so adjust that as well. */
13539 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13540 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13541 }
13542 }
13543 }
13544
13545 #endif
13546
13547 /* This function is called whenever a label is defined. It is used
13548 when handling branch delays; if a branch has a label, we assume we
13549 can not move it. */
13550
13551 void
13552 mips_define_label (symbolS *sym)
13553 {
13554 struct insn_label_list *l;
13555
13556 if (free_insn_labels == NULL)
13557 l = (struct insn_label_list *) xmalloc (sizeof *l);
13558 else
13559 {
13560 l = free_insn_labels;
13561 free_insn_labels = l->next;
13562 }
13563
13564 l->label = sym;
13565 l->next = insn_labels;
13566 insn_labels = l;
13567 }
13568 \f
13569 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13570
13571 /* Some special processing for a MIPS ELF file. */
13572
13573 void
13574 mips_elf_final_processing (void)
13575 {
13576 /* Write out the register information. */
13577 if (mips_abi != N64_ABI)
13578 {
13579 Elf32_RegInfo s;
13580
13581 s.ri_gprmask = mips_gprmask;
13582 s.ri_cprmask[0] = mips_cprmask[0];
13583 s.ri_cprmask[1] = mips_cprmask[1];
13584 s.ri_cprmask[2] = mips_cprmask[2];
13585 s.ri_cprmask[3] = mips_cprmask[3];
13586 /* The gp_value field is set by the MIPS ELF backend. */
13587
13588 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13589 ((Elf32_External_RegInfo *)
13590 mips_regmask_frag));
13591 }
13592 else
13593 {
13594 Elf64_Internal_RegInfo s;
13595
13596 s.ri_gprmask = mips_gprmask;
13597 s.ri_pad = 0;
13598 s.ri_cprmask[0] = mips_cprmask[0];
13599 s.ri_cprmask[1] = mips_cprmask[1];
13600 s.ri_cprmask[2] = mips_cprmask[2];
13601 s.ri_cprmask[3] = mips_cprmask[3];
13602 /* The gp_value field is set by the MIPS ELF backend. */
13603
13604 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13605 ((Elf64_External_RegInfo *)
13606 mips_regmask_frag));
13607 }
13608
13609 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13610 sort of BFD interface for this. */
13611 if (mips_any_noreorder)
13612 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13613 if (mips_pic != NO_PIC)
13614 {
13615 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13616 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13617 }
13618 if (mips_abicalls)
13619 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13620
13621 /* Set MIPS ELF flags for ASEs. */
13622 if (file_ase_mips16)
13623 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13624 #if 0 /* XXX FIXME */
13625 if (file_ase_mips3d)
13626 elf_elfheader (stdoutput)->e_flags |= ???;
13627 #endif
13628 if (file_ase_mdmx)
13629 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13630
13631 /* Set the MIPS ELF ABI flags. */
13632 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13633 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13634 else if (mips_abi == O64_ABI)
13635 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13636 else if (mips_abi == EABI_ABI)
13637 {
13638 if (!file_mips_gp32)
13639 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13640 else
13641 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13642 }
13643 else if (mips_abi == N32_ABI)
13644 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13645
13646 /* Nothing to do for N64_ABI. */
13647
13648 if (mips_32bitmode)
13649 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13650 }
13651
13652 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13653 \f
13654 typedef struct proc {
13655 symbolS *isym;
13656 unsigned long reg_mask;
13657 unsigned long reg_offset;
13658 unsigned long fpreg_mask;
13659 unsigned long fpreg_offset;
13660 unsigned long frame_offset;
13661 unsigned long frame_reg;
13662 unsigned long pc_reg;
13663 } procS;
13664
13665 static procS cur_proc;
13666 static procS *cur_proc_ptr;
13667 static int numprocs;
13668
13669 /* Fill in an rs_align_code fragment. */
13670
13671 void
13672 mips_handle_align (fragS *fragp)
13673 {
13674 if (fragp->fr_type != rs_align_code)
13675 return;
13676
13677 if (mips_opts.mips16)
13678 {
13679 static const unsigned char be_nop[] = { 0x65, 0x00 };
13680 static const unsigned char le_nop[] = { 0x00, 0x65 };
13681
13682 int bytes;
13683 char *p;
13684
13685 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13686 p = fragp->fr_literal + fragp->fr_fix;
13687
13688 if (bytes & 1)
13689 {
13690 *p++ = 0;
13691 fragp->fr_fix++;
13692 }
13693
13694 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13695 fragp->fr_var = 2;
13696 }
13697
13698 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13699 }
13700
13701 static void
13702 md_obj_begin (void)
13703 {
13704 }
13705
13706 static void
13707 md_obj_end (void)
13708 {
13709 /* check for premature end, nesting errors, etc */
13710 if (cur_proc_ptr)
13711 as_warn (_("missing .end at end of assembly"));
13712 }
13713
13714 static long
13715 get_number (void)
13716 {
13717 int negative = 0;
13718 long val = 0;
13719
13720 if (*input_line_pointer == '-')
13721 {
13722 ++input_line_pointer;
13723 negative = 1;
13724 }
13725 if (!ISDIGIT (*input_line_pointer))
13726 as_bad (_("expected simple number"));
13727 if (input_line_pointer[0] == '0')
13728 {
13729 if (input_line_pointer[1] == 'x')
13730 {
13731 input_line_pointer += 2;
13732 while (ISXDIGIT (*input_line_pointer))
13733 {
13734 val <<= 4;
13735 val |= hex_value (*input_line_pointer++);
13736 }
13737 return negative ? -val : val;
13738 }
13739 else
13740 {
13741 ++input_line_pointer;
13742 while (ISDIGIT (*input_line_pointer))
13743 {
13744 val <<= 3;
13745 val |= *input_line_pointer++ - '0';
13746 }
13747 return negative ? -val : val;
13748 }
13749 }
13750 if (!ISDIGIT (*input_line_pointer))
13751 {
13752 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13753 *input_line_pointer, *input_line_pointer);
13754 as_warn (_("invalid number"));
13755 return -1;
13756 }
13757 while (ISDIGIT (*input_line_pointer))
13758 {
13759 val *= 10;
13760 val += *input_line_pointer++ - '0';
13761 }
13762 return negative ? -val : val;
13763 }
13764
13765 /* The .file directive; just like the usual .file directive, but there
13766 is an initial number which is the ECOFF file index. In the non-ECOFF
13767 case .file implies DWARF-2. */
13768
13769 static void
13770 s_mips_file (int x ATTRIBUTE_UNUSED)
13771 {
13772 static int first_file_directive = 0;
13773
13774 if (ECOFF_DEBUGGING)
13775 {
13776 get_number ();
13777 s_app_file (0);
13778 }
13779 else
13780 {
13781 char *filename;
13782
13783 filename = dwarf2_directive_file (0);
13784
13785 /* Versions of GCC up to 3.1 start files with a ".file"
13786 directive even for stabs output. Make sure that this
13787 ".file" is handled. Note that you need a version of GCC
13788 after 3.1 in order to support DWARF-2 on MIPS. */
13789 if (filename != NULL && ! first_file_directive)
13790 {
13791 (void) new_logical_line (filename, -1);
13792 s_app_file_string (filename);
13793 }
13794 first_file_directive = 1;
13795 }
13796 }
13797
13798 /* The .loc directive, implying DWARF-2. */
13799
13800 static void
13801 s_mips_loc (int x ATTRIBUTE_UNUSED)
13802 {
13803 if (!ECOFF_DEBUGGING)
13804 dwarf2_directive_loc (0);
13805 }
13806
13807 /* The .end directive. */
13808
13809 static void
13810 s_mips_end (int x ATTRIBUTE_UNUSED)
13811 {
13812 symbolS *p;
13813
13814 /* Following functions need their own .frame and .cprestore directives. */
13815 mips_frame_reg_valid = 0;
13816 mips_cprestore_valid = 0;
13817
13818 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13819 {
13820 p = get_symbol ();
13821 demand_empty_rest_of_line ();
13822 }
13823 else
13824 p = NULL;
13825
13826 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13827 as_warn (_(".end not in text section"));
13828
13829 if (!cur_proc_ptr)
13830 {
13831 as_warn (_(".end directive without a preceding .ent directive."));
13832 demand_empty_rest_of_line ();
13833 return;
13834 }
13835
13836 if (p != NULL)
13837 {
13838 assert (S_GET_NAME (p));
13839 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13840 as_warn (_(".end symbol does not match .ent symbol."));
13841
13842 if (debug_type == DEBUG_STABS)
13843 stabs_generate_asm_endfunc (S_GET_NAME (p),
13844 S_GET_NAME (p));
13845 }
13846 else
13847 as_warn (_(".end directive missing or unknown symbol"));
13848
13849 #ifdef OBJ_ELF
13850 /* Generate a .pdr section. */
13851 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13852 && mips_flag_pdr)
13853 {
13854 segT saved_seg = now_seg;
13855 subsegT saved_subseg = now_subseg;
13856 valueT dot;
13857 expressionS exp;
13858 char *fragp;
13859
13860 dot = frag_now_fix ();
13861
13862 #ifdef md_flush_pending_output
13863 md_flush_pending_output ();
13864 #endif
13865
13866 assert (pdr_seg);
13867 subseg_set (pdr_seg, 0);
13868
13869 /* Write the symbol. */
13870 exp.X_op = O_symbol;
13871 exp.X_add_symbol = p;
13872 exp.X_add_number = 0;
13873 emit_expr (&exp, 4);
13874
13875 fragp = frag_more (7 * 4);
13876
13877 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13878 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13879 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13880 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13881 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13882 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13883 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13884
13885 subseg_set (saved_seg, saved_subseg);
13886 }
13887 #endif /* OBJ_ELF */
13888
13889 cur_proc_ptr = NULL;
13890 }
13891
13892 /* The .aent and .ent directives. */
13893
13894 static void
13895 s_mips_ent (int aent)
13896 {
13897 symbolS *symbolP;
13898
13899 symbolP = get_symbol ();
13900 if (*input_line_pointer == ',')
13901 ++input_line_pointer;
13902 SKIP_WHITESPACE ();
13903 if (ISDIGIT (*input_line_pointer)
13904 || *input_line_pointer == '-')
13905 get_number ();
13906
13907 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13908 as_warn (_(".ent or .aent not in text section."));
13909
13910 if (!aent && cur_proc_ptr)
13911 as_warn (_("missing .end"));
13912
13913 if (!aent)
13914 {
13915 /* This function needs its own .frame and .cprestore directives. */
13916 mips_frame_reg_valid = 0;
13917 mips_cprestore_valid = 0;
13918
13919 cur_proc_ptr = &cur_proc;
13920 memset (cur_proc_ptr, '\0', sizeof (procS));
13921
13922 cur_proc_ptr->isym = symbolP;
13923
13924 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13925
13926 ++numprocs;
13927
13928 if (debug_type == DEBUG_STABS)
13929 stabs_generate_asm_func (S_GET_NAME (symbolP),
13930 S_GET_NAME (symbolP));
13931 }
13932
13933 demand_empty_rest_of_line ();
13934 }
13935
13936 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13937 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13938 s_mips_frame is used so that we can set the PDR information correctly.
13939 We can't use the ecoff routines because they make reference to the ecoff
13940 symbol table (in the mdebug section). */
13941
13942 static void
13943 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13944 {
13945 #ifdef OBJ_ELF
13946 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13947 {
13948 long val;
13949
13950 if (cur_proc_ptr == (procS *) NULL)
13951 {
13952 as_warn (_(".frame outside of .ent"));
13953 demand_empty_rest_of_line ();
13954 return;
13955 }
13956
13957 cur_proc_ptr->frame_reg = tc_get_register (1);
13958
13959 SKIP_WHITESPACE ();
13960 if (*input_line_pointer++ != ','
13961 || get_absolute_expression_and_terminator (&val) != ',')
13962 {
13963 as_warn (_("Bad .frame directive"));
13964 --input_line_pointer;
13965 demand_empty_rest_of_line ();
13966 return;
13967 }
13968
13969 cur_proc_ptr->frame_offset = val;
13970 cur_proc_ptr->pc_reg = tc_get_register (0);
13971
13972 demand_empty_rest_of_line ();
13973 }
13974 else
13975 #endif /* OBJ_ELF */
13976 s_ignore (ignore);
13977 }
13978
13979 /* The .fmask and .mask directives. If the mdebug section is present
13980 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13981 embedded targets, s_mips_mask is used so that we can set the PDR
13982 information correctly. We can't use the ecoff routines because they
13983 make reference to the ecoff symbol table (in the mdebug section). */
13984
13985 static void
13986 s_mips_mask (int reg_type)
13987 {
13988 #ifdef OBJ_ELF
13989 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13990 {
13991 long mask, off;
13992
13993 if (cur_proc_ptr == (procS *) NULL)
13994 {
13995 as_warn (_(".mask/.fmask outside of .ent"));
13996 demand_empty_rest_of_line ();
13997 return;
13998 }
13999
14000 if (get_absolute_expression_and_terminator (&mask) != ',')
14001 {
14002 as_warn (_("Bad .mask/.fmask directive"));
14003 --input_line_pointer;
14004 demand_empty_rest_of_line ();
14005 return;
14006 }
14007
14008 off = get_absolute_expression ();
14009
14010 if (reg_type == 'F')
14011 {
14012 cur_proc_ptr->fpreg_mask = mask;
14013 cur_proc_ptr->fpreg_offset = off;
14014 }
14015 else
14016 {
14017 cur_proc_ptr->reg_mask = mask;
14018 cur_proc_ptr->reg_offset = off;
14019 }
14020
14021 demand_empty_rest_of_line ();
14022 }
14023 else
14024 #endif /* OBJ_ELF */
14025 s_ignore (reg_type);
14026 }
14027
14028 /* The .loc directive. */
14029
14030 #if 0
14031 static void
14032 s_loc (int x)
14033 {
14034 symbolS *symbolP;
14035 int lineno;
14036 int addroff;
14037
14038 assert (now_seg == text_section);
14039
14040 lineno = get_number ();
14041 addroff = frag_now_fix ();
14042
14043 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14044 S_SET_TYPE (symbolP, N_SLINE);
14045 S_SET_OTHER (symbolP, 0);
14046 S_SET_DESC (symbolP, lineno);
14047 symbolP->sy_segment = now_seg;
14048 }
14049 #endif
14050
14051 /* A table describing all the processors gas knows about. Names are
14052 matched in the order listed.
14053
14054 To ease comparison, please keep this table in the same order as
14055 gcc's mips_cpu_info_table[]. */
14056 static const struct mips_cpu_info mips_cpu_info_table[] =
14057 {
14058 /* Entries for generic ISAs */
14059 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14060 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14061 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14062 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14063 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14064 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14065 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
14066 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14067 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
14068
14069 /* MIPS I */
14070 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14071 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14072 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14073
14074 /* MIPS II */
14075 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14076
14077 /* MIPS III */
14078 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14079 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14080 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14081 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14082 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14083 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14084 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14085 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14086 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14087 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14088 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14089 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14090
14091 /* MIPS IV */
14092 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14093 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14094 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14095 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14096 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14097 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14098 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14099 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14100 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14101 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14102 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14103 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14104 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
14105
14106 /* MIPS 32 */
14107 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14108 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14109 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14110
14111 /* MIPS 64 */
14112 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14113 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14114
14115 /* Broadcom SB-1 CPU core */
14116 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14117
14118 /* End marker */
14119 { NULL, 0, 0, 0 }
14120 };
14121
14122
14123 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14124 with a final "000" replaced by "k". Ignore case.
14125
14126 Note: this function is shared between GCC and GAS. */
14127
14128 static bfd_boolean
14129 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
14130 {
14131 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14132 given++, canonical++;
14133
14134 return ((*given == 0 && *canonical == 0)
14135 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14136 }
14137
14138
14139 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14140 CPU name. We've traditionally allowed a lot of variation here.
14141
14142 Note: this function is shared between GCC and GAS. */
14143
14144 static bfd_boolean
14145 mips_matching_cpu_name_p (const char *canonical, const char *given)
14146 {
14147 /* First see if the name matches exactly, or with a final "000"
14148 turned into "k". */
14149 if (mips_strict_matching_cpu_name_p (canonical, given))
14150 return TRUE;
14151
14152 /* If not, try comparing based on numerical designation alone.
14153 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14154 if (TOLOWER (*given) == 'r')
14155 given++;
14156 if (!ISDIGIT (*given))
14157 return FALSE;
14158
14159 /* Skip over some well-known prefixes in the canonical name,
14160 hoping to find a number there too. */
14161 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14162 canonical += 2;
14163 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14164 canonical += 2;
14165 else if (TOLOWER (canonical[0]) == 'r')
14166 canonical += 1;
14167
14168 return mips_strict_matching_cpu_name_p (canonical, given);
14169 }
14170
14171
14172 /* Parse an option that takes the name of a processor as its argument.
14173 OPTION is the name of the option and CPU_STRING is the argument.
14174 Return the corresponding processor enumeration if the CPU_STRING is
14175 recognized, otherwise report an error and return null.
14176
14177 A similar function exists in GCC. */
14178
14179 static const struct mips_cpu_info *
14180 mips_parse_cpu (const char *option, const char *cpu_string)
14181 {
14182 const struct mips_cpu_info *p;
14183
14184 /* 'from-abi' selects the most compatible architecture for the given
14185 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14186 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14187 version. Look first at the -mgp options, if given, otherwise base
14188 the choice on MIPS_DEFAULT_64BIT.
14189
14190 Treat NO_ABI like the EABIs. One reason to do this is that the
14191 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14192 architecture. This code picks MIPS I for 'mips' and MIPS III for
14193 'mips64', just as we did in the days before 'from-abi'. */
14194 if (strcasecmp (cpu_string, "from-abi") == 0)
14195 {
14196 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14197 return mips_cpu_info_from_isa (ISA_MIPS1);
14198
14199 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14200 return mips_cpu_info_from_isa (ISA_MIPS3);
14201
14202 if (file_mips_gp32 >= 0)
14203 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14204
14205 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14206 ? ISA_MIPS3
14207 : ISA_MIPS1);
14208 }
14209
14210 /* 'default' has traditionally been a no-op. Probably not very useful. */
14211 if (strcasecmp (cpu_string, "default") == 0)
14212 return 0;
14213
14214 for (p = mips_cpu_info_table; p->name != 0; p++)
14215 if (mips_matching_cpu_name_p (p->name, cpu_string))
14216 return p;
14217
14218 as_bad ("Bad value (%s) for %s", cpu_string, option);
14219 return 0;
14220 }
14221
14222 /* Return the canonical processor information for ISA (a member of the
14223 ISA_MIPS* enumeration). */
14224
14225 static const struct mips_cpu_info *
14226 mips_cpu_info_from_isa (int isa)
14227 {
14228 int i;
14229
14230 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14231 if (mips_cpu_info_table[i].is_isa
14232 && isa == mips_cpu_info_table[i].isa)
14233 return (&mips_cpu_info_table[i]);
14234
14235 return NULL;
14236 }
14237
14238 static const struct mips_cpu_info *
14239 mips_cpu_info_from_arch (int arch)
14240 {
14241 int i;
14242
14243 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14244 if (arch == mips_cpu_info_table[i].cpu)
14245 return (&mips_cpu_info_table[i]);
14246
14247 return NULL;
14248 }
14249 \f
14250 static void
14251 show (FILE *stream, const char *string, int *col_p, int *first_p)
14252 {
14253 if (*first_p)
14254 {
14255 fprintf (stream, "%24s", "");
14256 *col_p = 24;
14257 }
14258 else
14259 {
14260 fprintf (stream, ", ");
14261 *col_p += 2;
14262 }
14263
14264 if (*col_p + strlen (string) > 72)
14265 {
14266 fprintf (stream, "\n%24s", "");
14267 *col_p = 24;
14268 }
14269
14270 fprintf (stream, "%s", string);
14271 *col_p += strlen (string);
14272
14273 *first_p = 0;
14274 }
14275
14276 void
14277 md_show_usage (FILE *stream)
14278 {
14279 int column, first;
14280 size_t i;
14281
14282 fprintf (stream, _("\
14283 MIPS options:\n\
14284 -EB generate big endian output\n\
14285 -EL generate little endian output\n\
14286 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14287 -G NUM allow referencing objects up to NUM bytes\n\
14288 implicitly with the gp register [default 8]\n"));
14289 fprintf (stream, _("\
14290 -mips1 generate MIPS ISA I instructions\n\
14291 -mips2 generate MIPS ISA II instructions\n\
14292 -mips3 generate MIPS ISA III instructions\n\
14293 -mips4 generate MIPS ISA IV instructions\n\
14294 -mips5 generate MIPS ISA V instructions\n\
14295 -mips32 generate MIPS32 ISA instructions\n\
14296 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14297 -mips64 generate MIPS64 ISA instructions\n\
14298 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14299 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14300
14301 first = 1;
14302
14303 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14304 show (stream, mips_cpu_info_table[i].name, &column, &first);
14305 show (stream, "from-abi", &column, &first);
14306 fputc ('\n', stream);
14307
14308 fprintf (stream, _("\
14309 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14310 -no-mCPU don't generate code specific to CPU.\n\
14311 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14312
14313 first = 1;
14314
14315 show (stream, "3900", &column, &first);
14316 show (stream, "4010", &column, &first);
14317 show (stream, "4100", &column, &first);
14318 show (stream, "4650", &column, &first);
14319 fputc ('\n', stream);
14320
14321 fprintf (stream, _("\
14322 -mips16 generate mips16 instructions\n\
14323 -no-mips16 do not generate mips16 instructions\n"));
14324 fprintf (stream, _("\
14325 -mfix-vr4120 work around certain VR4120 errata\n\
14326 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14327 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14328 -O0 remove unneeded NOPs, do not swap branches\n\
14329 -O remove unneeded NOPs and swap branches\n\
14330 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14331 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14332 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14333 #ifdef OBJ_ELF
14334 fprintf (stream, _("\
14335 -KPIC, -call_shared generate SVR4 position independent code\n\
14336 -non_shared do not generate position independent code\n\
14337 -xgot assume a 32 bit GOT\n\
14338 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14339 -mabi=ABI create ABI conformant object file for:\n"));
14340
14341 first = 1;
14342
14343 show (stream, "32", &column, &first);
14344 show (stream, "o64", &column, &first);
14345 show (stream, "n32", &column, &first);
14346 show (stream, "64", &column, &first);
14347 show (stream, "eabi", &column, &first);
14348
14349 fputc ('\n', stream);
14350
14351 fprintf (stream, _("\
14352 -32 create o32 ABI object file (default)\n\
14353 -n32 create n32 ABI object file\n\
14354 -64 create 64 ABI object file\n"));
14355 #endif
14356 }
14357
14358 enum dwarf2_format
14359 mips_dwarf2_format (void)
14360 {
14361 if (mips_abi == N64_ABI)
14362 {
14363 #ifdef TE_IRIX
14364 return dwarf2_format_64bit_irix;
14365 #else
14366 return dwarf2_format_64bit;
14367 #endif
14368 }
14369 else
14370 return dwarf2_format_32bit;
14371 }
14372
14373 int
14374 mips_dwarf2_addr_size (void)
14375 {
14376 if (mips_abi == N64_ABI)
14377 return 8;
14378 else
14379 return 4;
14380 }
This page took 0.497874 seconds and 4 git commands to generate.