* elf32-mips.c (mips_elf_generic_reloc): New Function.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-ctype.h"
30
31 #ifdef USE_STDARG
32 #include <stdarg.h>
33 #endif
34 #ifdef USE_VARARGS
35 #include <varargs.h>
36 #endif
37
38 #include "opcode/mips.h"
39 #include "itbl-ops.h"
40 #include "dwarf2dbg.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too. */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about. */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 int mips_flag_mdebug = -1;
81
82 #include "ecoff.h"
83
84 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85 static char *mips_regmask_frag;
86 #endif
87
88 #define ZERO 0
89 #define AT 1
90 #define TREG 24
91 #define PIC_CALL_REG 25
92 #define KT0 26
93 #define KT1 27
94 #define GP 28
95 #define SP 29
96 #define FP 30
97 #define RA 31
98
99 #define ILLEGAL_REG (32)
100
101 /* Allow override of standard little-endian ECOFF format. */
102
103 #ifndef ECOFF_LITTLE_FORMAT
104 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105 #endif
106
107 extern int target_big_endian;
108
109 /* The name of the readonly data section. */
110 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111 ? ".data" \
112 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113 ? ".rdata" \
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
119
120 /* The ABI to use. */
121 enum mips_abi_level
122 {
123 NO_ABI = 0,
124 O32_ABI,
125 O64_ABI,
126 N32_ABI,
127 N64_ABI,
128 EABI_ABI
129 };
130
131 /* MIPS ABI we are using for this output file. */
132 static enum mips_abi_level mips_abi = NO_ABI;
133
134 /* Whether or not we have code that can call pic code. */
135 int mips_abicalls = FALSE;
136
137 /* This is the set of options which may be modified by the .set
138 pseudo-op. We use a struct so that .set push and .set pop are more
139 reliable. */
140
141 struct mips_set_options
142 {
143 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
144 if it has not been initialized. Changed by `.set mipsN', and the
145 -mipsN command line option, and the default CPU. */
146 int isa;
147 /* Enabled Application Specific Extensions (ASEs). These are set to -1
148 if they have not been initialized. Changed by `.set <asename>', by
149 command line options, and based on the default architecture. */
150 int ase_mips3d;
151 int ase_mdmx;
152 /* Whether we are assembling for the mips16 processor. 0 if we are
153 not, 1 if we are, and -1 if the value has not been initialized.
154 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
155 -nomips16 command line options, and the default CPU. */
156 int mips16;
157 /* Non-zero if we should not reorder instructions. Changed by `.set
158 reorder' and `.set noreorder'. */
159 int noreorder;
160 /* Non-zero if we should not permit the $at ($1) register to be used
161 in instructions. Changed by `.set at' and `.set noat'. */
162 int noat;
163 /* Non-zero if we should warn when a macro instruction expands into
164 more than one machine instruction. Changed by `.set nomacro' and
165 `.set macro'. */
166 int warn_about_macros;
167 /* Non-zero if we should not move instructions. Changed by `.set
168 move', `.set volatile', `.set nomove', and `.set novolatile'. */
169 int nomove;
170 /* Non-zero if we should not optimize branches by moving the target
171 of the branch into the delay slot. Actually, we don't perform
172 this optimization anyhow. Changed by `.set bopt' and `.set
173 nobopt'. */
174 int nobopt;
175 /* Non-zero if we should not autoextend mips16 instructions.
176 Changed by `.set autoextend' and `.set noautoextend'. */
177 int noautoextend;
178 /* Restrict general purpose registers and floating point registers
179 to 32 bit. This is initially determined when -mgp32 or -mfp32
180 is passed but can changed if the assembler code uses .set mipsN. */
181 int gp32;
182 int fp32;
183 };
184
185 /* True if -mgp32 was passed. */
186 static int file_mips_gp32 = -1;
187
188 /* True if -mfp32 was passed. */
189 static int file_mips_fp32 = -1;
190
191 /* This is the struct we use to hold the current set of options. Note
192 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
193 -1 to indicate that they have not been initialized. */
194
195 static struct mips_set_options mips_opts =
196 {
197 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
198 };
199
200 /* These variables are filled in with the masks of registers used.
201 The object format code reads them and puts them in the appropriate
202 place. */
203 unsigned long mips_gprmask;
204 unsigned long mips_cprmask[4];
205
206 /* MIPS ISA we are using for this output file. */
207 static int file_mips_isa = ISA_UNKNOWN;
208
209 /* True if -mips16 was passed or implied by arguments passed on the
210 command line (e.g., by -march). */
211 static int file_ase_mips16;
212
213 /* True if -mips3d was passed or implied by arguments passed on the
214 command line (e.g., by -march). */
215 static int file_ase_mips3d;
216
217 /* True if -mdmx was passed or implied by arguments passed on the
218 command line (e.g., by -march). */
219 static int file_ase_mdmx;
220
221 /* The argument of the -march= flag. The architecture we are assembling. */
222 static int mips_arch = CPU_UNKNOWN;
223 static const char *mips_arch_string;
224 static const struct mips_cpu_info *mips_arch_info;
225
226 /* The argument of the -mtune= flag. The architecture for which we
227 are optimizing. */
228 static int mips_tune = CPU_UNKNOWN;
229 static const char *mips_tune_string;
230 static const struct mips_cpu_info *mips_tune_info;
231
232 /* True when generating 32-bit code for a 64-bit processor. */
233 static int mips_32bitmode = 0;
234
235 /* Some ISA's have delay slots for instructions which read or write
236 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
237 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
238 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
239 delay slot in this ISA. The uses of this macro assume that any
240 ISA that has delay slots for one of these, has them for all. They
241 also assume that ISAs which don't have delays for these insns, don't
242 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
243 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
244 (ISA) == ISA_MIPS1 \
245 || (ISA) == ISA_MIPS2 \
246 || (ISA) == ISA_MIPS3 \
247 )
248
249 /* True if the given ABI requires 32-bit registers. */
250 #define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
251
252 /* Likewise 64-bit registers. */
253 #define ABI_NEEDS_64BIT_REGS(ABI) \
254 ((ABI) == N32_ABI \
255 || (ABI) == N64_ABI \
256 || (ABI) == O64_ABI)
257
258 /* Return true if ISA supports 64 bit gp register instructions. */
259 #define ISA_HAS_64BIT_REGS(ISA) ( \
260 (ISA) == ISA_MIPS3 \
261 || (ISA) == ISA_MIPS4 \
262 || (ISA) == ISA_MIPS5 \
263 || (ISA) == ISA_MIPS64 \
264 )
265
266 /* Return true if ISA supports 64-bit right rotate (dror et al.)
267 instructions. */
268 #define ISA_HAS_DROR(ISA) ( \
269 0 \
270 )
271
272 /* Return true if ISA supports 32-bit right rotate (ror et al.)
273 instructions. */
274 #define ISA_HAS_ROR(ISA) ( \
275 (ISA) == ISA_MIPS32R2 \
276 )
277
278 #define HAVE_32BIT_GPRS \
279 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
280
281 #define HAVE_32BIT_FPRS \
282 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
283
284 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
285 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
286
287 #define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
288
289 #define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
290
291 /* We can only have 64bit addresses if the object file format
292 supports it. */
293 #define HAVE_32BIT_ADDRESSES \
294 (HAVE_32BIT_GPRS \
295 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
296 || ! HAVE_64BIT_OBJECTS) \
297 && mips_pic != EMBEDDED_PIC))
298
299 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
300 #define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
301 || HAVE_64BIT_GPRS)
302
303 /* Return true if the given CPU supports the MIPS16 ASE. */
304 #define CPU_HAS_MIPS16(cpu) \
305 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
306 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
307
308 /* Return true if the given CPU supports the MIPS3D ASE. */
309 #define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
310 )
311
312 /* Return true if the given CPU supports the MDMX ASE. */
313 #define CPU_HAS_MDMX(cpu) (FALSE \
314 )
315
316 /* True if CPU has a dror instruction. */
317 #define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
318
319 /* True if CPU has a ror instruction. */
320 #define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
321
322 /* Whether the processor uses hardware interlocks to protect
323 reads from the HI and LO registers, and thus does not
324 require nops to be inserted. */
325
326 #define hilo_interlocks (mips_arch == CPU_R4010 \
327 || mips_arch == CPU_VR5500 \
328 || mips_arch == CPU_SB1 \
329 )
330
331 /* Whether the processor uses hardware interlocks to protect reads
332 from the GPRs, and thus does not require nops to be inserted. */
333 #define gpr_interlocks \
334 (mips_opts.isa != ISA_MIPS1 \
335 || mips_arch == CPU_VR5400 \
336 || mips_arch == CPU_VR5500 \
337 || mips_arch == CPU_R3900)
338
339 /* As with other "interlocks" this is used by hardware that has FP
340 (co-processor) interlocks. */
341 /* Itbl support may require additional care here. */
342 #define cop_interlocks (mips_arch == CPU_R4300 \
343 || mips_arch == CPU_VR5400 \
344 || mips_arch == CPU_VR5500 \
345 || mips_arch == CPU_SB1 \
346 )
347
348 /* Is this a mfhi or mflo instruction? */
349 #define MF_HILO_INSN(PINFO) \
350 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
351
352 /* MIPS PIC level. */
353
354 enum mips_pic_level mips_pic;
355
356 /* Warn about all NOPS that the assembler generates. */
357 static int warn_nops = 0;
358
359 /* 1 if we should generate 32 bit offsets from the $gp register in
360 SVR4_PIC mode. Currently has no meaning in other modes. */
361 static int mips_big_got = 0;
362
363 /* 1 if trap instructions should used for overflow rather than break
364 instructions. */
365 static int mips_trap = 0;
366
367 /* 1 if double width floating point constants should not be constructed
368 by assembling two single width halves into two single width floating
369 point registers which just happen to alias the double width destination
370 register. On some architectures this aliasing can be disabled by a bit
371 in the status register, and the setting of this bit cannot be determined
372 automatically at assemble time. */
373 static int mips_disable_float_construction;
374
375 /* Non-zero if any .set noreorder directives were used. */
376
377 static int mips_any_noreorder;
378
379 /* Non-zero if nops should be inserted when the register referenced in
380 an mfhi/mflo instruction is read in the next two instructions. */
381 static int mips_7000_hilo_fix;
382
383 /* The size of the small data section. */
384 static unsigned int g_switch_value = 8;
385 /* Whether the -G option was used. */
386 static int g_switch_seen = 0;
387
388 #define N_RMASK 0xc4
389 #define N_VFP 0xd4
390
391 /* If we can determine in advance that GP optimization won't be
392 possible, we can skip the relaxation stuff that tries to produce
393 GP-relative references. This makes delay slot optimization work
394 better.
395
396 This function can only provide a guess, but it seems to work for
397 gcc output. It needs to guess right for gcc, otherwise gcc
398 will put what it thinks is a GP-relative instruction in a branch
399 delay slot.
400
401 I don't know if a fix is needed for the SVR4_PIC mode. I've only
402 fixed it for the non-PIC mode. KR 95/04/07 */
403 static int nopic_need_relax PARAMS ((symbolS *, int));
404
405 /* handle of the OPCODE hash table */
406 static struct hash_control *op_hash = NULL;
407
408 /* The opcode hash table we use for the mips16. */
409 static struct hash_control *mips16_op_hash = NULL;
410
411 /* This array holds the chars that always start a comment. If the
412 pre-processor is disabled, these aren't very useful */
413 const char comment_chars[] = "#";
414
415 /* This array holds the chars that only start a comment at the beginning of
416 a line. If the line seems to have the form '# 123 filename'
417 .line and .file directives will appear in the pre-processed output */
418 /* Note that input_file.c hand checks for '#' at the beginning of the
419 first line of the input file. This is because the compiler outputs
420 #NO_APP at the beginning of its output. */
421 /* Also note that C style comments are always supported. */
422 const char line_comment_chars[] = "#";
423
424 /* This array holds machine specific line separator characters. */
425 const char line_separator_chars[] = ";";
426
427 /* Chars that can be used to separate mant from exp in floating point nums */
428 const char EXP_CHARS[] = "eE";
429
430 /* Chars that mean this number is a floating point constant */
431 /* As in 0f12.456 */
432 /* or 0d1.2345e12 */
433 const char FLT_CHARS[] = "rRsSfFdDxXpP";
434
435 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
436 changed in read.c . Ideally it shouldn't have to know about it at all,
437 but nothing is ideal around here.
438 */
439
440 static char *insn_error;
441
442 static int auto_align = 1;
443
444 /* When outputting SVR4 PIC code, the assembler needs to know the
445 offset in the stack frame from which to restore the $gp register.
446 This is set by the .cprestore pseudo-op, and saved in this
447 variable. */
448 static offsetT mips_cprestore_offset = -1;
449
450 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
451 more optimizations, it can use a register value instead of a memory-saved
452 offset and even an other register than $gp as global pointer. */
453 static offsetT mips_cpreturn_offset = -1;
454 static int mips_cpreturn_register = -1;
455 static int mips_gp_register = GP;
456 static int mips_gprel_offset = 0;
457
458 /* Whether mips_cprestore_offset has been set in the current function
459 (or whether it has already been warned about, if not). */
460 static int mips_cprestore_valid = 0;
461
462 /* This is the register which holds the stack frame, as set by the
463 .frame pseudo-op. This is needed to implement .cprestore. */
464 static int mips_frame_reg = SP;
465
466 /* Whether mips_frame_reg has been set in the current function
467 (or whether it has already been warned about, if not). */
468 static int mips_frame_reg_valid = 0;
469
470 /* To output NOP instructions correctly, we need to keep information
471 about the previous two instructions. */
472
473 /* Whether we are optimizing. The default value of 2 means to remove
474 unneeded NOPs and swap branch instructions when possible. A value
475 of 1 means to not swap branches. A value of 0 means to always
476 insert NOPs. */
477 static int mips_optimize = 2;
478
479 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
480 equivalent to seeing no -g option at all. */
481 static int mips_debug = 0;
482
483 /* The previous instruction. */
484 static struct mips_cl_insn prev_insn;
485
486 /* The instruction before prev_insn. */
487 static struct mips_cl_insn prev_prev_insn;
488
489 /* If we don't want information for prev_insn or prev_prev_insn, we
490 point the insn_mo field at this dummy integer. */
491 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
492
493 /* Non-zero if prev_insn is valid. */
494 static int prev_insn_valid;
495
496 /* The frag for the previous instruction. */
497 static struct frag *prev_insn_frag;
498
499 /* The offset into prev_insn_frag for the previous instruction. */
500 static long prev_insn_where;
501
502 /* The reloc type for the previous instruction, if any. */
503 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
504
505 /* The reloc for the previous instruction, if any. */
506 static fixS *prev_insn_fixp[3];
507
508 /* Non-zero if the previous instruction was in a delay slot. */
509 static int prev_insn_is_delay_slot;
510
511 /* Non-zero if the previous instruction was in a .set noreorder. */
512 static int prev_insn_unreordered;
513
514 /* Non-zero if the previous instruction uses an extend opcode (if
515 mips16). */
516 static int prev_insn_extended;
517
518 /* Non-zero if the previous previous instruction was in a .set
519 noreorder. */
520 static int prev_prev_insn_unreordered;
521
522 /* If this is set, it points to a frag holding nop instructions which
523 were inserted before the start of a noreorder section. If those
524 nops turn out to be unnecessary, the size of the frag can be
525 decreased. */
526 static fragS *prev_nop_frag;
527
528 /* The number of nop instructions we created in prev_nop_frag. */
529 static int prev_nop_frag_holds;
530
531 /* The number of nop instructions that we know we need in
532 prev_nop_frag. */
533 static int prev_nop_frag_required;
534
535 /* The number of instructions we've seen since prev_nop_frag. */
536 static int prev_nop_frag_since;
537
538 /* For ECOFF and ELF, relocations against symbols are done in two
539 parts, with a HI relocation and a LO relocation. Each relocation
540 has only 16 bits of space to store an addend. This means that in
541 order for the linker to handle carries correctly, it must be able
542 to locate both the HI and the LO relocation. This means that the
543 relocations must appear in order in the relocation table.
544
545 In order to implement this, we keep track of each unmatched HI
546 relocation. We then sort them so that they immediately precede the
547 corresponding LO relocation. */
548
549 struct mips_hi_fixup
550 {
551 /* Next HI fixup. */
552 struct mips_hi_fixup *next;
553 /* This fixup. */
554 fixS *fixp;
555 /* The section this fixup is in. */
556 segT seg;
557 };
558
559 /* The list of unmatched HI relocs. */
560
561 static struct mips_hi_fixup *mips_hi_fixup_list;
562
563 /* The frag containing the last explicit relocation operator.
564 Null if explicit relocations have not been used. */
565
566 static fragS *prev_reloc_op_frag;
567
568 /* Map normal MIPS register numbers to mips16 register numbers. */
569
570 #define X ILLEGAL_REG
571 static const int mips32_to_16_reg_map[] =
572 {
573 X, X, 2, 3, 4, 5, 6, 7,
574 X, X, X, X, X, X, X, X,
575 0, 1, X, X, X, X, X, X,
576 X, X, X, X, X, X, X, X
577 };
578 #undef X
579
580 /* Map mips16 register numbers to normal MIPS register numbers. */
581
582 static const unsigned int mips16_to_32_reg_map[] =
583 {
584 16, 17, 2, 3, 4, 5, 6, 7
585 };
586
587 static int mips_fix_4122_bugs;
588
589 /* We don't relax branches by default, since this causes us to expand
590 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
591 fail to compute the offset before expanding the macro to the most
592 efficient expansion. */
593
594 static int mips_relax_branch;
595 \f
596 /* Since the MIPS does not have multiple forms of PC relative
597 instructions, we do not have to do relaxing as is done on other
598 platforms. However, we do have to handle GP relative addressing
599 correctly, which turns out to be a similar problem.
600
601 Every macro that refers to a symbol can occur in (at least) two
602 forms, one with GP relative addressing and one without. For
603 example, loading a global variable into a register generally uses
604 a macro instruction like this:
605 lw $4,i
606 If i can be addressed off the GP register (this is true if it is in
607 the .sbss or .sdata section, or if it is known to be smaller than
608 the -G argument) this will generate the following instruction:
609 lw $4,i($gp)
610 This instruction will use a GPREL reloc. If i can not be addressed
611 off the GP register, the following instruction sequence will be used:
612 lui $at,i
613 lw $4,i($at)
614 In this case the first instruction will have a HI16 reloc, and the
615 second reloc will have a LO16 reloc. Both relocs will be against
616 the symbol i.
617
618 The issue here is that we may not know whether i is GP addressable
619 until after we see the instruction that uses it. Therefore, we
620 want to be able to choose the final instruction sequence only at
621 the end of the assembly. This is similar to the way other
622 platforms choose the size of a PC relative instruction only at the
623 end of assembly.
624
625 When generating position independent code we do not use GP
626 addressing in quite the same way, but the issue still arises as
627 external symbols and local symbols must be handled differently.
628
629 We handle these issues by actually generating both possible
630 instruction sequences. The longer one is put in a frag_var with
631 type rs_machine_dependent. We encode what to do with the frag in
632 the subtype field. We encode (1) the number of existing bytes to
633 replace, (2) the number of new bytes to use, (3) the offset from
634 the start of the existing bytes to the first reloc we must generate
635 (that is, the offset is applied from the start of the existing
636 bytes after they are replaced by the new bytes, if any), (4) the
637 offset from the start of the existing bytes to the second reloc,
638 (5) whether a third reloc is needed (the third reloc is always four
639 bytes after the second reloc), and (6) whether to warn if this
640 variant is used (this is sometimes needed if .set nomacro or .set
641 noat is in effect). All these numbers are reasonably small.
642
643 Generating two instruction sequences must be handled carefully to
644 ensure that delay slots are handled correctly. Fortunately, there
645 are a limited number of cases. When the second instruction
646 sequence is generated, append_insn is directed to maintain the
647 existing delay slot information, so it continues to apply to any
648 code after the second instruction sequence. This means that the
649 second instruction sequence must not impose any requirements not
650 required by the first instruction sequence.
651
652 These variant frags are then handled in functions called by the
653 machine independent code. md_estimate_size_before_relax returns
654 the final size of the frag. md_convert_frag sets up the final form
655 of the frag. tc_gen_reloc adjust the first reloc and adds a second
656 one if needed. */
657 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
658 ((relax_substateT) \
659 (((old) << 23) \
660 | ((new) << 16) \
661 | (((reloc1) + 64) << 9) \
662 | (((reloc2) + 64) << 2) \
663 | ((reloc3) ? (1 << 1) : 0) \
664 | ((warn) ? 1 : 0)))
665 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
666 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
667 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
668 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
669 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
670 #define RELAX_WARN(i) ((i) & 1)
671
672 /* Branch without likely bit. If label is out of range, we turn:
673
674 beq reg1, reg2, label
675 delay slot
676
677 into
678
679 bne reg1, reg2, 0f
680 nop
681 j label
682 0: delay slot
683
684 with the following opcode replacements:
685
686 beq <-> bne
687 blez <-> bgtz
688 bltz <-> bgez
689 bc1f <-> bc1t
690
691 bltzal <-> bgezal (with jal label instead of j label)
692
693 Even though keeping the delay slot instruction in the delay slot of
694 the branch would be more efficient, it would be very tricky to do
695 correctly, because we'd have to introduce a variable frag *after*
696 the delay slot instruction, and expand that instead. Let's do it
697 the easy way for now, even if the branch-not-taken case now costs
698 one additional instruction. Out-of-range branches are not supposed
699 to be common, anyway.
700
701 Branch likely. If label is out of range, we turn:
702
703 beql reg1, reg2, label
704 delay slot (annulled if branch not taken)
705
706 into
707
708 beql reg1, reg2, 1f
709 nop
710 beql $0, $0, 2f
711 nop
712 1: j[al] label
713 delay slot (executed only if branch taken)
714 2:
715
716 It would be possible to generate a shorter sequence by losing the
717 likely bit, generating something like:
718
719 bne reg1, reg2, 0f
720 nop
721 j[al] label
722 delay slot (executed only if branch taken)
723 0:
724
725 beql -> bne
726 bnel -> beq
727 blezl -> bgtz
728 bgtzl -> blez
729 bltzl -> bgez
730 bgezl -> bltz
731 bc1fl -> bc1t
732 bc1tl -> bc1f
733
734 bltzall -> bgezal (with jal label instead of j label)
735 bgezall -> bltzal (ditto)
736
737
738 but it's not clear that it would actually improve performance. */
739 #define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
740 ((relax_substateT) \
741 (0xc0000000 \
742 | ((toofar) ? 1 : 0) \
743 | ((link) ? 2 : 0) \
744 | ((likely) ? 4 : 0) \
745 | ((uncond) ? 8 : 0)))
746 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
747 #define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
748 #define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
749 #define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
750 #define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
751
752 /* For mips16 code, we use an entirely different form of relaxation.
753 mips16 supports two versions of most instructions which take
754 immediate values: a small one which takes some small value, and a
755 larger one which takes a 16 bit value. Since branches also follow
756 this pattern, relaxing these values is required.
757
758 We can assemble both mips16 and normal MIPS code in a single
759 object. Therefore, we need to support this type of relaxation at
760 the same time that we support the relaxation described above. We
761 use the high bit of the subtype field to distinguish these cases.
762
763 The information we store for this type of relaxation is the
764 argument code found in the opcode file for this relocation, whether
765 the user explicitly requested a small or extended form, and whether
766 the relocation is in a jump or jal delay slot. That tells us the
767 size of the value, and how it should be stored. We also store
768 whether the fragment is considered to be extended or not. We also
769 store whether this is known to be a branch to a different section,
770 whether we have tried to relax this frag yet, and whether we have
771 ever extended a PC relative fragment because of a shift count. */
772 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
773 (0x80000000 \
774 | ((type) & 0xff) \
775 | ((small) ? 0x100 : 0) \
776 | ((ext) ? 0x200 : 0) \
777 | ((dslot) ? 0x400 : 0) \
778 | ((jal_dslot) ? 0x800 : 0))
779 #define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
780 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
781 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
782 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
783 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
784 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
785 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
786 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
787 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
788 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
789 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
790 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
791
792 /* Is the given value a sign-extended 32-bit value? */
793 #define IS_SEXT_32BIT_NUM(x) \
794 (((x) &~ (offsetT) 0x7fffffff) == 0 \
795 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
796
797 /* Is the given value a sign-extended 16-bit value? */
798 #define IS_SEXT_16BIT_NUM(x) \
799 (((x) &~ (offsetT) 0x7fff) == 0 \
800 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
801
802 \f
803 /* Prototypes for static functions. */
804
805 #ifdef __STDC__
806 #define internalError() \
807 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
808 #else
809 #define internalError() as_fatal (_("MIPS internal Error"));
810 #endif
811
812 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
813
814 static inline bfd_boolean reloc_needs_lo_p
815 PARAMS ((bfd_reloc_code_real_type));
816 static inline bfd_boolean fixup_has_matching_lo_p
817 PARAMS ((fixS *));
818 static int insn_uses_reg
819 PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
820 enum mips_regclass class));
821 static int reg_needs_delay
822 PARAMS ((unsigned int));
823 static void mips16_mark_labels
824 PARAMS ((void));
825 static void append_insn
826 PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
827 bfd_reloc_code_real_type *r));
828 static void mips_no_prev_insn
829 PARAMS ((int));
830 static void mips_emit_delays
831 PARAMS ((bfd_boolean));
832 #ifdef USE_STDARG
833 static void macro_build
834 PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
835 const char *fmt, ...));
836 #else
837 static void macro_build ();
838 #endif
839 static void mips16_macro_build
840 PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
841 static void macro_build_jalr
842 PARAMS ((int, expressionS *));
843 static void macro_build_lui
844 PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
845 static void macro_build_ldst_constoffset
846 PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
847 int valreg, int breg));
848 static void set_at
849 PARAMS ((int *counter, int reg, int unsignedp));
850 static void check_absolute_expr
851 PARAMS ((struct mips_cl_insn * ip, expressionS *));
852 static void load_register
853 PARAMS ((int *, int, expressionS *, int));
854 static void load_address
855 PARAMS ((int *, int, expressionS *, int *));
856 static void move_register
857 PARAMS ((int *, int, int));
858 static void macro
859 PARAMS ((struct mips_cl_insn * ip));
860 static void mips16_macro
861 PARAMS ((struct mips_cl_insn * ip));
862 #ifdef LOSING_COMPILER
863 static void macro2
864 PARAMS ((struct mips_cl_insn * ip));
865 #endif
866 static void mips_ip
867 PARAMS ((char *str, struct mips_cl_insn * ip));
868 static void mips16_ip
869 PARAMS ((char *str, struct mips_cl_insn * ip));
870 static void mips16_immed
871 PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
872 bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
873 static bfd_boolean parse_relocation
874 PARAMS ((char **, bfd_reloc_code_real_type *));
875 static size_t my_getSmallExpression
876 PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
877 static void my_getExpression
878 PARAMS ((expressionS *, char *));
879 #ifdef OBJ_ELF
880 static int support_64bit_objects
881 PARAMS((void));
882 #endif
883 static void mips_set_option_string
884 PARAMS ((const char **, const char *));
885 static symbolS *get_symbol
886 PARAMS ((void));
887 static void mips_align
888 PARAMS ((int to, int fill, symbolS *label));
889 static void s_align
890 PARAMS ((int));
891 static void s_change_sec
892 PARAMS ((int));
893 static void s_change_section
894 PARAMS ((int));
895 static void s_cons
896 PARAMS ((int));
897 static void s_float_cons
898 PARAMS ((int));
899 static void s_mips_globl
900 PARAMS ((int));
901 static void s_option
902 PARAMS ((int));
903 static void s_mipsset
904 PARAMS ((int));
905 static void s_abicalls
906 PARAMS ((int));
907 static void s_cpload
908 PARAMS ((int));
909 static void s_cpsetup
910 PARAMS ((int));
911 static void s_cplocal
912 PARAMS ((int));
913 static void s_cprestore
914 PARAMS ((int));
915 static void s_cpreturn
916 PARAMS ((int));
917 static void s_gpvalue
918 PARAMS ((int));
919 static void s_gpword
920 PARAMS ((int));
921 static void s_gpdword
922 PARAMS ((int));
923 static void s_cpadd
924 PARAMS ((int));
925 static void s_insn
926 PARAMS ((int));
927 static void md_obj_begin
928 PARAMS ((void));
929 static void md_obj_end
930 PARAMS ((void));
931 static long get_number
932 PARAMS ((void));
933 static void s_mips_ent
934 PARAMS ((int));
935 static void s_mips_end
936 PARAMS ((int));
937 static void s_mips_frame
938 PARAMS ((int));
939 static void s_mips_mask
940 PARAMS ((int));
941 static void s_mips_stab
942 PARAMS ((int));
943 static void s_mips_weakext
944 PARAMS ((int));
945 static void s_mips_file
946 PARAMS ((int));
947 static void s_mips_loc
948 PARAMS ((int));
949 static bfd_boolean pic_need_relax
950 PARAMS ((symbolS *, asection *));
951 static int mips16_extended_frag
952 PARAMS ((fragS *, asection *, long));
953 static int relaxed_branch_length (fragS *, asection *, int);
954 static int validate_mips_insn
955 PARAMS ((const struct mips_opcode *));
956 static void show
957 PARAMS ((FILE *, const char *, int *, int *));
958 #ifdef OBJ_ELF
959 static int mips_need_elf_addend_fixup
960 PARAMS ((fixS *));
961 #endif
962
963 /* Table and functions used to map between CPU/ISA names, and
964 ISA levels, and CPU numbers. */
965
966 struct mips_cpu_info
967 {
968 const char *name; /* CPU or ISA name. */
969 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
970 int isa; /* ISA level. */
971 int cpu; /* CPU number (default CPU if ISA). */
972 };
973
974 static void mips_set_architecture
975 PARAMS ((const struct mips_cpu_info *));
976 static void mips_set_tune
977 PARAMS ((const struct mips_cpu_info *));
978 static bfd_boolean mips_strict_matching_cpu_name_p
979 PARAMS ((const char *, const char *));
980 static bfd_boolean mips_matching_cpu_name_p
981 PARAMS ((const char *, const char *));
982 static const struct mips_cpu_info *mips_parse_cpu
983 PARAMS ((const char *, const char *));
984 static const struct mips_cpu_info *mips_cpu_info_from_isa
985 PARAMS ((int));
986 \f
987 /* Pseudo-op table.
988
989 The following pseudo-ops from the Kane and Heinrich MIPS book
990 should be defined here, but are currently unsupported: .alias,
991 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
992
993 The following pseudo-ops from the Kane and Heinrich MIPS book are
994 specific to the type of debugging information being generated, and
995 should be defined by the object format: .aent, .begin, .bend,
996 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
997 .vreg.
998
999 The following pseudo-ops from the Kane and Heinrich MIPS book are
1000 not MIPS CPU specific, but are also not specific to the object file
1001 format. This file is probably the best place to define them, but
1002 they are not currently supported: .asm0, .endr, .lab, .repeat,
1003 .struct. */
1004
1005 static const pseudo_typeS mips_pseudo_table[] =
1006 {
1007 /* MIPS specific pseudo-ops. */
1008 {"option", s_option, 0},
1009 {"set", s_mipsset, 0},
1010 {"rdata", s_change_sec, 'r'},
1011 {"sdata", s_change_sec, 's'},
1012 {"livereg", s_ignore, 0},
1013 {"abicalls", s_abicalls, 0},
1014 {"cpload", s_cpload, 0},
1015 {"cpsetup", s_cpsetup, 0},
1016 {"cplocal", s_cplocal, 0},
1017 {"cprestore", s_cprestore, 0},
1018 {"cpreturn", s_cpreturn, 0},
1019 {"gpvalue", s_gpvalue, 0},
1020 {"gpword", s_gpword, 0},
1021 {"gpdword", s_gpdword, 0},
1022 {"cpadd", s_cpadd, 0},
1023 {"insn", s_insn, 0},
1024
1025 /* Relatively generic pseudo-ops that happen to be used on MIPS
1026 chips. */
1027 {"asciiz", stringer, 1},
1028 {"bss", s_change_sec, 'b'},
1029 {"err", s_err, 0},
1030 {"half", s_cons, 1},
1031 {"dword", s_cons, 3},
1032 {"weakext", s_mips_weakext, 0},
1033
1034 /* These pseudo-ops are defined in read.c, but must be overridden
1035 here for one reason or another. */
1036 {"align", s_align, 0},
1037 {"byte", s_cons, 0},
1038 {"data", s_change_sec, 'd'},
1039 {"double", s_float_cons, 'd'},
1040 {"float", s_float_cons, 'f'},
1041 {"globl", s_mips_globl, 0},
1042 {"global", s_mips_globl, 0},
1043 {"hword", s_cons, 1},
1044 {"int", s_cons, 2},
1045 {"long", s_cons, 2},
1046 {"octa", s_cons, 4},
1047 {"quad", s_cons, 3},
1048 {"section", s_change_section, 0},
1049 {"short", s_cons, 1},
1050 {"single", s_float_cons, 'f'},
1051 {"stabn", s_mips_stab, 'n'},
1052 {"text", s_change_sec, 't'},
1053 {"word", s_cons, 2},
1054
1055 { "extern", ecoff_directive_extern, 0},
1056
1057 { NULL, NULL, 0 },
1058 };
1059
1060 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1061 {
1062 /* These pseudo-ops should be defined by the object file format.
1063 However, a.out doesn't support them, so we have versions here. */
1064 {"aent", s_mips_ent, 1},
1065 {"bgnb", s_ignore, 0},
1066 {"end", s_mips_end, 0},
1067 {"endb", s_ignore, 0},
1068 {"ent", s_mips_ent, 0},
1069 {"file", s_mips_file, 0},
1070 {"fmask", s_mips_mask, 'F'},
1071 {"frame", s_mips_frame, 0},
1072 {"loc", s_mips_loc, 0},
1073 {"mask", s_mips_mask, 'R'},
1074 {"verstamp", s_ignore, 0},
1075 { NULL, NULL, 0 },
1076 };
1077
1078 extern void pop_insert PARAMS ((const pseudo_typeS *));
1079
1080 void
1081 mips_pop_insert ()
1082 {
1083 pop_insert (mips_pseudo_table);
1084 if (! ECOFF_DEBUGGING)
1085 pop_insert (mips_nonecoff_pseudo_table);
1086 }
1087 \f
1088 /* Symbols labelling the current insn. */
1089
1090 struct insn_label_list
1091 {
1092 struct insn_label_list *next;
1093 symbolS *label;
1094 };
1095
1096 static struct insn_label_list *insn_labels;
1097 static struct insn_label_list *free_insn_labels;
1098
1099 static void mips_clear_insn_labels PARAMS ((void));
1100
1101 static inline void
1102 mips_clear_insn_labels ()
1103 {
1104 register struct insn_label_list **pl;
1105
1106 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1107 ;
1108 *pl = insn_labels;
1109 insn_labels = NULL;
1110 }
1111 \f
1112 static char *expr_end;
1113
1114 /* Expressions which appear in instructions. These are set by
1115 mips_ip. */
1116
1117 static expressionS imm_expr;
1118 static expressionS offset_expr;
1119
1120 /* Relocs associated with imm_expr and offset_expr. */
1121
1122 static bfd_reloc_code_real_type imm_reloc[3]
1123 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1124 static bfd_reloc_code_real_type offset_reloc[3]
1125 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1126
1127 /* These are set by mips16_ip if an explicit extension is used. */
1128
1129 static bfd_boolean mips16_small, mips16_ext;
1130
1131 #ifdef OBJ_ELF
1132 /* The pdr segment for per procedure frame/regmask info. Not used for
1133 ECOFF debugging. */
1134
1135 static segT pdr_seg;
1136 #endif
1137
1138 /* The default target format to use. */
1139
1140 const char *
1141 mips_target_format ()
1142 {
1143 switch (OUTPUT_FLAVOR)
1144 {
1145 case bfd_target_aout_flavour:
1146 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1147 case bfd_target_ecoff_flavour:
1148 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1149 case bfd_target_coff_flavour:
1150 return "pe-mips";
1151 case bfd_target_elf_flavour:
1152 #ifdef TE_TMIPS
1153 /* This is traditional mips. */
1154 return (target_big_endian
1155 ? (HAVE_64BIT_OBJECTS
1156 ? "elf64-tradbigmips"
1157 : (HAVE_NEWABI
1158 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1159 : (HAVE_64BIT_OBJECTS
1160 ? "elf64-tradlittlemips"
1161 : (HAVE_NEWABI
1162 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
1163 #else
1164 return (target_big_endian
1165 ? (HAVE_64BIT_OBJECTS
1166 ? "elf64-bigmips"
1167 : (HAVE_NEWABI
1168 ? "elf32-nbigmips" : "elf32-bigmips"))
1169 : (HAVE_64BIT_OBJECTS
1170 ? "elf64-littlemips"
1171 : (HAVE_NEWABI
1172 ? "elf32-nlittlemips" : "elf32-littlemips")));
1173 #endif
1174 default:
1175 abort ();
1176 return NULL;
1177 }
1178 }
1179
1180 /* This function is called once, at assembler startup time. It should
1181 set up all the tables, etc. that the MD part of the assembler will need. */
1182
1183 void
1184 md_begin ()
1185 {
1186 register const char *retval = NULL;
1187 int i = 0;
1188 int broken = 0;
1189
1190 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1191 as_warn (_("Could not set architecture and machine"));
1192
1193 op_hash = hash_new ();
1194
1195 for (i = 0; i < NUMOPCODES;)
1196 {
1197 const char *name = mips_opcodes[i].name;
1198
1199 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1200 if (retval != NULL)
1201 {
1202 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1203 mips_opcodes[i].name, retval);
1204 /* Probably a memory allocation problem? Give up now. */
1205 as_fatal (_("Broken assembler. No assembly attempted."));
1206 }
1207 do
1208 {
1209 if (mips_opcodes[i].pinfo != INSN_MACRO)
1210 {
1211 if (!validate_mips_insn (&mips_opcodes[i]))
1212 broken = 1;
1213 }
1214 ++i;
1215 }
1216 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1217 }
1218
1219 mips16_op_hash = hash_new ();
1220
1221 i = 0;
1222 while (i < bfd_mips16_num_opcodes)
1223 {
1224 const char *name = mips16_opcodes[i].name;
1225
1226 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1227 if (retval != NULL)
1228 as_fatal (_("internal: can't hash `%s': %s"),
1229 mips16_opcodes[i].name, retval);
1230 do
1231 {
1232 if (mips16_opcodes[i].pinfo != INSN_MACRO
1233 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1234 != mips16_opcodes[i].match))
1235 {
1236 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1237 mips16_opcodes[i].name, mips16_opcodes[i].args);
1238 broken = 1;
1239 }
1240 ++i;
1241 }
1242 while (i < bfd_mips16_num_opcodes
1243 && strcmp (mips16_opcodes[i].name, name) == 0);
1244 }
1245
1246 if (broken)
1247 as_fatal (_("Broken assembler. No assembly attempted."));
1248
1249 /* We add all the general register names to the symbol table. This
1250 helps us detect invalid uses of them. */
1251 for (i = 0; i < 32; i++)
1252 {
1253 char buf[5];
1254
1255 sprintf (buf, "$%d", i);
1256 symbol_table_insert (symbol_new (buf, reg_section, i,
1257 &zero_address_frag));
1258 }
1259 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1260 &zero_address_frag));
1261 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1262 &zero_address_frag));
1263 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1264 &zero_address_frag));
1265 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1266 &zero_address_frag));
1267 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1268 &zero_address_frag));
1269 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1270 &zero_address_frag));
1271 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1272 &zero_address_frag));
1273 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1274 &zero_address_frag));
1275 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1276 &zero_address_frag));
1277
1278 /* If we don't add these register names to the symbol table, they
1279 may end up being added as regular symbols by operand(), and then
1280 make it to the object file as undefined in case they're not
1281 regarded as local symbols. They're local in o32, since `$' is a
1282 local symbol prefix, but not in n32 or n64. */
1283 for (i = 0; i < 8; i++)
1284 {
1285 char buf[6];
1286
1287 sprintf (buf, "$fcc%i", i);
1288 symbol_table_insert (symbol_new (buf, reg_section, -1,
1289 &zero_address_frag));
1290 }
1291
1292 mips_no_prev_insn (FALSE);
1293
1294 mips_gprmask = 0;
1295 mips_cprmask[0] = 0;
1296 mips_cprmask[1] = 0;
1297 mips_cprmask[2] = 0;
1298 mips_cprmask[3] = 0;
1299
1300 /* set the default alignment for the text section (2**2) */
1301 record_alignment (text_section, 2);
1302
1303 if (USE_GLOBAL_POINTER_OPT)
1304 bfd_set_gp_size (stdoutput, g_switch_value);
1305
1306 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1307 {
1308 /* On a native system, sections must be aligned to 16 byte
1309 boundaries. When configured for an embedded ELF target, we
1310 don't bother. */
1311 if (strcmp (TARGET_OS, "elf") != 0)
1312 {
1313 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1314 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1315 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1316 }
1317
1318 /* Create a .reginfo section for register masks and a .mdebug
1319 section for debugging information. */
1320 {
1321 segT seg;
1322 subsegT subseg;
1323 flagword flags;
1324 segT sec;
1325
1326 seg = now_seg;
1327 subseg = now_subseg;
1328
1329 /* The ABI says this section should be loaded so that the
1330 running program can access it. However, we don't load it
1331 if we are configured for an embedded target */
1332 flags = SEC_READONLY | SEC_DATA;
1333 if (strcmp (TARGET_OS, "elf") != 0)
1334 flags |= SEC_ALLOC | SEC_LOAD;
1335
1336 if (mips_abi != N64_ABI)
1337 {
1338 sec = subseg_new (".reginfo", (subsegT) 0);
1339
1340 bfd_set_section_flags (stdoutput, sec, flags);
1341 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
1342
1343 #ifdef OBJ_ELF
1344 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1345 #endif
1346 }
1347 else
1348 {
1349 /* The 64-bit ABI uses a .MIPS.options section rather than
1350 .reginfo section. */
1351 sec = subseg_new (".MIPS.options", (subsegT) 0);
1352 bfd_set_section_flags (stdoutput, sec, flags);
1353 bfd_set_section_alignment (stdoutput, sec, 3);
1354
1355 #ifdef OBJ_ELF
1356 /* Set up the option header. */
1357 {
1358 Elf_Internal_Options opthdr;
1359 char *f;
1360
1361 opthdr.kind = ODK_REGINFO;
1362 opthdr.size = (sizeof (Elf_External_Options)
1363 + sizeof (Elf64_External_RegInfo));
1364 opthdr.section = 0;
1365 opthdr.info = 0;
1366 f = frag_more (sizeof (Elf_External_Options));
1367 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1368 (Elf_External_Options *) f);
1369
1370 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1371 }
1372 #endif
1373 }
1374
1375 if (ECOFF_DEBUGGING)
1376 {
1377 sec = subseg_new (".mdebug", (subsegT) 0);
1378 (void) bfd_set_section_flags (stdoutput, sec,
1379 SEC_HAS_CONTENTS | SEC_READONLY);
1380 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1381 }
1382 #ifdef OBJ_ELF
1383 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1384 {
1385 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1386 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1387 SEC_READONLY | SEC_RELOC
1388 | SEC_DEBUGGING);
1389 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1390 }
1391 #endif
1392
1393 subseg_set (seg, subseg);
1394 }
1395 }
1396
1397 if (! ECOFF_DEBUGGING)
1398 md_obj_begin ();
1399 }
1400
1401 void
1402 md_mips_end ()
1403 {
1404 if (! ECOFF_DEBUGGING)
1405 md_obj_end ();
1406 }
1407
1408 void
1409 md_assemble (str)
1410 char *str;
1411 {
1412 struct mips_cl_insn insn;
1413 bfd_reloc_code_real_type unused_reloc[3]
1414 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1415
1416 imm_expr.X_op = O_absent;
1417 offset_expr.X_op = O_absent;
1418 imm_reloc[0] = BFD_RELOC_UNUSED;
1419 imm_reloc[1] = BFD_RELOC_UNUSED;
1420 imm_reloc[2] = BFD_RELOC_UNUSED;
1421 offset_reloc[0] = BFD_RELOC_UNUSED;
1422 offset_reloc[1] = BFD_RELOC_UNUSED;
1423 offset_reloc[2] = BFD_RELOC_UNUSED;
1424
1425 if (mips_opts.mips16)
1426 mips16_ip (str, &insn);
1427 else
1428 {
1429 mips_ip (str, &insn);
1430 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1431 str, insn.insn_opcode));
1432 }
1433
1434 if (insn_error)
1435 {
1436 as_bad ("%s `%s'", insn_error, str);
1437 return;
1438 }
1439
1440 if (insn.insn_mo->pinfo == INSN_MACRO)
1441 {
1442 if (mips_opts.mips16)
1443 mips16_macro (&insn);
1444 else
1445 macro (&insn);
1446 }
1447 else
1448 {
1449 if (imm_expr.X_op != O_absent)
1450 append_insn (NULL, &insn, &imm_expr, imm_reloc);
1451 else if (offset_expr.X_op != O_absent)
1452 append_insn (NULL, &insn, &offset_expr, offset_reloc);
1453 else
1454 append_insn (NULL, &insn, NULL, unused_reloc);
1455 }
1456 }
1457
1458 /* Return true if the given relocation might need a matching %lo().
1459 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1460 applied to local symbols. */
1461
1462 static inline bfd_boolean
1463 reloc_needs_lo_p (reloc)
1464 bfd_reloc_code_real_type reloc;
1465 {
1466 return (reloc == BFD_RELOC_HI16_S
1467 || reloc == BFD_RELOC_MIPS_GOT16);
1468 }
1469
1470 /* Return true if the given fixup is followed by a matching R_MIPS_LO16
1471 relocation. */
1472
1473 static inline bfd_boolean
1474 fixup_has_matching_lo_p (fixp)
1475 fixS *fixp;
1476 {
1477 return (fixp->fx_next != NULL
1478 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1479 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1480 && fixp->fx_offset == fixp->fx_next->fx_offset);
1481 }
1482
1483 /* See whether instruction IP reads register REG. CLASS is the type
1484 of register. */
1485
1486 static int
1487 insn_uses_reg (ip, reg, class)
1488 struct mips_cl_insn *ip;
1489 unsigned int reg;
1490 enum mips_regclass class;
1491 {
1492 if (class == MIPS16_REG)
1493 {
1494 assert (mips_opts.mips16);
1495 reg = mips16_to_32_reg_map[reg];
1496 class = MIPS_GR_REG;
1497 }
1498
1499 /* Don't report on general register ZERO, since it never changes. */
1500 if (class == MIPS_GR_REG && reg == ZERO)
1501 return 0;
1502
1503 if (class == MIPS_FP_REG)
1504 {
1505 assert (! mips_opts.mips16);
1506 /* If we are called with either $f0 or $f1, we must check $f0.
1507 This is not optimal, because it will introduce an unnecessary
1508 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1509 need to distinguish reading both $f0 and $f1 or just one of
1510 them. Note that we don't have to check the other way,
1511 because there is no instruction that sets both $f0 and $f1
1512 and requires a delay. */
1513 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1514 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1515 == (reg &~ (unsigned) 1)))
1516 return 1;
1517 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1518 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1519 == (reg &~ (unsigned) 1)))
1520 return 1;
1521 }
1522 else if (! mips_opts.mips16)
1523 {
1524 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1525 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1526 return 1;
1527 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1528 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1529 return 1;
1530 }
1531 else
1532 {
1533 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1534 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1535 & MIPS16OP_MASK_RX)]
1536 == reg))
1537 return 1;
1538 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1539 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1540 & MIPS16OP_MASK_RY)]
1541 == reg))
1542 return 1;
1543 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1544 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1545 & MIPS16OP_MASK_MOVE32Z)]
1546 == reg))
1547 return 1;
1548 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1549 return 1;
1550 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1551 return 1;
1552 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1553 return 1;
1554 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1555 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1556 & MIPS16OP_MASK_REGR32) == reg)
1557 return 1;
1558 }
1559
1560 return 0;
1561 }
1562
1563 /* This function returns true if modifying a register requires a
1564 delay. */
1565
1566 static int
1567 reg_needs_delay (reg)
1568 unsigned int reg;
1569 {
1570 unsigned long prev_pinfo;
1571
1572 prev_pinfo = prev_insn.insn_mo->pinfo;
1573 if (! mips_opts.noreorder
1574 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1575 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1576 || (! gpr_interlocks
1577 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1578 {
1579 /* A load from a coprocessor or from memory. All load
1580 delays delay the use of general register rt for one
1581 instruction on the r3000. The r6000 and r4000 use
1582 interlocks. */
1583 /* Itbl support may require additional care here. */
1584 know (prev_pinfo & INSN_WRITE_GPR_T);
1585 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1586 return 1;
1587 }
1588
1589 return 0;
1590 }
1591
1592 /* Mark instruction labels in mips16 mode. This permits the linker to
1593 handle them specially, such as generating jalx instructions when
1594 needed. We also make them odd for the duration of the assembly, in
1595 order to generate the right sort of code. We will make them even
1596 in the adjust_symtab routine, while leaving them marked. This is
1597 convenient for the debugger and the disassembler. The linker knows
1598 to make them odd again. */
1599
1600 static void
1601 mips16_mark_labels ()
1602 {
1603 if (mips_opts.mips16)
1604 {
1605 struct insn_label_list *l;
1606 valueT val;
1607
1608 for (l = insn_labels; l != NULL; l = l->next)
1609 {
1610 #ifdef OBJ_ELF
1611 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1612 S_SET_OTHER (l->label, STO_MIPS16);
1613 #endif
1614 val = S_GET_VALUE (l->label);
1615 if ((val & 1) == 0)
1616 S_SET_VALUE (l->label, val + 1);
1617 }
1618 }
1619 }
1620
1621 /* Output an instruction. PLACE is where to put the instruction; if
1622 it is NULL, this uses frag_more to get room. IP is the instruction
1623 information. ADDRESS_EXPR is an operand of the instruction to be
1624 used with RELOC_TYPE. */
1625
1626 static void
1627 append_insn (place, ip, address_expr, reloc_type)
1628 char *place;
1629 struct mips_cl_insn *ip;
1630 expressionS *address_expr;
1631 bfd_reloc_code_real_type *reloc_type;
1632 {
1633 register unsigned long prev_pinfo, pinfo;
1634 char *f;
1635 fixS *fixp[3];
1636 int nops = 0;
1637 bfd_boolean force_new_frag = FALSE;
1638
1639 /* Mark instruction labels in mips16 mode. */
1640 mips16_mark_labels ();
1641
1642 prev_pinfo = prev_insn.insn_mo->pinfo;
1643 pinfo = ip->insn_mo->pinfo;
1644
1645 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1646 {
1647 int prev_prev_nop;
1648
1649 /* If the previous insn required any delay slots, see if we need
1650 to insert a NOP or two. There are eight kinds of possible
1651 hazards, of which an instruction can have at most one type.
1652 (1) a load from memory delay
1653 (2) a load from a coprocessor delay
1654 (3) an unconditional branch delay
1655 (4) a conditional branch delay
1656 (5) a move to coprocessor register delay
1657 (6) a load coprocessor register from memory delay
1658 (7) a coprocessor condition code delay
1659 (8) a HI/LO special register delay
1660
1661 There are a lot of optimizations we could do that we don't.
1662 In particular, we do not, in general, reorder instructions.
1663 If you use gcc with optimization, it will reorder
1664 instructions and generally do much more optimization then we
1665 do here; repeating all that work in the assembler would only
1666 benefit hand written assembly code, and does not seem worth
1667 it. */
1668
1669 /* This is how a NOP is emitted. */
1670 #define emit_nop() \
1671 (mips_opts.mips16 \
1672 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1673 : md_number_to_chars (frag_more (4), 0, 4))
1674
1675 /* The previous insn might require a delay slot, depending upon
1676 the contents of the current insn. */
1677 if (! mips_opts.mips16
1678 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1679 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1680 && ! cop_interlocks)
1681 || (! gpr_interlocks
1682 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1683 {
1684 /* A load from a coprocessor or from memory. All load
1685 delays delay the use of general register rt for one
1686 instruction on the r3000. The r6000 and r4000 use
1687 interlocks. */
1688 /* Itbl support may require additional care here. */
1689 know (prev_pinfo & INSN_WRITE_GPR_T);
1690 if (mips_optimize == 0
1691 || insn_uses_reg (ip,
1692 ((prev_insn.insn_opcode >> OP_SH_RT)
1693 & OP_MASK_RT),
1694 MIPS_GR_REG))
1695 ++nops;
1696 }
1697 else if (! mips_opts.mips16
1698 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1699 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1700 && ! cop_interlocks)
1701 || (mips_opts.isa == ISA_MIPS1
1702 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1703 {
1704 /* A generic coprocessor delay. The previous instruction
1705 modified a coprocessor general or control register. If
1706 it modified a control register, we need to avoid any
1707 coprocessor instruction (this is probably not always
1708 required, but it sometimes is). If it modified a general
1709 register, we avoid using that register.
1710
1711 On the r6000 and r4000 loading a coprocessor register
1712 from memory is interlocked, and does not require a delay.
1713
1714 This case is not handled very well. There is no special
1715 knowledge of CP0 handling, and the coprocessors other
1716 than the floating point unit are not distinguished at
1717 all. */
1718 /* Itbl support may require additional care here. FIXME!
1719 Need to modify this to include knowledge about
1720 user specified delays! */
1721 if (prev_pinfo & INSN_WRITE_FPR_T)
1722 {
1723 if (mips_optimize == 0
1724 || insn_uses_reg (ip,
1725 ((prev_insn.insn_opcode >> OP_SH_FT)
1726 & OP_MASK_FT),
1727 MIPS_FP_REG))
1728 ++nops;
1729 }
1730 else if (prev_pinfo & INSN_WRITE_FPR_S)
1731 {
1732 if (mips_optimize == 0
1733 || insn_uses_reg (ip,
1734 ((prev_insn.insn_opcode >> OP_SH_FS)
1735 & OP_MASK_FS),
1736 MIPS_FP_REG))
1737 ++nops;
1738 }
1739 else
1740 {
1741 /* We don't know exactly what the previous instruction
1742 does. If the current instruction uses a coprocessor
1743 register, we must insert a NOP. If previous
1744 instruction may set the condition codes, and the
1745 current instruction uses them, we must insert two
1746 NOPS. */
1747 /* Itbl support may require additional care here. */
1748 if (mips_optimize == 0
1749 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1750 && (pinfo & INSN_READ_COND_CODE)))
1751 nops += 2;
1752 else if (pinfo & INSN_COP)
1753 ++nops;
1754 }
1755 }
1756 else if (! mips_opts.mips16
1757 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1758 && (prev_pinfo & INSN_WRITE_COND_CODE)
1759 && ! cop_interlocks)
1760 {
1761 /* The previous instruction sets the coprocessor condition
1762 codes, but does not require a general coprocessor delay
1763 (this means it is a floating point comparison
1764 instruction). If this instruction uses the condition
1765 codes, we need to insert a single NOP. */
1766 /* Itbl support may require additional care here. */
1767 if (mips_optimize == 0
1768 || (pinfo & INSN_READ_COND_CODE))
1769 ++nops;
1770 }
1771
1772 /* If we're fixing up mfhi/mflo for the r7000 and the
1773 previous insn was an mfhi/mflo and the current insn
1774 reads the register that the mfhi/mflo wrote to, then
1775 insert two nops. */
1776
1777 else if (mips_7000_hilo_fix
1778 && MF_HILO_INSN (prev_pinfo)
1779 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1780 & OP_MASK_RD),
1781 MIPS_GR_REG))
1782 {
1783 nops += 2;
1784 }
1785
1786 /* If we're fixing up mfhi/mflo for the r7000 and the
1787 2nd previous insn was an mfhi/mflo and the current insn
1788 reads the register that the mfhi/mflo wrote to, then
1789 insert one nop. */
1790
1791 else if (mips_7000_hilo_fix
1792 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1793 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1794 & OP_MASK_RD),
1795 MIPS_GR_REG))
1796
1797 {
1798 ++nops;
1799 }
1800
1801 else if (prev_pinfo & INSN_READ_LO)
1802 {
1803 /* The previous instruction reads the LO register; if the
1804 current instruction writes to the LO register, we must
1805 insert two NOPS. Some newer processors have interlocks.
1806 Also the tx39's multiply instructions can be exectuted
1807 immediatly after a read from HI/LO (without the delay),
1808 though the tx39's divide insns still do require the
1809 delay. */
1810 if (! (hilo_interlocks
1811 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1812 && (mips_optimize == 0
1813 || (pinfo & INSN_WRITE_LO)))
1814 nops += 2;
1815 /* Most mips16 branch insns don't have a delay slot.
1816 If a read from LO is immediately followed by a branch
1817 to a write to LO we have a read followed by a write
1818 less than 2 insns away. We assume the target of
1819 a branch might be a write to LO, and insert a nop
1820 between a read and an immediately following branch. */
1821 else if (mips_opts.mips16
1822 && (mips_optimize == 0
1823 || (pinfo & MIPS16_INSN_BRANCH)))
1824 ++nops;
1825 }
1826 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1827 {
1828 /* The previous instruction reads the HI register; if the
1829 current instruction writes to the HI register, we must
1830 insert a NOP. Some newer processors have interlocks.
1831 Also the note tx39's multiply above. */
1832 if (! (hilo_interlocks
1833 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1834 && (mips_optimize == 0
1835 || (pinfo & INSN_WRITE_HI)))
1836 nops += 2;
1837 /* Most mips16 branch insns don't have a delay slot.
1838 If a read from HI is immediately followed by a branch
1839 to a write to HI we have a read followed by a write
1840 less than 2 insns away. We assume the target of
1841 a branch might be a write to HI, and insert a nop
1842 between a read and an immediately following branch. */
1843 else if (mips_opts.mips16
1844 && (mips_optimize == 0
1845 || (pinfo & MIPS16_INSN_BRANCH)))
1846 ++nops;
1847 }
1848
1849 /* If the previous instruction was in a noreorder section, then
1850 we don't want to insert the nop after all. */
1851 /* Itbl support may require additional care here. */
1852 if (prev_insn_unreordered)
1853 nops = 0;
1854
1855 /* There are two cases which require two intervening
1856 instructions: 1) setting the condition codes using a move to
1857 coprocessor instruction which requires a general coprocessor
1858 delay and then reading the condition codes 2) reading the HI
1859 or LO register and then writing to it (except on processors
1860 which have interlocks). If we are not already emitting a NOP
1861 instruction, we must check for these cases compared to the
1862 instruction previous to the previous instruction. */
1863 if ((! mips_opts.mips16
1864 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1865 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1866 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1867 && (pinfo & INSN_READ_COND_CODE)
1868 && ! cop_interlocks)
1869 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1870 && (pinfo & INSN_WRITE_LO)
1871 && ! (hilo_interlocks
1872 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1873 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1874 && (pinfo & INSN_WRITE_HI)
1875 && ! (hilo_interlocks
1876 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1877 prev_prev_nop = 1;
1878 else
1879 prev_prev_nop = 0;
1880
1881 if (prev_prev_insn_unreordered)
1882 prev_prev_nop = 0;
1883
1884 if (prev_prev_nop && nops == 0)
1885 ++nops;
1886
1887 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1888 {
1889 /* We're out of bits in pinfo, so we must resort to string
1890 ops here. Shortcuts are selected based on opcodes being
1891 limited to the VR4122 instruction set. */
1892 int min_nops = 0;
1893 const char *pn = prev_insn.insn_mo->name;
1894 const char *tn = ip->insn_mo->name;
1895 if (strncmp(pn, "macc", 4) == 0
1896 || strncmp(pn, "dmacc", 5) == 0)
1897 {
1898 /* Errata 21 - [D]DIV[U] after [D]MACC */
1899 if (strstr (tn, "div"))
1900 {
1901 min_nops = 1;
1902 }
1903
1904 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1905 if (pn[0] == 'd' /* dmacc */
1906 && (strncmp(tn, "dmult", 5) == 0
1907 || strncmp(tn, "dmacc", 5) == 0))
1908 {
1909 min_nops = 1;
1910 }
1911
1912 /* Errata 24 - MT{LO,HI} after [D]MACC */
1913 if (strcmp (tn, "mtlo") == 0
1914 || strcmp (tn, "mthi") == 0)
1915 {
1916 min_nops = 1;
1917 }
1918
1919 }
1920 else if (strncmp(pn, "dmult", 5) == 0
1921 && (strncmp(tn, "dmult", 5) == 0
1922 || strncmp(tn, "dmacc", 5) == 0))
1923 {
1924 /* Here is the rest of errata 23. */
1925 min_nops = 1;
1926 }
1927 if (nops < min_nops)
1928 nops = min_nops;
1929 }
1930
1931 /* If we are being given a nop instruction, don't bother with
1932 one of the nops we would otherwise output. This will only
1933 happen when a nop instruction is used with mips_optimize set
1934 to 0. */
1935 if (nops > 0
1936 && ! mips_opts.noreorder
1937 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1938 --nops;
1939
1940 /* Now emit the right number of NOP instructions. */
1941 if (nops > 0 && ! mips_opts.noreorder)
1942 {
1943 fragS *old_frag;
1944 unsigned long old_frag_offset;
1945 int i;
1946 struct insn_label_list *l;
1947
1948 old_frag = frag_now;
1949 old_frag_offset = frag_now_fix ();
1950
1951 for (i = 0; i < nops; i++)
1952 emit_nop ();
1953
1954 if (listing)
1955 {
1956 listing_prev_line ();
1957 /* We may be at the start of a variant frag. In case we
1958 are, make sure there is enough space for the frag
1959 after the frags created by listing_prev_line. The
1960 argument to frag_grow here must be at least as large
1961 as the argument to all other calls to frag_grow in
1962 this file. We don't have to worry about being in the
1963 middle of a variant frag, because the variants insert
1964 all needed nop instructions themselves. */
1965 frag_grow (40);
1966 }
1967
1968 for (l = insn_labels; l != NULL; l = l->next)
1969 {
1970 valueT val;
1971
1972 assert (S_GET_SEGMENT (l->label) == now_seg);
1973 symbol_set_frag (l->label, frag_now);
1974 val = (valueT) frag_now_fix ();
1975 /* mips16 text labels are stored as odd. */
1976 if (mips_opts.mips16)
1977 ++val;
1978 S_SET_VALUE (l->label, val);
1979 }
1980
1981 #ifndef NO_ECOFF_DEBUGGING
1982 if (ECOFF_DEBUGGING)
1983 ecoff_fix_loc (old_frag, old_frag_offset);
1984 #endif
1985 }
1986 else if (prev_nop_frag != NULL)
1987 {
1988 /* We have a frag holding nops we may be able to remove. If
1989 we don't need any nops, we can decrease the size of
1990 prev_nop_frag by the size of one instruction. If we do
1991 need some nops, we count them in prev_nops_required. */
1992 if (prev_nop_frag_since == 0)
1993 {
1994 if (nops == 0)
1995 {
1996 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1997 --prev_nop_frag_holds;
1998 }
1999 else
2000 prev_nop_frag_required += nops;
2001 }
2002 else
2003 {
2004 if (prev_prev_nop == 0)
2005 {
2006 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2007 --prev_nop_frag_holds;
2008 }
2009 else
2010 ++prev_nop_frag_required;
2011 }
2012
2013 if (prev_nop_frag_holds <= prev_nop_frag_required)
2014 prev_nop_frag = NULL;
2015
2016 ++prev_nop_frag_since;
2017
2018 /* Sanity check: by the time we reach the second instruction
2019 after prev_nop_frag, we should have used up all the nops
2020 one way or another. */
2021 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2022 }
2023 }
2024
2025 if (place == NULL
2026 && address_expr
2027 && *reloc_type == BFD_RELOC_16_PCREL_S2
2028 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2029 || pinfo & INSN_COND_BRANCH_LIKELY)
2030 && mips_relax_branch
2031 /* Don't try branch relaxation within .set nomacro, or within
2032 .set noat if we use $at for PIC computations. If it turns
2033 out that the branch was out-of-range, we'll get an error. */
2034 && !mips_opts.warn_about_macros
2035 && !(mips_opts.noat && mips_pic != NO_PIC)
2036 && !mips_opts.mips16)
2037 {
2038 f = frag_var (rs_machine_dependent,
2039 relaxed_branch_length
2040 (NULL, NULL,
2041 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2042 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2043 RELAX_BRANCH_ENCODE
2044 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2045 pinfo & INSN_COND_BRANCH_LIKELY,
2046 pinfo & INSN_WRITE_GPR_31,
2047 0),
2048 address_expr->X_add_symbol,
2049 address_expr->X_add_number,
2050 0);
2051 *reloc_type = BFD_RELOC_UNUSED;
2052 }
2053 else if (*reloc_type > BFD_RELOC_UNUSED)
2054 {
2055 /* We need to set up a variant frag. */
2056 assert (mips_opts.mips16 && address_expr != NULL);
2057 f = frag_var (rs_machine_dependent, 4, 0,
2058 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
2059 mips16_small, mips16_ext,
2060 (prev_pinfo
2061 & INSN_UNCOND_BRANCH_DELAY),
2062 (*prev_insn_reloc_type
2063 == BFD_RELOC_MIPS16_JMP)),
2064 make_expr_symbol (address_expr), 0, NULL);
2065 }
2066 else if (place != NULL)
2067 f = place;
2068 else if (mips_opts.mips16
2069 && ! ip->use_extend
2070 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2071 {
2072 /* Make sure there is enough room to swap this instruction with
2073 a following jump instruction. */
2074 frag_grow (6);
2075 f = frag_more (2);
2076 }
2077 else
2078 {
2079 if (mips_opts.mips16
2080 && mips_opts.noreorder
2081 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2082 as_warn (_("extended instruction in delay slot"));
2083
2084 f = frag_more (4);
2085 }
2086
2087 fixp[0] = fixp[1] = fixp[2] = NULL;
2088 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2089 {
2090 if (address_expr->X_op == O_constant)
2091 {
2092 valueT tmp;
2093
2094 switch (*reloc_type)
2095 {
2096 case BFD_RELOC_32:
2097 ip->insn_opcode |= address_expr->X_add_number;
2098 break;
2099
2100 case BFD_RELOC_MIPS_HIGHEST:
2101 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2102 tmp >>= 16;
2103 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2104 break;
2105
2106 case BFD_RELOC_MIPS_HIGHER:
2107 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2108 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2109 break;
2110
2111 case BFD_RELOC_HI16_S:
2112 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2113 >> 16) & 0xffff;
2114 break;
2115
2116 case BFD_RELOC_HI16:
2117 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2118 break;
2119
2120 case BFD_RELOC_LO16:
2121 case BFD_RELOC_MIPS_GOT_DISP:
2122 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2123 break;
2124
2125 case BFD_RELOC_MIPS_JMP:
2126 if ((address_expr->X_add_number & 3) != 0)
2127 as_bad (_("jump to misaligned address (0x%lx)"),
2128 (unsigned long) address_expr->X_add_number);
2129 if (address_expr->X_add_number & ~0xfffffff)
2130 as_bad (_("jump address range overflow (0x%lx)"),
2131 (unsigned long) address_expr->X_add_number);
2132 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2133 break;
2134
2135 case BFD_RELOC_MIPS16_JMP:
2136 if ((address_expr->X_add_number & 3) != 0)
2137 as_bad (_("jump to misaligned address (0x%lx)"),
2138 (unsigned long) address_expr->X_add_number);
2139 if (address_expr->X_add_number & ~0xfffffff)
2140 as_bad (_("jump address range overflow (0x%lx)"),
2141 (unsigned long) address_expr->X_add_number);
2142 ip->insn_opcode |=
2143 (((address_expr->X_add_number & 0x7c0000) << 3)
2144 | ((address_expr->X_add_number & 0xf800000) >> 7)
2145 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2146 break;
2147
2148 case BFD_RELOC_16_PCREL_S2:
2149 goto need_reloc;
2150
2151 default:
2152 internalError ();
2153 }
2154 }
2155 else
2156 {
2157 reloc_howto_type *howto;
2158
2159 need_reloc:
2160 /* Don't generate a reloc if we are writing into a variant frag. */
2161 if (place == NULL)
2162 {
2163 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[0]);
2164 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2165 bfd_get_reloc_size(howto),
2166 address_expr,
2167 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2168 reloc_type[0]);
2169
2170 /* These relocations can have an addend that won't fit in
2171 4 octets for 64bit assembly. */
2172 if (HAVE_64BIT_GPRS
2173 && ! howto->partial_inplace
2174 && (reloc_type[0] == BFD_RELOC_16
2175 || reloc_type[0] == BFD_RELOC_32
2176 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2177 || reloc_type[0] == BFD_RELOC_HI16_S
2178 || reloc_type[0] == BFD_RELOC_LO16
2179 || reloc_type[0] == BFD_RELOC_GPREL16
2180 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2181 || reloc_type[0] == BFD_RELOC_GPREL32
2182 || reloc_type[0] == BFD_RELOC_64
2183 || reloc_type[0] == BFD_RELOC_CTOR
2184 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2185 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2186 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2187 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2188 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2189 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2190 fixp[0]->fx_no_overflow = 1;
2191
2192 if (reloc_needs_lo_p (*reloc_type))
2193 {
2194 struct mips_hi_fixup *hi_fixup;
2195
2196 /* Reuse the last entry if it already has a matching %lo. */
2197 hi_fixup = mips_hi_fixup_list;
2198 if (hi_fixup == 0
2199 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2200 {
2201 hi_fixup = ((struct mips_hi_fixup *)
2202 xmalloc (sizeof (struct mips_hi_fixup)));
2203 hi_fixup->next = mips_hi_fixup_list;
2204 mips_hi_fixup_list = hi_fixup;
2205 }
2206 hi_fixup->fixp = fixp[0];
2207 hi_fixup->seg = now_seg;
2208 }
2209
2210 if (reloc_type[1] != BFD_RELOC_UNUSED)
2211 {
2212 /* FIXME: This symbol can be one of
2213 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2214 address_expr->X_op = O_absent;
2215 address_expr->X_add_symbol = 0;
2216 address_expr->X_add_number = 0;
2217
2218 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[1]);
2219 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2220 bfd_get_reloc_size(howto),
2221 address_expr, FALSE, reloc_type[1]);
2222
2223 /* These relocations can have an addend that won't fit in
2224 4 octets for 64bit assembly. */
2225 if (HAVE_64BIT_GPRS
2226 && ! howto->partial_inplace
2227 && (reloc_type[1] == BFD_RELOC_16
2228 || reloc_type[1] == BFD_RELOC_32
2229 || reloc_type[1] == BFD_RELOC_MIPS_JMP
2230 || reloc_type[1] == BFD_RELOC_HI16_S
2231 || reloc_type[1] == BFD_RELOC_LO16
2232 || reloc_type[1] == BFD_RELOC_GPREL16
2233 || reloc_type[1] == BFD_RELOC_MIPS_LITERAL
2234 || reloc_type[1] == BFD_RELOC_GPREL32
2235 || reloc_type[1] == BFD_RELOC_64
2236 || reloc_type[1] == BFD_RELOC_CTOR
2237 || reloc_type[1] == BFD_RELOC_MIPS_SUB
2238 || reloc_type[1] == BFD_RELOC_MIPS_HIGHEST
2239 || reloc_type[1] == BFD_RELOC_MIPS_HIGHER
2240 || reloc_type[1] == BFD_RELOC_MIPS_SCN_DISP
2241 || reloc_type[1] == BFD_RELOC_MIPS_REL16
2242 || reloc_type[1] == BFD_RELOC_MIPS_RELGOT))
2243 fixp[1]->fx_no_overflow = 1;
2244
2245 if (reloc_type[2] != BFD_RELOC_UNUSED)
2246 {
2247 address_expr->X_op = O_absent;
2248 address_expr->X_add_symbol = 0;
2249 address_expr->X_add_number = 0;
2250
2251 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[2]);
2252 fixp[2] = fix_new_exp (frag_now,
2253 f - frag_now->fr_literal,
2254 bfd_get_reloc_size(howto),
2255 address_expr, FALSE,
2256 reloc_type[2]);
2257
2258 /* These relocations can have an addend that won't fit in
2259 4 octets for 64bit assembly. */
2260 if (HAVE_64BIT_GPRS
2261 && ! howto->partial_inplace
2262 && (reloc_type[2] == BFD_RELOC_16
2263 || reloc_type[2] == BFD_RELOC_32
2264 || reloc_type[2] == BFD_RELOC_MIPS_JMP
2265 || reloc_type[2] == BFD_RELOC_HI16_S
2266 || reloc_type[2] == BFD_RELOC_LO16
2267 || reloc_type[2] == BFD_RELOC_GPREL16
2268 || reloc_type[2] == BFD_RELOC_MIPS_LITERAL
2269 || reloc_type[2] == BFD_RELOC_GPREL32
2270 || reloc_type[2] == BFD_RELOC_64
2271 || reloc_type[2] == BFD_RELOC_CTOR
2272 || reloc_type[2] == BFD_RELOC_MIPS_SUB
2273 || reloc_type[2] == BFD_RELOC_MIPS_HIGHEST
2274 || reloc_type[2] == BFD_RELOC_MIPS_HIGHER
2275 || reloc_type[2] == BFD_RELOC_MIPS_SCN_DISP
2276 || reloc_type[2] == BFD_RELOC_MIPS_REL16
2277 || reloc_type[2] == BFD_RELOC_MIPS_RELGOT))
2278 fixp[2]->fx_no_overflow = 1;
2279 }
2280 }
2281 }
2282 }
2283 }
2284
2285 if (! mips_opts.mips16)
2286 {
2287 md_number_to_chars (f, ip->insn_opcode, 4);
2288 #ifdef OBJ_ELF
2289 dwarf2_emit_insn (4);
2290 #endif
2291 }
2292 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2293 {
2294 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2295 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2296 #ifdef OBJ_ELF
2297 dwarf2_emit_insn (4);
2298 #endif
2299 }
2300 else
2301 {
2302 if (ip->use_extend)
2303 {
2304 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2305 f += 2;
2306 }
2307 md_number_to_chars (f, ip->insn_opcode, 2);
2308 #ifdef OBJ_ELF
2309 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2310 #endif
2311 }
2312
2313 /* Update the register mask information. */
2314 if (! mips_opts.mips16)
2315 {
2316 if (pinfo & INSN_WRITE_GPR_D)
2317 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2318 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2319 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2320 if (pinfo & INSN_READ_GPR_S)
2321 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2322 if (pinfo & INSN_WRITE_GPR_31)
2323 mips_gprmask |= 1 << RA;
2324 if (pinfo & INSN_WRITE_FPR_D)
2325 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2326 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2327 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2328 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2329 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2330 if ((pinfo & INSN_READ_FPR_R) != 0)
2331 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2332 if (pinfo & INSN_COP)
2333 {
2334 /* We don't keep enough information to sort these cases out.
2335 The itbl support does keep this information however, although
2336 we currently don't support itbl fprmats as part of the cop
2337 instruction. May want to add this support in the future. */
2338 }
2339 /* Never set the bit for $0, which is always zero. */
2340 mips_gprmask &= ~1 << 0;
2341 }
2342 else
2343 {
2344 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2345 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2346 & MIPS16OP_MASK_RX);
2347 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2348 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2349 & MIPS16OP_MASK_RY);
2350 if (pinfo & MIPS16_INSN_WRITE_Z)
2351 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2352 & MIPS16OP_MASK_RZ);
2353 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2354 mips_gprmask |= 1 << TREG;
2355 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2356 mips_gprmask |= 1 << SP;
2357 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2358 mips_gprmask |= 1 << RA;
2359 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2360 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2361 if (pinfo & MIPS16_INSN_READ_Z)
2362 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2363 & MIPS16OP_MASK_MOVE32Z);
2364 if (pinfo & MIPS16_INSN_READ_GPR_X)
2365 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2366 & MIPS16OP_MASK_REGR32);
2367 }
2368
2369 if (place == NULL && ! mips_opts.noreorder)
2370 {
2371 /* Filling the branch delay slot is more complex. We try to
2372 switch the branch with the previous instruction, which we can
2373 do if the previous instruction does not set up a condition
2374 that the branch tests and if the branch is not itself the
2375 target of any branch. */
2376 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2377 || (pinfo & INSN_COND_BRANCH_DELAY))
2378 {
2379 if (mips_optimize < 2
2380 /* If we have seen .set volatile or .set nomove, don't
2381 optimize. */
2382 || mips_opts.nomove != 0
2383 /* If we had to emit any NOP instructions, then we
2384 already know we can not swap. */
2385 || nops != 0
2386 /* If we don't even know the previous insn, we can not
2387 swap. */
2388 || ! prev_insn_valid
2389 /* If the previous insn is already in a branch delay
2390 slot, then we can not swap. */
2391 || prev_insn_is_delay_slot
2392 /* If the previous previous insn was in a .set
2393 noreorder, we can't swap. Actually, the MIPS
2394 assembler will swap in this situation. However, gcc
2395 configured -with-gnu-as will generate code like
2396 .set noreorder
2397 lw $4,XXX
2398 .set reorder
2399 INSN
2400 bne $4,$0,foo
2401 in which we can not swap the bne and INSN. If gcc is
2402 not configured -with-gnu-as, it does not output the
2403 .set pseudo-ops. We don't have to check
2404 prev_insn_unreordered, because prev_insn_valid will
2405 be 0 in that case. We don't want to use
2406 prev_prev_insn_valid, because we do want to be able
2407 to swap at the start of a function. */
2408 || prev_prev_insn_unreordered
2409 /* If the branch is itself the target of a branch, we
2410 can not swap. We cheat on this; all we check for is
2411 whether there is a label on this instruction. If
2412 there are any branches to anything other than a
2413 label, users must use .set noreorder. */
2414 || insn_labels != NULL
2415 /* If the previous instruction is in a variant frag, we
2416 can not do the swap. This does not apply to the
2417 mips16, which uses variant frags for different
2418 purposes. */
2419 || (! mips_opts.mips16
2420 && prev_insn_frag->fr_type == rs_machine_dependent)
2421 /* If the branch reads the condition codes, we don't
2422 even try to swap, because in the sequence
2423 ctc1 $X,$31
2424 INSN
2425 INSN
2426 bc1t LABEL
2427 we can not swap, and I don't feel like handling that
2428 case. */
2429 || (! mips_opts.mips16
2430 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2431 && (pinfo & INSN_READ_COND_CODE))
2432 /* We can not swap with an instruction that requires a
2433 delay slot, becase the target of the branch might
2434 interfere with that instruction. */
2435 || (! mips_opts.mips16
2436 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2437 && (prev_pinfo
2438 /* Itbl support may require additional care here. */
2439 & (INSN_LOAD_COPROC_DELAY
2440 | INSN_COPROC_MOVE_DELAY
2441 | INSN_WRITE_COND_CODE)))
2442 || (! (hilo_interlocks
2443 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2444 && (prev_pinfo
2445 & (INSN_READ_LO
2446 | INSN_READ_HI)))
2447 || (! mips_opts.mips16
2448 && ! gpr_interlocks
2449 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2450 || (! mips_opts.mips16
2451 && mips_opts.isa == ISA_MIPS1
2452 /* Itbl support may require additional care here. */
2453 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2454 /* We can not swap with a branch instruction. */
2455 || (prev_pinfo
2456 & (INSN_UNCOND_BRANCH_DELAY
2457 | INSN_COND_BRANCH_DELAY
2458 | INSN_COND_BRANCH_LIKELY))
2459 /* We do not swap with a trap instruction, since it
2460 complicates trap handlers to have the trap
2461 instruction be in a delay slot. */
2462 || (prev_pinfo & INSN_TRAP)
2463 /* If the branch reads a register that the previous
2464 instruction sets, we can not swap. */
2465 || (! mips_opts.mips16
2466 && (prev_pinfo & INSN_WRITE_GPR_T)
2467 && insn_uses_reg (ip,
2468 ((prev_insn.insn_opcode >> OP_SH_RT)
2469 & OP_MASK_RT),
2470 MIPS_GR_REG))
2471 || (! mips_opts.mips16
2472 && (prev_pinfo & INSN_WRITE_GPR_D)
2473 && insn_uses_reg (ip,
2474 ((prev_insn.insn_opcode >> OP_SH_RD)
2475 & OP_MASK_RD),
2476 MIPS_GR_REG))
2477 || (mips_opts.mips16
2478 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2479 && insn_uses_reg (ip,
2480 ((prev_insn.insn_opcode
2481 >> MIPS16OP_SH_RX)
2482 & MIPS16OP_MASK_RX),
2483 MIPS16_REG))
2484 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2485 && insn_uses_reg (ip,
2486 ((prev_insn.insn_opcode
2487 >> MIPS16OP_SH_RY)
2488 & MIPS16OP_MASK_RY),
2489 MIPS16_REG))
2490 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2491 && insn_uses_reg (ip,
2492 ((prev_insn.insn_opcode
2493 >> MIPS16OP_SH_RZ)
2494 & MIPS16OP_MASK_RZ),
2495 MIPS16_REG))
2496 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2497 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2498 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2499 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2500 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2501 && insn_uses_reg (ip,
2502 MIPS16OP_EXTRACT_REG32R (prev_insn.
2503 insn_opcode),
2504 MIPS_GR_REG))))
2505 /* If the branch writes a register that the previous
2506 instruction sets, we can not swap (we know that
2507 branches write only to RD or to $31). */
2508 || (! mips_opts.mips16
2509 && (prev_pinfo & INSN_WRITE_GPR_T)
2510 && (((pinfo & INSN_WRITE_GPR_D)
2511 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2512 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2513 || ((pinfo & INSN_WRITE_GPR_31)
2514 && (((prev_insn.insn_opcode >> OP_SH_RT)
2515 & OP_MASK_RT)
2516 == RA))))
2517 || (! mips_opts.mips16
2518 && (prev_pinfo & INSN_WRITE_GPR_D)
2519 && (((pinfo & INSN_WRITE_GPR_D)
2520 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2521 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2522 || ((pinfo & INSN_WRITE_GPR_31)
2523 && (((prev_insn.insn_opcode >> OP_SH_RD)
2524 & OP_MASK_RD)
2525 == RA))))
2526 || (mips_opts.mips16
2527 && (pinfo & MIPS16_INSN_WRITE_31)
2528 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2529 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2530 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2531 == RA))))
2532 /* If the branch writes a register that the previous
2533 instruction reads, we can not swap (we know that
2534 branches only write to RD or to $31). */
2535 || (! mips_opts.mips16
2536 && (pinfo & INSN_WRITE_GPR_D)
2537 && insn_uses_reg (&prev_insn,
2538 ((ip->insn_opcode >> OP_SH_RD)
2539 & OP_MASK_RD),
2540 MIPS_GR_REG))
2541 || (! mips_opts.mips16
2542 && (pinfo & INSN_WRITE_GPR_31)
2543 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2544 || (mips_opts.mips16
2545 && (pinfo & MIPS16_INSN_WRITE_31)
2546 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2547 /* If we are generating embedded PIC code, the branch
2548 might be expanded into a sequence which uses $at, so
2549 we can't swap with an instruction which reads it. */
2550 || (mips_pic == EMBEDDED_PIC
2551 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2552 /* If the previous previous instruction has a load
2553 delay, and sets a register that the branch reads, we
2554 can not swap. */
2555 || (! mips_opts.mips16
2556 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2557 /* Itbl support may require additional care here. */
2558 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2559 || (! gpr_interlocks
2560 && (prev_prev_insn.insn_mo->pinfo
2561 & INSN_LOAD_MEMORY_DELAY)))
2562 && insn_uses_reg (ip,
2563 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2564 & OP_MASK_RT),
2565 MIPS_GR_REG))
2566 /* If one instruction sets a condition code and the
2567 other one uses a condition code, we can not swap. */
2568 || ((pinfo & INSN_READ_COND_CODE)
2569 && (prev_pinfo & INSN_WRITE_COND_CODE))
2570 || ((pinfo & INSN_WRITE_COND_CODE)
2571 && (prev_pinfo & INSN_READ_COND_CODE))
2572 /* If the previous instruction uses the PC, we can not
2573 swap. */
2574 || (mips_opts.mips16
2575 && (prev_pinfo & MIPS16_INSN_READ_PC))
2576 /* If the previous instruction was extended, we can not
2577 swap. */
2578 || (mips_opts.mips16 && prev_insn_extended)
2579 /* If the previous instruction had a fixup in mips16
2580 mode, we can not swap. This normally means that the
2581 previous instruction was a 4 byte branch anyhow. */
2582 || (mips_opts.mips16 && prev_insn_fixp[0])
2583 /* If the previous instruction is a sync, sync.l, or
2584 sync.p, we can not swap. */
2585 || (prev_pinfo & INSN_SYNC))
2586 {
2587 /* We could do even better for unconditional branches to
2588 portions of this object file; we could pick up the
2589 instruction at the destination, put it in the delay
2590 slot, and bump the destination address. */
2591 emit_nop ();
2592 /* Update the previous insn information. */
2593 prev_prev_insn = *ip;
2594 prev_insn.insn_mo = &dummy_opcode;
2595 }
2596 else
2597 {
2598 /* It looks like we can actually do the swap. */
2599 if (! mips_opts.mips16)
2600 {
2601 char *prev_f;
2602 char temp[4];
2603
2604 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2605 memcpy (temp, prev_f, 4);
2606 memcpy (prev_f, f, 4);
2607 memcpy (f, temp, 4);
2608 if (prev_insn_fixp[0])
2609 {
2610 prev_insn_fixp[0]->fx_frag = frag_now;
2611 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2612 }
2613 if (prev_insn_fixp[1])
2614 {
2615 prev_insn_fixp[1]->fx_frag = frag_now;
2616 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2617 }
2618 if (prev_insn_fixp[2])
2619 {
2620 prev_insn_fixp[2]->fx_frag = frag_now;
2621 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2622 }
2623 if (prev_insn_fixp[0] && HAVE_NEWABI
2624 && prev_insn_frag != frag_now
2625 && (prev_insn_fixp[0]->fx_r_type
2626 == BFD_RELOC_MIPS_GOT_DISP
2627 || (prev_insn_fixp[0]->fx_r_type
2628 == BFD_RELOC_MIPS_CALL16)))
2629 {
2630 /* To avoid confusion in tc_gen_reloc, we must
2631 ensure that this does not become a variant
2632 frag. */
2633 force_new_frag = TRUE;
2634 }
2635 if (fixp[0])
2636 {
2637 fixp[0]->fx_frag = prev_insn_frag;
2638 fixp[0]->fx_where = prev_insn_where;
2639 }
2640 if (fixp[1])
2641 {
2642 fixp[1]->fx_frag = prev_insn_frag;
2643 fixp[1]->fx_where = prev_insn_where;
2644 }
2645 if (fixp[2])
2646 {
2647 fixp[2]->fx_frag = prev_insn_frag;
2648 fixp[2]->fx_where = prev_insn_where;
2649 }
2650 }
2651 else
2652 {
2653 char *prev_f;
2654 char temp[2];
2655
2656 assert (prev_insn_fixp[0] == NULL);
2657 assert (prev_insn_fixp[1] == NULL);
2658 assert (prev_insn_fixp[2] == NULL);
2659 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2660 memcpy (temp, prev_f, 2);
2661 memcpy (prev_f, f, 2);
2662 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2663 {
2664 assert (*reloc_type == BFD_RELOC_UNUSED);
2665 memcpy (f, temp, 2);
2666 }
2667 else
2668 {
2669 memcpy (f, f + 2, 2);
2670 memcpy (f + 2, temp, 2);
2671 }
2672 if (fixp[0])
2673 {
2674 fixp[0]->fx_frag = prev_insn_frag;
2675 fixp[0]->fx_where = prev_insn_where;
2676 }
2677 if (fixp[1])
2678 {
2679 fixp[1]->fx_frag = prev_insn_frag;
2680 fixp[1]->fx_where = prev_insn_where;
2681 }
2682 if (fixp[2])
2683 {
2684 fixp[2]->fx_frag = prev_insn_frag;
2685 fixp[2]->fx_where = prev_insn_where;
2686 }
2687 }
2688
2689 /* Update the previous insn information; leave prev_insn
2690 unchanged. */
2691 prev_prev_insn = *ip;
2692 }
2693 prev_insn_is_delay_slot = 1;
2694
2695 /* If that was an unconditional branch, forget the previous
2696 insn information. */
2697 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2698 {
2699 prev_prev_insn.insn_mo = &dummy_opcode;
2700 prev_insn.insn_mo = &dummy_opcode;
2701 }
2702
2703 prev_insn_fixp[0] = NULL;
2704 prev_insn_fixp[1] = NULL;
2705 prev_insn_fixp[2] = NULL;
2706 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2707 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2708 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2709 prev_insn_extended = 0;
2710 }
2711 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2712 {
2713 /* We don't yet optimize a branch likely. What we should do
2714 is look at the target, copy the instruction found there
2715 into the delay slot, and increment the branch to jump to
2716 the next instruction. */
2717 emit_nop ();
2718 /* Update the previous insn information. */
2719 prev_prev_insn = *ip;
2720 prev_insn.insn_mo = &dummy_opcode;
2721 prev_insn_fixp[0] = NULL;
2722 prev_insn_fixp[1] = NULL;
2723 prev_insn_fixp[2] = NULL;
2724 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2725 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2726 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2727 prev_insn_extended = 0;
2728 }
2729 else
2730 {
2731 /* Update the previous insn information. */
2732 if (nops > 0)
2733 prev_prev_insn.insn_mo = &dummy_opcode;
2734 else
2735 prev_prev_insn = prev_insn;
2736 prev_insn = *ip;
2737
2738 /* Any time we see a branch, we always fill the delay slot
2739 immediately; since this insn is not a branch, we know it
2740 is not in a delay slot. */
2741 prev_insn_is_delay_slot = 0;
2742
2743 prev_insn_fixp[0] = fixp[0];
2744 prev_insn_fixp[1] = fixp[1];
2745 prev_insn_fixp[2] = fixp[2];
2746 prev_insn_reloc_type[0] = reloc_type[0];
2747 prev_insn_reloc_type[1] = reloc_type[1];
2748 prev_insn_reloc_type[2] = reloc_type[2];
2749 if (mips_opts.mips16)
2750 prev_insn_extended = (ip->use_extend
2751 || *reloc_type > BFD_RELOC_UNUSED);
2752 }
2753
2754 prev_prev_insn_unreordered = prev_insn_unreordered;
2755 prev_insn_unreordered = 0;
2756 prev_insn_frag = frag_now;
2757 prev_insn_where = f - frag_now->fr_literal;
2758 prev_insn_valid = 1;
2759 }
2760 else if (place == NULL)
2761 {
2762 /* We need to record a bit of information even when we are not
2763 reordering, in order to determine the base address for mips16
2764 PC relative relocs. */
2765 prev_prev_insn = prev_insn;
2766 prev_insn = *ip;
2767 prev_insn_reloc_type[0] = reloc_type[0];
2768 prev_insn_reloc_type[1] = reloc_type[1];
2769 prev_insn_reloc_type[2] = reloc_type[2];
2770 prev_prev_insn_unreordered = prev_insn_unreordered;
2771 prev_insn_unreordered = 1;
2772 }
2773
2774 /* We just output an insn, so the next one doesn't have a label. */
2775 mips_clear_insn_labels ();
2776
2777 /* We must ensure that the frag to which an instruction that was
2778 moved from a non-variant frag doesn't become a variant frag,
2779 otherwise tc_gen_reloc may get confused. */
2780 if (force_new_frag)
2781 {
2782 frag_wane (frag_now);
2783 frag_new (0);
2784 }
2785 }
2786
2787 /* This function forgets that there was any previous instruction or
2788 label. If PRESERVE is non-zero, it remembers enough information to
2789 know whether nops are needed before a noreorder section. */
2790
2791 static void
2792 mips_no_prev_insn (preserve)
2793 int preserve;
2794 {
2795 if (! preserve)
2796 {
2797 prev_insn.insn_mo = &dummy_opcode;
2798 prev_prev_insn.insn_mo = &dummy_opcode;
2799 prev_nop_frag = NULL;
2800 prev_nop_frag_holds = 0;
2801 prev_nop_frag_required = 0;
2802 prev_nop_frag_since = 0;
2803 }
2804 prev_insn_valid = 0;
2805 prev_insn_is_delay_slot = 0;
2806 prev_insn_unreordered = 0;
2807 prev_insn_extended = 0;
2808 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2809 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2810 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2811 prev_prev_insn_unreordered = 0;
2812 mips_clear_insn_labels ();
2813 }
2814
2815 /* This function must be called whenever we turn on noreorder or emit
2816 something other than instructions. It inserts any NOPS which might
2817 be needed by the previous instruction, and clears the information
2818 kept for the previous instructions. The INSNS parameter is true if
2819 instructions are to follow. */
2820
2821 static void
2822 mips_emit_delays (insns)
2823 bfd_boolean insns;
2824 {
2825 if (! mips_opts.noreorder)
2826 {
2827 int nops;
2828
2829 nops = 0;
2830 if ((! mips_opts.mips16
2831 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2832 && (! cop_interlocks
2833 && (prev_insn.insn_mo->pinfo
2834 & (INSN_LOAD_COPROC_DELAY
2835 | INSN_COPROC_MOVE_DELAY
2836 | INSN_WRITE_COND_CODE))))
2837 || (! hilo_interlocks
2838 && (prev_insn.insn_mo->pinfo
2839 & (INSN_READ_LO
2840 | INSN_READ_HI)))
2841 || (! mips_opts.mips16
2842 && ! gpr_interlocks
2843 && (prev_insn.insn_mo->pinfo
2844 & INSN_LOAD_MEMORY_DELAY))
2845 || (! mips_opts.mips16
2846 && mips_opts.isa == ISA_MIPS1
2847 && (prev_insn.insn_mo->pinfo
2848 & INSN_COPROC_MEMORY_DELAY)))
2849 {
2850 /* Itbl support may require additional care here. */
2851 ++nops;
2852 if ((! mips_opts.mips16
2853 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2854 && (! cop_interlocks
2855 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2856 || (! hilo_interlocks
2857 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2858 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2859 ++nops;
2860
2861 if (prev_insn_unreordered)
2862 nops = 0;
2863 }
2864 else if ((! mips_opts.mips16
2865 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2866 && (! cop_interlocks
2867 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2868 || (! hilo_interlocks
2869 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2870 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2871 {
2872 /* Itbl support may require additional care here. */
2873 if (! prev_prev_insn_unreordered)
2874 ++nops;
2875 }
2876
2877 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2878 {
2879 int min_nops = 0;
2880 const char *pn = prev_insn.insn_mo->name;
2881 if (strncmp(pn, "macc", 4) == 0
2882 || strncmp(pn, "dmacc", 5) == 0
2883 || strncmp(pn, "dmult", 5) == 0)
2884 {
2885 min_nops = 1;
2886 }
2887 if (nops < min_nops)
2888 nops = min_nops;
2889 }
2890
2891 if (nops > 0)
2892 {
2893 struct insn_label_list *l;
2894
2895 if (insns)
2896 {
2897 /* Record the frag which holds the nop instructions, so
2898 that we can remove them if we don't need them. */
2899 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2900 prev_nop_frag = frag_now;
2901 prev_nop_frag_holds = nops;
2902 prev_nop_frag_required = 0;
2903 prev_nop_frag_since = 0;
2904 }
2905
2906 for (; nops > 0; --nops)
2907 emit_nop ();
2908
2909 if (insns)
2910 {
2911 /* Move on to a new frag, so that it is safe to simply
2912 decrease the size of prev_nop_frag. */
2913 frag_wane (frag_now);
2914 frag_new (0);
2915 }
2916
2917 for (l = insn_labels; l != NULL; l = l->next)
2918 {
2919 valueT val;
2920
2921 assert (S_GET_SEGMENT (l->label) == now_seg);
2922 symbol_set_frag (l->label, frag_now);
2923 val = (valueT) frag_now_fix ();
2924 /* mips16 text labels are stored as odd. */
2925 if (mips_opts.mips16)
2926 ++val;
2927 S_SET_VALUE (l->label, val);
2928 }
2929 }
2930 }
2931
2932 /* Mark instruction labels in mips16 mode. */
2933 if (insns)
2934 mips16_mark_labels ();
2935
2936 mips_no_prev_insn (insns);
2937 }
2938
2939 /* Build an instruction created by a macro expansion. This is passed
2940 a pointer to the count of instructions created so far, an
2941 expression, the name of the instruction to build, an operand format
2942 string, and corresponding arguments. */
2943
2944 #ifdef USE_STDARG
2945 static void
2946 macro_build (char *place,
2947 int *counter,
2948 expressionS * ep,
2949 const char *name,
2950 const char *fmt,
2951 ...)
2952 #else
2953 static void
2954 macro_build (place, counter, ep, name, fmt, va_alist)
2955 char *place;
2956 int *counter;
2957 expressionS *ep;
2958 const char *name;
2959 const char *fmt;
2960 va_dcl
2961 #endif
2962 {
2963 struct mips_cl_insn insn;
2964 bfd_reloc_code_real_type r[3];
2965 va_list args;
2966
2967 #ifdef USE_STDARG
2968 va_start (args, fmt);
2969 #else
2970 va_start (args);
2971 #endif
2972
2973 /*
2974 * If the macro is about to expand into a second instruction,
2975 * print a warning if needed. We need to pass ip as a parameter
2976 * to generate a better warning message here...
2977 */
2978 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2979 as_warn (_("Macro instruction expanded into multiple instructions"));
2980
2981 /*
2982 * If the macro is about to expand into a second instruction,
2983 * and it is in a delay slot, print a warning.
2984 */
2985 if (place == NULL
2986 && *counter == 1
2987 && mips_opts.noreorder
2988 && (prev_prev_insn.insn_mo->pinfo
2989 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2990 | INSN_COND_BRANCH_LIKELY)) != 0)
2991 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2992
2993 if (place == NULL)
2994 ++*counter; /* bump instruction counter */
2995
2996 if (mips_opts.mips16)
2997 {
2998 mips16_macro_build (place, counter, ep, name, fmt, args);
2999 va_end (args);
3000 return;
3001 }
3002
3003 r[0] = BFD_RELOC_UNUSED;
3004 r[1] = BFD_RELOC_UNUSED;
3005 r[2] = BFD_RELOC_UNUSED;
3006 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3007 assert (insn.insn_mo);
3008 assert (strcmp (name, insn.insn_mo->name) == 0);
3009
3010 /* Search until we get a match for NAME. */
3011 while (1)
3012 {
3013 /* It is assumed here that macros will never generate
3014 MDMX or MIPS-3D instructions. */
3015 if (strcmp (fmt, insn.insn_mo->args) == 0
3016 && insn.insn_mo->pinfo != INSN_MACRO
3017 && OPCODE_IS_MEMBER (insn.insn_mo,
3018 (mips_opts.isa
3019 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
3020 mips_arch)
3021 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
3022 break;
3023
3024 ++insn.insn_mo;
3025 assert (insn.insn_mo->name);
3026 assert (strcmp (name, insn.insn_mo->name) == 0);
3027 }
3028
3029 insn.insn_opcode = insn.insn_mo->match;
3030 for (;;)
3031 {
3032 switch (*fmt++)
3033 {
3034 case '\0':
3035 break;
3036
3037 case ',':
3038 case '(':
3039 case ')':
3040 continue;
3041
3042 case 't':
3043 case 'w':
3044 case 'E':
3045 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
3046 continue;
3047
3048 case 'c':
3049 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3050 continue;
3051
3052 case 'T':
3053 case 'W':
3054 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
3055 continue;
3056
3057 case 'd':
3058 case 'G':
3059 case 'K':
3060 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
3061 continue;
3062
3063 case 'U':
3064 {
3065 int tmp = va_arg (args, int);
3066
3067 insn.insn_opcode |= tmp << OP_SH_RT;
3068 insn.insn_opcode |= tmp << OP_SH_RD;
3069 continue;
3070 }
3071
3072 case 'V':
3073 case 'S':
3074 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
3075 continue;
3076
3077 case 'z':
3078 continue;
3079
3080 case '<':
3081 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
3082 continue;
3083
3084 case 'D':
3085 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
3086 continue;
3087
3088 case 'B':
3089 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
3090 continue;
3091
3092 case 'J':
3093 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
3094 continue;
3095
3096 case 'q':
3097 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
3098 continue;
3099
3100 case 'b':
3101 case 's':
3102 case 'r':
3103 case 'v':
3104 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
3105 continue;
3106
3107 case 'i':
3108 case 'j':
3109 case 'o':
3110 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3111 assert (*r == BFD_RELOC_GPREL16
3112 || *r == BFD_RELOC_MIPS_LITERAL
3113 || *r == BFD_RELOC_MIPS_HIGHER
3114 || *r == BFD_RELOC_HI16_S
3115 || *r == BFD_RELOC_LO16
3116 || *r == BFD_RELOC_MIPS_GOT16
3117 || *r == BFD_RELOC_MIPS_CALL16
3118 || *r == BFD_RELOC_MIPS_GOT_DISP
3119 || *r == BFD_RELOC_MIPS_GOT_PAGE
3120 || *r == BFD_RELOC_MIPS_GOT_OFST
3121 || *r == BFD_RELOC_MIPS_GOT_LO16
3122 || *r == BFD_RELOC_MIPS_CALL_LO16
3123 || (ep->X_op == O_subtract
3124 && *r == BFD_RELOC_PCREL_LO16));
3125 continue;
3126
3127 case 'u':
3128 *r = (bfd_reloc_code_real_type) va_arg (args, int);
3129 assert (ep != NULL
3130 && (ep->X_op == O_constant
3131 || (ep->X_op == O_symbol
3132 && (*r == BFD_RELOC_MIPS_HIGHEST
3133 || *r == BFD_RELOC_HI16_S
3134 || *r == BFD_RELOC_HI16
3135 || *r == BFD_RELOC_GPREL16
3136 || *r == BFD_RELOC_MIPS_GOT_HI16
3137 || *r == BFD_RELOC_MIPS_CALL_HI16))
3138 || (ep->X_op == O_subtract
3139 && *r == BFD_RELOC_PCREL_HI16_S)));
3140 continue;
3141
3142 case 'p':
3143 assert (ep != NULL);
3144 /*
3145 * This allows macro() to pass an immediate expression for
3146 * creating short branches without creating a symbol.
3147 * Note that the expression still might come from the assembly
3148 * input, in which case the value is not checked for range nor
3149 * is a relocation entry generated (yuck).
3150 */
3151 if (ep->X_op == O_constant)
3152 {
3153 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3154 ep = NULL;
3155 }
3156 else
3157 *r = BFD_RELOC_16_PCREL_S2;
3158 continue;
3159
3160 case 'a':
3161 assert (ep != NULL);
3162 *r = BFD_RELOC_MIPS_JMP;
3163 continue;
3164
3165 case 'C':
3166 insn.insn_opcode |= va_arg (args, unsigned long);
3167 continue;
3168
3169 default:
3170 internalError ();
3171 }
3172 break;
3173 }
3174 va_end (args);
3175 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3176
3177 append_insn (place, &insn, ep, r);
3178 }
3179
3180 static void
3181 mips16_macro_build (place, counter, ep, name, fmt, args)
3182 char *place;
3183 int *counter ATTRIBUTE_UNUSED;
3184 expressionS *ep;
3185 const char *name;
3186 const char *fmt;
3187 va_list args;
3188 {
3189 struct mips_cl_insn insn;
3190 bfd_reloc_code_real_type r[3]
3191 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3192
3193 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3194 assert (insn.insn_mo);
3195 assert (strcmp (name, insn.insn_mo->name) == 0);
3196
3197 while (strcmp (fmt, insn.insn_mo->args) != 0
3198 || insn.insn_mo->pinfo == INSN_MACRO)
3199 {
3200 ++insn.insn_mo;
3201 assert (insn.insn_mo->name);
3202 assert (strcmp (name, insn.insn_mo->name) == 0);
3203 }
3204
3205 insn.insn_opcode = insn.insn_mo->match;
3206 insn.use_extend = FALSE;
3207
3208 for (;;)
3209 {
3210 int c;
3211
3212 c = *fmt++;
3213 switch (c)
3214 {
3215 case '\0':
3216 break;
3217
3218 case ',':
3219 case '(':
3220 case ')':
3221 continue;
3222
3223 case 'y':
3224 case 'w':
3225 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3226 continue;
3227
3228 case 'x':
3229 case 'v':
3230 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3231 continue;
3232
3233 case 'z':
3234 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3235 continue;
3236
3237 case 'Z':
3238 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3239 continue;
3240
3241 case '0':
3242 case 'S':
3243 case 'P':
3244 case 'R':
3245 continue;
3246
3247 case 'X':
3248 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3249 continue;
3250
3251 case 'Y':
3252 {
3253 int regno;
3254
3255 regno = va_arg (args, int);
3256 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3257 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3258 }
3259 continue;
3260
3261 case '<':
3262 case '>':
3263 case '4':
3264 case '5':
3265 case 'H':
3266 case 'W':
3267 case 'D':
3268 case 'j':
3269 case '8':
3270 case 'V':
3271 case 'C':
3272 case 'U':
3273 case 'k':
3274 case 'K':
3275 case 'p':
3276 case 'q':
3277 {
3278 assert (ep != NULL);
3279
3280 if (ep->X_op != O_constant)
3281 *r = (int) BFD_RELOC_UNUSED + c;
3282 else
3283 {
3284 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3285 FALSE, &insn.insn_opcode, &insn.use_extend,
3286 &insn.extend);
3287 ep = NULL;
3288 *r = BFD_RELOC_UNUSED;
3289 }
3290 }
3291 continue;
3292
3293 case '6':
3294 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3295 continue;
3296 }
3297
3298 break;
3299 }
3300
3301 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3302
3303 append_insn (place, &insn, ep, r);
3304 }
3305
3306 /*
3307 * Generate a "jalr" instruction with a relocation hint to the called
3308 * function. This occurs in NewABI PIC code.
3309 */
3310 static void
3311 macro_build_jalr (icnt, ep)
3312 int icnt;
3313 expressionS *ep;
3314 {
3315 char *f = NULL;;
3316
3317 if (HAVE_NEWABI)
3318 {
3319 frag_grow (4);
3320 f = frag_more (0);
3321 }
3322 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3323 RA, PIC_CALL_REG);
3324 if (HAVE_NEWABI)
3325 fix_new_exp (frag_now, f - frag_now->fr_literal,
3326 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
3327 }
3328
3329 /*
3330 * Generate a "lui" instruction.
3331 */
3332 static void
3333 macro_build_lui (place, counter, ep, regnum)
3334 char *place;
3335 int *counter;
3336 expressionS *ep;
3337 int regnum;
3338 {
3339 expressionS high_expr;
3340 struct mips_cl_insn insn;
3341 bfd_reloc_code_real_type r[3]
3342 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3343 const char *name = "lui";
3344 const char *fmt = "t,u";
3345
3346 assert (! mips_opts.mips16);
3347
3348 if (place == NULL)
3349 high_expr = *ep;
3350 else
3351 {
3352 high_expr.X_op = O_constant;
3353 high_expr.X_add_number = ep->X_add_number;
3354 }
3355
3356 if (high_expr.X_op == O_constant)
3357 {
3358 /* we can compute the instruction now without a relocation entry */
3359 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3360 >> 16) & 0xffff;
3361 *r = BFD_RELOC_UNUSED;
3362 }
3363 else
3364 {
3365 assert (ep->X_op == O_symbol);
3366 /* _gp_disp is a special case, used from s_cpload. */
3367 assert (mips_pic == NO_PIC
3368 || (! HAVE_NEWABI
3369 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
3370 *r = BFD_RELOC_HI16_S;
3371 }
3372
3373 /*
3374 * If the macro is about to expand into a second instruction,
3375 * print a warning if needed. We need to pass ip as a parameter
3376 * to generate a better warning message here...
3377 */
3378 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3379 as_warn (_("Macro instruction expanded into multiple instructions"));
3380
3381 if (place == NULL)
3382 ++*counter; /* bump instruction counter */
3383
3384 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3385 assert (insn.insn_mo);
3386 assert (strcmp (name, insn.insn_mo->name) == 0);
3387 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3388
3389 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3390 if (*r == BFD_RELOC_UNUSED)
3391 {
3392 insn.insn_opcode |= high_expr.X_add_number;
3393 append_insn (place, &insn, NULL, r);
3394 }
3395 else
3396 append_insn (place, &insn, &high_expr, r);
3397 }
3398
3399 /* Generate a sequence of instructions to do a load or store from a constant
3400 offset off of a base register (breg) into/from a target register (treg),
3401 using AT if necessary. */
3402 static void
3403 macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3404 char *place;
3405 int *counter;
3406 expressionS *ep;
3407 const char *op;
3408 int treg, breg;
3409 {
3410 assert (ep->X_op == O_constant);
3411
3412 /* Right now, this routine can only handle signed 32-bit contants. */
3413 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3414 as_warn (_("operand overflow"));
3415
3416 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3417 {
3418 /* Signed 16-bit offset will fit in the op. Easy! */
3419 macro_build (place, counter, ep, op, "t,o(b)", treg,
3420 (int) BFD_RELOC_LO16, breg);
3421 }
3422 else
3423 {
3424 /* 32-bit offset, need multiple instructions and AT, like:
3425 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3426 addu $tempreg,$tempreg,$breg
3427 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3428 to handle the complete offset. */
3429 macro_build_lui (place, counter, ep, AT);
3430 if (place != NULL)
3431 place += 4;
3432 macro_build (place, counter, (expressionS *) NULL,
3433 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3434 ? "add" : "addu" : "daddu",
3435 "d,v,t", AT, AT, breg);
3436 if (place != NULL)
3437 place += 4;
3438 macro_build (place, counter, ep, op, "t,o(b)", treg,
3439 (int) BFD_RELOC_LO16, AT);
3440
3441 if (mips_opts.noat)
3442 as_warn (_("Macro used $at after \".set noat\""));
3443 }
3444 }
3445
3446 /* set_at()
3447 * Generates code to set the $at register to true (one)
3448 * if reg is less than the immediate expression.
3449 */
3450 static void
3451 set_at (counter, reg, unsignedp)
3452 int *counter;
3453 int reg;
3454 int unsignedp;
3455 {
3456 if (imm_expr.X_op == O_constant
3457 && imm_expr.X_add_number >= -0x8000
3458 && imm_expr.X_add_number < 0x8000)
3459 macro_build ((char *) NULL, counter, &imm_expr,
3460 unsignedp ? "sltiu" : "slti",
3461 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3462 else
3463 {
3464 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
3465 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3466 unsignedp ? "sltu" : "slt",
3467 "d,v,t", AT, reg, AT);
3468 }
3469 }
3470
3471 /* Warn if an expression is not a constant. */
3472
3473 static void
3474 check_absolute_expr (ip, ex)
3475 struct mips_cl_insn *ip;
3476 expressionS *ex;
3477 {
3478 if (ex->X_op == O_big)
3479 as_bad (_("unsupported large constant"));
3480 else if (ex->X_op != O_constant)
3481 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3482 }
3483
3484 /* Count the leading zeroes by performing a binary chop. This is a
3485 bulky bit of source, but performance is a LOT better for the
3486 majority of values than a simple loop to count the bits:
3487 for (lcnt = 0; (lcnt < 32); lcnt++)
3488 if ((v) & (1 << (31 - lcnt)))
3489 break;
3490 However it is not code size friendly, and the gain will drop a bit
3491 on certain cached systems.
3492 */
3493 #define COUNT_TOP_ZEROES(v) \
3494 (((v) & ~0xffff) == 0 \
3495 ? ((v) & ~0xff) == 0 \
3496 ? ((v) & ~0xf) == 0 \
3497 ? ((v) & ~0x3) == 0 \
3498 ? ((v) & ~0x1) == 0 \
3499 ? !(v) \
3500 ? 32 \
3501 : 31 \
3502 : 30 \
3503 : ((v) & ~0x7) == 0 \
3504 ? 29 \
3505 : 28 \
3506 : ((v) & ~0x3f) == 0 \
3507 ? ((v) & ~0x1f) == 0 \
3508 ? 27 \
3509 : 26 \
3510 : ((v) & ~0x7f) == 0 \
3511 ? 25 \
3512 : 24 \
3513 : ((v) & ~0xfff) == 0 \
3514 ? ((v) & ~0x3ff) == 0 \
3515 ? ((v) & ~0x1ff) == 0 \
3516 ? 23 \
3517 : 22 \
3518 : ((v) & ~0x7ff) == 0 \
3519 ? 21 \
3520 : 20 \
3521 : ((v) & ~0x3fff) == 0 \
3522 ? ((v) & ~0x1fff) == 0 \
3523 ? 19 \
3524 : 18 \
3525 : ((v) & ~0x7fff) == 0 \
3526 ? 17 \
3527 : 16 \
3528 : ((v) & ~0xffffff) == 0 \
3529 ? ((v) & ~0xfffff) == 0 \
3530 ? ((v) & ~0x3ffff) == 0 \
3531 ? ((v) & ~0x1ffff) == 0 \
3532 ? 15 \
3533 : 14 \
3534 : ((v) & ~0x7ffff) == 0 \
3535 ? 13 \
3536 : 12 \
3537 : ((v) & ~0x3fffff) == 0 \
3538 ? ((v) & ~0x1fffff) == 0 \
3539 ? 11 \
3540 : 10 \
3541 : ((v) & ~0x7fffff) == 0 \
3542 ? 9 \
3543 : 8 \
3544 : ((v) & ~0xfffffff) == 0 \
3545 ? ((v) & ~0x3ffffff) == 0 \
3546 ? ((v) & ~0x1ffffff) == 0 \
3547 ? 7 \
3548 : 6 \
3549 : ((v) & ~0x7ffffff) == 0 \
3550 ? 5 \
3551 : 4 \
3552 : ((v) & ~0x3fffffff) == 0 \
3553 ? ((v) & ~0x1fffffff) == 0 \
3554 ? 3 \
3555 : 2 \
3556 : ((v) & ~0x7fffffff) == 0 \
3557 ? 1 \
3558 : 0)
3559
3560 /* load_register()
3561 * This routine generates the least number of instructions neccessary to load
3562 * an absolute expression value into a register.
3563 */
3564 static void
3565 load_register (counter, reg, ep, dbl)
3566 int *counter;
3567 int reg;
3568 expressionS *ep;
3569 int dbl;
3570 {
3571 int freg;
3572 expressionS hi32, lo32;
3573
3574 if (ep->X_op != O_big)
3575 {
3576 assert (ep->X_op == O_constant);
3577 if (ep->X_add_number < 0x8000
3578 && (ep->X_add_number >= 0
3579 || (ep->X_add_number >= -0x8000
3580 && (! dbl
3581 || ! ep->X_unsigned
3582 || sizeof (ep->X_add_number) > 4))))
3583 {
3584 /* We can handle 16 bit signed values with an addiu to
3585 $zero. No need to ever use daddiu here, since $zero and
3586 the result are always correct in 32 bit mode. */
3587 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3588 (int) BFD_RELOC_LO16);
3589 return;
3590 }
3591 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3592 {
3593 /* We can handle 16 bit unsigned values with an ori to
3594 $zero. */
3595 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3596 (int) BFD_RELOC_LO16);
3597 return;
3598 }
3599 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3600 && (! dbl
3601 || ! ep->X_unsigned
3602 || sizeof (ep->X_add_number) > 4
3603 || (ep->X_add_number & 0x80000000) == 0))
3604 || ((HAVE_32BIT_GPRS || ! dbl)
3605 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3606 || (HAVE_32BIT_GPRS
3607 && ! dbl
3608 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3609 == ~ (offsetT) 0xffffffff)))
3610 {
3611 /* 32 bit values require an lui. */
3612 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3613 (int) BFD_RELOC_HI16);
3614 if ((ep->X_add_number & 0xffff) != 0)
3615 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3616 (int) BFD_RELOC_LO16);
3617 return;
3618 }
3619 }
3620
3621 /* The value is larger than 32 bits. */
3622
3623 if (HAVE_32BIT_GPRS)
3624 {
3625 as_bad (_("Number (0x%lx) larger than 32 bits"),
3626 (unsigned long) ep->X_add_number);
3627 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3628 (int) BFD_RELOC_LO16);
3629 return;
3630 }
3631
3632 if (ep->X_op != O_big)
3633 {
3634 hi32 = *ep;
3635 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3636 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3637 hi32.X_add_number &= 0xffffffff;
3638 lo32 = *ep;
3639 lo32.X_add_number &= 0xffffffff;
3640 }
3641 else
3642 {
3643 assert (ep->X_add_number > 2);
3644 if (ep->X_add_number == 3)
3645 generic_bignum[3] = 0;
3646 else if (ep->X_add_number > 4)
3647 as_bad (_("Number larger than 64 bits"));
3648 lo32.X_op = O_constant;
3649 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3650 hi32.X_op = O_constant;
3651 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3652 }
3653
3654 if (hi32.X_add_number == 0)
3655 freg = 0;
3656 else
3657 {
3658 int shift, bit;
3659 unsigned long hi, lo;
3660
3661 if (hi32.X_add_number == (offsetT) 0xffffffff)
3662 {
3663 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3664 {
3665 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3666 reg, 0, (int) BFD_RELOC_LO16);
3667 return;
3668 }
3669 if (lo32.X_add_number & 0x80000000)
3670 {
3671 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3672 (int) BFD_RELOC_HI16);
3673 if (lo32.X_add_number & 0xffff)
3674 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3675 reg, reg, (int) BFD_RELOC_LO16);
3676 return;
3677 }
3678 }
3679
3680 /* Check for 16bit shifted constant. We know that hi32 is
3681 non-zero, so start the mask on the first bit of the hi32
3682 value. */
3683 shift = 17;
3684 do
3685 {
3686 unsigned long himask, lomask;
3687
3688 if (shift < 32)
3689 {
3690 himask = 0xffff >> (32 - shift);
3691 lomask = (0xffff << shift) & 0xffffffff;
3692 }
3693 else
3694 {
3695 himask = 0xffff << (shift - 32);
3696 lomask = 0;
3697 }
3698 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3699 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3700 {
3701 expressionS tmp;
3702
3703 tmp.X_op = O_constant;
3704 if (shift < 32)
3705 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3706 | (lo32.X_add_number >> shift));
3707 else
3708 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3709 macro_build ((char *) NULL, counter, &tmp,
3710 "ori", "t,r,i", reg, 0,
3711 (int) BFD_RELOC_LO16);
3712 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3713 (shift >= 32) ? "dsll32" : "dsll",
3714 "d,w,<", reg, reg,
3715 (shift >= 32) ? shift - 32 : shift);
3716 return;
3717 }
3718 ++shift;
3719 }
3720 while (shift <= (64 - 16));
3721
3722 /* Find the bit number of the lowest one bit, and store the
3723 shifted value in hi/lo. */
3724 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3725 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3726 if (lo != 0)
3727 {
3728 bit = 0;
3729 while ((lo & 1) == 0)
3730 {
3731 lo >>= 1;
3732 ++bit;
3733 }
3734 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3735 hi >>= bit;
3736 }
3737 else
3738 {
3739 bit = 32;
3740 while ((hi & 1) == 0)
3741 {
3742 hi >>= 1;
3743 ++bit;
3744 }
3745 lo = hi;
3746 hi = 0;
3747 }
3748
3749 /* Optimize if the shifted value is a (power of 2) - 1. */
3750 if ((hi == 0 && ((lo + 1) & lo) == 0)
3751 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3752 {
3753 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3754 if (shift != 0)
3755 {
3756 expressionS tmp;
3757
3758 /* This instruction will set the register to be all
3759 ones. */
3760 tmp.X_op = O_constant;
3761 tmp.X_add_number = (offsetT) -1;
3762 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3763 reg, 0, (int) BFD_RELOC_LO16);
3764 if (bit != 0)
3765 {
3766 bit += shift;
3767 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3768 (bit >= 32) ? "dsll32" : "dsll",
3769 "d,w,<", reg, reg,
3770 (bit >= 32) ? bit - 32 : bit);
3771 }
3772 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3773 (shift >= 32) ? "dsrl32" : "dsrl",
3774 "d,w,<", reg, reg,
3775 (shift >= 32) ? shift - 32 : shift);
3776 return;
3777 }
3778 }
3779
3780 /* Sign extend hi32 before calling load_register, because we can
3781 generally get better code when we load a sign extended value. */
3782 if ((hi32.X_add_number & 0x80000000) != 0)
3783 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3784 load_register (counter, reg, &hi32, 0);
3785 freg = reg;
3786 }
3787 if ((lo32.X_add_number & 0xffff0000) == 0)
3788 {
3789 if (freg != 0)
3790 {
3791 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3792 "dsll32", "d,w,<", reg, freg, 0);
3793 freg = reg;
3794 }
3795 }
3796 else
3797 {
3798 expressionS mid16;
3799
3800 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3801 {
3802 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3803 (int) BFD_RELOC_HI16);
3804 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3805 "dsrl32", "d,w,<", reg, reg, 0);
3806 return;
3807 }
3808
3809 if (freg != 0)
3810 {
3811 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3812 "d,w,<", reg, freg, 16);
3813 freg = reg;
3814 }
3815 mid16 = lo32;
3816 mid16.X_add_number >>= 16;
3817 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3818 freg, (int) BFD_RELOC_LO16);
3819 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3820 "d,w,<", reg, reg, 16);
3821 freg = reg;
3822 }
3823 if ((lo32.X_add_number & 0xffff) != 0)
3824 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3825 (int) BFD_RELOC_LO16);
3826 }
3827
3828 /* Load an address into a register. */
3829
3830 static void
3831 load_address (counter, reg, ep, used_at)
3832 int *counter;
3833 int reg;
3834 expressionS *ep;
3835 int *used_at;
3836 {
3837 char *p = NULL;
3838
3839 if (ep->X_op != O_constant
3840 && ep->X_op != O_symbol)
3841 {
3842 as_bad (_("expression too complex"));
3843 ep->X_op = O_constant;
3844 }
3845
3846 if (ep->X_op == O_constant)
3847 {
3848 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
3849 return;
3850 }
3851
3852 if (mips_pic == NO_PIC)
3853 {
3854 /* If this is a reference to a GP relative symbol, we want
3855 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3856 Otherwise we want
3857 lui $reg,<sym> (BFD_RELOC_HI16_S)
3858 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3859 If we have an addend, we always use the latter form.
3860
3861 With 64bit address space and a usable $at we want
3862 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3863 lui $at,<sym> (BFD_RELOC_HI16_S)
3864 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3865 daddiu $at,<sym> (BFD_RELOC_LO16)
3866 dsll32 $reg,0
3867 daddu $reg,$reg,$at
3868
3869 If $at is already in use, we use a path which is suboptimal
3870 on superscalar processors.
3871 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3872 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3873 dsll $reg,16
3874 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3875 dsll $reg,16
3876 daddiu $reg,<sym> (BFD_RELOC_LO16)
3877 */
3878 if (HAVE_64BIT_ADDRESSES)
3879 {
3880 /* We don't do GP optimization for now because RELAX_ENCODE can't
3881 hold the data for such large chunks. */
3882
3883 if (*used_at == 0 && ! mips_opts.noat)
3884 {
3885 macro_build (p, counter, ep, "lui", "t,u",
3886 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3887 macro_build (p, counter, ep, "lui", "t,u",
3888 AT, (int) BFD_RELOC_HI16_S);
3889 macro_build (p, counter, ep, "daddiu", "t,r,j",
3890 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3891 macro_build (p, counter, ep, "daddiu", "t,r,j",
3892 AT, AT, (int) BFD_RELOC_LO16);
3893 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3894 "d,w,<", reg, reg, 0);
3895 macro_build (p, counter, (expressionS *) NULL, "daddu",
3896 "d,v,t", reg, reg, AT);
3897 *used_at = 1;
3898 }
3899 else
3900 {
3901 macro_build (p, counter, ep, "lui", "t,u",
3902 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3903 macro_build (p, counter, ep, "daddiu", "t,r,j",
3904 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3905 macro_build (p, counter, (expressionS *) NULL, "dsll",
3906 "d,w,<", reg, reg, 16);
3907 macro_build (p, counter, ep, "daddiu", "t,r,j",
3908 reg, reg, (int) BFD_RELOC_HI16_S);
3909 macro_build (p, counter, (expressionS *) NULL, "dsll",
3910 "d,w,<", reg, reg, 16);
3911 macro_build (p, counter, ep, "daddiu", "t,r,j",
3912 reg, reg, (int) BFD_RELOC_LO16);
3913 }
3914 }
3915 else
3916 {
3917 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3918 && ! nopic_need_relax (ep->X_add_symbol, 1))
3919 {
3920 frag_grow (20);
3921 macro_build ((char *) NULL, counter, ep,
3922 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3923 ? "addi" : "addiu" : "daddiu", "t,r,j",
3924 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
3925 p = frag_var (rs_machine_dependent, 8, 0,
3926 RELAX_ENCODE (4, 8, 0, 4, 0,
3927 mips_opts.warn_about_macros),
3928 ep->X_add_symbol, 0, NULL);
3929 }
3930 macro_build_lui (p, counter, ep, reg);
3931 if (p != NULL)
3932 p += 4;
3933 macro_build (p, counter, ep,
3934 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3935 ? "addi" : "addiu" : "daddiu",
3936 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3937 }
3938 }
3939 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3940 {
3941 expressionS ex;
3942
3943 /* If this is a reference to an external symbol, we want
3944 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3945 Otherwise we want
3946 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3947 nop
3948 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3949 If there is a constant, it must be added in after.
3950
3951 If we have NewABI, we want
3952 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3953 unless we're referencing a global symbol with a non-zero
3954 offset, in which case cst must be added separately. */
3955 if (HAVE_NEWABI)
3956 {
3957 frag_grow (12);
3958
3959 if (ep->X_add_number)
3960 {
3961 frag_now->tc_frag_data.tc_fr_offset =
3962 ex.X_add_number = ep->X_add_number;
3963 ep->X_add_number = 0;
3964 macro_build ((char *) NULL, counter, ep,
3965 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3966 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3967 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3968 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3969 ex.X_op = O_constant;
3970 macro_build ((char *) NULL, counter, &ex,
3971 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
3972 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3973 p = frag_var (rs_machine_dependent, 8, 0,
3974 RELAX_ENCODE (8, 4, 0, 0, 0,
3975 mips_opts.warn_about_macros),
3976 ep->X_add_symbol, 0, (char *) NULL);
3977 ep->X_add_number = ex.X_add_number;
3978 }
3979
3980 macro_build (p, counter, ep,
3981 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3982 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3983
3984 if (! p)
3985 {
3986 /* To avoid confusion in tc_gen_reloc, we must ensure
3987 that this does not become a variant frag. */
3988 frag_wane (frag_now);
3989 frag_new (0);
3990 }
3991 }
3992 else
3993 {
3994 ex.X_add_number = ep->X_add_number;
3995 ep->X_add_number = 0;
3996 frag_grow (20);
3997 macro_build ((char *) NULL, counter, ep,
3998 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3999 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4000 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
4001 p = frag_var (rs_machine_dependent, 4, 0,
4002 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
4003 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
4004 macro_build (p, counter, ep,
4005 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4006 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4007
4008 if (ex.X_add_number != 0)
4009 {
4010 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4011 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4012 ex.X_op = O_constant;
4013 macro_build ((char *) NULL, counter, &ex,
4014 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4015 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4016 }
4017 }
4018 }
4019 else if (mips_pic == SVR4_PIC)
4020 {
4021 expressionS ex;
4022 int off;
4023
4024 /* This is the large GOT case. If this is a reference to an
4025 external symbol, we want
4026 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4027 addu $reg,$reg,$gp
4028 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
4029
4030 Otherwise, for a reference to a local symbol in old ABI, we want
4031 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4032 nop
4033 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
4034 If there is a constant, it must be added in after.
4035
4036 In the NewABI, for local symbols, with or without offsets, we want:
4037 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4038 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4039 */
4040 if (HAVE_NEWABI)
4041 {
4042 frag_grow (24);
4043
4044 frag_now->tc_frag_data.tc_fr_offset =
4045 ex.X_add_number = ep->X_add_number;
4046 ep->X_add_number = 0;
4047 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4048 (int) BFD_RELOC_MIPS_GOT_HI16);
4049 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4050 HAVE_32BIT_ADDRESSES ? "add" : "daddu", "d,v,t", reg,
4051 reg, mips_gp_register);
4052 macro_build ((char *) NULL, counter, ep,
4053 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4054 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4055 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4056 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4057 else if (ex.X_add_number)
4058 {
4059 ex.X_op = O_constant;
4060 macro_build ((char *) NULL, counter, &ex,
4061 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
4062 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4063 }
4064
4065 ep->X_add_number = ex.X_add_number;
4066 p = frag_var (rs_machine_dependent, 8, 0,
4067 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
4068 mips_opts.warn_about_macros),
4069 ep->X_add_symbol, 0, (char *) NULL);
4070 macro_build (p, counter, ep,
4071 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4072 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
4073 macro_build (p + 4, counter, ep,
4074 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
4075 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
4076 }
4077 else
4078 {
4079 ex.X_add_number = ep->X_add_number;
4080 ep->X_add_number = 0;
4081 if (reg_needs_delay (mips_gp_register))
4082 off = 4;
4083 else
4084 off = 0;
4085 frag_grow (32);
4086 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4087 (int) BFD_RELOC_MIPS_GOT_HI16);
4088 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4089 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
4090 reg, mips_gp_register);
4091 macro_build ((char *) NULL, counter, ep,
4092 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4093 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4094 p = frag_var (rs_machine_dependent, 12 + off, 0,
4095 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4096 mips_opts.warn_about_macros),
4097 ep->X_add_symbol, 0, NULL);
4098 if (off > 0)
4099 {
4100 /* We need a nop before loading from $gp. This special
4101 check is required because the lui which starts the main
4102 instruction stream does not refer to $gp, and so will not
4103 insert the nop which may be required. */
4104 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4105 p += 4;
4106 }
4107 macro_build (p, counter, ep,
4108 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4109 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4110 p += 4;
4111 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4112 p += 4;
4113 macro_build (p, counter, ep,
4114 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4115 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4116
4117 if (ex.X_add_number != 0)
4118 {
4119 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4120 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4121 ex.X_op = O_constant;
4122 macro_build ((char *) NULL, counter, &ex,
4123 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4124 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4125 }
4126 }
4127 }
4128 else if (mips_pic == EMBEDDED_PIC)
4129 {
4130 /* We always do
4131 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
4132 */
4133 macro_build ((char *) NULL, counter, ep,
4134 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4135 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
4136 }
4137 else
4138 abort ();
4139 }
4140
4141 /* Move the contents of register SOURCE into register DEST. */
4142
4143 static void
4144 move_register (counter, dest, source)
4145 int *counter;
4146 int dest;
4147 int source;
4148 {
4149 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4150 HAVE_32BIT_GPRS ? "addu" : "daddu",
4151 "d,v,t", dest, source, 0);
4152 }
4153
4154 /*
4155 * Build macros
4156 * This routine implements the seemingly endless macro or synthesized
4157 * instructions and addressing modes in the mips assembly language. Many
4158 * of these macros are simple and are similar to each other. These could
4159 * probably be handled by some kind of table or grammer aproach instead of
4160 * this verbose method. Others are not simple macros but are more like
4161 * optimizing code generation.
4162 * One interesting optimization is when several store macros appear
4163 * consecutivly that would load AT with the upper half of the same address.
4164 * The ensuing load upper instructions are ommited. This implies some kind
4165 * of global optimization. We currently only optimize within a single macro.
4166 * For many of the load and store macros if the address is specified as a
4167 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4168 * first load register 'at' with zero and use it as the base register. The
4169 * mips assembler simply uses register $zero. Just one tiny optimization
4170 * we're missing.
4171 */
4172 static void
4173 macro (ip)
4174 struct mips_cl_insn *ip;
4175 {
4176 register int treg, sreg, dreg, breg;
4177 int tempreg;
4178 int mask;
4179 int icnt = 0;
4180 int used_at = 0;
4181 expressionS expr1;
4182 const char *s;
4183 const char *s2;
4184 const char *fmt;
4185 int likely = 0;
4186 int dbl = 0;
4187 int coproc = 0;
4188 int lr = 0;
4189 int imm = 0;
4190 offsetT maxnum;
4191 int off;
4192 bfd_reloc_code_real_type r;
4193 int hold_mips_optimize;
4194
4195 assert (! mips_opts.mips16);
4196
4197 treg = (ip->insn_opcode >> 16) & 0x1f;
4198 dreg = (ip->insn_opcode >> 11) & 0x1f;
4199 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4200 mask = ip->insn_mo->mask;
4201
4202 expr1.X_op = O_constant;
4203 expr1.X_op_symbol = NULL;
4204 expr1.X_add_symbol = NULL;
4205 expr1.X_add_number = 1;
4206
4207 /* Umatched fixups should not be put in the same frag as a relaxable
4208 macro. For example, suppose we have:
4209
4210 lui $4,%hi(l1) # 1
4211 la $5,l2 # 2
4212 addiu $4,$4,%lo(l1) # 3
4213
4214 If instructions 1 and 2 were put in the same frag, md_frob_file would
4215 move the fixup for #1 after the fixups for the "unrelaxed" version of
4216 #2. This would confuse tc_gen_reloc, which expects the relocations
4217 for #2 to be the last for that frag.
4218
4219 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4220 it assumes that they belong to a relaxable macro. We mustn't put
4221 other uses of such relocations into a variant frag.
4222
4223 To avoid both problems, finish the current frag it contains a
4224 %reloc() operator. The macro then goes into a new frag. */
4225 if (prev_reloc_op_frag == frag_now)
4226 {
4227 frag_wane (frag_now);
4228 frag_new (0);
4229 }
4230
4231 switch (mask)
4232 {
4233 case M_DABS:
4234 dbl = 1;
4235 case M_ABS:
4236 /* bgez $a0,.+12
4237 move v0,$a0
4238 sub v0,$zero,$a0
4239 */
4240
4241 mips_emit_delays (TRUE);
4242 ++mips_opts.noreorder;
4243 mips_any_noreorder = 1;
4244
4245 expr1.X_add_number = 8;
4246 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4247 if (dreg == sreg)
4248 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4249 0);
4250 else
4251 move_register (&icnt, dreg, sreg);
4252 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4253 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
4254
4255 --mips_opts.noreorder;
4256 return;
4257
4258 case M_ADD_I:
4259 s = "addi";
4260 s2 = "add";
4261 goto do_addi;
4262 case M_ADDU_I:
4263 s = "addiu";
4264 s2 = "addu";
4265 goto do_addi;
4266 case M_DADD_I:
4267 dbl = 1;
4268 s = "daddi";
4269 s2 = "dadd";
4270 goto do_addi;
4271 case M_DADDU_I:
4272 dbl = 1;
4273 s = "daddiu";
4274 s2 = "daddu";
4275 do_addi:
4276 if (imm_expr.X_op == O_constant
4277 && imm_expr.X_add_number >= -0x8000
4278 && imm_expr.X_add_number < 0x8000)
4279 {
4280 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4281 (int) BFD_RELOC_LO16);
4282 return;
4283 }
4284 load_register (&icnt, AT, &imm_expr, dbl);
4285 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4286 treg, sreg, AT);
4287 break;
4288
4289 case M_AND_I:
4290 s = "andi";
4291 s2 = "and";
4292 goto do_bit;
4293 case M_OR_I:
4294 s = "ori";
4295 s2 = "or";
4296 goto do_bit;
4297 case M_NOR_I:
4298 s = "";
4299 s2 = "nor";
4300 goto do_bit;
4301 case M_XOR_I:
4302 s = "xori";
4303 s2 = "xor";
4304 do_bit:
4305 if (imm_expr.X_op == O_constant
4306 && imm_expr.X_add_number >= 0
4307 && imm_expr.X_add_number < 0x10000)
4308 {
4309 if (mask != M_NOR_I)
4310 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4311 sreg, (int) BFD_RELOC_LO16);
4312 else
4313 {
4314 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4315 treg, sreg, (int) BFD_RELOC_LO16);
4316 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4317 "d,v,t", treg, treg, 0);
4318 }
4319 return;
4320 }
4321
4322 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4323 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4324 treg, sreg, AT);
4325 break;
4326
4327 case M_BEQ_I:
4328 s = "beq";
4329 goto beq_i;
4330 case M_BEQL_I:
4331 s = "beql";
4332 likely = 1;
4333 goto beq_i;
4334 case M_BNE_I:
4335 s = "bne";
4336 goto beq_i;
4337 case M_BNEL_I:
4338 s = "bnel";
4339 likely = 1;
4340 beq_i:
4341 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4342 {
4343 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4344 0);
4345 return;
4346 }
4347 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4348 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4349 break;
4350
4351 case M_BGEL:
4352 likely = 1;
4353 case M_BGE:
4354 if (treg == 0)
4355 {
4356 macro_build ((char *) NULL, &icnt, &offset_expr,
4357 likely ? "bgezl" : "bgez", "s,p", sreg);
4358 return;
4359 }
4360 if (sreg == 0)
4361 {
4362 macro_build ((char *) NULL, &icnt, &offset_expr,
4363 likely ? "blezl" : "blez", "s,p", treg);
4364 return;
4365 }
4366 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4367 AT, sreg, treg);
4368 macro_build ((char *) NULL, &icnt, &offset_expr,
4369 likely ? "beql" : "beq", "s,t,p", AT, 0);
4370 break;
4371
4372 case M_BGTL_I:
4373 likely = 1;
4374 case M_BGT_I:
4375 /* check for > max integer */
4376 maxnum = 0x7fffffff;
4377 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4378 {
4379 maxnum <<= 16;
4380 maxnum |= 0xffff;
4381 maxnum <<= 16;
4382 maxnum |= 0xffff;
4383 }
4384 if (imm_expr.X_op == O_constant
4385 && imm_expr.X_add_number >= maxnum
4386 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4387 {
4388 do_false:
4389 /* result is always false */
4390 if (! likely)
4391 {
4392 if (warn_nops)
4393 as_warn (_("Branch %s is always false (nop)"),
4394 ip->insn_mo->name);
4395 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4396 "", 0);
4397 }
4398 else
4399 {
4400 if (warn_nops)
4401 as_warn (_("Branch likely %s is always false"),
4402 ip->insn_mo->name);
4403 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4404 "s,t,p", 0, 0);
4405 }
4406 return;
4407 }
4408 if (imm_expr.X_op != O_constant)
4409 as_bad (_("Unsupported large constant"));
4410 ++imm_expr.X_add_number;
4411 /* FALLTHROUGH */
4412 case M_BGE_I:
4413 case M_BGEL_I:
4414 if (mask == M_BGEL_I)
4415 likely = 1;
4416 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4417 {
4418 macro_build ((char *) NULL, &icnt, &offset_expr,
4419 likely ? "bgezl" : "bgez", "s,p", sreg);
4420 return;
4421 }
4422 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4423 {
4424 macro_build ((char *) NULL, &icnt, &offset_expr,
4425 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4426 return;
4427 }
4428 maxnum = 0x7fffffff;
4429 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4430 {
4431 maxnum <<= 16;
4432 maxnum |= 0xffff;
4433 maxnum <<= 16;
4434 maxnum |= 0xffff;
4435 }
4436 maxnum = - maxnum - 1;
4437 if (imm_expr.X_op == O_constant
4438 && imm_expr.X_add_number <= maxnum
4439 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4440 {
4441 do_true:
4442 /* result is always true */
4443 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4444 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4445 return;
4446 }
4447 set_at (&icnt, sreg, 0);
4448 macro_build ((char *) NULL, &icnt, &offset_expr,
4449 likely ? "beql" : "beq", "s,t,p", AT, 0);
4450 break;
4451
4452 case M_BGEUL:
4453 likely = 1;
4454 case M_BGEU:
4455 if (treg == 0)
4456 goto do_true;
4457 if (sreg == 0)
4458 {
4459 macro_build ((char *) NULL, &icnt, &offset_expr,
4460 likely ? "beql" : "beq", "s,t,p", 0, treg);
4461 return;
4462 }
4463 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4464 "d,v,t", AT, sreg, treg);
4465 macro_build ((char *) NULL, &icnt, &offset_expr,
4466 likely ? "beql" : "beq", "s,t,p", AT, 0);
4467 break;
4468
4469 case M_BGTUL_I:
4470 likely = 1;
4471 case M_BGTU_I:
4472 if (sreg == 0
4473 || (HAVE_32BIT_GPRS
4474 && imm_expr.X_op == O_constant
4475 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4476 goto do_false;
4477 if (imm_expr.X_op != O_constant)
4478 as_bad (_("Unsupported large constant"));
4479 ++imm_expr.X_add_number;
4480 /* FALLTHROUGH */
4481 case M_BGEU_I:
4482 case M_BGEUL_I:
4483 if (mask == M_BGEUL_I)
4484 likely = 1;
4485 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4486 goto do_true;
4487 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4488 {
4489 macro_build ((char *) NULL, &icnt, &offset_expr,
4490 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4491 return;
4492 }
4493 set_at (&icnt, sreg, 1);
4494 macro_build ((char *) NULL, &icnt, &offset_expr,
4495 likely ? "beql" : "beq", "s,t,p", AT, 0);
4496 break;
4497
4498 case M_BGTL:
4499 likely = 1;
4500 case M_BGT:
4501 if (treg == 0)
4502 {
4503 macro_build ((char *) NULL, &icnt, &offset_expr,
4504 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4505 return;
4506 }
4507 if (sreg == 0)
4508 {
4509 macro_build ((char *) NULL, &icnt, &offset_expr,
4510 likely ? "bltzl" : "bltz", "s,p", treg);
4511 return;
4512 }
4513 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4514 AT, treg, sreg);
4515 macro_build ((char *) NULL, &icnt, &offset_expr,
4516 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4517 break;
4518
4519 case M_BGTUL:
4520 likely = 1;
4521 case M_BGTU:
4522 if (treg == 0)
4523 {
4524 macro_build ((char *) NULL, &icnt, &offset_expr,
4525 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4526 return;
4527 }
4528 if (sreg == 0)
4529 goto do_false;
4530 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4531 "d,v,t", AT, treg, sreg);
4532 macro_build ((char *) NULL, &icnt, &offset_expr,
4533 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4534 break;
4535
4536 case M_BLEL:
4537 likely = 1;
4538 case M_BLE:
4539 if (treg == 0)
4540 {
4541 macro_build ((char *) NULL, &icnt, &offset_expr,
4542 likely ? "blezl" : "blez", "s,p", sreg);
4543 return;
4544 }
4545 if (sreg == 0)
4546 {
4547 macro_build ((char *) NULL, &icnt, &offset_expr,
4548 likely ? "bgezl" : "bgez", "s,p", treg);
4549 return;
4550 }
4551 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4552 AT, treg, sreg);
4553 macro_build ((char *) NULL, &icnt, &offset_expr,
4554 likely ? "beql" : "beq", "s,t,p", AT, 0);
4555 break;
4556
4557 case M_BLEL_I:
4558 likely = 1;
4559 case M_BLE_I:
4560 maxnum = 0x7fffffff;
4561 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4562 {
4563 maxnum <<= 16;
4564 maxnum |= 0xffff;
4565 maxnum <<= 16;
4566 maxnum |= 0xffff;
4567 }
4568 if (imm_expr.X_op == O_constant
4569 && imm_expr.X_add_number >= maxnum
4570 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4571 goto do_true;
4572 if (imm_expr.X_op != O_constant)
4573 as_bad (_("Unsupported large constant"));
4574 ++imm_expr.X_add_number;
4575 /* FALLTHROUGH */
4576 case M_BLT_I:
4577 case M_BLTL_I:
4578 if (mask == M_BLTL_I)
4579 likely = 1;
4580 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4581 {
4582 macro_build ((char *) NULL, &icnt, &offset_expr,
4583 likely ? "bltzl" : "bltz", "s,p", sreg);
4584 return;
4585 }
4586 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4587 {
4588 macro_build ((char *) NULL, &icnt, &offset_expr,
4589 likely ? "blezl" : "blez", "s,p", sreg);
4590 return;
4591 }
4592 set_at (&icnt, sreg, 0);
4593 macro_build ((char *) NULL, &icnt, &offset_expr,
4594 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4595 break;
4596
4597 case M_BLEUL:
4598 likely = 1;
4599 case M_BLEU:
4600 if (treg == 0)
4601 {
4602 macro_build ((char *) NULL, &icnt, &offset_expr,
4603 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4604 return;
4605 }
4606 if (sreg == 0)
4607 goto do_true;
4608 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4609 "d,v,t", AT, treg, sreg);
4610 macro_build ((char *) NULL, &icnt, &offset_expr,
4611 likely ? "beql" : "beq", "s,t,p", AT, 0);
4612 break;
4613
4614 case M_BLEUL_I:
4615 likely = 1;
4616 case M_BLEU_I:
4617 if (sreg == 0
4618 || (HAVE_32BIT_GPRS
4619 && imm_expr.X_op == O_constant
4620 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4621 goto do_true;
4622 if (imm_expr.X_op != O_constant)
4623 as_bad (_("Unsupported large constant"));
4624 ++imm_expr.X_add_number;
4625 /* FALLTHROUGH */
4626 case M_BLTU_I:
4627 case M_BLTUL_I:
4628 if (mask == M_BLTUL_I)
4629 likely = 1;
4630 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4631 goto do_false;
4632 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4633 {
4634 macro_build ((char *) NULL, &icnt, &offset_expr,
4635 likely ? "beql" : "beq",
4636 "s,t,p", sreg, 0);
4637 return;
4638 }
4639 set_at (&icnt, sreg, 1);
4640 macro_build ((char *) NULL, &icnt, &offset_expr,
4641 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4642 break;
4643
4644 case M_BLTL:
4645 likely = 1;
4646 case M_BLT:
4647 if (treg == 0)
4648 {
4649 macro_build ((char *) NULL, &icnt, &offset_expr,
4650 likely ? "bltzl" : "bltz", "s,p", sreg);
4651 return;
4652 }
4653 if (sreg == 0)
4654 {
4655 macro_build ((char *) NULL, &icnt, &offset_expr,
4656 likely ? "bgtzl" : "bgtz", "s,p", treg);
4657 return;
4658 }
4659 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4660 AT, sreg, treg);
4661 macro_build ((char *) NULL, &icnt, &offset_expr,
4662 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4663 break;
4664
4665 case M_BLTUL:
4666 likely = 1;
4667 case M_BLTU:
4668 if (treg == 0)
4669 goto do_false;
4670 if (sreg == 0)
4671 {
4672 macro_build ((char *) NULL, &icnt, &offset_expr,
4673 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4674 return;
4675 }
4676 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4677 "d,v,t", AT, sreg,
4678 treg);
4679 macro_build ((char *) NULL, &icnt, &offset_expr,
4680 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4681 break;
4682
4683 case M_DDIV_3:
4684 dbl = 1;
4685 case M_DIV_3:
4686 s = "mflo";
4687 goto do_div3;
4688 case M_DREM_3:
4689 dbl = 1;
4690 case M_REM_3:
4691 s = "mfhi";
4692 do_div3:
4693 if (treg == 0)
4694 {
4695 as_warn (_("Divide by zero."));
4696 if (mips_trap)
4697 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4698 "s,t,q", 0, 0, 7);
4699 else
4700 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4701 "c", 7);
4702 return;
4703 }
4704
4705 mips_emit_delays (TRUE);
4706 ++mips_opts.noreorder;
4707 mips_any_noreorder = 1;
4708 if (mips_trap)
4709 {
4710 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4711 "s,t,q", treg, 0, 7);
4712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4713 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4714 }
4715 else
4716 {
4717 expr1.X_add_number = 8;
4718 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4719 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4720 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4721 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4722 "c", 7);
4723 }
4724 expr1.X_add_number = -1;
4725 macro_build ((char *) NULL, &icnt, &expr1,
4726 dbl ? "daddiu" : "addiu",
4727 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4728 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4729 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4730 if (dbl)
4731 {
4732 expr1.X_add_number = 1;
4733 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4734 (int) BFD_RELOC_LO16);
4735 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4736 "d,w,<", AT, AT, 31);
4737 }
4738 else
4739 {
4740 expr1.X_add_number = 0x80000000;
4741 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4742 (int) BFD_RELOC_HI16);
4743 }
4744 if (mips_trap)
4745 {
4746 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4747 "s,t,q", sreg, AT, 6);
4748 /* We want to close the noreorder block as soon as possible, so
4749 that later insns are available for delay slot filling. */
4750 --mips_opts.noreorder;
4751 }
4752 else
4753 {
4754 expr1.X_add_number = 8;
4755 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4756 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4757 0);
4758
4759 /* We want to close the noreorder block as soon as possible, so
4760 that later insns are available for delay slot filling. */
4761 --mips_opts.noreorder;
4762
4763 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4764 "c", 6);
4765 }
4766 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4767 break;
4768
4769 case M_DIV_3I:
4770 s = "div";
4771 s2 = "mflo";
4772 goto do_divi;
4773 case M_DIVU_3I:
4774 s = "divu";
4775 s2 = "mflo";
4776 goto do_divi;
4777 case M_REM_3I:
4778 s = "div";
4779 s2 = "mfhi";
4780 goto do_divi;
4781 case M_REMU_3I:
4782 s = "divu";
4783 s2 = "mfhi";
4784 goto do_divi;
4785 case M_DDIV_3I:
4786 dbl = 1;
4787 s = "ddiv";
4788 s2 = "mflo";
4789 goto do_divi;
4790 case M_DDIVU_3I:
4791 dbl = 1;
4792 s = "ddivu";
4793 s2 = "mflo";
4794 goto do_divi;
4795 case M_DREM_3I:
4796 dbl = 1;
4797 s = "ddiv";
4798 s2 = "mfhi";
4799 goto do_divi;
4800 case M_DREMU_3I:
4801 dbl = 1;
4802 s = "ddivu";
4803 s2 = "mfhi";
4804 do_divi:
4805 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4806 {
4807 as_warn (_("Divide by zero."));
4808 if (mips_trap)
4809 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4810 "s,t,q", 0, 0, 7);
4811 else
4812 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4813 "c", 7);
4814 return;
4815 }
4816 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4817 {
4818 if (strcmp (s2, "mflo") == 0)
4819 move_register (&icnt, dreg, sreg);
4820 else
4821 move_register (&icnt, dreg, 0);
4822 return;
4823 }
4824 if (imm_expr.X_op == O_constant
4825 && imm_expr.X_add_number == -1
4826 && s[strlen (s) - 1] != 'u')
4827 {
4828 if (strcmp (s2, "mflo") == 0)
4829 {
4830 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4831 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4832 }
4833 else
4834 move_register (&icnt, dreg, 0);
4835 return;
4836 }
4837
4838 load_register (&icnt, AT, &imm_expr, dbl);
4839 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4840 sreg, AT);
4841 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4842 break;
4843
4844 case M_DIVU_3:
4845 s = "divu";
4846 s2 = "mflo";
4847 goto do_divu3;
4848 case M_REMU_3:
4849 s = "divu";
4850 s2 = "mfhi";
4851 goto do_divu3;
4852 case M_DDIVU_3:
4853 s = "ddivu";
4854 s2 = "mflo";
4855 goto do_divu3;
4856 case M_DREMU_3:
4857 s = "ddivu";
4858 s2 = "mfhi";
4859 do_divu3:
4860 mips_emit_delays (TRUE);
4861 ++mips_opts.noreorder;
4862 mips_any_noreorder = 1;
4863 if (mips_trap)
4864 {
4865 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4866 "s,t,q", treg, 0, 7);
4867 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4868 sreg, treg);
4869 /* We want to close the noreorder block as soon as possible, so
4870 that later insns are available for delay slot filling. */
4871 --mips_opts.noreorder;
4872 }
4873 else
4874 {
4875 expr1.X_add_number = 8;
4876 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4877 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4878 sreg, treg);
4879
4880 /* We want to close the noreorder block as soon as possible, so
4881 that later insns are available for delay slot filling. */
4882 --mips_opts.noreorder;
4883 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4884 "c", 7);
4885 }
4886 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4887 return;
4888
4889 case M_DLA_AB:
4890 dbl = 1;
4891 case M_LA_AB:
4892 /* Load the address of a symbol into a register. If breg is not
4893 zero, we then add a base register to it. */
4894
4895 if (dbl && HAVE_32BIT_GPRS)
4896 as_warn (_("dla used to load 32-bit register"));
4897
4898 if (! dbl && HAVE_64BIT_OBJECTS)
4899 as_warn (_("la used to load 64-bit address"));
4900
4901 if (offset_expr.X_op == O_constant
4902 && offset_expr.X_add_number >= -0x8000
4903 && offset_expr.X_add_number < 0x8000)
4904 {
4905 macro_build ((char *) NULL, &icnt, &offset_expr,
4906 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" :
4907 HAVE_NEWABI ? "addi" : "addiu",
4908 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4909 return;
4910 }
4911
4912 if (treg == breg)
4913 {
4914 tempreg = AT;
4915 used_at = 1;
4916 }
4917 else
4918 {
4919 tempreg = treg;
4920 used_at = 0;
4921 }
4922
4923 /* When generating embedded PIC code, we permit expressions of
4924 the form
4925 la $treg,foo-bar
4926 la $treg,foo-bar($breg)
4927 where bar is an address in the current section. These are used
4928 when getting the addresses of functions. We don't permit
4929 X_add_number to be non-zero, because if the symbol is
4930 external the relaxing code needs to know that any addend is
4931 purely the offset to X_op_symbol. */
4932 if (mips_pic == EMBEDDED_PIC
4933 && offset_expr.X_op == O_subtract
4934 && (symbol_constant_p (offset_expr.X_op_symbol)
4935 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4936 : (symbol_equated_p (offset_expr.X_op_symbol)
4937 && (S_GET_SEGMENT
4938 (symbol_get_value_expression (offset_expr.X_op_symbol)
4939 ->X_add_symbol)
4940 == now_seg)))
4941 && (offset_expr.X_add_number == 0
4942 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4943 {
4944 if (breg == 0)
4945 {
4946 tempreg = treg;
4947 used_at = 0;
4948 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4949 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4950 }
4951 else
4952 {
4953 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4954 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4955 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4956 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4957 "d,v,t", tempreg, tempreg, breg);
4958 }
4959 macro_build ((char *) NULL, &icnt, &offset_expr,
4960 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4961 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4962 if (! used_at)
4963 return;
4964 break;
4965 }
4966
4967 if (offset_expr.X_op != O_symbol
4968 && offset_expr.X_op != O_constant)
4969 {
4970 as_bad (_("expression too complex"));
4971 offset_expr.X_op = O_constant;
4972 }
4973
4974 if (offset_expr.X_op == O_constant)
4975 load_register (&icnt, tempreg, &offset_expr,
4976 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4977 ? (dbl || HAVE_64BIT_ADDRESSES)
4978 : HAVE_64BIT_ADDRESSES));
4979 else if (mips_pic == NO_PIC)
4980 {
4981 /* If this is a reference to a GP relative symbol, we want
4982 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4983 Otherwise we want
4984 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4985 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4986 If we have a constant, we need two instructions anyhow,
4987 so we may as well always use the latter form.
4988
4989 With 64bit address space and a usable $at we want
4990 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4991 lui $at,<sym> (BFD_RELOC_HI16_S)
4992 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4993 daddiu $at,<sym> (BFD_RELOC_LO16)
4994 dsll32 $tempreg,0
4995 daddu $tempreg,$tempreg,$at
4996
4997 If $at is already in use, we use a path which is suboptimal
4998 on superscalar processors.
4999 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5000 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5001 dsll $tempreg,16
5002 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5003 dsll $tempreg,16
5004 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5005 */
5006 char *p = NULL;
5007 if (HAVE_64BIT_ADDRESSES)
5008 {
5009 /* We don't do GP optimization for now because RELAX_ENCODE can't
5010 hold the data for such large chunks. */
5011
5012 if (used_at == 0 && ! mips_opts.noat)
5013 {
5014 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5015 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5016 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5017 AT, (int) BFD_RELOC_HI16_S);
5018 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5019 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5020 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5021 AT, AT, (int) BFD_RELOC_LO16);
5022 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5023 "d,w,<", tempreg, tempreg, 0);
5024 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5025 "d,v,t", tempreg, tempreg, AT);
5026 used_at = 1;
5027 }
5028 else
5029 {
5030 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5031 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5032 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5033 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5034 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5035 tempreg, tempreg, 16);
5036 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5037 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5038 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5039 tempreg, tempreg, 16);
5040 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5041 tempreg, tempreg, (int) BFD_RELOC_LO16);
5042 }
5043 }
5044 else
5045 {
5046 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5047 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5048 {
5049 frag_grow (20);
5050 macro_build ((char *) NULL, &icnt, &offset_expr,
5051 HAVE_NEWABI ? "addi" : "addiu",
5052 "t,r,j", tempreg, mips_gp_register,
5053 (int) BFD_RELOC_GPREL16);
5054 p = frag_var (rs_machine_dependent, 8, 0,
5055 RELAX_ENCODE (4, 8, 0, 4, 0,
5056 mips_opts.warn_about_macros),
5057 offset_expr.X_add_symbol, 0, NULL);
5058 }
5059 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5060 if (p != NULL)
5061 p += 4;
5062 macro_build (p, &icnt, &offset_expr,
5063 HAVE_NEWABI ? "addi" : "addiu",
5064 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5065 }
5066 }
5067 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
5068 {
5069 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5070
5071 /* If this is a reference to an external symbol, and there
5072 is no constant, we want
5073 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5074 or if tempreg is PIC_CALL_REG
5075 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5076 For a local symbol, we want
5077 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5078 nop
5079 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5080
5081 If we have a small constant, and this is a reference to
5082 an external symbol, we want
5083 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5084 nop
5085 addiu $tempreg,$tempreg,<constant>
5086 For a local symbol, we want the same instruction
5087 sequence, but we output a BFD_RELOC_LO16 reloc on the
5088 addiu instruction.
5089
5090 If we have a large constant, and this is a reference to
5091 an external symbol, we want
5092 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5093 lui $at,<hiconstant>
5094 addiu $at,$at,<loconstant>
5095 addu $tempreg,$tempreg,$at
5096 For a local symbol, we want the same instruction
5097 sequence, but we output a BFD_RELOC_LO16 reloc on the
5098 addiu instruction.
5099 */
5100
5101 expr1.X_add_number = offset_expr.X_add_number;
5102 offset_expr.X_add_number = 0;
5103 frag_grow (32);
5104 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5105 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5106 macro_build ((char *) NULL, &icnt, &offset_expr,
5107 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5108 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
5109 if (expr1.X_add_number == 0)
5110 {
5111 int off;
5112 char *p;
5113
5114 if (breg == 0)
5115 off = 0;
5116 else
5117 {
5118 /* We're going to put in an addu instruction using
5119 tempreg, so we may as well insert the nop right
5120 now. */
5121 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5122 "nop", "");
5123 off = 4;
5124 }
5125 p = frag_var (rs_machine_dependent, 8 - off, 0,
5126 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5127 (breg == 0
5128 ? mips_opts.warn_about_macros
5129 : 0)),
5130 offset_expr.X_add_symbol, 0, NULL);
5131 if (breg == 0)
5132 {
5133 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5134 p += 4;
5135 }
5136 macro_build (p, &icnt, &expr1,
5137 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5138 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5139 /* FIXME: If breg == 0, and the next instruction uses
5140 $tempreg, then if this variant case is used an extra
5141 nop will be generated. */
5142 }
5143 else if (expr1.X_add_number >= -0x8000
5144 && expr1.X_add_number < 0x8000)
5145 {
5146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147 "nop", "");
5148 macro_build ((char *) NULL, &icnt, &expr1,
5149 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5150 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5151 frag_var (rs_machine_dependent, 0, 0,
5152 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5153 offset_expr.X_add_symbol, 0, NULL);
5154 }
5155 else
5156 {
5157 int off1;
5158
5159 /* If we are going to add in a base register, and the
5160 target register and the base register are the same,
5161 then we are using AT as a temporary register. Since
5162 we want to load the constant into AT, we add our
5163 current AT (from the global offset table) and the
5164 register into the register now, and pretend we were
5165 not using a base register. */
5166 if (breg != treg)
5167 off1 = 0;
5168 else
5169 {
5170 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5171 "nop", "");
5172 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5173 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5174 "d,v,t", treg, AT, breg);
5175 breg = 0;
5176 tempreg = treg;
5177 off1 = -8;
5178 }
5179
5180 /* Set mips_optimize around the lui instruction to avoid
5181 inserting an unnecessary nop after the lw. */
5182 hold_mips_optimize = mips_optimize;
5183 mips_optimize = 2;
5184 macro_build_lui (NULL, &icnt, &expr1, AT);
5185 mips_optimize = hold_mips_optimize;
5186
5187 macro_build ((char *) NULL, &icnt, &expr1,
5188 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5189 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5190 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5191 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5192 "d,v,t", tempreg, tempreg, AT);
5193 frag_var (rs_machine_dependent, 0, 0,
5194 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5195 offset_expr.X_add_symbol, 0, NULL);
5196 used_at = 1;
5197 }
5198 }
5199 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5200 {
5201 char *p = NULL;
5202 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5203 int adj = 0;
5204
5205 /* If this is a reference to an external, and there is no
5206 constant, or local symbol (*), with or without a
5207 constant, we want
5208 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5209 or if tempreg is PIC_CALL_REG
5210 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5211
5212 If we have a small constant, and this is a reference to
5213 an external symbol, we want
5214 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5215 addiu $tempreg,$tempreg,<constant>
5216
5217 If we have a large constant, and this is a reference to
5218 an external symbol, we want
5219 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5220 lui $at,<hiconstant>
5221 addiu $at,$at,<loconstant>
5222 addu $tempreg,$tempreg,$at
5223
5224 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5225 local symbols, even though it introduces an additional
5226 instruction. */
5227
5228 frag_grow (28);
5229 if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5230 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5231 if (offset_expr.X_add_number)
5232 {
5233 frag_now->tc_frag_data.tc_fr_offset =
5234 expr1.X_add_number = offset_expr.X_add_number;
5235 offset_expr.X_add_number = 0;
5236
5237 macro_build ((char *) NULL, &icnt, &offset_expr,
5238 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5239 "t,o(b)", tempreg, lw_reloc_type,
5240 mips_gp_register);
5241
5242 if (expr1.X_add_number >= -0x8000
5243 && expr1.X_add_number < 0x8000)
5244 {
5245 macro_build ((char *) NULL, &icnt, &expr1,
5246 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5247 "t,r,j", tempreg, tempreg,
5248 (int) BFD_RELOC_LO16);
5249 p = frag_var (rs_machine_dependent, 4, 0,
5250 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5251 offset_expr.X_add_symbol, 0, NULL);
5252 }
5253 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5254 {
5255 int dreg;
5256
5257 /* If we are going to add in a base register, and the
5258 target register and the base register are the same,
5259 then we are using AT as a temporary register. Since
5260 we want to load the constant into AT, we add our
5261 current AT (from the global offset table) and the
5262 register into the register now, and pretend we were
5263 not using a base register. */
5264 if (breg != treg)
5265 dreg = tempreg;
5266 else
5267 {
5268 assert (tempreg == AT);
5269 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5270 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5271 "d,v,t", treg, AT, breg);
5272 dreg = treg;
5273 adj = 4;
5274 }
5275
5276 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5277 macro_build ((char *) NULL, &icnt, &expr1,
5278 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5279 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5280 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5281 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5282 "d,v,t", dreg, dreg, AT);
5283
5284 p = frag_var (rs_machine_dependent, 4 + adj, 0,
5285 RELAX_ENCODE (16 + adj, 4 + adj,
5286 0, 0, 0, 0),
5287 offset_expr.X_add_symbol, 0, NULL);
5288
5289 used_at = 1;
5290 }
5291 else
5292 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5293
5294 offset_expr.X_add_number = expr1.X_add_number;
5295
5296 macro_build (p, &icnt, &offset_expr,
5297 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5298 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_DISP,
5299 mips_gp_register);
5300 if (adj)
5301 {
5302 macro_build (p + 4, &icnt, (expressionS *) NULL,
5303 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5304 "d,v,t", treg, tempreg, breg);
5305 breg = 0;
5306 tempreg = treg;
5307 }
5308 }
5309 else
5310 {
5311 macro_build ((char *) NULL, &icnt, &offset_expr,
5312 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5313 "t,o(b)", tempreg, lw_reloc_type,
5314 mips_gp_register);
5315 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5316 p = frag_var (rs_machine_dependent, 0, 0,
5317 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5318 offset_expr.X_add_symbol, 0, NULL);
5319 }
5320
5321 if (! p)
5322 {
5323 /* To avoid confusion in tc_gen_reloc, we must ensure
5324 that this does not become a variant frag. */
5325 frag_wane (frag_now);
5326 frag_new (0);
5327 }
5328 }
5329 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
5330 {
5331 int gpdel;
5332 char *p;
5333 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5334 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5335 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5336
5337 /* This is the large GOT case. If this is a reference to an
5338 external symbol, and there is no constant, we want
5339 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5340 addu $tempreg,$tempreg,$gp
5341 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5342 or if tempreg is PIC_CALL_REG
5343 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5344 addu $tempreg,$tempreg,$gp
5345 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5346 For a local symbol, we want
5347 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5348 nop
5349 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5350
5351 If we have a small constant, and this is a reference to
5352 an external symbol, we want
5353 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5354 addu $tempreg,$tempreg,$gp
5355 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5356 nop
5357 addiu $tempreg,$tempreg,<constant>
5358 For a local symbol, we want
5359 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5360 nop
5361 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5362
5363 If we have a large constant, and this is a reference to
5364 an external symbol, we want
5365 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5366 addu $tempreg,$tempreg,$gp
5367 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5368 lui $at,<hiconstant>
5369 addiu $at,$at,<loconstant>
5370 addu $tempreg,$tempreg,$at
5371 For a local symbol, we want
5372 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5373 lui $at,<hiconstant>
5374 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5375 addu $tempreg,$tempreg,$at
5376 */
5377
5378 expr1.X_add_number = offset_expr.X_add_number;
5379 offset_expr.X_add_number = 0;
5380 frag_grow (52);
5381 if (reg_needs_delay (mips_gp_register))
5382 gpdel = 4;
5383 else
5384 gpdel = 0;
5385 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5386 {
5387 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5388 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5389 }
5390 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5391 tempreg, lui_reloc_type);
5392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5393 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5394 "d,v,t", tempreg, tempreg, mips_gp_register);
5395 macro_build ((char *) NULL, &icnt, &offset_expr,
5396 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5397 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5398 if (expr1.X_add_number == 0)
5399 {
5400 int off;
5401
5402 if (breg == 0)
5403 off = 0;
5404 else
5405 {
5406 /* We're going to put in an addu instruction using
5407 tempreg, so we may as well insert the nop right
5408 now. */
5409 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5410 "nop", "");
5411 off = 4;
5412 }
5413
5414 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5415 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5416 8 + gpdel, 0,
5417 (breg == 0
5418 ? mips_opts.warn_about_macros
5419 : 0)),
5420 offset_expr.X_add_symbol, 0, NULL);
5421 }
5422 else if (expr1.X_add_number >= -0x8000
5423 && expr1.X_add_number < 0x8000)
5424 {
5425 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5426 "nop", "");
5427 macro_build ((char *) NULL, &icnt, &expr1,
5428 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5429 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5430
5431 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5432 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5433 (breg == 0
5434 ? mips_opts.warn_about_macros
5435 : 0)),
5436 offset_expr.X_add_symbol, 0, NULL);
5437 }
5438 else
5439 {
5440 int adj, dreg;
5441
5442 /* If we are going to add in a base register, and the
5443 target register and the base register are the same,
5444 then we are using AT as a temporary register. Since
5445 we want to load the constant into AT, we add our
5446 current AT (from the global offset table) and the
5447 register into the register now, and pretend we were
5448 not using a base register. */
5449 if (breg != treg)
5450 {
5451 adj = 0;
5452 dreg = tempreg;
5453 }
5454 else
5455 {
5456 assert (tempreg == AT);
5457 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5458 "nop", "");
5459 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5460 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5461 "d,v,t", treg, AT, breg);
5462 dreg = treg;
5463 adj = 8;
5464 }
5465
5466 /* Set mips_optimize around the lui instruction to avoid
5467 inserting an unnecessary nop after the lw. */
5468 hold_mips_optimize = mips_optimize;
5469 mips_optimize = 2;
5470 macro_build_lui (NULL, &icnt, &expr1, AT);
5471 mips_optimize = hold_mips_optimize;
5472
5473 macro_build ((char *) NULL, &icnt, &expr1,
5474 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5475 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5476 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5477 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5478 "d,v,t", dreg, dreg, AT);
5479
5480 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5481 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5482 8 + gpdel, 0,
5483 (breg == 0
5484 ? mips_opts.warn_about_macros
5485 : 0)),
5486 offset_expr.X_add_symbol, 0, NULL);
5487
5488 used_at = 1;
5489 }
5490
5491 if (gpdel > 0)
5492 {
5493 /* This is needed because this instruction uses $gp, but
5494 the first instruction on the main stream does not. */
5495 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5496 p += 4;
5497 }
5498
5499 macro_build (p, &icnt, &offset_expr,
5500 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5501 "t,o(b)", tempreg,
5502 local_reloc_type,
5503 mips_gp_register);
5504 p += 4;
5505 if (expr1.X_add_number >= -0x8000
5506 && expr1.X_add_number < 0x8000)
5507 {
5508 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5509 p += 4;
5510 macro_build (p, &icnt, &expr1,
5511 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5512 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5513 /* FIXME: If add_number is 0, and there was no base
5514 register, the external symbol case ended with a load,
5515 so if the symbol turns out to not be external, and
5516 the next instruction uses tempreg, an unnecessary nop
5517 will be inserted. */
5518 }
5519 else
5520 {
5521 if (breg == treg)
5522 {
5523 /* We must add in the base register now, as in the
5524 external symbol case. */
5525 assert (tempreg == AT);
5526 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5527 p += 4;
5528 macro_build (p, &icnt, (expressionS *) NULL,
5529 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5530 "d,v,t", treg, AT, breg);
5531 p += 4;
5532 tempreg = treg;
5533 /* We set breg to 0 because we have arranged to add
5534 it in in both cases. */
5535 breg = 0;
5536 }
5537
5538 macro_build_lui (p, &icnt, &expr1, AT);
5539 p += 4;
5540 macro_build (p, &icnt, &expr1,
5541 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5542 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5543 p += 4;
5544 macro_build (p, &icnt, (expressionS *) NULL,
5545 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5546 "d,v,t", tempreg, tempreg, AT);
5547 p += 4;
5548 }
5549 }
5550 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5551 {
5552 char *p = NULL;
5553 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5554 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5555 int adj = 0;
5556
5557 /* This is the large GOT case. If this is a reference to an
5558 external symbol, and there is no constant, we want
5559 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5560 add $tempreg,$tempreg,$gp
5561 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5562 or if tempreg is PIC_CALL_REG
5563 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5564 add $tempreg,$tempreg,$gp
5565 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5566
5567 If we have a small constant, and this is a reference to
5568 an external symbol, we want
5569 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5570 add $tempreg,$tempreg,$gp
5571 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5572 addi $tempreg,$tempreg,<constant>
5573
5574 If we have a large constant, and this is a reference to
5575 an external symbol, we want
5576 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5577 addu $tempreg,$tempreg,$gp
5578 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5579 lui $at,<hiconstant>
5580 addi $at,$at,<loconstant>
5581 add $tempreg,$tempreg,$at
5582
5583 If we have NewABI, and we know it's a local symbol, we want
5584 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5585 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5586 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5587
5588 frag_grow (40);
5589
5590 frag_now->tc_frag_data.tc_fr_offset =
5591 expr1.X_add_number = offset_expr.X_add_number;
5592 offset_expr.X_add_number = 0;
5593
5594 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5595 {
5596 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5597 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5598 }
5599 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5600 tempreg, lui_reloc_type);
5601 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5602 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5603 "d,v,t", tempreg, tempreg, mips_gp_register);
5604 macro_build ((char *) NULL, &icnt, &offset_expr,
5605 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5606 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5607
5608 if (expr1.X_add_number == 0)
5609 {
5610 p = frag_var (rs_machine_dependent, 8, 0,
5611 RELAX_ENCODE (12, 8, 0, 4, 0,
5612 mips_opts.warn_about_macros),
5613 offset_expr.X_add_symbol, 0, NULL);
5614 }
5615 else if (expr1.X_add_number >= -0x8000
5616 && expr1.X_add_number < 0x8000)
5617 {
5618 macro_build ((char *) NULL, &icnt, &expr1,
5619 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5620 "t,r,j", tempreg, tempreg,
5621 (int) BFD_RELOC_LO16);
5622 p = frag_var (rs_machine_dependent, 8, 0,
5623 RELAX_ENCODE (16, 8, 0, 4, 0,
5624 mips_opts.warn_about_macros),
5625 offset_expr.X_add_symbol, 0, NULL);
5626 }
5627 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5628 {
5629 int dreg;
5630
5631 /* If we are going to add in a base register, and the
5632 target register and the base register are the same,
5633 then we are using AT as a temporary register. Since
5634 we want to load the constant into AT, we add our
5635 current AT (from the global offset table) and the
5636 register into the register now, and pretend we were
5637 not using a base register. */
5638 if (breg != treg)
5639 dreg = tempreg;
5640 else
5641 {
5642 assert (tempreg == AT);
5643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5644 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5645 "d,v,t", treg, AT, breg);
5646 dreg = treg;
5647 adj = 4;
5648 }
5649
5650 /* Set mips_optimize around the lui instruction to avoid
5651 inserting an unnecessary nop after the lw. */
5652 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5653 macro_build ((char *) NULL, &icnt, &expr1,
5654 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5655 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5657 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5658 "d,v,t", dreg, dreg, AT);
5659
5660 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5661 RELAX_ENCODE (24 + adj, 8 + adj,
5662 0, 4, 0,
5663 (breg == 0
5664 ? mips_opts.warn_about_macros
5665 : 0)),
5666 offset_expr.X_add_symbol, 0, NULL);
5667
5668 used_at = 1;
5669 }
5670 else
5671 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5672
5673 offset_expr.X_add_number = expr1.X_add_number;
5674 macro_build (p, &icnt, &offset_expr,
5675 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5676 tempreg,
5677 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5678 macro_build (p + 4, &icnt, &offset_expr,
5679 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
5680 tempreg, tempreg, (int) BFD_RELOC_MIPS_GOT_OFST);
5681 if (adj)
5682 {
5683 macro_build (p + 8, &icnt, (expressionS *) NULL,
5684 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5685 "d,v,t", treg, tempreg, breg);
5686 breg = 0;
5687 tempreg = treg;
5688 }
5689 }
5690 else if (mips_pic == EMBEDDED_PIC)
5691 {
5692 /* We use
5693 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5694 */
5695 macro_build ((char *) NULL, &icnt, &offset_expr,
5696 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5697 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
5698 }
5699 else
5700 abort ();
5701
5702 if (breg != 0)
5703 {
5704 char *s;
5705
5706 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5707 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" :
5708 HAVE_NEWABI ? "add" : "addu";
5709 else
5710 s = HAVE_64BIT_ADDRESSES ? "daddu" : HAVE_NEWABI ? "add" : "addu";
5711
5712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5713 "d,v,t", treg, tempreg, breg);
5714 }
5715
5716 if (! used_at)
5717 return;
5718
5719 break;
5720
5721 case M_J_A:
5722 /* The j instruction may not be used in PIC code, since it
5723 requires an absolute address. We convert it to a b
5724 instruction. */
5725 if (mips_pic == NO_PIC)
5726 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5727 else
5728 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5729 return;
5730
5731 /* The jal instructions must be handled as macros because when
5732 generating PIC code they expand to multi-instruction
5733 sequences. Normally they are simple instructions. */
5734 case M_JAL_1:
5735 dreg = RA;
5736 /* Fall through. */
5737 case M_JAL_2:
5738 if (mips_pic == NO_PIC
5739 || mips_pic == EMBEDDED_PIC)
5740 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5741 "d,s", dreg, sreg);
5742 else if (mips_pic == SVR4_PIC)
5743 {
5744 if (sreg != PIC_CALL_REG)
5745 as_warn (_("MIPS PIC call to register other than $25"));
5746
5747 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5748 "d,s", dreg, sreg);
5749 if (! HAVE_NEWABI)
5750 {
5751 if (mips_cprestore_offset < 0)
5752 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5753 else
5754 {
5755 if (! mips_frame_reg_valid)
5756 {
5757 as_warn (_("No .frame pseudo-op used in PIC code"));
5758 /* Quiet this warning. */
5759 mips_frame_reg_valid = 1;
5760 }
5761 if (! mips_cprestore_valid)
5762 {
5763 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5764 /* Quiet this warning. */
5765 mips_cprestore_valid = 1;
5766 }
5767 expr1.X_add_number = mips_cprestore_offset;
5768 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5769 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5770 mips_gp_register, mips_frame_reg);
5771 }
5772 }
5773 }
5774 else
5775 abort ();
5776
5777 return;
5778
5779 case M_JAL_A:
5780 if (mips_pic == NO_PIC)
5781 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5782 else if (mips_pic == SVR4_PIC)
5783 {
5784 char *p;
5785
5786 /* If this is a reference to an external symbol, and we are
5787 using a small GOT, we want
5788 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5789 nop
5790 jalr $ra,$25
5791 nop
5792 lw $gp,cprestore($sp)
5793 The cprestore value is set using the .cprestore
5794 pseudo-op. If we are using a big GOT, we want
5795 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5796 addu $25,$25,$gp
5797 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5798 nop
5799 jalr $ra,$25
5800 nop
5801 lw $gp,cprestore($sp)
5802 If the symbol is not external, we want
5803 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5804 nop
5805 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5806 jalr $ra,$25
5807 nop
5808 lw $gp,cprestore($sp)
5809
5810 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5811 sequences above, minus nops, unless the symbol is local,
5812 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5813 GOT_DISP. */
5814 if (HAVE_NEWABI)
5815 {
5816 if (! mips_big_got)
5817 {
5818 frag_grow (4);
5819 macro_build ((char *) NULL, &icnt, &offset_expr,
5820 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5821 "t,o(b)", PIC_CALL_REG,
5822 (int) BFD_RELOC_MIPS_CALL16,
5823 mips_gp_register);
5824 frag_var (rs_machine_dependent, 0, 0,
5825 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5826 offset_expr.X_add_symbol, 0, NULL);
5827 }
5828 else
5829 {
5830 frag_grow (20);
5831 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5832 "t,u", PIC_CALL_REG,
5833 (int) BFD_RELOC_MIPS_CALL_HI16);
5834 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5835 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5836 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5837 mips_gp_register);
5838 macro_build ((char *) NULL, &icnt, &offset_expr,
5839 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5840 "t,o(b)", PIC_CALL_REG,
5841 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5842 p = frag_var (rs_machine_dependent, 8, 0,
5843 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5844 offset_expr.X_add_symbol, 0, NULL);
5845 macro_build (p, &icnt, &offset_expr,
5846 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5847 PIC_CALL_REG, (int) BFD_RELOC_MIPS_GOT_PAGE,
5848 mips_gp_register);
5849 macro_build (p + 4, &icnt, &offset_expr,
5850 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5851 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5852 (int) BFD_RELOC_MIPS_GOT_OFST);
5853 }
5854
5855 macro_build_jalr (icnt, &offset_expr);
5856 }
5857 else
5858 {
5859 frag_grow (40);
5860 if (! mips_big_got)
5861 {
5862 macro_build ((char *) NULL, &icnt, &offset_expr,
5863 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5864 "t,o(b)", PIC_CALL_REG,
5865 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5866 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5867 "nop", "");
5868 p = frag_var (rs_machine_dependent, 4, 0,
5869 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5870 offset_expr.X_add_symbol, 0, NULL);
5871 }
5872 else
5873 {
5874 int gpdel;
5875
5876 if (reg_needs_delay (mips_gp_register))
5877 gpdel = 4;
5878 else
5879 gpdel = 0;
5880 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5881 "t,u", PIC_CALL_REG,
5882 (int) BFD_RELOC_MIPS_CALL_HI16);
5883 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5884 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5885 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5886 mips_gp_register);
5887 macro_build ((char *) NULL, &icnt, &offset_expr,
5888 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5889 "t,o(b)", PIC_CALL_REG,
5890 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5892 "nop", "");
5893 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5894 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5895 8 + gpdel, 0, 0),
5896 offset_expr.X_add_symbol, 0, NULL);
5897 if (gpdel > 0)
5898 {
5899 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5900 p += 4;
5901 }
5902 macro_build (p, &icnt, &offset_expr,
5903 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5904 "t,o(b)", PIC_CALL_REG,
5905 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5906 p += 4;
5907 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5908 p += 4;
5909 }
5910 macro_build (p, &icnt, &offset_expr,
5911 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5912 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5913 (int) BFD_RELOC_LO16);
5914 macro_build_jalr (icnt, &offset_expr);
5915
5916 if (mips_cprestore_offset < 0)
5917 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5918 else
5919 {
5920 if (! mips_frame_reg_valid)
5921 {
5922 as_warn (_("No .frame pseudo-op used in PIC code"));
5923 /* Quiet this warning. */
5924 mips_frame_reg_valid = 1;
5925 }
5926 if (! mips_cprestore_valid)
5927 {
5928 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5929 /* Quiet this warning. */
5930 mips_cprestore_valid = 1;
5931 }
5932 if (mips_opts.noreorder)
5933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5934 "nop", "");
5935 expr1.X_add_number = mips_cprestore_offset;
5936 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5937 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5938 mips_gp_register, mips_frame_reg);
5939 }
5940 }
5941 }
5942 else if (mips_pic == EMBEDDED_PIC)
5943 {
5944 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5945 /* The linker may expand the call to a longer sequence which
5946 uses $at, so we must break rather than return. */
5947 break;
5948 }
5949 else
5950 abort ();
5951
5952 return;
5953
5954 case M_LB_AB:
5955 s = "lb";
5956 goto ld;
5957 case M_LBU_AB:
5958 s = "lbu";
5959 goto ld;
5960 case M_LH_AB:
5961 s = "lh";
5962 goto ld;
5963 case M_LHU_AB:
5964 s = "lhu";
5965 goto ld;
5966 case M_LW_AB:
5967 s = "lw";
5968 goto ld;
5969 case M_LWC0_AB:
5970 s = "lwc0";
5971 /* Itbl support may require additional care here. */
5972 coproc = 1;
5973 goto ld;
5974 case M_LWC1_AB:
5975 s = "lwc1";
5976 /* Itbl support may require additional care here. */
5977 coproc = 1;
5978 goto ld;
5979 case M_LWC2_AB:
5980 s = "lwc2";
5981 /* Itbl support may require additional care here. */
5982 coproc = 1;
5983 goto ld;
5984 case M_LWC3_AB:
5985 s = "lwc3";
5986 /* Itbl support may require additional care here. */
5987 coproc = 1;
5988 goto ld;
5989 case M_LWL_AB:
5990 s = "lwl";
5991 lr = 1;
5992 goto ld;
5993 case M_LWR_AB:
5994 s = "lwr";
5995 lr = 1;
5996 goto ld;
5997 case M_LDC1_AB:
5998 if (mips_arch == CPU_R4650)
5999 {
6000 as_bad (_("opcode not supported on this processor"));
6001 return;
6002 }
6003 s = "ldc1";
6004 /* Itbl support may require additional care here. */
6005 coproc = 1;
6006 goto ld;
6007 case M_LDC2_AB:
6008 s = "ldc2";
6009 /* Itbl support may require additional care here. */
6010 coproc = 1;
6011 goto ld;
6012 case M_LDC3_AB:
6013 s = "ldc3";
6014 /* Itbl support may require additional care here. */
6015 coproc = 1;
6016 goto ld;
6017 case M_LDL_AB:
6018 s = "ldl";
6019 lr = 1;
6020 goto ld;
6021 case M_LDR_AB:
6022 s = "ldr";
6023 lr = 1;
6024 goto ld;
6025 case M_LL_AB:
6026 s = "ll";
6027 goto ld;
6028 case M_LLD_AB:
6029 s = "lld";
6030 goto ld;
6031 case M_LWU_AB:
6032 s = "lwu";
6033 ld:
6034 if (breg == treg || coproc || lr)
6035 {
6036 tempreg = AT;
6037 used_at = 1;
6038 }
6039 else
6040 {
6041 tempreg = treg;
6042 used_at = 0;
6043 }
6044 goto ld_st;
6045 case M_SB_AB:
6046 s = "sb";
6047 goto st;
6048 case M_SH_AB:
6049 s = "sh";
6050 goto st;
6051 case M_SW_AB:
6052 s = "sw";
6053 goto st;
6054 case M_SWC0_AB:
6055 s = "swc0";
6056 /* Itbl support may require additional care here. */
6057 coproc = 1;
6058 goto st;
6059 case M_SWC1_AB:
6060 s = "swc1";
6061 /* Itbl support may require additional care here. */
6062 coproc = 1;
6063 goto st;
6064 case M_SWC2_AB:
6065 s = "swc2";
6066 /* Itbl support may require additional care here. */
6067 coproc = 1;
6068 goto st;
6069 case M_SWC3_AB:
6070 s = "swc3";
6071 /* Itbl support may require additional care here. */
6072 coproc = 1;
6073 goto st;
6074 case M_SWL_AB:
6075 s = "swl";
6076 goto st;
6077 case M_SWR_AB:
6078 s = "swr";
6079 goto st;
6080 case M_SC_AB:
6081 s = "sc";
6082 goto st;
6083 case M_SCD_AB:
6084 s = "scd";
6085 goto st;
6086 case M_SDC1_AB:
6087 if (mips_arch == CPU_R4650)
6088 {
6089 as_bad (_("opcode not supported on this processor"));
6090 return;
6091 }
6092 s = "sdc1";
6093 coproc = 1;
6094 /* Itbl support may require additional care here. */
6095 goto st;
6096 case M_SDC2_AB:
6097 s = "sdc2";
6098 /* Itbl support may require additional care here. */
6099 coproc = 1;
6100 goto st;
6101 case M_SDC3_AB:
6102 s = "sdc3";
6103 /* Itbl support may require additional care here. */
6104 coproc = 1;
6105 goto st;
6106 case M_SDL_AB:
6107 s = "sdl";
6108 goto st;
6109 case M_SDR_AB:
6110 s = "sdr";
6111 st:
6112 tempreg = AT;
6113 used_at = 1;
6114 ld_st:
6115 /* Itbl support may require additional care here. */
6116 if (mask == M_LWC1_AB
6117 || mask == M_SWC1_AB
6118 || mask == M_LDC1_AB
6119 || mask == M_SDC1_AB
6120 || mask == M_L_DAB
6121 || mask == M_S_DAB)
6122 fmt = "T,o(b)";
6123 else if (coproc)
6124 fmt = "E,o(b)";
6125 else
6126 fmt = "t,o(b)";
6127
6128 /* For embedded PIC, we allow loads where the offset is calculated
6129 by subtracting a symbol in the current segment from an unknown
6130 symbol, relative to a base register, e.g.:
6131 <op> $treg, <sym>-<localsym>($breg)
6132 This is used by the compiler for switch statements. */
6133 if (mips_pic == EMBEDDED_PIC
6134 && offset_expr.X_op == O_subtract
6135 && (symbol_constant_p (offset_expr.X_op_symbol)
6136 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
6137 : (symbol_equated_p (offset_expr.X_op_symbol)
6138 && (S_GET_SEGMENT
6139 (symbol_get_value_expression (offset_expr.X_op_symbol)
6140 ->X_add_symbol)
6141 == now_seg)))
6142 && breg != 0
6143 && (offset_expr.X_add_number == 0
6144 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
6145 {
6146 /* For this case, we output the instructions:
6147 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
6148 addiu $tempreg,$tempreg,$breg
6149 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
6150 If the relocation would fit entirely in 16 bits, it would be
6151 nice to emit:
6152 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
6153 instead, but that seems quite difficult. */
6154 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6155 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
6156 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6157 ((bfd_arch_bits_per_address (stdoutput) == 32
6158 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
6159 ? HAVE_NEWABI ? "add" : "addu" : "daddu"),
6160 "d,v,t", tempreg, tempreg, breg);
6161 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6162 (int) BFD_RELOC_PCREL_LO16, tempreg);
6163 if (! used_at)
6164 return;
6165 break;
6166 }
6167
6168 if (offset_expr.X_op != O_constant
6169 && offset_expr.X_op != O_symbol)
6170 {
6171 as_bad (_("expression too complex"));
6172 offset_expr.X_op = O_constant;
6173 }
6174
6175 /* A constant expression in PIC code can be handled just as it
6176 is in non PIC code. */
6177 if (mips_pic == NO_PIC
6178 || offset_expr.X_op == O_constant)
6179 {
6180 char *p;
6181
6182 /* If this is a reference to a GP relative symbol, and there
6183 is no base register, we want
6184 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6185 Otherwise, if there is no base register, we want
6186 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6187 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6188 If we have a constant, we need two instructions anyhow,
6189 so we always use the latter form.
6190
6191 If we have a base register, and this is a reference to a
6192 GP relative symbol, we want
6193 addu $tempreg,$breg,$gp
6194 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6195 Otherwise we want
6196 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6197 addu $tempreg,$tempreg,$breg
6198 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6199 With a constant we always use the latter case.
6200
6201 With 64bit address space and no base register and $at usable,
6202 we want
6203 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6204 lui $at,<sym> (BFD_RELOC_HI16_S)
6205 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6206 dsll32 $tempreg,0
6207 daddu $tempreg,$at
6208 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6209 If we have a base register, we want
6210 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6211 lui $at,<sym> (BFD_RELOC_HI16_S)
6212 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6213 daddu $at,$breg
6214 dsll32 $tempreg,0
6215 daddu $tempreg,$at
6216 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6217
6218 Without $at we can't generate the optimal path for superscalar
6219 processors here since this would require two temporary registers.
6220 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6221 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6222 dsll $tempreg,16
6223 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6224 dsll $tempreg,16
6225 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6226 If we have a base register, we want
6227 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6228 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6229 dsll $tempreg,16
6230 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6231 dsll $tempreg,16
6232 daddu $tempreg,$tempreg,$breg
6233 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6234
6235 If we have 64-bit addresses, as an optimization, for
6236 addresses which are 32-bit constants (e.g. kseg0/kseg1
6237 addresses) we fall back to the 32-bit address generation
6238 mechanism since it is more efficient. Note that due to
6239 the signed offset used by memory operations, the 32-bit
6240 range is shifted down by 32768 here. This code should
6241 probably attempt to generate 64-bit constants more
6242 efficiently in general.
6243
6244 As an extension for architectures with 64-bit registers,
6245 we don't truncate 64-bit addresses given as literal
6246 constants down to 32 bits, to support existing practice
6247 in the mips64 Linux (the kernel), that compiles source
6248 files with -mabi=64, assembling them as o32 or n32 (with
6249 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6250 the whole kernel is loaded into a memory region that is
6251 addressible with sign-extended 32-bit addresses, it is
6252 wasteful to compute the upper 32 bits of every
6253 non-literal address, that takes more space and time.
6254 Some day this should probably be implemented as an
6255 assembler option, such that the kernel doesn't have to
6256 use such ugly hacks, even though it will still have to
6257 end up converting the binary to ELF32 for a number of
6258 platforms whose boot loaders don't support ELF64
6259 binaries. */
6260 if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
6261 || (offset_expr.X_op == O_constant
6262 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
6263 && HAVE_64BIT_ADDRESS_CONSTANTS))
6264 {
6265 p = NULL;
6266
6267 /* We don't do GP optimization for now because RELAX_ENCODE can't
6268 hold the data for such large chunks. */
6269
6270 if (used_at == 0 && ! mips_opts.noat)
6271 {
6272 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6273 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6274 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6275 AT, (int) BFD_RELOC_HI16_S);
6276 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6277 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6278 if (breg != 0)
6279 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6280 "d,v,t", AT, AT, breg);
6281 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
6282 "d,w,<", tempreg, tempreg, 0);
6283 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6284 "d,v,t", tempreg, tempreg, AT);
6285 macro_build (p, &icnt, &offset_expr, s,
6286 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6287 used_at = 1;
6288 }
6289 else
6290 {
6291 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6292 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6293 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6294 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6295 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6296 "d,w,<", tempreg, tempreg, 16);
6297 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6298 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
6299 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6300 "d,w,<", tempreg, tempreg, 16);
6301 if (breg != 0)
6302 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6303 "d,v,t", tempreg, tempreg, breg);
6304 macro_build (p, &icnt, &offset_expr, s,
6305 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6306 }
6307
6308 return;
6309 }
6310 else if (offset_expr.X_op == O_constant
6311 && !HAVE_64BIT_ADDRESS_CONSTANTS
6312 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6313 as_bad (_("load/store address overflow (max 32 bits)"));
6314
6315 if (breg == 0)
6316 {
6317 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6318 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6319 p = NULL;
6320 else
6321 {
6322 frag_grow (20);
6323 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6324 treg, (int) BFD_RELOC_GPREL16,
6325 mips_gp_register);
6326 p = frag_var (rs_machine_dependent, 8, 0,
6327 RELAX_ENCODE (4, 8, 0, 4, 0,
6328 (mips_opts.warn_about_macros
6329 || (used_at
6330 && mips_opts.noat))),
6331 offset_expr.X_add_symbol, 0, NULL);
6332 used_at = 0;
6333 }
6334 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6335 if (p != NULL)
6336 p += 4;
6337 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6338 (int) BFD_RELOC_LO16, tempreg);
6339 }
6340 else
6341 {
6342 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6343 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6344 p = NULL;
6345 else
6346 {
6347 frag_grow (28);
6348 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6349 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6350 ? "add" : "addu" : "daddu",
6351 "d,v,t", tempreg, breg, mips_gp_register);
6352 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6353 treg, (int) BFD_RELOC_GPREL16, tempreg);
6354 p = frag_var (rs_machine_dependent, 12, 0,
6355 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
6356 offset_expr.X_add_symbol, 0, NULL);
6357 }
6358 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6359 if (p != NULL)
6360 p += 4;
6361 macro_build (p, &icnt, (expressionS *) NULL,
6362 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6363 ? "add" : "addu" : "daddu",
6364 "d,v,t", tempreg, tempreg, breg);
6365 if (p != NULL)
6366 p += 4;
6367 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6368 (int) BFD_RELOC_LO16, tempreg);
6369 }
6370 }
6371 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6372 {
6373 char *p;
6374 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
6375
6376 /* If this is a reference to an external symbol, we want
6377 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6378 nop
6379 <op> $treg,0($tempreg)
6380 Otherwise we want
6381 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6382 nop
6383 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6384 <op> $treg,0($tempreg)
6385
6386 For NewABI, we want
6387 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6388 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6389
6390 If there is a base register, we add it to $tempreg before
6391 the <op>. If there is a constant, we stick it in the
6392 <op> instruction. We don't handle constants larger than
6393 16 bits, because we have no way to load the upper 16 bits
6394 (actually, we could handle them for the subset of cases
6395 in which we are not using $at). */
6396 assert (offset_expr.X_op == O_symbol);
6397 if (HAVE_NEWABI)
6398 {
6399 macro_build ((char *) NULL, &icnt, &offset_expr,
6400 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6401 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6402 mips_gp_register);
6403 if (breg != 0)
6404 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6405 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6406 "d,v,t", tempreg, tempreg, breg);
6407 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6408 (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6409
6410 if (! used_at)
6411 return;
6412
6413 break;
6414 }
6415 expr1.X_add_number = offset_expr.X_add_number;
6416 offset_expr.X_add_number = 0;
6417 if (expr1.X_add_number < -0x8000
6418 || expr1.X_add_number >= 0x8000)
6419 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6420 frag_grow (20);
6421 macro_build ((char *) NULL, &icnt, &offset_expr,
6422 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
6423 (int) lw_reloc_type, mips_gp_register);
6424 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6425 p = frag_var (rs_machine_dependent, 4, 0,
6426 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
6427 offset_expr.X_add_symbol, 0, NULL);
6428 macro_build (p, &icnt, &offset_expr,
6429 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6430 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6431 if (breg != 0)
6432 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6433 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6434 "d,v,t", tempreg, tempreg, breg);
6435 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6436 (int) BFD_RELOC_LO16, tempreg);
6437 }
6438 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
6439 {
6440 int gpdel;
6441 char *p;
6442
6443 /* If this is a reference to an external symbol, we want
6444 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6445 addu $tempreg,$tempreg,$gp
6446 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6447 <op> $treg,0($tempreg)
6448 Otherwise we want
6449 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6450 nop
6451 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6452 <op> $treg,0($tempreg)
6453 If there is a base register, we add it to $tempreg before
6454 the <op>. If there is a constant, we stick it in the
6455 <op> instruction. We don't handle constants larger than
6456 16 bits, because we have no way to load the upper 16 bits
6457 (actually, we could handle them for the subset of cases
6458 in which we are not using $at). */
6459 assert (offset_expr.X_op == O_symbol);
6460 expr1.X_add_number = offset_expr.X_add_number;
6461 offset_expr.X_add_number = 0;
6462 if (expr1.X_add_number < -0x8000
6463 || expr1.X_add_number >= 0x8000)
6464 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6465 if (reg_needs_delay (mips_gp_register))
6466 gpdel = 4;
6467 else
6468 gpdel = 0;
6469 frag_grow (36);
6470 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6471 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6472 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6473 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6474 "d,v,t", tempreg, tempreg, mips_gp_register);
6475 macro_build ((char *) NULL, &icnt, &offset_expr,
6476 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6477 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6478 tempreg);
6479 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6480 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
6481 offset_expr.X_add_symbol, 0, NULL);
6482 if (gpdel > 0)
6483 {
6484 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6485 p += 4;
6486 }
6487 macro_build (p, &icnt, &offset_expr,
6488 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6489 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6490 mips_gp_register);
6491 p += 4;
6492 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6493 p += 4;
6494 macro_build (p, &icnt, &offset_expr,
6495 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
6496 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6497 if (breg != 0)
6498 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6499 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6500 "d,v,t", tempreg, tempreg, breg);
6501 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6502 (int) BFD_RELOC_LO16, tempreg);
6503 }
6504 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6505 {
6506 char *p;
6507 int bregsz = breg != 0 ? 4 : 0;
6508
6509 /* If this is a reference to an external symbol, we want
6510 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6511 add $tempreg,$tempreg,$gp
6512 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6513 <op> $treg,<ofst>($tempreg)
6514 Otherwise, for local symbols, we want:
6515 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6516 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6517 assert (offset_expr.X_op == O_symbol);
6518 frag_now->tc_frag_data.tc_fr_offset =
6519 expr1.X_add_number = offset_expr.X_add_number;
6520 offset_expr.X_add_number = 0;
6521 if (expr1.X_add_number < -0x8000
6522 || expr1.X_add_number >= 0x8000)
6523 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6524 frag_grow (36);
6525 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6526 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6527 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6528 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6529 "d,v,t", tempreg, tempreg, mips_gp_register);
6530 macro_build ((char *) NULL, &icnt, &offset_expr,
6531 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6532 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6533 tempreg);
6534 if (breg != 0)
6535 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6536 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6537 "d,v,t", tempreg, tempreg, breg);
6538 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6539 (int) BFD_RELOC_LO16, tempreg);
6540
6541 offset_expr.X_add_number = expr1.X_add_number;
6542 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6543 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6544 0, 4 + bregsz, 0, 0),
6545 offset_expr.X_add_symbol, 0, NULL);
6546 macro_build (p, &icnt, &offset_expr,
6547 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6548 "t,o(b)", tempreg,
6549 (int) BFD_RELOC_MIPS_GOT_PAGE,
6550 mips_gp_register);
6551 if (breg != 0)
6552 macro_build (p + 4, &icnt, (expressionS *) NULL,
6553 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6554 "d,v,t", tempreg, tempreg, breg);
6555 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6556 (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6557 }
6558 else if (mips_pic == EMBEDDED_PIC)
6559 {
6560 /* If there is no base register, we want
6561 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6562 If there is a base register, we want
6563 addu $tempreg,$breg,$gp
6564 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
6565 */
6566 assert (offset_expr.X_op == O_symbol);
6567 if (breg == 0)
6568 {
6569 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6570 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
6571 used_at = 0;
6572 }
6573 else
6574 {
6575 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6576 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6577 "d,v,t", tempreg, breg, mips_gp_register);
6578 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6579 treg, (int) BFD_RELOC_GPREL16, tempreg);
6580 }
6581 }
6582 else
6583 abort ();
6584
6585 if (! used_at)
6586 return;
6587
6588 break;
6589
6590 case M_LI:
6591 case M_LI_S:
6592 load_register (&icnt, treg, &imm_expr, 0);
6593 return;
6594
6595 case M_DLI:
6596 load_register (&icnt, treg, &imm_expr, 1);
6597 return;
6598
6599 case M_LI_SS:
6600 if (imm_expr.X_op == O_constant)
6601 {
6602 load_register (&icnt, AT, &imm_expr, 0);
6603 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6604 "mtc1", "t,G", AT, treg);
6605 break;
6606 }
6607 else
6608 {
6609 assert (offset_expr.X_op == O_symbol
6610 && strcmp (segment_name (S_GET_SEGMENT
6611 (offset_expr.X_add_symbol)),
6612 ".lit4") == 0
6613 && offset_expr.X_add_number == 0);
6614 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6615 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
6616 return;
6617 }
6618
6619 case M_LI_D:
6620 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6621 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6622 order 32 bits of the value and the low order 32 bits are either
6623 zero or in OFFSET_EXPR. */
6624 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6625 {
6626 if (HAVE_64BIT_GPRS)
6627 load_register (&icnt, treg, &imm_expr, 1);
6628 else
6629 {
6630 int hreg, lreg;
6631
6632 if (target_big_endian)
6633 {
6634 hreg = treg;
6635 lreg = treg + 1;
6636 }
6637 else
6638 {
6639 hreg = treg + 1;
6640 lreg = treg;
6641 }
6642
6643 if (hreg <= 31)
6644 load_register (&icnt, hreg, &imm_expr, 0);
6645 if (lreg <= 31)
6646 {
6647 if (offset_expr.X_op == O_absent)
6648 move_register (&icnt, lreg, 0);
6649 else
6650 {
6651 assert (offset_expr.X_op == O_constant);
6652 load_register (&icnt, lreg, &offset_expr, 0);
6653 }
6654 }
6655 }
6656 return;
6657 }
6658
6659 /* We know that sym is in the .rdata section. First we get the
6660 upper 16 bits of the address. */
6661 if (mips_pic == NO_PIC)
6662 {
6663 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6664 }
6665 else if (mips_pic == SVR4_PIC)
6666 {
6667 macro_build ((char *) NULL, &icnt, &offset_expr,
6668 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6669 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6670 mips_gp_register);
6671 }
6672 else if (mips_pic == EMBEDDED_PIC)
6673 {
6674 /* For embedded PIC we pick up the entire address off $gp in
6675 a single instruction. */
6676 macro_build ((char *) NULL, &icnt, &offset_expr,
6677 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6678 mips_gp_register, (int) BFD_RELOC_GPREL16);
6679 offset_expr.X_op = O_constant;
6680 offset_expr.X_add_number = 0;
6681 }
6682 else
6683 abort ();
6684
6685 /* Now we load the register(s). */
6686 if (HAVE_64BIT_GPRS)
6687 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6688 treg, (int) BFD_RELOC_LO16, AT);
6689 else
6690 {
6691 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6692 treg, (int) BFD_RELOC_LO16, AT);
6693 if (treg != RA)
6694 {
6695 /* FIXME: How in the world do we deal with the possible
6696 overflow here? */
6697 offset_expr.X_add_number += 4;
6698 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6699 treg + 1, (int) BFD_RELOC_LO16, AT);
6700 }
6701 }
6702
6703 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6704 does not become a variant frag. */
6705 frag_wane (frag_now);
6706 frag_new (0);
6707
6708 break;
6709
6710 case M_LI_DD:
6711 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6712 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6713 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6714 the value and the low order 32 bits are either zero or in
6715 OFFSET_EXPR. */
6716 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6717 {
6718 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6719 if (HAVE_64BIT_FPRS)
6720 {
6721 assert (HAVE_64BIT_GPRS);
6722 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6723 "dmtc1", "t,S", AT, treg);
6724 }
6725 else
6726 {
6727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6728 "mtc1", "t,G", AT, treg + 1);
6729 if (offset_expr.X_op == O_absent)
6730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6731 "mtc1", "t,G", 0, treg);
6732 else
6733 {
6734 assert (offset_expr.X_op == O_constant);
6735 load_register (&icnt, AT, &offset_expr, 0);
6736 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6737 "mtc1", "t,G", AT, treg);
6738 }
6739 }
6740 break;
6741 }
6742
6743 assert (offset_expr.X_op == O_symbol
6744 && offset_expr.X_add_number == 0);
6745 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6746 if (strcmp (s, ".lit8") == 0)
6747 {
6748 if (mips_opts.isa != ISA_MIPS1)
6749 {
6750 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6751 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6752 mips_gp_register);
6753 return;
6754 }
6755 breg = mips_gp_register;
6756 r = BFD_RELOC_MIPS_LITERAL;
6757 goto dob;
6758 }
6759 else
6760 {
6761 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6762 if (mips_pic == SVR4_PIC)
6763 macro_build ((char *) NULL, &icnt, &offset_expr,
6764 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6765 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6766 mips_gp_register);
6767 else
6768 {
6769 /* FIXME: This won't work for a 64 bit address. */
6770 macro_build_lui (NULL, &icnt, &offset_expr, AT);
6771 }
6772
6773 if (mips_opts.isa != ISA_MIPS1)
6774 {
6775 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6776 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6777
6778 /* To avoid confusion in tc_gen_reloc, we must ensure
6779 that this does not become a variant frag. */
6780 frag_wane (frag_now);
6781 frag_new (0);
6782
6783 break;
6784 }
6785 breg = AT;
6786 r = BFD_RELOC_LO16;
6787 goto dob;
6788 }
6789
6790 case M_L_DOB:
6791 if (mips_arch == CPU_R4650)
6792 {
6793 as_bad (_("opcode not supported on this processor"));
6794 return;
6795 }
6796 /* Even on a big endian machine $fn comes before $fn+1. We have
6797 to adjust when loading from memory. */
6798 r = BFD_RELOC_LO16;
6799 dob:
6800 assert (mips_opts.isa == ISA_MIPS1);
6801 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6802 target_big_endian ? treg + 1 : treg,
6803 (int) r, breg);
6804 /* FIXME: A possible overflow which I don't know how to deal
6805 with. */
6806 offset_expr.X_add_number += 4;
6807 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6808 target_big_endian ? treg : treg + 1,
6809 (int) r, breg);
6810
6811 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6812 does not become a variant frag. */
6813 frag_wane (frag_now);
6814 frag_new (0);
6815
6816 if (breg != AT)
6817 return;
6818 break;
6819
6820 case M_L_DAB:
6821 /*
6822 * The MIPS assembler seems to check for X_add_number not
6823 * being double aligned and generating:
6824 * lui at,%hi(foo+1)
6825 * addu at,at,v1
6826 * addiu at,at,%lo(foo+1)
6827 * lwc1 f2,0(at)
6828 * lwc1 f3,4(at)
6829 * But, the resulting address is the same after relocation so why
6830 * generate the extra instruction?
6831 */
6832 if (mips_arch == CPU_R4650)
6833 {
6834 as_bad (_("opcode not supported on this processor"));
6835 return;
6836 }
6837 /* Itbl support may require additional care here. */
6838 coproc = 1;
6839 if (mips_opts.isa != ISA_MIPS1)
6840 {
6841 s = "ldc1";
6842 goto ld;
6843 }
6844
6845 s = "lwc1";
6846 fmt = "T,o(b)";
6847 goto ldd_std;
6848
6849 case M_S_DAB:
6850 if (mips_arch == CPU_R4650)
6851 {
6852 as_bad (_("opcode not supported on this processor"));
6853 return;
6854 }
6855
6856 if (mips_opts.isa != ISA_MIPS1)
6857 {
6858 s = "sdc1";
6859 goto st;
6860 }
6861
6862 s = "swc1";
6863 fmt = "T,o(b)";
6864 /* Itbl support may require additional care here. */
6865 coproc = 1;
6866 goto ldd_std;
6867
6868 case M_LD_AB:
6869 if (HAVE_64BIT_GPRS)
6870 {
6871 s = "ld";
6872 goto ld;
6873 }
6874
6875 s = "lw";
6876 fmt = "t,o(b)";
6877 goto ldd_std;
6878
6879 case M_SD_AB:
6880 if (HAVE_64BIT_GPRS)
6881 {
6882 s = "sd";
6883 goto st;
6884 }
6885
6886 s = "sw";
6887 fmt = "t,o(b)";
6888
6889 ldd_std:
6890 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6891 loads for the case of doing a pair of loads to simulate an 'ld'.
6892 This is not currently done by the compiler, and assembly coders
6893 writing embedded-pic code can cope. */
6894
6895 if (offset_expr.X_op != O_symbol
6896 && offset_expr.X_op != O_constant)
6897 {
6898 as_bad (_("expression too complex"));
6899 offset_expr.X_op = O_constant;
6900 }
6901
6902 /* Even on a big endian machine $fn comes before $fn+1. We have
6903 to adjust when loading from memory. We set coproc if we must
6904 load $fn+1 first. */
6905 /* Itbl support may require additional care here. */
6906 if (! target_big_endian)
6907 coproc = 0;
6908
6909 if (mips_pic == NO_PIC
6910 || offset_expr.X_op == O_constant)
6911 {
6912 char *p;
6913
6914 /* If this is a reference to a GP relative symbol, we want
6915 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6916 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6917 If we have a base register, we use this
6918 addu $at,$breg,$gp
6919 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6920 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6921 If this is not a GP relative symbol, we want
6922 lui $at,<sym> (BFD_RELOC_HI16_S)
6923 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6924 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6925 If there is a base register, we add it to $at after the
6926 lui instruction. If there is a constant, we always use
6927 the last case. */
6928 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6929 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6930 {
6931 p = NULL;
6932 used_at = 1;
6933 }
6934 else
6935 {
6936 int off;
6937
6938 if (breg == 0)
6939 {
6940 frag_grow (28);
6941 tempreg = mips_gp_register;
6942 off = 0;
6943 used_at = 0;
6944 }
6945 else
6946 {
6947 frag_grow (36);
6948 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6949 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6950 ? "add" : "addu" : "daddu",
6951 "d,v,t", AT, breg, mips_gp_register);
6952 tempreg = AT;
6953 off = 4;
6954 used_at = 1;
6955 }
6956
6957 /* Itbl support may require additional care here. */
6958 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6959 coproc ? treg + 1 : treg,
6960 (int) BFD_RELOC_GPREL16, tempreg);
6961 offset_expr.X_add_number += 4;
6962
6963 /* Set mips_optimize to 2 to avoid inserting an
6964 undesired nop. */
6965 hold_mips_optimize = mips_optimize;
6966 mips_optimize = 2;
6967 /* Itbl support may require additional care here. */
6968 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6969 coproc ? treg : treg + 1,
6970 (int) BFD_RELOC_GPREL16, tempreg);
6971 mips_optimize = hold_mips_optimize;
6972
6973 p = frag_var (rs_machine_dependent, 12 + off, 0,
6974 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6975 used_at && mips_opts.noat),
6976 offset_expr.X_add_symbol, 0, NULL);
6977
6978 /* We just generated two relocs. When tc_gen_reloc
6979 handles this case, it will skip the first reloc and
6980 handle the second. The second reloc already has an
6981 extra addend of 4, which we added above. We must
6982 subtract it out, and then subtract another 4 to make
6983 the first reloc come out right. The second reloc
6984 will come out right because we are going to add 4 to
6985 offset_expr when we build its instruction below.
6986
6987 If we have a symbol, then we don't want to include
6988 the offset, because it will wind up being included
6989 when we generate the reloc. */
6990
6991 if (offset_expr.X_op == O_constant)
6992 offset_expr.X_add_number -= 8;
6993 else
6994 {
6995 offset_expr.X_add_number = -4;
6996 offset_expr.X_op = O_constant;
6997 }
6998 }
6999 macro_build_lui (p, &icnt, &offset_expr, AT);
7000 if (p != NULL)
7001 p += 4;
7002 if (breg != 0)
7003 {
7004 macro_build (p, &icnt, (expressionS *) NULL,
7005 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7006 ? "add" : "addu" : "daddu",
7007 "d,v,t", AT, breg, AT);
7008 if (p != NULL)
7009 p += 4;
7010 }
7011 /* Itbl support may require additional care here. */
7012 macro_build (p, &icnt, &offset_expr, s, fmt,
7013 coproc ? treg + 1 : treg,
7014 (int) BFD_RELOC_LO16, AT);
7015 if (p != NULL)
7016 p += 4;
7017 /* FIXME: How do we handle overflow here? */
7018 offset_expr.X_add_number += 4;
7019 /* Itbl support may require additional care here. */
7020 macro_build (p, &icnt, &offset_expr, s, fmt,
7021 coproc ? treg : treg + 1,
7022 (int) BFD_RELOC_LO16, AT);
7023 }
7024 else if (mips_pic == SVR4_PIC && ! mips_big_got)
7025 {
7026 int off;
7027
7028 /* If this is a reference to an external symbol, we want
7029 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7030 nop
7031 <op> $treg,0($at)
7032 <op> $treg+1,4($at)
7033 Otherwise we want
7034 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7035 nop
7036 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7037 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7038 If there is a base register we add it to $at before the
7039 lwc1 instructions. If there is a constant we include it
7040 in the lwc1 instructions. */
7041 used_at = 1;
7042 expr1.X_add_number = offset_expr.X_add_number;
7043 offset_expr.X_add_number = 0;
7044 if (expr1.X_add_number < -0x8000
7045 || expr1.X_add_number >= 0x8000 - 4)
7046 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7047 if (breg == 0)
7048 off = 0;
7049 else
7050 off = 4;
7051 frag_grow (24 + off);
7052 macro_build ((char *) NULL, &icnt, &offset_expr,
7053 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
7054 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
7055 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7056 if (breg != 0)
7057 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7058 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7059 ? "add" : "addu" : "daddu",
7060 "d,v,t", AT, breg, AT);
7061 /* Itbl support may require additional care here. */
7062 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7063 coproc ? treg + 1 : treg,
7064 (int) BFD_RELOC_LO16, AT);
7065 expr1.X_add_number += 4;
7066
7067 /* Set mips_optimize to 2 to avoid inserting an undesired
7068 nop. */
7069 hold_mips_optimize = mips_optimize;
7070 mips_optimize = 2;
7071 /* Itbl support may require additional care here. */
7072 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7073 coproc ? treg : treg + 1,
7074 (int) BFD_RELOC_LO16, AT);
7075 mips_optimize = hold_mips_optimize;
7076
7077 (void) frag_var (rs_machine_dependent, 0, 0,
7078 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
7079 offset_expr.X_add_symbol, 0, NULL);
7080 }
7081 else if (mips_pic == SVR4_PIC)
7082 {
7083 int gpdel, off;
7084 char *p;
7085
7086 /* If this is a reference to an external symbol, we want
7087 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7088 addu $at,$at,$gp
7089 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
7090 nop
7091 <op> $treg,0($at)
7092 <op> $treg+1,4($at)
7093 Otherwise we want
7094 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7095 nop
7096 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7097 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7098 If there is a base register we add it to $at before the
7099 lwc1 instructions. If there is a constant we include it
7100 in the lwc1 instructions. */
7101 used_at = 1;
7102 expr1.X_add_number = offset_expr.X_add_number;
7103 offset_expr.X_add_number = 0;
7104 if (expr1.X_add_number < -0x8000
7105 || expr1.X_add_number >= 0x8000 - 4)
7106 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7107 if (reg_needs_delay (mips_gp_register))
7108 gpdel = 4;
7109 else
7110 gpdel = 0;
7111 if (breg == 0)
7112 off = 0;
7113 else
7114 off = 4;
7115 frag_grow (56);
7116 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
7117 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
7118 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7119 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7120 ? "add" : "addu" : "daddu",
7121 "d,v,t", AT, AT, mips_gp_register);
7122 macro_build ((char *) NULL, &icnt, &offset_expr,
7123 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7124 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
7125 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7126 if (breg != 0)
7127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7128 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7129 ? "add" : "addu" : "daddu",
7130 "d,v,t", AT, breg, AT);
7131 /* Itbl support may require additional care here. */
7132 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7133 coproc ? treg + 1 : treg,
7134 (int) BFD_RELOC_LO16, AT);
7135 expr1.X_add_number += 4;
7136
7137 /* Set mips_optimize to 2 to avoid inserting an undesired
7138 nop. */
7139 hold_mips_optimize = mips_optimize;
7140 mips_optimize = 2;
7141 /* Itbl support may require additional care here. */
7142 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7143 coproc ? treg : treg + 1,
7144 (int) BFD_RELOC_LO16, AT);
7145 mips_optimize = hold_mips_optimize;
7146 expr1.X_add_number -= 4;
7147
7148 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
7149 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
7150 8 + gpdel + off, 1, 0),
7151 offset_expr.X_add_symbol, 0, NULL);
7152 if (gpdel > 0)
7153 {
7154 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7155 p += 4;
7156 }
7157 macro_build (p, &icnt, &offset_expr,
7158 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
7159 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
7160 mips_gp_register);
7161 p += 4;
7162 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7163 p += 4;
7164 if (breg != 0)
7165 {
7166 macro_build (p, &icnt, (expressionS *) NULL,
7167 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7168 ? "add" : "addu" : "daddu",
7169 "d,v,t", AT, breg, AT);
7170 p += 4;
7171 }
7172 /* Itbl support may require additional care here. */
7173 macro_build (p, &icnt, &expr1, s, fmt,
7174 coproc ? treg + 1 : treg,
7175 (int) BFD_RELOC_LO16, AT);
7176 p += 4;
7177 expr1.X_add_number += 4;
7178
7179 /* Set mips_optimize to 2 to avoid inserting an undesired
7180 nop. */
7181 hold_mips_optimize = mips_optimize;
7182 mips_optimize = 2;
7183 /* Itbl support may require additional care here. */
7184 macro_build (p, &icnt, &expr1, s, fmt,
7185 coproc ? treg : treg + 1,
7186 (int) BFD_RELOC_LO16, AT);
7187 mips_optimize = hold_mips_optimize;
7188 }
7189 else if (mips_pic == EMBEDDED_PIC)
7190 {
7191 /* If there is no base register, we use
7192 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
7193 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
7194 If we have a base register, we use
7195 addu $at,$breg,$gp
7196 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
7197 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
7198 */
7199 if (breg == 0)
7200 {
7201 tempreg = mips_gp_register;
7202 used_at = 0;
7203 }
7204 else
7205 {
7206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7207 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7208 "d,v,t", AT, breg, mips_gp_register);
7209 tempreg = AT;
7210 used_at = 1;
7211 }
7212
7213 /* Itbl support may require additional care here. */
7214 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7215 coproc ? treg + 1 : treg,
7216 (int) BFD_RELOC_GPREL16, tempreg);
7217 offset_expr.X_add_number += 4;
7218 /* Itbl support may require additional care here. */
7219 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7220 coproc ? treg : treg + 1,
7221 (int) BFD_RELOC_GPREL16, tempreg);
7222 }
7223 else
7224 abort ();
7225
7226 if (! used_at)
7227 return;
7228
7229 break;
7230
7231 case M_LD_OB:
7232 s = "lw";
7233 goto sd_ob;
7234 case M_SD_OB:
7235 s = "sw";
7236 sd_ob:
7237 assert (HAVE_32BIT_ADDRESSES);
7238 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7239 (int) BFD_RELOC_LO16, breg);
7240 offset_expr.X_add_number += 4;
7241 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7242 (int) BFD_RELOC_LO16, breg);
7243 return;
7244
7245 /* New code added to support COPZ instructions.
7246 This code builds table entries out of the macros in mip_opcodes.
7247 R4000 uses interlocks to handle coproc delays.
7248 Other chips (like the R3000) require nops to be inserted for delays.
7249
7250 FIXME: Currently, we require that the user handle delays.
7251 In order to fill delay slots for non-interlocked chips,
7252 we must have a way to specify delays based on the coprocessor.
7253 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7254 What are the side-effects of the cop instruction?
7255 What cache support might we have and what are its effects?
7256 Both coprocessor & memory require delays. how long???
7257 What registers are read/set/modified?
7258
7259 If an itbl is provided to interpret cop instructions,
7260 this knowledge can be encoded in the itbl spec. */
7261
7262 case M_COP0:
7263 s = "c0";
7264 goto copz;
7265 case M_COP1:
7266 s = "c1";
7267 goto copz;
7268 case M_COP2:
7269 s = "c2";
7270 goto copz;
7271 case M_COP3:
7272 s = "c3";
7273 copz:
7274 /* For now we just do C (same as Cz). The parameter will be
7275 stored in insn_opcode by mips_ip. */
7276 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
7277 ip->insn_opcode);
7278 return;
7279
7280 case M_MOVE:
7281 move_register (&icnt, dreg, sreg);
7282 return;
7283
7284 #ifdef LOSING_COMPILER
7285 default:
7286 /* Try and see if this is a new itbl instruction.
7287 This code builds table entries out of the macros in mip_opcodes.
7288 FIXME: For now we just assemble the expression and pass it's
7289 value along as a 32-bit immediate.
7290 We may want to have the assembler assemble this value,
7291 so that we gain the assembler's knowledge of delay slots,
7292 symbols, etc.
7293 Would it be more efficient to use mask (id) here? */
7294 if (itbl_have_entries
7295 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
7296 {
7297 s = ip->insn_mo->name;
7298 s2 = "cop3";
7299 coproc = ITBL_DECODE_PNUM (immed_expr);;
7300 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
7301 return;
7302 }
7303 macro2 (ip);
7304 return;
7305 }
7306 if (mips_opts.noat)
7307 as_warn (_("Macro used $at after \".set noat\""));
7308 }
7309
7310 static void
7311 macro2 (ip)
7312 struct mips_cl_insn *ip;
7313 {
7314 register int treg, sreg, dreg, breg;
7315 int tempreg;
7316 int mask;
7317 int icnt = 0;
7318 int used_at;
7319 expressionS expr1;
7320 const char *s;
7321 const char *s2;
7322 const char *fmt;
7323 int likely = 0;
7324 int dbl = 0;
7325 int coproc = 0;
7326 int lr = 0;
7327 int imm = 0;
7328 int off;
7329 offsetT maxnum;
7330 bfd_reloc_code_real_type r;
7331 char *p;
7332
7333 treg = (ip->insn_opcode >> 16) & 0x1f;
7334 dreg = (ip->insn_opcode >> 11) & 0x1f;
7335 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7336 mask = ip->insn_mo->mask;
7337
7338 expr1.X_op = O_constant;
7339 expr1.X_op_symbol = NULL;
7340 expr1.X_add_symbol = NULL;
7341 expr1.X_add_number = 1;
7342
7343 switch (mask)
7344 {
7345 #endif /* LOSING_COMPILER */
7346
7347 case M_DMUL:
7348 dbl = 1;
7349 case M_MUL:
7350 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7351 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7352 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7353 dreg);
7354 return;
7355
7356 case M_DMUL_I:
7357 dbl = 1;
7358 case M_MUL_I:
7359 /* The MIPS assembler some times generates shifts and adds. I'm
7360 not trying to be that fancy. GCC should do this for us
7361 anyway. */
7362 load_register (&icnt, AT, &imm_expr, dbl);
7363 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7364 dbl ? "dmult" : "mult", "s,t", sreg, AT);
7365 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7366 dreg);
7367 break;
7368
7369 case M_DMULO_I:
7370 dbl = 1;
7371 case M_MULO_I:
7372 imm = 1;
7373 goto do_mulo;
7374
7375 case M_DMULO:
7376 dbl = 1;
7377 case M_MULO:
7378 do_mulo:
7379 mips_emit_delays (TRUE);
7380 ++mips_opts.noreorder;
7381 mips_any_noreorder = 1;
7382 if (imm)
7383 load_register (&icnt, AT, &imm_expr, dbl);
7384 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7385 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7386 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7387 dreg);
7388 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7389 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7391 AT);
7392 if (mips_trap)
7393 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7394 "s,t,q", dreg, AT, 6);
7395 else
7396 {
7397 expr1.X_add_number = 8;
7398 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
7399 AT);
7400 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7401 0);
7402 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7403 "c", 6);
7404 }
7405 --mips_opts.noreorder;
7406 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
7407 break;
7408
7409 case M_DMULOU_I:
7410 dbl = 1;
7411 case M_MULOU_I:
7412 imm = 1;
7413 goto do_mulou;
7414
7415 case M_DMULOU:
7416 dbl = 1;
7417 case M_MULOU:
7418 do_mulou:
7419 mips_emit_delays (TRUE);
7420 ++mips_opts.noreorder;
7421 mips_any_noreorder = 1;
7422 if (imm)
7423 load_register (&icnt, AT, &imm_expr, dbl);
7424 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7425 dbl ? "dmultu" : "multu",
7426 "s,t", sreg, imm ? AT : treg);
7427 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7428 AT);
7429 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7430 dreg);
7431 if (mips_trap)
7432 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7433 "s,t,q", AT, 0, 6);
7434 else
7435 {
7436 expr1.X_add_number = 8;
7437 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7438 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7439 0);
7440 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7441 "c", 6);
7442 }
7443 --mips_opts.noreorder;
7444 break;
7445
7446 case M_DROL:
7447 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7448 {
7449 if (dreg == sreg)
7450 {
7451 tempreg = AT;
7452 used_at = 1;
7453 }
7454 else
7455 {
7456 tempreg = dreg;
7457 used_at = 0;
7458 }
7459 macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7460 "d,w", tempreg, treg);
7461 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7462 "d,t,s", dreg, sreg, tempreg);
7463 if (used_at)
7464 break;
7465 return;
7466 }
7467 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7468 "d,v,t", AT, 0, treg);
7469 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7470 "d,t,s", AT, sreg, AT);
7471 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7472 "d,t,s", dreg, sreg, treg);
7473 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7474 "d,v,t", dreg, dreg, AT);
7475 break;
7476
7477 case M_ROL:
7478 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7479 {
7480 if (dreg == sreg)
7481 {
7482 tempreg = AT;
7483 used_at = 1;
7484 }
7485 else
7486 {
7487 tempreg = dreg;
7488 used_at = 0;
7489 }
7490 macro_build ((char *) NULL, &icnt, NULL, "negu",
7491 "d,w", tempreg, treg);
7492 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7493 "d,t,s", dreg, sreg, tempreg);
7494 if (used_at)
7495 break;
7496 return;
7497 }
7498 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7499 "d,v,t", AT, 0, treg);
7500 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7501 "d,t,s", AT, sreg, AT);
7502 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7503 "d,t,s", dreg, sreg, treg);
7504 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7505 "d,v,t", dreg, dreg, AT);
7506 break;
7507
7508 case M_DROL_I:
7509 {
7510 unsigned int rot;
7511 char *l, *r;
7512
7513 if (imm_expr.X_op != O_constant)
7514 as_bad (_("Improper rotate count"));
7515 rot = imm_expr.X_add_number & 0x3f;
7516 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7517 {
7518 rot = (64 - rot) & 0x3f;
7519 if (rot >= 32)
7520 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7521 "d,w,<", dreg, sreg, rot - 32);
7522 else
7523 macro_build ((char *) NULL, &icnt, NULL, "dror",
7524 "d,w,<", dreg, sreg, rot);
7525 return;
7526 }
7527 if (rot == 0)
7528 {
7529 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7530 "d,w,<", dreg, sreg, 0);
7531 return;
7532 }
7533 l = (rot < 0x20) ? "dsll" : "dsll32";
7534 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7535 rot &= 0x1f;
7536 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7537 "d,w,<", AT, sreg, rot);
7538 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7539 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7540 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7541 "d,v,t", dreg, dreg, AT);
7542 }
7543 break;
7544
7545 case M_ROL_I:
7546 {
7547 unsigned int rot;
7548
7549 if (imm_expr.X_op != O_constant)
7550 as_bad (_("Improper rotate count"));
7551 rot = imm_expr.X_add_number & 0x1f;
7552 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7553 {
7554 macro_build ((char *) NULL, &icnt, NULL, "ror",
7555 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
7556 return;
7557 }
7558 if (rot == 0)
7559 {
7560 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7561 "d,w,<", dreg, sreg, 0);
7562 return;
7563 }
7564 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7565 "d,w,<", AT, sreg, rot);
7566 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7567 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7569 "d,v,t", dreg, dreg, AT);
7570 }
7571 break;
7572
7573 case M_DROR:
7574 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7575 {
7576 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7577 "d,t,s", dreg, sreg, treg);
7578 return;
7579 }
7580 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7581 "d,v,t", AT, 0, treg);
7582 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7583 "d,t,s", AT, sreg, AT);
7584 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7585 "d,t,s", dreg, sreg, treg);
7586 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7587 "d,v,t", dreg, dreg, AT);
7588 break;
7589
7590 case M_ROR:
7591 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7592 {
7593 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7594 "d,t,s", dreg, sreg, treg);
7595 return;
7596 }
7597 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7598 "d,v,t", AT, 0, treg);
7599 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7600 "d,t,s", AT, sreg, AT);
7601 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7602 "d,t,s", dreg, sreg, treg);
7603 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7604 "d,v,t", dreg, dreg, AT);
7605 break;
7606
7607 case M_DROR_I:
7608 {
7609 unsigned int rot;
7610 char *l, *r;
7611
7612 if (imm_expr.X_op != O_constant)
7613 as_bad (_("Improper rotate count"));
7614 rot = imm_expr.X_add_number & 0x3f;
7615 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
7616 {
7617 if (rot >= 32)
7618 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7619 "d,w,<", dreg, sreg, rot - 32);
7620 else
7621 macro_build ((char *) NULL, &icnt, NULL, "dror",
7622 "d,w,<", dreg, sreg, rot);
7623 return;
7624 }
7625 if (rot == 0)
7626 {
7627 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7628 "d,w,<", dreg, sreg, 0);
7629 return;
7630 }
7631 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7632 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7633 rot &= 0x1f;
7634 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7635 "d,w,<", AT, sreg, rot);
7636 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7637 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7638 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7639 "d,v,t", dreg, dreg, AT);
7640 }
7641 break;
7642
7643 case M_ROR_I:
7644 {
7645 unsigned int rot;
7646
7647 if (imm_expr.X_op != O_constant)
7648 as_bad (_("Improper rotate count"));
7649 rot = imm_expr.X_add_number & 0x1f;
7650 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
7651 {
7652 macro_build ((char *) NULL, &icnt, NULL, "ror",
7653 "d,w,<", dreg, sreg, rot);
7654 return;
7655 }
7656 if (rot == 0)
7657 {
7658 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7659 "d,w,<", dreg, sreg, 0);
7660 return;
7661 }
7662 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7663 "d,w,<", AT, sreg, rot);
7664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7665 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7667 "d,v,t", dreg, dreg, AT);
7668 }
7669 break;
7670
7671 case M_S_DOB:
7672 if (mips_arch == CPU_R4650)
7673 {
7674 as_bad (_("opcode not supported on this processor"));
7675 return;
7676 }
7677 assert (mips_opts.isa == ISA_MIPS1);
7678 /* Even on a big endian machine $fn comes before $fn+1. We have
7679 to adjust when storing to memory. */
7680 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7681 target_big_endian ? treg + 1 : treg,
7682 (int) BFD_RELOC_LO16, breg);
7683 offset_expr.X_add_number += 4;
7684 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7685 target_big_endian ? treg : treg + 1,
7686 (int) BFD_RELOC_LO16, breg);
7687 return;
7688
7689 case M_SEQ:
7690 if (sreg == 0)
7691 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7692 treg, (int) BFD_RELOC_LO16);
7693 else if (treg == 0)
7694 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7695 sreg, (int) BFD_RELOC_LO16);
7696 else
7697 {
7698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7699 "d,v,t", dreg, sreg, treg);
7700 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7701 dreg, (int) BFD_RELOC_LO16);
7702 }
7703 return;
7704
7705 case M_SEQ_I:
7706 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7707 {
7708 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7709 sreg, (int) BFD_RELOC_LO16);
7710 return;
7711 }
7712 if (sreg == 0)
7713 {
7714 as_warn (_("Instruction %s: result is always false"),
7715 ip->insn_mo->name);
7716 move_register (&icnt, dreg, 0);
7717 return;
7718 }
7719 if (imm_expr.X_op == O_constant
7720 && imm_expr.X_add_number >= 0
7721 && imm_expr.X_add_number < 0x10000)
7722 {
7723 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7724 sreg, (int) BFD_RELOC_LO16);
7725 used_at = 0;
7726 }
7727 else if (imm_expr.X_op == O_constant
7728 && imm_expr.X_add_number > -0x8000
7729 && imm_expr.X_add_number < 0)
7730 {
7731 imm_expr.X_add_number = -imm_expr.X_add_number;
7732 macro_build ((char *) NULL, &icnt, &imm_expr,
7733 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7734 "t,r,j", dreg, sreg,
7735 (int) BFD_RELOC_LO16);
7736 used_at = 0;
7737 }
7738 else
7739 {
7740 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7741 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7742 "d,v,t", dreg, sreg, AT);
7743 used_at = 1;
7744 }
7745 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7746 (int) BFD_RELOC_LO16);
7747 if (used_at)
7748 break;
7749 return;
7750
7751 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7752 s = "slt";
7753 goto sge;
7754 case M_SGEU:
7755 s = "sltu";
7756 sge:
7757 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7758 dreg, sreg, treg);
7759 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7760 (int) BFD_RELOC_LO16);
7761 return;
7762
7763 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7764 case M_SGEU_I:
7765 if (imm_expr.X_op == O_constant
7766 && imm_expr.X_add_number >= -0x8000
7767 && imm_expr.X_add_number < 0x8000)
7768 {
7769 macro_build ((char *) NULL, &icnt, &imm_expr,
7770 mask == M_SGE_I ? "slti" : "sltiu",
7771 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7772 used_at = 0;
7773 }
7774 else
7775 {
7776 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7777 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7778 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7779 AT);
7780 used_at = 1;
7781 }
7782 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7783 (int) BFD_RELOC_LO16);
7784 if (used_at)
7785 break;
7786 return;
7787
7788 case M_SGT: /* sreg > treg <==> treg < sreg */
7789 s = "slt";
7790 goto sgt;
7791 case M_SGTU:
7792 s = "sltu";
7793 sgt:
7794 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7795 dreg, treg, sreg);
7796 return;
7797
7798 case M_SGT_I: /* sreg > I <==> I < sreg */
7799 s = "slt";
7800 goto sgti;
7801 case M_SGTU_I:
7802 s = "sltu";
7803 sgti:
7804 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7805 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7806 dreg, AT, sreg);
7807 break;
7808
7809 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
7810 s = "slt";
7811 goto sle;
7812 case M_SLEU:
7813 s = "sltu";
7814 sle:
7815 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7816 dreg, treg, sreg);
7817 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7818 (int) BFD_RELOC_LO16);
7819 return;
7820
7821 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
7822 s = "slt";
7823 goto slei;
7824 case M_SLEU_I:
7825 s = "sltu";
7826 slei:
7827 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7828 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7829 dreg, AT, sreg);
7830 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7831 (int) BFD_RELOC_LO16);
7832 break;
7833
7834 case M_SLT_I:
7835 if (imm_expr.X_op == O_constant
7836 && imm_expr.X_add_number >= -0x8000
7837 && imm_expr.X_add_number < 0x8000)
7838 {
7839 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7840 dreg, sreg, (int) BFD_RELOC_LO16);
7841 return;
7842 }
7843 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7844 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7845 dreg, sreg, AT);
7846 break;
7847
7848 case M_SLTU_I:
7849 if (imm_expr.X_op == O_constant
7850 && imm_expr.X_add_number >= -0x8000
7851 && imm_expr.X_add_number < 0x8000)
7852 {
7853 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7854 dreg, sreg, (int) BFD_RELOC_LO16);
7855 return;
7856 }
7857 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7858 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7859 "d,v,t", dreg, sreg, AT);
7860 break;
7861
7862 case M_SNE:
7863 if (sreg == 0)
7864 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7865 "d,v,t", dreg, 0, treg);
7866 else if (treg == 0)
7867 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7868 "d,v,t", dreg, 0, sreg);
7869 else
7870 {
7871 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7872 "d,v,t", dreg, sreg, treg);
7873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7874 "d,v,t", dreg, 0, dreg);
7875 }
7876 return;
7877
7878 case M_SNE_I:
7879 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7880 {
7881 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7882 "d,v,t", dreg, 0, sreg);
7883 return;
7884 }
7885 if (sreg == 0)
7886 {
7887 as_warn (_("Instruction %s: result is always true"),
7888 ip->insn_mo->name);
7889 macro_build ((char *) NULL, &icnt, &expr1,
7890 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7891 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7892 return;
7893 }
7894 if (imm_expr.X_op == O_constant
7895 && imm_expr.X_add_number >= 0
7896 && imm_expr.X_add_number < 0x10000)
7897 {
7898 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7899 dreg, sreg, (int) BFD_RELOC_LO16);
7900 used_at = 0;
7901 }
7902 else if (imm_expr.X_op == O_constant
7903 && imm_expr.X_add_number > -0x8000
7904 && imm_expr.X_add_number < 0)
7905 {
7906 imm_expr.X_add_number = -imm_expr.X_add_number;
7907 macro_build ((char *) NULL, &icnt, &imm_expr,
7908 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
7909 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7910 used_at = 0;
7911 }
7912 else
7913 {
7914 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7915 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7916 "d,v,t", dreg, sreg, AT);
7917 used_at = 1;
7918 }
7919 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7920 "d,v,t", dreg, 0, dreg);
7921 if (used_at)
7922 break;
7923 return;
7924
7925 case M_DSUB_I:
7926 dbl = 1;
7927 case M_SUB_I:
7928 if (imm_expr.X_op == O_constant
7929 && imm_expr.X_add_number > -0x8000
7930 && imm_expr.X_add_number <= 0x8000)
7931 {
7932 imm_expr.X_add_number = -imm_expr.X_add_number;
7933 macro_build ((char *) NULL, &icnt, &imm_expr,
7934 dbl ? "daddi" : "addi",
7935 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7936 return;
7937 }
7938 load_register (&icnt, AT, &imm_expr, dbl);
7939 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7940 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
7941 break;
7942
7943 case M_DSUBU_I:
7944 dbl = 1;
7945 case M_SUBU_I:
7946 if (imm_expr.X_op == O_constant
7947 && imm_expr.X_add_number > -0x8000
7948 && imm_expr.X_add_number <= 0x8000)
7949 {
7950 imm_expr.X_add_number = -imm_expr.X_add_number;
7951 macro_build ((char *) NULL, &icnt, &imm_expr,
7952 dbl ? "daddiu" : "addiu",
7953 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7954 return;
7955 }
7956 load_register (&icnt, AT, &imm_expr, dbl);
7957 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7958 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
7959 break;
7960
7961 case M_TEQ_I:
7962 s = "teq";
7963 goto trap;
7964 case M_TGE_I:
7965 s = "tge";
7966 goto trap;
7967 case M_TGEU_I:
7968 s = "tgeu";
7969 goto trap;
7970 case M_TLT_I:
7971 s = "tlt";
7972 goto trap;
7973 case M_TLTU_I:
7974 s = "tltu";
7975 goto trap;
7976 case M_TNE_I:
7977 s = "tne";
7978 trap:
7979 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7980 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7981 AT);
7982 break;
7983
7984 case M_TRUNCWS:
7985 case M_TRUNCWD:
7986 assert (mips_opts.isa == ISA_MIPS1);
7987 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7988 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7989
7990 /*
7991 * Is the double cfc1 instruction a bug in the mips assembler;
7992 * or is there a reason for it?
7993 */
7994 mips_emit_delays (TRUE);
7995 ++mips_opts.noreorder;
7996 mips_any_noreorder = 1;
7997 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7998 treg, RA);
7999 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
8000 treg, RA);
8001 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8002 expr1.X_add_number = 3;
8003 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
8004 (int) BFD_RELOC_LO16);
8005 expr1.X_add_number = 2;
8006 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
8007 (int) BFD_RELOC_LO16);
8008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
8009 AT, RA);
8010 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8011 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8012 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
8013 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
8014 treg, RA);
8015 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8016 --mips_opts.noreorder;
8017 break;
8018
8019 case M_ULH:
8020 s = "lb";
8021 goto ulh;
8022 case M_ULHU:
8023 s = "lbu";
8024 ulh:
8025 if (offset_expr.X_add_number >= 0x7fff)
8026 as_bad (_("operand overflow"));
8027 if (! target_big_endian)
8028 ++offset_expr.X_add_number;
8029 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
8030 (int) BFD_RELOC_LO16, breg);
8031 if (! target_big_endian)
8032 --offset_expr.X_add_number;
8033 else
8034 ++offset_expr.X_add_number;
8035 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
8036 (int) BFD_RELOC_LO16, breg);
8037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8038 AT, AT, 8);
8039 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8040 treg, treg, AT);
8041 break;
8042
8043 case M_ULD:
8044 s = "ldl";
8045 s2 = "ldr";
8046 off = 7;
8047 goto ulw;
8048 case M_ULW:
8049 s = "lwl";
8050 s2 = "lwr";
8051 off = 3;
8052 ulw:
8053 if (offset_expr.X_add_number >= 0x8000 - off)
8054 as_bad (_("operand overflow"));
8055 if (treg != breg)
8056 tempreg = treg;
8057 else
8058 tempreg = AT;
8059 if (! target_big_endian)
8060 offset_expr.X_add_number += off;
8061 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
8062 (int) BFD_RELOC_LO16, breg);
8063 if (! target_big_endian)
8064 offset_expr.X_add_number -= off;
8065 else
8066 offset_expr.X_add_number += off;
8067 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
8068 (int) BFD_RELOC_LO16, breg);
8069
8070 /* If necessary, move the result in tempreg the final destination. */
8071 if (treg == tempreg)
8072 return;
8073 /* Protect second load's delay slot. */
8074 if (!gpr_interlocks)
8075 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8076 move_register (&icnt, treg, tempreg);
8077 break;
8078
8079 case M_ULD_A:
8080 s = "ldl";
8081 s2 = "ldr";
8082 off = 7;
8083 goto ulwa;
8084 case M_ULW_A:
8085 s = "lwl";
8086 s2 = "lwr";
8087 off = 3;
8088 ulwa:
8089 used_at = 1;
8090 load_address (&icnt, AT, &offset_expr, &used_at);
8091 if (breg != 0)
8092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8093 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8094 ? "add" : "addu" : "daddu",
8095 "d,v,t", AT, AT, breg);
8096 if (! target_big_endian)
8097 expr1.X_add_number = off;
8098 else
8099 expr1.X_add_number = 0;
8100 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8101 (int) BFD_RELOC_LO16, AT);
8102 if (! target_big_endian)
8103 expr1.X_add_number = 0;
8104 else
8105 expr1.X_add_number = off;
8106 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8107 (int) BFD_RELOC_LO16, AT);
8108 break;
8109
8110 case M_ULH_A:
8111 case M_ULHU_A:
8112 used_at = 1;
8113 load_address (&icnt, AT, &offset_expr, &used_at);
8114 if (breg != 0)
8115 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8116 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8117 ? "add" : "addu" : "daddu",
8118 "d,v,t", AT, AT, breg);
8119 if (target_big_endian)
8120 expr1.X_add_number = 0;
8121 macro_build ((char *) NULL, &icnt, &expr1,
8122 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
8123 (int) BFD_RELOC_LO16, AT);
8124 if (target_big_endian)
8125 expr1.X_add_number = 1;
8126 else
8127 expr1.X_add_number = 0;
8128 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8129 (int) BFD_RELOC_LO16, AT);
8130 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8131 treg, treg, 8);
8132 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8133 treg, treg, AT);
8134 break;
8135
8136 case M_USH:
8137 if (offset_expr.X_add_number >= 0x7fff)
8138 as_bad (_("operand overflow"));
8139 if (target_big_endian)
8140 ++offset_expr.X_add_number;
8141 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
8142 (int) BFD_RELOC_LO16, breg);
8143 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8144 AT, treg, 8);
8145 if (target_big_endian)
8146 --offset_expr.X_add_number;
8147 else
8148 ++offset_expr.X_add_number;
8149 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
8150 (int) BFD_RELOC_LO16, breg);
8151 break;
8152
8153 case M_USD:
8154 s = "sdl";
8155 s2 = "sdr";
8156 off = 7;
8157 goto usw;
8158 case M_USW:
8159 s = "swl";
8160 s2 = "swr";
8161 off = 3;
8162 usw:
8163 if (offset_expr.X_add_number >= 0x8000 - off)
8164 as_bad (_("operand overflow"));
8165 if (! target_big_endian)
8166 offset_expr.X_add_number += off;
8167 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
8168 (int) BFD_RELOC_LO16, breg);
8169 if (! target_big_endian)
8170 offset_expr.X_add_number -= off;
8171 else
8172 offset_expr.X_add_number += off;
8173 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
8174 (int) BFD_RELOC_LO16, breg);
8175 return;
8176
8177 case M_USD_A:
8178 s = "sdl";
8179 s2 = "sdr";
8180 off = 7;
8181 goto uswa;
8182 case M_USW_A:
8183 s = "swl";
8184 s2 = "swr";
8185 off = 3;
8186 uswa:
8187 used_at = 1;
8188 load_address (&icnt, AT, &offset_expr, &used_at);
8189 if (breg != 0)
8190 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8191 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8192 ? "add" : "addu" : "daddu",
8193 "d,v,t", AT, AT, breg);
8194 if (! target_big_endian)
8195 expr1.X_add_number = off;
8196 else
8197 expr1.X_add_number = 0;
8198 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8199 (int) BFD_RELOC_LO16, AT);
8200 if (! target_big_endian)
8201 expr1.X_add_number = 0;
8202 else
8203 expr1.X_add_number = off;
8204 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8205 (int) BFD_RELOC_LO16, AT);
8206 break;
8207
8208 case M_USH_A:
8209 used_at = 1;
8210 load_address (&icnt, AT, &offset_expr, &used_at);
8211 if (breg != 0)
8212 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8213 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8214 ? "add" : "addu" : "daddu",
8215 "d,v,t", AT, AT, breg);
8216 if (! target_big_endian)
8217 expr1.X_add_number = 0;
8218 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8219 (int) BFD_RELOC_LO16, AT);
8220 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8221 treg, treg, 8);
8222 if (! target_big_endian)
8223 expr1.X_add_number = 1;
8224 else
8225 expr1.X_add_number = 0;
8226 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8227 (int) BFD_RELOC_LO16, AT);
8228 if (! target_big_endian)
8229 expr1.X_add_number = 0;
8230 else
8231 expr1.X_add_number = 1;
8232 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8233 (int) BFD_RELOC_LO16, AT);
8234 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8235 treg, treg, 8);
8236 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8237 treg, treg, AT);
8238 break;
8239
8240 default:
8241 /* FIXME: Check if this is one of the itbl macros, since they
8242 are added dynamically. */
8243 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8244 break;
8245 }
8246 if (mips_opts.noat)
8247 as_warn (_("Macro used $at after \".set noat\""));
8248 }
8249
8250 /* Implement macros in mips16 mode. */
8251
8252 static void
8253 mips16_macro (ip)
8254 struct mips_cl_insn *ip;
8255 {
8256 int mask;
8257 int xreg, yreg, zreg, tmp;
8258 int icnt;
8259 expressionS expr1;
8260 int dbl;
8261 const char *s, *s2, *s3;
8262
8263 mask = ip->insn_mo->mask;
8264
8265 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
8266 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
8267 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
8268
8269 icnt = 0;
8270
8271 expr1.X_op = O_constant;
8272 expr1.X_op_symbol = NULL;
8273 expr1.X_add_symbol = NULL;
8274 expr1.X_add_number = 1;
8275
8276 dbl = 0;
8277
8278 switch (mask)
8279 {
8280 default:
8281 internalError ();
8282
8283 case M_DDIV_3:
8284 dbl = 1;
8285 case M_DIV_3:
8286 s = "mflo";
8287 goto do_div3;
8288 case M_DREM_3:
8289 dbl = 1;
8290 case M_REM_3:
8291 s = "mfhi";
8292 do_div3:
8293 mips_emit_delays (TRUE);
8294 ++mips_opts.noreorder;
8295 mips_any_noreorder = 1;
8296 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8297 dbl ? "ddiv" : "div",
8298 "0,x,y", xreg, yreg);
8299 expr1.X_add_number = 2;
8300 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
8302 7);
8303
8304 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8305 since that causes an overflow. We should do that as well,
8306 but I don't see how to do the comparisons without a temporary
8307 register. */
8308 --mips_opts.noreorder;
8309 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
8310 break;
8311
8312 case M_DIVU_3:
8313 s = "divu";
8314 s2 = "mflo";
8315 goto do_divu3;
8316 case M_REMU_3:
8317 s = "divu";
8318 s2 = "mfhi";
8319 goto do_divu3;
8320 case M_DDIVU_3:
8321 s = "ddivu";
8322 s2 = "mflo";
8323 goto do_divu3;
8324 case M_DREMU_3:
8325 s = "ddivu";
8326 s2 = "mfhi";
8327 do_divu3:
8328 mips_emit_delays (TRUE);
8329 ++mips_opts.noreorder;
8330 mips_any_noreorder = 1;
8331 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
8332 xreg, yreg);
8333 expr1.X_add_number = 2;
8334 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8335 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
8336 "6", 7);
8337 --mips_opts.noreorder;
8338 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
8339 break;
8340
8341 case M_DMUL:
8342 dbl = 1;
8343 case M_MUL:
8344 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8345 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
8346 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
8347 zreg);
8348 return;
8349
8350 case M_DSUBU_I:
8351 dbl = 1;
8352 goto do_subu;
8353 case M_SUBU_I:
8354 do_subu:
8355 if (imm_expr.X_op != O_constant)
8356 as_bad (_("Unsupported large constant"));
8357 imm_expr.X_add_number = -imm_expr.X_add_number;
8358 macro_build ((char *) NULL, &icnt, &imm_expr,
8359 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
8360 break;
8361
8362 case M_SUBU_I_2:
8363 if (imm_expr.X_op != O_constant)
8364 as_bad (_("Unsupported large constant"));
8365 imm_expr.X_add_number = -imm_expr.X_add_number;
8366 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
8367 "x,k", xreg);
8368 break;
8369
8370 case M_DSUBU_I_2:
8371 if (imm_expr.X_op != O_constant)
8372 as_bad (_("Unsupported large constant"));
8373 imm_expr.X_add_number = -imm_expr.X_add_number;
8374 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
8375 "y,j", yreg);
8376 break;
8377
8378 case M_BEQ:
8379 s = "cmp";
8380 s2 = "bteqz";
8381 goto do_branch;
8382 case M_BNE:
8383 s = "cmp";
8384 s2 = "btnez";
8385 goto do_branch;
8386 case M_BLT:
8387 s = "slt";
8388 s2 = "btnez";
8389 goto do_branch;
8390 case M_BLTU:
8391 s = "sltu";
8392 s2 = "btnez";
8393 goto do_branch;
8394 case M_BLE:
8395 s = "slt";
8396 s2 = "bteqz";
8397 goto do_reverse_branch;
8398 case M_BLEU:
8399 s = "sltu";
8400 s2 = "bteqz";
8401 goto do_reverse_branch;
8402 case M_BGE:
8403 s = "slt";
8404 s2 = "bteqz";
8405 goto do_branch;
8406 case M_BGEU:
8407 s = "sltu";
8408 s2 = "bteqz";
8409 goto do_branch;
8410 case M_BGT:
8411 s = "slt";
8412 s2 = "btnez";
8413 goto do_reverse_branch;
8414 case M_BGTU:
8415 s = "sltu";
8416 s2 = "btnez";
8417
8418 do_reverse_branch:
8419 tmp = xreg;
8420 xreg = yreg;
8421 yreg = tmp;
8422
8423 do_branch:
8424 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
8425 xreg, yreg);
8426 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8427 break;
8428
8429 case M_BEQ_I:
8430 s = "cmpi";
8431 s2 = "bteqz";
8432 s3 = "x,U";
8433 goto do_branch_i;
8434 case M_BNE_I:
8435 s = "cmpi";
8436 s2 = "btnez";
8437 s3 = "x,U";
8438 goto do_branch_i;
8439 case M_BLT_I:
8440 s = "slti";
8441 s2 = "btnez";
8442 s3 = "x,8";
8443 goto do_branch_i;
8444 case M_BLTU_I:
8445 s = "sltiu";
8446 s2 = "btnez";
8447 s3 = "x,8";
8448 goto do_branch_i;
8449 case M_BLE_I:
8450 s = "slti";
8451 s2 = "btnez";
8452 s3 = "x,8";
8453 goto do_addone_branch_i;
8454 case M_BLEU_I:
8455 s = "sltiu";
8456 s2 = "btnez";
8457 s3 = "x,8";
8458 goto do_addone_branch_i;
8459 case M_BGE_I:
8460 s = "slti";
8461 s2 = "bteqz";
8462 s3 = "x,8";
8463 goto do_branch_i;
8464 case M_BGEU_I:
8465 s = "sltiu";
8466 s2 = "bteqz";
8467 s3 = "x,8";
8468 goto do_branch_i;
8469 case M_BGT_I:
8470 s = "slti";
8471 s2 = "bteqz";
8472 s3 = "x,8";
8473 goto do_addone_branch_i;
8474 case M_BGTU_I:
8475 s = "sltiu";
8476 s2 = "bteqz";
8477 s3 = "x,8";
8478
8479 do_addone_branch_i:
8480 if (imm_expr.X_op != O_constant)
8481 as_bad (_("Unsupported large constant"));
8482 ++imm_expr.X_add_number;
8483
8484 do_branch_i:
8485 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8486 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8487 break;
8488
8489 case M_ABS:
8490 expr1.X_add_number = 0;
8491 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
8492 if (xreg != yreg)
8493 move_register (&icnt, xreg, yreg);
8494 expr1.X_add_number = 2;
8495 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8496 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8497 "neg", "x,w", xreg, xreg);
8498 }
8499 }
8500
8501 /* For consistency checking, verify that all bits are specified either
8502 by the match/mask part of the instruction definition, or by the
8503 operand list. */
8504 static int
8505 validate_mips_insn (opc)
8506 const struct mips_opcode *opc;
8507 {
8508 const char *p = opc->args;
8509 char c;
8510 unsigned long used_bits = opc->mask;
8511
8512 if ((used_bits & opc->match) != opc->match)
8513 {
8514 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8515 opc->name, opc->args);
8516 return 0;
8517 }
8518 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8519 while (*p)
8520 switch (c = *p++)
8521 {
8522 case ',': break;
8523 case '(': break;
8524 case ')': break;
8525 case '+':
8526 switch (c = *p++)
8527 {
8528 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8529 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8530 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8531 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8532 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8533 default:
8534 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8535 c, opc->name, opc->args);
8536 return 0;
8537 }
8538 break;
8539 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8540 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8541 case 'A': break;
8542 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
8543 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8544 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8545 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8546 case 'F': break;
8547 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8548 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
8549 case 'I': break;
8550 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
8551 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8552 case 'L': break;
8553 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8554 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
8555 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8556 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8557 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8558 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8559 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8560 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8561 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8562 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8563 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8564 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8565 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8566 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8567 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8568 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8569 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8570 case 'f': break;
8571 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8572 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8573 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8574 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8575 case 'l': break;
8576 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8577 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8578 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8579 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8580 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8581 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8582 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8583 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8584 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8585 case 'x': break;
8586 case 'z': break;
8587 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
8588 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8589 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8590 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8591 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8592 case '[': break;
8593 case ']': break;
8594 default:
8595 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8596 c, opc->name, opc->args);
8597 return 0;
8598 }
8599 #undef USE_BITS
8600 if (used_bits != 0xffffffff)
8601 {
8602 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8603 ~used_bits & 0xffffffff, opc->name, opc->args);
8604 return 0;
8605 }
8606 return 1;
8607 }
8608
8609 /* This routine assembles an instruction into its binary format. As a
8610 side effect, it sets one of the global variables imm_reloc or
8611 offset_reloc to the type of relocation to do if one of the operands
8612 is an address expression. */
8613
8614 static void
8615 mips_ip (str, ip)
8616 char *str;
8617 struct mips_cl_insn *ip;
8618 {
8619 char *s;
8620 const char *args;
8621 char c = 0;
8622 struct mips_opcode *insn;
8623 char *argsStart;
8624 unsigned int regno;
8625 unsigned int lastregno = 0;
8626 unsigned int lastpos = 0;
8627 unsigned int limlo, limhi;
8628 char *s_reset;
8629 char save_c = 0;
8630
8631 insn_error = NULL;
8632
8633 /* If the instruction contains a '.', we first try to match an instruction
8634 including the '.'. Then we try again without the '.'. */
8635 insn = NULL;
8636 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
8637 continue;
8638
8639 /* If we stopped on whitespace, then replace the whitespace with null for
8640 the call to hash_find. Save the character we replaced just in case we
8641 have to re-parse the instruction. */
8642 if (ISSPACE (*s))
8643 {
8644 save_c = *s;
8645 *s++ = '\0';
8646 }
8647
8648 insn = (struct mips_opcode *) hash_find (op_hash, str);
8649
8650 /* If we didn't find the instruction in the opcode table, try again, but
8651 this time with just the instruction up to, but not including the
8652 first '.'. */
8653 if (insn == NULL)
8654 {
8655 /* Restore the character we overwrite above (if any). */
8656 if (save_c)
8657 *(--s) = save_c;
8658
8659 /* Scan up to the first '.' or whitespace. */
8660 for (s = str;
8661 *s != '\0' && *s != '.' && !ISSPACE (*s);
8662 ++s)
8663 continue;
8664
8665 /* If we did not find a '.', then we can quit now. */
8666 if (*s != '.')
8667 {
8668 insn_error = "unrecognized opcode";
8669 return;
8670 }
8671
8672 /* Lookup the instruction in the hash table. */
8673 *s++ = '\0';
8674 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8675 {
8676 insn_error = "unrecognized opcode";
8677 return;
8678 }
8679 }
8680
8681 argsStart = s;
8682 for (;;)
8683 {
8684 bfd_boolean ok;
8685
8686 assert (strcmp (insn->name, str) == 0);
8687
8688 if (OPCODE_IS_MEMBER (insn,
8689 (mips_opts.isa
8690 | (file_ase_mips16 ? INSN_MIPS16 : 0)
8691 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
8692 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
8693 mips_arch))
8694 ok = TRUE;
8695 else
8696 ok = FALSE;
8697
8698 if (insn->pinfo != INSN_MACRO)
8699 {
8700 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
8701 ok = FALSE;
8702 }
8703
8704 if (! ok)
8705 {
8706 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8707 && strcmp (insn->name, insn[1].name) == 0)
8708 {
8709 ++insn;
8710 continue;
8711 }
8712 else
8713 {
8714 if (!insn_error)
8715 {
8716 static char buf[100];
8717 if (mips_arch_info->is_isa)
8718 sprintf (buf,
8719 _("opcode not supported at this ISA level (%s)"),
8720 mips_cpu_info_from_isa (mips_opts.isa)->name);
8721 else
8722 sprintf (buf,
8723 _("opcode not supported on this processor: %s (%s)"),
8724 mips_arch_info->name,
8725 mips_cpu_info_from_isa (mips_opts.isa)->name);
8726 insn_error = buf;
8727 }
8728 if (save_c)
8729 *(--s) = save_c;
8730 return;
8731 }
8732 }
8733
8734 ip->insn_mo = insn;
8735 ip->insn_opcode = insn->match;
8736 insn_error = NULL;
8737 for (args = insn->args;; ++args)
8738 {
8739 int is_mdmx;
8740
8741 s += strspn (s, " \t");
8742 is_mdmx = 0;
8743 switch (*args)
8744 {
8745 case '\0': /* end of args */
8746 if (*s == '\0')
8747 return;
8748 break;
8749
8750 case ',':
8751 if (*s++ == *args)
8752 continue;
8753 s--;
8754 switch (*++args)
8755 {
8756 case 'r':
8757 case 'v':
8758 ip->insn_opcode |= lastregno << OP_SH_RS;
8759 continue;
8760
8761 case 'w':
8762 ip->insn_opcode |= lastregno << OP_SH_RT;
8763 continue;
8764
8765 case 'W':
8766 ip->insn_opcode |= lastregno << OP_SH_FT;
8767 continue;
8768
8769 case 'V':
8770 ip->insn_opcode |= lastregno << OP_SH_FS;
8771 continue;
8772 }
8773 break;
8774
8775 case '(':
8776 /* Handle optional base register.
8777 Either the base register is omitted or
8778 we must have a left paren. */
8779 /* This is dependent on the next operand specifier
8780 is a base register specification. */
8781 assert (args[1] == 'b' || args[1] == '5'
8782 || args[1] == '-' || args[1] == '4');
8783 if (*s == '\0')
8784 return;
8785
8786 case ')': /* these must match exactly */
8787 case '[':
8788 case ']':
8789 if (*s++ == *args)
8790 continue;
8791 break;
8792
8793 case '+': /* Opcode extension character. */
8794 switch (*++args)
8795 {
8796 case 'A': /* ins/ext position, becomes LSB. */
8797 limlo = 0;
8798 limhi = 31;
8799 my_getExpression (&imm_expr, s);
8800 check_absolute_expr (ip, &imm_expr);
8801 if ((unsigned long) imm_expr.X_add_number < limlo
8802 || (unsigned long) imm_expr.X_add_number > limhi)
8803 {
8804 as_bad (_("Improper position (%lu)"),
8805 (unsigned long) imm_expr.X_add_number);
8806 imm_expr.X_add_number = limlo;
8807 }
8808 lastpos = imm_expr.X_add_number;
8809 ip->insn_opcode |= (imm_expr.X_add_number
8810 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8811 imm_expr.X_op = O_absent;
8812 s = expr_end;
8813 continue;
8814
8815 case 'B': /* ins size, becomes MSB. */
8816 limlo = 1;
8817 limhi = 32;
8818 my_getExpression (&imm_expr, s);
8819 check_absolute_expr (ip, &imm_expr);
8820 /* Check for negative input so that small negative numbers
8821 will not succeed incorrectly. The checks against
8822 (pos+size) transitively check "size" itself,
8823 assuming that "pos" is reasonable. */
8824 if ((long) imm_expr.X_add_number < 0
8825 || ((unsigned long) imm_expr.X_add_number
8826 + lastpos) < limlo
8827 || ((unsigned long) imm_expr.X_add_number
8828 + lastpos) > limhi)
8829 {
8830 as_bad (_("Improper insert size (%lu, position %lu)"),
8831 (unsigned long) imm_expr.X_add_number,
8832 (unsigned long) lastpos);
8833 imm_expr.X_add_number = limlo - lastpos;
8834 }
8835 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8836 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8837 imm_expr.X_op = O_absent;
8838 s = expr_end;
8839 continue;
8840
8841 case 'C': /* ext size, becomes MSBD. */
8842 limlo = 1;
8843 limhi = 32;
8844 my_getExpression (&imm_expr, s);
8845 check_absolute_expr (ip, &imm_expr);
8846 /* Check for negative input so that small negative numbers
8847 will not succeed incorrectly. The checks against
8848 (pos+size) transitively check "size" itself,
8849 assuming that "pos" is reasonable. */
8850 if ((long) imm_expr.X_add_number < 0
8851 || ((unsigned long) imm_expr.X_add_number
8852 + lastpos) < limlo
8853 || ((unsigned long) imm_expr.X_add_number
8854 + lastpos) > limhi)
8855 {
8856 as_bad (_("Improper extract size (%lu, position %lu)"),
8857 (unsigned long) imm_expr.X_add_number,
8858 (unsigned long) lastpos);
8859 imm_expr.X_add_number = limlo - lastpos;
8860 }
8861 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8862 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8863 imm_expr.X_op = O_absent;
8864 s = expr_end;
8865 continue;
8866
8867 case 'D':
8868 /* +D is for disassembly only; never match. */
8869 break;
8870
8871 default:
8872 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8873 *args, insn->name, insn->args);
8874 /* Further processing is fruitless. */
8875 return;
8876 }
8877 break;
8878
8879 case '<': /* must be at least one digit */
8880 /*
8881 * According to the manual, if the shift amount is greater
8882 * than 31 or less than 0, then the shift amount should be
8883 * mod 32. In reality the mips assembler issues an error.
8884 * We issue a warning and mask out all but the low 5 bits.
8885 */
8886 my_getExpression (&imm_expr, s);
8887 check_absolute_expr (ip, &imm_expr);
8888 if ((unsigned long) imm_expr.X_add_number > 31)
8889 {
8890 as_warn (_("Improper shift amount (%lu)"),
8891 (unsigned long) imm_expr.X_add_number);
8892 imm_expr.X_add_number &= OP_MASK_SHAMT;
8893 }
8894 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
8895 imm_expr.X_op = O_absent;
8896 s = expr_end;
8897 continue;
8898
8899 case '>': /* shift amount minus 32 */
8900 my_getExpression (&imm_expr, s);
8901 check_absolute_expr (ip, &imm_expr);
8902 if ((unsigned long) imm_expr.X_add_number < 32
8903 || (unsigned long) imm_expr.X_add_number > 63)
8904 break;
8905 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
8906 imm_expr.X_op = O_absent;
8907 s = expr_end;
8908 continue;
8909
8910 case 'k': /* cache code */
8911 case 'h': /* prefx code */
8912 my_getExpression (&imm_expr, s);
8913 check_absolute_expr (ip, &imm_expr);
8914 if ((unsigned long) imm_expr.X_add_number > 31)
8915 {
8916 as_warn (_("Invalid value for `%s' (%lu)"),
8917 ip->insn_mo->name,
8918 (unsigned long) imm_expr.X_add_number);
8919 imm_expr.X_add_number &= 0x1f;
8920 }
8921 if (*args == 'k')
8922 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8923 else
8924 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8925 imm_expr.X_op = O_absent;
8926 s = expr_end;
8927 continue;
8928
8929 case 'c': /* break code */
8930 my_getExpression (&imm_expr, s);
8931 check_absolute_expr (ip, &imm_expr);
8932 if ((unsigned long) imm_expr.X_add_number > 1023)
8933 {
8934 as_warn (_("Illegal break code (%lu)"),
8935 (unsigned long) imm_expr.X_add_number);
8936 imm_expr.X_add_number &= OP_MASK_CODE;
8937 }
8938 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
8939 imm_expr.X_op = O_absent;
8940 s = expr_end;
8941 continue;
8942
8943 case 'q': /* lower break code */
8944 my_getExpression (&imm_expr, s);
8945 check_absolute_expr (ip, &imm_expr);
8946 if ((unsigned long) imm_expr.X_add_number > 1023)
8947 {
8948 as_warn (_("Illegal lower break code (%lu)"),
8949 (unsigned long) imm_expr.X_add_number);
8950 imm_expr.X_add_number &= OP_MASK_CODE2;
8951 }
8952 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
8953 imm_expr.X_op = O_absent;
8954 s = expr_end;
8955 continue;
8956
8957 case 'B': /* 20-bit syscall/break code. */
8958 my_getExpression (&imm_expr, s);
8959 check_absolute_expr (ip, &imm_expr);
8960 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8961 as_warn (_("Illegal 20-bit code (%lu)"),
8962 (unsigned long) imm_expr.X_add_number);
8963 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
8964 imm_expr.X_op = O_absent;
8965 s = expr_end;
8966 continue;
8967
8968 case 'C': /* Coprocessor code */
8969 my_getExpression (&imm_expr, s);
8970 check_absolute_expr (ip, &imm_expr);
8971 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
8972 {
8973 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8974 (unsigned long) imm_expr.X_add_number);
8975 imm_expr.X_add_number &= ((1 << 25) - 1);
8976 }
8977 ip->insn_opcode |= imm_expr.X_add_number;
8978 imm_expr.X_op = O_absent;
8979 s = expr_end;
8980 continue;
8981
8982 case 'J': /* 19-bit wait code. */
8983 my_getExpression (&imm_expr, s);
8984 check_absolute_expr (ip, &imm_expr);
8985 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8986 as_warn (_("Illegal 19-bit code (%lu)"),
8987 (unsigned long) imm_expr.X_add_number);
8988 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
8989 imm_expr.X_op = O_absent;
8990 s = expr_end;
8991 continue;
8992
8993 case 'P': /* Performance register */
8994 my_getExpression (&imm_expr, s);
8995 check_absolute_expr (ip, &imm_expr);
8996 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
8997 {
8998 as_warn (_("Invalid performance register (%lu)"),
8999 (unsigned long) imm_expr.X_add_number);
9000 imm_expr.X_add_number &= OP_MASK_PERFREG;
9001 }
9002 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
9003 imm_expr.X_op = O_absent;
9004 s = expr_end;
9005 continue;
9006
9007 case 'b': /* base register */
9008 case 'd': /* destination register */
9009 case 's': /* source register */
9010 case 't': /* target register */
9011 case 'r': /* both target and source */
9012 case 'v': /* both dest and source */
9013 case 'w': /* both dest and target */
9014 case 'E': /* coprocessor target register */
9015 case 'G': /* coprocessor destination register */
9016 case 'K': /* 'rdhwr' destination register */
9017 case 'x': /* ignore register name */
9018 case 'z': /* must be zero register */
9019 case 'U': /* destination register (clo/clz). */
9020 s_reset = s;
9021 if (s[0] == '$')
9022 {
9023
9024 if (ISDIGIT (s[1]))
9025 {
9026 ++s;
9027 regno = 0;
9028 do
9029 {
9030 regno *= 10;
9031 regno += *s - '0';
9032 ++s;
9033 }
9034 while (ISDIGIT (*s));
9035 if (regno > 31)
9036 as_bad (_("Invalid register number (%d)"), regno);
9037 }
9038 else if (*args == 'E' || *args == 'G' || *args == 'K')
9039 goto notreg;
9040 else
9041 {
9042 if (s[1] == 'r' && s[2] == 'a')
9043 {
9044 s += 3;
9045 regno = RA;
9046 }
9047 else if (s[1] == 'f' && s[2] == 'p')
9048 {
9049 s += 3;
9050 regno = FP;
9051 }
9052 else if (s[1] == 's' && s[2] == 'p')
9053 {
9054 s += 3;
9055 regno = SP;
9056 }
9057 else if (s[1] == 'g' && s[2] == 'p')
9058 {
9059 s += 3;
9060 regno = GP;
9061 }
9062 else if (s[1] == 'a' && s[2] == 't')
9063 {
9064 s += 3;
9065 regno = AT;
9066 }
9067 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9068 {
9069 s += 4;
9070 regno = KT0;
9071 }
9072 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9073 {
9074 s += 4;
9075 regno = KT1;
9076 }
9077 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9078 {
9079 s += 5;
9080 regno = ZERO;
9081 }
9082 else if (itbl_have_entries)
9083 {
9084 char *p, *n;
9085 unsigned long r;
9086
9087 p = s + 1; /* advance past '$' */
9088 n = itbl_get_field (&p); /* n is name */
9089
9090 /* See if this is a register defined in an
9091 itbl entry. */
9092 if (itbl_get_reg_val (n, &r))
9093 {
9094 /* Get_field advances to the start of
9095 the next field, so we need to back
9096 rack to the end of the last field. */
9097 if (p)
9098 s = p - 1;
9099 else
9100 s = strchr (s, '\0');
9101 regno = r;
9102 }
9103 else
9104 goto notreg;
9105 }
9106 else
9107 goto notreg;
9108 }
9109 if (regno == AT
9110 && ! mips_opts.noat
9111 && *args != 'E'
9112 && *args != 'G'
9113 && *args != 'K')
9114 as_warn (_("Used $at without \".set noat\""));
9115 c = *args;
9116 if (*s == ' ')
9117 ++s;
9118 if (args[1] != *s)
9119 {
9120 if (c == 'r' || c == 'v' || c == 'w')
9121 {
9122 regno = lastregno;
9123 s = s_reset;
9124 ++args;
9125 }
9126 }
9127 /* 'z' only matches $0. */
9128 if (c == 'z' && regno != 0)
9129 break;
9130
9131 /* Now that we have assembled one operand, we use the args string
9132 * to figure out where it goes in the instruction. */
9133 switch (c)
9134 {
9135 case 'r':
9136 case 's':
9137 case 'v':
9138 case 'b':
9139 ip->insn_opcode |= regno << OP_SH_RS;
9140 break;
9141 case 'd':
9142 case 'G':
9143 case 'K':
9144 ip->insn_opcode |= regno << OP_SH_RD;
9145 break;
9146 case 'U':
9147 ip->insn_opcode |= regno << OP_SH_RD;
9148 ip->insn_opcode |= regno << OP_SH_RT;
9149 break;
9150 case 'w':
9151 case 't':
9152 case 'E':
9153 ip->insn_opcode |= regno << OP_SH_RT;
9154 break;
9155 case 'x':
9156 /* This case exists because on the r3000 trunc
9157 expands into a macro which requires a gp
9158 register. On the r6000 or r4000 it is
9159 assembled into a single instruction which
9160 ignores the register. Thus the insn version
9161 is MIPS_ISA2 and uses 'x', and the macro
9162 version is MIPS_ISA1 and uses 't'. */
9163 break;
9164 case 'z':
9165 /* This case is for the div instruction, which
9166 acts differently if the destination argument
9167 is $0. This only matches $0, and is checked
9168 outside the switch. */
9169 break;
9170 case 'D':
9171 /* Itbl operand; not yet implemented. FIXME ?? */
9172 break;
9173 /* What about all other operands like 'i', which
9174 can be specified in the opcode table? */
9175 }
9176 lastregno = regno;
9177 continue;
9178 }
9179 notreg:
9180 switch (*args++)
9181 {
9182 case 'r':
9183 case 'v':
9184 ip->insn_opcode |= lastregno << OP_SH_RS;
9185 continue;
9186 case 'w':
9187 ip->insn_opcode |= lastregno << OP_SH_RT;
9188 continue;
9189 }
9190 break;
9191
9192 case 'O': /* MDMX alignment immediate constant. */
9193 my_getExpression (&imm_expr, s);
9194 check_absolute_expr (ip, &imm_expr);
9195 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9196 {
9197 as_warn ("Improper align amount (%ld), using low bits",
9198 (long) imm_expr.X_add_number);
9199 imm_expr.X_add_number &= OP_MASK_ALN;
9200 }
9201 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
9202 imm_expr.X_op = O_absent;
9203 s = expr_end;
9204 continue;
9205
9206 case 'Q': /* MDMX vector, element sel, or const. */
9207 if (s[0] != '$')
9208 {
9209 /* MDMX Immediate. */
9210 my_getExpression (&imm_expr, s);
9211 check_absolute_expr (ip, &imm_expr);
9212 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9213 {
9214 as_warn (_("Invalid MDMX Immediate (%ld)"),
9215 (long) imm_expr.X_add_number);
9216 imm_expr.X_add_number &= OP_MASK_FT;
9217 }
9218 imm_expr.X_add_number &= OP_MASK_FT;
9219 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9220 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9221 else
9222 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9223 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
9224 imm_expr.X_op = O_absent;
9225 s = expr_end;
9226 continue;
9227 }
9228 /* Not MDMX Immediate. Fall through. */
9229 case 'X': /* MDMX destination register. */
9230 case 'Y': /* MDMX source register. */
9231 case 'Z': /* MDMX target register. */
9232 is_mdmx = 1;
9233 case 'D': /* floating point destination register */
9234 case 'S': /* floating point source register */
9235 case 'T': /* floating point target register */
9236 case 'R': /* floating point source register */
9237 case 'V':
9238 case 'W':
9239 s_reset = s;
9240 /* Accept $fN for FP and MDMX register numbers, and in
9241 addition accept $vN for MDMX register numbers. */
9242 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
9243 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
9244 && ISDIGIT (s[2])))
9245 {
9246 s += 2;
9247 regno = 0;
9248 do
9249 {
9250 regno *= 10;
9251 regno += *s - '0';
9252 ++s;
9253 }
9254 while (ISDIGIT (*s));
9255
9256 if (regno > 31)
9257 as_bad (_("Invalid float register number (%d)"), regno);
9258
9259 if ((regno & 1) != 0
9260 && HAVE_32BIT_FPRS
9261 && ! (strcmp (str, "mtc1") == 0
9262 || strcmp (str, "mfc1") == 0
9263 || strcmp (str, "lwc1") == 0
9264 || strcmp (str, "swc1") == 0
9265 || strcmp (str, "l.s") == 0
9266 || strcmp (str, "s.s") == 0))
9267 as_warn (_("Float register should be even, was %d"),
9268 regno);
9269
9270 c = *args;
9271 if (*s == ' ')
9272 ++s;
9273 if (args[1] != *s)
9274 {
9275 if (c == 'V' || c == 'W')
9276 {
9277 regno = lastregno;
9278 s = s_reset;
9279 ++args;
9280 }
9281 }
9282 switch (c)
9283 {
9284 case 'D':
9285 case 'X':
9286 ip->insn_opcode |= regno << OP_SH_FD;
9287 break;
9288 case 'V':
9289 case 'S':
9290 case 'Y':
9291 ip->insn_opcode |= regno << OP_SH_FS;
9292 break;
9293 case 'Q':
9294 /* This is like 'Z', but also needs to fix the MDMX
9295 vector/scalar select bits. Note that the
9296 scalar immediate case is handled above. */
9297 if (*s == '[')
9298 {
9299 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9300 int max_el = (is_qh ? 3 : 7);
9301 s++;
9302 my_getExpression(&imm_expr, s);
9303 check_absolute_expr (ip, &imm_expr);
9304 s = expr_end;
9305 if (imm_expr.X_add_number > max_el)
9306 as_bad(_("Bad element selector %ld"),
9307 (long) imm_expr.X_add_number);
9308 imm_expr.X_add_number &= max_el;
9309 ip->insn_opcode |= (imm_expr.X_add_number
9310 << (OP_SH_VSEL +
9311 (is_qh ? 2 : 1)));
9312 if (*s != ']')
9313 as_warn(_("Expecting ']' found '%s'"), s);
9314 else
9315 s++;
9316 }
9317 else
9318 {
9319 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9320 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9321 << OP_SH_VSEL);
9322 else
9323 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9324 OP_SH_VSEL);
9325 }
9326 /* Fall through */
9327 case 'W':
9328 case 'T':
9329 case 'Z':
9330 ip->insn_opcode |= regno << OP_SH_FT;
9331 break;
9332 case 'R':
9333 ip->insn_opcode |= regno << OP_SH_FR;
9334 break;
9335 }
9336 lastregno = regno;
9337 continue;
9338 }
9339
9340 switch (*args++)
9341 {
9342 case 'V':
9343 ip->insn_opcode |= lastregno << OP_SH_FS;
9344 continue;
9345 case 'W':
9346 ip->insn_opcode |= lastregno << OP_SH_FT;
9347 continue;
9348 }
9349 break;
9350
9351 case 'I':
9352 my_getExpression (&imm_expr, s);
9353 if (imm_expr.X_op != O_big
9354 && imm_expr.X_op != O_constant)
9355 insn_error = _("absolute expression required");
9356 s = expr_end;
9357 continue;
9358
9359 case 'A':
9360 my_getExpression (&offset_expr, s);
9361 *imm_reloc = BFD_RELOC_32;
9362 s = expr_end;
9363 continue;
9364
9365 case 'F':
9366 case 'L':
9367 case 'f':
9368 case 'l':
9369 {
9370 int f64;
9371 int using_gprs;
9372 char *save_in;
9373 char *err;
9374 unsigned char temp[8];
9375 int len;
9376 unsigned int length;
9377 segT seg;
9378 subsegT subseg;
9379 char *p;
9380
9381 /* These only appear as the last operand in an
9382 instruction, and every instruction that accepts
9383 them in any variant accepts them in all variants.
9384 This means we don't have to worry about backing out
9385 any changes if the instruction does not match.
9386
9387 The difference between them is the size of the
9388 floating point constant and where it goes. For 'F'
9389 and 'L' the constant is 64 bits; for 'f' and 'l' it
9390 is 32 bits. Where the constant is placed is based
9391 on how the MIPS assembler does things:
9392 F -- .rdata
9393 L -- .lit8
9394 f -- immediate value
9395 l -- .lit4
9396
9397 The .lit4 and .lit8 sections are only used if
9398 permitted by the -G argument.
9399
9400 When generating embedded PIC code, we use the
9401 .lit8 section but not the .lit4 section (we can do
9402 .lit4 inline easily; we need to put .lit8
9403 somewhere in the data segment, and using .lit8
9404 permits the linker to eventually combine identical
9405 .lit8 entries).
9406
9407 The code below needs to know whether the target register
9408 is 32 or 64 bits wide. It relies on the fact 'f' and
9409 'F' are used with GPR-based instructions and 'l' and
9410 'L' are used with FPR-based instructions. */
9411
9412 f64 = *args == 'F' || *args == 'L';
9413 using_gprs = *args == 'F' || *args == 'f';
9414
9415 save_in = input_line_pointer;
9416 input_line_pointer = s;
9417 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9418 length = len;
9419 s = input_line_pointer;
9420 input_line_pointer = save_in;
9421 if (err != NULL && *err != '\0')
9422 {
9423 as_bad (_("Bad floating point constant: %s"), err);
9424 memset (temp, '\0', sizeof temp);
9425 length = f64 ? 8 : 4;
9426 }
9427
9428 assert (length == (unsigned) (f64 ? 8 : 4));
9429
9430 if (*args == 'f'
9431 || (*args == 'l'
9432 && (! USE_GLOBAL_POINTER_OPT
9433 || mips_pic == EMBEDDED_PIC
9434 || g_switch_value < 4
9435 || (temp[0] == 0 && temp[1] == 0)
9436 || (temp[2] == 0 && temp[3] == 0))))
9437 {
9438 imm_expr.X_op = O_constant;
9439 if (! target_big_endian)
9440 imm_expr.X_add_number = bfd_getl32 (temp);
9441 else
9442 imm_expr.X_add_number = bfd_getb32 (temp);
9443 }
9444 else if (length > 4
9445 && ! mips_disable_float_construction
9446 /* Constants can only be constructed in GPRs and
9447 copied to FPRs if the GPRs are at least as wide
9448 as the FPRs. Force the constant into memory if
9449 we are using 64-bit FPRs but the GPRs are only
9450 32 bits wide. */
9451 && (using_gprs
9452 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
9453 && ((temp[0] == 0 && temp[1] == 0)
9454 || (temp[2] == 0 && temp[3] == 0))
9455 && ((temp[4] == 0 && temp[5] == 0)
9456 || (temp[6] == 0 && temp[7] == 0)))
9457 {
9458 /* The value is simple enough to load with a couple of
9459 instructions. If using 32-bit registers, set
9460 imm_expr to the high order 32 bits and offset_expr to
9461 the low order 32 bits. Otherwise, set imm_expr to
9462 the entire 64 bit constant. */
9463 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
9464 {
9465 imm_expr.X_op = O_constant;
9466 offset_expr.X_op = O_constant;
9467 if (! target_big_endian)
9468 {
9469 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9470 offset_expr.X_add_number = bfd_getl32 (temp);
9471 }
9472 else
9473 {
9474 imm_expr.X_add_number = bfd_getb32 (temp);
9475 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9476 }
9477 if (offset_expr.X_add_number == 0)
9478 offset_expr.X_op = O_absent;
9479 }
9480 else if (sizeof (imm_expr.X_add_number) > 4)
9481 {
9482 imm_expr.X_op = O_constant;
9483 if (! target_big_endian)
9484 imm_expr.X_add_number = bfd_getl64 (temp);
9485 else
9486 imm_expr.X_add_number = bfd_getb64 (temp);
9487 }
9488 else
9489 {
9490 imm_expr.X_op = O_big;
9491 imm_expr.X_add_number = 4;
9492 if (! target_big_endian)
9493 {
9494 generic_bignum[0] = bfd_getl16 (temp);
9495 generic_bignum[1] = bfd_getl16 (temp + 2);
9496 generic_bignum[2] = bfd_getl16 (temp + 4);
9497 generic_bignum[3] = bfd_getl16 (temp + 6);
9498 }
9499 else
9500 {
9501 generic_bignum[0] = bfd_getb16 (temp + 6);
9502 generic_bignum[1] = bfd_getb16 (temp + 4);
9503 generic_bignum[2] = bfd_getb16 (temp + 2);
9504 generic_bignum[3] = bfd_getb16 (temp);
9505 }
9506 }
9507 }
9508 else
9509 {
9510 const char *newname;
9511 segT new_seg;
9512
9513 /* Switch to the right section. */
9514 seg = now_seg;
9515 subseg = now_subseg;
9516 switch (*args)
9517 {
9518 default: /* unused default case avoids warnings. */
9519 case 'L':
9520 newname = RDATA_SECTION_NAME;
9521 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9522 || mips_pic == EMBEDDED_PIC)
9523 newname = ".lit8";
9524 break;
9525 case 'F':
9526 if (mips_pic == EMBEDDED_PIC)
9527 newname = ".lit8";
9528 else
9529 newname = RDATA_SECTION_NAME;
9530 break;
9531 case 'l':
9532 assert (!USE_GLOBAL_POINTER_OPT
9533 || g_switch_value >= 4);
9534 newname = ".lit4";
9535 break;
9536 }
9537 new_seg = subseg_new (newname, (subsegT) 0);
9538 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9539 bfd_set_section_flags (stdoutput, new_seg,
9540 (SEC_ALLOC
9541 | SEC_LOAD
9542 | SEC_READONLY
9543 | SEC_DATA));
9544 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9545 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9546 && strcmp (TARGET_OS, "elf") != 0)
9547 record_alignment (new_seg, 4);
9548 else
9549 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9550 if (seg == now_seg)
9551 as_bad (_("Can't use floating point insn in this section"));
9552
9553 /* Set the argument to the current address in the
9554 section. */
9555 offset_expr.X_op = O_symbol;
9556 offset_expr.X_add_symbol =
9557 symbol_new ("L0\001", now_seg,
9558 (valueT) frag_now_fix (), frag_now);
9559 offset_expr.X_add_number = 0;
9560
9561 /* Put the floating point number into the section. */
9562 p = frag_more ((int) length);
9563 memcpy (p, temp, length);
9564
9565 /* Switch back to the original section. */
9566 subseg_set (seg, subseg);
9567 }
9568 }
9569 continue;
9570
9571 case 'i': /* 16 bit unsigned immediate */
9572 case 'j': /* 16 bit signed immediate */
9573 *imm_reloc = BFD_RELOC_LO16;
9574 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
9575 {
9576 int more;
9577 offsetT minval, maxval;
9578
9579 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9580 && strcmp (insn->name, insn[1].name) == 0);
9581
9582 /* If the expression was written as an unsigned number,
9583 only treat it as signed if there are no more
9584 alternatives. */
9585 if (more
9586 && *args == 'j'
9587 && sizeof (imm_expr.X_add_number) <= 4
9588 && imm_expr.X_op == O_constant
9589 && imm_expr.X_add_number < 0
9590 && imm_expr.X_unsigned
9591 && HAVE_64BIT_GPRS)
9592 break;
9593
9594 /* For compatibility with older assemblers, we accept
9595 0x8000-0xffff as signed 16-bit numbers when only
9596 signed numbers are allowed. */
9597 if (*args == 'i')
9598 minval = 0, maxval = 0xffff;
9599 else if (more)
9600 minval = -0x8000, maxval = 0x7fff;
9601 else
9602 minval = -0x8000, maxval = 0xffff;
9603
9604 if (imm_expr.X_op != O_constant
9605 || imm_expr.X_add_number < minval
9606 || imm_expr.X_add_number > maxval)
9607 {
9608 if (more)
9609 break;
9610 if (imm_expr.X_op == O_constant
9611 || imm_expr.X_op == O_big)
9612 as_bad (_("expression out of range"));
9613 }
9614 }
9615 s = expr_end;
9616 continue;
9617
9618 case 'o': /* 16 bit offset */
9619 /* Check whether there is only a single bracketed expression
9620 left. If so, it must be the base register and the
9621 constant must be zero. */
9622 if (*s == '(' && strchr (s + 1, '(') == 0)
9623 {
9624 offset_expr.X_op = O_constant;
9625 offset_expr.X_add_number = 0;
9626 continue;
9627 }
9628
9629 /* If this value won't fit into a 16 bit offset, then go
9630 find a macro that will generate the 32 bit offset
9631 code pattern. */
9632 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
9633 && (offset_expr.X_op != O_constant
9634 || offset_expr.X_add_number >= 0x8000
9635 || offset_expr.X_add_number < -0x8000))
9636 break;
9637
9638 s = expr_end;
9639 continue;
9640
9641 case 'p': /* pc relative offset */
9642 *offset_reloc = BFD_RELOC_16_PCREL_S2;
9643 my_getExpression (&offset_expr, s);
9644 s = expr_end;
9645 continue;
9646
9647 case 'u': /* upper 16 bits */
9648 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9649 && imm_expr.X_op == O_constant
9650 && (imm_expr.X_add_number < 0
9651 || imm_expr.X_add_number >= 0x10000))
9652 as_bad (_("lui expression not in range 0..65535"));
9653 s = expr_end;
9654 continue;
9655
9656 case 'a': /* 26 bit address */
9657 my_getExpression (&offset_expr, s);
9658 s = expr_end;
9659 *offset_reloc = BFD_RELOC_MIPS_JMP;
9660 continue;
9661
9662 case 'N': /* 3 bit branch condition code */
9663 case 'M': /* 3 bit compare condition code */
9664 if (strncmp (s, "$fcc", 4) != 0)
9665 break;
9666 s += 4;
9667 regno = 0;
9668 do
9669 {
9670 regno *= 10;
9671 regno += *s - '0';
9672 ++s;
9673 }
9674 while (ISDIGIT (*s));
9675 if (regno > 7)
9676 as_bad (_("invalid condition code register $fcc%d"), regno);
9677 if (*args == 'N')
9678 ip->insn_opcode |= regno << OP_SH_BCC;
9679 else
9680 ip->insn_opcode |= regno << OP_SH_CCC;
9681 continue;
9682
9683 case 'H':
9684 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9685 s += 2;
9686 if (ISDIGIT (*s))
9687 {
9688 c = 0;
9689 do
9690 {
9691 c *= 10;
9692 c += *s - '0';
9693 ++s;
9694 }
9695 while (ISDIGIT (*s));
9696 }
9697 else
9698 c = 8; /* Invalid sel value. */
9699
9700 if (c > 7)
9701 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9702 ip->insn_opcode |= c;
9703 continue;
9704
9705 case 'e':
9706 /* Must be at least one digit. */
9707 my_getExpression (&imm_expr, s);
9708 check_absolute_expr (ip, &imm_expr);
9709
9710 if ((unsigned long) imm_expr.X_add_number
9711 > (unsigned long) OP_MASK_VECBYTE)
9712 {
9713 as_bad (_("bad byte vector index (%ld)"),
9714 (long) imm_expr.X_add_number);
9715 imm_expr.X_add_number = 0;
9716 }
9717
9718 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9719 imm_expr.X_op = O_absent;
9720 s = expr_end;
9721 continue;
9722
9723 case '%':
9724 my_getExpression (&imm_expr, s);
9725 check_absolute_expr (ip, &imm_expr);
9726
9727 if ((unsigned long) imm_expr.X_add_number
9728 > (unsigned long) OP_MASK_VECALIGN)
9729 {
9730 as_bad (_("bad byte vector index (%ld)"),
9731 (long) imm_expr.X_add_number);
9732 imm_expr.X_add_number = 0;
9733 }
9734
9735 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9736 imm_expr.X_op = O_absent;
9737 s = expr_end;
9738 continue;
9739
9740 default:
9741 as_bad (_("bad char = '%c'\n"), *args);
9742 internalError ();
9743 }
9744 break;
9745 }
9746 /* Args don't match. */
9747 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9748 !strcmp (insn->name, insn[1].name))
9749 {
9750 ++insn;
9751 s = argsStart;
9752 insn_error = _("illegal operands");
9753 continue;
9754 }
9755 if (save_c)
9756 *(--s) = save_c;
9757 insn_error = _("illegal operands");
9758 return;
9759 }
9760 }
9761
9762 /* This routine assembles an instruction into its binary format when
9763 assembling for the mips16. As a side effect, it sets one of the
9764 global variables imm_reloc or offset_reloc to the type of
9765 relocation to do if one of the operands is an address expression.
9766 It also sets mips16_small and mips16_ext if the user explicitly
9767 requested a small or extended instruction. */
9768
9769 static void
9770 mips16_ip (str, ip)
9771 char *str;
9772 struct mips_cl_insn *ip;
9773 {
9774 char *s;
9775 const char *args;
9776 struct mips_opcode *insn;
9777 char *argsstart;
9778 unsigned int regno;
9779 unsigned int lastregno = 0;
9780 char *s_reset;
9781
9782 insn_error = NULL;
9783
9784 mips16_small = FALSE;
9785 mips16_ext = FALSE;
9786
9787 for (s = str; ISLOWER (*s); ++s)
9788 ;
9789 switch (*s)
9790 {
9791 case '\0':
9792 break;
9793
9794 case ' ':
9795 *s++ = '\0';
9796 break;
9797
9798 case '.':
9799 if (s[1] == 't' && s[2] == ' ')
9800 {
9801 *s = '\0';
9802 mips16_small = TRUE;
9803 s += 3;
9804 break;
9805 }
9806 else if (s[1] == 'e' && s[2] == ' ')
9807 {
9808 *s = '\0';
9809 mips16_ext = TRUE;
9810 s += 3;
9811 break;
9812 }
9813 /* Fall through. */
9814 default:
9815 insn_error = _("unknown opcode");
9816 return;
9817 }
9818
9819 if (mips_opts.noautoextend && ! mips16_ext)
9820 mips16_small = TRUE;
9821
9822 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9823 {
9824 insn_error = _("unrecognized opcode");
9825 return;
9826 }
9827
9828 argsstart = s;
9829 for (;;)
9830 {
9831 assert (strcmp (insn->name, str) == 0);
9832
9833 ip->insn_mo = insn;
9834 ip->insn_opcode = insn->match;
9835 ip->use_extend = FALSE;
9836 imm_expr.X_op = O_absent;
9837 imm_reloc[0] = BFD_RELOC_UNUSED;
9838 imm_reloc[1] = BFD_RELOC_UNUSED;
9839 imm_reloc[2] = BFD_RELOC_UNUSED;
9840 offset_expr.X_op = O_absent;
9841 offset_reloc[0] = BFD_RELOC_UNUSED;
9842 offset_reloc[1] = BFD_RELOC_UNUSED;
9843 offset_reloc[2] = BFD_RELOC_UNUSED;
9844 for (args = insn->args; 1; ++args)
9845 {
9846 int c;
9847
9848 if (*s == ' ')
9849 ++s;
9850
9851 /* In this switch statement we call break if we did not find
9852 a match, continue if we did find a match, or return if we
9853 are done. */
9854
9855 c = *args;
9856 switch (c)
9857 {
9858 case '\0':
9859 if (*s == '\0')
9860 {
9861 /* Stuff the immediate value in now, if we can. */
9862 if (imm_expr.X_op == O_constant
9863 && *imm_reloc > BFD_RELOC_UNUSED
9864 && insn->pinfo != INSN_MACRO)
9865 {
9866 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
9867 imm_expr.X_add_number, TRUE, mips16_small,
9868 mips16_ext, &ip->insn_opcode,
9869 &ip->use_extend, &ip->extend);
9870 imm_expr.X_op = O_absent;
9871 *imm_reloc = BFD_RELOC_UNUSED;
9872 }
9873
9874 return;
9875 }
9876 break;
9877
9878 case ',':
9879 if (*s++ == c)
9880 continue;
9881 s--;
9882 switch (*++args)
9883 {
9884 case 'v':
9885 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9886 continue;
9887 case 'w':
9888 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9889 continue;
9890 }
9891 break;
9892
9893 case '(':
9894 case ')':
9895 if (*s++ == c)
9896 continue;
9897 break;
9898
9899 case 'v':
9900 case 'w':
9901 if (s[0] != '$')
9902 {
9903 if (c == 'v')
9904 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9905 else
9906 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9907 ++args;
9908 continue;
9909 }
9910 /* Fall through. */
9911 case 'x':
9912 case 'y':
9913 case 'z':
9914 case 'Z':
9915 case '0':
9916 case 'S':
9917 case 'R':
9918 case 'X':
9919 case 'Y':
9920 if (s[0] != '$')
9921 break;
9922 s_reset = s;
9923 if (ISDIGIT (s[1]))
9924 {
9925 ++s;
9926 regno = 0;
9927 do
9928 {
9929 regno *= 10;
9930 regno += *s - '0';
9931 ++s;
9932 }
9933 while (ISDIGIT (*s));
9934 if (regno > 31)
9935 {
9936 as_bad (_("invalid register number (%d)"), regno);
9937 regno = 2;
9938 }
9939 }
9940 else
9941 {
9942 if (s[1] == 'r' && s[2] == 'a')
9943 {
9944 s += 3;
9945 regno = RA;
9946 }
9947 else if (s[1] == 'f' && s[2] == 'p')
9948 {
9949 s += 3;
9950 regno = FP;
9951 }
9952 else if (s[1] == 's' && s[2] == 'p')
9953 {
9954 s += 3;
9955 regno = SP;
9956 }
9957 else if (s[1] == 'g' && s[2] == 'p')
9958 {
9959 s += 3;
9960 regno = GP;
9961 }
9962 else if (s[1] == 'a' && s[2] == 't')
9963 {
9964 s += 3;
9965 regno = AT;
9966 }
9967 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9968 {
9969 s += 4;
9970 regno = KT0;
9971 }
9972 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9973 {
9974 s += 4;
9975 regno = KT1;
9976 }
9977 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9978 {
9979 s += 5;
9980 regno = ZERO;
9981 }
9982 else
9983 break;
9984 }
9985
9986 if (*s == ' ')
9987 ++s;
9988 if (args[1] != *s)
9989 {
9990 if (c == 'v' || c == 'w')
9991 {
9992 regno = mips16_to_32_reg_map[lastregno];
9993 s = s_reset;
9994 ++args;
9995 }
9996 }
9997
9998 switch (c)
9999 {
10000 case 'x':
10001 case 'y':
10002 case 'z':
10003 case 'v':
10004 case 'w':
10005 case 'Z':
10006 regno = mips32_to_16_reg_map[regno];
10007 break;
10008
10009 case '0':
10010 if (regno != 0)
10011 regno = ILLEGAL_REG;
10012 break;
10013
10014 case 'S':
10015 if (regno != SP)
10016 regno = ILLEGAL_REG;
10017 break;
10018
10019 case 'R':
10020 if (regno != RA)
10021 regno = ILLEGAL_REG;
10022 break;
10023
10024 case 'X':
10025 case 'Y':
10026 if (regno == AT && ! mips_opts.noat)
10027 as_warn (_("used $at without \".set noat\""));
10028 break;
10029
10030 default:
10031 internalError ();
10032 }
10033
10034 if (regno == ILLEGAL_REG)
10035 break;
10036
10037 switch (c)
10038 {
10039 case 'x':
10040 case 'v':
10041 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
10042 break;
10043 case 'y':
10044 case 'w':
10045 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
10046 break;
10047 case 'z':
10048 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
10049 break;
10050 case 'Z':
10051 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
10052 case '0':
10053 case 'S':
10054 case 'R':
10055 break;
10056 case 'X':
10057 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
10058 break;
10059 case 'Y':
10060 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
10061 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
10062 break;
10063 default:
10064 internalError ();
10065 }
10066
10067 lastregno = regno;
10068 continue;
10069
10070 case 'P':
10071 if (strncmp (s, "$pc", 3) == 0)
10072 {
10073 s += 3;
10074 continue;
10075 }
10076 break;
10077
10078 case '<':
10079 case '>':
10080 case '[':
10081 case ']':
10082 case '4':
10083 case '5':
10084 case 'H':
10085 case 'W':
10086 case 'D':
10087 case 'j':
10088 case '8':
10089 case 'V':
10090 case 'C':
10091 case 'U':
10092 case 'k':
10093 case 'K':
10094 if (s[0] == '%'
10095 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
10096 {
10097 /* This is %gprel(SYMBOL). We need to read SYMBOL,
10098 and generate the appropriate reloc. If the text
10099 inside %gprel is not a symbol name with an
10100 optional offset, then we generate a normal reloc
10101 and will probably fail later. */
10102 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
10103 if (imm_expr.X_op == O_symbol)
10104 {
10105 mips16_ext = TRUE;
10106 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
10107 s = expr_end;
10108 ip->use_extend = TRUE;
10109 ip->extend = 0;
10110 continue;
10111 }
10112 }
10113 else
10114 {
10115 /* Just pick up a normal expression. */
10116 my_getExpression (&imm_expr, s);
10117 }
10118
10119 if (imm_expr.X_op == O_register)
10120 {
10121 /* What we thought was an expression turned out to
10122 be a register. */
10123
10124 if (s[0] == '(' && args[1] == '(')
10125 {
10126 /* It looks like the expression was omitted
10127 before a register indirection, which means
10128 that the expression is implicitly zero. We
10129 still set up imm_expr, so that we handle
10130 explicit extensions correctly. */
10131 imm_expr.X_op = O_constant;
10132 imm_expr.X_add_number = 0;
10133 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10134 continue;
10135 }
10136
10137 break;
10138 }
10139
10140 /* We need to relax this instruction. */
10141 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
10142 s = expr_end;
10143 continue;
10144
10145 case 'p':
10146 case 'q':
10147 case 'A':
10148 case 'B':
10149 case 'E':
10150 /* We use offset_reloc rather than imm_reloc for the PC
10151 relative operands. This lets macros with both
10152 immediate and address operands work correctly. */
10153 my_getExpression (&offset_expr, s);
10154
10155 if (offset_expr.X_op == O_register)
10156 break;
10157
10158 /* We need to relax this instruction. */
10159 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
10160 s = expr_end;
10161 continue;
10162
10163 case '6': /* break code */
10164 my_getExpression (&imm_expr, s);
10165 check_absolute_expr (ip, &imm_expr);
10166 if ((unsigned long) imm_expr.X_add_number > 63)
10167 {
10168 as_warn (_("Invalid value for `%s' (%lu)"),
10169 ip->insn_mo->name,
10170 (unsigned long) imm_expr.X_add_number);
10171 imm_expr.X_add_number &= 0x3f;
10172 }
10173 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
10174 imm_expr.X_op = O_absent;
10175 s = expr_end;
10176 continue;
10177
10178 case 'a': /* 26 bit address */
10179 my_getExpression (&offset_expr, s);
10180 s = expr_end;
10181 *offset_reloc = BFD_RELOC_MIPS16_JMP;
10182 ip->insn_opcode <<= 16;
10183 continue;
10184
10185 case 'l': /* register list for entry macro */
10186 case 'L': /* register list for exit macro */
10187 {
10188 int mask;
10189
10190 if (c == 'l')
10191 mask = 0;
10192 else
10193 mask = 7 << 3;
10194 while (*s != '\0')
10195 {
10196 int freg, reg1, reg2;
10197
10198 while (*s == ' ' || *s == ',')
10199 ++s;
10200 if (*s != '$')
10201 {
10202 as_bad (_("can't parse register list"));
10203 break;
10204 }
10205 ++s;
10206 if (*s != 'f')
10207 freg = 0;
10208 else
10209 {
10210 freg = 1;
10211 ++s;
10212 }
10213 reg1 = 0;
10214 while (ISDIGIT (*s))
10215 {
10216 reg1 *= 10;
10217 reg1 += *s - '0';
10218 ++s;
10219 }
10220 if (*s == ' ')
10221 ++s;
10222 if (*s != '-')
10223 reg2 = reg1;
10224 else
10225 {
10226 ++s;
10227 if (*s != '$')
10228 break;
10229 ++s;
10230 if (freg)
10231 {
10232 if (*s == 'f')
10233 ++s;
10234 else
10235 {
10236 as_bad (_("invalid register list"));
10237 break;
10238 }
10239 }
10240 reg2 = 0;
10241 while (ISDIGIT (*s))
10242 {
10243 reg2 *= 10;
10244 reg2 += *s - '0';
10245 ++s;
10246 }
10247 }
10248 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10249 {
10250 mask &= ~ (7 << 3);
10251 mask |= 5 << 3;
10252 }
10253 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10254 {
10255 mask &= ~ (7 << 3);
10256 mask |= 6 << 3;
10257 }
10258 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10259 mask |= (reg2 - 3) << 3;
10260 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10261 mask |= (reg2 - 15) << 1;
10262 else if (reg1 == RA && reg2 == RA)
10263 mask |= 1;
10264 else
10265 {
10266 as_bad (_("invalid register list"));
10267 break;
10268 }
10269 }
10270 /* The mask is filled in in the opcode table for the
10271 benefit of the disassembler. We remove it before
10272 applying the actual mask. */
10273 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10274 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10275 }
10276 continue;
10277
10278 case 'e': /* extend code */
10279 my_getExpression (&imm_expr, s);
10280 check_absolute_expr (ip, &imm_expr);
10281 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10282 {
10283 as_warn (_("Invalid value for `%s' (%lu)"),
10284 ip->insn_mo->name,
10285 (unsigned long) imm_expr.X_add_number);
10286 imm_expr.X_add_number &= 0x7ff;
10287 }
10288 ip->insn_opcode |= imm_expr.X_add_number;
10289 imm_expr.X_op = O_absent;
10290 s = expr_end;
10291 continue;
10292
10293 default:
10294 internalError ();
10295 }
10296 break;
10297 }
10298
10299 /* Args don't match. */
10300 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10301 strcmp (insn->name, insn[1].name) == 0)
10302 {
10303 ++insn;
10304 s = argsstart;
10305 continue;
10306 }
10307
10308 insn_error = _("illegal operands");
10309
10310 return;
10311 }
10312 }
10313
10314 /* This structure holds information we know about a mips16 immediate
10315 argument type. */
10316
10317 struct mips16_immed_operand
10318 {
10319 /* The type code used in the argument string in the opcode table. */
10320 int type;
10321 /* The number of bits in the short form of the opcode. */
10322 int nbits;
10323 /* The number of bits in the extended form of the opcode. */
10324 int extbits;
10325 /* The amount by which the short form is shifted when it is used;
10326 for example, the sw instruction has a shift count of 2. */
10327 int shift;
10328 /* The amount by which the short form is shifted when it is stored
10329 into the instruction code. */
10330 int op_shift;
10331 /* Non-zero if the short form is unsigned. */
10332 int unsp;
10333 /* Non-zero if the extended form is unsigned. */
10334 int extu;
10335 /* Non-zero if the value is PC relative. */
10336 int pcrel;
10337 };
10338
10339 /* The mips16 immediate operand types. */
10340
10341 static const struct mips16_immed_operand mips16_immed_operands[] =
10342 {
10343 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10344 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10345 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10346 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10347 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10348 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10349 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10350 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10351 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10352 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10353 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10354 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10355 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10356 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10357 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10358 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10359 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10360 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10361 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10362 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10363 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10364 };
10365
10366 #define MIPS16_NUM_IMMED \
10367 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10368
10369 /* Handle a mips16 instruction with an immediate value. This or's the
10370 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10371 whether an extended value is needed; if one is needed, it sets
10372 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10373 If SMALL is true, an unextended opcode was explicitly requested.
10374 If EXT is true, an extended opcode was explicitly requested. If
10375 WARN is true, warn if EXT does not match reality. */
10376
10377 static void
10378 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
10379 extend)
10380 char *file;
10381 unsigned int line;
10382 int type;
10383 offsetT val;
10384 bfd_boolean warn;
10385 bfd_boolean small;
10386 bfd_boolean ext;
10387 unsigned long *insn;
10388 bfd_boolean *use_extend;
10389 unsigned short *extend;
10390 {
10391 register const struct mips16_immed_operand *op;
10392 int mintiny, maxtiny;
10393 bfd_boolean needext;
10394
10395 op = mips16_immed_operands;
10396 while (op->type != type)
10397 {
10398 ++op;
10399 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10400 }
10401
10402 if (op->unsp)
10403 {
10404 if (type == '<' || type == '>' || type == '[' || type == ']')
10405 {
10406 mintiny = 1;
10407 maxtiny = 1 << op->nbits;
10408 }
10409 else
10410 {
10411 mintiny = 0;
10412 maxtiny = (1 << op->nbits) - 1;
10413 }
10414 }
10415 else
10416 {
10417 mintiny = - (1 << (op->nbits - 1));
10418 maxtiny = (1 << (op->nbits - 1)) - 1;
10419 }
10420
10421 /* Branch offsets have an implicit 0 in the lowest bit. */
10422 if (type == 'p' || type == 'q')
10423 val /= 2;
10424
10425 if ((val & ((1 << op->shift) - 1)) != 0
10426 || val < (mintiny << op->shift)
10427 || val > (maxtiny << op->shift))
10428 needext = TRUE;
10429 else
10430 needext = FALSE;
10431
10432 if (warn && ext && ! needext)
10433 as_warn_where (file, line,
10434 _("extended operand requested but not required"));
10435 if (small && needext)
10436 as_bad_where (file, line, _("invalid unextended operand value"));
10437
10438 if (small || (! ext && ! needext))
10439 {
10440 int insnval;
10441
10442 *use_extend = FALSE;
10443 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10444 insnval <<= op->op_shift;
10445 *insn |= insnval;
10446 }
10447 else
10448 {
10449 long minext, maxext;
10450 int extval;
10451
10452 if (op->extu)
10453 {
10454 minext = 0;
10455 maxext = (1 << op->extbits) - 1;
10456 }
10457 else
10458 {
10459 minext = - (1 << (op->extbits - 1));
10460 maxext = (1 << (op->extbits - 1)) - 1;
10461 }
10462 if (val < minext || val > maxext)
10463 as_bad_where (file, line,
10464 _("operand value out of range for instruction"));
10465
10466 *use_extend = TRUE;
10467 if (op->extbits == 16)
10468 {
10469 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10470 val &= 0x1f;
10471 }
10472 else if (op->extbits == 15)
10473 {
10474 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10475 val &= 0xf;
10476 }
10477 else
10478 {
10479 extval = ((val & 0x1f) << 6) | (val & 0x20);
10480 val = 0;
10481 }
10482
10483 *extend = (unsigned short) extval;
10484 *insn |= val;
10485 }
10486 }
10487 \f
10488 static const struct percent_op_match
10489 {
10490 const char *str;
10491 bfd_reloc_code_real_type reloc;
10492 } percent_op[] =
10493 {
10494 {"%lo", BFD_RELOC_LO16},
10495 #ifdef OBJ_ELF
10496 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10497 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10498 {"%call16", BFD_RELOC_MIPS_CALL16},
10499 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10500 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10501 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10502 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10503 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10504 {"%got", BFD_RELOC_MIPS_GOT16},
10505 {"%gp_rel", BFD_RELOC_GPREL16},
10506 {"%half", BFD_RELOC_16},
10507 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10508 {"%higher", BFD_RELOC_MIPS_HIGHER},
10509 {"%neg", BFD_RELOC_MIPS_SUB},
10510 #endif
10511 {"%hi", BFD_RELOC_HI16_S}
10512 };
10513
10514
10515 /* Return true if *STR points to a relocation operator. When returning true,
10516 move *STR over the operator and store its relocation code in *RELOC.
10517 Leave both *STR and *RELOC alone when returning false. */
10518
10519 static bfd_boolean
10520 parse_relocation (str, reloc)
10521 char **str;
10522 bfd_reloc_code_real_type *reloc;
10523 {
10524 size_t i;
10525
10526 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10527 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
10528 {
10529 *str += strlen (percent_op[i].str);
10530 *reloc = percent_op[i].reloc;
10531
10532 /* Check whether the output BFD supports this relocation.
10533 If not, issue an error and fall back on something safe. */
10534 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
10535 {
10536 as_bad ("relocation %s isn't supported by the current ABI",
10537 percent_op[i].str);
10538 *reloc = BFD_RELOC_LO16;
10539 }
10540 return TRUE;
10541 }
10542 return FALSE;
10543 }
10544
10545
10546 /* Parse string STR as a 16-bit relocatable operand. Store the
10547 expression in *EP and the relocations in the array starting
10548 at RELOC. Return the number of relocation operators used.
10549
10550 On exit, EXPR_END points to the first character after the expression.
10551 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
10552
10553 static size_t
10554 my_getSmallExpression (ep, reloc, str)
10555 expressionS *ep;
10556 bfd_reloc_code_real_type *reloc;
10557 char *str;
10558 {
10559 bfd_reloc_code_real_type reversed_reloc[3];
10560 size_t reloc_index, i;
10561 int crux_depth, str_depth;
10562 char *crux;
10563
10564 /* Search for the start of the main expression, recoding relocations
10565 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10566 of the main expression and with CRUX_DEPTH containing the number
10567 of open brackets at that point. */
10568 reloc_index = -1;
10569 str_depth = 0;
10570 do
10571 {
10572 reloc_index++;
10573 crux = str;
10574 crux_depth = str_depth;
10575
10576 /* Skip over whitespace and brackets, keeping count of the number
10577 of brackets. */
10578 while (*str == ' ' || *str == '\t' || *str == '(')
10579 if (*str++ == '(')
10580 str_depth++;
10581 }
10582 while (*str == '%'
10583 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10584 && parse_relocation (&str, &reversed_reloc[reloc_index]));
10585
10586 my_getExpression (ep, crux);
10587 str = expr_end;
10588
10589 /* Match every open bracket. */
10590 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
10591 if (*str++ == ')')
10592 crux_depth--;
10593
10594 if (crux_depth > 0)
10595 as_bad ("unclosed '('");
10596
10597 expr_end = str;
10598
10599 if (reloc_index == 0)
10600 reloc[0] = BFD_RELOC_LO16;
10601 else
10602 {
10603 prev_reloc_op_frag = frag_now;
10604 for (i = 0; i < reloc_index; i++)
10605 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10606 }
10607
10608 return reloc_index;
10609 }
10610
10611 static void
10612 my_getExpression (ep, str)
10613 expressionS *ep;
10614 char *str;
10615 {
10616 char *save_in;
10617 valueT val;
10618
10619 save_in = input_line_pointer;
10620 input_line_pointer = str;
10621 expression (ep);
10622 expr_end = input_line_pointer;
10623 input_line_pointer = save_in;
10624
10625 /* If we are in mips16 mode, and this is an expression based on `.',
10626 then we bump the value of the symbol by 1 since that is how other
10627 text symbols are handled. We don't bother to handle complex
10628 expressions, just `.' plus or minus a constant. */
10629 if (mips_opts.mips16
10630 && ep->X_op == O_symbol
10631 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10632 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
10633 && symbol_get_frag (ep->X_add_symbol) == frag_now
10634 && symbol_constant_p (ep->X_add_symbol)
10635 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10636 S_SET_VALUE (ep->X_add_symbol, val + 1);
10637 }
10638
10639 /* Turn a string in input_line_pointer into a floating point constant
10640 of type TYPE, and store the appropriate bytes in *LITP. The number
10641 of LITTLENUMS emitted is stored in *SIZEP. An error message is
10642 returned, or NULL on OK. */
10643
10644 char *
10645 md_atof (type, litP, sizeP)
10646 int type;
10647 char *litP;
10648 int *sizeP;
10649 {
10650 int prec;
10651 LITTLENUM_TYPE words[4];
10652 char *t;
10653 int i;
10654
10655 switch (type)
10656 {
10657 case 'f':
10658 prec = 2;
10659 break;
10660
10661 case 'd':
10662 prec = 4;
10663 break;
10664
10665 default:
10666 *sizeP = 0;
10667 return _("bad call to md_atof");
10668 }
10669
10670 t = atof_ieee (input_line_pointer, type, words);
10671 if (t)
10672 input_line_pointer = t;
10673
10674 *sizeP = prec * 2;
10675
10676 if (! target_big_endian)
10677 {
10678 for (i = prec - 1; i >= 0; i--)
10679 {
10680 md_number_to_chars (litP, (valueT) words[i], 2);
10681 litP += 2;
10682 }
10683 }
10684 else
10685 {
10686 for (i = 0; i < prec; i++)
10687 {
10688 md_number_to_chars (litP, (valueT) words[i], 2);
10689 litP += 2;
10690 }
10691 }
10692
10693 return NULL;
10694 }
10695
10696 void
10697 md_number_to_chars (buf, val, n)
10698 char *buf;
10699 valueT val;
10700 int n;
10701 {
10702 if (target_big_endian)
10703 number_to_chars_bigendian (buf, val, n);
10704 else
10705 number_to_chars_littleendian (buf, val, n);
10706 }
10707 \f
10708 #ifdef OBJ_ELF
10709 static int support_64bit_objects(void)
10710 {
10711 const char **list, **l;
10712 int yes;
10713
10714 list = bfd_target_list ();
10715 for (l = list; *l != NULL; l++)
10716 #ifdef TE_TMIPS
10717 /* This is traditional mips */
10718 if (strcmp (*l, "elf64-tradbigmips") == 0
10719 || strcmp (*l, "elf64-tradlittlemips") == 0)
10720 #else
10721 if (strcmp (*l, "elf64-bigmips") == 0
10722 || strcmp (*l, "elf64-littlemips") == 0)
10723 #endif
10724 break;
10725 yes = (*l != NULL);
10726 free (list);
10727 return yes;
10728 }
10729 #endif /* OBJ_ELF */
10730
10731 const char *md_shortopts = "nO::g::G:";
10732
10733 struct option md_longopts[] =
10734 {
10735 /* Options which specify architecture. */
10736 #define OPTION_ARCH_BASE (OPTION_MD_BASE)
10737 #define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10738 {"march", required_argument, NULL, OPTION_MARCH},
10739 #define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10740 {"mtune", required_argument, NULL, OPTION_MTUNE},
10741 #define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
10742 {"mips0", no_argument, NULL, OPTION_MIPS1},
10743 {"mips1", no_argument, NULL, OPTION_MIPS1},
10744 #define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
10745 {"mips2", no_argument, NULL, OPTION_MIPS2},
10746 #define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
10747 {"mips3", no_argument, NULL, OPTION_MIPS3},
10748 #define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
10749 {"mips4", no_argument, NULL, OPTION_MIPS4},
10750 #define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
10751 {"mips5", no_argument, NULL, OPTION_MIPS5},
10752 #define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
10753 {"mips32", no_argument, NULL, OPTION_MIPS32},
10754 #define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
10755 {"mips64", no_argument, NULL, OPTION_MIPS64},
10756 #define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10757 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
10758
10759 /* Options which specify Application Specific Extensions (ASEs). */
10760 #define OPTION_ASE_BASE (OPTION_ARCH_BASE + 10)
10761 #define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10762 {"mips16", no_argument, NULL, OPTION_MIPS16},
10763 #define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10764 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10765 #define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10766 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10767 #define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10768 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10769 #define OPTION_MDMX (OPTION_ASE_BASE + 4)
10770 {"mdmx", no_argument, NULL, OPTION_MDMX},
10771 #define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10772 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10773
10774 /* Old-style architecture options. Don't add more of these. */
10775 #define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10776 #define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10777 {"m4650", no_argument, NULL, OPTION_M4650},
10778 #define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10779 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10780 #define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10781 {"m4010", no_argument, NULL, OPTION_M4010},
10782 #define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10783 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10784 #define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10785 {"m4100", no_argument, NULL, OPTION_M4100},
10786 #define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10787 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10788 #define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10789 {"m3900", no_argument, NULL, OPTION_M3900},
10790 #define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10791 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10792
10793 /* Options which enable bug fixes. */
10794 #define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10795 #define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10796 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10797 #define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10798 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10799 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10800 #define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10801 #define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10802 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10803 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10804
10805 /* Miscellaneous options. */
10806 #define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10807 #define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
10808 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
10809 #define OPTION_TRAP (OPTION_MISC_BASE + 1)
10810 {"trap", no_argument, NULL, OPTION_TRAP},
10811 {"no-break", no_argument, NULL, OPTION_TRAP},
10812 #define OPTION_BREAK (OPTION_MISC_BASE + 2)
10813 {"break", no_argument, NULL, OPTION_BREAK},
10814 {"no-trap", no_argument, NULL, OPTION_BREAK},
10815 #define OPTION_EB (OPTION_MISC_BASE + 3)
10816 {"EB", no_argument, NULL, OPTION_EB},
10817 #define OPTION_EL (OPTION_MISC_BASE + 4)
10818 {"EL", no_argument, NULL, OPTION_EL},
10819 #define OPTION_FP32 (OPTION_MISC_BASE + 5)
10820 {"mfp32", no_argument, NULL, OPTION_FP32},
10821 #define OPTION_GP32 (OPTION_MISC_BASE + 6)
10822 {"mgp32", no_argument, NULL, OPTION_GP32},
10823 #define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
10824 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
10825 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
10826 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
10827 #define OPTION_FP64 (OPTION_MISC_BASE + 9)
10828 {"mfp64", no_argument, NULL, OPTION_FP64},
10829 #define OPTION_GP64 (OPTION_MISC_BASE + 10)
10830 {"mgp64", no_argument, NULL, OPTION_GP64},
10831 #define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10832 #define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
10833 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10834 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
10835
10836 /* ELF-specific options. */
10837 #ifdef OBJ_ELF
10838 #define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
10839 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
10840 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10841 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
10842 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
10843 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
10844 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
10845 {"xgot", no_argument, NULL, OPTION_XGOT},
10846 #define OPTION_MABI (OPTION_ELF_BASE + 3)
10847 {"mabi", required_argument, NULL, OPTION_MABI},
10848 #define OPTION_32 (OPTION_ELF_BASE + 4)
10849 {"32", no_argument, NULL, OPTION_32},
10850 #define OPTION_N32 (OPTION_ELF_BASE + 5)
10851 {"n32", no_argument, NULL, OPTION_N32},
10852 #define OPTION_64 (OPTION_ELF_BASE + 6)
10853 {"64", no_argument, NULL, OPTION_64},
10854 #define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10855 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10856 #define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10857 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
10858 #endif /* OBJ_ELF */
10859
10860 {NULL, no_argument, NULL, 0}
10861 };
10862 size_t md_longopts_size = sizeof (md_longopts);
10863
10864 /* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10865 NEW_VALUE. Warn if another value was already specified. Note:
10866 we have to defer parsing the -march and -mtune arguments in order
10867 to handle 'from-abi' correctly, since the ABI might be specified
10868 in a later argument. */
10869
10870 static void
10871 mips_set_option_string (string_ptr, new_value)
10872 const char **string_ptr, *new_value;
10873 {
10874 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10875 as_warn (_("A different %s was already specified, is now %s"),
10876 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10877 new_value);
10878
10879 *string_ptr = new_value;
10880 }
10881
10882 int
10883 md_parse_option (c, arg)
10884 int c;
10885 char *arg;
10886 {
10887 switch (c)
10888 {
10889 case OPTION_CONSTRUCT_FLOATS:
10890 mips_disable_float_construction = 0;
10891 break;
10892
10893 case OPTION_NO_CONSTRUCT_FLOATS:
10894 mips_disable_float_construction = 1;
10895 break;
10896
10897 case OPTION_TRAP:
10898 mips_trap = 1;
10899 break;
10900
10901 case OPTION_BREAK:
10902 mips_trap = 0;
10903 break;
10904
10905 case OPTION_EB:
10906 target_big_endian = 1;
10907 break;
10908
10909 case OPTION_EL:
10910 target_big_endian = 0;
10911 break;
10912
10913 case 'n':
10914 warn_nops = 1;
10915 break;
10916
10917 case 'O':
10918 if (arg && arg[1] == '0')
10919 mips_optimize = 1;
10920 else
10921 mips_optimize = 2;
10922 break;
10923
10924 case 'g':
10925 if (arg == NULL)
10926 mips_debug = 2;
10927 else
10928 mips_debug = atoi (arg);
10929 /* When the MIPS assembler sees -g or -g2, it does not do
10930 optimizations which limit full symbolic debugging. We take
10931 that to be equivalent to -O0. */
10932 if (mips_debug == 2)
10933 mips_optimize = 1;
10934 break;
10935
10936 case OPTION_MIPS1:
10937 file_mips_isa = ISA_MIPS1;
10938 break;
10939
10940 case OPTION_MIPS2:
10941 file_mips_isa = ISA_MIPS2;
10942 break;
10943
10944 case OPTION_MIPS3:
10945 file_mips_isa = ISA_MIPS3;
10946 break;
10947
10948 case OPTION_MIPS4:
10949 file_mips_isa = ISA_MIPS4;
10950 break;
10951
10952 case OPTION_MIPS5:
10953 file_mips_isa = ISA_MIPS5;
10954 break;
10955
10956 case OPTION_MIPS32:
10957 file_mips_isa = ISA_MIPS32;
10958 break;
10959
10960 case OPTION_MIPS32R2:
10961 file_mips_isa = ISA_MIPS32R2;
10962 break;
10963
10964 case OPTION_MIPS64:
10965 file_mips_isa = ISA_MIPS64;
10966 break;
10967
10968 case OPTION_MTUNE:
10969 mips_set_option_string (&mips_tune_string, arg);
10970 break;
10971
10972 case OPTION_MARCH:
10973 mips_set_option_string (&mips_arch_string, arg);
10974 break;
10975
10976 case OPTION_M4650:
10977 mips_set_option_string (&mips_arch_string, "4650");
10978 mips_set_option_string (&mips_tune_string, "4650");
10979 break;
10980
10981 case OPTION_NO_M4650:
10982 break;
10983
10984 case OPTION_M4010:
10985 mips_set_option_string (&mips_arch_string, "4010");
10986 mips_set_option_string (&mips_tune_string, "4010");
10987 break;
10988
10989 case OPTION_NO_M4010:
10990 break;
10991
10992 case OPTION_M4100:
10993 mips_set_option_string (&mips_arch_string, "4100");
10994 mips_set_option_string (&mips_tune_string, "4100");
10995 break;
10996
10997 case OPTION_NO_M4100:
10998 break;
10999
11000 case OPTION_M3900:
11001 mips_set_option_string (&mips_arch_string, "3900");
11002 mips_set_option_string (&mips_tune_string, "3900");
11003 break;
11004
11005 case OPTION_NO_M3900:
11006 break;
11007
11008 case OPTION_MDMX:
11009 mips_opts.ase_mdmx = 1;
11010 break;
11011
11012 case OPTION_NO_MDMX:
11013 mips_opts.ase_mdmx = 0;
11014 break;
11015
11016 case OPTION_MIPS16:
11017 mips_opts.mips16 = 1;
11018 mips_no_prev_insn (FALSE);
11019 break;
11020
11021 case OPTION_NO_MIPS16:
11022 mips_opts.mips16 = 0;
11023 mips_no_prev_insn (FALSE);
11024 break;
11025
11026 case OPTION_MIPS3D:
11027 mips_opts.ase_mips3d = 1;
11028 break;
11029
11030 case OPTION_NO_MIPS3D:
11031 mips_opts.ase_mips3d = 0;
11032 break;
11033
11034 case OPTION_MEMBEDDED_PIC:
11035 mips_pic = EMBEDDED_PIC;
11036 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
11037 {
11038 as_bad (_("-G may not be used with embedded PIC code"));
11039 return 0;
11040 }
11041 g_switch_value = 0x7fffffff;
11042 break;
11043
11044 case OPTION_FIX_VR4122:
11045 mips_fix_4122_bugs = 1;
11046 break;
11047
11048 case OPTION_NO_FIX_VR4122:
11049 mips_fix_4122_bugs = 0;
11050 break;
11051
11052 case OPTION_RELAX_BRANCH:
11053 mips_relax_branch = 1;
11054 break;
11055
11056 case OPTION_NO_RELAX_BRANCH:
11057 mips_relax_branch = 0;
11058 break;
11059
11060 #ifdef OBJ_ELF
11061 /* When generating ELF code, we permit -KPIC and -call_shared to
11062 select SVR4_PIC, and -non_shared to select no PIC. This is
11063 intended to be compatible with Irix 5. */
11064 case OPTION_CALL_SHARED:
11065 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11066 {
11067 as_bad (_("-call_shared is supported only for ELF format"));
11068 return 0;
11069 }
11070 mips_pic = SVR4_PIC;
11071 mips_abicalls = TRUE;
11072 if (g_switch_seen && g_switch_value != 0)
11073 {
11074 as_bad (_("-G may not be used with SVR4 PIC code"));
11075 return 0;
11076 }
11077 g_switch_value = 0;
11078 break;
11079
11080 case OPTION_NON_SHARED:
11081 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11082 {
11083 as_bad (_("-non_shared is supported only for ELF format"));
11084 return 0;
11085 }
11086 mips_pic = NO_PIC;
11087 mips_abicalls = FALSE;
11088 break;
11089
11090 /* The -xgot option tells the assembler to use 32 offsets when
11091 accessing the got in SVR4_PIC mode. It is for Irix
11092 compatibility. */
11093 case OPTION_XGOT:
11094 mips_big_got = 1;
11095 break;
11096 #endif /* OBJ_ELF */
11097
11098 case 'G':
11099 if (! USE_GLOBAL_POINTER_OPT)
11100 {
11101 as_bad (_("-G is not supported for this configuration"));
11102 return 0;
11103 }
11104 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
11105 {
11106 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
11107 return 0;
11108 }
11109 else
11110 g_switch_value = atoi (arg);
11111 g_switch_seen = 1;
11112 break;
11113
11114 #ifdef OBJ_ELF
11115 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11116 and -mabi=64. */
11117 case OPTION_32:
11118 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11119 {
11120 as_bad (_("-32 is supported for ELF format only"));
11121 return 0;
11122 }
11123 mips_abi = O32_ABI;
11124 break;
11125
11126 case OPTION_N32:
11127 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11128 {
11129 as_bad (_("-n32 is supported for ELF format only"));
11130 return 0;
11131 }
11132 mips_abi = N32_ABI;
11133 break;
11134
11135 case OPTION_64:
11136 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11137 {
11138 as_bad (_("-64 is supported for ELF format only"));
11139 return 0;
11140 }
11141 mips_abi = N64_ABI;
11142 if (! support_64bit_objects())
11143 as_fatal (_("No compiled in support for 64 bit object file format"));
11144 break;
11145 #endif /* OBJ_ELF */
11146
11147 case OPTION_GP32:
11148 file_mips_gp32 = 1;
11149 break;
11150
11151 case OPTION_GP64:
11152 file_mips_gp32 = 0;
11153 break;
11154
11155 case OPTION_FP32:
11156 file_mips_fp32 = 1;
11157 break;
11158
11159 case OPTION_FP64:
11160 file_mips_fp32 = 0;
11161 break;
11162
11163 #ifdef OBJ_ELF
11164 case OPTION_MABI:
11165 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11166 {
11167 as_bad (_("-mabi is supported for ELF format only"));
11168 return 0;
11169 }
11170 if (strcmp (arg, "32") == 0)
11171 mips_abi = O32_ABI;
11172 else if (strcmp (arg, "o64") == 0)
11173 mips_abi = O64_ABI;
11174 else if (strcmp (arg, "n32") == 0)
11175 mips_abi = N32_ABI;
11176 else if (strcmp (arg, "64") == 0)
11177 {
11178 mips_abi = N64_ABI;
11179 if (! support_64bit_objects())
11180 as_fatal (_("No compiled in support for 64 bit object file "
11181 "format"));
11182 }
11183 else if (strcmp (arg, "eabi") == 0)
11184 mips_abi = EABI_ABI;
11185 else
11186 {
11187 as_fatal (_("invalid abi -mabi=%s"), arg);
11188 return 0;
11189 }
11190 break;
11191 #endif /* OBJ_ELF */
11192
11193 case OPTION_M7000_HILO_FIX:
11194 mips_7000_hilo_fix = TRUE;
11195 break;
11196
11197 case OPTION_MNO_7000_HILO_FIX:
11198 mips_7000_hilo_fix = FALSE;
11199 break;
11200
11201 #ifdef OBJ_ELF
11202 case OPTION_MDEBUG:
11203 mips_flag_mdebug = TRUE;
11204 break;
11205
11206 case OPTION_NO_MDEBUG:
11207 mips_flag_mdebug = FALSE;
11208 break;
11209 #endif /* OBJ_ELF */
11210
11211 default:
11212 return 0;
11213 }
11214
11215 return 1;
11216 }
11217 \f
11218 /* Set up globals to generate code for the ISA or processor
11219 described by INFO. */
11220
11221 static void
11222 mips_set_architecture (info)
11223 const struct mips_cpu_info *info;
11224 {
11225 if (info != 0)
11226 {
11227 mips_arch_info = info;
11228 mips_arch = info->cpu;
11229 mips_opts.isa = info->isa;
11230 }
11231 }
11232
11233
11234 /* Likewise for tuning. */
11235
11236 static void
11237 mips_set_tune (info)
11238 const struct mips_cpu_info *info;
11239 {
11240 if (info != 0)
11241 {
11242 mips_tune_info = info;
11243 mips_tune = info->cpu;
11244 }
11245 }
11246
11247
11248 void
11249 mips_after_parse_args ()
11250 {
11251 /* GP relative stuff not working for PE */
11252 if (strncmp (TARGET_OS, "pe", 2) == 0
11253 && g_switch_value != 0)
11254 {
11255 if (g_switch_seen)
11256 as_bad (_("-G not supported in this configuration."));
11257 g_switch_value = 0;
11258 }
11259
11260 if (mips_abi == NO_ABI)
11261 mips_abi = MIPS_DEFAULT_ABI;
11262
11263 /* The following code determines the architecture and register size.
11264 Similar code was added to GCC 3.3 (see override_options() in
11265 config/mips/mips.c). The GAS and GCC code should be kept in sync
11266 as much as possible. */
11267
11268 if (mips_arch_string != 0)
11269 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
11270
11271 if (mips_tune_string != 0)
11272 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
11273
11274 if (file_mips_isa != ISA_UNKNOWN)
11275 {
11276 /* Handle -mipsN. At this point, file_mips_isa contains the
11277 ISA level specified by -mipsN, while mips_opts.isa contains
11278 the -march selection (if any). */
11279 if (mips_arch_info != 0)
11280 {
11281 /* -march takes precedence over -mipsN, since it is more descriptive.
11282 There's no harm in specifying both as long as the ISA levels
11283 are the same. */
11284 if (file_mips_isa != mips_opts.isa)
11285 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11286 mips_cpu_info_from_isa (file_mips_isa)->name,
11287 mips_cpu_info_from_isa (mips_opts.isa)->name);
11288 }
11289 else
11290 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
11291 }
11292
11293 if (mips_arch_info == 0)
11294 mips_set_architecture (mips_parse_cpu ("default CPU",
11295 MIPS_CPU_STRING_DEFAULT));
11296
11297 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11298 as_bad ("-march=%s is not compatible with the selected ABI",
11299 mips_arch_info->name);
11300
11301 /* Optimize for mips_arch, unless -mtune selects a different processor. */
11302 if (mips_tune_info == 0)
11303 mips_set_tune (mips_arch_info);
11304
11305 if (file_mips_gp32 >= 0)
11306 {
11307 /* The user specified the size of the integer registers. Make sure
11308 it agrees with the ABI and ISA. */
11309 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11310 as_bad (_("-mgp64 used with a 32-bit processor"));
11311 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11312 as_bad (_("-mgp32 used with a 64-bit ABI"));
11313 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11314 as_bad (_("-mgp64 used with a 32-bit ABI"));
11315 }
11316 else
11317 {
11318 /* Infer the integer register size from the ABI and processor.
11319 Restrict ourselves to 32-bit registers if that's all the
11320 processor has, or if the ABI cannot handle 64-bit registers. */
11321 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11322 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
11323 }
11324
11325 /* ??? GAS treats single-float processors as though they had 64-bit
11326 float registers (although it complains when double-precision
11327 instructions are used). As things stand, saying they have 32-bit
11328 registers would lead to spurious "register must be even" messages.
11329 So here we assume float registers are always the same size as
11330 integer ones, unless the user says otherwise. */
11331 if (file_mips_fp32 < 0)
11332 file_mips_fp32 = file_mips_gp32;
11333
11334 /* End of GCC-shared inference code. */
11335
11336 /* ??? When do we want this flag to be set? Who uses it? */
11337 if (file_mips_gp32 == 1
11338 && mips_abi == NO_ABI
11339 && ISA_HAS_64BIT_REGS (mips_opts.isa))
11340 mips_32bitmode = 1;
11341
11342 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11343 as_bad (_("trap exception not supported at ISA 1"));
11344
11345 /* If the selected architecture includes support for ASEs, enable
11346 generation of code for them. */
11347 if (mips_opts.mips16 == -1)
11348 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
11349 if (mips_opts.ase_mips3d == -1)
11350 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
11351 if (mips_opts.ase_mdmx == -1)
11352 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
11353
11354 file_mips_isa = mips_opts.isa;
11355 file_ase_mips16 = mips_opts.mips16;
11356 file_ase_mips3d = mips_opts.ase_mips3d;
11357 file_ase_mdmx = mips_opts.ase_mdmx;
11358 mips_opts.gp32 = file_mips_gp32;
11359 mips_opts.fp32 = file_mips_fp32;
11360
11361 if (mips_flag_mdebug < 0)
11362 {
11363 #ifdef OBJ_MAYBE_ECOFF
11364 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11365 mips_flag_mdebug = 1;
11366 else
11367 #endif /* OBJ_MAYBE_ECOFF */
11368 mips_flag_mdebug = 0;
11369 }
11370 }
11371 \f
11372 void
11373 mips_init_after_args ()
11374 {
11375 /* initialize opcodes */
11376 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
11377 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
11378 }
11379
11380 long
11381 md_pcrel_from (fixP)
11382 fixS *fixP;
11383 {
11384 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11385 switch (fixP->fx_r_type)
11386 {
11387 case BFD_RELOC_16_PCREL_S2:
11388 case BFD_RELOC_MIPS_JMP:
11389 /* Return the address of the delay slot. */
11390 return addr + 4;
11391 default:
11392 return addr;
11393 }
11394 }
11395
11396 /* This is called before the symbol table is processed. In order to
11397 work with gcc when using mips-tfile, we must keep all local labels.
11398 However, in other cases, we want to discard them. If we were
11399 called with -g, but we didn't see any debugging information, it may
11400 mean that gcc is smuggling debugging information through to
11401 mips-tfile, in which case we must generate all local labels. */
11402
11403 void
11404 mips_frob_file_before_adjust ()
11405 {
11406 #ifndef NO_ECOFF_DEBUGGING
11407 if (ECOFF_DEBUGGING
11408 && mips_debug != 0
11409 && ! ecoff_debugging_seen)
11410 flag_keep_locals = 1;
11411 #endif
11412 }
11413
11414 /* Sort any unmatched HI16_S relocs so that they immediately precede
11415 the corresponding LO reloc. This is called before md_apply_fix3 and
11416 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11417 explicit use of the %hi modifier. */
11418
11419 void
11420 mips_frob_file ()
11421 {
11422 struct mips_hi_fixup *l;
11423
11424 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11425 {
11426 segment_info_type *seginfo;
11427 int pass;
11428
11429 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
11430
11431 /* If a GOT16 relocation turns out to be against a global symbol,
11432 there isn't supposed to be a matching LO. */
11433 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11434 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11435 continue;
11436
11437 /* Check quickly whether the next fixup happens to be a matching %lo. */
11438 if (fixup_has_matching_lo_p (l->fixp))
11439 continue;
11440
11441 /* Look through the fixups for this segment for a matching %lo.
11442 When we find one, move the %hi just in front of it. We do
11443 this in two passes. In the first pass, we try to find a
11444 unique %lo. In the second pass, we permit multiple %hi
11445 relocs for a single %lo (this is a GNU extension). */
11446 seginfo = seg_info (l->seg);
11447 for (pass = 0; pass < 2; pass++)
11448 {
11449 fixS *f, *prev;
11450
11451 prev = NULL;
11452 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11453 {
11454 /* Check whether this is a %lo fixup which matches l->fixp. */
11455 if (f->fx_r_type == BFD_RELOC_LO16
11456 && f->fx_addsy == l->fixp->fx_addsy
11457 && f->fx_offset == l->fixp->fx_offset
11458 && (pass == 1
11459 || prev == NULL
11460 || !reloc_needs_lo_p (prev->fx_r_type)
11461 || !fixup_has_matching_lo_p (prev)))
11462 {
11463 fixS **pf;
11464
11465 /* Move l->fixp before f. */
11466 for (pf = &seginfo->fix_root;
11467 *pf != l->fixp;
11468 pf = &(*pf)->fx_next)
11469 assert (*pf != NULL);
11470
11471 *pf = l->fixp->fx_next;
11472
11473 l->fixp->fx_next = f;
11474 if (prev == NULL)
11475 seginfo->fix_root = l->fixp;
11476 else
11477 prev->fx_next = l->fixp;
11478
11479 break;
11480 }
11481
11482 prev = f;
11483 }
11484
11485 if (f != NULL)
11486 break;
11487
11488 #if 0 /* GCC code motion plus incomplete dead code elimination
11489 can leave a %hi without a %lo. */
11490 if (pass == 1)
11491 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11492 _("Unmatched %%hi reloc"));
11493 #endif
11494 }
11495 }
11496 }
11497
11498 /* When generating embedded PIC code we need to use a special
11499 relocation to represent the difference of two symbols in the .text
11500 section (switch tables use a difference of this sort). See
11501 include/coff/mips.h for details. This macro checks whether this
11502 fixup requires the special reloc. */
11503 #define SWITCH_TABLE(fixp) \
11504 ((fixp)->fx_r_type == BFD_RELOC_32 \
11505 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
11506 && (fixp)->fx_addsy != NULL \
11507 && (fixp)->fx_subsy != NULL \
11508 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11509 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11510
11511 /* When generating embedded PIC code we must keep all PC relative
11512 relocations, in case the linker has to relax a call. We also need
11513 to keep relocations for switch table entries.
11514
11515 We may have combined relocations without symbols in the N32/N64 ABI.
11516 We have to prevent gas from dropping them. */
11517
11518 int
11519 mips_force_relocation (fixp)
11520 fixS *fixp;
11521 {
11522 if (generic_force_reloc (fixp))
11523 return 1;
11524
11525 if (HAVE_NEWABI
11526 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11527 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11528 || fixp->fx_r_type == BFD_RELOC_HI16_S
11529 || fixp->fx_r_type == BFD_RELOC_LO16))
11530 return 1;
11531
11532 return (mips_pic == EMBEDDED_PIC
11533 && (fixp->fx_pcrel
11534 || SWITCH_TABLE (fixp)
11535 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11536 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11537 }
11538
11539 /* This hook is called before a fix is simplified. We don't really
11540 decide whether to skip a fix here. Rather, we turn global symbols
11541 used as branch targets into local symbols, such that they undergo
11542 simplification. We can only do this if the symbol is defined and
11543 it is in the same section as the branch. If this doesn't hold, we
11544 emit a better error message than just saying the relocation is not
11545 valid for the selected object format.
11546
11547 FIXP is the fix-up we're going to try to simplify, SEG is the
11548 segment in which the fix up occurs. The return value should be
11549 non-zero to indicate the fix-up is valid for further
11550 simplifications. */
11551
11552 int
11553 mips_validate_fix (fixP, seg)
11554 struct fix *fixP;
11555 asection *seg;
11556 {
11557 /* There's a lot of discussion on whether it should be possible to
11558 use R_MIPS_PC16 to represent branch relocations. The outcome
11559 seems to be that it can, but gas/bfd are very broken in creating
11560 RELA relocations for this, so for now we only accept branches to
11561 symbols in the same section. Anything else is of dubious value,
11562 since there's no guarantee that at link time the symbol would be
11563 in range. Even for branches to local symbols this is arguably
11564 wrong, since it we assume the symbol is not going to be
11565 overridden, which should be possible per ELF library semantics,
11566 but then, there isn't a dynamic relocation that could be used to
11567 this effect, and the target would likely be out of range as well.
11568
11569 Unfortunately, it seems that there is too much code out there
11570 that relies on branches to symbols that are global to be resolved
11571 as if they were local, like the IRIX tools do, so we do it as
11572 well, but with a warning so that people are reminded to fix their
11573 code. If we ever get back to using R_MIPS_PC16 for branch
11574 targets, this entire block should go away (and probably the
11575 whole function). */
11576
11577 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11578 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11579 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11580 && mips_pic != EMBEDDED_PIC)
11581 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11582 && fixP->fx_addsy)
11583 {
11584 if (! S_IS_DEFINED (fixP->fx_addsy))
11585 {
11586 as_bad_where (fixP->fx_file, fixP->fx_line,
11587 _("Cannot branch to undefined symbol."));
11588 /* Avoid any further errors about this fixup. */
11589 fixP->fx_done = 1;
11590 }
11591 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11592 {
11593 as_bad_where (fixP->fx_file, fixP->fx_line,
11594 _("Cannot branch to symbol in another section."));
11595 fixP->fx_done = 1;
11596 }
11597 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11598 {
11599 symbolS *sym = fixP->fx_addsy;
11600
11601 as_warn_where (fixP->fx_file, fixP->fx_line,
11602 _("Pretending global symbol used as branch target is local."));
11603
11604 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11605 S_GET_SEGMENT (sym),
11606 S_GET_VALUE (sym),
11607 symbol_get_frag (sym));
11608 copy_symbol_attributes (fixP->fx_addsy, sym);
11609 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11610 assert (symbol_resolved_p (sym));
11611 symbol_mark_resolved (fixP->fx_addsy);
11612 }
11613 }
11614
11615 return 1;
11616 }
11617
11618 #ifdef OBJ_ELF
11619 static int
11620 mips_need_elf_addend_fixup (fixP)
11621 fixS *fixP;
11622 {
11623 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11624 return 1;
11625 if (mips_pic == EMBEDDED_PIC
11626 && S_IS_WEAK (fixP->fx_addsy))
11627 return 1;
11628 if (mips_pic != EMBEDDED_PIC
11629 && (S_IS_WEAK (fixP->fx_addsy)
11630 || S_IS_EXTERNAL (fixP->fx_addsy))
11631 && !S_IS_COMMON (fixP->fx_addsy))
11632 return 1;
11633 if (((bfd_get_section_flags (stdoutput,
11634 S_GET_SEGMENT (fixP->fx_addsy))
11635 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11636 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11637 ".gnu.linkonce",
11638 sizeof (".gnu.linkonce") - 1))
11639 return 1;
11640 return 0;
11641 }
11642 #endif
11643
11644 /* Apply a fixup to the object file. */
11645
11646 void
11647 md_apply_fix3 (fixP, valP, seg)
11648 fixS *fixP;
11649 valueT *valP;
11650 segT seg ATTRIBUTE_UNUSED;
11651 {
11652 bfd_byte *buf;
11653 long insn;
11654 static int previous_fx_r_type = 0;
11655 reloc_howto_type *howto;
11656
11657 /* We ignore generic BFD relocations we don't know about. */
11658 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11659 if (! howto)
11660 return;
11661
11662 assert (fixP->fx_size == 4
11663 || fixP->fx_r_type == BFD_RELOC_16
11664 || fixP->fx_r_type == BFD_RELOC_64
11665 || fixP->fx_r_type == BFD_RELOC_CTOR
11666 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11667 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11668 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
11669
11670 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
11671
11672 /* If we aren't adjusting this fixup to be against the section
11673 symbol, we need to adjust the value. */
11674 #ifdef OBJ_ELF
11675 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
11676 {
11677 if (mips_need_elf_addend_fixup (fixP)
11678 && howto->partial_inplace
11679 && fixP->fx_r_type != BFD_RELOC_GPREL16
11680 && fixP->fx_r_type != BFD_RELOC_GPREL32
11681 && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11682 {
11683 /* In this case, the bfd_install_relocation routine will
11684 incorrectly add the symbol value back in. We just want
11685 the addend to appear in the object file.
11686
11687 The condition above used to include
11688 "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11689
11690 However, howto can't be trusted here, because we
11691 might change the reloc type in tc_gen_reloc. We can
11692 check howto->partial_inplace because that conversion
11693 happens to preserve howto->partial_inplace; but it
11694 does not preserve howto->pcrel_offset. I've just
11695 eliminated the check, because all MIPS PC-relative
11696 relocations are marked howto->pcrel_offset.
11697
11698 howto->pcrel_offset was originally added for
11699 R_MIPS_PC16, which is generated for code like
11700
11701 globl g1 .text
11702 .text
11703 .space 20
11704 g1:
11705 x:
11706 bal g1
11707 */
11708 *valP -= S_GET_VALUE (fixP->fx_addsy);
11709 }
11710
11711 /* This code was generated using trial and error and so is
11712 fragile and not trustworthy. If you change it, you should
11713 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11714 they still pass. */
11715 if (fixP->fx_pcrel)
11716 {
11717 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11718
11719 /* BFD's REL handling, for MIPS, is _very_ weird.
11720 This gives the right results, but it can't possibly
11721 be the way things are supposed to work. */
11722 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11723 }
11724 }
11725 #endif
11726
11727 /* We are not done if this is a composite relocation to set up gp. */
11728 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11729 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11730 || (fixP->fx_r_type == BFD_RELOC_64
11731 && (previous_fx_r_type == BFD_RELOC_GPREL32
11732 || previous_fx_r_type == BFD_RELOC_GPREL16))
11733 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11734 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11735 || fixP->fx_r_type == BFD_RELOC_LO16))))
11736 fixP->fx_done = 1;
11737 previous_fx_r_type = fixP->fx_r_type;
11738
11739 switch (fixP->fx_r_type)
11740 {
11741 case BFD_RELOC_MIPS_JMP:
11742 case BFD_RELOC_MIPS_SHIFT5:
11743 case BFD_RELOC_MIPS_SHIFT6:
11744 case BFD_RELOC_MIPS_GOT_DISP:
11745 case BFD_RELOC_MIPS_GOT_PAGE:
11746 case BFD_RELOC_MIPS_GOT_OFST:
11747 case BFD_RELOC_MIPS_SUB:
11748 case BFD_RELOC_MIPS_INSERT_A:
11749 case BFD_RELOC_MIPS_INSERT_B:
11750 case BFD_RELOC_MIPS_DELETE:
11751 case BFD_RELOC_MIPS_HIGHEST:
11752 case BFD_RELOC_MIPS_HIGHER:
11753 case BFD_RELOC_MIPS_SCN_DISP:
11754 case BFD_RELOC_MIPS_REL16:
11755 case BFD_RELOC_MIPS_RELGOT:
11756 case BFD_RELOC_MIPS_JALR:
11757 case BFD_RELOC_HI16:
11758 case BFD_RELOC_HI16_S:
11759 case BFD_RELOC_GPREL16:
11760 case BFD_RELOC_MIPS_LITERAL:
11761 case BFD_RELOC_MIPS_CALL16:
11762 case BFD_RELOC_MIPS_GOT16:
11763 case BFD_RELOC_GPREL32:
11764 case BFD_RELOC_MIPS_GOT_HI16:
11765 case BFD_RELOC_MIPS_GOT_LO16:
11766 case BFD_RELOC_MIPS_CALL_HI16:
11767 case BFD_RELOC_MIPS_CALL_LO16:
11768 case BFD_RELOC_MIPS16_GPREL:
11769 if (fixP->fx_pcrel)
11770 as_bad_where (fixP->fx_file, fixP->fx_line,
11771 _("Invalid PC relative reloc"));
11772 /* Nothing needed to do. The value comes from the reloc entry */
11773 break;
11774
11775 case BFD_RELOC_MIPS16_JMP:
11776 /* We currently always generate a reloc against a symbol, which
11777 means that we don't want an addend even if the symbol is
11778 defined. */
11779 *valP = 0;
11780 break;
11781
11782 case BFD_RELOC_PCREL_HI16_S:
11783 /* The addend for this is tricky if it is internal, so we just
11784 do everything here rather than in bfd_install_relocation. */
11785 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11786 break;
11787 if (fixP->fx_addsy
11788 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11789 {
11790 /* For an external symbol adjust by the address to make it
11791 pcrel_offset. We use the address of the RELLO reloc
11792 which follows this one. */
11793 *valP += (fixP->fx_next->fx_frag->fr_address
11794 + fixP->fx_next->fx_where);
11795 }
11796 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
11797 if (target_big_endian)
11798 buf += 2;
11799 md_number_to_chars ((char *) buf, *valP, 2);
11800 break;
11801
11802 case BFD_RELOC_PCREL_LO16:
11803 /* The addend for this is tricky if it is internal, so we just
11804 do everything here rather than in bfd_install_relocation. */
11805 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
11806 break;
11807 if (fixP->fx_addsy
11808 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
11809 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
11810 if (target_big_endian)
11811 buf += 2;
11812 md_number_to_chars ((char *) buf, *valP, 2);
11813 break;
11814
11815 case BFD_RELOC_64:
11816 /* This is handled like BFD_RELOC_32, but we output a sign
11817 extended value if we are only 32 bits. */
11818 if (fixP->fx_done
11819 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11820 {
11821 if (8 <= sizeof (valueT))
11822 md_number_to_chars (buf, *valP, 8);
11823 else
11824 {
11825 valueT hiv;
11826
11827 if ((*valP & 0x80000000) != 0)
11828 hiv = 0xffffffff;
11829 else
11830 hiv = 0;
11831 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11832 *valP, 4);
11833 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11834 hiv, 4);
11835 }
11836 }
11837 break;
11838
11839 case BFD_RELOC_RVA:
11840 case BFD_RELOC_32:
11841 /* If we are deleting this reloc entry, we must fill in the
11842 value now. This can happen if we have a .word which is not
11843 resolved when it appears but is later defined. We also need
11844 to fill in the value if this is an embedded PIC switch table
11845 entry. */
11846 if (fixP->fx_done
11847 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11848 md_number_to_chars (buf, *valP, 4);
11849 break;
11850
11851 case BFD_RELOC_16:
11852 /* If we are deleting this reloc entry, we must fill in the
11853 value now. */
11854 assert (fixP->fx_size == 2);
11855 if (fixP->fx_done)
11856 md_number_to_chars (buf, *valP, 2);
11857 break;
11858
11859 case BFD_RELOC_LO16:
11860 /* When handling an embedded PIC switch statement, we can wind
11861 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11862 if (fixP->fx_done)
11863 {
11864 if (*valP + 0x8000 > 0xffff)
11865 as_bad_where (fixP->fx_file, fixP->fx_line,
11866 _("relocation overflow"));
11867 if (target_big_endian)
11868 buf += 2;
11869 md_number_to_chars ((char *) buf, *valP, 2);
11870 }
11871 break;
11872
11873 case BFD_RELOC_16_PCREL_S2:
11874 if ((*valP & 0x3) != 0)
11875 as_bad_where (fixP->fx_file, fixP->fx_line,
11876 _("Branch to odd address (%lx)"), (long) *valP);
11877
11878 /*
11879 * We need to save the bits in the instruction since fixup_segment()
11880 * might be deleting the relocation entry (i.e., a branch within
11881 * the current segment).
11882 */
11883 if (! fixP->fx_done)
11884 break;
11885
11886 /* update old instruction data */
11887 if (target_big_endian)
11888 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11889 else
11890 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11891
11892 if (*valP + 0x20000 <= 0x3ffff)
11893 {
11894 insn |= (*valP >> 2) & 0xffff;
11895 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11896 }
11897 else if (mips_pic == NO_PIC
11898 && fixP->fx_done
11899 && fixP->fx_frag->fr_address >= text_section->vma
11900 && (fixP->fx_frag->fr_address
11901 < text_section->vma + text_section->_raw_size)
11902 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11903 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11904 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11905 {
11906 /* The branch offset is too large. If this is an
11907 unconditional branch, and we are not generating PIC code,
11908 we can convert it to an absolute jump instruction. */
11909 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11910 insn = 0x0c000000; /* jal */
11911 else
11912 insn = 0x08000000; /* j */
11913 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11914 fixP->fx_done = 0;
11915 fixP->fx_addsy = section_symbol (text_section);
11916 *valP += md_pcrel_from (fixP);
11917 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11918 }
11919 else
11920 {
11921 /* If we got here, we have branch-relaxation disabled,
11922 and there's nothing we can do to fix this instruction
11923 without turning it into a longer sequence. */
11924 as_bad_where (fixP->fx_file, fixP->fx_line,
11925 _("Branch out of range"));
11926 }
11927 break;
11928
11929 case BFD_RELOC_VTABLE_INHERIT:
11930 fixP->fx_done = 0;
11931 if (fixP->fx_addsy
11932 && !S_IS_DEFINED (fixP->fx_addsy)
11933 && !S_IS_WEAK (fixP->fx_addsy))
11934 S_SET_WEAK (fixP->fx_addsy);
11935 break;
11936
11937 case BFD_RELOC_VTABLE_ENTRY:
11938 fixP->fx_done = 0;
11939 break;
11940
11941 default:
11942 internalError ();
11943 }
11944
11945 /* Remember value for tc_gen_reloc. */
11946 fixP->fx_addnumber = *valP;
11947 }
11948
11949 #if 0
11950 void
11951 printInsn (oc)
11952 unsigned long oc;
11953 {
11954 const struct mips_opcode *p;
11955 int treg, sreg, dreg, shamt;
11956 short imm;
11957 const char *args;
11958 int i;
11959
11960 for (i = 0; i < NUMOPCODES; ++i)
11961 {
11962 p = &mips_opcodes[i];
11963 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11964 {
11965 printf ("%08lx %s\t", oc, p->name);
11966 treg = (oc >> 16) & 0x1f;
11967 sreg = (oc >> 21) & 0x1f;
11968 dreg = (oc >> 11) & 0x1f;
11969 shamt = (oc >> 6) & 0x1f;
11970 imm = oc;
11971 for (args = p->args;; ++args)
11972 {
11973 switch (*args)
11974 {
11975 case '\0':
11976 printf ("\n");
11977 break;
11978
11979 case ',':
11980 case '(':
11981 case ')':
11982 printf ("%c", *args);
11983 continue;
11984
11985 case 'r':
11986 assert (treg == sreg);
11987 printf ("$%d,$%d", treg, sreg);
11988 continue;
11989
11990 case 'd':
11991 case 'G':
11992 printf ("$%d", dreg);
11993 continue;
11994
11995 case 't':
11996 case 'E':
11997 printf ("$%d", treg);
11998 continue;
11999
12000 case 'k':
12001 printf ("0x%x", treg);
12002 continue;
12003
12004 case 'b':
12005 case 's':
12006 printf ("$%d", sreg);
12007 continue;
12008
12009 case 'a':
12010 printf ("0x%08lx", oc & 0x1ffffff);
12011 continue;
12012
12013 case 'i':
12014 case 'j':
12015 case 'o':
12016 case 'u':
12017 printf ("%d", imm);
12018 continue;
12019
12020 case '<':
12021 case '>':
12022 printf ("$%d", shamt);
12023 continue;
12024
12025 default:
12026 internalError ();
12027 }
12028 break;
12029 }
12030 return;
12031 }
12032 }
12033 printf (_("%08lx UNDEFINED\n"), oc);
12034 }
12035 #endif
12036
12037 static symbolS *
12038 get_symbol ()
12039 {
12040 int c;
12041 char *name;
12042 symbolS *p;
12043
12044 name = input_line_pointer;
12045 c = get_symbol_end ();
12046 p = (symbolS *) symbol_find_or_make (name);
12047 *input_line_pointer = c;
12048 return p;
12049 }
12050
12051 /* Align the current frag to a given power of two. The MIPS assembler
12052 also automatically adjusts any preceding label. */
12053
12054 static void
12055 mips_align (to, fill, label)
12056 int to;
12057 int fill;
12058 symbolS *label;
12059 {
12060 mips_emit_delays (FALSE);
12061 frag_align (to, fill, 0);
12062 record_alignment (now_seg, to);
12063 if (label != NULL)
12064 {
12065 assert (S_GET_SEGMENT (label) == now_seg);
12066 symbol_set_frag (label, frag_now);
12067 S_SET_VALUE (label, (valueT) frag_now_fix ());
12068 }
12069 }
12070
12071 /* Align to a given power of two. .align 0 turns off the automatic
12072 alignment used by the data creating pseudo-ops. */
12073
12074 static void
12075 s_align (x)
12076 int x ATTRIBUTE_UNUSED;
12077 {
12078 register int temp;
12079 register long temp_fill;
12080 long max_alignment = 15;
12081
12082 /*
12083
12084 o Note that the assembler pulls down any immediately preceeding label
12085 to the aligned address.
12086 o It's not documented but auto alignment is reinstated by
12087 a .align pseudo instruction.
12088 o Note also that after auto alignment is turned off the mips assembler
12089 issues an error on attempt to assemble an improperly aligned data item.
12090 We don't.
12091
12092 */
12093
12094 temp = get_absolute_expression ();
12095 if (temp > max_alignment)
12096 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12097 else if (temp < 0)
12098 {
12099 as_warn (_("Alignment negative: 0 assumed."));
12100 temp = 0;
12101 }
12102 if (*input_line_pointer == ',')
12103 {
12104 ++input_line_pointer;
12105 temp_fill = get_absolute_expression ();
12106 }
12107 else
12108 temp_fill = 0;
12109 if (temp)
12110 {
12111 auto_align = 1;
12112 mips_align (temp, (int) temp_fill,
12113 insn_labels != NULL ? insn_labels->label : NULL);
12114 }
12115 else
12116 {
12117 auto_align = 0;
12118 }
12119
12120 demand_empty_rest_of_line ();
12121 }
12122
12123 void
12124 mips_flush_pending_output ()
12125 {
12126 mips_emit_delays (FALSE);
12127 mips_clear_insn_labels ();
12128 }
12129
12130 static void
12131 s_change_sec (sec)
12132 int sec;
12133 {
12134 segT seg;
12135
12136 /* When generating embedded PIC code, we only use the .text, .lit8,
12137 .sdata and .sbss sections. We change the .data and .rdata
12138 pseudo-ops to use .sdata. */
12139 if (mips_pic == EMBEDDED_PIC
12140 && (sec == 'd' || sec == 'r'))
12141 sec = 's';
12142
12143 #ifdef OBJ_ELF
12144 /* The ELF backend needs to know that we are changing sections, so
12145 that .previous works correctly. We could do something like check
12146 for an obj_section_change_hook macro, but that might be confusing
12147 as it would not be appropriate to use it in the section changing
12148 functions in read.c, since obj-elf.c intercepts those. FIXME:
12149 This should be cleaner, somehow. */
12150 obj_elf_section_change_hook ();
12151 #endif
12152
12153 mips_emit_delays (FALSE);
12154 switch (sec)
12155 {
12156 case 't':
12157 s_text (0);
12158 break;
12159 case 'd':
12160 s_data (0);
12161 break;
12162 case 'b':
12163 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12164 demand_empty_rest_of_line ();
12165 break;
12166
12167 case 'r':
12168 if (USE_GLOBAL_POINTER_OPT)
12169 {
12170 seg = subseg_new (RDATA_SECTION_NAME,
12171 (subsegT) get_absolute_expression ());
12172 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12173 {
12174 bfd_set_section_flags (stdoutput, seg,
12175 (SEC_ALLOC
12176 | SEC_LOAD
12177 | SEC_READONLY
12178 | SEC_RELOC
12179 | SEC_DATA));
12180 if (strcmp (TARGET_OS, "elf") != 0)
12181 record_alignment (seg, 4);
12182 }
12183 demand_empty_rest_of_line ();
12184 }
12185 else
12186 {
12187 as_bad (_("No read only data section in this object file format"));
12188 demand_empty_rest_of_line ();
12189 return;
12190 }
12191 break;
12192
12193 case 's':
12194 if (USE_GLOBAL_POINTER_OPT)
12195 {
12196 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12197 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12198 {
12199 bfd_set_section_flags (stdoutput, seg,
12200 SEC_ALLOC | SEC_LOAD | SEC_RELOC
12201 | SEC_DATA);
12202 if (strcmp (TARGET_OS, "elf") != 0)
12203 record_alignment (seg, 4);
12204 }
12205 demand_empty_rest_of_line ();
12206 break;
12207 }
12208 else
12209 {
12210 as_bad (_("Global pointers not supported; recompile -G 0"));
12211 demand_empty_rest_of_line ();
12212 return;
12213 }
12214 }
12215
12216 auto_align = 1;
12217 }
12218
12219 void
12220 s_change_section (ignore)
12221 int ignore ATTRIBUTE_UNUSED;
12222 {
12223 #ifdef OBJ_ELF
12224 char *section_name;
12225 char c;
12226 char next_c = 0;
12227 int section_type;
12228 int section_flag;
12229 int section_entry_size;
12230 int section_alignment;
12231
12232 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12233 return;
12234
12235 section_name = input_line_pointer;
12236 c = get_symbol_end ();
12237 if (c)
12238 next_c = *(input_line_pointer + 1);
12239
12240 /* Do we have .section Name<,"flags">? */
12241 if (c != ',' || (c == ',' && next_c == '"'))
12242 {
12243 /* just after name is now '\0'. */
12244 *input_line_pointer = c;
12245 input_line_pointer = section_name;
12246 obj_elf_section (ignore);
12247 return;
12248 }
12249 input_line_pointer++;
12250
12251 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12252 if (c == ',')
12253 section_type = get_absolute_expression ();
12254 else
12255 section_type = 0;
12256 if (*input_line_pointer++ == ',')
12257 section_flag = get_absolute_expression ();
12258 else
12259 section_flag = 0;
12260 if (*input_line_pointer++ == ',')
12261 section_entry_size = get_absolute_expression ();
12262 else
12263 section_entry_size = 0;
12264 if (*input_line_pointer++ == ',')
12265 section_alignment = get_absolute_expression ();
12266 else
12267 section_alignment = 0;
12268
12269 section_name = xstrdup (section_name);
12270
12271 obj_elf_change_section (section_name, section_type, section_flag,
12272 section_entry_size, 0, 0, 0);
12273
12274 if (now_seg->name != section_name)
12275 free (section_name);
12276 #endif /* OBJ_ELF */
12277 }
12278
12279 void
12280 mips_enable_auto_align ()
12281 {
12282 auto_align = 1;
12283 }
12284
12285 static void
12286 s_cons (log_size)
12287 int log_size;
12288 {
12289 symbolS *label;
12290
12291 label = insn_labels != NULL ? insn_labels->label : NULL;
12292 mips_emit_delays (FALSE);
12293 if (log_size > 0 && auto_align)
12294 mips_align (log_size, 0, label);
12295 mips_clear_insn_labels ();
12296 cons (1 << log_size);
12297 }
12298
12299 static void
12300 s_float_cons (type)
12301 int type;
12302 {
12303 symbolS *label;
12304
12305 label = insn_labels != NULL ? insn_labels->label : NULL;
12306
12307 mips_emit_delays (FALSE);
12308
12309 if (auto_align)
12310 {
12311 if (type == 'd')
12312 mips_align (3, 0, label);
12313 else
12314 mips_align (2, 0, label);
12315 }
12316
12317 mips_clear_insn_labels ();
12318
12319 float_cons (type);
12320 }
12321
12322 /* Handle .globl. We need to override it because on Irix 5 you are
12323 permitted to say
12324 .globl foo .text
12325 where foo is an undefined symbol, to mean that foo should be
12326 considered to be the address of a function. */
12327
12328 static void
12329 s_mips_globl (x)
12330 int x ATTRIBUTE_UNUSED;
12331 {
12332 char *name;
12333 int c;
12334 symbolS *symbolP;
12335 flagword flag;
12336
12337 name = input_line_pointer;
12338 c = get_symbol_end ();
12339 symbolP = symbol_find_or_make (name);
12340 *input_line_pointer = c;
12341 SKIP_WHITESPACE ();
12342
12343 /* On Irix 5, every global symbol that is not explicitly labelled as
12344 being a function is apparently labelled as being an object. */
12345 flag = BSF_OBJECT;
12346
12347 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12348 {
12349 char *secname;
12350 asection *sec;
12351
12352 secname = input_line_pointer;
12353 c = get_symbol_end ();
12354 sec = bfd_get_section_by_name (stdoutput, secname);
12355 if (sec == NULL)
12356 as_bad (_("%s: no such section"), secname);
12357 *input_line_pointer = c;
12358
12359 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12360 flag = BSF_FUNCTION;
12361 }
12362
12363 symbol_get_bfdsym (symbolP)->flags |= flag;
12364
12365 S_SET_EXTERNAL (symbolP);
12366 demand_empty_rest_of_line ();
12367 }
12368
12369 static void
12370 s_option (x)
12371 int x ATTRIBUTE_UNUSED;
12372 {
12373 char *opt;
12374 char c;
12375
12376 opt = input_line_pointer;
12377 c = get_symbol_end ();
12378
12379 if (*opt == 'O')
12380 {
12381 /* FIXME: What does this mean? */
12382 }
12383 else if (strncmp (opt, "pic", 3) == 0)
12384 {
12385 int i;
12386
12387 i = atoi (opt + 3);
12388 if (i == 0)
12389 mips_pic = NO_PIC;
12390 else if (i == 2)
12391 {
12392 mips_pic = SVR4_PIC;
12393 mips_abicalls = TRUE;
12394 }
12395 else
12396 as_bad (_(".option pic%d not supported"), i);
12397
12398 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12399 {
12400 if (g_switch_seen && g_switch_value != 0)
12401 as_warn (_("-G may not be used with SVR4 PIC code"));
12402 g_switch_value = 0;
12403 bfd_set_gp_size (stdoutput, 0);
12404 }
12405 }
12406 else
12407 as_warn (_("Unrecognized option \"%s\""), opt);
12408
12409 *input_line_pointer = c;
12410 demand_empty_rest_of_line ();
12411 }
12412
12413 /* This structure is used to hold a stack of .set values. */
12414
12415 struct mips_option_stack
12416 {
12417 struct mips_option_stack *next;
12418 struct mips_set_options options;
12419 };
12420
12421 static struct mips_option_stack *mips_opts_stack;
12422
12423 /* Handle the .set pseudo-op. */
12424
12425 static void
12426 s_mipsset (x)
12427 int x ATTRIBUTE_UNUSED;
12428 {
12429 char *name = input_line_pointer, ch;
12430
12431 while (!is_end_of_line[(unsigned char) *input_line_pointer])
12432 ++input_line_pointer;
12433 ch = *input_line_pointer;
12434 *input_line_pointer = '\0';
12435
12436 if (strcmp (name, "reorder") == 0)
12437 {
12438 if (mips_opts.noreorder && prev_nop_frag != NULL)
12439 {
12440 /* If we still have pending nops, we can discard them. The
12441 usual nop handling will insert any that are still
12442 needed. */
12443 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12444 * (mips_opts.mips16 ? 2 : 4));
12445 prev_nop_frag = NULL;
12446 }
12447 mips_opts.noreorder = 0;
12448 }
12449 else if (strcmp (name, "noreorder") == 0)
12450 {
12451 mips_emit_delays (TRUE);
12452 mips_opts.noreorder = 1;
12453 mips_any_noreorder = 1;
12454 }
12455 else if (strcmp (name, "at") == 0)
12456 {
12457 mips_opts.noat = 0;
12458 }
12459 else if (strcmp (name, "noat") == 0)
12460 {
12461 mips_opts.noat = 1;
12462 }
12463 else if (strcmp (name, "macro") == 0)
12464 {
12465 mips_opts.warn_about_macros = 0;
12466 }
12467 else if (strcmp (name, "nomacro") == 0)
12468 {
12469 if (mips_opts.noreorder == 0)
12470 as_bad (_("`noreorder' must be set before `nomacro'"));
12471 mips_opts.warn_about_macros = 1;
12472 }
12473 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12474 {
12475 mips_opts.nomove = 0;
12476 }
12477 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12478 {
12479 mips_opts.nomove = 1;
12480 }
12481 else if (strcmp (name, "bopt") == 0)
12482 {
12483 mips_opts.nobopt = 0;
12484 }
12485 else if (strcmp (name, "nobopt") == 0)
12486 {
12487 mips_opts.nobopt = 1;
12488 }
12489 else if (strcmp (name, "mips16") == 0
12490 || strcmp (name, "MIPS-16") == 0)
12491 mips_opts.mips16 = 1;
12492 else if (strcmp (name, "nomips16") == 0
12493 || strcmp (name, "noMIPS-16") == 0)
12494 mips_opts.mips16 = 0;
12495 else if (strcmp (name, "mips3d") == 0)
12496 mips_opts.ase_mips3d = 1;
12497 else if (strcmp (name, "nomips3d") == 0)
12498 mips_opts.ase_mips3d = 0;
12499 else if (strcmp (name, "mdmx") == 0)
12500 mips_opts.ase_mdmx = 1;
12501 else if (strcmp (name, "nomdmx") == 0)
12502 mips_opts.ase_mdmx = 0;
12503 else if (strncmp (name, "mips", 4) == 0)
12504 {
12505 int reset = 0;
12506
12507 /* Permit the user to change the ISA on the fly. Needless to
12508 say, misuse can cause serious problems. */
12509 if (strcmp (name, "mips0") == 0)
12510 {
12511 reset = 1;
12512 mips_opts.isa = file_mips_isa;
12513 }
12514 else if (strcmp (name, "mips1") == 0)
12515 mips_opts.isa = ISA_MIPS1;
12516 else if (strcmp (name, "mips2") == 0)
12517 mips_opts.isa = ISA_MIPS2;
12518 else if (strcmp (name, "mips3") == 0)
12519 mips_opts.isa = ISA_MIPS3;
12520 else if (strcmp (name, "mips4") == 0)
12521 mips_opts.isa = ISA_MIPS4;
12522 else if (strcmp (name, "mips5") == 0)
12523 mips_opts.isa = ISA_MIPS5;
12524 else if (strcmp (name, "mips32") == 0)
12525 mips_opts.isa = ISA_MIPS32;
12526 else if (strcmp (name, "mips32r2") == 0)
12527 mips_opts.isa = ISA_MIPS32R2;
12528 else if (strcmp (name, "mips64") == 0)
12529 mips_opts.isa = ISA_MIPS64;
12530 else
12531 as_bad (_("unknown ISA level %s"), name + 4);
12532
12533 switch (mips_opts.isa)
12534 {
12535 case 0:
12536 break;
12537 case ISA_MIPS1:
12538 case ISA_MIPS2:
12539 case ISA_MIPS32:
12540 case ISA_MIPS32R2:
12541 mips_opts.gp32 = 1;
12542 mips_opts.fp32 = 1;
12543 break;
12544 case ISA_MIPS3:
12545 case ISA_MIPS4:
12546 case ISA_MIPS5:
12547 case ISA_MIPS64:
12548 mips_opts.gp32 = 0;
12549 mips_opts.fp32 = 0;
12550 break;
12551 default:
12552 as_bad (_("unknown ISA level %s"), name + 4);
12553 break;
12554 }
12555 if (reset)
12556 {
12557 mips_opts.gp32 = file_mips_gp32;
12558 mips_opts.fp32 = file_mips_fp32;
12559 }
12560 }
12561 else if (strcmp (name, "autoextend") == 0)
12562 mips_opts.noautoextend = 0;
12563 else if (strcmp (name, "noautoextend") == 0)
12564 mips_opts.noautoextend = 1;
12565 else if (strcmp (name, "push") == 0)
12566 {
12567 struct mips_option_stack *s;
12568
12569 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12570 s->next = mips_opts_stack;
12571 s->options = mips_opts;
12572 mips_opts_stack = s;
12573 }
12574 else if (strcmp (name, "pop") == 0)
12575 {
12576 struct mips_option_stack *s;
12577
12578 s = mips_opts_stack;
12579 if (s == NULL)
12580 as_bad (_(".set pop with no .set push"));
12581 else
12582 {
12583 /* If we're changing the reorder mode we need to handle
12584 delay slots correctly. */
12585 if (s->options.noreorder && ! mips_opts.noreorder)
12586 mips_emit_delays (TRUE);
12587 else if (! s->options.noreorder && mips_opts.noreorder)
12588 {
12589 if (prev_nop_frag != NULL)
12590 {
12591 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12592 * (mips_opts.mips16 ? 2 : 4));
12593 prev_nop_frag = NULL;
12594 }
12595 }
12596
12597 mips_opts = s->options;
12598 mips_opts_stack = s->next;
12599 free (s);
12600 }
12601 }
12602 else
12603 {
12604 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12605 }
12606 *input_line_pointer = ch;
12607 demand_empty_rest_of_line ();
12608 }
12609
12610 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
12611 .option pic2. It means to generate SVR4 PIC calls. */
12612
12613 static void
12614 s_abicalls (ignore)
12615 int ignore ATTRIBUTE_UNUSED;
12616 {
12617 mips_pic = SVR4_PIC;
12618 mips_abicalls = TRUE;
12619 if (USE_GLOBAL_POINTER_OPT)
12620 {
12621 if (g_switch_seen && g_switch_value != 0)
12622 as_warn (_("-G may not be used with SVR4 PIC code"));
12623 g_switch_value = 0;
12624 }
12625 bfd_set_gp_size (stdoutput, 0);
12626 demand_empty_rest_of_line ();
12627 }
12628
12629 /* Handle the .cpload pseudo-op. This is used when generating SVR4
12630 PIC code. It sets the $gp register for the function based on the
12631 function address, which is in the register named in the argument.
12632 This uses a relocation against _gp_disp, which is handled specially
12633 by the linker. The result is:
12634 lui $gp,%hi(_gp_disp)
12635 addiu $gp,$gp,%lo(_gp_disp)
12636 addu $gp,$gp,.cpload argument
12637 The .cpload argument is normally $25 == $t9. */
12638
12639 static void
12640 s_cpload (ignore)
12641 int ignore ATTRIBUTE_UNUSED;
12642 {
12643 expressionS ex;
12644 int icnt = 0;
12645
12646 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12647 .cpload is ignored. */
12648 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12649 {
12650 s_ignore (0);
12651 return;
12652 }
12653
12654 /* .cpload should be in a .set noreorder section. */
12655 if (mips_opts.noreorder == 0)
12656 as_warn (_(".cpload not in noreorder section"));
12657
12658 ex.X_op = O_symbol;
12659 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12660 ex.X_op_symbol = NULL;
12661 ex.X_add_number = 0;
12662
12663 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12664 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12665
12666 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12667 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12668 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
12669
12670 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
12671 mips_gp_register, mips_gp_register, tc_get_register (0));
12672
12673 demand_empty_rest_of_line ();
12674 }
12675
12676 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12677 .cpsetup $reg1, offset|$reg2, label
12678
12679 If offset is given, this results in:
12680 sd $gp, offset($sp)
12681 lui $gp, %hi(%neg(%gp_rel(label)))
12682 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12683 daddu $gp, $gp, $reg1
12684
12685 If $reg2 is given, this results in:
12686 daddu $reg2, $gp, $0
12687 lui $gp, %hi(%neg(%gp_rel(label)))
12688 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12689 daddu $gp, $gp, $reg1
12690 $reg1 is normally $25 == $t9. */
12691 static void
12692 s_cpsetup (ignore)
12693 int ignore ATTRIBUTE_UNUSED;
12694 {
12695 expressionS ex_off;
12696 expressionS ex_sym;
12697 int reg1;
12698 int icnt = 0;
12699 char *f;
12700
12701 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
12702 We also need NewABI support. */
12703 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12704 {
12705 s_ignore (0);
12706 return;
12707 }
12708
12709 reg1 = tc_get_register (0);
12710 SKIP_WHITESPACE ();
12711 if (*input_line_pointer != ',')
12712 {
12713 as_bad (_("missing argument separator ',' for .cpsetup"));
12714 return;
12715 }
12716 else
12717 ++input_line_pointer;
12718 SKIP_WHITESPACE ();
12719 if (*input_line_pointer == '$')
12720 {
12721 mips_cpreturn_register = tc_get_register (0);
12722 mips_cpreturn_offset = -1;
12723 }
12724 else
12725 {
12726 mips_cpreturn_offset = get_absolute_expression ();
12727 mips_cpreturn_register = -1;
12728 }
12729 SKIP_WHITESPACE ();
12730 if (*input_line_pointer != ',')
12731 {
12732 as_bad (_("missing argument separator ',' for .cpsetup"));
12733 return;
12734 }
12735 else
12736 ++input_line_pointer;
12737 SKIP_WHITESPACE ();
12738 expression (&ex_sym);
12739
12740 if (mips_cpreturn_register == -1)
12741 {
12742 ex_off.X_op = O_constant;
12743 ex_off.X_add_symbol = NULL;
12744 ex_off.X_op_symbol = NULL;
12745 ex_off.X_add_number = mips_cpreturn_offset;
12746
12747 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12748 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12749 }
12750 else
12751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12752 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12753
12754 /* Ensure there's room for the next two instructions, so that `f'
12755 doesn't end up with an address in the wrong frag. */
12756 frag_grow (8);
12757 f = frag_more (0);
12758 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12759 (int) BFD_RELOC_GPREL16);
12760 fix_new (frag_now, f - frag_now->fr_literal,
12761 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12762 fix_new (frag_now, f - frag_now->fr_literal,
12763 4, NULL, 0, 0, BFD_RELOC_HI16_S);
12764
12765 f = frag_more (0);
12766 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12767 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
12768 fix_new (frag_now, f - frag_now->fr_literal,
12769 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12770 fix_new (frag_now, f - frag_now->fr_literal,
12771 4, NULL, 0, 0, BFD_RELOC_LO16);
12772
12773 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12774 HAVE_64BIT_ADDRESSES ? "daddu" : "add", "d,v,t",
12775 mips_gp_register, mips_gp_register, reg1);
12776
12777 demand_empty_rest_of_line ();
12778 }
12779
12780 static void
12781 s_cplocal (ignore)
12782 int ignore ATTRIBUTE_UNUSED;
12783 {
12784 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12785 .cplocal is ignored. */
12786 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12787 {
12788 s_ignore (0);
12789 return;
12790 }
12791
12792 mips_gp_register = tc_get_register (0);
12793 demand_empty_rest_of_line ();
12794 }
12795
12796 /* Handle the .cprestore pseudo-op. This stores $gp into a given
12797 offset from $sp. The offset is remembered, and after making a PIC
12798 call $gp is restored from that location. */
12799
12800 static void
12801 s_cprestore (ignore)
12802 int ignore ATTRIBUTE_UNUSED;
12803 {
12804 expressionS ex;
12805 int icnt = 0;
12806
12807 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12808 .cprestore is ignored. */
12809 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
12810 {
12811 s_ignore (0);
12812 return;
12813 }
12814
12815 mips_cprestore_offset = get_absolute_expression ();
12816 mips_cprestore_valid = 1;
12817
12818 ex.X_op = O_constant;
12819 ex.X_add_symbol = NULL;
12820 ex.X_op_symbol = NULL;
12821 ex.X_add_number = mips_cprestore_offset;
12822
12823 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12824 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12825 mips_gp_register, SP);
12826
12827 demand_empty_rest_of_line ();
12828 }
12829
12830 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12831 was given in the preceeding .gpsetup, it results in:
12832 ld $gp, offset($sp)
12833
12834 If a register $reg2 was given there, it results in:
12835 daddiu $gp, $gp, $reg2
12836 */
12837 static void
12838 s_cpreturn (ignore)
12839 int ignore ATTRIBUTE_UNUSED;
12840 {
12841 expressionS ex;
12842 int icnt = 0;
12843
12844 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12845 We also need NewABI support. */
12846 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12847 {
12848 s_ignore (0);
12849 return;
12850 }
12851
12852 if (mips_cpreturn_register == -1)
12853 {
12854 ex.X_op = O_constant;
12855 ex.X_add_symbol = NULL;
12856 ex.X_op_symbol = NULL;
12857 ex.X_add_number = mips_cpreturn_offset;
12858
12859 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12860 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12861 }
12862 else
12863 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12864 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12865
12866 demand_empty_rest_of_line ();
12867 }
12868
12869 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12870 code. It sets the offset to use in gp_rel relocations. */
12871
12872 static void
12873 s_gpvalue (ignore)
12874 int ignore ATTRIBUTE_UNUSED;
12875 {
12876 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12877 We also need NewABI support. */
12878 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12879 {
12880 s_ignore (0);
12881 return;
12882 }
12883
12884 mips_gprel_offset = get_absolute_expression ();
12885
12886 demand_empty_rest_of_line ();
12887 }
12888
12889 /* Handle the .gpword pseudo-op. This is used when generating PIC
12890 code. It generates a 32 bit GP relative reloc. */
12891
12892 static void
12893 s_gpword (ignore)
12894 int ignore ATTRIBUTE_UNUSED;
12895 {
12896 symbolS *label;
12897 expressionS ex;
12898 char *p;
12899
12900 /* When not generating PIC code, this is treated as .word. */
12901 if (mips_pic != SVR4_PIC)
12902 {
12903 s_cons (2);
12904 return;
12905 }
12906
12907 label = insn_labels != NULL ? insn_labels->label : NULL;
12908 mips_emit_delays (TRUE);
12909 if (auto_align)
12910 mips_align (2, 0, label);
12911 mips_clear_insn_labels ();
12912
12913 expression (&ex);
12914
12915 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12916 {
12917 as_bad (_("Unsupported use of .gpword"));
12918 ignore_rest_of_line ();
12919 }
12920
12921 p = frag_more (4);
12922 md_number_to_chars (p, (valueT) 0, 4);
12923 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12924 BFD_RELOC_GPREL32);
12925
12926 demand_empty_rest_of_line ();
12927 }
12928
12929 static void
12930 s_gpdword (ignore)
12931 int ignore ATTRIBUTE_UNUSED;
12932 {
12933 symbolS *label;
12934 expressionS ex;
12935 char *p;
12936
12937 /* When not generating PIC code, this is treated as .dword. */
12938 if (mips_pic != SVR4_PIC)
12939 {
12940 s_cons (3);
12941 return;
12942 }
12943
12944 label = insn_labels != NULL ? insn_labels->label : NULL;
12945 mips_emit_delays (TRUE);
12946 if (auto_align)
12947 mips_align (3, 0, label);
12948 mips_clear_insn_labels ();
12949
12950 expression (&ex);
12951
12952 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12953 {
12954 as_bad (_("Unsupported use of .gpdword"));
12955 ignore_rest_of_line ();
12956 }
12957
12958 p = frag_more (8);
12959 md_number_to_chars (p, (valueT) 0, 8);
12960 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
12961 BFD_RELOC_GPREL32);
12962
12963 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12964 ex.X_op = O_absent;
12965 ex.X_add_symbol = 0;
12966 ex.X_add_number = 0;
12967 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
12968 BFD_RELOC_64);
12969
12970 demand_empty_rest_of_line ();
12971 }
12972
12973 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
12974 tables in SVR4 PIC code. */
12975
12976 static void
12977 s_cpadd (ignore)
12978 int ignore ATTRIBUTE_UNUSED;
12979 {
12980 int icnt = 0;
12981 int reg;
12982
12983 /* This is ignored when not generating SVR4 PIC code. */
12984 if (mips_pic != SVR4_PIC)
12985 {
12986 s_ignore (0);
12987 return;
12988 }
12989
12990 /* Add $gp to the register named as an argument. */
12991 reg = tc_get_register (0);
12992 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12993 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI ? "add" : "addu" : "daddu",
12994 "d,v,t", reg, reg, mips_gp_register);
12995
12996 demand_empty_rest_of_line ();
12997 }
12998
12999 /* Handle the .insn pseudo-op. This marks instruction labels in
13000 mips16 mode. This permits the linker to handle them specially,
13001 such as generating jalx instructions when needed. We also make
13002 them odd for the duration of the assembly, in order to generate the
13003 right sort of code. We will make them even in the adjust_symtab
13004 routine, while leaving them marked. This is convenient for the
13005 debugger and the disassembler. The linker knows to make them odd
13006 again. */
13007
13008 static void
13009 s_insn (ignore)
13010 int ignore ATTRIBUTE_UNUSED;
13011 {
13012 mips16_mark_labels ();
13013
13014 demand_empty_rest_of_line ();
13015 }
13016
13017 /* Handle a .stabn directive. We need these in order to mark a label
13018 as being a mips16 text label correctly. Sometimes the compiler
13019 will emit a label, followed by a .stabn, and then switch sections.
13020 If the label and .stabn are in mips16 mode, then the label is
13021 really a mips16 text label. */
13022
13023 static void
13024 s_mips_stab (type)
13025 int type;
13026 {
13027 if (type == 'n')
13028 mips16_mark_labels ();
13029
13030 s_stab (type);
13031 }
13032
13033 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
13034 */
13035
13036 static void
13037 s_mips_weakext (ignore)
13038 int ignore ATTRIBUTE_UNUSED;
13039 {
13040 char *name;
13041 int c;
13042 symbolS *symbolP;
13043 expressionS exp;
13044
13045 name = input_line_pointer;
13046 c = get_symbol_end ();
13047 symbolP = symbol_find_or_make (name);
13048 S_SET_WEAK (symbolP);
13049 *input_line_pointer = c;
13050
13051 SKIP_WHITESPACE ();
13052
13053 if (! is_end_of_line[(unsigned char) *input_line_pointer])
13054 {
13055 if (S_IS_DEFINED (symbolP))
13056 {
13057 as_bad ("ignoring attempt to redefine symbol %s",
13058 S_GET_NAME (symbolP));
13059 ignore_rest_of_line ();
13060 return;
13061 }
13062
13063 if (*input_line_pointer == ',')
13064 {
13065 ++input_line_pointer;
13066 SKIP_WHITESPACE ();
13067 }
13068
13069 expression (&exp);
13070 if (exp.X_op != O_symbol)
13071 {
13072 as_bad ("bad .weakext directive");
13073 ignore_rest_of_line ();
13074 return;
13075 }
13076 symbol_set_value_expression (symbolP, &exp);
13077 }
13078
13079 demand_empty_rest_of_line ();
13080 }
13081
13082 /* Parse a register string into a number. Called from the ECOFF code
13083 to parse .frame. The argument is non-zero if this is the frame
13084 register, so that we can record it in mips_frame_reg. */
13085
13086 int
13087 tc_get_register (frame)
13088 int frame;
13089 {
13090 int reg;
13091
13092 SKIP_WHITESPACE ();
13093 if (*input_line_pointer++ != '$')
13094 {
13095 as_warn (_("expected `$'"));
13096 reg = ZERO;
13097 }
13098 else if (ISDIGIT (*input_line_pointer))
13099 {
13100 reg = get_absolute_expression ();
13101 if (reg < 0 || reg >= 32)
13102 {
13103 as_warn (_("Bad register number"));
13104 reg = ZERO;
13105 }
13106 }
13107 else
13108 {
13109 if (strncmp (input_line_pointer, "ra", 2) == 0)
13110 {
13111 reg = RA;
13112 input_line_pointer += 2;
13113 }
13114 else if (strncmp (input_line_pointer, "fp", 2) == 0)
13115 {
13116 reg = FP;
13117 input_line_pointer += 2;
13118 }
13119 else if (strncmp (input_line_pointer, "sp", 2) == 0)
13120 {
13121 reg = SP;
13122 input_line_pointer += 2;
13123 }
13124 else if (strncmp (input_line_pointer, "gp", 2) == 0)
13125 {
13126 reg = GP;
13127 input_line_pointer += 2;
13128 }
13129 else if (strncmp (input_line_pointer, "at", 2) == 0)
13130 {
13131 reg = AT;
13132 input_line_pointer += 2;
13133 }
13134 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13135 {
13136 reg = KT0;
13137 input_line_pointer += 3;
13138 }
13139 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13140 {
13141 reg = KT1;
13142 input_line_pointer += 3;
13143 }
13144 else if (strncmp (input_line_pointer, "zero", 4) == 0)
13145 {
13146 reg = ZERO;
13147 input_line_pointer += 4;
13148 }
13149 else
13150 {
13151 as_warn (_("Unrecognized register name"));
13152 reg = ZERO;
13153 while (ISALNUM(*input_line_pointer))
13154 input_line_pointer++;
13155 }
13156 }
13157 if (frame)
13158 {
13159 mips_frame_reg = reg != 0 ? reg : SP;
13160 mips_frame_reg_valid = 1;
13161 mips_cprestore_valid = 0;
13162 }
13163 return reg;
13164 }
13165
13166 valueT
13167 md_section_align (seg, addr)
13168 asection *seg;
13169 valueT addr;
13170 {
13171 int align = bfd_get_section_alignment (stdoutput, seg);
13172
13173 #ifdef OBJ_ELF
13174 /* We don't need to align ELF sections to the full alignment.
13175 However, Irix 5 may prefer that we align them at least to a 16
13176 byte boundary. We don't bother to align the sections if we are
13177 targeted for an embedded system. */
13178 if (strcmp (TARGET_OS, "elf") == 0)
13179 return addr;
13180 if (align > 4)
13181 align = 4;
13182 #endif
13183
13184 return ((addr + (1 << align) - 1) & (-1 << align));
13185 }
13186
13187 /* Utility routine, called from above as well. If called while the
13188 input file is still being read, it's only an approximation. (For
13189 example, a symbol may later become defined which appeared to be
13190 undefined earlier.) */
13191
13192 static int
13193 nopic_need_relax (sym, before_relaxing)
13194 symbolS *sym;
13195 int before_relaxing;
13196 {
13197 if (sym == 0)
13198 return 0;
13199
13200 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
13201 {
13202 const char *symname;
13203 int change;
13204
13205 /* Find out whether this symbol can be referenced off the $gp
13206 register. It can be if it is smaller than the -G size or if
13207 it is in the .sdata or .sbss section. Certain symbols can
13208 not be referenced off the $gp, although it appears as though
13209 they can. */
13210 symname = S_GET_NAME (sym);
13211 if (symname != (const char *) NULL
13212 && (strcmp (symname, "eprol") == 0
13213 || strcmp (symname, "etext") == 0
13214 || strcmp (symname, "_gp") == 0
13215 || strcmp (symname, "edata") == 0
13216 || strcmp (symname, "_fbss") == 0
13217 || strcmp (symname, "_fdata") == 0
13218 || strcmp (symname, "_ftext") == 0
13219 || strcmp (symname, "end") == 0
13220 || strcmp (symname, "_gp_disp") == 0))
13221 change = 1;
13222 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13223 && (0
13224 #ifndef NO_ECOFF_DEBUGGING
13225 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13226 && (symbol_get_obj (sym)->ecoff_extern_size
13227 <= g_switch_value))
13228 #endif
13229 /* We must defer this decision until after the whole
13230 file has been read, since there might be a .extern
13231 after the first use of this symbol. */
13232 || (before_relaxing
13233 #ifndef NO_ECOFF_DEBUGGING
13234 && symbol_get_obj (sym)->ecoff_extern_size == 0
13235 #endif
13236 && S_GET_VALUE (sym) == 0)
13237 || (S_GET_VALUE (sym) != 0
13238 && S_GET_VALUE (sym) <= g_switch_value)))
13239 change = 0;
13240 else
13241 {
13242 const char *segname;
13243
13244 segname = segment_name (S_GET_SEGMENT (sym));
13245 assert (strcmp (segname, ".lit8") != 0
13246 && strcmp (segname, ".lit4") != 0);
13247 change = (strcmp (segname, ".sdata") != 0
13248 && strcmp (segname, ".sbss") != 0
13249 && strncmp (segname, ".sdata.", 7) != 0
13250 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
13251 }
13252 return change;
13253 }
13254 else
13255 /* We are not optimizing for the $gp register. */
13256 return 1;
13257 }
13258
13259
13260 /* Return true if the given symbol should be considered local for SVR4 PIC. */
13261
13262 static bfd_boolean
13263 pic_need_relax (sym, segtype)
13264 symbolS *sym;
13265 asection *segtype;
13266 {
13267 asection *symsec;
13268 bfd_boolean linkonce;
13269
13270 /* Handle the case of a symbol equated to another symbol. */
13271 while (symbol_equated_reloc_p (sym))
13272 {
13273 symbolS *n;
13274
13275 /* It's possible to get a loop here in a badly written
13276 program. */
13277 n = symbol_get_value_expression (sym)->X_add_symbol;
13278 if (n == sym)
13279 break;
13280 sym = n;
13281 }
13282
13283 symsec = S_GET_SEGMENT (sym);
13284
13285 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13286 linkonce = FALSE;
13287 if (symsec != segtype && ! S_IS_LOCAL (sym))
13288 {
13289 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13290 != 0)
13291 linkonce = TRUE;
13292
13293 /* The GNU toolchain uses an extension for ELF: a section
13294 beginning with the magic string .gnu.linkonce is a linkonce
13295 section. */
13296 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13297 sizeof ".gnu.linkonce" - 1) == 0)
13298 linkonce = TRUE;
13299 }
13300
13301 /* This must duplicate the test in adjust_reloc_syms. */
13302 return (symsec != &bfd_und_section
13303 && symsec != &bfd_abs_section
13304 && ! bfd_is_com_section (symsec)
13305 && !linkonce
13306 #ifdef OBJ_ELF
13307 /* A global or weak symbol is treated as external. */
13308 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13309 || (! S_IS_WEAK (sym)
13310 && (! S_IS_EXTERNAL (sym)
13311 || mips_pic == EMBEDDED_PIC)))
13312 #endif
13313 );
13314 }
13315
13316
13317 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13318 extended opcode. SEC is the section the frag is in. */
13319
13320 static int
13321 mips16_extended_frag (fragp, sec, stretch)
13322 fragS *fragp;
13323 asection *sec;
13324 long stretch;
13325 {
13326 int type;
13327 register const struct mips16_immed_operand *op;
13328 offsetT val;
13329 int mintiny, maxtiny;
13330 segT symsec;
13331 fragS *sym_frag;
13332
13333 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13334 return 0;
13335 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13336 return 1;
13337
13338 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13339 op = mips16_immed_operands;
13340 while (op->type != type)
13341 {
13342 ++op;
13343 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13344 }
13345
13346 if (op->unsp)
13347 {
13348 if (type == '<' || type == '>' || type == '[' || type == ']')
13349 {
13350 mintiny = 1;
13351 maxtiny = 1 << op->nbits;
13352 }
13353 else
13354 {
13355 mintiny = 0;
13356 maxtiny = (1 << op->nbits) - 1;
13357 }
13358 }
13359 else
13360 {
13361 mintiny = - (1 << (op->nbits - 1));
13362 maxtiny = (1 << (op->nbits - 1)) - 1;
13363 }
13364
13365 sym_frag = symbol_get_frag (fragp->fr_symbol);
13366 val = S_GET_VALUE (fragp->fr_symbol);
13367 symsec = S_GET_SEGMENT (fragp->fr_symbol);
13368
13369 if (op->pcrel)
13370 {
13371 addressT addr;
13372
13373 /* We won't have the section when we are called from
13374 mips_relax_frag. However, we will always have been called
13375 from md_estimate_size_before_relax first. If this is a
13376 branch to a different section, we mark it as such. If SEC is
13377 NULL, and the frag is not marked, then it must be a branch to
13378 the same section. */
13379 if (sec == NULL)
13380 {
13381 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13382 return 1;
13383 }
13384 else
13385 {
13386 /* Must have been called from md_estimate_size_before_relax. */
13387 if (symsec != sec)
13388 {
13389 fragp->fr_subtype =
13390 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13391
13392 /* FIXME: We should support this, and let the linker
13393 catch branches and loads that are out of range. */
13394 as_bad_where (fragp->fr_file, fragp->fr_line,
13395 _("unsupported PC relative reference to different section"));
13396
13397 return 1;
13398 }
13399 if (fragp != sym_frag && sym_frag->fr_address == 0)
13400 /* Assume non-extended on the first relaxation pass.
13401 The address we have calculated will be bogus if this is
13402 a forward branch to another frag, as the forward frag
13403 will have fr_address == 0. */
13404 return 0;
13405 }
13406
13407 /* In this case, we know for sure that the symbol fragment is in
13408 the same section. If the relax_marker of the symbol fragment
13409 differs from the relax_marker of this fragment, we have not
13410 yet adjusted the symbol fragment fr_address. We want to add
13411 in STRETCH in order to get a better estimate of the address.
13412 This particularly matters because of the shift bits. */
13413 if (stretch != 0
13414 && sym_frag->relax_marker != fragp->relax_marker)
13415 {
13416 fragS *f;
13417
13418 /* Adjust stretch for any alignment frag. Note that if have
13419 been expanding the earlier code, the symbol may be
13420 defined in what appears to be an earlier frag. FIXME:
13421 This doesn't handle the fr_subtype field, which specifies
13422 a maximum number of bytes to skip when doing an
13423 alignment. */
13424 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
13425 {
13426 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13427 {
13428 if (stretch < 0)
13429 stretch = - ((- stretch)
13430 & ~ ((1 << (int) f->fr_offset) - 1));
13431 else
13432 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13433 if (stretch == 0)
13434 break;
13435 }
13436 }
13437 if (f != NULL)
13438 val += stretch;
13439 }
13440
13441 addr = fragp->fr_address + fragp->fr_fix;
13442
13443 /* The base address rules are complicated. The base address of
13444 a branch is the following instruction. The base address of a
13445 PC relative load or add is the instruction itself, but if it
13446 is in a delay slot (in which case it can not be extended) use
13447 the address of the instruction whose delay slot it is in. */
13448 if (type == 'p' || type == 'q')
13449 {
13450 addr += 2;
13451
13452 /* If we are currently assuming that this frag should be
13453 extended, then, the current address is two bytes
13454 higher. */
13455 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13456 addr += 2;
13457
13458 /* Ignore the low bit in the target, since it will be set
13459 for a text label. */
13460 if ((val & 1) != 0)
13461 --val;
13462 }
13463 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13464 addr -= 4;
13465 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13466 addr -= 2;
13467
13468 val -= addr & ~ ((1 << op->shift) - 1);
13469
13470 /* Branch offsets have an implicit 0 in the lowest bit. */
13471 if (type == 'p' || type == 'q')
13472 val /= 2;
13473
13474 /* If any of the shifted bits are set, we must use an extended
13475 opcode. If the address depends on the size of this
13476 instruction, this can lead to a loop, so we arrange to always
13477 use an extended opcode. We only check this when we are in
13478 the main relaxation loop, when SEC is NULL. */
13479 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13480 {
13481 fragp->fr_subtype =
13482 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13483 return 1;
13484 }
13485
13486 /* If we are about to mark a frag as extended because the value
13487 is precisely maxtiny + 1, then there is a chance of an
13488 infinite loop as in the following code:
13489 la $4,foo
13490 .skip 1020
13491 .align 2
13492 foo:
13493 In this case when the la is extended, foo is 0x3fc bytes
13494 away, so the la can be shrunk, but then foo is 0x400 away, so
13495 the la must be extended. To avoid this loop, we mark the
13496 frag as extended if it was small, and is about to become
13497 extended with a value of maxtiny + 1. */
13498 if (val == ((maxtiny + 1) << op->shift)
13499 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13500 && sec == NULL)
13501 {
13502 fragp->fr_subtype =
13503 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13504 return 1;
13505 }
13506 }
13507 else if (symsec != absolute_section && sec != NULL)
13508 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13509
13510 if ((val & ((1 << op->shift) - 1)) != 0
13511 || val < (mintiny << op->shift)
13512 || val > (maxtiny << op->shift))
13513 return 1;
13514 else
13515 return 0;
13516 }
13517
13518 /* Compute the length of a branch sequence, and adjust the
13519 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13520 worst-case length is computed, with UPDATE being used to indicate
13521 whether an unconditional (-1), branch-likely (+1) or regular (0)
13522 branch is to be computed. */
13523 static int
13524 relaxed_branch_length (fragp, sec, update)
13525 fragS *fragp;
13526 asection *sec;
13527 int update;
13528 {
13529 bfd_boolean toofar;
13530 int length;
13531
13532 if (fragp
13533 && S_IS_DEFINED (fragp->fr_symbol)
13534 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13535 {
13536 addressT addr;
13537 offsetT val;
13538
13539 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13540
13541 addr = fragp->fr_address + fragp->fr_fix + 4;
13542
13543 val -= addr;
13544
13545 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13546 }
13547 else if (fragp)
13548 /* If the symbol is not defined or it's in a different segment,
13549 assume the user knows what's going on and emit a short
13550 branch. */
13551 toofar = FALSE;
13552 else
13553 toofar = TRUE;
13554
13555 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13556 fragp->fr_subtype
13557 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
13558 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13559 RELAX_BRANCH_LINK (fragp->fr_subtype),
13560 toofar);
13561
13562 length = 4;
13563 if (toofar)
13564 {
13565 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13566 length += 8;
13567
13568 if (mips_pic != NO_PIC)
13569 {
13570 /* Additional space for PIC loading of target address. */
13571 length += 8;
13572 if (mips_opts.isa == ISA_MIPS1)
13573 /* Additional space for $at-stabilizing nop. */
13574 length += 4;
13575 }
13576
13577 /* If branch is conditional. */
13578 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13579 length += 8;
13580 }
13581
13582 return length;
13583 }
13584
13585 /* Estimate the size of a frag before relaxing. Unless this is the
13586 mips16, we are not really relaxing here, and the final size is
13587 encoded in the subtype information. For the mips16, we have to
13588 decide whether we are using an extended opcode or not. */
13589
13590 int
13591 md_estimate_size_before_relax (fragp, segtype)
13592 fragS *fragp;
13593 asection *segtype;
13594 {
13595 int change;
13596
13597 if (RELAX_BRANCH_P (fragp->fr_subtype))
13598 {
13599
13600 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13601
13602 return fragp->fr_var;
13603 }
13604
13605 if (RELAX_MIPS16_P (fragp->fr_subtype))
13606 /* We don't want to modify the EXTENDED bit here; it might get us
13607 into infinite loops. We change it only in mips_relax_frag(). */
13608 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
13609
13610 if (mips_pic == NO_PIC)
13611 change = nopic_need_relax (fragp->fr_symbol, 0);
13612 else if (mips_pic == SVR4_PIC)
13613 change = pic_need_relax (fragp->fr_symbol, segtype);
13614 else
13615 abort ();
13616
13617 if (change)
13618 {
13619 /* Record the offset to the first reloc in the fr_opcode field.
13620 This lets md_convert_frag and tc_gen_reloc know that the code
13621 must be expanded. */
13622 fragp->fr_opcode = (fragp->fr_literal
13623 + fragp->fr_fix
13624 - RELAX_OLD (fragp->fr_subtype)
13625 + RELAX_RELOC1 (fragp->fr_subtype));
13626 /* FIXME: This really needs as_warn_where. */
13627 if (RELAX_WARN (fragp->fr_subtype))
13628 as_warn (_("AT used after \".set noat\" or macro used after "
13629 "\".set nomacro\""));
13630
13631 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
13632 }
13633
13634 return 0;
13635 }
13636
13637 /* This is called to see whether a reloc against a defined symbol
13638 should be converted into a reloc against a section. Don't adjust
13639 MIPS16 jump relocations, so we don't have to worry about the format
13640 of the offset in the .o file. Don't adjust relocations against
13641 mips16 symbols, so that the linker can find them if it needs to set
13642 up a stub. */
13643
13644 int
13645 mips_fix_adjustable (fixp)
13646 fixS *fixp;
13647 {
13648 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13649 return 0;
13650
13651 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13652 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13653 return 0;
13654
13655 if (fixp->fx_addsy == NULL)
13656 return 1;
13657
13658 #ifdef OBJ_ELF
13659 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13660 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13661 && fixp->fx_subsy == NULL)
13662 return 0;
13663 #endif
13664
13665 return 1;
13666 }
13667
13668 /* Translate internal representation of relocation info to BFD target
13669 format. */
13670
13671 arelent **
13672 tc_gen_reloc (section, fixp)
13673 asection *section ATTRIBUTE_UNUSED;
13674 fixS *fixp;
13675 {
13676 static arelent *retval[4];
13677 arelent *reloc;
13678 bfd_reloc_code_real_type code;
13679
13680 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13681 retval[1] = NULL;
13682
13683 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13684 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13685 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13686
13687 if (mips_pic == EMBEDDED_PIC
13688 && SWITCH_TABLE (fixp))
13689 {
13690 /* For a switch table entry we use a special reloc. The addend
13691 is actually the difference between the reloc address and the
13692 subtrahend. */
13693 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13694 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13695 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13696 fixp->fx_r_type = BFD_RELOC_GPREL32;
13697 }
13698 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13699 {
13700 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13701 reloc->addend = fixp->fx_addnumber;
13702 else
13703 {
13704 /* We use a special addend for an internal RELLO reloc. */
13705 if (symbol_section_p (fixp->fx_addsy))
13706 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13707 else
13708 reloc->addend = fixp->fx_addnumber + reloc->address;
13709 }
13710 }
13711 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13712 {
13713 assert (fixp->fx_next != NULL
13714 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13715
13716 /* The reloc is relative to the RELLO; adjust the addend
13717 accordingly. */
13718 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13719 reloc->addend = fixp->fx_next->fx_addnumber;
13720 else
13721 {
13722 /* We use a special addend for an internal RELHI reloc. */
13723 if (symbol_section_p (fixp->fx_addsy))
13724 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13725 + fixp->fx_next->fx_where
13726 - S_GET_VALUE (fixp->fx_subsy));
13727 else
13728 reloc->addend = (fixp->fx_addnumber
13729 + fixp->fx_next->fx_frag->fr_address
13730 + fixp->fx_next->fx_where);
13731 }
13732 }
13733 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13734 reloc->addend = fixp->fx_addnumber;
13735 else
13736 {
13737 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13738 /* A gruesome hack which is a result of the gruesome gas reloc
13739 handling. */
13740 reloc->addend = reloc->address;
13741 else
13742 reloc->addend = -reloc->address;
13743 }
13744
13745 /* If this is a variant frag, we may need to adjust the existing
13746 reloc and generate a new one. */
13747 if (fixp->fx_frag->fr_opcode != NULL
13748 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13749 && ! HAVE_NEWABI)
13750 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13751 && HAVE_NEWABI)
13752 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13753 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13754 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13755 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13756 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13757 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
13758 )
13759 {
13760 arelent *reloc2;
13761
13762 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13763
13764 /* If this is not the last reloc in this frag, then we have two
13765 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13766 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13767 the second one handle all of them. */
13768 if (fixp->fx_next != NULL
13769 && fixp->fx_frag == fixp->fx_next->fx_frag)
13770 {
13771 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13772 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
13773 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13774 && (fixp->fx_next->fx_r_type
13775 == BFD_RELOC_MIPS_GOT_LO16))
13776 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13777 && (fixp->fx_next->fx_r_type
13778 == BFD_RELOC_MIPS_CALL_LO16)));
13779 retval[0] = NULL;
13780 return retval;
13781 }
13782
13783 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13784 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13785 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
13786 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13787 retval[2] = NULL;
13788 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13789 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
13790 reloc2->address = (reloc->address
13791 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13792 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13793 reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
13794 + fixp->fx_frag->tc_frag_data.tc_fr_offset;
13795 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13796 assert (reloc2->howto != NULL);
13797
13798 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13799 {
13800 arelent *reloc3;
13801
13802 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13803 retval[3] = NULL;
13804 *reloc3 = *reloc2;
13805 reloc3->address += 4;
13806 }
13807
13808 if (mips_pic == NO_PIC)
13809 {
13810 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
13811 fixp->fx_r_type = BFD_RELOC_HI16_S;
13812 }
13813 else if (mips_pic == SVR4_PIC)
13814 {
13815 switch (fixp->fx_r_type)
13816 {
13817 default:
13818 abort ();
13819 case BFD_RELOC_MIPS_GOT16:
13820 break;
13821 case BFD_RELOC_MIPS_GOT_LO16:
13822 case BFD_RELOC_MIPS_CALL_LO16:
13823 if (HAVE_NEWABI)
13824 {
13825 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13826 reloc2->howto = bfd_reloc_type_lookup
13827 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13828 }
13829 else
13830 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13831 break;
13832 case BFD_RELOC_MIPS_CALL16:
13833 case BFD_RELOC_MIPS_GOT_OFST:
13834 case BFD_RELOC_MIPS_GOT_DISP:
13835 if (HAVE_NEWABI)
13836 {
13837 /* It may seem nonsensical to relax GOT_DISP to
13838 GOT_DISP, but we're actually turning a GOT_DISP
13839 without offset into a GOT_DISP with an offset,
13840 getting rid of the separate addition, which we can
13841 do when the symbol is found to be local. */
13842 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13843 retval[1] = NULL;
13844 }
13845 else
13846 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13847 break;
13848 }
13849 }
13850 else
13851 abort ();
13852 }
13853
13854 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13855 entry to be used in the relocation's section offset. */
13856 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13857 {
13858 reloc->address = reloc->addend;
13859 reloc->addend = 0;
13860 }
13861
13862 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13863 fixup_segment converted a non-PC relative reloc into a PC
13864 relative reloc. In such a case, we need to convert the reloc
13865 code. */
13866 code = fixp->fx_r_type;
13867 if (fixp->fx_pcrel)
13868 {
13869 switch (code)
13870 {
13871 case BFD_RELOC_8:
13872 code = BFD_RELOC_8_PCREL;
13873 break;
13874 case BFD_RELOC_16:
13875 code = BFD_RELOC_16_PCREL;
13876 break;
13877 case BFD_RELOC_32:
13878 code = BFD_RELOC_32_PCREL;
13879 break;
13880 case BFD_RELOC_64:
13881 code = BFD_RELOC_64_PCREL;
13882 break;
13883 case BFD_RELOC_8_PCREL:
13884 case BFD_RELOC_16_PCREL:
13885 case BFD_RELOC_32_PCREL:
13886 case BFD_RELOC_64_PCREL:
13887 case BFD_RELOC_16_PCREL_S2:
13888 case BFD_RELOC_PCREL_HI16_S:
13889 case BFD_RELOC_PCREL_LO16:
13890 break;
13891 default:
13892 as_bad_where (fixp->fx_file, fixp->fx_line,
13893 _("Cannot make %s relocation PC relative"),
13894 bfd_get_reloc_code_name (code));
13895 }
13896 }
13897
13898 /* To support a PC relative reloc when generating embedded PIC code
13899 for ECOFF, we use a Cygnus extension. We check for that here to
13900 make sure that we don't let such a reloc escape normally. */
13901 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13902 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13903 && code == BFD_RELOC_16_PCREL_S2
13904 && mips_pic != EMBEDDED_PIC)
13905 reloc->howto = NULL;
13906 else
13907 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13908
13909 if (reloc->howto == NULL)
13910 {
13911 as_bad_where (fixp->fx_file, fixp->fx_line,
13912 _("Can not represent %s relocation in this object file format"),
13913 bfd_get_reloc_code_name (code));
13914 retval[0] = NULL;
13915 }
13916
13917 return retval;
13918 }
13919
13920 /* Relax a machine dependent frag. This returns the amount by which
13921 the current size of the frag should change. */
13922
13923 int
13924 mips_relax_frag (sec, fragp, stretch)
13925 asection *sec;
13926 fragS *fragp;
13927 long stretch;
13928 {
13929 if (RELAX_BRANCH_P (fragp->fr_subtype))
13930 {
13931 offsetT old_var = fragp->fr_var;
13932
13933 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
13934
13935 return fragp->fr_var - old_var;
13936 }
13937
13938 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13939 return 0;
13940
13941 if (mips16_extended_frag (fragp, NULL, stretch))
13942 {
13943 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13944 return 0;
13945 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13946 return 2;
13947 }
13948 else
13949 {
13950 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13951 return 0;
13952 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13953 return -2;
13954 }
13955
13956 return 0;
13957 }
13958
13959 /* Convert a machine dependent frag. */
13960
13961 void
13962 md_convert_frag (abfd, asec, fragp)
13963 bfd *abfd ATTRIBUTE_UNUSED;
13964 segT asec;
13965 fragS *fragp;
13966 {
13967 int old, new;
13968 char *fixptr;
13969
13970 if (RELAX_BRANCH_P (fragp->fr_subtype))
13971 {
13972 bfd_byte *buf;
13973 unsigned long insn;
13974 expressionS exp;
13975 fixS *fixp;
13976
13977 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13978
13979 if (target_big_endian)
13980 insn = bfd_getb32 (buf);
13981 else
13982 insn = bfd_getl32 (buf);
13983
13984 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13985 {
13986 /* We generate a fixup instead of applying it right now
13987 because, if there are linker relaxations, we're going to
13988 need the relocations. */
13989 exp.X_op = O_symbol;
13990 exp.X_add_symbol = fragp->fr_symbol;
13991 exp.X_add_number = fragp->fr_offset;
13992
13993 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13994 4, &exp, 1,
13995 BFD_RELOC_16_PCREL_S2);
13996 fixp->fx_file = fragp->fr_file;
13997 fixp->fx_line = fragp->fr_line;
13998
13999 md_number_to_chars ((char *)buf, insn, 4);
14000 buf += 4;
14001 }
14002 else
14003 {
14004 int i;
14005
14006 as_warn_where (fragp->fr_file, fragp->fr_line,
14007 _("relaxed out-of-range branch into a jump"));
14008
14009 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14010 goto uncond;
14011
14012 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14013 {
14014 /* Reverse the branch. */
14015 switch ((insn >> 28) & 0xf)
14016 {
14017 case 4:
14018 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14019 have the condition reversed by tweaking a single
14020 bit, and their opcodes all have 0x4???????. */
14021 assert ((insn & 0xf1000000) == 0x41000000);
14022 insn ^= 0x00010000;
14023 break;
14024
14025 case 0:
14026 /* bltz 0x04000000 bgez 0x04010000
14027 bltzal 0x04100000 bgezal 0x04110000 */
14028 assert ((insn & 0xfc0e0000) == 0x04000000);
14029 insn ^= 0x00010000;
14030 break;
14031
14032 case 1:
14033 /* beq 0x10000000 bne 0x14000000
14034 blez 0x18000000 bgtz 0x1c000000 */
14035 insn ^= 0x04000000;
14036 break;
14037
14038 default:
14039 abort ();
14040 }
14041 }
14042
14043 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14044 {
14045 /* Clear the and-link bit. */
14046 assert ((insn & 0xfc1c0000) == 0x04100000);
14047
14048 /* bltzal 0x04100000 bgezal 0x04110000
14049 bltzall 0x04120000 bgezall 0x04130000 */
14050 insn &= ~0x00100000;
14051 }
14052
14053 /* Branch over the branch (if the branch was likely) or the
14054 full jump (not likely case). Compute the offset from the
14055 current instruction to branch to. */
14056 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14057 i = 16;
14058 else
14059 {
14060 /* How many bytes in instructions we've already emitted? */
14061 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14062 /* How many bytes in instructions from here to the end? */
14063 i = fragp->fr_var - i;
14064 }
14065 /* Convert to instruction count. */
14066 i >>= 2;
14067 /* Branch counts from the next instruction. */
14068 i--;
14069 insn |= i;
14070 /* Branch over the jump. */
14071 md_number_to_chars ((char *)buf, insn, 4);
14072 buf += 4;
14073
14074 /* Nop */
14075 md_number_to_chars ((char*)buf, 0, 4);
14076 buf += 4;
14077
14078 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14079 {
14080 /* beql $0, $0, 2f */
14081 insn = 0x50000000;
14082 /* Compute the PC offset from the current instruction to
14083 the end of the variable frag. */
14084 /* How many bytes in instructions we've already emitted? */
14085 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14086 /* How many bytes in instructions from here to the end? */
14087 i = fragp->fr_var - i;
14088 /* Convert to instruction count. */
14089 i >>= 2;
14090 /* Don't decrement i, because we want to branch over the
14091 delay slot. */
14092
14093 insn |= i;
14094 md_number_to_chars ((char *)buf, insn, 4);
14095 buf += 4;
14096
14097 md_number_to_chars ((char *)buf, 0, 4);
14098 buf += 4;
14099 }
14100
14101 uncond:
14102 if (mips_pic == NO_PIC)
14103 {
14104 /* j or jal. */
14105 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14106 ? 0x0c000000 : 0x08000000);
14107 exp.X_op = O_symbol;
14108 exp.X_add_symbol = fragp->fr_symbol;
14109 exp.X_add_number = fragp->fr_offset;
14110
14111 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14112 4, &exp, 0, BFD_RELOC_MIPS_JMP);
14113 fixp->fx_file = fragp->fr_file;
14114 fixp->fx_line = fragp->fr_line;
14115
14116 md_number_to_chars ((char*)buf, insn, 4);
14117 buf += 4;
14118 }
14119 else
14120 {
14121 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
14122 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14123 exp.X_op = O_symbol;
14124 exp.X_add_symbol = fragp->fr_symbol;
14125 exp.X_add_number = fragp->fr_offset;
14126
14127 if (fragp->fr_offset)
14128 {
14129 exp.X_add_symbol = make_expr_symbol (&exp);
14130 exp.X_add_number = 0;
14131 }
14132
14133 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14134 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14135 fixp->fx_file = fragp->fr_file;
14136 fixp->fx_line = fragp->fr_line;
14137
14138 md_number_to_chars ((char*)buf, insn, 4);
14139 buf += 4;
14140
14141 if (mips_opts.isa == ISA_MIPS1)
14142 {
14143 /* nop */
14144 md_number_to_chars ((char*)buf, 0, 4);
14145 buf += 4;
14146 }
14147
14148 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
14149 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14150
14151 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14152 4, &exp, 0, BFD_RELOC_LO16);
14153 fixp->fx_file = fragp->fr_file;
14154 fixp->fx_line = fragp->fr_line;
14155
14156 md_number_to_chars ((char*)buf, insn, 4);
14157 buf += 4;
14158
14159 /* j(al)r $at. */
14160 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14161 insn = 0x0020f809;
14162 else
14163 insn = 0x00200008;
14164
14165 md_number_to_chars ((char*)buf, insn, 4);
14166 buf += 4;
14167 }
14168 }
14169
14170 assert (buf == (bfd_byte *)fragp->fr_literal
14171 + fragp->fr_fix + fragp->fr_var);
14172
14173 fragp->fr_fix += fragp->fr_var;
14174
14175 return;
14176 }
14177
14178 if (RELAX_MIPS16_P (fragp->fr_subtype))
14179 {
14180 int type;
14181 register const struct mips16_immed_operand *op;
14182 bfd_boolean small, ext;
14183 offsetT val;
14184 bfd_byte *buf;
14185 unsigned long insn;
14186 bfd_boolean use_extend;
14187 unsigned short extend;
14188
14189 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14190 op = mips16_immed_operands;
14191 while (op->type != type)
14192 ++op;
14193
14194 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14195 {
14196 small = FALSE;
14197 ext = TRUE;
14198 }
14199 else
14200 {
14201 small = TRUE;
14202 ext = FALSE;
14203 }
14204
14205 resolve_symbol_value (fragp->fr_symbol);
14206 val = S_GET_VALUE (fragp->fr_symbol);
14207 if (op->pcrel)
14208 {
14209 addressT addr;
14210
14211 addr = fragp->fr_address + fragp->fr_fix;
14212
14213 /* The rules for the base address of a PC relative reloc are
14214 complicated; see mips16_extended_frag. */
14215 if (type == 'p' || type == 'q')
14216 {
14217 addr += 2;
14218 if (ext)
14219 addr += 2;
14220 /* Ignore the low bit in the target, since it will be
14221 set for a text label. */
14222 if ((val & 1) != 0)
14223 --val;
14224 }
14225 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14226 addr -= 4;
14227 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14228 addr -= 2;
14229
14230 addr &= ~ (addressT) ((1 << op->shift) - 1);
14231 val -= addr;
14232
14233 /* Make sure the section winds up with the alignment we have
14234 assumed. */
14235 if (op->shift > 0)
14236 record_alignment (asec, op->shift);
14237 }
14238
14239 if (ext
14240 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14241 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14242 as_warn_where (fragp->fr_file, fragp->fr_line,
14243 _("extended instruction in delay slot"));
14244
14245 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14246
14247 if (target_big_endian)
14248 insn = bfd_getb16 (buf);
14249 else
14250 insn = bfd_getl16 (buf);
14251
14252 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14253 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14254 small, ext, &insn, &use_extend, &extend);
14255
14256 if (use_extend)
14257 {
14258 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
14259 fragp->fr_fix += 2;
14260 buf += 2;
14261 }
14262
14263 md_number_to_chars ((char *) buf, insn, 2);
14264 fragp->fr_fix += 2;
14265 buf += 2;
14266 }
14267 else
14268 {
14269 if (fragp->fr_opcode == NULL)
14270 return;
14271
14272 old = RELAX_OLD (fragp->fr_subtype);
14273 new = RELAX_NEW (fragp->fr_subtype);
14274 fixptr = fragp->fr_literal + fragp->fr_fix;
14275
14276 if (new > 0)
14277 memmove (fixptr - old, fixptr, new);
14278
14279 fragp->fr_fix += new - old;
14280 }
14281 }
14282
14283 #ifdef OBJ_ELF
14284
14285 /* This function is called after the relocs have been generated.
14286 We've been storing mips16 text labels as odd. Here we convert them
14287 back to even for the convenience of the debugger. */
14288
14289 void
14290 mips_frob_file_after_relocs ()
14291 {
14292 asymbol **syms;
14293 unsigned int count, i;
14294
14295 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14296 return;
14297
14298 syms = bfd_get_outsymbols (stdoutput);
14299 count = bfd_get_symcount (stdoutput);
14300 for (i = 0; i < count; i++, syms++)
14301 {
14302 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14303 && ((*syms)->value & 1) != 0)
14304 {
14305 (*syms)->value &= ~1;
14306 /* If the symbol has an odd size, it was probably computed
14307 incorrectly, so adjust that as well. */
14308 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14309 ++elf_symbol (*syms)->internal_elf_sym.st_size;
14310 }
14311 }
14312 }
14313
14314 #endif
14315
14316 /* This function is called whenever a label is defined. It is used
14317 when handling branch delays; if a branch has a label, we assume we
14318 can not move it. */
14319
14320 void
14321 mips_define_label (sym)
14322 symbolS *sym;
14323 {
14324 struct insn_label_list *l;
14325
14326 if (free_insn_labels == NULL)
14327 l = (struct insn_label_list *) xmalloc (sizeof *l);
14328 else
14329 {
14330 l = free_insn_labels;
14331 free_insn_labels = l->next;
14332 }
14333
14334 l->label = sym;
14335 l->next = insn_labels;
14336 insn_labels = l;
14337 }
14338 \f
14339 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14340
14341 /* Some special processing for a MIPS ELF file. */
14342
14343 void
14344 mips_elf_final_processing ()
14345 {
14346 /* Write out the register information. */
14347 if (mips_abi != N64_ABI)
14348 {
14349 Elf32_RegInfo s;
14350
14351 s.ri_gprmask = mips_gprmask;
14352 s.ri_cprmask[0] = mips_cprmask[0];
14353 s.ri_cprmask[1] = mips_cprmask[1];
14354 s.ri_cprmask[2] = mips_cprmask[2];
14355 s.ri_cprmask[3] = mips_cprmask[3];
14356 /* The gp_value field is set by the MIPS ELF backend. */
14357
14358 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14359 ((Elf32_External_RegInfo *)
14360 mips_regmask_frag));
14361 }
14362 else
14363 {
14364 Elf64_Internal_RegInfo s;
14365
14366 s.ri_gprmask = mips_gprmask;
14367 s.ri_pad = 0;
14368 s.ri_cprmask[0] = mips_cprmask[0];
14369 s.ri_cprmask[1] = mips_cprmask[1];
14370 s.ri_cprmask[2] = mips_cprmask[2];
14371 s.ri_cprmask[3] = mips_cprmask[3];
14372 /* The gp_value field is set by the MIPS ELF backend. */
14373
14374 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14375 ((Elf64_External_RegInfo *)
14376 mips_regmask_frag));
14377 }
14378
14379 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14380 sort of BFD interface for this. */
14381 if (mips_any_noreorder)
14382 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14383 if (mips_pic != NO_PIC)
14384 {
14385 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
14386 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14387 }
14388 if (mips_abicalls)
14389 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14390
14391 /* Set MIPS ELF flags for ASEs. */
14392 if (file_ase_mips16)
14393 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
14394 #if 0 /* XXX FIXME */
14395 if (file_ase_mips3d)
14396 elf_elfheader (stdoutput)->e_flags |= ???;
14397 #endif
14398 if (file_ase_mdmx)
14399 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
14400
14401 /* Set the MIPS ELF ABI flags. */
14402 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
14403 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
14404 else if (mips_abi == O64_ABI)
14405 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
14406 else if (mips_abi == EABI_ABI)
14407 {
14408 if (!file_mips_gp32)
14409 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14410 else
14411 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14412 }
14413 else if (mips_abi == N32_ABI)
14414 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14415
14416 /* Nothing to do for N64_ABI. */
14417
14418 if (mips_32bitmode)
14419 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14420 }
14421
14422 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14423 \f
14424 typedef struct proc {
14425 symbolS *isym;
14426 unsigned long reg_mask;
14427 unsigned long reg_offset;
14428 unsigned long fpreg_mask;
14429 unsigned long fpreg_offset;
14430 unsigned long frame_offset;
14431 unsigned long frame_reg;
14432 unsigned long pc_reg;
14433 } procS;
14434
14435 static procS cur_proc;
14436 static procS *cur_proc_ptr;
14437 static int numprocs;
14438
14439 /* Fill in an rs_align_code fragment. */
14440
14441 void
14442 mips_handle_align (fragp)
14443 fragS *fragp;
14444 {
14445 if (fragp->fr_type != rs_align_code)
14446 return;
14447
14448 if (mips_opts.mips16)
14449 {
14450 static const unsigned char be_nop[] = { 0x65, 0x00 };
14451 static const unsigned char le_nop[] = { 0x00, 0x65 };
14452
14453 int bytes;
14454 char *p;
14455
14456 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14457 p = fragp->fr_literal + fragp->fr_fix;
14458
14459 if (bytes & 1)
14460 {
14461 *p++ = 0;
14462 fragp->fr_fix++;
14463 }
14464
14465 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14466 fragp->fr_var = 2;
14467 }
14468
14469 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
14470 }
14471
14472 static void
14473 md_obj_begin ()
14474 {
14475 }
14476
14477 static void
14478 md_obj_end ()
14479 {
14480 /* check for premature end, nesting errors, etc */
14481 if (cur_proc_ptr)
14482 as_warn (_("missing .end at end of assembly"));
14483 }
14484
14485 static long
14486 get_number ()
14487 {
14488 int negative = 0;
14489 long val = 0;
14490
14491 if (*input_line_pointer == '-')
14492 {
14493 ++input_line_pointer;
14494 negative = 1;
14495 }
14496 if (!ISDIGIT (*input_line_pointer))
14497 as_bad (_("expected simple number"));
14498 if (input_line_pointer[0] == '0')
14499 {
14500 if (input_line_pointer[1] == 'x')
14501 {
14502 input_line_pointer += 2;
14503 while (ISXDIGIT (*input_line_pointer))
14504 {
14505 val <<= 4;
14506 val |= hex_value (*input_line_pointer++);
14507 }
14508 return negative ? -val : val;
14509 }
14510 else
14511 {
14512 ++input_line_pointer;
14513 while (ISDIGIT (*input_line_pointer))
14514 {
14515 val <<= 3;
14516 val |= *input_line_pointer++ - '0';
14517 }
14518 return negative ? -val : val;
14519 }
14520 }
14521 if (!ISDIGIT (*input_line_pointer))
14522 {
14523 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14524 *input_line_pointer, *input_line_pointer);
14525 as_warn (_("invalid number"));
14526 return -1;
14527 }
14528 while (ISDIGIT (*input_line_pointer))
14529 {
14530 val *= 10;
14531 val += *input_line_pointer++ - '0';
14532 }
14533 return negative ? -val : val;
14534 }
14535
14536 /* The .file directive; just like the usual .file directive, but there
14537 is an initial number which is the ECOFF file index. In the non-ECOFF
14538 case .file implies DWARF-2. */
14539
14540 static void
14541 s_mips_file (x)
14542 int x ATTRIBUTE_UNUSED;
14543 {
14544 static int first_file_directive = 0;
14545
14546 if (ECOFF_DEBUGGING)
14547 {
14548 get_number ();
14549 s_app_file (0);
14550 }
14551 else
14552 {
14553 char *filename;
14554
14555 filename = dwarf2_directive_file (0);
14556
14557 /* Versions of GCC up to 3.1 start files with a ".file"
14558 directive even for stabs output. Make sure that this
14559 ".file" is handled. Note that you need a version of GCC
14560 after 3.1 in order to support DWARF-2 on MIPS. */
14561 if (filename != NULL && ! first_file_directive)
14562 {
14563 (void) new_logical_line (filename, -1);
14564 s_app_file_string (filename);
14565 }
14566 first_file_directive = 1;
14567 }
14568 }
14569
14570 /* The .loc directive, implying DWARF-2. */
14571
14572 static void
14573 s_mips_loc (x)
14574 int x ATTRIBUTE_UNUSED;
14575 {
14576 if (!ECOFF_DEBUGGING)
14577 dwarf2_directive_loc (0);
14578 }
14579
14580 /* The .end directive. */
14581
14582 static void
14583 s_mips_end (x)
14584 int x ATTRIBUTE_UNUSED;
14585 {
14586 symbolS *p;
14587
14588 /* Following functions need their own .frame and .cprestore directives. */
14589 mips_frame_reg_valid = 0;
14590 mips_cprestore_valid = 0;
14591
14592 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14593 {
14594 p = get_symbol ();
14595 demand_empty_rest_of_line ();
14596 }
14597 else
14598 p = NULL;
14599
14600 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14601 as_warn (_(".end not in text section"));
14602
14603 if (!cur_proc_ptr)
14604 {
14605 as_warn (_(".end directive without a preceding .ent directive."));
14606 demand_empty_rest_of_line ();
14607 return;
14608 }
14609
14610 if (p != NULL)
14611 {
14612 assert (S_GET_NAME (p));
14613 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14614 as_warn (_(".end symbol does not match .ent symbol."));
14615
14616 if (debug_type == DEBUG_STABS)
14617 stabs_generate_asm_endfunc (S_GET_NAME (p),
14618 S_GET_NAME (p));
14619 }
14620 else
14621 as_warn (_(".end directive missing or unknown symbol"));
14622
14623 #ifdef OBJ_ELF
14624 /* Generate a .pdr section. */
14625 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14626 {
14627 segT saved_seg = now_seg;
14628 subsegT saved_subseg = now_subseg;
14629 valueT dot;
14630 expressionS exp;
14631 char *fragp;
14632
14633 dot = frag_now_fix ();
14634
14635 #ifdef md_flush_pending_output
14636 md_flush_pending_output ();
14637 #endif
14638
14639 assert (pdr_seg);
14640 subseg_set (pdr_seg, 0);
14641
14642 /* Write the symbol. */
14643 exp.X_op = O_symbol;
14644 exp.X_add_symbol = p;
14645 exp.X_add_number = 0;
14646 emit_expr (&exp, 4);
14647
14648 fragp = frag_more (7 * 4);
14649
14650 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14651 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14652 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14653 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14654 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14655 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14656 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
14657
14658 subseg_set (saved_seg, saved_subseg);
14659 }
14660 #endif /* OBJ_ELF */
14661
14662 cur_proc_ptr = NULL;
14663 }
14664
14665 /* The .aent and .ent directives. */
14666
14667 static void
14668 s_mips_ent (aent)
14669 int aent;
14670 {
14671 symbolS *symbolP;
14672
14673 symbolP = get_symbol ();
14674 if (*input_line_pointer == ',')
14675 ++input_line_pointer;
14676 SKIP_WHITESPACE ();
14677 if (ISDIGIT (*input_line_pointer)
14678 || *input_line_pointer == '-')
14679 get_number ();
14680
14681 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
14682 as_warn (_(".ent or .aent not in text section."));
14683
14684 if (!aent && cur_proc_ptr)
14685 as_warn (_("missing .end"));
14686
14687 if (!aent)
14688 {
14689 /* This function needs its own .frame and .cprestore directives. */
14690 mips_frame_reg_valid = 0;
14691 mips_cprestore_valid = 0;
14692
14693 cur_proc_ptr = &cur_proc;
14694 memset (cur_proc_ptr, '\0', sizeof (procS));
14695
14696 cur_proc_ptr->isym = symbolP;
14697
14698 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
14699
14700 ++numprocs;
14701
14702 if (debug_type == DEBUG_STABS)
14703 stabs_generate_asm_func (S_GET_NAME (symbolP),
14704 S_GET_NAME (symbolP));
14705 }
14706
14707 demand_empty_rest_of_line ();
14708 }
14709
14710 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
14711 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
14712 s_mips_frame is used so that we can set the PDR information correctly.
14713 We can't use the ecoff routines because they make reference to the ecoff
14714 symbol table (in the mdebug section). */
14715
14716 static void
14717 s_mips_frame (ignore)
14718 int ignore ATTRIBUTE_UNUSED;
14719 {
14720 #ifdef OBJ_ELF
14721 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14722 {
14723 long val;
14724
14725 if (cur_proc_ptr == (procS *) NULL)
14726 {
14727 as_warn (_(".frame outside of .ent"));
14728 demand_empty_rest_of_line ();
14729 return;
14730 }
14731
14732 cur_proc_ptr->frame_reg = tc_get_register (1);
14733
14734 SKIP_WHITESPACE ();
14735 if (*input_line_pointer++ != ','
14736 || get_absolute_expression_and_terminator (&val) != ',')
14737 {
14738 as_warn (_("Bad .frame directive"));
14739 --input_line_pointer;
14740 demand_empty_rest_of_line ();
14741 return;
14742 }
14743
14744 cur_proc_ptr->frame_offset = val;
14745 cur_proc_ptr->pc_reg = tc_get_register (0);
14746
14747 demand_empty_rest_of_line ();
14748 }
14749 else
14750 #endif /* OBJ_ELF */
14751 s_ignore (ignore);
14752 }
14753
14754 /* The .fmask and .mask directives. If the mdebug section is present
14755 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
14756 embedded targets, s_mips_mask is used so that we can set the PDR
14757 information correctly. We can't use the ecoff routines because they
14758 make reference to the ecoff symbol table (in the mdebug section). */
14759
14760 static void
14761 s_mips_mask (reg_type)
14762 char reg_type;
14763 {
14764 #ifdef OBJ_ELF
14765 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14766 {
14767 long mask, off;
14768
14769 if (cur_proc_ptr == (procS *) NULL)
14770 {
14771 as_warn (_(".mask/.fmask outside of .ent"));
14772 demand_empty_rest_of_line ();
14773 return;
14774 }
14775
14776 if (get_absolute_expression_and_terminator (&mask) != ',')
14777 {
14778 as_warn (_("Bad .mask/.fmask directive"));
14779 --input_line_pointer;
14780 demand_empty_rest_of_line ();
14781 return;
14782 }
14783
14784 off = get_absolute_expression ();
14785
14786 if (reg_type == 'F')
14787 {
14788 cur_proc_ptr->fpreg_mask = mask;
14789 cur_proc_ptr->fpreg_offset = off;
14790 }
14791 else
14792 {
14793 cur_proc_ptr->reg_mask = mask;
14794 cur_proc_ptr->reg_offset = off;
14795 }
14796
14797 demand_empty_rest_of_line ();
14798 }
14799 else
14800 #endif /* OBJ_ELF */
14801 s_ignore (reg_type);
14802 }
14803
14804 /* The .loc directive. */
14805
14806 #if 0
14807 static void
14808 s_loc (x)
14809 int x;
14810 {
14811 symbolS *symbolP;
14812 int lineno;
14813 int addroff;
14814
14815 assert (now_seg == text_section);
14816
14817 lineno = get_number ();
14818 addroff = frag_now_fix ();
14819
14820 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14821 S_SET_TYPE (symbolP, N_SLINE);
14822 S_SET_OTHER (symbolP, 0);
14823 S_SET_DESC (symbolP, lineno);
14824 symbolP->sy_segment = now_seg;
14825 }
14826 #endif
14827
14828 /* A table describing all the processors gas knows about. Names are
14829 matched in the order listed.
14830
14831 To ease comparison, please keep this table in the same order as
14832 gcc's mips_cpu_info_table[]. */
14833 static const struct mips_cpu_info mips_cpu_info_table[] =
14834 {
14835 /* Entries for generic ISAs */
14836 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14837 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14838 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14839 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14840 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14841 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
14842 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
14843 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14844
14845 /* MIPS I */
14846 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14847 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14848 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14849
14850 /* MIPS II */
14851 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14852
14853 /* MIPS III */
14854 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14855 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14856 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14857 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14858 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14859 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14860 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14861 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14862 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14863 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14864 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14865 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14866
14867 /* MIPS IV */
14868 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14869 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14870 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14871 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14872 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14873 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14874 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14875 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14876 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14877 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14878 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14879 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14880
14881 /* MIPS 32 */
14882 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14883 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14884 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14885
14886 /* MIPS 64 */
14887 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14888 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
14889
14890 /* Broadcom SB-1 CPU core */
14891 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
14892
14893 /* End marker */
14894 { NULL, 0, 0, 0 }
14895 };
14896
14897
14898 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14899 with a final "000" replaced by "k". Ignore case.
14900
14901 Note: this function is shared between GCC and GAS. */
14902
14903 static bfd_boolean
14904 mips_strict_matching_cpu_name_p (canonical, given)
14905 const char *canonical, *given;
14906 {
14907 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14908 given++, canonical++;
14909
14910 return ((*given == 0 && *canonical == 0)
14911 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14912 }
14913
14914
14915 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14916 CPU name. We've traditionally allowed a lot of variation here.
14917
14918 Note: this function is shared between GCC and GAS. */
14919
14920 static bfd_boolean
14921 mips_matching_cpu_name_p (canonical, given)
14922 const char *canonical, *given;
14923 {
14924 /* First see if the name matches exactly, or with a final "000"
14925 turned into "k". */
14926 if (mips_strict_matching_cpu_name_p (canonical, given))
14927 return TRUE;
14928
14929 /* If not, try comparing based on numerical designation alone.
14930 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14931 if (TOLOWER (*given) == 'r')
14932 given++;
14933 if (!ISDIGIT (*given))
14934 return FALSE;
14935
14936 /* Skip over some well-known prefixes in the canonical name,
14937 hoping to find a number there too. */
14938 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14939 canonical += 2;
14940 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14941 canonical += 2;
14942 else if (TOLOWER (canonical[0]) == 'r')
14943 canonical += 1;
14944
14945 return mips_strict_matching_cpu_name_p (canonical, given);
14946 }
14947
14948
14949 /* Parse an option that takes the name of a processor as its argument.
14950 OPTION is the name of the option and CPU_STRING is the argument.
14951 Return the corresponding processor enumeration if the CPU_STRING is
14952 recognized, otherwise report an error and return null.
14953
14954 A similar function exists in GCC. */
14955
14956 static const struct mips_cpu_info *
14957 mips_parse_cpu (option, cpu_string)
14958 const char *option, *cpu_string;
14959 {
14960 const struct mips_cpu_info *p;
14961
14962 /* 'from-abi' selects the most compatible architecture for the given
14963 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14964 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14965 version. Look first at the -mgp options, if given, otherwise base
14966 the choice on MIPS_DEFAULT_64BIT.
14967
14968 Treat NO_ABI like the EABIs. One reason to do this is that the
14969 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14970 architecture. This code picks MIPS I for 'mips' and MIPS III for
14971 'mips64', just as we did in the days before 'from-abi'. */
14972 if (strcasecmp (cpu_string, "from-abi") == 0)
14973 {
14974 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14975 return mips_cpu_info_from_isa (ISA_MIPS1);
14976
14977 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14978 return mips_cpu_info_from_isa (ISA_MIPS3);
14979
14980 if (file_mips_gp32 >= 0)
14981 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14982
14983 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14984 ? ISA_MIPS3
14985 : ISA_MIPS1);
14986 }
14987
14988 /* 'default' has traditionally been a no-op. Probably not very useful. */
14989 if (strcasecmp (cpu_string, "default") == 0)
14990 return 0;
14991
14992 for (p = mips_cpu_info_table; p->name != 0; p++)
14993 if (mips_matching_cpu_name_p (p->name, cpu_string))
14994 return p;
14995
14996 as_bad ("Bad value (%s) for %s", cpu_string, option);
14997 return 0;
14998 }
14999
15000 /* Return the canonical processor information for ISA (a member of the
15001 ISA_MIPS* enumeration). */
15002
15003 static const struct mips_cpu_info *
15004 mips_cpu_info_from_isa (isa)
15005 int isa;
15006 {
15007 int i;
15008
15009 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15010 if (mips_cpu_info_table[i].is_isa
15011 && isa == mips_cpu_info_table[i].isa)
15012 return (&mips_cpu_info_table[i]);
15013
15014 return NULL;
15015 }
15016 \f
15017 static void
15018 show (stream, string, col_p, first_p)
15019 FILE *stream;
15020 const char *string;
15021 int *col_p;
15022 int *first_p;
15023 {
15024 if (*first_p)
15025 {
15026 fprintf (stream, "%24s", "");
15027 *col_p = 24;
15028 }
15029 else
15030 {
15031 fprintf (stream, ", ");
15032 *col_p += 2;
15033 }
15034
15035 if (*col_p + strlen (string) > 72)
15036 {
15037 fprintf (stream, "\n%24s", "");
15038 *col_p = 24;
15039 }
15040
15041 fprintf (stream, "%s", string);
15042 *col_p += strlen (string);
15043
15044 *first_p = 0;
15045 }
15046
15047 void
15048 md_show_usage (stream)
15049 FILE *stream;
15050 {
15051 int column, first;
15052 size_t i;
15053
15054 fprintf (stream, _("\
15055 MIPS options:\n\
15056 -membedded-pic generate embedded position independent code\n\
15057 -EB generate big endian output\n\
15058 -EL generate little endian output\n\
15059 -g, -g2 do not remove unneeded NOPs or swap branches\n\
15060 -G NUM allow referencing objects up to NUM bytes\n\
15061 implicitly with the gp register [default 8]\n"));
15062 fprintf (stream, _("\
15063 -mips1 generate MIPS ISA I instructions\n\
15064 -mips2 generate MIPS ISA II instructions\n\
15065 -mips3 generate MIPS ISA III instructions\n\
15066 -mips4 generate MIPS ISA IV instructions\n\
15067 -mips5 generate MIPS ISA V instructions\n\
15068 -mips32 generate MIPS32 ISA instructions\n\
15069 -mips32r2 generate MIPS32 release 2 ISA instructions\n\
15070 -mips64 generate MIPS64 ISA instructions\n\
15071 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
15072
15073 first = 1;
15074
15075 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15076 show (stream, mips_cpu_info_table[i].name, &column, &first);
15077 show (stream, "from-abi", &column, &first);
15078 fputc ('\n', stream);
15079
15080 fprintf (stream, _("\
15081 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15082 -no-mCPU don't generate code specific to CPU.\n\
15083 For -mCPU and -no-mCPU, CPU must be one of:\n"));
15084
15085 first = 1;
15086
15087 show (stream, "3900", &column, &first);
15088 show (stream, "4010", &column, &first);
15089 show (stream, "4100", &column, &first);
15090 show (stream, "4650", &column, &first);
15091 fputc ('\n', stream);
15092
15093 fprintf (stream, _("\
15094 -mips16 generate mips16 instructions\n\
15095 -no-mips16 do not generate mips16 instructions\n"));
15096 fprintf (stream, _("\
15097 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
15098 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
15099 -O0 remove unneeded NOPs, do not swap branches\n\
15100 -O remove unneeded NOPs and swap branches\n\
15101 -n warn about NOPs generated from macros\n\
15102 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
15103 --trap, --no-break trap exception on div by 0 and mult overflow\n\
15104 --break, --no-trap break exception on div by 0 and mult overflow\n"));
15105 #ifdef OBJ_ELF
15106 fprintf (stream, _("\
15107 -KPIC, -call_shared generate SVR4 position independent code\n\
15108 -non_shared do not generate position independent code\n\
15109 -xgot assume a 32 bit GOT\n\
15110 -mabi=ABI create ABI conformant object file for:\n"));
15111
15112 first = 1;
15113
15114 show (stream, "32", &column, &first);
15115 show (stream, "o64", &column, &first);
15116 show (stream, "n32", &column, &first);
15117 show (stream, "64", &column, &first);
15118 show (stream, "eabi", &column, &first);
15119
15120 fputc ('\n', stream);
15121
15122 fprintf (stream, _("\
15123 -32 create o32 ABI object file (default)\n\
15124 -n32 create n32 ABI object file\n\
15125 -64 create 64 ABI object file\n"));
15126 #endif
15127 }
15128
15129 enum dwarf2_format
15130 mips_dwarf2_format ()
15131 {
15132 if (mips_abi == N64_ABI)
15133 {
15134 #ifdef TE_IRIX
15135 return dwarf2_format_64bit_irix;
15136 #else
15137 return dwarf2_format_64bit;
15138 #endif
15139 }
15140 else
15141 return dwarf2_format_32bit;
15142 }
This page took 0.51734 seconds and 4 git commands to generate.