* config/tc-mips.c (mips_cl_insn): Replace reloc_type array with
[binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #include <stdarg.h>
32
33 #include "opcode/mips.h"
34 #include "itbl-ops.h"
35 #include "dwarf2dbg.h"
36
37 #ifdef DEBUG
38 #define DBG(x) printf x
39 #else
40 #define DBG(x)
41 #endif
42
43 #ifdef OBJ_MAYBE_ELF
44 /* Clean up namespace so we can include obj-elf.h too. */
45 static int mips_output_flavor (void);
46 static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
47 #undef OBJ_PROCESS_STAB
48 #undef OUTPUT_FLAVOR
49 #undef S_GET_ALIGN
50 #undef S_GET_SIZE
51 #undef S_SET_ALIGN
52 #undef S_SET_SIZE
53 #undef obj_frob_file
54 #undef obj_frob_file_after_relocs
55 #undef obj_frob_symbol
56 #undef obj_pop_insert
57 #undef obj_sec_sym_ok_for_reloc
58 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60 #include "obj-elf.h"
61 /* Fix any of them that we actually care about. */
62 #undef OUTPUT_FLAVOR
63 #define OUTPUT_FLAVOR mips_output_flavor()
64 #endif
65
66 #if defined (OBJ_ELF)
67 #include "elf/mips.h"
68 #endif
69
70 #ifndef ECOFF_DEBUGGING
71 #define NO_ECOFF_DEBUGGING
72 #define ECOFF_DEBUGGING 0
73 #endif
74
75 int mips_flag_mdebug = -1;
76
77 /* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80 #ifdef TE_IRIX
81 int mips_flag_pdr = FALSE;
82 #else
83 int mips_flag_pdr = TRUE;
84 #endif
85
86 #include "ecoff.h"
87
88 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89 static char *mips_regmask_frag;
90 #endif
91
92 #define ZERO 0
93 #define AT 1
94 #define TREG 24
95 #define PIC_CALL_REG 25
96 #define KT0 26
97 #define KT1 27
98 #define GP 28
99 #define SP 29
100 #define FP 30
101 #define RA 31
102
103 #define ILLEGAL_REG (32)
104
105 /* Allow override of standard little-endian ECOFF format. */
106
107 #ifndef ECOFF_LITTLE_FORMAT
108 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109 #endif
110
111 extern int target_big_endian;
112
113 /* The name of the readonly data section. */
114 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
122 /* Information about an instruction, including its format, operands
123 and fixups. */
124 struct mips_cl_insn
125 {
126 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
127 const struct mips_opcode *insn_mo;
128
129 /* True if this is a mips16 instruction and if we want the extended
130 form of INSN_MO. */
131 bfd_boolean use_extend;
132
133 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
134 unsigned short extend;
135
136 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
137 a copy of INSN_MO->match with the operands filled in. */
138 unsigned long insn_opcode;
139
140 /* The frag that contains the instruction. */
141 struct frag *frag;
142
143 /* The offset into FRAG of the first instruction byte. */
144 long where;
145
146 /* The relocs associated with the instruction, if any. */
147 fixS *fixp[3];
148
149 /* True if this entry describes a real instruction. */
150 unsigned int valid_p : 1;
151
152 /* True if this instruction occured in a .set noreorder block. */
153 unsigned int noreorder_p : 1;
154
155 /* True if this instruction corresponds to an assembler-filled
156 delay slot. Always false if noreorder_p. */
157 unsigned int delay_slot_p : 1;
158
159 /* True for extended mips16 instructions. */
160 unsigned int extended_p : 1;
161
162 /* True for mips16 instructions that jump to an absolute address. */
163 unsigned int mips16_absolute_jump_p : 1;
164 };
165
166 /* The ABI to use. */
167 enum mips_abi_level
168 {
169 NO_ABI = 0,
170 O32_ABI,
171 O64_ABI,
172 N32_ABI,
173 N64_ABI,
174 EABI_ABI
175 };
176
177 /* MIPS ABI we are using for this output file. */
178 static enum mips_abi_level mips_abi = NO_ABI;
179
180 /* Whether or not we have code that can call pic code. */
181 int mips_abicalls = FALSE;
182
183 /* Whether or not we have code which can be put into a shared
184 library. */
185 static bfd_boolean mips_in_shared = TRUE;
186
187 /* This is the set of options which may be modified by the .set
188 pseudo-op. We use a struct so that .set push and .set pop are more
189 reliable. */
190
191 struct mips_set_options
192 {
193 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
194 if it has not been initialized. Changed by `.set mipsN', and the
195 -mipsN command line option, and the default CPU. */
196 int isa;
197 /* Enabled Application Specific Extensions (ASEs). These are set to -1
198 if they have not been initialized. Changed by `.set <asename>', by
199 command line options, and based on the default architecture. */
200 int ase_mips3d;
201 int ase_mdmx;
202 /* Whether we are assembling for the mips16 processor. 0 if we are
203 not, 1 if we are, and -1 if the value has not been initialized.
204 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
205 -nomips16 command line options, and the default CPU. */
206 int mips16;
207 /* Non-zero if we should not reorder instructions. Changed by `.set
208 reorder' and `.set noreorder'. */
209 int noreorder;
210 /* Non-zero if we should not permit the $at ($1) register to be used
211 in instructions. Changed by `.set at' and `.set noat'. */
212 int noat;
213 /* Non-zero if we should warn when a macro instruction expands into
214 more than one machine instruction. Changed by `.set nomacro' and
215 `.set macro'. */
216 int warn_about_macros;
217 /* Non-zero if we should not move instructions. Changed by `.set
218 move', `.set volatile', `.set nomove', and `.set novolatile'. */
219 int nomove;
220 /* Non-zero if we should not optimize branches by moving the target
221 of the branch into the delay slot. Actually, we don't perform
222 this optimization anyhow. Changed by `.set bopt' and `.set
223 nobopt'. */
224 int nobopt;
225 /* Non-zero if we should not autoextend mips16 instructions.
226 Changed by `.set autoextend' and `.set noautoextend'. */
227 int noautoextend;
228 /* Restrict general purpose registers and floating point registers
229 to 32 bit. This is initially determined when -mgp32 or -mfp32
230 is passed but can changed if the assembler code uses .set mipsN. */
231 int gp32;
232 int fp32;
233 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
234 command line option, and the default CPU. */
235 int arch;
236 /* True if ".set sym32" is in effect. */
237 bfd_boolean sym32;
238 };
239
240 /* True if -mgp32 was passed. */
241 static int file_mips_gp32 = -1;
242
243 /* True if -mfp32 was passed. */
244 static int file_mips_fp32 = -1;
245
246 /* This is the struct we use to hold the current set of options. Note
247 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
248 -1 to indicate that they have not been initialized. */
249
250 static struct mips_set_options mips_opts =
251 {
252 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
253 };
254
255 /* These variables are filled in with the masks of registers used.
256 The object format code reads them and puts them in the appropriate
257 place. */
258 unsigned long mips_gprmask;
259 unsigned long mips_cprmask[4];
260
261 /* MIPS ISA we are using for this output file. */
262 static int file_mips_isa = ISA_UNKNOWN;
263
264 /* True if -mips16 was passed or implied by arguments passed on the
265 command line (e.g., by -march). */
266 static int file_ase_mips16;
267
268 /* True if -mips3d was passed or implied by arguments passed on the
269 command line (e.g., by -march). */
270 static int file_ase_mips3d;
271
272 /* True if -mdmx was passed or implied by arguments passed on the
273 command line (e.g., by -march). */
274 static int file_ase_mdmx;
275
276 /* The argument of the -march= flag. The architecture we are assembling. */
277 static int file_mips_arch = CPU_UNKNOWN;
278 static const char *mips_arch_string;
279
280 /* The argument of the -mtune= flag. The architecture for which we
281 are optimizing. */
282 static int mips_tune = CPU_UNKNOWN;
283 static const char *mips_tune_string;
284
285 /* True when generating 32-bit code for a 64-bit processor. */
286 static int mips_32bitmode = 0;
287
288 /* True if the given ABI requires 32-bit registers. */
289 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
290
291 /* Likewise 64-bit registers. */
292 #define ABI_NEEDS_64BIT_REGS(ABI) \
293 ((ABI) == N32_ABI \
294 || (ABI) == N64_ABI \
295 || (ABI) == O64_ABI)
296
297 /* Return true if ISA supports 64 bit gp register instructions. */
298 #define ISA_HAS_64BIT_REGS(ISA) ( \
299 (ISA) == ISA_MIPS3 \
300 || (ISA) == ISA_MIPS4 \
301 || (ISA) == ISA_MIPS5 \
302 || (ISA) == ISA_MIPS64 \
303 || (ISA) == ISA_MIPS64R2 \
304 )
305
306 /* Return true if ISA supports 64-bit right rotate (dror et al.)
307 instructions. */
308 #define ISA_HAS_DROR(ISA) ( \
309 (ISA) == ISA_MIPS64R2 \
310 )
311
312 /* Return true if ISA supports 32-bit right rotate (ror et al.)
313 instructions. */
314 #define ISA_HAS_ROR(ISA) ( \
315 (ISA) == ISA_MIPS32R2 \
316 || (ISA) == ISA_MIPS64R2 \
317 )
318
319 #define HAVE_32BIT_GPRS \
320 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
321
322 #define HAVE_32BIT_FPRS \
323 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
324
325 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
326 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
327
328 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
329
330 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
331
332 /* True if relocations are stored in-place. */
333 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
334
335 /* The ABI-derived address size. */
336 #define HAVE_64BIT_ADDRESSES \
337 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
338 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
339
340 /* The size of symbolic constants (i.e., expressions of the form
341 "SYMBOL" or "SYMBOL + OFFSET"). */
342 #define HAVE_32BIT_SYMBOLS \
343 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
344 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
345
346 /* Addresses are loaded in different ways, depending on the address size
347 in use. The n32 ABI Documentation also mandates the use of additions
348 with overflow checking, but existing implementations don't follow it. */
349 #define ADDRESS_ADD_INSN \
350 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
351
352 #define ADDRESS_ADDI_INSN \
353 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
354
355 #define ADDRESS_LOAD_INSN \
356 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
357
358 #define ADDRESS_STORE_INSN \
359 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
360
361 /* Return true if the given CPU supports the MIPS16 ASE. */
362 #define CPU_HAS_MIPS16(cpu) \
363 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
364 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
365
366 /* Return true if the given CPU supports the MIPS3D ASE. */
367 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
368 )
369
370 /* Return true if the given CPU supports the MDMX ASE. */
371 #define CPU_HAS_MDMX(cpu) (FALSE \
372 )
373
374 /* True if CPU has a dror instruction. */
375 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
376
377 /* True if CPU has a ror instruction. */
378 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
379
380 /* True if mflo and mfhi can be immediately followed by instructions
381 which write to the HI and LO registers.
382
383 According to MIPS specifications, MIPS ISAs I, II, and III need
384 (at least) two instructions between the reads of HI/LO and
385 instructions which write them, and later ISAs do not. Contradicting
386 the MIPS specifications, some MIPS IV processor user manuals (e.g.
387 the UM for the NEC Vr5000) document needing the instructions between
388 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
389 MIPS64 and later ISAs to have the interlocks, plus any specific
390 earlier-ISA CPUs for which CPU documentation declares that the
391 instructions are really interlocked. */
392 #define hilo_interlocks \
393 (mips_opts.isa == ISA_MIPS32 \
394 || mips_opts.isa == ISA_MIPS32R2 \
395 || mips_opts.isa == ISA_MIPS64 \
396 || mips_opts.isa == ISA_MIPS64R2 \
397 || mips_opts.arch == CPU_R4010 \
398 || mips_opts.arch == CPU_R10000 \
399 || mips_opts.arch == CPU_R12000 \
400 || mips_opts.arch == CPU_RM7000 \
401 || mips_opts.arch == CPU_VR5500 \
402 )
403
404 /* Whether the processor uses hardware interlocks to protect reads
405 from the GPRs after they are loaded from memory, and thus does not
406 require nops to be inserted. This applies to instructions marked
407 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
408 level I. */
409 #define gpr_interlocks \
410 (mips_opts.isa != ISA_MIPS1 \
411 || mips_opts.arch == CPU_R3900)
412
413 /* Whether the processor uses hardware interlocks to avoid delays
414 required by coprocessor instructions, and thus does not require
415 nops to be inserted. This applies to instructions marked
416 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
417 between instructions marked INSN_WRITE_COND_CODE and ones marked
418 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
419 levels I, II, and III. */
420 /* Itbl support may require additional care here. */
421 #define cop_interlocks \
422 ((mips_opts.isa != ISA_MIPS1 \
423 && mips_opts.isa != ISA_MIPS2 \
424 && mips_opts.isa != ISA_MIPS3) \
425 || mips_opts.arch == CPU_R4300 \
426 )
427
428 /* Whether the processor uses hardware interlocks to protect reads
429 from coprocessor registers after they are loaded from memory, and
430 thus does not require nops to be inserted. This applies to
431 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
432 requires at MIPS ISA level I. */
433 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
434
435 /* Is this a mfhi or mflo instruction? */
436 #define MF_HILO_INSN(PINFO) \
437 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
438
439 /* MIPS PIC level. */
440
441 enum mips_pic_level mips_pic;
442
443 /* 1 if we should generate 32 bit offsets from the $gp register in
444 SVR4_PIC mode. Currently has no meaning in other modes. */
445 static int mips_big_got = 0;
446
447 /* 1 if trap instructions should used for overflow rather than break
448 instructions. */
449 static int mips_trap = 0;
450
451 /* 1 if double width floating point constants should not be constructed
452 by assembling two single width halves into two single width floating
453 point registers which just happen to alias the double width destination
454 register. On some architectures this aliasing can be disabled by a bit
455 in the status register, and the setting of this bit cannot be determined
456 automatically at assemble time. */
457 static int mips_disable_float_construction;
458
459 /* Non-zero if any .set noreorder directives were used. */
460
461 static int mips_any_noreorder;
462
463 /* Non-zero if nops should be inserted when the register referenced in
464 an mfhi/mflo instruction is read in the next two instructions. */
465 static int mips_7000_hilo_fix;
466
467 /* The size of the small data section. */
468 static unsigned int g_switch_value = 8;
469 /* Whether the -G option was used. */
470 static int g_switch_seen = 0;
471
472 #define N_RMASK 0xc4
473 #define N_VFP 0xd4
474
475 /* If we can determine in advance that GP optimization won't be
476 possible, we can skip the relaxation stuff that tries to produce
477 GP-relative references. This makes delay slot optimization work
478 better.
479
480 This function can only provide a guess, but it seems to work for
481 gcc output. It needs to guess right for gcc, otherwise gcc
482 will put what it thinks is a GP-relative instruction in a branch
483 delay slot.
484
485 I don't know if a fix is needed for the SVR4_PIC mode. I've only
486 fixed it for the non-PIC mode. KR 95/04/07 */
487 static int nopic_need_relax (symbolS *, int);
488
489 /* handle of the OPCODE hash table */
490 static struct hash_control *op_hash = NULL;
491
492 /* The opcode hash table we use for the mips16. */
493 static struct hash_control *mips16_op_hash = NULL;
494
495 /* This array holds the chars that always start a comment. If the
496 pre-processor is disabled, these aren't very useful */
497 const char comment_chars[] = "#";
498
499 /* This array holds the chars that only start a comment at the beginning of
500 a line. If the line seems to have the form '# 123 filename'
501 .line and .file directives will appear in the pre-processed output */
502 /* Note that input_file.c hand checks for '#' at the beginning of the
503 first line of the input file. This is because the compiler outputs
504 #NO_APP at the beginning of its output. */
505 /* Also note that C style comments are always supported. */
506 const char line_comment_chars[] = "#";
507
508 /* This array holds machine specific line separator characters. */
509 const char line_separator_chars[] = ";";
510
511 /* Chars that can be used to separate mant from exp in floating point nums */
512 const char EXP_CHARS[] = "eE";
513
514 /* Chars that mean this number is a floating point constant */
515 /* As in 0f12.456 */
516 /* or 0d1.2345e12 */
517 const char FLT_CHARS[] = "rRsSfFdDxXpP";
518
519 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
520 changed in read.c . Ideally it shouldn't have to know about it at all,
521 but nothing is ideal around here.
522 */
523
524 static char *insn_error;
525
526 static int auto_align = 1;
527
528 /* When outputting SVR4 PIC code, the assembler needs to know the
529 offset in the stack frame from which to restore the $gp register.
530 This is set by the .cprestore pseudo-op, and saved in this
531 variable. */
532 static offsetT mips_cprestore_offset = -1;
533
534 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
535 more optimizations, it can use a register value instead of a memory-saved
536 offset and even an other register than $gp as global pointer. */
537 static offsetT mips_cpreturn_offset = -1;
538 static int mips_cpreturn_register = -1;
539 static int mips_gp_register = GP;
540 static int mips_gprel_offset = 0;
541
542 /* Whether mips_cprestore_offset has been set in the current function
543 (or whether it has already been warned about, if not). */
544 static int mips_cprestore_valid = 0;
545
546 /* This is the register which holds the stack frame, as set by the
547 .frame pseudo-op. This is needed to implement .cprestore. */
548 static int mips_frame_reg = SP;
549
550 /* Whether mips_frame_reg has been set in the current function
551 (or whether it has already been warned about, if not). */
552 static int mips_frame_reg_valid = 0;
553
554 /* To output NOP instructions correctly, we need to keep information
555 about the previous two instructions. */
556
557 /* Whether we are optimizing. The default value of 2 means to remove
558 unneeded NOPs and swap branch instructions when possible. A value
559 of 1 means to not swap branches. A value of 0 means to always
560 insert NOPs. */
561 static int mips_optimize = 2;
562
563 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
564 equivalent to seeing no -g option at all. */
565 static int mips_debug = 0;
566
567 /* A list of previous instructions, with index 0 being the most recent. */
568 static struct mips_cl_insn history[2];
569
570 /* If we don't want information for a history[] entry, we
571 point the insn_mo field at this dummy integer. */
572 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0, 0 };
573
574 /* If this is set, it points to a frag holding nop instructions which
575 were inserted before the start of a noreorder section. If those
576 nops turn out to be unnecessary, the size of the frag can be
577 decreased. */
578 static fragS *prev_nop_frag;
579
580 /* The number of nop instructions we created in prev_nop_frag. */
581 static int prev_nop_frag_holds;
582
583 /* The number of nop instructions that we know we need in
584 prev_nop_frag. */
585 static int prev_nop_frag_required;
586
587 /* The number of instructions we've seen since prev_nop_frag. */
588 static int prev_nop_frag_since;
589
590 /* For ECOFF and ELF, relocations against symbols are done in two
591 parts, with a HI relocation and a LO relocation. Each relocation
592 has only 16 bits of space to store an addend. This means that in
593 order for the linker to handle carries correctly, it must be able
594 to locate both the HI and the LO relocation. This means that the
595 relocations must appear in order in the relocation table.
596
597 In order to implement this, we keep track of each unmatched HI
598 relocation. We then sort them so that they immediately precede the
599 corresponding LO relocation. */
600
601 struct mips_hi_fixup
602 {
603 /* Next HI fixup. */
604 struct mips_hi_fixup *next;
605 /* This fixup. */
606 fixS *fixp;
607 /* The section this fixup is in. */
608 segT seg;
609 };
610
611 /* The list of unmatched HI relocs. */
612
613 static struct mips_hi_fixup *mips_hi_fixup_list;
614
615 /* The frag containing the last explicit relocation operator.
616 Null if explicit relocations have not been used. */
617
618 static fragS *prev_reloc_op_frag;
619
620 /* Map normal MIPS register numbers to mips16 register numbers. */
621
622 #define X ILLEGAL_REG
623 static const int mips32_to_16_reg_map[] =
624 {
625 X, X, 2, 3, 4, 5, 6, 7,
626 X, X, X, X, X, X, X, X,
627 0, 1, X, X, X, X, X, X,
628 X, X, X, X, X, X, X, X
629 };
630 #undef X
631
632 /* Map mips16 register numbers to normal MIPS register numbers. */
633
634 static const unsigned int mips16_to_32_reg_map[] =
635 {
636 16, 17, 2, 3, 4, 5, 6, 7
637 };
638
639 static int mips_fix_vr4120;
640
641 /* We don't relax branches by default, since this causes us to expand
642 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
643 fail to compute the offset before expanding the macro to the most
644 efficient expansion. */
645
646 static int mips_relax_branch;
647 \f
648 /* The expansion of many macros depends on the type of symbol that
649 they refer to. For example, when generating position-dependent code,
650 a macro that refers to a symbol may have two different expansions,
651 one which uses GP-relative addresses and one which uses absolute
652 addresses. When generating SVR4-style PIC, a macro may have
653 different expansions for local and global symbols.
654
655 We handle these situations by generating both sequences and putting
656 them in variant frags. In position-dependent code, the first sequence
657 will be the GP-relative one and the second sequence will be the
658 absolute one. In SVR4 PIC, the first sequence will be for global
659 symbols and the second will be for local symbols.
660
661 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
662 SECOND are the lengths of the two sequences in bytes. These fields
663 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
664 the subtype has the following flags:
665
666 RELAX_USE_SECOND
667 Set if it has been decided that we should use the second
668 sequence instead of the first.
669
670 RELAX_SECOND_LONGER
671 Set in the first variant frag if the macro's second implementation
672 is longer than its first. This refers to the macro as a whole,
673 not an individual relaxation.
674
675 RELAX_NOMACRO
676 Set in the first variant frag if the macro appeared in a .set nomacro
677 block and if one alternative requires a warning but the other does not.
678
679 RELAX_DELAY_SLOT
680 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
681 delay slot.
682
683 The frag's "opcode" points to the first fixup for relaxable code.
684
685 Relaxable macros are generated using a sequence such as:
686
687 relax_start (SYMBOL);
688 ... generate first expansion ...
689 relax_switch ();
690 ... generate second expansion ...
691 relax_end ();
692
693 The code and fixups for the unwanted alternative are discarded
694 by md_convert_frag. */
695 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
696
697 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
698 #define RELAX_SECOND(X) ((X) & 0xff)
699 #define RELAX_USE_SECOND 0x10000
700 #define RELAX_SECOND_LONGER 0x20000
701 #define RELAX_NOMACRO 0x40000
702 #define RELAX_DELAY_SLOT 0x80000
703
704 /* Branch without likely bit. If label is out of range, we turn:
705
706 beq reg1, reg2, label
707 delay slot
708
709 into
710
711 bne reg1, reg2, 0f
712 nop
713 j label
714 0: delay slot
715
716 with the following opcode replacements:
717
718 beq <-> bne
719 blez <-> bgtz
720 bltz <-> bgez
721 bc1f <-> bc1t
722
723 bltzal <-> bgezal (with jal label instead of j label)
724
725 Even though keeping the delay slot instruction in the delay slot of
726 the branch would be more efficient, it would be very tricky to do
727 correctly, because we'd have to introduce a variable frag *after*
728 the delay slot instruction, and expand that instead. Let's do it
729 the easy way for now, even if the branch-not-taken case now costs
730 one additional instruction. Out-of-range branches are not supposed
731 to be common, anyway.
732
733 Branch likely. If label is out of range, we turn:
734
735 beql reg1, reg2, label
736 delay slot (annulled if branch not taken)
737
738 into
739
740 beql reg1, reg2, 1f
741 nop
742 beql $0, $0, 2f
743 nop
744 1: j[al] label
745 delay slot (executed only if branch taken)
746 2:
747
748 It would be possible to generate a shorter sequence by losing the
749 likely bit, generating something like:
750
751 bne reg1, reg2, 0f
752 nop
753 j[al] label
754 delay slot (executed only if branch taken)
755 0:
756
757 beql -> bne
758 bnel -> beq
759 blezl -> bgtz
760 bgtzl -> blez
761 bltzl -> bgez
762 bgezl -> bltz
763 bc1fl -> bc1t
764 bc1tl -> bc1f
765
766 bltzall -> bgezal (with jal label instead of j label)
767 bgezall -> bltzal (ditto)
768
769
770 but it's not clear that it would actually improve performance. */
771 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
772 ((relax_substateT) \
773 (0xc0000000 \
774 | ((toofar) ? 1 : 0) \
775 | ((link) ? 2 : 0) \
776 | ((likely) ? 4 : 0) \
777 | ((uncond) ? 8 : 0)))
778 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
779 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
780 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
781 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
782 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
783
784 /* For mips16 code, we use an entirely different form of relaxation.
785 mips16 supports two versions of most instructions which take
786 immediate values: a small one which takes some small value, and a
787 larger one which takes a 16 bit value. Since branches also follow
788 this pattern, relaxing these values is required.
789
790 We can assemble both mips16 and normal MIPS code in a single
791 object. Therefore, we need to support this type of relaxation at
792 the same time that we support the relaxation described above. We
793 use the high bit of the subtype field to distinguish these cases.
794
795 The information we store for this type of relaxation is the
796 argument code found in the opcode file for this relocation, whether
797 the user explicitly requested a small or extended form, and whether
798 the relocation is in a jump or jal delay slot. That tells us the
799 size of the value, and how it should be stored. We also store
800 whether the fragment is considered to be extended or not. We also
801 store whether this is known to be a branch to a different section,
802 whether we have tried to relax this frag yet, and whether we have
803 ever extended a PC relative fragment because of a shift count. */
804 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
805 (0x80000000 \
806 | ((type) & 0xff) \
807 | ((small) ? 0x100 : 0) \
808 | ((ext) ? 0x200 : 0) \
809 | ((dslot) ? 0x400 : 0) \
810 | ((jal_dslot) ? 0x800 : 0))
811 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
812 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
813 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
814 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
815 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
816 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
817 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
818 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
819 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
820 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
821 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
822 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
823
824 /* Is the given value a sign-extended 32-bit value? */
825 #define IS_SEXT_32BIT_NUM(x) \
826 (((x) &~ (offsetT) 0x7fffffff) == 0 \
827 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
828
829 /* Is the given value a sign-extended 16-bit value? */
830 #define IS_SEXT_16BIT_NUM(x) \
831 (((x) &~ (offsetT) 0x7fff) == 0 \
832 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
833
834 \f
835 /* Global variables used when generating relaxable macros. See the
836 comment above RELAX_ENCODE for more details about how relaxation
837 is used. */
838 static struct {
839 /* 0 if we're not emitting a relaxable macro.
840 1 if we're emitting the first of the two relaxation alternatives.
841 2 if we're emitting the second alternative. */
842 int sequence;
843
844 /* The first relaxable fixup in the current frag. (In other words,
845 the first fixup that refers to relaxable code.) */
846 fixS *first_fixup;
847
848 /* sizes[0] says how many bytes of the first alternative are stored in
849 the current frag. Likewise sizes[1] for the second alternative. */
850 unsigned int sizes[2];
851
852 /* The symbol on which the choice of sequence depends. */
853 symbolS *symbol;
854 } mips_relax;
855 \f
856 /* Global variables used to decide whether a macro needs a warning. */
857 static struct {
858 /* True if the macro is in a branch delay slot. */
859 bfd_boolean delay_slot_p;
860
861 /* For relaxable macros, sizes[0] is the length of the first alternative
862 in bytes and sizes[1] is the length of the second alternative.
863 For non-relaxable macros, both elements give the length of the
864 macro in bytes. */
865 unsigned int sizes[2];
866
867 /* The first variant frag for this macro. */
868 fragS *first_frag;
869 } mips_macro_warning;
870 \f
871 /* Prototypes for static functions. */
872
873 #define internalError() \
874 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
875
876 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
877
878 static void append_insn
879 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
880 static void mips_no_prev_insn (int);
881 static void mips16_macro_build
882 (expressionS *, const char *, const char *, va_list);
883 static void load_register (int, expressionS *, int);
884 static void macro_start (void);
885 static void macro_end (void);
886 static void macro (struct mips_cl_insn * ip);
887 static void mips16_macro (struct mips_cl_insn * ip);
888 #ifdef LOSING_COMPILER
889 static void macro2 (struct mips_cl_insn * ip);
890 #endif
891 static void mips_ip (char *str, struct mips_cl_insn * ip);
892 static void mips16_ip (char *str, struct mips_cl_insn * ip);
893 static void mips16_immed
894 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
895 unsigned long *, bfd_boolean *, unsigned short *);
896 static size_t my_getSmallExpression
897 (expressionS *, bfd_reloc_code_real_type *, char *);
898 static void my_getExpression (expressionS *, char *);
899 static void s_align (int);
900 static void s_change_sec (int);
901 static void s_change_section (int);
902 static void s_cons (int);
903 static void s_float_cons (int);
904 static void s_mips_globl (int);
905 static void s_option (int);
906 static void s_mipsset (int);
907 static void s_abicalls (int);
908 static void s_cpload (int);
909 static void s_cpsetup (int);
910 static void s_cplocal (int);
911 static void s_cprestore (int);
912 static void s_cpreturn (int);
913 static void s_gpvalue (int);
914 static void s_gpword (int);
915 static void s_gpdword (int);
916 static void s_cpadd (int);
917 static void s_insn (int);
918 static void md_obj_begin (void);
919 static void md_obj_end (void);
920 static void s_mips_ent (int);
921 static void s_mips_end (int);
922 static void s_mips_frame (int);
923 static void s_mips_mask (int reg_type);
924 static void s_mips_stab (int);
925 static void s_mips_weakext (int);
926 static void s_mips_file (int);
927 static void s_mips_loc (int);
928 static bfd_boolean pic_need_relax (symbolS *, asection *);
929 static int relaxed_branch_length (fragS *, asection *, int);
930 static int validate_mips_insn (const struct mips_opcode *);
931
932 /* Table and functions used to map between CPU/ISA names, and
933 ISA levels, and CPU numbers. */
934
935 struct mips_cpu_info
936 {
937 const char *name; /* CPU or ISA name. */
938 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
939 int isa; /* ISA level. */
940 int cpu; /* CPU number (default CPU if ISA). */
941 };
942
943 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
944 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
945 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
946 \f
947 /* Pseudo-op table.
948
949 The following pseudo-ops from the Kane and Heinrich MIPS book
950 should be defined here, but are currently unsupported: .alias,
951 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
952
953 The following pseudo-ops from the Kane and Heinrich MIPS book are
954 specific to the type of debugging information being generated, and
955 should be defined by the object format: .aent, .begin, .bend,
956 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
957 .vreg.
958
959 The following pseudo-ops from the Kane and Heinrich MIPS book are
960 not MIPS CPU specific, but are also not specific to the object file
961 format. This file is probably the best place to define them, but
962 they are not currently supported: .asm0, .endr, .lab, .repeat,
963 .struct. */
964
965 static const pseudo_typeS mips_pseudo_table[] =
966 {
967 /* MIPS specific pseudo-ops. */
968 {"option", s_option, 0},
969 {"set", s_mipsset, 0},
970 {"rdata", s_change_sec, 'r'},
971 {"sdata", s_change_sec, 's'},
972 {"livereg", s_ignore, 0},
973 {"abicalls", s_abicalls, 0},
974 {"cpload", s_cpload, 0},
975 {"cpsetup", s_cpsetup, 0},
976 {"cplocal", s_cplocal, 0},
977 {"cprestore", s_cprestore, 0},
978 {"cpreturn", s_cpreturn, 0},
979 {"gpvalue", s_gpvalue, 0},
980 {"gpword", s_gpword, 0},
981 {"gpdword", s_gpdword, 0},
982 {"cpadd", s_cpadd, 0},
983 {"insn", s_insn, 0},
984
985 /* Relatively generic pseudo-ops that happen to be used on MIPS
986 chips. */
987 {"asciiz", stringer, 1},
988 {"bss", s_change_sec, 'b'},
989 {"err", s_err, 0},
990 {"half", s_cons, 1},
991 {"dword", s_cons, 3},
992 {"weakext", s_mips_weakext, 0},
993
994 /* These pseudo-ops are defined in read.c, but must be overridden
995 here for one reason or another. */
996 {"align", s_align, 0},
997 {"byte", s_cons, 0},
998 {"data", s_change_sec, 'd'},
999 {"double", s_float_cons, 'd'},
1000 {"float", s_float_cons, 'f'},
1001 {"globl", s_mips_globl, 0},
1002 {"global", s_mips_globl, 0},
1003 {"hword", s_cons, 1},
1004 {"int", s_cons, 2},
1005 {"long", s_cons, 2},
1006 {"octa", s_cons, 4},
1007 {"quad", s_cons, 3},
1008 {"section", s_change_section, 0},
1009 {"short", s_cons, 1},
1010 {"single", s_float_cons, 'f'},
1011 {"stabn", s_mips_stab, 'n'},
1012 {"text", s_change_sec, 't'},
1013 {"word", s_cons, 2},
1014
1015 { "extern", ecoff_directive_extern, 0},
1016
1017 { NULL, NULL, 0 },
1018 };
1019
1020 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1021 {
1022 /* These pseudo-ops should be defined by the object file format.
1023 However, a.out doesn't support them, so we have versions here. */
1024 {"aent", s_mips_ent, 1},
1025 {"bgnb", s_ignore, 0},
1026 {"end", s_mips_end, 0},
1027 {"endb", s_ignore, 0},
1028 {"ent", s_mips_ent, 0},
1029 {"file", s_mips_file, 0},
1030 {"fmask", s_mips_mask, 'F'},
1031 {"frame", s_mips_frame, 0},
1032 {"loc", s_mips_loc, 0},
1033 {"mask", s_mips_mask, 'R'},
1034 {"verstamp", s_ignore, 0},
1035 { NULL, NULL, 0 },
1036 };
1037
1038 extern void pop_insert (const pseudo_typeS *);
1039
1040 void
1041 mips_pop_insert (void)
1042 {
1043 pop_insert (mips_pseudo_table);
1044 if (! ECOFF_DEBUGGING)
1045 pop_insert (mips_nonecoff_pseudo_table);
1046 }
1047 \f
1048 /* Symbols labelling the current insn. */
1049
1050 struct insn_label_list
1051 {
1052 struct insn_label_list *next;
1053 symbolS *label;
1054 };
1055
1056 static struct insn_label_list *insn_labels;
1057 static struct insn_label_list *free_insn_labels;
1058
1059 static void mips_clear_insn_labels (void);
1060
1061 static inline void
1062 mips_clear_insn_labels (void)
1063 {
1064 register struct insn_label_list **pl;
1065
1066 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1067 ;
1068 *pl = insn_labels;
1069 insn_labels = NULL;
1070 }
1071 \f
1072 static char *expr_end;
1073
1074 /* Expressions which appear in instructions. These are set by
1075 mips_ip. */
1076
1077 static expressionS imm_expr;
1078 static expressionS imm2_expr;
1079 static expressionS offset_expr;
1080
1081 /* Relocs associated with imm_expr and offset_expr. */
1082
1083 static bfd_reloc_code_real_type imm_reloc[3]
1084 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1085 static bfd_reloc_code_real_type offset_reloc[3]
1086 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1087
1088 /* These are set by mips16_ip if an explicit extension is used. */
1089
1090 static bfd_boolean mips16_small, mips16_ext;
1091
1092 #ifdef OBJ_ELF
1093 /* The pdr segment for per procedure frame/regmask info. Not used for
1094 ECOFF debugging. */
1095
1096 static segT pdr_seg;
1097 #endif
1098
1099 /* The default target format to use. */
1100
1101 const char *
1102 mips_target_format (void)
1103 {
1104 switch (OUTPUT_FLAVOR)
1105 {
1106 case bfd_target_ecoff_flavour:
1107 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1108 case bfd_target_coff_flavour:
1109 return "pe-mips";
1110 case bfd_target_elf_flavour:
1111 #ifdef TE_TMIPS
1112 /* This is traditional mips. */
1113 return (target_big_endian
1114 ? (HAVE_64BIT_OBJECTS
1115 ? "elf64-tradbigmips"
1116 : (HAVE_NEWABI
1117 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1118 : (HAVE_64BIT_OBJECTS
1119 ? "elf64-tradlittlemips"
1120 : (HAVE_NEWABI
1121 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1122 #else
1123 return (target_big_endian
1124 ? (HAVE_64BIT_OBJECTS
1125 ? "elf64-bigmips"
1126 : (HAVE_NEWABI
1127 ? "elf32-nbigmips" : "elf32-bigmips"))
1128 : (HAVE_64BIT_OBJECTS
1129 ? "elf64-littlemips"
1130 : (HAVE_NEWABI
1131 ? "elf32-nlittlemips" : "elf32-littlemips")));
1132 #endif
1133 default:
1134 abort ();
1135 return NULL;
1136 }
1137 }
1138
1139 /* This function is called once, at assembler startup time. It should
1140 set up all the tables, etc. that the MD part of the assembler will need. */
1141
1142 void
1143 md_begin (void)
1144 {
1145 register const char *retval = NULL;
1146 int i = 0;
1147 int broken = 0;
1148
1149 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1150 as_warn (_("Could not set architecture and machine"));
1151
1152 op_hash = hash_new ();
1153
1154 for (i = 0; i < NUMOPCODES;)
1155 {
1156 const char *name = mips_opcodes[i].name;
1157
1158 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1159 if (retval != NULL)
1160 {
1161 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1162 mips_opcodes[i].name, retval);
1163 /* Probably a memory allocation problem? Give up now. */
1164 as_fatal (_("Broken assembler. No assembly attempted."));
1165 }
1166 do
1167 {
1168 if (mips_opcodes[i].pinfo != INSN_MACRO)
1169 {
1170 if (!validate_mips_insn (&mips_opcodes[i]))
1171 broken = 1;
1172 }
1173 ++i;
1174 }
1175 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1176 }
1177
1178 mips16_op_hash = hash_new ();
1179
1180 i = 0;
1181 while (i < bfd_mips16_num_opcodes)
1182 {
1183 const char *name = mips16_opcodes[i].name;
1184
1185 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1186 if (retval != NULL)
1187 as_fatal (_("internal: can't hash `%s': %s"),
1188 mips16_opcodes[i].name, retval);
1189 do
1190 {
1191 if (mips16_opcodes[i].pinfo != INSN_MACRO
1192 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1193 != mips16_opcodes[i].match))
1194 {
1195 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1196 mips16_opcodes[i].name, mips16_opcodes[i].args);
1197 broken = 1;
1198 }
1199 ++i;
1200 }
1201 while (i < bfd_mips16_num_opcodes
1202 && strcmp (mips16_opcodes[i].name, name) == 0);
1203 }
1204
1205 if (broken)
1206 as_fatal (_("Broken assembler. No assembly attempted."));
1207
1208 /* We add all the general register names to the symbol table. This
1209 helps us detect invalid uses of them. */
1210 for (i = 0; i < 32; i++)
1211 {
1212 char buf[5];
1213
1214 sprintf (buf, "$%d", i);
1215 symbol_table_insert (symbol_new (buf, reg_section, i,
1216 &zero_address_frag));
1217 }
1218 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1219 &zero_address_frag));
1220 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1221 &zero_address_frag));
1222 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1223 &zero_address_frag));
1224 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1225 &zero_address_frag));
1226 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1227 &zero_address_frag));
1228 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1229 &zero_address_frag));
1230 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1231 &zero_address_frag));
1232 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1233 &zero_address_frag));
1234 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1235 &zero_address_frag));
1236
1237 /* If we don't add these register names to the symbol table, they
1238 may end up being added as regular symbols by operand(), and then
1239 make it to the object file as undefined in case they're not
1240 regarded as local symbols. They're local in o32, since `$' is a
1241 local symbol prefix, but not in n32 or n64. */
1242 for (i = 0; i < 8; i++)
1243 {
1244 char buf[6];
1245
1246 sprintf (buf, "$fcc%i", i);
1247 symbol_table_insert (symbol_new (buf, reg_section, -1,
1248 &zero_address_frag));
1249 }
1250
1251 mips_no_prev_insn (FALSE);
1252
1253 mips_gprmask = 0;
1254 mips_cprmask[0] = 0;
1255 mips_cprmask[1] = 0;
1256 mips_cprmask[2] = 0;
1257 mips_cprmask[3] = 0;
1258
1259 /* set the default alignment for the text section (2**2) */
1260 record_alignment (text_section, 2);
1261
1262 bfd_set_gp_size (stdoutput, g_switch_value);
1263
1264 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1265 {
1266 /* On a native system, sections must be aligned to 16 byte
1267 boundaries. When configured for an embedded ELF target, we
1268 don't bother. */
1269 if (strcmp (TARGET_OS, "elf") != 0)
1270 {
1271 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1272 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1273 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1274 }
1275
1276 /* Create a .reginfo section for register masks and a .mdebug
1277 section for debugging information. */
1278 {
1279 segT seg;
1280 subsegT subseg;
1281 flagword flags;
1282 segT sec;
1283
1284 seg = now_seg;
1285 subseg = now_subseg;
1286
1287 /* The ABI says this section should be loaded so that the
1288 running program can access it. However, we don't load it
1289 if we are configured for an embedded target */
1290 flags = SEC_READONLY | SEC_DATA;
1291 if (strcmp (TARGET_OS, "elf") != 0)
1292 flags |= SEC_ALLOC | SEC_LOAD;
1293
1294 if (mips_abi != N64_ABI)
1295 {
1296 sec = subseg_new (".reginfo", (subsegT) 0);
1297
1298 bfd_set_section_flags (stdoutput, sec, flags);
1299 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1300
1301 #ifdef OBJ_ELF
1302 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1303 #endif
1304 }
1305 else
1306 {
1307 /* The 64-bit ABI uses a .MIPS.options section rather than
1308 .reginfo section. */
1309 sec = subseg_new (".MIPS.options", (subsegT) 0);
1310 bfd_set_section_flags (stdoutput, sec, flags);
1311 bfd_set_section_alignment (stdoutput, sec, 3);
1312
1313 #ifdef OBJ_ELF
1314 /* Set up the option header. */
1315 {
1316 Elf_Internal_Options opthdr;
1317 char *f;
1318
1319 opthdr.kind = ODK_REGINFO;
1320 opthdr.size = (sizeof (Elf_External_Options)
1321 + sizeof (Elf64_External_RegInfo));
1322 opthdr.section = 0;
1323 opthdr.info = 0;
1324 f = frag_more (sizeof (Elf_External_Options));
1325 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1326 (Elf_External_Options *) f);
1327
1328 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1329 }
1330 #endif
1331 }
1332
1333 if (ECOFF_DEBUGGING)
1334 {
1335 sec = subseg_new (".mdebug", (subsegT) 0);
1336 (void) bfd_set_section_flags (stdoutput, sec,
1337 SEC_HAS_CONTENTS | SEC_READONLY);
1338 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1339 }
1340 #ifdef OBJ_ELF
1341 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1342 {
1343 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1344 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1345 SEC_READONLY | SEC_RELOC
1346 | SEC_DEBUGGING);
1347 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1348 }
1349 #endif
1350
1351 subseg_set (seg, subseg);
1352 }
1353 }
1354
1355 if (! ECOFF_DEBUGGING)
1356 md_obj_begin ();
1357 }
1358
1359 void
1360 md_mips_end (void)
1361 {
1362 if (! ECOFF_DEBUGGING)
1363 md_obj_end ();
1364 }
1365
1366 void
1367 md_assemble (char *str)
1368 {
1369 struct mips_cl_insn insn;
1370 bfd_reloc_code_real_type unused_reloc[3]
1371 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1372
1373 imm_expr.X_op = O_absent;
1374 imm2_expr.X_op = O_absent;
1375 offset_expr.X_op = O_absent;
1376 imm_reloc[0] = BFD_RELOC_UNUSED;
1377 imm_reloc[1] = BFD_RELOC_UNUSED;
1378 imm_reloc[2] = BFD_RELOC_UNUSED;
1379 offset_reloc[0] = BFD_RELOC_UNUSED;
1380 offset_reloc[1] = BFD_RELOC_UNUSED;
1381 offset_reloc[2] = BFD_RELOC_UNUSED;
1382
1383 if (mips_opts.mips16)
1384 mips16_ip (str, &insn);
1385 else
1386 {
1387 mips_ip (str, &insn);
1388 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1389 str, insn.insn_opcode));
1390 }
1391
1392 if (insn_error)
1393 {
1394 as_bad ("%s `%s'", insn_error, str);
1395 return;
1396 }
1397
1398 if (insn.insn_mo->pinfo == INSN_MACRO)
1399 {
1400 macro_start ();
1401 if (mips_opts.mips16)
1402 mips16_macro (&insn);
1403 else
1404 macro (&insn);
1405 macro_end ();
1406 }
1407 else
1408 {
1409 if (imm_expr.X_op != O_absent)
1410 append_insn (&insn, &imm_expr, imm_reloc);
1411 else if (offset_expr.X_op != O_absent)
1412 append_insn (&insn, &offset_expr, offset_reloc);
1413 else
1414 append_insn (&insn, NULL, unused_reloc);
1415 }
1416 }
1417
1418 /* Return true if the given relocation might need a matching %lo().
1419 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1420 applied to local symbols. */
1421
1422 static inline bfd_boolean
1423 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1424 {
1425 return (HAVE_IN_PLACE_ADDENDS
1426 && (reloc == BFD_RELOC_HI16_S
1427 || reloc == BFD_RELOC_MIPS_GOT16
1428 || reloc == BFD_RELOC_MIPS16_HI16_S));
1429 }
1430
1431 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1432 relocation. */
1433
1434 static inline bfd_boolean
1435 fixup_has_matching_lo_p (fixS *fixp)
1436 {
1437 return (fixp->fx_next != NULL
1438 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1439 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1440 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1441 && fixp->fx_offset == fixp->fx_next->fx_offset);
1442 }
1443
1444 /* See whether instruction IP reads register REG. CLASS is the type
1445 of register. */
1446
1447 static int
1448 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1449 enum mips_regclass class)
1450 {
1451 if (class == MIPS16_REG)
1452 {
1453 assert (mips_opts.mips16);
1454 reg = mips16_to_32_reg_map[reg];
1455 class = MIPS_GR_REG;
1456 }
1457
1458 /* Don't report on general register ZERO, since it never changes. */
1459 if (class == MIPS_GR_REG && reg == ZERO)
1460 return 0;
1461
1462 if (class == MIPS_FP_REG)
1463 {
1464 assert (! mips_opts.mips16);
1465 /* If we are called with either $f0 or $f1, we must check $f0.
1466 This is not optimal, because it will introduce an unnecessary
1467 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1468 need to distinguish reading both $f0 and $f1 or just one of
1469 them. Note that we don't have to check the other way,
1470 because there is no instruction that sets both $f0 and $f1
1471 and requires a delay. */
1472 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1473 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1474 == (reg &~ (unsigned) 1)))
1475 return 1;
1476 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1477 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1478 == (reg &~ (unsigned) 1)))
1479 return 1;
1480 }
1481 else if (! mips_opts.mips16)
1482 {
1483 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1484 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1485 return 1;
1486 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1487 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1488 return 1;
1489 }
1490 else
1491 {
1492 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1493 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1494 & MIPS16OP_MASK_RX)]
1495 == reg))
1496 return 1;
1497 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1498 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1499 & MIPS16OP_MASK_RY)]
1500 == reg))
1501 return 1;
1502 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1503 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1504 & MIPS16OP_MASK_MOVE32Z)]
1505 == reg))
1506 return 1;
1507 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1508 return 1;
1509 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1510 return 1;
1511 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1512 return 1;
1513 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1514 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1515 & MIPS16OP_MASK_REGR32) == reg)
1516 return 1;
1517 }
1518
1519 return 0;
1520 }
1521
1522 /* This function returns true if modifying a register requires a
1523 delay. */
1524
1525 static int
1526 reg_needs_delay (unsigned int reg)
1527 {
1528 unsigned long prev_pinfo;
1529
1530 prev_pinfo = history[0].insn_mo->pinfo;
1531 if (! mips_opts.noreorder
1532 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1533 && ! gpr_interlocks)
1534 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1535 && ! cop_interlocks)))
1536 {
1537 /* A load from a coprocessor or from memory. All load delays
1538 delay the use of general register rt for one instruction. */
1539 /* Itbl support may require additional care here. */
1540 know (prev_pinfo & INSN_WRITE_GPR_T);
1541 if (reg == ((history[0].insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1542 return 1;
1543 }
1544
1545 return 0;
1546 }
1547
1548 /* Mark instruction labels in mips16 mode. This permits the linker to
1549 handle them specially, such as generating jalx instructions when
1550 needed. We also make them odd for the duration of the assembly, in
1551 order to generate the right sort of code. We will make them even
1552 in the adjust_symtab routine, while leaving them marked. This is
1553 convenient for the debugger and the disassembler. The linker knows
1554 to make them odd again. */
1555
1556 static void
1557 mips16_mark_labels (void)
1558 {
1559 if (mips_opts.mips16)
1560 {
1561 struct insn_label_list *l;
1562 valueT val;
1563
1564 for (l = insn_labels; l != NULL; l = l->next)
1565 {
1566 #ifdef OBJ_ELF
1567 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1568 S_SET_OTHER (l->label, STO_MIPS16);
1569 #endif
1570 val = S_GET_VALUE (l->label);
1571 if ((val & 1) == 0)
1572 S_SET_VALUE (l->label, val + 1);
1573 }
1574 }
1575 }
1576
1577 /* End the current frag. Make it a variant frag and record the
1578 relaxation info. */
1579
1580 static void
1581 relax_close_frag (void)
1582 {
1583 mips_macro_warning.first_frag = frag_now;
1584 frag_var (rs_machine_dependent, 0, 0,
1585 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1586 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1587
1588 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1589 mips_relax.first_fixup = 0;
1590 }
1591
1592 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1593 See the comment above RELAX_ENCODE for more details. */
1594
1595 static void
1596 relax_start (symbolS *symbol)
1597 {
1598 assert (mips_relax.sequence == 0);
1599 mips_relax.sequence = 1;
1600 mips_relax.symbol = symbol;
1601 }
1602
1603 /* Start generating the second version of a relaxable sequence.
1604 See the comment above RELAX_ENCODE for more details. */
1605
1606 static void
1607 relax_switch (void)
1608 {
1609 assert (mips_relax.sequence == 1);
1610 mips_relax.sequence = 2;
1611 }
1612
1613 /* End the current relaxable sequence. */
1614
1615 static void
1616 relax_end (void)
1617 {
1618 assert (mips_relax.sequence == 2);
1619 relax_close_frag ();
1620 mips_relax.sequence = 0;
1621 }
1622
1623 /* Output an instruction. IP is the instruction information.
1624 ADDRESS_EXPR is an operand of the instruction to be used with
1625 RELOC_TYPE. */
1626
1627 static void
1628 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
1629 bfd_reloc_code_real_type *reloc_type)
1630 {
1631 register unsigned long prev_pinfo, pinfo;
1632 char *f;
1633 fixS *fixp[3];
1634 int nops = 0;
1635 relax_stateT prev_insn_frag_type = 0;
1636 bfd_boolean relaxed_branch = FALSE;
1637 bfd_boolean force_new_frag = FALSE;
1638
1639 /* Mark instruction labels in mips16 mode. */
1640 mips16_mark_labels ();
1641
1642 prev_pinfo = history[0].insn_mo->pinfo;
1643 pinfo = ip->insn_mo->pinfo;
1644
1645 if (mips_relax.sequence != 2
1646 && (!mips_opts.noreorder || prev_nop_frag != NULL))
1647 {
1648 int prev_prev_nop;
1649
1650 /* If the previous insn required any delay slots, see if we need
1651 to insert a NOP or two. There are eight kinds of possible
1652 hazards, of which an instruction can have at most one type.
1653 (1) a load from memory delay
1654 (2) a load from a coprocessor delay
1655 (3) an unconditional branch delay
1656 (4) a conditional branch delay
1657 (5) a move to coprocessor register delay
1658 (6) a load coprocessor register from memory delay
1659 (7) a coprocessor condition code delay
1660 (8) a HI/LO special register delay
1661
1662 There are a lot of optimizations we could do that we don't.
1663 In particular, we do not, in general, reorder instructions.
1664 If you use gcc with optimization, it will reorder
1665 instructions and generally do much more optimization then we
1666 do here; repeating all that work in the assembler would only
1667 benefit hand written assembly code, and does not seem worth
1668 it. */
1669
1670 /* This is how a NOP is emitted. */
1671 #define emit_nop() \
1672 (mips_opts.mips16 \
1673 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1674 : md_number_to_chars (frag_more (4), 0, 4))
1675
1676 /* The previous insn might require a delay slot, depending upon
1677 the contents of the current insn. */
1678 if (! mips_opts.mips16
1679 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1680 && ! gpr_interlocks)
1681 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1682 && ! cop_interlocks)))
1683 {
1684 /* A load from a coprocessor or from memory. All load
1685 delays delay the use of general register rt for one
1686 instruction. */
1687 /* Itbl support may require additional care here. */
1688 know (prev_pinfo & INSN_WRITE_GPR_T);
1689 if (mips_optimize == 0
1690 || insn_uses_reg (ip,
1691 ((history[0].insn_opcode >> OP_SH_RT)
1692 & OP_MASK_RT),
1693 MIPS_GR_REG))
1694 ++nops;
1695 }
1696 else if (! mips_opts.mips16
1697 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1698 && ! cop_interlocks)
1699 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1700 && ! cop_mem_interlocks)))
1701 {
1702 /* A generic coprocessor delay. The previous instruction
1703 modified a coprocessor general or control register. If
1704 it modified a control register, we need to avoid any
1705 coprocessor instruction (this is probably not always
1706 required, but it sometimes is). If it modified a general
1707 register, we avoid using that register.
1708
1709 This case is not handled very well. There is no special
1710 knowledge of CP0 handling, and the coprocessors other
1711 than the floating point unit are not distinguished at
1712 all. */
1713 /* Itbl support may require additional care here. FIXME!
1714 Need to modify this to include knowledge about
1715 user specified delays! */
1716 if (prev_pinfo & INSN_WRITE_FPR_T)
1717 {
1718 if (mips_optimize == 0
1719 || insn_uses_reg (ip,
1720 ((history[0].insn_opcode >> OP_SH_FT)
1721 & OP_MASK_FT),
1722 MIPS_FP_REG))
1723 ++nops;
1724 }
1725 else if (prev_pinfo & INSN_WRITE_FPR_S)
1726 {
1727 if (mips_optimize == 0
1728 || insn_uses_reg (ip,
1729 ((history[0].insn_opcode >> OP_SH_FS)
1730 & OP_MASK_FS),
1731 MIPS_FP_REG))
1732 ++nops;
1733 }
1734 else
1735 {
1736 /* We don't know exactly what the previous instruction
1737 does. If the current instruction uses a coprocessor
1738 register, we must insert a NOP. If previous
1739 instruction may set the condition codes, and the
1740 current instruction uses them, we must insert two
1741 NOPS. */
1742 /* Itbl support may require additional care here. */
1743 if (mips_optimize == 0
1744 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1745 && (pinfo & INSN_READ_COND_CODE)))
1746 nops += 2;
1747 else if (pinfo & INSN_COP)
1748 ++nops;
1749 }
1750 }
1751 else if (! mips_opts.mips16
1752 && (prev_pinfo & INSN_WRITE_COND_CODE)
1753 && ! cop_interlocks)
1754 {
1755 /* The previous instruction sets the coprocessor condition
1756 codes, but does not require a general coprocessor delay
1757 (this means it is a floating point comparison
1758 instruction). If this instruction uses the condition
1759 codes, we need to insert a single NOP. */
1760 /* Itbl support may require additional care here. */
1761 if (mips_optimize == 0
1762 || (pinfo & INSN_READ_COND_CODE))
1763 ++nops;
1764 }
1765
1766 /* If we're fixing up mfhi/mflo for the r7000 and the
1767 previous insn was an mfhi/mflo and the current insn
1768 reads the register that the mfhi/mflo wrote to, then
1769 insert two nops. */
1770
1771 else if (mips_7000_hilo_fix
1772 && MF_HILO_INSN (prev_pinfo)
1773 && insn_uses_reg (ip, ((history[0].insn_opcode >> OP_SH_RD)
1774 & OP_MASK_RD),
1775 MIPS_GR_REG))
1776 {
1777 nops += 2;
1778 }
1779
1780 /* If we're fixing up mfhi/mflo for the r7000 and the
1781 2nd previous insn was an mfhi/mflo and the current insn
1782 reads the register that the mfhi/mflo wrote to, then
1783 insert one nop. */
1784
1785 else if (mips_7000_hilo_fix
1786 && MF_HILO_INSN (history[1].insn_opcode)
1787 && insn_uses_reg (ip, ((history[1].insn_opcode >> OP_SH_RD)
1788 & OP_MASK_RD),
1789 MIPS_GR_REG))
1790
1791 {
1792 ++nops;
1793 }
1794
1795 else if (prev_pinfo & INSN_READ_LO)
1796 {
1797 /* The previous instruction reads the LO register; if the
1798 current instruction writes to the LO register, we must
1799 insert two NOPS. Some newer processors have interlocks.
1800 Also the tx39's multiply instructions can be executed
1801 immediately after a read from HI/LO (without the delay),
1802 though the tx39's divide insns still do require the
1803 delay. */
1804 if (! (hilo_interlocks
1805 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1806 && (mips_optimize == 0
1807 || (pinfo & INSN_WRITE_LO)))
1808 nops += 2;
1809 /* Most mips16 branch insns don't have a delay slot.
1810 If a read from LO is immediately followed by a branch
1811 to a write to LO we have a read followed by a write
1812 less than 2 insns away. We assume the target of
1813 a branch might be a write to LO, and insert a nop
1814 between a read and an immediately following branch. */
1815 else if (mips_opts.mips16
1816 && (mips_optimize == 0
1817 || (pinfo & MIPS16_INSN_BRANCH)))
1818 ++nops;
1819 }
1820 else if (history[0].insn_mo->pinfo & INSN_READ_HI)
1821 {
1822 /* The previous instruction reads the HI register; if the
1823 current instruction writes to the HI register, we must
1824 insert a NOP. Some newer processors have interlocks.
1825 Also the note tx39's multiply above. */
1826 if (! (hilo_interlocks
1827 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1828 && (mips_optimize == 0
1829 || (pinfo & INSN_WRITE_HI)))
1830 nops += 2;
1831 /* Most mips16 branch insns don't have a delay slot.
1832 If a read from HI is immediately followed by a branch
1833 to a write to HI we have a read followed by a write
1834 less than 2 insns away. We assume the target of
1835 a branch might be a write to HI, and insert a nop
1836 between a read and an immediately following branch. */
1837 else if (mips_opts.mips16
1838 && (mips_optimize == 0
1839 || (pinfo & MIPS16_INSN_BRANCH)))
1840 ++nops;
1841 }
1842
1843 /* If the previous instruction was in a noreorder section, then
1844 we don't want to insert the nop after all. */
1845 /* Itbl support may require additional care here. */
1846 if (history[0].noreorder_p)
1847 nops = 0;
1848
1849 /* There are two cases which require two intervening
1850 instructions: 1) setting the condition codes using a move to
1851 coprocessor instruction which requires a general coprocessor
1852 delay and then reading the condition codes 2) reading the HI
1853 or LO register and then writing to it (except on processors
1854 which have interlocks). If we are not already emitting a NOP
1855 instruction, we must check for these cases compared to the
1856 instruction previous to the previous instruction. */
1857 if ((! mips_opts.mips16
1858 && (history[1].insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1859 && (history[1].insn_mo->pinfo & INSN_WRITE_COND_CODE)
1860 && (pinfo & INSN_READ_COND_CODE)
1861 && ! cop_interlocks)
1862 || ((history[1].insn_mo->pinfo & INSN_READ_LO)
1863 && (pinfo & INSN_WRITE_LO)
1864 && ! (hilo_interlocks
1865 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
1866 || ((history[1].insn_mo->pinfo & INSN_READ_HI)
1867 && (pinfo & INSN_WRITE_HI)
1868 && ! (hilo_interlocks
1869 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
1870 prev_prev_nop = 1;
1871 else
1872 prev_prev_nop = 0;
1873
1874 if (history[1].noreorder_p)
1875 prev_prev_nop = 0;
1876
1877 if (prev_prev_nop && nops == 0)
1878 ++nops;
1879
1880 if (mips_fix_vr4120 && history[0].insn_mo->name)
1881 {
1882 /* We're out of bits in pinfo, so we must resort to string
1883 ops here. Shortcuts are selected based on opcodes being
1884 limited to the VR4120 instruction set. */
1885 int min_nops = 0;
1886 const char *pn = history[0].insn_mo->name;
1887 const char *tn = ip->insn_mo->name;
1888 if (strncmp (pn, "macc", 4) == 0
1889 || strncmp (pn, "dmacc", 5) == 0)
1890 {
1891 /* Errata 21 - [D]DIV[U] after [D]MACC */
1892 if (strstr (tn, "div"))
1893 min_nops = 1;
1894
1895 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1896 instruction is executed immediately after a MACC or
1897 DMACC instruction, the result of [either instruction]
1898 is incorrect." */
1899 if (strncmp (tn, "mult", 4) == 0
1900 || strncmp (tn, "dmult", 5) == 0)
1901 min_nops = 1;
1902
1903 /* Errata 23 - Continuous DMULT[U]/DMACC instructions.
1904 Applies on top of VR4181A MD(1) errata. */
1905 if (pn[0] == 'd' && strncmp (tn, "dmacc", 5) == 0)
1906 min_nops = 1;
1907
1908 /* Errata 24 - MT{LO,HI} after [D]MACC */
1909 if (strcmp (tn, "mtlo") == 0
1910 || strcmp (tn, "mthi") == 0)
1911 min_nops = 1;
1912 }
1913 else if (strncmp (pn, "dmult", 5) == 0
1914 && (strncmp (tn, "dmult", 5) == 0
1915 || strncmp (tn, "dmacc", 5) == 0))
1916 {
1917 /* Here is the rest of errata 23. */
1918 min_nops = 1;
1919 }
1920 else if ((strncmp (pn, "dmult", 5) == 0 || strstr (pn, "div"))
1921 && (strncmp (tn, "macc", 4) == 0
1922 || strncmp (tn, "dmacc", 5) == 0))
1923 {
1924 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1925 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1926 DDIV or DDIVU instruction, the result of the MACC or
1927 DMACC instruction is incorrect.". This partly overlaps
1928 the workaround for errata 23. */
1929 min_nops = 1;
1930 }
1931 if (nops < min_nops)
1932 nops = min_nops;
1933 }
1934
1935 /* If we are being given a nop instruction, don't bother with
1936 one of the nops we would otherwise output. This will only
1937 happen when a nop instruction is used with mips_optimize set
1938 to 0. */
1939 if (nops > 0
1940 && ! mips_opts.noreorder
1941 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1942 --nops;
1943
1944 /* Now emit the right number of NOP instructions. */
1945 if (nops > 0 && ! mips_opts.noreorder)
1946 {
1947 fragS *old_frag;
1948 unsigned long old_frag_offset;
1949 int i;
1950 struct insn_label_list *l;
1951
1952 old_frag = frag_now;
1953 old_frag_offset = frag_now_fix ();
1954
1955 for (i = 0; i < nops; i++)
1956 emit_nop ();
1957
1958 if (listing)
1959 {
1960 listing_prev_line ();
1961 /* We may be at the start of a variant frag. In case we
1962 are, make sure there is enough space for the frag
1963 after the frags created by listing_prev_line. The
1964 argument to frag_grow here must be at least as large
1965 as the argument to all other calls to frag_grow in
1966 this file. We don't have to worry about being in the
1967 middle of a variant frag, because the variants insert
1968 all needed nop instructions themselves. */
1969 frag_grow (40);
1970 }
1971
1972 for (l = insn_labels; l != NULL; l = l->next)
1973 {
1974 valueT val;
1975
1976 assert (S_GET_SEGMENT (l->label) == now_seg);
1977 symbol_set_frag (l->label, frag_now);
1978 val = (valueT) frag_now_fix ();
1979 /* mips16 text labels are stored as odd. */
1980 if (mips_opts.mips16)
1981 ++val;
1982 S_SET_VALUE (l->label, val);
1983 }
1984
1985 #ifndef NO_ECOFF_DEBUGGING
1986 if (ECOFF_DEBUGGING)
1987 ecoff_fix_loc (old_frag, old_frag_offset);
1988 #endif
1989 }
1990 else if (prev_nop_frag != NULL)
1991 {
1992 /* We have a frag holding nops we may be able to remove. If
1993 we don't need any nops, we can decrease the size of
1994 prev_nop_frag by the size of one instruction. If we do
1995 need some nops, we count them in prev_nops_required. */
1996 if (prev_nop_frag_since == 0)
1997 {
1998 if (nops == 0)
1999 {
2000 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2001 --prev_nop_frag_holds;
2002 }
2003 else
2004 prev_nop_frag_required += nops;
2005 }
2006 else
2007 {
2008 if (prev_prev_nop == 0)
2009 {
2010 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2011 --prev_nop_frag_holds;
2012 }
2013 else
2014 ++prev_nop_frag_required;
2015 }
2016
2017 if (prev_nop_frag_holds <= prev_nop_frag_required)
2018 prev_nop_frag = NULL;
2019
2020 ++prev_nop_frag_since;
2021
2022 /* Sanity check: by the time we reach the second instruction
2023 after prev_nop_frag, we should have used up all the nops
2024 one way or another. */
2025 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2026 }
2027 }
2028
2029 #ifdef OBJ_ELF
2030 /* The value passed to dwarf2_emit_insn is the distance between
2031 the beginning of the current instruction and the address that
2032 should be recorded in the debug tables. For MIPS16 debug info
2033 we want to use ISA-encoded addresses, so we pass -1 for an
2034 address higher by one than the current. */
2035 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2036 #endif
2037
2038 /* Record the frag type before frag_var. */
2039 if (history[0].frag)
2040 prev_insn_frag_type = history[0].frag->fr_type;
2041
2042 if (address_expr
2043 && *reloc_type == BFD_RELOC_16_PCREL_S2
2044 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2045 || pinfo & INSN_COND_BRANCH_LIKELY)
2046 && mips_relax_branch
2047 /* Don't try branch relaxation within .set nomacro, or within
2048 .set noat if we use $at for PIC computations. If it turns
2049 out that the branch was out-of-range, we'll get an error. */
2050 && !mips_opts.warn_about_macros
2051 && !(mips_opts.noat && mips_pic != NO_PIC)
2052 && !mips_opts.mips16)
2053 {
2054 relaxed_branch = TRUE;
2055 f = frag_var (rs_machine_dependent,
2056 relaxed_branch_length
2057 (NULL, NULL,
2058 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2059 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2060 RELAX_BRANCH_ENCODE
2061 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2062 pinfo & INSN_COND_BRANCH_LIKELY,
2063 pinfo & INSN_WRITE_GPR_31,
2064 0),
2065 address_expr->X_add_symbol,
2066 address_expr->X_add_number,
2067 0);
2068 *reloc_type = BFD_RELOC_UNUSED;
2069 }
2070 else if (*reloc_type > BFD_RELOC_UNUSED)
2071 {
2072 /* We need to set up a variant frag. */
2073 assert (mips_opts.mips16 && address_expr != NULL);
2074 f = frag_var (rs_machine_dependent, 4, 0,
2075 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2076 mips16_small, mips16_ext,
2077 (prev_pinfo
2078 & INSN_UNCOND_BRANCH_DELAY),
2079 history[0].mips16_absolute_jump_p),
2080 make_expr_symbol (address_expr), 0, NULL);
2081 }
2082 else if (mips_opts.mips16
2083 && ! ip->use_extend
2084 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2085 {
2086 /* Make sure there is enough room to swap this instruction with
2087 a following jump instruction. */
2088 frag_grow (6);
2089 f = frag_more (2);
2090 }
2091 else
2092 {
2093 if (mips_opts.mips16
2094 && mips_opts.noreorder
2095 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2096 as_warn (_("extended instruction in delay slot"));
2097
2098 if (mips_relax.sequence)
2099 {
2100 /* If we've reached the end of this frag, turn it into a variant
2101 frag and record the information for the instructions we've
2102 written so far. */
2103 if (frag_room () < 4)
2104 relax_close_frag ();
2105 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2106 }
2107
2108 if (mips_relax.sequence != 2)
2109 mips_macro_warning.sizes[0] += 4;
2110 if (mips_relax.sequence != 1)
2111 mips_macro_warning.sizes[1] += 4;
2112
2113 f = frag_more (4);
2114 }
2115
2116 fixp[0] = fixp[1] = fixp[2] = NULL;
2117 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2118 {
2119 if (address_expr->X_op == O_constant)
2120 {
2121 unsigned int tmp;
2122
2123 switch (*reloc_type)
2124 {
2125 case BFD_RELOC_32:
2126 ip->insn_opcode |= address_expr->X_add_number;
2127 break;
2128
2129 case BFD_RELOC_MIPS_HIGHEST:
2130 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2131 ip->insn_opcode |= tmp & 0xffff;
2132 break;
2133
2134 case BFD_RELOC_MIPS_HIGHER:
2135 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2136 ip->insn_opcode |= tmp & 0xffff;
2137 break;
2138
2139 case BFD_RELOC_HI16_S:
2140 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2141 ip->insn_opcode |= tmp & 0xffff;
2142 break;
2143
2144 case BFD_RELOC_HI16:
2145 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2146 break;
2147
2148 case BFD_RELOC_UNUSED:
2149 case BFD_RELOC_LO16:
2150 case BFD_RELOC_MIPS_GOT_DISP:
2151 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2152 break;
2153
2154 case BFD_RELOC_MIPS_JMP:
2155 if ((address_expr->X_add_number & 3) != 0)
2156 as_bad (_("jump to misaligned address (0x%lx)"),
2157 (unsigned long) address_expr->X_add_number);
2158 if (address_expr->X_add_number & ~0xfffffff)
2159 as_bad (_("jump address range overflow (0x%lx)"),
2160 (unsigned long) address_expr->X_add_number);
2161 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2162 break;
2163
2164 case BFD_RELOC_MIPS16_JMP:
2165 if ((address_expr->X_add_number & 3) != 0)
2166 as_bad (_("jump to misaligned address (0x%lx)"),
2167 (unsigned long) address_expr->X_add_number);
2168 if (address_expr->X_add_number & ~0xfffffff)
2169 as_bad (_("jump address range overflow (0x%lx)"),
2170 (unsigned long) address_expr->X_add_number);
2171 ip->insn_opcode |=
2172 (((address_expr->X_add_number & 0x7c0000) << 3)
2173 | ((address_expr->X_add_number & 0xf800000) >> 7)
2174 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2175 break;
2176
2177 case BFD_RELOC_16_PCREL_S2:
2178 goto need_reloc;
2179
2180 default:
2181 internalError ();
2182 }
2183 }
2184 else if (*reloc_type < BFD_RELOC_UNUSED)
2185 need_reloc:
2186 {
2187 reloc_howto_type *howto;
2188 int i;
2189
2190 /* In a compound relocation, it is the final (outermost)
2191 operator that determines the relocated field. */
2192 for (i = 1; i < 3; i++)
2193 if (reloc_type[i] == BFD_RELOC_UNUSED)
2194 break;
2195
2196 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2197 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2198 bfd_get_reloc_size(howto),
2199 address_expr,
2200 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2201 reloc_type[0]);
2202
2203 /* These relocations can have an addend that won't fit in
2204 4 octets for 64bit assembly. */
2205 if (HAVE_64BIT_GPRS
2206 && ! howto->partial_inplace
2207 && (reloc_type[0] == BFD_RELOC_16
2208 || reloc_type[0] == BFD_RELOC_32
2209 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2210 || reloc_type[0] == BFD_RELOC_HI16_S
2211 || reloc_type[0] == BFD_RELOC_LO16
2212 || reloc_type[0] == BFD_RELOC_GPREL16
2213 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2214 || reloc_type[0] == BFD_RELOC_GPREL32
2215 || reloc_type[0] == BFD_RELOC_64
2216 || reloc_type[0] == BFD_RELOC_CTOR
2217 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2218 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2219 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2220 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2221 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2222 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2223 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2224 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2225 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2226 fixp[0]->fx_no_overflow = 1;
2227
2228 if (mips_relax.sequence)
2229 {
2230 if (mips_relax.first_fixup == 0)
2231 mips_relax.first_fixup = fixp[0];
2232 }
2233 else if (reloc_needs_lo_p (*reloc_type))
2234 {
2235 struct mips_hi_fixup *hi_fixup;
2236
2237 /* Reuse the last entry if it already has a matching %lo. */
2238 hi_fixup = mips_hi_fixup_list;
2239 if (hi_fixup == 0
2240 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2241 {
2242 hi_fixup = ((struct mips_hi_fixup *)
2243 xmalloc (sizeof (struct mips_hi_fixup)));
2244 hi_fixup->next = mips_hi_fixup_list;
2245 mips_hi_fixup_list = hi_fixup;
2246 }
2247 hi_fixup->fixp = fixp[0];
2248 hi_fixup->seg = now_seg;
2249 }
2250
2251 /* Add fixups for the second and third relocations, if given.
2252 Note that the ABI allows the second relocation to be
2253 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2254 moment we only use RSS_UNDEF, but we could add support
2255 for the others if it ever becomes necessary. */
2256 for (i = 1; i < 3; i++)
2257 if (reloc_type[i] != BFD_RELOC_UNUSED)
2258 {
2259 fixp[i] = fix_new (frag_now, fixp[0]->fx_where,
2260 fixp[0]->fx_size, NULL, 0,
2261 FALSE, reloc_type[i]);
2262
2263 /* Use fx_tcbit to mark compound relocs. */
2264 fixp[0]->fx_tcbit = 1;
2265 fixp[i]->fx_tcbit = 1;
2266 }
2267 }
2268 }
2269
2270 if (! mips_opts.mips16)
2271 md_number_to_chars (f, ip->insn_opcode, 4);
2272 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2273 {
2274 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2275 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2276 }
2277 else
2278 {
2279 if (ip->use_extend)
2280 {
2281 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2282 f += 2;
2283 }
2284 md_number_to_chars (f, ip->insn_opcode, 2);
2285 }
2286
2287 /* Update the register mask information. */
2288 if (! mips_opts.mips16)
2289 {
2290 if (pinfo & INSN_WRITE_GPR_D)
2291 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2292 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2293 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2294 if (pinfo & INSN_READ_GPR_S)
2295 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2296 if (pinfo & INSN_WRITE_GPR_31)
2297 mips_gprmask |= 1 << RA;
2298 if (pinfo & INSN_WRITE_FPR_D)
2299 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2300 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2301 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2302 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2303 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2304 if ((pinfo & INSN_READ_FPR_R) != 0)
2305 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2306 if (pinfo & INSN_COP)
2307 {
2308 /* We don't keep enough information to sort these cases out.
2309 The itbl support does keep this information however, although
2310 we currently don't support itbl fprmats as part of the cop
2311 instruction. May want to add this support in the future. */
2312 }
2313 /* Never set the bit for $0, which is always zero. */
2314 mips_gprmask &= ~1 << 0;
2315 }
2316 else
2317 {
2318 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2319 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2320 & MIPS16OP_MASK_RX);
2321 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2322 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2323 & MIPS16OP_MASK_RY);
2324 if (pinfo & MIPS16_INSN_WRITE_Z)
2325 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2326 & MIPS16OP_MASK_RZ);
2327 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2328 mips_gprmask |= 1 << TREG;
2329 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2330 mips_gprmask |= 1 << SP;
2331 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2332 mips_gprmask |= 1 << RA;
2333 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2334 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2335 if (pinfo & MIPS16_INSN_READ_Z)
2336 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2337 & MIPS16OP_MASK_MOVE32Z);
2338 if (pinfo & MIPS16_INSN_READ_GPR_X)
2339 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2340 & MIPS16OP_MASK_REGR32);
2341 }
2342
2343 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2344 {
2345 /* Filling the branch delay slot is more complex. We try to
2346 switch the branch with the previous instruction, which we can
2347 do if the previous instruction does not set up a condition
2348 that the branch tests and if the branch is not itself the
2349 target of any branch. */
2350 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2351 || (pinfo & INSN_COND_BRANCH_DELAY))
2352 {
2353 if (mips_optimize < 2
2354 /* If we have seen .set volatile or .set nomove, don't
2355 optimize. */
2356 || mips_opts.nomove != 0
2357 /* If we had to emit any NOP instructions, then we
2358 already know we can not swap. */
2359 || nops != 0
2360 /* If we don't even know the previous insn, we can not
2361 swap. */
2362 || ! history[0].valid_p
2363 /* If the previous insn is already in a branch delay
2364 slot, then we can not swap. */
2365 || history[0].delay_slot_p
2366 /* If the previous previous insn was in a .set
2367 noreorder, we can't swap. Actually, the MIPS
2368 assembler will swap in this situation. However, gcc
2369 configured -with-gnu-as will generate code like
2370 .set noreorder
2371 lw $4,XXX
2372 .set reorder
2373 INSN
2374 bne $4,$0,foo
2375 in which we can not swap the bne and INSN. If gcc is
2376 not configured -with-gnu-as, it does not output the
2377 .set pseudo-ops. We don't have to check
2378 history[0].noreorder_p, because history[0].valid_p will
2379 be 0 in that case. We don't want to use
2380 history[1].valid_p, because we do want to be able
2381 to swap at the start of a function. */
2382 || history[1].noreorder_p
2383 /* If the branch is itself the target of a branch, we
2384 can not swap. We cheat on this; all we check for is
2385 whether there is a label on this instruction. If
2386 there are any branches to anything other than a
2387 label, users must use .set noreorder. */
2388 || insn_labels != NULL
2389 /* If the previous instruction is in a variant frag
2390 other than this branch's one, we cannot do the swap.
2391 This does not apply to the mips16, which uses variant
2392 frags for different purposes. */
2393 || (! mips_opts.mips16
2394 && prev_insn_frag_type == rs_machine_dependent)
2395 /* If the branch reads the condition codes, we don't
2396 even try to swap, because in the sequence
2397 ctc1 $X,$31
2398 INSN
2399 INSN
2400 bc1t LABEL
2401 we can not swap, and I don't feel like handling that
2402 case. */
2403 || (! mips_opts.mips16
2404 && (pinfo & INSN_READ_COND_CODE)
2405 && ! cop_interlocks)
2406 /* We can not swap with an instruction that requires a
2407 delay slot, because the target of the branch might
2408 interfere with that instruction. */
2409 || (! mips_opts.mips16
2410 && (prev_pinfo
2411 /* Itbl support may require additional care here. */
2412 & (INSN_LOAD_COPROC_DELAY
2413 | INSN_COPROC_MOVE_DELAY
2414 | INSN_WRITE_COND_CODE))
2415 && ! cop_interlocks)
2416 || (! (hilo_interlocks
2417 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
2418 && (prev_pinfo
2419 & (INSN_READ_LO
2420 | INSN_READ_HI)))
2421 || (! mips_opts.mips16
2422 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2423 && ! gpr_interlocks)
2424 || (! mips_opts.mips16
2425 /* Itbl support may require additional care here. */
2426 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2427 && ! cop_mem_interlocks)
2428 /* We can not swap with a branch instruction. */
2429 || (prev_pinfo
2430 & (INSN_UNCOND_BRANCH_DELAY
2431 | INSN_COND_BRANCH_DELAY
2432 | INSN_COND_BRANCH_LIKELY))
2433 /* We do not swap with a trap instruction, since it
2434 complicates trap handlers to have the trap
2435 instruction be in a delay slot. */
2436 || (prev_pinfo & INSN_TRAP)
2437 /* If the branch reads a register that the previous
2438 instruction sets, we can not swap. */
2439 || (! mips_opts.mips16
2440 && (prev_pinfo & INSN_WRITE_GPR_T)
2441 && insn_uses_reg (ip,
2442 ((history[0].insn_opcode >> OP_SH_RT)
2443 & OP_MASK_RT),
2444 MIPS_GR_REG))
2445 || (! mips_opts.mips16
2446 && (prev_pinfo & INSN_WRITE_GPR_D)
2447 && insn_uses_reg (ip,
2448 ((history[0].insn_opcode >> OP_SH_RD)
2449 & OP_MASK_RD),
2450 MIPS_GR_REG))
2451 || (mips_opts.mips16
2452 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2453 && insn_uses_reg (ip,
2454 ((history[0].insn_opcode
2455 >> MIPS16OP_SH_RX)
2456 & MIPS16OP_MASK_RX),
2457 MIPS16_REG))
2458 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2459 && insn_uses_reg (ip,
2460 ((history[0].insn_opcode
2461 >> MIPS16OP_SH_RY)
2462 & MIPS16OP_MASK_RY),
2463 MIPS16_REG))
2464 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2465 && insn_uses_reg (ip,
2466 ((history[0].insn_opcode
2467 >> MIPS16OP_SH_RZ)
2468 & MIPS16OP_MASK_RZ),
2469 MIPS16_REG))
2470 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2471 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2472 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2473 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2474 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2475 && insn_uses_reg (ip,
2476 MIPS16OP_EXTRACT_REG32R
2477 (history[0].insn_opcode),
2478 MIPS_GR_REG))))
2479 /* If the branch writes a register that the previous
2480 instruction sets, we can not swap (we know that
2481 branches write only to RD or to $31). */
2482 || (! mips_opts.mips16
2483 && (prev_pinfo & INSN_WRITE_GPR_T)
2484 && (((pinfo & INSN_WRITE_GPR_D)
2485 && (((history[0].insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2486 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2487 || ((pinfo & INSN_WRITE_GPR_31)
2488 && (((history[0].insn_opcode >> OP_SH_RT)
2489 & OP_MASK_RT)
2490 == RA))))
2491 || (! mips_opts.mips16
2492 && (prev_pinfo & INSN_WRITE_GPR_D)
2493 && (((pinfo & INSN_WRITE_GPR_D)
2494 && (((history[0].insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2495 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2496 || ((pinfo & INSN_WRITE_GPR_31)
2497 && (((history[0].insn_opcode >> OP_SH_RD)
2498 & OP_MASK_RD)
2499 == RA))))
2500 || (mips_opts.mips16
2501 && (pinfo & MIPS16_INSN_WRITE_31)
2502 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2503 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2504 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
2505 == RA))))
2506 /* If the branch writes a register that the previous
2507 instruction reads, we can not swap (we know that
2508 branches only write to RD or to $31). */
2509 || (! mips_opts.mips16
2510 && (pinfo & INSN_WRITE_GPR_D)
2511 && insn_uses_reg (&history[0],
2512 ((ip->insn_opcode >> OP_SH_RD)
2513 & OP_MASK_RD),
2514 MIPS_GR_REG))
2515 || (! mips_opts.mips16
2516 && (pinfo & INSN_WRITE_GPR_31)
2517 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2518 || (mips_opts.mips16
2519 && (pinfo & MIPS16_INSN_WRITE_31)
2520 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
2521 /* If the previous previous instruction has a load
2522 delay, and sets a register that the branch reads, we
2523 can not swap. */
2524 || (! mips_opts.mips16
2525 /* Itbl support may require additional care here. */
2526 && (((history[1].insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2527 && ! cop_interlocks)
2528 || ((history[1].insn_mo->pinfo
2529 & INSN_LOAD_MEMORY_DELAY)
2530 && ! gpr_interlocks))
2531 && insn_uses_reg (ip,
2532 ((history[1].insn_opcode >> OP_SH_RT)
2533 & OP_MASK_RT),
2534 MIPS_GR_REG))
2535 /* If one instruction sets a condition code and the
2536 other one uses a condition code, we can not swap. */
2537 || ((pinfo & INSN_READ_COND_CODE)
2538 && (prev_pinfo & INSN_WRITE_COND_CODE))
2539 || ((pinfo & INSN_WRITE_COND_CODE)
2540 && (prev_pinfo & INSN_READ_COND_CODE))
2541 /* If the previous instruction uses the PC, we can not
2542 swap. */
2543 || (mips_opts.mips16
2544 && (prev_pinfo & MIPS16_INSN_READ_PC))
2545 /* If the previous instruction was extended, we can not
2546 swap. */
2547 || (mips_opts.mips16 && history[0].extended_p)
2548 /* If the previous instruction had a fixup in mips16
2549 mode, we can not swap. This normally means that the
2550 previous instruction was a 4 byte branch anyhow. */
2551 || (mips_opts.mips16 && history[0].fixp[0])
2552 /* If the previous instruction is a sync, sync.l, or
2553 sync.p, we can not swap. */
2554 || (prev_pinfo & INSN_SYNC))
2555 {
2556 /* We could do even better for unconditional branches to
2557 portions of this object file; we could pick up the
2558 instruction at the destination, put it in the delay
2559 slot, and bump the destination address. */
2560 emit_nop ();
2561 if (mips_relax.sequence)
2562 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2563 /* Update the previous insn information. */
2564 history[1].insn_mo = ip->insn_mo;
2565 history[1].use_extend = ip->use_extend;
2566 history[1].extend = ip->extend;
2567 history[1].insn_opcode = ip->insn_opcode;
2568 history[0].insn_mo = &dummy_opcode;
2569 }
2570 else
2571 {
2572 /* It looks like we can actually do the swap. */
2573 if (! mips_opts.mips16)
2574 {
2575 char *prev_f;
2576 char temp[4];
2577
2578 prev_f = history[0].frag->fr_literal + history[0].where;
2579 if (!relaxed_branch)
2580 {
2581 /* If this is not a relaxed branch, then just
2582 swap the instructions. */
2583 memcpy (temp, prev_f, 4);
2584 memcpy (prev_f, f, 4);
2585 memcpy (f, temp, 4);
2586 }
2587 else
2588 {
2589 /* If this is a relaxed branch, then we move the
2590 instruction to be placed in the delay slot to
2591 the current frag, shrinking the fixed part of
2592 the originating frag. If the branch occupies
2593 the tail of the latter, we move it backwards,
2594 into the space freed by the moved instruction. */
2595 f = frag_more (4);
2596 memcpy (f, prev_f, 4);
2597 history[0].frag->fr_fix -= 4;
2598 if (history[0].frag->fr_type == rs_machine_dependent)
2599 memmove (prev_f, prev_f + 4, history[0].frag->fr_var);
2600 }
2601
2602 if (history[0].fixp[0])
2603 {
2604 history[0].fixp[0]->fx_frag = frag_now;
2605 history[0].fixp[0]->fx_where = f - frag_now->fr_literal;
2606 }
2607 if (history[0].fixp[1])
2608 {
2609 history[0].fixp[1]->fx_frag = frag_now;
2610 history[0].fixp[1]->fx_where = f - frag_now->fr_literal;
2611 }
2612 if (history[0].fixp[2])
2613 {
2614 history[0].fixp[2]->fx_frag = frag_now;
2615 history[0].fixp[2]->fx_where = f - frag_now->fr_literal;
2616 }
2617 if (history[0].fixp[0] && HAVE_NEWABI
2618 && history[0].frag != frag_now
2619 && (history[0].fixp[0]->fx_r_type
2620 == BFD_RELOC_MIPS_GOT_DISP
2621 || (history[0].fixp[0]->fx_r_type
2622 == BFD_RELOC_MIPS_CALL16)))
2623 {
2624 /* To avoid confusion in tc_gen_reloc, we must
2625 ensure that this does not become a variant
2626 frag. */
2627 force_new_frag = TRUE;
2628 }
2629
2630 if (!relaxed_branch)
2631 {
2632 if (fixp[0])
2633 {
2634 fixp[0]->fx_frag = history[0].frag;
2635 fixp[0]->fx_where = history[0].where;
2636 }
2637 if (fixp[1])
2638 {
2639 fixp[1]->fx_frag = history[0].frag;
2640 fixp[1]->fx_where = history[0].where;
2641 }
2642 if (fixp[2])
2643 {
2644 fixp[2]->fx_frag = history[0].frag;
2645 fixp[2]->fx_where = history[0].where;
2646 }
2647 }
2648 else if (history[0].frag->fr_type == rs_machine_dependent)
2649 {
2650 if (fixp[0])
2651 fixp[0]->fx_where -= 4;
2652 if (fixp[1])
2653 fixp[1]->fx_where -= 4;
2654 if (fixp[2])
2655 fixp[2]->fx_where -= 4;
2656 }
2657 }
2658 else
2659 {
2660 char *prev_f;
2661 char temp[2];
2662
2663 assert (history[0].fixp[0] == NULL);
2664 assert (history[0].fixp[1] == NULL);
2665 assert (history[0].fixp[2] == NULL);
2666 prev_f = history[0].frag->fr_literal + history[0].where;
2667 memcpy (temp, prev_f, 2);
2668 memcpy (prev_f, f, 2);
2669 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2670 {
2671 assert (*reloc_type == BFD_RELOC_UNUSED);
2672 memcpy (f, temp, 2);
2673 }
2674 else
2675 {
2676 memcpy (f, f + 2, 2);
2677 memcpy (f + 2, temp, 2);
2678 }
2679 if (fixp[0])
2680 {
2681 fixp[0]->fx_frag = history[0].frag;
2682 fixp[0]->fx_where = history[0].where;
2683 }
2684 if (fixp[1])
2685 {
2686 fixp[1]->fx_frag = history[0].frag;
2687 fixp[1]->fx_where = history[0].where;
2688 }
2689 if (fixp[2])
2690 {
2691 fixp[2]->fx_frag = history[0].frag;
2692 fixp[2]->fx_where = history[0].where;
2693 }
2694 }
2695
2696 /* Update the previous insn information; leave history[0]
2697 unchanged. */
2698 history[1].insn_mo = ip->insn_mo;
2699 history[1].use_extend = ip->use_extend;
2700 history[1].extend = ip->extend;
2701 history[1].insn_opcode = ip->insn_opcode;
2702 }
2703 history[0].delay_slot_p = 1;
2704
2705 /* If that was an unconditional branch, forget the previous
2706 insn information. */
2707 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2708 {
2709 history[1].insn_mo = &dummy_opcode;
2710 history[0].insn_mo = &dummy_opcode;
2711 }
2712
2713 history[0].fixp[0] = NULL;
2714 history[0].fixp[1] = NULL;
2715 history[0].fixp[2] = NULL;
2716 history[0].mips16_absolute_jump_p = 0;
2717 history[0].extended_p = 0;
2718 }
2719 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2720 {
2721 /* We don't yet optimize a branch likely. What we should do
2722 is look at the target, copy the instruction found there
2723 into the delay slot, and increment the branch to jump to
2724 the next instruction. */
2725 emit_nop ();
2726 /* Update the previous insn information. */
2727 history[1].insn_mo = ip->insn_mo;
2728 history[1].use_extend = ip->use_extend;
2729 history[1].extend = ip->extend;
2730 history[1].insn_opcode = ip->insn_opcode;
2731 history[0].insn_mo = &dummy_opcode;
2732 history[0].fixp[0] = NULL;
2733 history[0].fixp[1] = NULL;
2734 history[0].fixp[2] = NULL;
2735 history[0].mips16_absolute_jump_p = 0;
2736 history[0].extended_p = 0;
2737 history[0].delay_slot_p = 1;
2738 }
2739 else
2740 {
2741 /* Update the previous insn information. */
2742 if (nops > 0)
2743 history[1].insn_mo = &dummy_opcode;
2744 else
2745 {
2746 history[1].insn_mo = history[0].insn_mo;
2747 history[1].use_extend = history[0].use_extend;
2748 history[1].extend = history[0].extend;
2749 history[1].insn_opcode = history[0].insn_opcode;
2750 }
2751 history[0].insn_mo = ip->insn_mo;
2752 history[0].use_extend = ip->use_extend;
2753 history[0].extend = ip->extend;
2754 history[0].insn_opcode = ip->insn_opcode;
2755
2756 /* Any time we see a branch, we always fill the delay slot
2757 immediately; since this insn is not a branch, we know it
2758 is not in a delay slot. */
2759 history[0].delay_slot_p = 0;
2760
2761 history[0].fixp[0] = fixp[0];
2762 history[0].fixp[1] = fixp[1];
2763 history[0].fixp[2] = fixp[2];
2764 history[0].mips16_absolute_jump_p = (reloc_type[0]
2765 == BFD_RELOC_MIPS16_JMP);
2766 if (mips_opts.mips16)
2767 history[0].extended_p = (ip->use_extend
2768 || *reloc_type > BFD_RELOC_UNUSED);
2769 }
2770
2771 history[1].noreorder_p = history[0].noreorder_p;
2772 history[0].noreorder_p = 0;
2773 history[0].frag = frag_now;
2774 history[0].where = f - frag_now->fr_literal;
2775 history[0].valid_p = 1;
2776 }
2777 else if (mips_relax.sequence != 2)
2778 {
2779 /* We need to record a bit of information even when we are not
2780 reordering, in order to determine the base address for mips16
2781 PC relative relocs. */
2782 history[1].insn_mo = history[0].insn_mo;
2783 history[1].use_extend = history[0].use_extend;
2784 history[1].extend = history[0].extend;
2785 history[1].insn_opcode = history[0].insn_opcode;
2786 history[0].insn_mo = ip->insn_mo;
2787 history[0].use_extend = ip->use_extend;
2788 history[0].extend = ip->extend;
2789 history[0].insn_opcode = ip->insn_opcode;
2790 history[0].mips16_absolute_jump_p = (reloc_type[0]
2791 == BFD_RELOC_MIPS16_JMP);
2792 history[1].noreorder_p = history[0].noreorder_p;
2793 history[0].noreorder_p = 1;
2794 }
2795
2796 /* We just output an insn, so the next one doesn't have a label. */
2797 mips_clear_insn_labels ();
2798 }
2799
2800 /* This function forgets that there was any previous instruction or
2801 label. If PRESERVE is non-zero, it remembers enough information to
2802 know whether nops are needed before a noreorder section. */
2803
2804 static void
2805 mips_no_prev_insn (int preserve)
2806 {
2807 if (! preserve)
2808 {
2809 history[0].insn_mo = &dummy_opcode;
2810 history[1].insn_mo = &dummy_opcode;
2811 prev_nop_frag = NULL;
2812 prev_nop_frag_holds = 0;
2813 prev_nop_frag_required = 0;
2814 prev_nop_frag_since = 0;
2815 }
2816 history[0].valid_p = 0;
2817 history[0].delay_slot_p = 0;
2818 history[0].noreorder_p = 0;
2819 history[0].extended_p = 0;
2820 history[0].mips16_absolute_jump_p = 0;
2821 history[1].noreorder_p = 0;
2822 mips_clear_insn_labels ();
2823 }
2824
2825 /* This function must be called whenever we turn on noreorder or emit
2826 something other than instructions. It inserts any NOPS which might
2827 be needed by the previous instruction, and clears the information
2828 kept for the previous instructions. The INSNS parameter is true if
2829 instructions are to follow. */
2830
2831 static void
2832 mips_emit_delays (bfd_boolean insns)
2833 {
2834 if (! mips_opts.noreorder)
2835 {
2836 int nops;
2837
2838 nops = 0;
2839 if ((! mips_opts.mips16
2840 && ((history[0].insn_mo->pinfo
2841 & (INSN_LOAD_COPROC_DELAY
2842 | INSN_COPROC_MOVE_DELAY
2843 | INSN_WRITE_COND_CODE))
2844 && ! cop_interlocks))
2845 || (! hilo_interlocks
2846 && (history[0].insn_mo->pinfo
2847 & (INSN_READ_LO
2848 | INSN_READ_HI)))
2849 || (! mips_opts.mips16
2850 && (history[0].insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2851 && ! gpr_interlocks)
2852 || (! mips_opts.mips16
2853 && (history[0].insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2854 && ! cop_mem_interlocks))
2855 {
2856 /* Itbl support may require additional care here. */
2857 ++nops;
2858 if ((! mips_opts.mips16
2859 && ((history[0].insn_mo->pinfo & INSN_WRITE_COND_CODE)
2860 && ! cop_interlocks))
2861 || (! hilo_interlocks
2862 && ((history[0].insn_mo->pinfo & INSN_READ_HI)
2863 || (history[0].insn_mo->pinfo & INSN_READ_LO))))
2864 ++nops;
2865
2866 if (history[0].noreorder_p)
2867 nops = 0;
2868 }
2869 else if ((! mips_opts.mips16
2870 && ((history[1].insn_mo->pinfo & INSN_WRITE_COND_CODE)
2871 && ! cop_interlocks))
2872 || (! hilo_interlocks
2873 && ((history[1].insn_mo->pinfo & INSN_READ_HI)
2874 || (history[1].insn_mo->pinfo & INSN_READ_LO))))
2875 {
2876 /* Itbl support may require additional care here. */
2877 if (! history[1].noreorder_p)
2878 ++nops;
2879 }
2880
2881 if (mips_fix_vr4120 && history[0].insn_mo->name)
2882 {
2883 int min_nops = 0;
2884 const char *pn = history[0].insn_mo->name;
2885 if (strncmp (pn, "macc", 4) == 0
2886 || strncmp (pn, "dmacc", 5) == 0
2887 || strncmp (pn, "dmult", 5) == 0
2888 || strstr (pn, "div"))
2889 min_nops = 1;
2890 if (nops < min_nops)
2891 nops = min_nops;
2892 }
2893
2894 if (nops > 0)
2895 {
2896 struct insn_label_list *l;
2897
2898 if (insns)
2899 {
2900 /* Record the frag which holds the nop instructions, so
2901 that we can remove them if we don't need them. */
2902 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2903 prev_nop_frag = frag_now;
2904 prev_nop_frag_holds = nops;
2905 prev_nop_frag_required = 0;
2906 prev_nop_frag_since = 0;
2907 }
2908
2909 for (; nops > 0; --nops)
2910 emit_nop ();
2911
2912 if (insns)
2913 {
2914 /* Move on to a new frag, so that it is safe to simply
2915 decrease the size of prev_nop_frag. */
2916 frag_wane (frag_now);
2917 frag_new (0);
2918 }
2919
2920 for (l = insn_labels; l != NULL; l = l->next)
2921 {
2922 valueT val;
2923
2924 assert (S_GET_SEGMENT (l->label) == now_seg);
2925 symbol_set_frag (l->label, frag_now);
2926 val = (valueT) frag_now_fix ();
2927 /* mips16 text labels are stored as odd. */
2928 if (mips_opts.mips16)
2929 ++val;
2930 S_SET_VALUE (l->label, val);
2931 }
2932 }
2933 }
2934
2935 /* Mark instruction labels in mips16 mode. */
2936 if (insns)
2937 mips16_mark_labels ();
2938
2939 mips_no_prev_insn (insns);
2940 }
2941
2942 /* Set up global variables for the start of a new macro. */
2943
2944 static void
2945 macro_start (void)
2946 {
2947 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2948 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2949 && (history[0].insn_mo->pinfo
2950 & (INSN_UNCOND_BRANCH_DELAY
2951 | INSN_COND_BRANCH_DELAY
2952 | INSN_COND_BRANCH_LIKELY)) != 0);
2953 }
2954
2955 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2956 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2957 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2958
2959 static const char *
2960 macro_warning (relax_substateT subtype)
2961 {
2962 if (subtype & RELAX_DELAY_SLOT)
2963 return _("Macro instruction expanded into multiple instructions"
2964 " in a branch delay slot");
2965 else if (subtype & RELAX_NOMACRO)
2966 return _("Macro instruction expanded into multiple instructions");
2967 else
2968 return 0;
2969 }
2970
2971 /* Finish up a macro. Emit warnings as appropriate. */
2972
2973 static void
2974 macro_end (void)
2975 {
2976 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2977 {
2978 relax_substateT subtype;
2979
2980 /* Set up the relaxation warning flags. */
2981 subtype = 0;
2982 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2983 subtype |= RELAX_SECOND_LONGER;
2984 if (mips_opts.warn_about_macros)
2985 subtype |= RELAX_NOMACRO;
2986 if (mips_macro_warning.delay_slot_p)
2987 subtype |= RELAX_DELAY_SLOT;
2988
2989 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2990 {
2991 /* Either the macro has a single implementation or both
2992 implementations are longer than 4 bytes. Emit the
2993 warning now. */
2994 const char *msg = macro_warning (subtype);
2995 if (msg != 0)
2996 as_warn (msg);
2997 }
2998 else
2999 {
3000 /* One implementation might need a warning but the other
3001 definitely doesn't. */
3002 mips_macro_warning.first_frag->fr_subtype |= subtype;
3003 }
3004 }
3005 }
3006
3007 /* Read a macro's relocation codes from *ARGS and store them in *R.
3008 The first argument in *ARGS will be either the code for a single
3009 relocation or -1 followed by the three codes that make up a
3010 composite relocation. */
3011
3012 static void
3013 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3014 {
3015 int i, next;
3016
3017 next = va_arg (*args, int);
3018 if (next >= 0)
3019 r[0] = (bfd_reloc_code_real_type) next;
3020 else
3021 for (i = 0; i < 3; i++)
3022 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3023 }
3024
3025 /* Build an instruction created by a macro expansion. This is passed
3026 a pointer to the count of instructions created so far, an
3027 expression, the name of the instruction to build, an operand format
3028 string, and corresponding arguments. */
3029
3030 static void
3031 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3032 {
3033 struct mips_cl_insn insn;
3034 bfd_reloc_code_real_type r[3];
3035 va_list args;
3036
3037 va_start (args, fmt);
3038
3039 if (mips_opts.mips16)
3040 {
3041 mips16_macro_build (ep, name, fmt, args);
3042 va_end (args);
3043 return;
3044 }
3045
3046 r[0] = BFD_RELOC_UNUSED;
3047 r[1] = BFD_RELOC_UNUSED;
3048 r[2] = BFD_RELOC_UNUSED;
3049 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3050 assert (insn.insn_mo);
3051 assert (strcmp (name, insn.insn_mo->name) == 0);
3052
3053 /* Search until we get a match for NAME. */
3054 while (1)
3055 {
3056 /* It is assumed here that macros will never generate
3057 MDMX or MIPS-3D instructions. */
3058 if (strcmp (fmt, insn.insn_mo->args) == 0
3059 && insn.insn_mo->pinfo != INSN_MACRO
3060 && OPCODE_IS_MEMBER (insn.insn_mo,
3061 (mips_opts.isa
3062 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3063 mips_opts.arch)
3064 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3065 break;
3066
3067 ++insn.insn_mo;
3068 assert (insn.insn_mo->name);
3069 assert (strcmp (name, insn.insn_mo->name) == 0);
3070 }
3071
3072 insn.insn_opcode = insn.insn_mo->match;
3073 for (;;)
3074 {
3075 switch (*fmt++)
3076 {
3077 case '\0':
3078 break;
3079
3080 case ',':
3081 case '(':
3082 case ')':
3083 continue;
3084
3085 case '+':
3086 switch (*fmt++)
3087 {
3088 case 'A':
3089 case 'E':
3090 insn.insn_opcode |= (va_arg (args, int)
3091 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3092 continue;
3093
3094 case 'B':
3095 case 'F':
3096 /* Note that in the macro case, these arguments are already
3097 in MSB form. (When handling the instruction in the
3098 non-macro case, these arguments are sizes from which
3099 MSB values must be calculated.) */
3100 insn.insn_opcode |= (va_arg (args, int)
3101 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3102 continue;
3103
3104 case 'C':
3105 case 'G':
3106 case 'H':
3107 /* Note that in the macro case, these arguments are already
3108 in MSBD form. (When handling the instruction in the
3109 non-macro case, these arguments are sizes from which
3110 MSBD values must be calculated.) */
3111 insn.insn_opcode |= (va_arg (args, int)
3112 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3113 continue;
3114
3115 default:
3116 internalError ();
3117 }
3118 continue;
3119
3120 case 't':
3121 case 'w':
3122 case 'E':
3123 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3124 continue;
3125
3126 case 'c':
3127 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3128 continue;
3129
3130 case 'T':
3131 case 'W':
3132 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3133 continue;
3134
3135 case 'd':
3136 case 'G':
3137 case 'K':
3138 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3139 continue;
3140
3141 case 'U':
3142 {
3143 int tmp = va_arg (args, int);
3144
3145 insn.insn_opcode |= tmp << OP_SH_RT;
3146 insn.insn_opcode |= tmp << OP_SH_RD;
3147 continue;
3148 }
3149
3150 case 'V':
3151 case 'S':
3152 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3153 continue;
3154
3155 case 'z':
3156 continue;
3157
3158 case '<':
3159 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3160 continue;
3161
3162 case 'D':
3163 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3164 continue;
3165
3166 case 'B':
3167 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3168 continue;
3169
3170 case 'J':
3171 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3172 continue;
3173
3174 case 'q':
3175 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3176 continue;
3177
3178 case 'b':
3179 case 's':
3180 case 'r':
3181 case 'v':
3182 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3183 continue;
3184
3185 case 'i':
3186 case 'j':
3187 case 'o':
3188 macro_read_relocs (&args, r);
3189 assert (*r == BFD_RELOC_GPREL16
3190 || *r == BFD_RELOC_MIPS_LITERAL
3191 || *r == BFD_RELOC_MIPS_HIGHER
3192 || *r == BFD_RELOC_HI16_S
3193 || *r == BFD_RELOC_LO16
3194 || *r == BFD_RELOC_MIPS_GOT16
3195 || *r == BFD_RELOC_MIPS_CALL16
3196 || *r == BFD_RELOC_MIPS_GOT_DISP
3197 || *r == BFD_RELOC_MIPS_GOT_PAGE
3198 || *r == BFD_RELOC_MIPS_GOT_OFST
3199 || *r == BFD_RELOC_MIPS_GOT_LO16
3200 || *r == BFD_RELOC_MIPS_CALL_LO16);
3201 continue;
3202
3203 case 'u':
3204 macro_read_relocs (&args, r);
3205 assert (ep != NULL
3206 && (ep->X_op == O_constant
3207 || (ep->X_op == O_symbol
3208 && (*r == BFD_RELOC_MIPS_HIGHEST
3209 || *r == BFD_RELOC_HI16_S
3210 || *r == BFD_RELOC_HI16
3211 || *r == BFD_RELOC_GPREL16
3212 || *r == BFD_RELOC_MIPS_GOT_HI16
3213 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3214 continue;
3215
3216 case 'p':
3217 assert (ep != NULL);
3218 /*
3219 * This allows macro() to pass an immediate expression for
3220 * creating short branches without creating a symbol.
3221 * Note that the expression still might come from the assembly
3222 * input, in which case the value is not checked for range nor
3223 * is a relocation entry generated (yuck).
3224 */
3225 if (ep->X_op == O_constant)
3226 {
3227 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3228 ep = NULL;
3229 }
3230 else
3231 *r = BFD_RELOC_16_PCREL_S2;
3232 continue;
3233
3234 case 'a':
3235 assert (ep != NULL);
3236 *r = BFD_RELOC_MIPS_JMP;
3237 continue;
3238
3239 case 'C':
3240 insn.insn_opcode |= va_arg (args, unsigned long);
3241 continue;
3242
3243 default:
3244 internalError ();
3245 }
3246 break;
3247 }
3248 va_end (args);
3249 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3250
3251 append_insn (&insn, ep, r);
3252 }
3253
3254 static void
3255 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3256 va_list args)
3257 {
3258 struct mips_cl_insn insn;
3259 bfd_reloc_code_real_type r[3]
3260 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3261
3262 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3263 assert (insn.insn_mo);
3264 assert (strcmp (name, insn.insn_mo->name) == 0);
3265
3266 while (strcmp (fmt, insn.insn_mo->args) != 0
3267 || insn.insn_mo->pinfo == INSN_MACRO)
3268 {
3269 ++insn.insn_mo;
3270 assert (insn.insn_mo->name);
3271 assert (strcmp (name, insn.insn_mo->name) == 0);
3272 }
3273
3274 insn.insn_opcode = insn.insn_mo->match;
3275 insn.use_extend = FALSE;
3276
3277 for (;;)
3278 {
3279 int c;
3280
3281 c = *fmt++;
3282 switch (c)
3283 {
3284 case '\0':
3285 break;
3286
3287 case ',':
3288 case '(':
3289 case ')':
3290 continue;
3291
3292 case 'y':
3293 case 'w':
3294 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3295 continue;
3296
3297 case 'x':
3298 case 'v':
3299 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3300 continue;
3301
3302 case 'z':
3303 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3304 continue;
3305
3306 case 'Z':
3307 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3308 continue;
3309
3310 case '0':
3311 case 'S':
3312 case 'P':
3313 case 'R':
3314 continue;
3315
3316 case 'X':
3317 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3318 continue;
3319
3320 case 'Y':
3321 {
3322 int regno;
3323
3324 regno = va_arg (args, int);
3325 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3326 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3327 }
3328 continue;
3329
3330 case '<':
3331 case '>':
3332 case '4':
3333 case '5':
3334 case 'H':
3335 case 'W':
3336 case 'D':
3337 case 'j':
3338 case '8':
3339 case 'V':
3340 case 'C':
3341 case 'U':
3342 case 'k':
3343 case 'K':
3344 case 'p':
3345 case 'q':
3346 {
3347 assert (ep != NULL);
3348
3349 if (ep->X_op != O_constant)
3350 *r = (int) BFD_RELOC_UNUSED + c;
3351 else
3352 {
3353 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3354 FALSE, &insn.insn_opcode, &insn.use_extend,
3355 &insn.extend);
3356 ep = NULL;
3357 *r = BFD_RELOC_UNUSED;
3358 }
3359 }
3360 continue;
3361
3362 case '6':
3363 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3364 continue;
3365 }
3366
3367 break;
3368 }
3369
3370 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3371
3372 append_insn (&insn, ep, r);
3373 }
3374
3375 /*
3376 * Generate a "jalr" instruction with a relocation hint to the called
3377 * function. This occurs in NewABI PIC code.
3378 */
3379 static void
3380 macro_build_jalr (expressionS *ep)
3381 {
3382 char *f = NULL;
3383
3384 if (HAVE_NEWABI)
3385 {
3386 frag_grow (8);
3387 f = frag_more (0);
3388 }
3389 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3390 if (HAVE_NEWABI)
3391 fix_new_exp (frag_now, f - frag_now->fr_literal,
3392 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3393 }
3394
3395 /*
3396 * Generate a "lui" instruction.
3397 */
3398 static void
3399 macro_build_lui (expressionS *ep, int regnum)
3400 {
3401 expressionS high_expr;
3402 struct mips_cl_insn insn;
3403 bfd_reloc_code_real_type r[3]
3404 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3405 const char *name = "lui";
3406 const char *fmt = "t,u";
3407
3408 assert (! mips_opts.mips16);
3409
3410 high_expr = *ep;
3411
3412 if (high_expr.X_op == O_constant)
3413 {
3414 /* we can compute the instruction now without a relocation entry */
3415 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3416 >> 16) & 0xffff;
3417 *r = BFD_RELOC_UNUSED;
3418 }
3419 else
3420 {
3421 assert (ep->X_op == O_symbol);
3422 /* _gp_disp is a special case, used from s_cpload.
3423 __gnu_local_gp is used if mips_no_shared. */
3424 assert (mips_pic == NO_PIC
3425 || (! HAVE_NEWABI
3426 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3427 || (! mips_in_shared
3428 && strcmp (S_GET_NAME (ep->X_add_symbol),
3429 "__gnu_local_gp") == 0));
3430 *r = BFD_RELOC_HI16_S;
3431 }
3432
3433 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3434 assert (insn.insn_mo);
3435 assert (strcmp (name, insn.insn_mo->name) == 0);
3436 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3437
3438 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3439 if (*r == BFD_RELOC_UNUSED)
3440 {
3441 insn.insn_opcode |= high_expr.X_add_number;
3442 append_insn (&insn, NULL, r);
3443 }
3444 else
3445 append_insn (&insn, &high_expr, r);
3446 }
3447
3448 /* Generate a sequence of instructions to do a load or store from a constant
3449 offset off of a base register (breg) into/from a target register (treg),
3450 using AT if necessary. */
3451 static void
3452 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3453 int treg, int breg, int dbl)
3454 {
3455 assert (ep->X_op == O_constant);
3456
3457 /* Sign-extending 32-bit constants makes their handling easier. */
3458 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3459 == ~((bfd_vma) 0x7fffffff)))
3460 {
3461 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3462 as_bad (_("constant too large"));
3463
3464 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3465 - 0x80000000);
3466 }
3467
3468 /* Right now, this routine can only handle signed 32-bit constants. */
3469 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3470 as_warn (_("operand overflow"));
3471
3472 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3473 {
3474 /* Signed 16-bit offset will fit in the op. Easy! */
3475 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3476 }
3477 else
3478 {
3479 /* 32-bit offset, need multiple instructions and AT, like:
3480 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3481 addu $tempreg,$tempreg,$breg
3482 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3483 to handle the complete offset. */
3484 macro_build_lui (ep, AT);
3485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3486 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3487
3488 if (mips_opts.noat)
3489 as_bad (_("Macro used $at after \".set noat\""));
3490 }
3491 }
3492
3493 /* set_at()
3494 * Generates code to set the $at register to true (one)
3495 * if reg is less than the immediate expression.
3496 */
3497 static void
3498 set_at (int reg, int unsignedp)
3499 {
3500 if (imm_expr.X_op == O_constant
3501 && imm_expr.X_add_number >= -0x8000
3502 && imm_expr.X_add_number < 0x8000)
3503 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3504 AT, reg, BFD_RELOC_LO16);
3505 else
3506 {
3507 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3508 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3509 }
3510 }
3511
3512 static void
3513 normalize_constant_expr (expressionS *ex)
3514 {
3515 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3516 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3517 - 0x80000000);
3518 }
3519
3520 /* Warn if an expression is not a constant. */
3521
3522 static void
3523 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3524 {
3525 if (ex->X_op == O_big)
3526 as_bad (_("unsupported large constant"));
3527 else if (ex->X_op != O_constant)
3528 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3529
3530 normalize_constant_expr (ex);
3531 }
3532
3533 /* Count the leading zeroes by performing a binary chop. This is a
3534 bulky bit of source, but performance is a LOT better for the
3535 majority of values than a simple loop to count the bits:
3536 for (lcnt = 0; (lcnt < 32); lcnt++)
3537 if ((v) & (1 << (31 - lcnt)))
3538 break;
3539 However it is not code size friendly, and the gain will drop a bit
3540 on certain cached systems.
3541 */
3542 #define COUNT_TOP_ZEROES(v) \
3543 (((v) & ~0xffff) == 0 \
3544 ? ((v) & ~0xff) == 0 \
3545 ? ((v) & ~0xf) == 0 \
3546 ? ((v) & ~0x3) == 0 \
3547 ? ((v) & ~0x1) == 0 \
3548 ? !(v) \
3549 ? 32 \
3550 : 31 \
3551 : 30 \
3552 : ((v) & ~0x7) == 0 \
3553 ? 29 \
3554 : 28 \
3555 : ((v) & ~0x3f) == 0 \
3556 ? ((v) & ~0x1f) == 0 \
3557 ? 27 \
3558 : 26 \
3559 : ((v) & ~0x7f) == 0 \
3560 ? 25 \
3561 : 24 \
3562 : ((v) & ~0xfff) == 0 \
3563 ? ((v) & ~0x3ff) == 0 \
3564 ? ((v) & ~0x1ff) == 0 \
3565 ? 23 \
3566 : 22 \
3567 : ((v) & ~0x7ff) == 0 \
3568 ? 21 \
3569 : 20 \
3570 : ((v) & ~0x3fff) == 0 \
3571 ? ((v) & ~0x1fff) == 0 \
3572 ? 19 \
3573 : 18 \
3574 : ((v) & ~0x7fff) == 0 \
3575 ? 17 \
3576 : 16 \
3577 : ((v) & ~0xffffff) == 0 \
3578 ? ((v) & ~0xfffff) == 0 \
3579 ? ((v) & ~0x3ffff) == 0 \
3580 ? ((v) & ~0x1ffff) == 0 \
3581 ? 15 \
3582 : 14 \
3583 : ((v) & ~0x7ffff) == 0 \
3584 ? 13 \
3585 : 12 \
3586 : ((v) & ~0x3fffff) == 0 \
3587 ? ((v) & ~0x1fffff) == 0 \
3588 ? 11 \
3589 : 10 \
3590 : ((v) & ~0x7fffff) == 0 \
3591 ? 9 \
3592 : 8 \
3593 : ((v) & ~0xfffffff) == 0 \
3594 ? ((v) & ~0x3ffffff) == 0 \
3595 ? ((v) & ~0x1ffffff) == 0 \
3596 ? 7 \
3597 : 6 \
3598 : ((v) & ~0x7ffffff) == 0 \
3599 ? 5 \
3600 : 4 \
3601 : ((v) & ~0x3fffffff) == 0 \
3602 ? ((v) & ~0x1fffffff) == 0 \
3603 ? 3 \
3604 : 2 \
3605 : ((v) & ~0x7fffffff) == 0 \
3606 ? 1 \
3607 : 0)
3608
3609 /* load_register()
3610 * This routine generates the least number of instructions necessary to load
3611 * an absolute expression value into a register.
3612 */
3613 static void
3614 load_register (int reg, expressionS *ep, int dbl)
3615 {
3616 int freg;
3617 expressionS hi32, lo32;
3618
3619 if (ep->X_op != O_big)
3620 {
3621 assert (ep->X_op == O_constant);
3622
3623 /* Sign-extending 32-bit constants makes their handling easier. */
3624 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3625 == ~((bfd_vma) 0x7fffffff)))
3626 {
3627 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3628 as_bad (_("constant too large"));
3629
3630 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3631 - 0x80000000);
3632 }
3633
3634 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3635 {
3636 /* We can handle 16 bit signed values with an addiu to
3637 $zero. No need to ever use daddiu here, since $zero and
3638 the result are always correct in 32 bit mode. */
3639 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3640 return;
3641 }
3642 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3643 {
3644 /* We can handle 16 bit unsigned values with an ori to
3645 $zero. */
3646 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3647 return;
3648 }
3649 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3650 {
3651 /* 32 bit values require an lui. */
3652 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3653 if ((ep->X_add_number & 0xffff) != 0)
3654 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3655 return;
3656 }
3657 }
3658
3659 /* The value is larger than 32 bits. */
3660
3661 if (HAVE_32BIT_GPRS)
3662 {
3663 as_bad (_("Number (0x%lx) larger than 32 bits"),
3664 (unsigned long) ep->X_add_number);
3665 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3666 return;
3667 }
3668
3669 if (ep->X_op != O_big)
3670 {
3671 hi32 = *ep;
3672 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3673 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3674 hi32.X_add_number &= 0xffffffff;
3675 lo32 = *ep;
3676 lo32.X_add_number &= 0xffffffff;
3677 }
3678 else
3679 {
3680 assert (ep->X_add_number > 2);
3681 if (ep->X_add_number == 3)
3682 generic_bignum[3] = 0;
3683 else if (ep->X_add_number > 4)
3684 as_bad (_("Number larger than 64 bits"));
3685 lo32.X_op = O_constant;
3686 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3687 hi32.X_op = O_constant;
3688 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3689 }
3690
3691 if (hi32.X_add_number == 0)
3692 freg = 0;
3693 else
3694 {
3695 int shift, bit;
3696 unsigned long hi, lo;
3697
3698 if (hi32.X_add_number == (offsetT) 0xffffffff)
3699 {
3700 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3701 {
3702 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3703 return;
3704 }
3705 if (lo32.X_add_number & 0x80000000)
3706 {
3707 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3708 if (lo32.X_add_number & 0xffff)
3709 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3710 return;
3711 }
3712 }
3713
3714 /* Check for 16bit shifted constant. We know that hi32 is
3715 non-zero, so start the mask on the first bit of the hi32
3716 value. */
3717 shift = 17;
3718 do
3719 {
3720 unsigned long himask, lomask;
3721
3722 if (shift < 32)
3723 {
3724 himask = 0xffff >> (32 - shift);
3725 lomask = (0xffff << shift) & 0xffffffff;
3726 }
3727 else
3728 {
3729 himask = 0xffff << (shift - 32);
3730 lomask = 0;
3731 }
3732 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3733 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3734 {
3735 expressionS tmp;
3736
3737 tmp.X_op = O_constant;
3738 if (shift < 32)
3739 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3740 | (lo32.X_add_number >> shift));
3741 else
3742 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3743 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3744 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3745 reg, reg, (shift >= 32) ? shift - 32 : shift);
3746 return;
3747 }
3748 ++shift;
3749 }
3750 while (shift <= (64 - 16));
3751
3752 /* Find the bit number of the lowest one bit, and store the
3753 shifted value in hi/lo. */
3754 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3755 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3756 if (lo != 0)
3757 {
3758 bit = 0;
3759 while ((lo & 1) == 0)
3760 {
3761 lo >>= 1;
3762 ++bit;
3763 }
3764 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3765 hi >>= bit;
3766 }
3767 else
3768 {
3769 bit = 32;
3770 while ((hi & 1) == 0)
3771 {
3772 hi >>= 1;
3773 ++bit;
3774 }
3775 lo = hi;
3776 hi = 0;
3777 }
3778
3779 /* Optimize if the shifted value is a (power of 2) - 1. */
3780 if ((hi == 0 && ((lo + 1) & lo) == 0)
3781 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3782 {
3783 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3784 if (shift != 0)
3785 {
3786 expressionS tmp;
3787
3788 /* This instruction will set the register to be all
3789 ones. */
3790 tmp.X_op = O_constant;
3791 tmp.X_add_number = (offsetT) -1;
3792 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3793 if (bit != 0)
3794 {
3795 bit += shift;
3796 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3797 reg, reg, (bit >= 32) ? bit - 32 : bit);
3798 }
3799 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3800 reg, reg, (shift >= 32) ? shift - 32 : shift);
3801 return;
3802 }
3803 }
3804
3805 /* Sign extend hi32 before calling load_register, because we can
3806 generally get better code when we load a sign extended value. */
3807 if ((hi32.X_add_number & 0x80000000) != 0)
3808 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3809 load_register (reg, &hi32, 0);
3810 freg = reg;
3811 }
3812 if ((lo32.X_add_number & 0xffff0000) == 0)
3813 {
3814 if (freg != 0)
3815 {
3816 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3817 freg = reg;
3818 }
3819 }
3820 else
3821 {
3822 expressionS mid16;
3823
3824 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3825 {
3826 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3827 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3828 return;
3829 }
3830
3831 if (freg != 0)
3832 {
3833 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3834 freg = reg;
3835 }
3836 mid16 = lo32;
3837 mid16.X_add_number >>= 16;
3838 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3839 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3840 freg = reg;
3841 }
3842 if ((lo32.X_add_number & 0xffff) != 0)
3843 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3844 }
3845
3846 static inline void
3847 load_delay_nop (void)
3848 {
3849 if (!gpr_interlocks)
3850 macro_build (NULL, "nop", "");
3851 }
3852
3853 /* Load an address into a register. */
3854
3855 static void
3856 load_address (int reg, expressionS *ep, int *used_at)
3857 {
3858 if (ep->X_op != O_constant
3859 && ep->X_op != O_symbol)
3860 {
3861 as_bad (_("expression too complex"));
3862 ep->X_op = O_constant;
3863 }
3864
3865 if (ep->X_op == O_constant)
3866 {
3867 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3868 return;
3869 }
3870
3871 if (mips_pic == NO_PIC)
3872 {
3873 /* If this is a reference to a GP relative symbol, we want
3874 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3875 Otherwise we want
3876 lui $reg,<sym> (BFD_RELOC_HI16_S)
3877 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3878 If we have an addend, we always use the latter form.
3879
3880 With 64bit address space and a usable $at we want
3881 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3882 lui $at,<sym> (BFD_RELOC_HI16_S)
3883 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3884 daddiu $at,<sym> (BFD_RELOC_LO16)
3885 dsll32 $reg,0
3886 daddu $reg,$reg,$at
3887
3888 If $at is already in use, we use a path which is suboptimal
3889 on superscalar processors.
3890 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3891 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3892 dsll $reg,16
3893 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3894 dsll $reg,16
3895 daddiu $reg,<sym> (BFD_RELOC_LO16)
3896
3897 For GP relative symbols in 64bit address space we can use
3898 the same sequence as in 32bit address space. */
3899 if (HAVE_64BIT_SYMBOLS)
3900 {
3901 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3902 && !nopic_need_relax (ep->X_add_symbol, 1))
3903 {
3904 relax_start (ep->X_add_symbol);
3905 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3906 mips_gp_register, BFD_RELOC_GPREL16);
3907 relax_switch ();
3908 }
3909
3910 if (*used_at == 0 && !mips_opts.noat)
3911 {
3912 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3913 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3914 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3915 BFD_RELOC_MIPS_HIGHER);
3916 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3917 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3918 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3919 *used_at = 1;
3920 }
3921 else
3922 {
3923 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3924 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3925 BFD_RELOC_MIPS_HIGHER);
3926 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3927 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3928 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3929 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3930 }
3931
3932 if (mips_relax.sequence)
3933 relax_end ();
3934 }
3935 else
3936 {
3937 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3938 && !nopic_need_relax (ep->X_add_symbol, 1))
3939 {
3940 relax_start (ep->X_add_symbol);
3941 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3942 mips_gp_register, BFD_RELOC_GPREL16);
3943 relax_switch ();
3944 }
3945 macro_build_lui (ep, reg);
3946 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3947 reg, reg, BFD_RELOC_LO16);
3948 if (mips_relax.sequence)
3949 relax_end ();
3950 }
3951 }
3952 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3953 {
3954 expressionS ex;
3955
3956 /* If this is a reference to an external symbol, we want
3957 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3958 Otherwise we want
3959 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3960 nop
3961 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3962 If there is a constant, it must be added in after.
3963
3964 If we have NewABI, we want
3965 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3966 unless we're referencing a global symbol with a non-zero
3967 offset, in which case cst must be added separately. */
3968 if (HAVE_NEWABI)
3969 {
3970 if (ep->X_add_number)
3971 {
3972 ex.X_add_number = ep->X_add_number;
3973 ep->X_add_number = 0;
3974 relax_start (ep->X_add_symbol);
3975 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3976 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3977 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3978 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3979 ex.X_op = O_constant;
3980 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3981 reg, reg, BFD_RELOC_LO16);
3982 ep->X_add_number = ex.X_add_number;
3983 relax_switch ();
3984 }
3985 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3986 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3987 if (mips_relax.sequence)
3988 relax_end ();
3989 }
3990 else
3991 {
3992 ex.X_add_number = ep->X_add_number;
3993 ep->X_add_number = 0;
3994 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3995 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3996 load_delay_nop ();
3997 relax_start (ep->X_add_symbol);
3998 relax_switch ();
3999 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4000 BFD_RELOC_LO16);
4001 relax_end ();
4002
4003 if (ex.X_add_number != 0)
4004 {
4005 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4006 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4007 ex.X_op = O_constant;
4008 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
4009 reg, reg, BFD_RELOC_LO16);
4010 }
4011 }
4012 }
4013 else if (mips_pic == SVR4_PIC)
4014 {
4015 expressionS ex;
4016
4017 /* This is the large GOT case. If this is a reference to an
4018 external symbol, we want
4019 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4020 addu $reg,$reg,$gp
4021 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
4022
4023 Otherwise, for a reference to a local symbol in old ABI, we want
4024 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4025 nop
4026 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4027 If there is a constant, it must be added in after.
4028
4029 In the NewABI, for local symbols, with or without offsets, we want:
4030 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4031 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4032 */
4033 if (HAVE_NEWABI)
4034 {
4035 ex.X_add_number = ep->X_add_number;
4036 ep->X_add_number = 0;
4037 relax_start (ep->X_add_symbol);
4038 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4040 reg, reg, mips_gp_register);
4041 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4042 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4043 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4044 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4045 else if (ex.X_add_number)
4046 {
4047 ex.X_op = O_constant;
4048 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4049 BFD_RELOC_LO16);
4050 }
4051
4052 ep->X_add_number = ex.X_add_number;
4053 relax_switch ();
4054 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4055 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4056 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4057 BFD_RELOC_MIPS_GOT_OFST);
4058 relax_end ();
4059 }
4060 else
4061 {
4062 ex.X_add_number = ep->X_add_number;
4063 ep->X_add_number = 0;
4064 relax_start (ep->X_add_symbol);
4065 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4066 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4067 reg, reg, mips_gp_register);
4068 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4069 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4070 relax_switch ();
4071 if (reg_needs_delay (mips_gp_register))
4072 {
4073 /* We need a nop before loading from $gp. This special
4074 check is required because the lui which starts the main
4075 instruction stream does not refer to $gp, and so will not
4076 insert the nop which may be required. */
4077 macro_build (NULL, "nop", "");
4078 }
4079 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4080 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4081 load_delay_nop ();
4082 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4083 BFD_RELOC_LO16);
4084 relax_end ();
4085
4086 if (ex.X_add_number != 0)
4087 {
4088 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4089 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4090 ex.X_op = O_constant;
4091 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4092 BFD_RELOC_LO16);
4093 }
4094 }
4095 }
4096 else
4097 abort ();
4098
4099 if (mips_opts.noat && *used_at == 1)
4100 as_bad (_("Macro used $at after \".set noat\""));
4101 }
4102
4103 /* Move the contents of register SOURCE into register DEST. */
4104
4105 static void
4106 move_register (int dest, int source)
4107 {
4108 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4109 dest, source, 0);
4110 }
4111
4112 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4113 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4114 The two alternatives are:
4115
4116 Global symbol Local sybmol
4117 ------------- ------------
4118 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4119 ... ...
4120 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4121
4122 load_got_offset emits the first instruction and add_got_offset
4123 emits the second for a 16-bit offset or add_got_offset_hilo emits
4124 a sequence to add a 32-bit offset using a scratch register. */
4125
4126 static void
4127 load_got_offset (int dest, expressionS *local)
4128 {
4129 expressionS global;
4130
4131 global = *local;
4132 global.X_add_number = 0;
4133
4134 relax_start (local->X_add_symbol);
4135 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4136 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4137 relax_switch ();
4138 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4139 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4140 relax_end ();
4141 }
4142
4143 static void
4144 add_got_offset (int dest, expressionS *local)
4145 {
4146 expressionS global;
4147
4148 global.X_op = O_constant;
4149 global.X_op_symbol = NULL;
4150 global.X_add_symbol = NULL;
4151 global.X_add_number = local->X_add_number;
4152
4153 relax_start (local->X_add_symbol);
4154 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4155 dest, dest, BFD_RELOC_LO16);
4156 relax_switch ();
4157 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4158 relax_end ();
4159 }
4160
4161 static void
4162 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4163 {
4164 expressionS global;
4165 int hold_mips_optimize;
4166
4167 global.X_op = O_constant;
4168 global.X_op_symbol = NULL;
4169 global.X_add_symbol = NULL;
4170 global.X_add_number = local->X_add_number;
4171
4172 relax_start (local->X_add_symbol);
4173 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4174 relax_switch ();
4175 /* Set mips_optimize around the lui instruction to avoid
4176 inserting an unnecessary nop after the lw. */
4177 hold_mips_optimize = mips_optimize;
4178 mips_optimize = 2;
4179 macro_build_lui (&global, tmp);
4180 mips_optimize = hold_mips_optimize;
4181 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4182 relax_end ();
4183
4184 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4185 }
4186
4187 /*
4188 * Build macros
4189 * This routine implements the seemingly endless macro or synthesized
4190 * instructions and addressing modes in the mips assembly language. Many
4191 * of these macros are simple and are similar to each other. These could
4192 * probably be handled by some kind of table or grammar approach instead of
4193 * this verbose method. Others are not simple macros but are more like
4194 * optimizing code generation.
4195 * One interesting optimization is when several store macros appear
4196 * consecutively that would load AT with the upper half of the same address.
4197 * The ensuing load upper instructions are ommited. This implies some kind
4198 * of global optimization. We currently only optimize within a single macro.
4199 * For many of the load and store macros if the address is specified as a
4200 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4201 * first load register 'at' with zero and use it as the base register. The
4202 * mips assembler simply uses register $zero. Just one tiny optimization
4203 * we're missing.
4204 */
4205 static void
4206 macro (struct mips_cl_insn *ip)
4207 {
4208 register int treg, sreg, dreg, breg;
4209 int tempreg;
4210 int mask;
4211 int used_at = 0;
4212 expressionS expr1;
4213 const char *s;
4214 const char *s2;
4215 const char *fmt;
4216 int likely = 0;
4217 int dbl = 0;
4218 int coproc = 0;
4219 int lr = 0;
4220 int imm = 0;
4221 int call = 0;
4222 int off;
4223 offsetT maxnum;
4224 bfd_reloc_code_real_type r;
4225 int hold_mips_optimize;
4226
4227 assert (! mips_opts.mips16);
4228
4229 treg = (ip->insn_opcode >> 16) & 0x1f;
4230 dreg = (ip->insn_opcode >> 11) & 0x1f;
4231 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4232 mask = ip->insn_mo->mask;
4233
4234 expr1.X_op = O_constant;
4235 expr1.X_op_symbol = NULL;
4236 expr1.X_add_symbol = NULL;
4237 expr1.X_add_number = 1;
4238
4239 switch (mask)
4240 {
4241 case M_DABS:
4242 dbl = 1;
4243 case M_ABS:
4244 /* bgez $a0,.+12
4245 move v0,$a0
4246 sub v0,$zero,$a0
4247 */
4248
4249 mips_emit_delays (TRUE);
4250 ++mips_opts.noreorder;
4251 mips_any_noreorder = 1;
4252
4253 expr1.X_add_number = 8;
4254 macro_build (&expr1, "bgez", "s,p", sreg);
4255 if (dreg == sreg)
4256 macro_build (NULL, "nop", "", 0);
4257 else
4258 move_register (dreg, sreg);
4259 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4260
4261 --mips_opts.noreorder;
4262 break;
4263
4264 case M_ADD_I:
4265 s = "addi";
4266 s2 = "add";
4267 goto do_addi;
4268 case M_ADDU_I:
4269 s = "addiu";
4270 s2 = "addu";
4271 goto do_addi;
4272 case M_DADD_I:
4273 dbl = 1;
4274 s = "daddi";
4275 s2 = "dadd";
4276 goto do_addi;
4277 case M_DADDU_I:
4278 dbl = 1;
4279 s = "daddiu";
4280 s2 = "daddu";
4281 do_addi:
4282 if (imm_expr.X_op == O_constant
4283 && imm_expr.X_add_number >= -0x8000
4284 && imm_expr.X_add_number < 0x8000)
4285 {
4286 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4287 break;
4288 }
4289 used_at = 1;
4290 load_register (AT, &imm_expr, dbl);
4291 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4292 break;
4293
4294 case M_AND_I:
4295 s = "andi";
4296 s2 = "and";
4297 goto do_bit;
4298 case M_OR_I:
4299 s = "ori";
4300 s2 = "or";
4301 goto do_bit;
4302 case M_NOR_I:
4303 s = "";
4304 s2 = "nor";
4305 goto do_bit;
4306 case M_XOR_I:
4307 s = "xori";
4308 s2 = "xor";
4309 do_bit:
4310 if (imm_expr.X_op == O_constant
4311 && imm_expr.X_add_number >= 0
4312 && imm_expr.X_add_number < 0x10000)
4313 {
4314 if (mask != M_NOR_I)
4315 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4316 else
4317 {
4318 macro_build (&imm_expr, "ori", "t,r,i",
4319 treg, sreg, BFD_RELOC_LO16);
4320 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4321 }
4322 break;
4323 }
4324
4325 used_at = 1;
4326 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4327 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4328 break;
4329
4330 case M_BEQ_I:
4331 s = "beq";
4332 goto beq_i;
4333 case M_BEQL_I:
4334 s = "beql";
4335 likely = 1;
4336 goto beq_i;
4337 case M_BNE_I:
4338 s = "bne";
4339 goto beq_i;
4340 case M_BNEL_I:
4341 s = "bnel";
4342 likely = 1;
4343 beq_i:
4344 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4345 {
4346 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4347 break;
4348 }
4349 used_at = 1;
4350 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4351 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4352 break;
4353
4354 case M_BGEL:
4355 likely = 1;
4356 case M_BGE:
4357 if (treg == 0)
4358 {
4359 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4360 break;
4361 }
4362 if (sreg == 0)
4363 {
4364 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4365 break;
4366 }
4367 used_at = 1;
4368 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4369 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4370 break;
4371
4372 case M_BGTL_I:
4373 likely = 1;
4374 case M_BGT_I:
4375 /* check for > max integer */
4376 maxnum = 0x7fffffff;
4377 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4378 {
4379 maxnum <<= 16;
4380 maxnum |= 0xffff;
4381 maxnum <<= 16;
4382 maxnum |= 0xffff;
4383 }
4384 if (imm_expr.X_op == O_constant
4385 && imm_expr.X_add_number >= maxnum
4386 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4387 {
4388 do_false:
4389 /* result is always false */
4390 if (! likely)
4391 macro_build (NULL, "nop", "", 0);
4392 else
4393 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4394 break;
4395 }
4396 if (imm_expr.X_op != O_constant)
4397 as_bad (_("Unsupported large constant"));
4398 ++imm_expr.X_add_number;
4399 /* FALLTHROUGH */
4400 case M_BGE_I:
4401 case M_BGEL_I:
4402 if (mask == M_BGEL_I)
4403 likely = 1;
4404 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4405 {
4406 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4407 break;
4408 }
4409 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4410 {
4411 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4412 break;
4413 }
4414 maxnum = 0x7fffffff;
4415 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4416 {
4417 maxnum <<= 16;
4418 maxnum |= 0xffff;
4419 maxnum <<= 16;
4420 maxnum |= 0xffff;
4421 }
4422 maxnum = - maxnum - 1;
4423 if (imm_expr.X_op == O_constant
4424 && imm_expr.X_add_number <= maxnum
4425 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4426 {
4427 do_true:
4428 /* result is always true */
4429 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4430 macro_build (&offset_expr, "b", "p");
4431 break;
4432 }
4433 used_at = 1;
4434 set_at (sreg, 0);
4435 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4436 break;
4437
4438 case M_BGEUL:
4439 likely = 1;
4440 case M_BGEU:
4441 if (treg == 0)
4442 goto do_true;
4443 if (sreg == 0)
4444 {
4445 macro_build (&offset_expr, likely ? "beql" : "beq",
4446 "s,t,p", 0, treg);
4447 break;
4448 }
4449 used_at = 1;
4450 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4451 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4452 break;
4453
4454 case M_BGTUL_I:
4455 likely = 1;
4456 case M_BGTU_I:
4457 if (sreg == 0
4458 || (HAVE_32BIT_GPRS
4459 && imm_expr.X_op == O_constant
4460 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4461 goto do_false;
4462 if (imm_expr.X_op != O_constant)
4463 as_bad (_("Unsupported large constant"));
4464 ++imm_expr.X_add_number;
4465 /* FALLTHROUGH */
4466 case M_BGEU_I:
4467 case M_BGEUL_I:
4468 if (mask == M_BGEUL_I)
4469 likely = 1;
4470 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4471 goto do_true;
4472 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4473 {
4474 macro_build (&offset_expr, likely ? "bnel" : "bne",
4475 "s,t,p", sreg, 0);
4476 break;
4477 }
4478 used_at = 1;
4479 set_at (sreg, 1);
4480 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4481 break;
4482
4483 case M_BGTL:
4484 likely = 1;
4485 case M_BGT:
4486 if (treg == 0)
4487 {
4488 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4489 break;
4490 }
4491 if (sreg == 0)
4492 {
4493 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4494 break;
4495 }
4496 used_at = 1;
4497 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4498 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4499 break;
4500
4501 case M_BGTUL:
4502 likely = 1;
4503 case M_BGTU:
4504 if (treg == 0)
4505 {
4506 macro_build (&offset_expr, likely ? "bnel" : "bne",
4507 "s,t,p", sreg, 0);
4508 break;
4509 }
4510 if (sreg == 0)
4511 goto do_false;
4512 used_at = 1;
4513 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4514 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4515 break;
4516
4517 case M_BLEL:
4518 likely = 1;
4519 case M_BLE:
4520 if (treg == 0)
4521 {
4522 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4523 break;
4524 }
4525 if (sreg == 0)
4526 {
4527 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4528 break;
4529 }
4530 used_at = 1;
4531 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4532 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4533 break;
4534
4535 case M_BLEL_I:
4536 likely = 1;
4537 case M_BLE_I:
4538 maxnum = 0x7fffffff;
4539 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4540 {
4541 maxnum <<= 16;
4542 maxnum |= 0xffff;
4543 maxnum <<= 16;
4544 maxnum |= 0xffff;
4545 }
4546 if (imm_expr.X_op == O_constant
4547 && imm_expr.X_add_number >= maxnum
4548 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4549 goto do_true;
4550 if (imm_expr.X_op != O_constant)
4551 as_bad (_("Unsupported large constant"));
4552 ++imm_expr.X_add_number;
4553 /* FALLTHROUGH */
4554 case M_BLT_I:
4555 case M_BLTL_I:
4556 if (mask == M_BLTL_I)
4557 likely = 1;
4558 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4559 {
4560 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4561 break;
4562 }
4563 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4564 {
4565 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4566 break;
4567 }
4568 used_at = 1;
4569 set_at (sreg, 0);
4570 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4571 break;
4572
4573 case M_BLEUL:
4574 likely = 1;
4575 case M_BLEU:
4576 if (treg == 0)
4577 {
4578 macro_build (&offset_expr, likely ? "beql" : "beq",
4579 "s,t,p", sreg, 0);
4580 break;
4581 }
4582 if (sreg == 0)
4583 goto do_true;
4584 used_at = 1;
4585 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4586 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4587 break;
4588
4589 case M_BLEUL_I:
4590 likely = 1;
4591 case M_BLEU_I:
4592 if (sreg == 0
4593 || (HAVE_32BIT_GPRS
4594 && imm_expr.X_op == O_constant
4595 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4596 goto do_true;
4597 if (imm_expr.X_op != O_constant)
4598 as_bad (_("Unsupported large constant"));
4599 ++imm_expr.X_add_number;
4600 /* FALLTHROUGH */
4601 case M_BLTU_I:
4602 case M_BLTUL_I:
4603 if (mask == M_BLTUL_I)
4604 likely = 1;
4605 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4606 goto do_false;
4607 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4608 {
4609 macro_build (&offset_expr, likely ? "beql" : "beq",
4610 "s,t,p", sreg, 0);
4611 break;
4612 }
4613 used_at = 1;
4614 set_at (sreg, 1);
4615 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4616 break;
4617
4618 case M_BLTL:
4619 likely = 1;
4620 case M_BLT:
4621 if (treg == 0)
4622 {
4623 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4624 break;
4625 }
4626 if (sreg == 0)
4627 {
4628 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4629 break;
4630 }
4631 used_at = 1;
4632 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4633 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4634 break;
4635
4636 case M_BLTUL:
4637 likely = 1;
4638 case M_BLTU:
4639 if (treg == 0)
4640 goto do_false;
4641 if (sreg == 0)
4642 {
4643 macro_build (&offset_expr, likely ? "bnel" : "bne",
4644 "s,t,p", 0, treg);
4645 break;
4646 }
4647 used_at = 1;
4648 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4649 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4650 break;
4651
4652 case M_DEXT:
4653 {
4654 unsigned long pos;
4655 unsigned long size;
4656
4657 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4658 {
4659 as_bad (_("Unsupported large constant"));
4660 pos = size = 1;
4661 }
4662 else
4663 {
4664 pos = (unsigned long) imm_expr.X_add_number;
4665 size = (unsigned long) imm2_expr.X_add_number;
4666 }
4667
4668 if (pos > 63)
4669 {
4670 as_bad (_("Improper position (%lu)"), pos);
4671 pos = 1;
4672 }
4673 if (size == 0 || size > 64
4674 || (pos + size - 1) > 63)
4675 {
4676 as_bad (_("Improper extract size (%lu, position %lu)"),
4677 size, pos);
4678 size = 1;
4679 }
4680
4681 if (size <= 32 && pos < 32)
4682 {
4683 s = "dext";
4684 fmt = "t,r,+A,+C";
4685 }
4686 else if (size <= 32)
4687 {
4688 s = "dextu";
4689 fmt = "t,r,+E,+H";
4690 }
4691 else
4692 {
4693 s = "dextm";
4694 fmt = "t,r,+A,+G";
4695 }
4696 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4697 }
4698 break;
4699
4700 case M_DINS:
4701 {
4702 unsigned long pos;
4703 unsigned long size;
4704
4705 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4706 {
4707 as_bad (_("Unsupported large constant"));
4708 pos = size = 1;
4709 }
4710 else
4711 {
4712 pos = (unsigned long) imm_expr.X_add_number;
4713 size = (unsigned long) imm2_expr.X_add_number;
4714 }
4715
4716 if (pos > 63)
4717 {
4718 as_bad (_("Improper position (%lu)"), pos);
4719 pos = 1;
4720 }
4721 if (size == 0 || size > 64
4722 || (pos + size - 1) > 63)
4723 {
4724 as_bad (_("Improper insert size (%lu, position %lu)"),
4725 size, pos);
4726 size = 1;
4727 }
4728
4729 if (pos < 32 && (pos + size - 1) < 32)
4730 {
4731 s = "dins";
4732 fmt = "t,r,+A,+B";
4733 }
4734 else if (pos >= 32)
4735 {
4736 s = "dinsu";
4737 fmt = "t,r,+E,+F";
4738 }
4739 else
4740 {
4741 s = "dinsm";
4742 fmt = "t,r,+A,+F";
4743 }
4744 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4745 pos + size - 1);
4746 }
4747 break;
4748
4749 case M_DDIV_3:
4750 dbl = 1;
4751 case M_DIV_3:
4752 s = "mflo";
4753 goto do_div3;
4754 case M_DREM_3:
4755 dbl = 1;
4756 case M_REM_3:
4757 s = "mfhi";
4758 do_div3:
4759 if (treg == 0)
4760 {
4761 as_warn (_("Divide by zero."));
4762 if (mips_trap)
4763 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4764 else
4765 macro_build (NULL, "break", "c", 7);
4766 break;
4767 }
4768
4769 mips_emit_delays (TRUE);
4770 ++mips_opts.noreorder;
4771 mips_any_noreorder = 1;
4772 if (mips_trap)
4773 {
4774 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4775 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4776 }
4777 else
4778 {
4779 expr1.X_add_number = 8;
4780 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4781 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4782 macro_build (NULL, "break", "c", 7);
4783 }
4784 expr1.X_add_number = -1;
4785 used_at = 1;
4786 load_register (AT, &expr1, dbl);
4787 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4788 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4789 if (dbl)
4790 {
4791 expr1.X_add_number = 1;
4792 load_register (AT, &expr1, dbl);
4793 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4794 }
4795 else
4796 {
4797 expr1.X_add_number = 0x80000000;
4798 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4799 }
4800 if (mips_trap)
4801 {
4802 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4803 /* We want to close the noreorder block as soon as possible, so
4804 that later insns are available for delay slot filling. */
4805 --mips_opts.noreorder;
4806 }
4807 else
4808 {
4809 expr1.X_add_number = 8;
4810 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4811 macro_build (NULL, "nop", "", 0);
4812
4813 /* We want to close the noreorder block as soon as possible, so
4814 that later insns are available for delay slot filling. */
4815 --mips_opts.noreorder;
4816
4817 macro_build (NULL, "break", "c", 6);
4818 }
4819 macro_build (NULL, s, "d", dreg);
4820 break;
4821
4822 case M_DIV_3I:
4823 s = "div";
4824 s2 = "mflo";
4825 goto do_divi;
4826 case M_DIVU_3I:
4827 s = "divu";
4828 s2 = "mflo";
4829 goto do_divi;
4830 case M_REM_3I:
4831 s = "div";
4832 s2 = "mfhi";
4833 goto do_divi;
4834 case M_REMU_3I:
4835 s = "divu";
4836 s2 = "mfhi";
4837 goto do_divi;
4838 case M_DDIV_3I:
4839 dbl = 1;
4840 s = "ddiv";
4841 s2 = "mflo";
4842 goto do_divi;
4843 case M_DDIVU_3I:
4844 dbl = 1;
4845 s = "ddivu";
4846 s2 = "mflo";
4847 goto do_divi;
4848 case M_DREM_3I:
4849 dbl = 1;
4850 s = "ddiv";
4851 s2 = "mfhi";
4852 goto do_divi;
4853 case M_DREMU_3I:
4854 dbl = 1;
4855 s = "ddivu";
4856 s2 = "mfhi";
4857 do_divi:
4858 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4859 {
4860 as_warn (_("Divide by zero."));
4861 if (mips_trap)
4862 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4863 else
4864 macro_build (NULL, "break", "c", 7);
4865 break;
4866 }
4867 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4868 {
4869 if (strcmp (s2, "mflo") == 0)
4870 move_register (dreg, sreg);
4871 else
4872 move_register (dreg, 0);
4873 break;
4874 }
4875 if (imm_expr.X_op == O_constant
4876 && imm_expr.X_add_number == -1
4877 && s[strlen (s) - 1] != 'u')
4878 {
4879 if (strcmp (s2, "mflo") == 0)
4880 {
4881 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4882 }
4883 else
4884 move_register (dreg, 0);
4885 break;
4886 }
4887
4888 used_at = 1;
4889 load_register (AT, &imm_expr, dbl);
4890 macro_build (NULL, s, "z,s,t", sreg, AT);
4891 macro_build (NULL, s2, "d", dreg);
4892 break;
4893
4894 case M_DIVU_3:
4895 s = "divu";
4896 s2 = "mflo";
4897 goto do_divu3;
4898 case M_REMU_3:
4899 s = "divu";
4900 s2 = "mfhi";
4901 goto do_divu3;
4902 case M_DDIVU_3:
4903 s = "ddivu";
4904 s2 = "mflo";
4905 goto do_divu3;
4906 case M_DREMU_3:
4907 s = "ddivu";
4908 s2 = "mfhi";
4909 do_divu3:
4910 mips_emit_delays (TRUE);
4911 ++mips_opts.noreorder;
4912 mips_any_noreorder = 1;
4913 if (mips_trap)
4914 {
4915 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4916 macro_build (NULL, s, "z,s,t", sreg, treg);
4917 /* We want to close the noreorder block as soon as possible, so
4918 that later insns are available for delay slot filling. */
4919 --mips_opts.noreorder;
4920 }
4921 else
4922 {
4923 expr1.X_add_number = 8;
4924 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4925 macro_build (NULL, s, "z,s,t", sreg, treg);
4926
4927 /* We want to close the noreorder block as soon as possible, so
4928 that later insns are available for delay slot filling. */
4929 --mips_opts.noreorder;
4930 macro_build (NULL, "break", "c", 7);
4931 }
4932 macro_build (NULL, s2, "d", dreg);
4933 break;
4934
4935 case M_DLCA_AB:
4936 dbl = 1;
4937 case M_LCA_AB:
4938 call = 1;
4939 goto do_la;
4940 case M_DLA_AB:
4941 dbl = 1;
4942 case M_LA_AB:
4943 do_la:
4944 /* Load the address of a symbol into a register. If breg is not
4945 zero, we then add a base register to it. */
4946
4947 if (dbl && HAVE_32BIT_GPRS)
4948 as_warn (_("dla used to load 32-bit register"));
4949
4950 if (! dbl && HAVE_64BIT_OBJECTS)
4951 as_warn (_("la used to load 64-bit address"));
4952
4953 if (offset_expr.X_op == O_constant
4954 && offset_expr.X_add_number >= -0x8000
4955 && offset_expr.X_add_number < 0x8000)
4956 {
4957 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4958 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4959 break;
4960 }
4961
4962 if (!mips_opts.noat && (treg == breg))
4963 {
4964 tempreg = AT;
4965 used_at = 1;
4966 }
4967 else
4968 {
4969 tempreg = treg;
4970 }
4971
4972 if (offset_expr.X_op != O_symbol
4973 && offset_expr.X_op != O_constant)
4974 {
4975 as_bad (_("expression too complex"));
4976 offset_expr.X_op = O_constant;
4977 }
4978
4979 if (offset_expr.X_op == O_constant)
4980 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4981 else if (mips_pic == NO_PIC)
4982 {
4983 /* If this is a reference to a GP relative symbol, we want
4984 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4985 Otherwise we want
4986 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4987 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4988 If we have a constant, we need two instructions anyhow,
4989 so we may as well always use the latter form.
4990
4991 With 64bit address space and a usable $at we want
4992 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4993 lui $at,<sym> (BFD_RELOC_HI16_S)
4994 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4995 daddiu $at,<sym> (BFD_RELOC_LO16)
4996 dsll32 $tempreg,0
4997 daddu $tempreg,$tempreg,$at
4998
4999 If $at is already in use, we use a path which is suboptimal
5000 on superscalar processors.
5001 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5002 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5003 dsll $tempreg,16
5004 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5005 dsll $tempreg,16
5006 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5007
5008 For GP relative symbols in 64bit address space we can use
5009 the same sequence as in 32bit address space. */
5010 if (HAVE_64BIT_SYMBOLS)
5011 {
5012 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5013 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5014 {
5015 relax_start (offset_expr.X_add_symbol);
5016 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5017 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5018 relax_switch ();
5019 }
5020
5021 if (used_at == 0 && !mips_opts.noat)
5022 {
5023 macro_build (&offset_expr, "lui", "t,u",
5024 tempreg, BFD_RELOC_MIPS_HIGHEST);
5025 macro_build (&offset_expr, "lui", "t,u",
5026 AT, BFD_RELOC_HI16_S);
5027 macro_build (&offset_expr, "daddiu", "t,r,j",
5028 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5029 macro_build (&offset_expr, "daddiu", "t,r,j",
5030 AT, AT, BFD_RELOC_LO16);
5031 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5032 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5033 used_at = 1;
5034 }
5035 else
5036 {
5037 macro_build (&offset_expr, "lui", "t,u",
5038 tempreg, BFD_RELOC_MIPS_HIGHEST);
5039 macro_build (&offset_expr, "daddiu", "t,r,j",
5040 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5041 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5042 macro_build (&offset_expr, "daddiu", "t,r,j",
5043 tempreg, tempreg, BFD_RELOC_HI16_S);
5044 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5045 macro_build (&offset_expr, "daddiu", "t,r,j",
5046 tempreg, tempreg, BFD_RELOC_LO16);
5047 }
5048
5049 if (mips_relax.sequence)
5050 relax_end ();
5051 }
5052 else
5053 {
5054 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5055 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5056 {
5057 relax_start (offset_expr.X_add_symbol);
5058 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5059 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5060 relax_switch ();
5061 }
5062 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5063 as_bad (_("offset too large"));
5064 macro_build_lui (&offset_expr, tempreg);
5065 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5066 tempreg, tempreg, BFD_RELOC_LO16);
5067 if (mips_relax.sequence)
5068 relax_end ();
5069 }
5070 }
5071 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5072 {
5073 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5074
5075 /* If this is a reference to an external symbol, and there
5076 is no constant, we want
5077 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5078 or for lca or if tempreg is PIC_CALL_REG
5079 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5080 For a local symbol, we want
5081 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5082 nop
5083 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5084
5085 If we have a small constant, and this is a reference to
5086 an external symbol, we want
5087 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5088 nop
5089 addiu $tempreg,$tempreg,<constant>
5090 For a local symbol, we want the same instruction
5091 sequence, but we output a BFD_RELOC_LO16 reloc on the
5092 addiu instruction.
5093
5094 If we have a large constant, and this is a reference to
5095 an external symbol, we want
5096 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5097 lui $at,<hiconstant>
5098 addiu $at,$at,<loconstant>
5099 addu $tempreg,$tempreg,$at
5100 For a local symbol, we want the same instruction
5101 sequence, but we output a BFD_RELOC_LO16 reloc on the
5102 addiu instruction.
5103 */
5104
5105 if (offset_expr.X_add_number == 0)
5106 {
5107 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5108 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5109
5110 relax_start (offset_expr.X_add_symbol);
5111 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5112 lw_reloc_type, mips_gp_register);
5113 if (breg != 0)
5114 {
5115 /* We're going to put in an addu instruction using
5116 tempreg, so we may as well insert the nop right
5117 now. */
5118 load_delay_nop ();
5119 }
5120 relax_switch ();
5121 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5122 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5123 load_delay_nop ();
5124 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5125 tempreg, tempreg, BFD_RELOC_LO16);
5126 relax_end ();
5127 /* FIXME: If breg == 0, and the next instruction uses
5128 $tempreg, then if this variant case is used an extra
5129 nop will be generated. */
5130 }
5131 else if (offset_expr.X_add_number >= -0x8000
5132 && offset_expr.X_add_number < 0x8000)
5133 {
5134 load_got_offset (tempreg, &offset_expr);
5135 load_delay_nop ();
5136 add_got_offset (tempreg, &offset_expr);
5137 }
5138 else
5139 {
5140 expr1.X_add_number = offset_expr.X_add_number;
5141 offset_expr.X_add_number =
5142 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5143 load_got_offset (tempreg, &offset_expr);
5144 offset_expr.X_add_number = expr1.X_add_number;
5145 /* If we are going to add in a base register, and the
5146 target register and the base register are the same,
5147 then we are using AT as a temporary register. Since
5148 we want to load the constant into AT, we add our
5149 current AT (from the global offset table) and the
5150 register into the register now, and pretend we were
5151 not using a base register. */
5152 if (breg == treg)
5153 {
5154 load_delay_nop ();
5155 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5156 treg, AT, breg);
5157 breg = 0;
5158 tempreg = treg;
5159 }
5160 add_got_offset_hilo (tempreg, &offset_expr, AT);
5161 used_at = 1;
5162 }
5163 }
5164 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5165 {
5166 int add_breg_early = 0;
5167
5168 /* If this is a reference to an external, and there is no
5169 constant, or local symbol (*), with or without a
5170 constant, we want
5171 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5172 or for lca or if tempreg is PIC_CALL_REG
5173 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5174
5175 If we have a small constant, and this is a reference to
5176 an external symbol, we want
5177 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5178 addiu $tempreg,$tempreg,<constant>
5179
5180 If we have a large constant, and this is a reference to
5181 an external symbol, we want
5182 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5183 lui $at,<hiconstant>
5184 addiu $at,$at,<loconstant>
5185 addu $tempreg,$tempreg,$at
5186
5187 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5188 local symbols, even though it introduces an additional
5189 instruction. */
5190
5191 if (offset_expr.X_add_number)
5192 {
5193 expr1.X_add_number = offset_expr.X_add_number;
5194 offset_expr.X_add_number = 0;
5195
5196 relax_start (offset_expr.X_add_symbol);
5197 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5198 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5199
5200 if (expr1.X_add_number >= -0x8000
5201 && expr1.X_add_number < 0x8000)
5202 {
5203 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5204 tempreg, tempreg, BFD_RELOC_LO16);
5205 }
5206 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5207 {
5208 int dreg;
5209
5210 /* If we are going to add in a base register, and the
5211 target register and the base register are the same,
5212 then we are using AT as a temporary register. Since
5213 we want to load the constant into AT, we add our
5214 current AT (from the global offset table) and the
5215 register into the register now, and pretend we were
5216 not using a base register. */
5217 if (breg != treg)
5218 dreg = tempreg;
5219 else
5220 {
5221 assert (tempreg == AT);
5222 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5223 treg, AT, breg);
5224 dreg = treg;
5225 add_breg_early = 1;
5226 }
5227
5228 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5229 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5230 dreg, dreg, AT);
5231
5232 used_at = 1;
5233 }
5234 else
5235 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5236
5237 relax_switch ();
5238 offset_expr.X_add_number = expr1.X_add_number;
5239
5240 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5241 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5242 if (add_breg_early)
5243 {
5244 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5245 treg, tempreg, breg);
5246 breg = 0;
5247 tempreg = treg;
5248 }
5249 relax_end ();
5250 }
5251 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5252 {
5253 relax_start (offset_expr.X_add_symbol);
5254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5255 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5256 relax_switch ();
5257 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5258 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5259 relax_end ();
5260 }
5261 else
5262 {
5263 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5264 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5265 }
5266 }
5267 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5268 {
5269 int gpdelay;
5270 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5271 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5272 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5273
5274 /* This is the large GOT case. If this is a reference to an
5275 external symbol, and there is no constant, we want
5276 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5277 addu $tempreg,$tempreg,$gp
5278 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5279 or for lca or if tempreg is PIC_CALL_REG
5280 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5281 addu $tempreg,$tempreg,$gp
5282 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5283 For a local symbol, we want
5284 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5285 nop
5286 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5287
5288 If we have a small constant, and this is a reference to
5289 an external symbol, we want
5290 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5291 addu $tempreg,$tempreg,$gp
5292 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5293 nop
5294 addiu $tempreg,$tempreg,<constant>
5295 For a local symbol, we want
5296 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5297 nop
5298 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5299
5300 If we have a large constant, and this is a reference to
5301 an external symbol, we want
5302 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5303 addu $tempreg,$tempreg,$gp
5304 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5305 lui $at,<hiconstant>
5306 addiu $at,$at,<loconstant>
5307 addu $tempreg,$tempreg,$at
5308 For a local symbol, we want
5309 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5310 lui $at,<hiconstant>
5311 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5312 addu $tempreg,$tempreg,$at
5313 */
5314
5315 expr1.X_add_number = offset_expr.X_add_number;
5316 offset_expr.X_add_number = 0;
5317 relax_start (offset_expr.X_add_symbol);
5318 gpdelay = reg_needs_delay (mips_gp_register);
5319 if (expr1.X_add_number == 0 && breg == 0
5320 && (call || tempreg == PIC_CALL_REG))
5321 {
5322 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5323 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5324 }
5325 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5326 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5327 tempreg, tempreg, mips_gp_register);
5328 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5329 tempreg, lw_reloc_type, tempreg);
5330 if (expr1.X_add_number == 0)
5331 {
5332 if (breg != 0)
5333 {
5334 /* We're going to put in an addu instruction using
5335 tempreg, so we may as well insert the nop right
5336 now. */
5337 load_delay_nop ();
5338 }
5339 }
5340 else if (expr1.X_add_number >= -0x8000
5341 && expr1.X_add_number < 0x8000)
5342 {
5343 load_delay_nop ();
5344 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5345 tempreg, tempreg, BFD_RELOC_LO16);
5346 }
5347 else
5348 {
5349 int dreg;
5350
5351 /* If we are going to add in a base register, and the
5352 target register and the base register are the same,
5353 then we are using AT as a temporary register. Since
5354 we want to load the constant into AT, we add our
5355 current AT (from the global offset table) and the
5356 register into the register now, and pretend we were
5357 not using a base register. */
5358 if (breg != treg)
5359 dreg = tempreg;
5360 else
5361 {
5362 assert (tempreg == AT);
5363 load_delay_nop ();
5364 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5365 treg, AT, breg);
5366 dreg = treg;
5367 }
5368
5369 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5370 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5371
5372 used_at = 1;
5373 }
5374 offset_expr.X_add_number =
5375 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5376 relax_switch ();
5377
5378 if (gpdelay)
5379 {
5380 /* This is needed because this instruction uses $gp, but
5381 the first instruction on the main stream does not. */
5382 macro_build (NULL, "nop", "");
5383 }
5384
5385 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5386 local_reloc_type, mips_gp_register);
5387 if (expr1.X_add_number >= -0x8000
5388 && expr1.X_add_number < 0x8000)
5389 {
5390 load_delay_nop ();
5391 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5392 tempreg, tempreg, BFD_RELOC_LO16);
5393 /* FIXME: If add_number is 0, and there was no base
5394 register, the external symbol case ended with a load,
5395 so if the symbol turns out to not be external, and
5396 the next instruction uses tempreg, an unnecessary nop
5397 will be inserted. */
5398 }
5399 else
5400 {
5401 if (breg == treg)
5402 {
5403 /* We must add in the base register now, as in the
5404 external symbol case. */
5405 assert (tempreg == AT);
5406 load_delay_nop ();
5407 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5408 treg, AT, breg);
5409 tempreg = treg;
5410 /* We set breg to 0 because we have arranged to add
5411 it in in both cases. */
5412 breg = 0;
5413 }
5414
5415 macro_build_lui (&expr1, AT);
5416 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5417 AT, AT, BFD_RELOC_LO16);
5418 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5419 tempreg, tempreg, AT);
5420 used_at = 1;
5421 }
5422 relax_end ();
5423 }
5424 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5425 {
5426 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5427 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5428 int add_breg_early = 0;
5429
5430 /* This is the large GOT case. If this is a reference to an
5431 external symbol, and there is no constant, we want
5432 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5433 add $tempreg,$tempreg,$gp
5434 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5435 or for lca or if tempreg is PIC_CALL_REG
5436 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5437 add $tempreg,$tempreg,$gp
5438 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5439
5440 If we have a small constant, and this is a reference to
5441 an external symbol, we want
5442 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5443 add $tempreg,$tempreg,$gp
5444 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5445 addi $tempreg,$tempreg,<constant>
5446
5447 If we have a large constant, and this is a reference to
5448 an external symbol, we want
5449 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5450 addu $tempreg,$tempreg,$gp
5451 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5452 lui $at,<hiconstant>
5453 addi $at,$at,<loconstant>
5454 add $tempreg,$tempreg,$at
5455
5456 If we have NewABI, and we know it's a local symbol, we want
5457 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5458 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5459 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5460
5461 relax_start (offset_expr.X_add_symbol);
5462
5463 expr1.X_add_number = offset_expr.X_add_number;
5464 offset_expr.X_add_number = 0;
5465
5466 if (expr1.X_add_number == 0 && breg == 0
5467 && (call || tempreg == PIC_CALL_REG))
5468 {
5469 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5470 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5471 }
5472 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5473 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5474 tempreg, tempreg, mips_gp_register);
5475 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5476 tempreg, lw_reloc_type, tempreg);
5477
5478 if (expr1.X_add_number == 0)
5479 ;
5480 else if (expr1.X_add_number >= -0x8000
5481 && expr1.X_add_number < 0x8000)
5482 {
5483 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5484 tempreg, tempreg, BFD_RELOC_LO16);
5485 }
5486 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5487 {
5488 int dreg;
5489
5490 /* If we are going to add in a base register, and the
5491 target register and the base register are the same,
5492 then we are using AT as a temporary register. Since
5493 we want to load the constant into AT, we add our
5494 current AT (from the global offset table) and the
5495 register into the register now, and pretend we were
5496 not using a base register. */
5497 if (breg != treg)
5498 dreg = tempreg;
5499 else
5500 {
5501 assert (tempreg == AT);
5502 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5503 treg, AT, breg);
5504 dreg = treg;
5505 add_breg_early = 1;
5506 }
5507
5508 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5509 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5510
5511 used_at = 1;
5512 }
5513 else
5514 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5515
5516 relax_switch ();
5517 offset_expr.X_add_number = expr1.X_add_number;
5518 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5519 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5520 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5521 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5522 if (add_breg_early)
5523 {
5524 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5525 treg, tempreg, breg);
5526 breg = 0;
5527 tempreg = treg;
5528 }
5529 relax_end ();
5530 }
5531 else
5532 abort ();
5533
5534 if (breg != 0)
5535 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5536 break;
5537
5538 case M_J_A:
5539 /* The j instruction may not be used in PIC code, since it
5540 requires an absolute address. We convert it to a b
5541 instruction. */
5542 if (mips_pic == NO_PIC)
5543 macro_build (&offset_expr, "j", "a");
5544 else
5545 macro_build (&offset_expr, "b", "p");
5546 break;
5547
5548 /* The jal instructions must be handled as macros because when
5549 generating PIC code they expand to multi-instruction
5550 sequences. Normally they are simple instructions. */
5551 case M_JAL_1:
5552 dreg = RA;
5553 /* Fall through. */
5554 case M_JAL_2:
5555 if (mips_pic == NO_PIC)
5556 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5557 else if (mips_pic == SVR4_PIC)
5558 {
5559 if (sreg != PIC_CALL_REG)
5560 as_warn (_("MIPS PIC call to register other than $25"));
5561
5562 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5563 if (! HAVE_NEWABI)
5564 {
5565 if (mips_cprestore_offset < 0)
5566 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5567 else
5568 {
5569 if (! mips_frame_reg_valid)
5570 {
5571 as_warn (_("No .frame pseudo-op used in PIC code"));
5572 /* Quiet this warning. */
5573 mips_frame_reg_valid = 1;
5574 }
5575 if (! mips_cprestore_valid)
5576 {
5577 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5578 /* Quiet this warning. */
5579 mips_cprestore_valid = 1;
5580 }
5581 expr1.X_add_number = mips_cprestore_offset;
5582 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5583 mips_gp_register,
5584 mips_frame_reg,
5585 HAVE_64BIT_ADDRESSES);
5586 }
5587 }
5588 }
5589 else
5590 abort ();
5591
5592 break;
5593
5594 case M_JAL_A:
5595 if (mips_pic == NO_PIC)
5596 macro_build (&offset_expr, "jal", "a");
5597 else if (mips_pic == SVR4_PIC)
5598 {
5599 /* If this is a reference to an external symbol, and we are
5600 using a small GOT, we want
5601 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5602 nop
5603 jalr $ra,$25
5604 nop
5605 lw $gp,cprestore($sp)
5606 The cprestore value is set using the .cprestore
5607 pseudo-op. If we are using a big GOT, we want
5608 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5609 addu $25,$25,$gp
5610 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5611 nop
5612 jalr $ra,$25
5613 nop
5614 lw $gp,cprestore($sp)
5615 If the symbol is not external, we want
5616 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5617 nop
5618 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5619 jalr $ra,$25
5620 nop
5621 lw $gp,cprestore($sp)
5622
5623 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5624 sequences above, minus nops, unless the symbol is local,
5625 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5626 GOT_DISP. */
5627 if (HAVE_NEWABI)
5628 {
5629 if (! mips_big_got)
5630 {
5631 relax_start (offset_expr.X_add_symbol);
5632 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5633 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5634 mips_gp_register);
5635 relax_switch ();
5636 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5637 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5638 mips_gp_register);
5639 relax_end ();
5640 }
5641 else
5642 {
5643 relax_start (offset_expr.X_add_symbol);
5644 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5645 BFD_RELOC_MIPS_CALL_HI16);
5646 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5647 PIC_CALL_REG, mips_gp_register);
5648 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5649 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5650 PIC_CALL_REG);
5651 relax_switch ();
5652 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5653 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5654 mips_gp_register);
5655 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5656 PIC_CALL_REG, PIC_CALL_REG,
5657 BFD_RELOC_MIPS_GOT_OFST);
5658 relax_end ();
5659 }
5660
5661 macro_build_jalr (&offset_expr);
5662 }
5663 else
5664 {
5665 relax_start (offset_expr.X_add_symbol);
5666 if (! mips_big_got)
5667 {
5668 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5669 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5670 mips_gp_register);
5671 load_delay_nop ();
5672 relax_switch ();
5673 }
5674 else
5675 {
5676 int gpdelay;
5677
5678 gpdelay = reg_needs_delay (mips_gp_register);
5679 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5680 BFD_RELOC_MIPS_CALL_HI16);
5681 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5682 PIC_CALL_REG, mips_gp_register);
5683 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5684 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5685 PIC_CALL_REG);
5686 load_delay_nop ();
5687 relax_switch ();
5688 if (gpdelay)
5689 macro_build (NULL, "nop", "");
5690 }
5691 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5692 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5693 mips_gp_register);
5694 load_delay_nop ();
5695 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5696 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5697 relax_end ();
5698 macro_build_jalr (&offset_expr);
5699
5700 if (mips_cprestore_offset < 0)
5701 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5702 else
5703 {
5704 if (! mips_frame_reg_valid)
5705 {
5706 as_warn (_("No .frame pseudo-op used in PIC code"));
5707 /* Quiet this warning. */
5708 mips_frame_reg_valid = 1;
5709 }
5710 if (! mips_cprestore_valid)
5711 {
5712 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5713 /* Quiet this warning. */
5714 mips_cprestore_valid = 1;
5715 }
5716 if (mips_opts.noreorder)
5717 macro_build (NULL, "nop", "");
5718 expr1.X_add_number = mips_cprestore_offset;
5719 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5720 mips_gp_register,
5721 mips_frame_reg,
5722 HAVE_64BIT_ADDRESSES);
5723 }
5724 }
5725 }
5726 else
5727 abort ();
5728
5729 break;
5730
5731 case M_LB_AB:
5732 s = "lb";
5733 goto ld;
5734 case M_LBU_AB:
5735 s = "lbu";
5736 goto ld;
5737 case M_LH_AB:
5738 s = "lh";
5739 goto ld;
5740 case M_LHU_AB:
5741 s = "lhu";
5742 goto ld;
5743 case M_LW_AB:
5744 s = "lw";
5745 goto ld;
5746 case M_LWC0_AB:
5747 s = "lwc0";
5748 /* Itbl support may require additional care here. */
5749 coproc = 1;
5750 goto ld;
5751 case M_LWC1_AB:
5752 s = "lwc1";
5753 /* Itbl support may require additional care here. */
5754 coproc = 1;
5755 goto ld;
5756 case M_LWC2_AB:
5757 s = "lwc2";
5758 /* Itbl support may require additional care here. */
5759 coproc = 1;
5760 goto ld;
5761 case M_LWC3_AB:
5762 s = "lwc3";
5763 /* Itbl support may require additional care here. */
5764 coproc = 1;
5765 goto ld;
5766 case M_LWL_AB:
5767 s = "lwl";
5768 lr = 1;
5769 goto ld;
5770 case M_LWR_AB:
5771 s = "lwr";
5772 lr = 1;
5773 goto ld;
5774 case M_LDC1_AB:
5775 if (mips_opts.arch == CPU_R4650)
5776 {
5777 as_bad (_("opcode not supported on this processor"));
5778 break;
5779 }
5780 s = "ldc1";
5781 /* Itbl support may require additional care here. */
5782 coproc = 1;
5783 goto ld;
5784 case M_LDC2_AB:
5785 s = "ldc2";
5786 /* Itbl support may require additional care here. */
5787 coproc = 1;
5788 goto ld;
5789 case M_LDC3_AB:
5790 s = "ldc3";
5791 /* Itbl support may require additional care here. */
5792 coproc = 1;
5793 goto ld;
5794 case M_LDL_AB:
5795 s = "ldl";
5796 lr = 1;
5797 goto ld;
5798 case M_LDR_AB:
5799 s = "ldr";
5800 lr = 1;
5801 goto ld;
5802 case M_LL_AB:
5803 s = "ll";
5804 goto ld;
5805 case M_LLD_AB:
5806 s = "lld";
5807 goto ld;
5808 case M_LWU_AB:
5809 s = "lwu";
5810 ld:
5811 if (breg == treg || coproc || lr)
5812 {
5813 tempreg = AT;
5814 used_at = 1;
5815 }
5816 else
5817 {
5818 tempreg = treg;
5819 }
5820 goto ld_st;
5821 case M_SB_AB:
5822 s = "sb";
5823 goto st;
5824 case M_SH_AB:
5825 s = "sh";
5826 goto st;
5827 case M_SW_AB:
5828 s = "sw";
5829 goto st;
5830 case M_SWC0_AB:
5831 s = "swc0";
5832 /* Itbl support may require additional care here. */
5833 coproc = 1;
5834 goto st;
5835 case M_SWC1_AB:
5836 s = "swc1";
5837 /* Itbl support may require additional care here. */
5838 coproc = 1;
5839 goto st;
5840 case M_SWC2_AB:
5841 s = "swc2";
5842 /* Itbl support may require additional care here. */
5843 coproc = 1;
5844 goto st;
5845 case M_SWC3_AB:
5846 s = "swc3";
5847 /* Itbl support may require additional care here. */
5848 coproc = 1;
5849 goto st;
5850 case M_SWL_AB:
5851 s = "swl";
5852 goto st;
5853 case M_SWR_AB:
5854 s = "swr";
5855 goto st;
5856 case M_SC_AB:
5857 s = "sc";
5858 goto st;
5859 case M_SCD_AB:
5860 s = "scd";
5861 goto st;
5862 case M_SDC1_AB:
5863 if (mips_opts.arch == CPU_R4650)
5864 {
5865 as_bad (_("opcode not supported on this processor"));
5866 break;
5867 }
5868 s = "sdc1";
5869 coproc = 1;
5870 /* Itbl support may require additional care here. */
5871 goto st;
5872 case M_SDC2_AB:
5873 s = "sdc2";
5874 /* Itbl support may require additional care here. */
5875 coproc = 1;
5876 goto st;
5877 case M_SDC3_AB:
5878 s = "sdc3";
5879 /* Itbl support may require additional care here. */
5880 coproc = 1;
5881 goto st;
5882 case M_SDL_AB:
5883 s = "sdl";
5884 goto st;
5885 case M_SDR_AB:
5886 s = "sdr";
5887 st:
5888 tempreg = AT;
5889 used_at = 1;
5890 ld_st:
5891 /* Itbl support may require additional care here. */
5892 if (mask == M_LWC1_AB
5893 || mask == M_SWC1_AB
5894 || mask == M_LDC1_AB
5895 || mask == M_SDC1_AB
5896 || mask == M_L_DAB
5897 || mask == M_S_DAB)
5898 fmt = "T,o(b)";
5899 else if (coproc)
5900 fmt = "E,o(b)";
5901 else
5902 fmt = "t,o(b)";
5903
5904 if (offset_expr.X_op != O_constant
5905 && offset_expr.X_op != O_symbol)
5906 {
5907 as_bad (_("expression too complex"));
5908 offset_expr.X_op = O_constant;
5909 }
5910
5911 /* A constant expression in PIC code can be handled just as it
5912 is in non PIC code. */
5913 if (offset_expr.X_op == O_constant)
5914 {
5915 if (HAVE_32BIT_ADDRESSES
5916 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5917 as_bad (_("constant too large"));
5918
5919 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5920 & ~(bfd_vma) 0xffff);
5921 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5922 if (breg != 0)
5923 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5924 tempreg, tempreg, breg);
5925 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5926 }
5927 else if (mips_pic == NO_PIC)
5928 {
5929 /* If this is a reference to a GP relative symbol, and there
5930 is no base register, we want
5931 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5932 Otherwise, if there is no base register, we want
5933 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5934 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5935 If we have a constant, we need two instructions anyhow,
5936 so we always use the latter form.
5937
5938 If we have a base register, and this is a reference to a
5939 GP relative symbol, we want
5940 addu $tempreg,$breg,$gp
5941 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5942 Otherwise we want
5943 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5944 addu $tempreg,$tempreg,$breg
5945 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5946 With a constant we always use the latter case.
5947
5948 With 64bit address space and no base register and $at usable,
5949 we want
5950 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5951 lui $at,<sym> (BFD_RELOC_HI16_S)
5952 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5953 dsll32 $tempreg,0
5954 daddu $tempreg,$at
5955 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5956 If we have a base register, we want
5957 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5958 lui $at,<sym> (BFD_RELOC_HI16_S)
5959 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5960 daddu $at,$breg
5961 dsll32 $tempreg,0
5962 daddu $tempreg,$at
5963 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5964
5965 Without $at we can't generate the optimal path for superscalar
5966 processors here since this would require two temporary registers.
5967 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5968 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5969 dsll $tempreg,16
5970 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5971 dsll $tempreg,16
5972 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5973 If we have a base register, we want
5974 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5975 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5976 dsll $tempreg,16
5977 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5978 dsll $tempreg,16
5979 daddu $tempreg,$tempreg,$breg
5980 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5981
5982 For GP relative symbols in 64bit address space we can use
5983 the same sequence as in 32bit address space. */
5984 if (HAVE_64BIT_SYMBOLS)
5985 {
5986 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5987 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5988 {
5989 relax_start (offset_expr.X_add_symbol);
5990 if (breg == 0)
5991 {
5992 macro_build (&offset_expr, s, fmt, treg,
5993 BFD_RELOC_GPREL16, mips_gp_register);
5994 }
5995 else
5996 {
5997 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5998 tempreg, breg, mips_gp_register);
5999 macro_build (&offset_expr, s, fmt, treg,
6000 BFD_RELOC_GPREL16, tempreg);
6001 }
6002 relax_switch ();
6003 }
6004
6005 if (used_at == 0 && !mips_opts.noat)
6006 {
6007 macro_build (&offset_expr, "lui", "t,u", tempreg,
6008 BFD_RELOC_MIPS_HIGHEST);
6009 macro_build (&offset_expr, "lui", "t,u", AT,
6010 BFD_RELOC_HI16_S);
6011 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6012 tempreg, BFD_RELOC_MIPS_HIGHER);
6013 if (breg != 0)
6014 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6015 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6016 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6017 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6018 tempreg);
6019 used_at = 1;
6020 }
6021 else
6022 {
6023 macro_build (&offset_expr, "lui", "t,u", tempreg,
6024 BFD_RELOC_MIPS_HIGHEST);
6025 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6026 tempreg, BFD_RELOC_MIPS_HIGHER);
6027 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6028 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6029 tempreg, BFD_RELOC_HI16_S);
6030 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6031 if (breg != 0)
6032 macro_build (NULL, "daddu", "d,v,t",
6033 tempreg, tempreg, breg);
6034 macro_build (&offset_expr, s, fmt, treg,
6035 BFD_RELOC_LO16, tempreg);
6036 }
6037
6038 if (mips_relax.sequence)
6039 relax_end ();
6040 break;
6041 }
6042
6043 if (breg == 0)
6044 {
6045 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6046 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6047 {
6048 relax_start (offset_expr.X_add_symbol);
6049 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6050 mips_gp_register);
6051 relax_switch ();
6052 }
6053 macro_build_lui (&offset_expr, tempreg);
6054 macro_build (&offset_expr, s, fmt, treg,
6055 BFD_RELOC_LO16, tempreg);
6056 if (mips_relax.sequence)
6057 relax_end ();
6058 }
6059 else
6060 {
6061 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6062 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6063 {
6064 relax_start (offset_expr.X_add_symbol);
6065 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6066 tempreg, breg, mips_gp_register);
6067 macro_build (&offset_expr, s, fmt, treg,
6068 BFD_RELOC_GPREL16, tempreg);
6069 relax_switch ();
6070 }
6071 macro_build_lui (&offset_expr, tempreg);
6072 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6073 tempreg, tempreg, breg);
6074 macro_build (&offset_expr, s, fmt, treg,
6075 BFD_RELOC_LO16, tempreg);
6076 if (mips_relax.sequence)
6077 relax_end ();
6078 }
6079 }
6080 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6081 {
6082 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6083
6084 /* If this is a reference to an external symbol, we want
6085 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6086 nop
6087 <op> $treg,0($tempreg)
6088 Otherwise we want
6089 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6090 nop
6091 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6092 <op> $treg,0($tempreg)
6093
6094 For NewABI, we want
6095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6096 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6097
6098 If there is a base register, we add it to $tempreg before
6099 the <op>. If there is a constant, we stick it in the
6100 <op> instruction. We don't handle constants larger than
6101 16 bits, because we have no way to load the upper 16 bits
6102 (actually, we could handle them for the subset of cases
6103 in which we are not using $at). */
6104 assert (offset_expr.X_op == O_symbol);
6105 if (HAVE_NEWABI)
6106 {
6107 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6108 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6109 if (breg != 0)
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_MIPS_GOT_OFST, tempreg);
6114 break;
6115 }
6116 expr1.X_add_number = offset_expr.X_add_number;
6117 offset_expr.X_add_number = 0;
6118 if (expr1.X_add_number < -0x8000
6119 || expr1.X_add_number >= 0x8000)
6120 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6121 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6122 lw_reloc_type, mips_gp_register);
6123 load_delay_nop ();
6124 relax_start (offset_expr.X_add_symbol);
6125 relax_switch ();
6126 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6127 tempreg, BFD_RELOC_LO16);
6128 relax_end ();
6129 if (breg != 0)
6130 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6131 tempreg, tempreg, breg);
6132 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6133 }
6134 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6135 {
6136 int gpdelay;
6137
6138 /* If this is a reference to an external symbol, we want
6139 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6140 addu $tempreg,$tempreg,$gp
6141 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6142 <op> $treg,0($tempreg)
6143 Otherwise we want
6144 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6145 nop
6146 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6147 <op> $treg,0($tempreg)
6148 If there is a base register, we add it to $tempreg before
6149 the <op>. If there is a constant, we stick it in the
6150 <op> instruction. We don't handle constants larger than
6151 16 bits, because we have no way to load the upper 16 bits
6152 (actually, we could handle them for the subset of cases
6153 in which we are not using $at). */
6154 assert (offset_expr.X_op == O_symbol);
6155 expr1.X_add_number = offset_expr.X_add_number;
6156 offset_expr.X_add_number = 0;
6157 if (expr1.X_add_number < -0x8000
6158 || expr1.X_add_number >= 0x8000)
6159 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6160 gpdelay = reg_needs_delay (mips_gp_register);
6161 relax_start (offset_expr.X_add_symbol);
6162 macro_build (&offset_expr, "lui", "t,u", tempreg,
6163 BFD_RELOC_MIPS_GOT_HI16);
6164 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6165 mips_gp_register);
6166 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6167 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6168 relax_switch ();
6169 if (gpdelay)
6170 macro_build (NULL, "nop", "");
6171 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6172 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6173 load_delay_nop ();
6174 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6175 tempreg, BFD_RELOC_LO16);
6176 relax_end ();
6177
6178 if (breg != 0)
6179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6180 tempreg, tempreg, breg);
6181 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6182 }
6183 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6184 {
6185 /* If this is a reference to an external symbol, we want
6186 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6187 add $tempreg,$tempreg,$gp
6188 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6189 <op> $treg,<ofst>($tempreg)
6190 Otherwise, for local symbols, we want:
6191 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6192 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6193 assert (offset_expr.X_op == O_symbol);
6194 expr1.X_add_number = offset_expr.X_add_number;
6195 offset_expr.X_add_number = 0;
6196 if (expr1.X_add_number < -0x8000
6197 || expr1.X_add_number >= 0x8000)
6198 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6199 relax_start (offset_expr.X_add_symbol);
6200 macro_build (&offset_expr, "lui", "t,u", tempreg,
6201 BFD_RELOC_MIPS_GOT_HI16);
6202 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6203 mips_gp_register);
6204 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6205 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6206 if (breg != 0)
6207 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6208 tempreg, tempreg, breg);
6209 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6210
6211 relax_switch ();
6212 offset_expr.X_add_number = expr1.X_add_number;
6213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6214 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6215 if (breg != 0)
6216 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6217 tempreg, tempreg, breg);
6218 macro_build (&offset_expr, s, fmt, treg,
6219 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6220 relax_end ();
6221 }
6222 else
6223 abort ();
6224
6225 break;
6226
6227 case M_LI:
6228 case M_LI_S:
6229 load_register (treg, &imm_expr, 0);
6230 break;
6231
6232 case M_DLI:
6233 load_register (treg, &imm_expr, 1);
6234 break;
6235
6236 case M_LI_SS:
6237 if (imm_expr.X_op == O_constant)
6238 {
6239 used_at = 1;
6240 load_register (AT, &imm_expr, 0);
6241 macro_build (NULL, "mtc1", "t,G", AT, treg);
6242 break;
6243 }
6244 else
6245 {
6246 assert (offset_expr.X_op == O_symbol
6247 && strcmp (segment_name (S_GET_SEGMENT
6248 (offset_expr.X_add_symbol)),
6249 ".lit4") == 0
6250 && offset_expr.X_add_number == 0);
6251 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6252 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6253 break;
6254 }
6255
6256 case M_LI_D:
6257 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6258 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6259 order 32 bits of the value and the low order 32 bits are either
6260 zero or in OFFSET_EXPR. */
6261 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6262 {
6263 if (HAVE_64BIT_GPRS)
6264 load_register (treg, &imm_expr, 1);
6265 else
6266 {
6267 int hreg, lreg;
6268
6269 if (target_big_endian)
6270 {
6271 hreg = treg;
6272 lreg = treg + 1;
6273 }
6274 else
6275 {
6276 hreg = treg + 1;
6277 lreg = treg;
6278 }
6279
6280 if (hreg <= 31)
6281 load_register (hreg, &imm_expr, 0);
6282 if (lreg <= 31)
6283 {
6284 if (offset_expr.X_op == O_absent)
6285 move_register (lreg, 0);
6286 else
6287 {
6288 assert (offset_expr.X_op == O_constant);
6289 load_register (lreg, &offset_expr, 0);
6290 }
6291 }
6292 }
6293 break;
6294 }
6295
6296 /* We know that sym is in the .rdata section. First we get the
6297 upper 16 bits of the address. */
6298 if (mips_pic == NO_PIC)
6299 {
6300 macro_build_lui (&offset_expr, AT);
6301 used_at = 1;
6302 }
6303 else if (mips_pic == SVR4_PIC)
6304 {
6305 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6306 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6307 used_at = 1;
6308 }
6309 else
6310 abort ();
6311
6312 /* Now we load the register(s). */
6313 if (HAVE_64BIT_GPRS)
6314 {
6315 used_at = 1;
6316 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6317 }
6318 else
6319 {
6320 used_at = 1;
6321 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6322 if (treg != RA)
6323 {
6324 /* FIXME: How in the world do we deal with the possible
6325 overflow here? */
6326 offset_expr.X_add_number += 4;
6327 macro_build (&offset_expr, "lw", "t,o(b)",
6328 treg + 1, BFD_RELOC_LO16, AT);
6329 }
6330 }
6331 break;
6332
6333 case M_LI_DD:
6334 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6335 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6336 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6337 the value and the low order 32 bits are either zero or in
6338 OFFSET_EXPR. */
6339 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6340 {
6341 used_at = 1;
6342 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6343 if (HAVE_64BIT_FPRS)
6344 {
6345 assert (HAVE_64BIT_GPRS);
6346 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6347 }
6348 else
6349 {
6350 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6351 if (offset_expr.X_op == O_absent)
6352 macro_build (NULL, "mtc1", "t,G", 0, treg);
6353 else
6354 {
6355 assert (offset_expr.X_op == O_constant);
6356 load_register (AT, &offset_expr, 0);
6357 macro_build (NULL, "mtc1", "t,G", AT, treg);
6358 }
6359 }
6360 break;
6361 }
6362
6363 assert (offset_expr.X_op == O_symbol
6364 && offset_expr.X_add_number == 0);
6365 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6366 if (strcmp (s, ".lit8") == 0)
6367 {
6368 if (mips_opts.isa != ISA_MIPS1)
6369 {
6370 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6371 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6372 break;
6373 }
6374 breg = mips_gp_register;
6375 r = BFD_RELOC_MIPS_LITERAL;
6376 goto dob;
6377 }
6378 else
6379 {
6380 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6381 used_at = 1;
6382 if (mips_pic == SVR4_PIC)
6383 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6384 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6385 else
6386 {
6387 /* FIXME: This won't work for a 64 bit address. */
6388 macro_build_lui (&offset_expr, AT);
6389 }
6390
6391 if (mips_opts.isa != ISA_MIPS1)
6392 {
6393 macro_build (&offset_expr, "ldc1", "T,o(b)",
6394 treg, BFD_RELOC_LO16, AT);
6395 break;
6396 }
6397 breg = AT;
6398 r = BFD_RELOC_LO16;
6399 goto dob;
6400 }
6401
6402 case M_L_DOB:
6403 if (mips_opts.arch == CPU_R4650)
6404 {
6405 as_bad (_("opcode not supported on this processor"));
6406 break;
6407 }
6408 /* Even on a big endian machine $fn comes before $fn+1. We have
6409 to adjust when loading from memory. */
6410 r = BFD_RELOC_LO16;
6411 dob:
6412 assert (mips_opts.isa == ISA_MIPS1);
6413 macro_build (&offset_expr, "lwc1", "T,o(b)",
6414 target_big_endian ? treg + 1 : treg, r, breg);
6415 /* FIXME: A possible overflow which I don't know how to deal
6416 with. */
6417 offset_expr.X_add_number += 4;
6418 macro_build (&offset_expr, "lwc1", "T,o(b)",
6419 target_big_endian ? treg : treg + 1, r, breg);
6420 break;
6421
6422 case M_L_DAB:
6423 /*
6424 * The MIPS assembler seems to check for X_add_number not
6425 * being double aligned and generating:
6426 * lui at,%hi(foo+1)
6427 * addu at,at,v1
6428 * addiu at,at,%lo(foo+1)
6429 * lwc1 f2,0(at)
6430 * lwc1 f3,4(at)
6431 * But, the resulting address is the same after relocation so why
6432 * generate the extra instruction?
6433 */
6434 if (mips_opts.arch == CPU_R4650)
6435 {
6436 as_bad (_("opcode not supported on this processor"));
6437 break;
6438 }
6439 /* Itbl support may require additional care here. */
6440 coproc = 1;
6441 if (mips_opts.isa != ISA_MIPS1)
6442 {
6443 s = "ldc1";
6444 goto ld;
6445 }
6446
6447 s = "lwc1";
6448 fmt = "T,o(b)";
6449 goto ldd_std;
6450
6451 case M_S_DAB:
6452 if (mips_opts.arch == CPU_R4650)
6453 {
6454 as_bad (_("opcode not supported on this processor"));
6455 break;
6456 }
6457
6458 if (mips_opts.isa != ISA_MIPS1)
6459 {
6460 s = "sdc1";
6461 goto st;
6462 }
6463
6464 s = "swc1";
6465 fmt = "T,o(b)";
6466 /* Itbl support may require additional care here. */
6467 coproc = 1;
6468 goto ldd_std;
6469
6470 case M_LD_AB:
6471 if (HAVE_64BIT_GPRS)
6472 {
6473 s = "ld";
6474 goto ld;
6475 }
6476
6477 s = "lw";
6478 fmt = "t,o(b)";
6479 goto ldd_std;
6480
6481 case M_SD_AB:
6482 if (HAVE_64BIT_GPRS)
6483 {
6484 s = "sd";
6485 goto st;
6486 }
6487
6488 s = "sw";
6489 fmt = "t,o(b)";
6490
6491 ldd_std:
6492 if (offset_expr.X_op != O_symbol
6493 && offset_expr.X_op != O_constant)
6494 {
6495 as_bad (_("expression too complex"));
6496 offset_expr.X_op = O_constant;
6497 }
6498
6499 /* Even on a big endian machine $fn comes before $fn+1. We have
6500 to adjust when loading from memory. We set coproc if we must
6501 load $fn+1 first. */
6502 /* Itbl support may require additional care here. */
6503 if (! target_big_endian)
6504 coproc = 0;
6505
6506 if (mips_pic == NO_PIC
6507 || offset_expr.X_op == O_constant)
6508 {
6509 /* If this is a reference to a GP relative symbol, we want
6510 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6511 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6512 If we have a base register, we use this
6513 addu $at,$breg,$gp
6514 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6515 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6516 If this is not a GP relative symbol, we want
6517 lui $at,<sym> (BFD_RELOC_HI16_S)
6518 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6519 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6520 If there is a base register, we add it to $at after the
6521 lui instruction. If there is a constant, we always use
6522 the last case. */
6523 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6524 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6525 {
6526 relax_start (offset_expr.X_add_symbol);
6527 if (breg == 0)
6528 {
6529 tempreg = mips_gp_register;
6530 }
6531 else
6532 {
6533 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6534 AT, breg, mips_gp_register);
6535 tempreg = AT;
6536 used_at = 1;
6537 }
6538
6539 /* Itbl support may require additional care here. */
6540 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6541 BFD_RELOC_GPREL16, tempreg);
6542 offset_expr.X_add_number += 4;
6543
6544 /* Set mips_optimize to 2 to avoid inserting an
6545 undesired nop. */
6546 hold_mips_optimize = mips_optimize;
6547 mips_optimize = 2;
6548 /* Itbl support may require additional care here. */
6549 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6550 BFD_RELOC_GPREL16, tempreg);
6551 mips_optimize = hold_mips_optimize;
6552
6553 relax_switch ();
6554
6555 /* We just generated two relocs. When tc_gen_reloc
6556 handles this case, it will skip the first reloc and
6557 handle the second. The second reloc already has an
6558 extra addend of 4, which we added above. We must
6559 subtract it out, and then subtract another 4 to make
6560 the first reloc come out right. The second reloc
6561 will come out right because we are going to add 4 to
6562 offset_expr when we build its instruction below.
6563
6564 If we have a symbol, then we don't want to include
6565 the offset, because it will wind up being included
6566 when we generate the reloc. */
6567
6568 if (offset_expr.X_op == O_constant)
6569 offset_expr.X_add_number -= 8;
6570 else
6571 {
6572 offset_expr.X_add_number = -4;
6573 offset_expr.X_op = O_constant;
6574 }
6575 }
6576 used_at = 1;
6577 macro_build_lui (&offset_expr, AT);
6578 if (breg != 0)
6579 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6580 /* Itbl support may require additional care here. */
6581 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6582 BFD_RELOC_LO16, AT);
6583 /* FIXME: How do we handle overflow here? */
6584 offset_expr.X_add_number += 4;
6585 /* Itbl support may require additional care here. */
6586 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6587 BFD_RELOC_LO16, AT);
6588 if (mips_relax.sequence)
6589 relax_end ();
6590 }
6591 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6592 {
6593 /* If this is a reference to an external symbol, we want
6594 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6595 nop
6596 <op> $treg,0($at)
6597 <op> $treg+1,4($at)
6598 Otherwise we want
6599 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6600 nop
6601 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6602 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6603 If there is a base register we add it to $at before the
6604 lwc1 instructions. If there is a constant we include it
6605 in the lwc1 instructions. */
6606 used_at = 1;
6607 expr1.X_add_number = offset_expr.X_add_number;
6608 if (expr1.X_add_number < -0x8000
6609 || expr1.X_add_number >= 0x8000 - 4)
6610 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6611 load_got_offset (AT, &offset_expr);
6612 load_delay_nop ();
6613 if (breg != 0)
6614 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6615
6616 /* Set mips_optimize to 2 to avoid inserting an undesired
6617 nop. */
6618 hold_mips_optimize = mips_optimize;
6619 mips_optimize = 2;
6620
6621 /* Itbl support may require additional care here. */
6622 relax_start (offset_expr.X_add_symbol);
6623 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6624 BFD_RELOC_LO16, AT);
6625 expr1.X_add_number += 4;
6626 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6627 BFD_RELOC_LO16, AT);
6628 relax_switch ();
6629 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6630 BFD_RELOC_LO16, AT);
6631 offset_expr.X_add_number += 4;
6632 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6633 BFD_RELOC_LO16, AT);
6634 relax_end ();
6635
6636 mips_optimize = hold_mips_optimize;
6637 }
6638 else if (mips_pic == SVR4_PIC)
6639 {
6640 int gpdelay;
6641
6642 /* If this is a reference to an external symbol, we want
6643 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6644 addu $at,$at,$gp
6645 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6646 nop
6647 <op> $treg,0($at)
6648 <op> $treg+1,4($at)
6649 Otherwise we want
6650 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6651 nop
6652 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6653 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6654 If there is a base register we add it to $at before the
6655 lwc1 instructions. If there is a constant we include it
6656 in the lwc1 instructions. */
6657 used_at = 1;
6658 expr1.X_add_number = offset_expr.X_add_number;
6659 offset_expr.X_add_number = 0;
6660 if (expr1.X_add_number < -0x8000
6661 || expr1.X_add_number >= 0x8000 - 4)
6662 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6663 gpdelay = reg_needs_delay (mips_gp_register);
6664 relax_start (offset_expr.X_add_symbol);
6665 macro_build (&offset_expr, "lui", "t,u",
6666 AT, BFD_RELOC_MIPS_GOT_HI16);
6667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6668 AT, AT, mips_gp_register);
6669 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6670 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6671 load_delay_nop ();
6672 if (breg != 0)
6673 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6674 /* Itbl support may require additional care here. */
6675 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6676 BFD_RELOC_LO16, AT);
6677 expr1.X_add_number += 4;
6678
6679 /* Set mips_optimize to 2 to avoid inserting an undesired
6680 nop. */
6681 hold_mips_optimize = mips_optimize;
6682 mips_optimize = 2;
6683 /* Itbl support may require additional care here. */
6684 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6685 BFD_RELOC_LO16, AT);
6686 mips_optimize = hold_mips_optimize;
6687 expr1.X_add_number -= 4;
6688
6689 relax_switch ();
6690 offset_expr.X_add_number = expr1.X_add_number;
6691 if (gpdelay)
6692 macro_build (NULL, "nop", "");
6693 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6694 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6695 load_delay_nop ();
6696 if (breg != 0)
6697 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6698 /* Itbl support may require additional care here. */
6699 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6700 BFD_RELOC_LO16, AT);
6701 offset_expr.X_add_number += 4;
6702
6703 /* Set mips_optimize to 2 to avoid inserting an undesired
6704 nop. */
6705 hold_mips_optimize = mips_optimize;
6706 mips_optimize = 2;
6707 /* Itbl support may require additional care here. */
6708 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6709 BFD_RELOC_LO16, AT);
6710 mips_optimize = hold_mips_optimize;
6711 relax_end ();
6712 }
6713 else
6714 abort ();
6715
6716 break;
6717
6718 case M_LD_OB:
6719 s = "lw";
6720 goto sd_ob;
6721 case M_SD_OB:
6722 s = "sw";
6723 sd_ob:
6724 assert (HAVE_32BIT_ADDRESSES);
6725 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6726 offset_expr.X_add_number += 4;
6727 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6728 break;
6729
6730 /* New code added to support COPZ instructions.
6731 This code builds table entries out of the macros in mip_opcodes.
6732 R4000 uses interlocks to handle coproc delays.
6733 Other chips (like the R3000) require nops to be inserted for delays.
6734
6735 FIXME: Currently, we require that the user handle delays.
6736 In order to fill delay slots for non-interlocked chips,
6737 we must have a way to specify delays based on the coprocessor.
6738 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6739 What are the side-effects of the cop instruction?
6740 What cache support might we have and what are its effects?
6741 Both coprocessor & memory require delays. how long???
6742 What registers are read/set/modified?
6743
6744 If an itbl is provided to interpret cop instructions,
6745 this knowledge can be encoded in the itbl spec. */
6746
6747 case M_COP0:
6748 s = "c0";
6749 goto copz;
6750 case M_COP1:
6751 s = "c1";
6752 goto copz;
6753 case M_COP2:
6754 s = "c2";
6755 goto copz;
6756 case M_COP3:
6757 s = "c3";
6758 copz:
6759 /* For now we just do C (same as Cz). The parameter will be
6760 stored in insn_opcode by mips_ip. */
6761 macro_build (NULL, s, "C", ip->insn_opcode);
6762 break;
6763
6764 case M_MOVE:
6765 move_register (dreg, sreg);
6766 break;
6767
6768 #ifdef LOSING_COMPILER
6769 default:
6770 /* Try and see if this is a new itbl instruction.
6771 This code builds table entries out of the macros in mip_opcodes.
6772 FIXME: For now we just assemble the expression and pass it's
6773 value along as a 32-bit immediate.
6774 We may want to have the assembler assemble this value,
6775 so that we gain the assembler's knowledge of delay slots,
6776 symbols, etc.
6777 Would it be more efficient to use mask (id) here? */
6778 if (itbl_have_entries
6779 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6780 {
6781 s = ip->insn_mo->name;
6782 s2 = "cop3";
6783 coproc = ITBL_DECODE_PNUM (immed_expr);;
6784 macro_build (&immed_expr, s, "C");
6785 break;
6786 }
6787 macro2 (ip);
6788 break;
6789 }
6790 if (mips_opts.noat && used_at)
6791 as_bad (_("Macro used $at after \".set noat\""));
6792 }
6793
6794 static void
6795 macro2 (struct mips_cl_insn *ip)
6796 {
6797 register int treg, sreg, dreg, breg;
6798 int tempreg;
6799 int mask;
6800 int used_at;
6801 expressionS expr1;
6802 const char *s;
6803 const char *s2;
6804 const char *fmt;
6805 int likely = 0;
6806 int dbl = 0;
6807 int coproc = 0;
6808 int lr = 0;
6809 int imm = 0;
6810 int off;
6811 offsetT maxnum;
6812 bfd_reloc_code_real_type r;
6813
6814 treg = (ip->insn_opcode >> 16) & 0x1f;
6815 dreg = (ip->insn_opcode >> 11) & 0x1f;
6816 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6817 mask = ip->insn_mo->mask;
6818
6819 expr1.X_op = O_constant;
6820 expr1.X_op_symbol = NULL;
6821 expr1.X_add_symbol = NULL;
6822 expr1.X_add_number = 1;
6823
6824 switch (mask)
6825 {
6826 #endif /* LOSING_COMPILER */
6827
6828 case M_DMUL:
6829 dbl = 1;
6830 case M_MUL:
6831 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6832 macro_build (NULL, "mflo", "d", dreg);
6833 break;
6834
6835 case M_DMUL_I:
6836 dbl = 1;
6837 case M_MUL_I:
6838 /* The MIPS assembler some times generates shifts and adds. I'm
6839 not trying to be that fancy. GCC should do this for us
6840 anyway. */
6841 used_at = 1;
6842 load_register (AT, &imm_expr, dbl);
6843 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6844 macro_build (NULL, "mflo", "d", dreg);
6845 break;
6846
6847 case M_DMULO_I:
6848 dbl = 1;
6849 case M_MULO_I:
6850 imm = 1;
6851 goto do_mulo;
6852
6853 case M_DMULO:
6854 dbl = 1;
6855 case M_MULO:
6856 do_mulo:
6857 mips_emit_delays (TRUE);
6858 ++mips_opts.noreorder;
6859 mips_any_noreorder = 1;
6860 used_at = 1;
6861 if (imm)
6862 load_register (AT, &imm_expr, dbl);
6863 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6864 macro_build (NULL, "mflo", "d", dreg);
6865 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6866 macro_build (NULL, "mfhi", "d", AT);
6867 if (mips_trap)
6868 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6869 else
6870 {
6871 expr1.X_add_number = 8;
6872 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6873 macro_build (NULL, "nop", "", 0);
6874 macro_build (NULL, "break", "c", 6);
6875 }
6876 --mips_opts.noreorder;
6877 macro_build (NULL, "mflo", "d", dreg);
6878 break;
6879
6880 case M_DMULOU_I:
6881 dbl = 1;
6882 case M_MULOU_I:
6883 imm = 1;
6884 goto do_mulou;
6885
6886 case M_DMULOU:
6887 dbl = 1;
6888 case M_MULOU:
6889 do_mulou:
6890 mips_emit_delays (TRUE);
6891 ++mips_opts.noreorder;
6892 mips_any_noreorder = 1;
6893 used_at = 1;
6894 if (imm)
6895 load_register (AT, &imm_expr, dbl);
6896 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6897 sreg, imm ? AT : treg);
6898 macro_build (NULL, "mfhi", "d", AT);
6899 macro_build (NULL, "mflo", "d", dreg);
6900 if (mips_trap)
6901 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6902 else
6903 {
6904 expr1.X_add_number = 8;
6905 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6906 macro_build (NULL, "nop", "", 0);
6907 macro_build (NULL, "break", "c", 6);
6908 }
6909 --mips_opts.noreorder;
6910 break;
6911
6912 case M_DROL:
6913 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6914 {
6915 if (dreg == sreg)
6916 {
6917 tempreg = AT;
6918 used_at = 1;
6919 }
6920 else
6921 {
6922 tempreg = dreg;
6923 }
6924 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6925 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6926 break;
6927 }
6928 used_at = 1;
6929 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6930 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6931 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6932 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6933 break;
6934
6935 case M_ROL:
6936 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6937 {
6938 if (dreg == sreg)
6939 {
6940 tempreg = AT;
6941 used_at = 1;
6942 }
6943 else
6944 {
6945 tempreg = dreg;
6946 }
6947 macro_build (NULL, "negu", "d,w", tempreg, treg);
6948 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6949 break;
6950 }
6951 used_at = 1;
6952 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6953 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6954 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6955 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6956 break;
6957
6958 case M_DROL_I:
6959 {
6960 unsigned int rot;
6961 char *l, *r;
6962
6963 if (imm_expr.X_op != O_constant)
6964 as_bad (_("Improper rotate count"));
6965 rot = imm_expr.X_add_number & 0x3f;
6966 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6967 {
6968 rot = (64 - rot) & 0x3f;
6969 if (rot >= 32)
6970 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6971 else
6972 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6973 break;
6974 }
6975 if (rot == 0)
6976 {
6977 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6978 break;
6979 }
6980 l = (rot < 0x20) ? "dsll" : "dsll32";
6981 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6982 rot &= 0x1f;
6983 used_at = 1;
6984 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6985 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6986 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6987 }
6988 break;
6989
6990 case M_ROL_I:
6991 {
6992 unsigned int rot;
6993
6994 if (imm_expr.X_op != O_constant)
6995 as_bad (_("Improper rotate count"));
6996 rot = imm_expr.X_add_number & 0x1f;
6997 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6998 {
6999 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7000 break;
7001 }
7002 if (rot == 0)
7003 {
7004 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7005 break;
7006 }
7007 used_at = 1;
7008 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7009 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7010 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7011 }
7012 break;
7013
7014 case M_DROR:
7015 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7016 {
7017 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
7018 break;
7019 }
7020 used_at = 1;
7021 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7022 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7023 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7024 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7025 break;
7026
7027 case M_ROR:
7028 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7029 {
7030 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7031 break;
7032 }
7033 used_at = 1;
7034 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7035 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7036 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7037 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7038 break;
7039
7040 case M_DROR_I:
7041 {
7042 unsigned int rot;
7043 char *l, *r;
7044
7045 if (imm_expr.X_op != O_constant)
7046 as_bad (_("Improper rotate count"));
7047 rot = imm_expr.X_add_number & 0x3f;
7048 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7049 {
7050 if (rot >= 32)
7051 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7052 else
7053 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7054 break;
7055 }
7056 if (rot == 0)
7057 {
7058 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7059 break;
7060 }
7061 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7062 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7063 rot &= 0x1f;
7064 used_at = 1;
7065 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7066 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7067 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7068 }
7069 break;
7070
7071 case M_ROR_I:
7072 {
7073 unsigned int rot;
7074
7075 if (imm_expr.X_op != O_constant)
7076 as_bad (_("Improper rotate count"));
7077 rot = imm_expr.X_add_number & 0x1f;
7078 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7079 {
7080 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7081 break;
7082 }
7083 if (rot == 0)
7084 {
7085 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7086 break;
7087 }
7088 used_at = 1;
7089 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7090 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7091 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7092 }
7093 break;
7094
7095 case M_S_DOB:
7096 if (mips_opts.arch == CPU_R4650)
7097 {
7098 as_bad (_("opcode not supported on this processor"));
7099 break;
7100 }
7101 assert (mips_opts.isa == ISA_MIPS1);
7102 /* Even on a big endian machine $fn comes before $fn+1. We have
7103 to adjust when storing to memory. */
7104 macro_build (&offset_expr, "swc1", "T,o(b)",
7105 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7106 offset_expr.X_add_number += 4;
7107 macro_build (&offset_expr, "swc1", "T,o(b)",
7108 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7109 break;
7110
7111 case M_SEQ:
7112 if (sreg == 0)
7113 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7114 else if (treg == 0)
7115 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7116 else
7117 {
7118 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7119 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7120 }
7121 break;
7122
7123 case M_SEQ_I:
7124 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7125 {
7126 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7127 break;
7128 }
7129 if (sreg == 0)
7130 {
7131 as_warn (_("Instruction %s: result is always false"),
7132 ip->insn_mo->name);
7133 move_register (dreg, 0);
7134 break;
7135 }
7136 if (imm_expr.X_op == O_constant
7137 && imm_expr.X_add_number >= 0
7138 && imm_expr.X_add_number < 0x10000)
7139 {
7140 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7141 }
7142 else if (imm_expr.X_op == O_constant
7143 && imm_expr.X_add_number > -0x8000
7144 && imm_expr.X_add_number < 0)
7145 {
7146 imm_expr.X_add_number = -imm_expr.X_add_number;
7147 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7148 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7149 }
7150 else
7151 {
7152 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7153 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7154 used_at = 1;
7155 }
7156 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7157 break;
7158
7159 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7160 s = "slt";
7161 goto sge;
7162 case M_SGEU:
7163 s = "sltu";
7164 sge:
7165 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7166 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7167 break;
7168
7169 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7170 case M_SGEU_I:
7171 if (imm_expr.X_op == O_constant
7172 && imm_expr.X_add_number >= -0x8000
7173 && imm_expr.X_add_number < 0x8000)
7174 {
7175 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7176 dreg, sreg, BFD_RELOC_LO16);
7177 }
7178 else
7179 {
7180 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7181 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7182 dreg, sreg, AT);
7183 used_at = 1;
7184 }
7185 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7186 break;
7187
7188 case M_SGT: /* sreg > treg <==> treg < sreg */
7189 s = "slt";
7190 goto sgt;
7191 case M_SGTU:
7192 s = "sltu";
7193 sgt:
7194 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7195 break;
7196
7197 case M_SGT_I: /* sreg > I <==> I < sreg */
7198 s = "slt";
7199 goto sgti;
7200 case M_SGTU_I:
7201 s = "sltu";
7202 sgti:
7203 used_at = 1;
7204 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7205 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7206 break;
7207
7208 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7209 s = "slt";
7210 goto sle;
7211 case M_SLEU:
7212 s = "sltu";
7213 sle:
7214 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7215 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7216 break;
7217
7218 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7219 s = "slt";
7220 goto slei;
7221 case M_SLEU_I:
7222 s = "sltu";
7223 slei:
7224 used_at = 1;
7225 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7226 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7227 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7228 break;
7229
7230 case M_SLT_I:
7231 if (imm_expr.X_op == O_constant
7232 && imm_expr.X_add_number >= -0x8000
7233 && imm_expr.X_add_number < 0x8000)
7234 {
7235 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7236 break;
7237 }
7238 used_at = 1;
7239 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7240 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7241 break;
7242
7243 case M_SLTU_I:
7244 if (imm_expr.X_op == O_constant
7245 && imm_expr.X_add_number >= -0x8000
7246 && imm_expr.X_add_number < 0x8000)
7247 {
7248 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7249 BFD_RELOC_LO16);
7250 break;
7251 }
7252 used_at = 1;
7253 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7254 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7255 break;
7256
7257 case M_SNE:
7258 if (sreg == 0)
7259 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7260 else if (treg == 0)
7261 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7262 else
7263 {
7264 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7265 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7266 }
7267 break;
7268
7269 case M_SNE_I:
7270 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7271 {
7272 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7273 break;
7274 }
7275 if (sreg == 0)
7276 {
7277 as_warn (_("Instruction %s: result is always true"),
7278 ip->insn_mo->name);
7279 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7280 dreg, 0, BFD_RELOC_LO16);
7281 break;
7282 }
7283 if (imm_expr.X_op == O_constant
7284 && imm_expr.X_add_number >= 0
7285 && imm_expr.X_add_number < 0x10000)
7286 {
7287 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7288 }
7289 else if (imm_expr.X_op == O_constant
7290 && imm_expr.X_add_number > -0x8000
7291 && imm_expr.X_add_number < 0)
7292 {
7293 imm_expr.X_add_number = -imm_expr.X_add_number;
7294 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7295 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7296 }
7297 else
7298 {
7299 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7300 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7301 used_at = 1;
7302 }
7303 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7304 break;
7305
7306 case M_DSUB_I:
7307 dbl = 1;
7308 case M_SUB_I:
7309 if (imm_expr.X_op == O_constant
7310 && imm_expr.X_add_number > -0x8000
7311 && imm_expr.X_add_number <= 0x8000)
7312 {
7313 imm_expr.X_add_number = -imm_expr.X_add_number;
7314 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7315 dreg, sreg, BFD_RELOC_LO16);
7316 break;
7317 }
7318 used_at = 1;
7319 load_register (AT, &imm_expr, dbl);
7320 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7321 break;
7322
7323 case M_DSUBU_I:
7324 dbl = 1;
7325 case M_SUBU_I:
7326 if (imm_expr.X_op == O_constant
7327 && imm_expr.X_add_number > -0x8000
7328 && imm_expr.X_add_number <= 0x8000)
7329 {
7330 imm_expr.X_add_number = -imm_expr.X_add_number;
7331 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7332 dreg, sreg, BFD_RELOC_LO16);
7333 break;
7334 }
7335 used_at = 1;
7336 load_register (AT, &imm_expr, dbl);
7337 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7338 break;
7339
7340 case M_TEQ_I:
7341 s = "teq";
7342 goto trap;
7343 case M_TGE_I:
7344 s = "tge";
7345 goto trap;
7346 case M_TGEU_I:
7347 s = "tgeu";
7348 goto trap;
7349 case M_TLT_I:
7350 s = "tlt";
7351 goto trap;
7352 case M_TLTU_I:
7353 s = "tltu";
7354 goto trap;
7355 case M_TNE_I:
7356 s = "tne";
7357 trap:
7358 used_at = 1;
7359 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7360 macro_build (NULL, s, "s,t", sreg, AT);
7361 break;
7362
7363 case M_TRUNCWS:
7364 case M_TRUNCWD:
7365 assert (mips_opts.isa == ISA_MIPS1);
7366 used_at = 1;
7367 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7368 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7369
7370 /*
7371 * Is the double cfc1 instruction a bug in the mips assembler;
7372 * or is there a reason for it?
7373 */
7374 mips_emit_delays (TRUE);
7375 ++mips_opts.noreorder;
7376 mips_any_noreorder = 1;
7377 macro_build (NULL, "cfc1", "t,G", treg, RA);
7378 macro_build (NULL, "cfc1", "t,G", treg, RA);
7379 macro_build (NULL, "nop", "");
7380 expr1.X_add_number = 3;
7381 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7382 expr1.X_add_number = 2;
7383 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7384 macro_build (NULL, "ctc1", "t,G", AT, RA);
7385 macro_build (NULL, "nop", "");
7386 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7387 dreg, sreg);
7388 macro_build (NULL, "ctc1", "t,G", treg, RA);
7389 macro_build (NULL, "nop", "");
7390 --mips_opts.noreorder;
7391 break;
7392
7393 case M_ULH:
7394 s = "lb";
7395 goto ulh;
7396 case M_ULHU:
7397 s = "lbu";
7398 ulh:
7399 used_at = 1;
7400 if (offset_expr.X_add_number >= 0x7fff)
7401 as_bad (_("operand overflow"));
7402 if (! target_big_endian)
7403 ++offset_expr.X_add_number;
7404 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7405 if (! target_big_endian)
7406 --offset_expr.X_add_number;
7407 else
7408 ++offset_expr.X_add_number;
7409 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7410 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7411 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7412 break;
7413
7414 case M_ULD:
7415 s = "ldl";
7416 s2 = "ldr";
7417 off = 7;
7418 goto ulw;
7419 case M_ULW:
7420 s = "lwl";
7421 s2 = "lwr";
7422 off = 3;
7423 ulw:
7424 if (offset_expr.X_add_number >= 0x8000 - off)
7425 as_bad (_("operand overflow"));
7426 if (treg != breg)
7427 tempreg = treg;
7428 else
7429 {
7430 used_at = 1;
7431 tempreg = AT;
7432 }
7433 if (! target_big_endian)
7434 offset_expr.X_add_number += off;
7435 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7436 if (! target_big_endian)
7437 offset_expr.X_add_number -= off;
7438 else
7439 offset_expr.X_add_number += off;
7440 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7441
7442 /* If necessary, move the result in tempreg the final destination. */
7443 if (treg == tempreg)
7444 break;
7445 /* Protect second load's delay slot. */
7446 load_delay_nop ();
7447 move_register (treg, tempreg);
7448 break;
7449
7450 case M_ULD_A:
7451 s = "ldl";
7452 s2 = "ldr";
7453 off = 7;
7454 goto ulwa;
7455 case M_ULW_A:
7456 s = "lwl";
7457 s2 = "lwr";
7458 off = 3;
7459 ulwa:
7460 used_at = 1;
7461 load_address (AT, &offset_expr, &used_at);
7462 if (breg != 0)
7463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7464 if (! target_big_endian)
7465 expr1.X_add_number = off;
7466 else
7467 expr1.X_add_number = 0;
7468 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7469 if (! target_big_endian)
7470 expr1.X_add_number = 0;
7471 else
7472 expr1.X_add_number = off;
7473 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7474 break;
7475
7476 case M_ULH_A:
7477 case M_ULHU_A:
7478 used_at = 1;
7479 load_address (AT, &offset_expr, &used_at);
7480 if (breg != 0)
7481 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7482 if (target_big_endian)
7483 expr1.X_add_number = 0;
7484 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7485 treg, BFD_RELOC_LO16, AT);
7486 if (target_big_endian)
7487 expr1.X_add_number = 1;
7488 else
7489 expr1.X_add_number = 0;
7490 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7491 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7492 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7493 break;
7494
7495 case M_USH:
7496 used_at = 1;
7497 if (offset_expr.X_add_number >= 0x7fff)
7498 as_bad (_("operand overflow"));
7499 if (target_big_endian)
7500 ++offset_expr.X_add_number;
7501 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7502 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7503 if (target_big_endian)
7504 --offset_expr.X_add_number;
7505 else
7506 ++offset_expr.X_add_number;
7507 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7508 break;
7509
7510 case M_USD:
7511 s = "sdl";
7512 s2 = "sdr";
7513 off = 7;
7514 goto usw;
7515 case M_USW:
7516 s = "swl";
7517 s2 = "swr";
7518 off = 3;
7519 usw:
7520 if (offset_expr.X_add_number >= 0x8000 - off)
7521 as_bad (_("operand overflow"));
7522 if (! target_big_endian)
7523 offset_expr.X_add_number += off;
7524 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7525 if (! target_big_endian)
7526 offset_expr.X_add_number -= off;
7527 else
7528 offset_expr.X_add_number += off;
7529 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7530 break;
7531
7532 case M_USD_A:
7533 s = "sdl";
7534 s2 = "sdr";
7535 off = 7;
7536 goto uswa;
7537 case M_USW_A:
7538 s = "swl";
7539 s2 = "swr";
7540 off = 3;
7541 uswa:
7542 used_at = 1;
7543 load_address (AT, &offset_expr, &used_at);
7544 if (breg != 0)
7545 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7546 if (! target_big_endian)
7547 expr1.X_add_number = off;
7548 else
7549 expr1.X_add_number = 0;
7550 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7551 if (! target_big_endian)
7552 expr1.X_add_number = 0;
7553 else
7554 expr1.X_add_number = off;
7555 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7556 break;
7557
7558 case M_USH_A:
7559 used_at = 1;
7560 load_address (AT, &offset_expr, &used_at);
7561 if (breg != 0)
7562 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7563 if (! target_big_endian)
7564 expr1.X_add_number = 0;
7565 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7566 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7567 if (! target_big_endian)
7568 expr1.X_add_number = 1;
7569 else
7570 expr1.X_add_number = 0;
7571 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7572 if (! target_big_endian)
7573 expr1.X_add_number = 0;
7574 else
7575 expr1.X_add_number = 1;
7576 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7577 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7578 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7579 break;
7580
7581 default:
7582 /* FIXME: Check if this is one of the itbl macros, since they
7583 are added dynamically. */
7584 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7585 break;
7586 }
7587 if (mips_opts.noat && used_at)
7588 as_bad (_("Macro used $at after \".set noat\""));
7589 }
7590
7591 /* Implement macros in mips16 mode. */
7592
7593 static void
7594 mips16_macro (struct mips_cl_insn *ip)
7595 {
7596 int mask;
7597 int xreg, yreg, zreg, tmp;
7598 expressionS expr1;
7599 int dbl;
7600 const char *s, *s2, *s3;
7601
7602 mask = ip->insn_mo->mask;
7603
7604 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7605 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7606 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7607
7608 expr1.X_op = O_constant;
7609 expr1.X_op_symbol = NULL;
7610 expr1.X_add_symbol = NULL;
7611 expr1.X_add_number = 1;
7612
7613 dbl = 0;
7614
7615 switch (mask)
7616 {
7617 default:
7618 internalError ();
7619
7620 case M_DDIV_3:
7621 dbl = 1;
7622 case M_DIV_3:
7623 s = "mflo";
7624 goto do_div3;
7625 case M_DREM_3:
7626 dbl = 1;
7627 case M_REM_3:
7628 s = "mfhi";
7629 do_div3:
7630 mips_emit_delays (TRUE);
7631 ++mips_opts.noreorder;
7632 mips_any_noreorder = 1;
7633 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7634 expr1.X_add_number = 2;
7635 macro_build (&expr1, "bnez", "x,p", yreg);
7636 macro_build (NULL, "break", "6", 7);
7637
7638 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7639 since that causes an overflow. We should do that as well,
7640 but I don't see how to do the comparisons without a temporary
7641 register. */
7642 --mips_opts.noreorder;
7643 macro_build (NULL, s, "x", zreg);
7644 break;
7645
7646 case M_DIVU_3:
7647 s = "divu";
7648 s2 = "mflo";
7649 goto do_divu3;
7650 case M_REMU_3:
7651 s = "divu";
7652 s2 = "mfhi";
7653 goto do_divu3;
7654 case M_DDIVU_3:
7655 s = "ddivu";
7656 s2 = "mflo";
7657 goto do_divu3;
7658 case M_DREMU_3:
7659 s = "ddivu";
7660 s2 = "mfhi";
7661 do_divu3:
7662 mips_emit_delays (TRUE);
7663 ++mips_opts.noreorder;
7664 mips_any_noreorder = 1;
7665 macro_build (NULL, s, "0,x,y", xreg, yreg);
7666 expr1.X_add_number = 2;
7667 macro_build (&expr1, "bnez", "x,p", yreg);
7668 macro_build (NULL, "break", "6", 7);
7669 --mips_opts.noreorder;
7670 macro_build (NULL, s2, "x", zreg);
7671 break;
7672
7673 case M_DMUL:
7674 dbl = 1;
7675 case M_MUL:
7676 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7677 macro_build (NULL, "mflo", "x", zreg);
7678 break;
7679
7680 case M_DSUBU_I:
7681 dbl = 1;
7682 goto do_subu;
7683 case M_SUBU_I:
7684 do_subu:
7685 if (imm_expr.X_op != O_constant)
7686 as_bad (_("Unsupported large constant"));
7687 imm_expr.X_add_number = -imm_expr.X_add_number;
7688 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7689 break;
7690
7691 case M_SUBU_I_2:
7692 if (imm_expr.X_op != O_constant)
7693 as_bad (_("Unsupported large constant"));
7694 imm_expr.X_add_number = -imm_expr.X_add_number;
7695 macro_build (&imm_expr, "addiu", "x,k", xreg);
7696 break;
7697
7698 case M_DSUBU_I_2:
7699 if (imm_expr.X_op != O_constant)
7700 as_bad (_("Unsupported large constant"));
7701 imm_expr.X_add_number = -imm_expr.X_add_number;
7702 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7703 break;
7704
7705 case M_BEQ:
7706 s = "cmp";
7707 s2 = "bteqz";
7708 goto do_branch;
7709 case M_BNE:
7710 s = "cmp";
7711 s2 = "btnez";
7712 goto do_branch;
7713 case M_BLT:
7714 s = "slt";
7715 s2 = "btnez";
7716 goto do_branch;
7717 case M_BLTU:
7718 s = "sltu";
7719 s2 = "btnez";
7720 goto do_branch;
7721 case M_BLE:
7722 s = "slt";
7723 s2 = "bteqz";
7724 goto do_reverse_branch;
7725 case M_BLEU:
7726 s = "sltu";
7727 s2 = "bteqz";
7728 goto do_reverse_branch;
7729 case M_BGE:
7730 s = "slt";
7731 s2 = "bteqz";
7732 goto do_branch;
7733 case M_BGEU:
7734 s = "sltu";
7735 s2 = "bteqz";
7736 goto do_branch;
7737 case M_BGT:
7738 s = "slt";
7739 s2 = "btnez";
7740 goto do_reverse_branch;
7741 case M_BGTU:
7742 s = "sltu";
7743 s2 = "btnez";
7744
7745 do_reverse_branch:
7746 tmp = xreg;
7747 xreg = yreg;
7748 yreg = tmp;
7749
7750 do_branch:
7751 macro_build (NULL, s, "x,y", xreg, yreg);
7752 macro_build (&offset_expr, s2, "p");
7753 break;
7754
7755 case M_BEQ_I:
7756 s = "cmpi";
7757 s2 = "bteqz";
7758 s3 = "x,U";
7759 goto do_branch_i;
7760 case M_BNE_I:
7761 s = "cmpi";
7762 s2 = "btnez";
7763 s3 = "x,U";
7764 goto do_branch_i;
7765 case M_BLT_I:
7766 s = "slti";
7767 s2 = "btnez";
7768 s3 = "x,8";
7769 goto do_branch_i;
7770 case M_BLTU_I:
7771 s = "sltiu";
7772 s2 = "btnez";
7773 s3 = "x,8";
7774 goto do_branch_i;
7775 case M_BLE_I:
7776 s = "slti";
7777 s2 = "btnez";
7778 s3 = "x,8";
7779 goto do_addone_branch_i;
7780 case M_BLEU_I:
7781 s = "sltiu";
7782 s2 = "btnez";
7783 s3 = "x,8";
7784 goto do_addone_branch_i;
7785 case M_BGE_I:
7786 s = "slti";
7787 s2 = "bteqz";
7788 s3 = "x,8";
7789 goto do_branch_i;
7790 case M_BGEU_I:
7791 s = "sltiu";
7792 s2 = "bteqz";
7793 s3 = "x,8";
7794 goto do_branch_i;
7795 case M_BGT_I:
7796 s = "slti";
7797 s2 = "bteqz";
7798 s3 = "x,8";
7799 goto do_addone_branch_i;
7800 case M_BGTU_I:
7801 s = "sltiu";
7802 s2 = "bteqz";
7803 s3 = "x,8";
7804
7805 do_addone_branch_i:
7806 if (imm_expr.X_op != O_constant)
7807 as_bad (_("Unsupported large constant"));
7808 ++imm_expr.X_add_number;
7809
7810 do_branch_i:
7811 macro_build (&imm_expr, s, s3, xreg);
7812 macro_build (&offset_expr, s2, "p");
7813 break;
7814
7815 case M_ABS:
7816 expr1.X_add_number = 0;
7817 macro_build (&expr1, "slti", "x,8", yreg);
7818 if (xreg != yreg)
7819 move_register (xreg, yreg);
7820 expr1.X_add_number = 2;
7821 macro_build (&expr1, "bteqz", "p");
7822 macro_build (NULL, "neg", "x,w", xreg, xreg);
7823 }
7824 }
7825
7826 /* For consistency checking, verify that all bits are specified either
7827 by the match/mask part of the instruction definition, or by the
7828 operand list. */
7829 static int
7830 validate_mips_insn (const struct mips_opcode *opc)
7831 {
7832 const char *p = opc->args;
7833 char c;
7834 unsigned long used_bits = opc->mask;
7835
7836 if ((used_bits & opc->match) != opc->match)
7837 {
7838 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7839 opc->name, opc->args);
7840 return 0;
7841 }
7842 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7843 while (*p)
7844 switch (c = *p++)
7845 {
7846 case ',': break;
7847 case '(': break;
7848 case ')': break;
7849 case '+':
7850 switch (c = *p++)
7851 {
7852 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7853 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7854 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7855 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7856 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7857 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7858 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7859 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7860 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7861 case 'I': break;
7862 default:
7863 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7864 c, opc->name, opc->args);
7865 return 0;
7866 }
7867 break;
7868 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7869 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7870 case 'A': break;
7871 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7872 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7873 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7874 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7875 case 'F': break;
7876 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7877 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7878 case 'I': break;
7879 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7880 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7881 case 'L': break;
7882 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7883 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7884 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7885 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7886 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7887 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7888 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7889 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7890 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7891 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7892 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7893 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7894 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7895 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7896 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7897 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7898 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7899 case 'f': break;
7900 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7901 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7902 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7903 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7904 case 'l': break;
7905 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7906 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7907 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7908 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7909 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7910 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7911 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7912 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7913 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7914 case 'x': break;
7915 case 'z': break;
7916 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7917 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7918 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7919 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7920 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7921 case '[': break;
7922 case ']': break;
7923 default:
7924 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7925 c, opc->name, opc->args);
7926 return 0;
7927 }
7928 #undef USE_BITS
7929 if (used_bits != 0xffffffff)
7930 {
7931 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7932 ~used_bits & 0xffffffff, opc->name, opc->args);
7933 return 0;
7934 }
7935 return 1;
7936 }
7937
7938 /* This routine assembles an instruction into its binary format. As a
7939 side effect, it sets one of the global variables imm_reloc or
7940 offset_reloc to the type of relocation to do if one of the operands
7941 is an address expression. */
7942
7943 static void
7944 mips_ip (char *str, struct mips_cl_insn *ip)
7945 {
7946 char *s;
7947 const char *args;
7948 char c = 0;
7949 struct mips_opcode *insn;
7950 char *argsStart;
7951 unsigned int regno;
7952 unsigned int lastregno = 0;
7953 unsigned int lastpos = 0;
7954 unsigned int limlo, limhi;
7955 char *s_reset;
7956 char save_c = 0;
7957
7958 insn_error = NULL;
7959
7960 /* If the instruction contains a '.', we first try to match an instruction
7961 including the '.'. Then we try again without the '.'. */
7962 insn = NULL;
7963 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7964 continue;
7965
7966 /* If we stopped on whitespace, then replace the whitespace with null for
7967 the call to hash_find. Save the character we replaced just in case we
7968 have to re-parse the instruction. */
7969 if (ISSPACE (*s))
7970 {
7971 save_c = *s;
7972 *s++ = '\0';
7973 }
7974
7975 insn = (struct mips_opcode *) hash_find (op_hash, str);
7976
7977 /* If we didn't find the instruction in the opcode table, try again, but
7978 this time with just the instruction up to, but not including the
7979 first '.'. */
7980 if (insn == NULL)
7981 {
7982 /* Restore the character we overwrite above (if any). */
7983 if (save_c)
7984 *(--s) = save_c;
7985
7986 /* Scan up to the first '.' or whitespace. */
7987 for (s = str;
7988 *s != '\0' && *s != '.' && !ISSPACE (*s);
7989 ++s)
7990 continue;
7991
7992 /* If we did not find a '.', then we can quit now. */
7993 if (*s != '.')
7994 {
7995 insn_error = "unrecognized opcode";
7996 return;
7997 }
7998
7999 /* Lookup the instruction in the hash table. */
8000 *s++ = '\0';
8001 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8002 {
8003 insn_error = "unrecognized opcode";
8004 return;
8005 }
8006 }
8007
8008 argsStart = s;
8009 for (;;)
8010 {
8011 bfd_boolean ok;
8012
8013 assert (strcmp (insn->name, str) == 0);
8014
8015 if (OPCODE_IS_MEMBER (insn,
8016 (mips_opts.isa
8017 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8018 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8019 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8020 mips_opts.arch))
8021 ok = TRUE;
8022 else
8023 ok = FALSE;
8024
8025 if (insn->pinfo != INSN_MACRO)
8026 {
8027 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8028 ok = FALSE;
8029 }
8030
8031 if (! ok)
8032 {
8033 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8034 && strcmp (insn->name, insn[1].name) == 0)
8035 {
8036 ++insn;
8037 continue;
8038 }
8039 else
8040 {
8041 if (!insn_error)
8042 {
8043 static char buf[100];
8044 sprintf (buf,
8045 _("opcode not supported on this processor: %s (%s)"),
8046 mips_cpu_info_from_arch (mips_opts.arch)->name,
8047 mips_cpu_info_from_isa (mips_opts.isa)->name);
8048 insn_error = buf;
8049 }
8050 if (save_c)
8051 *(--s) = save_c;
8052 return;
8053 }
8054 }
8055
8056 ip->insn_mo = insn;
8057 ip->insn_opcode = insn->match;
8058 insn_error = NULL;
8059 for (args = insn->args;; ++args)
8060 {
8061 int is_mdmx;
8062
8063 s += strspn (s, " \t");
8064 is_mdmx = 0;
8065 switch (*args)
8066 {
8067 case '\0': /* end of args */
8068 if (*s == '\0')
8069 return;
8070 break;
8071
8072 case ',':
8073 if (*s++ == *args)
8074 continue;
8075 s--;
8076 switch (*++args)
8077 {
8078 case 'r':
8079 case 'v':
8080 ip->insn_opcode |= lastregno << OP_SH_RS;
8081 continue;
8082
8083 case 'w':
8084 ip->insn_opcode |= lastregno << OP_SH_RT;
8085 continue;
8086
8087 case 'W':
8088 ip->insn_opcode |= lastregno << OP_SH_FT;
8089 continue;
8090
8091 case 'V':
8092 ip->insn_opcode |= lastregno << OP_SH_FS;
8093 continue;
8094 }
8095 break;
8096
8097 case '(':
8098 /* Handle optional base register.
8099 Either the base register is omitted or
8100 we must have a left paren. */
8101 /* This is dependent on the next operand specifier
8102 is a base register specification. */
8103 assert (args[1] == 'b' || args[1] == '5'
8104 || args[1] == '-' || args[1] == '4');
8105 if (*s == '\0')
8106 return;
8107
8108 case ')': /* these must match exactly */
8109 case '[':
8110 case ']':
8111 if (*s++ == *args)
8112 continue;
8113 break;
8114
8115 case '+': /* Opcode extension character. */
8116 switch (*++args)
8117 {
8118 case 'A': /* ins/ext position, becomes LSB. */
8119 limlo = 0;
8120 limhi = 31;
8121 goto do_lsb;
8122 case 'E':
8123 limlo = 32;
8124 limhi = 63;
8125 goto do_lsb;
8126 do_lsb:
8127 my_getExpression (&imm_expr, s);
8128 check_absolute_expr (ip, &imm_expr);
8129 if ((unsigned long) imm_expr.X_add_number < limlo
8130 || (unsigned long) imm_expr.X_add_number > limhi)
8131 {
8132 as_bad (_("Improper position (%lu)"),
8133 (unsigned long) imm_expr.X_add_number);
8134 imm_expr.X_add_number = limlo;
8135 }
8136 lastpos = imm_expr.X_add_number;
8137 ip->insn_opcode |= (imm_expr.X_add_number
8138 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8139 imm_expr.X_op = O_absent;
8140 s = expr_end;
8141 continue;
8142
8143 case 'B': /* ins size, becomes MSB. */
8144 limlo = 1;
8145 limhi = 32;
8146 goto do_msb;
8147 case 'F':
8148 limlo = 33;
8149 limhi = 64;
8150 goto do_msb;
8151 do_msb:
8152 my_getExpression (&imm_expr, s);
8153 check_absolute_expr (ip, &imm_expr);
8154 /* Check for negative input so that small negative numbers
8155 will not succeed incorrectly. The checks against
8156 (pos+size) transitively check "size" itself,
8157 assuming that "pos" is reasonable. */
8158 if ((long) imm_expr.X_add_number < 0
8159 || ((unsigned long) imm_expr.X_add_number
8160 + lastpos) < limlo
8161 || ((unsigned long) imm_expr.X_add_number
8162 + lastpos) > limhi)
8163 {
8164 as_bad (_("Improper insert size (%lu, position %lu)"),
8165 (unsigned long) imm_expr.X_add_number,
8166 (unsigned long) lastpos);
8167 imm_expr.X_add_number = limlo - lastpos;
8168 }
8169 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8170 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8171 imm_expr.X_op = O_absent;
8172 s = expr_end;
8173 continue;
8174
8175 case 'C': /* ext size, becomes MSBD. */
8176 limlo = 1;
8177 limhi = 32;
8178 goto do_msbd;
8179 case 'G':
8180 limlo = 33;
8181 limhi = 64;
8182 goto do_msbd;
8183 case 'H':
8184 limlo = 33;
8185 limhi = 64;
8186 goto do_msbd;
8187 do_msbd:
8188 my_getExpression (&imm_expr, s);
8189 check_absolute_expr (ip, &imm_expr);
8190 /* Check for negative input so that small negative numbers
8191 will not succeed incorrectly. The checks against
8192 (pos+size) transitively check "size" itself,
8193 assuming that "pos" is reasonable. */
8194 if ((long) imm_expr.X_add_number < 0
8195 || ((unsigned long) imm_expr.X_add_number
8196 + lastpos) < limlo
8197 || ((unsigned long) imm_expr.X_add_number
8198 + lastpos) > limhi)
8199 {
8200 as_bad (_("Improper extract size (%lu, position %lu)"),
8201 (unsigned long) imm_expr.X_add_number,
8202 (unsigned long) lastpos);
8203 imm_expr.X_add_number = limlo - lastpos;
8204 }
8205 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8206 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8207 imm_expr.X_op = O_absent;
8208 s = expr_end;
8209 continue;
8210
8211 case 'D':
8212 /* +D is for disassembly only; never match. */
8213 break;
8214
8215 case 'I':
8216 /* "+I" is like "I", except that imm2_expr is used. */
8217 my_getExpression (&imm2_expr, s);
8218 if (imm2_expr.X_op != O_big
8219 && imm2_expr.X_op != O_constant)
8220 insn_error = _("absolute expression required");
8221 normalize_constant_expr (&imm2_expr);
8222 s = expr_end;
8223 continue;
8224
8225 default:
8226 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8227 *args, insn->name, insn->args);
8228 /* Further processing is fruitless. */
8229 return;
8230 }
8231 break;
8232
8233 case '<': /* must be at least one digit */
8234 /*
8235 * According to the manual, if the shift amount is greater
8236 * than 31 or less than 0, then the shift amount should be
8237 * mod 32. In reality the mips assembler issues an error.
8238 * We issue a warning and mask out all but the low 5 bits.
8239 */
8240 my_getExpression (&imm_expr, s);
8241 check_absolute_expr (ip, &imm_expr);
8242 if ((unsigned long) imm_expr.X_add_number > 31)
8243 {
8244 as_warn (_("Improper shift amount (%lu)"),
8245 (unsigned long) imm_expr.X_add_number);
8246 imm_expr.X_add_number &= OP_MASK_SHAMT;
8247 }
8248 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8249 imm_expr.X_op = O_absent;
8250 s = expr_end;
8251 continue;
8252
8253 case '>': /* shift amount minus 32 */
8254 my_getExpression (&imm_expr, s);
8255 check_absolute_expr (ip, &imm_expr);
8256 if ((unsigned long) imm_expr.X_add_number < 32
8257 || (unsigned long) imm_expr.X_add_number > 63)
8258 break;
8259 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8260 imm_expr.X_op = O_absent;
8261 s = expr_end;
8262 continue;
8263
8264 case 'k': /* cache code */
8265 case 'h': /* prefx code */
8266 my_getExpression (&imm_expr, s);
8267 check_absolute_expr (ip, &imm_expr);
8268 if ((unsigned long) imm_expr.X_add_number > 31)
8269 {
8270 as_warn (_("Invalid value for `%s' (%lu)"),
8271 ip->insn_mo->name,
8272 (unsigned long) imm_expr.X_add_number);
8273 imm_expr.X_add_number &= 0x1f;
8274 }
8275 if (*args == 'k')
8276 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8277 else
8278 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8279 imm_expr.X_op = O_absent;
8280 s = expr_end;
8281 continue;
8282
8283 case 'c': /* break code */
8284 my_getExpression (&imm_expr, s);
8285 check_absolute_expr (ip, &imm_expr);
8286 if ((unsigned long) imm_expr.X_add_number > 1023)
8287 {
8288 as_warn (_("Illegal break code (%lu)"),
8289 (unsigned long) imm_expr.X_add_number);
8290 imm_expr.X_add_number &= OP_MASK_CODE;
8291 }
8292 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8293 imm_expr.X_op = O_absent;
8294 s = expr_end;
8295 continue;
8296
8297 case 'q': /* lower break code */
8298 my_getExpression (&imm_expr, s);
8299 check_absolute_expr (ip, &imm_expr);
8300 if ((unsigned long) imm_expr.X_add_number > 1023)
8301 {
8302 as_warn (_("Illegal lower break code (%lu)"),
8303 (unsigned long) imm_expr.X_add_number);
8304 imm_expr.X_add_number &= OP_MASK_CODE2;
8305 }
8306 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8307 imm_expr.X_op = O_absent;
8308 s = expr_end;
8309 continue;
8310
8311 case 'B': /* 20-bit syscall/break code. */
8312 my_getExpression (&imm_expr, s);
8313 check_absolute_expr (ip, &imm_expr);
8314 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8315 as_warn (_("Illegal 20-bit code (%lu)"),
8316 (unsigned long) imm_expr.X_add_number);
8317 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8318 imm_expr.X_op = O_absent;
8319 s = expr_end;
8320 continue;
8321
8322 case 'C': /* Coprocessor code */
8323 my_getExpression (&imm_expr, s);
8324 check_absolute_expr (ip, &imm_expr);
8325 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8326 {
8327 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8328 (unsigned long) imm_expr.X_add_number);
8329 imm_expr.X_add_number &= ((1 << 25) - 1);
8330 }
8331 ip->insn_opcode |= imm_expr.X_add_number;
8332 imm_expr.X_op = O_absent;
8333 s = expr_end;
8334 continue;
8335
8336 case 'J': /* 19-bit wait code. */
8337 my_getExpression (&imm_expr, s);
8338 check_absolute_expr (ip, &imm_expr);
8339 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8340 as_warn (_("Illegal 19-bit code (%lu)"),
8341 (unsigned long) imm_expr.X_add_number);
8342 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8343 imm_expr.X_op = O_absent;
8344 s = expr_end;
8345 continue;
8346
8347 case 'P': /* Performance register */
8348 my_getExpression (&imm_expr, s);
8349 check_absolute_expr (ip, &imm_expr);
8350 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8351 {
8352 as_warn (_("Invalid performance register (%lu)"),
8353 (unsigned long) imm_expr.X_add_number);
8354 imm_expr.X_add_number &= OP_MASK_PERFREG;
8355 }
8356 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8357 imm_expr.X_op = O_absent;
8358 s = expr_end;
8359 continue;
8360
8361 case 'b': /* base register */
8362 case 'd': /* destination register */
8363 case 's': /* source register */
8364 case 't': /* target register */
8365 case 'r': /* both target and source */
8366 case 'v': /* both dest and source */
8367 case 'w': /* both dest and target */
8368 case 'E': /* coprocessor target register */
8369 case 'G': /* coprocessor destination register */
8370 case 'K': /* 'rdhwr' destination register */
8371 case 'x': /* ignore register name */
8372 case 'z': /* must be zero register */
8373 case 'U': /* destination register (clo/clz). */
8374 s_reset = s;
8375 if (s[0] == '$')
8376 {
8377
8378 if (ISDIGIT (s[1]))
8379 {
8380 ++s;
8381 regno = 0;
8382 do
8383 {
8384 regno *= 10;
8385 regno += *s - '0';
8386 ++s;
8387 }
8388 while (ISDIGIT (*s));
8389 if (regno > 31)
8390 as_bad (_("Invalid register number (%d)"), regno);
8391 }
8392 else if (*args == 'E' || *args == 'G' || *args == 'K')
8393 goto notreg;
8394 else
8395 {
8396 if (s[1] == 'r' && s[2] == 'a')
8397 {
8398 s += 3;
8399 regno = RA;
8400 }
8401 else if (s[1] == 'f' && s[2] == 'p')
8402 {
8403 s += 3;
8404 regno = FP;
8405 }
8406 else if (s[1] == 's' && s[2] == 'p')
8407 {
8408 s += 3;
8409 regno = SP;
8410 }
8411 else if (s[1] == 'g' && s[2] == 'p')
8412 {
8413 s += 3;
8414 regno = GP;
8415 }
8416 else if (s[1] == 'a' && s[2] == 't')
8417 {
8418 s += 3;
8419 regno = AT;
8420 }
8421 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8422 {
8423 s += 4;
8424 regno = KT0;
8425 }
8426 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8427 {
8428 s += 4;
8429 regno = KT1;
8430 }
8431 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8432 {
8433 s += 5;
8434 regno = ZERO;
8435 }
8436 else if (itbl_have_entries)
8437 {
8438 char *p, *n;
8439 unsigned long r;
8440
8441 p = s + 1; /* advance past '$' */
8442 n = itbl_get_field (&p); /* n is name */
8443
8444 /* See if this is a register defined in an
8445 itbl entry. */
8446 if (itbl_get_reg_val (n, &r))
8447 {
8448 /* Get_field advances to the start of
8449 the next field, so we need to back
8450 rack to the end of the last field. */
8451 if (p)
8452 s = p - 1;
8453 else
8454 s = strchr (s, '\0');
8455 regno = r;
8456 }
8457 else
8458 goto notreg;
8459 }
8460 else
8461 goto notreg;
8462 }
8463 if (regno == AT
8464 && ! mips_opts.noat
8465 && *args != 'E'
8466 && *args != 'G'
8467 && *args != 'K')
8468 as_warn (_("Used $at without \".set noat\""));
8469 c = *args;
8470 if (*s == ' ')
8471 ++s;
8472 if (args[1] != *s)
8473 {
8474 if (c == 'r' || c == 'v' || c == 'w')
8475 {
8476 regno = lastregno;
8477 s = s_reset;
8478 ++args;
8479 }
8480 }
8481 /* 'z' only matches $0. */
8482 if (c == 'z' && regno != 0)
8483 break;
8484
8485 /* Now that we have assembled one operand, we use the args string
8486 * to figure out where it goes in the instruction. */
8487 switch (c)
8488 {
8489 case 'r':
8490 case 's':
8491 case 'v':
8492 case 'b':
8493 ip->insn_opcode |= regno << OP_SH_RS;
8494 break;
8495 case 'd':
8496 case 'G':
8497 case 'K':
8498 ip->insn_opcode |= regno << OP_SH_RD;
8499 break;
8500 case 'U':
8501 ip->insn_opcode |= regno << OP_SH_RD;
8502 ip->insn_opcode |= regno << OP_SH_RT;
8503 break;
8504 case 'w':
8505 case 't':
8506 case 'E':
8507 ip->insn_opcode |= regno << OP_SH_RT;
8508 break;
8509 case 'x':
8510 /* This case exists because on the r3000 trunc
8511 expands into a macro which requires a gp
8512 register. On the r6000 or r4000 it is
8513 assembled into a single instruction which
8514 ignores the register. Thus the insn version
8515 is MIPS_ISA2 and uses 'x', and the macro
8516 version is MIPS_ISA1 and uses 't'. */
8517 break;
8518 case 'z':
8519 /* This case is for the div instruction, which
8520 acts differently if the destination argument
8521 is $0. This only matches $0, and is checked
8522 outside the switch. */
8523 break;
8524 case 'D':
8525 /* Itbl operand; not yet implemented. FIXME ?? */
8526 break;
8527 /* What about all other operands like 'i', which
8528 can be specified in the opcode table? */
8529 }
8530 lastregno = regno;
8531 continue;
8532 }
8533 notreg:
8534 switch (*args++)
8535 {
8536 case 'r':
8537 case 'v':
8538 ip->insn_opcode |= lastregno << OP_SH_RS;
8539 continue;
8540 case 'w':
8541 ip->insn_opcode |= lastregno << OP_SH_RT;
8542 continue;
8543 }
8544 break;
8545
8546 case 'O': /* MDMX alignment immediate constant. */
8547 my_getExpression (&imm_expr, s);
8548 check_absolute_expr (ip, &imm_expr);
8549 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8550 {
8551 as_warn ("Improper align amount (%ld), using low bits",
8552 (long) imm_expr.X_add_number);
8553 imm_expr.X_add_number &= OP_MASK_ALN;
8554 }
8555 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8556 imm_expr.X_op = O_absent;
8557 s = expr_end;
8558 continue;
8559
8560 case 'Q': /* MDMX vector, element sel, or const. */
8561 if (s[0] != '$')
8562 {
8563 /* MDMX Immediate. */
8564 my_getExpression (&imm_expr, s);
8565 check_absolute_expr (ip, &imm_expr);
8566 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8567 {
8568 as_warn (_("Invalid MDMX Immediate (%ld)"),
8569 (long) imm_expr.X_add_number);
8570 imm_expr.X_add_number &= OP_MASK_FT;
8571 }
8572 imm_expr.X_add_number &= OP_MASK_FT;
8573 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8574 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8575 else
8576 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8577 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8578 imm_expr.X_op = O_absent;
8579 s = expr_end;
8580 continue;
8581 }
8582 /* Not MDMX Immediate. Fall through. */
8583 case 'X': /* MDMX destination register. */
8584 case 'Y': /* MDMX source register. */
8585 case 'Z': /* MDMX target register. */
8586 is_mdmx = 1;
8587 case 'D': /* floating point destination register */
8588 case 'S': /* floating point source register */
8589 case 'T': /* floating point target register */
8590 case 'R': /* floating point source register */
8591 case 'V':
8592 case 'W':
8593 s_reset = s;
8594 /* Accept $fN for FP and MDMX register numbers, and in
8595 addition accept $vN for MDMX register numbers. */
8596 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8597 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8598 && ISDIGIT (s[2])))
8599 {
8600 s += 2;
8601 regno = 0;
8602 do
8603 {
8604 regno *= 10;
8605 regno += *s - '0';
8606 ++s;
8607 }
8608 while (ISDIGIT (*s));
8609
8610 if (regno > 31)
8611 as_bad (_("Invalid float register number (%d)"), regno);
8612
8613 if ((regno & 1) != 0
8614 && HAVE_32BIT_FPRS
8615 && ! (strcmp (str, "mtc1") == 0
8616 || strcmp (str, "mfc1") == 0
8617 || strcmp (str, "lwc1") == 0
8618 || strcmp (str, "swc1") == 0
8619 || strcmp (str, "l.s") == 0
8620 || strcmp (str, "s.s") == 0))
8621 as_warn (_("Float register should be even, was %d"),
8622 regno);
8623
8624 c = *args;
8625 if (*s == ' ')
8626 ++s;
8627 if (args[1] != *s)
8628 {
8629 if (c == 'V' || c == 'W')
8630 {
8631 regno = lastregno;
8632 s = s_reset;
8633 ++args;
8634 }
8635 }
8636 switch (c)
8637 {
8638 case 'D':
8639 case 'X':
8640 ip->insn_opcode |= regno << OP_SH_FD;
8641 break;
8642 case 'V':
8643 case 'S':
8644 case 'Y':
8645 ip->insn_opcode |= regno << OP_SH_FS;
8646 break;
8647 case 'Q':
8648 /* This is like 'Z', but also needs to fix the MDMX
8649 vector/scalar select bits. Note that the
8650 scalar immediate case is handled above. */
8651 if (*s == '[')
8652 {
8653 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8654 int max_el = (is_qh ? 3 : 7);
8655 s++;
8656 my_getExpression(&imm_expr, s);
8657 check_absolute_expr (ip, &imm_expr);
8658 s = expr_end;
8659 if (imm_expr.X_add_number > max_el)
8660 as_bad(_("Bad element selector %ld"),
8661 (long) imm_expr.X_add_number);
8662 imm_expr.X_add_number &= max_el;
8663 ip->insn_opcode |= (imm_expr.X_add_number
8664 << (OP_SH_VSEL +
8665 (is_qh ? 2 : 1)));
8666 imm_expr.X_op = O_absent;
8667 if (*s != ']')
8668 as_warn(_("Expecting ']' found '%s'"), s);
8669 else
8670 s++;
8671 }
8672 else
8673 {
8674 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8675 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8676 << OP_SH_VSEL);
8677 else
8678 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8679 OP_SH_VSEL);
8680 }
8681 /* Fall through */
8682 case 'W':
8683 case 'T':
8684 case 'Z':
8685 ip->insn_opcode |= regno << OP_SH_FT;
8686 break;
8687 case 'R':
8688 ip->insn_opcode |= regno << OP_SH_FR;
8689 break;
8690 }
8691 lastregno = regno;
8692 continue;
8693 }
8694
8695 switch (*args++)
8696 {
8697 case 'V':
8698 ip->insn_opcode |= lastregno << OP_SH_FS;
8699 continue;
8700 case 'W':
8701 ip->insn_opcode |= lastregno << OP_SH_FT;
8702 continue;
8703 }
8704 break;
8705
8706 case 'I':
8707 my_getExpression (&imm_expr, s);
8708 if (imm_expr.X_op != O_big
8709 && imm_expr.X_op != O_constant)
8710 insn_error = _("absolute expression required");
8711 normalize_constant_expr (&imm_expr);
8712 s = expr_end;
8713 continue;
8714
8715 case 'A':
8716 my_getExpression (&offset_expr, s);
8717 *imm_reloc = BFD_RELOC_32;
8718 s = expr_end;
8719 continue;
8720
8721 case 'F':
8722 case 'L':
8723 case 'f':
8724 case 'l':
8725 {
8726 int f64;
8727 int using_gprs;
8728 char *save_in;
8729 char *err;
8730 unsigned char temp[8];
8731 int len;
8732 unsigned int length;
8733 segT seg;
8734 subsegT subseg;
8735 char *p;
8736
8737 /* These only appear as the last operand in an
8738 instruction, and every instruction that accepts
8739 them in any variant accepts them in all variants.
8740 This means we don't have to worry about backing out
8741 any changes if the instruction does not match.
8742
8743 The difference between them is the size of the
8744 floating point constant and where it goes. For 'F'
8745 and 'L' the constant is 64 bits; for 'f' and 'l' it
8746 is 32 bits. Where the constant is placed is based
8747 on how the MIPS assembler does things:
8748 F -- .rdata
8749 L -- .lit8
8750 f -- immediate value
8751 l -- .lit4
8752
8753 The .lit4 and .lit8 sections are only used if
8754 permitted by the -G argument.
8755
8756 The code below needs to know whether the target register
8757 is 32 or 64 bits wide. It relies on the fact 'f' and
8758 'F' are used with GPR-based instructions and 'l' and
8759 'L' are used with FPR-based instructions. */
8760
8761 f64 = *args == 'F' || *args == 'L';
8762 using_gprs = *args == 'F' || *args == 'f';
8763
8764 save_in = input_line_pointer;
8765 input_line_pointer = s;
8766 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8767 length = len;
8768 s = input_line_pointer;
8769 input_line_pointer = save_in;
8770 if (err != NULL && *err != '\0')
8771 {
8772 as_bad (_("Bad floating point constant: %s"), err);
8773 memset (temp, '\0', sizeof temp);
8774 length = f64 ? 8 : 4;
8775 }
8776
8777 assert (length == (unsigned) (f64 ? 8 : 4));
8778
8779 if (*args == 'f'
8780 || (*args == 'l'
8781 && (g_switch_value < 4
8782 || (temp[0] == 0 && temp[1] == 0)
8783 || (temp[2] == 0 && temp[3] == 0))))
8784 {
8785 imm_expr.X_op = O_constant;
8786 if (! target_big_endian)
8787 imm_expr.X_add_number = bfd_getl32 (temp);
8788 else
8789 imm_expr.X_add_number = bfd_getb32 (temp);
8790 }
8791 else if (length > 4
8792 && ! mips_disable_float_construction
8793 /* Constants can only be constructed in GPRs and
8794 copied to FPRs if the GPRs are at least as wide
8795 as the FPRs. Force the constant into memory if
8796 we are using 64-bit FPRs but the GPRs are only
8797 32 bits wide. */
8798 && (using_gprs
8799 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8800 && ((temp[0] == 0 && temp[1] == 0)
8801 || (temp[2] == 0 && temp[3] == 0))
8802 && ((temp[4] == 0 && temp[5] == 0)
8803 || (temp[6] == 0 && temp[7] == 0)))
8804 {
8805 /* The value is simple enough to load with a couple of
8806 instructions. If using 32-bit registers, set
8807 imm_expr to the high order 32 bits and offset_expr to
8808 the low order 32 bits. Otherwise, set imm_expr to
8809 the entire 64 bit constant. */
8810 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8811 {
8812 imm_expr.X_op = O_constant;
8813 offset_expr.X_op = O_constant;
8814 if (! target_big_endian)
8815 {
8816 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8817 offset_expr.X_add_number = bfd_getl32 (temp);
8818 }
8819 else
8820 {
8821 imm_expr.X_add_number = bfd_getb32 (temp);
8822 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8823 }
8824 if (offset_expr.X_add_number == 0)
8825 offset_expr.X_op = O_absent;
8826 }
8827 else if (sizeof (imm_expr.X_add_number) > 4)
8828 {
8829 imm_expr.X_op = O_constant;
8830 if (! target_big_endian)
8831 imm_expr.X_add_number = bfd_getl64 (temp);
8832 else
8833 imm_expr.X_add_number = bfd_getb64 (temp);
8834 }
8835 else
8836 {
8837 imm_expr.X_op = O_big;
8838 imm_expr.X_add_number = 4;
8839 if (! target_big_endian)
8840 {
8841 generic_bignum[0] = bfd_getl16 (temp);
8842 generic_bignum[1] = bfd_getl16 (temp + 2);
8843 generic_bignum[2] = bfd_getl16 (temp + 4);
8844 generic_bignum[3] = bfd_getl16 (temp + 6);
8845 }
8846 else
8847 {
8848 generic_bignum[0] = bfd_getb16 (temp + 6);
8849 generic_bignum[1] = bfd_getb16 (temp + 4);
8850 generic_bignum[2] = bfd_getb16 (temp + 2);
8851 generic_bignum[3] = bfd_getb16 (temp);
8852 }
8853 }
8854 }
8855 else
8856 {
8857 const char *newname;
8858 segT new_seg;
8859
8860 /* Switch to the right section. */
8861 seg = now_seg;
8862 subseg = now_subseg;
8863 switch (*args)
8864 {
8865 default: /* unused default case avoids warnings. */
8866 case 'L':
8867 newname = RDATA_SECTION_NAME;
8868 if (g_switch_value >= 8)
8869 newname = ".lit8";
8870 break;
8871 case 'F':
8872 newname = RDATA_SECTION_NAME;
8873 break;
8874 case 'l':
8875 assert (g_switch_value >= 4);
8876 newname = ".lit4";
8877 break;
8878 }
8879 new_seg = subseg_new (newname, (subsegT) 0);
8880 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8881 bfd_set_section_flags (stdoutput, new_seg,
8882 (SEC_ALLOC
8883 | SEC_LOAD
8884 | SEC_READONLY
8885 | SEC_DATA));
8886 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8887 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8888 && strcmp (TARGET_OS, "elf") != 0)
8889 record_alignment (new_seg, 4);
8890 else
8891 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8892 if (seg == now_seg)
8893 as_bad (_("Can't use floating point insn in this section"));
8894
8895 /* Set the argument to the current address in the
8896 section. */
8897 offset_expr.X_op = O_symbol;
8898 offset_expr.X_add_symbol =
8899 symbol_new ("L0\001", now_seg,
8900 (valueT) frag_now_fix (), frag_now);
8901 offset_expr.X_add_number = 0;
8902
8903 /* Put the floating point number into the section. */
8904 p = frag_more ((int) length);
8905 memcpy (p, temp, length);
8906
8907 /* Switch back to the original section. */
8908 subseg_set (seg, subseg);
8909 }
8910 }
8911 continue;
8912
8913 case 'i': /* 16 bit unsigned immediate */
8914 case 'j': /* 16 bit signed immediate */
8915 *imm_reloc = BFD_RELOC_LO16;
8916 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8917 {
8918 int more;
8919 offsetT minval, maxval;
8920
8921 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8922 && strcmp (insn->name, insn[1].name) == 0);
8923
8924 /* If the expression was written as an unsigned number,
8925 only treat it as signed if there are no more
8926 alternatives. */
8927 if (more
8928 && *args == 'j'
8929 && sizeof (imm_expr.X_add_number) <= 4
8930 && imm_expr.X_op == O_constant
8931 && imm_expr.X_add_number < 0
8932 && imm_expr.X_unsigned
8933 && HAVE_64BIT_GPRS)
8934 break;
8935
8936 /* For compatibility with older assemblers, we accept
8937 0x8000-0xffff as signed 16-bit numbers when only
8938 signed numbers are allowed. */
8939 if (*args == 'i')
8940 minval = 0, maxval = 0xffff;
8941 else if (more)
8942 minval = -0x8000, maxval = 0x7fff;
8943 else
8944 minval = -0x8000, maxval = 0xffff;
8945
8946 if (imm_expr.X_op != O_constant
8947 || imm_expr.X_add_number < minval
8948 || imm_expr.X_add_number > maxval)
8949 {
8950 if (more)
8951 break;
8952 if (imm_expr.X_op == O_constant
8953 || imm_expr.X_op == O_big)
8954 as_bad (_("expression out of range"));
8955 }
8956 }
8957 s = expr_end;
8958 continue;
8959
8960 case 'o': /* 16 bit offset */
8961 /* Check whether there is only a single bracketed expression
8962 left. If so, it must be the base register and the
8963 constant must be zero. */
8964 if (*s == '(' && strchr (s + 1, '(') == 0)
8965 {
8966 offset_expr.X_op = O_constant;
8967 offset_expr.X_add_number = 0;
8968 continue;
8969 }
8970
8971 /* If this value won't fit into a 16 bit offset, then go
8972 find a macro that will generate the 32 bit offset
8973 code pattern. */
8974 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8975 && (offset_expr.X_op != O_constant
8976 || offset_expr.X_add_number >= 0x8000
8977 || offset_expr.X_add_number < -0x8000))
8978 break;
8979
8980 s = expr_end;
8981 continue;
8982
8983 case 'p': /* pc relative offset */
8984 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8985 my_getExpression (&offset_expr, s);
8986 s = expr_end;
8987 continue;
8988
8989 case 'u': /* upper 16 bits */
8990 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8991 && imm_expr.X_op == O_constant
8992 && (imm_expr.X_add_number < 0
8993 || imm_expr.X_add_number >= 0x10000))
8994 as_bad (_("lui expression not in range 0..65535"));
8995 s = expr_end;
8996 continue;
8997
8998 case 'a': /* 26 bit address */
8999 my_getExpression (&offset_expr, s);
9000 s = expr_end;
9001 *offset_reloc = BFD_RELOC_MIPS_JMP;
9002 continue;
9003
9004 case 'N': /* 3 bit branch condition code */
9005 case 'M': /* 3 bit compare condition code */
9006 if (strncmp (s, "$fcc", 4) != 0)
9007 break;
9008 s += 4;
9009 regno = 0;
9010 do
9011 {
9012 regno *= 10;
9013 regno += *s - '0';
9014 ++s;
9015 }
9016 while (ISDIGIT (*s));
9017 if (regno > 7)
9018 as_bad (_("Invalid condition code register $fcc%d"), regno);
9019 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9020 || strcmp(str + strlen(str) - 5, "any2f") == 0
9021 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9022 && (regno & 1) != 0)
9023 as_warn(_("Condition code register should be even for %s, was %d"),
9024 str, regno);
9025 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9026 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9027 && (regno & 3) != 0)
9028 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9029 str, regno);
9030 if (*args == 'N')
9031 ip->insn_opcode |= regno << OP_SH_BCC;
9032 else
9033 ip->insn_opcode |= regno << OP_SH_CCC;
9034 continue;
9035
9036 case 'H':
9037 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9038 s += 2;
9039 if (ISDIGIT (*s))
9040 {
9041 c = 0;
9042 do
9043 {
9044 c *= 10;
9045 c += *s - '0';
9046 ++s;
9047 }
9048 while (ISDIGIT (*s));
9049 }
9050 else
9051 c = 8; /* Invalid sel value. */
9052
9053 if (c > 7)
9054 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9055 ip->insn_opcode |= c;
9056 continue;
9057
9058 case 'e':
9059 /* Must be at least one digit. */
9060 my_getExpression (&imm_expr, s);
9061 check_absolute_expr (ip, &imm_expr);
9062
9063 if ((unsigned long) imm_expr.X_add_number
9064 > (unsigned long) OP_MASK_VECBYTE)
9065 {
9066 as_bad (_("bad byte vector index (%ld)"),
9067 (long) imm_expr.X_add_number);
9068 imm_expr.X_add_number = 0;
9069 }
9070
9071 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9072 imm_expr.X_op = O_absent;
9073 s = expr_end;
9074 continue;
9075
9076 case '%':
9077 my_getExpression (&imm_expr, s);
9078 check_absolute_expr (ip, &imm_expr);
9079
9080 if ((unsigned long) imm_expr.X_add_number
9081 > (unsigned long) OP_MASK_VECALIGN)
9082 {
9083 as_bad (_("bad byte vector index (%ld)"),
9084 (long) imm_expr.X_add_number);
9085 imm_expr.X_add_number = 0;
9086 }
9087
9088 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9089 imm_expr.X_op = O_absent;
9090 s = expr_end;
9091 continue;
9092
9093 default:
9094 as_bad (_("bad char = '%c'\n"), *args);
9095 internalError ();
9096 }
9097 break;
9098 }
9099 /* Args don't match. */
9100 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9101 !strcmp (insn->name, insn[1].name))
9102 {
9103 ++insn;
9104 s = argsStart;
9105 insn_error = _("illegal operands");
9106 continue;
9107 }
9108 if (save_c)
9109 *(--s) = save_c;
9110 insn_error = _("illegal operands");
9111 return;
9112 }
9113 }
9114
9115 /* This routine assembles an instruction into its binary format when
9116 assembling for the mips16. As a side effect, it sets one of the
9117 global variables imm_reloc or offset_reloc to the type of
9118 relocation to do if one of the operands is an address expression.
9119 It also sets mips16_small and mips16_ext if the user explicitly
9120 requested a small or extended instruction. */
9121
9122 static void
9123 mips16_ip (char *str, struct mips_cl_insn *ip)
9124 {
9125 char *s;
9126 const char *args;
9127 struct mips_opcode *insn;
9128 char *argsstart;
9129 unsigned int regno;
9130 unsigned int lastregno = 0;
9131 char *s_reset;
9132 size_t i;
9133
9134 insn_error = NULL;
9135
9136 mips16_small = FALSE;
9137 mips16_ext = FALSE;
9138
9139 for (s = str; ISLOWER (*s); ++s)
9140 ;
9141 switch (*s)
9142 {
9143 case '\0':
9144 break;
9145
9146 case ' ':
9147 *s++ = '\0';
9148 break;
9149
9150 case '.':
9151 if (s[1] == 't' && s[2] == ' ')
9152 {
9153 *s = '\0';
9154 mips16_small = TRUE;
9155 s += 3;
9156 break;
9157 }
9158 else if (s[1] == 'e' && s[2] == ' ')
9159 {
9160 *s = '\0';
9161 mips16_ext = TRUE;
9162 s += 3;
9163 break;
9164 }
9165 /* Fall through. */
9166 default:
9167 insn_error = _("unknown opcode");
9168 return;
9169 }
9170
9171 if (mips_opts.noautoextend && ! mips16_ext)
9172 mips16_small = TRUE;
9173
9174 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9175 {
9176 insn_error = _("unrecognized opcode");
9177 return;
9178 }
9179
9180 argsstart = s;
9181 for (;;)
9182 {
9183 assert (strcmp (insn->name, str) == 0);
9184
9185 ip->insn_mo = insn;
9186 ip->insn_opcode = insn->match;
9187 ip->use_extend = FALSE;
9188 imm_expr.X_op = O_absent;
9189 imm_reloc[0] = BFD_RELOC_UNUSED;
9190 imm_reloc[1] = BFD_RELOC_UNUSED;
9191 imm_reloc[2] = BFD_RELOC_UNUSED;
9192 imm2_expr.X_op = O_absent;
9193 offset_expr.X_op = O_absent;
9194 offset_reloc[0] = BFD_RELOC_UNUSED;
9195 offset_reloc[1] = BFD_RELOC_UNUSED;
9196 offset_reloc[2] = BFD_RELOC_UNUSED;
9197 for (args = insn->args; 1; ++args)
9198 {
9199 int c;
9200
9201 if (*s == ' ')
9202 ++s;
9203
9204 /* In this switch statement we call break if we did not find
9205 a match, continue if we did find a match, or return if we
9206 are done. */
9207
9208 c = *args;
9209 switch (c)
9210 {
9211 case '\0':
9212 if (*s == '\0')
9213 {
9214 /* Stuff the immediate value in now, if we can. */
9215 if (imm_expr.X_op == O_constant
9216 && *imm_reloc > BFD_RELOC_UNUSED
9217 && insn->pinfo != INSN_MACRO)
9218 {
9219 valueT tmp;
9220
9221 switch (*offset_reloc)
9222 {
9223 case BFD_RELOC_MIPS16_HI16_S:
9224 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9225 break;
9226
9227 case BFD_RELOC_MIPS16_HI16:
9228 tmp = imm_expr.X_add_number >> 16;
9229 break;
9230
9231 case BFD_RELOC_MIPS16_LO16:
9232 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9233 - 0x8000;
9234 break;
9235
9236 case BFD_RELOC_UNUSED:
9237 tmp = imm_expr.X_add_number;
9238 break;
9239
9240 default:
9241 internalError ();
9242 }
9243 *offset_reloc = BFD_RELOC_UNUSED;
9244
9245 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9246 tmp, TRUE, mips16_small,
9247 mips16_ext, &ip->insn_opcode,
9248 &ip->use_extend, &ip->extend);
9249 imm_expr.X_op = O_absent;
9250 *imm_reloc = BFD_RELOC_UNUSED;
9251 }
9252
9253 return;
9254 }
9255 break;
9256
9257 case ',':
9258 if (*s++ == c)
9259 continue;
9260 s--;
9261 switch (*++args)
9262 {
9263 case 'v':
9264 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9265 continue;
9266 case 'w':
9267 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9268 continue;
9269 }
9270 break;
9271
9272 case '(':
9273 case ')':
9274 if (*s++ == c)
9275 continue;
9276 break;
9277
9278 case 'v':
9279 case 'w':
9280 if (s[0] != '$')
9281 {
9282 if (c == 'v')
9283 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9284 else
9285 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9286 ++args;
9287 continue;
9288 }
9289 /* Fall through. */
9290 case 'x':
9291 case 'y':
9292 case 'z':
9293 case 'Z':
9294 case '0':
9295 case 'S':
9296 case 'R':
9297 case 'X':
9298 case 'Y':
9299 if (s[0] != '$')
9300 break;
9301 s_reset = s;
9302 if (ISDIGIT (s[1]))
9303 {
9304 ++s;
9305 regno = 0;
9306 do
9307 {
9308 regno *= 10;
9309 regno += *s - '0';
9310 ++s;
9311 }
9312 while (ISDIGIT (*s));
9313 if (regno > 31)
9314 {
9315 as_bad (_("invalid register number (%d)"), regno);
9316 regno = 2;
9317 }
9318 }
9319 else
9320 {
9321 if (s[1] == 'r' && s[2] == 'a')
9322 {
9323 s += 3;
9324 regno = RA;
9325 }
9326 else if (s[1] == 'f' && s[2] == 'p')
9327 {
9328 s += 3;
9329 regno = FP;
9330 }
9331 else if (s[1] == 's' && s[2] == 'p')
9332 {
9333 s += 3;
9334 regno = SP;
9335 }
9336 else if (s[1] == 'g' && s[2] == 'p')
9337 {
9338 s += 3;
9339 regno = GP;
9340 }
9341 else if (s[1] == 'a' && s[2] == 't')
9342 {
9343 s += 3;
9344 regno = AT;
9345 }
9346 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9347 {
9348 s += 4;
9349 regno = KT0;
9350 }
9351 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9352 {
9353 s += 4;
9354 regno = KT1;
9355 }
9356 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9357 {
9358 s += 5;
9359 regno = ZERO;
9360 }
9361 else
9362 break;
9363 }
9364
9365 if (*s == ' ')
9366 ++s;
9367 if (args[1] != *s)
9368 {
9369 if (c == 'v' || c == 'w')
9370 {
9371 regno = mips16_to_32_reg_map[lastregno];
9372 s = s_reset;
9373 ++args;
9374 }
9375 }
9376
9377 switch (c)
9378 {
9379 case 'x':
9380 case 'y':
9381 case 'z':
9382 case 'v':
9383 case 'w':
9384 case 'Z':
9385 regno = mips32_to_16_reg_map[regno];
9386 break;
9387
9388 case '0':
9389 if (regno != 0)
9390 regno = ILLEGAL_REG;
9391 break;
9392
9393 case 'S':
9394 if (regno != SP)
9395 regno = ILLEGAL_REG;
9396 break;
9397
9398 case 'R':
9399 if (regno != RA)
9400 regno = ILLEGAL_REG;
9401 break;
9402
9403 case 'X':
9404 case 'Y':
9405 if (regno == AT && ! mips_opts.noat)
9406 as_warn (_("used $at without \".set noat\""));
9407 break;
9408
9409 default:
9410 internalError ();
9411 }
9412
9413 if (regno == ILLEGAL_REG)
9414 break;
9415
9416 switch (c)
9417 {
9418 case 'x':
9419 case 'v':
9420 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9421 break;
9422 case 'y':
9423 case 'w':
9424 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9425 break;
9426 case 'z':
9427 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9428 break;
9429 case 'Z':
9430 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9431 case '0':
9432 case 'S':
9433 case 'R':
9434 break;
9435 case 'X':
9436 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9437 break;
9438 case 'Y':
9439 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9440 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9441 break;
9442 default:
9443 internalError ();
9444 }
9445
9446 lastregno = regno;
9447 continue;
9448
9449 case 'P':
9450 if (strncmp (s, "$pc", 3) == 0)
9451 {
9452 s += 3;
9453 continue;
9454 }
9455 break;
9456
9457 case '5':
9458 case 'H':
9459 case 'W':
9460 case 'D':
9461 case 'j':
9462 case 'V':
9463 case 'C':
9464 case 'U':
9465 case 'k':
9466 case 'K':
9467 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9468 if (i > 0)
9469 {
9470 if (imm_expr.X_op != O_constant)
9471 {
9472 mips16_ext = TRUE;
9473 ip->use_extend = TRUE;
9474 ip->extend = 0;
9475 }
9476 else
9477 {
9478 /* We need to relax this instruction. */
9479 *offset_reloc = *imm_reloc;
9480 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9481 }
9482 s = expr_end;
9483 continue;
9484 }
9485 *imm_reloc = BFD_RELOC_UNUSED;
9486 /* Fall through. */
9487 case '<':
9488 case '>':
9489 case '[':
9490 case ']':
9491 case '4':
9492 case '8':
9493 my_getExpression (&imm_expr, s);
9494 if (imm_expr.X_op == O_register)
9495 {
9496 /* What we thought was an expression turned out to
9497 be a register. */
9498
9499 if (s[0] == '(' && args[1] == '(')
9500 {
9501 /* It looks like the expression was omitted
9502 before a register indirection, which means
9503 that the expression is implicitly zero. We
9504 still set up imm_expr, so that we handle
9505 explicit extensions correctly. */
9506 imm_expr.X_op = O_constant;
9507 imm_expr.X_add_number = 0;
9508 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9509 continue;
9510 }
9511
9512 break;
9513 }
9514
9515 /* We need to relax this instruction. */
9516 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9517 s = expr_end;
9518 continue;
9519
9520 case 'p':
9521 case 'q':
9522 case 'A':
9523 case 'B':
9524 case 'E':
9525 /* We use offset_reloc rather than imm_reloc for the PC
9526 relative operands. This lets macros with both
9527 immediate and address operands work correctly. */
9528 my_getExpression (&offset_expr, s);
9529
9530 if (offset_expr.X_op == O_register)
9531 break;
9532
9533 /* We need to relax this instruction. */
9534 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9535 s = expr_end;
9536 continue;
9537
9538 case '6': /* break code */
9539 my_getExpression (&imm_expr, s);
9540 check_absolute_expr (ip, &imm_expr);
9541 if ((unsigned long) imm_expr.X_add_number > 63)
9542 {
9543 as_warn (_("Invalid value for `%s' (%lu)"),
9544 ip->insn_mo->name,
9545 (unsigned long) imm_expr.X_add_number);
9546 imm_expr.X_add_number &= 0x3f;
9547 }
9548 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9549 imm_expr.X_op = O_absent;
9550 s = expr_end;
9551 continue;
9552
9553 case 'a': /* 26 bit address */
9554 my_getExpression (&offset_expr, s);
9555 s = expr_end;
9556 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9557 ip->insn_opcode <<= 16;
9558 continue;
9559
9560 case 'l': /* register list for entry macro */
9561 case 'L': /* register list for exit macro */
9562 {
9563 int mask;
9564
9565 if (c == 'l')
9566 mask = 0;
9567 else
9568 mask = 7 << 3;
9569 while (*s != '\0')
9570 {
9571 int freg, reg1, reg2;
9572
9573 while (*s == ' ' || *s == ',')
9574 ++s;
9575 if (*s != '$')
9576 {
9577 as_bad (_("can't parse register list"));
9578 break;
9579 }
9580 ++s;
9581 if (*s != 'f')
9582 freg = 0;
9583 else
9584 {
9585 freg = 1;
9586 ++s;
9587 }
9588 reg1 = 0;
9589 while (ISDIGIT (*s))
9590 {
9591 reg1 *= 10;
9592 reg1 += *s - '0';
9593 ++s;
9594 }
9595 if (*s == ' ')
9596 ++s;
9597 if (*s != '-')
9598 reg2 = reg1;
9599 else
9600 {
9601 ++s;
9602 if (*s != '$')
9603 break;
9604 ++s;
9605 if (freg)
9606 {
9607 if (*s == 'f')
9608 ++s;
9609 else
9610 {
9611 as_bad (_("invalid register list"));
9612 break;
9613 }
9614 }
9615 reg2 = 0;
9616 while (ISDIGIT (*s))
9617 {
9618 reg2 *= 10;
9619 reg2 += *s - '0';
9620 ++s;
9621 }
9622 }
9623 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9624 {
9625 mask &= ~ (7 << 3);
9626 mask |= 5 << 3;
9627 }
9628 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9629 {
9630 mask &= ~ (7 << 3);
9631 mask |= 6 << 3;
9632 }
9633 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9634 mask |= (reg2 - 3) << 3;
9635 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9636 mask |= (reg2 - 15) << 1;
9637 else if (reg1 == RA && reg2 == RA)
9638 mask |= 1;
9639 else
9640 {
9641 as_bad (_("invalid register list"));
9642 break;
9643 }
9644 }
9645 /* The mask is filled in in the opcode table for the
9646 benefit of the disassembler. We remove it before
9647 applying the actual mask. */
9648 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9649 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9650 }
9651 continue;
9652
9653 case 'e': /* extend code */
9654 my_getExpression (&imm_expr, s);
9655 check_absolute_expr (ip, &imm_expr);
9656 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9657 {
9658 as_warn (_("Invalid value for `%s' (%lu)"),
9659 ip->insn_mo->name,
9660 (unsigned long) imm_expr.X_add_number);
9661 imm_expr.X_add_number &= 0x7ff;
9662 }
9663 ip->insn_opcode |= imm_expr.X_add_number;
9664 imm_expr.X_op = O_absent;
9665 s = expr_end;
9666 continue;
9667
9668 default:
9669 internalError ();
9670 }
9671 break;
9672 }
9673
9674 /* Args don't match. */
9675 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9676 strcmp (insn->name, insn[1].name) == 0)
9677 {
9678 ++insn;
9679 s = argsstart;
9680 continue;
9681 }
9682
9683 insn_error = _("illegal operands");
9684
9685 return;
9686 }
9687 }
9688
9689 /* This structure holds information we know about a mips16 immediate
9690 argument type. */
9691
9692 struct mips16_immed_operand
9693 {
9694 /* The type code used in the argument string in the opcode table. */
9695 int type;
9696 /* The number of bits in the short form of the opcode. */
9697 int nbits;
9698 /* The number of bits in the extended form of the opcode. */
9699 int extbits;
9700 /* The amount by which the short form is shifted when it is used;
9701 for example, the sw instruction has a shift count of 2. */
9702 int shift;
9703 /* The amount by which the short form is shifted when it is stored
9704 into the instruction code. */
9705 int op_shift;
9706 /* Non-zero if the short form is unsigned. */
9707 int unsp;
9708 /* Non-zero if the extended form is unsigned. */
9709 int extu;
9710 /* Non-zero if the value is PC relative. */
9711 int pcrel;
9712 };
9713
9714 /* The mips16 immediate operand types. */
9715
9716 static const struct mips16_immed_operand mips16_immed_operands[] =
9717 {
9718 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9719 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9720 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9721 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9722 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9723 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9724 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9725 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9726 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9727 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9728 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9729 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9730 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9731 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9732 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9733 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9734 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9735 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9736 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9737 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9738 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9739 };
9740
9741 #define MIPS16_NUM_IMMED \
9742 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9743
9744 /* Handle a mips16 instruction with an immediate value. This or's the
9745 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9746 whether an extended value is needed; if one is needed, it sets
9747 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9748 If SMALL is true, an unextended opcode was explicitly requested.
9749 If EXT is true, an extended opcode was explicitly requested. If
9750 WARN is true, warn if EXT does not match reality. */
9751
9752 static void
9753 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9754 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9755 unsigned long *insn, bfd_boolean *use_extend,
9756 unsigned short *extend)
9757 {
9758 register const struct mips16_immed_operand *op;
9759 int mintiny, maxtiny;
9760 bfd_boolean needext;
9761
9762 op = mips16_immed_operands;
9763 while (op->type != type)
9764 {
9765 ++op;
9766 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9767 }
9768
9769 if (op->unsp)
9770 {
9771 if (type == '<' || type == '>' || type == '[' || type == ']')
9772 {
9773 mintiny = 1;
9774 maxtiny = 1 << op->nbits;
9775 }
9776 else
9777 {
9778 mintiny = 0;
9779 maxtiny = (1 << op->nbits) - 1;
9780 }
9781 }
9782 else
9783 {
9784 mintiny = - (1 << (op->nbits - 1));
9785 maxtiny = (1 << (op->nbits - 1)) - 1;
9786 }
9787
9788 /* Branch offsets have an implicit 0 in the lowest bit. */
9789 if (type == 'p' || type == 'q')
9790 val /= 2;
9791
9792 if ((val & ((1 << op->shift) - 1)) != 0
9793 || val < (mintiny << op->shift)
9794 || val > (maxtiny << op->shift))
9795 needext = TRUE;
9796 else
9797 needext = FALSE;
9798
9799 if (warn && ext && ! needext)
9800 as_warn_where (file, line,
9801 _("extended operand requested but not required"));
9802 if (small && needext)
9803 as_bad_where (file, line, _("invalid unextended operand value"));
9804
9805 if (small || (! ext && ! needext))
9806 {
9807 int insnval;
9808
9809 *use_extend = FALSE;
9810 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9811 insnval <<= op->op_shift;
9812 *insn |= insnval;
9813 }
9814 else
9815 {
9816 long minext, maxext;
9817 int extval;
9818
9819 if (op->extu)
9820 {
9821 minext = 0;
9822 maxext = (1 << op->extbits) - 1;
9823 }
9824 else
9825 {
9826 minext = - (1 << (op->extbits - 1));
9827 maxext = (1 << (op->extbits - 1)) - 1;
9828 }
9829 if (val < minext || val > maxext)
9830 as_bad_where (file, line,
9831 _("operand value out of range for instruction"));
9832
9833 *use_extend = TRUE;
9834 if (op->extbits == 16)
9835 {
9836 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9837 val &= 0x1f;
9838 }
9839 else if (op->extbits == 15)
9840 {
9841 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9842 val &= 0xf;
9843 }
9844 else
9845 {
9846 extval = ((val & 0x1f) << 6) | (val & 0x20);
9847 val = 0;
9848 }
9849
9850 *extend = (unsigned short) extval;
9851 *insn |= val;
9852 }
9853 }
9854 \f
9855 struct percent_op_match
9856 {
9857 const char *str;
9858 bfd_reloc_code_real_type reloc;
9859 };
9860
9861 static const struct percent_op_match mips_percent_op[] =
9862 {
9863 {"%lo", BFD_RELOC_LO16},
9864 #ifdef OBJ_ELF
9865 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9866 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9867 {"%call16", BFD_RELOC_MIPS_CALL16},
9868 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9869 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9870 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9871 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9872 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9873 {"%got", BFD_RELOC_MIPS_GOT16},
9874 {"%gp_rel", BFD_RELOC_GPREL16},
9875 {"%half", BFD_RELOC_16},
9876 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9877 {"%higher", BFD_RELOC_MIPS_HIGHER},
9878 {"%neg", BFD_RELOC_MIPS_SUB},
9879 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9880 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9881 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9882 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9883 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9884 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9885 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9886 #endif
9887 {"%hi", BFD_RELOC_HI16_S}
9888 };
9889
9890 static const struct percent_op_match mips16_percent_op[] =
9891 {
9892 {"%lo", BFD_RELOC_MIPS16_LO16},
9893 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9894 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9895 };
9896
9897
9898 /* Return true if *STR points to a relocation operator. When returning true,
9899 move *STR over the operator and store its relocation code in *RELOC.
9900 Leave both *STR and *RELOC alone when returning false. */
9901
9902 static bfd_boolean
9903 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9904 {
9905 const struct percent_op_match *percent_op;
9906 size_t limit, i;
9907
9908 if (mips_opts.mips16)
9909 {
9910 percent_op = mips16_percent_op;
9911 limit = ARRAY_SIZE (mips16_percent_op);
9912 }
9913 else
9914 {
9915 percent_op = mips_percent_op;
9916 limit = ARRAY_SIZE (mips_percent_op);
9917 }
9918
9919 for (i = 0; i < limit; i++)
9920 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9921 {
9922 int len = strlen (percent_op[i].str);
9923
9924 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9925 continue;
9926
9927 *str += strlen (percent_op[i].str);
9928 *reloc = percent_op[i].reloc;
9929
9930 /* Check whether the output BFD supports this relocation.
9931 If not, issue an error and fall back on something safe. */
9932 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9933 {
9934 as_bad ("relocation %s isn't supported by the current ABI",
9935 percent_op[i].str);
9936 *reloc = BFD_RELOC_UNUSED;
9937 }
9938 return TRUE;
9939 }
9940 return FALSE;
9941 }
9942
9943
9944 /* Parse string STR as a 16-bit relocatable operand. Store the
9945 expression in *EP and the relocations in the array starting
9946 at RELOC. Return the number of relocation operators used.
9947
9948 On exit, EXPR_END points to the first character after the expression. */
9949
9950 static size_t
9951 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9952 char *str)
9953 {
9954 bfd_reloc_code_real_type reversed_reloc[3];
9955 size_t reloc_index, i;
9956 int crux_depth, str_depth;
9957 char *crux;
9958
9959 /* Search for the start of the main expression, recoding relocations
9960 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9961 of the main expression and with CRUX_DEPTH containing the number
9962 of open brackets at that point. */
9963 reloc_index = -1;
9964 str_depth = 0;
9965 do
9966 {
9967 reloc_index++;
9968 crux = str;
9969 crux_depth = str_depth;
9970
9971 /* Skip over whitespace and brackets, keeping count of the number
9972 of brackets. */
9973 while (*str == ' ' || *str == '\t' || *str == '(')
9974 if (*str++ == '(')
9975 str_depth++;
9976 }
9977 while (*str == '%'
9978 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9979 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9980
9981 my_getExpression (ep, crux);
9982 str = expr_end;
9983
9984 /* Match every open bracket. */
9985 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9986 if (*str++ == ')')
9987 crux_depth--;
9988
9989 if (crux_depth > 0)
9990 as_bad ("unclosed '('");
9991
9992 expr_end = str;
9993
9994 if (reloc_index != 0)
9995 {
9996 prev_reloc_op_frag = frag_now;
9997 for (i = 0; i < reloc_index; i++)
9998 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9999 }
10000
10001 return reloc_index;
10002 }
10003
10004 static void
10005 my_getExpression (expressionS *ep, char *str)
10006 {
10007 char *save_in;
10008 valueT val;
10009
10010 save_in = input_line_pointer;
10011 input_line_pointer = str;
10012 expression (ep);
10013 expr_end = input_line_pointer;
10014 input_line_pointer = save_in;
10015
10016 /* If we are in mips16 mode, and this is an expression based on `.',
10017 then we bump the value of the symbol by 1 since that is how other
10018 text symbols are handled. We don't bother to handle complex
10019 expressions, just `.' plus or minus a constant. */
10020 if (mips_opts.mips16
10021 && ep->X_op == O_symbol
10022 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10023 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10024 && symbol_get_frag (ep->X_add_symbol) == frag_now
10025 && symbol_constant_p (ep->X_add_symbol)
10026 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10027 S_SET_VALUE (ep->X_add_symbol, val + 1);
10028 }
10029
10030 /* Turn a string in input_line_pointer into a floating point constant
10031 of type TYPE, and store the appropriate bytes in *LITP. The number
10032 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10033 returned, or NULL on OK. */
10034
10035 char *
10036 md_atof (int type, char *litP, int *sizeP)
10037 {
10038 int prec;
10039 LITTLENUM_TYPE words[4];
10040 char *t;
10041 int i;
10042
10043 switch (type)
10044 {
10045 case 'f':
10046 prec = 2;
10047 break;
10048
10049 case 'd':
10050 prec = 4;
10051 break;
10052
10053 default:
10054 *sizeP = 0;
10055 return _("bad call to md_atof");
10056 }
10057
10058 t = atof_ieee (input_line_pointer, type, words);
10059 if (t)
10060 input_line_pointer = t;
10061
10062 *sizeP = prec * 2;
10063
10064 if (! target_big_endian)
10065 {
10066 for (i = prec - 1; i >= 0; i--)
10067 {
10068 md_number_to_chars (litP, words[i], 2);
10069 litP += 2;
10070 }
10071 }
10072 else
10073 {
10074 for (i = 0; i < prec; i++)
10075 {
10076 md_number_to_chars (litP, words[i], 2);
10077 litP += 2;
10078 }
10079 }
10080
10081 return NULL;
10082 }
10083
10084 void
10085 md_number_to_chars (char *buf, valueT val, int n)
10086 {
10087 if (target_big_endian)
10088 number_to_chars_bigendian (buf, val, n);
10089 else
10090 number_to_chars_littleendian (buf, val, n);
10091 }
10092 \f
10093 #ifdef OBJ_ELF
10094 static int support_64bit_objects(void)
10095 {
10096 const char **list, **l;
10097 int yes;
10098
10099 list = bfd_target_list ();
10100 for (l = list; *l != NULL; l++)
10101 #ifdef TE_TMIPS
10102 /* This is traditional mips */
10103 if (strcmp (*l, "elf64-tradbigmips") == 0
10104 || strcmp (*l, "elf64-tradlittlemips") == 0)
10105 #else
10106 if (strcmp (*l, "elf64-bigmips") == 0
10107 || strcmp (*l, "elf64-littlemips") == 0)
10108 #endif
10109 break;
10110 yes = (*l != NULL);
10111 free (list);
10112 return yes;
10113 }
10114 #endif /* OBJ_ELF */
10115
10116 const char *md_shortopts = "O::g::G:";
10117
10118 struct option md_longopts[] =
10119 {
10120 /* Options which specify architecture. */
10121 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10122 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10123 {"march", required_argument, NULL, OPTION_MARCH},
10124 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10125 {"mtune", required_argument, NULL, OPTION_MTUNE},
10126 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10127 {"mips0", no_argument, NULL, OPTION_MIPS1},
10128 {"mips1", no_argument, NULL, OPTION_MIPS1},
10129 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10130 {"mips2", no_argument, NULL, OPTION_MIPS2},
10131 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10132 {"mips3", no_argument, NULL, OPTION_MIPS3},
10133 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10134 {"mips4", no_argument, NULL, OPTION_MIPS4},
10135 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10136 {"mips5", no_argument, NULL, OPTION_MIPS5},
10137 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10138 {"mips32", no_argument, NULL, OPTION_MIPS32},
10139 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10140 {"mips64", no_argument, NULL, OPTION_MIPS64},
10141 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10142 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10143 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10144 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10145
10146 /* Options which specify Application Specific Extensions (ASEs). */
10147 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10148 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10149 {"mips16", no_argument, NULL, OPTION_MIPS16},
10150 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10151 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10152 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10153 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10154 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10155 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10156 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10157 {"mdmx", no_argument, NULL, OPTION_MDMX},
10158 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10159 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10160
10161 /* Old-style architecture options. Don't add more of these. */
10162 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10163 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10164 {"m4650", no_argument, NULL, OPTION_M4650},
10165 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10166 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10167 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10168 {"m4010", no_argument, NULL, OPTION_M4010},
10169 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10170 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10171 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10172 {"m4100", no_argument, NULL, OPTION_M4100},
10173 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10174 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10175 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10176 {"m3900", no_argument, NULL, OPTION_M3900},
10177 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10178 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10179
10180 /* Options which enable bug fixes. */
10181 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10182 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10183 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10184 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10185 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10186 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10187 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10188 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10189 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10190 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10191
10192 /* Miscellaneous options. */
10193 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10194 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10195 {"trap", no_argument, NULL, OPTION_TRAP},
10196 {"no-break", no_argument, NULL, OPTION_TRAP},
10197 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10198 {"break", no_argument, NULL, OPTION_BREAK},
10199 {"no-trap", no_argument, NULL, OPTION_BREAK},
10200 #define OPTION_EB (OPTION_MISC_BASE + 2)
10201 {"EB", no_argument, NULL, OPTION_EB},
10202 #define OPTION_EL (OPTION_MISC_BASE + 3)
10203 {"EL", no_argument, NULL, OPTION_EL},
10204 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10205 {"mfp32", no_argument, NULL, OPTION_FP32},
10206 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10207 {"mgp32", no_argument, NULL, OPTION_GP32},
10208 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10209 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10210 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10211 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10212 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10213 {"mfp64", no_argument, NULL, OPTION_FP64},
10214 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10215 {"mgp64", no_argument, NULL, OPTION_GP64},
10216 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10217 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10218 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10219 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10220 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10221 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10222 {"mshared", no_argument, NULL, OPTION_MSHARED},
10223 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10224 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10225 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10226 {"msym32", no_argument, NULL, OPTION_MSYM32},
10227 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10228
10229 /* ELF-specific options. */
10230 #ifdef OBJ_ELF
10231 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10232 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10233 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10234 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10235 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10236 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10237 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10238 {"xgot", no_argument, NULL, OPTION_XGOT},
10239 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10240 {"mabi", required_argument, NULL, OPTION_MABI},
10241 #define OPTION_32 (OPTION_ELF_BASE + 4)
10242 {"32", no_argument, NULL, OPTION_32},
10243 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10244 {"n32", no_argument, NULL, OPTION_N32},
10245 #define OPTION_64 (OPTION_ELF_BASE + 6)
10246 {"64", no_argument, NULL, OPTION_64},
10247 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10248 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10249 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10250 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10251 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10252 {"mpdr", no_argument, NULL, OPTION_PDR},
10253 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10254 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10255 #endif /* OBJ_ELF */
10256
10257 {NULL, no_argument, NULL, 0}
10258 };
10259 size_t md_longopts_size = sizeof (md_longopts);
10260
10261 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10262 NEW_VALUE. Warn if another value was already specified. Note:
10263 we have to defer parsing the -march and -mtune arguments in order
10264 to handle 'from-abi' correctly, since the ABI might be specified
10265 in a later argument. */
10266
10267 static void
10268 mips_set_option_string (const char **string_ptr, const char *new_value)
10269 {
10270 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10271 as_warn (_("A different %s was already specified, is now %s"),
10272 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10273 new_value);
10274
10275 *string_ptr = new_value;
10276 }
10277
10278 int
10279 md_parse_option (int c, char *arg)
10280 {
10281 switch (c)
10282 {
10283 case OPTION_CONSTRUCT_FLOATS:
10284 mips_disable_float_construction = 0;
10285 break;
10286
10287 case OPTION_NO_CONSTRUCT_FLOATS:
10288 mips_disable_float_construction = 1;
10289 break;
10290
10291 case OPTION_TRAP:
10292 mips_trap = 1;
10293 break;
10294
10295 case OPTION_BREAK:
10296 mips_trap = 0;
10297 break;
10298
10299 case OPTION_EB:
10300 target_big_endian = 1;
10301 break;
10302
10303 case OPTION_EL:
10304 target_big_endian = 0;
10305 break;
10306
10307 case 'O':
10308 if (arg && arg[1] == '0')
10309 mips_optimize = 1;
10310 else
10311 mips_optimize = 2;
10312 break;
10313
10314 case 'g':
10315 if (arg == NULL)
10316 mips_debug = 2;
10317 else
10318 mips_debug = atoi (arg);
10319 /* When the MIPS assembler sees -g or -g2, it does not do
10320 optimizations which limit full symbolic debugging. We take
10321 that to be equivalent to -O0. */
10322 if (mips_debug == 2)
10323 mips_optimize = 1;
10324 break;
10325
10326 case OPTION_MIPS1:
10327 file_mips_isa = ISA_MIPS1;
10328 break;
10329
10330 case OPTION_MIPS2:
10331 file_mips_isa = ISA_MIPS2;
10332 break;
10333
10334 case OPTION_MIPS3:
10335 file_mips_isa = ISA_MIPS3;
10336 break;
10337
10338 case OPTION_MIPS4:
10339 file_mips_isa = ISA_MIPS4;
10340 break;
10341
10342 case OPTION_MIPS5:
10343 file_mips_isa = ISA_MIPS5;
10344 break;
10345
10346 case OPTION_MIPS32:
10347 file_mips_isa = ISA_MIPS32;
10348 break;
10349
10350 case OPTION_MIPS32R2:
10351 file_mips_isa = ISA_MIPS32R2;
10352 break;
10353
10354 case OPTION_MIPS64R2:
10355 file_mips_isa = ISA_MIPS64R2;
10356 break;
10357
10358 case OPTION_MIPS64:
10359 file_mips_isa = ISA_MIPS64;
10360 break;
10361
10362 case OPTION_MTUNE:
10363 mips_set_option_string (&mips_tune_string, arg);
10364 break;
10365
10366 case OPTION_MARCH:
10367 mips_set_option_string (&mips_arch_string, arg);
10368 break;
10369
10370 case OPTION_M4650:
10371 mips_set_option_string (&mips_arch_string, "4650");
10372 mips_set_option_string (&mips_tune_string, "4650");
10373 break;
10374
10375 case OPTION_NO_M4650:
10376 break;
10377
10378 case OPTION_M4010:
10379 mips_set_option_string (&mips_arch_string, "4010");
10380 mips_set_option_string (&mips_tune_string, "4010");
10381 break;
10382
10383 case OPTION_NO_M4010:
10384 break;
10385
10386 case OPTION_M4100:
10387 mips_set_option_string (&mips_arch_string, "4100");
10388 mips_set_option_string (&mips_tune_string, "4100");
10389 break;
10390
10391 case OPTION_NO_M4100:
10392 break;
10393
10394 case OPTION_M3900:
10395 mips_set_option_string (&mips_arch_string, "3900");
10396 mips_set_option_string (&mips_tune_string, "3900");
10397 break;
10398
10399 case OPTION_NO_M3900:
10400 break;
10401
10402 case OPTION_MDMX:
10403 mips_opts.ase_mdmx = 1;
10404 break;
10405
10406 case OPTION_NO_MDMX:
10407 mips_opts.ase_mdmx = 0;
10408 break;
10409
10410 case OPTION_MIPS16:
10411 mips_opts.mips16 = 1;
10412 mips_no_prev_insn (FALSE);
10413 break;
10414
10415 case OPTION_NO_MIPS16:
10416 mips_opts.mips16 = 0;
10417 mips_no_prev_insn (FALSE);
10418 break;
10419
10420 case OPTION_MIPS3D:
10421 mips_opts.ase_mips3d = 1;
10422 break;
10423
10424 case OPTION_NO_MIPS3D:
10425 mips_opts.ase_mips3d = 0;
10426 break;
10427
10428 case OPTION_FIX_VR4120:
10429 mips_fix_vr4120 = 1;
10430 break;
10431
10432 case OPTION_NO_FIX_VR4120:
10433 mips_fix_vr4120 = 0;
10434 break;
10435
10436 case OPTION_RELAX_BRANCH:
10437 mips_relax_branch = 1;
10438 break;
10439
10440 case OPTION_NO_RELAX_BRANCH:
10441 mips_relax_branch = 0;
10442 break;
10443
10444 case OPTION_MSHARED:
10445 mips_in_shared = TRUE;
10446 break;
10447
10448 case OPTION_MNO_SHARED:
10449 mips_in_shared = FALSE;
10450 break;
10451
10452 case OPTION_MSYM32:
10453 mips_opts.sym32 = TRUE;
10454 break;
10455
10456 case OPTION_MNO_SYM32:
10457 mips_opts.sym32 = FALSE;
10458 break;
10459
10460 #ifdef OBJ_ELF
10461 /* When generating ELF code, we permit -KPIC and -call_shared to
10462 select SVR4_PIC, and -non_shared to select no PIC. This is
10463 intended to be compatible with Irix 5. */
10464 case OPTION_CALL_SHARED:
10465 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10466 {
10467 as_bad (_("-call_shared is supported only for ELF format"));
10468 return 0;
10469 }
10470 mips_pic = SVR4_PIC;
10471 mips_abicalls = TRUE;
10472 if (g_switch_seen && g_switch_value != 0)
10473 {
10474 as_bad (_("-G may not be used with SVR4 PIC code"));
10475 return 0;
10476 }
10477 g_switch_value = 0;
10478 break;
10479
10480 case OPTION_NON_SHARED:
10481 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10482 {
10483 as_bad (_("-non_shared is supported only for ELF format"));
10484 return 0;
10485 }
10486 mips_pic = NO_PIC;
10487 mips_abicalls = FALSE;
10488 break;
10489
10490 /* The -xgot option tells the assembler to use 32 offsets when
10491 accessing the got in SVR4_PIC mode. It is for Irix
10492 compatibility. */
10493 case OPTION_XGOT:
10494 mips_big_got = 1;
10495 break;
10496 #endif /* OBJ_ELF */
10497
10498 case 'G':
10499 g_switch_value = atoi (arg);
10500 g_switch_seen = 1;
10501 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10502 {
10503 as_bad (_("-G may not be used with SVR4 PIC code"));
10504 return 0;
10505 }
10506 break;
10507
10508 #ifdef OBJ_ELF
10509 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10510 and -mabi=64. */
10511 case OPTION_32:
10512 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10513 {
10514 as_bad (_("-32 is supported for ELF format only"));
10515 return 0;
10516 }
10517 mips_abi = O32_ABI;
10518 break;
10519
10520 case OPTION_N32:
10521 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10522 {
10523 as_bad (_("-n32 is supported for ELF format only"));
10524 return 0;
10525 }
10526 mips_abi = N32_ABI;
10527 break;
10528
10529 case OPTION_64:
10530 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10531 {
10532 as_bad (_("-64 is supported for ELF format only"));
10533 return 0;
10534 }
10535 mips_abi = N64_ABI;
10536 if (! support_64bit_objects())
10537 as_fatal (_("No compiled in support for 64 bit object file format"));
10538 break;
10539 #endif /* OBJ_ELF */
10540
10541 case OPTION_GP32:
10542 file_mips_gp32 = 1;
10543 break;
10544
10545 case OPTION_GP64:
10546 file_mips_gp32 = 0;
10547 break;
10548
10549 case OPTION_FP32:
10550 file_mips_fp32 = 1;
10551 break;
10552
10553 case OPTION_FP64:
10554 file_mips_fp32 = 0;
10555 break;
10556
10557 #ifdef OBJ_ELF
10558 case OPTION_MABI:
10559 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10560 {
10561 as_bad (_("-mabi is supported for ELF format only"));
10562 return 0;
10563 }
10564 if (strcmp (arg, "32") == 0)
10565 mips_abi = O32_ABI;
10566 else if (strcmp (arg, "o64") == 0)
10567 mips_abi = O64_ABI;
10568 else if (strcmp (arg, "n32") == 0)
10569 mips_abi = N32_ABI;
10570 else if (strcmp (arg, "64") == 0)
10571 {
10572 mips_abi = N64_ABI;
10573 if (! support_64bit_objects())
10574 as_fatal (_("No compiled in support for 64 bit object file "
10575 "format"));
10576 }
10577 else if (strcmp (arg, "eabi") == 0)
10578 mips_abi = EABI_ABI;
10579 else
10580 {
10581 as_fatal (_("invalid abi -mabi=%s"), arg);
10582 return 0;
10583 }
10584 break;
10585 #endif /* OBJ_ELF */
10586
10587 case OPTION_M7000_HILO_FIX:
10588 mips_7000_hilo_fix = TRUE;
10589 break;
10590
10591 case OPTION_MNO_7000_HILO_FIX:
10592 mips_7000_hilo_fix = FALSE;
10593 break;
10594
10595 #ifdef OBJ_ELF
10596 case OPTION_MDEBUG:
10597 mips_flag_mdebug = TRUE;
10598 break;
10599
10600 case OPTION_NO_MDEBUG:
10601 mips_flag_mdebug = FALSE;
10602 break;
10603
10604 case OPTION_PDR:
10605 mips_flag_pdr = TRUE;
10606 break;
10607
10608 case OPTION_NO_PDR:
10609 mips_flag_pdr = FALSE;
10610 break;
10611 #endif /* OBJ_ELF */
10612
10613 default:
10614 return 0;
10615 }
10616
10617 return 1;
10618 }
10619 \f
10620 /* Set up globals to generate code for the ISA or processor
10621 described by INFO. */
10622
10623 static void
10624 mips_set_architecture (const struct mips_cpu_info *info)
10625 {
10626 if (info != 0)
10627 {
10628 file_mips_arch = info->cpu;
10629 mips_opts.arch = info->cpu;
10630 mips_opts.isa = info->isa;
10631 }
10632 }
10633
10634
10635 /* Likewise for tuning. */
10636
10637 static void
10638 mips_set_tune (const struct mips_cpu_info *info)
10639 {
10640 if (info != 0)
10641 mips_tune = info->cpu;
10642 }
10643
10644
10645 void
10646 mips_after_parse_args (void)
10647 {
10648 const struct mips_cpu_info *arch_info = 0;
10649 const struct mips_cpu_info *tune_info = 0;
10650
10651 /* GP relative stuff not working for PE */
10652 if (strncmp (TARGET_OS, "pe", 2) == 0)
10653 {
10654 if (g_switch_seen && g_switch_value != 0)
10655 as_bad (_("-G not supported in this configuration."));
10656 g_switch_value = 0;
10657 }
10658
10659 if (mips_abi == NO_ABI)
10660 mips_abi = MIPS_DEFAULT_ABI;
10661
10662 /* The following code determines the architecture and register size.
10663 Similar code was added to GCC 3.3 (see override_options() in
10664 config/mips/mips.c). The GAS and GCC code should be kept in sync
10665 as much as possible. */
10666
10667 if (mips_arch_string != 0)
10668 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10669
10670 if (file_mips_isa != ISA_UNKNOWN)
10671 {
10672 /* Handle -mipsN. At this point, file_mips_isa contains the
10673 ISA level specified by -mipsN, while arch_info->isa contains
10674 the -march selection (if any). */
10675 if (arch_info != 0)
10676 {
10677 /* -march takes precedence over -mipsN, since it is more descriptive.
10678 There's no harm in specifying both as long as the ISA levels
10679 are the same. */
10680 if (file_mips_isa != arch_info->isa)
10681 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10682 mips_cpu_info_from_isa (file_mips_isa)->name,
10683 mips_cpu_info_from_isa (arch_info->isa)->name);
10684 }
10685 else
10686 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10687 }
10688
10689 if (arch_info == 0)
10690 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10691
10692 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10693 as_bad ("-march=%s is not compatible with the selected ABI",
10694 arch_info->name);
10695
10696 mips_set_architecture (arch_info);
10697
10698 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10699 if (mips_tune_string != 0)
10700 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10701
10702 if (tune_info == 0)
10703 mips_set_tune (arch_info);
10704 else
10705 mips_set_tune (tune_info);
10706
10707 if (file_mips_gp32 >= 0)
10708 {
10709 /* The user specified the size of the integer registers. Make sure
10710 it agrees with the ABI and ISA. */
10711 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10712 as_bad (_("-mgp64 used with a 32-bit processor"));
10713 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10714 as_bad (_("-mgp32 used with a 64-bit ABI"));
10715 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10716 as_bad (_("-mgp64 used with a 32-bit ABI"));
10717 }
10718 else
10719 {
10720 /* Infer the integer register size from the ABI and processor.
10721 Restrict ourselves to 32-bit registers if that's all the
10722 processor has, or if the ABI cannot handle 64-bit registers. */
10723 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10724 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10725 }
10726
10727 /* ??? GAS treats single-float processors as though they had 64-bit
10728 float registers (although it complains when double-precision
10729 instructions are used). As things stand, saying they have 32-bit
10730 registers would lead to spurious "register must be even" messages.
10731 So here we assume float registers are always the same size as
10732 integer ones, unless the user says otherwise. */
10733 if (file_mips_fp32 < 0)
10734 file_mips_fp32 = file_mips_gp32;
10735
10736 /* End of GCC-shared inference code. */
10737
10738 /* This flag is set when we have a 64-bit capable CPU but use only
10739 32-bit wide registers. Note that EABI does not use it. */
10740 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10741 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10742 || mips_abi == O32_ABI))
10743 mips_32bitmode = 1;
10744
10745 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10746 as_bad (_("trap exception not supported at ISA 1"));
10747
10748 /* If the selected architecture includes support for ASEs, enable
10749 generation of code for them. */
10750 if (mips_opts.mips16 == -1)
10751 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10752 if (mips_opts.ase_mips3d == -1)
10753 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10754 if (mips_opts.ase_mdmx == -1)
10755 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10756
10757 file_mips_isa = mips_opts.isa;
10758 file_ase_mips16 = mips_opts.mips16;
10759 file_ase_mips3d = mips_opts.ase_mips3d;
10760 file_ase_mdmx = mips_opts.ase_mdmx;
10761 mips_opts.gp32 = file_mips_gp32;
10762 mips_opts.fp32 = file_mips_fp32;
10763
10764 if (mips_flag_mdebug < 0)
10765 {
10766 #ifdef OBJ_MAYBE_ECOFF
10767 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10768 mips_flag_mdebug = 1;
10769 else
10770 #endif /* OBJ_MAYBE_ECOFF */
10771 mips_flag_mdebug = 0;
10772 }
10773 }
10774 \f
10775 void
10776 mips_init_after_args (void)
10777 {
10778 /* initialize opcodes */
10779 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10780 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10781 }
10782
10783 long
10784 md_pcrel_from (fixS *fixP)
10785 {
10786 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10787 switch (fixP->fx_r_type)
10788 {
10789 case BFD_RELOC_16_PCREL_S2:
10790 case BFD_RELOC_MIPS_JMP:
10791 /* Return the address of the delay slot. */
10792 return addr + 4;
10793 default:
10794 return addr;
10795 }
10796 }
10797
10798 /* This is called before the symbol table is processed. In order to
10799 work with gcc when using mips-tfile, we must keep all local labels.
10800 However, in other cases, we want to discard them. If we were
10801 called with -g, but we didn't see any debugging information, it may
10802 mean that gcc is smuggling debugging information through to
10803 mips-tfile, in which case we must generate all local labels. */
10804
10805 void
10806 mips_frob_file_before_adjust (void)
10807 {
10808 #ifndef NO_ECOFF_DEBUGGING
10809 if (ECOFF_DEBUGGING
10810 && mips_debug != 0
10811 && ! ecoff_debugging_seen)
10812 flag_keep_locals = 1;
10813 #endif
10814 }
10815
10816 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10817 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10818 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10819 relocation operators.
10820
10821 For our purposes, a %lo() expression matches a %got() or %hi()
10822 expression if:
10823
10824 (a) it refers to the same symbol; and
10825 (b) the offset applied in the %lo() expression is no lower than
10826 the offset applied in the %got() or %hi().
10827
10828 (b) allows us to cope with code like:
10829
10830 lui $4,%hi(foo)
10831 lh $4,%lo(foo+2)($4)
10832
10833 ...which is legal on RELA targets, and has a well-defined behaviour
10834 if the user knows that adding 2 to "foo" will not induce a carry to
10835 the high 16 bits.
10836
10837 When several %lo()s match a particular %got() or %hi(), we use the
10838 following rules to distinguish them:
10839
10840 (1) %lo()s with smaller offsets are a better match than %lo()s with
10841 higher offsets.
10842
10843 (2) %lo()s with no matching %got() or %hi() are better than those
10844 that already have a matching %got() or %hi().
10845
10846 (3) later %lo()s are better than earlier %lo()s.
10847
10848 These rules are applied in order.
10849
10850 (1) means, among other things, that %lo()s with identical offsets are
10851 chosen if they exist.
10852
10853 (2) means that we won't associate several high-part relocations with
10854 the same low-part relocation unless there's no alternative. Having
10855 several high parts for the same low part is a GNU extension; this rule
10856 allows careful users to avoid it.
10857
10858 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10859 with the last high-part relocation being at the front of the list.
10860 It therefore makes sense to choose the last matching low-part
10861 relocation, all other things being equal. It's also easier
10862 to code that way. */
10863
10864 void
10865 mips_frob_file (void)
10866 {
10867 struct mips_hi_fixup *l;
10868
10869 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10870 {
10871 segment_info_type *seginfo;
10872 bfd_boolean matched_lo_p;
10873 fixS **hi_pos, **lo_pos, **pos;
10874
10875 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10876
10877 /* If a GOT16 relocation turns out to be against a global symbol,
10878 there isn't supposed to be a matching LO. */
10879 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10880 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10881 continue;
10882
10883 /* Check quickly whether the next fixup happens to be a matching %lo. */
10884 if (fixup_has_matching_lo_p (l->fixp))
10885 continue;
10886
10887 seginfo = seg_info (l->seg);
10888
10889 /* Set HI_POS to the position of this relocation in the chain.
10890 Set LO_POS to the position of the chosen low-part relocation.
10891 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10892 relocation that matches an immediately-preceding high-part
10893 relocation. */
10894 hi_pos = NULL;
10895 lo_pos = NULL;
10896 matched_lo_p = FALSE;
10897 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10898 {
10899 if (*pos == l->fixp)
10900 hi_pos = pos;
10901
10902 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10903 && (*pos)->fx_addsy == l->fixp->fx_addsy
10904 && (*pos)->fx_offset >= l->fixp->fx_offset
10905 && (lo_pos == NULL
10906 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10907 || (!matched_lo_p
10908 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10909 lo_pos = pos;
10910
10911 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10912 && fixup_has_matching_lo_p (*pos));
10913 }
10914
10915 /* If we found a match, remove the high-part relocation from its
10916 current position and insert it before the low-part relocation.
10917 Make the offsets match so that fixup_has_matching_lo_p()
10918 will return true.
10919
10920 We don't warn about unmatched high-part relocations since some
10921 versions of gcc have been known to emit dead "lui ...%hi(...)"
10922 instructions. */
10923 if (lo_pos != NULL)
10924 {
10925 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10926 if (l->fixp->fx_next != *lo_pos)
10927 {
10928 *hi_pos = l->fixp->fx_next;
10929 l->fixp->fx_next = *lo_pos;
10930 *lo_pos = l->fixp;
10931 }
10932 }
10933 }
10934 }
10935
10936 /* We may have combined relocations without symbols in the N32/N64 ABI.
10937 We have to prevent gas from dropping them. */
10938
10939 int
10940 mips_force_relocation (fixS *fixp)
10941 {
10942 if (generic_force_reloc (fixp))
10943 return 1;
10944
10945 if (HAVE_NEWABI
10946 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10947 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10948 || fixp->fx_r_type == BFD_RELOC_HI16_S
10949 || fixp->fx_r_type == BFD_RELOC_LO16))
10950 return 1;
10951
10952 return 0;
10953 }
10954
10955 /* This hook is called before a fix is simplified. We don't really
10956 decide whether to skip a fix here. Rather, we turn global symbols
10957 used as branch targets into local symbols, such that they undergo
10958 simplification. We can only do this if the symbol is defined and
10959 it is in the same section as the branch. If this doesn't hold, we
10960 emit a better error message than just saying the relocation is not
10961 valid for the selected object format.
10962
10963 FIXP is the fix-up we're going to try to simplify, SEG is the
10964 segment in which the fix up occurs. The return value should be
10965 non-zero to indicate the fix-up is valid for further
10966 simplifications. */
10967
10968 int
10969 mips_validate_fix (struct fix *fixP, asection *seg)
10970 {
10971 /* There's a lot of discussion on whether it should be possible to
10972 use R_MIPS_PC16 to represent branch relocations. The outcome
10973 seems to be that it can, but gas/bfd are very broken in creating
10974 RELA relocations for this, so for now we only accept branches to
10975 symbols in the same section. Anything else is of dubious value,
10976 since there's no guarantee that at link time the symbol would be
10977 in range. Even for branches to local symbols this is arguably
10978 wrong, since it we assume the symbol is not going to be
10979 overridden, which should be possible per ELF library semantics,
10980 but then, there isn't a dynamic relocation that could be used to
10981 this effect, and the target would likely be out of range as well.
10982
10983 Unfortunately, it seems that there is too much code out there
10984 that relies on branches to symbols that are global to be resolved
10985 as if they were local, like the IRIX tools do, so we do it as
10986 well, but with a warning so that people are reminded to fix their
10987 code. If we ever get back to using R_MIPS_PC16 for branch
10988 targets, this entire block should go away (and probably the
10989 whole function). */
10990
10991 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10992 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10993 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10994 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10995 && fixP->fx_addsy)
10996 {
10997 if (! S_IS_DEFINED (fixP->fx_addsy))
10998 {
10999 as_bad_where (fixP->fx_file, fixP->fx_line,
11000 _("Cannot branch to undefined symbol."));
11001 /* Avoid any further errors about this fixup. */
11002 fixP->fx_done = 1;
11003 }
11004 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11005 {
11006 as_bad_where (fixP->fx_file, fixP->fx_line,
11007 _("Cannot branch to symbol in another section."));
11008 fixP->fx_done = 1;
11009 }
11010 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11011 {
11012 symbolS *sym = fixP->fx_addsy;
11013
11014 if (mips_pic == SVR4_PIC)
11015 as_warn_where (fixP->fx_file, fixP->fx_line,
11016 _("Pretending global symbol used as branch target is local."));
11017
11018 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11019 S_GET_SEGMENT (sym),
11020 S_GET_VALUE (sym),
11021 symbol_get_frag (sym));
11022 copy_symbol_attributes (fixP->fx_addsy, sym);
11023 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11024 assert (symbol_resolved_p (sym));
11025 symbol_mark_resolved (fixP->fx_addsy);
11026 }
11027 }
11028
11029 return 1;
11030 }
11031
11032 /* Apply a fixup to the object file. */
11033
11034 void
11035 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11036 {
11037 bfd_byte *buf;
11038 long insn;
11039 reloc_howto_type *howto;
11040
11041 /* We ignore generic BFD relocations we don't know about. */
11042 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11043 if (! howto)
11044 return;
11045
11046 assert (fixP->fx_size == 4
11047 || fixP->fx_r_type == BFD_RELOC_16
11048 || fixP->fx_r_type == BFD_RELOC_64
11049 || fixP->fx_r_type == BFD_RELOC_CTOR
11050 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11051 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11052 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11053
11054 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11055
11056 assert (! fixP->fx_pcrel);
11057
11058 /* Don't treat parts of a composite relocation as done. There are two
11059 reasons for this:
11060
11061 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11062 should nevertheless be emitted if the first part is.
11063
11064 (2) In normal usage, composite relocations are never assembly-time
11065 constants. The easiest way of dealing with the pathological
11066 exceptions is to generate a relocation against STN_UNDEF and
11067 leave everything up to the linker. */
11068 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
11069 fixP->fx_done = 1;
11070
11071 switch (fixP->fx_r_type)
11072 {
11073 case BFD_RELOC_MIPS_TLS_GD:
11074 case BFD_RELOC_MIPS_TLS_LDM:
11075 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11076 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11077 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11078 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11079 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11080 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11081 /* fall through */
11082
11083 case BFD_RELOC_MIPS_JMP:
11084 case BFD_RELOC_MIPS_SHIFT5:
11085 case BFD_RELOC_MIPS_SHIFT6:
11086 case BFD_RELOC_MIPS_GOT_DISP:
11087 case BFD_RELOC_MIPS_GOT_PAGE:
11088 case BFD_RELOC_MIPS_GOT_OFST:
11089 case BFD_RELOC_MIPS_SUB:
11090 case BFD_RELOC_MIPS_INSERT_A:
11091 case BFD_RELOC_MIPS_INSERT_B:
11092 case BFD_RELOC_MIPS_DELETE:
11093 case BFD_RELOC_MIPS_HIGHEST:
11094 case BFD_RELOC_MIPS_HIGHER:
11095 case BFD_RELOC_MIPS_SCN_DISP:
11096 case BFD_RELOC_MIPS_REL16:
11097 case BFD_RELOC_MIPS_RELGOT:
11098 case BFD_RELOC_MIPS_JALR:
11099 case BFD_RELOC_HI16:
11100 case BFD_RELOC_HI16_S:
11101 case BFD_RELOC_GPREL16:
11102 case BFD_RELOC_MIPS_LITERAL:
11103 case BFD_RELOC_MIPS_CALL16:
11104 case BFD_RELOC_MIPS_GOT16:
11105 case BFD_RELOC_GPREL32:
11106 case BFD_RELOC_MIPS_GOT_HI16:
11107 case BFD_RELOC_MIPS_GOT_LO16:
11108 case BFD_RELOC_MIPS_CALL_HI16:
11109 case BFD_RELOC_MIPS_CALL_LO16:
11110 case BFD_RELOC_MIPS16_GPREL:
11111 case BFD_RELOC_MIPS16_HI16:
11112 case BFD_RELOC_MIPS16_HI16_S:
11113 assert (! fixP->fx_pcrel);
11114 /* Nothing needed to do. The value comes from the reloc entry */
11115 break;
11116
11117 case BFD_RELOC_MIPS16_JMP:
11118 /* We currently always generate a reloc against a symbol, which
11119 means that we don't want an addend even if the symbol is
11120 defined. */
11121 *valP = 0;
11122 break;
11123
11124 case BFD_RELOC_64:
11125 /* This is handled like BFD_RELOC_32, but we output a sign
11126 extended value if we are only 32 bits. */
11127 if (fixP->fx_done)
11128 {
11129 if (8 <= sizeof (valueT))
11130 md_number_to_chars ((char *) buf, *valP, 8);
11131 else
11132 {
11133 valueT hiv;
11134
11135 if ((*valP & 0x80000000) != 0)
11136 hiv = 0xffffffff;
11137 else
11138 hiv = 0;
11139 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11140 *valP, 4);
11141 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11142 hiv, 4);
11143 }
11144 }
11145 break;
11146
11147 case BFD_RELOC_RVA:
11148 case BFD_RELOC_32:
11149 /* If we are deleting this reloc entry, we must fill in the
11150 value now. This can happen if we have a .word which is not
11151 resolved when it appears but is later defined. */
11152 if (fixP->fx_done)
11153 md_number_to_chars ((char *) buf, *valP, 4);
11154 break;
11155
11156 case BFD_RELOC_16:
11157 /* If we are deleting this reloc entry, we must fill in the
11158 value now. */
11159 if (fixP->fx_done)
11160 md_number_to_chars ((char *) buf, *valP, 2);
11161 break;
11162
11163 case BFD_RELOC_LO16:
11164 case BFD_RELOC_MIPS16_LO16:
11165 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11166 may be safe to remove, but if so it's not obvious. */
11167 /* When handling an embedded PIC switch statement, we can wind
11168 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11169 if (fixP->fx_done)
11170 {
11171 if (*valP + 0x8000 > 0xffff)
11172 as_bad_where (fixP->fx_file, fixP->fx_line,
11173 _("relocation overflow"));
11174 if (target_big_endian)
11175 buf += 2;
11176 md_number_to_chars ((char *) buf, *valP, 2);
11177 }
11178 break;
11179
11180 case BFD_RELOC_16_PCREL_S2:
11181 if ((*valP & 0x3) != 0)
11182 as_bad_where (fixP->fx_file, fixP->fx_line,
11183 _("Branch to odd address (%lx)"), (long) *valP);
11184
11185 /*
11186 * We need to save the bits in the instruction since fixup_segment()
11187 * might be deleting the relocation entry (i.e., a branch within
11188 * the current segment).
11189 */
11190 if (! fixP->fx_done)
11191 break;
11192
11193 /* update old instruction data */
11194 if (target_big_endian)
11195 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11196 else
11197 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11198
11199 if (*valP + 0x20000 <= 0x3ffff)
11200 {
11201 insn |= (*valP >> 2) & 0xffff;
11202 md_number_to_chars ((char *) buf, insn, 4);
11203 }
11204 else if (mips_pic == NO_PIC
11205 && fixP->fx_done
11206 && fixP->fx_frag->fr_address >= text_section->vma
11207 && (fixP->fx_frag->fr_address
11208 < text_section->vma + bfd_get_section_size (text_section))
11209 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11210 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11211 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11212 {
11213 /* The branch offset is too large. If this is an
11214 unconditional branch, and we are not generating PIC code,
11215 we can convert it to an absolute jump instruction. */
11216 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11217 insn = 0x0c000000; /* jal */
11218 else
11219 insn = 0x08000000; /* j */
11220 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11221 fixP->fx_done = 0;
11222 fixP->fx_addsy = section_symbol (text_section);
11223 *valP += md_pcrel_from (fixP);
11224 md_number_to_chars ((char *) buf, insn, 4);
11225 }
11226 else
11227 {
11228 /* If we got here, we have branch-relaxation disabled,
11229 and there's nothing we can do to fix this instruction
11230 without turning it into a longer sequence. */
11231 as_bad_where (fixP->fx_file, fixP->fx_line,
11232 _("Branch out of range"));
11233 }
11234 break;
11235
11236 case BFD_RELOC_VTABLE_INHERIT:
11237 fixP->fx_done = 0;
11238 if (fixP->fx_addsy
11239 && !S_IS_DEFINED (fixP->fx_addsy)
11240 && !S_IS_WEAK (fixP->fx_addsy))
11241 S_SET_WEAK (fixP->fx_addsy);
11242 break;
11243
11244 case BFD_RELOC_VTABLE_ENTRY:
11245 fixP->fx_done = 0;
11246 break;
11247
11248 default:
11249 internalError ();
11250 }
11251
11252 /* Remember value for tc_gen_reloc. */
11253 fixP->fx_addnumber = *valP;
11254 }
11255
11256 static symbolS *
11257 get_symbol (void)
11258 {
11259 int c;
11260 char *name;
11261 symbolS *p;
11262
11263 name = input_line_pointer;
11264 c = get_symbol_end ();
11265 p = (symbolS *) symbol_find_or_make (name);
11266 *input_line_pointer = c;
11267 return p;
11268 }
11269
11270 /* Align the current frag to a given power of two. The MIPS assembler
11271 also automatically adjusts any preceding label. */
11272
11273 static void
11274 mips_align (int to, int fill, symbolS *label)
11275 {
11276 mips_emit_delays (FALSE);
11277 frag_align (to, fill, 0);
11278 record_alignment (now_seg, to);
11279 if (label != NULL)
11280 {
11281 assert (S_GET_SEGMENT (label) == now_seg);
11282 symbol_set_frag (label, frag_now);
11283 S_SET_VALUE (label, (valueT) frag_now_fix ());
11284 }
11285 }
11286
11287 /* Align to a given power of two. .align 0 turns off the automatic
11288 alignment used by the data creating pseudo-ops. */
11289
11290 static void
11291 s_align (int x ATTRIBUTE_UNUSED)
11292 {
11293 register int temp;
11294 register long temp_fill;
11295 long max_alignment = 15;
11296
11297 /*
11298
11299 o Note that the assembler pulls down any immediately preceding label
11300 to the aligned address.
11301 o It's not documented but auto alignment is reinstated by
11302 a .align pseudo instruction.
11303 o Note also that after auto alignment is turned off the mips assembler
11304 issues an error on attempt to assemble an improperly aligned data item.
11305 We don't.
11306
11307 */
11308
11309 temp = get_absolute_expression ();
11310 if (temp > max_alignment)
11311 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11312 else if (temp < 0)
11313 {
11314 as_warn (_("Alignment negative: 0 assumed."));
11315 temp = 0;
11316 }
11317 if (*input_line_pointer == ',')
11318 {
11319 ++input_line_pointer;
11320 temp_fill = get_absolute_expression ();
11321 }
11322 else
11323 temp_fill = 0;
11324 if (temp)
11325 {
11326 auto_align = 1;
11327 mips_align (temp, (int) temp_fill,
11328 insn_labels != NULL ? insn_labels->label : NULL);
11329 }
11330 else
11331 {
11332 auto_align = 0;
11333 }
11334
11335 demand_empty_rest_of_line ();
11336 }
11337
11338 void
11339 mips_flush_pending_output (void)
11340 {
11341 mips_emit_delays (FALSE);
11342 mips_clear_insn_labels ();
11343 }
11344
11345 static void
11346 s_change_sec (int sec)
11347 {
11348 segT seg;
11349
11350 #ifdef OBJ_ELF
11351 /* The ELF backend needs to know that we are changing sections, so
11352 that .previous works correctly. We could do something like check
11353 for an obj_section_change_hook macro, but that might be confusing
11354 as it would not be appropriate to use it in the section changing
11355 functions in read.c, since obj-elf.c intercepts those. FIXME:
11356 This should be cleaner, somehow. */
11357 obj_elf_section_change_hook ();
11358 #endif
11359
11360 mips_emit_delays (FALSE);
11361 switch (sec)
11362 {
11363 case 't':
11364 s_text (0);
11365 break;
11366 case 'd':
11367 s_data (0);
11368 break;
11369 case 'b':
11370 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11371 demand_empty_rest_of_line ();
11372 break;
11373
11374 case 'r':
11375 seg = subseg_new (RDATA_SECTION_NAME,
11376 (subsegT) get_absolute_expression ());
11377 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11378 {
11379 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11380 | SEC_READONLY | SEC_RELOC
11381 | SEC_DATA));
11382 if (strcmp (TARGET_OS, "elf") != 0)
11383 record_alignment (seg, 4);
11384 }
11385 demand_empty_rest_of_line ();
11386 break;
11387
11388 case 's':
11389 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11390 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11391 {
11392 bfd_set_section_flags (stdoutput, seg,
11393 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11394 if (strcmp (TARGET_OS, "elf") != 0)
11395 record_alignment (seg, 4);
11396 }
11397 demand_empty_rest_of_line ();
11398 break;
11399 }
11400
11401 auto_align = 1;
11402 }
11403
11404 void
11405 s_change_section (int ignore ATTRIBUTE_UNUSED)
11406 {
11407 #ifdef OBJ_ELF
11408 char *section_name;
11409 char c;
11410 char next_c = 0;
11411 int section_type;
11412 int section_flag;
11413 int section_entry_size;
11414 int section_alignment;
11415
11416 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11417 return;
11418
11419 section_name = input_line_pointer;
11420 c = get_symbol_end ();
11421 if (c)
11422 next_c = *(input_line_pointer + 1);
11423
11424 /* Do we have .section Name<,"flags">? */
11425 if (c != ',' || (c == ',' && next_c == '"'))
11426 {
11427 /* just after name is now '\0'. */
11428 *input_line_pointer = c;
11429 input_line_pointer = section_name;
11430 obj_elf_section (ignore);
11431 return;
11432 }
11433 input_line_pointer++;
11434
11435 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11436 if (c == ',')
11437 section_type = get_absolute_expression ();
11438 else
11439 section_type = 0;
11440 if (*input_line_pointer++ == ',')
11441 section_flag = get_absolute_expression ();
11442 else
11443 section_flag = 0;
11444 if (*input_line_pointer++ == ',')
11445 section_entry_size = get_absolute_expression ();
11446 else
11447 section_entry_size = 0;
11448 if (*input_line_pointer++ == ',')
11449 section_alignment = get_absolute_expression ();
11450 else
11451 section_alignment = 0;
11452
11453 section_name = xstrdup (section_name);
11454
11455 /* When using the generic form of .section (as implemented by obj-elf.c),
11456 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11457 traditionally had to fall back on the more common @progbits instead.
11458
11459 There's nothing really harmful in this, since bfd will correct
11460 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11461 means that, for backwards compatibiltiy, the special_section entries
11462 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11463
11464 Even so, we shouldn't force users of the MIPS .section syntax to
11465 incorrectly label the sections as SHT_PROGBITS. The best compromise
11466 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11467 generic type-checking code. */
11468 if (section_type == SHT_MIPS_DWARF)
11469 section_type = SHT_PROGBITS;
11470
11471 obj_elf_change_section (section_name, section_type, section_flag,
11472 section_entry_size, 0, 0, 0);
11473
11474 if (now_seg->name != section_name)
11475 free (section_name);
11476 #endif /* OBJ_ELF */
11477 }
11478
11479 void
11480 mips_enable_auto_align (void)
11481 {
11482 auto_align = 1;
11483 }
11484
11485 static void
11486 s_cons (int log_size)
11487 {
11488 symbolS *label;
11489
11490 label = insn_labels != NULL ? insn_labels->label : NULL;
11491 mips_emit_delays (FALSE);
11492 if (log_size > 0 && auto_align)
11493 mips_align (log_size, 0, label);
11494 mips_clear_insn_labels ();
11495 cons (1 << log_size);
11496 }
11497
11498 static void
11499 s_float_cons (int type)
11500 {
11501 symbolS *label;
11502
11503 label = insn_labels != NULL ? insn_labels->label : NULL;
11504
11505 mips_emit_delays (FALSE);
11506
11507 if (auto_align)
11508 {
11509 if (type == 'd')
11510 mips_align (3, 0, label);
11511 else
11512 mips_align (2, 0, label);
11513 }
11514
11515 mips_clear_insn_labels ();
11516
11517 float_cons (type);
11518 }
11519
11520 /* Handle .globl. We need to override it because on Irix 5 you are
11521 permitted to say
11522 .globl foo .text
11523 where foo is an undefined symbol, to mean that foo should be
11524 considered to be the address of a function. */
11525
11526 static void
11527 s_mips_globl (int x ATTRIBUTE_UNUSED)
11528 {
11529 char *name;
11530 int c;
11531 symbolS *symbolP;
11532 flagword flag;
11533
11534 name = input_line_pointer;
11535 c = get_symbol_end ();
11536 symbolP = symbol_find_or_make (name);
11537 *input_line_pointer = c;
11538 SKIP_WHITESPACE ();
11539
11540 /* On Irix 5, every global symbol that is not explicitly labelled as
11541 being a function is apparently labelled as being an object. */
11542 flag = BSF_OBJECT;
11543
11544 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11545 {
11546 char *secname;
11547 asection *sec;
11548
11549 secname = input_line_pointer;
11550 c = get_symbol_end ();
11551 sec = bfd_get_section_by_name (stdoutput, secname);
11552 if (sec == NULL)
11553 as_bad (_("%s: no such section"), secname);
11554 *input_line_pointer = c;
11555
11556 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11557 flag = BSF_FUNCTION;
11558 }
11559
11560 symbol_get_bfdsym (symbolP)->flags |= flag;
11561
11562 S_SET_EXTERNAL (symbolP);
11563 demand_empty_rest_of_line ();
11564 }
11565
11566 static void
11567 s_option (int x ATTRIBUTE_UNUSED)
11568 {
11569 char *opt;
11570 char c;
11571
11572 opt = input_line_pointer;
11573 c = get_symbol_end ();
11574
11575 if (*opt == 'O')
11576 {
11577 /* FIXME: What does this mean? */
11578 }
11579 else if (strncmp (opt, "pic", 3) == 0)
11580 {
11581 int i;
11582
11583 i = atoi (opt + 3);
11584 if (i == 0)
11585 mips_pic = NO_PIC;
11586 else if (i == 2)
11587 {
11588 mips_pic = SVR4_PIC;
11589 mips_abicalls = TRUE;
11590 }
11591 else
11592 as_bad (_(".option pic%d not supported"), i);
11593
11594 if (mips_pic == SVR4_PIC)
11595 {
11596 if (g_switch_seen && g_switch_value != 0)
11597 as_warn (_("-G may not be used with SVR4 PIC code"));
11598 g_switch_value = 0;
11599 bfd_set_gp_size (stdoutput, 0);
11600 }
11601 }
11602 else
11603 as_warn (_("Unrecognized option \"%s\""), opt);
11604
11605 *input_line_pointer = c;
11606 demand_empty_rest_of_line ();
11607 }
11608
11609 /* This structure is used to hold a stack of .set values. */
11610
11611 struct mips_option_stack
11612 {
11613 struct mips_option_stack *next;
11614 struct mips_set_options options;
11615 };
11616
11617 static struct mips_option_stack *mips_opts_stack;
11618
11619 /* Handle the .set pseudo-op. */
11620
11621 static void
11622 s_mipsset (int x ATTRIBUTE_UNUSED)
11623 {
11624 char *name = input_line_pointer, ch;
11625
11626 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11627 ++input_line_pointer;
11628 ch = *input_line_pointer;
11629 *input_line_pointer = '\0';
11630
11631 if (strcmp (name, "reorder") == 0)
11632 {
11633 if (mips_opts.noreorder && prev_nop_frag != NULL)
11634 {
11635 /* If we still have pending nops, we can discard them. The
11636 usual nop handling will insert any that are still
11637 needed. */
11638 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11639 * (mips_opts.mips16 ? 2 : 4));
11640 prev_nop_frag = NULL;
11641 }
11642 mips_opts.noreorder = 0;
11643 }
11644 else if (strcmp (name, "noreorder") == 0)
11645 {
11646 mips_emit_delays (TRUE);
11647 mips_opts.noreorder = 1;
11648 mips_any_noreorder = 1;
11649 }
11650 else if (strcmp (name, "at") == 0)
11651 {
11652 mips_opts.noat = 0;
11653 }
11654 else if (strcmp (name, "noat") == 0)
11655 {
11656 mips_opts.noat = 1;
11657 }
11658 else if (strcmp (name, "macro") == 0)
11659 {
11660 mips_opts.warn_about_macros = 0;
11661 }
11662 else if (strcmp (name, "nomacro") == 0)
11663 {
11664 if (mips_opts.noreorder == 0)
11665 as_bad (_("`noreorder' must be set before `nomacro'"));
11666 mips_opts.warn_about_macros = 1;
11667 }
11668 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11669 {
11670 mips_opts.nomove = 0;
11671 }
11672 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11673 {
11674 mips_opts.nomove = 1;
11675 }
11676 else if (strcmp (name, "bopt") == 0)
11677 {
11678 mips_opts.nobopt = 0;
11679 }
11680 else if (strcmp (name, "nobopt") == 0)
11681 {
11682 mips_opts.nobopt = 1;
11683 }
11684 else if (strcmp (name, "mips16") == 0
11685 || strcmp (name, "MIPS-16") == 0)
11686 mips_opts.mips16 = 1;
11687 else if (strcmp (name, "nomips16") == 0
11688 || strcmp (name, "noMIPS-16") == 0)
11689 mips_opts.mips16 = 0;
11690 else if (strcmp (name, "mips3d") == 0)
11691 mips_opts.ase_mips3d = 1;
11692 else if (strcmp (name, "nomips3d") == 0)
11693 mips_opts.ase_mips3d = 0;
11694 else if (strcmp (name, "mdmx") == 0)
11695 mips_opts.ase_mdmx = 1;
11696 else if (strcmp (name, "nomdmx") == 0)
11697 mips_opts.ase_mdmx = 0;
11698 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11699 {
11700 int reset = 0;
11701
11702 /* Permit the user to change the ISA and architecture on the fly.
11703 Needless to say, misuse can cause serious problems. */
11704 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11705 {
11706 reset = 1;
11707 mips_opts.isa = file_mips_isa;
11708 mips_opts.arch = file_mips_arch;
11709 }
11710 else if (strncmp (name, "arch=", 5) == 0)
11711 {
11712 const struct mips_cpu_info *p;
11713
11714 p = mips_parse_cpu("internal use", name + 5);
11715 if (!p)
11716 as_bad (_("unknown architecture %s"), name + 5);
11717 else
11718 {
11719 mips_opts.arch = p->cpu;
11720 mips_opts.isa = p->isa;
11721 }
11722 }
11723 else if (strncmp (name, "mips", 4) == 0)
11724 {
11725 const struct mips_cpu_info *p;
11726
11727 p = mips_parse_cpu("internal use", name);
11728 if (!p)
11729 as_bad (_("unknown ISA level %s"), name + 4);
11730 else
11731 {
11732 mips_opts.arch = p->cpu;
11733 mips_opts.isa = p->isa;
11734 }
11735 }
11736 else
11737 as_bad (_("unknown ISA or architecture %s"), name);
11738
11739 switch (mips_opts.isa)
11740 {
11741 case 0:
11742 break;
11743 case ISA_MIPS1:
11744 case ISA_MIPS2:
11745 case ISA_MIPS32:
11746 case ISA_MIPS32R2:
11747 mips_opts.gp32 = 1;
11748 mips_opts.fp32 = 1;
11749 break;
11750 case ISA_MIPS3:
11751 case ISA_MIPS4:
11752 case ISA_MIPS5:
11753 case ISA_MIPS64:
11754 case ISA_MIPS64R2:
11755 mips_opts.gp32 = 0;
11756 mips_opts.fp32 = 0;
11757 break;
11758 default:
11759 as_bad (_("unknown ISA level %s"), name + 4);
11760 break;
11761 }
11762 if (reset)
11763 {
11764 mips_opts.gp32 = file_mips_gp32;
11765 mips_opts.fp32 = file_mips_fp32;
11766 }
11767 }
11768 else if (strcmp (name, "autoextend") == 0)
11769 mips_opts.noautoextend = 0;
11770 else if (strcmp (name, "noautoextend") == 0)
11771 mips_opts.noautoextend = 1;
11772 else if (strcmp (name, "push") == 0)
11773 {
11774 struct mips_option_stack *s;
11775
11776 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11777 s->next = mips_opts_stack;
11778 s->options = mips_opts;
11779 mips_opts_stack = s;
11780 }
11781 else if (strcmp (name, "pop") == 0)
11782 {
11783 struct mips_option_stack *s;
11784
11785 s = mips_opts_stack;
11786 if (s == NULL)
11787 as_bad (_(".set pop with no .set push"));
11788 else
11789 {
11790 /* If we're changing the reorder mode we need to handle
11791 delay slots correctly. */
11792 if (s->options.noreorder && ! mips_opts.noreorder)
11793 mips_emit_delays (TRUE);
11794 else if (! s->options.noreorder && mips_opts.noreorder)
11795 {
11796 if (prev_nop_frag != NULL)
11797 {
11798 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11799 * (mips_opts.mips16 ? 2 : 4));
11800 prev_nop_frag = NULL;
11801 }
11802 }
11803
11804 mips_opts = s->options;
11805 mips_opts_stack = s->next;
11806 free (s);
11807 }
11808 }
11809 else if (strcmp (name, "sym32") == 0)
11810 mips_opts.sym32 = TRUE;
11811 else if (strcmp (name, "nosym32") == 0)
11812 mips_opts.sym32 = FALSE;
11813 else
11814 {
11815 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11816 }
11817 *input_line_pointer = ch;
11818 demand_empty_rest_of_line ();
11819 }
11820
11821 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11822 .option pic2. It means to generate SVR4 PIC calls. */
11823
11824 static void
11825 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11826 {
11827 mips_pic = SVR4_PIC;
11828 mips_abicalls = TRUE;
11829
11830 if (g_switch_seen && g_switch_value != 0)
11831 as_warn (_("-G may not be used with SVR4 PIC code"));
11832 g_switch_value = 0;
11833
11834 bfd_set_gp_size (stdoutput, 0);
11835 demand_empty_rest_of_line ();
11836 }
11837
11838 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11839 PIC code. It sets the $gp register for the function based on the
11840 function address, which is in the register named in the argument.
11841 This uses a relocation against _gp_disp, which is handled specially
11842 by the linker. The result is:
11843 lui $gp,%hi(_gp_disp)
11844 addiu $gp,$gp,%lo(_gp_disp)
11845 addu $gp,$gp,.cpload argument
11846 The .cpload argument is normally $25 == $t9.
11847
11848 The -mno-shared option changes this to:
11849 lui $gp,%hi(__gnu_local_gp)
11850 addiu $gp,$gp,%lo(__gnu_local_gp)
11851 and the argument is ignored. This saves an instruction, but the
11852 resulting code is not position independent; it uses an absolute
11853 address for __gnu_local_gp. Thus code assembled with -mno-shared
11854 can go into an ordinary executable, but not into a shared library. */
11855
11856 static void
11857 s_cpload (int ignore ATTRIBUTE_UNUSED)
11858 {
11859 expressionS ex;
11860 int reg;
11861 int in_shared;
11862
11863 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11864 .cpload is ignored. */
11865 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11866 {
11867 s_ignore (0);
11868 return;
11869 }
11870
11871 /* .cpload should be in a .set noreorder section. */
11872 if (mips_opts.noreorder == 0)
11873 as_warn (_(".cpload not in noreorder section"));
11874
11875 reg = tc_get_register (0);
11876
11877 /* If we need to produce a 64-bit address, we are better off using
11878 the default instruction sequence. */
11879 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11880
11881 ex.X_op = O_symbol;
11882 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11883 "__gnu_local_gp");
11884 ex.X_op_symbol = NULL;
11885 ex.X_add_number = 0;
11886
11887 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11888 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11889
11890 macro_start ();
11891 macro_build_lui (&ex, mips_gp_register);
11892 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11893 mips_gp_register, BFD_RELOC_LO16);
11894 if (in_shared)
11895 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11896 mips_gp_register, reg);
11897 macro_end ();
11898
11899 demand_empty_rest_of_line ();
11900 }
11901
11902 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11903 .cpsetup $reg1, offset|$reg2, label
11904
11905 If offset is given, this results in:
11906 sd $gp, offset($sp)
11907 lui $gp, %hi(%neg(%gp_rel(label)))
11908 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11909 daddu $gp, $gp, $reg1
11910
11911 If $reg2 is given, this results in:
11912 daddu $reg2, $gp, $0
11913 lui $gp, %hi(%neg(%gp_rel(label)))
11914 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11915 daddu $gp, $gp, $reg1
11916 $reg1 is normally $25 == $t9.
11917
11918 The -mno-shared option replaces the last three instructions with
11919 lui $gp,%hi(_gp)
11920 addiu $gp,$gp,%lo(_gp)
11921 */
11922
11923 static void
11924 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11925 {
11926 expressionS ex_off;
11927 expressionS ex_sym;
11928 int reg1;
11929
11930 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11931 We also need NewABI support. */
11932 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11933 {
11934 s_ignore (0);
11935 return;
11936 }
11937
11938 reg1 = tc_get_register (0);
11939 SKIP_WHITESPACE ();
11940 if (*input_line_pointer != ',')
11941 {
11942 as_bad (_("missing argument separator ',' for .cpsetup"));
11943 return;
11944 }
11945 else
11946 ++input_line_pointer;
11947 SKIP_WHITESPACE ();
11948 if (*input_line_pointer == '$')
11949 {
11950 mips_cpreturn_register = tc_get_register (0);
11951 mips_cpreturn_offset = -1;
11952 }
11953 else
11954 {
11955 mips_cpreturn_offset = get_absolute_expression ();
11956 mips_cpreturn_register = -1;
11957 }
11958 SKIP_WHITESPACE ();
11959 if (*input_line_pointer != ',')
11960 {
11961 as_bad (_("missing argument separator ',' for .cpsetup"));
11962 return;
11963 }
11964 else
11965 ++input_line_pointer;
11966 SKIP_WHITESPACE ();
11967 expression (&ex_sym);
11968
11969 macro_start ();
11970 if (mips_cpreturn_register == -1)
11971 {
11972 ex_off.X_op = O_constant;
11973 ex_off.X_add_symbol = NULL;
11974 ex_off.X_op_symbol = NULL;
11975 ex_off.X_add_number = mips_cpreturn_offset;
11976
11977 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11978 BFD_RELOC_LO16, SP);
11979 }
11980 else
11981 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11982 mips_gp_register, 0);
11983
11984 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11985 {
11986 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11987 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11988 BFD_RELOC_HI16_S);
11989
11990 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11991 mips_gp_register, -1, BFD_RELOC_GPREL16,
11992 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11993
11994 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11995 mips_gp_register, reg1);
11996 }
11997 else
11998 {
11999 expressionS ex;
12000
12001 ex.X_op = O_symbol;
12002 ex.X_add_symbol = symbol_find_or_make ("_gp");
12003 ex.X_op_symbol = NULL;
12004 ex.X_add_number = 0;
12005
12006 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12007 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12008
12009 macro_build_lui (&ex, mips_gp_register);
12010 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12011 mips_gp_register, BFD_RELOC_LO16);
12012 }
12013
12014 macro_end ();
12015
12016 demand_empty_rest_of_line ();
12017 }
12018
12019 static void
12020 s_cplocal (int ignore ATTRIBUTE_UNUSED)
12021 {
12022 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12023 .cplocal is ignored. */
12024 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12025 {
12026 s_ignore (0);
12027 return;
12028 }
12029
12030 mips_gp_register = tc_get_register (0);
12031 demand_empty_rest_of_line ();
12032 }
12033
12034 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12035 offset from $sp. The offset is remembered, and after making a PIC
12036 call $gp is restored from that location. */
12037
12038 static void
12039 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12040 {
12041 expressionS ex;
12042
12043 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12044 .cprestore is ignored. */
12045 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12046 {
12047 s_ignore (0);
12048 return;
12049 }
12050
12051 mips_cprestore_offset = get_absolute_expression ();
12052 mips_cprestore_valid = 1;
12053
12054 ex.X_op = O_constant;
12055 ex.X_add_symbol = NULL;
12056 ex.X_op_symbol = NULL;
12057 ex.X_add_number = mips_cprestore_offset;
12058
12059 macro_start ();
12060 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12061 SP, HAVE_64BIT_ADDRESSES);
12062 macro_end ();
12063
12064 demand_empty_rest_of_line ();
12065 }
12066
12067 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12068 was given in the preceding .cpsetup, it results in:
12069 ld $gp, offset($sp)
12070
12071 If a register $reg2 was given there, it results in:
12072 daddu $gp, $reg2, $0
12073 */
12074 static void
12075 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12076 {
12077 expressionS ex;
12078
12079 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12080 We also need NewABI support. */
12081 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12082 {
12083 s_ignore (0);
12084 return;
12085 }
12086
12087 macro_start ();
12088 if (mips_cpreturn_register == -1)
12089 {
12090 ex.X_op = O_constant;
12091 ex.X_add_symbol = NULL;
12092 ex.X_op_symbol = NULL;
12093 ex.X_add_number = mips_cpreturn_offset;
12094
12095 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12096 }
12097 else
12098 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12099 mips_cpreturn_register, 0);
12100 macro_end ();
12101
12102 demand_empty_rest_of_line ();
12103 }
12104
12105 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12106 code. It sets the offset to use in gp_rel relocations. */
12107
12108 static void
12109 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12110 {
12111 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12112 We also need NewABI support. */
12113 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12114 {
12115 s_ignore (0);
12116 return;
12117 }
12118
12119 mips_gprel_offset = get_absolute_expression ();
12120
12121 demand_empty_rest_of_line ();
12122 }
12123
12124 /* Handle the .gpword pseudo-op. This is used when generating PIC
12125 code. It generates a 32 bit GP relative reloc. */
12126
12127 static void
12128 s_gpword (int ignore ATTRIBUTE_UNUSED)
12129 {
12130 symbolS *label;
12131 expressionS ex;
12132 char *p;
12133
12134 /* When not generating PIC code, this is treated as .word. */
12135 if (mips_pic != SVR4_PIC)
12136 {
12137 s_cons (2);
12138 return;
12139 }
12140
12141 label = insn_labels != NULL ? insn_labels->label : NULL;
12142 mips_emit_delays (TRUE);
12143 if (auto_align)
12144 mips_align (2, 0, label);
12145 mips_clear_insn_labels ();
12146
12147 expression (&ex);
12148
12149 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12150 {
12151 as_bad (_("Unsupported use of .gpword"));
12152 ignore_rest_of_line ();
12153 }
12154
12155 p = frag_more (4);
12156 md_number_to_chars (p, 0, 4);
12157 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12158 BFD_RELOC_GPREL32);
12159
12160 demand_empty_rest_of_line ();
12161 }
12162
12163 static void
12164 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12165 {
12166 symbolS *label;
12167 expressionS ex;
12168 char *p;
12169
12170 /* When not generating PIC code, this is treated as .dword. */
12171 if (mips_pic != SVR4_PIC)
12172 {
12173 s_cons (3);
12174 return;
12175 }
12176
12177 label = insn_labels != NULL ? insn_labels->label : NULL;
12178 mips_emit_delays (TRUE);
12179 if (auto_align)
12180 mips_align (3, 0, label);
12181 mips_clear_insn_labels ();
12182
12183 expression (&ex);
12184
12185 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12186 {
12187 as_bad (_("Unsupported use of .gpdword"));
12188 ignore_rest_of_line ();
12189 }
12190
12191 p = frag_more (8);
12192 md_number_to_chars (p, 0, 8);
12193 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12194 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12195
12196 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12197 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12198 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12199
12200 demand_empty_rest_of_line ();
12201 }
12202
12203 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12204 tables in SVR4 PIC code. */
12205
12206 static void
12207 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12208 {
12209 int reg;
12210
12211 /* This is ignored when not generating SVR4 PIC code. */
12212 if (mips_pic != SVR4_PIC)
12213 {
12214 s_ignore (0);
12215 return;
12216 }
12217
12218 /* Add $gp to the register named as an argument. */
12219 macro_start ();
12220 reg = tc_get_register (0);
12221 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12222 macro_end ();
12223
12224 demand_empty_rest_of_line ();
12225 }
12226
12227 /* Handle the .insn pseudo-op. This marks instruction labels in
12228 mips16 mode. This permits the linker to handle them specially,
12229 such as generating jalx instructions when needed. We also make
12230 them odd for the duration of the assembly, in order to generate the
12231 right sort of code. We will make them even in the adjust_symtab
12232 routine, while leaving them marked. This is convenient for the
12233 debugger and the disassembler. The linker knows to make them odd
12234 again. */
12235
12236 static void
12237 s_insn (int ignore ATTRIBUTE_UNUSED)
12238 {
12239 mips16_mark_labels ();
12240
12241 demand_empty_rest_of_line ();
12242 }
12243
12244 /* Handle a .stabn directive. We need these in order to mark a label
12245 as being a mips16 text label correctly. Sometimes the compiler
12246 will emit a label, followed by a .stabn, and then switch sections.
12247 If the label and .stabn are in mips16 mode, then the label is
12248 really a mips16 text label. */
12249
12250 static void
12251 s_mips_stab (int type)
12252 {
12253 if (type == 'n')
12254 mips16_mark_labels ();
12255
12256 s_stab (type);
12257 }
12258
12259 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12260 */
12261
12262 static void
12263 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12264 {
12265 char *name;
12266 int c;
12267 symbolS *symbolP;
12268 expressionS exp;
12269
12270 name = input_line_pointer;
12271 c = get_symbol_end ();
12272 symbolP = symbol_find_or_make (name);
12273 S_SET_WEAK (symbolP);
12274 *input_line_pointer = c;
12275
12276 SKIP_WHITESPACE ();
12277
12278 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12279 {
12280 if (S_IS_DEFINED (symbolP))
12281 {
12282 as_bad ("ignoring attempt to redefine symbol %s",
12283 S_GET_NAME (symbolP));
12284 ignore_rest_of_line ();
12285 return;
12286 }
12287
12288 if (*input_line_pointer == ',')
12289 {
12290 ++input_line_pointer;
12291 SKIP_WHITESPACE ();
12292 }
12293
12294 expression (&exp);
12295 if (exp.X_op != O_symbol)
12296 {
12297 as_bad ("bad .weakext directive");
12298 ignore_rest_of_line ();
12299 return;
12300 }
12301 symbol_set_value_expression (symbolP, &exp);
12302 }
12303
12304 demand_empty_rest_of_line ();
12305 }
12306
12307 /* Parse a register string into a number. Called from the ECOFF code
12308 to parse .frame. The argument is non-zero if this is the frame
12309 register, so that we can record it in mips_frame_reg. */
12310
12311 int
12312 tc_get_register (int frame)
12313 {
12314 int reg;
12315
12316 SKIP_WHITESPACE ();
12317 if (*input_line_pointer++ != '$')
12318 {
12319 as_warn (_("expected `$'"));
12320 reg = ZERO;
12321 }
12322 else if (ISDIGIT (*input_line_pointer))
12323 {
12324 reg = get_absolute_expression ();
12325 if (reg < 0 || reg >= 32)
12326 {
12327 as_warn (_("Bad register number"));
12328 reg = ZERO;
12329 }
12330 }
12331 else
12332 {
12333 if (strncmp (input_line_pointer, "ra", 2) == 0)
12334 {
12335 reg = RA;
12336 input_line_pointer += 2;
12337 }
12338 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12339 {
12340 reg = FP;
12341 input_line_pointer += 2;
12342 }
12343 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12344 {
12345 reg = SP;
12346 input_line_pointer += 2;
12347 }
12348 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12349 {
12350 reg = GP;
12351 input_line_pointer += 2;
12352 }
12353 else if (strncmp (input_line_pointer, "at", 2) == 0)
12354 {
12355 reg = AT;
12356 input_line_pointer += 2;
12357 }
12358 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12359 {
12360 reg = KT0;
12361 input_line_pointer += 3;
12362 }
12363 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12364 {
12365 reg = KT1;
12366 input_line_pointer += 3;
12367 }
12368 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12369 {
12370 reg = ZERO;
12371 input_line_pointer += 4;
12372 }
12373 else
12374 {
12375 as_warn (_("Unrecognized register name"));
12376 reg = ZERO;
12377 while (ISALNUM(*input_line_pointer))
12378 input_line_pointer++;
12379 }
12380 }
12381 if (frame)
12382 {
12383 mips_frame_reg = reg != 0 ? reg : SP;
12384 mips_frame_reg_valid = 1;
12385 mips_cprestore_valid = 0;
12386 }
12387 return reg;
12388 }
12389
12390 valueT
12391 md_section_align (asection *seg, valueT addr)
12392 {
12393 int align = bfd_get_section_alignment (stdoutput, seg);
12394
12395 #ifdef OBJ_ELF
12396 /* We don't need to align ELF sections to the full alignment.
12397 However, Irix 5 may prefer that we align them at least to a 16
12398 byte boundary. We don't bother to align the sections if we are
12399 targeted for an embedded system. */
12400 if (strcmp (TARGET_OS, "elf") == 0)
12401 return addr;
12402 if (align > 4)
12403 align = 4;
12404 #endif
12405
12406 return ((addr + (1 << align) - 1) & (-1 << align));
12407 }
12408
12409 /* Utility routine, called from above as well. If called while the
12410 input file is still being read, it's only an approximation. (For
12411 example, a symbol may later become defined which appeared to be
12412 undefined earlier.) */
12413
12414 static int
12415 nopic_need_relax (symbolS *sym, int before_relaxing)
12416 {
12417 if (sym == 0)
12418 return 0;
12419
12420 if (g_switch_value > 0)
12421 {
12422 const char *symname;
12423 int change;
12424
12425 /* Find out whether this symbol can be referenced off the $gp
12426 register. It can be if it is smaller than the -G size or if
12427 it is in the .sdata or .sbss section. Certain symbols can
12428 not be referenced off the $gp, although it appears as though
12429 they can. */
12430 symname = S_GET_NAME (sym);
12431 if (symname != (const char *) NULL
12432 && (strcmp (symname, "eprol") == 0
12433 || strcmp (symname, "etext") == 0
12434 || strcmp (symname, "_gp") == 0
12435 || strcmp (symname, "edata") == 0
12436 || strcmp (symname, "_fbss") == 0
12437 || strcmp (symname, "_fdata") == 0
12438 || strcmp (symname, "_ftext") == 0
12439 || strcmp (symname, "end") == 0
12440 || strcmp (symname, "_gp_disp") == 0))
12441 change = 1;
12442 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12443 && (0
12444 #ifndef NO_ECOFF_DEBUGGING
12445 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12446 && (symbol_get_obj (sym)->ecoff_extern_size
12447 <= g_switch_value))
12448 #endif
12449 /* We must defer this decision until after the whole
12450 file has been read, since there might be a .extern
12451 after the first use of this symbol. */
12452 || (before_relaxing
12453 #ifndef NO_ECOFF_DEBUGGING
12454 && symbol_get_obj (sym)->ecoff_extern_size == 0
12455 #endif
12456 && S_GET_VALUE (sym) == 0)
12457 || (S_GET_VALUE (sym) != 0
12458 && S_GET_VALUE (sym) <= g_switch_value)))
12459 change = 0;
12460 else
12461 {
12462 const char *segname;
12463
12464 segname = segment_name (S_GET_SEGMENT (sym));
12465 assert (strcmp (segname, ".lit8") != 0
12466 && strcmp (segname, ".lit4") != 0);
12467 change = (strcmp (segname, ".sdata") != 0
12468 && strcmp (segname, ".sbss") != 0
12469 && strncmp (segname, ".sdata.", 7) != 0
12470 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12471 }
12472 return change;
12473 }
12474 else
12475 /* We are not optimizing for the $gp register. */
12476 return 1;
12477 }
12478
12479
12480 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12481
12482 static bfd_boolean
12483 pic_need_relax (symbolS *sym, asection *segtype)
12484 {
12485 asection *symsec;
12486 bfd_boolean linkonce;
12487
12488 /* Handle the case of a symbol equated to another symbol. */
12489 while (symbol_equated_reloc_p (sym))
12490 {
12491 symbolS *n;
12492
12493 /* It's possible to get a loop here in a badly written
12494 program. */
12495 n = symbol_get_value_expression (sym)->X_add_symbol;
12496 if (n == sym)
12497 break;
12498 sym = n;
12499 }
12500
12501 symsec = S_GET_SEGMENT (sym);
12502
12503 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12504 linkonce = FALSE;
12505 if (symsec != segtype && ! S_IS_LOCAL (sym))
12506 {
12507 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12508 != 0)
12509 linkonce = TRUE;
12510
12511 /* The GNU toolchain uses an extension for ELF: a section
12512 beginning with the magic string .gnu.linkonce is a linkonce
12513 section. */
12514 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12515 sizeof ".gnu.linkonce" - 1) == 0)
12516 linkonce = TRUE;
12517 }
12518
12519 /* This must duplicate the test in adjust_reloc_syms. */
12520 return (symsec != &bfd_und_section
12521 && symsec != &bfd_abs_section
12522 && ! bfd_is_com_section (symsec)
12523 && !linkonce
12524 #ifdef OBJ_ELF
12525 /* A global or weak symbol is treated as external. */
12526 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12527 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12528 #endif
12529 );
12530 }
12531
12532
12533 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12534 extended opcode. SEC is the section the frag is in. */
12535
12536 static int
12537 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12538 {
12539 int type;
12540 register const struct mips16_immed_operand *op;
12541 offsetT val;
12542 int mintiny, maxtiny;
12543 segT symsec;
12544 fragS *sym_frag;
12545
12546 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12547 return 0;
12548 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12549 return 1;
12550
12551 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12552 op = mips16_immed_operands;
12553 while (op->type != type)
12554 {
12555 ++op;
12556 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12557 }
12558
12559 if (op->unsp)
12560 {
12561 if (type == '<' || type == '>' || type == '[' || type == ']')
12562 {
12563 mintiny = 1;
12564 maxtiny = 1 << op->nbits;
12565 }
12566 else
12567 {
12568 mintiny = 0;
12569 maxtiny = (1 << op->nbits) - 1;
12570 }
12571 }
12572 else
12573 {
12574 mintiny = - (1 << (op->nbits - 1));
12575 maxtiny = (1 << (op->nbits - 1)) - 1;
12576 }
12577
12578 sym_frag = symbol_get_frag (fragp->fr_symbol);
12579 val = S_GET_VALUE (fragp->fr_symbol);
12580 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12581
12582 if (op->pcrel)
12583 {
12584 addressT addr;
12585
12586 /* We won't have the section when we are called from
12587 mips_relax_frag. However, we will always have been called
12588 from md_estimate_size_before_relax first. If this is a
12589 branch to a different section, we mark it as such. If SEC is
12590 NULL, and the frag is not marked, then it must be a branch to
12591 the same section. */
12592 if (sec == NULL)
12593 {
12594 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12595 return 1;
12596 }
12597 else
12598 {
12599 /* Must have been called from md_estimate_size_before_relax. */
12600 if (symsec != sec)
12601 {
12602 fragp->fr_subtype =
12603 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12604
12605 /* FIXME: We should support this, and let the linker
12606 catch branches and loads that are out of range. */
12607 as_bad_where (fragp->fr_file, fragp->fr_line,
12608 _("unsupported PC relative reference to different section"));
12609
12610 return 1;
12611 }
12612 if (fragp != sym_frag && sym_frag->fr_address == 0)
12613 /* Assume non-extended on the first relaxation pass.
12614 The address we have calculated will be bogus if this is
12615 a forward branch to another frag, as the forward frag
12616 will have fr_address == 0. */
12617 return 0;
12618 }
12619
12620 /* In this case, we know for sure that the symbol fragment is in
12621 the same section. If the relax_marker of the symbol fragment
12622 differs from the relax_marker of this fragment, we have not
12623 yet adjusted the symbol fragment fr_address. We want to add
12624 in STRETCH in order to get a better estimate of the address.
12625 This particularly matters because of the shift bits. */
12626 if (stretch != 0
12627 && sym_frag->relax_marker != fragp->relax_marker)
12628 {
12629 fragS *f;
12630
12631 /* Adjust stretch for any alignment frag. Note that if have
12632 been expanding the earlier code, the symbol may be
12633 defined in what appears to be an earlier frag. FIXME:
12634 This doesn't handle the fr_subtype field, which specifies
12635 a maximum number of bytes to skip when doing an
12636 alignment. */
12637 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12638 {
12639 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12640 {
12641 if (stretch < 0)
12642 stretch = - ((- stretch)
12643 & ~ ((1 << (int) f->fr_offset) - 1));
12644 else
12645 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12646 if (stretch == 0)
12647 break;
12648 }
12649 }
12650 if (f != NULL)
12651 val += stretch;
12652 }
12653
12654 addr = fragp->fr_address + fragp->fr_fix;
12655
12656 /* The base address rules are complicated. The base address of
12657 a branch is the following instruction. The base address of a
12658 PC relative load or add is the instruction itself, but if it
12659 is in a delay slot (in which case it can not be extended) use
12660 the address of the instruction whose delay slot it is in. */
12661 if (type == 'p' || type == 'q')
12662 {
12663 addr += 2;
12664
12665 /* If we are currently assuming that this frag should be
12666 extended, then, the current address is two bytes
12667 higher. */
12668 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12669 addr += 2;
12670
12671 /* Ignore the low bit in the target, since it will be set
12672 for a text label. */
12673 if ((val & 1) != 0)
12674 --val;
12675 }
12676 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12677 addr -= 4;
12678 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12679 addr -= 2;
12680
12681 val -= addr & ~ ((1 << op->shift) - 1);
12682
12683 /* Branch offsets have an implicit 0 in the lowest bit. */
12684 if (type == 'p' || type == 'q')
12685 val /= 2;
12686
12687 /* If any of the shifted bits are set, we must use an extended
12688 opcode. If the address depends on the size of this
12689 instruction, this can lead to a loop, so we arrange to always
12690 use an extended opcode. We only check this when we are in
12691 the main relaxation loop, when SEC is NULL. */
12692 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12693 {
12694 fragp->fr_subtype =
12695 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12696 return 1;
12697 }
12698
12699 /* If we are about to mark a frag as extended because the value
12700 is precisely maxtiny + 1, then there is a chance of an
12701 infinite loop as in the following code:
12702 la $4,foo
12703 .skip 1020
12704 .align 2
12705 foo:
12706 In this case when the la is extended, foo is 0x3fc bytes
12707 away, so the la can be shrunk, but then foo is 0x400 away, so
12708 the la must be extended. To avoid this loop, we mark the
12709 frag as extended if it was small, and is about to become
12710 extended with a value of maxtiny + 1. */
12711 if (val == ((maxtiny + 1) << op->shift)
12712 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12713 && sec == NULL)
12714 {
12715 fragp->fr_subtype =
12716 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12717 return 1;
12718 }
12719 }
12720 else if (symsec != absolute_section && sec != NULL)
12721 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12722
12723 if ((val & ((1 << op->shift) - 1)) != 0
12724 || val < (mintiny << op->shift)
12725 || val > (maxtiny << op->shift))
12726 return 1;
12727 else
12728 return 0;
12729 }
12730
12731 /* Compute the length of a branch sequence, and adjust the
12732 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12733 worst-case length is computed, with UPDATE being used to indicate
12734 whether an unconditional (-1), branch-likely (+1) or regular (0)
12735 branch is to be computed. */
12736 static int
12737 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12738 {
12739 bfd_boolean toofar;
12740 int length;
12741
12742 if (fragp
12743 && S_IS_DEFINED (fragp->fr_symbol)
12744 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12745 {
12746 addressT addr;
12747 offsetT val;
12748
12749 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12750
12751 addr = fragp->fr_address + fragp->fr_fix + 4;
12752
12753 val -= addr;
12754
12755 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12756 }
12757 else if (fragp)
12758 /* If the symbol is not defined or it's in a different segment,
12759 assume the user knows what's going on and emit a short
12760 branch. */
12761 toofar = FALSE;
12762 else
12763 toofar = TRUE;
12764
12765 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12766 fragp->fr_subtype
12767 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12768 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12769 RELAX_BRANCH_LINK (fragp->fr_subtype),
12770 toofar);
12771
12772 length = 4;
12773 if (toofar)
12774 {
12775 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12776 length += 8;
12777
12778 if (mips_pic != NO_PIC)
12779 {
12780 /* Additional space for PIC loading of target address. */
12781 length += 8;
12782 if (mips_opts.isa == ISA_MIPS1)
12783 /* Additional space for $at-stabilizing nop. */
12784 length += 4;
12785 }
12786
12787 /* If branch is conditional. */
12788 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12789 length += 8;
12790 }
12791
12792 return length;
12793 }
12794
12795 /* Estimate the size of a frag before relaxing. Unless this is the
12796 mips16, we are not really relaxing here, and the final size is
12797 encoded in the subtype information. For the mips16, we have to
12798 decide whether we are using an extended opcode or not. */
12799
12800 int
12801 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12802 {
12803 int change;
12804
12805 if (RELAX_BRANCH_P (fragp->fr_subtype))
12806 {
12807
12808 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12809
12810 return fragp->fr_var;
12811 }
12812
12813 if (RELAX_MIPS16_P (fragp->fr_subtype))
12814 /* We don't want to modify the EXTENDED bit here; it might get us
12815 into infinite loops. We change it only in mips_relax_frag(). */
12816 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12817
12818 if (mips_pic == NO_PIC)
12819 change = nopic_need_relax (fragp->fr_symbol, 0);
12820 else if (mips_pic == SVR4_PIC)
12821 change = pic_need_relax (fragp->fr_symbol, segtype);
12822 else
12823 abort ();
12824
12825 if (change)
12826 {
12827 fragp->fr_subtype |= RELAX_USE_SECOND;
12828 return -RELAX_FIRST (fragp->fr_subtype);
12829 }
12830 else
12831 return -RELAX_SECOND (fragp->fr_subtype);
12832 }
12833
12834 /* This is called to see whether a reloc against a defined symbol
12835 should be converted into a reloc against a section. */
12836
12837 int
12838 mips_fix_adjustable (fixS *fixp)
12839 {
12840 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12841 about the format of the offset in the .o file. */
12842 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12843 return 0;
12844
12845 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12846 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12847 return 0;
12848
12849 if (fixp->fx_addsy == NULL)
12850 return 1;
12851
12852 /* If symbol SYM is in a mergeable section, relocations of the form
12853 SYM + 0 can usually be made section-relative. The mergeable data
12854 is then identified by the section offset rather than by the symbol.
12855
12856 However, if we're generating REL LO16 relocations, the offset is split
12857 between the LO16 and parterning high part relocation. The linker will
12858 need to recalculate the complete offset in order to correctly identify
12859 the merge data.
12860
12861 The linker has traditionally not looked for the parterning high part
12862 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12863 placed anywhere. Rather than break backwards compatibility by changing
12864 this, it seems better not to force the issue, and instead keep the
12865 original symbol. This will work with either linker behavior. */
12866 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12867 && HAVE_IN_PLACE_ADDENDS
12868 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12869 return 0;
12870
12871 #ifdef OBJ_ELF
12872 /* Don't adjust relocations against mips16 symbols, so that the linker
12873 can find them if it needs to set up a stub. */
12874 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12875 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12876 && fixp->fx_subsy == NULL)
12877 return 0;
12878 #endif
12879
12880 return 1;
12881 }
12882
12883 /* Translate internal representation of relocation info to BFD target
12884 format. */
12885
12886 arelent **
12887 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12888 {
12889 static arelent *retval[4];
12890 arelent *reloc;
12891 bfd_reloc_code_real_type code;
12892
12893 memset (retval, 0, sizeof(retval));
12894 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12895 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12896 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12897 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12898
12899 assert (! fixp->fx_pcrel);
12900 reloc->addend = fixp->fx_addnumber;
12901
12902 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12903 entry to be used in the relocation's section offset. */
12904 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12905 {
12906 reloc->address = reloc->addend;
12907 reloc->addend = 0;
12908 }
12909
12910 code = fixp->fx_r_type;
12911
12912 /* To support a PC relative reloc, we used a Cygnus extension.
12913 We check for that here to make sure that we don't let such a
12914 reloc escape normally. (FIXME: This was formerly used by
12915 embedded-PIC support, but is now used by branch handling in
12916 general. That probably should be fixed.) */
12917 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12918 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12919 && code == BFD_RELOC_16_PCREL_S2)
12920 reloc->howto = NULL;
12921 else
12922 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12923
12924 if (reloc->howto == NULL)
12925 {
12926 as_bad_where (fixp->fx_file, fixp->fx_line,
12927 _("Can not represent %s relocation in this object file format"),
12928 bfd_get_reloc_code_name (code));
12929 retval[0] = NULL;
12930 }
12931
12932 return retval;
12933 }
12934
12935 /* Relax a machine dependent frag. This returns the amount by which
12936 the current size of the frag should change. */
12937
12938 int
12939 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12940 {
12941 if (RELAX_BRANCH_P (fragp->fr_subtype))
12942 {
12943 offsetT old_var = fragp->fr_var;
12944
12945 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12946
12947 return fragp->fr_var - old_var;
12948 }
12949
12950 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12951 return 0;
12952
12953 if (mips16_extended_frag (fragp, NULL, stretch))
12954 {
12955 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12956 return 0;
12957 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12958 return 2;
12959 }
12960 else
12961 {
12962 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12963 return 0;
12964 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12965 return -2;
12966 }
12967
12968 return 0;
12969 }
12970
12971 /* Convert a machine dependent frag. */
12972
12973 void
12974 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12975 {
12976 if (RELAX_BRANCH_P (fragp->fr_subtype))
12977 {
12978 bfd_byte *buf;
12979 unsigned long insn;
12980 expressionS exp;
12981 fixS *fixp;
12982
12983 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12984
12985 if (target_big_endian)
12986 insn = bfd_getb32 (buf);
12987 else
12988 insn = bfd_getl32 (buf);
12989
12990 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12991 {
12992 /* We generate a fixup instead of applying it right now
12993 because, if there are linker relaxations, we're going to
12994 need the relocations. */
12995 exp.X_op = O_symbol;
12996 exp.X_add_symbol = fragp->fr_symbol;
12997 exp.X_add_number = fragp->fr_offset;
12998
12999 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13000 4, &exp, 1,
13001 BFD_RELOC_16_PCREL_S2);
13002 fixp->fx_file = fragp->fr_file;
13003 fixp->fx_line = fragp->fr_line;
13004
13005 md_number_to_chars ((char *) buf, insn, 4);
13006 buf += 4;
13007 }
13008 else
13009 {
13010 int i;
13011
13012 as_warn_where (fragp->fr_file, fragp->fr_line,
13013 _("relaxed out-of-range branch into a jump"));
13014
13015 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13016 goto uncond;
13017
13018 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13019 {
13020 /* Reverse the branch. */
13021 switch ((insn >> 28) & 0xf)
13022 {
13023 case 4:
13024 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13025 have the condition reversed by tweaking a single
13026 bit, and their opcodes all have 0x4???????. */
13027 assert ((insn & 0xf1000000) == 0x41000000);
13028 insn ^= 0x00010000;
13029 break;
13030
13031 case 0:
13032 /* bltz 0x04000000 bgez 0x04010000
13033 bltzal 0x04100000 bgezal 0x04110000 */
13034 assert ((insn & 0xfc0e0000) == 0x04000000);
13035 insn ^= 0x00010000;
13036 break;
13037
13038 case 1:
13039 /* beq 0x10000000 bne 0x14000000
13040 blez 0x18000000 bgtz 0x1c000000 */
13041 insn ^= 0x04000000;
13042 break;
13043
13044 default:
13045 abort ();
13046 }
13047 }
13048
13049 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13050 {
13051 /* Clear the and-link bit. */
13052 assert ((insn & 0xfc1c0000) == 0x04100000);
13053
13054 /* bltzal 0x04100000 bgezal 0x04110000
13055 bltzall 0x04120000 bgezall 0x04130000 */
13056 insn &= ~0x00100000;
13057 }
13058
13059 /* Branch over the branch (if the branch was likely) or the
13060 full jump (not likely case). Compute the offset from the
13061 current instruction to branch to. */
13062 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13063 i = 16;
13064 else
13065 {
13066 /* How many bytes in instructions we've already emitted? */
13067 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13068 /* How many bytes in instructions from here to the end? */
13069 i = fragp->fr_var - i;
13070 }
13071 /* Convert to instruction count. */
13072 i >>= 2;
13073 /* Branch counts from the next instruction. */
13074 i--;
13075 insn |= i;
13076 /* Branch over the jump. */
13077 md_number_to_chars ((char *) buf, insn, 4);
13078 buf += 4;
13079
13080 /* Nop */
13081 md_number_to_chars ((char *) buf, 0, 4);
13082 buf += 4;
13083
13084 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13085 {
13086 /* beql $0, $0, 2f */
13087 insn = 0x50000000;
13088 /* Compute the PC offset from the current instruction to
13089 the end of the variable frag. */
13090 /* How many bytes in instructions we've already emitted? */
13091 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13092 /* How many bytes in instructions from here to the end? */
13093 i = fragp->fr_var - i;
13094 /* Convert to instruction count. */
13095 i >>= 2;
13096 /* Don't decrement i, because we want to branch over the
13097 delay slot. */
13098
13099 insn |= i;
13100 md_number_to_chars ((char *) buf, insn, 4);
13101 buf += 4;
13102
13103 md_number_to_chars ((char *) buf, 0, 4);
13104 buf += 4;
13105 }
13106
13107 uncond:
13108 if (mips_pic == NO_PIC)
13109 {
13110 /* j or jal. */
13111 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13112 ? 0x0c000000 : 0x08000000);
13113 exp.X_op = O_symbol;
13114 exp.X_add_symbol = fragp->fr_symbol;
13115 exp.X_add_number = fragp->fr_offset;
13116
13117 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13118 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13119 fixp->fx_file = fragp->fr_file;
13120 fixp->fx_line = fragp->fr_line;
13121
13122 md_number_to_chars ((char *) buf, insn, 4);
13123 buf += 4;
13124 }
13125 else
13126 {
13127 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13128 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13129 exp.X_op = O_symbol;
13130 exp.X_add_symbol = fragp->fr_symbol;
13131 exp.X_add_number = fragp->fr_offset;
13132
13133 if (fragp->fr_offset)
13134 {
13135 exp.X_add_symbol = make_expr_symbol (&exp);
13136 exp.X_add_number = 0;
13137 }
13138
13139 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13140 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13141 fixp->fx_file = fragp->fr_file;
13142 fixp->fx_line = fragp->fr_line;
13143
13144 md_number_to_chars ((char *) buf, insn, 4);
13145 buf += 4;
13146
13147 if (mips_opts.isa == ISA_MIPS1)
13148 {
13149 /* nop */
13150 md_number_to_chars ((char *) buf, 0, 4);
13151 buf += 4;
13152 }
13153
13154 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13155 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13156
13157 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13158 4, &exp, 0, BFD_RELOC_LO16);
13159 fixp->fx_file = fragp->fr_file;
13160 fixp->fx_line = fragp->fr_line;
13161
13162 md_number_to_chars ((char *) buf, insn, 4);
13163 buf += 4;
13164
13165 /* j(al)r $at. */
13166 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13167 insn = 0x0020f809;
13168 else
13169 insn = 0x00200008;
13170
13171 md_number_to_chars ((char *) buf, insn, 4);
13172 buf += 4;
13173 }
13174 }
13175
13176 assert (buf == (bfd_byte *)fragp->fr_literal
13177 + fragp->fr_fix + fragp->fr_var);
13178
13179 fragp->fr_fix += fragp->fr_var;
13180
13181 return;
13182 }
13183
13184 if (RELAX_MIPS16_P (fragp->fr_subtype))
13185 {
13186 int type;
13187 register const struct mips16_immed_operand *op;
13188 bfd_boolean small, ext;
13189 offsetT val;
13190 bfd_byte *buf;
13191 unsigned long insn;
13192 bfd_boolean use_extend;
13193 unsigned short extend;
13194
13195 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13196 op = mips16_immed_operands;
13197 while (op->type != type)
13198 ++op;
13199
13200 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13201 {
13202 small = FALSE;
13203 ext = TRUE;
13204 }
13205 else
13206 {
13207 small = TRUE;
13208 ext = FALSE;
13209 }
13210
13211 resolve_symbol_value (fragp->fr_symbol);
13212 val = S_GET_VALUE (fragp->fr_symbol);
13213 if (op->pcrel)
13214 {
13215 addressT addr;
13216
13217 addr = fragp->fr_address + fragp->fr_fix;
13218
13219 /* The rules for the base address of a PC relative reloc are
13220 complicated; see mips16_extended_frag. */
13221 if (type == 'p' || type == 'q')
13222 {
13223 addr += 2;
13224 if (ext)
13225 addr += 2;
13226 /* Ignore the low bit in the target, since it will be
13227 set for a text label. */
13228 if ((val & 1) != 0)
13229 --val;
13230 }
13231 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13232 addr -= 4;
13233 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13234 addr -= 2;
13235
13236 addr &= ~ (addressT) ((1 << op->shift) - 1);
13237 val -= addr;
13238
13239 /* Make sure the section winds up with the alignment we have
13240 assumed. */
13241 if (op->shift > 0)
13242 record_alignment (asec, op->shift);
13243 }
13244
13245 if (ext
13246 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13247 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13248 as_warn_where (fragp->fr_file, fragp->fr_line,
13249 _("extended instruction in delay slot"));
13250
13251 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13252
13253 if (target_big_endian)
13254 insn = bfd_getb16 (buf);
13255 else
13256 insn = bfd_getl16 (buf);
13257
13258 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13259 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13260 small, ext, &insn, &use_extend, &extend);
13261
13262 if (use_extend)
13263 {
13264 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13265 fragp->fr_fix += 2;
13266 buf += 2;
13267 }
13268
13269 md_number_to_chars ((char *) buf, insn, 2);
13270 fragp->fr_fix += 2;
13271 buf += 2;
13272 }
13273 else
13274 {
13275 int first, second;
13276 fixS *fixp;
13277
13278 first = RELAX_FIRST (fragp->fr_subtype);
13279 second = RELAX_SECOND (fragp->fr_subtype);
13280 fixp = (fixS *) fragp->fr_opcode;
13281
13282 /* Possibly emit a warning if we've chosen the longer option. */
13283 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13284 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13285 {
13286 const char *msg = macro_warning (fragp->fr_subtype);
13287 if (msg != 0)
13288 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13289 }
13290
13291 /* Go through all the fixups for the first sequence. Disable them
13292 (by marking them as done) if we're going to use the second
13293 sequence instead. */
13294 while (fixp
13295 && fixp->fx_frag == fragp
13296 && fixp->fx_where < fragp->fr_fix - second)
13297 {
13298 if (fragp->fr_subtype & RELAX_USE_SECOND)
13299 fixp->fx_done = 1;
13300 fixp = fixp->fx_next;
13301 }
13302
13303 /* Go through the fixups for the second sequence. Disable them if
13304 we're going to use the first sequence, otherwise adjust their
13305 addresses to account for the relaxation. */
13306 while (fixp && fixp->fx_frag == fragp)
13307 {
13308 if (fragp->fr_subtype & RELAX_USE_SECOND)
13309 fixp->fx_where -= first;
13310 else
13311 fixp->fx_done = 1;
13312 fixp = fixp->fx_next;
13313 }
13314
13315 /* Now modify the frag contents. */
13316 if (fragp->fr_subtype & RELAX_USE_SECOND)
13317 {
13318 char *start;
13319
13320 start = fragp->fr_literal + fragp->fr_fix - first - second;
13321 memmove (start, start + first, second);
13322 fragp->fr_fix -= first;
13323 }
13324 else
13325 fragp->fr_fix -= second;
13326 }
13327 }
13328
13329 #ifdef OBJ_ELF
13330
13331 /* This function is called after the relocs have been generated.
13332 We've been storing mips16 text labels as odd. Here we convert them
13333 back to even for the convenience of the debugger. */
13334
13335 void
13336 mips_frob_file_after_relocs (void)
13337 {
13338 asymbol **syms;
13339 unsigned int count, i;
13340
13341 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13342 return;
13343
13344 syms = bfd_get_outsymbols (stdoutput);
13345 count = bfd_get_symcount (stdoutput);
13346 for (i = 0; i < count; i++, syms++)
13347 {
13348 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13349 && ((*syms)->value & 1) != 0)
13350 {
13351 (*syms)->value &= ~1;
13352 /* If the symbol has an odd size, it was probably computed
13353 incorrectly, so adjust that as well. */
13354 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13355 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13356 }
13357 }
13358 }
13359
13360 #endif
13361
13362 /* This function is called whenever a label is defined. It is used
13363 when handling branch delays; if a branch has a label, we assume we
13364 can not move it. */
13365
13366 void
13367 mips_define_label (symbolS *sym)
13368 {
13369 struct insn_label_list *l;
13370
13371 if (free_insn_labels == NULL)
13372 l = (struct insn_label_list *) xmalloc (sizeof *l);
13373 else
13374 {
13375 l = free_insn_labels;
13376 free_insn_labels = l->next;
13377 }
13378
13379 l->label = sym;
13380 l->next = insn_labels;
13381 insn_labels = l;
13382 }
13383 \f
13384 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13385
13386 /* Some special processing for a MIPS ELF file. */
13387
13388 void
13389 mips_elf_final_processing (void)
13390 {
13391 /* Write out the register information. */
13392 if (mips_abi != N64_ABI)
13393 {
13394 Elf32_RegInfo s;
13395
13396 s.ri_gprmask = mips_gprmask;
13397 s.ri_cprmask[0] = mips_cprmask[0];
13398 s.ri_cprmask[1] = mips_cprmask[1];
13399 s.ri_cprmask[2] = mips_cprmask[2];
13400 s.ri_cprmask[3] = mips_cprmask[3];
13401 /* The gp_value field is set by the MIPS ELF backend. */
13402
13403 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13404 ((Elf32_External_RegInfo *)
13405 mips_regmask_frag));
13406 }
13407 else
13408 {
13409 Elf64_Internal_RegInfo s;
13410
13411 s.ri_gprmask = mips_gprmask;
13412 s.ri_pad = 0;
13413 s.ri_cprmask[0] = mips_cprmask[0];
13414 s.ri_cprmask[1] = mips_cprmask[1];
13415 s.ri_cprmask[2] = mips_cprmask[2];
13416 s.ri_cprmask[3] = mips_cprmask[3];
13417 /* The gp_value field is set by the MIPS ELF backend. */
13418
13419 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13420 ((Elf64_External_RegInfo *)
13421 mips_regmask_frag));
13422 }
13423
13424 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13425 sort of BFD interface for this. */
13426 if (mips_any_noreorder)
13427 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13428 if (mips_pic != NO_PIC)
13429 {
13430 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13431 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13432 }
13433 if (mips_abicalls)
13434 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13435
13436 /* Set MIPS ELF flags for ASEs. */
13437 if (file_ase_mips16)
13438 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13439 #if 0 /* XXX FIXME */
13440 if (file_ase_mips3d)
13441 elf_elfheader (stdoutput)->e_flags |= ???;
13442 #endif
13443 if (file_ase_mdmx)
13444 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13445
13446 /* Set the MIPS ELF ABI flags. */
13447 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13448 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13449 else if (mips_abi == O64_ABI)
13450 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13451 else if (mips_abi == EABI_ABI)
13452 {
13453 if (!file_mips_gp32)
13454 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13455 else
13456 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13457 }
13458 else if (mips_abi == N32_ABI)
13459 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13460
13461 /* Nothing to do for N64_ABI. */
13462
13463 if (mips_32bitmode)
13464 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13465 }
13466
13467 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13468 \f
13469 typedef struct proc {
13470 symbolS *func_sym;
13471 symbolS *func_end_sym;
13472 unsigned long reg_mask;
13473 unsigned long reg_offset;
13474 unsigned long fpreg_mask;
13475 unsigned long fpreg_offset;
13476 unsigned long frame_offset;
13477 unsigned long frame_reg;
13478 unsigned long pc_reg;
13479 } procS;
13480
13481 static procS cur_proc;
13482 static procS *cur_proc_ptr;
13483 static int numprocs;
13484
13485 /* Fill in an rs_align_code fragment. */
13486
13487 void
13488 mips_handle_align (fragS *fragp)
13489 {
13490 if (fragp->fr_type != rs_align_code)
13491 return;
13492
13493 if (mips_opts.mips16)
13494 {
13495 static const unsigned char be_nop[] = { 0x65, 0x00 };
13496 static const unsigned char le_nop[] = { 0x00, 0x65 };
13497
13498 int bytes;
13499 char *p;
13500
13501 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13502 p = fragp->fr_literal + fragp->fr_fix;
13503
13504 if (bytes & 1)
13505 {
13506 *p++ = 0;
13507 fragp->fr_fix++;
13508 }
13509
13510 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13511 fragp->fr_var = 2;
13512 }
13513
13514 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13515 }
13516
13517 static void
13518 md_obj_begin (void)
13519 {
13520 }
13521
13522 static void
13523 md_obj_end (void)
13524 {
13525 /* check for premature end, nesting errors, etc */
13526 if (cur_proc_ptr)
13527 as_warn (_("missing .end at end of assembly"));
13528 }
13529
13530 static long
13531 get_number (void)
13532 {
13533 int negative = 0;
13534 long val = 0;
13535
13536 if (*input_line_pointer == '-')
13537 {
13538 ++input_line_pointer;
13539 negative = 1;
13540 }
13541 if (!ISDIGIT (*input_line_pointer))
13542 as_bad (_("expected simple number"));
13543 if (input_line_pointer[0] == '0')
13544 {
13545 if (input_line_pointer[1] == 'x')
13546 {
13547 input_line_pointer += 2;
13548 while (ISXDIGIT (*input_line_pointer))
13549 {
13550 val <<= 4;
13551 val |= hex_value (*input_line_pointer++);
13552 }
13553 return negative ? -val : val;
13554 }
13555 else
13556 {
13557 ++input_line_pointer;
13558 while (ISDIGIT (*input_line_pointer))
13559 {
13560 val <<= 3;
13561 val |= *input_line_pointer++ - '0';
13562 }
13563 return negative ? -val : val;
13564 }
13565 }
13566 if (!ISDIGIT (*input_line_pointer))
13567 {
13568 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13569 *input_line_pointer, *input_line_pointer);
13570 as_warn (_("invalid number"));
13571 return -1;
13572 }
13573 while (ISDIGIT (*input_line_pointer))
13574 {
13575 val *= 10;
13576 val += *input_line_pointer++ - '0';
13577 }
13578 return negative ? -val : val;
13579 }
13580
13581 /* The .file directive; just like the usual .file directive, but there
13582 is an initial number which is the ECOFF file index. In the non-ECOFF
13583 case .file implies DWARF-2. */
13584
13585 static void
13586 s_mips_file (int x ATTRIBUTE_UNUSED)
13587 {
13588 static int first_file_directive = 0;
13589
13590 if (ECOFF_DEBUGGING)
13591 {
13592 get_number ();
13593 s_app_file (0);
13594 }
13595 else
13596 {
13597 char *filename;
13598
13599 filename = dwarf2_directive_file (0);
13600
13601 /* Versions of GCC up to 3.1 start files with a ".file"
13602 directive even for stabs output. Make sure that this
13603 ".file" is handled. Note that you need a version of GCC
13604 after 3.1 in order to support DWARF-2 on MIPS. */
13605 if (filename != NULL && ! first_file_directive)
13606 {
13607 (void) new_logical_line (filename, -1);
13608 s_app_file_string (filename, 0);
13609 }
13610 first_file_directive = 1;
13611 }
13612 }
13613
13614 /* The .loc directive, implying DWARF-2. */
13615
13616 static void
13617 s_mips_loc (int x ATTRIBUTE_UNUSED)
13618 {
13619 if (!ECOFF_DEBUGGING)
13620 dwarf2_directive_loc (0);
13621 }
13622
13623 /* The .end directive. */
13624
13625 static void
13626 s_mips_end (int x ATTRIBUTE_UNUSED)
13627 {
13628 symbolS *p;
13629
13630 /* Following functions need their own .frame and .cprestore directives. */
13631 mips_frame_reg_valid = 0;
13632 mips_cprestore_valid = 0;
13633
13634 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13635 {
13636 p = get_symbol ();
13637 demand_empty_rest_of_line ();
13638 }
13639 else
13640 p = NULL;
13641
13642 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13643 as_warn (_(".end not in text section"));
13644
13645 if (!cur_proc_ptr)
13646 {
13647 as_warn (_(".end directive without a preceding .ent directive."));
13648 demand_empty_rest_of_line ();
13649 return;
13650 }
13651
13652 if (p != NULL)
13653 {
13654 assert (S_GET_NAME (p));
13655 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13656 as_warn (_(".end symbol does not match .ent symbol."));
13657
13658 if (debug_type == DEBUG_STABS)
13659 stabs_generate_asm_endfunc (S_GET_NAME (p),
13660 S_GET_NAME (p));
13661 }
13662 else
13663 as_warn (_(".end directive missing or unknown symbol"));
13664
13665 #ifdef OBJ_ELF
13666 /* Create an expression to calculate the size of the function. */
13667 if (p && cur_proc_ptr)
13668 {
13669 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13670 expressionS *exp = xmalloc (sizeof (expressionS));
13671
13672 obj->size = exp;
13673 exp->X_op = O_subtract;
13674 exp->X_add_symbol = symbol_temp_new_now ();
13675 exp->X_op_symbol = p;
13676 exp->X_add_number = 0;
13677
13678 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13679 }
13680
13681 /* Generate a .pdr section. */
13682 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13683 && mips_flag_pdr)
13684 {
13685 segT saved_seg = now_seg;
13686 subsegT saved_subseg = now_subseg;
13687 valueT dot;
13688 expressionS exp;
13689 char *fragp;
13690
13691 dot = frag_now_fix ();
13692
13693 #ifdef md_flush_pending_output
13694 md_flush_pending_output ();
13695 #endif
13696
13697 assert (pdr_seg);
13698 subseg_set (pdr_seg, 0);
13699
13700 /* Write the symbol. */
13701 exp.X_op = O_symbol;
13702 exp.X_add_symbol = p;
13703 exp.X_add_number = 0;
13704 emit_expr (&exp, 4);
13705
13706 fragp = frag_more (7 * 4);
13707
13708 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13709 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13710 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13711 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13712 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13713 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13714 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13715
13716 subseg_set (saved_seg, saved_subseg);
13717 }
13718 #endif /* OBJ_ELF */
13719
13720 cur_proc_ptr = NULL;
13721 }
13722
13723 /* The .aent and .ent directives. */
13724
13725 static void
13726 s_mips_ent (int aent)
13727 {
13728 symbolS *symbolP;
13729
13730 symbolP = get_symbol ();
13731 if (*input_line_pointer == ',')
13732 ++input_line_pointer;
13733 SKIP_WHITESPACE ();
13734 if (ISDIGIT (*input_line_pointer)
13735 || *input_line_pointer == '-')
13736 get_number ();
13737
13738 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13739 as_warn (_(".ent or .aent not in text section."));
13740
13741 if (!aent && cur_proc_ptr)
13742 as_warn (_("missing .end"));
13743
13744 if (!aent)
13745 {
13746 /* This function needs its own .frame and .cprestore directives. */
13747 mips_frame_reg_valid = 0;
13748 mips_cprestore_valid = 0;
13749
13750 cur_proc_ptr = &cur_proc;
13751 memset (cur_proc_ptr, '\0', sizeof (procS));
13752
13753 cur_proc_ptr->func_sym = symbolP;
13754
13755 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13756
13757 ++numprocs;
13758
13759 if (debug_type == DEBUG_STABS)
13760 stabs_generate_asm_func (S_GET_NAME (symbolP),
13761 S_GET_NAME (symbolP));
13762 }
13763
13764 demand_empty_rest_of_line ();
13765 }
13766
13767 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13768 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13769 s_mips_frame is used so that we can set the PDR information correctly.
13770 We can't use the ecoff routines because they make reference to the ecoff
13771 symbol table (in the mdebug section). */
13772
13773 static void
13774 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13775 {
13776 #ifdef OBJ_ELF
13777 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13778 {
13779 long val;
13780
13781 if (cur_proc_ptr == (procS *) NULL)
13782 {
13783 as_warn (_(".frame outside of .ent"));
13784 demand_empty_rest_of_line ();
13785 return;
13786 }
13787
13788 cur_proc_ptr->frame_reg = tc_get_register (1);
13789
13790 SKIP_WHITESPACE ();
13791 if (*input_line_pointer++ != ','
13792 || get_absolute_expression_and_terminator (&val) != ',')
13793 {
13794 as_warn (_("Bad .frame directive"));
13795 --input_line_pointer;
13796 demand_empty_rest_of_line ();
13797 return;
13798 }
13799
13800 cur_proc_ptr->frame_offset = val;
13801 cur_proc_ptr->pc_reg = tc_get_register (0);
13802
13803 demand_empty_rest_of_line ();
13804 }
13805 else
13806 #endif /* OBJ_ELF */
13807 s_ignore (ignore);
13808 }
13809
13810 /* The .fmask and .mask directives. If the mdebug section is present
13811 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13812 embedded targets, s_mips_mask is used so that we can set the PDR
13813 information correctly. We can't use the ecoff routines because they
13814 make reference to the ecoff symbol table (in the mdebug section). */
13815
13816 static void
13817 s_mips_mask (int reg_type)
13818 {
13819 #ifdef OBJ_ELF
13820 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13821 {
13822 long mask, off;
13823
13824 if (cur_proc_ptr == (procS *) NULL)
13825 {
13826 as_warn (_(".mask/.fmask outside of .ent"));
13827 demand_empty_rest_of_line ();
13828 return;
13829 }
13830
13831 if (get_absolute_expression_and_terminator (&mask) != ',')
13832 {
13833 as_warn (_("Bad .mask/.fmask directive"));
13834 --input_line_pointer;
13835 demand_empty_rest_of_line ();
13836 return;
13837 }
13838
13839 off = get_absolute_expression ();
13840
13841 if (reg_type == 'F')
13842 {
13843 cur_proc_ptr->fpreg_mask = mask;
13844 cur_proc_ptr->fpreg_offset = off;
13845 }
13846 else
13847 {
13848 cur_proc_ptr->reg_mask = mask;
13849 cur_proc_ptr->reg_offset = off;
13850 }
13851
13852 demand_empty_rest_of_line ();
13853 }
13854 else
13855 #endif /* OBJ_ELF */
13856 s_ignore (reg_type);
13857 }
13858
13859 /* A table describing all the processors gas knows about. Names are
13860 matched in the order listed.
13861
13862 To ease comparison, please keep this table in the same order as
13863 gcc's mips_cpu_info_table[]. */
13864 static const struct mips_cpu_info mips_cpu_info_table[] =
13865 {
13866 /* Entries for generic ISAs */
13867 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13868 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13869 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13870 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13871 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13872 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13873 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13874 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13875 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13876
13877 /* MIPS I */
13878 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13879 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13880 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13881
13882 /* MIPS II */
13883 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13884
13885 /* MIPS III */
13886 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13887 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13888 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13889 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13890 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13891 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13892 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13893 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13894 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13895 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13896 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13897 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13898
13899 /* MIPS IV */
13900 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13901 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13902 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13903 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13904 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13905 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13906 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13907 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13908 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13909 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13910 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13911 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13912 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13913
13914 /* MIPS 32 */
13915 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13916 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13917 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13918
13919 /* MIPS 64 */
13920 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13921 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13922
13923 /* Broadcom SB-1 CPU core */
13924 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13925
13926 /* End marker */
13927 { NULL, 0, 0, 0 }
13928 };
13929
13930
13931 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13932 with a final "000" replaced by "k". Ignore case.
13933
13934 Note: this function is shared between GCC and GAS. */
13935
13936 static bfd_boolean
13937 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13938 {
13939 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13940 given++, canonical++;
13941
13942 return ((*given == 0 && *canonical == 0)
13943 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13944 }
13945
13946
13947 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13948 CPU name. We've traditionally allowed a lot of variation here.
13949
13950 Note: this function is shared between GCC and GAS. */
13951
13952 static bfd_boolean
13953 mips_matching_cpu_name_p (const char *canonical, const char *given)
13954 {
13955 /* First see if the name matches exactly, or with a final "000"
13956 turned into "k". */
13957 if (mips_strict_matching_cpu_name_p (canonical, given))
13958 return TRUE;
13959
13960 /* If not, try comparing based on numerical designation alone.
13961 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13962 if (TOLOWER (*given) == 'r')
13963 given++;
13964 if (!ISDIGIT (*given))
13965 return FALSE;
13966
13967 /* Skip over some well-known prefixes in the canonical name,
13968 hoping to find a number there too. */
13969 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13970 canonical += 2;
13971 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13972 canonical += 2;
13973 else if (TOLOWER (canonical[0]) == 'r')
13974 canonical += 1;
13975
13976 return mips_strict_matching_cpu_name_p (canonical, given);
13977 }
13978
13979
13980 /* Parse an option that takes the name of a processor as its argument.
13981 OPTION is the name of the option and CPU_STRING is the argument.
13982 Return the corresponding processor enumeration if the CPU_STRING is
13983 recognized, otherwise report an error and return null.
13984
13985 A similar function exists in GCC. */
13986
13987 static const struct mips_cpu_info *
13988 mips_parse_cpu (const char *option, const char *cpu_string)
13989 {
13990 const struct mips_cpu_info *p;
13991
13992 /* 'from-abi' selects the most compatible architecture for the given
13993 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13994 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13995 version. Look first at the -mgp options, if given, otherwise base
13996 the choice on MIPS_DEFAULT_64BIT.
13997
13998 Treat NO_ABI like the EABIs. One reason to do this is that the
13999 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14000 architecture. This code picks MIPS I for 'mips' and MIPS III for
14001 'mips64', just as we did in the days before 'from-abi'. */
14002 if (strcasecmp (cpu_string, "from-abi") == 0)
14003 {
14004 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14005 return mips_cpu_info_from_isa (ISA_MIPS1);
14006
14007 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14008 return mips_cpu_info_from_isa (ISA_MIPS3);
14009
14010 if (file_mips_gp32 >= 0)
14011 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14012
14013 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14014 ? ISA_MIPS3
14015 : ISA_MIPS1);
14016 }
14017
14018 /* 'default' has traditionally been a no-op. Probably not very useful. */
14019 if (strcasecmp (cpu_string, "default") == 0)
14020 return 0;
14021
14022 for (p = mips_cpu_info_table; p->name != 0; p++)
14023 if (mips_matching_cpu_name_p (p->name, cpu_string))
14024 return p;
14025
14026 as_bad ("Bad value (%s) for %s", cpu_string, option);
14027 return 0;
14028 }
14029
14030 /* Return the canonical processor information for ISA (a member of the
14031 ISA_MIPS* enumeration). */
14032
14033 static const struct mips_cpu_info *
14034 mips_cpu_info_from_isa (int isa)
14035 {
14036 int i;
14037
14038 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14039 if (mips_cpu_info_table[i].is_isa
14040 && isa == mips_cpu_info_table[i].isa)
14041 return (&mips_cpu_info_table[i]);
14042
14043 return NULL;
14044 }
14045
14046 static const struct mips_cpu_info *
14047 mips_cpu_info_from_arch (int arch)
14048 {
14049 int i;
14050
14051 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14052 if (arch == mips_cpu_info_table[i].cpu)
14053 return (&mips_cpu_info_table[i]);
14054
14055 return NULL;
14056 }
14057 \f
14058 static void
14059 show (FILE *stream, const char *string, int *col_p, int *first_p)
14060 {
14061 if (*first_p)
14062 {
14063 fprintf (stream, "%24s", "");
14064 *col_p = 24;
14065 }
14066 else
14067 {
14068 fprintf (stream, ", ");
14069 *col_p += 2;
14070 }
14071
14072 if (*col_p + strlen (string) > 72)
14073 {
14074 fprintf (stream, "\n%24s", "");
14075 *col_p = 24;
14076 }
14077
14078 fprintf (stream, "%s", string);
14079 *col_p += strlen (string);
14080
14081 *first_p = 0;
14082 }
14083
14084 void
14085 md_show_usage (FILE *stream)
14086 {
14087 int column, first;
14088 size_t i;
14089
14090 fprintf (stream, _("\
14091 MIPS options:\n\
14092 -EB generate big endian output\n\
14093 -EL generate little endian output\n\
14094 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14095 -G NUM allow referencing objects up to NUM bytes\n\
14096 implicitly with the gp register [default 8]\n"));
14097 fprintf (stream, _("\
14098 -mips1 generate MIPS ISA I instructions\n\
14099 -mips2 generate MIPS ISA II instructions\n\
14100 -mips3 generate MIPS ISA III instructions\n\
14101 -mips4 generate MIPS ISA IV instructions\n\
14102 -mips5 generate MIPS ISA V instructions\n\
14103 -mips32 generate MIPS32 ISA instructions\n\
14104 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14105 -mips64 generate MIPS64 ISA instructions\n\
14106 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14107 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14108
14109 first = 1;
14110
14111 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14112 show (stream, mips_cpu_info_table[i].name, &column, &first);
14113 show (stream, "from-abi", &column, &first);
14114 fputc ('\n', stream);
14115
14116 fprintf (stream, _("\
14117 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14118 -no-mCPU don't generate code specific to CPU.\n\
14119 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14120
14121 first = 1;
14122
14123 show (stream, "3900", &column, &first);
14124 show (stream, "4010", &column, &first);
14125 show (stream, "4100", &column, &first);
14126 show (stream, "4650", &column, &first);
14127 fputc ('\n', stream);
14128
14129 fprintf (stream, _("\
14130 -mips16 generate mips16 instructions\n\
14131 -no-mips16 do not generate mips16 instructions\n"));
14132 fprintf (stream, _("\
14133 -mfix-vr4120 work around certain VR4120 errata\n\
14134 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14135 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14136 -mno-shared optimize output for executables\n\
14137 -msym32 assume all symbols have 32-bit values\n\
14138 -O0 remove unneeded NOPs, do not swap branches\n\
14139 -O remove unneeded NOPs and swap branches\n\
14140 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14141 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14142 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14143 #ifdef OBJ_ELF
14144 fprintf (stream, _("\
14145 -KPIC, -call_shared generate SVR4 position independent code\n\
14146 -non_shared do not generate position independent code\n\
14147 -xgot assume a 32 bit GOT\n\
14148 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14149 -mshared, -mno-shared disable/enable .cpload optimization for\n\
14150 non-shared code\n\
14151 -mabi=ABI create ABI conformant object file for:\n"));
14152
14153 first = 1;
14154
14155 show (stream, "32", &column, &first);
14156 show (stream, "o64", &column, &first);
14157 show (stream, "n32", &column, &first);
14158 show (stream, "64", &column, &first);
14159 show (stream, "eabi", &column, &first);
14160
14161 fputc ('\n', stream);
14162
14163 fprintf (stream, _("\
14164 -32 create o32 ABI object file (default)\n\
14165 -n32 create n32 ABI object file\n\
14166 -64 create 64 ABI object file\n"));
14167 #endif
14168 }
14169
14170 enum dwarf2_format
14171 mips_dwarf2_format (void)
14172 {
14173 if (mips_abi == N64_ABI)
14174 {
14175 #ifdef TE_IRIX
14176 return dwarf2_format_64bit_irix;
14177 #else
14178 return dwarf2_format_64bit;
14179 #endif
14180 }
14181 else
14182 return dwarf2_format_32bit;
14183 }
14184
14185 int
14186 mips_dwarf2_addr_size (void)
14187 {
14188 if (mips_abi == N64_ABI)
14189 return 8;
14190 else
14191 return 4;
14192 }