bfd/ChangeLog
[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 /* The ABI to use. */
123 enum mips_abi_level
124 {
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131 };
132
133 /* MIPS ABI we are using for this output file. */
134 static enum mips_abi_level mips_abi = NO_ABI;
135
136 /* Whether or not we have code that can call pic code. */
137 int mips_abicalls = FALSE;
138
139 /* Whether or not we have code which can be put into a shared
140 library. */
141 static bfd_boolean mips_in_shared = TRUE;
142
143 /* This is the set of options which may be modified by the .set
144 pseudo-op. We use a struct so that .set push and .set pop are more
145 reliable. */
146
147 struct mips_set_options
148 {
149 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
150 if it has not been initialized. Changed by `.set mipsN', and the
151 -mipsN command line option, and the default CPU. */
152 int isa;
153 /* Enabled Application Specific Extensions (ASEs). These are set to -1
154 if they have not been initialized. Changed by `.set <asename>', by
155 command line options, and based on the default architecture. */
156 int ase_mips3d;
157 int ase_mdmx;
158 /* Whether we are assembling for the mips16 processor. 0 if we are
159 not, 1 if we are, and -1 if the value has not been initialized.
160 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
161 -nomips16 command line options, and the default CPU. */
162 int mips16;
163 /* Non-zero if we should not reorder instructions. Changed by `.set
164 reorder' and `.set noreorder'. */
165 int noreorder;
166 /* Non-zero if we should not permit the $at ($1) register to be used
167 in instructions. Changed by `.set at' and `.set noat'. */
168 int noat;
169 /* Non-zero if we should warn when a macro instruction expands into
170 more than one machine instruction. Changed by `.set nomacro' and
171 `.set macro'. */
172 int warn_about_macros;
173 /* Non-zero if we should not move instructions. Changed by `.set
174 move', `.set volatile', `.set nomove', and `.set novolatile'. */
175 int nomove;
176 /* Non-zero if we should not optimize branches by moving the target
177 of the branch into the delay slot. Actually, we don't perform
178 this optimization anyhow. Changed by `.set bopt' and `.set
179 nobopt'. */
180 int nobopt;
181 /* Non-zero if we should not autoextend mips16 instructions.
182 Changed by `.set autoextend' and `.set noautoextend'. */
183 int noautoextend;
184 /* Restrict general purpose registers and floating point registers
185 to 32 bit. This is initially determined when -mgp32 or -mfp32
186 is passed but can changed if the assembler code uses .set mipsN. */
187 int gp32;
188 int fp32;
189 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
190 command line option, and the default CPU. */
191 int arch;
192 /* True if ".set sym32" is in effect. */
193 bfd_boolean sym32;
194 };
195
196 /* True if -mgp32 was passed. */
197 static int file_mips_gp32 = -1;
198
199 /* True if -mfp32 was passed. */
200 static int file_mips_fp32 = -1;
201
202 /* This is the struct we use to hold the current set of options. Note
203 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
204 -1 to indicate that they have not been initialized. */
205
206 static struct mips_set_options mips_opts =
207 {
208 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
209 };
210
211 /* These variables are filled in with the masks of registers used.
212 The object format code reads them and puts them in the appropriate
213 place. */
214 unsigned long mips_gprmask;
215 unsigned long mips_cprmask[4];
216
217 /* MIPS ISA we are using for this output file. */
218 static int file_mips_isa = ISA_UNKNOWN;
219
220 /* True if -mips16 was passed or implied by arguments passed on the
221 command line (e.g., by -march). */
222 static int file_ase_mips16;
223
224 /* True if -mips3d was passed or implied by arguments passed on the
225 command line (e.g., by -march). */
226 static int file_ase_mips3d;
227
228 /* True if -mdmx was passed or implied by arguments passed on the
229 command line (e.g., by -march). */
230 static int file_ase_mdmx;
231
232 /* The argument of the -march= flag. The architecture we are assembling. */
233 static int file_mips_arch = CPU_UNKNOWN;
234 static const char *mips_arch_string;
235
236 /* The argument of the -mtune= flag. The architecture for which we
237 are optimizing. */
238 static int mips_tune = CPU_UNKNOWN;
239 static const char *mips_tune_string;
240
241 /* True when generating 32-bit code for a 64-bit processor. */
242 static int mips_32bitmode = 0;
243
244 /* True if the given ABI requires 32-bit registers. */
245 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
246
247 /* Likewise 64-bit registers. */
248 #define ABI_NEEDS_64BIT_REGS(ABI) \
249 ((ABI) == N32_ABI \
250 || (ABI) == N64_ABI \
251 || (ABI) == O64_ABI)
252
253 /* Return true if ISA supports 64 bit gp register instructions. */
254 #define ISA_HAS_64BIT_REGS(ISA) ( \
255 (ISA) == ISA_MIPS3 \
256 || (ISA) == ISA_MIPS4 \
257 || (ISA) == ISA_MIPS5 \
258 || (ISA) == ISA_MIPS64 \
259 || (ISA) == ISA_MIPS64R2 \
260 )
261
262 /* Return true if ISA supports 64-bit right rotate (dror et al.)
263 instructions. */
264 #define ISA_HAS_DROR(ISA) ( \
265 (ISA) == ISA_MIPS64R2 \
266 )
267
268 /* Return true if ISA supports 32-bit right rotate (ror et al.)
269 instructions. */
270 #define ISA_HAS_ROR(ISA) ( \
271 (ISA) == ISA_MIPS32R2 \
272 || (ISA) == ISA_MIPS64R2 \
273 )
274
275 #define HAVE_32BIT_GPRS \
276 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
277
278 #define HAVE_32BIT_FPRS \
279 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
284 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
285
286 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
287
288 /* True if relocations are stored in-place. */
289 #define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
290
291 /* The ABI-derived address size. */
292 #define HAVE_64BIT_ADDRESSES \
293 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
294 #define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
295
296 /* The size of symbolic constants (i.e., expressions of the form
297 "SYMBOL" or "SYMBOL + OFFSET"). */
298 #define HAVE_32BIT_SYMBOLS \
299 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
300 #define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
301
302 /* Addresses are loaded in different ways, depending on the address size
303 in use. The n32 ABI Documentation also mandates the use of additions
304 with overflow checking, but existing implementations don't follow it. */
305 #define ADDRESS_ADD_INSN \
306 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
307
308 #define ADDRESS_ADDI_INSN \
309 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
310
311 #define ADDRESS_LOAD_INSN \
312 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
313
314 #define ADDRESS_STORE_INSN \
315 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
316
317 /* Return true if the given CPU supports the MIPS16 ASE. */
318 #define CPU_HAS_MIPS16(cpu) \
319 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
320 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
321
322 /* Return true if the given CPU supports the MIPS3D ASE. */
323 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
324 )
325
326 /* Return true if the given CPU supports the MDMX ASE. */
327 #define CPU_HAS_MDMX(cpu) (FALSE \
328 )
329
330 /* True if CPU has a dror instruction. */
331 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
332
333 /* True if CPU has a ror instruction. */
334 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
335
336 /* True if mflo and mfhi can be immediately followed by instructions
337 which write to the HI and LO registers.
338
339 According to MIPS specifications, MIPS ISAs I, II, and III need
340 (at least) two instructions between the reads of HI/LO and
341 instructions which write them, and later ISAs do not. Contradicting
342 the MIPS specifications, some MIPS IV processor user manuals (e.g.
343 the UM for the NEC Vr5000) document needing the instructions between
344 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
345 MIPS64 and later ISAs to have the interlocks, plus any specific
346 earlier-ISA CPUs for which CPU documentation declares that the
347 instructions are really interlocked. */
348 #define hilo_interlocks \
349 (mips_opts.isa == ISA_MIPS32 \
350 || mips_opts.isa == ISA_MIPS32R2 \
351 || mips_opts.isa == ISA_MIPS64 \
352 || mips_opts.isa == ISA_MIPS64R2 \
353 || mips_opts.arch == CPU_R4010 \
354 || mips_opts.arch == CPU_R10000 \
355 || mips_opts.arch == CPU_R12000 \
356 || mips_opts.arch == CPU_RM7000 \
357 || mips_opts.arch == CPU_VR5500 \
358 )
359
360 /* Whether the processor uses hardware interlocks to protect reads
361 from the GPRs after they are loaded from memory, and thus does not
362 require nops to be inserted. This applies to instructions marked
363 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
364 level I. */
365 #define gpr_interlocks \
366 (mips_opts.isa != ISA_MIPS1 \
367 || mips_opts.arch == CPU_R3900)
368
369 /* Whether the processor uses hardware interlocks to avoid delays
370 required by coprocessor instructions, and thus does not require
371 nops to be inserted. This applies to instructions marked
372 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
373 between instructions marked INSN_WRITE_COND_CODE and ones marked
374 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
375 levels I, II, and III. */
376 /* Itbl support may require additional care here. */
377 #define cop_interlocks \
378 ((mips_opts.isa != ISA_MIPS1 \
379 && mips_opts.isa != ISA_MIPS2 \
380 && mips_opts.isa != ISA_MIPS3) \
381 || mips_opts.arch == CPU_R4300 \
382 )
383
384 /* Whether the processor uses hardware interlocks to protect reads
385 from coprocessor registers after they are loaded from memory, and
386 thus does not require nops to be inserted. This applies to
387 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
388 requires at MIPS ISA level I. */
389 #define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
390
391 /* Is this a mfhi or mflo instruction? */
392 #define MF_HILO_INSN(PINFO) \
393 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
394
395 /* MIPS PIC level. */
396
397 enum mips_pic_level mips_pic;
398
399 /* 1 if we should generate 32 bit offsets from the $gp register in
400 SVR4_PIC mode. Currently has no meaning in other modes. */
401 static int mips_big_got = 0;
402
403 /* 1 if trap instructions should used for overflow rather than break
404 instructions. */
405 static int mips_trap = 0;
406
407 /* 1 if double width floating point constants should not be constructed
408 by assembling two single width halves into two single width floating
409 point registers which just happen to alias the double width destination
410 register. On some architectures this aliasing can be disabled by a bit
411 in the status register, and the setting of this bit cannot be determined
412 automatically at assemble time. */
413 static int mips_disable_float_construction;
414
415 /* Non-zero if any .set noreorder directives were used. */
416
417 static int mips_any_noreorder;
418
419 /* Non-zero if nops should be inserted when the register referenced in
420 an mfhi/mflo instruction is read in the next two instructions. */
421 static int mips_7000_hilo_fix;
422
423 /* The size of the small data section. */
424 static unsigned int g_switch_value = 8;
425 /* Whether the -G option was used. */
426 static int g_switch_seen = 0;
427
428 #define N_RMASK 0xc4
429 #define N_VFP 0xd4
430
431 /* If we can determine in advance that GP optimization won't be
432 possible, we can skip the relaxation stuff that tries to produce
433 GP-relative references. This makes delay slot optimization work
434 better.
435
436 This function can only provide a guess, but it seems to work for
437 gcc output. It needs to guess right for gcc, otherwise gcc
438 will put what it thinks is a GP-relative instruction in a branch
439 delay slot.
440
441 I don't know if a fix is needed for the SVR4_PIC mode. I've only
442 fixed it for the non-PIC mode. KR 95/04/07 */
443 static int nopic_need_relax (symbolS *, int);
444
445 /* handle of the OPCODE hash table */
446 static struct hash_control *op_hash = NULL;
447
448 /* The opcode hash table we use for the mips16. */
449 static struct hash_control *mips16_op_hash = NULL;
450
451 /* This array holds the chars that always start a comment. If the
452 pre-processor is disabled, these aren't very useful */
453 const char comment_chars[] = "#";
454
455 /* This array holds the chars that only start a comment at the beginning of
456 a line. If the line seems to have the form '# 123 filename'
457 .line and .file directives will appear in the pre-processed output */
458 /* Note that input_file.c hand checks for '#' at the beginning of the
459 first line of the input file. This is because the compiler outputs
460 #NO_APP at the beginning of its output. */
461 /* Also note that C style comments are always supported. */
462 const char line_comment_chars[] = "#";
463
464 /* This array holds machine specific line separator characters. */
465 const char line_separator_chars[] = ";";
466
467 /* Chars that can be used to separate mant from exp in floating point nums */
468 const char EXP_CHARS[] = "eE";
469
470 /* Chars that mean this number is a floating point constant */
471 /* As in 0f12.456 */
472 /* or 0d1.2345e12 */
473 const char FLT_CHARS[] = "rRsSfFdDxXpP";
474
475 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
476 changed in read.c . Ideally it shouldn't have to know about it at all,
477 but nothing is ideal around here.
478 */
479
480 static char *insn_error;
481
482 static int auto_align = 1;
483
484 /* When outputting SVR4 PIC code, the assembler needs to know the
485 offset in the stack frame from which to restore the $gp register.
486 This is set by the .cprestore pseudo-op, and saved in this
487 variable. */
488 static offsetT mips_cprestore_offset = -1;
489
490 /* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
491 more optimizations, it can use a register value instead of a memory-saved
492 offset and even an other register than $gp as global pointer. */
493 static offsetT mips_cpreturn_offset = -1;
494 static int mips_cpreturn_register = -1;
495 static int mips_gp_register = GP;
496 static int mips_gprel_offset = 0;
497
498 /* Whether mips_cprestore_offset has been set in the current function
499 (or whether it has already been warned about, if not). */
500 static int mips_cprestore_valid = 0;
501
502 /* This is the register which holds the stack frame, as set by the
503 .frame pseudo-op. This is needed to implement .cprestore. */
504 static int mips_frame_reg = SP;
505
506 /* Whether mips_frame_reg has been set in the current function
507 (or whether it has already been warned about, if not). */
508 static int mips_frame_reg_valid = 0;
509
510 /* To output NOP instructions correctly, we need to keep information
511 about the previous two instructions. */
512
513 /* Whether we are optimizing. The default value of 2 means to remove
514 unneeded NOPs and swap branch instructions when possible. A value
515 of 1 means to not swap branches. A value of 0 means to always
516 insert NOPs. */
517 static int mips_optimize = 2;
518
519 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
520 equivalent to seeing no -g option at all. */
521 static int mips_debug = 0;
522
523 /* The previous instruction. */
524 static struct mips_cl_insn prev_insn;
525
526 /* The instruction before prev_insn. */
527 static struct mips_cl_insn prev_prev_insn;
528
529 /* If we don't want information for prev_insn or prev_prev_insn, we
530 point the insn_mo field at this dummy integer. */
531 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0, 0 };
532
533 /* Non-zero if prev_insn is valid. */
534 static int prev_insn_valid;
535
536 /* The frag for the previous instruction. */
537 static struct frag *prev_insn_frag;
538
539 /* The offset into prev_insn_frag for the previous instruction. */
540 static long prev_insn_where;
541
542 /* The reloc type for the previous instruction, if any. */
543 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
544
545 /* The reloc for the previous instruction, if any. */
546 static fixS *prev_insn_fixp[3];
547
548 /* Non-zero if the previous instruction was in a delay slot. */
549 static int prev_insn_is_delay_slot;
550
551 /* Non-zero if the previous instruction was in a .set noreorder. */
552 static int prev_insn_unreordered;
553
554 /* Non-zero if the previous instruction uses an extend opcode (if
555 mips16). */
556 static int prev_insn_extended;
557
558 /* Non-zero if the previous previous instruction was in a .set
559 noreorder. */
560 static int prev_prev_insn_unreordered;
561
562 /* If this is set, it points to a frag holding nop instructions which
563 were inserted before the start of a noreorder section. If those
564 nops turn out to be unnecessary, the size of the frag can be
565 decreased. */
566 static fragS *prev_nop_frag;
567
568 /* The number of nop instructions we created in prev_nop_frag. */
569 static int prev_nop_frag_holds;
570
571 /* The number of nop instructions that we know we need in
572 prev_nop_frag. */
573 static int prev_nop_frag_required;
574
575 /* The number of instructions we've seen since prev_nop_frag. */
576 static int prev_nop_frag_since;
577
578 /* For ECOFF and ELF, relocations against symbols are done in two
579 parts, with a HI relocation and a LO relocation. Each relocation
580 has only 16 bits of space to store an addend. This means that in
581 order for the linker to handle carries correctly, it must be able
582 to locate both the HI and the LO relocation. This means that the
583 relocations must appear in order in the relocation table.
584
585 In order to implement this, we keep track of each unmatched HI
586 relocation. We then sort them so that they immediately precede the
587 corresponding LO relocation. */
588
589 struct mips_hi_fixup
590 {
591 /* Next HI fixup. */
592 struct mips_hi_fixup *next;
593 /* This fixup. */
594 fixS *fixp;
595 /* The section this fixup is in. */
596 segT seg;
597 };
598
599 /* The list of unmatched HI relocs. */
600
601 static struct mips_hi_fixup *mips_hi_fixup_list;
602
603 /* The frag containing the last explicit relocation operator.
604 Null if explicit relocations have not been used. */
605
606 static fragS *prev_reloc_op_frag;
607
608 /* Map normal MIPS register numbers to mips16 register numbers. */
609
610 #define X ILLEGAL_REG
611 static const int mips32_to_16_reg_map[] =
612 {
613 X, X, 2, 3, 4, 5, 6, 7,
614 X, X, X, X, X, X, X, X,
615 0, 1, X, X, X, X, X, X,
616 X, X, X, X, X, X, X, X
617 };
618 #undef X
619
620 /* Map mips16 register numbers to normal MIPS register numbers. */
621
622 static const unsigned int mips16_to_32_reg_map[] =
623 {
624 16, 17, 2, 3, 4, 5, 6, 7
625 };
626
627 static int mips_fix_vr4120;
628
629 /* We don't relax branches by default, since this causes us to expand
630 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
631 fail to compute the offset before expanding the macro to the most
632 efficient expansion. */
633
634 static int mips_relax_branch;
635 \f
636 /* The expansion of many macros depends on the type of symbol that
637 they refer to. For example, when generating position-dependent code,
638 a macro that refers to a symbol may have two different expansions,
639 one which uses GP-relative addresses and one which uses absolute
640 addresses. When generating SVR4-style PIC, a macro may have
641 different expansions for local and global symbols.
642
643 We handle these situations by generating both sequences and putting
644 them in variant frags. In position-dependent code, the first sequence
645 will be the GP-relative one and the second sequence will be the
646 absolute one. In SVR4 PIC, the first sequence will be for global
647 symbols and the second will be for local symbols.
648
649 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
650 SECOND are the lengths of the two sequences in bytes. These fields
651 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
652 the subtype has the following flags:
653
654 RELAX_USE_SECOND
655 Set if it has been decided that we should use the second
656 sequence instead of the first.
657
658 RELAX_SECOND_LONGER
659 Set in the first variant frag if the macro's second implementation
660 is longer than its first. This refers to the macro as a whole,
661 not an individual relaxation.
662
663 RELAX_NOMACRO
664 Set in the first variant frag if the macro appeared in a .set nomacro
665 block and if one alternative requires a warning but the other does not.
666
667 RELAX_DELAY_SLOT
668 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
669 delay slot.
670
671 The frag's "opcode" points to the first fixup for relaxable code.
672
673 Relaxable macros are generated using a sequence such as:
674
675 relax_start (SYMBOL);
676 ... generate first expansion ...
677 relax_switch ();
678 ... generate second expansion ...
679 relax_end ();
680
681 The code and fixups for the unwanted alternative are discarded
682 by md_convert_frag. */
683 #define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
684
685 #define RELAX_FIRST(X) (((X) >> 8) & 0xff)
686 #define RELAX_SECOND(X) ((X) & 0xff)
687 #define RELAX_USE_SECOND 0x10000
688 #define RELAX_SECOND_LONGER 0x20000
689 #define RELAX_NOMACRO 0x40000
690 #define RELAX_DELAY_SLOT 0x80000
691
692 /* Branch without likely bit. If label is out of range, we turn:
693
694 beq reg1, reg2, label
695 delay slot
696
697 into
698
699 bne reg1, reg2, 0f
700 nop
701 j label
702 0: delay slot
703
704 with the following opcode replacements:
705
706 beq <-> bne
707 blez <-> bgtz
708 bltz <-> bgez
709 bc1f <-> bc1t
710
711 bltzal <-> bgezal (with jal label instead of j label)
712
713 Even though keeping the delay slot instruction in the delay slot of
714 the branch would be more efficient, it would be very tricky to do
715 correctly, because we'd have to introduce a variable frag *after*
716 the delay slot instruction, and expand that instead. Let's do it
717 the easy way for now, even if the branch-not-taken case now costs
718 one additional instruction. Out-of-range branches are not supposed
719 to be common, anyway.
720
721 Branch likely. If label is out of range, we turn:
722
723 beql reg1, reg2, label
724 delay slot (annulled if branch not taken)
725
726 into
727
728 beql reg1, reg2, 1f
729 nop
730 beql $0, $0, 2f
731 nop
732 1: j[al] label
733 delay slot (executed only if branch taken)
734 2:
735
736 It would be possible to generate a shorter sequence by losing the
737 likely bit, generating something like:
738
739 bne reg1, reg2, 0f
740 nop
741 j[al] label
742 delay slot (executed only if branch taken)
743 0:
744
745 beql -> bne
746 bnel -> beq
747 blezl -> bgtz
748 bgtzl -> blez
749 bltzl -> bgez
750 bgezl -> bltz
751 bc1fl -> bc1t
752 bc1tl -> bc1f
753
754 bltzall -> bgezal (with jal label instead of j label)
755 bgezall -> bltzal (ditto)
756
757
758 but it's not clear that it would actually improve performance. */
759 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
760 ((relax_substateT) \
761 (0xc0000000 \
762 | ((toofar) ? 1 : 0) \
763 | ((link) ? 2 : 0) \
764 | ((likely) ? 4 : 0) \
765 | ((uncond) ? 8 : 0)))
766 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
767 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
768 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
769 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
770 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
771
772 /* For mips16 code, we use an entirely different form of relaxation.
773 mips16 supports two versions of most instructions which take
774 immediate values: a small one which takes some small value, and a
775 larger one which takes a 16 bit value. Since branches also follow
776 this pattern, relaxing these values is required.
777
778 We can assemble both mips16 and normal MIPS code in a single
779 object. Therefore, we need to support this type of relaxation at
780 the same time that we support the relaxation described above. We
781 use the high bit of the subtype field to distinguish these cases.
782
783 The information we store for this type of relaxation is the
784 argument code found in the opcode file for this relocation, whether
785 the user explicitly requested a small or extended form, and whether
786 the relocation is in a jump or jal delay slot. That tells us the
787 size of the value, and how it should be stored. We also store
788 whether the fragment is considered to be extended or not. We also
789 store whether this is known to be a branch to a different section,
790 whether we have tried to relax this frag yet, and whether we have
791 ever extended a PC relative fragment because of a shift count. */
792 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
793 (0x80000000 \
794 | ((type) & 0xff) \
795 | ((small) ? 0x100 : 0) \
796 | ((ext) ? 0x200 : 0) \
797 | ((dslot) ? 0x400 : 0) \
798 | ((jal_dslot) ? 0x800 : 0))
799 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
800 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
801 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
802 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
803 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
804 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
805 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
806 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
807 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
808 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
809 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
810 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
811
812 /* Is the given value a sign-extended 32-bit value? */
813 #define IS_SEXT_32BIT_NUM(x) \
814 (((x) &~ (offsetT) 0x7fffffff) == 0 \
815 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
816
817 /* Is the given value a sign-extended 16-bit value? */
818 #define IS_SEXT_16BIT_NUM(x) \
819 (((x) &~ (offsetT) 0x7fff) == 0 \
820 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
821
822 \f
823 /* Global variables used when generating relaxable macros. See the
824 comment above RELAX_ENCODE for more details about how relaxation
825 is used. */
826 static struct {
827 /* 0 if we're not emitting a relaxable macro.
828 1 if we're emitting the first of the two relaxation alternatives.
829 2 if we're emitting the second alternative. */
830 int sequence;
831
832 /* The first relaxable fixup in the current frag. (In other words,
833 the first fixup that refers to relaxable code.) */
834 fixS *first_fixup;
835
836 /* sizes[0] says how many bytes of the first alternative are stored in
837 the current frag. Likewise sizes[1] for the second alternative. */
838 unsigned int sizes[2];
839
840 /* The symbol on which the choice of sequence depends. */
841 symbolS *symbol;
842 } mips_relax;
843 \f
844 /* Global variables used to decide whether a macro needs a warning. */
845 static struct {
846 /* True if the macro is in a branch delay slot. */
847 bfd_boolean delay_slot_p;
848
849 /* For relaxable macros, sizes[0] is the length of the first alternative
850 in bytes and sizes[1] is the length of the second alternative.
851 For non-relaxable macros, both elements give the length of the
852 macro in bytes. */
853 unsigned int sizes[2];
854
855 /* The first variant frag for this macro. */
856 fragS *first_frag;
857 } mips_macro_warning;
858 \f
859 /* Prototypes for static functions. */
860
861 #define internalError() \
862 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
863
864 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
865
866 static void append_insn
867 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
868 static void mips_no_prev_insn (int);
869 static void mips16_macro_build
870 (expressionS *, const char *, const char *, va_list);
871 static void load_register (int, expressionS *, int);
872 static void macro_start (void);
873 static void macro_end (void);
874 static void macro (struct mips_cl_insn * ip);
875 static void mips16_macro (struct mips_cl_insn * ip);
876 #ifdef LOSING_COMPILER
877 static void macro2 (struct mips_cl_insn * ip);
878 #endif
879 static void mips_ip (char *str, struct mips_cl_insn * ip);
880 static void mips16_ip (char *str, struct mips_cl_insn * ip);
881 static void mips16_immed
882 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
883 unsigned long *, bfd_boolean *, unsigned short *);
884 static size_t my_getSmallExpression
885 (expressionS *, bfd_reloc_code_real_type *, char *);
886 static void my_getExpression (expressionS *, char *);
887 static void s_align (int);
888 static void s_change_sec (int);
889 static void s_change_section (int);
890 static void s_cons (int);
891 static void s_float_cons (int);
892 static void s_mips_globl (int);
893 static void s_option (int);
894 static void s_mipsset (int);
895 static void s_abicalls (int);
896 static void s_cpload (int);
897 static void s_cpsetup (int);
898 static void s_cplocal (int);
899 static void s_cprestore (int);
900 static void s_cpreturn (int);
901 static void s_gpvalue (int);
902 static void s_gpword (int);
903 static void s_gpdword (int);
904 static void s_cpadd (int);
905 static void s_insn (int);
906 static void md_obj_begin (void);
907 static void md_obj_end (void);
908 static void s_mips_ent (int);
909 static void s_mips_end (int);
910 static void s_mips_frame (int);
911 static void s_mips_mask (int reg_type);
912 static void s_mips_stab (int);
913 static void s_mips_weakext (int);
914 static void s_mips_file (int);
915 static void s_mips_loc (int);
916 static bfd_boolean pic_need_relax (symbolS *, asection *);
917 static int relaxed_branch_length (fragS *, asection *, int);
918 static int validate_mips_insn (const struct mips_opcode *);
919
920 /* Table and functions used to map between CPU/ISA names, and
921 ISA levels, and CPU numbers. */
922
923 struct mips_cpu_info
924 {
925 const char *name; /* CPU or ISA name. */
926 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
927 int isa; /* ISA level. */
928 int cpu; /* CPU number (default CPU if ISA). */
929 };
930
931 static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
932 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
933 static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
934 \f
935 /* Pseudo-op table.
936
937 The following pseudo-ops from the Kane and Heinrich MIPS book
938 should be defined here, but are currently unsupported: .alias,
939 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
940
941 The following pseudo-ops from the Kane and Heinrich MIPS book are
942 specific to the type of debugging information being generated, and
943 should be defined by the object format: .aent, .begin, .bend,
944 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
945 .vreg.
946
947 The following pseudo-ops from the Kane and Heinrich MIPS book are
948 not MIPS CPU specific, but are also not specific to the object file
949 format. This file is probably the best place to define them, but
950 they are not currently supported: .asm0, .endr, .lab, .repeat,
951 .struct. */
952
953 static const pseudo_typeS mips_pseudo_table[] =
954 {
955 /* MIPS specific pseudo-ops. */
956 {"option", s_option, 0},
957 {"set", s_mipsset, 0},
958 {"rdata", s_change_sec, 'r'},
959 {"sdata", s_change_sec, 's'},
960 {"livereg", s_ignore, 0},
961 {"abicalls", s_abicalls, 0},
962 {"cpload", s_cpload, 0},
963 {"cpsetup", s_cpsetup, 0},
964 {"cplocal", s_cplocal, 0},
965 {"cprestore", s_cprestore, 0},
966 {"cpreturn", s_cpreturn, 0},
967 {"gpvalue", s_gpvalue, 0},
968 {"gpword", s_gpword, 0},
969 {"gpdword", s_gpdword, 0},
970 {"cpadd", s_cpadd, 0},
971 {"insn", s_insn, 0},
972
973 /* Relatively generic pseudo-ops that happen to be used on MIPS
974 chips. */
975 {"asciiz", stringer, 1},
976 {"bss", s_change_sec, 'b'},
977 {"err", s_err, 0},
978 {"half", s_cons, 1},
979 {"dword", s_cons, 3},
980 {"weakext", s_mips_weakext, 0},
981
982 /* These pseudo-ops are defined in read.c, but must be overridden
983 here for one reason or another. */
984 {"align", s_align, 0},
985 {"byte", s_cons, 0},
986 {"data", s_change_sec, 'd'},
987 {"double", s_float_cons, 'd'},
988 {"float", s_float_cons, 'f'},
989 {"globl", s_mips_globl, 0},
990 {"global", s_mips_globl, 0},
991 {"hword", s_cons, 1},
992 {"int", s_cons, 2},
993 {"long", s_cons, 2},
994 {"octa", s_cons, 4},
995 {"quad", s_cons, 3},
996 {"section", s_change_section, 0},
997 {"short", s_cons, 1},
998 {"single", s_float_cons, 'f'},
999 {"stabn", s_mips_stab, 'n'},
1000 {"text", s_change_sec, 't'},
1001 {"word", s_cons, 2},
1002
1003 { "extern", ecoff_directive_extern, 0},
1004
1005 { NULL, NULL, 0 },
1006 };
1007
1008 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1009 {
1010 /* These pseudo-ops should be defined by the object file format.
1011 However, a.out doesn't support them, so we have versions here. */
1012 {"aent", s_mips_ent, 1},
1013 {"bgnb", s_ignore, 0},
1014 {"end", s_mips_end, 0},
1015 {"endb", s_ignore, 0},
1016 {"ent", s_mips_ent, 0},
1017 {"file", s_mips_file, 0},
1018 {"fmask", s_mips_mask, 'F'},
1019 {"frame", s_mips_frame, 0},
1020 {"loc", s_mips_loc, 0},
1021 {"mask", s_mips_mask, 'R'},
1022 {"verstamp", s_ignore, 0},
1023 { NULL, NULL, 0 },
1024 };
1025
1026 extern void pop_insert (const pseudo_typeS *);
1027
1028 void
1029 mips_pop_insert (void)
1030 {
1031 pop_insert (mips_pseudo_table);
1032 if (! ECOFF_DEBUGGING)
1033 pop_insert (mips_nonecoff_pseudo_table);
1034 }
1035 \f
1036 /* Symbols labelling the current insn. */
1037
1038 struct insn_label_list
1039 {
1040 struct insn_label_list *next;
1041 symbolS *label;
1042 };
1043
1044 static struct insn_label_list *insn_labels;
1045 static struct insn_label_list *free_insn_labels;
1046
1047 static void mips_clear_insn_labels (void);
1048
1049 static inline void
1050 mips_clear_insn_labels (void)
1051 {
1052 register struct insn_label_list **pl;
1053
1054 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1055 ;
1056 *pl = insn_labels;
1057 insn_labels = NULL;
1058 }
1059 \f
1060 static char *expr_end;
1061
1062 /* Expressions which appear in instructions. These are set by
1063 mips_ip. */
1064
1065 static expressionS imm_expr;
1066 static expressionS imm2_expr;
1067 static expressionS offset_expr;
1068
1069 /* Relocs associated with imm_expr and offset_expr. */
1070
1071 static bfd_reloc_code_real_type imm_reloc[3]
1072 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1073 static bfd_reloc_code_real_type offset_reloc[3]
1074 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1075
1076 /* These are set by mips16_ip if an explicit extension is used. */
1077
1078 static bfd_boolean mips16_small, mips16_ext;
1079
1080 #ifdef OBJ_ELF
1081 /* The pdr segment for per procedure frame/regmask info. Not used for
1082 ECOFF debugging. */
1083
1084 static segT pdr_seg;
1085 #endif
1086
1087 /* The default target format to use. */
1088
1089 const char *
1090 mips_target_format (void)
1091 {
1092 switch (OUTPUT_FLAVOR)
1093 {
1094 case bfd_target_ecoff_flavour:
1095 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1096 case bfd_target_coff_flavour:
1097 return "pe-mips";
1098 case bfd_target_elf_flavour:
1099 #ifdef TE_TMIPS
1100 /* This is traditional mips. */
1101 return (target_big_endian
1102 ? (HAVE_64BIT_OBJECTS
1103 ? "elf64-tradbigmips"
1104 : (HAVE_NEWABI
1105 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1106 : (HAVE_64BIT_OBJECTS
1107 ? "elf64-tradlittlemips"
1108 : (HAVE_NEWABI
1109 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1110 #else
1111 return (target_big_endian
1112 ? (HAVE_64BIT_OBJECTS
1113 ? "elf64-bigmips"
1114 : (HAVE_NEWABI
1115 ? "elf32-nbigmips" : "elf32-bigmips"))
1116 : (HAVE_64BIT_OBJECTS
1117 ? "elf64-littlemips"
1118 : (HAVE_NEWABI
1119 ? "elf32-nlittlemips" : "elf32-littlemips")));
1120 #endif
1121 default:
1122 abort ();
1123 return NULL;
1124 }
1125 }
1126
1127 /* This function is called once, at assembler startup time. It should
1128 set up all the tables, etc. that the MD part of the assembler will need. */
1129
1130 void
1131 md_begin (void)
1132 {
1133 register const char *retval = NULL;
1134 int i = 0;
1135 int broken = 0;
1136
1137 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
1138 as_warn (_("Could not set architecture and machine"));
1139
1140 op_hash = hash_new ();
1141
1142 for (i = 0; i < NUMOPCODES;)
1143 {
1144 const char *name = mips_opcodes[i].name;
1145
1146 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
1147 if (retval != NULL)
1148 {
1149 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1150 mips_opcodes[i].name, retval);
1151 /* Probably a memory allocation problem? Give up now. */
1152 as_fatal (_("Broken assembler. No assembly attempted."));
1153 }
1154 do
1155 {
1156 if (mips_opcodes[i].pinfo != INSN_MACRO)
1157 {
1158 if (!validate_mips_insn (&mips_opcodes[i]))
1159 broken = 1;
1160 }
1161 ++i;
1162 }
1163 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1164 }
1165
1166 mips16_op_hash = hash_new ();
1167
1168 i = 0;
1169 while (i < bfd_mips16_num_opcodes)
1170 {
1171 const char *name = mips16_opcodes[i].name;
1172
1173 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
1174 if (retval != NULL)
1175 as_fatal (_("internal: can't hash `%s': %s"),
1176 mips16_opcodes[i].name, retval);
1177 do
1178 {
1179 if (mips16_opcodes[i].pinfo != INSN_MACRO
1180 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1181 != mips16_opcodes[i].match))
1182 {
1183 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1184 mips16_opcodes[i].name, mips16_opcodes[i].args);
1185 broken = 1;
1186 }
1187 ++i;
1188 }
1189 while (i < bfd_mips16_num_opcodes
1190 && strcmp (mips16_opcodes[i].name, name) == 0);
1191 }
1192
1193 if (broken)
1194 as_fatal (_("Broken assembler. No assembly attempted."));
1195
1196 /* We add all the general register names to the symbol table. This
1197 helps us detect invalid uses of them. */
1198 for (i = 0; i < 32; i++)
1199 {
1200 char buf[5];
1201
1202 sprintf (buf, "$%d", i);
1203 symbol_table_insert (symbol_new (buf, reg_section, i,
1204 &zero_address_frag));
1205 }
1206 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1207 &zero_address_frag));
1208 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1209 &zero_address_frag));
1210 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1211 &zero_address_frag));
1212 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1213 &zero_address_frag));
1214 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1215 &zero_address_frag));
1216 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1217 &zero_address_frag));
1218 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1219 &zero_address_frag));
1220 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1221 &zero_address_frag));
1222 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1223 &zero_address_frag));
1224
1225 /* If we don't add these register names to the symbol table, they
1226 may end up being added as regular symbols by operand(), and then
1227 make it to the object file as undefined in case they're not
1228 regarded as local symbols. They're local in o32, since `$' is a
1229 local symbol prefix, but not in n32 or n64. */
1230 for (i = 0; i < 8; i++)
1231 {
1232 char buf[6];
1233
1234 sprintf (buf, "$fcc%i", i);
1235 symbol_table_insert (symbol_new (buf, reg_section, -1,
1236 &zero_address_frag));
1237 }
1238
1239 mips_no_prev_insn (FALSE);
1240
1241 mips_gprmask = 0;
1242 mips_cprmask[0] = 0;
1243 mips_cprmask[1] = 0;
1244 mips_cprmask[2] = 0;
1245 mips_cprmask[3] = 0;
1246
1247 /* set the default alignment for the text section (2**2) */
1248 record_alignment (text_section, 2);
1249
1250 bfd_set_gp_size (stdoutput, g_switch_value);
1251
1252 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1253 {
1254 /* On a native system, sections must be aligned to 16 byte
1255 boundaries. When configured for an embedded ELF target, we
1256 don't bother. */
1257 if (strcmp (TARGET_OS, "elf") != 0)
1258 {
1259 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1260 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1261 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1262 }
1263
1264 /* Create a .reginfo section for register masks and a .mdebug
1265 section for debugging information. */
1266 {
1267 segT seg;
1268 subsegT subseg;
1269 flagword flags;
1270 segT sec;
1271
1272 seg = now_seg;
1273 subseg = now_subseg;
1274
1275 /* The ABI says this section should be loaded so that the
1276 running program can access it. However, we don't load it
1277 if we are configured for an embedded target */
1278 flags = SEC_READONLY | SEC_DATA;
1279 if (strcmp (TARGET_OS, "elf") != 0)
1280 flags |= SEC_ALLOC | SEC_LOAD;
1281
1282 if (mips_abi != N64_ABI)
1283 {
1284 sec = subseg_new (".reginfo", (subsegT) 0);
1285
1286 bfd_set_section_flags (stdoutput, sec, flags);
1287 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1288
1289 #ifdef OBJ_ELF
1290 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1291 #endif
1292 }
1293 else
1294 {
1295 /* The 64-bit ABI uses a .MIPS.options section rather than
1296 .reginfo section. */
1297 sec = subseg_new (".MIPS.options", (subsegT) 0);
1298 bfd_set_section_flags (stdoutput, sec, flags);
1299 bfd_set_section_alignment (stdoutput, sec, 3);
1300
1301 #ifdef OBJ_ELF
1302 /* Set up the option header. */
1303 {
1304 Elf_Internal_Options opthdr;
1305 char *f;
1306
1307 opthdr.kind = ODK_REGINFO;
1308 opthdr.size = (sizeof (Elf_External_Options)
1309 + sizeof (Elf64_External_RegInfo));
1310 opthdr.section = 0;
1311 opthdr.info = 0;
1312 f = frag_more (sizeof (Elf_External_Options));
1313 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1314 (Elf_External_Options *) f);
1315
1316 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1317 }
1318 #endif
1319 }
1320
1321 if (ECOFF_DEBUGGING)
1322 {
1323 sec = subseg_new (".mdebug", (subsegT) 0);
1324 (void) bfd_set_section_flags (stdoutput, sec,
1325 SEC_HAS_CONTENTS | SEC_READONLY);
1326 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1327 }
1328 #ifdef OBJ_ELF
1329 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
1330 {
1331 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1332 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1333 SEC_READONLY | SEC_RELOC
1334 | SEC_DEBUGGING);
1335 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1336 }
1337 #endif
1338
1339 subseg_set (seg, subseg);
1340 }
1341 }
1342
1343 if (! ECOFF_DEBUGGING)
1344 md_obj_begin ();
1345 }
1346
1347 void
1348 md_mips_end (void)
1349 {
1350 if (! ECOFF_DEBUGGING)
1351 md_obj_end ();
1352 }
1353
1354 void
1355 md_assemble (char *str)
1356 {
1357 struct mips_cl_insn insn;
1358 bfd_reloc_code_real_type unused_reloc[3]
1359 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1360
1361 imm_expr.X_op = O_absent;
1362 imm2_expr.X_op = O_absent;
1363 offset_expr.X_op = O_absent;
1364 imm_reloc[0] = BFD_RELOC_UNUSED;
1365 imm_reloc[1] = BFD_RELOC_UNUSED;
1366 imm_reloc[2] = BFD_RELOC_UNUSED;
1367 offset_reloc[0] = BFD_RELOC_UNUSED;
1368 offset_reloc[1] = BFD_RELOC_UNUSED;
1369 offset_reloc[2] = BFD_RELOC_UNUSED;
1370
1371 if (mips_opts.mips16)
1372 mips16_ip (str, &insn);
1373 else
1374 {
1375 mips_ip (str, &insn);
1376 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1377 str, insn.insn_opcode));
1378 }
1379
1380 if (insn_error)
1381 {
1382 as_bad ("%s `%s'", insn_error, str);
1383 return;
1384 }
1385
1386 if (insn.insn_mo->pinfo == INSN_MACRO)
1387 {
1388 macro_start ();
1389 if (mips_opts.mips16)
1390 mips16_macro (&insn);
1391 else
1392 macro (&insn);
1393 macro_end ();
1394 }
1395 else
1396 {
1397 if (imm_expr.X_op != O_absent)
1398 append_insn (&insn, &imm_expr, imm_reloc);
1399 else if (offset_expr.X_op != O_absent)
1400 append_insn (&insn, &offset_expr, offset_reloc);
1401 else
1402 append_insn (&insn, NULL, unused_reloc);
1403 }
1404 }
1405
1406 /* Return true if the given relocation might need a matching %lo().
1407 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1408 applied to local symbols. */
1409
1410 static inline bfd_boolean
1411 reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
1412 {
1413 return (HAVE_IN_PLACE_ADDENDS
1414 && (reloc == BFD_RELOC_HI16_S
1415 || reloc == BFD_RELOC_MIPS_GOT16
1416 || reloc == BFD_RELOC_MIPS16_HI16_S));
1417 }
1418
1419 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1420 relocation. */
1421
1422 static inline bfd_boolean
1423 fixup_has_matching_lo_p (fixS *fixp)
1424 {
1425 return (fixp->fx_next != NULL
1426 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1427 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
1428 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1429 && fixp->fx_offset == fixp->fx_next->fx_offset);
1430 }
1431
1432 /* See whether instruction IP reads register REG. CLASS is the type
1433 of register. */
1434
1435 static int
1436 insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1437 enum mips_regclass class)
1438 {
1439 if (class == MIPS16_REG)
1440 {
1441 assert (mips_opts.mips16);
1442 reg = mips16_to_32_reg_map[reg];
1443 class = MIPS_GR_REG;
1444 }
1445
1446 /* Don't report on general register ZERO, since it never changes. */
1447 if (class == MIPS_GR_REG && reg == ZERO)
1448 return 0;
1449
1450 if (class == MIPS_FP_REG)
1451 {
1452 assert (! mips_opts.mips16);
1453 /* If we are called with either $f0 or $f1, we must check $f0.
1454 This is not optimal, because it will introduce an unnecessary
1455 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1456 need to distinguish reading both $f0 and $f1 or just one of
1457 them. Note that we don't have to check the other way,
1458 because there is no instruction that sets both $f0 and $f1
1459 and requires a delay. */
1460 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1461 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1462 == (reg &~ (unsigned) 1)))
1463 return 1;
1464 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1465 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1466 == (reg &~ (unsigned) 1)))
1467 return 1;
1468 }
1469 else if (! mips_opts.mips16)
1470 {
1471 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1472 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1473 return 1;
1474 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1475 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1476 return 1;
1477 }
1478 else
1479 {
1480 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1481 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1482 & MIPS16OP_MASK_RX)]
1483 == reg))
1484 return 1;
1485 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1486 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1487 & MIPS16OP_MASK_RY)]
1488 == reg))
1489 return 1;
1490 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1491 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1492 & MIPS16OP_MASK_MOVE32Z)]
1493 == reg))
1494 return 1;
1495 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1496 return 1;
1497 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1498 return 1;
1499 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1500 return 1;
1501 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1502 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1503 & MIPS16OP_MASK_REGR32) == reg)
1504 return 1;
1505 }
1506
1507 return 0;
1508 }
1509
1510 /* This function returns true if modifying a register requires a
1511 delay. */
1512
1513 static int
1514 reg_needs_delay (unsigned int reg)
1515 {
1516 unsigned long prev_pinfo;
1517
1518 prev_pinfo = prev_insn.insn_mo->pinfo;
1519 if (! mips_opts.noreorder
1520 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1521 && ! gpr_interlocks)
1522 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1523 && ! cop_interlocks)))
1524 {
1525 /* A load from a coprocessor or from memory. All load delays
1526 delay the use of general register rt for one instruction. */
1527 /* Itbl support may require additional care here. */
1528 know (prev_pinfo & INSN_WRITE_GPR_T);
1529 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1530 return 1;
1531 }
1532
1533 return 0;
1534 }
1535
1536 /* Mark instruction labels in mips16 mode. This permits the linker to
1537 handle them specially, such as generating jalx instructions when
1538 needed. We also make them odd for the duration of the assembly, in
1539 order to generate the right sort of code. We will make them even
1540 in the adjust_symtab routine, while leaving them marked. This is
1541 convenient for the debugger and the disassembler. The linker knows
1542 to make them odd again. */
1543
1544 static void
1545 mips16_mark_labels (void)
1546 {
1547 if (mips_opts.mips16)
1548 {
1549 struct insn_label_list *l;
1550 valueT val;
1551
1552 for (l = insn_labels; l != NULL; l = l->next)
1553 {
1554 #ifdef OBJ_ELF
1555 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1556 S_SET_OTHER (l->label, STO_MIPS16);
1557 #endif
1558 val = S_GET_VALUE (l->label);
1559 if ((val & 1) == 0)
1560 S_SET_VALUE (l->label, val + 1);
1561 }
1562 }
1563 }
1564
1565 /* End the current frag. Make it a variant frag and record the
1566 relaxation info. */
1567
1568 static void
1569 relax_close_frag (void)
1570 {
1571 mips_macro_warning.first_frag = frag_now;
1572 frag_var (rs_machine_dependent, 0, 0,
1573 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
1574 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1575
1576 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1577 mips_relax.first_fixup = 0;
1578 }
1579
1580 /* Start a new relaxation sequence whose expansion depends on SYMBOL.
1581 See the comment above RELAX_ENCODE for more details. */
1582
1583 static void
1584 relax_start (symbolS *symbol)
1585 {
1586 assert (mips_relax.sequence == 0);
1587 mips_relax.sequence = 1;
1588 mips_relax.symbol = symbol;
1589 }
1590
1591 /* Start generating the second version of a relaxable sequence.
1592 See the comment above RELAX_ENCODE for more details. */
1593
1594 static void
1595 relax_switch (void)
1596 {
1597 assert (mips_relax.sequence == 1);
1598 mips_relax.sequence = 2;
1599 }
1600
1601 /* End the current relaxable sequence. */
1602
1603 static void
1604 relax_end (void)
1605 {
1606 assert (mips_relax.sequence == 2);
1607 relax_close_frag ();
1608 mips_relax.sequence = 0;
1609 }
1610
1611 /* Output an instruction. IP is the instruction information.
1612 ADDRESS_EXPR is an operand of the instruction to be used with
1613 RELOC_TYPE. */
1614
1615 static void
1616 append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
1617 bfd_reloc_code_real_type *reloc_type)
1618 {
1619 register unsigned long prev_pinfo, pinfo;
1620 char *f;
1621 fixS *fixp[3];
1622 int nops = 0;
1623 relax_stateT prev_insn_frag_type = 0;
1624 bfd_boolean relaxed_branch = FALSE;
1625 bfd_boolean force_new_frag = FALSE;
1626
1627 /* Mark instruction labels in mips16 mode. */
1628 mips16_mark_labels ();
1629
1630 prev_pinfo = prev_insn.insn_mo->pinfo;
1631 pinfo = ip->insn_mo->pinfo;
1632
1633 if (mips_relax.sequence != 2
1634 && (!mips_opts.noreorder || prev_nop_frag != NULL))
1635 {
1636 int prev_prev_nop;
1637
1638 /* If the previous insn required any delay slots, see if we need
1639 to insert a NOP or two. There are eight kinds of possible
1640 hazards, of which an instruction can have at most one type.
1641 (1) a load from memory delay
1642 (2) a load from a coprocessor delay
1643 (3) an unconditional branch delay
1644 (4) a conditional branch delay
1645 (5) a move to coprocessor register delay
1646 (6) a load coprocessor register from memory delay
1647 (7) a coprocessor condition code delay
1648 (8) a HI/LO special register delay
1649
1650 There are a lot of optimizations we could do that we don't.
1651 In particular, we do not, in general, reorder instructions.
1652 If you use gcc with optimization, it will reorder
1653 instructions and generally do much more optimization then we
1654 do here; repeating all that work in the assembler would only
1655 benefit hand written assembly code, and does not seem worth
1656 it. */
1657
1658 /* This is how a NOP is emitted. */
1659 #define emit_nop() \
1660 (mips_opts.mips16 \
1661 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1662 : md_number_to_chars (frag_more (4), 0, 4))
1663
1664 /* The previous insn might require a delay slot, depending upon
1665 the contents of the current insn. */
1666 if (! mips_opts.mips16
1667 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1668 && ! gpr_interlocks)
1669 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1670 && ! cop_interlocks)))
1671 {
1672 /* A load from a coprocessor or from memory. All load
1673 delays delay the use of general register rt for one
1674 instruction. */
1675 /* Itbl support may require additional care here. */
1676 know (prev_pinfo & INSN_WRITE_GPR_T);
1677 if (mips_optimize == 0
1678 || insn_uses_reg (ip,
1679 ((prev_insn.insn_opcode >> OP_SH_RT)
1680 & OP_MASK_RT),
1681 MIPS_GR_REG))
1682 ++nops;
1683 }
1684 else if (! mips_opts.mips16
1685 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1686 && ! cop_interlocks)
1687 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1688 && ! cop_mem_interlocks)))
1689 {
1690 /* A generic coprocessor delay. The previous instruction
1691 modified a coprocessor general or control register. If
1692 it modified a control register, we need to avoid any
1693 coprocessor instruction (this is probably not always
1694 required, but it sometimes is). If it modified a general
1695 register, we avoid using that register.
1696
1697 This case is not handled very well. There is no special
1698 knowledge of CP0 handling, and the coprocessors other
1699 than the floating point unit are not distinguished at
1700 all. */
1701 /* Itbl support may require additional care here. FIXME!
1702 Need to modify this to include knowledge about
1703 user specified delays! */
1704 if (prev_pinfo & INSN_WRITE_FPR_T)
1705 {
1706 if (mips_optimize == 0
1707 || insn_uses_reg (ip,
1708 ((prev_insn.insn_opcode >> OP_SH_FT)
1709 & OP_MASK_FT),
1710 MIPS_FP_REG))
1711 ++nops;
1712 }
1713 else if (prev_pinfo & INSN_WRITE_FPR_S)
1714 {
1715 if (mips_optimize == 0
1716 || insn_uses_reg (ip,
1717 ((prev_insn.insn_opcode >> OP_SH_FS)
1718 & OP_MASK_FS),
1719 MIPS_FP_REG))
1720 ++nops;
1721 }
1722 else
1723 {
1724 /* We don't know exactly what the previous instruction
1725 does. If the current instruction uses a coprocessor
1726 register, we must insert a NOP. If previous
1727 instruction may set the condition codes, and the
1728 current instruction uses them, we must insert two
1729 NOPS. */
1730 /* Itbl support may require additional care here. */
1731 if (mips_optimize == 0
1732 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1733 && (pinfo & INSN_READ_COND_CODE)))
1734 nops += 2;
1735 else if (pinfo & INSN_COP)
1736 ++nops;
1737 }
1738 }
1739 else if (! mips_opts.mips16
1740 && (prev_pinfo & INSN_WRITE_COND_CODE)
1741 && ! cop_interlocks)
1742 {
1743 /* The previous instruction sets the coprocessor condition
1744 codes, but does not require a general coprocessor delay
1745 (this means it is a floating point comparison
1746 instruction). If this instruction uses the condition
1747 codes, we need to insert a single NOP. */
1748 /* Itbl support may require additional care here. */
1749 if (mips_optimize == 0
1750 || (pinfo & INSN_READ_COND_CODE))
1751 ++nops;
1752 }
1753
1754 /* If we're fixing up mfhi/mflo for the r7000 and the
1755 previous insn was an mfhi/mflo and the current insn
1756 reads the register that the mfhi/mflo wrote to, then
1757 insert two nops. */
1758
1759 else if (mips_7000_hilo_fix
1760 && MF_HILO_INSN (prev_pinfo)
1761 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1762 & OP_MASK_RD),
1763 MIPS_GR_REG))
1764 {
1765 nops += 2;
1766 }
1767
1768 /* If we're fixing up mfhi/mflo for the r7000 and the
1769 2nd previous insn was an mfhi/mflo and the current insn
1770 reads the register that the mfhi/mflo wrote to, then
1771 insert one nop. */
1772
1773 else if (mips_7000_hilo_fix
1774 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1775 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1776 & OP_MASK_RD),
1777 MIPS_GR_REG))
1778
1779 {
1780 ++nops;
1781 }
1782
1783 else if (prev_pinfo & INSN_READ_LO)
1784 {
1785 /* The previous instruction reads the LO register; if the
1786 current instruction writes to the LO register, we must
1787 insert two NOPS. Some newer processors have interlocks.
1788 Also the tx39's multiply instructions can be executed
1789 immediately after a read from HI/LO (without the delay),
1790 though the tx39's divide insns still do require the
1791 delay. */
1792 if (! (hilo_interlocks
1793 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1794 && (mips_optimize == 0
1795 || (pinfo & INSN_WRITE_LO)))
1796 nops += 2;
1797 /* Most mips16 branch insns don't have a delay slot.
1798 If a read from LO is immediately followed by a branch
1799 to a write to LO we have a read followed by a write
1800 less than 2 insns away. We assume the target of
1801 a branch might be a write to LO, and insert a nop
1802 between a read and an immediately following branch. */
1803 else if (mips_opts.mips16
1804 && (mips_optimize == 0
1805 || (pinfo & MIPS16_INSN_BRANCH)))
1806 ++nops;
1807 }
1808 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1809 {
1810 /* The previous instruction reads the HI register; if the
1811 current instruction writes to the HI register, we must
1812 insert a NOP. Some newer processors have interlocks.
1813 Also the note tx39's multiply above. */
1814 if (! (hilo_interlocks
1815 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
1816 && (mips_optimize == 0
1817 || (pinfo & INSN_WRITE_HI)))
1818 nops += 2;
1819 /* Most mips16 branch insns don't have a delay slot.
1820 If a read from HI is immediately followed by a branch
1821 to a write to HI we have a read followed by a write
1822 less than 2 insns away. We assume the target of
1823 a branch might be a write to HI, and insert a nop
1824 between a read and an immediately following branch. */
1825 else if (mips_opts.mips16
1826 && (mips_optimize == 0
1827 || (pinfo & MIPS16_INSN_BRANCH)))
1828 ++nops;
1829 }
1830
1831 /* If the previous instruction was in a noreorder section, then
1832 we don't want to insert the nop after all. */
1833 /* Itbl support may require additional care here. */
1834 if (prev_insn_unreordered)
1835 nops = 0;
1836
1837 /* There are two cases which require two intervening
1838 instructions: 1) setting the condition codes using a move to
1839 coprocessor instruction which requires a general coprocessor
1840 delay and then reading the condition codes 2) reading the HI
1841 or LO register and then writing to it (except on processors
1842 which have interlocks). If we are not already emitting a NOP
1843 instruction, we must check for these cases compared to the
1844 instruction previous to the previous instruction. */
1845 if ((! mips_opts.mips16
1846 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1847 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1848 && (pinfo & INSN_READ_COND_CODE)
1849 && ! cop_interlocks)
1850 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1851 && (pinfo & INSN_WRITE_LO)
1852 && ! (hilo_interlocks
1853 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
1854 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1855 && (pinfo & INSN_WRITE_HI)
1856 && ! (hilo_interlocks
1857 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
1858 prev_prev_nop = 1;
1859 else
1860 prev_prev_nop = 0;
1861
1862 if (prev_prev_insn_unreordered)
1863 prev_prev_nop = 0;
1864
1865 if (prev_prev_nop && nops == 0)
1866 ++nops;
1867
1868 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
1869 {
1870 /* We're out of bits in pinfo, so we must resort to string
1871 ops here. Shortcuts are selected based on opcodes being
1872 limited to the VR4120 instruction set. */
1873 int min_nops = 0;
1874 const char *pn = prev_insn.insn_mo->name;
1875 const char *tn = ip->insn_mo->name;
1876 if (strncmp (pn, "macc", 4) == 0
1877 || strncmp (pn, "dmacc", 5) == 0)
1878 {
1879 /* Errata 21 - [D]DIV[U] after [D]MACC */
1880 if (strstr (tn, "div"))
1881 min_nops = 1;
1882
1883 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1884 instruction is executed immediately after a MACC or
1885 DMACC instruction, the result of [either instruction]
1886 is incorrect." */
1887 if (strncmp (tn, "mult", 4) == 0
1888 || strncmp (tn, "dmult", 5) == 0)
1889 min_nops = 1;
1890
1891 /* Errata 23 - Continuous DMULT[U]/DMACC instructions.
1892 Applies on top of VR4181A MD(1) errata. */
1893 if (pn[0] == 'd' && strncmp (tn, "dmacc", 5) == 0)
1894 min_nops = 1;
1895
1896 /* Errata 24 - MT{LO,HI} after [D]MACC */
1897 if (strcmp (tn, "mtlo") == 0
1898 || strcmp (tn, "mthi") == 0)
1899 min_nops = 1;
1900 }
1901 else if (strncmp (pn, "dmult", 5) == 0
1902 && (strncmp (tn, "dmult", 5) == 0
1903 || strncmp (tn, "dmacc", 5) == 0))
1904 {
1905 /* Here is the rest of errata 23. */
1906 min_nops = 1;
1907 }
1908 else if ((strncmp (pn, "dmult", 5) == 0 || strstr (pn, "div"))
1909 && (strncmp (tn, "macc", 4) == 0
1910 || strncmp (tn, "dmacc", 5) == 0))
1911 {
1912 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1913 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1914 DDIV or DDIVU instruction, the result of the MACC or
1915 DMACC instruction is incorrect.". This partly overlaps
1916 the workaround for errata 23. */
1917 min_nops = 1;
1918 }
1919 if (nops < min_nops)
1920 nops = min_nops;
1921 }
1922
1923 /* If we are being given a nop instruction, don't bother with
1924 one of the nops we would otherwise output. This will only
1925 happen when a nop instruction is used with mips_optimize set
1926 to 0. */
1927 if (nops > 0
1928 && ! mips_opts.noreorder
1929 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1930 --nops;
1931
1932 /* Now emit the right number of NOP instructions. */
1933 if (nops > 0 && ! mips_opts.noreorder)
1934 {
1935 fragS *old_frag;
1936 unsigned long old_frag_offset;
1937 int i;
1938 struct insn_label_list *l;
1939
1940 old_frag = frag_now;
1941 old_frag_offset = frag_now_fix ();
1942
1943 for (i = 0; i < nops; i++)
1944 emit_nop ();
1945
1946 if (listing)
1947 {
1948 listing_prev_line ();
1949 /* We may be at the start of a variant frag. In case we
1950 are, make sure there is enough space for the frag
1951 after the frags created by listing_prev_line. The
1952 argument to frag_grow here must be at least as large
1953 as the argument to all other calls to frag_grow in
1954 this file. We don't have to worry about being in the
1955 middle of a variant frag, because the variants insert
1956 all needed nop instructions themselves. */
1957 frag_grow (40);
1958 }
1959
1960 for (l = insn_labels; l != NULL; l = l->next)
1961 {
1962 valueT val;
1963
1964 assert (S_GET_SEGMENT (l->label) == now_seg);
1965 symbol_set_frag (l->label, frag_now);
1966 val = (valueT) frag_now_fix ();
1967 /* mips16 text labels are stored as odd. */
1968 if (mips_opts.mips16)
1969 ++val;
1970 S_SET_VALUE (l->label, val);
1971 }
1972
1973 #ifndef NO_ECOFF_DEBUGGING
1974 if (ECOFF_DEBUGGING)
1975 ecoff_fix_loc (old_frag, old_frag_offset);
1976 #endif
1977 }
1978 else if (prev_nop_frag != NULL)
1979 {
1980 /* We have a frag holding nops we may be able to remove. If
1981 we don't need any nops, we can decrease the size of
1982 prev_nop_frag by the size of one instruction. If we do
1983 need some nops, we count them in prev_nops_required. */
1984 if (prev_nop_frag_since == 0)
1985 {
1986 if (nops == 0)
1987 {
1988 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1989 --prev_nop_frag_holds;
1990 }
1991 else
1992 prev_nop_frag_required += nops;
1993 }
1994 else
1995 {
1996 if (prev_prev_nop == 0)
1997 {
1998 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1999 --prev_nop_frag_holds;
2000 }
2001 else
2002 ++prev_nop_frag_required;
2003 }
2004
2005 if (prev_nop_frag_holds <= prev_nop_frag_required)
2006 prev_nop_frag = NULL;
2007
2008 ++prev_nop_frag_since;
2009
2010 /* Sanity check: by the time we reach the second instruction
2011 after prev_nop_frag, we should have used up all the nops
2012 one way or another. */
2013 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2014 }
2015 }
2016
2017 #ifdef OBJ_ELF
2018 /* The value passed to dwarf2_emit_insn is the distance between
2019 the beginning of the current instruction and the address that
2020 should be recorded in the debug tables. For MIPS16 debug info
2021 we want to use ISA-encoded addresses, so we pass -1 for an
2022 address higher by one than the current. */
2023 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2024 #endif
2025
2026 /* Record the frag type before frag_var. */
2027 if (prev_insn_frag)
2028 prev_insn_frag_type = prev_insn_frag->fr_type;
2029
2030 if (address_expr
2031 && *reloc_type == BFD_RELOC_16_PCREL_S2
2032 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2033 || pinfo & INSN_COND_BRANCH_LIKELY)
2034 && mips_relax_branch
2035 /* Don't try branch relaxation within .set nomacro, or within
2036 .set noat if we use $at for PIC computations. If it turns
2037 out that the branch was out-of-range, we'll get an error. */
2038 && !mips_opts.warn_about_macros
2039 && !(mips_opts.noat && mips_pic != NO_PIC)
2040 && !mips_opts.mips16)
2041 {
2042 relaxed_branch = TRUE;
2043 f = frag_var (rs_machine_dependent,
2044 relaxed_branch_length
2045 (NULL, NULL,
2046 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2047 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2048 RELAX_BRANCH_ENCODE
2049 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2050 pinfo & INSN_COND_BRANCH_LIKELY,
2051 pinfo & INSN_WRITE_GPR_31,
2052 0),
2053 address_expr->X_add_symbol,
2054 address_expr->X_add_number,
2055 0);
2056 *reloc_type = BFD_RELOC_UNUSED;
2057 }
2058 else if (*reloc_type > BFD_RELOC_UNUSED)
2059 {
2060 /* We need to set up a variant frag. */
2061 assert (mips_opts.mips16 && address_expr != NULL);
2062 f = frag_var (rs_machine_dependent, 4, 0,
2063 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2064 mips16_small, mips16_ext,
2065 (prev_pinfo
2066 & INSN_UNCOND_BRANCH_DELAY),
2067 (*prev_insn_reloc_type
2068 == BFD_RELOC_MIPS16_JMP)),
2069 make_expr_symbol (address_expr), 0, NULL);
2070 }
2071 else if (mips_opts.mips16
2072 && ! ip->use_extend
2073 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2074 {
2075 /* Make sure there is enough room to swap this instruction with
2076 a following jump instruction. */
2077 frag_grow (6);
2078 f = frag_more (2);
2079 }
2080 else
2081 {
2082 if (mips_opts.mips16
2083 && mips_opts.noreorder
2084 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2085 as_warn (_("extended instruction in delay slot"));
2086
2087 if (mips_relax.sequence)
2088 {
2089 /* If we've reached the end of this frag, turn it into a variant
2090 frag and record the information for the instructions we've
2091 written so far. */
2092 if (frag_room () < 4)
2093 relax_close_frag ();
2094 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2095 }
2096
2097 if (mips_relax.sequence != 2)
2098 mips_macro_warning.sizes[0] += 4;
2099 if (mips_relax.sequence != 1)
2100 mips_macro_warning.sizes[1] += 4;
2101
2102 f = frag_more (4);
2103 }
2104
2105 fixp[0] = fixp[1] = fixp[2] = NULL;
2106 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
2107 {
2108 if (address_expr->X_op == O_constant)
2109 {
2110 unsigned int tmp;
2111
2112 switch (*reloc_type)
2113 {
2114 case BFD_RELOC_32:
2115 ip->insn_opcode |= address_expr->X_add_number;
2116 break;
2117
2118 case BFD_RELOC_MIPS_HIGHEST:
2119 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2120 ip->insn_opcode |= tmp & 0xffff;
2121 break;
2122
2123 case BFD_RELOC_MIPS_HIGHER:
2124 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2125 ip->insn_opcode |= tmp & 0xffff;
2126 break;
2127
2128 case BFD_RELOC_HI16_S:
2129 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2130 ip->insn_opcode |= tmp & 0xffff;
2131 break;
2132
2133 case BFD_RELOC_HI16:
2134 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2135 break;
2136
2137 case BFD_RELOC_UNUSED:
2138 case BFD_RELOC_LO16:
2139 case BFD_RELOC_MIPS_GOT_DISP:
2140 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2141 break;
2142
2143 case BFD_RELOC_MIPS_JMP:
2144 if ((address_expr->X_add_number & 3) != 0)
2145 as_bad (_("jump to misaligned address (0x%lx)"),
2146 (unsigned long) address_expr->X_add_number);
2147 if (address_expr->X_add_number & ~0xfffffff)
2148 as_bad (_("jump address range overflow (0x%lx)"),
2149 (unsigned long) address_expr->X_add_number);
2150 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2151 break;
2152
2153 case BFD_RELOC_MIPS16_JMP:
2154 if ((address_expr->X_add_number & 3) != 0)
2155 as_bad (_("jump to misaligned address (0x%lx)"),
2156 (unsigned long) address_expr->X_add_number);
2157 if (address_expr->X_add_number & ~0xfffffff)
2158 as_bad (_("jump address range overflow (0x%lx)"),
2159 (unsigned long) address_expr->X_add_number);
2160 ip->insn_opcode |=
2161 (((address_expr->X_add_number & 0x7c0000) << 3)
2162 | ((address_expr->X_add_number & 0xf800000) >> 7)
2163 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2164 break;
2165
2166 case BFD_RELOC_16_PCREL_S2:
2167 goto need_reloc;
2168
2169 default:
2170 internalError ();
2171 }
2172 }
2173 else if (*reloc_type < BFD_RELOC_UNUSED)
2174 need_reloc:
2175 {
2176 reloc_howto_type *howto;
2177 int i;
2178
2179 /* In a compound relocation, it is the final (outermost)
2180 operator that determines the relocated field. */
2181 for (i = 1; i < 3; i++)
2182 if (reloc_type[i] == BFD_RELOC_UNUSED)
2183 break;
2184
2185 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2186 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2187 bfd_get_reloc_size(howto),
2188 address_expr,
2189 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2190 reloc_type[0]);
2191
2192 /* These relocations can have an addend that won't fit in
2193 4 octets for 64bit assembly. */
2194 if (HAVE_64BIT_GPRS
2195 && ! howto->partial_inplace
2196 && (reloc_type[0] == BFD_RELOC_16
2197 || reloc_type[0] == BFD_RELOC_32
2198 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2199 || reloc_type[0] == BFD_RELOC_HI16_S
2200 || reloc_type[0] == BFD_RELOC_LO16
2201 || reloc_type[0] == BFD_RELOC_GPREL16
2202 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2203 || reloc_type[0] == BFD_RELOC_GPREL32
2204 || reloc_type[0] == BFD_RELOC_64
2205 || reloc_type[0] == BFD_RELOC_CTOR
2206 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2207 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2208 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2209 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2210 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2211 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2212 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2213 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2214 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
2215 fixp[0]->fx_no_overflow = 1;
2216
2217 if (mips_relax.sequence)
2218 {
2219 if (mips_relax.first_fixup == 0)
2220 mips_relax.first_fixup = fixp[0];
2221 }
2222 else if (reloc_needs_lo_p (*reloc_type))
2223 {
2224 struct mips_hi_fixup *hi_fixup;
2225
2226 /* Reuse the last entry if it already has a matching %lo. */
2227 hi_fixup = mips_hi_fixup_list;
2228 if (hi_fixup == 0
2229 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2230 {
2231 hi_fixup = ((struct mips_hi_fixup *)
2232 xmalloc (sizeof (struct mips_hi_fixup)));
2233 hi_fixup->next = mips_hi_fixup_list;
2234 mips_hi_fixup_list = hi_fixup;
2235 }
2236 hi_fixup->fixp = fixp[0];
2237 hi_fixup->seg = now_seg;
2238 }
2239
2240 /* Add fixups for the second and third relocations, if given.
2241 Note that the ABI allows the second relocation to be
2242 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2243 moment we only use RSS_UNDEF, but we could add support
2244 for the others if it ever becomes necessary. */
2245 for (i = 1; i < 3; i++)
2246 if (reloc_type[i] != BFD_RELOC_UNUSED)
2247 {
2248 fixp[i] = fix_new (frag_now, fixp[0]->fx_where,
2249 fixp[0]->fx_size, NULL, 0,
2250 FALSE, reloc_type[i]);
2251
2252 /* Use fx_tcbit to mark compound relocs. */
2253 fixp[0]->fx_tcbit = 1;
2254 fixp[i]->fx_tcbit = 1;
2255 }
2256 }
2257 }
2258
2259 if (! mips_opts.mips16)
2260 md_number_to_chars (f, ip->insn_opcode, 4);
2261 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2262 {
2263 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2264 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2265 }
2266 else
2267 {
2268 if (ip->use_extend)
2269 {
2270 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2271 f += 2;
2272 }
2273 md_number_to_chars (f, ip->insn_opcode, 2);
2274 }
2275
2276 /* Update the register mask information. */
2277 if (! mips_opts.mips16)
2278 {
2279 if (pinfo & INSN_WRITE_GPR_D)
2280 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2281 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2282 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2283 if (pinfo & INSN_READ_GPR_S)
2284 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2285 if (pinfo & INSN_WRITE_GPR_31)
2286 mips_gprmask |= 1 << RA;
2287 if (pinfo & INSN_WRITE_FPR_D)
2288 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2289 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2290 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2291 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2292 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2293 if ((pinfo & INSN_READ_FPR_R) != 0)
2294 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2295 if (pinfo & INSN_COP)
2296 {
2297 /* We don't keep enough information to sort these cases out.
2298 The itbl support does keep this information however, although
2299 we currently don't support itbl fprmats as part of the cop
2300 instruction. May want to add this support in the future. */
2301 }
2302 /* Never set the bit for $0, which is always zero. */
2303 mips_gprmask &= ~1 << 0;
2304 }
2305 else
2306 {
2307 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2308 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2309 & MIPS16OP_MASK_RX);
2310 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2311 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2312 & MIPS16OP_MASK_RY);
2313 if (pinfo & MIPS16_INSN_WRITE_Z)
2314 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2315 & MIPS16OP_MASK_RZ);
2316 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2317 mips_gprmask |= 1 << TREG;
2318 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2319 mips_gprmask |= 1 << SP;
2320 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2321 mips_gprmask |= 1 << RA;
2322 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2323 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2324 if (pinfo & MIPS16_INSN_READ_Z)
2325 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2326 & MIPS16OP_MASK_MOVE32Z);
2327 if (pinfo & MIPS16_INSN_READ_GPR_X)
2328 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2329 & MIPS16OP_MASK_REGR32);
2330 }
2331
2332 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2333 {
2334 /* Filling the branch delay slot is more complex. We try to
2335 switch the branch with the previous instruction, which we can
2336 do if the previous instruction does not set up a condition
2337 that the branch tests and if the branch is not itself the
2338 target of any branch. */
2339 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2340 || (pinfo & INSN_COND_BRANCH_DELAY))
2341 {
2342 if (mips_optimize < 2
2343 /* If we have seen .set volatile or .set nomove, don't
2344 optimize. */
2345 || mips_opts.nomove != 0
2346 /* If we had to emit any NOP instructions, then we
2347 already know we can not swap. */
2348 || nops != 0
2349 /* If we don't even know the previous insn, we can not
2350 swap. */
2351 || ! prev_insn_valid
2352 /* If the previous insn is already in a branch delay
2353 slot, then we can not swap. */
2354 || prev_insn_is_delay_slot
2355 /* If the previous previous insn was in a .set
2356 noreorder, we can't swap. Actually, the MIPS
2357 assembler will swap in this situation. However, gcc
2358 configured -with-gnu-as will generate code like
2359 .set noreorder
2360 lw $4,XXX
2361 .set reorder
2362 INSN
2363 bne $4,$0,foo
2364 in which we can not swap the bne and INSN. If gcc is
2365 not configured -with-gnu-as, it does not output the
2366 .set pseudo-ops. We don't have to check
2367 prev_insn_unreordered, because prev_insn_valid will
2368 be 0 in that case. We don't want to use
2369 prev_prev_insn_valid, because we do want to be able
2370 to swap at the start of a function. */
2371 || prev_prev_insn_unreordered
2372 /* If the branch is itself the target of a branch, we
2373 can not swap. We cheat on this; all we check for is
2374 whether there is a label on this instruction. If
2375 there are any branches to anything other than a
2376 label, users must use .set noreorder. */
2377 || insn_labels != NULL
2378 /* If the previous instruction is in a variant frag
2379 other than this branch's one, we cannot do the swap.
2380 This does not apply to the mips16, which uses variant
2381 frags for different purposes. */
2382 || (! mips_opts.mips16
2383 && prev_insn_frag_type == rs_machine_dependent)
2384 /* If the branch reads the condition codes, we don't
2385 even try to swap, because in the sequence
2386 ctc1 $X,$31
2387 INSN
2388 INSN
2389 bc1t LABEL
2390 we can not swap, and I don't feel like handling that
2391 case. */
2392 || (! mips_opts.mips16
2393 && (pinfo & INSN_READ_COND_CODE)
2394 && ! cop_interlocks)
2395 /* We can not swap with an instruction that requires a
2396 delay slot, because the target of the branch might
2397 interfere with that instruction. */
2398 || (! mips_opts.mips16
2399 && (prev_pinfo
2400 /* Itbl support may require additional care here. */
2401 & (INSN_LOAD_COPROC_DELAY
2402 | INSN_COPROC_MOVE_DELAY
2403 | INSN_WRITE_COND_CODE))
2404 && ! cop_interlocks)
2405 || (! (hilo_interlocks
2406 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
2407 && (prev_pinfo
2408 & (INSN_READ_LO
2409 | INSN_READ_HI)))
2410 || (! mips_opts.mips16
2411 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2412 && ! gpr_interlocks)
2413 || (! mips_opts.mips16
2414 /* Itbl support may require additional care here. */
2415 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2416 && ! cop_mem_interlocks)
2417 /* We can not swap with a branch instruction. */
2418 || (prev_pinfo
2419 & (INSN_UNCOND_BRANCH_DELAY
2420 | INSN_COND_BRANCH_DELAY
2421 | INSN_COND_BRANCH_LIKELY))
2422 /* We do not swap with a trap instruction, since it
2423 complicates trap handlers to have the trap
2424 instruction be in a delay slot. */
2425 || (prev_pinfo & INSN_TRAP)
2426 /* If the branch reads a register that the previous
2427 instruction sets, we can not swap. */
2428 || (! mips_opts.mips16
2429 && (prev_pinfo & INSN_WRITE_GPR_T)
2430 && insn_uses_reg (ip,
2431 ((prev_insn.insn_opcode >> OP_SH_RT)
2432 & OP_MASK_RT),
2433 MIPS_GR_REG))
2434 || (! mips_opts.mips16
2435 && (prev_pinfo & INSN_WRITE_GPR_D)
2436 && insn_uses_reg (ip,
2437 ((prev_insn.insn_opcode >> OP_SH_RD)
2438 & OP_MASK_RD),
2439 MIPS_GR_REG))
2440 || (mips_opts.mips16
2441 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2442 && insn_uses_reg (ip,
2443 ((prev_insn.insn_opcode
2444 >> MIPS16OP_SH_RX)
2445 & MIPS16OP_MASK_RX),
2446 MIPS16_REG))
2447 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2448 && insn_uses_reg (ip,
2449 ((prev_insn.insn_opcode
2450 >> MIPS16OP_SH_RY)
2451 & MIPS16OP_MASK_RY),
2452 MIPS16_REG))
2453 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2454 && insn_uses_reg (ip,
2455 ((prev_insn.insn_opcode
2456 >> MIPS16OP_SH_RZ)
2457 & MIPS16OP_MASK_RZ),
2458 MIPS16_REG))
2459 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2460 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2461 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2462 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2463 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2464 && insn_uses_reg (ip,
2465 MIPS16OP_EXTRACT_REG32R (prev_insn.
2466 insn_opcode),
2467 MIPS_GR_REG))))
2468 /* If the branch writes a register that the previous
2469 instruction sets, we can not swap (we know that
2470 branches write only to RD or to $31). */
2471 || (! mips_opts.mips16
2472 && (prev_pinfo & INSN_WRITE_GPR_T)
2473 && (((pinfo & INSN_WRITE_GPR_D)
2474 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2475 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2476 || ((pinfo & INSN_WRITE_GPR_31)
2477 && (((prev_insn.insn_opcode >> OP_SH_RT)
2478 & OP_MASK_RT)
2479 == RA))))
2480 || (! mips_opts.mips16
2481 && (prev_pinfo & INSN_WRITE_GPR_D)
2482 && (((pinfo & INSN_WRITE_GPR_D)
2483 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2484 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2485 || ((pinfo & INSN_WRITE_GPR_31)
2486 && (((prev_insn.insn_opcode >> OP_SH_RD)
2487 & OP_MASK_RD)
2488 == RA))))
2489 || (mips_opts.mips16
2490 && (pinfo & MIPS16_INSN_WRITE_31)
2491 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2492 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2493 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2494 == RA))))
2495 /* If the branch writes a register that the previous
2496 instruction reads, we can not swap (we know that
2497 branches only write to RD or to $31). */
2498 || (! mips_opts.mips16
2499 && (pinfo & INSN_WRITE_GPR_D)
2500 && insn_uses_reg (&prev_insn,
2501 ((ip->insn_opcode >> OP_SH_RD)
2502 & OP_MASK_RD),
2503 MIPS_GR_REG))
2504 || (! mips_opts.mips16
2505 && (pinfo & INSN_WRITE_GPR_31)
2506 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2507 || (mips_opts.mips16
2508 && (pinfo & MIPS16_INSN_WRITE_31)
2509 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2510 /* If the previous previous instruction has a load
2511 delay, and sets a register that the branch reads, we
2512 can not swap. */
2513 || (! mips_opts.mips16
2514 /* Itbl support may require additional care here. */
2515 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2516 && ! cop_interlocks)
2517 || ((prev_prev_insn.insn_mo->pinfo
2518 & INSN_LOAD_MEMORY_DELAY)
2519 && ! gpr_interlocks))
2520 && insn_uses_reg (ip,
2521 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2522 & OP_MASK_RT),
2523 MIPS_GR_REG))
2524 /* If one instruction sets a condition code and the
2525 other one uses a condition code, we can not swap. */
2526 || ((pinfo & INSN_READ_COND_CODE)
2527 && (prev_pinfo & INSN_WRITE_COND_CODE))
2528 || ((pinfo & INSN_WRITE_COND_CODE)
2529 && (prev_pinfo & INSN_READ_COND_CODE))
2530 /* If the previous instruction uses the PC, we can not
2531 swap. */
2532 || (mips_opts.mips16
2533 && (prev_pinfo & MIPS16_INSN_READ_PC))
2534 /* If the previous instruction was extended, we can not
2535 swap. */
2536 || (mips_opts.mips16 && prev_insn_extended)
2537 /* If the previous instruction had a fixup in mips16
2538 mode, we can not swap. This normally means that the
2539 previous instruction was a 4 byte branch anyhow. */
2540 || (mips_opts.mips16 && prev_insn_fixp[0])
2541 /* If the previous instruction is a sync, sync.l, or
2542 sync.p, we can not swap. */
2543 || (prev_pinfo & INSN_SYNC))
2544 {
2545 /* We could do even better for unconditional branches to
2546 portions of this object file; we could pick up the
2547 instruction at the destination, put it in the delay
2548 slot, and bump the destination address. */
2549 emit_nop ();
2550 if (mips_relax.sequence)
2551 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2552 /* Update the previous insn information. */
2553 prev_prev_insn = *ip;
2554 prev_insn.insn_mo = &dummy_opcode;
2555 }
2556 else
2557 {
2558 /* It looks like we can actually do the swap. */
2559 if (! mips_opts.mips16)
2560 {
2561 char *prev_f;
2562 char temp[4];
2563
2564 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2565 if (!relaxed_branch)
2566 {
2567 /* If this is not a relaxed branch, then just
2568 swap the instructions. */
2569 memcpy (temp, prev_f, 4);
2570 memcpy (prev_f, f, 4);
2571 memcpy (f, temp, 4);
2572 }
2573 else
2574 {
2575 /* If this is a relaxed branch, then we move the
2576 instruction to be placed in the delay slot to
2577 the current frag, shrinking the fixed part of
2578 the originating frag. If the branch occupies
2579 the tail of the latter, we move it backwards,
2580 into the space freed by the moved instruction. */
2581 f = frag_more (4);
2582 memcpy (f, prev_f, 4);
2583 prev_insn_frag->fr_fix -= 4;
2584 if (prev_insn_frag->fr_type == rs_machine_dependent)
2585 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2586 }
2587
2588 if (prev_insn_fixp[0])
2589 {
2590 prev_insn_fixp[0]->fx_frag = frag_now;
2591 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2592 }
2593 if (prev_insn_fixp[1])
2594 {
2595 prev_insn_fixp[1]->fx_frag = frag_now;
2596 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2597 }
2598 if (prev_insn_fixp[2])
2599 {
2600 prev_insn_fixp[2]->fx_frag = frag_now;
2601 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2602 }
2603 if (prev_insn_fixp[0] && HAVE_NEWABI
2604 && prev_insn_frag != frag_now
2605 && (prev_insn_fixp[0]->fx_r_type
2606 == BFD_RELOC_MIPS_GOT_DISP
2607 || (prev_insn_fixp[0]->fx_r_type
2608 == BFD_RELOC_MIPS_CALL16)))
2609 {
2610 /* To avoid confusion in tc_gen_reloc, we must
2611 ensure that this does not become a variant
2612 frag. */
2613 force_new_frag = TRUE;
2614 }
2615
2616 if (!relaxed_branch)
2617 {
2618 if (fixp[0])
2619 {
2620 fixp[0]->fx_frag = prev_insn_frag;
2621 fixp[0]->fx_where = prev_insn_where;
2622 }
2623 if (fixp[1])
2624 {
2625 fixp[1]->fx_frag = prev_insn_frag;
2626 fixp[1]->fx_where = prev_insn_where;
2627 }
2628 if (fixp[2])
2629 {
2630 fixp[2]->fx_frag = prev_insn_frag;
2631 fixp[2]->fx_where = prev_insn_where;
2632 }
2633 }
2634 else if (prev_insn_frag->fr_type == rs_machine_dependent)
2635 {
2636 if (fixp[0])
2637 fixp[0]->fx_where -= 4;
2638 if (fixp[1])
2639 fixp[1]->fx_where -= 4;
2640 if (fixp[2])
2641 fixp[2]->fx_where -= 4;
2642 }
2643 }
2644 else
2645 {
2646 char *prev_f;
2647 char temp[2];
2648
2649 assert (prev_insn_fixp[0] == NULL);
2650 assert (prev_insn_fixp[1] == NULL);
2651 assert (prev_insn_fixp[2] == NULL);
2652 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2653 memcpy (temp, prev_f, 2);
2654 memcpy (prev_f, f, 2);
2655 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2656 {
2657 assert (*reloc_type == BFD_RELOC_UNUSED);
2658 memcpy (f, temp, 2);
2659 }
2660 else
2661 {
2662 memcpy (f, f + 2, 2);
2663 memcpy (f + 2, temp, 2);
2664 }
2665 if (fixp[0])
2666 {
2667 fixp[0]->fx_frag = prev_insn_frag;
2668 fixp[0]->fx_where = prev_insn_where;
2669 }
2670 if (fixp[1])
2671 {
2672 fixp[1]->fx_frag = prev_insn_frag;
2673 fixp[1]->fx_where = prev_insn_where;
2674 }
2675 if (fixp[2])
2676 {
2677 fixp[2]->fx_frag = prev_insn_frag;
2678 fixp[2]->fx_where = prev_insn_where;
2679 }
2680 }
2681
2682 /* Update the previous insn information; leave prev_insn
2683 unchanged. */
2684 prev_prev_insn = *ip;
2685 }
2686 prev_insn_is_delay_slot = 1;
2687
2688 /* If that was an unconditional branch, forget the previous
2689 insn information. */
2690 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2691 {
2692 prev_prev_insn.insn_mo = &dummy_opcode;
2693 prev_insn.insn_mo = &dummy_opcode;
2694 }
2695
2696 prev_insn_fixp[0] = NULL;
2697 prev_insn_fixp[1] = NULL;
2698 prev_insn_fixp[2] = NULL;
2699 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2700 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2701 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2702 prev_insn_extended = 0;
2703 }
2704 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2705 {
2706 /* We don't yet optimize a branch likely. What we should do
2707 is look at the target, copy the instruction found there
2708 into the delay slot, and increment the branch to jump to
2709 the next instruction. */
2710 emit_nop ();
2711 /* Update the previous insn information. */
2712 prev_prev_insn = *ip;
2713 prev_insn.insn_mo = &dummy_opcode;
2714 prev_insn_fixp[0] = NULL;
2715 prev_insn_fixp[1] = NULL;
2716 prev_insn_fixp[2] = NULL;
2717 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2718 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2719 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2720 prev_insn_extended = 0;
2721 prev_insn_is_delay_slot = 1;
2722 }
2723 else
2724 {
2725 /* Update the previous insn information. */
2726 if (nops > 0)
2727 prev_prev_insn.insn_mo = &dummy_opcode;
2728 else
2729 prev_prev_insn = prev_insn;
2730 prev_insn = *ip;
2731
2732 /* Any time we see a branch, we always fill the delay slot
2733 immediately; since this insn is not a branch, we know it
2734 is not in a delay slot. */
2735 prev_insn_is_delay_slot = 0;
2736
2737 prev_insn_fixp[0] = fixp[0];
2738 prev_insn_fixp[1] = fixp[1];
2739 prev_insn_fixp[2] = fixp[2];
2740 prev_insn_reloc_type[0] = reloc_type[0];
2741 prev_insn_reloc_type[1] = reloc_type[1];
2742 prev_insn_reloc_type[2] = reloc_type[2];
2743 if (mips_opts.mips16)
2744 prev_insn_extended = (ip->use_extend
2745 || *reloc_type > BFD_RELOC_UNUSED);
2746 }
2747
2748 prev_prev_insn_unreordered = prev_insn_unreordered;
2749 prev_insn_unreordered = 0;
2750 prev_insn_frag = frag_now;
2751 prev_insn_where = f - frag_now->fr_literal;
2752 prev_insn_valid = 1;
2753 }
2754 else if (mips_relax.sequence != 2)
2755 {
2756 /* We need to record a bit of information even when we are not
2757 reordering, in order to determine the base address for mips16
2758 PC relative relocs. */
2759 prev_prev_insn = prev_insn;
2760 prev_insn = *ip;
2761 prev_insn_reloc_type[0] = reloc_type[0];
2762 prev_insn_reloc_type[1] = reloc_type[1];
2763 prev_insn_reloc_type[2] = reloc_type[2];
2764 prev_prev_insn_unreordered = prev_insn_unreordered;
2765 prev_insn_unreordered = 1;
2766 }
2767
2768 /* We just output an insn, so the next one doesn't have a label. */
2769 mips_clear_insn_labels ();
2770 }
2771
2772 /* This function forgets that there was any previous instruction or
2773 label. If PRESERVE is non-zero, it remembers enough information to
2774 know whether nops are needed before a noreorder section. */
2775
2776 static void
2777 mips_no_prev_insn (int preserve)
2778 {
2779 if (! preserve)
2780 {
2781 prev_insn.insn_mo = &dummy_opcode;
2782 prev_prev_insn.insn_mo = &dummy_opcode;
2783 prev_nop_frag = NULL;
2784 prev_nop_frag_holds = 0;
2785 prev_nop_frag_required = 0;
2786 prev_nop_frag_since = 0;
2787 }
2788 prev_insn_valid = 0;
2789 prev_insn_is_delay_slot = 0;
2790 prev_insn_unreordered = 0;
2791 prev_insn_extended = 0;
2792 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2793 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2794 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2795 prev_prev_insn_unreordered = 0;
2796 mips_clear_insn_labels ();
2797 }
2798
2799 /* This function must be called whenever we turn on noreorder or emit
2800 something other than instructions. It inserts any NOPS which might
2801 be needed by the previous instruction, and clears the information
2802 kept for the previous instructions. The INSNS parameter is true if
2803 instructions are to follow. */
2804
2805 static void
2806 mips_emit_delays (bfd_boolean insns)
2807 {
2808 if (! mips_opts.noreorder)
2809 {
2810 int nops;
2811
2812 nops = 0;
2813 if ((! mips_opts.mips16
2814 && ((prev_insn.insn_mo->pinfo
2815 & (INSN_LOAD_COPROC_DELAY
2816 | INSN_COPROC_MOVE_DELAY
2817 | INSN_WRITE_COND_CODE))
2818 && ! cop_interlocks))
2819 || (! hilo_interlocks
2820 && (prev_insn.insn_mo->pinfo
2821 & (INSN_READ_LO
2822 | INSN_READ_HI)))
2823 || (! mips_opts.mips16
2824 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2825 && ! gpr_interlocks)
2826 || (! mips_opts.mips16
2827 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2828 && ! cop_mem_interlocks))
2829 {
2830 /* Itbl support may require additional care here. */
2831 ++nops;
2832 if ((! mips_opts.mips16
2833 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2834 && ! cop_interlocks))
2835 || (! hilo_interlocks
2836 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2837 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2838 ++nops;
2839
2840 if (prev_insn_unreordered)
2841 nops = 0;
2842 }
2843 else if ((! mips_opts.mips16
2844 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2845 && ! cop_interlocks))
2846 || (! hilo_interlocks
2847 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2848 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2849 {
2850 /* Itbl support may require additional care here. */
2851 if (! prev_prev_insn_unreordered)
2852 ++nops;
2853 }
2854
2855 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
2856 {
2857 int min_nops = 0;
2858 const char *pn = prev_insn.insn_mo->name;
2859 if (strncmp (pn, "macc", 4) == 0
2860 || strncmp (pn, "dmacc", 5) == 0
2861 || strncmp (pn, "dmult", 5) == 0
2862 || strstr (pn, "div"))
2863 min_nops = 1;
2864 if (nops < min_nops)
2865 nops = min_nops;
2866 }
2867
2868 if (nops > 0)
2869 {
2870 struct insn_label_list *l;
2871
2872 if (insns)
2873 {
2874 /* Record the frag which holds the nop instructions, so
2875 that we can remove them if we don't need them. */
2876 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2877 prev_nop_frag = frag_now;
2878 prev_nop_frag_holds = nops;
2879 prev_nop_frag_required = 0;
2880 prev_nop_frag_since = 0;
2881 }
2882
2883 for (; nops > 0; --nops)
2884 emit_nop ();
2885
2886 if (insns)
2887 {
2888 /* Move on to a new frag, so that it is safe to simply
2889 decrease the size of prev_nop_frag. */
2890 frag_wane (frag_now);
2891 frag_new (0);
2892 }
2893
2894 for (l = insn_labels; l != NULL; l = l->next)
2895 {
2896 valueT val;
2897
2898 assert (S_GET_SEGMENT (l->label) == now_seg);
2899 symbol_set_frag (l->label, frag_now);
2900 val = (valueT) frag_now_fix ();
2901 /* mips16 text labels are stored as odd. */
2902 if (mips_opts.mips16)
2903 ++val;
2904 S_SET_VALUE (l->label, val);
2905 }
2906 }
2907 }
2908
2909 /* Mark instruction labels in mips16 mode. */
2910 if (insns)
2911 mips16_mark_labels ();
2912
2913 mips_no_prev_insn (insns);
2914 }
2915
2916 /* Set up global variables for the start of a new macro. */
2917
2918 static void
2919 macro_start (void)
2920 {
2921 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2922 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2923 && (prev_insn.insn_mo->pinfo
2924 & (INSN_UNCOND_BRANCH_DELAY
2925 | INSN_COND_BRANCH_DELAY
2926 | INSN_COND_BRANCH_LIKELY)) != 0);
2927 }
2928
2929 /* Given that a macro is longer than 4 bytes, return the appropriate warning
2930 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2931 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2932
2933 static const char *
2934 macro_warning (relax_substateT subtype)
2935 {
2936 if (subtype & RELAX_DELAY_SLOT)
2937 return _("Macro instruction expanded into multiple instructions"
2938 " in a branch delay slot");
2939 else if (subtype & RELAX_NOMACRO)
2940 return _("Macro instruction expanded into multiple instructions");
2941 else
2942 return 0;
2943 }
2944
2945 /* Finish up a macro. Emit warnings as appropriate. */
2946
2947 static void
2948 macro_end (void)
2949 {
2950 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2951 {
2952 relax_substateT subtype;
2953
2954 /* Set up the relaxation warning flags. */
2955 subtype = 0;
2956 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2957 subtype |= RELAX_SECOND_LONGER;
2958 if (mips_opts.warn_about_macros)
2959 subtype |= RELAX_NOMACRO;
2960 if (mips_macro_warning.delay_slot_p)
2961 subtype |= RELAX_DELAY_SLOT;
2962
2963 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2964 {
2965 /* Either the macro has a single implementation or both
2966 implementations are longer than 4 bytes. Emit the
2967 warning now. */
2968 const char *msg = macro_warning (subtype);
2969 if (msg != 0)
2970 as_warn (msg);
2971 }
2972 else
2973 {
2974 /* One implementation might need a warning but the other
2975 definitely doesn't. */
2976 mips_macro_warning.first_frag->fr_subtype |= subtype;
2977 }
2978 }
2979 }
2980
2981 /* Read a macro's relocation codes from *ARGS and store them in *R.
2982 The first argument in *ARGS will be either the code for a single
2983 relocation or -1 followed by the three codes that make up a
2984 composite relocation. */
2985
2986 static void
2987 macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2988 {
2989 int i, next;
2990
2991 next = va_arg (*args, int);
2992 if (next >= 0)
2993 r[0] = (bfd_reloc_code_real_type) next;
2994 else
2995 for (i = 0; i < 3; i++)
2996 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2997 }
2998
2999 /* Build an instruction created by a macro expansion. This is passed
3000 a pointer to the count of instructions created so far, an
3001 expression, the name of the instruction to build, an operand format
3002 string, and corresponding arguments. */
3003
3004 static void
3005 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
3006 {
3007 struct mips_cl_insn insn;
3008 bfd_reloc_code_real_type r[3];
3009 va_list args;
3010
3011 va_start (args, fmt);
3012
3013 if (mips_opts.mips16)
3014 {
3015 mips16_macro_build (ep, name, fmt, args);
3016 va_end (args);
3017 return;
3018 }
3019
3020 r[0] = BFD_RELOC_UNUSED;
3021 r[1] = BFD_RELOC_UNUSED;
3022 r[2] = BFD_RELOC_UNUSED;
3023 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3024 assert (insn.insn_mo);
3025 assert (strcmp (name, insn.insn_mo->name) == 0);
3026
3027 /* Search until we get a match for NAME. */
3028 while (1)
3029 {
3030 /* It is assumed here that macros will never generate
3031 MDMX or MIPS-3D instructions. */
3032 if (strcmp (fmt, insn.insn_mo->args) == 0
3033 && insn.insn_mo->pinfo != INSN_MACRO
3034 && OPCODE_IS_MEMBER (insn.insn_mo,
3035 (mips_opts.isa
3036 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3037 mips_opts.arch)
3038 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3039 break;
3040
3041 ++insn.insn_mo;
3042 assert (insn.insn_mo->name);
3043 assert (strcmp (name, insn.insn_mo->name) == 0);
3044 }
3045
3046 insn.insn_opcode = insn.insn_mo->match;
3047 for (;;)
3048 {
3049 switch (*fmt++)
3050 {
3051 case '\0':
3052 break;
3053
3054 case ',':
3055 case '(':
3056 case ')':
3057 continue;
3058
3059 case '+':
3060 switch (*fmt++)
3061 {
3062 case 'A':
3063 case 'E':
3064 insn.insn_opcode |= (va_arg (args, int)
3065 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3066 continue;
3067
3068 case 'B':
3069 case 'F':
3070 /* Note that in the macro case, these arguments are already
3071 in MSB form. (When handling the instruction in the
3072 non-macro case, these arguments are sizes from which
3073 MSB values must be calculated.) */
3074 insn.insn_opcode |= (va_arg (args, int)
3075 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3076 continue;
3077
3078 case 'C':
3079 case 'G':
3080 case 'H':
3081 /* Note that in the macro case, these arguments are already
3082 in MSBD form. (When handling the instruction in the
3083 non-macro case, these arguments are sizes from which
3084 MSBD values must be calculated.) */
3085 insn.insn_opcode |= (va_arg (args, int)
3086 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3087 continue;
3088
3089 default:
3090 internalError ();
3091 }
3092 continue;
3093
3094 case 't':
3095 case 'w':
3096 case 'E':
3097 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3098 continue;
3099
3100 case 'c':
3101 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3102 continue;
3103
3104 case 'T':
3105 case 'W':
3106 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3107 continue;
3108
3109 case 'd':
3110 case 'G':
3111 case 'K':
3112 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3113 continue;
3114
3115 case 'U':
3116 {
3117 int tmp = va_arg (args, int);
3118
3119 insn.insn_opcode |= tmp << OP_SH_RT;
3120 insn.insn_opcode |= tmp << OP_SH_RD;
3121 continue;
3122 }
3123
3124 case 'V':
3125 case 'S':
3126 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3127 continue;
3128
3129 case 'z':
3130 continue;
3131
3132 case '<':
3133 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3134 continue;
3135
3136 case 'D':
3137 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3138 continue;
3139
3140 case 'B':
3141 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3142 continue;
3143
3144 case 'J':
3145 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3146 continue;
3147
3148 case 'q':
3149 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3150 continue;
3151
3152 case 'b':
3153 case 's':
3154 case 'r':
3155 case 'v':
3156 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3157 continue;
3158
3159 case 'i':
3160 case 'j':
3161 case 'o':
3162 macro_read_relocs (&args, r);
3163 assert (*r == BFD_RELOC_GPREL16
3164 || *r == BFD_RELOC_MIPS_LITERAL
3165 || *r == BFD_RELOC_MIPS_HIGHER
3166 || *r == BFD_RELOC_HI16_S
3167 || *r == BFD_RELOC_LO16
3168 || *r == BFD_RELOC_MIPS_GOT16
3169 || *r == BFD_RELOC_MIPS_CALL16
3170 || *r == BFD_RELOC_MIPS_GOT_DISP
3171 || *r == BFD_RELOC_MIPS_GOT_PAGE
3172 || *r == BFD_RELOC_MIPS_GOT_OFST
3173 || *r == BFD_RELOC_MIPS_GOT_LO16
3174 || *r == BFD_RELOC_MIPS_CALL_LO16);
3175 continue;
3176
3177 case 'u':
3178 macro_read_relocs (&args, r);
3179 assert (ep != NULL
3180 && (ep->X_op == O_constant
3181 || (ep->X_op == O_symbol
3182 && (*r == BFD_RELOC_MIPS_HIGHEST
3183 || *r == BFD_RELOC_HI16_S
3184 || *r == BFD_RELOC_HI16
3185 || *r == BFD_RELOC_GPREL16
3186 || *r == BFD_RELOC_MIPS_GOT_HI16
3187 || *r == BFD_RELOC_MIPS_CALL_HI16))));
3188 continue;
3189
3190 case 'p':
3191 assert (ep != NULL);
3192 /*
3193 * This allows macro() to pass an immediate expression for
3194 * creating short branches without creating a symbol.
3195 * Note that the expression still might come from the assembly
3196 * input, in which case the value is not checked for range nor
3197 * is a relocation entry generated (yuck).
3198 */
3199 if (ep->X_op == O_constant)
3200 {
3201 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3202 ep = NULL;
3203 }
3204 else
3205 *r = BFD_RELOC_16_PCREL_S2;
3206 continue;
3207
3208 case 'a':
3209 assert (ep != NULL);
3210 *r = BFD_RELOC_MIPS_JMP;
3211 continue;
3212
3213 case 'C':
3214 insn.insn_opcode |= va_arg (args, unsigned long);
3215 continue;
3216
3217 default:
3218 internalError ();
3219 }
3220 break;
3221 }
3222 va_end (args);
3223 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3224
3225 append_insn (&insn, ep, r);
3226 }
3227
3228 static void
3229 mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
3230 va_list args)
3231 {
3232 struct mips_cl_insn insn;
3233 bfd_reloc_code_real_type r[3]
3234 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3235
3236 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3237 assert (insn.insn_mo);
3238 assert (strcmp (name, insn.insn_mo->name) == 0);
3239
3240 while (strcmp (fmt, insn.insn_mo->args) != 0
3241 || insn.insn_mo->pinfo == INSN_MACRO)
3242 {
3243 ++insn.insn_mo;
3244 assert (insn.insn_mo->name);
3245 assert (strcmp (name, insn.insn_mo->name) == 0);
3246 }
3247
3248 insn.insn_opcode = insn.insn_mo->match;
3249 insn.use_extend = FALSE;
3250
3251 for (;;)
3252 {
3253 int c;
3254
3255 c = *fmt++;
3256 switch (c)
3257 {
3258 case '\0':
3259 break;
3260
3261 case ',':
3262 case '(':
3263 case ')':
3264 continue;
3265
3266 case 'y':
3267 case 'w':
3268 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3269 continue;
3270
3271 case 'x':
3272 case 'v':
3273 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3274 continue;
3275
3276 case 'z':
3277 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3278 continue;
3279
3280 case 'Z':
3281 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3282 continue;
3283
3284 case '0':
3285 case 'S':
3286 case 'P':
3287 case 'R':
3288 continue;
3289
3290 case 'X':
3291 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3292 continue;
3293
3294 case 'Y':
3295 {
3296 int regno;
3297
3298 regno = va_arg (args, int);
3299 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3300 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3301 }
3302 continue;
3303
3304 case '<':
3305 case '>':
3306 case '4':
3307 case '5':
3308 case 'H':
3309 case 'W':
3310 case 'D':
3311 case 'j':
3312 case '8':
3313 case 'V':
3314 case 'C':
3315 case 'U':
3316 case 'k':
3317 case 'K':
3318 case 'p':
3319 case 'q':
3320 {
3321 assert (ep != NULL);
3322
3323 if (ep->X_op != O_constant)
3324 *r = (int) BFD_RELOC_UNUSED + c;
3325 else
3326 {
3327 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3328 FALSE, &insn.insn_opcode, &insn.use_extend,
3329 &insn.extend);
3330 ep = NULL;
3331 *r = BFD_RELOC_UNUSED;
3332 }
3333 }
3334 continue;
3335
3336 case '6':
3337 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3338 continue;
3339 }
3340
3341 break;
3342 }
3343
3344 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3345
3346 append_insn (&insn, ep, r);
3347 }
3348
3349 /*
3350 * Generate a "jalr" instruction with a relocation hint to the called
3351 * function. This occurs in NewABI PIC code.
3352 */
3353 static void
3354 macro_build_jalr (expressionS *ep)
3355 {
3356 char *f = NULL;
3357
3358 if (HAVE_NEWABI)
3359 {
3360 frag_grow (8);
3361 f = frag_more (0);
3362 }
3363 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
3364 if (HAVE_NEWABI)
3365 fix_new_exp (frag_now, f - frag_now->fr_literal,
3366 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3367 }
3368
3369 /*
3370 * Generate a "lui" instruction.
3371 */
3372 static void
3373 macro_build_lui (expressionS *ep, int regnum)
3374 {
3375 expressionS high_expr;
3376 struct mips_cl_insn insn;
3377 bfd_reloc_code_real_type r[3]
3378 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3379 const char *name = "lui";
3380 const char *fmt = "t,u";
3381
3382 assert (! mips_opts.mips16);
3383
3384 high_expr = *ep;
3385
3386 if (high_expr.X_op == O_constant)
3387 {
3388 /* we can compute the instruction now without a relocation entry */
3389 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3390 >> 16) & 0xffff;
3391 *r = BFD_RELOC_UNUSED;
3392 }
3393 else
3394 {
3395 assert (ep->X_op == O_symbol);
3396 /* _gp_disp is a special case, used from s_cpload.
3397 __gnu_local_gp is used if mips_no_shared. */
3398 assert (mips_pic == NO_PIC
3399 || (! HAVE_NEWABI
3400 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3401 || (! mips_in_shared
3402 && strcmp (S_GET_NAME (ep->X_add_symbol),
3403 "__gnu_local_gp") == 0));
3404 *r = BFD_RELOC_HI16_S;
3405 }
3406
3407 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3408 assert (insn.insn_mo);
3409 assert (strcmp (name, insn.insn_mo->name) == 0);
3410 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3411
3412 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3413 if (*r == BFD_RELOC_UNUSED)
3414 {
3415 insn.insn_opcode |= high_expr.X_add_number;
3416 append_insn (&insn, NULL, r);
3417 }
3418 else
3419 append_insn (&insn, &high_expr, r);
3420 }
3421
3422 /* Generate a sequence of instructions to do a load or store from a constant
3423 offset off of a base register (breg) into/from a target register (treg),
3424 using AT if necessary. */
3425 static void
3426 macro_build_ldst_constoffset (expressionS *ep, const char *op,
3427 int treg, int breg, int dbl)
3428 {
3429 assert (ep->X_op == O_constant);
3430
3431 /* Sign-extending 32-bit constants makes their handling easier. */
3432 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3433 == ~((bfd_vma) 0x7fffffff)))
3434 {
3435 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3436 as_bad (_("constant too large"));
3437
3438 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3439 - 0x80000000);
3440 }
3441
3442 /* Right now, this routine can only handle signed 32-bit constants. */
3443 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
3444 as_warn (_("operand overflow"));
3445
3446 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3447 {
3448 /* Signed 16-bit offset will fit in the op. Easy! */
3449 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
3450 }
3451 else
3452 {
3453 /* 32-bit offset, need multiple instructions and AT, like:
3454 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3455 addu $tempreg,$tempreg,$breg
3456 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3457 to handle the complete offset. */
3458 macro_build_lui (ep, AT);
3459 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3460 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
3461
3462 if (mips_opts.noat)
3463 as_bad (_("Macro used $at after \".set noat\""));
3464 }
3465 }
3466
3467 /* set_at()
3468 * Generates code to set the $at register to true (one)
3469 * if reg is less than the immediate expression.
3470 */
3471 static void
3472 set_at (int reg, int unsignedp)
3473 {
3474 if (imm_expr.X_op == O_constant
3475 && imm_expr.X_add_number >= -0x8000
3476 && imm_expr.X_add_number < 0x8000)
3477 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3478 AT, reg, BFD_RELOC_LO16);
3479 else
3480 {
3481 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3482 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
3483 }
3484 }
3485
3486 static void
3487 normalize_constant_expr (expressionS *ex)
3488 {
3489 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3490 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3491 - 0x80000000);
3492 }
3493
3494 /* Warn if an expression is not a constant. */
3495
3496 static void
3497 check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
3498 {
3499 if (ex->X_op == O_big)
3500 as_bad (_("unsupported large constant"));
3501 else if (ex->X_op != O_constant)
3502 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3503
3504 normalize_constant_expr (ex);
3505 }
3506
3507 /* Count the leading zeroes by performing a binary chop. This is a
3508 bulky bit of source, but performance is a LOT better for the
3509 majority of values than a simple loop to count the bits:
3510 for (lcnt = 0; (lcnt < 32); lcnt++)
3511 if ((v) & (1 << (31 - lcnt)))
3512 break;
3513 However it is not code size friendly, and the gain will drop a bit
3514 on certain cached systems.
3515 */
3516 #define COUNT_TOP_ZEROES(v) \
3517 (((v) & ~0xffff) == 0 \
3518 ? ((v) & ~0xff) == 0 \
3519 ? ((v) & ~0xf) == 0 \
3520 ? ((v) & ~0x3) == 0 \
3521 ? ((v) & ~0x1) == 0 \
3522 ? !(v) \
3523 ? 32 \
3524 : 31 \
3525 : 30 \
3526 : ((v) & ~0x7) == 0 \
3527 ? 29 \
3528 : 28 \
3529 : ((v) & ~0x3f) == 0 \
3530 ? ((v) & ~0x1f) == 0 \
3531 ? 27 \
3532 : 26 \
3533 : ((v) & ~0x7f) == 0 \
3534 ? 25 \
3535 : 24 \
3536 : ((v) & ~0xfff) == 0 \
3537 ? ((v) & ~0x3ff) == 0 \
3538 ? ((v) & ~0x1ff) == 0 \
3539 ? 23 \
3540 : 22 \
3541 : ((v) & ~0x7ff) == 0 \
3542 ? 21 \
3543 : 20 \
3544 : ((v) & ~0x3fff) == 0 \
3545 ? ((v) & ~0x1fff) == 0 \
3546 ? 19 \
3547 : 18 \
3548 : ((v) & ~0x7fff) == 0 \
3549 ? 17 \
3550 : 16 \
3551 : ((v) & ~0xffffff) == 0 \
3552 ? ((v) & ~0xfffff) == 0 \
3553 ? ((v) & ~0x3ffff) == 0 \
3554 ? ((v) & ~0x1ffff) == 0 \
3555 ? 15 \
3556 : 14 \
3557 : ((v) & ~0x7ffff) == 0 \
3558 ? 13 \
3559 : 12 \
3560 : ((v) & ~0x3fffff) == 0 \
3561 ? ((v) & ~0x1fffff) == 0 \
3562 ? 11 \
3563 : 10 \
3564 : ((v) & ~0x7fffff) == 0 \
3565 ? 9 \
3566 : 8 \
3567 : ((v) & ~0xfffffff) == 0 \
3568 ? ((v) & ~0x3ffffff) == 0 \
3569 ? ((v) & ~0x1ffffff) == 0 \
3570 ? 7 \
3571 : 6 \
3572 : ((v) & ~0x7ffffff) == 0 \
3573 ? 5 \
3574 : 4 \
3575 : ((v) & ~0x3fffffff) == 0 \
3576 ? ((v) & ~0x1fffffff) == 0 \
3577 ? 3 \
3578 : 2 \
3579 : ((v) & ~0x7fffffff) == 0 \
3580 ? 1 \
3581 : 0)
3582
3583 /* load_register()
3584 * This routine generates the least number of instructions necessary to load
3585 * an absolute expression value into a register.
3586 */
3587 static void
3588 load_register (int reg, expressionS *ep, int dbl)
3589 {
3590 int freg;
3591 expressionS hi32, lo32;
3592
3593 if (ep->X_op != O_big)
3594 {
3595 assert (ep->X_op == O_constant);
3596
3597 /* Sign-extending 32-bit constants makes their handling easier. */
3598 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3599 == ~((bfd_vma) 0x7fffffff)))
3600 {
3601 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
3602 as_bad (_("constant too large"));
3603
3604 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3605 - 0x80000000);
3606 }
3607
3608 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
3609 {
3610 /* We can handle 16 bit signed values with an addiu to
3611 $zero. No need to ever use daddiu here, since $zero and
3612 the result are always correct in 32 bit mode. */
3613 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3614 return;
3615 }
3616 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3617 {
3618 /* We can handle 16 bit unsigned values with an ori to
3619 $zero. */
3620 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3621 return;
3622 }
3623 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
3624 {
3625 /* 32 bit values require an lui. */
3626 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
3627 if ((ep->X_add_number & 0xffff) != 0)
3628 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3629 return;
3630 }
3631 }
3632
3633 /* The value is larger than 32 bits. */
3634
3635 if (HAVE_32BIT_GPRS)
3636 {
3637 as_bad (_("Number (0x%lx) larger than 32 bits"),
3638 (unsigned long) ep->X_add_number);
3639 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3640 return;
3641 }
3642
3643 if (ep->X_op != O_big)
3644 {
3645 hi32 = *ep;
3646 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3647 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3648 hi32.X_add_number &= 0xffffffff;
3649 lo32 = *ep;
3650 lo32.X_add_number &= 0xffffffff;
3651 }
3652 else
3653 {
3654 assert (ep->X_add_number > 2);
3655 if (ep->X_add_number == 3)
3656 generic_bignum[3] = 0;
3657 else if (ep->X_add_number > 4)
3658 as_bad (_("Number larger than 64 bits"));
3659 lo32.X_op = O_constant;
3660 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3661 hi32.X_op = O_constant;
3662 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3663 }
3664
3665 if (hi32.X_add_number == 0)
3666 freg = 0;
3667 else
3668 {
3669 int shift, bit;
3670 unsigned long hi, lo;
3671
3672 if (hi32.X_add_number == (offsetT) 0xffffffff)
3673 {
3674 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3675 {
3676 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3677 return;
3678 }
3679 if (lo32.X_add_number & 0x80000000)
3680 {
3681 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3682 if (lo32.X_add_number & 0xffff)
3683 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
3684 return;
3685 }
3686 }
3687
3688 /* Check for 16bit shifted constant. We know that hi32 is
3689 non-zero, so start the mask on the first bit of the hi32
3690 value. */
3691 shift = 17;
3692 do
3693 {
3694 unsigned long himask, lomask;
3695
3696 if (shift < 32)
3697 {
3698 himask = 0xffff >> (32 - shift);
3699 lomask = (0xffff << shift) & 0xffffffff;
3700 }
3701 else
3702 {
3703 himask = 0xffff << (shift - 32);
3704 lomask = 0;
3705 }
3706 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3707 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3708 {
3709 expressionS tmp;
3710
3711 tmp.X_op = O_constant;
3712 if (shift < 32)
3713 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3714 | (lo32.X_add_number >> shift));
3715 else
3716 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3717 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3718 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3719 reg, reg, (shift >= 32) ? shift - 32 : shift);
3720 return;
3721 }
3722 ++shift;
3723 }
3724 while (shift <= (64 - 16));
3725
3726 /* Find the bit number of the lowest one bit, and store the
3727 shifted value in hi/lo. */
3728 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3729 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3730 if (lo != 0)
3731 {
3732 bit = 0;
3733 while ((lo & 1) == 0)
3734 {
3735 lo >>= 1;
3736 ++bit;
3737 }
3738 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3739 hi >>= bit;
3740 }
3741 else
3742 {
3743 bit = 32;
3744 while ((hi & 1) == 0)
3745 {
3746 hi >>= 1;
3747 ++bit;
3748 }
3749 lo = hi;
3750 hi = 0;
3751 }
3752
3753 /* Optimize if the shifted value is a (power of 2) - 1. */
3754 if ((hi == 0 && ((lo + 1) & lo) == 0)
3755 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3756 {
3757 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3758 if (shift != 0)
3759 {
3760 expressionS tmp;
3761
3762 /* This instruction will set the register to be all
3763 ones. */
3764 tmp.X_op = O_constant;
3765 tmp.X_add_number = (offsetT) -1;
3766 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
3767 if (bit != 0)
3768 {
3769 bit += shift;
3770 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3771 reg, reg, (bit >= 32) ? bit - 32 : bit);
3772 }
3773 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3774 reg, reg, (shift >= 32) ? shift - 32 : shift);
3775 return;
3776 }
3777 }
3778
3779 /* Sign extend hi32 before calling load_register, because we can
3780 generally get better code when we load a sign extended value. */
3781 if ((hi32.X_add_number & 0x80000000) != 0)
3782 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3783 load_register (reg, &hi32, 0);
3784 freg = reg;
3785 }
3786 if ((lo32.X_add_number & 0xffff0000) == 0)
3787 {
3788 if (freg != 0)
3789 {
3790 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
3791 freg = reg;
3792 }
3793 }
3794 else
3795 {
3796 expressionS mid16;
3797
3798 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3799 {
3800 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3801 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
3802 return;
3803 }
3804
3805 if (freg != 0)
3806 {
3807 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
3808 freg = reg;
3809 }
3810 mid16 = lo32;
3811 mid16.X_add_number >>= 16;
3812 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3813 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3814 freg = reg;
3815 }
3816 if ((lo32.X_add_number & 0xffff) != 0)
3817 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3818 }
3819
3820 static inline void
3821 load_delay_nop (void)
3822 {
3823 if (!gpr_interlocks)
3824 macro_build (NULL, "nop", "");
3825 }
3826
3827 /* Load an address into a register. */
3828
3829 static void
3830 load_address (int reg, expressionS *ep, int *used_at)
3831 {
3832 if (ep->X_op != O_constant
3833 && ep->X_op != O_symbol)
3834 {
3835 as_bad (_("expression too complex"));
3836 ep->X_op = O_constant;
3837 }
3838
3839 if (ep->X_op == O_constant)
3840 {
3841 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
3842 return;
3843 }
3844
3845 if (mips_pic == NO_PIC)
3846 {
3847 /* If this is a reference to a GP relative symbol, we want
3848 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3849 Otherwise we want
3850 lui $reg,<sym> (BFD_RELOC_HI16_S)
3851 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3852 If we have an addend, we always use the latter form.
3853
3854 With 64bit address space and a usable $at we want
3855 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3856 lui $at,<sym> (BFD_RELOC_HI16_S)
3857 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3858 daddiu $at,<sym> (BFD_RELOC_LO16)
3859 dsll32 $reg,0
3860 daddu $reg,$reg,$at
3861
3862 If $at is already in use, we use a path which is suboptimal
3863 on superscalar processors.
3864 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3865 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3866 dsll $reg,16
3867 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3868 dsll $reg,16
3869 daddiu $reg,<sym> (BFD_RELOC_LO16)
3870
3871 For GP relative symbols in 64bit address space we can use
3872 the same sequence as in 32bit address space. */
3873 if (HAVE_64BIT_SYMBOLS)
3874 {
3875 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3876 && !nopic_need_relax (ep->X_add_symbol, 1))
3877 {
3878 relax_start (ep->X_add_symbol);
3879 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3880 mips_gp_register, BFD_RELOC_GPREL16);
3881 relax_switch ();
3882 }
3883
3884 if (*used_at == 0 && !mips_opts.noat)
3885 {
3886 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3887 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3888 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3889 BFD_RELOC_MIPS_HIGHER);
3890 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3891 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3892 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
3893 *used_at = 1;
3894 }
3895 else
3896 {
3897 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3898 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3899 BFD_RELOC_MIPS_HIGHER);
3900 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3901 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3902 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3903 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
3904 }
3905
3906 if (mips_relax.sequence)
3907 relax_end ();
3908 }
3909 else
3910 {
3911 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3912 && !nopic_need_relax (ep->X_add_symbol, 1))
3913 {
3914 relax_start (ep->X_add_symbol);
3915 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3916 mips_gp_register, BFD_RELOC_GPREL16);
3917 relax_switch ();
3918 }
3919 macro_build_lui (ep, reg);
3920 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3921 reg, reg, BFD_RELOC_LO16);
3922 if (mips_relax.sequence)
3923 relax_end ();
3924 }
3925 }
3926 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3927 {
3928 expressionS ex;
3929
3930 /* If this is a reference to an external symbol, we want
3931 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3932 Otherwise we want
3933 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3934 nop
3935 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3936 If there is a constant, it must be added in after.
3937
3938 If we have NewABI, we want
3939 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3940 unless we're referencing a global symbol with a non-zero
3941 offset, in which case cst must be added separately. */
3942 if (HAVE_NEWABI)
3943 {
3944 if (ep->X_add_number)
3945 {
3946 ex.X_add_number = ep->X_add_number;
3947 ep->X_add_number = 0;
3948 relax_start (ep->X_add_symbol);
3949 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3950 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3951 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3952 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3953 ex.X_op = O_constant;
3954 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3955 reg, reg, BFD_RELOC_LO16);
3956 ep->X_add_number = ex.X_add_number;
3957 relax_switch ();
3958 }
3959 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3960 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3961 if (mips_relax.sequence)
3962 relax_end ();
3963 }
3964 else
3965 {
3966 ex.X_add_number = ep->X_add_number;
3967 ep->X_add_number = 0;
3968 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3969 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3970 load_delay_nop ();
3971 relax_start (ep->X_add_symbol);
3972 relax_switch ();
3973 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3974 BFD_RELOC_LO16);
3975 relax_end ();
3976
3977 if (ex.X_add_number != 0)
3978 {
3979 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3980 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3981 ex.X_op = O_constant;
3982 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
3983 reg, reg, BFD_RELOC_LO16);
3984 }
3985 }
3986 }
3987 else if (mips_pic == SVR4_PIC)
3988 {
3989 expressionS ex;
3990
3991 /* This is the large GOT case. If this is a reference to an
3992 external symbol, we want
3993 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3994 addu $reg,$reg,$gp
3995 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3996
3997 Otherwise, for a reference to a local symbol in old ABI, we want
3998 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3999 nop
4000 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4001 If there is a constant, it must be added in after.
4002
4003 In the NewABI, for local symbols, with or without offsets, we want:
4004 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4005 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4006 */
4007 if (HAVE_NEWABI)
4008 {
4009 ex.X_add_number = ep->X_add_number;
4010 ep->X_add_number = 0;
4011 relax_start (ep->X_add_symbol);
4012 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4013 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4014 reg, reg, mips_gp_register);
4015 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4016 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4017 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4018 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4019 else if (ex.X_add_number)
4020 {
4021 ex.X_op = O_constant;
4022 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4023 BFD_RELOC_LO16);
4024 }
4025
4026 ep->X_add_number = ex.X_add_number;
4027 relax_switch ();
4028 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4029 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4030 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4031 BFD_RELOC_MIPS_GOT_OFST);
4032 relax_end ();
4033 }
4034 else
4035 {
4036 ex.X_add_number = ep->X_add_number;
4037 ep->X_add_number = 0;
4038 relax_start (ep->X_add_symbol);
4039 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4040 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4041 reg, reg, mips_gp_register);
4042 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4043 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4044 relax_switch ();
4045 if (reg_needs_delay (mips_gp_register))
4046 {
4047 /* We need a nop before loading from $gp. This special
4048 check is required because the lui which starts the main
4049 instruction stream does not refer to $gp, and so will not
4050 insert the nop which may be required. */
4051 macro_build (NULL, "nop", "");
4052 }
4053 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4054 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4055 load_delay_nop ();
4056 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4057 BFD_RELOC_LO16);
4058 relax_end ();
4059
4060 if (ex.X_add_number != 0)
4061 {
4062 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4063 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4064 ex.X_op = O_constant;
4065 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4066 BFD_RELOC_LO16);
4067 }
4068 }
4069 }
4070 else
4071 abort ();
4072
4073 if (mips_opts.noat && *used_at == 1)
4074 as_bad (_("Macro used $at after \".set noat\""));
4075 }
4076
4077 /* Move the contents of register SOURCE into register DEST. */
4078
4079 static void
4080 move_register (int dest, int source)
4081 {
4082 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4083 dest, source, 0);
4084 }
4085
4086 /* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
4087 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4088 The two alternatives are:
4089
4090 Global symbol Local sybmol
4091 ------------- ------------
4092 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4093 ... ...
4094 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4095
4096 load_got_offset emits the first instruction and add_got_offset
4097 emits the second for a 16-bit offset or add_got_offset_hilo emits
4098 a sequence to add a 32-bit offset using a scratch register. */
4099
4100 static void
4101 load_got_offset (int dest, expressionS *local)
4102 {
4103 expressionS global;
4104
4105 global = *local;
4106 global.X_add_number = 0;
4107
4108 relax_start (local->X_add_symbol);
4109 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4110 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4111 relax_switch ();
4112 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4113 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4114 relax_end ();
4115 }
4116
4117 static void
4118 add_got_offset (int dest, expressionS *local)
4119 {
4120 expressionS global;
4121
4122 global.X_op = O_constant;
4123 global.X_op_symbol = NULL;
4124 global.X_add_symbol = NULL;
4125 global.X_add_number = local->X_add_number;
4126
4127 relax_start (local->X_add_symbol);
4128 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4129 dest, dest, BFD_RELOC_LO16);
4130 relax_switch ();
4131 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4132 relax_end ();
4133 }
4134
4135 static void
4136 add_got_offset_hilo (int dest, expressionS *local, int tmp)
4137 {
4138 expressionS global;
4139 int hold_mips_optimize;
4140
4141 global.X_op = O_constant;
4142 global.X_op_symbol = NULL;
4143 global.X_add_symbol = NULL;
4144 global.X_add_number = local->X_add_number;
4145
4146 relax_start (local->X_add_symbol);
4147 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4148 relax_switch ();
4149 /* Set mips_optimize around the lui instruction to avoid
4150 inserting an unnecessary nop after the lw. */
4151 hold_mips_optimize = mips_optimize;
4152 mips_optimize = 2;
4153 macro_build_lui (&global, tmp);
4154 mips_optimize = hold_mips_optimize;
4155 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4156 relax_end ();
4157
4158 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4159 }
4160
4161 /*
4162 * Build macros
4163 * This routine implements the seemingly endless macro or synthesized
4164 * instructions and addressing modes in the mips assembly language. Many
4165 * of these macros are simple and are similar to each other. These could
4166 * probably be handled by some kind of table or grammar approach instead of
4167 * this verbose method. Others are not simple macros but are more like
4168 * optimizing code generation.
4169 * One interesting optimization is when several store macros appear
4170 * consecutively that would load AT with the upper half of the same address.
4171 * The ensuing load upper instructions are ommited. This implies some kind
4172 * of global optimization. We currently only optimize within a single macro.
4173 * For many of the load and store macros if the address is specified as a
4174 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4175 * first load register 'at' with zero and use it as the base register. The
4176 * mips assembler simply uses register $zero. Just one tiny optimization
4177 * we're missing.
4178 */
4179 static void
4180 macro (struct mips_cl_insn *ip)
4181 {
4182 register int treg, sreg, dreg, breg;
4183 int tempreg;
4184 int mask;
4185 int used_at = 0;
4186 expressionS expr1;
4187 const char *s;
4188 const char *s2;
4189 const char *fmt;
4190 int likely = 0;
4191 int dbl = 0;
4192 int coproc = 0;
4193 int lr = 0;
4194 int imm = 0;
4195 int call = 0;
4196 int off;
4197 offsetT maxnum;
4198 bfd_reloc_code_real_type r;
4199 int hold_mips_optimize;
4200
4201 assert (! mips_opts.mips16);
4202
4203 treg = (ip->insn_opcode >> 16) & 0x1f;
4204 dreg = (ip->insn_opcode >> 11) & 0x1f;
4205 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4206 mask = ip->insn_mo->mask;
4207
4208 expr1.X_op = O_constant;
4209 expr1.X_op_symbol = NULL;
4210 expr1.X_add_symbol = NULL;
4211 expr1.X_add_number = 1;
4212
4213 switch (mask)
4214 {
4215 case M_DABS:
4216 dbl = 1;
4217 case M_ABS:
4218 /* bgez $a0,.+12
4219 move v0,$a0
4220 sub v0,$zero,$a0
4221 */
4222
4223 mips_emit_delays (TRUE);
4224 ++mips_opts.noreorder;
4225 mips_any_noreorder = 1;
4226
4227 expr1.X_add_number = 8;
4228 macro_build (&expr1, "bgez", "s,p", sreg);
4229 if (dreg == sreg)
4230 macro_build (NULL, "nop", "", 0);
4231 else
4232 move_register (dreg, sreg);
4233 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4234
4235 --mips_opts.noreorder;
4236 break;
4237
4238 case M_ADD_I:
4239 s = "addi";
4240 s2 = "add";
4241 goto do_addi;
4242 case M_ADDU_I:
4243 s = "addiu";
4244 s2 = "addu";
4245 goto do_addi;
4246 case M_DADD_I:
4247 dbl = 1;
4248 s = "daddi";
4249 s2 = "dadd";
4250 goto do_addi;
4251 case M_DADDU_I:
4252 dbl = 1;
4253 s = "daddiu";
4254 s2 = "daddu";
4255 do_addi:
4256 if (imm_expr.X_op == O_constant
4257 && imm_expr.X_add_number >= -0x8000
4258 && imm_expr.X_add_number < 0x8000)
4259 {
4260 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
4261 break;
4262 }
4263 used_at = 1;
4264 load_register (AT, &imm_expr, dbl);
4265 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4266 break;
4267
4268 case M_AND_I:
4269 s = "andi";
4270 s2 = "and";
4271 goto do_bit;
4272 case M_OR_I:
4273 s = "ori";
4274 s2 = "or";
4275 goto do_bit;
4276 case M_NOR_I:
4277 s = "";
4278 s2 = "nor";
4279 goto do_bit;
4280 case M_XOR_I:
4281 s = "xori";
4282 s2 = "xor";
4283 do_bit:
4284 if (imm_expr.X_op == O_constant
4285 && imm_expr.X_add_number >= 0
4286 && imm_expr.X_add_number < 0x10000)
4287 {
4288 if (mask != M_NOR_I)
4289 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
4290 else
4291 {
4292 macro_build (&imm_expr, "ori", "t,r,i",
4293 treg, sreg, BFD_RELOC_LO16);
4294 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
4295 }
4296 break;
4297 }
4298
4299 used_at = 1;
4300 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4301 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
4302 break;
4303
4304 case M_BEQ_I:
4305 s = "beq";
4306 goto beq_i;
4307 case M_BEQL_I:
4308 s = "beql";
4309 likely = 1;
4310 goto beq_i;
4311 case M_BNE_I:
4312 s = "bne";
4313 goto beq_i;
4314 case M_BNEL_I:
4315 s = "bnel";
4316 likely = 1;
4317 beq_i:
4318 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4319 {
4320 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
4321 break;
4322 }
4323 used_at = 1;
4324 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4325 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
4326 break;
4327
4328 case M_BGEL:
4329 likely = 1;
4330 case M_BGE:
4331 if (treg == 0)
4332 {
4333 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4334 break;
4335 }
4336 if (sreg == 0)
4337 {
4338 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
4339 break;
4340 }
4341 used_at = 1;
4342 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4343 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4344 break;
4345
4346 case M_BGTL_I:
4347 likely = 1;
4348 case M_BGT_I:
4349 /* check for > max integer */
4350 maxnum = 0x7fffffff;
4351 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4352 {
4353 maxnum <<= 16;
4354 maxnum |= 0xffff;
4355 maxnum <<= 16;
4356 maxnum |= 0xffff;
4357 }
4358 if (imm_expr.X_op == O_constant
4359 && imm_expr.X_add_number >= maxnum
4360 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4361 {
4362 do_false:
4363 /* result is always false */
4364 if (! likely)
4365 macro_build (NULL, "nop", "", 0);
4366 else
4367 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
4368 break;
4369 }
4370 if (imm_expr.X_op != O_constant)
4371 as_bad (_("Unsupported large constant"));
4372 ++imm_expr.X_add_number;
4373 /* FALLTHROUGH */
4374 case M_BGE_I:
4375 case M_BGEL_I:
4376 if (mask == M_BGEL_I)
4377 likely = 1;
4378 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4379 {
4380 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
4381 break;
4382 }
4383 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4384 {
4385 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4386 break;
4387 }
4388 maxnum = 0x7fffffff;
4389 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4390 {
4391 maxnum <<= 16;
4392 maxnum |= 0xffff;
4393 maxnum <<= 16;
4394 maxnum |= 0xffff;
4395 }
4396 maxnum = - maxnum - 1;
4397 if (imm_expr.X_op == O_constant
4398 && imm_expr.X_add_number <= maxnum
4399 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4400 {
4401 do_true:
4402 /* result is always true */
4403 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4404 macro_build (&offset_expr, "b", "p");
4405 break;
4406 }
4407 used_at = 1;
4408 set_at (sreg, 0);
4409 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4410 break;
4411
4412 case M_BGEUL:
4413 likely = 1;
4414 case M_BGEU:
4415 if (treg == 0)
4416 goto do_true;
4417 if (sreg == 0)
4418 {
4419 macro_build (&offset_expr, likely ? "beql" : "beq",
4420 "s,t,p", 0, treg);
4421 break;
4422 }
4423 used_at = 1;
4424 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4425 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4426 break;
4427
4428 case M_BGTUL_I:
4429 likely = 1;
4430 case M_BGTU_I:
4431 if (sreg == 0
4432 || (HAVE_32BIT_GPRS
4433 && imm_expr.X_op == O_constant
4434 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4435 goto do_false;
4436 if (imm_expr.X_op != O_constant)
4437 as_bad (_("Unsupported large constant"));
4438 ++imm_expr.X_add_number;
4439 /* FALLTHROUGH */
4440 case M_BGEU_I:
4441 case M_BGEUL_I:
4442 if (mask == M_BGEUL_I)
4443 likely = 1;
4444 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4445 goto do_true;
4446 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4447 {
4448 macro_build (&offset_expr, likely ? "bnel" : "bne",
4449 "s,t,p", sreg, 0);
4450 break;
4451 }
4452 used_at = 1;
4453 set_at (sreg, 1);
4454 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4455 break;
4456
4457 case M_BGTL:
4458 likely = 1;
4459 case M_BGT:
4460 if (treg == 0)
4461 {
4462 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
4463 break;
4464 }
4465 if (sreg == 0)
4466 {
4467 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
4468 break;
4469 }
4470 used_at = 1;
4471 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4472 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4473 break;
4474
4475 case M_BGTUL:
4476 likely = 1;
4477 case M_BGTU:
4478 if (treg == 0)
4479 {
4480 macro_build (&offset_expr, likely ? "bnel" : "bne",
4481 "s,t,p", sreg, 0);
4482 break;
4483 }
4484 if (sreg == 0)
4485 goto do_false;
4486 used_at = 1;
4487 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4488 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4489 break;
4490
4491 case M_BLEL:
4492 likely = 1;
4493 case M_BLE:
4494 if (treg == 0)
4495 {
4496 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4497 break;
4498 }
4499 if (sreg == 0)
4500 {
4501 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
4502 break;
4503 }
4504 used_at = 1;
4505 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4506 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4507 break;
4508
4509 case M_BLEL_I:
4510 likely = 1;
4511 case M_BLE_I:
4512 maxnum = 0x7fffffff;
4513 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4514 {
4515 maxnum <<= 16;
4516 maxnum |= 0xffff;
4517 maxnum <<= 16;
4518 maxnum |= 0xffff;
4519 }
4520 if (imm_expr.X_op == O_constant
4521 && imm_expr.X_add_number >= maxnum
4522 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4523 goto do_true;
4524 if (imm_expr.X_op != O_constant)
4525 as_bad (_("Unsupported large constant"));
4526 ++imm_expr.X_add_number;
4527 /* FALLTHROUGH */
4528 case M_BLT_I:
4529 case M_BLTL_I:
4530 if (mask == M_BLTL_I)
4531 likely = 1;
4532 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4533 {
4534 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4535 break;
4536 }
4537 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4538 {
4539 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
4540 break;
4541 }
4542 used_at = 1;
4543 set_at (sreg, 0);
4544 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4545 break;
4546
4547 case M_BLEUL:
4548 likely = 1;
4549 case M_BLEU:
4550 if (treg == 0)
4551 {
4552 macro_build (&offset_expr, likely ? "beql" : "beq",
4553 "s,t,p", sreg, 0);
4554 break;
4555 }
4556 if (sreg == 0)
4557 goto do_true;
4558 used_at = 1;
4559 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4560 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
4561 break;
4562
4563 case M_BLEUL_I:
4564 likely = 1;
4565 case M_BLEU_I:
4566 if (sreg == 0
4567 || (HAVE_32BIT_GPRS
4568 && imm_expr.X_op == O_constant
4569 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4570 goto do_true;
4571 if (imm_expr.X_op != O_constant)
4572 as_bad (_("Unsupported large constant"));
4573 ++imm_expr.X_add_number;
4574 /* FALLTHROUGH */
4575 case M_BLTU_I:
4576 case M_BLTUL_I:
4577 if (mask == M_BLTUL_I)
4578 likely = 1;
4579 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4580 goto do_false;
4581 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4582 {
4583 macro_build (&offset_expr, likely ? "beql" : "beq",
4584 "s,t,p", sreg, 0);
4585 break;
4586 }
4587 used_at = 1;
4588 set_at (sreg, 1);
4589 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4590 break;
4591
4592 case M_BLTL:
4593 likely = 1;
4594 case M_BLT:
4595 if (treg == 0)
4596 {
4597 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
4598 break;
4599 }
4600 if (sreg == 0)
4601 {
4602 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
4603 break;
4604 }
4605 used_at = 1;
4606 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4607 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4608 break;
4609
4610 case M_BLTUL:
4611 likely = 1;
4612 case M_BLTU:
4613 if (treg == 0)
4614 goto do_false;
4615 if (sreg == 0)
4616 {
4617 macro_build (&offset_expr, likely ? "bnel" : "bne",
4618 "s,t,p", 0, treg);
4619 break;
4620 }
4621 used_at = 1;
4622 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4623 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
4624 break;
4625
4626 case M_DEXT:
4627 {
4628 unsigned long pos;
4629 unsigned long size;
4630
4631 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4632 {
4633 as_bad (_("Unsupported large constant"));
4634 pos = size = 1;
4635 }
4636 else
4637 {
4638 pos = (unsigned long) imm_expr.X_add_number;
4639 size = (unsigned long) imm2_expr.X_add_number;
4640 }
4641
4642 if (pos > 63)
4643 {
4644 as_bad (_("Improper position (%lu)"), pos);
4645 pos = 1;
4646 }
4647 if (size == 0 || size > 64
4648 || (pos + size - 1) > 63)
4649 {
4650 as_bad (_("Improper extract size (%lu, position %lu)"),
4651 size, pos);
4652 size = 1;
4653 }
4654
4655 if (size <= 32 && pos < 32)
4656 {
4657 s = "dext";
4658 fmt = "t,r,+A,+C";
4659 }
4660 else if (size <= 32)
4661 {
4662 s = "dextu";
4663 fmt = "t,r,+E,+H";
4664 }
4665 else
4666 {
4667 s = "dextm";
4668 fmt = "t,r,+A,+G";
4669 }
4670 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
4671 }
4672 break;
4673
4674 case M_DINS:
4675 {
4676 unsigned long pos;
4677 unsigned long size;
4678
4679 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4680 {
4681 as_bad (_("Unsupported large constant"));
4682 pos = size = 1;
4683 }
4684 else
4685 {
4686 pos = (unsigned long) imm_expr.X_add_number;
4687 size = (unsigned long) imm2_expr.X_add_number;
4688 }
4689
4690 if (pos > 63)
4691 {
4692 as_bad (_("Improper position (%lu)"), pos);
4693 pos = 1;
4694 }
4695 if (size == 0 || size > 64
4696 || (pos + size - 1) > 63)
4697 {
4698 as_bad (_("Improper insert size (%lu, position %lu)"),
4699 size, pos);
4700 size = 1;
4701 }
4702
4703 if (pos < 32 && (pos + size - 1) < 32)
4704 {
4705 s = "dins";
4706 fmt = "t,r,+A,+B";
4707 }
4708 else if (pos >= 32)
4709 {
4710 s = "dinsu";
4711 fmt = "t,r,+E,+F";
4712 }
4713 else
4714 {
4715 s = "dinsm";
4716 fmt = "t,r,+A,+F";
4717 }
4718 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4719 pos + size - 1);
4720 }
4721 break;
4722
4723 case M_DDIV_3:
4724 dbl = 1;
4725 case M_DIV_3:
4726 s = "mflo";
4727 goto do_div3;
4728 case M_DREM_3:
4729 dbl = 1;
4730 case M_REM_3:
4731 s = "mfhi";
4732 do_div3:
4733 if (treg == 0)
4734 {
4735 as_warn (_("Divide by zero."));
4736 if (mips_trap)
4737 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4738 else
4739 macro_build (NULL, "break", "c", 7);
4740 break;
4741 }
4742
4743 mips_emit_delays (TRUE);
4744 ++mips_opts.noreorder;
4745 mips_any_noreorder = 1;
4746 if (mips_trap)
4747 {
4748 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4749 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4750 }
4751 else
4752 {
4753 expr1.X_add_number = 8;
4754 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4755 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4756 macro_build (NULL, "break", "c", 7);
4757 }
4758 expr1.X_add_number = -1;
4759 used_at = 1;
4760 load_register (AT, &expr1, dbl);
4761 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4762 macro_build (&expr1, "bne", "s,t,p", treg, AT);
4763 if (dbl)
4764 {
4765 expr1.X_add_number = 1;
4766 load_register (AT, &expr1, dbl);
4767 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
4768 }
4769 else
4770 {
4771 expr1.X_add_number = 0x80000000;
4772 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
4773 }
4774 if (mips_trap)
4775 {
4776 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
4777 /* We want to close the noreorder block as soon as possible, so
4778 that later insns are available for delay slot filling. */
4779 --mips_opts.noreorder;
4780 }
4781 else
4782 {
4783 expr1.X_add_number = 8;
4784 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4785 macro_build (NULL, "nop", "", 0);
4786
4787 /* We want to close the noreorder block as soon as possible, so
4788 that later insns are available for delay slot filling. */
4789 --mips_opts.noreorder;
4790
4791 macro_build (NULL, "break", "c", 6);
4792 }
4793 macro_build (NULL, s, "d", dreg);
4794 break;
4795
4796 case M_DIV_3I:
4797 s = "div";
4798 s2 = "mflo";
4799 goto do_divi;
4800 case M_DIVU_3I:
4801 s = "divu";
4802 s2 = "mflo";
4803 goto do_divi;
4804 case M_REM_3I:
4805 s = "div";
4806 s2 = "mfhi";
4807 goto do_divi;
4808 case M_REMU_3I:
4809 s = "divu";
4810 s2 = "mfhi";
4811 goto do_divi;
4812 case M_DDIV_3I:
4813 dbl = 1;
4814 s = "ddiv";
4815 s2 = "mflo";
4816 goto do_divi;
4817 case M_DDIVU_3I:
4818 dbl = 1;
4819 s = "ddivu";
4820 s2 = "mflo";
4821 goto do_divi;
4822 case M_DREM_3I:
4823 dbl = 1;
4824 s = "ddiv";
4825 s2 = "mfhi";
4826 goto do_divi;
4827 case M_DREMU_3I:
4828 dbl = 1;
4829 s = "ddivu";
4830 s2 = "mfhi";
4831 do_divi:
4832 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4833 {
4834 as_warn (_("Divide by zero."));
4835 if (mips_trap)
4836 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
4837 else
4838 macro_build (NULL, "break", "c", 7);
4839 break;
4840 }
4841 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4842 {
4843 if (strcmp (s2, "mflo") == 0)
4844 move_register (dreg, sreg);
4845 else
4846 move_register (dreg, 0);
4847 break;
4848 }
4849 if (imm_expr.X_op == O_constant
4850 && imm_expr.X_add_number == -1
4851 && s[strlen (s) - 1] != 'u')
4852 {
4853 if (strcmp (s2, "mflo") == 0)
4854 {
4855 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4856 }
4857 else
4858 move_register (dreg, 0);
4859 break;
4860 }
4861
4862 used_at = 1;
4863 load_register (AT, &imm_expr, dbl);
4864 macro_build (NULL, s, "z,s,t", sreg, AT);
4865 macro_build (NULL, s2, "d", dreg);
4866 break;
4867
4868 case M_DIVU_3:
4869 s = "divu";
4870 s2 = "mflo";
4871 goto do_divu3;
4872 case M_REMU_3:
4873 s = "divu";
4874 s2 = "mfhi";
4875 goto do_divu3;
4876 case M_DDIVU_3:
4877 s = "ddivu";
4878 s2 = "mflo";
4879 goto do_divu3;
4880 case M_DREMU_3:
4881 s = "ddivu";
4882 s2 = "mfhi";
4883 do_divu3:
4884 mips_emit_delays (TRUE);
4885 ++mips_opts.noreorder;
4886 mips_any_noreorder = 1;
4887 if (mips_trap)
4888 {
4889 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4890 macro_build (NULL, s, "z,s,t", sreg, treg);
4891 /* We want to close the noreorder block as soon as possible, so
4892 that later insns are available for delay slot filling. */
4893 --mips_opts.noreorder;
4894 }
4895 else
4896 {
4897 expr1.X_add_number = 8;
4898 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4899 macro_build (NULL, s, "z,s,t", sreg, treg);
4900
4901 /* We want to close the noreorder block as soon as possible, so
4902 that later insns are available for delay slot filling. */
4903 --mips_opts.noreorder;
4904 macro_build (NULL, "break", "c", 7);
4905 }
4906 macro_build (NULL, s2, "d", dreg);
4907 break;
4908
4909 case M_DLCA_AB:
4910 dbl = 1;
4911 case M_LCA_AB:
4912 call = 1;
4913 goto do_la;
4914 case M_DLA_AB:
4915 dbl = 1;
4916 case M_LA_AB:
4917 do_la:
4918 /* Load the address of a symbol into a register. If breg is not
4919 zero, we then add a base register to it. */
4920
4921 if (dbl && HAVE_32BIT_GPRS)
4922 as_warn (_("dla used to load 32-bit register"));
4923
4924 if (! dbl && HAVE_64BIT_OBJECTS)
4925 as_warn (_("la used to load 64-bit address"));
4926
4927 if (offset_expr.X_op == O_constant
4928 && offset_expr.X_add_number >= -0x8000
4929 && offset_expr.X_add_number < 0x8000)
4930 {
4931 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
4932 "t,r,j", treg, sreg, BFD_RELOC_LO16);
4933 break;
4934 }
4935
4936 if (!mips_opts.noat && (treg == breg))
4937 {
4938 tempreg = AT;
4939 used_at = 1;
4940 }
4941 else
4942 {
4943 tempreg = treg;
4944 }
4945
4946 if (offset_expr.X_op != O_symbol
4947 && offset_expr.X_op != O_constant)
4948 {
4949 as_bad (_("expression too complex"));
4950 offset_expr.X_op = O_constant;
4951 }
4952
4953 if (offset_expr.X_op == O_constant)
4954 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
4955 else if (mips_pic == NO_PIC)
4956 {
4957 /* If this is a reference to a GP relative symbol, we want
4958 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4959 Otherwise we want
4960 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4961 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4962 If we have a constant, we need two instructions anyhow,
4963 so we may as well always use the latter form.
4964
4965 With 64bit address space and a usable $at we want
4966 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4967 lui $at,<sym> (BFD_RELOC_HI16_S)
4968 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4969 daddiu $at,<sym> (BFD_RELOC_LO16)
4970 dsll32 $tempreg,0
4971 daddu $tempreg,$tempreg,$at
4972
4973 If $at is already in use, we use a path which is suboptimal
4974 on superscalar processors.
4975 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4976 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4977 dsll $tempreg,16
4978 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4979 dsll $tempreg,16
4980 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4981
4982 For GP relative symbols in 64bit address space we can use
4983 the same sequence as in 32bit address space. */
4984 if (HAVE_64BIT_SYMBOLS)
4985 {
4986 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4987 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4988 {
4989 relax_start (offset_expr.X_add_symbol);
4990 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4991 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4992 relax_switch ();
4993 }
4994
4995 if (used_at == 0 && !mips_opts.noat)
4996 {
4997 macro_build (&offset_expr, "lui", "t,u",
4998 tempreg, BFD_RELOC_MIPS_HIGHEST);
4999 macro_build (&offset_expr, "lui", "t,u",
5000 AT, BFD_RELOC_HI16_S);
5001 macro_build (&offset_expr, "daddiu", "t,r,j",
5002 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5003 macro_build (&offset_expr, "daddiu", "t,r,j",
5004 AT, AT, BFD_RELOC_LO16);
5005 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5006 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5007 used_at = 1;
5008 }
5009 else
5010 {
5011 macro_build (&offset_expr, "lui", "t,u",
5012 tempreg, BFD_RELOC_MIPS_HIGHEST);
5013 macro_build (&offset_expr, "daddiu", "t,r,j",
5014 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
5015 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5016 macro_build (&offset_expr, "daddiu", "t,r,j",
5017 tempreg, tempreg, BFD_RELOC_HI16_S);
5018 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5019 macro_build (&offset_expr, "daddiu", "t,r,j",
5020 tempreg, tempreg, BFD_RELOC_LO16);
5021 }
5022
5023 if (mips_relax.sequence)
5024 relax_end ();
5025 }
5026 else
5027 {
5028 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5029 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5030 {
5031 relax_start (offset_expr.X_add_symbol);
5032 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5033 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5034 relax_switch ();
5035 }
5036 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5037 as_bad (_("offset too large"));
5038 macro_build_lui (&offset_expr, tempreg);
5039 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5040 tempreg, tempreg, BFD_RELOC_LO16);
5041 if (mips_relax.sequence)
5042 relax_end ();
5043 }
5044 }
5045 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5046 {
5047 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5048
5049 /* If this is a reference to an external symbol, and there
5050 is no constant, we want
5051 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5052 or for lca or if tempreg is PIC_CALL_REG
5053 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5054 For a local symbol, we want
5055 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5056 nop
5057 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5058
5059 If we have a small constant, and this is a reference to
5060 an external symbol, we want
5061 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5062 nop
5063 addiu $tempreg,$tempreg,<constant>
5064 For a local symbol, we want the same instruction
5065 sequence, but we output a BFD_RELOC_LO16 reloc on the
5066 addiu instruction.
5067
5068 If we have a large constant, and this is a reference to
5069 an external symbol, we want
5070 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5071 lui $at,<hiconstant>
5072 addiu $at,$at,<loconstant>
5073 addu $tempreg,$tempreg,$at
5074 For a local symbol, we want the same instruction
5075 sequence, but we output a BFD_RELOC_LO16 reloc on the
5076 addiu instruction.
5077 */
5078
5079 if (offset_expr.X_add_number == 0)
5080 {
5081 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5082 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5083
5084 relax_start (offset_expr.X_add_symbol);
5085 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5086 lw_reloc_type, mips_gp_register);
5087 if (breg != 0)
5088 {
5089 /* We're going to put in an addu instruction using
5090 tempreg, so we may as well insert the nop right
5091 now. */
5092 load_delay_nop ();
5093 }
5094 relax_switch ();
5095 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5096 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5097 load_delay_nop ();
5098 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5099 tempreg, tempreg, BFD_RELOC_LO16);
5100 relax_end ();
5101 /* FIXME: If breg == 0, and the next instruction uses
5102 $tempreg, then if this variant case is used an extra
5103 nop will be generated. */
5104 }
5105 else if (offset_expr.X_add_number >= -0x8000
5106 && offset_expr.X_add_number < 0x8000)
5107 {
5108 load_got_offset (tempreg, &offset_expr);
5109 load_delay_nop ();
5110 add_got_offset (tempreg, &offset_expr);
5111 }
5112 else
5113 {
5114 expr1.X_add_number = offset_expr.X_add_number;
5115 offset_expr.X_add_number =
5116 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
5117 load_got_offset (tempreg, &offset_expr);
5118 offset_expr.X_add_number = expr1.X_add_number;
5119 /* If we are going to add in a base register, and the
5120 target register and the base register are the same,
5121 then we are using AT as a temporary register. Since
5122 we want to load the constant into AT, we add our
5123 current AT (from the global offset table) and the
5124 register into the register now, and pretend we were
5125 not using a base register. */
5126 if (breg == treg)
5127 {
5128 load_delay_nop ();
5129 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5130 treg, AT, breg);
5131 breg = 0;
5132 tempreg = treg;
5133 }
5134 add_got_offset_hilo (tempreg, &offset_expr, AT);
5135 used_at = 1;
5136 }
5137 }
5138 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5139 {
5140 int add_breg_early = 0;
5141
5142 /* If this is a reference to an external, and there is no
5143 constant, or local symbol (*), with or without a
5144 constant, we want
5145 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5146 or for lca or if tempreg is PIC_CALL_REG
5147 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5148
5149 If we have a small constant, and this is a reference to
5150 an external symbol, we want
5151 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5152 addiu $tempreg,$tempreg,<constant>
5153
5154 If we have a large constant, and this is a reference to
5155 an external symbol, we want
5156 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5157 lui $at,<hiconstant>
5158 addiu $at,$at,<loconstant>
5159 addu $tempreg,$tempreg,$at
5160
5161 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5162 local symbols, even though it introduces an additional
5163 instruction. */
5164
5165 if (offset_expr.X_add_number)
5166 {
5167 expr1.X_add_number = offset_expr.X_add_number;
5168 offset_expr.X_add_number = 0;
5169
5170 relax_start (offset_expr.X_add_symbol);
5171 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5172 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5173
5174 if (expr1.X_add_number >= -0x8000
5175 && expr1.X_add_number < 0x8000)
5176 {
5177 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5178 tempreg, tempreg, BFD_RELOC_LO16);
5179 }
5180 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5181 {
5182 int dreg;
5183
5184 /* If we are going to add in a base register, and the
5185 target register and the base register are the same,
5186 then we are using AT as a temporary register. Since
5187 we want to load the constant into AT, we add our
5188 current AT (from the global offset table) and the
5189 register into the register now, and pretend we were
5190 not using a base register. */
5191 if (breg != treg)
5192 dreg = tempreg;
5193 else
5194 {
5195 assert (tempreg == AT);
5196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5197 treg, AT, breg);
5198 dreg = treg;
5199 add_breg_early = 1;
5200 }
5201
5202 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5203 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5204 dreg, dreg, AT);
5205
5206 used_at = 1;
5207 }
5208 else
5209 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5210
5211 relax_switch ();
5212 offset_expr.X_add_number = expr1.X_add_number;
5213
5214 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5215 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5216 if (add_breg_early)
5217 {
5218 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5219 treg, tempreg, breg);
5220 breg = 0;
5221 tempreg = treg;
5222 }
5223 relax_end ();
5224 }
5225 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5226 {
5227 relax_start (offset_expr.X_add_symbol);
5228 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5229 BFD_RELOC_MIPS_CALL16, mips_gp_register);
5230 relax_switch ();
5231 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5232 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5233 relax_end ();
5234 }
5235 else
5236 {
5237 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5238 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5239 }
5240 }
5241 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5242 {
5243 int gpdelay;
5244 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5245 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5246 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5247
5248 /* This is the large GOT case. If this is a reference to an
5249 external symbol, and there is no constant, we want
5250 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5251 addu $tempreg,$tempreg,$gp
5252 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5253 or for lca or if tempreg is PIC_CALL_REG
5254 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5255 addu $tempreg,$tempreg,$gp
5256 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5257 For a local symbol, we want
5258 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5259 nop
5260 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5261
5262 If we have a small constant, and this is a reference to
5263 an external symbol, we want
5264 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5265 addu $tempreg,$tempreg,$gp
5266 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5267 nop
5268 addiu $tempreg,$tempreg,<constant>
5269 For a local symbol, we want
5270 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5271 nop
5272 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5273
5274 If we have a large constant, and this is a reference to
5275 an external symbol, 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 lui $at,<hiconstant>
5280 addiu $at,$at,<loconstant>
5281 addu $tempreg,$tempreg,$at
5282 For a local symbol, we want
5283 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5284 lui $at,<hiconstant>
5285 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5286 addu $tempreg,$tempreg,$at
5287 */
5288
5289 expr1.X_add_number = offset_expr.X_add_number;
5290 offset_expr.X_add_number = 0;
5291 relax_start (offset_expr.X_add_symbol);
5292 gpdelay = reg_needs_delay (mips_gp_register);
5293 if (expr1.X_add_number == 0 && breg == 0
5294 && (call || tempreg == PIC_CALL_REG))
5295 {
5296 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5297 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5298 }
5299 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5300 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5301 tempreg, tempreg, mips_gp_register);
5302 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5303 tempreg, lw_reloc_type, tempreg);
5304 if (expr1.X_add_number == 0)
5305 {
5306 if (breg != 0)
5307 {
5308 /* We're going to put in an addu instruction using
5309 tempreg, so we may as well insert the nop right
5310 now. */
5311 load_delay_nop ();
5312 }
5313 }
5314 else if (expr1.X_add_number >= -0x8000
5315 && expr1.X_add_number < 0x8000)
5316 {
5317 load_delay_nop ();
5318 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5319 tempreg, tempreg, BFD_RELOC_LO16);
5320 }
5321 else
5322 {
5323 int dreg;
5324
5325 /* If we are going to add in a base register, and the
5326 target register and the base register are the same,
5327 then we are using AT as a temporary register. Since
5328 we want to load the constant into AT, we add our
5329 current AT (from the global offset table) and the
5330 register into the register now, and pretend we were
5331 not using a base register. */
5332 if (breg != treg)
5333 dreg = tempreg;
5334 else
5335 {
5336 assert (tempreg == AT);
5337 load_delay_nop ();
5338 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5339 treg, AT, breg);
5340 dreg = treg;
5341 }
5342
5343 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5344 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5345
5346 used_at = 1;
5347 }
5348 offset_expr.X_add_number =
5349 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5350 relax_switch ();
5351
5352 if (gpdelay)
5353 {
5354 /* This is needed because this instruction uses $gp, but
5355 the first instruction on the main stream does not. */
5356 macro_build (NULL, "nop", "");
5357 }
5358
5359 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5360 local_reloc_type, mips_gp_register);
5361 if (expr1.X_add_number >= -0x8000
5362 && expr1.X_add_number < 0x8000)
5363 {
5364 load_delay_nop ();
5365 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5366 tempreg, tempreg, BFD_RELOC_LO16);
5367 /* FIXME: If add_number is 0, and there was no base
5368 register, the external symbol case ended with a load,
5369 so if the symbol turns out to not be external, and
5370 the next instruction uses tempreg, an unnecessary nop
5371 will be inserted. */
5372 }
5373 else
5374 {
5375 if (breg == treg)
5376 {
5377 /* We must add in the base register now, as in the
5378 external symbol case. */
5379 assert (tempreg == AT);
5380 load_delay_nop ();
5381 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5382 treg, AT, breg);
5383 tempreg = treg;
5384 /* We set breg to 0 because we have arranged to add
5385 it in in both cases. */
5386 breg = 0;
5387 }
5388
5389 macro_build_lui (&expr1, AT);
5390 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5391 AT, AT, BFD_RELOC_LO16);
5392 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5393 tempreg, tempreg, AT);
5394 used_at = 1;
5395 }
5396 relax_end ();
5397 }
5398 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5399 {
5400 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5401 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5402 int add_breg_early = 0;
5403
5404 /* This is the large GOT case. If this is a reference to an
5405 external symbol, and there is no constant, we want
5406 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5407 add $tempreg,$tempreg,$gp
5408 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5409 or for lca or if tempreg is PIC_CALL_REG
5410 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5411 add $tempreg,$tempreg,$gp
5412 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5413
5414 If we have a small constant, and this is a reference to
5415 an external symbol, we want
5416 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5417 add $tempreg,$tempreg,$gp
5418 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5419 addi $tempreg,$tempreg,<constant>
5420
5421 If we have a large constant, and this is a reference to
5422 an external symbol, we want
5423 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5424 addu $tempreg,$tempreg,$gp
5425 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5426 lui $at,<hiconstant>
5427 addi $at,$at,<loconstant>
5428 add $tempreg,$tempreg,$at
5429
5430 If we have NewABI, and we know it's a local symbol, we want
5431 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5432 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5433 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5434
5435 relax_start (offset_expr.X_add_symbol);
5436
5437 expr1.X_add_number = offset_expr.X_add_number;
5438 offset_expr.X_add_number = 0;
5439
5440 if (expr1.X_add_number == 0 && breg == 0
5441 && (call || tempreg == PIC_CALL_REG))
5442 {
5443 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5444 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5445 }
5446 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5447 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5448 tempreg, tempreg, mips_gp_register);
5449 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5450 tempreg, lw_reloc_type, tempreg);
5451
5452 if (expr1.X_add_number == 0)
5453 ;
5454 else if (expr1.X_add_number >= -0x8000
5455 && expr1.X_add_number < 0x8000)
5456 {
5457 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5458 tempreg, tempreg, BFD_RELOC_LO16);
5459 }
5460 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
5461 {
5462 int dreg;
5463
5464 /* If we are going to add in a base register, and the
5465 target register and the base register are the same,
5466 then we are using AT as a temporary register. Since
5467 we want to load the constant into AT, we add our
5468 current AT (from the global offset table) and the
5469 register into the register now, and pretend we were
5470 not using a base register. */
5471 if (breg != treg)
5472 dreg = tempreg;
5473 else
5474 {
5475 assert (tempreg == AT);
5476 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5477 treg, AT, breg);
5478 dreg = treg;
5479 add_breg_early = 1;
5480 }
5481
5482 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
5483 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
5484
5485 used_at = 1;
5486 }
5487 else
5488 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5489
5490 relax_switch ();
5491 offset_expr.X_add_number = expr1.X_add_number;
5492 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5493 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5494 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5495 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5496 if (add_breg_early)
5497 {
5498 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5499 treg, tempreg, breg);
5500 breg = 0;
5501 tempreg = treg;
5502 }
5503 relax_end ();
5504 }
5505 else
5506 abort ();
5507
5508 if (breg != 0)
5509 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
5510 break;
5511
5512 case M_J_A:
5513 /* The j instruction may not be used in PIC code, since it
5514 requires an absolute address. We convert it to a b
5515 instruction. */
5516 if (mips_pic == NO_PIC)
5517 macro_build (&offset_expr, "j", "a");
5518 else
5519 macro_build (&offset_expr, "b", "p");
5520 break;
5521
5522 /* The jal instructions must be handled as macros because when
5523 generating PIC code they expand to multi-instruction
5524 sequences. Normally they are simple instructions. */
5525 case M_JAL_1:
5526 dreg = RA;
5527 /* Fall through. */
5528 case M_JAL_2:
5529 if (mips_pic == NO_PIC)
5530 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5531 else if (mips_pic == SVR4_PIC)
5532 {
5533 if (sreg != PIC_CALL_REG)
5534 as_warn (_("MIPS PIC call to register other than $25"));
5535
5536 macro_build (NULL, "jalr", "d,s", dreg, sreg);
5537 if (! HAVE_NEWABI)
5538 {
5539 if (mips_cprestore_offset < 0)
5540 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5541 else
5542 {
5543 if (! mips_frame_reg_valid)
5544 {
5545 as_warn (_("No .frame pseudo-op used in PIC code"));
5546 /* Quiet this warning. */
5547 mips_frame_reg_valid = 1;
5548 }
5549 if (! mips_cprestore_valid)
5550 {
5551 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5552 /* Quiet this warning. */
5553 mips_cprestore_valid = 1;
5554 }
5555 expr1.X_add_number = mips_cprestore_offset;
5556 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5557 mips_gp_register,
5558 mips_frame_reg,
5559 HAVE_64BIT_ADDRESSES);
5560 }
5561 }
5562 }
5563 else
5564 abort ();
5565
5566 break;
5567
5568 case M_JAL_A:
5569 if (mips_pic == NO_PIC)
5570 macro_build (&offset_expr, "jal", "a");
5571 else if (mips_pic == SVR4_PIC)
5572 {
5573 /* If this is a reference to an external symbol, and we are
5574 using a small GOT, we want
5575 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5576 nop
5577 jalr $ra,$25
5578 nop
5579 lw $gp,cprestore($sp)
5580 The cprestore value is set using the .cprestore
5581 pseudo-op. If we are using a big GOT, we want
5582 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5583 addu $25,$25,$gp
5584 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5585 nop
5586 jalr $ra,$25
5587 nop
5588 lw $gp,cprestore($sp)
5589 If the symbol is not external, we want
5590 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5591 nop
5592 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5593 jalr $ra,$25
5594 nop
5595 lw $gp,cprestore($sp)
5596
5597 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5598 sequences above, minus nops, unless the symbol is local,
5599 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5600 GOT_DISP. */
5601 if (HAVE_NEWABI)
5602 {
5603 if (! mips_big_got)
5604 {
5605 relax_start (offset_expr.X_add_symbol);
5606 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5607 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5608 mips_gp_register);
5609 relax_switch ();
5610 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5611 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
5612 mips_gp_register);
5613 relax_end ();
5614 }
5615 else
5616 {
5617 relax_start (offset_expr.X_add_symbol);
5618 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5619 BFD_RELOC_MIPS_CALL_HI16);
5620 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5621 PIC_CALL_REG, mips_gp_register);
5622 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5623 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5624 PIC_CALL_REG);
5625 relax_switch ();
5626 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5627 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5628 mips_gp_register);
5629 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5630 PIC_CALL_REG, PIC_CALL_REG,
5631 BFD_RELOC_MIPS_GOT_OFST);
5632 relax_end ();
5633 }
5634
5635 macro_build_jalr (&offset_expr);
5636 }
5637 else
5638 {
5639 relax_start (offset_expr.X_add_symbol);
5640 if (! mips_big_got)
5641 {
5642 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5643 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5644 mips_gp_register);
5645 load_delay_nop ();
5646 relax_switch ();
5647 }
5648 else
5649 {
5650 int gpdelay;
5651
5652 gpdelay = reg_needs_delay (mips_gp_register);
5653 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5654 BFD_RELOC_MIPS_CALL_HI16);
5655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5656 PIC_CALL_REG, mips_gp_register);
5657 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5658 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5659 PIC_CALL_REG);
5660 load_delay_nop ();
5661 relax_switch ();
5662 if (gpdelay)
5663 macro_build (NULL, "nop", "");
5664 }
5665 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5666 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5667 mips_gp_register);
5668 load_delay_nop ();
5669 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5670 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
5671 relax_end ();
5672 macro_build_jalr (&offset_expr);
5673
5674 if (mips_cprestore_offset < 0)
5675 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5676 else
5677 {
5678 if (! mips_frame_reg_valid)
5679 {
5680 as_warn (_("No .frame pseudo-op used in PIC code"));
5681 /* Quiet this warning. */
5682 mips_frame_reg_valid = 1;
5683 }
5684 if (! mips_cprestore_valid)
5685 {
5686 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5687 /* Quiet this warning. */
5688 mips_cprestore_valid = 1;
5689 }
5690 if (mips_opts.noreorder)
5691 macro_build (NULL, "nop", "");
5692 expr1.X_add_number = mips_cprestore_offset;
5693 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
5694 mips_gp_register,
5695 mips_frame_reg,
5696 HAVE_64BIT_ADDRESSES);
5697 }
5698 }
5699 }
5700 else
5701 abort ();
5702
5703 break;
5704
5705 case M_LB_AB:
5706 s = "lb";
5707 goto ld;
5708 case M_LBU_AB:
5709 s = "lbu";
5710 goto ld;
5711 case M_LH_AB:
5712 s = "lh";
5713 goto ld;
5714 case M_LHU_AB:
5715 s = "lhu";
5716 goto ld;
5717 case M_LW_AB:
5718 s = "lw";
5719 goto ld;
5720 case M_LWC0_AB:
5721 s = "lwc0";
5722 /* Itbl support may require additional care here. */
5723 coproc = 1;
5724 goto ld;
5725 case M_LWC1_AB:
5726 s = "lwc1";
5727 /* Itbl support may require additional care here. */
5728 coproc = 1;
5729 goto ld;
5730 case M_LWC2_AB:
5731 s = "lwc2";
5732 /* Itbl support may require additional care here. */
5733 coproc = 1;
5734 goto ld;
5735 case M_LWC3_AB:
5736 s = "lwc3";
5737 /* Itbl support may require additional care here. */
5738 coproc = 1;
5739 goto ld;
5740 case M_LWL_AB:
5741 s = "lwl";
5742 lr = 1;
5743 goto ld;
5744 case M_LWR_AB:
5745 s = "lwr";
5746 lr = 1;
5747 goto ld;
5748 case M_LDC1_AB:
5749 if (mips_opts.arch == CPU_R4650)
5750 {
5751 as_bad (_("opcode not supported on this processor"));
5752 break;
5753 }
5754 s = "ldc1";
5755 /* Itbl support may require additional care here. */
5756 coproc = 1;
5757 goto ld;
5758 case M_LDC2_AB:
5759 s = "ldc2";
5760 /* Itbl support may require additional care here. */
5761 coproc = 1;
5762 goto ld;
5763 case M_LDC3_AB:
5764 s = "ldc3";
5765 /* Itbl support may require additional care here. */
5766 coproc = 1;
5767 goto ld;
5768 case M_LDL_AB:
5769 s = "ldl";
5770 lr = 1;
5771 goto ld;
5772 case M_LDR_AB:
5773 s = "ldr";
5774 lr = 1;
5775 goto ld;
5776 case M_LL_AB:
5777 s = "ll";
5778 goto ld;
5779 case M_LLD_AB:
5780 s = "lld";
5781 goto ld;
5782 case M_LWU_AB:
5783 s = "lwu";
5784 ld:
5785 if (breg == treg || coproc || lr)
5786 {
5787 tempreg = AT;
5788 used_at = 1;
5789 }
5790 else
5791 {
5792 tempreg = treg;
5793 }
5794 goto ld_st;
5795 case M_SB_AB:
5796 s = "sb";
5797 goto st;
5798 case M_SH_AB:
5799 s = "sh";
5800 goto st;
5801 case M_SW_AB:
5802 s = "sw";
5803 goto st;
5804 case M_SWC0_AB:
5805 s = "swc0";
5806 /* Itbl support may require additional care here. */
5807 coproc = 1;
5808 goto st;
5809 case M_SWC1_AB:
5810 s = "swc1";
5811 /* Itbl support may require additional care here. */
5812 coproc = 1;
5813 goto st;
5814 case M_SWC2_AB:
5815 s = "swc2";
5816 /* Itbl support may require additional care here. */
5817 coproc = 1;
5818 goto st;
5819 case M_SWC3_AB:
5820 s = "swc3";
5821 /* Itbl support may require additional care here. */
5822 coproc = 1;
5823 goto st;
5824 case M_SWL_AB:
5825 s = "swl";
5826 goto st;
5827 case M_SWR_AB:
5828 s = "swr";
5829 goto st;
5830 case M_SC_AB:
5831 s = "sc";
5832 goto st;
5833 case M_SCD_AB:
5834 s = "scd";
5835 goto st;
5836 case M_SDC1_AB:
5837 if (mips_opts.arch == CPU_R4650)
5838 {
5839 as_bad (_("opcode not supported on this processor"));
5840 break;
5841 }
5842 s = "sdc1";
5843 coproc = 1;
5844 /* Itbl support may require additional care here. */
5845 goto st;
5846 case M_SDC2_AB:
5847 s = "sdc2";
5848 /* Itbl support may require additional care here. */
5849 coproc = 1;
5850 goto st;
5851 case M_SDC3_AB:
5852 s = "sdc3";
5853 /* Itbl support may require additional care here. */
5854 coproc = 1;
5855 goto st;
5856 case M_SDL_AB:
5857 s = "sdl";
5858 goto st;
5859 case M_SDR_AB:
5860 s = "sdr";
5861 st:
5862 tempreg = AT;
5863 used_at = 1;
5864 ld_st:
5865 /* Itbl support may require additional care here. */
5866 if (mask == M_LWC1_AB
5867 || mask == M_SWC1_AB
5868 || mask == M_LDC1_AB
5869 || mask == M_SDC1_AB
5870 || mask == M_L_DAB
5871 || mask == M_S_DAB)
5872 fmt = "T,o(b)";
5873 else if (coproc)
5874 fmt = "E,o(b)";
5875 else
5876 fmt = "t,o(b)";
5877
5878 if (offset_expr.X_op != O_constant
5879 && offset_expr.X_op != O_symbol)
5880 {
5881 as_bad (_("expression too complex"));
5882 offset_expr.X_op = O_constant;
5883 }
5884
5885 /* A constant expression in PIC code can be handled just as it
5886 is in non PIC code. */
5887 if (offset_expr.X_op == O_constant)
5888 {
5889 if (HAVE_32BIT_ADDRESSES
5890 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5891 as_bad (_("constant too large"));
5892
5893 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5894 & ~(bfd_vma) 0xffff);
5895 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5896 if (breg != 0)
5897 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5898 tempreg, tempreg, breg);
5899 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5900 }
5901 else if (mips_pic == NO_PIC)
5902 {
5903 /* If this is a reference to a GP relative symbol, and there
5904 is no base register, we want
5905 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5906 Otherwise, if there is no base register, we want
5907 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5908 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5909 If we have a constant, we need two instructions anyhow,
5910 so we always use the latter form.
5911
5912 If we have a base register, and this is a reference to a
5913 GP relative symbol, we want
5914 addu $tempreg,$breg,$gp
5915 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5916 Otherwise we want
5917 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5918 addu $tempreg,$tempreg,$breg
5919 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5920 With a constant we always use the latter case.
5921
5922 With 64bit address space and no base register and $at usable,
5923 we want
5924 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5925 lui $at,<sym> (BFD_RELOC_HI16_S)
5926 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5927 dsll32 $tempreg,0
5928 daddu $tempreg,$at
5929 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5930 If we have a base register, we want
5931 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5932 lui $at,<sym> (BFD_RELOC_HI16_S)
5933 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5934 daddu $at,$breg
5935 dsll32 $tempreg,0
5936 daddu $tempreg,$at
5937 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5938
5939 Without $at we can't generate the optimal path for superscalar
5940 processors here since this would require two temporary registers.
5941 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5942 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5943 dsll $tempreg,16
5944 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5945 dsll $tempreg,16
5946 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5947 If we have a base register, we want
5948 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5949 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5950 dsll $tempreg,16
5951 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5952 dsll $tempreg,16
5953 daddu $tempreg,$tempreg,$breg
5954 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5955
5956 For GP relative symbols in 64bit address space we can use
5957 the same sequence as in 32bit address space. */
5958 if (HAVE_64BIT_SYMBOLS)
5959 {
5960 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5961 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5962 {
5963 relax_start (offset_expr.X_add_symbol);
5964 if (breg == 0)
5965 {
5966 macro_build (&offset_expr, s, fmt, treg,
5967 BFD_RELOC_GPREL16, mips_gp_register);
5968 }
5969 else
5970 {
5971 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5972 tempreg, breg, mips_gp_register);
5973 macro_build (&offset_expr, s, fmt, treg,
5974 BFD_RELOC_GPREL16, tempreg);
5975 }
5976 relax_switch ();
5977 }
5978
5979 if (used_at == 0 && !mips_opts.noat)
5980 {
5981 macro_build (&offset_expr, "lui", "t,u", tempreg,
5982 BFD_RELOC_MIPS_HIGHEST);
5983 macro_build (&offset_expr, "lui", "t,u", AT,
5984 BFD_RELOC_HI16_S);
5985 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5986 tempreg, BFD_RELOC_MIPS_HIGHER);
5987 if (breg != 0)
5988 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5989 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5990 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5991 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5992 tempreg);
5993 used_at = 1;
5994 }
5995 else
5996 {
5997 macro_build (&offset_expr, "lui", "t,u", tempreg,
5998 BFD_RELOC_MIPS_HIGHEST);
5999 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6000 tempreg, BFD_RELOC_MIPS_HIGHER);
6001 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6002 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6003 tempreg, BFD_RELOC_HI16_S);
6004 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6005 if (breg != 0)
6006 macro_build (NULL, "daddu", "d,v,t",
6007 tempreg, tempreg, breg);
6008 macro_build (&offset_expr, s, fmt, treg,
6009 BFD_RELOC_LO16, tempreg);
6010 }
6011
6012 if (mips_relax.sequence)
6013 relax_end ();
6014 break;
6015 }
6016
6017 if (breg == 0)
6018 {
6019 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6020 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6021 {
6022 relax_start (offset_expr.X_add_symbol);
6023 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6024 mips_gp_register);
6025 relax_switch ();
6026 }
6027 macro_build_lui (&offset_expr, tempreg);
6028 macro_build (&offset_expr, s, fmt, treg,
6029 BFD_RELOC_LO16, tempreg);
6030 if (mips_relax.sequence)
6031 relax_end ();
6032 }
6033 else
6034 {
6035 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6036 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6037 {
6038 relax_start (offset_expr.X_add_symbol);
6039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6040 tempreg, breg, mips_gp_register);
6041 macro_build (&offset_expr, s, fmt, treg,
6042 BFD_RELOC_GPREL16, tempreg);
6043 relax_switch ();
6044 }
6045 macro_build_lui (&offset_expr, tempreg);
6046 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6047 tempreg, tempreg, breg);
6048 macro_build (&offset_expr, s, fmt, treg,
6049 BFD_RELOC_LO16, tempreg);
6050 if (mips_relax.sequence)
6051 relax_end ();
6052 }
6053 }
6054 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6055 {
6056 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6057
6058 /* If this is a reference to an external symbol, we want
6059 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6060 nop
6061 <op> $treg,0($tempreg)
6062 Otherwise we want
6063 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6064 nop
6065 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6066 <op> $treg,0($tempreg)
6067
6068 For NewABI, we want
6069 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6070 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6071
6072 If there is a base register, we add it to $tempreg before
6073 the <op>. If there is a constant, we stick it in the
6074 <op> instruction. We don't handle constants larger than
6075 16 bits, because we have no way to load the upper 16 bits
6076 (actually, we could handle them for the subset of cases
6077 in which we are not using $at). */
6078 assert (offset_expr.X_op == O_symbol);
6079 if (HAVE_NEWABI)
6080 {
6081 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6082 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6083 if (breg != 0)
6084 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6085 tempreg, tempreg, breg);
6086 macro_build (&offset_expr, s, fmt, treg,
6087 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6088 break;
6089 }
6090 expr1.X_add_number = offset_expr.X_add_number;
6091 offset_expr.X_add_number = 0;
6092 if (expr1.X_add_number < -0x8000
6093 || expr1.X_add_number >= 0x8000)
6094 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6095 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6096 lw_reloc_type, mips_gp_register);
6097 load_delay_nop ();
6098 relax_start (offset_expr.X_add_symbol);
6099 relax_switch ();
6100 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6101 tempreg, BFD_RELOC_LO16);
6102 relax_end ();
6103 if (breg != 0)
6104 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6105 tempreg, tempreg, breg);
6106 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6107 }
6108 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6109 {
6110 int gpdelay;
6111
6112 /* If this is a reference to an external symbol, we want
6113 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6114 addu $tempreg,$tempreg,$gp
6115 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6116 <op> $treg,0($tempreg)
6117 Otherwise we want
6118 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6119 nop
6120 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6121 <op> $treg,0($tempreg)
6122 If there is a base register, we add it to $tempreg before
6123 the <op>. If there is a constant, we stick it in the
6124 <op> instruction. We don't handle constants larger than
6125 16 bits, because we have no way to load the upper 16 bits
6126 (actually, we could handle them for the subset of cases
6127 in which we are not using $at). */
6128 assert (offset_expr.X_op == O_symbol);
6129 expr1.X_add_number = offset_expr.X_add_number;
6130 offset_expr.X_add_number = 0;
6131 if (expr1.X_add_number < -0x8000
6132 || expr1.X_add_number >= 0x8000)
6133 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6134 gpdelay = reg_needs_delay (mips_gp_register);
6135 relax_start (offset_expr.X_add_symbol);
6136 macro_build (&offset_expr, "lui", "t,u", tempreg,
6137 BFD_RELOC_MIPS_GOT_HI16);
6138 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6139 mips_gp_register);
6140 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6141 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6142 relax_switch ();
6143 if (gpdelay)
6144 macro_build (NULL, "nop", "");
6145 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6146 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6147 load_delay_nop ();
6148 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6149 tempreg, BFD_RELOC_LO16);
6150 relax_end ();
6151
6152 if (breg != 0)
6153 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6154 tempreg, tempreg, breg);
6155 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6156 }
6157 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6158 {
6159 /* If this is a reference to an external symbol, we want
6160 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6161 add $tempreg,$tempreg,$gp
6162 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6163 <op> $treg,<ofst>($tempreg)
6164 Otherwise, for local symbols, we want:
6165 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6166 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6167 assert (offset_expr.X_op == O_symbol);
6168 expr1.X_add_number = offset_expr.X_add_number;
6169 offset_expr.X_add_number = 0;
6170 if (expr1.X_add_number < -0x8000
6171 || expr1.X_add_number >= 0x8000)
6172 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6173 relax_start (offset_expr.X_add_symbol);
6174 macro_build (&offset_expr, "lui", "t,u", tempreg,
6175 BFD_RELOC_MIPS_GOT_HI16);
6176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6177 mips_gp_register);
6178 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6179 BFD_RELOC_MIPS_GOT_LO16, tempreg);
6180 if (breg != 0)
6181 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6182 tempreg, tempreg, breg);
6183 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6184
6185 relax_switch ();
6186 offset_expr.X_add_number = expr1.X_add_number;
6187 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6188 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
6189 if (breg != 0)
6190 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6191 tempreg, tempreg, breg);
6192 macro_build (&offset_expr, s, fmt, treg,
6193 BFD_RELOC_MIPS_GOT_OFST, tempreg);
6194 relax_end ();
6195 }
6196 else
6197 abort ();
6198
6199 break;
6200
6201 case M_LI:
6202 case M_LI_S:
6203 load_register (treg, &imm_expr, 0);
6204 break;
6205
6206 case M_DLI:
6207 load_register (treg, &imm_expr, 1);
6208 break;
6209
6210 case M_LI_SS:
6211 if (imm_expr.X_op == O_constant)
6212 {
6213 used_at = 1;
6214 load_register (AT, &imm_expr, 0);
6215 macro_build (NULL, "mtc1", "t,G", AT, treg);
6216 break;
6217 }
6218 else
6219 {
6220 assert (offset_expr.X_op == O_symbol
6221 && strcmp (segment_name (S_GET_SEGMENT
6222 (offset_expr.X_add_symbol)),
6223 ".lit4") == 0
6224 && offset_expr.X_add_number == 0);
6225 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
6226 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6227 break;
6228 }
6229
6230 case M_LI_D:
6231 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6232 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6233 order 32 bits of the value and the low order 32 bits are either
6234 zero or in OFFSET_EXPR. */
6235 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6236 {
6237 if (HAVE_64BIT_GPRS)
6238 load_register (treg, &imm_expr, 1);
6239 else
6240 {
6241 int hreg, lreg;
6242
6243 if (target_big_endian)
6244 {
6245 hreg = treg;
6246 lreg = treg + 1;
6247 }
6248 else
6249 {
6250 hreg = treg + 1;
6251 lreg = treg;
6252 }
6253
6254 if (hreg <= 31)
6255 load_register (hreg, &imm_expr, 0);
6256 if (lreg <= 31)
6257 {
6258 if (offset_expr.X_op == O_absent)
6259 move_register (lreg, 0);
6260 else
6261 {
6262 assert (offset_expr.X_op == O_constant);
6263 load_register (lreg, &offset_expr, 0);
6264 }
6265 }
6266 }
6267 break;
6268 }
6269
6270 /* We know that sym is in the .rdata section. First we get the
6271 upper 16 bits of the address. */
6272 if (mips_pic == NO_PIC)
6273 {
6274 macro_build_lui (&offset_expr, AT);
6275 used_at = 1;
6276 }
6277 else if (mips_pic == SVR4_PIC)
6278 {
6279 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6280 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6281 used_at = 1;
6282 }
6283 else
6284 abort ();
6285
6286 /* Now we load the register(s). */
6287 if (HAVE_64BIT_GPRS)
6288 {
6289 used_at = 1;
6290 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6291 }
6292 else
6293 {
6294 used_at = 1;
6295 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6296 if (treg != RA)
6297 {
6298 /* FIXME: How in the world do we deal with the possible
6299 overflow here? */
6300 offset_expr.X_add_number += 4;
6301 macro_build (&offset_expr, "lw", "t,o(b)",
6302 treg + 1, BFD_RELOC_LO16, AT);
6303 }
6304 }
6305 break;
6306
6307 case M_LI_DD:
6308 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6309 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6310 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6311 the value and the low order 32 bits are either zero or in
6312 OFFSET_EXPR. */
6313 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6314 {
6315 used_at = 1;
6316 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
6317 if (HAVE_64BIT_FPRS)
6318 {
6319 assert (HAVE_64BIT_GPRS);
6320 macro_build (NULL, "dmtc1", "t,S", AT, treg);
6321 }
6322 else
6323 {
6324 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
6325 if (offset_expr.X_op == O_absent)
6326 macro_build (NULL, "mtc1", "t,G", 0, treg);
6327 else
6328 {
6329 assert (offset_expr.X_op == O_constant);
6330 load_register (AT, &offset_expr, 0);
6331 macro_build (NULL, "mtc1", "t,G", AT, treg);
6332 }
6333 }
6334 break;
6335 }
6336
6337 assert (offset_expr.X_op == O_symbol
6338 && offset_expr.X_add_number == 0);
6339 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6340 if (strcmp (s, ".lit8") == 0)
6341 {
6342 if (mips_opts.isa != ISA_MIPS1)
6343 {
6344 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
6345 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6346 break;
6347 }
6348 breg = mips_gp_register;
6349 r = BFD_RELOC_MIPS_LITERAL;
6350 goto dob;
6351 }
6352 else
6353 {
6354 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6355 used_at = 1;
6356 if (mips_pic == SVR4_PIC)
6357 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6358 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6359 else
6360 {
6361 /* FIXME: This won't work for a 64 bit address. */
6362 macro_build_lui (&offset_expr, AT);
6363 }
6364
6365 if (mips_opts.isa != ISA_MIPS1)
6366 {
6367 macro_build (&offset_expr, "ldc1", "T,o(b)",
6368 treg, BFD_RELOC_LO16, AT);
6369 break;
6370 }
6371 breg = AT;
6372 r = BFD_RELOC_LO16;
6373 goto dob;
6374 }
6375
6376 case M_L_DOB:
6377 if (mips_opts.arch == CPU_R4650)
6378 {
6379 as_bad (_("opcode not supported on this processor"));
6380 break;
6381 }
6382 /* Even on a big endian machine $fn comes before $fn+1. We have
6383 to adjust when loading from memory. */
6384 r = BFD_RELOC_LO16;
6385 dob:
6386 assert (mips_opts.isa == ISA_MIPS1);
6387 macro_build (&offset_expr, "lwc1", "T,o(b)",
6388 target_big_endian ? treg + 1 : treg, r, breg);
6389 /* FIXME: A possible overflow which I don't know how to deal
6390 with. */
6391 offset_expr.X_add_number += 4;
6392 macro_build (&offset_expr, "lwc1", "T,o(b)",
6393 target_big_endian ? treg : treg + 1, r, breg);
6394 break;
6395
6396 case M_L_DAB:
6397 /*
6398 * The MIPS assembler seems to check for X_add_number not
6399 * being double aligned and generating:
6400 * lui at,%hi(foo+1)
6401 * addu at,at,v1
6402 * addiu at,at,%lo(foo+1)
6403 * lwc1 f2,0(at)
6404 * lwc1 f3,4(at)
6405 * But, the resulting address is the same after relocation so why
6406 * generate the extra instruction?
6407 */
6408 if (mips_opts.arch == CPU_R4650)
6409 {
6410 as_bad (_("opcode not supported on this processor"));
6411 break;
6412 }
6413 /* Itbl support may require additional care here. */
6414 coproc = 1;
6415 if (mips_opts.isa != ISA_MIPS1)
6416 {
6417 s = "ldc1";
6418 goto ld;
6419 }
6420
6421 s = "lwc1";
6422 fmt = "T,o(b)";
6423 goto ldd_std;
6424
6425 case M_S_DAB:
6426 if (mips_opts.arch == CPU_R4650)
6427 {
6428 as_bad (_("opcode not supported on this processor"));
6429 break;
6430 }
6431
6432 if (mips_opts.isa != ISA_MIPS1)
6433 {
6434 s = "sdc1";
6435 goto st;
6436 }
6437
6438 s = "swc1";
6439 fmt = "T,o(b)";
6440 /* Itbl support may require additional care here. */
6441 coproc = 1;
6442 goto ldd_std;
6443
6444 case M_LD_AB:
6445 if (HAVE_64BIT_GPRS)
6446 {
6447 s = "ld";
6448 goto ld;
6449 }
6450
6451 s = "lw";
6452 fmt = "t,o(b)";
6453 goto ldd_std;
6454
6455 case M_SD_AB:
6456 if (HAVE_64BIT_GPRS)
6457 {
6458 s = "sd";
6459 goto st;
6460 }
6461
6462 s = "sw";
6463 fmt = "t,o(b)";
6464
6465 ldd_std:
6466 if (offset_expr.X_op != O_symbol
6467 && offset_expr.X_op != O_constant)
6468 {
6469 as_bad (_("expression too complex"));
6470 offset_expr.X_op = O_constant;
6471 }
6472
6473 /* Even on a big endian machine $fn comes before $fn+1. We have
6474 to adjust when loading from memory. We set coproc if we must
6475 load $fn+1 first. */
6476 /* Itbl support may require additional care here. */
6477 if (! target_big_endian)
6478 coproc = 0;
6479
6480 if (mips_pic == NO_PIC
6481 || offset_expr.X_op == O_constant)
6482 {
6483 /* If this is a reference to a GP relative symbol, we want
6484 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6485 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6486 If we have a base register, we use this
6487 addu $at,$breg,$gp
6488 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6489 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6490 If this is not a GP relative symbol, we want
6491 lui $at,<sym> (BFD_RELOC_HI16_S)
6492 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6493 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6494 If there is a base register, we add it to $at after the
6495 lui instruction. If there is a constant, we always use
6496 the last case. */
6497 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6498 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6499 {
6500 relax_start (offset_expr.X_add_symbol);
6501 if (breg == 0)
6502 {
6503 tempreg = mips_gp_register;
6504 }
6505 else
6506 {
6507 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6508 AT, breg, mips_gp_register);
6509 tempreg = AT;
6510 used_at = 1;
6511 }
6512
6513 /* Itbl support may require additional care here. */
6514 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6515 BFD_RELOC_GPREL16, tempreg);
6516 offset_expr.X_add_number += 4;
6517
6518 /* Set mips_optimize to 2 to avoid inserting an
6519 undesired nop. */
6520 hold_mips_optimize = mips_optimize;
6521 mips_optimize = 2;
6522 /* Itbl support may require additional care here. */
6523 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6524 BFD_RELOC_GPREL16, tempreg);
6525 mips_optimize = hold_mips_optimize;
6526
6527 relax_switch ();
6528
6529 /* We just generated two relocs. When tc_gen_reloc
6530 handles this case, it will skip the first reloc and
6531 handle the second. The second reloc already has an
6532 extra addend of 4, which we added above. We must
6533 subtract it out, and then subtract another 4 to make
6534 the first reloc come out right. The second reloc
6535 will come out right because we are going to add 4 to
6536 offset_expr when we build its instruction below.
6537
6538 If we have a symbol, then we don't want to include
6539 the offset, because it will wind up being included
6540 when we generate the reloc. */
6541
6542 if (offset_expr.X_op == O_constant)
6543 offset_expr.X_add_number -= 8;
6544 else
6545 {
6546 offset_expr.X_add_number = -4;
6547 offset_expr.X_op = O_constant;
6548 }
6549 }
6550 used_at = 1;
6551 macro_build_lui (&offset_expr, AT);
6552 if (breg != 0)
6553 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6554 /* Itbl support may require additional care here. */
6555 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6556 BFD_RELOC_LO16, AT);
6557 /* FIXME: How do we handle overflow here? */
6558 offset_expr.X_add_number += 4;
6559 /* Itbl support may require additional care here. */
6560 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6561 BFD_RELOC_LO16, AT);
6562 if (mips_relax.sequence)
6563 relax_end ();
6564 }
6565 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6566 {
6567 /* If this is a reference to an external symbol, we want
6568 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6569 nop
6570 <op> $treg,0($at)
6571 <op> $treg+1,4($at)
6572 Otherwise we want
6573 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6574 nop
6575 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6576 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6577 If there is a base register we add it to $at before the
6578 lwc1 instructions. If there is a constant we include it
6579 in the lwc1 instructions. */
6580 used_at = 1;
6581 expr1.X_add_number = offset_expr.X_add_number;
6582 if (expr1.X_add_number < -0x8000
6583 || expr1.X_add_number >= 0x8000 - 4)
6584 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6585 load_got_offset (AT, &offset_expr);
6586 load_delay_nop ();
6587 if (breg != 0)
6588 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6589
6590 /* Set mips_optimize to 2 to avoid inserting an undesired
6591 nop. */
6592 hold_mips_optimize = mips_optimize;
6593 mips_optimize = 2;
6594
6595 /* Itbl support may require additional care here. */
6596 relax_start (offset_expr.X_add_symbol);
6597 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6598 BFD_RELOC_LO16, AT);
6599 expr1.X_add_number += 4;
6600 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6601 BFD_RELOC_LO16, AT);
6602 relax_switch ();
6603 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6604 BFD_RELOC_LO16, AT);
6605 offset_expr.X_add_number += 4;
6606 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6607 BFD_RELOC_LO16, AT);
6608 relax_end ();
6609
6610 mips_optimize = hold_mips_optimize;
6611 }
6612 else if (mips_pic == SVR4_PIC)
6613 {
6614 int gpdelay;
6615
6616 /* If this is a reference to an external symbol, we want
6617 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6618 addu $at,$at,$gp
6619 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6620 nop
6621 <op> $treg,0($at)
6622 <op> $treg+1,4($at)
6623 Otherwise we want
6624 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6625 nop
6626 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6627 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6628 If there is a base register we add it to $at before the
6629 lwc1 instructions. If there is a constant we include it
6630 in the lwc1 instructions. */
6631 used_at = 1;
6632 expr1.X_add_number = offset_expr.X_add_number;
6633 offset_expr.X_add_number = 0;
6634 if (expr1.X_add_number < -0x8000
6635 || expr1.X_add_number >= 0x8000 - 4)
6636 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6637 gpdelay = reg_needs_delay (mips_gp_register);
6638 relax_start (offset_expr.X_add_symbol);
6639 macro_build (&offset_expr, "lui", "t,u",
6640 AT, BFD_RELOC_MIPS_GOT_HI16);
6641 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6642 AT, AT, mips_gp_register);
6643 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6644 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6645 load_delay_nop ();
6646 if (breg != 0)
6647 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6648 /* Itbl support may require additional care here. */
6649 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6650 BFD_RELOC_LO16, AT);
6651 expr1.X_add_number += 4;
6652
6653 /* Set mips_optimize to 2 to avoid inserting an undesired
6654 nop. */
6655 hold_mips_optimize = mips_optimize;
6656 mips_optimize = 2;
6657 /* Itbl support may require additional care here. */
6658 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6659 BFD_RELOC_LO16, AT);
6660 mips_optimize = hold_mips_optimize;
6661 expr1.X_add_number -= 4;
6662
6663 relax_switch ();
6664 offset_expr.X_add_number = expr1.X_add_number;
6665 if (gpdelay)
6666 macro_build (NULL, "nop", "");
6667 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6668 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6669 load_delay_nop ();
6670 if (breg != 0)
6671 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
6672 /* Itbl support may require additional care here. */
6673 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6674 BFD_RELOC_LO16, AT);
6675 offset_expr.X_add_number += 4;
6676
6677 /* Set mips_optimize to 2 to avoid inserting an undesired
6678 nop. */
6679 hold_mips_optimize = mips_optimize;
6680 mips_optimize = 2;
6681 /* Itbl support may require additional care here. */
6682 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6683 BFD_RELOC_LO16, AT);
6684 mips_optimize = hold_mips_optimize;
6685 relax_end ();
6686 }
6687 else
6688 abort ();
6689
6690 break;
6691
6692 case M_LD_OB:
6693 s = "lw";
6694 goto sd_ob;
6695 case M_SD_OB:
6696 s = "sw";
6697 sd_ob:
6698 assert (HAVE_32BIT_ADDRESSES);
6699 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
6700 offset_expr.X_add_number += 4;
6701 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
6702 break;
6703
6704 /* New code added to support COPZ instructions.
6705 This code builds table entries out of the macros in mip_opcodes.
6706 R4000 uses interlocks to handle coproc delays.
6707 Other chips (like the R3000) require nops to be inserted for delays.
6708
6709 FIXME: Currently, we require that the user handle delays.
6710 In order to fill delay slots for non-interlocked chips,
6711 we must have a way to specify delays based on the coprocessor.
6712 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6713 What are the side-effects of the cop instruction?
6714 What cache support might we have and what are its effects?
6715 Both coprocessor & memory require delays. how long???
6716 What registers are read/set/modified?
6717
6718 If an itbl is provided to interpret cop instructions,
6719 this knowledge can be encoded in the itbl spec. */
6720
6721 case M_COP0:
6722 s = "c0";
6723 goto copz;
6724 case M_COP1:
6725 s = "c1";
6726 goto copz;
6727 case M_COP2:
6728 s = "c2";
6729 goto copz;
6730 case M_COP3:
6731 s = "c3";
6732 copz:
6733 /* For now we just do C (same as Cz). The parameter will be
6734 stored in insn_opcode by mips_ip. */
6735 macro_build (NULL, s, "C", ip->insn_opcode);
6736 break;
6737
6738 case M_MOVE:
6739 move_register (dreg, sreg);
6740 break;
6741
6742 #ifdef LOSING_COMPILER
6743 default:
6744 /* Try and see if this is a new itbl instruction.
6745 This code builds table entries out of the macros in mip_opcodes.
6746 FIXME: For now we just assemble the expression and pass it's
6747 value along as a 32-bit immediate.
6748 We may want to have the assembler assemble this value,
6749 so that we gain the assembler's knowledge of delay slots,
6750 symbols, etc.
6751 Would it be more efficient to use mask (id) here? */
6752 if (itbl_have_entries
6753 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6754 {
6755 s = ip->insn_mo->name;
6756 s2 = "cop3";
6757 coproc = ITBL_DECODE_PNUM (immed_expr);;
6758 macro_build (&immed_expr, s, "C");
6759 break;
6760 }
6761 macro2 (ip);
6762 break;
6763 }
6764 if (mips_opts.noat && used_at)
6765 as_bad (_("Macro used $at after \".set noat\""));
6766 }
6767
6768 static void
6769 macro2 (struct mips_cl_insn *ip)
6770 {
6771 register int treg, sreg, dreg, breg;
6772 int tempreg;
6773 int mask;
6774 int used_at;
6775 expressionS expr1;
6776 const char *s;
6777 const char *s2;
6778 const char *fmt;
6779 int likely = 0;
6780 int dbl = 0;
6781 int coproc = 0;
6782 int lr = 0;
6783 int imm = 0;
6784 int off;
6785 offsetT maxnum;
6786 bfd_reloc_code_real_type r;
6787
6788 treg = (ip->insn_opcode >> 16) & 0x1f;
6789 dreg = (ip->insn_opcode >> 11) & 0x1f;
6790 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6791 mask = ip->insn_mo->mask;
6792
6793 expr1.X_op = O_constant;
6794 expr1.X_op_symbol = NULL;
6795 expr1.X_add_symbol = NULL;
6796 expr1.X_add_number = 1;
6797
6798 switch (mask)
6799 {
6800 #endif /* LOSING_COMPILER */
6801
6802 case M_DMUL:
6803 dbl = 1;
6804 case M_MUL:
6805 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6806 macro_build (NULL, "mflo", "d", dreg);
6807 break;
6808
6809 case M_DMUL_I:
6810 dbl = 1;
6811 case M_MUL_I:
6812 /* The MIPS assembler some times generates shifts and adds. I'm
6813 not trying to be that fancy. GCC should do this for us
6814 anyway. */
6815 used_at = 1;
6816 load_register (AT, &imm_expr, dbl);
6817 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6818 macro_build (NULL, "mflo", "d", dreg);
6819 break;
6820
6821 case M_DMULO_I:
6822 dbl = 1;
6823 case M_MULO_I:
6824 imm = 1;
6825 goto do_mulo;
6826
6827 case M_DMULO:
6828 dbl = 1;
6829 case M_MULO:
6830 do_mulo:
6831 mips_emit_delays (TRUE);
6832 ++mips_opts.noreorder;
6833 mips_any_noreorder = 1;
6834 used_at = 1;
6835 if (imm)
6836 load_register (AT, &imm_expr, dbl);
6837 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6838 macro_build (NULL, "mflo", "d", dreg);
6839 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6840 macro_build (NULL, "mfhi", "d", AT);
6841 if (mips_trap)
6842 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
6843 else
6844 {
6845 expr1.X_add_number = 8;
6846 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6847 macro_build (NULL, "nop", "", 0);
6848 macro_build (NULL, "break", "c", 6);
6849 }
6850 --mips_opts.noreorder;
6851 macro_build (NULL, "mflo", "d", dreg);
6852 break;
6853
6854 case M_DMULOU_I:
6855 dbl = 1;
6856 case M_MULOU_I:
6857 imm = 1;
6858 goto do_mulou;
6859
6860 case M_DMULOU:
6861 dbl = 1;
6862 case M_MULOU:
6863 do_mulou:
6864 mips_emit_delays (TRUE);
6865 ++mips_opts.noreorder;
6866 mips_any_noreorder = 1;
6867 used_at = 1;
6868 if (imm)
6869 load_register (AT, &imm_expr, dbl);
6870 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
6871 sreg, imm ? AT : treg);
6872 macro_build (NULL, "mfhi", "d", AT);
6873 macro_build (NULL, "mflo", "d", dreg);
6874 if (mips_trap)
6875 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
6876 else
6877 {
6878 expr1.X_add_number = 8;
6879 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6880 macro_build (NULL, "nop", "", 0);
6881 macro_build (NULL, "break", "c", 6);
6882 }
6883 --mips_opts.noreorder;
6884 break;
6885
6886 case M_DROL:
6887 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6888 {
6889 if (dreg == sreg)
6890 {
6891 tempreg = AT;
6892 used_at = 1;
6893 }
6894 else
6895 {
6896 tempreg = dreg;
6897 }
6898 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6899 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
6900 break;
6901 }
6902 used_at = 1;
6903 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6904 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6905 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6906 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6907 break;
6908
6909 case M_ROL:
6910 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6911 {
6912 if (dreg == sreg)
6913 {
6914 tempreg = AT;
6915 used_at = 1;
6916 }
6917 else
6918 {
6919 tempreg = dreg;
6920 }
6921 macro_build (NULL, "negu", "d,w", tempreg, treg);
6922 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
6923 break;
6924 }
6925 used_at = 1;
6926 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6927 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6928 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6929 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6930 break;
6931
6932 case M_DROL_I:
6933 {
6934 unsigned int rot;
6935 char *l, *r;
6936
6937 if (imm_expr.X_op != O_constant)
6938 as_bad (_("Improper rotate count"));
6939 rot = imm_expr.X_add_number & 0x3f;
6940 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6941 {
6942 rot = (64 - rot) & 0x3f;
6943 if (rot >= 32)
6944 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
6945 else
6946 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
6947 break;
6948 }
6949 if (rot == 0)
6950 {
6951 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
6952 break;
6953 }
6954 l = (rot < 0x20) ? "dsll" : "dsll32";
6955 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6956 rot &= 0x1f;
6957 used_at = 1;
6958 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6959 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6960 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6961 }
6962 break;
6963
6964 case M_ROL_I:
6965 {
6966 unsigned int rot;
6967
6968 if (imm_expr.X_op != O_constant)
6969 as_bad (_("Improper rotate count"));
6970 rot = imm_expr.X_add_number & 0x1f;
6971 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
6972 {
6973 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
6974 break;
6975 }
6976 if (rot == 0)
6977 {
6978 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
6979 break;
6980 }
6981 used_at = 1;
6982 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6983 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6984 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6985 }
6986 break;
6987
6988 case M_DROR:
6989 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
6990 {
6991 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
6992 break;
6993 }
6994 used_at = 1;
6995 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6996 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6997 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6998 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
6999 break;
7000
7001 case M_ROR:
7002 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7003 {
7004 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
7005 break;
7006 }
7007 used_at = 1;
7008 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7009 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7010 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7011 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7012 break;
7013
7014 case M_DROR_I:
7015 {
7016 unsigned int rot;
7017 char *l, *r;
7018
7019 if (imm_expr.X_op != O_constant)
7020 as_bad (_("Improper rotate count"));
7021 rot = imm_expr.X_add_number & 0x3f;
7022 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
7023 {
7024 if (rot >= 32)
7025 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
7026 else
7027 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
7028 break;
7029 }
7030 if (rot == 0)
7031 {
7032 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
7033 break;
7034 }
7035 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7036 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7037 rot &= 0x1f;
7038 used_at = 1;
7039 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7040 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7041 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7042 }
7043 break;
7044
7045 case M_ROR_I:
7046 {
7047 unsigned int rot;
7048
7049 if (imm_expr.X_op != O_constant)
7050 as_bad (_("Improper rotate count"));
7051 rot = imm_expr.X_add_number & 0x1f;
7052 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
7053 {
7054 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
7055 break;
7056 }
7057 if (rot == 0)
7058 {
7059 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
7060 break;
7061 }
7062 used_at = 1;
7063 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7064 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7065 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
7066 }
7067 break;
7068
7069 case M_S_DOB:
7070 if (mips_opts.arch == CPU_R4650)
7071 {
7072 as_bad (_("opcode not supported on this processor"));
7073 break;
7074 }
7075 assert (mips_opts.isa == ISA_MIPS1);
7076 /* Even on a big endian machine $fn comes before $fn+1. We have
7077 to adjust when storing to memory. */
7078 macro_build (&offset_expr, "swc1", "T,o(b)",
7079 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
7080 offset_expr.X_add_number += 4;
7081 macro_build (&offset_expr, "swc1", "T,o(b)",
7082 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
7083 break;
7084
7085 case M_SEQ:
7086 if (sreg == 0)
7087 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
7088 else if (treg == 0)
7089 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7090 else
7091 {
7092 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7093 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7094 }
7095 break;
7096
7097 case M_SEQ_I:
7098 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7099 {
7100 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7101 break;
7102 }
7103 if (sreg == 0)
7104 {
7105 as_warn (_("Instruction %s: result is always false"),
7106 ip->insn_mo->name);
7107 move_register (dreg, 0);
7108 break;
7109 }
7110 if (imm_expr.X_op == O_constant
7111 && imm_expr.X_add_number >= 0
7112 && imm_expr.X_add_number < 0x10000)
7113 {
7114 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7115 }
7116 else if (imm_expr.X_op == O_constant
7117 && imm_expr.X_add_number > -0x8000
7118 && imm_expr.X_add_number < 0)
7119 {
7120 imm_expr.X_add_number = -imm_expr.X_add_number;
7121 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7122 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7123 }
7124 else
7125 {
7126 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7127 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7128 used_at = 1;
7129 }
7130 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
7131 break;
7132
7133 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7134 s = "slt";
7135 goto sge;
7136 case M_SGEU:
7137 s = "sltu";
7138 sge:
7139 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7140 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7141 break;
7142
7143 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7144 case M_SGEU_I:
7145 if (imm_expr.X_op == O_constant
7146 && imm_expr.X_add_number >= -0x8000
7147 && imm_expr.X_add_number < 0x8000)
7148 {
7149 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7150 dreg, sreg, BFD_RELOC_LO16);
7151 }
7152 else
7153 {
7154 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7155 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7156 dreg, sreg, AT);
7157 used_at = 1;
7158 }
7159 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7160 break;
7161
7162 case M_SGT: /* sreg > treg <==> treg < sreg */
7163 s = "slt";
7164 goto sgt;
7165 case M_SGTU:
7166 s = "sltu";
7167 sgt:
7168 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7169 break;
7170
7171 case M_SGT_I: /* sreg > I <==> I < sreg */
7172 s = "slt";
7173 goto sgti;
7174 case M_SGTU_I:
7175 s = "sltu";
7176 sgti:
7177 used_at = 1;
7178 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7179 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7180 break;
7181
7182 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7183 s = "slt";
7184 goto sle;
7185 case M_SLEU:
7186 s = "sltu";
7187 sle:
7188 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7189 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7190 break;
7191
7192 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7193 s = "slt";
7194 goto slei;
7195 case M_SLEU_I:
7196 s = "sltu";
7197 slei:
7198 used_at = 1;
7199 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7200 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7201 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
7202 break;
7203
7204 case M_SLT_I:
7205 if (imm_expr.X_op == O_constant
7206 && imm_expr.X_add_number >= -0x8000
7207 && imm_expr.X_add_number < 0x8000)
7208 {
7209 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7210 break;
7211 }
7212 used_at = 1;
7213 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7214 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
7215 break;
7216
7217 case M_SLTU_I:
7218 if (imm_expr.X_op == O_constant
7219 && imm_expr.X_add_number >= -0x8000
7220 && imm_expr.X_add_number < 0x8000)
7221 {
7222 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
7223 BFD_RELOC_LO16);
7224 break;
7225 }
7226 used_at = 1;
7227 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7228 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
7229 break;
7230
7231 case M_SNE:
7232 if (sreg == 0)
7233 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
7234 else if (treg == 0)
7235 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7236 else
7237 {
7238 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7239 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7240 }
7241 break;
7242
7243 case M_SNE_I:
7244 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7245 {
7246 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
7247 break;
7248 }
7249 if (sreg == 0)
7250 {
7251 as_warn (_("Instruction %s: result is always true"),
7252 ip->insn_mo->name);
7253 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7254 dreg, 0, BFD_RELOC_LO16);
7255 break;
7256 }
7257 if (imm_expr.X_op == O_constant
7258 && imm_expr.X_add_number >= 0
7259 && imm_expr.X_add_number < 0x10000)
7260 {
7261 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
7262 }
7263 else if (imm_expr.X_op == O_constant
7264 && imm_expr.X_add_number > -0x8000
7265 && imm_expr.X_add_number < 0)
7266 {
7267 imm_expr.X_add_number = -imm_expr.X_add_number;
7268 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7269 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
7270 }
7271 else
7272 {
7273 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7274 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
7275 used_at = 1;
7276 }
7277 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
7278 break;
7279
7280 case M_DSUB_I:
7281 dbl = 1;
7282 case M_SUB_I:
7283 if (imm_expr.X_op == O_constant
7284 && imm_expr.X_add_number > -0x8000
7285 && imm_expr.X_add_number <= 0x8000)
7286 {
7287 imm_expr.X_add_number = -imm_expr.X_add_number;
7288 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7289 dreg, sreg, BFD_RELOC_LO16);
7290 break;
7291 }
7292 used_at = 1;
7293 load_register (AT, &imm_expr, dbl);
7294 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7295 break;
7296
7297 case M_DSUBU_I:
7298 dbl = 1;
7299 case M_SUBU_I:
7300 if (imm_expr.X_op == O_constant
7301 && imm_expr.X_add_number > -0x8000
7302 && imm_expr.X_add_number <= 0x8000)
7303 {
7304 imm_expr.X_add_number = -imm_expr.X_add_number;
7305 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7306 dreg, sreg, BFD_RELOC_LO16);
7307 break;
7308 }
7309 used_at = 1;
7310 load_register (AT, &imm_expr, dbl);
7311 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7312 break;
7313
7314 case M_TEQ_I:
7315 s = "teq";
7316 goto trap;
7317 case M_TGE_I:
7318 s = "tge";
7319 goto trap;
7320 case M_TGEU_I:
7321 s = "tgeu";
7322 goto trap;
7323 case M_TLT_I:
7324 s = "tlt";
7325 goto trap;
7326 case M_TLTU_I:
7327 s = "tltu";
7328 goto trap;
7329 case M_TNE_I:
7330 s = "tne";
7331 trap:
7332 used_at = 1;
7333 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7334 macro_build (NULL, s, "s,t", sreg, AT);
7335 break;
7336
7337 case M_TRUNCWS:
7338 case M_TRUNCWD:
7339 assert (mips_opts.isa == ISA_MIPS1);
7340 used_at = 1;
7341 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7342 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7343
7344 /*
7345 * Is the double cfc1 instruction a bug in the mips assembler;
7346 * or is there a reason for it?
7347 */
7348 mips_emit_delays (TRUE);
7349 ++mips_opts.noreorder;
7350 mips_any_noreorder = 1;
7351 macro_build (NULL, "cfc1", "t,G", treg, RA);
7352 macro_build (NULL, "cfc1", "t,G", treg, RA);
7353 macro_build (NULL, "nop", "");
7354 expr1.X_add_number = 3;
7355 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
7356 expr1.X_add_number = 2;
7357 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7358 macro_build (NULL, "ctc1", "t,G", AT, RA);
7359 macro_build (NULL, "nop", "");
7360 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7361 dreg, sreg);
7362 macro_build (NULL, "ctc1", "t,G", treg, RA);
7363 macro_build (NULL, "nop", "");
7364 --mips_opts.noreorder;
7365 break;
7366
7367 case M_ULH:
7368 s = "lb";
7369 goto ulh;
7370 case M_ULHU:
7371 s = "lbu";
7372 ulh:
7373 used_at = 1;
7374 if (offset_expr.X_add_number >= 0x7fff)
7375 as_bad (_("operand overflow"));
7376 if (! target_big_endian)
7377 ++offset_expr.X_add_number;
7378 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
7379 if (! target_big_endian)
7380 --offset_expr.X_add_number;
7381 else
7382 ++offset_expr.X_add_number;
7383 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7384 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7385 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7386 break;
7387
7388 case M_ULD:
7389 s = "ldl";
7390 s2 = "ldr";
7391 off = 7;
7392 goto ulw;
7393 case M_ULW:
7394 s = "lwl";
7395 s2 = "lwr";
7396 off = 3;
7397 ulw:
7398 if (offset_expr.X_add_number >= 0x8000 - off)
7399 as_bad (_("operand overflow"));
7400 if (treg != breg)
7401 tempreg = treg;
7402 else
7403 {
7404 used_at = 1;
7405 tempreg = AT;
7406 }
7407 if (! target_big_endian)
7408 offset_expr.X_add_number += off;
7409 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7410 if (! target_big_endian)
7411 offset_expr.X_add_number -= off;
7412 else
7413 offset_expr.X_add_number += off;
7414 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
7415
7416 /* If necessary, move the result in tempreg the final destination. */
7417 if (treg == tempreg)
7418 break;
7419 /* Protect second load's delay slot. */
7420 load_delay_nop ();
7421 move_register (treg, tempreg);
7422 break;
7423
7424 case M_ULD_A:
7425 s = "ldl";
7426 s2 = "ldr";
7427 off = 7;
7428 goto ulwa;
7429 case M_ULW_A:
7430 s = "lwl";
7431 s2 = "lwr";
7432 off = 3;
7433 ulwa:
7434 used_at = 1;
7435 load_address (AT, &offset_expr, &used_at);
7436 if (breg != 0)
7437 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7438 if (! target_big_endian)
7439 expr1.X_add_number = off;
7440 else
7441 expr1.X_add_number = 0;
7442 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7443 if (! target_big_endian)
7444 expr1.X_add_number = 0;
7445 else
7446 expr1.X_add_number = off;
7447 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7448 break;
7449
7450 case M_ULH_A:
7451 case M_ULHU_A:
7452 used_at = 1;
7453 load_address (AT, &offset_expr, &used_at);
7454 if (breg != 0)
7455 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7456 if (target_big_endian)
7457 expr1.X_add_number = 0;
7458 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7459 treg, BFD_RELOC_LO16, AT);
7460 if (target_big_endian)
7461 expr1.X_add_number = 1;
7462 else
7463 expr1.X_add_number = 0;
7464 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7465 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7466 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7467 break;
7468
7469 case M_USH:
7470 used_at = 1;
7471 if (offset_expr.X_add_number >= 0x7fff)
7472 as_bad (_("operand overflow"));
7473 if (target_big_endian)
7474 ++offset_expr.X_add_number;
7475 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7476 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
7477 if (target_big_endian)
7478 --offset_expr.X_add_number;
7479 else
7480 ++offset_expr.X_add_number;
7481 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
7482 break;
7483
7484 case M_USD:
7485 s = "sdl";
7486 s2 = "sdr";
7487 off = 7;
7488 goto usw;
7489 case M_USW:
7490 s = "swl";
7491 s2 = "swr";
7492 off = 3;
7493 usw:
7494 if (offset_expr.X_add_number >= 0x8000 - off)
7495 as_bad (_("operand overflow"));
7496 if (! target_big_endian)
7497 offset_expr.X_add_number += off;
7498 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7499 if (! target_big_endian)
7500 offset_expr.X_add_number -= off;
7501 else
7502 offset_expr.X_add_number += off;
7503 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
7504 break;
7505
7506 case M_USD_A:
7507 s = "sdl";
7508 s2 = "sdr";
7509 off = 7;
7510 goto uswa;
7511 case M_USW_A:
7512 s = "swl";
7513 s2 = "swr";
7514 off = 3;
7515 uswa:
7516 used_at = 1;
7517 load_address (AT, &offset_expr, &used_at);
7518 if (breg != 0)
7519 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7520 if (! target_big_endian)
7521 expr1.X_add_number = off;
7522 else
7523 expr1.X_add_number = 0;
7524 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7525 if (! target_big_endian)
7526 expr1.X_add_number = 0;
7527 else
7528 expr1.X_add_number = off;
7529 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
7530 break;
7531
7532 case M_USH_A:
7533 used_at = 1;
7534 load_address (AT, &offset_expr, &used_at);
7535 if (breg != 0)
7536 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
7537 if (! target_big_endian)
7538 expr1.X_add_number = 0;
7539 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7540 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
7541 if (! target_big_endian)
7542 expr1.X_add_number = 1;
7543 else
7544 expr1.X_add_number = 0;
7545 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7546 if (! target_big_endian)
7547 expr1.X_add_number = 0;
7548 else
7549 expr1.X_add_number = 1;
7550 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7551 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7552 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
7553 break;
7554
7555 default:
7556 /* FIXME: Check if this is one of the itbl macros, since they
7557 are added dynamically. */
7558 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7559 break;
7560 }
7561 if (mips_opts.noat && used_at)
7562 as_bad (_("Macro used $at after \".set noat\""));
7563 }
7564
7565 /* Implement macros in mips16 mode. */
7566
7567 static void
7568 mips16_macro (struct mips_cl_insn *ip)
7569 {
7570 int mask;
7571 int xreg, yreg, zreg, tmp;
7572 expressionS expr1;
7573 int dbl;
7574 const char *s, *s2, *s3;
7575
7576 mask = ip->insn_mo->mask;
7577
7578 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7579 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7580 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7581
7582 expr1.X_op = O_constant;
7583 expr1.X_op_symbol = NULL;
7584 expr1.X_add_symbol = NULL;
7585 expr1.X_add_number = 1;
7586
7587 dbl = 0;
7588
7589 switch (mask)
7590 {
7591 default:
7592 internalError ();
7593
7594 case M_DDIV_3:
7595 dbl = 1;
7596 case M_DIV_3:
7597 s = "mflo";
7598 goto do_div3;
7599 case M_DREM_3:
7600 dbl = 1;
7601 case M_REM_3:
7602 s = "mfhi";
7603 do_div3:
7604 mips_emit_delays (TRUE);
7605 ++mips_opts.noreorder;
7606 mips_any_noreorder = 1;
7607 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
7608 expr1.X_add_number = 2;
7609 macro_build (&expr1, "bnez", "x,p", yreg);
7610 macro_build (NULL, "break", "6", 7);
7611
7612 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7613 since that causes an overflow. We should do that as well,
7614 but I don't see how to do the comparisons without a temporary
7615 register. */
7616 --mips_opts.noreorder;
7617 macro_build (NULL, s, "x", zreg);
7618 break;
7619
7620 case M_DIVU_3:
7621 s = "divu";
7622 s2 = "mflo";
7623 goto do_divu3;
7624 case M_REMU_3:
7625 s = "divu";
7626 s2 = "mfhi";
7627 goto do_divu3;
7628 case M_DDIVU_3:
7629 s = "ddivu";
7630 s2 = "mflo";
7631 goto do_divu3;
7632 case M_DREMU_3:
7633 s = "ddivu";
7634 s2 = "mfhi";
7635 do_divu3:
7636 mips_emit_delays (TRUE);
7637 ++mips_opts.noreorder;
7638 mips_any_noreorder = 1;
7639 macro_build (NULL, s, "0,x,y", xreg, yreg);
7640 expr1.X_add_number = 2;
7641 macro_build (&expr1, "bnez", "x,p", yreg);
7642 macro_build (NULL, "break", "6", 7);
7643 --mips_opts.noreorder;
7644 macro_build (NULL, s2, "x", zreg);
7645 break;
7646
7647 case M_DMUL:
7648 dbl = 1;
7649 case M_MUL:
7650 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7651 macro_build (NULL, "mflo", "x", zreg);
7652 break;
7653
7654 case M_DSUBU_I:
7655 dbl = 1;
7656 goto do_subu;
7657 case M_SUBU_I:
7658 do_subu:
7659 if (imm_expr.X_op != O_constant)
7660 as_bad (_("Unsupported large constant"));
7661 imm_expr.X_add_number = -imm_expr.X_add_number;
7662 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7663 break;
7664
7665 case M_SUBU_I_2:
7666 if (imm_expr.X_op != O_constant)
7667 as_bad (_("Unsupported large constant"));
7668 imm_expr.X_add_number = -imm_expr.X_add_number;
7669 macro_build (&imm_expr, "addiu", "x,k", xreg);
7670 break;
7671
7672 case M_DSUBU_I_2:
7673 if (imm_expr.X_op != O_constant)
7674 as_bad (_("Unsupported large constant"));
7675 imm_expr.X_add_number = -imm_expr.X_add_number;
7676 macro_build (&imm_expr, "daddiu", "y,j", yreg);
7677 break;
7678
7679 case M_BEQ:
7680 s = "cmp";
7681 s2 = "bteqz";
7682 goto do_branch;
7683 case M_BNE:
7684 s = "cmp";
7685 s2 = "btnez";
7686 goto do_branch;
7687 case M_BLT:
7688 s = "slt";
7689 s2 = "btnez";
7690 goto do_branch;
7691 case M_BLTU:
7692 s = "sltu";
7693 s2 = "btnez";
7694 goto do_branch;
7695 case M_BLE:
7696 s = "slt";
7697 s2 = "bteqz";
7698 goto do_reverse_branch;
7699 case M_BLEU:
7700 s = "sltu";
7701 s2 = "bteqz";
7702 goto do_reverse_branch;
7703 case M_BGE:
7704 s = "slt";
7705 s2 = "bteqz";
7706 goto do_branch;
7707 case M_BGEU:
7708 s = "sltu";
7709 s2 = "bteqz";
7710 goto do_branch;
7711 case M_BGT:
7712 s = "slt";
7713 s2 = "btnez";
7714 goto do_reverse_branch;
7715 case M_BGTU:
7716 s = "sltu";
7717 s2 = "btnez";
7718
7719 do_reverse_branch:
7720 tmp = xreg;
7721 xreg = yreg;
7722 yreg = tmp;
7723
7724 do_branch:
7725 macro_build (NULL, s, "x,y", xreg, yreg);
7726 macro_build (&offset_expr, s2, "p");
7727 break;
7728
7729 case M_BEQ_I:
7730 s = "cmpi";
7731 s2 = "bteqz";
7732 s3 = "x,U";
7733 goto do_branch_i;
7734 case M_BNE_I:
7735 s = "cmpi";
7736 s2 = "btnez";
7737 s3 = "x,U";
7738 goto do_branch_i;
7739 case M_BLT_I:
7740 s = "slti";
7741 s2 = "btnez";
7742 s3 = "x,8";
7743 goto do_branch_i;
7744 case M_BLTU_I:
7745 s = "sltiu";
7746 s2 = "btnez";
7747 s3 = "x,8";
7748 goto do_branch_i;
7749 case M_BLE_I:
7750 s = "slti";
7751 s2 = "btnez";
7752 s3 = "x,8";
7753 goto do_addone_branch_i;
7754 case M_BLEU_I:
7755 s = "sltiu";
7756 s2 = "btnez";
7757 s3 = "x,8";
7758 goto do_addone_branch_i;
7759 case M_BGE_I:
7760 s = "slti";
7761 s2 = "bteqz";
7762 s3 = "x,8";
7763 goto do_branch_i;
7764 case M_BGEU_I:
7765 s = "sltiu";
7766 s2 = "bteqz";
7767 s3 = "x,8";
7768 goto do_branch_i;
7769 case M_BGT_I:
7770 s = "slti";
7771 s2 = "bteqz";
7772 s3 = "x,8";
7773 goto do_addone_branch_i;
7774 case M_BGTU_I:
7775 s = "sltiu";
7776 s2 = "bteqz";
7777 s3 = "x,8";
7778
7779 do_addone_branch_i:
7780 if (imm_expr.X_op != O_constant)
7781 as_bad (_("Unsupported large constant"));
7782 ++imm_expr.X_add_number;
7783
7784 do_branch_i:
7785 macro_build (&imm_expr, s, s3, xreg);
7786 macro_build (&offset_expr, s2, "p");
7787 break;
7788
7789 case M_ABS:
7790 expr1.X_add_number = 0;
7791 macro_build (&expr1, "slti", "x,8", yreg);
7792 if (xreg != yreg)
7793 move_register (xreg, yreg);
7794 expr1.X_add_number = 2;
7795 macro_build (&expr1, "bteqz", "p");
7796 macro_build (NULL, "neg", "x,w", xreg, xreg);
7797 }
7798 }
7799
7800 /* For consistency checking, verify that all bits are specified either
7801 by the match/mask part of the instruction definition, or by the
7802 operand list. */
7803 static int
7804 validate_mips_insn (const struct mips_opcode *opc)
7805 {
7806 const char *p = opc->args;
7807 char c;
7808 unsigned long used_bits = opc->mask;
7809
7810 if ((used_bits & opc->match) != opc->match)
7811 {
7812 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7813 opc->name, opc->args);
7814 return 0;
7815 }
7816 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7817 while (*p)
7818 switch (c = *p++)
7819 {
7820 case ',': break;
7821 case '(': break;
7822 case ')': break;
7823 case '+':
7824 switch (c = *p++)
7825 {
7826 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7827 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7828 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7829 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7830 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7831 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7832 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7833 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7834 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7835 case 'I': break;
7836 default:
7837 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7838 c, opc->name, opc->args);
7839 return 0;
7840 }
7841 break;
7842 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7843 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7844 case 'A': break;
7845 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7846 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7847 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7848 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7849 case 'F': break;
7850 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7851 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7852 case 'I': break;
7853 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7854 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7855 case 'L': break;
7856 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7857 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7858 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7859 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7860 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7861 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7862 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7863 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7864 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7865 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7866 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7867 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7868 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7869 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7870 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7871 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7872 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7873 case 'f': break;
7874 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7875 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7876 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7877 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7878 case 'l': break;
7879 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7880 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7881 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7882 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7883 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7884 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7885 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7886 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7887 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7888 case 'x': break;
7889 case 'z': break;
7890 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7891 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7892 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7893 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7894 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7895 case '[': break;
7896 case ']': break;
7897 default:
7898 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7899 c, opc->name, opc->args);
7900 return 0;
7901 }
7902 #undef USE_BITS
7903 if (used_bits != 0xffffffff)
7904 {
7905 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7906 ~used_bits & 0xffffffff, opc->name, opc->args);
7907 return 0;
7908 }
7909 return 1;
7910 }
7911
7912 /* This routine assembles an instruction into its binary format. As a
7913 side effect, it sets one of the global variables imm_reloc or
7914 offset_reloc to the type of relocation to do if one of the operands
7915 is an address expression. */
7916
7917 static void
7918 mips_ip (char *str, struct mips_cl_insn *ip)
7919 {
7920 char *s;
7921 const char *args;
7922 char c = 0;
7923 struct mips_opcode *insn;
7924 char *argsStart;
7925 unsigned int regno;
7926 unsigned int lastregno = 0;
7927 unsigned int lastpos = 0;
7928 unsigned int limlo, limhi;
7929 char *s_reset;
7930 char save_c = 0;
7931
7932 insn_error = NULL;
7933
7934 /* If the instruction contains a '.', we first try to match an instruction
7935 including the '.'. Then we try again without the '.'. */
7936 insn = NULL;
7937 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7938 continue;
7939
7940 /* If we stopped on whitespace, then replace the whitespace with null for
7941 the call to hash_find. Save the character we replaced just in case we
7942 have to re-parse the instruction. */
7943 if (ISSPACE (*s))
7944 {
7945 save_c = *s;
7946 *s++ = '\0';
7947 }
7948
7949 insn = (struct mips_opcode *) hash_find (op_hash, str);
7950
7951 /* If we didn't find the instruction in the opcode table, try again, but
7952 this time with just the instruction up to, but not including the
7953 first '.'. */
7954 if (insn == NULL)
7955 {
7956 /* Restore the character we overwrite above (if any). */
7957 if (save_c)
7958 *(--s) = save_c;
7959
7960 /* Scan up to the first '.' or whitespace. */
7961 for (s = str;
7962 *s != '\0' && *s != '.' && !ISSPACE (*s);
7963 ++s)
7964 continue;
7965
7966 /* If we did not find a '.', then we can quit now. */
7967 if (*s != '.')
7968 {
7969 insn_error = "unrecognized opcode";
7970 return;
7971 }
7972
7973 /* Lookup the instruction in the hash table. */
7974 *s++ = '\0';
7975 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7976 {
7977 insn_error = "unrecognized opcode";
7978 return;
7979 }
7980 }
7981
7982 argsStart = s;
7983 for (;;)
7984 {
7985 bfd_boolean ok;
7986
7987 assert (strcmp (insn->name, str) == 0);
7988
7989 if (OPCODE_IS_MEMBER (insn,
7990 (mips_opts.isa
7991 | (file_ase_mips16 ? INSN_MIPS16 : 0)
7992 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
7993 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7994 mips_opts.arch))
7995 ok = TRUE;
7996 else
7997 ok = FALSE;
7998
7999 if (insn->pinfo != INSN_MACRO)
8000 {
8001 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8002 ok = FALSE;
8003 }
8004
8005 if (! ok)
8006 {
8007 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8008 && strcmp (insn->name, insn[1].name) == 0)
8009 {
8010 ++insn;
8011 continue;
8012 }
8013 else
8014 {
8015 if (!insn_error)
8016 {
8017 static char buf[100];
8018 sprintf (buf,
8019 _("opcode not supported on this processor: %s (%s)"),
8020 mips_cpu_info_from_arch (mips_opts.arch)->name,
8021 mips_cpu_info_from_isa (mips_opts.isa)->name);
8022 insn_error = buf;
8023 }
8024 if (save_c)
8025 *(--s) = save_c;
8026 return;
8027 }
8028 }
8029
8030 ip->insn_mo = insn;
8031 ip->insn_opcode = insn->match;
8032 insn_error = NULL;
8033 for (args = insn->args;; ++args)
8034 {
8035 int is_mdmx;
8036
8037 s += strspn (s, " \t");
8038 is_mdmx = 0;
8039 switch (*args)
8040 {
8041 case '\0': /* end of args */
8042 if (*s == '\0')
8043 return;
8044 break;
8045
8046 case ',':
8047 if (*s++ == *args)
8048 continue;
8049 s--;
8050 switch (*++args)
8051 {
8052 case 'r':
8053 case 'v':
8054 ip->insn_opcode |= lastregno << OP_SH_RS;
8055 continue;
8056
8057 case 'w':
8058 ip->insn_opcode |= lastregno << OP_SH_RT;
8059 continue;
8060
8061 case 'W':
8062 ip->insn_opcode |= lastregno << OP_SH_FT;
8063 continue;
8064
8065 case 'V':
8066 ip->insn_opcode |= lastregno << OP_SH_FS;
8067 continue;
8068 }
8069 break;
8070
8071 case '(':
8072 /* Handle optional base register.
8073 Either the base register is omitted or
8074 we must have a left paren. */
8075 /* This is dependent on the next operand specifier
8076 is a base register specification. */
8077 assert (args[1] == 'b' || args[1] == '5'
8078 || args[1] == '-' || args[1] == '4');
8079 if (*s == '\0')
8080 return;
8081
8082 case ')': /* these must match exactly */
8083 case '[':
8084 case ']':
8085 if (*s++ == *args)
8086 continue;
8087 break;
8088
8089 case '+': /* Opcode extension character. */
8090 switch (*++args)
8091 {
8092 case 'A': /* ins/ext position, becomes LSB. */
8093 limlo = 0;
8094 limhi = 31;
8095 goto do_lsb;
8096 case 'E':
8097 limlo = 32;
8098 limhi = 63;
8099 goto do_lsb;
8100 do_lsb:
8101 my_getExpression (&imm_expr, s);
8102 check_absolute_expr (ip, &imm_expr);
8103 if ((unsigned long) imm_expr.X_add_number < limlo
8104 || (unsigned long) imm_expr.X_add_number > limhi)
8105 {
8106 as_bad (_("Improper position (%lu)"),
8107 (unsigned long) imm_expr.X_add_number);
8108 imm_expr.X_add_number = limlo;
8109 }
8110 lastpos = imm_expr.X_add_number;
8111 ip->insn_opcode |= (imm_expr.X_add_number
8112 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8113 imm_expr.X_op = O_absent;
8114 s = expr_end;
8115 continue;
8116
8117 case 'B': /* ins size, becomes MSB. */
8118 limlo = 1;
8119 limhi = 32;
8120 goto do_msb;
8121 case 'F':
8122 limlo = 33;
8123 limhi = 64;
8124 goto do_msb;
8125 do_msb:
8126 my_getExpression (&imm_expr, s);
8127 check_absolute_expr (ip, &imm_expr);
8128 /* Check for negative input so that small negative numbers
8129 will not succeed incorrectly. The checks against
8130 (pos+size) transitively check "size" itself,
8131 assuming that "pos" is reasonable. */
8132 if ((long) imm_expr.X_add_number < 0
8133 || ((unsigned long) imm_expr.X_add_number
8134 + lastpos) < limlo
8135 || ((unsigned long) imm_expr.X_add_number
8136 + lastpos) > limhi)
8137 {
8138 as_bad (_("Improper insert size (%lu, position %lu)"),
8139 (unsigned long) imm_expr.X_add_number,
8140 (unsigned long) lastpos);
8141 imm_expr.X_add_number = limlo - lastpos;
8142 }
8143 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8144 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8145 imm_expr.X_op = O_absent;
8146 s = expr_end;
8147 continue;
8148
8149 case 'C': /* ext size, becomes MSBD. */
8150 limlo = 1;
8151 limhi = 32;
8152 goto do_msbd;
8153 case 'G':
8154 limlo = 33;
8155 limhi = 64;
8156 goto do_msbd;
8157 case 'H':
8158 limlo = 33;
8159 limhi = 64;
8160 goto do_msbd;
8161 do_msbd:
8162 my_getExpression (&imm_expr, s);
8163 check_absolute_expr (ip, &imm_expr);
8164 /* Check for negative input so that small negative numbers
8165 will not succeed incorrectly. The checks against
8166 (pos+size) transitively check "size" itself,
8167 assuming that "pos" is reasonable. */
8168 if ((long) imm_expr.X_add_number < 0
8169 || ((unsigned long) imm_expr.X_add_number
8170 + lastpos) < limlo
8171 || ((unsigned long) imm_expr.X_add_number
8172 + lastpos) > limhi)
8173 {
8174 as_bad (_("Improper extract size (%lu, position %lu)"),
8175 (unsigned long) imm_expr.X_add_number,
8176 (unsigned long) lastpos);
8177 imm_expr.X_add_number = limlo - lastpos;
8178 }
8179 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8180 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8181 imm_expr.X_op = O_absent;
8182 s = expr_end;
8183 continue;
8184
8185 case 'D':
8186 /* +D is for disassembly only; never match. */
8187 break;
8188
8189 case 'I':
8190 /* "+I" is like "I", except that imm2_expr is used. */
8191 my_getExpression (&imm2_expr, s);
8192 if (imm2_expr.X_op != O_big
8193 && imm2_expr.X_op != O_constant)
8194 insn_error = _("absolute expression required");
8195 normalize_constant_expr (&imm2_expr);
8196 s = expr_end;
8197 continue;
8198
8199 default:
8200 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8201 *args, insn->name, insn->args);
8202 /* Further processing is fruitless. */
8203 return;
8204 }
8205 break;
8206
8207 case '<': /* must be at least one digit */
8208 /*
8209 * According to the manual, if the shift amount is greater
8210 * than 31 or less than 0, then the shift amount should be
8211 * mod 32. In reality the mips assembler issues an error.
8212 * We issue a warning and mask out all but the low 5 bits.
8213 */
8214 my_getExpression (&imm_expr, s);
8215 check_absolute_expr (ip, &imm_expr);
8216 if ((unsigned long) imm_expr.X_add_number > 31)
8217 {
8218 as_warn (_("Improper shift amount (%lu)"),
8219 (unsigned long) imm_expr.X_add_number);
8220 imm_expr.X_add_number &= OP_MASK_SHAMT;
8221 }
8222 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8223 imm_expr.X_op = O_absent;
8224 s = expr_end;
8225 continue;
8226
8227 case '>': /* shift amount minus 32 */
8228 my_getExpression (&imm_expr, s);
8229 check_absolute_expr (ip, &imm_expr);
8230 if ((unsigned long) imm_expr.X_add_number < 32
8231 || (unsigned long) imm_expr.X_add_number > 63)
8232 break;
8233 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8234 imm_expr.X_op = O_absent;
8235 s = expr_end;
8236 continue;
8237
8238 case 'k': /* cache code */
8239 case 'h': /* prefx code */
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 (_("Invalid value for `%s' (%lu)"),
8245 ip->insn_mo->name,
8246 (unsigned long) imm_expr.X_add_number);
8247 imm_expr.X_add_number &= 0x1f;
8248 }
8249 if (*args == 'k')
8250 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8251 else
8252 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8253 imm_expr.X_op = O_absent;
8254 s = expr_end;
8255 continue;
8256
8257 case 'c': /* break code */
8258 my_getExpression (&imm_expr, s);
8259 check_absolute_expr (ip, &imm_expr);
8260 if ((unsigned long) imm_expr.X_add_number > 1023)
8261 {
8262 as_warn (_("Illegal break code (%lu)"),
8263 (unsigned long) imm_expr.X_add_number);
8264 imm_expr.X_add_number &= OP_MASK_CODE;
8265 }
8266 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8267 imm_expr.X_op = O_absent;
8268 s = expr_end;
8269 continue;
8270
8271 case 'q': /* lower break code */
8272 my_getExpression (&imm_expr, s);
8273 check_absolute_expr (ip, &imm_expr);
8274 if ((unsigned long) imm_expr.X_add_number > 1023)
8275 {
8276 as_warn (_("Illegal lower break code (%lu)"),
8277 (unsigned long) imm_expr.X_add_number);
8278 imm_expr.X_add_number &= OP_MASK_CODE2;
8279 }
8280 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8281 imm_expr.X_op = O_absent;
8282 s = expr_end;
8283 continue;
8284
8285 case 'B': /* 20-bit syscall/break code. */
8286 my_getExpression (&imm_expr, s);
8287 check_absolute_expr (ip, &imm_expr);
8288 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8289 as_warn (_("Illegal 20-bit code (%lu)"),
8290 (unsigned long) imm_expr.X_add_number);
8291 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8292 imm_expr.X_op = O_absent;
8293 s = expr_end;
8294 continue;
8295
8296 case 'C': /* Coprocessor code */
8297 my_getExpression (&imm_expr, s);
8298 check_absolute_expr (ip, &imm_expr);
8299 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8300 {
8301 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8302 (unsigned long) imm_expr.X_add_number);
8303 imm_expr.X_add_number &= ((1 << 25) - 1);
8304 }
8305 ip->insn_opcode |= imm_expr.X_add_number;
8306 imm_expr.X_op = O_absent;
8307 s = expr_end;
8308 continue;
8309
8310 case 'J': /* 19-bit wait code. */
8311 my_getExpression (&imm_expr, s);
8312 check_absolute_expr (ip, &imm_expr);
8313 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8314 as_warn (_("Illegal 19-bit code (%lu)"),
8315 (unsigned long) imm_expr.X_add_number);
8316 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8317 imm_expr.X_op = O_absent;
8318 s = expr_end;
8319 continue;
8320
8321 case 'P': /* Performance register */
8322 my_getExpression (&imm_expr, s);
8323 check_absolute_expr (ip, &imm_expr);
8324 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8325 {
8326 as_warn (_("Invalid performance register (%lu)"),
8327 (unsigned long) imm_expr.X_add_number);
8328 imm_expr.X_add_number &= OP_MASK_PERFREG;
8329 }
8330 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
8331 imm_expr.X_op = O_absent;
8332 s = expr_end;
8333 continue;
8334
8335 case 'b': /* base register */
8336 case 'd': /* destination register */
8337 case 's': /* source register */
8338 case 't': /* target register */
8339 case 'r': /* both target and source */
8340 case 'v': /* both dest and source */
8341 case 'w': /* both dest and target */
8342 case 'E': /* coprocessor target register */
8343 case 'G': /* coprocessor destination register */
8344 case 'K': /* 'rdhwr' destination register */
8345 case 'x': /* ignore register name */
8346 case 'z': /* must be zero register */
8347 case 'U': /* destination register (clo/clz). */
8348 s_reset = s;
8349 if (s[0] == '$')
8350 {
8351
8352 if (ISDIGIT (s[1]))
8353 {
8354 ++s;
8355 regno = 0;
8356 do
8357 {
8358 regno *= 10;
8359 regno += *s - '0';
8360 ++s;
8361 }
8362 while (ISDIGIT (*s));
8363 if (regno > 31)
8364 as_bad (_("Invalid register number (%d)"), regno);
8365 }
8366 else if (*args == 'E' || *args == 'G' || *args == 'K')
8367 goto notreg;
8368 else
8369 {
8370 if (s[1] == 'r' && s[2] == 'a')
8371 {
8372 s += 3;
8373 regno = RA;
8374 }
8375 else if (s[1] == 'f' && s[2] == 'p')
8376 {
8377 s += 3;
8378 regno = FP;
8379 }
8380 else if (s[1] == 's' && s[2] == 'p')
8381 {
8382 s += 3;
8383 regno = SP;
8384 }
8385 else if (s[1] == 'g' && s[2] == 'p')
8386 {
8387 s += 3;
8388 regno = GP;
8389 }
8390 else if (s[1] == 'a' && s[2] == 't')
8391 {
8392 s += 3;
8393 regno = AT;
8394 }
8395 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8396 {
8397 s += 4;
8398 regno = KT0;
8399 }
8400 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8401 {
8402 s += 4;
8403 regno = KT1;
8404 }
8405 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8406 {
8407 s += 5;
8408 regno = ZERO;
8409 }
8410 else if (itbl_have_entries)
8411 {
8412 char *p, *n;
8413 unsigned long r;
8414
8415 p = s + 1; /* advance past '$' */
8416 n = itbl_get_field (&p); /* n is name */
8417
8418 /* See if this is a register defined in an
8419 itbl entry. */
8420 if (itbl_get_reg_val (n, &r))
8421 {
8422 /* Get_field advances to the start of
8423 the next field, so we need to back
8424 rack to the end of the last field. */
8425 if (p)
8426 s = p - 1;
8427 else
8428 s = strchr (s, '\0');
8429 regno = r;
8430 }
8431 else
8432 goto notreg;
8433 }
8434 else
8435 goto notreg;
8436 }
8437 if (regno == AT
8438 && ! mips_opts.noat
8439 && *args != 'E'
8440 && *args != 'G'
8441 && *args != 'K')
8442 as_warn (_("Used $at without \".set noat\""));
8443 c = *args;
8444 if (*s == ' ')
8445 ++s;
8446 if (args[1] != *s)
8447 {
8448 if (c == 'r' || c == 'v' || c == 'w')
8449 {
8450 regno = lastregno;
8451 s = s_reset;
8452 ++args;
8453 }
8454 }
8455 /* 'z' only matches $0. */
8456 if (c == 'z' && regno != 0)
8457 break;
8458
8459 /* Now that we have assembled one operand, we use the args string
8460 * to figure out where it goes in the instruction. */
8461 switch (c)
8462 {
8463 case 'r':
8464 case 's':
8465 case 'v':
8466 case 'b':
8467 ip->insn_opcode |= regno << OP_SH_RS;
8468 break;
8469 case 'd':
8470 case 'G':
8471 case 'K':
8472 ip->insn_opcode |= regno << OP_SH_RD;
8473 break;
8474 case 'U':
8475 ip->insn_opcode |= regno << OP_SH_RD;
8476 ip->insn_opcode |= regno << OP_SH_RT;
8477 break;
8478 case 'w':
8479 case 't':
8480 case 'E':
8481 ip->insn_opcode |= regno << OP_SH_RT;
8482 break;
8483 case 'x':
8484 /* This case exists because on the r3000 trunc
8485 expands into a macro which requires a gp
8486 register. On the r6000 or r4000 it is
8487 assembled into a single instruction which
8488 ignores the register. Thus the insn version
8489 is MIPS_ISA2 and uses 'x', and the macro
8490 version is MIPS_ISA1 and uses 't'. */
8491 break;
8492 case 'z':
8493 /* This case is for the div instruction, which
8494 acts differently if the destination argument
8495 is $0. This only matches $0, and is checked
8496 outside the switch. */
8497 break;
8498 case 'D':
8499 /* Itbl operand; not yet implemented. FIXME ?? */
8500 break;
8501 /* What about all other operands like 'i', which
8502 can be specified in the opcode table? */
8503 }
8504 lastregno = regno;
8505 continue;
8506 }
8507 notreg:
8508 switch (*args++)
8509 {
8510 case 'r':
8511 case 'v':
8512 ip->insn_opcode |= lastregno << OP_SH_RS;
8513 continue;
8514 case 'w':
8515 ip->insn_opcode |= lastregno << OP_SH_RT;
8516 continue;
8517 }
8518 break;
8519
8520 case 'O': /* MDMX alignment immediate constant. */
8521 my_getExpression (&imm_expr, s);
8522 check_absolute_expr (ip, &imm_expr);
8523 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8524 {
8525 as_warn ("Improper align amount (%ld), using low bits",
8526 (long) imm_expr.X_add_number);
8527 imm_expr.X_add_number &= OP_MASK_ALN;
8528 }
8529 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8530 imm_expr.X_op = O_absent;
8531 s = expr_end;
8532 continue;
8533
8534 case 'Q': /* MDMX vector, element sel, or const. */
8535 if (s[0] != '$')
8536 {
8537 /* MDMX Immediate. */
8538 my_getExpression (&imm_expr, s);
8539 check_absolute_expr (ip, &imm_expr);
8540 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8541 {
8542 as_warn (_("Invalid MDMX Immediate (%ld)"),
8543 (long) imm_expr.X_add_number);
8544 imm_expr.X_add_number &= OP_MASK_FT;
8545 }
8546 imm_expr.X_add_number &= OP_MASK_FT;
8547 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8548 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8549 else
8550 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8551 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8552 imm_expr.X_op = O_absent;
8553 s = expr_end;
8554 continue;
8555 }
8556 /* Not MDMX Immediate. Fall through. */
8557 case 'X': /* MDMX destination register. */
8558 case 'Y': /* MDMX source register. */
8559 case 'Z': /* MDMX target register. */
8560 is_mdmx = 1;
8561 case 'D': /* floating point destination register */
8562 case 'S': /* floating point source register */
8563 case 'T': /* floating point target register */
8564 case 'R': /* floating point source register */
8565 case 'V':
8566 case 'W':
8567 s_reset = s;
8568 /* Accept $fN for FP and MDMX register numbers, and in
8569 addition accept $vN for MDMX register numbers. */
8570 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8571 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8572 && ISDIGIT (s[2])))
8573 {
8574 s += 2;
8575 regno = 0;
8576 do
8577 {
8578 regno *= 10;
8579 regno += *s - '0';
8580 ++s;
8581 }
8582 while (ISDIGIT (*s));
8583
8584 if (regno > 31)
8585 as_bad (_("Invalid float register number (%d)"), regno);
8586
8587 if ((regno & 1) != 0
8588 && HAVE_32BIT_FPRS
8589 && ! (strcmp (str, "mtc1") == 0
8590 || strcmp (str, "mfc1") == 0
8591 || strcmp (str, "lwc1") == 0
8592 || strcmp (str, "swc1") == 0
8593 || strcmp (str, "l.s") == 0
8594 || strcmp (str, "s.s") == 0))
8595 as_warn (_("Float register should be even, was %d"),
8596 regno);
8597
8598 c = *args;
8599 if (*s == ' ')
8600 ++s;
8601 if (args[1] != *s)
8602 {
8603 if (c == 'V' || c == 'W')
8604 {
8605 regno = lastregno;
8606 s = s_reset;
8607 ++args;
8608 }
8609 }
8610 switch (c)
8611 {
8612 case 'D':
8613 case 'X':
8614 ip->insn_opcode |= regno << OP_SH_FD;
8615 break;
8616 case 'V':
8617 case 'S':
8618 case 'Y':
8619 ip->insn_opcode |= regno << OP_SH_FS;
8620 break;
8621 case 'Q':
8622 /* This is like 'Z', but also needs to fix the MDMX
8623 vector/scalar select bits. Note that the
8624 scalar immediate case is handled above. */
8625 if (*s == '[')
8626 {
8627 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8628 int max_el = (is_qh ? 3 : 7);
8629 s++;
8630 my_getExpression(&imm_expr, s);
8631 check_absolute_expr (ip, &imm_expr);
8632 s = expr_end;
8633 if (imm_expr.X_add_number > max_el)
8634 as_bad(_("Bad element selector %ld"),
8635 (long) imm_expr.X_add_number);
8636 imm_expr.X_add_number &= max_el;
8637 ip->insn_opcode |= (imm_expr.X_add_number
8638 << (OP_SH_VSEL +
8639 (is_qh ? 2 : 1)));
8640 imm_expr.X_op = O_absent;
8641 if (*s != ']')
8642 as_warn(_("Expecting ']' found '%s'"), s);
8643 else
8644 s++;
8645 }
8646 else
8647 {
8648 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8649 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8650 << OP_SH_VSEL);
8651 else
8652 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8653 OP_SH_VSEL);
8654 }
8655 /* Fall through */
8656 case 'W':
8657 case 'T':
8658 case 'Z':
8659 ip->insn_opcode |= regno << OP_SH_FT;
8660 break;
8661 case 'R':
8662 ip->insn_opcode |= regno << OP_SH_FR;
8663 break;
8664 }
8665 lastregno = regno;
8666 continue;
8667 }
8668
8669 switch (*args++)
8670 {
8671 case 'V':
8672 ip->insn_opcode |= lastregno << OP_SH_FS;
8673 continue;
8674 case 'W':
8675 ip->insn_opcode |= lastregno << OP_SH_FT;
8676 continue;
8677 }
8678 break;
8679
8680 case 'I':
8681 my_getExpression (&imm_expr, s);
8682 if (imm_expr.X_op != O_big
8683 && imm_expr.X_op != O_constant)
8684 insn_error = _("absolute expression required");
8685 normalize_constant_expr (&imm_expr);
8686 s = expr_end;
8687 continue;
8688
8689 case 'A':
8690 my_getExpression (&offset_expr, s);
8691 *imm_reloc = BFD_RELOC_32;
8692 s = expr_end;
8693 continue;
8694
8695 case 'F':
8696 case 'L':
8697 case 'f':
8698 case 'l':
8699 {
8700 int f64;
8701 int using_gprs;
8702 char *save_in;
8703 char *err;
8704 unsigned char temp[8];
8705 int len;
8706 unsigned int length;
8707 segT seg;
8708 subsegT subseg;
8709 char *p;
8710
8711 /* These only appear as the last operand in an
8712 instruction, and every instruction that accepts
8713 them in any variant accepts them in all variants.
8714 This means we don't have to worry about backing out
8715 any changes if the instruction does not match.
8716
8717 The difference between them is the size of the
8718 floating point constant and where it goes. For 'F'
8719 and 'L' the constant is 64 bits; for 'f' and 'l' it
8720 is 32 bits. Where the constant is placed is based
8721 on how the MIPS assembler does things:
8722 F -- .rdata
8723 L -- .lit8
8724 f -- immediate value
8725 l -- .lit4
8726
8727 The .lit4 and .lit8 sections are only used if
8728 permitted by the -G argument.
8729
8730 The code below needs to know whether the target register
8731 is 32 or 64 bits wide. It relies on the fact 'f' and
8732 'F' are used with GPR-based instructions and 'l' and
8733 'L' are used with FPR-based instructions. */
8734
8735 f64 = *args == 'F' || *args == 'L';
8736 using_gprs = *args == 'F' || *args == 'f';
8737
8738 save_in = input_line_pointer;
8739 input_line_pointer = s;
8740 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8741 length = len;
8742 s = input_line_pointer;
8743 input_line_pointer = save_in;
8744 if (err != NULL && *err != '\0')
8745 {
8746 as_bad (_("Bad floating point constant: %s"), err);
8747 memset (temp, '\0', sizeof temp);
8748 length = f64 ? 8 : 4;
8749 }
8750
8751 assert (length == (unsigned) (f64 ? 8 : 4));
8752
8753 if (*args == 'f'
8754 || (*args == 'l'
8755 && (g_switch_value < 4
8756 || (temp[0] == 0 && temp[1] == 0)
8757 || (temp[2] == 0 && temp[3] == 0))))
8758 {
8759 imm_expr.X_op = O_constant;
8760 if (! target_big_endian)
8761 imm_expr.X_add_number = bfd_getl32 (temp);
8762 else
8763 imm_expr.X_add_number = bfd_getb32 (temp);
8764 }
8765 else if (length > 4
8766 && ! mips_disable_float_construction
8767 /* Constants can only be constructed in GPRs and
8768 copied to FPRs if the GPRs are at least as wide
8769 as the FPRs. Force the constant into memory if
8770 we are using 64-bit FPRs but the GPRs are only
8771 32 bits wide. */
8772 && (using_gprs
8773 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8774 && ((temp[0] == 0 && temp[1] == 0)
8775 || (temp[2] == 0 && temp[3] == 0))
8776 && ((temp[4] == 0 && temp[5] == 0)
8777 || (temp[6] == 0 && temp[7] == 0)))
8778 {
8779 /* The value is simple enough to load with a couple of
8780 instructions. If using 32-bit registers, set
8781 imm_expr to the high order 32 bits and offset_expr to
8782 the low order 32 bits. Otherwise, set imm_expr to
8783 the entire 64 bit constant. */
8784 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8785 {
8786 imm_expr.X_op = O_constant;
8787 offset_expr.X_op = O_constant;
8788 if (! target_big_endian)
8789 {
8790 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8791 offset_expr.X_add_number = bfd_getl32 (temp);
8792 }
8793 else
8794 {
8795 imm_expr.X_add_number = bfd_getb32 (temp);
8796 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8797 }
8798 if (offset_expr.X_add_number == 0)
8799 offset_expr.X_op = O_absent;
8800 }
8801 else if (sizeof (imm_expr.X_add_number) > 4)
8802 {
8803 imm_expr.X_op = O_constant;
8804 if (! target_big_endian)
8805 imm_expr.X_add_number = bfd_getl64 (temp);
8806 else
8807 imm_expr.X_add_number = bfd_getb64 (temp);
8808 }
8809 else
8810 {
8811 imm_expr.X_op = O_big;
8812 imm_expr.X_add_number = 4;
8813 if (! target_big_endian)
8814 {
8815 generic_bignum[0] = bfd_getl16 (temp);
8816 generic_bignum[1] = bfd_getl16 (temp + 2);
8817 generic_bignum[2] = bfd_getl16 (temp + 4);
8818 generic_bignum[3] = bfd_getl16 (temp + 6);
8819 }
8820 else
8821 {
8822 generic_bignum[0] = bfd_getb16 (temp + 6);
8823 generic_bignum[1] = bfd_getb16 (temp + 4);
8824 generic_bignum[2] = bfd_getb16 (temp + 2);
8825 generic_bignum[3] = bfd_getb16 (temp);
8826 }
8827 }
8828 }
8829 else
8830 {
8831 const char *newname;
8832 segT new_seg;
8833
8834 /* Switch to the right section. */
8835 seg = now_seg;
8836 subseg = now_subseg;
8837 switch (*args)
8838 {
8839 default: /* unused default case avoids warnings. */
8840 case 'L':
8841 newname = RDATA_SECTION_NAME;
8842 if (g_switch_value >= 8)
8843 newname = ".lit8";
8844 break;
8845 case 'F':
8846 newname = RDATA_SECTION_NAME;
8847 break;
8848 case 'l':
8849 assert (g_switch_value >= 4);
8850 newname = ".lit4";
8851 break;
8852 }
8853 new_seg = subseg_new (newname, (subsegT) 0);
8854 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8855 bfd_set_section_flags (stdoutput, new_seg,
8856 (SEC_ALLOC
8857 | SEC_LOAD
8858 | SEC_READONLY
8859 | SEC_DATA));
8860 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8861 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8862 && strcmp (TARGET_OS, "elf") != 0)
8863 record_alignment (new_seg, 4);
8864 else
8865 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8866 if (seg == now_seg)
8867 as_bad (_("Can't use floating point insn in this section"));
8868
8869 /* Set the argument to the current address in the
8870 section. */
8871 offset_expr.X_op = O_symbol;
8872 offset_expr.X_add_symbol =
8873 symbol_new ("L0\001", now_seg,
8874 (valueT) frag_now_fix (), frag_now);
8875 offset_expr.X_add_number = 0;
8876
8877 /* Put the floating point number into the section. */
8878 p = frag_more ((int) length);
8879 memcpy (p, temp, length);
8880
8881 /* Switch back to the original section. */
8882 subseg_set (seg, subseg);
8883 }
8884 }
8885 continue;
8886
8887 case 'i': /* 16 bit unsigned immediate */
8888 case 'j': /* 16 bit signed immediate */
8889 *imm_reloc = BFD_RELOC_LO16;
8890 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
8891 {
8892 int more;
8893 offsetT minval, maxval;
8894
8895 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8896 && strcmp (insn->name, insn[1].name) == 0);
8897
8898 /* If the expression was written as an unsigned number,
8899 only treat it as signed if there are no more
8900 alternatives. */
8901 if (more
8902 && *args == 'j'
8903 && sizeof (imm_expr.X_add_number) <= 4
8904 && imm_expr.X_op == O_constant
8905 && imm_expr.X_add_number < 0
8906 && imm_expr.X_unsigned
8907 && HAVE_64BIT_GPRS)
8908 break;
8909
8910 /* For compatibility with older assemblers, we accept
8911 0x8000-0xffff as signed 16-bit numbers when only
8912 signed numbers are allowed. */
8913 if (*args == 'i')
8914 minval = 0, maxval = 0xffff;
8915 else if (more)
8916 minval = -0x8000, maxval = 0x7fff;
8917 else
8918 minval = -0x8000, maxval = 0xffff;
8919
8920 if (imm_expr.X_op != O_constant
8921 || imm_expr.X_add_number < minval
8922 || imm_expr.X_add_number > maxval)
8923 {
8924 if (more)
8925 break;
8926 if (imm_expr.X_op == O_constant
8927 || imm_expr.X_op == O_big)
8928 as_bad (_("expression out of range"));
8929 }
8930 }
8931 s = expr_end;
8932 continue;
8933
8934 case 'o': /* 16 bit offset */
8935 /* Check whether there is only a single bracketed expression
8936 left. If so, it must be the base register and the
8937 constant must be zero. */
8938 if (*s == '(' && strchr (s + 1, '(') == 0)
8939 {
8940 offset_expr.X_op = O_constant;
8941 offset_expr.X_add_number = 0;
8942 continue;
8943 }
8944
8945 /* If this value won't fit into a 16 bit offset, then go
8946 find a macro that will generate the 32 bit offset
8947 code pattern. */
8948 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
8949 && (offset_expr.X_op != O_constant
8950 || offset_expr.X_add_number >= 0x8000
8951 || offset_expr.X_add_number < -0x8000))
8952 break;
8953
8954 s = expr_end;
8955 continue;
8956
8957 case 'p': /* pc relative offset */
8958 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8959 my_getExpression (&offset_expr, s);
8960 s = expr_end;
8961 continue;
8962
8963 case 'u': /* upper 16 bits */
8964 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8965 && imm_expr.X_op == O_constant
8966 && (imm_expr.X_add_number < 0
8967 || imm_expr.X_add_number >= 0x10000))
8968 as_bad (_("lui expression not in range 0..65535"));
8969 s = expr_end;
8970 continue;
8971
8972 case 'a': /* 26 bit address */
8973 my_getExpression (&offset_expr, s);
8974 s = expr_end;
8975 *offset_reloc = BFD_RELOC_MIPS_JMP;
8976 continue;
8977
8978 case 'N': /* 3 bit branch condition code */
8979 case 'M': /* 3 bit compare condition code */
8980 if (strncmp (s, "$fcc", 4) != 0)
8981 break;
8982 s += 4;
8983 regno = 0;
8984 do
8985 {
8986 regno *= 10;
8987 regno += *s - '0';
8988 ++s;
8989 }
8990 while (ISDIGIT (*s));
8991 if (regno > 7)
8992 as_bad (_("Invalid condition code register $fcc%d"), regno);
8993 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8994 || strcmp(str + strlen(str) - 5, "any2f") == 0
8995 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8996 && (regno & 1) != 0)
8997 as_warn(_("Condition code register should be even for %s, was %d"),
8998 str, regno);
8999 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9000 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9001 && (regno & 3) != 0)
9002 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9003 str, regno);
9004 if (*args == 'N')
9005 ip->insn_opcode |= regno << OP_SH_BCC;
9006 else
9007 ip->insn_opcode |= regno << OP_SH_CCC;
9008 continue;
9009
9010 case 'H':
9011 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9012 s += 2;
9013 if (ISDIGIT (*s))
9014 {
9015 c = 0;
9016 do
9017 {
9018 c *= 10;
9019 c += *s - '0';
9020 ++s;
9021 }
9022 while (ISDIGIT (*s));
9023 }
9024 else
9025 c = 8; /* Invalid sel value. */
9026
9027 if (c > 7)
9028 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9029 ip->insn_opcode |= c;
9030 continue;
9031
9032 case 'e':
9033 /* Must be at least one digit. */
9034 my_getExpression (&imm_expr, s);
9035 check_absolute_expr (ip, &imm_expr);
9036
9037 if ((unsigned long) imm_expr.X_add_number
9038 > (unsigned long) OP_MASK_VECBYTE)
9039 {
9040 as_bad (_("bad byte vector index (%ld)"),
9041 (long) imm_expr.X_add_number);
9042 imm_expr.X_add_number = 0;
9043 }
9044
9045 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9046 imm_expr.X_op = O_absent;
9047 s = expr_end;
9048 continue;
9049
9050 case '%':
9051 my_getExpression (&imm_expr, s);
9052 check_absolute_expr (ip, &imm_expr);
9053
9054 if ((unsigned long) imm_expr.X_add_number
9055 > (unsigned long) OP_MASK_VECALIGN)
9056 {
9057 as_bad (_("bad byte vector index (%ld)"),
9058 (long) imm_expr.X_add_number);
9059 imm_expr.X_add_number = 0;
9060 }
9061
9062 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9063 imm_expr.X_op = O_absent;
9064 s = expr_end;
9065 continue;
9066
9067 default:
9068 as_bad (_("bad char = '%c'\n"), *args);
9069 internalError ();
9070 }
9071 break;
9072 }
9073 /* Args don't match. */
9074 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9075 !strcmp (insn->name, insn[1].name))
9076 {
9077 ++insn;
9078 s = argsStart;
9079 insn_error = _("illegal operands");
9080 continue;
9081 }
9082 if (save_c)
9083 *(--s) = save_c;
9084 insn_error = _("illegal operands");
9085 return;
9086 }
9087 }
9088
9089 /* This routine assembles an instruction into its binary format when
9090 assembling for the mips16. As a side effect, it sets one of the
9091 global variables imm_reloc or offset_reloc to the type of
9092 relocation to do if one of the operands is an address expression.
9093 It also sets mips16_small and mips16_ext if the user explicitly
9094 requested a small or extended instruction. */
9095
9096 static void
9097 mips16_ip (char *str, struct mips_cl_insn *ip)
9098 {
9099 char *s;
9100 const char *args;
9101 struct mips_opcode *insn;
9102 char *argsstart;
9103 unsigned int regno;
9104 unsigned int lastregno = 0;
9105 char *s_reset;
9106 size_t i;
9107
9108 insn_error = NULL;
9109
9110 mips16_small = FALSE;
9111 mips16_ext = FALSE;
9112
9113 for (s = str; ISLOWER (*s); ++s)
9114 ;
9115 switch (*s)
9116 {
9117 case '\0':
9118 break;
9119
9120 case ' ':
9121 *s++ = '\0';
9122 break;
9123
9124 case '.':
9125 if (s[1] == 't' && s[2] == ' ')
9126 {
9127 *s = '\0';
9128 mips16_small = TRUE;
9129 s += 3;
9130 break;
9131 }
9132 else if (s[1] == 'e' && s[2] == ' ')
9133 {
9134 *s = '\0';
9135 mips16_ext = TRUE;
9136 s += 3;
9137 break;
9138 }
9139 /* Fall through. */
9140 default:
9141 insn_error = _("unknown opcode");
9142 return;
9143 }
9144
9145 if (mips_opts.noautoextend && ! mips16_ext)
9146 mips16_small = TRUE;
9147
9148 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9149 {
9150 insn_error = _("unrecognized opcode");
9151 return;
9152 }
9153
9154 argsstart = s;
9155 for (;;)
9156 {
9157 assert (strcmp (insn->name, str) == 0);
9158
9159 ip->insn_mo = insn;
9160 ip->insn_opcode = insn->match;
9161 ip->use_extend = FALSE;
9162 imm_expr.X_op = O_absent;
9163 imm_reloc[0] = BFD_RELOC_UNUSED;
9164 imm_reloc[1] = BFD_RELOC_UNUSED;
9165 imm_reloc[2] = BFD_RELOC_UNUSED;
9166 imm2_expr.X_op = O_absent;
9167 offset_expr.X_op = O_absent;
9168 offset_reloc[0] = BFD_RELOC_UNUSED;
9169 offset_reloc[1] = BFD_RELOC_UNUSED;
9170 offset_reloc[2] = BFD_RELOC_UNUSED;
9171 for (args = insn->args; 1; ++args)
9172 {
9173 int c;
9174
9175 if (*s == ' ')
9176 ++s;
9177
9178 /* In this switch statement we call break if we did not find
9179 a match, continue if we did find a match, or return if we
9180 are done. */
9181
9182 c = *args;
9183 switch (c)
9184 {
9185 case '\0':
9186 if (*s == '\0')
9187 {
9188 /* Stuff the immediate value in now, if we can. */
9189 if (imm_expr.X_op == O_constant
9190 && *imm_reloc > BFD_RELOC_UNUSED
9191 && insn->pinfo != INSN_MACRO)
9192 {
9193 valueT tmp;
9194
9195 switch (*offset_reloc)
9196 {
9197 case BFD_RELOC_MIPS16_HI16_S:
9198 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9199 break;
9200
9201 case BFD_RELOC_MIPS16_HI16:
9202 tmp = imm_expr.X_add_number >> 16;
9203 break;
9204
9205 case BFD_RELOC_MIPS16_LO16:
9206 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9207 - 0x8000;
9208 break;
9209
9210 case BFD_RELOC_UNUSED:
9211 tmp = imm_expr.X_add_number;
9212 break;
9213
9214 default:
9215 internalError ();
9216 }
9217 *offset_reloc = BFD_RELOC_UNUSED;
9218
9219 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9220 tmp, TRUE, mips16_small,
9221 mips16_ext, &ip->insn_opcode,
9222 &ip->use_extend, &ip->extend);
9223 imm_expr.X_op = O_absent;
9224 *imm_reloc = BFD_RELOC_UNUSED;
9225 }
9226
9227 return;
9228 }
9229 break;
9230
9231 case ',':
9232 if (*s++ == c)
9233 continue;
9234 s--;
9235 switch (*++args)
9236 {
9237 case 'v':
9238 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9239 continue;
9240 case 'w':
9241 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9242 continue;
9243 }
9244 break;
9245
9246 case '(':
9247 case ')':
9248 if (*s++ == c)
9249 continue;
9250 break;
9251
9252 case 'v':
9253 case 'w':
9254 if (s[0] != '$')
9255 {
9256 if (c == 'v')
9257 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9258 else
9259 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9260 ++args;
9261 continue;
9262 }
9263 /* Fall through. */
9264 case 'x':
9265 case 'y':
9266 case 'z':
9267 case 'Z':
9268 case '0':
9269 case 'S':
9270 case 'R':
9271 case 'X':
9272 case 'Y':
9273 if (s[0] != '$')
9274 break;
9275 s_reset = s;
9276 if (ISDIGIT (s[1]))
9277 {
9278 ++s;
9279 regno = 0;
9280 do
9281 {
9282 regno *= 10;
9283 regno += *s - '0';
9284 ++s;
9285 }
9286 while (ISDIGIT (*s));
9287 if (regno > 31)
9288 {
9289 as_bad (_("invalid register number (%d)"), regno);
9290 regno = 2;
9291 }
9292 }
9293 else
9294 {
9295 if (s[1] == 'r' && s[2] == 'a')
9296 {
9297 s += 3;
9298 regno = RA;
9299 }
9300 else if (s[1] == 'f' && s[2] == 'p')
9301 {
9302 s += 3;
9303 regno = FP;
9304 }
9305 else if (s[1] == 's' && s[2] == 'p')
9306 {
9307 s += 3;
9308 regno = SP;
9309 }
9310 else if (s[1] == 'g' && s[2] == 'p')
9311 {
9312 s += 3;
9313 regno = GP;
9314 }
9315 else if (s[1] == 'a' && s[2] == 't')
9316 {
9317 s += 3;
9318 regno = AT;
9319 }
9320 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9321 {
9322 s += 4;
9323 regno = KT0;
9324 }
9325 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9326 {
9327 s += 4;
9328 regno = KT1;
9329 }
9330 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9331 {
9332 s += 5;
9333 regno = ZERO;
9334 }
9335 else
9336 break;
9337 }
9338
9339 if (*s == ' ')
9340 ++s;
9341 if (args[1] != *s)
9342 {
9343 if (c == 'v' || c == 'w')
9344 {
9345 regno = mips16_to_32_reg_map[lastregno];
9346 s = s_reset;
9347 ++args;
9348 }
9349 }
9350
9351 switch (c)
9352 {
9353 case 'x':
9354 case 'y':
9355 case 'z':
9356 case 'v':
9357 case 'w':
9358 case 'Z':
9359 regno = mips32_to_16_reg_map[regno];
9360 break;
9361
9362 case '0':
9363 if (regno != 0)
9364 regno = ILLEGAL_REG;
9365 break;
9366
9367 case 'S':
9368 if (regno != SP)
9369 regno = ILLEGAL_REG;
9370 break;
9371
9372 case 'R':
9373 if (regno != RA)
9374 regno = ILLEGAL_REG;
9375 break;
9376
9377 case 'X':
9378 case 'Y':
9379 if (regno == AT && ! mips_opts.noat)
9380 as_warn (_("used $at without \".set noat\""));
9381 break;
9382
9383 default:
9384 internalError ();
9385 }
9386
9387 if (regno == ILLEGAL_REG)
9388 break;
9389
9390 switch (c)
9391 {
9392 case 'x':
9393 case 'v':
9394 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9395 break;
9396 case 'y':
9397 case 'w':
9398 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9399 break;
9400 case 'z':
9401 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9402 break;
9403 case 'Z':
9404 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9405 case '0':
9406 case 'S':
9407 case 'R':
9408 break;
9409 case 'X':
9410 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9411 break;
9412 case 'Y':
9413 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9414 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9415 break;
9416 default:
9417 internalError ();
9418 }
9419
9420 lastregno = regno;
9421 continue;
9422
9423 case 'P':
9424 if (strncmp (s, "$pc", 3) == 0)
9425 {
9426 s += 3;
9427 continue;
9428 }
9429 break;
9430
9431 case '5':
9432 case 'H':
9433 case 'W':
9434 case 'D':
9435 case 'j':
9436 case 'V':
9437 case 'C':
9438 case 'U':
9439 case 'k':
9440 case 'K':
9441 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9442 if (i > 0)
9443 {
9444 if (imm_expr.X_op != O_constant)
9445 {
9446 mips16_ext = TRUE;
9447 ip->use_extend = TRUE;
9448 ip->extend = 0;
9449 }
9450 else
9451 {
9452 /* We need to relax this instruction. */
9453 *offset_reloc = *imm_reloc;
9454 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9455 }
9456 s = expr_end;
9457 continue;
9458 }
9459 *imm_reloc = BFD_RELOC_UNUSED;
9460 /* Fall through. */
9461 case '<':
9462 case '>':
9463 case '[':
9464 case ']':
9465 case '4':
9466 case '8':
9467 my_getExpression (&imm_expr, s);
9468 if (imm_expr.X_op == O_register)
9469 {
9470 /* What we thought was an expression turned out to
9471 be a register. */
9472
9473 if (s[0] == '(' && args[1] == '(')
9474 {
9475 /* It looks like the expression was omitted
9476 before a register indirection, which means
9477 that the expression is implicitly zero. We
9478 still set up imm_expr, so that we handle
9479 explicit extensions correctly. */
9480 imm_expr.X_op = O_constant;
9481 imm_expr.X_add_number = 0;
9482 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9483 continue;
9484 }
9485
9486 break;
9487 }
9488
9489 /* We need to relax this instruction. */
9490 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9491 s = expr_end;
9492 continue;
9493
9494 case 'p':
9495 case 'q':
9496 case 'A':
9497 case 'B':
9498 case 'E':
9499 /* We use offset_reloc rather than imm_reloc for the PC
9500 relative operands. This lets macros with both
9501 immediate and address operands work correctly. */
9502 my_getExpression (&offset_expr, s);
9503
9504 if (offset_expr.X_op == O_register)
9505 break;
9506
9507 /* We need to relax this instruction. */
9508 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9509 s = expr_end;
9510 continue;
9511
9512 case '6': /* break code */
9513 my_getExpression (&imm_expr, s);
9514 check_absolute_expr (ip, &imm_expr);
9515 if ((unsigned long) imm_expr.X_add_number > 63)
9516 {
9517 as_warn (_("Invalid value for `%s' (%lu)"),
9518 ip->insn_mo->name,
9519 (unsigned long) imm_expr.X_add_number);
9520 imm_expr.X_add_number &= 0x3f;
9521 }
9522 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9523 imm_expr.X_op = O_absent;
9524 s = expr_end;
9525 continue;
9526
9527 case 'a': /* 26 bit address */
9528 my_getExpression (&offset_expr, s);
9529 s = expr_end;
9530 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9531 ip->insn_opcode <<= 16;
9532 continue;
9533
9534 case 'l': /* register list for entry macro */
9535 case 'L': /* register list for exit macro */
9536 {
9537 int mask;
9538
9539 if (c == 'l')
9540 mask = 0;
9541 else
9542 mask = 7 << 3;
9543 while (*s != '\0')
9544 {
9545 int freg, reg1, reg2;
9546
9547 while (*s == ' ' || *s == ',')
9548 ++s;
9549 if (*s != '$')
9550 {
9551 as_bad (_("can't parse register list"));
9552 break;
9553 }
9554 ++s;
9555 if (*s != 'f')
9556 freg = 0;
9557 else
9558 {
9559 freg = 1;
9560 ++s;
9561 }
9562 reg1 = 0;
9563 while (ISDIGIT (*s))
9564 {
9565 reg1 *= 10;
9566 reg1 += *s - '0';
9567 ++s;
9568 }
9569 if (*s == ' ')
9570 ++s;
9571 if (*s != '-')
9572 reg2 = reg1;
9573 else
9574 {
9575 ++s;
9576 if (*s != '$')
9577 break;
9578 ++s;
9579 if (freg)
9580 {
9581 if (*s == 'f')
9582 ++s;
9583 else
9584 {
9585 as_bad (_("invalid register list"));
9586 break;
9587 }
9588 }
9589 reg2 = 0;
9590 while (ISDIGIT (*s))
9591 {
9592 reg2 *= 10;
9593 reg2 += *s - '0';
9594 ++s;
9595 }
9596 }
9597 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9598 {
9599 mask &= ~ (7 << 3);
9600 mask |= 5 << 3;
9601 }
9602 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9603 {
9604 mask &= ~ (7 << 3);
9605 mask |= 6 << 3;
9606 }
9607 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9608 mask |= (reg2 - 3) << 3;
9609 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9610 mask |= (reg2 - 15) << 1;
9611 else if (reg1 == RA && reg2 == RA)
9612 mask |= 1;
9613 else
9614 {
9615 as_bad (_("invalid register list"));
9616 break;
9617 }
9618 }
9619 /* The mask is filled in in the opcode table for the
9620 benefit of the disassembler. We remove it before
9621 applying the actual mask. */
9622 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9623 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9624 }
9625 continue;
9626
9627 case 'e': /* extend code */
9628 my_getExpression (&imm_expr, s);
9629 check_absolute_expr (ip, &imm_expr);
9630 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9631 {
9632 as_warn (_("Invalid value for `%s' (%lu)"),
9633 ip->insn_mo->name,
9634 (unsigned long) imm_expr.X_add_number);
9635 imm_expr.X_add_number &= 0x7ff;
9636 }
9637 ip->insn_opcode |= imm_expr.X_add_number;
9638 imm_expr.X_op = O_absent;
9639 s = expr_end;
9640 continue;
9641
9642 default:
9643 internalError ();
9644 }
9645 break;
9646 }
9647
9648 /* Args don't match. */
9649 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9650 strcmp (insn->name, insn[1].name) == 0)
9651 {
9652 ++insn;
9653 s = argsstart;
9654 continue;
9655 }
9656
9657 insn_error = _("illegal operands");
9658
9659 return;
9660 }
9661 }
9662
9663 /* This structure holds information we know about a mips16 immediate
9664 argument type. */
9665
9666 struct mips16_immed_operand
9667 {
9668 /* The type code used in the argument string in the opcode table. */
9669 int type;
9670 /* The number of bits in the short form of the opcode. */
9671 int nbits;
9672 /* The number of bits in the extended form of the opcode. */
9673 int extbits;
9674 /* The amount by which the short form is shifted when it is used;
9675 for example, the sw instruction has a shift count of 2. */
9676 int shift;
9677 /* The amount by which the short form is shifted when it is stored
9678 into the instruction code. */
9679 int op_shift;
9680 /* Non-zero if the short form is unsigned. */
9681 int unsp;
9682 /* Non-zero if the extended form is unsigned. */
9683 int extu;
9684 /* Non-zero if the value is PC relative. */
9685 int pcrel;
9686 };
9687
9688 /* The mips16 immediate operand types. */
9689
9690 static const struct mips16_immed_operand mips16_immed_operands[] =
9691 {
9692 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9693 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9694 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9695 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9696 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9697 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9698 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9699 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9700 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9701 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9702 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9703 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9704 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9705 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9706 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9707 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9708 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9709 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9710 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9711 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9712 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9713 };
9714
9715 #define MIPS16_NUM_IMMED \
9716 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9717
9718 /* Handle a mips16 instruction with an immediate value. This or's the
9719 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9720 whether an extended value is needed; if one is needed, it sets
9721 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9722 If SMALL is true, an unextended opcode was explicitly requested.
9723 If EXT is true, an extended opcode was explicitly requested. If
9724 WARN is true, warn if EXT does not match reality. */
9725
9726 static void
9727 mips16_immed (char *file, unsigned int line, int type, offsetT val,
9728 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9729 unsigned long *insn, bfd_boolean *use_extend,
9730 unsigned short *extend)
9731 {
9732 register const struct mips16_immed_operand *op;
9733 int mintiny, maxtiny;
9734 bfd_boolean needext;
9735
9736 op = mips16_immed_operands;
9737 while (op->type != type)
9738 {
9739 ++op;
9740 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9741 }
9742
9743 if (op->unsp)
9744 {
9745 if (type == '<' || type == '>' || type == '[' || type == ']')
9746 {
9747 mintiny = 1;
9748 maxtiny = 1 << op->nbits;
9749 }
9750 else
9751 {
9752 mintiny = 0;
9753 maxtiny = (1 << op->nbits) - 1;
9754 }
9755 }
9756 else
9757 {
9758 mintiny = - (1 << (op->nbits - 1));
9759 maxtiny = (1 << (op->nbits - 1)) - 1;
9760 }
9761
9762 /* Branch offsets have an implicit 0 in the lowest bit. */
9763 if (type == 'p' || type == 'q')
9764 val /= 2;
9765
9766 if ((val & ((1 << op->shift) - 1)) != 0
9767 || val < (mintiny << op->shift)
9768 || val > (maxtiny << op->shift))
9769 needext = TRUE;
9770 else
9771 needext = FALSE;
9772
9773 if (warn && ext && ! needext)
9774 as_warn_where (file, line,
9775 _("extended operand requested but not required"));
9776 if (small && needext)
9777 as_bad_where (file, line, _("invalid unextended operand value"));
9778
9779 if (small || (! ext && ! needext))
9780 {
9781 int insnval;
9782
9783 *use_extend = FALSE;
9784 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9785 insnval <<= op->op_shift;
9786 *insn |= insnval;
9787 }
9788 else
9789 {
9790 long minext, maxext;
9791 int extval;
9792
9793 if (op->extu)
9794 {
9795 minext = 0;
9796 maxext = (1 << op->extbits) - 1;
9797 }
9798 else
9799 {
9800 minext = - (1 << (op->extbits - 1));
9801 maxext = (1 << (op->extbits - 1)) - 1;
9802 }
9803 if (val < minext || val > maxext)
9804 as_bad_where (file, line,
9805 _("operand value out of range for instruction"));
9806
9807 *use_extend = TRUE;
9808 if (op->extbits == 16)
9809 {
9810 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9811 val &= 0x1f;
9812 }
9813 else if (op->extbits == 15)
9814 {
9815 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9816 val &= 0xf;
9817 }
9818 else
9819 {
9820 extval = ((val & 0x1f) << 6) | (val & 0x20);
9821 val = 0;
9822 }
9823
9824 *extend = (unsigned short) extval;
9825 *insn |= val;
9826 }
9827 }
9828 \f
9829 struct percent_op_match
9830 {
9831 const char *str;
9832 bfd_reloc_code_real_type reloc;
9833 };
9834
9835 static const struct percent_op_match mips_percent_op[] =
9836 {
9837 {"%lo", BFD_RELOC_LO16},
9838 #ifdef OBJ_ELF
9839 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9840 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9841 {"%call16", BFD_RELOC_MIPS_CALL16},
9842 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9843 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9844 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9845 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9846 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9847 {"%got", BFD_RELOC_MIPS_GOT16},
9848 {"%gp_rel", BFD_RELOC_GPREL16},
9849 {"%half", BFD_RELOC_16},
9850 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9851 {"%higher", BFD_RELOC_MIPS_HIGHER},
9852 {"%neg", BFD_RELOC_MIPS_SUB},
9853 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
9854 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
9855 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
9856 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
9857 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
9858 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
9859 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
9860 #endif
9861 {"%hi", BFD_RELOC_HI16_S}
9862 };
9863
9864 static const struct percent_op_match mips16_percent_op[] =
9865 {
9866 {"%lo", BFD_RELOC_MIPS16_LO16},
9867 {"%gprel", BFD_RELOC_MIPS16_GPREL},
9868 {"%hi", BFD_RELOC_MIPS16_HI16_S}
9869 };
9870
9871
9872 /* Return true if *STR points to a relocation operator. When returning true,
9873 move *STR over the operator and store its relocation code in *RELOC.
9874 Leave both *STR and *RELOC alone when returning false. */
9875
9876 static bfd_boolean
9877 parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
9878 {
9879 const struct percent_op_match *percent_op;
9880 size_t limit, i;
9881
9882 if (mips_opts.mips16)
9883 {
9884 percent_op = mips16_percent_op;
9885 limit = ARRAY_SIZE (mips16_percent_op);
9886 }
9887 else
9888 {
9889 percent_op = mips_percent_op;
9890 limit = ARRAY_SIZE (mips_percent_op);
9891 }
9892
9893 for (i = 0; i < limit; i++)
9894 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
9895 {
9896 int len = strlen (percent_op[i].str);
9897
9898 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
9899 continue;
9900
9901 *str += strlen (percent_op[i].str);
9902 *reloc = percent_op[i].reloc;
9903
9904 /* Check whether the output BFD supports this relocation.
9905 If not, issue an error and fall back on something safe. */
9906 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
9907 {
9908 as_bad ("relocation %s isn't supported by the current ABI",
9909 percent_op[i].str);
9910 *reloc = BFD_RELOC_UNUSED;
9911 }
9912 return TRUE;
9913 }
9914 return FALSE;
9915 }
9916
9917
9918 /* Parse string STR as a 16-bit relocatable operand. Store the
9919 expression in *EP and the relocations in the array starting
9920 at RELOC. Return the number of relocation operators used.
9921
9922 On exit, EXPR_END points to the first character after the expression. */
9923
9924 static size_t
9925 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9926 char *str)
9927 {
9928 bfd_reloc_code_real_type reversed_reloc[3];
9929 size_t reloc_index, i;
9930 int crux_depth, str_depth;
9931 char *crux;
9932
9933 /* Search for the start of the main expression, recoding relocations
9934 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9935 of the main expression and with CRUX_DEPTH containing the number
9936 of open brackets at that point. */
9937 reloc_index = -1;
9938 str_depth = 0;
9939 do
9940 {
9941 reloc_index++;
9942 crux = str;
9943 crux_depth = str_depth;
9944
9945 /* Skip over whitespace and brackets, keeping count of the number
9946 of brackets. */
9947 while (*str == ' ' || *str == '\t' || *str == '(')
9948 if (*str++ == '(')
9949 str_depth++;
9950 }
9951 while (*str == '%'
9952 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9953 && parse_relocation (&str, &reversed_reloc[reloc_index]));
9954
9955 my_getExpression (ep, crux);
9956 str = expr_end;
9957
9958 /* Match every open bracket. */
9959 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
9960 if (*str++ == ')')
9961 crux_depth--;
9962
9963 if (crux_depth > 0)
9964 as_bad ("unclosed '('");
9965
9966 expr_end = str;
9967
9968 if (reloc_index != 0)
9969 {
9970 prev_reloc_op_frag = frag_now;
9971 for (i = 0; i < reloc_index; i++)
9972 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9973 }
9974
9975 return reloc_index;
9976 }
9977
9978 static void
9979 my_getExpression (expressionS *ep, char *str)
9980 {
9981 char *save_in;
9982 valueT val;
9983
9984 save_in = input_line_pointer;
9985 input_line_pointer = str;
9986 expression (ep);
9987 expr_end = input_line_pointer;
9988 input_line_pointer = save_in;
9989
9990 /* If we are in mips16 mode, and this is an expression based on `.',
9991 then we bump the value of the symbol by 1 since that is how other
9992 text symbols are handled. We don't bother to handle complex
9993 expressions, just `.' plus or minus a constant. */
9994 if (mips_opts.mips16
9995 && ep->X_op == O_symbol
9996 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9997 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9998 && symbol_get_frag (ep->X_add_symbol) == frag_now
9999 && symbol_constant_p (ep->X_add_symbol)
10000 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10001 S_SET_VALUE (ep->X_add_symbol, val + 1);
10002 }
10003
10004 /* Turn a string in input_line_pointer into a floating point constant
10005 of type TYPE, and store the appropriate bytes in *LITP. The number
10006 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10007 returned, or NULL on OK. */
10008
10009 char *
10010 md_atof (int type, char *litP, int *sizeP)
10011 {
10012 int prec;
10013 LITTLENUM_TYPE words[4];
10014 char *t;
10015 int i;
10016
10017 switch (type)
10018 {
10019 case 'f':
10020 prec = 2;
10021 break;
10022
10023 case 'd':
10024 prec = 4;
10025 break;
10026
10027 default:
10028 *sizeP = 0;
10029 return _("bad call to md_atof");
10030 }
10031
10032 t = atof_ieee (input_line_pointer, type, words);
10033 if (t)
10034 input_line_pointer = t;
10035
10036 *sizeP = prec * 2;
10037
10038 if (! target_big_endian)
10039 {
10040 for (i = prec - 1; i >= 0; i--)
10041 {
10042 md_number_to_chars (litP, words[i], 2);
10043 litP += 2;
10044 }
10045 }
10046 else
10047 {
10048 for (i = 0; i < prec; i++)
10049 {
10050 md_number_to_chars (litP, words[i], 2);
10051 litP += 2;
10052 }
10053 }
10054
10055 return NULL;
10056 }
10057
10058 void
10059 md_number_to_chars (char *buf, valueT val, int n)
10060 {
10061 if (target_big_endian)
10062 number_to_chars_bigendian (buf, val, n);
10063 else
10064 number_to_chars_littleendian (buf, val, n);
10065 }
10066 \f
10067 #ifdef OBJ_ELF
10068 static int support_64bit_objects(void)
10069 {
10070 const char **list, **l;
10071 int yes;
10072
10073 list = bfd_target_list ();
10074 for (l = list; *l != NULL; l++)
10075 #ifdef TE_TMIPS
10076 /* This is traditional mips */
10077 if (strcmp (*l, "elf64-tradbigmips") == 0
10078 || strcmp (*l, "elf64-tradlittlemips") == 0)
10079 #else
10080 if (strcmp (*l, "elf64-bigmips") == 0
10081 || strcmp (*l, "elf64-littlemips") == 0)
10082 #endif
10083 break;
10084 yes = (*l != NULL);
10085 free (list);
10086 return yes;
10087 }
10088 #endif /* OBJ_ELF */
10089
10090 const char *md_shortopts = "O::g::G:";
10091
10092 struct option md_longopts[] =
10093 {
10094 /* Options which specify architecture. */
10095 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10096 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10097 {"march", required_argument, NULL, OPTION_MARCH},
10098 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10099 {"mtune", required_argument, NULL, OPTION_MTUNE},
10100 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10101 {"mips0", no_argument, NULL, OPTION_MIPS1},
10102 {"mips1", no_argument, NULL, OPTION_MIPS1},
10103 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10104 {"mips2", no_argument, NULL, OPTION_MIPS2},
10105 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10106 {"mips3", no_argument, NULL, OPTION_MIPS3},
10107 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10108 {"mips4", no_argument, NULL, OPTION_MIPS4},
10109 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10110 {"mips5", no_argument, NULL, OPTION_MIPS5},
10111 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10112 {"mips32", no_argument, NULL, OPTION_MIPS32},
10113 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10114 {"mips64", no_argument, NULL, OPTION_MIPS64},
10115 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10116 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10117 #define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10118 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
10119
10120 /* Options which specify Application Specific Extensions (ASEs). */
10121 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
10122 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10123 {"mips16", no_argument, NULL, OPTION_MIPS16},
10124 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10125 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10126 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10127 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10128 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10129 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10130 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10131 {"mdmx", no_argument, NULL, OPTION_MDMX},
10132 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10133 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10134
10135 /* Old-style architecture options. Don't add more of these. */
10136 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10137 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10138 {"m4650", no_argument, NULL, OPTION_M4650},
10139 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10140 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10141 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10142 {"m4010", no_argument, NULL, OPTION_M4010},
10143 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10144 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10145 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10146 {"m4100", no_argument, NULL, OPTION_M4100},
10147 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10148 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10149 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10150 {"m3900", no_argument, NULL, OPTION_M3900},
10151 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10152 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10153
10154 /* Options which enable bug fixes. */
10155 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10156 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10157 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10158 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10159 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10160 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10161 #define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10162 #define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10163 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10164 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
10165
10166 /* Miscellaneous options. */
10167 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10168 #define OPTION_TRAP (OPTION_MISC_BASE + 0)
10169 {"trap", no_argument, NULL, OPTION_TRAP},
10170 {"no-break", no_argument, NULL, OPTION_TRAP},
10171 #define OPTION_BREAK (OPTION_MISC_BASE + 1)
10172 {"break", no_argument, NULL, OPTION_BREAK},
10173 {"no-trap", no_argument, NULL, OPTION_BREAK},
10174 #define OPTION_EB (OPTION_MISC_BASE + 2)
10175 {"EB", no_argument, NULL, OPTION_EB},
10176 #define OPTION_EL (OPTION_MISC_BASE + 3)
10177 {"EL", no_argument, NULL, OPTION_EL},
10178 #define OPTION_FP32 (OPTION_MISC_BASE + 4)
10179 {"mfp32", no_argument, NULL, OPTION_FP32},
10180 #define OPTION_GP32 (OPTION_MISC_BASE + 5)
10181 {"mgp32", no_argument, NULL, OPTION_GP32},
10182 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
10183 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10184 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10185 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10186 #define OPTION_FP64 (OPTION_MISC_BASE + 8)
10187 {"mfp64", no_argument, NULL, OPTION_FP64},
10188 #define OPTION_GP64 (OPTION_MISC_BASE + 9)
10189 {"mgp64", no_argument, NULL, OPTION_GP64},
10190 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10191 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10192 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10193 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10194 #define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10195 #define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10196 {"mshared", no_argument, NULL, OPTION_MSHARED},
10197 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
10198 #define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10199 #define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10200 {"msym32", no_argument, NULL, OPTION_MSYM32},
10201 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
10202
10203 /* ELF-specific options. */
10204 #ifdef OBJ_ELF
10205 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
10206 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10207 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10208 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10209 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10210 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10211 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10212 {"xgot", no_argument, NULL, OPTION_XGOT},
10213 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10214 {"mabi", required_argument, NULL, OPTION_MABI},
10215 #define OPTION_32 (OPTION_ELF_BASE + 4)
10216 {"32", no_argument, NULL, OPTION_32},
10217 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10218 {"n32", no_argument, NULL, OPTION_N32},
10219 #define OPTION_64 (OPTION_ELF_BASE + 6)
10220 {"64", no_argument, NULL, OPTION_64},
10221 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10222 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10223 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10224 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10225 #define OPTION_PDR (OPTION_ELF_BASE + 9)
10226 {"mpdr", no_argument, NULL, OPTION_PDR},
10227 #define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10228 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
10229 #endif /* OBJ_ELF */
10230
10231 {NULL, no_argument, NULL, 0}
10232 };
10233 size_t md_longopts_size = sizeof (md_longopts);
10234
10235 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10236 NEW_VALUE. Warn if another value was already specified. Note:
10237 we have to defer parsing the -march and -mtune arguments in order
10238 to handle 'from-abi' correctly, since the ABI might be specified
10239 in a later argument. */
10240
10241 static void
10242 mips_set_option_string (const char **string_ptr, const char *new_value)
10243 {
10244 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10245 as_warn (_("A different %s was already specified, is now %s"),
10246 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10247 new_value);
10248
10249 *string_ptr = new_value;
10250 }
10251
10252 int
10253 md_parse_option (int c, char *arg)
10254 {
10255 switch (c)
10256 {
10257 case OPTION_CONSTRUCT_FLOATS:
10258 mips_disable_float_construction = 0;
10259 break;
10260
10261 case OPTION_NO_CONSTRUCT_FLOATS:
10262 mips_disable_float_construction = 1;
10263 break;
10264
10265 case OPTION_TRAP:
10266 mips_trap = 1;
10267 break;
10268
10269 case OPTION_BREAK:
10270 mips_trap = 0;
10271 break;
10272
10273 case OPTION_EB:
10274 target_big_endian = 1;
10275 break;
10276
10277 case OPTION_EL:
10278 target_big_endian = 0;
10279 break;
10280
10281 case 'O':
10282 if (arg && arg[1] == '0')
10283 mips_optimize = 1;
10284 else
10285 mips_optimize = 2;
10286 break;
10287
10288 case 'g':
10289 if (arg == NULL)
10290 mips_debug = 2;
10291 else
10292 mips_debug = atoi (arg);
10293 /* When the MIPS assembler sees -g or -g2, it does not do
10294 optimizations which limit full symbolic debugging. We take
10295 that to be equivalent to -O0. */
10296 if (mips_debug == 2)
10297 mips_optimize = 1;
10298 break;
10299
10300 case OPTION_MIPS1:
10301 file_mips_isa = ISA_MIPS1;
10302 break;
10303
10304 case OPTION_MIPS2:
10305 file_mips_isa = ISA_MIPS2;
10306 break;
10307
10308 case OPTION_MIPS3:
10309 file_mips_isa = ISA_MIPS3;
10310 break;
10311
10312 case OPTION_MIPS4:
10313 file_mips_isa = ISA_MIPS4;
10314 break;
10315
10316 case OPTION_MIPS5:
10317 file_mips_isa = ISA_MIPS5;
10318 break;
10319
10320 case OPTION_MIPS32:
10321 file_mips_isa = ISA_MIPS32;
10322 break;
10323
10324 case OPTION_MIPS32R2:
10325 file_mips_isa = ISA_MIPS32R2;
10326 break;
10327
10328 case OPTION_MIPS64R2:
10329 file_mips_isa = ISA_MIPS64R2;
10330 break;
10331
10332 case OPTION_MIPS64:
10333 file_mips_isa = ISA_MIPS64;
10334 break;
10335
10336 case OPTION_MTUNE:
10337 mips_set_option_string (&mips_tune_string, arg);
10338 break;
10339
10340 case OPTION_MARCH:
10341 mips_set_option_string (&mips_arch_string, arg);
10342 break;
10343
10344 case OPTION_M4650:
10345 mips_set_option_string (&mips_arch_string, "4650");
10346 mips_set_option_string (&mips_tune_string, "4650");
10347 break;
10348
10349 case OPTION_NO_M4650:
10350 break;
10351
10352 case OPTION_M4010:
10353 mips_set_option_string (&mips_arch_string, "4010");
10354 mips_set_option_string (&mips_tune_string, "4010");
10355 break;
10356
10357 case OPTION_NO_M4010:
10358 break;
10359
10360 case OPTION_M4100:
10361 mips_set_option_string (&mips_arch_string, "4100");
10362 mips_set_option_string (&mips_tune_string, "4100");
10363 break;
10364
10365 case OPTION_NO_M4100:
10366 break;
10367
10368 case OPTION_M3900:
10369 mips_set_option_string (&mips_arch_string, "3900");
10370 mips_set_option_string (&mips_tune_string, "3900");
10371 break;
10372
10373 case OPTION_NO_M3900:
10374 break;
10375
10376 case OPTION_MDMX:
10377 mips_opts.ase_mdmx = 1;
10378 break;
10379
10380 case OPTION_NO_MDMX:
10381 mips_opts.ase_mdmx = 0;
10382 break;
10383
10384 case OPTION_MIPS16:
10385 mips_opts.mips16 = 1;
10386 mips_no_prev_insn (FALSE);
10387 break;
10388
10389 case OPTION_NO_MIPS16:
10390 mips_opts.mips16 = 0;
10391 mips_no_prev_insn (FALSE);
10392 break;
10393
10394 case OPTION_MIPS3D:
10395 mips_opts.ase_mips3d = 1;
10396 break;
10397
10398 case OPTION_NO_MIPS3D:
10399 mips_opts.ase_mips3d = 0;
10400 break;
10401
10402 case OPTION_FIX_VR4120:
10403 mips_fix_vr4120 = 1;
10404 break;
10405
10406 case OPTION_NO_FIX_VR4120:
10407 mips_fix_vr4120 = 0;
10408 break;
10409
10410 case OPTION_RELAX_BRANCH:
10411 mips_relax_branch = 1;
10412 break;
10413
10414 case OPTION_NO_RELAX_BRANCH:
10415 mips_relax_branch = 0;
10416 break;
10417
10418 case OPTION_MSHARED:
10419 mips_in_shared = TRUE;
10420 break;
10421
10422 case OPTION_MNO_SHARED:
10423 mips_in_shared = FALSE;
10424 break;
10425
10426 case OPTION_MSYM32:
10427 mips_opts.sym32 = TRUE;
10428 break;
10429
10430 case OPTION_MNO_SYM32:
10431 mips_opts.sym32 = FALSE;
10432 break;
10433
10434 #ifdef OBJ_ELF
10435 /* When generating ELF code, we permit -KPIC and -call_shared to
10436 select SVR4_PIC, and -non_shared to select no PIC. This is
10437 intended to be compatible with Irix 5. */
10438 case OPTION_CALL_SHARED:
10439 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10440 {
10441 as_bad (_("-call_shared is supported only for ELF format"));
10442 return 0;
10443 }
10444 mips_pic = SVR4_PIC;
10445 mips_abicalls = TRUE;
10446 if (g_switch_seen && g_switch_value != 0)
10447 {
10448 as_bad (_("-G may not be used with SVR4 PIC code"));
10449 return 0;
10450 }
10451 g_switch_value = 0;
10452 break;
10453
10454 case OPTION_NON_SHARED:
10455 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10456 {
10457 as_bad (_("-non_shared is supported only for ELF format"));
10458 return 0;
10459 }
10460 mips_pic = NO_PIC;
10461 mips_abicalls = FALSE;
10462 break;
10463
10464 /* The -xgot option tells the assembler to use 32 offsets when
10465 accessing the got in SVR4_PIC mode. It is for Irix
10466 compatibility. */
10467 case OPTION_XGOT:
10468 mips_big_got = 1;
10469 break;
10470 #endif /* OBJ_ELF */
10471
10472 case 'G':
10473 g_switch_value = atoi (arg);
10474 g_switch_seen = 1;
10475 if (mips_pic == SVR4_PIC && g_switch_value != 0)
10476 {
10477 as_bad (_("-G may not be used with SVR4 PIC code"));
10478 return 0;
10479 }
10480 break;
10481
10482 #ifdef OBJ_ELF
10483 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10484 and -mabi=64. */
10485 case OPTION_32:
10486 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10487 {
10488 as_bad (_("-32 is supported for ELF format only"));
10489 return 0;
10490 }
10491 mips_abi = O32_ABI;
10492 break;
10493
10494 case OPTION_N32:
10495 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10496 {
10497 as_bad (_("-n32 is supported for ELF format only"));
10498 return 0;
10499 }
10500 mips_abi = N32_ABI;
10501 break;
10502
10503 case OPTION_64:
10504 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10505 {
10506 as_bad (_("-64 is supported for ELF format only"));
10507 return 0;
10508 }
10509 mips_abi = N64_ABI;
10510 if (! support_64bit_objects())
10511 as_fatal (_("No compiled in support for 64 bit object file format"));
10512 break;
10513 #endif /* OBJ_ELF */
10514
10515 case OPTION_GP32:
10516 file_mips_gp32 = 1;
10517 break;
10518
10519 case OPTION_GP64:
10520 file_mips_gp32 = 0;
10521 break;
10522
10523 case OPTION_FP32:
10524 file_mips_fp32 = 1;
10525 break;
10526
10527 case OPTION_FP64:
10528 file_mips_fp32 = 0;
10529 break;
10530
10531 #ifdef OBJ_ELF
10532 case OPTION_MABI:
10533 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10534 {
10535 as_bad (_("-mabi is supported for ELF format only"));
10536 return 0;
10537 }
10538 if (strcmp (arg, "32") == 0)
10539 mips_abi = O32_ABI;
10540 else if (strcmp (arg, "o64") == 0)
10541 mips_abi = O64_ABI;
10542 else if (strcmp (arg, "n32") == 0)
10543 mips_abi = N32_ABI;
10544 else if (strcmp (arg, "64") == 0)
10545 {
10546 mips_abi = N64_ABI;
10547 if (! support_64bit_objects())
10548 as_fatal (_("No compiled in support for 64 bit object file "
10549 "format"));
10550 }
10551 else if (strcmp (arg, "eabi") == 0)
10552 mips_abi = EABI_ABI;
10553 else
10554 {
10555 as_fatal (_("invalid abi -mabi=%s"), arg);
10556 return 0;
10557 }
10558 break;
10559 #endif /* OBJ_ELF */
10560
10561 case OPTION_M7000_HILO_FIX:
10562 mips_7000_hilo_fix = TRUE;
10563 break;
10564
10565 case OPTION_MNO_7000_HILO_FIX:
10566 mips_7000_hilo_fix = FALSE;
10567 break;
10568
10569 #ifdef OBJ_ELF
10570 case OPTION_MDEBUG:
10571 mips_flag_mdebug = TRUE;
10572 break;
10573
10574 case OPTION_NO_MDEBUG:
10575 mips_flag_mdebug = FALSE;
10576 break;
10577
10578 case OPTION_PDR:
10579 mips_flag_pdr = TRUE;
10580 break;
10581
10582 case OPTION_NO_PDR:
10583 mips_flag_pdr = FALSE;
10584 break;
10585 #endif /* OBJ_ELF */
10586
10587 default:
10588 return 0;
10589 }
10590
10591 return 1;
10592 }
10593 \f
10594 /* Set up globals to generate code for the ISA or processor
10595 described by INFO. */
10596
10597 static void
10598 mips_set_architecture (const struct mips_cpu_info *info)
10599 {
10600 if (info != 0)
10601 {
10602 file_mips_arch = info->cpu;
10603 mips_opts.arch = info->cpu;
10604 mips_opts.isa = info->isa;
10605 }
10606 }
10607
10608
10609 /* Likewise for tuning. */
10610
10611 static void
10612 mips_set_tune (const struct mips_cpu_info *info)
10613 {
10614 if (info != 0)
10615 mips_tune = info->cpu;
10616 }
10617
10618
10619 void
10620 mips_after_parse_args (void)
10621 {
10622 const struct mips_cpu_info *arch_info = 0;
10623 const struct mips_cpu_info *tune_info = 0;
10624
10625 /* GP relative stuff not working for PE */
10626 if (strncmp (TARGET_OS, "pe", 2) == 0)
10627 {
10628 if (g_switch_seen && g_switch_value != 0)
10629 as_bad (_("-G not supported in this configuration."));
10630 g_switch_value = 0;
10631 }
10632
10633 if (mips_abi == NO_ABI)
10634 mips_abi = MIPS_DEFAULT_ABI;
10635
10636 /* The following code determines the architecture and register size.
10637 Similar code was added to GCC 3.3 (see override_options() in
10638 config/mips/mips.c). The GAS and GCC code should be kept in sync
10639 as much as possible. */
10640
10641 if (mips_arch_string != 0)
10642 arch_info = mips_parse_cpu ("-march", mips_arch_string);
10643
10644 if (file_mips_isa != ISA_UNKNOWN)
10645 {
10646 /* Handle -mipsN. At this point, file_mips_isa contains the
10647 ISA level specified by -mipsN, while arch_info->isa contains
10648 the -march selection (if any). */
10649 if (arch_info != 0)
10650 {
10651 /* -march takes precedence over -mipsN, since it is more descriptive.
10652 There's no harm in specifying both as long as the ISA levels
10653 are the same. */
10654 if (file_mips_isa != arch_info->isa)
10655 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10656 mips_cpu_info_from_isa (file_mips_isa)->name,
10657 mips_cpu_info_from_isa (arch_info->isa)->name);
10658 }
10659 else
10660 arch_info = mips_cpu_info_from_isa (file_mips_isa);
10661 }
10662
10663 if (arch_info == 0)
10664 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
10665
10666 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
10667 as_bad ("-march=%s is not compatible with the selected ABI",
10668 arch_info->name);
10669
10670 mips_set_architecture (arch_info);
10671
10672 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10673 if (mips_tune_string != 0)
10674 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
10675
10676 if (tune_info == 0)
10677 mips_set_tune (arch_info);
10678 else
10679 mips_set_tune (tune_info);
10680
10681 if (file_mips_gp32 >= 0)
10682 {
10683 /* The user specified the size of the integer registers. Make sure
10684 it agrees with the ABI and ISA. */
10685 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10686 as_bad (_("-mgp64 used with a 32-bit processor"));
10687 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10688 as_bad (_("-mgp32 used with a 64-bit ABI"));
10689 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10690 as_bad (_("-mgp64 used with a 32-bit ABI"));
10691 }
10692 else
10693 {
10694 /* Infer the integer register size from the ABI and processor.
10695 Restrict ourselves to 32-bit registers if that's all the
10696 processor has, or if the ABI cannot handle 64-bit registers. */
10697 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10698 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
10699 }
10700
10701 /* ??? GAS treats single-float processors as though they had 64-bit
10702 float registers (although it complains when double-precision
10703 instructions are used). As things stand, saying they have 32-bit
10704 registers would lead to spurious "register must be even" messages.
10705 So here we assume float registers are always the same size as
10706 integer ones, unless the user says otherwise. */
10707 if (file_mips_fp32 < 0)
10708 file_mips_fp32 = file_mips_gp32;
10709
10710 /* End of GCC-shared inference code. */
10711
10712 /* This flag is set when we have a 64-bit capable CPU but use only
10713 32-bit wide registers. Note that EABI does not use it. */
10714 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10715 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10716 || mips_abi == O32_ABI))
10717 mips_32bitmode = 1;
10718
10719 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10720 as_bad (_("trap exception not supported at ISA 1"));
10721
10722 /* If the selected architecture includes support for ASEs, enable
10723 generation of code for them. */
10724 if (mips_opts.mips16 == -1)
10725 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
10726 if (mips_opts.ase_mips3d == -1)
10727 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
10728 if (mips_opts.ase_mdmx == -1)
10729 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
10730
10731 file_mips_isa = mips_opts.isa;
10732 file_ase_mips16 = mips_opts.mips16;
10733 file_ase_mips3d = mips_opts.ase_mips3d;
10734 file_ase_mdmx = mips_opts.ase_mdmx;
10735 mips_opts.gp32 = file_mips_gp32;
10736 mips_opts.fp32 = file_mips_fp32;
10737
10738 if (mips_flag_mdebug < 0)
10739 {
10740 #ifdef OBJ_MAYBE_ECOFF
10741 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10742 mips_flag_mdebug = 1;
10743 else
10744 #endif /* OBJ_MAYBE_ECOFF */
10745 mips_flag_mdebug = 0;
10746 }
10747 }
10748 \f
10749 void
10750 mips_init_after_args (void)
10751 {
10752 /* initialize opcodes */
10753 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10754 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10755 }
10756
10757 long
10758 md_pcrel_from (fixS *fixP)
10759 {
10760 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10761 switch (fixP->fx_r_type)
10762 {
10763 case BFD_RELOC_16_PCREL_S2:
10764 case BFD_RELOC_MIPS_JMP:
10765 /* Return the address of the delay slot. */
10766 return addr + 4;
10767 default:
10768 return addr;
10769 }
10770 }
10771
10772 /* This is called before the symbol table is processed. In order to
10773 work with gcc when using mips-tfile, we must keep all local labels.
10774 However, in other cases, we want to discard them. If we were
10775 called with -g, but we didn't see any debugging information, it may
10776 mean that gcc is smuggling debugging information through to
10777 mips-tfile, in which case we must generate all local labels. */
10778
10779 void
10780 mips_frob_file_before_adjust (void)
10781 {
10782 #ifndef NO_ECOFF_DEBUGGING
10783 if (ECOFF_DEBUGGING
10784 && mips_debug != 0
10785 && ! ecoff_debugging_seen)
10786 flag_keep_locals = 1;
10787 #endif
10788 }
10789
10790 /* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10791 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10792 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10793 relocation operators.
10794
10795 For our purposes, a %lo() expression matches a %got() or %hi()
10796 expression if:
10797
10798 (a) it refers to the same symbol; and
10799 (b) the offset applied in the %lo() expression is no lower than
10800 the offset applied in the %got() or %hi().
10801
10802 (b) allows us to cope with code like:
10803
10804 lui $4,%hi(foo)
10805 lh $4,%lo(foo+2)($4)
10806
10807 ...which is legal on RELA targets, and has a well-defined behaviour
10808 if the user knows that adding 2 to "foo" will not induce a carry to
10809 the high 16 bits.
10810
10811 When several %lo()s match a particular %got() or %hi(), we use the
10812 following rules to distinguish them:
10813
10814 (1) %lo()s with smaller offsets are a better match than %lo()s with
10815 higher offsets.
10816
10817 (2) %lo()s with no matching %got() or %hi() are better than those
10818 that already have a matching %got() or %hi().
10819
10820 (3) later %lo()s are better than earlier %lo()s.
10821
10822 These rules are applied in order.
10823
10824 (1) means, among other things, that %lo()s with identical offsets are
10825 chosen if they exist.
10826
10827 (2) means that we won't associate several high-part relocations with
10828 the same low-part relocation unless there's no alternative. Having
10829 several high parts for the same low part is a GNU extension; this rule
10830 allows careful users to avoid it.
10831
10832 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10833 with the last high-part relocation being at the front of the list.
10834 It therefore makes sense to choose the last matching low-part
10835 relocation, all other things being equal. It's also easier
10836 to code that way. */
10837
10838 void
10839 mips_frob_file (void)
10840 {
10841 struct mips_hi_fixup *l;
10842
10843 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10844 {
10845 segment_info_type *seginfo;
10846 bfd_boolean matched_lo_p;
10847 fixS **hi_pos, **lo_pos, **pos;
10848
10849 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
10850
10851 /* If a GOT16 relocation turns out to be against a global symbol,
10852 there isn't supposed to be a matching LO. */
10853 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10854 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10855 continue;
10856
10857 /* Check quickly whether the next fixup happens to be a matching %lo. */
10858 if (fixup_has_matching_lo_p (l->fixp))
10859 continue;
10860
10861 seginfo = seg_info (l->seg);
10862
10863 /* Set HI_POS to the position of this relocation in the chain.
10864 Set LO_POS to the position of the chosen low-part relocation.
10865 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10866 relocation that matches an immediately-preceding high-part
10867 relocation. */
10868 hi_pos = NULL;
10869 lo_pos = NULL;
10870 matched_lo_p = FALSE;
10871 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10872 {
10873 if (*pos == l->fixp)
10874 hi_pos = pos;
10875
10876 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10877 && (*pos)->fx_addsy == l->fixp->fx_addsy
10878 && (*pos)->fx_offset >= l->fixp->fx_offset
10879 && (lo_pos == NULL
10880 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10881 || (!matched_lo_p
10882 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10883 lo_pos = pos;
10884
10885 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10886 && fixup_has_matching_lo_p (*pos));
10887 }
10888
10889 /* If we found a match, remove the high-part relocation from its
10890 current position and insert it before the low-part relocation.
10891 Make the offsets match so that fixup_has_matching_lo_p()
10892 will return true.
10893
10894 We don't warn about unmatched high-part relocations since some
10895 versions of gcc have been known to emit dead "lui ...%hi(...)"
10896 instructions. */
10897 if (lo_pos != NULL)
10898 {
10899 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10900 if (l->fixp->fx_next != *lo_pos)
10901 {
10902 *hi_pos = l->fixp->fx_next;
10903 l->fixp->fx_next = *lo_pos;
10904 *lo_pos = l->fixp;
10905 }
10906 }
10907 }
10908 }
10909
10910 /* We may have combined relocations without symbols in the N32/N64 ABI.
10911 We have to prevent gas from dropping them. */
10912
10913 int
10914 mips_force_relocation (fixS *fixp)
10915 {
10916 if (generic_force_reloc (fixp))
10917 return 1;
10918
10919 if (HAVE_NEWABI
10920 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10921 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10922 || fixp->fx_r_type == BFD_RELOC_HI16_S
10923 || fixp->fx_r_type == BFD_RELOC_LO16))
10924 return 1;
10925
10926 return 0;
10927 }
10928
10929 /* This hook is called before a fix is simplified. We don't really
10930 decide whether to skip a fix here. Rather, we turn global symbols
10931 used as branch targets into local symbols, such that they undergo
10932 simplification. We can only do this if the symbol is defined and
10933 it is in the same section as the branch. If this doesn't hold, we
10934 emit a better error message than just saying the relocation is not
10935 valid for the selected object format.
10936
10937 FIXP is the fix-up we're going to try to simplify, SEG is the
10938 segment in which the fix up occurs. The return value should be
10939 non-zero to indicate the fix-up is valid for further
10940 simplifications. */
10941
10942 int
10943 mips_validate_fix (struct fix *fixP, asection *seg)
10944 {
10945 /* There's a lot of discussion on whether it should be possible to
10946 use R_MIPS_PC16 to represent branch relocations. The outcome
10947 seems to be that it can, but gas/bfd are very broken in creating
10948 RELA relocations for this, so for now we only accept branches to
10949 symbols in the same section. Anything else is of dubious value,
10950 since there's no guarantee that at link time the symbol would be
10951 in range. Even for branches to local symbols this is arguably
10952 wrong, since it we assume the symbol is not going to be
10953 overridden, which should be possible per ELF library semantics,
10954 but then, there isn't a dynamic relocation that could be used to
10955 this effect, and the target would likely be out of range as well.
10956
10957 Unfortunately, it seems that there is too much code out there
10958 that relies on branches to symbols that are global to be resolved
10959 as if they were local, like the IRIX tools do, so we do it as
10960 well, but with a warning so that people are reminded to fix their
10961 code. If we ever get back to using R_MIPS_PC16 for branch
10962 targets, this entire block should go away (and probably the
10963 whole function). */
10964
10965 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
10966 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10967 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
10968 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10969 && fixP->fx_addsy)
10970 {
10971 if (! S_IS_DEFINED (fixP->fx_addsy))
10972 {
10973 as_bad_where (fixP->fx_file, fixP->fx_line,
10974 _("Cannot branch to undefined symbol."));
10975 /* Avoid any further errors about this fixup. */
10976 fixP->fx_done = 1;
10977 }
10978 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10979 {
10980 as_bad_where (fixP->fx_file, fixP->fx_line,
10981 _("Cannot branch to symbol in another section."));
10982 fixP->fx_done = 1;
10983 }
10984 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10985 {
10986 symbolS *sym = fixP->fx_addsy;
10987
10988 if (mips_pic == SVR4_PIC)
10989 as_warn_where (fixP->fx_file, fixP->fx_line,
10990 _("Pretending global symbol used as branch target is local."));
10991
10992 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10993 S_GET_SEGMENT (sym),
10994 S_GET_VALUE (sym),
10995 symbol_get_frag (sym));
10996 copy_symbol_attributes (fixP->fx_addsy, sym);
10997 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10998 assert (symbol_resolved_p (sym));
10999 symbol_mark_resolved (fixP->fx_addsy);
11000 }
11001 }
11002
11003 return 1;
11004 }
11005
11006 /* Apply a fixup to the object file. */
11007
11008 void
11009 md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
11010 {
11011 bfd_byte *buf;
11012 long insn;
11013 reloc_howto_type *howto;
11014
11015 /* We ignore generic BFD relocations we don't know about. */
11016 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11017 if (! howto)
11018 return;
11019
11020 assert (fixP->fx_size == 4
11021 || fixP->fx_r_type == BFD_RELOC_16
11022 || fixP->fx_r_type == BFD_RELOC_64
11023 || fixP->fx_r_type == BFD_RELOC_CTOR
11024 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11025 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11026 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11027
11028 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11029
11030 assert (! fixP->fx_pcrel);
11031
11032 /* Don't treat parts of a composite relocation as done. There are two
11033 reasons for this:
11034
11035 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11036 should nevertheless be emitted if the first part is.
11037
11038 (2) In normal usage, composite relocations are never assembly-time
11039 constants. The easiest way of dealing with the pathological
11040 exceptions is to generate a relocation against STN_UNDEF and
11041 leave everything up to the linker. */
11042 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
11043 fixP->fx_done = 1;
11044
11045 switch (fixP->fx_r_type)
11046 {
11047 case BFD_RELOC_MIPS_TLS_GD:
11048 case BFD_RELOC_MIPS_TLS_LDM:
11049 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11050 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11051 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11052 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11053 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11054 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11055 /* fall through */
11056
11057 case BFD_RELOC_MIPS_JMP:
11058 case BFD_RELOC_MIPS_SHIFT5:
11059 case BFD_RELOC_MIPS_SHIFT6:
11060 case BFD_RELOC_MIPS_GOT_DISP:
11061 case BFD_RELOC_MIPS_GOT_PAGE:
11062 case BFD_RELOC_MIPS_GOT_OFST:
11063 case BFD_RELOC_MIPS_SUB:
11064 case BFD_RELOC_MIPS_INSERT_A:
11065 case BFD_RELOC_MIPS_INSERT_B:
11066 case BFD_RELOC_MIPS_DELETE:
11067 case BFD_RELOC_MIPS_HIGHEST:
11068 case BFD_RELOC_MIPS_HIGHER:
11069 case BFD_RELOC_MIPS_SCN_DISP:
11070 case BFD_RELOC_MIPS_REL16:
11071 case BFD_RELOC_MIPS_RELGOT:
11072 case BFD_RELOC_MIPS_JALR:
11073 case BFD_RELOC_HI16:
11074 case BFD_RELOC_HI16_S:
11075 case BFD_RELOC_GPREL16:
11076 case BFD_RELOC_MIPS_LITERAL:
11077 case BFD_RELOC_MIPS_CALL16:
11078 case BFD_RELOC_MIPS_GOT16:
11079 case BFD_RELOC_GPREL32:
11080 case BFD_RELOC_MIPS_GOT_HI16:
11081 case BFD_RELOC_MIPS_GOT_LO16:
11082 case BFD_RELOC_MIPS_CALL_HI16:
11083 case BFD_RELOC_MIPS_CALL_LO16:
11084 case BFD_RELOC_MIPS16_GPREL:
11085 case BFD_RELOC_MIPS16_HI16:
11086 case BFD_RELOC_MIPS16_HI16_S:
11087 assert (! fixP->fx_pcrel);
11088 /* Nothing needed to do. The value comes from the reloc entry */
11089 break;
11090
11091 case BFD_RELOC_MIPS16_JMP:
11092 /* We currently always generate a reloc against a symbol, which
11093 means that we don't want an addend even if the symbol is
11094 defined. */
11095 *valP = 0;
11096 break;
11097
11098 case BFD_RELOC_64:
11099 /* This is handled like BFD_RELOC_32, but we output a sign
11100 extended value if we are only 32 bits. */
11101 if (fixP->fx_done)
11102 {
11103 if (8 <= sizeof (valueT))
11104 md_number_to_chars ((char *) buf, *valP, 8);
11105 else
11106 {
11107 valueT hiv;
11108
11109 if ((*valP & 0x80000000) != 0)
11110 hiv = 0xffffffff;
11111 else
11112 hiv = 0;
11113 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11114 *valP, 4);
11115 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11116 hiv, 4);
11117 }
11118 }
11119 break;
11120
11121 case BFD_RELOC_RVA:
11122 case BFD_RELOC_32:
11123 /* If we are deleting this reloc entry, we must fill in the
11124 value now. This can happen if we have a .word which is not
11125 resolved when it appears but is later defined. */
11126 if (fixP->fx_done)
11127 md_number_to_chars ((char *) buf, *valP, 4);
11128 break;
11129
11130 case BFD_RELOC_16:
11131 /* If we are deleting this reloc entry, we must fill in the
11132 value now. */
11133 if (fixP->fx_done)
11134 md_number_to_chars ((char *) buf, *valP, 2);
11135 break;
11136
11137 case BFD_RELOC_LO16:
11138 case BFD_RELOC_MIPS16_LO16:
11139 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11140 may be safe to remove, but if so it's not obvious. */
11141 /* When handling an embedded PIC switch statement, we can wind
11142 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11143 if (fixP->fx_done)
11144 {
11145 if (*valP + 0x8000 > 0xffff)
11146 as_bad_where (fixP->fx_file, fixP->fx_line,
11147 _("relocation overflow"));
11148 if (target_big_endian)
11149 buf += 2;
11150 md_number_to_chars ((char *) buf, *valP, 2);
11151 }
11152 break;
11153
11154 case BFD_RELOC_16_PCREL_S2:
11155 if ((*valP & 0x3) != 0)
11156 as_bad_where (fixP->fx_file, fixP->fx_line,
11157 _("Branch to odd address (%lx)"), (long) *valP);
11158
11159 /*
11160 * We need to save the bits in the instruction since fixup_segment()
11161 * might be deleting the relocation entry (i.e., a branch within
11162 * the current segment).
11163 */
11164 if (! fixP->fx_done)
11165 break;
11166
11167 /* update old instruction data */
11168 if (target_big_endian)
11169 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11170 else
11171 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11172
11173 if (*valP + 0x20000 <= 0x3ffff)
11174 {
11175 insn |= (*valP >> 2) & 0xffff;
11176 md_number_to_chars ((char *) buf, insn, 4);
11177 }
11178 else if (mips_pic == NO_PIC
11179 && fixP->fx_done
11180 && fixP->fx_frag->fr_address >= text_section->vma
11181 && (fixP->fx_frag->fr_address
11182 < text_section->vma + bfd_get_section_size (text_section))
11183 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11184 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11185 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11186 {
11187 /* The branch offset is too large. If this is an
11188 unconditional branch, and we are not generating PIC code,
11189 we can convert it to an absolute jump instruction. */
11190 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11191 insn = 0x0c000000; /* jal */
11192 else
11193 insn = 0x08000000; /* j */
11194 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11195 fixP->fx_done = 0;
11196 fixP->fx_addsy = section_symbol (text_section);
11197 *valP += md_pcrel_from (fixP);
11198 md_number_to_chars ((char *) buf, insn, 4);
11199 }
11200 else
11201 {
11202 /* If we got here, we have branch-relaxation disabled,
11203 and there's nothing we can do to fix this instruction
11204 without turning it into a longer sequence. */
11205 as_bad_where (fixP->fx_file, fixP->fx_line,
11206 _("Branch out of range"));
11207 }
11208 break;
11209
11210 case BFD_RELOC_VTABLE_INHERIT:
11211 fixP->fx_done = 0;
11212 if (fixP->fx_addsy
11213 && !S_IS_DEFINED (fixP->fx_addsy)
11214 && !S_IS_WEAK (fixP->fx_addsy))
11215 S_SET_WEAK (fixP->fx_addsy);
11216 break;
11217
11218 case BFD_RELOC_VTABLE_ENTRY:
11219 fixP->fx_done = 0;
11220 break;
11221
11222 default:
11223 internalError ();
11224 }
11225
11226 /* Remember value for tc_gen_reloc. */
11227 fixP->fx_addnumber = *valP;
11228 }
11229
11230 static symbolS *
11231 get_symbol (void)
11232 {
11233 int c;
11234 char *name;
11235 symbolS *p;
11236
11237 name = input_line_pointer;
11238 c = get_symbol_end ();
11239 p = (symbolS *) symbol_find_or_make (name);
11240 *input_line_pointer = c;
11241 return p;
11242 }
11243
11244 /* Align the current frag to a given power of two. The MIPS assembler
11245 also automatically adjusts any preceding label. */
11246
11247 static void
11248 mips_align (int to, int fill, symbolS *label)
11249 {
11250 mips_emit_delays (FALSE);
11251 frag_align (to, fill, 0);
11252 record_alignment (now_seg, to);
11253 if (label != NULL)
11254 {
11255 assert (S_GET_SEGMENT (label) == now_seg);
11256 symbol_set_frag (label, frag_now);
11257 S_SET_VALUE (label, (valueT) frag_now_fix ());
11258 }
11259 }
11260
11261 /* Align to a given power of two. .align 0 turns off the automatic
11262 alignment used by the data creating pseudo-ops. */
11263
11264 static void
11265 s_align (int x ATTRIBUTE_UNUSED)
11266 {
11267 register int temp;
11268 register long temp_fill;
11269 long max_alignment = 15;
11270
11271 /*
11272
11273 o Note that the assembler pulls down any immediately preceding label
11274 to the aligned address.
11275 o It's not documented but auto alignment is reinstated by
11276 a .align pseudo instruction.
11277 o Note also that after auto alignment is turned off the mips assembler
11278 issues an error on attempt to assemble an improperly aligned data item.
11279 We don't.
11280
11281 */
11282
11283 temp = get_absolute_expression ();
11284 if (temp > max_alignment)
11285 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11286 else if (temp < 0)
11287 {
11288 as_warn (_("Alignment negative: 0 assumed."));
11289 temp = 0;
11290 }
11291 if (*input_line_pointer == ',')
11292 {
11293 ++input_line_pointer;
11294 temp_fill = get_absolute_expression ();
11295 }
11296 else
11297 temp_fill = 0;
11298 if (temp)
11299 {
11300 auto_align = 1;
11301 mips_align (temp, (int) temp_fill,
11302 insn_labels != NULL ? insn_labels->label : NULL);
11303 }
11304 else
11305 {
11306 auto_align = 0;
11307 }
11308
11309 demand_empty_rest_of_line ();
11310 }
11311
11312 void
11313 mips_flush_pending_output (void)
11314 {
11315 mips_emit_delays (FALSE);
11316 mips_clear_insn_labels ();
11317 }
11318
11319 static void
11320 s_change_sec (int sec)
11321 {
11322 segT seg;
11323
11324 #ifdef OBJ_ELF
11325 /* The ELF backend needs to know that we are changing sections, so
11326 that .previous works correctly. We could do something like check
11327 for an obj_section_change_hook macro, but that might be confusing
11328 as it would not be appropriate to use it in the section changing
11329 functions in read.c, since obj-elf.c intercepts those. FIXME:
11330 This should be cleaner, somehow. */
11331 obj_elf_section_change_hook ();
11332 #endif
11333
11334 mips_emit_delays (FALSE);
11335 switch (sec)
11336 {
11337 case 't':
11338 s_text (0);
11339 break;
11340 case 'd':
11341 s_data (0);
11342 break;
11343 case 'b':
11344 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11345 demand_empty_rest_of_line ();
11346 break;
11347
11348 case 'r':
11349 seg = subseg_new (RDATA_SECTION_NAME,
11350 (subsegT) get_absolute_expression ());
11351 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11352 {
11353 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11354 | SEC_READONLY | SEC_RELOC
11355 | SEC_DATA));
11356 if (strcmp (TARGET_OS, "elf") != 0)
11357 record_alignment (seg, 4);
11358 }
11359 demand_empty_rest_of_line ();
11360 break;
11361
11362 case 's':
11363 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11364 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11365 {
11366 bfd_set_section_flags (stdoutput, seg,
11367 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11368 if (strcmp (TARGET_OS, "elf") != 0)
11369 record_alignment (seg, 4);
11370 }
11371 demand_empty_rest_of_line ();
11372 break;
11373 }
11374
11375 auto_align = 1;
11376 }
11377
11378 void
11379 s_change_section (int ignore ATTRIBUTE_UNUSED)
11380 {
11381 #ifdef OBJ_ELF
11382 char *section_name;
11383 char c;
11384 char next_c = 0;
11385 int section_type;
11386 int section_flag;
11387 int section_entry_size;
11388 int section_alignment;
11389
11390 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11391 return;
11392
11393 section_name = input_line_pointer;
11394 c = get_symbol_end ();
11395 if (c)
11396 next_c = *(input_line_pointer + 1);
11397
11398 /* Do we have .section Name<,"flags">? */
11399 if (c != ',' || (c == ',' && next_c == '"'))
11400 {
11401 /* just after name is now '\0'. */
11402 *input_line_pointer = c;
11403 input_line_pointer = section_name;
11404 obj_elf_section (ignore);
11405 return;
11406 }
11407 input_line_pointer++;
11408
11409 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11410 if (c == ',')
11411 section_type = get_absolute_expression ();
11412 else
11413 section_type = 0;
11414 if (*input_line_pointer++ == ',')
11415 section_flag = get_absolute_expression ();
11416 else
11417 section_flag = 0;
11418 if (*input_line_pointer++ == ',')
11419 section_entry_size = get_absolute_expression ();
11420 else
11421 section_entry_size = 0;
11422 if (*input_line_pointer++ == ',')
11423 section_alignment = get_absolute_expression ();
11424 else
11425 section_alignment = 0;
11426
11427 section_name = xstrdup (section_name);
11428
11429 /* When using the generic form of .section (as implemented by obj-elf.c),
11430 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11431 traditionally had to fall back on the more common @progbits instead.
11432
11433 There's nothing really harmful in this, since bfd will correct
11434 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11435 means that, for backwards compatibiltiy, the special_section entries
11436 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11437
11438 Even so, we shouldn't force users of the MIPS .section syntax to
11439 incorrectly label the sections as SHT_PROGBITS. The best compromise
11440 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11441 generic type-checking code. */
11442 if (section_type == SHT_MIPS_DWARF)
11443 section_type = SHT_PROGBITS;
11444
11445 obj_elf_change_section (section_name, section_type, section_flag,
11446 section_entry_size, 0, 0, 0);
11447
11448 if (now_seg->name != section_name)
11449 free (section_name);
11450 #endif /* OBJ_ELF */
11451 }
11452
11453 void
11454 mips_enable_auto_align (void)
11455 {
11456 auto_align = 1;
11457 }
11458
11459 static void
11460 s_cons (int log_size)
11461 {
11462 symbolS *label;
11463
11464 label = insn_labels != NULL ? insn_labels->label : NULL;
11465 mips_emit_delays (FALSE);
11466 if (log_size > 0 && auto_align)
11467 mips_align (log_size, 0, label);
11468 mips_clear_insn_labels ();
11469 cons (1 << log_size);
11470 }
11471
11472 static void
11473 s_float_cons (int type)
11474 {
11475 symbolS *label;
11476
11477 label = insn_labels != NULL ? insn_labels->label : NULL;
11478
11479 mips_emit_delays (FALSE);
11480
11481 if (auto_align)
11482 {
11483 if (type == 'd')
11484 mips_align (3, 0, label);
11485 else
11486 mips_align (2, 0, label);
11487 }
11488
11489 mips_clear_insn_labels ();
11490
11491 float_cons (type);
11492 }
11493
11494 /* Handle .globl. We need to override it because on Irix 5 you are
11495 permitted to say
11496 .globl foo .text
11497 where foo is an undefined symbol, to mean that foo should be
11498 considered to be the address of a function. */
11499
11500 static void
11501 s_mips_globl (int x ATTRIBUTE_UNUSED)
11502 {
11503 char *name;
11504 int c;
11505 symbolS *symbolP;
11506 flagword flag;
11507
11508 name = input_line_pointer;
11509 c = get_symbol_end ();
11510 symbolP = symbol_find_or_make (name);
11511 *input_line_pointer = c;
11512 SKIP_WHITESPACE ();
11513
11514 /* On Irix 5, every global symbol that is not explicitly labelled as
11515 being a function is apparently labelled as being an object. */
11516 flag = BSF_OBJECT;
11517
11518 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11519 {
11520 char *secname;
11521 asection *sec;
11522
11523 secname = input_line_pointer;
11524 c = get_symbol_end ();
11525 sec = bfd_get_section_by_name (stdoutput, secname);
11526 if (sec == NULL)
11527 as_bad (_("%s: no such section"), secname);
11528 *input_line_pointer = c;
11529
11530 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11531 flag = BSF_FUNCTION;
11532 }
11533
11534 symbol_get_bfdsym (symbolP)->flags |= flag;
11535
11536 S_SET_EXTERNAL (symbolP);
11537 demand_empty_rest_of_line ();
11538 }
11539
11540 static void
11541 s_option (int x ATTRIBUTE_UNUSED)
11542 {
11543 char *opt;
11544 char c;
11545
11546 opt = input_line_pointer;
11547 c = get_symbol_end ();
11548
11549 if (*opt == 'O')
11550 {
11551 /* FIXME: What does this mean? */
11552 }
11553 else if (strncmp (opt, "pic", 3) == 0)
11554 {
11555 int i;
11556
11557 i = atoi (opt + 3);
11558 if (i == 0)
11559 mips_pic = NO_PIC;
11560 else if (i == 2)
11561 {
11562 mips_pic = SVR4_PIC;
11563 mips_abicalls = TRUE;
11564 }
11565 else
11566 as_bad (_(".option pic%d not supported"), i);
11567
11568 if (mips_pic == SVR4_PIC)
11569 {
11570 if (g_switch_seen && g_switch_value != 0)
11571 as_warn (_("-G may not be used with SVR4 PIC code"));
11572 g_switch_value = 0;
11573 bfd_set_gp_size (stdoutput, 0);
11574 }
11575 }
11576 else
11577 as_warn (_("Unrecognized option \"%s\""), opt);
11578
11579 *input_line_pointer = c;
11580 demand_empty_rest_of_line ();
11581 }
11582
11583 /* This structure is used to hold a stack of .set values. */
11584
11585 struct mips_option_stack
11586 {
11587 struct mips_option_stack *next;
11588 struct mips_set_options options;
11589 };
11590
11591 static struct mips_option_stack *mips_opts_stack;
11592
11593 /* Handle the .set pseudo-op. */
11594
11595 static void
11596 s_mipsset (int x ATTRIBUTE_UNUSED)
11597 {
11598 char *name = input_line_pointer, ch;
11599
11600 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11601 ++input_line_pointer;
11602 ch = *input_line_pointer;
11603 *input_line_pointer = '\0';
11604
11605 if (strcmp (name, "reorder") == 0)
11606 {
11607 if (mips_opts.noreorder && prev_nop_frag != NULL)
11608 {
11609 /* If we still have pending nops, we can discard them. The
11610 usual nop handling will insert any that are still
11611 needed. */
11612 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11613 * (mips_opts.mips16 ? 2 : 4));
11614 prev_nop_frag = NULL;
11615 }
11616 mips_opts.noreorder = 0;
11617 }
11618 else if (strcmp (name, "noreorder") == 0)
11619 {
11620 mips_emit_delays (TRUE);
11621 mips_opts.noreorder = 1;
11622 mips_any_noreorder = 1;
11623 }
11624 else if (strcmp (name, "at") == 0)
11625 {
11626 mips_opts.noat = 0;
11627 }
11628 else if (strcmp (name, "noat") == 0)
11629 {
11630 mips_opts.noat = 1;
11631 }
11632 else if (strcmp (name, "macro") == 0)
11633 {
11634 mips_opts.warn_about_macros = 0;
11635 }
11636 else if (strcmp (name, "nomacro") == 0)
11637 {
11638 if (mips_opts.noreorder == 0)
11639 as_bad (_("`noreorder' must be set before `nomacro'"));
11640 mips_opts.warn_about_macros = 1;
11641 }
11642 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11643 {
11644 mips_opts.nomove = 0;
11645 }
11646 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11647 {
11648 mips_opts.nomove = 1;
11649 }
11650 else if (strcmp (name, "bopt") == 0)
11651 {
11652 mips_opts.nobopt = 0;
11653 }
11654 else if (strcmp (name, "nobopt") == 0)
11655 {
11656 mips_opts.nobopt = 1;
11657 }
11658 else if (strcmp (name, "mips16") == 0
11659 || strcmp (name, "MIPS-16") == 0)
11660 mips_opts.mips16 = 1;
11661 else if (strcmp (name, "nomips16") == 0
11662 || strcmp (name, "noMIPS-16") == 0)
11663 mips_opts.mips16 = 0;
11664 else if (strcmp (name, "mips3d") == 0)
11665 mips_opts.ase_mips3d = 1;
11666 else if (strcmp (name, "nomips3d") == 0)
11667 mips_opts.ase_mips3d = 0;
11668 else if (strcmp (name, "mdmx") == 0)
11669 mips_opts.ase_mdmx = 1;
11670 else if (strcmp (name, "nomdmx") == 0)
11671 mips_opts.ase_mdmx = 0;
11672 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
11673 {
11674 int reset = 0;
11675
11676 /* Permit the user to change the ISA and architecture on the fly.
11677 Needless to say, misuse can cause serious problems. */
11678 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
11679 {
11680 reset = 1;
11681 mips_opts.isa = file_mips_isa;
11682 mips_opts.arch = file_mips_arch;
11683 }
11684 else if (strncmp (name, "arch=", 5) == 0)
11685 {
11686 const struct mips_cpu_info *p;
11687
11688 p = mips_parse_cpu("internal use", name + 5);
11689 if (!p)
11690 as_bad (_("unknown architecture %s"), name + 5);
11691 else
11692 {
11693 mips_opts.arch = p->cpu;
11694 mips_opts.isa = p->isa;
11695 }
11696 }
11697 else if (strncmp (name, "mips", 4) == 0)
11698 {
11699 const struct mips_cpu_info *p;
11700
11701 p = mips_parse_cpu("internal use", name);
11702 if (!p)
11703 as_bad (_("unknown ISA level %s"), name + 4);
11704 else
11705 {
11706 mips_opts.arch = p->cpu;
11707 mips_opts.isa = p->isa;
11708 }
11709 }
11710 else
11711 as_bad (_("unknown ISA or architecture %s"), name);
11712
11713 switch (mips_opts.isa)
11714 {
11715 case 0:
11716 break;
11717 case ISA_MIPS1:
11718 case ISA_MIPS2:
11719 case ISA_MIPS32:
11720 case ISA_MIPS32R2:
11721 mips_opts.gp32 = 1;
11722 mips_opts.fp32 = 1;
11723 break;
11724 case ISA_MIPS3:
11725 case ISA_MIPS4:
11726 case ISA_MIPS5:
11727 case ISA_MIPS64:
11728 case ISA_MIPS64R2:
11729 mips_opts.gp32 = 0;
11730 mips_opts.fp32 = 0;
11731 break;
11732 default:
11733 as_bad (_("unknown ISA level %s"), name + 4);
11734 break;
11735 }
11736 if (reset)
11737 {
11738 mips_opts.gp32 = file_mips_gp32;
11739 mips_opts.fp32 = file_mips_fp32;
11740 }
11741 }
11742 else if (strcmp (name, "autoextend") == 0)
11743 mips_opts.noautoextend = 0;
11744 else if (strcmp (name, "noautoextend") == 0)
11745 mips_opts.noautoextend = 1;
11746 else if (strcmp (name, "push") == 0)
11747 {
11748 struct mips_option_stack *s;
11749
11750 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11751 s->next = mips_opts_stack;
11752 s->options = mips_opts;
11753 mips_opts_stack = s;
11754 }
11755 else if (strcmp (name, "pop") == 0)
11756 {
11757 struct mips_option_stack *s;
11758
11759 s = mips_opts_stack;
11760 if (s == NULL)
11761 as_bad (_(".set pop with no .set push"));
11762 else
11763 {
11764 /* If we're changing the reorder mode we need to handle
11765 delay slots correctly. */
11766 if (s->options.noreorder && ! mips_opts.noreorder)
11767 mips_emit_delays (TRUE);
11768 else if (! s->options.noreorder && mips_opts.noreorder)
11769 {
11770 if (prev_nop_frag != NULL)
11771 {
11772 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11773 * (mips_opts.mips16 ? 2 : 4));
11774 prev_nop_frag = NULL;
11775 }
11776 }
11777
11778 mips_opts = s->options;
11779 mips_opts_stack = s->next;
11780 free (s);
11781 }
11782 }
11783 else if (strcmp (name, "sym32") == 0)
11784 mips_opts.sym32 = TRUE;
11785 else if (strcmp (name, "nosym32") == 0)
11786 mips_opts.sym32 = FALSE;
11787 else
11788 {
11789 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11790 }
11791 *input_line_pointer = ch;
11792 demand_empty_rest_of_line ();
11793 }
11794
11795 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11796 .option pic2. It means to generate SVR4 PIC calls. */
11797
11798 static void
11799 s_abicalls (int ignore ATTRIBUTE_UNUSED)
11800 {
11801 mips_pic = SVR4_PIC;
11802 mips_abicalls = TRUE;
11803
11804 if (g_switch_seen && g_switch_value != 0)
11805 as_warn (_("-G may not be used with SVR4 PIC code"));
11806 g_switch_value = 0;
11807
11808 bfd_set_gp_size (stdoutput, 0);
11809 demand_empty_rest_of_line ();
11810 }
11811
11812 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11813 PIC code. It sets the $gp register for the function based on the
11814 function address, which is in the register named in the argument.
11815 This uses a relocation against _gp_disp, which is handled specially
11816 by the linker. The result is:
11817 lui $gp,%hi(_gp_disp)
11818 addiu $gp,$gp,%lo(_gp_disp)
11819 addu $gp,$gp,.cpload argument
11820 The .cpload argument is normally $25 == $t9.
11821
11822 The -mno-shared option changes this to:
11823 lui $gp,%hi(__gnu_local_gp)
11824 addiu $gp,$gp,%lo(__gnu_local_gp)
11825 and the argument is ignored. This saves an instruction, but the
11826 resulting code is not position independent; it uses an absolute
11827 address for __gnu_local_gp. Thus code assembled with -mno-shared
11828 can go into an ordinary executable, but not into a shared library. */
11829
11830 static void
11831 s_cpload (int ignore ATTRIBUTE_UNUSED)
11832 {
11833 expressionS ex;
11834 int reg;
11835 int in_shared;
11836
11837 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11838 .cpload is ignored. */
11839 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11840 {
11841 s_ignore (0);
11842 return;
11843 }
11844
11845 /* .cpload should be in a .set noreorder section. */
11846 if (mips_opts.noreorder == 0)
11847 as_warn (_(".cpload not in noreorder section"));
11848
11849 reg = tc_get_register (0);
11850
11851 /* If we need to produce a 64-bit address, we are better off using
11852 the default instruction sequence. */
11853 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
11854
11855 ex.X_op = O_symbol;
11856 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
11857 "__gnu_local_gp");
11858 ex.X_op_symbol = NULL;
11859 ex.X_add_number = 0;
11860
11861 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11862 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11863
11864 macro_start ();
11865 macro_build_lui (&ex, mips_gp_register);
11866 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11867 mips_gp_register, BFD_RELOC_LO16);
11868 if (in_shared)
11869 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11870 mips_gp_register, reg);
11871 macro_end ();
11872
11873 demand_empty_rest_of_line ();
11874 }
11875
11876 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11877 .cpsetup $reg1, offset|$reg2, label
11878
11879 If offset is given, this results in:
11880 sd $gp, offset($sp)
11881 lui $gp, %hi(%neg(%gp_rel(label)))
11882 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11883 daddu $gp, $gp, $reg1
11884
11885 If $reg2 is given, this results in:
11886 daddu $reg2, $gp, $0
11887 lui $gp, %hi(%neg(%gp_rel(label)))
11888 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11889 daddu $gp, $gp, $reg1
11890 $reg1 is normally $25 == $t9.
11891
11892 The -mno-shared option replaces the last three instructions with
11893 lui $gp,%hi(_gp)
11894 addiu $gp,$gp,%lo(_gp)
11895 */
11896
11897 static void
11898 s_cpsetup (int ignore ATTRIBUTE_UNUSED)
11899 {
11900 expressionS ex_off;
11901 expressionS ex_sym;
11902 int reg1;
11903
11904 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
11905 We also need NewABI support. */
11906 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11907 {
11908 s_ignore (0);
11909 return;
11910 }
11911
11912 reg1 = tc_get_register (0);
11913 SKIP_WHITESPACE ();
11914 if (*input_line_pointer != ',')
11915 {
11916 as_bad (_("missing argument separator ',' for .cpsetup"));
11917 return;
11918 }
11919 else
11920 ++input_line_pointer;
11921 SKIP_WHITESPACE ();
11922 if (*input_line_pointer == '$')
11923 {
11924 mips_cpreturn_register = tc_get_register (0);
11925 mips_cpreturn_offset = -1;
11926 }
11927 else
11928 {
11929 mips_cpreturn_offset = get_absolute_expression ();
11930 mips_cpreturn_register = -1;
11931 }
11932 SKIP_WHITESPACE ();
11933 if (*input_line_pointer != ',')
11934 {
11935 as_bad (_("missing argument separator ',' for .cpsetup"));
11936 return;
11937 }
11938 else
11939 ++input_line_pointer;
11940 SKIP_WHITESPACE ();
11941 expression (&ex_sym);
11942
11943 macro_start ();
11944 if (mips_cpreturn_register == -1)
11945 {
11946 ex_off.X_op = O_constant;
11947 ex_off.X_add_symbol = NULL;
11948 ex_off.X_op_symbol = NULL;
11949 ex_off.X_add_number = mips_cpreturn_offset;
11950
11951 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
11952 BFD_RELOC_LO16, SP);
11953 }
11954 else
11955 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
11956 mips_gp_register, 0);
11957
11958 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
11959 {
11960 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11961 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11962 BFD_RELOC_HI16_S);
11963
11964 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11965 mips_gp_register, -1, BFD_RELOC_GPREL16,
11966 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11967
11968 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11969 mips_gp_register, reg1);
11970 }
11971 else
11972 {
11973 expressionS ex;
11974
11975 ex.X_op = O_symbol;
11976 ex.X_add_symbol = symbol_find_or_make ("_gp");
11977 ex.X_op_symbol = NULL;
11978 ex.X_add_number = 0;
11979
11980 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11981 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11982
11983 macro_build_lui (&ex, mips_gp_register);
11984 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11985 mips_gp_register, BFD_RELOC_LO16);
11986 }
11987
11988 macro_end ();
11989
11990 demand_empty_rest_of_line ();
11991 }
11992
11993 static void
11994 s_cplocal (int ignore ATTRIBUTE_UNUSED)
11995 {
11996 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11997 .cplocal is ignored. */
11998 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11999 {
12000 s_ignore (0);
12001 return;
12002 }
12003
12004 mips_gp_register = tc_get_register (0);
12005 demand_empty_rest_of_line ();
12006 }
12007
12008 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12009 offset from $sp. The offset is remembered, and after making a PIC
12010 call $gp is restored from that location. */
12011
12012 static void
12013 s_cprestore (int ignore ATTRIBUTE_UNUSED)
12014 {
12015 expressionS ex;
12016
12017 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12018 .cprestore is ignored. */
12019 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12020 {
12021 s_ignore (0);
12022 return;
12023 }
12024
12025 mips_cprestore_offset = get_absolute_expression ();
12026 mips_cprestore_valid = 1;
12027
12028 ex.X_op = O_constant;
12029 ex.X_add_symbol = NULL;
12030 ex.X_op_symbol = NULL;
12031 ex.X_add_number = mips_cprestore_offset;
12032
12033 macro_start ();
12034 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12035 SP, HAVE_64BIT_ADDRESSES);
12036 macro_end ();
12037
12038 demand_empty_rest_of_line ();
12039 }
12040
12041 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12042 was given in the preceding .cpsetup, it results in:
12043 ld $gp, offset($sp)
12044
12045 If a register $reg2 was given there, it results in:
12046 daddu $gp, $reg2, $0
12047 */
12048 static void
12049 s_cpreturn (int ignore ATTRIBUTE_UNUSED)
12050 {
12051 expressionS ex;
12052
12053 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12054 We also need NewABI support. */
12055 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12056 {
12057 s_ignore (0);
12058 return;
12059 }
12060
12061 macro_start ();
12062 if (mips_cpreturn_register == -1)
12063 {
12064 ex.X_op = O_constant;
12065 ex.X_add_symbol = NULL;
12066 ex.X_op_symbol = NULL;
12067 ex.X_add_number = mips_cpreturn_offset;
12068
12069 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
12070 }
12071 else
12072 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
12073 mips_cpreturn_register, 0);
12074 macro_end ();
12075
12076 demand_empty_rest_of_line ();
12077 }
12078
12079 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12080 code. It sets the offset to use in gp_rel relocations. */
12081
12082 static void
12083 s_gpvalue (int ignore ATTRIBUTE_UNUSED)
12084 {
12085 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12086 We also need NewABI support. */
12087 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12088 {
12089 s_ignore (0);
12090 return;
12091 }
12092
12093 mips_gprel_offset = get_absolute_expression ();
12094
12095 demand_empty_rest_of_line ();
12096 }
12097
12098 /* Handle the .gpword pseudo-op. This is used when generating PIC
12099 code. It generates a 32 bit GP relative reloc. */
12100
12101 static void
12102 s_gpword (int ignore ATTRIBUTE_UNUSED)
12103 {
12104 symbolS *label;
12105 expressionS ex;
12106 char *p;
12107
12108 /* When not generating PIC code, this is treated as .word. */
12109 if (mips_pic != SVR4_PIC)
12110 {
12111 s_cons (2);
12112 return;
12113 }
12114
12115 label = insn_labels != NULL ? insn_labels->label : NULL;
12116 mips_emit_delays (TRUE);
12117 if (auto_align)
12118 mips_align (2, 0, label);
12119 mips_clear_insn_labels ();
12120
12121 expression (&ex);
12122
12123 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12124 {
12125 as_bad (_("Unsupported use of .gpword"));
12126 ignore_rest_of_line ();
12127 }
12128
12129 p = frag_more (4);
12130 md_number_to_chars (p, 0, 4);
12131 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12132 BFD_RELOC_GPREL32);
12133
12134 demand_empty_rest_of_line ();
12135 }
12136
12137 static void
12138 s_gpdword (int ignore ATTRIBUTE_UNUSED)
12139 {
12140 symbolS *label;
12141 expressionS ex;
12142 char *p;
12143
12144 /* When not generating PIC code, this is treated as .dword. */
12145 if (mips_pic != SVR4_PIC)
12146 {
12147 s_cons (3);
12148 return;
12149 }
12150
12151 label = insn_labels != NULL ? insn_labels->label : NULL;
12152 mips_emit_delays (TRUE);
12153 if (auto_align)
12154 mips_align (3, 0, label);
12155 mips_clear_insn_labels ();
12156
12157 expression (&ex);
12158
12159 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12160 {
12161 as_bad (_("Unsupported use of .gpdword"));
12162 ignore_rest_of_line ();
12163 }
12164
12165 p = frag_more (8);
12166 md_number_to_chars (p, 0, 8);
12167 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12168 BFD_RELOC_GPREL32)->fx_tcbit = 1;
12169
12170 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12171 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12172 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
12173
12174 demand_empty_rest_of_line ();
12175 }
12176
12177 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12178 tables in SVR4 PIC code. */
12179
12180 static void
12181 s_cpadd (int ignore ATTRIBUTE_UNUSED)
12182 {
12183 int reg;
12184
12185 /* This is ignored when not generating SVR4 PIC code. */
12186 if (mips_pic != SVR4_PIC)
12187 {
12188 s_ignore (0);
12189 return;
12190 }
12191
12192 /* Add $gp to the register named as an argument. */
12193 macro_start ();
12194 reg = tc_get_register (0);
12195 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
12196 macro_end ();
12197
12198 demand_empty_rest_of_line ();
12199 }
12200
12201 /* Handle the .insn pseudo-op. This marks instruction labels in
12202 mips16 mode. This permits the linker to handle them specially,
12203 such as generating jalx instructions when needed. We also make
12204 them odd for the duration of the assembly, in order to generate the
12205 right sort of code. We will make them even in the adjust_symtab
12206 routine, while leaving them marked. This is convenient for the
12207 debugger and the disassembler. The linker knows to make them odd
12208 again. */
12209
12210 static void
12211 s_insn (int ignore ATTRIBUTE_UNUSED)
12212 {
12213 mips16_mark_labels ();
12214
12215 demand_empty_rest_of_line ();
12216 }
12217
12218 /* Handle a .stabn directive. We need these in order to mark a label
12219 as being a mips16 text label correctly. Sometimes the compiler
12220 will emit a label, followed by a .stabn, and then switch sections.
12221 If the label and .stabn are in mips16 mode, then the label is
12222 really a mips16 text label. */
12223
12224 static void
12225 s_mips_stab (int type)
12226 {
12227 if (type == 'n')
12228 mips16_mark_labels ();
12229
12230 s_stab (type);
12231 }
12232
12233 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12234 */
12235
12236 static void
12237 s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
12238 {
12239 char *name;
12240 int c;
12241 symbolS *symbolP;
12242 expressionS exp;
12243
12244 name = input_line_pointer;
12245 c = get_symbol_end ();
12246 symbolP = symbol_find_or_make (name);
12247 S_SET_WEAK (symbolP);
12248 *input_line_pointer = c;
12249
12250 SKIP_WHITESPACE ();
12251
12252 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12253 {
12254 if (S_IS_DEFINED (symbolP))
12255 {
12256 as_bad ("ignoring attempt to redefine symbol %s",
12257 S_GET_NAME (symbolP));
12258 ignore_rest_of_line ();
12259 return;
12260 }
12261
12262 if (*input_line_pointer == ',')
12263 {
12264 ++input_line_pointer;
12265 SKIP_WHITESPACE ();
12266 }
12267
12268 expression (&exp);
12269 if (exp.X_op != O_symbol)
12270 {
12271 as_bad ("bad .weakext directive");
12272 ignore_rest_of_line ();
12273 return;
12274 }
12275 symbol_set_value_expression (symbolP, &exp);
12276 }
12277
12278 demand_empty_rest_of_line ();
12279 }
12280
12281 /* Parse a register string into a number. Called from the ECOFF code
12282 to parse .frame. The argument is non-zero if this is the frame
12283 register, so that we can record it in mips_frame_reg. */
12284
12285 int
12286 tc_get_register (int frame)
12287 {
12288 int reg;
12289
12290 SKIP_WHITESPACE ();
12291 if (*input_line_pointer++ != '$')
12292 {
12293 as_warn (_("expected `$'"));
12294 reg = ZERO;
12295 }
12296 else if (ISDIGIT (*input_line_pointer))
12297 {
12298 reg = get_absolute_expression ();
12299 if (reg < 0 || reg >= 32)
12300 {
12301 as_warn (_("Bad register number"));
12302 reg = ZERO;
12303 }
12304 }
12305 else
12306 {
12307 if (strncmp (input_line_pointer, "ra", 2) == 0)
12308 {
12309 reg = RA;
12310 input_line_pointer += 2;
12311 }
12312 else if (strncmp (input_line_pointer, "fp", 2) == 0)
12313 {
12314 reg = FP;
12315 input_line_pointer += 2;
12316 }
12317 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12318 {
12319 reg = SP;
12320 input_line_pointer += 2;
12321 }
12322 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12323 {
12324 reg = GP;
12325 input_line_pointer += 2;
12326 }
12327 else if (strncmp (input_line_pointer, "at", 2) == 0)
12328 {
12329 reg = AT;
12330 input_line_pointer += 2;
12331 }
12332 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12333 {
12334 reg = KT0;
12335 input_line_pointer += 3;
12336 }
12337 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12338 {
12339 reg = KT1;
12340 input_line_pointer += 3;
12341 }
12342 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12343 {
12344 reg = ZERO;
12345 input_line_pointer += 4;
12346 }
12347 else
12348 {
12349 as_warn (_("Unrecognized register name"));
12350 reg = ZERO;
12351 while (ISALNUM(*input_line_pointer))
12352 input_line_pointer++;
12353 }
12354 }
12355 if (frame)
12356 {
12357 mips_frame_reg = reg != 0 ? reg : SP;
12358 mips_frame_reg_valid = 1;
12359 mips_cprestore_valid = 0;
12360 }
12361 return reg;
12362 }
12363
12364 valueT
12365 md_section_align (asection *seg, valueT addr)
12366 {
12367 int align = bfd_get_section_alignment (stdoutput, seg);
12368
12369 #ifdef OBJ_ELF
12370 /* We don't need to align ELF sections to the full alignment.
12371 However, Irix 5 may prefer that we align them at least to a 16
12372 byte boundary. We don't bother to align the sections if we are
12373 targeted for an embedded system. */
12374 if (strcmp (TARGET_OS, "elf") == 0)
12375 return addr;
12376 if (align > 4)
12377 align = 4;
12378 #endif
12379
12380 return ((addr + (1 << align) - 1) & (-1 << align));
12381 }
12382
12383 /* Utility routine, called from above as well. If called while the
12384 input file is still being read, it's only an approximation. (For
12385 example, a symbol may later become defined which appeared to be
12386 undefined earlier.) */
12387
12388 static int
12389 nopic_need_relax (symbolS *sym, int before_relaxing)
12390 {
12391 if (sym == 0)
12392 return 0;
12393
12394 if (g_switch_value > 0)
12395 {
12396 const char *symname;
12397 int change;
12398
12399 /* Find out whether this symbol can be referenced off the $gp
12400 register. It can be if it is smaller than the -G size or if
12401 it is in the .sdata or .sbss section. Certain symbols can
12402 not be referenced off the $gp, although it appears as though
12403 they can. */
12404 symname = S_GET_NAME (sym);
12405 if (symname != (const char *) NULL
12406 && (strcmp (symname, "eprol") == 0
12407 || strcmp (symname, "etext") == 0
12408 || strcmp (symname, "_gp") == 0
12409 || strcmp (symname, "edata") == 0
12410 || strcmp (symname, "_fbss") == 0
12411 || strcmp (symname, "_fdata") == 0
12412 || strcmp (symname, "_ftext") == 0
12413 || strcmp (symname, "end") == 0
12414 || strcmp (symname, "_gp_disp") == 0))
12415 change = 1;
12416 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12417 && (0
12418 #ifndef NO_ECOFF_DEBUGGING
12419 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12420 && (symbol_get_obj (sym)->ecoff_extern_size
12421 <= g_switch_value))
12422 #endif
12423 /* We must defer this decision until after the whole
12424 file has been read, since there might be a .extern
12425 after the first use of this symbol. */
12426 || (before_relaxing
12427 #ifndef NO_ECOFF_DEBUGGING
12428 && symbol_get_obj (sym)->ecoff_extern_size == 0
12429 #endif
12430 && S_GET_VALUE (sym) == 0)
12431 || (S_GET_VALUE (sym) != 0
12432 && S_GET_VALUE (sym) <= g_switch_value)))
12433 change = 0;
12434 else
12435 {
12436 const char *segname;
12437
12438 segname = segment_name (S_GET_SEGMENT (sym));
12439 assert (strcmp (segname, ".lit8") != 0
12440 && strcmp (segname, ".lit4") != 0);
12441 change = (strcmp (segname, ".sdata") != 0
12442 && strcmp (segname, ".sbss") != 0
12443 && strncmp (segname, ".sdata.", 7) != 0
12444 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
12445 }
12446 return change;
12447 }
12448 else
12449 /* We are not optimizing for the $gp register. */
12450 return 1;
12451 }
12452
12453
12454 /* Return true if the given symbol should be considered local for SVR4 PIC. */
12455
12456 static bfd_boolean
12457 pic_need_relax (symbolS *sym, asection *segtype)
12458 {
12459 asection *symsec;
12460 bfd_boolean linkonce;
12461
12462 /* Handle the case of a symbol equated to another symbol. */
12463 while (symbol_equated_reloc_p (sym))
12464 {
12465 symbolS *n;
12466
12467 /* It's possible to get a loop here in a badly written
12468 program. */
12469 n = symbol_get_value_expression (sym)->X_add_symbol;
12470 if (n == sym)
12471 break;
12472 sym = n;
12473 }
12474
12475 symsec = S_GET_SEGMENT (sym);
12476
12477 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12478 linkonce = FALSE;
12479 if (symsec != segtype && ! S_IS_LOCAL (sym))
12480 {
12481 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12482 != 0)
12483 linkonce = TRUE;
12484
12485 /* The GNU toolchain uses an extension for ELF: a section
12486 beginning with the magic string .gnu.linkonce is a linkonce
12487 section. */
12488 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12489 sizeof ".gnu.linkonce" - 1) == 0)
12490 linkonce = TRUE;
12491 }
12492
12493 /* This must duplicate the test in adjust_reloc_syms. */
12494 return (symsec != &bfd_und_section
12495 && symsec != &bfd_abs_section
12496 && ! bfd_is_com_section (symsec)
12497 && !linkonce
12498 #ifdef OBJ_ELF
12499 /* A global or weak symbol is treated as external. */
12500 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12501 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
12502 #endif
12503 );
12504 }
12505
12506
12507 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12508 extended opcode. SEC is the section the frag is in. */
12509
12510 static int
12511 mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
12512 {
12513 int type;
12514 register const struct mips16_immed_operand *op;
12515 offsetT val;
12516 int mintiny, maxtiny;
12517 segT symsec;
12518 fragS *sym_frag;
12519
12520 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12521 return 0;
12522 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12523 return 1;
12524
12525 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12526 op = mips16_immed_operands;
12527 while (op->type != type)
12528 {
12529 ++op;
12530 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12531 }
12532
12533 if (op->unsp)
12534 {
12535 if (type == '<' || type == '>' || type == '[' || type == ']')
12536 {
12537 mintiny = 1;
12538 maxtiny = 1 << op->nbits;
12539 }
12540 else
12541 {
12542 mintiny = 0;
12543 maxtiny = (1 << op->nbits) - 1;
12544 }
12545 }
12546 else
12547 {
12548 mintiny = - (1 << (op->nbits - 1));
12549 maxtiny = (1 << (op->nbits - 1)) - 1;
12550 }
12551
12552 sym_frag = symbol_get_frag (fragp->fr_symbol);
12553 val = S_GET_VALUE (fragp->fr_symbol);
12554 symsec = S_GET_SEGMENT (fragp->fr_symbol);
12555
12556 if (op->pcrel)
12557 {
12558 addressT addr;
12559
12560 /* We won't have the section when we are called from
12561 mips_relax_frag. However, we will always have been called
12562 from md_estimate_size_before_relax first. If this is a
12563 branch to a different section, we mark it as such. If SEC is
12564 NULL, and the frag is not marked, then it must be a branch to
12565 the same section. */
12566 if (sec == NULL)
12567 {
12568 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12569 return 1;
12570 }
12571 else
12572 {
12573 /* Must have been called from md_estimate_size_before_relax. */
12574 if (symsec != sec)
12575 {
12576 fragp->fr_subtype =
12577 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12578
12579 /* FIXME: We should support this, and let the linker
12580 catch branches and loads that are out of range. */
12581 as_bad_where (fragp->fr_file, fragp->fr_line,
12582 _("unsupported PC relative reference to different section"));
12583
12584 return 1;
12585 }
12586 if (fragp != sym_frag && sym_frag->fr_address == 0)
12587 /* Assume non-extended on the first relaxation pass.
12588 The address we have calculated will be bogus if this is
12589 a forward branch to another frag, as the forward frag
12590 will have fr_address == 0. */
12591 return 0;
12592 }
12593
12594 /* In this case, we know for sure that the symbol fragment is in
12595 the same section. If the relax_marker of the symbol fragment
12596 differs from the relax_marker of this fragment, we have not
12597 yet adjusted the symbol fragment fr_address. We want to add
12598 in STRETCH in order to get a better estimate of the address.
12599 This particularly matters because of the shift bits. */
12600 if (stretch != 0
12601 && sym_frag->relax_marker != fragp->relax_marker)
12602 {
12603 fragS *f;
12604
12605 /* Adjust stretch for any alignment frag. Note that if have
12606 been expanding the earlier code, the symbol may be
12607 defined in what appears to be an earlier frag. FIXME:
12608 This doesn't handle the fr_subtype field, which specifies
12609 a maximum number of bytes to skip when doing an
12610 alignment. */
12611 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12612 {
12613 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12614 {
12615 if (stretch < 0)
12616 stretch = - ((- stretch)
12617 & ~ ((1 << (int) f->fr_offset) - 1));
12618 else
12619 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12620 if (stretch == 0)
12621 break;
12622 }
12623 }
12624 if (f != NULL)
12625 val += stretch;
12626 }
12627
12628 addr = fragp->fr_address + fragp->fr_fix;
12629
12630 /* The base address rules are complicated. The base address of
12631 a branch is the following instruction. The base address of a
12632 PC relative load or add is the instruction itself, but if it
12633 is in a delay slot (in which case it can not be extended) use
12634 the address of the instruction whose delay slot it is in. */
12635 if (type == 'p' || type == 'q')
12636 {
12637 addr += 2;
12638
12639 /* If we are currently assuming that this frag should be
12640 extended, then, the current address is two bytes
12641 higher. */
12642 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12643 addr += 2;
12644
12645 /* Ignore the low bit in the target, since it will be set
12646 for a text label. */
12647 if ((val & 1) != 0)
12648 --val;
12649 }
12650 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12651 addr -= 4;
12652 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12653 addr -= 2;
12654
12655 val -= addr & ~ ((1 << op->shift) - 1);
12656
12657 /* Branch offsets have an implicit 0 in the lowest bit. */
12658 if (type == 'p' || type == 'q')
12659 val /= 2;
12660
12661 /* If any of the shifted bits are set, we must use an extended
12662 opcode. If the address depends on the size of this
12663 instruction, this can lead to a loop, so we arrange to always
12664 use an extended opcode. We only check this when we are in
12665 the main relaxation loop, when SEC is NULL. */
12666 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12667 {
12668 fragp->fr_subtype =
12669 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12670 return 1;
12671 }
12672
12673 /* If we are about to mark a frag as extended because the value
12674 is precisely maxtiny + 1, then there is a chance of an
12675 infinite loop as in the following code:
12676 la $4,foo
12677 .skip 1020
12678 .align 2
12679 foo:
12680 In this case when the la is extended, foo is 0x3fc bytes
12681 away, so the la can be shrunk, but then foo is 0x400 away, so
12682 the la must be extended. To avoid this loop, we mark the
12683 frag as extended if it was small, and is about to become
12684 extended with a value of maxtiny + 1. */
12685 if (val == ((maxtiny + 1) << op->shift)
12686 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12687 && sec == NULL)
12688 {
12689 fragp->fr_subtype =
12690 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12691 return 1;
12692 }
12693 }
12694 else if (symsec != absolute_section && sec != NULL)
12695 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12696
12697 if ((val & ((1 << op->shift) - 1)) != 0
12698 || val < (mintiny << op->shift)
12699 || val > (maxtiny << op->shift))
12700 return 1;
12701 else
12702 return 0;
12703 }
12704
12705 /* Compute the length of a branch sequence, and adjust the
12706 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12707 worst-case length is computed, with UPDATE being used to indicate
12708 whether an unconditional (-1), branch-likely (+1) or regular (0)
12709 branch is to be computed. */
12710 static int
12711 relaxed_branch_length (fragS *fragp, asection *sec, int update)
12712 {
12713 bfd_boolean toofar;
12714 int length;
12715
12716 if (fragp
12717 && S_IS_DEFINED (fragp->fr_symbol)
12718 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12719 {
12720 addressT addr;
12721 offsetT val;
12722
12723 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12724
12725 addr = fragp->fr_address + fragp->fr_fix + 4;
12726
12727 val -= addr;
12728
12729 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12730 }
12731 else if (fragp)
12732 /* If the symbol is not defined or it's in a different segment,
12733 assume the user knows what's going on and emit a short
12734 branch. */
12735 toofar = FALSE;
12736 else
12737 toofar = TRUE;
12738
12739 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12740 fragp->fr_subtype
12741 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
12742 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12743 RELAX_BRANCH_LINK (fragp->fr_subtype),
12744 toofar);
12745
12746 length = 4;
12747 if (toofar)
12748 {
12749 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12750 length += 8;
12751
12752 if (mips_pic != NO_PIC)
12753 {
12754 /* Additional space for PIC loading of target address. */
12755 length += 8;
12756 if (mips_opts.isa == ISA_MIPS1)
12757 /* Additional space for $at-stabilizing nop. */
12758 length += 4;
12759 }
12760
12761 /* If branch is conditional. */
12762 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12763 length += 8;
12764 }
12765
12766 return length;
12767 }
12768
12769 /* Estimate the size of a frag before relaxing. Unless this is the
12770 mips16, we are not really relaxing here, and the final size is
12771 encoded in the subtype information. For the mips16, we have to
12772 decide whether we are using an extended opcode or not. */
12773
12774 int
12775 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
12776 {
12777 int change;
12778
12779 if (RELAX_BRANCH_P (fragp->fr_subtype))
12780 {
12781
12782 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12783
12784 return fragp->fr_var;
12785 }
12786
12787 if (RELAX_MIPS16_P (fragp->fr_subtype))
12788 /* We don't want to modify the EXTENDED bit here; it might get us
12789 into infinite loops. We change it only in mips_relax_frag(). */
12790 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
12791
12792 if (mips_pic == NO_PIC)
12793 change = nopic_need_relax (fragp->fr_symbol, 0);
12794 else if (mips_pic == SVR4_PIC)
12795 change = pic_need_relax (fragp->fr_symbol, segtype);
12796 else
12797 abort ();
12798
12799 if (change)
12800 {
12801 fragp->fr_subtype |= RELAX_USE_SECOND;
12802 return -RELAX_FIRST (fragp->fr_subtype);
12803 }
12804 else
12805 return -RELAX_SECOND (fragp->fr_subtype);
12806 }
12807
12808 /* This is called to see whether a reloc against a defined symbol
12809 should be converted into a reloc against a section. */
12810
12811 int
12812 mips_fix_adjustable (fixS *fixp)
12813 {
12814 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12815 about the format of the offset in the .o file. */
12816 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12817 return 0;
12818
12819 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12820 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12821 return 0;
12822
12823 if (fixp->fx_addsy == NULL)
12824 return 1;
12825
12826 /* If symbol SYM is in a mergeable section, relocations of the form
12827 SYM + 0 can usually be made section-relative. The mergeable data
12828 is then identified by the section offset rather than by the symbol.
12829
12830 However, if we're generating REL LO16 relocations, the offset is split
12831 between the LO16 and parterning high part relocation. The linker will
12832 need to recalculate the complete offset in order to correctly identify
12833 the merge data.
12834
12835 The linker has traditionally not looked for the parterning high part
12836 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12837 placed anywhere. Rather than break backwards compatibility by changing
12838 this, it seems better not to force the issue, and instead keep the
12839 original symbol. This will work with either linker behavior. */
12840 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12841 && HAVE_IN_PLACE_ADDENDS
12842 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12843 return 0;
12844
12845 #ifdef OBJ_ELF
12846 /* Don't adjust relocations against mips16 symbols, so that the linker
12847 can find them if it needs to set up a stub. */
12848 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12849 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12850 && fixp->fx_subsy == NULL)
12851 return 0;
12852 #endif
12853
12854 return 1;
12855 }
12856
12857 /* Translate internal representation of relocation info to BFD target
12858 format. */
12859
12860 arelent **
12861 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
12862 {
12863 static arelent *retval[4];
12864 arelent *reloc;
12865 bfd_reloc_code_real_type code;
12866
12867 memset (retval, 0, sizeof(retval));
12868 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
12869 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12870 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12871 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12872
12873 assert (! fixp->fx_pcrel);
12874 reloc->addend = fixp->fx_addnumber;
12875
12876 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12877 entry to be used in the relocation's section offset. */
12878 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12879 {
12880 reloc->address = reloc->addend;
12881 reloc->addend = 0;
12882 }
12883
12884 code = fixp->fx_r_type;
12885
12886 /* To support a PC relative reloc, we used a Cygnus extension.
12887 We check for that here to make sure that we don't let such a
12888 reloc escape normally. (FIXME: This was formerly used by
12889 embedded-PIC support, but is now used by branch handling in
12890 general. That probably should be fixed.) */
12891 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12892 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12893 && code == BFD_RELOC_16_PCREL_S2)
12894 reloc->howto = NULL;
12895 else
12896 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12897
12898 if (reloc->howto == NULL)
12899 {
12900 as_bad_where (fixp->fx_file, fixp->fx_line,
12901 _("Can not represent %s relocation in this object file format"),
12902 bfd_get_reloc_code_name (code));
12903 retval[0] = NULL;
12904 }
12905
12906 return retval;
12907 }
12908
12909 /* Relax a machine dependent frag. This returns the amount by which
12910 the current size of the frag should change. */
12911
12912 int
12913 mips_relax_frag (asection *sec, fragS *fragp, long stretch)
12914 {
12915 if (RELAX_BRANCH_P (fragp->fr_subtype))
12916 {
12917 offsetT old_var = fragp->fr_var;
12918
12919 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
12920
12921 return fragp->fr_var - old_var;
12922 }
12923
12924 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12925 return 0;
12926
12927 if (mips16_extended_frag (fragp, NULL, stretch))
12928 {
12929 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12930 return 0;
12931 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12932 return 2;
12933 }
12934 else
12935 {
12936 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12937 return 0;
12938 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12939 return -2;
12940 }
12941
12942 return 0;
12943 }
12944
12945 /* Convert a machine dependent frag. */
12946
12947 void
12948 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
12949 {
12950 if (RELAX_BRANCH_P (fragp->fr_subtype))
12951 {
12952 bfd_byte *buf;
12953 unsigned long insn;
12954 expressionS exp;
12955 fixS *fixp;
12956
12957 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12958
12959 if (target_big_endian)
12960 insn = bfd_getb32 (buf);
12961 else
12962 insn = bfd_getl32 (buf);
12963
12964 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12965 {
12966 /* We generate a fixup instead of applying it right now
12967 because, if there are linker relaxations, we're going to
12968 need the relocations. */
12969 exp.X_op = O_symbol;
12970 exp.X_add_symbol = fragp->fr_symbol;
12971 exp.X_add_number = fragp->fr_offset;
12972
12973 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
12974 4, &exp, 1,
12975 BFD_RELOC_16_PCREL_S2);
12976 fixp->fx_file = fragp->fr_file;
12977 fixp->fx_line = fragp->fr_line;
12978
12979 md_number_to_chars ((char *) buf, insn, 4);
12980 buf += 4;
12981 }
12982 else
12983 {
12984 int i;
12985
12986 as_warn_where (fragp->fr_file, fragp->fr_line,
12987 _("relaxed out-of-range branch into a jump"));
12988
12989 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12990 goto uncond;
12991
12992 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12993 {
12994 /* Reverse the branch. */
12995 switch ((insn >> 28) & 0xf)
12996 {
12997 case 4:
12998 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12999 have the condition reversed by tweaking a single
13000 bit, and their opcodes all have 0x4???????. */
13001 assert ((insn & 0xf1000000) == 0x41000000);
13002 insn ^= 0x00010000;
13003 break;
13004
13005 case 0:
13006 /* bltz 0x04000000 bgez 0x04010000
13007 bltzal 0x04100000 bgezal 0x04110000 */
13008 assert ((insn & 0xfc0e0000) == 0x04000000);
13009 insn ^= 0x00010000;
13010 break;
13011
13012 case 1:
13013 /* beq 0x10000000 bne 0x14000000
13014 blez 0x18000000 bgtz 0x1c000000 */
13015 insn ^= 0x04000000;
13016 break;
13017
13018 default:
13019 abort ();
13020 }
13021 }
13022
13023 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13024 {
13025 /* Clear the and-link bit. */
13026 assert ((insn & 0xfc1c0000) == 0x04100000);
13027
13028 /* bltzal 0x04100000 bgezal 0x04110000
13029 bltzall 0x04120000 bgezall 0x04130000 */
13030 insn &= ~0x00100000;
13031 }
13032
13033 /* Branch over the branch (if the branch was likely) or the
13034 full jump (not likely case). Compute the offset from the
13035 current instruction to branch to. */
13036 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13037 i = 16;
13038 else
13039 {
13040 /* How many bytes in instructions we've already emitted? */
13041 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13042 /* How many bytes in instructions from here to the end? */
13043 i = fragp->fr_var - i;
13044 }
13045 /* Convert to instruction count. */
13046 i >>= 2;
13047 /* Branch counts from the next instruction. */
13048 i--;
13049 insn |= i;
13050 /* Branch over the jump. */
13051 md_number_to_chars ((char *) buf, insn, 4);
13052 buf += 4;
13053
13054 /* Nop */
13055 md_number_to_chars ((char *) buf, 0, 4);
13056 buf += 4;
13057
13058 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13059 {
13060 /* beql $0, $0, 2f */
13061 insn = 0x50000000;
13062 /* Compute the PC offset from the current instruction to
13063 the end of the variable frag. */
13064 /* How many bytes in instructions we've already emitted? */
13065 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13066 /* How many bytes in instructions from here to the end? */
13067 i = fragp->fr_var - i;
13068 /* Convert to instruction count. */
13069 i >>= 2;
13070 /* Don't decrement i, because we want to branch over the
13071 delay slot. */
13072
13073 insn |= i;
13074 md_number_to_chars ((char *) buf, insn, 4);
13075 buf += 4;
13076
13077 md_number_to_chars ((char *) buf, 0, 4);
13078 buf += 4;
13079 }
13080
13081 uncond:
13082 if (mips_pic == NO_PIC)
13083 {
13084 /* j or jal. */
13085 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13086 ? 0x0c000000 : 0x08000000);
13087 exp.X_op = O_symbol;
13088 exp.X_add_symbol = fragp->fr_symbol;
13089 exp.X_add_number = fragp->fr_offset;
13090
13091 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13092 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13093 fixp->fx_file = fragp->fr_file;
13094 fixp->fx_line = fragp->fr_line;
13095
13096 md_number_to_chars ((char *) buf, insn, 4);
13097 buf += 4;
13098 }
13099 else
13100 {
13101 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13102 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13103 exp.X_op = O_symbol;
13104 exp.X_add_symbol = fragp->fr_symbol;
13105 exp.X_add_number = fragp->fr_offset;
13106
13107 if (fragp->fr_offset)
13108 {
13109 exp.X_add_symbol = make_expr_symbol (&exp);
13110 exp.X_add_number = 0;
13111 }
13112
13113 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13114 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13115 fixp->fx_file = fragp->fr_file;
13116 fixp->fx_line = fragp->fr_line;
13117
13118 md_number_to_chars ((char *) buf, insn, 4);
13119 buf += 4;
13120
13121 if (mips_opts.isa == ISA_MIPS1)
13122 {
13123 /* nop */
13124 md_number_to_chars ((char *) buf, 0, 4);
13125 buf += 4;
13126 }
13127
13128 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13129 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13130
13131 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13132 4, &exp, 0, BFD_RELOC_LO16);
13133 fixp->fx_file = fragp->fr_file;
13134 fixp->fx_line = fragp->fr_line;
13135
13136 md_number_to_chars ((char *) buf, insn, 4);
13137 buf += 4;
13138
13139 /* j(al)r $at. */
13140 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13141 insn = 0x0020f809;
13142 else
13143 insn = 0x00200008;
13144
13145 md_number_to_chars ((char *) buf, insn, 4);
13146 buf += 4;
13147 }
13148 }
13149
13150 assert (buf == (bfd_byte *)fragp->fr_literal
13151 + fragp->fr_fix + fragp->fr_var);
13152
13153 fragp->fr_fix += fragp->fr_var;
13154
13155 return;
13156 }
13157
13158 if (RELAX_MIPS16_P (fragp->fr_subtype))
13159 {
13160 int type;
13161 register const struct mips16_immed_operand *op;
13162 bfd_boolean small, ext;
13163 offsetT val;
13164 bfd_byte *buf;
13165 unsigned long insn;
13166 bfd_boolean use_extend;
13167 unsigned short extend;
13168
13169 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13170 op = mips16_immed_operands;
13171 while (op->type != type)
13172 ++op;
13173
13174 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13175 {
13176 small = FALSE;
13177 ext = TRUE;
13178 }
13179 else
13180 {
13181 small = TRUE;
13182 ext = FALSE;
13183 }
13184
13185 resolve_symbol_value (fragp->fr_symbol);
13186 val = S_GET_VALUE (fragp->fr_symbol);
13187 if (op->pcrel)
13188 {
13189 addressT addr;
13190
13191 addr = fragp->fr_address + fragp->fr_fix;
13192
13193 /* The rules for the base address of a PC relative reloc are
13194 complicated; see mips16_extended_frag. */
13195 if (type == 'p' || type == 'q')
13196 {
13197 addr += 2;
13198 if (ext)
13199 addr += 2;
13200 /* Ignore the low bit in the target, since it will be
13201 set for a text label. */
13202 if ((val & 1) != 0)
13203 --val;
13204 }
13205 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13206 addr -= 4;
13207 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13208 addr -= 2;
13209
13210 addr &= ~ (addressT) ((1 << op->shift) - 1);
13211 val -= addr;
13212
13213 /* Make sure the section winds up with the alignment we have
13214 assumed. */
13215 if (op->shift > 0)
13216 record_alignment (asec, op->shift);
13217 }
13218
13219 if (ext
13220 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13221 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13222 as_warn_where (fragp->fr_file, fragp->fr_line,
13223 _("extended instruction in delay slot"));
13224
13225 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13226
13227 if (target_big_endian)
13228 insn = bfd_getb16 (buf);
13229 else
13230 insn = bfd_getl16 (buf);
13231
13232 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13233 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13234 small, ext, &insn, &use_extend, &extend);
13235
13236 if (use_extend)
13237 {
13238 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
13239 fragp->fr_fix += 2;
13240 buf += 2;
13241 }
13242
13243 md_number_to_chars ((char *) buf, insn, 2);
13244 fragp->fr_fix += 2;
13245 buf += 2;
13246 }
13247 else
13248 {
13249 int first, second;
13250 fixS *fixp;
13251
13252 first = RELAX_FIRST (fragp->fr_subtype);
13253 second = RELAX_SECOND (fragp->fr_subtype);
13254 fixp = (fixS *) fragp->fr_opcode;
13255
13256 /* Possibly emit a warning if we've chosen the longer option. */
13257 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13258 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13259 {
13260 const char *msg = macro_warning (fragp->fr_subtype);
13261 if (msg != 0)
13262 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13263 }
13264
13265 /* Go through all the fixups for the first sequence. Disable them
13266 (by marking them as done) if we're going to use the second
13267 sequence instead. */
13268 while (fixp
13269 && fixp->fx_frag == fragp
13270 && fixp->fx_where < fragp->fr_fix - second)
13271 {
13272 if (fragp->fr_subtype & RELAX_USE_SECOND)
13273 fixp->fx_done = 1;
13274 fixp = fixp->fx_next;
13275 }
13276
13277 /* Go through the fixups for the second sequence. Disable them if
13278 we're going to use the first sequence, otherwise adjust their
13279 addresses to account for the relaxation. */
13280 while (fixp && fixp->fx_frag == fragp)
13281 {
13282 if (fragp->fr_subtype & RELAX_USE_SECOND)
13283 fixp->fx_where -= first;
13284 else
13285 fixp->fx_done = 1;
13286 fixp = fixp->fx_next;
13287 }
13288
13289 /* Now modify the frag contents. */
13290 if (fragp->fr_subtype & RELAX_USE_SECOND)
13291 {
13292 char *start;
13293
13294 start = fragp->fr_literal + fragp->fr_fix - first - second;
13295 memmove (start, start + first, second);
13296 fragp->fr_fix -= first;
13297 }
13298 else
13299 fragp->fr_fix -= second;
13300 }
13301 }
13302
13303 #ifdef OBJ_ELF
13304
13305 /* This function is called after the relocs have been generated.
13306 We've been storing mips16 text labels as odd. Here we convert them
13307 back to even for the convenience of the debugger. */
13308
13309 void
13310 mips_frob_file_after_relocs (void)
13311 {
13312 asymbol **syms;
13313 unsigned int count, i;
13314
13315 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13316 return;
13317
13318 syms = bfd_get_outsymbols (stdoutput);
13319 count = bfd_get_symcount (stdoutput);
13320 for (i = 0; i < count; i++, syms++)
13321 {
13322 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13323 && ((*syms)->value & 1) != 0)
13324 {
13325 (*syms)->value &= ~1;
13326 /* If the symbol has an odd size, it was probably computed
13327 incorrectly, so adjust that as well. */
13328 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13329 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13330 }
13331 }
13332 }
13333
13334 #endif
13335
13336 /* This function is called whenever a label is defined. It is used
13337 when handling branch delays; if a branch has a label, we assume we
13338 can not move it. */
13339
13340 void
13341 mips_define_label (symbolS *sym)
13342 {
13343 struct insn_label_list *l;
13344
13345 if (free_insn_labels == NULL)
13346 l = (struct insn_label_list *) xmalloc (sizeof *l);
13347 else
13348 {
13349 l = free_insn_labels;
13350 free_insn_labels = l->next;
13351 }
13352
13353 l->label = sym;
13354 l->next = insn_labels;
13355 insn_labels = l;
13356 }
13357 \f
13358 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13359
13360 /* Some special processing for a MIPS ELF file. */
13361
13362 void
13363 mips_elf_final_processing (void)
13364 {
13365 /* Write out the register information. */
13366 if (mips_abi != N64_ABI)
13367 {
13368 Elf32_RegInfo s;
13369
13370 s.ri_gprmask = mips_gprmask;
13371 s.ri_cprmask[0] = mips_cprmask[0];
13372 s.ri_cprmask[1] = mips_cprmask[1];
13373 s.ri_cprmask[2] = mips_cprmask[2];
13374 s.ri_cprmask[3] = mips_cprmask[3];
13375 /* The gp_value field is set by the MIPS ELF backend. */
13376
13377 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13378 ((Elf32_External_RegInfo *)
13379 mips_regmask_frag));
13380 }
13381 else
13382 {
13383 Elf64_Internal_RegInfo s;
13384
13385 s.ri_gprmask = mips_gprmask;
13386 s.ri_pad = 0;
13387 s.ri_cprmask[0] = mips_cprmask[0];
13388 s.ri_cprmask[1] = mips_cprmask[1];
13389 s.ri_cprmask[2] = mips_cprmask[2];
13390 s.ri_cprmask[3] = mips_cprmask[3];
13391 /* The gp_value field is set by the MIPS ELF backend. */
13392
13393 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13394 ((Elf64_External_RegInfo *)
13395 mips_regmask_frag));
13396 }
13397
13398 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13399 sort of BFD interface for this. */
13400 if (mips_any_noreorder)
13401 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13402 if (mips_pic != NO_PIC)
13403 {
13404 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13405 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13406 }
13407 if (mips_abicalls)
13408 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13409
13410 /* Set MIPS ELF flags for ASEs. */
13411 if (file_ase_mips16)
13412 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
13413 #if 0 /* XXX FIXME */
13414 if (file_ase_mips3d)
13415 elf_elfheader (stdoutput)->e_flags |= ???;
13416 #endif
13417 if (file_ase_mdmx)
13418 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
13419
13420 /* Set the MIPS ELF ABI flags. */
13421 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
13422 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
13423 else if (mips_abi == O64_ABI)
13424 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
13425 else if (mips_abi == EABI_ABI)
13426 {
13427 if (!file_mips_gp32)
13428 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13429 else
13430 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13431 }
13432 else if (mips_abi == N32_ABI)
13433 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13434
13435 /* Nothing to do for N64_ABI. */
13436
13437 if (mips_32bitmode)
13438 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13439 }
13440
13441 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13442 \f
13443 typedef struct proc {
13444 symbolS *func_sym;
13445 symbolS *func_end_sym;
13446 unsigned long reg_mask;
13447 unsigned long reg_offset;
13448 unsigned long fpreg_mask;
13449 unsigned long fpreg_offset;
13450 unsigned long frame_offset;
13451 unsigned long frame_reg;
13452 unsigned long pc_reg;
13453 } procS;
13454
13455 static procS cur_proc;
13456 static procS *cur_proc_ptr;
13457 static int numprocs;
13458
13459 /* Fill in an rs_align_code fragment. */
13460
13461 void
13462 mips_handle_align (fragS *fragp)
13463 {
13464 if (fragp->fr_type != rs_align_code)
13465 return;
13466
13467 if (mips_opts.mips16)
13468 {
13469 static const unsigned char be_nop[] = { 0x65, 0x00 };
13470 static const unsigned char le_nop[] = { 0x00, 0x65 };
13471
13472 int bytes;
13473 char *p;
13474
13475 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13476 p = fragp->fr_literal + fragp->fr_fix;
13477
13478 if (bytes & 1)
13479 {
13480 *p++ = 0;
13481 fragp->fr_fix++;
13482 }
13483
13484 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13485 fragp->fr_var = 2;
13486 }
13487
13488 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
13489 }
13490
13491 static void
13492 md_obj_begin (void)
13493 {
13494 }
13495
13496 static void
13497 md_obj_end (void)
13498 {
13499 /* check for premature end, nesting errors, etc */
13500 if (cur_proc_ptr)
13501 as_warn (_("missing .end at end of assembly"));
13502 }
13503
13504 static long
13505 get_number (void)
13506 {
13507 int negative = 0;
13508 long val = 0;
13509
13510 if (*input_line_pointer == '-')
13511 {
13512 ++input_line_pointer;
13513 negative = 1;
13514 }
13515 if (!ISDIGIT (*input_line_pointer))
13516 as_bad (_("expected simple number"));
13517 if (input_line_pointer[0] == '0')
13518 {
13519 if (input_line_pointer[1] == 'x')
13520 {
13521 input_line_pointer += 2;
13522 while (ISXDIGIT (*input_line_pointer))
13523 {
13524 val <<= 4;
13525 val |= hex_value (*input_line_pointer++);
13526 }
13527 return negative ? -val : val;
13528 }
13529 else
13530 {
13531 ++input_line_pointer;
13532 while (ISDIGIT (*input_line_pointer))
13533 {
13534 val <<= 3;
13535 val |= *input_line_pointer++ - '0';
13536 }
13537 return negative ? -val : val;
13538 }
13539 }
13540 if (!ISDIGIT (*input_line_pointer))
13541 {
13542 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13543 *input_line_pointer, *input_line_pointer);
13544 as_warn (_("invalid number"));
13545 return -1;
13546 }
13547 while (ISDIGIT (*input_line_pointer))
13548 {
13549 val *= 10;
13550 val += *input_line_pointer++ - '0';
13551 }
13552 return negative ? -val : val;
13553 }
13554
13555 /* The .file directive; just like the usual .file directive, but there
13556 is an initial number which is the ECOFF file index. In the non-ECOFF
13557 case .file implies DWARF-2. */
13558
13559 static void
13560 s_mips_file (int x ATTRIBUTE_UNUSED)
13561 {
13562 static int first_file_directive = 0;
13563
13564 if (ECOFF_DEBUGGING)
13565 {
13566 get_number ();
13567 s_app_file (0);
13568 }
13569 else
13570 {
13571 char *filename;
13572
13573 filename = dwarf2_directive_file (0);
13574
13575 /* Versions of GCC up to 3.1 start files with a ".file"
13576 directive even for stabs output. Make sure that this
13577 ".file" is handled. Note that you need a version of GCC
13578 after 3.1 in order to support DWARF-2 on MIPS. */
13579 if (filename != NULL && ! first_file_directive)
13580 {
13581 (void) new_logical_line (filename, -1);
13582 s_app_file_string (filename, 0);
13583 }
13584 first_file_directive = 1;
13585 }
13586 }
13587
13588 /* The .loc directive, implying DWARF-2. */
13589
13590 static void
13591 s_mips_loc (int x ATTRIBUTE_UNUSED)
13592 {
13593 if (!ECOFF_DEBUGGING)
13594 dwarf2_directive_loc (0);
13595 }
13596
13597 /* The .end directive. */
13598
13599 static void
13600 s_mips_end (int x ATTRIBUTE_UNUSED)
13601 {
13602 symbolS *p;
13603
13604 /* Following functions need their own .frame and .cprestore directives. */
13605 mips_frame_reg_valid = 0;
13606 mips_cprestore_valid = 0;
13607
13608 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13609 {
13610 p = get_symbol ();
13611 demand_empty_rest_of_line ();
13612 }
13613 else
13614 p = NULL;
13615
13616 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13617 as_warn (_(".end not in text section"));
13618
13619 if (!cur_proc_ptr)
13620 {
13621 as_warn (_(".end directive without a preceding .ent directive."));
13622 demand_empty_rest_of_line ();
13623 return;
13624 }
13625
13626 if (p != NULL)
13627 {
13628 assert (S_GET_NAME (p));
13629 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
13630 as_warn (_(".end symbol does not match .ent symbol."));
13631
13632 if (debug_type == DEBUG_STABS)
13633 stabs_generate_asm_endfunc (S_GET_NAME (p),
13634 S_GET_NAME (p));
13635 }
13636 else
13637 as_warn (_(".end directive missing or unknown symbol"));
13638
13639 #ifdef OBJ_ELF
13640 /* Create an expression to calculate the size of the function. */
13641 if (p && cur_proc_ptr)
13642 {
13643 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13644 expressionS *exp = xmalloc (sizeof (expressionS));
13645
13646 obj->size = exp;
13647 exp->X_op = O_subtract;
13648 exp->X_add_symbol = symbol_temp_new_now ();
13649 exp->X_op_symbol = p;
13650 exp->X_add_number = 0;
13651
13652 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13653 }
13654
13655 /* Generate a .pdr section. */
13656 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13657 && mips_flag_pdr)
13658 {
13659 segT saved_seg = now_seg;
13660 subsegT saved_subseg = now_subseg;
13661 valueT dot;
13662 expressionS exp;
13663 char *fragp;
13664
13665 dot = frag_now_fix ();
13666
13667 #ifdef md_flush_pending_output
13668 md_flush_pending_output ();
13669 #endif
13670
13671 assert (pdr_seg);
13672 subseg_set (pdr_seg, 0);
13673
13674 /* Write the symbol. */
13675 exp.X_op = O_symbol;
13676 exp.X_add_symbol = p;
13677 exp.X_add_number = 0;
13678 emit_expr (&exp, 4);
13679
13680 fragp = frag_more (7 * 4);
13681
13682 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13683 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13684 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13685 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13686 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13687 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13688 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
13689
13690 subseg_set (saved_seg, saved_subseg);
13691 }
13692 #endif /* OBJ_ELF */
13693
13694 cur_proc_ptr = NULL;
13695 }
13696
13697 /* The .aent and .ent directives. */
13698
13699 static void
13700 s_mips_ent (int aent)
13701 {
13702 symbolS *symbolP;
13703
13704 symbolP = get_symbol ();
13705 if (*input_line_pointer == ',')
13706 ++input_line_pointer;
13707 SKIP_WHITESPACE ();
13708 if (ISDIGIT (*input_line_pointer)
13709 || *input_line_pointer == '-')
13710 get_number ();
13711
13712 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
13713 as_warn (_(".ent or .aent not in text section."));
13714
13715 if (!aent && cur_proc_ptr)
13716 as_warn (_("missing .end"));
13717
13718 if (!aent)
13719 {
13720 /* This function needs its own .frame and .cprestore directives. */
13721 mips_frame_reg_valid = 0;
13722 mips_cprestore_valid = 0;
13723
13724 cur_proc_ptr = &cur_proc;
13725 memset (cur_proc_ptr, '\0', sizeof (procS));
13726
13727 cur_proc_ptr->func_sym = symbolP;
13728
13729 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13730
13731 ++numprocs;
13732
13733 if (debug_type == DEBUG_STABS)
13734 stabs_generate_asm_func (S_GET_NAME (symbolP),
13735 S_GET_NAME (symbolP));
13736 }
13737
13738 demand_empty_rest_of_line ();
13739 }
13740
13741 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13742 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13743 s_mips_frame is used so that we can set the PDR information correctly.
13744 We can't use the ecoff routines because they make reference to the ecoff
13745 symbol table (in the mdebug section). */
13746
13747 static void
13748 s_mips_frame (int ignore ATTRIBUTE_UNUSED)
13749 {
13750 #ifdef OBJ_ELF
13751 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13752 {
13753 long val;
13754
13755 if (cur_proc_ptr == (procS *) NULL)
13756 {
13757 as_warn (_(".frame outside of .ent"));
13758 demand_empty_rest_of_line ();
13759 return;
13760 }
13761
13762 cur_proc_ptr->frame_reg = tc_get_register (1);
13763
13764 SKIP_WHITESPACE ();
13765 if (*input_line_pointer++ != ','
13766 || get_absolute_expression_and_terminator (&val) != ',')
13767 {
13768 as_warn (_("Bad .frame directive"));
13769 --input_line_pointer;
13770 demand_empty_rest_of_line ();
13771 return;
13772 }
13773
13774 cur_proc_ptr->frame_offset = val;
13775 cur_proc_ptr->pc_reg = tc_get_register (0);
13776
13777 demand_empty_rest_of_line ();
13778 }
13779 else
13780 #endif /* OBJ_ELF */
13781 s_ignore (ignore);
13782 }
13783
13784 /* The .fmask and .mask directives. If the mdebug section is present
13785 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13786 embedded targets, s_mips_mask is used so that we can set the PDR
13787 information correctly. We can't use the ecoff routines because they
13788 make reference to the ecoff symbol table (in the mdebug section). */
13789
13790 static void
13791 s_mips_mask (int reg_type)
13792 {
13793 #ifdef OBJ_ELF
13794 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13795 {
13796 long mask, off;
13797
13798 if (cur_proc_ptr == (procS *) NULL)
13799 {
13800 as_warn (_(".mask/.fmask outside of .ent"));
13801 demand_empty_rest_of_line ();
13802 return;
13803 }
13804
13805 if (get_absolute_expression_and_terminator (&mask) != ',')
13806 {
13807 as_warn (_("Bad .mask/.fmask directive"));
13808 --input_line_pointer;
13809 demand_empty_rest_of_line ();
13810 return;
13811 }
13812
13813 off = get_absolute_expression ();
13814
13815 if (reg_type == 'F')
13816 {
13817 cur_proc_ptr->fpreg_mask = mask;
13818 cur_proc_ptr->fpreg_offset = off;
13819 }
13820 else
13821 {
13822 cur_proc_ptr->reg_mask = mask;
13823 cur_proc_ptr->reg_offset = off;
13824 }
13825
13826 demand_empty_rest_of_line ();
13827 }
13828 else
13829 #endif /* OBJ_ELF */
13830 s_ignore (reg_type);
13831 }
13832
13833 /* A table describing all the processors gas knows about. Names are
13834 matched in the order listed.
13835
13836 To ease comparison, please keep this table in the same order as
13837 gcc's mips_cpu_info_table[]. */
13838 static const struct mips_cpu_info mips_cpu_info_table[] =
13839 {
13840 /* Entries for generic ISAs */
13841 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13842 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13843 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13844 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13845 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13846 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13847 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
13848 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13849 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
13850
13851 /* MIPS I */
13852 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13853 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13854 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13855
13856 /* MIPS II */
13857 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13858
13859 /* MIPS III */
13860 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13861 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13862 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13863 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13864 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13865 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13866 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
13867 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13868 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13869 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13870 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13871 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13872
13873 /* MIPS IV */
13874 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13875 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13876 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13877 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13878 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13879 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
13880 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13881 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13882 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13883 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13884 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13885 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
13886 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
13887
13888 /* MIPS 32 */
13889 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
13890 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13891 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
13892
13893 /* MIPS 64 */
13894 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13895 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
13896
13897 /* Broadcom SB-1 CPU core */
13898 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
13899
13900 /* End marker */
13901 { NULL, 0, 0, 0 }
13902 };
13903
13904
13905 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13906 with a final "000" replaced by "k". Ignore case.
13907
13908 Note: this function is shared between GCC and GAS. */
13909
13910 static bfd_boolean
13911 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
13912 {
13913 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13914 given++, canonical++;
13915
13916 return ((*given == 0 && *canonical == 0)
13917 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13918 }
13919
13920
13921 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13922 CPU name. We've traditionally allowed a lot of variation here.
13923
13924 Note: this function is shared between GCC and GAS. */
13925
13926 static bfd_boolean
13927 mips_matching_cpu_name_p (const char *canonical, const char *given)
13928 {
13929 /* First see if the name matches exactly, or with a final "000"
13930 turned into "k". */
13931 if (mips_strict_matching_cpu_name_p (canonical, given))
13932 return TRUE;
13933
13934 /* If not, try comparing based on numerical designation alone.
13935 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13936 if (TOLOWER (*given) == 'r')
13937 given++;
13938 if (!ISDIGIT (*given))
13939 return FALSE;
13940
13941 /* Skip over some well-known prefixes in the canonical name,
13942 hoping to find a number there too. */
13943 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13944 canonical += 2;
13945 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13946 canonical += 2;
13947 else if (TOLOWER (canonical[0]) == 'r')
13948 canonical += 1;
13949
13950 return mips_strict_matching_cpu_name_p (canonical, given);
13951 }
13952
13953
13954 /* Parse an option that takes the name of a processor as its argument.
13955 OPTION is the name of the option and CPU_STRING is the argument.
13956 Return the corresponding processor enumeration if the CPU_STRING is
13957 recognized, otherwise report an error and return null.
13958
13959 A similar function exists in GCC. */
13960
13961 static const struct mips_cpu_info *
13962 mips_parse_cpu (const char *option, const char *cpu_string)
13963 {
13964 const struct mips_cpu_info *p;
13965
13966 /* 'from-abi' selects the most compatible architecture for the given
13967 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13968 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13969 version. Look first at the -mgp options, if given, otherwise base
13970 the choice on MIPS_DEFAULT_64BIT.
13971
13972 Treat NO_ABI like the EABIs. One reason to do this is that the
13973 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13974 architecture. This code picks MIPS I for 'mips' and MIPS III for
13975 'mips64', just as we did in the days before 'from-abi'. */
13976 if (strcasecmp (cpu_string, "from-abi") == 0)
13977 {
13978 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13979 return mips_cpu_info_from_isa (ISA_MIPS1);
13980
13981 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13982 return mips_cpu_info_from_isa (ISA_MIPS3);
13983
13984 if (file_mips_gp32 >= 0)
13985 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13986
13987 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13988 ? ISA_MIPS3
13989 : ISA_MIPS1);
13990 }
13991
13992 /* 'default' has traditionally been a no-op. Probably not very useful. */
13993 if (strcasecmp (cpu_string, "default") == 0)
13994 return 0;
13995
13996 for (p = mips_cpu_info_table; p->name != 0; p++)
13997 if (mips_matching_cpu_name_p (p->name, cpu_string))
13998 return p;
13999
14000 as_bad ("Bad value (%s) for %s", cpu_string, option);
14001 return 0;
14002 }
14003
14004 /* Return the canonical processor information for ISA (a member of the
14005 ISA_MIPS* enumeration). */
14006
14007 static const struct mips_cpu_info *
14008 mips_cpu_info_from_isa (int isa)
14009 {
14010 int i;
14011
14012 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14013 if (mips_cpu_info_table[i].is_isa
14014 && isa == mips_cpu_info_table[i].isa)
14015 return (&mips_cpu_info_table[i]);
14016
14017 return NULL;
14018 }
14019
14020 static const struct mips_cpu_info *
14021 mips_cpu_info_from_arch (int arch)
14022 {
14023 int i;
14024
14025 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14026 if (arch == mips_cpu_info_table[i].cpu)
14027 return (&mips_cpu_info_table[i]);
14028
14029 return NULL;
14030 }
14031 \f
14032 static void
14033 show (FILE *stream, const char *string, int *col_p, int *first_p)
14034 {
14035 if (*first_p)
14036 {
14037 fprintf (stream, "%24s", "");
14038 *col_p = 24;
14039 }
14040 else
14041 {
14042 fprintf (stream, ", ");
14043 *col_p += 2;
14044 }
14045
14046 if (*col_p + strlen (string) > 72)
14047 {
14048 fprintf (stream, "\n%24s", "");
14049 *col_p = 24;
14050 }
14051
14052 fprintf (stream, "%s", string);
14053 *col_p += strlen (string);
14054
14055 *first_p = 0;
14056 }
14057
14058 void
14059 md_show_usage (FILE *stream)
14060 {
14061 int column, first;
14062 size_t i;
14063
14064 fprintf (stream, _("\
14065 MIPS options:\n\
14066 -EB generate big endian output\n\
14067 -EL generate little endian output\n\
14068 -g, -g2 do not remove unneeded NOPs or swap branches\n\
14069 -G NUM allow referencing objects up to NUM bytes\n\
14070 implicitly with the gp register [default 8]\n"));
14071 fprintf (stream, _("\
14072 -mips1 generate MIPS ISA I instructions\n\
14073 -mips2 generate MIPS ISA II instructions\n\
14074 -mips3 generate MIPS ISA III instructions\n\
14075 -mips4 generate MIPS ISA IV instructions\n\
14076 -mips5 generate MIPS ISA V instructions\n\
14077 -mips32 generate MIPS32 ISA instructions\n\
14078 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
14079 -mips64 generate MIPS64 ISA instructions\n\
14080 -mips64r2 generate MIPS64 release 2 ISA instructions\n\
14081 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14082
14083 first = 1;
14084
14085 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14086 show (stream, mips_cpu_info_table[i].name, &column, &first);
14087 show (stream, "from-abi", &column, &first);
14088 fputc ('\n', stream);
14089
14090 fprintf (stream, _("\
14091 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14092 -no-mCPU don't generate code specific to CPU.\n\
14093 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14094
14095 first = 1;
14096
14097 show (stream, "3900", &column, &first);
14098 show (stream, "4010", &column, &first);
14099 show (stream, "4100", &column, &first);
14100 show (stream, "4650", &column, &first);
14101 fputc ('\n', stream);
14102
14103 fprintf (stream, _("\
14104 -mips16 generate mips16 instructions\n\
14105 -no-mips16 do not generate mips16 instructions\n"));
14106 fprintf (stream, _("\
14107 -mfix-vr4120 work around certain VR4120 errata\n\
14108 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14109 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14110 -mno-shared optimize output for executables\n\
14111 -msym32 assume all symbols have 32-bit values\n\
14112 -O0 remove unneeded NOPs, do not swap branches\n\
14113 -O remove unneeded NOPs and swap branches\n\
14114 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
14115 --trap, --no-break trap exception on div by 0 and mult overflow\n\
14116 --break, --no-trap break exception on div by 0 and mult overflow\n"));
14117 #ifdef OBJ_ELF
14118 fprintf (stream, _("\
14119 -KPIC, -call_shared generate SVR4 position independent code\n\
14120 -non_shared do not generate position independent code\n\
14121 -xgot assume a 32 bit GOT\n\
14122 -mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
14123 -mshared, -mno-shared disable/enable .cpload optimization for\n\
14124 non-shared code\n\
14125 -mabi=ABI create ABI conformant object file for:\n"));
14126
14127 first = 1;
14128
14129 show (stream, "32", &column, &first);
14130 show (stream, "o64", &column, &first);
14131 show (stream, "n32", &column, &first);
14132 show (stream, "64", &column, &first);
14133 show (stream, "eabi", &column, &first);
14134
14135 fputc ('\n', stream);
14136
14137 fprintf (stream, _("\
14138 -32 create o32 ABI object file (default)\n\
14139 -n32 create n32 ABI object file\n\
14140 -64 create 64 ABI object file\n"));
14141 #endif
14142 }
14143
14144 enum dwarf2_format
14145 mips_dwarf2_format (void)
14146 {
14147 if (mips_abi == N64_ABI)
14148 {
14149 #ifdef TE_IRIX
14150 return dwarf2_format_64bit_irix;
14151 #else
14152 return dwarf2_format_64bit;
14153 #endif
14154 }
14155 else
14156 return dwarf2_format_32bit;
14157 }
14158
14159 int
14160 mips_dwarf2_addr_size (void)
14161 {
14162 if (mips_abi == N64_ABI)
14163 return 8;
14164 else
14165 return 4;
14166 }