Add support for ia64-hpux target.
[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
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
41 #ifdef DEBUG
42 #define DBG(x) printf x
43 #else
44 #define DBG(x)
45 #endif
46
47 #ifdef OBJ_MAYBE_ELF
48 /* Clean up namespace so we can include obj-elf.h too. */
49 static int mips_output_flavor PARAMS ((void));
50 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51 #undef OBJ_PROCESS_STAB
52 #undef OUTPUT_FLAVOR
53 #undef S_GET_ALIGN
54 #undef S_GET_SIZE
55 #undef S_SET_ALIGN
56 #undef S_SET_SIZE
57 #undef obj_frob_file
58 #undef obj_frob_file_after_relocs
59 #undef obj_frob_symbol
60 #undef obj_pop_insert
61 #undef obj_sec_sym_ok_for_reloc
62 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64 #include "obj-elf.h"
65 /* Fix any of them that we actually care about. */
66 #undef OUTPUT_FLAVOR
67 #define OUTPUT_FLAVOR mips_output_flavor()
68 #endif
69
70 #if defined (OBJ_ELF)
71 #include "elf/mips.h"
72 #endif
73
74 #ifndef ECOFF_DEBUGGING
75 #define NO_ECOFF_DEBUGGING
76 #define ECOFF_DEBUGGING 0
77 #endif
78
79 #include "ecoff.h"
80
81 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82 static char *mips_regmask_frag;
83 #endif
84
85 #define AT 1
86 #define TREG 24
87 #define PIC_CALL_REG 25
88 #define KT0 26
89 #define KT1 27
90 #define GP 28
91 #define SP 29
92 #define FP 30
93 #define RA 31
94
95 #define ILLEGAL_REG (32)
96
97 /* Allow override of standard little-endian ECOFF format. */
98
99 #ifndef ECOFF_LITTLE_FORMAT
100 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101 #endif
102
103 extern int target_big_endian;
104
105 /* The name of the readonly data section. */
106 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107 ? ".data" \
108 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109 ? ".rdata" \
110 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111 ? ".rdata" \
112 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113 ? ".rodata" \
114 : (abort (), ""))
115
116 /* The ABI to use. */
117 enum mips_abi_level
118 {
119 NO_ABI = 0,
120 O32_ABI,
121 O64_ABI,
122 N32_ABI,
123 N64_ABI,
124 EABI_ABI
125 };
126
127 /* MIPS ABI we are using for this output file. */
128 static enum mips_abi_level file_mips_abi = NO_ABI;
129
130 /* This is the set of options which may be modified by the .set
131 pseudo-op. We use a struct so that .set push and .set pop are more
132 reliable. */
133
134 struct mips_set_options
135 {
136 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
137 if it has not been initialized. Changed by `.set mipsN', and the
138 -mipsN command line option, and the default CPU. */
139 int isa;
140 /* Whether we are assembling for the mips16 processor. 0 if we are
141 not, 1 if we are, and -1 if the value has not been initialized.
142 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
143 -nomips16 command line options, and the default CPU. */
144 int mips16;
145 /* Non-zero if we should not reorder instructions. Changed by `.set
146 reorder' and `.set noreorder'. */
147 int noreorder;
148 /* Non-zero if we should not permit the $at ($1) register to be used
149 in instructions. Changed by `.set at' and `.set noat'. */
150 int noat;
151 /* Non-zero if we should warn when a macro instruction expands into
152 more than one machine instruction. Changed by `.set nomacro' and
153 `.set macro'. */
154 int warn_about_macros;
155 /* Non-zero if we should not move instructions. Changed by `.set
156 move', `.set volatile', `.set nomove', and `.set novolatile'. */
157 int nomove;
158 /* Non-zero if we should not optimize branches by moving the target
159 of the branch into the delay slot. Actually, we don't perform
160 this optimization anyhow. Changed by `.set bopt' and `.set
161 nobopt'. */
162 int nobopt;
163 /* Non-zero if we should not autoextend mips16 instructions.
164 Changed by `.set autoextend' and `.set noautoextend'. */
165 int noautoextend;
166 /* Restrict general purpose registers and floating point registers
167 to 32 bit. This is initially determined when -mgp32 or -mfp32
168 is passed but can changed if the assembler code uses .set mipsN. */
169 int gp32;
170 int fp32;
171 /* The ABI currently in use. This is changed by .set mipsN to loosen
172 restrictions and doesn't affect the whole file. */
173 enum mips_abi_level abi;
174 };
175
176 /* True if -mgp32 was passed. */
177 static int file_mips_gp32 = -1;
178
179 /* True if -mfp32 was passed. */
180 static int file_mips_fp32 = -1;
181
182 /* This is the struct we use to hold the current set of options. Note
183 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
184 -1 to indicate that they have not been initialized. */
185
186 static struct mips_set_options mips_opts =
187 {
188 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
189 };
190
191 /* These variables are filled in with the masks of registers used.
192 The object format code reads them and puts them in the appropriate
193 place. */
194 unsigned long mips_gprmask;
195 unsigned long mips_cprmask[4];
196
197 /* MIPS ISA we are using for this output file. */
198 static int file_mips_isa = ISA_UNKNOWN;
199
200 /* The argument of the -mcpu= flag. Historical for code generation. */
201 static int mips_cpu = CPU_UNKNOWN;
202
203 /* The argument of the -march= flag. The architecture we are assembling. */
204 static int mips_arch = CPU_UNKNOWN;
205
206 /* The argument of the -mtune= flag. The architecture for which we
207 are optimizing. */
208 static int mips_tune = CPU_UNKNOWN;
209
210 /* Whether we should mark the file EABI64 or EABI32. */
211 static int mips_eabi64 = 0;
212
213 /* If they asked for mips1 or mips2 and a cpu that is
214 mips3 or greater, then mark the object file 32BITMODE. */
215 static int mips_32bitmode = 0;
216
217 /* Some ISA's have delay slots for instructions which read or write
218 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
219 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
220 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
221 delay slot in this ISA. The uses of this macro assume that any
222 ISA that has delay slots for one of these, has them for all. They
223 also assume that ISAs which don't have delays for these insns, don't
224 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
225 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
226 (ISA) == ISA_MIPS1 \
227 || (ISA) == ISA_MIPS2 \
228 || (ISA) == ISA_MIPS3 \
229 )
230
231 /* Return true if ISA supports 64 bit gp register instructions. */
232 #define ISA_HAS_64BIT_REGS(ISA) ( \
233 (ISA) == ISA_MIPS3 \
234 || (ISA) == ISA_MIPS4 \
235 || (ISA) == ISA_MIPS5 \
236 || (ISA) == ISA_MIPS64 \
237 )
238
239 #define HAVE_32BIT_GPRS \
240 (mips_opts.gp32 \
241 || mips_opts.abi == O32_ABI \
242 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
243
244 #define HAVE_32BIT_FPRS \
245 (mips_opts.fp32 \
246 || mips_opts.abi == O32_ABI \
247 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
248
249 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
250 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
251
252 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
253
254 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
255
256 /* We can only have 64bit addresses if the object file format
257 supports it. */
258 #define HAVE_32BIT_ADDRESSES \
259 (HAVE_32BIT_GPRS \
260 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
261 || ! HAVE_64BIT_OBJECTS) \
262 && mips_pic != EMBEDDED_PIC))
263
264 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
265
266 /* Whether the processor uses hardware interlocks to protect
267 reads from the HI and LO registers, and thus does not
268 require nops to be inserted. */
269
270 #define hilo_interlocks (mips_arch == CPU_R4010 \
271 || mips_arch == CPU_SB1 \
272 )
273
274 /* Whether the processor uses hardware interlocks to protect reads
275 from the GPRs, and thus does not require nops to be inserted. */
276 #define gpr_interlocks \
277 (mips_opts.isa != ISA_MIPS1 \
278 || mips_arch == CPU_R3900)
279
280 /* As with other "interlocks" this is used by hardware that has FP
281 (co-processor) interlocks. */
282 /* Itbl support may require additional care here. */
283 #define cop_interlocks (mips_arch == CPU_R4300 \
284 || mips_arch == CPU_SB1 \
285 )
286
287 /* Is this a mfhi or mflo instruction? */
288 #define MF_HILO_INSN(PINFO) \
289 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
290
291 /* MIPS PIC level. */
292
293 enum mips_pic_level
294 {
295 /* Do not generate PIC code. */
296 NO_PIC,
297
298 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
299 not sure what it is supposed to do. */
300 IRIX4_PIC,
301
302 /* Generate PIC code as in the SVR4 MIPS ABI. */
303 SVR4_PIC,
304
305 /* Generate PIC code without using a global offset table: the data
306 segment has a maximum size of 64K, all data references are off
307 the $gp register, and all text references are PC relative. This
308 is used on some embedded systems. */
309 EMBEDDED_PIC
310 };
311
312 static enum mips_pic_level mips_pic;
313
314 /* Warn about all NOPS that the assembler generates. */
315 static int warn_nops = 0;
316
317 /* 1 if we should generate 32 bit offsets from the GP register in
318 SVR4_PIC mode. Currently has no meaning in other modes. */
319 static int mips_big_got;
320
321 /* 1 if trap instructions should used for overflow rather than break
322 instructions. */
323 static int mips_trap;
324
325 /* 1 if double width floating point constants should not be constructed
326 by assembling two single width halves into two single width floating
327 point registers which just happen to alias the double width destination
328 register. On some architectures this aliasing can be disabled by a bit
329 in the status register, and the setting of this bit cannot be determined
330 automatically at assemble time. */
331 static int mips_disable_float_construction;
332
333 /* Non-zero if any .set noreorder directives were used. */
334
335 static int mips_any_noreorder;
336
337 /* Non-zero if nops should be inserted when the register referenced in
338 an mfhi/mflo instruction is read in the next two instructions. */
339 static int mips_7000_hilo_fix;
340
341 /* The size of the small data section. */
342 static unsigned int g_switch_value = 8;
343 /* Whether the -G option was used. */
344 static int g_switch_seen = 0;
345
346 #define N_RMASK 0xc4
347 #define N_VFP 0xd4
348
349 /* If we can determine in advance that GP optimization won't be
350 possible, we can skip the relaxation stuff that tries to produce
351 GP-relative references. This makes delay slot optimization work
352 better.
353
354 This function can only provide a guess, but it seems to work for
355 gcc output. It needs to guess right for gcc, otherwise gcc
356 will put what it thinks is a GP-relative instruction in a branch
357 delay slot.
358
359 I don't know if a fix is needed for the SVR4_PIC mode. I've only
360 fixed it for the non-PIC mode. KR 95/04/07 */
361 static int nopic_need_relax PARAMS ((symbolS *, int));
362
363 /* handle of the OPCODE hash table */
364 static struct hash_control *op_hash = NULL;
365
366 /* The opcode hash table we use for the mips16. */
367 static struct hash_control *mips16_op_hash = NULL;
368
369 /* This array holds the chars that always start a comment. If the
370 pre-processor is disabled, these aren't very useful */
371 const char comment_chars[] = "#";
372
373 /* This array holds the chars that only start a comment at the beginning of
374 a line. If the line seems to have the form '# 123 filename'
375 .line and .file directives will appear in the pre-processed output */
376 /* Note that input_file.c hand checks for '#' at the beginning of the
377 first line of the input file. This is because the compiler outputs
378 #NO_APP at the beginning of its output. */
379 /* Also note that C style comments are always supported. */
380 const char line_comment_chars[] = "#";
381
382 /* This array holds machine specific line separator characters. */
383 const char line_separator_chars[] = ";";
384
385 /* Chars that can be used to separate mant from exp in floating point nums */
386 const char EXP_CHARS[] = "eE";
387
388 /* Chars that mean this number is a floating point constant */
389 /* As in 0f12.456 */
390 /* or 0d1.2345e12 */
391 const char FLT_CHARS[] = "rRsSfFdDxXpP";
392
393 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
394 changed in read.c . Ideally it shouldn't have to know about it at all,
395 but nothing is ideal around here.
396 */
397
398 static char *insn_error;
399
400 static int auto_align = 1;
401
402 /* When outputting SVR4 PIC code, the assembler needs to know the
403 offset in the stack frame from which to restore the $gp register.
404 This is set by the .cprestore pseudo-op, and saved in this
405 variable. */
406 static offsetT mips_cprestore_offset = -1;
407
408 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
409 more optimizations, it can use a register value instead of a memory-saved
410 offset and even an other register than $gp as global pointer. */
411 static offsetT mips_cpreturn_offset = -1;
412 static int mips_cpreturn_register = -1;
413 static int mips_gp_register = GP;
414
415 /* Whether mips_cprestore_offset has been set in the current function
416 (or whether it has already been warned about, if not). */
417 static int mips_cprestore_valid = 0;
418
419 /* This is the register which holds the stack frame, as set by the
420 .frame pseudo-op. This is needed to implement .cprestore. */
421 static int mips_frame_reg = SP;
422
423 /* Whether mips_frame_reg has been set in the current function
424 (or whether it has already been warned about, if not). */
425 static int mips_frame_reg_valid = 0;
426
427 /* To output NOP instructions correctly, we need to keep information
428 about the previous two instructions. */
429
430 /* Whether we are optimizing. The default value of 2 means to remove
431 unneeded NOPs and swap branch instructions when possible. A value
432 of 1 means to not swap branches. A value of 0 means to always
433 insert NOPs. */
434 static int mips_optimize = 2;
435
436 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
437 equivalent to seeing no -g option at all. */
438 static int mips_debug = 0;
439
440 /* The previous instruction. */
441 static struct mips_cl_insn prev_insn;
442
443 /* The instruction before prev_insn. */
444 static struct mips_cl_insn prev_prev_insn;
445
446 /* If we don't want information for prev_insn or prev_prev_insn, we
447 point the insn_mo field at this dummy integer. */
448 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
449
450 /* Non-zero if prev_insn is valid. */
451 static int prev_insn_valid;
452
453 /* The frag for the previous instruction. */
454 static struct frag *prev_insn_frag;
455
456 /* The offset into prev_insn_frag for the previous instruction. */
457 static long prev_insn_where;
458
459 /* The reloc type for the previous instruction, if any. */
460 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
461
462 /* The reloc for the previous instruction, if any. */
463 static fixS *prev_insn_fixp[3];
464
465 /* Non-zero if the previous instruction was in a delay slot. */
466 static int prev_insn_is_delay_slot;
467
468 /* Non-zero if the previous instruction was in a .set noreorder. */
469 static int prev_insn_unreordered;
470
471 /* Non-zero if the previous instruction uses an extend opcode (if
472 mips16). */
473 static int prev_insn_extended;
474
475 /* Non-zero if the previous previous instruction was in a .set
476 noreorder. */
477 static int prev_prev_insn_unreordered;
478
479 /* If this is set, it points to a frag holding nop instructions which
480 were inserted before the start of a noreorder section. If those
481 nops turn out to be unnecessary, the size of the frag can be
482 decreased. */
483 static fragS *prev_nop_frag;
484
485 /* The number of nop instructions we created in prev_nop_frag. */
486 static int prev_nop_frag_holds;
487
488 /* The number of nop instructions that we know we need in
489 prev_nop_frag. */
490 static int prev_nop_frag_required;
491
492 /* The number of instructions we've seen since prev_nop_frag. */
493 static int prev_nop_frag_since;
494
495 /* For ECOFF and ELF, relocations against symbols are done in two
496 parts, with a HI relocation and a LO relocation. Each relocation
497 has only 16 bits of space to store an addend. This means that in
498 order for the linker to handle carries correctly, it must be able
499 to locate both the HI and the LO relocation. This means that the
500 relocations must appear in order in the relocation table.
501
502 In order to implement this, we keep track of each unmatched HI
503 relocation. We then sort them so that they immediately precede the
504 corresponding LO relocation. */
505
506 struct mips_hi_fixup
507 {
508 /* Next HI fixup. */
509 struct mips_hi_fixup *next;
510 /* This fixup. */
511 fixS *fixp;
512 /* The section this fixup is in. */
513 segT seg;
514 };
515
516 /* The list of unmatched HI relocs. */
517
518 static struct mips_hi_fixup *mips_hi_fixup_list;
519
520 /* Map normal MIPS register numbers to mips16 register numbers. */
521
522 #define X ILLEGAL_REG
523 static const int mips32_to_16_reg_map[] =
524 {
525 X, X, 2, 3, 4, 5, 6, 7,
526 X, X, X, X, X, X, X, X,
527 0, 1, X, X, X, X, X, X,
528 X, X, X, X, X, X, X, X
529 };
530 #undef X
531
532 /* Map mips16 register numbers to normal MIPS register numbers. */
533
534 static const unsigned int mips16_to_32_reg_map[] =
535 {
536 16, 17, 2, 3, 4, 5, 6, 7
537 };
538 \f
539 /* Since the MIPS does not have multiple forms of PC relative
540 instructions, we do not have to do relaxing as is done on other
541 platforms. However, we do have to handle GP relative addressing
542 correctly, which turns out to be a similar problem.
543
544 Every macro that refers to a symbol can occur in (at least) two
545 forms, one with GP relative addressing and one without. For
546 example, loading a global variable into a register generally uses
547 a macro instruction like this:
548 lw $4,i
549 If i can be addressed off the GP register (this is true if it is in
550 the .sbss or .sdata section, or if it is known to be smaller than
551 the -G argument) this will generate the following instruction:
552 lw $4,i($gp)
553 This instruction will use a GPREL reloc. If i can not be addressed
554 off the GP register, the following instruction sequence will be used:
555 lui $at,i
556 lw $4,i($at)
557 In this case the first instruction will have a HI16 reloc, and the
558 second reloc will have a LO16 reloc. Both relocs will be against
559 the symbol i.
560
561 The issue here is that we may not know whether i is GP addressable
562 until after we see the instruction that uses it. Therefore, we
563 want to be able to choose the final instruction sequence only at
564 the end of the assembly. This is similar to the way other
565 platforms choose the size of a PC relative instruction only at the
566 end of assembly.
567
568 When generating position independent code we do not use GP
569 addressing in quite the same way, but the issue still arises as
570 external symbols and local symbols must be handled differently.
571
572 We handle these issues by actually generating both possible
573 instruction sequences. The longer one is put in a frag_var with
574 type rs_machine_dependent. We encode what to do with the frag in
575 the subtype field. We encode (1) the number of existing bytes to
576 replace, (2) the number of new bytes to use, (3) the offset from
577 the start of the existing bytes to the first reloc we must generate
578 (that is, the offset is applied from the start of the existing
579 bytes after they are replaced by the new bytes, if any), (4) the
580 offset from the start of the existing bytes to the second reloc,
581 (5) whether a third reloc is needed (the third reloc is always four
582 bytes after the second reloc), and (6) whether to warn if this
583 variant is used (this is sometimes needed if .set nomacro or .set
584 noat is in effect). All these numbers are reasonably small.
585
586 Generating two instruction sequences must be handled carefully to
587 ensure that delay slots are handled correctly. Fortunately, there
588 are a limited number of cases. When the second instruction
589 sequence is generated, append_insn is directed to maintain the
590 existing delay slot information, so it continues to apply to any
591 code after the second instruction sequence. This means that the
592 second instruction sequence must not impose any requirements not
593 required by the first instruction sequence.
594
595 These variant frags are then handled in functions called by the
596 machine independent code. md_estimate_size_before_relax returns
597 the final size of the frag. md_convert_frag sets up the final form
598 of the frag. tc_gen_reloc adjust the first reloc and adds a second
599 one if needed. */
600 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
601 ((relax_substateT) \
602 (((old) << 23) \
603 | ((new) << 16) \
604 | (((reloc1) + 64) << 9) \
605 | (((reloc2) + 64) << 2) \
606 | ((reloc3) ? (1 << 1) : 0) \
607 | ((warn) ? 1 : 0)))
608 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
609 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
610 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
611 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
612 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
613 #define RELAX_WARN(i) ((i) & 1)
614
615 /* For mips16 code, we use an entirely different form of relaxation.
616 mips16 supports two versions of most instructions which take
617 immediate values: a small one which takes some small value, and a
618 larger one which takes a 16 bit value. Since branches also follow
619 this pattern, relaxing these values is required.
620
621 We can assemble both mips16 and normal MIPS code in a single
622 object. Therefore, we need to support this type of relaxation at
623 the same time that we support the relaxation described above. We
624 use the high bit of the subtype field to distinguish these cases.
625
626 The information we store for this type of relaxation is the
627 argument code found in the opcode file for this relocation, whether
628 the user explicitly requested a small or extended form, and whether
629 the relocation is in a jump or jal delay slot. That tells us the
630 size of the value, and how it should be stored. We also store
631 whether the fragment is considered to be extended or not. We also
632 store whether this is known to be a branch to a different section,
633 whether we have tried to relax this frag yet, and whether we have
634 ever extended a PC relative fragment because of a shift count. */
635 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
636 (0x80000000 \
637 | ((type) & 0xff) \
638 | ((small) ? 0x100 : 0) \
639 | ((ext) ? 0x200 : 0) \
640 | ((dslot) ? 0x400 : 0) \
641 | ((jal_dslot) ? 0x800 : 0))
642 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
643 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
644 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
645 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
646 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
647 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
648 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
649 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
650 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
651 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
652 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
653 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
654 \f
655 /* Prototypes for static functions. */
656
657 #ifdef __STDC__
658 #define internalError() \
659 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
660 #else
661 #define internalError() as_fatal (_("MIPS internal Error"));
662 #endif
663
664 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
665
666 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
667 unsigned int reg, enum mips_regclass class));
668 static int reg_needs_delay PARAMS ((unsigned int));
669 static void mips16_mark_labels PARAMS ((void));
670 static void append_insn PARAMS ((char *place,
671 struct mips_cl_insn * ip,
672 expressionS * p,
673 bfd_reloc_code_real_type *r,
674 boolean));
675 static void mips_no_prev_insn PARAMS ((int));
676 static void mips_emit_delays PARAMS ((boolean));
677 #ifdef USE_STDARG
678 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
679 const char *name, const char *fmt,
680 ...));
681 #else
682 static void macro_build ();
683 #endif
684 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
685 const char *, const char *,
686 va_list));
687 static void macro_build_lui PARAMS ((char *place, int *counter,
688 expressionS * ep, int regnum));
689 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
690 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
691 expressionS *));
692 static void load_register PARAMS ((int *, int, expressionS *, int));
693 static void load_address PARAMS ((int *, int, expressionS *, int, int *));
694 static void move_register PARAMS ((int *, int, int));
695 static void macro PARAMS ((struct mips_cl_insn * ip));
696 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
697 #ifdef LOSING_COMPILER
698 static void macro2 PARAMS ((struct mips_cl_insn * ip));
699 #endif
700 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
701 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
702 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
703 boolean, boolean, unsigned long *,
704 boolean *, unsigned short *));
705 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
706 static int my_getSmallExpression PARAMS ((expressionS *, char *));
707 static void my_getExpression PARAMS ((expressionS *, char *));
708 #ifdef OBJ_ELF
709 static int support_64bit_objects PARAMS((void));
710 #endif
711 static symbolS *get_symbol PARAMS ((void));
712 static void mips_align PARAMS ((int to, int fill, symbolS *label));
713 static void s_align PARAMS ((int));
714 static void s_change_sec PARAMS ((int));
715 static void s_cons PARAMS ((int));
716 static void s_float_cons PARAMS ((int));
717 static void s_mips_globl PARAMS ((int));
718 static void s_option PARAMS ((int));
719 static void s_mipsset PARAMS ((int));
720 static void s_abicalls PARAMS ((int));
721 static void s_cpload PARAMS ((int));
722 static void s_cpsetup PARAMS ((int));
723 static void s_cplocal PARAMS ((int));
724 static void s_cprestore PARAMS ((int));
725 static void s_cpreturn PARAMS ((int));
726 static void s_gpvalue PARAMS ((int));
727 static void s_gpword PARAMS ((int));
728 static void s_cpadd PARAMS ((int));
729 static void s_insn PARAMS ((int));
730 static void md_obj_begin PARAMS ((void));
731 static void md_obj_end PARAMS ((void));
732 static long get_number PARAMS ((void));
733 static void s_mips_ent PARAMS ((int));
734 static void s_mips_end PARAMS ((int));
735 static void s_mips_frame PARAMS ((int));
736 static void s_mips_mask PARAMS ((int));
737 static void s_mips_stab PARAMS ((int));
738 static void s_mips_weakext PARAMS ((int));
739 static void s_file PARAMS ((int));
740 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
741 static const char *mips_isa_to_str PARAMS ((int));
742 static const char *mips_cpu_to_str PARAMS ((int));
743 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
744 static void show PARAMS ((FILE *, char *, int *, int *));
745 #ifdef OBJ_ELF
746 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
747 #endif
748
749 /* Return values of my_getSmallExpression(). */
750
751 enum small_ex_type
752 {
753 S_EX_NONE = 0,
754 S_EX_REGISTER,
755
756 /* Direct relocation creation by %percent_op(). */
757 S_EX_HALF,
758 S_EX_HI,
759 S_EX_LO,
760 S_EX_GP_REL,
761 S_EX_GOT,
762 S_EX_CALL16,
763 S_EX_GOT_DISP,
764 S_EX_GOT_PAGE,
765 S_EX_GOT_OFST,
766 S_EX_GOT_HI,
767 S_EX_GOT_LO,
768 S_EX_NEG,
769 S_EX_HIGHER,
770 S_EX_HIGHEST,
771 S_EX_CALL_HI,
772 S_EX_CALL_LO
773 };
774
775 /* Table and functions used to map between CPU/ISA names, and
776 ISA levels, and CPU numbers. */
777
778 struct mips_cpu_info
779 {
780 const char *name; /* CPU or ISA name. */
781 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
782 int isa; /* ISA level. */
783 int cpu; /* CPU number (default CPU if ISA). */
784 };
785
786 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
787 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
788 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
789 \f
790 /* Pseudo-op table.
791
792 The following pseudo-ops from the Kane and Heinrich MIPS book
793 should be defined here, but are currently unsupported: .alias,
794 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
795
796 The following pseudo-ops from the Kane and Heinrich MIPS book are
797 specific to the type of debugging information being generated, and
798 should be defined by the object format: .aent, .begin, .bend,
799 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
800 .vreg.
801
802 The following pseudo-ops from the Kane and Heinrich MIPS book are
803 not MIPS CPU specific, but are also not specific to the object file
804 format. This file is probably the best place to define them, but
805 they are not currently supported: .asm0, .endr, .lab, .repeat,
806 .struct. */
807
808 static const pseudo_typeS mips_pseudo_table[] =
809 {
810 /* MIPS specific pseudo-ops. */
811 {"option", s_option, 0},
812 {"set", s_mipsset, 0},
813 {"rdata", s_change_sec, 'r'},
814 {"sdata", s_change_sec, 's'},
815 {"livereg", s_ignore, 0},
816 {"abicalls", s_abicalls, 0},
817 {"cpload", s_cpload, 0},
818 {"cpsetup", s_cpsetup, 0},
819 {"cplocal", s_cplocal, 0},
820 {"cprestore", s_cprestore, 0},
821 {"cpreturn", s_cpreturn, 0},
822 {"gpvalue", s_gpvalue, 0},
823 {"gpword", s_gpword, 0},
824 {"cpadd", s_cpadd, 0},
825 {"insn", s_insn, 0},
826
827 /* Relatively generic pseudo-ops that happen to be used on MIPS
828 chips. */
829 {"asciiz", stringer, 1},
830 {"bss", s_change_sec, 'b'},
831 {"err", s_err, 0},
832 {"half", s_cons, 1},
833 {"dword", s_cons, 3},
834 {"weakext", s_mips_weakext, 0},
835
836 /* These pseudo-ops are defined in read.c, but must be overridden
837 here for one reason or another. */
838 {"align", s_align, 0},
839 {"byte", s_cons, 0},
840 {"data", s_change_sec, 'd'},
841 {"double", s_float_cons, 'd'},
842 {"float", s_float_cons, 'f'},
843 {"globl", s_mips_globl, 0},
844 {"global", s_mips_globl, 0},
845 {"hword", s_cons, 1},
846 {"int", s_cons, 2},
847 {"long", s_cons, 2},
848 {"octa", s_cons, 4},
849 {"quad", s_cons, 3},
850 {"short", s_cons, 1},
851 {"single", s_float_cons, 'f'},
852 {"stabn", s_mips_stab, 'n'},
853 {"text", s_change_sec, 't'},
854 {"word", s_cons, 2},
855
856 #ifdef MIPS_STABS_ELF
857 { "extern", ecoff_directive_extern, 0},
858 #endif
859
860 { NULL, NULL, 0 },
861 };
862
863 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
864 {
865 /* These pseudo-ops should be defined by the object file format.
866 However, a.out doesn't support them, so we have versions here. */
867 {"aent", s_mips_ent, 1},
868 {"bgnb", s_ignore, 0},
869 {"end", s_mips_end, 0},
870 {"endb", s_ignore, 0},
871 {"ent", s_mips_ent, 0},
872 {"file", s_file, 0},
873 {"fmask", s_mips_mask, 'F'},
874 {"frame", s_mips_frame, 0},
875 {"loc", s_ignore, 0},
876 {"mask", s_mips_mask, 'R'},
877 {"verstamp", s_ignore, 0},
878 { NULL, NULL, 0 },
879 };
880
881 extern void pop_insert PARAMS ((const pseudo_typeS *));
882
883 void
884 mips_pop_insert ()
885 {
886 pop_insert (mips_pseudo_table);
887 if (! ECOFF_DEBUGGING)
888 pop_insert (mips_nonecoff_pseudo_table);
889 }
890 \f
891 /* Symbols labelling the current insn. */
892
893 struct insn_label_list
894 {
895 struct insn_label_list *next;
896 symbolS *label;
897 };
898
899 static struct insn_label_list *insn_labels;
900 static struct insn_label_list *free_insn_labels;
901
902 static void mips_clear_insn_labels PARAMS ((void));
903
904 static inline void
905 mips_clear_insn_labels ()
906 {
907 register struct insn_label_list **pl;
908
909 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
910 ;
911 *pl = insn_labels;
912 insn_labels = NULL;
913 }
914 \f
915 static char *expr_end;
916
917 /* Expressions which appear in instructions. These are set by
918 mips_ip. */
919
920 static expressionS imm_expr;
921 static expressionS offset_expr;
922
923 /* Relocs associated with imm_expr and offset_expr. */
924
925 static bfd_reloc_code_real_type imm_reloc[3]
926 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
927 static bfd_reloc_code_real_type offset_reloc[3]
928 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
929
930 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
931
932 static boolean imm_unmatched_hi;
933
934 /* These are set by mips16_ip if an explicit extension is used. */
935
936 static boolean mips16_small, mips16_ext;
937
938 #ifdef MIPS_STABS_ELF
939 /* The pdr segment for per procedure frame/regmask info */
940
941 static segT pdr_seg;
942 #endif
943
944 static const char *
945 mips_isa_to_str (isa)
946 int isa;
947 {
948 const struct mips_cpu_info *ci;
949 static char s[20];
950
951 ci = mips_cpu_info_from_isa (isa);
952 if (ci != NULL)
953 return (ci->name);
954
955 sprintf (s, "ISA#%d", isa);
956 return s;
957 }
958
959 static const char *
960 mips_cpu_to_str (cpu)
961 int cpu;
962 {
963 const struct mips_cpu_info *ci;
964 static char s[16];
965
966 ci = mips_cpu_info_from_cpu (cpu);
967 if (ci != NULL)
968 return (ci->name);
969
970 sprintf (s, "CPU#%d", cpu);
971 return s;
972 }
973
974 /* The default target format to use. */
975
976 const char *
977 mips_target_format ()
978 {
979 switch (OUTPUT_FLAVOR)
980 {
981 case bfd_target_aout_flavour:
982 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
983 case bfd_target_ecoff_flavour:
984 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
985 case bfd_target_coff_flavour:
986 return "pe-mips";
987 case bfd_target_elf_flavour:
988 #ifdef TE_TMIPS
989 /* This is traditional mips */
990 return (target_big_endian
991 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
992 : "elf32-tradbigmips")
993 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
994 : "elf32-tradlittlemips"));
995 #else
996 return (target_big_endian
997 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
998 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
999 : "elf32-littlemips"));
1000 #endif
1001 default:
1002 abort ();
1003 return NULL;
1004 }
1005 }
1006
1007 /* This function is called once, at assembler startup time. It should
1008 set up all the tables, etc. that the MD part of the assembler will need. */
1009
1010 void
1011 md_begin ()
1012 {
1013 register const char *retval = NULL;
1014 int i = 0;
1015 const char *cpu;
1016 char *a = NULL;
1017 int broken = 0;
1018 int mips_isa_from_cpu;
1019 int target_cpu_had_mips16 = 0;
1020 const struct mips_cpu_info *ci;
1021
1022 /* GP relative stuff not working for PE */
1023 if (strncmp (TARGET_OS, "pe", 2) == 0
1024 && g_switch_value != 0)
1025 {
1026 if (g_switch_seen)
1027 as_bad (_("-G not supported in this configuration."));
1028 g_switch_value = 0;
1029 }
1030
1031 cpu = TARGET_CPU;
1032 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1033 {
1034 a = xmalloc (sizeof TARGET_CPU);
1035 strcpy (a, TARGET_CPU);
1036 a[(sizeof TARGET_CPU) - 3] = '\0';
1037 cpu = a;
1038 }
1039
1040 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1041 {
1042 target_cpu_had_mips16 = 1;
1043 cpu += sizeof "mips16" - 1;
1044 }
1045
1046 if (mips_opts.mips16 < 0)
1047 mips_opts.mips16 = target_cpu_had_mips16;
1048
1049 /* Backward compatibility for historic -mcpu= option. Check for
1050 incompatible options, warn if -mcpu is used. */
1051 if (mips_cpu != CPU_UNKNOWN
1052 && mips_arch != CPU_UNKNOWN
1053 && mips_cpu != mips_arch)
1054 {
1055 as_fatal (_("The -mcpu option can't be used together with -march. "
1056 "Use -mtune instead of -mcpu."));
1057 }
1058
1059 if (mips_cpu != CPU_UNKNOWN
1060 && mips_tune != CPU_UNKNOWN
1061 && mips_cpu != mips_tune)
1062 {
1063 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1064 "Use -march instead of -mcpu."));
1065 }
1066
1067 #if 1
1068 /* For backward compatibility, let -mipsN set various defaults. */
1069 /* This code should go away, to be replaced with something rather more
1070 draconian. Until GCC 3.1 has been released for some reasonable
1071 amount of time, however, we need to support this. */
1072 if (mips_opts.isa != ISA_UNKNOWN)
1073 {
1074 /* Translate -mipsN to the appropriate settings of file_mips_gp32
1075 and file_mips_fp32. Tag binaries as using the mipsN ISA. */
1076 if (file_mips_gp32 < 0)
1077 {
1078 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1079 file_mips_gp32 = 0;
1080 else
1081 file_mips_gp32 = 1;
1082 }
1083 if (file_mips_fp32 < 0)
1084 {
1085 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1086 file_mips_fp32 = 0;
1087 else
1088 file_mips_fp32 = 1;
1089 }
1090
1091 ci = mips_cpu_info_from_isa (mips_opts.isa);
1092 assert (ci != NULL);
1093 /* -mipsN has higher priority than -mcpu but lower than -march. */
1094 if (mips_arch == CPU_UNKNOWN)
1095 mips_arch = ci->cpu;
1096
1097 /* Default mips_abi. */
1098 if (mips_opts.abi == NO_ABI)
1099 {
1100 if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1101 mips_opts.abi = O32_ABI;
1102 else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1103 mips_opts.abi = O64_ABI;
1104 }
1105 }
1106
1107 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1108 {
1109 ci = mips_cpu_info_from_cpu (mips_cpu);
1110 assert (ci != NULL);
1111 mips_arch = ci->cpu;
1112 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1113 "-mtune instead."));
1114 }
1115
1116 /* Set tune from -mcpu, not from -mipsN. */
1117 if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1118 {
1119 ci = mips_cpu_info_from_cpu (mips_cpu);
1120 assert (ci != NULL);
1121 mips_tune = ci->cpu;
1122 }
1123
1124 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1125 specified on the command line, or some other value if one was.
1126 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1127 the command line, or will be set otherwise if one was. */
1128
1129 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1130 /* Handled above. */;
1131 #else
1132 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1133 {
1134 ci = mips_cpu_info_from_cpu (mips_cpu);
1135 assert (ci != NULL);
1136 mips_arch = ci->cpu;
1137 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1138 "-mtune instead."));
1139 }
1140
1141 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1142 specified on the command line, or some other value if one was.
1143 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1144 the command line, or will be set otherwise if one was. */
1145
1146 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1147 {
1148 /* We have to check if the isa is the default isa of arch. Otherwise
1149 we'll get invalid object file headers. */
1150 ci = mips_cpu_info_from_cpu (mips_arch);
1151 assert (ci != NULL);
1152 if (mips_opts.isa != ci->isa)
1153 {
1154 /* This really should be an error instead of a warning, but old
1155 compilers only have -mcpu which sets both arch and tune. For
1156 now, we discard arch and preserve tune. */
1157 as_warn (_("The -march option is incompatible to -mipsN and "
1158 "therefore ignored."));
1159 if (mips_tune == CPU_UNKNOWN)
1160 mips_tune = mips_arch;
1161 ci = mips_cpu_info_from_isa (mips_opts.isa);
1162 assert (ci != NULL);
1163 mips_arch = ci->cpu;
1164 }
1165 }
1166 #endif
1167 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1168 {
1169 /* We have ARCH, we need ISA. */
1170 ci = mips_cpu_info_from_cpu (mips_arch);
1171 assert (ci != NULL);
1172 mips_opts.isa = ci->isa;
1173 }
1174 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1175 {
1176 /* We have ISA, we need default ARCH. */
1177 ci = mips_cpu_info_from_isa (mips_opts.isa);
1178 assert (ci != NULL);
1179 mips_arch = ci->cpu;
1180 }
1181 else
1182 {
1183 /* We need to set both ISA and ARCH from target cpu. */
1184 ci = mips_cpu_info_from_name (cpu);
1185 if (ci == NULL)
1186 ci = mips_cpu_info_from_cpu (CPU_R3000);
1187 assert (ci != NULL);
1188 mips_opts.isa = ci->isa;
1189 mips_arch = ci->cpu;
1190 }
1191
1192 if (mips_tune == CPU_UNKNOWN)
1193 mips_tune = mips_arch;
1194
1195 ci = mips_cpu_info_from_cpu (mips_arch);
1196 assert (ci != NULL);
1197 mips_isa_from_cpu = ci->isa;
1198
1199 /* End of TARGET_CPU processing, get rid of malloced memory
1200 if necessary. */
1201 cpu = NULL;
1202 if (a != NULL)
1203 {
1204 free (a);
1205 a = NULL;
1206 }
1207
1208 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1209 as_bad (_("trap exception not supported at ISA 1"));
1210
1211 /* Set the EABI kind based on the ISA before the user gets
1212 to change the ISA with directives. This isn't really
1213 the best, but then neither is basing the abi on the isa. */
1214 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1215 && mips_opts.abi == EABI_ABI)
1216 mips_eabi64 = 1;
1217
1218 /* If they asked for mips1 or mips2 and a cpu that is
1219 mips3 or greater, then mark the object file 32BITMODE. */
1220 if (mips_isa_from_cpu != ISA_UNKNOWN
1221 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1222 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1223 mips_32bitmode = 1;
1224
1225 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1226 as_warn (_("Could not set architecture and machine"));
1227
1228 if (file_mips_gp32 < 0)
1229 file_mips_gp32 = 0;
1230 if (file_mips_fp32 < 0)
1231 file_mips_fp32 = 0;
1232
1233 file_mips_isa = mips_opts.isa;
1234 file_mips_abi = mips_opts.abi;
1235 mips_opts.gp32 = file_mips_gp32;
1236 mips_opts.fp32 = file_mips_fp32;
1237
1238 op_hash = hash_new ();
1239
1240 for (i = 0; i < NUMOPCODES;)
1241 {
1242 const char *name = mips_opcodes[i].name;
1243
1244 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1245 if (retval != NULL)
1246 {
1247 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1248 mips_opcodes[i].name, retval);
1249 /* Probably a memory allocation problem? Give up now. */
1250 as_fatal (_("Broken assembler. No assembly attempted."));
1251 }
1252 do
1253 {
1254 if (mips_opcodes[i].pinfo != INSN_MACRO)
1255 {
1256 if (!validate_mips_insn (&mips_opcodes[i]))
1257 broken = 1;
1258 }
1259 ++i;
1260 }
1261 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1262 }
1263
1264 mips16_op_hash = hash_new ();
1265
1266 i = 0;
1267 while (i < bfd_mips16_num_opcodes)
1268 {
1269 const char *name = mips16_opcodes[i].name;
1270
1271 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1272 if (retval != NULL)
1273 as_fatal (_("internal: can't hash `%s': %s"),
1274 mips16_opcodes[i].name, retval);
1275 do
1276 {
1277 if (mips16_opcodes[i].pinfo != INSN_MACRO
1278 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1279 != mips16_opcodes[i].match))
1280 {
1281 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1282 mips16_opcodes[i].name, mips16_opcodes[i].args);
1283 broken = 1;
1284 }
1285 ++i;
1286 }
1287 while (i < bfd_mips16_num_opcodes
1288 && strcmp (mips16_opcodes[i].name, name) == 0);
1289 }
1290
1291 if (broken)
1292 as_fatal (_("Broken assembler. No assembly attempted."));
1293
1294 /* We add all the general register names to the symbol table. This
1295 helps us detect invalid uses of them. */
1296 for (i = 0; i < 32; i++)
1297 {
1298 char buf[5];
1299
1300 sprintf (buf, "$%d", i);
1301 symbol_table_insert (symbol_new (buf, reg_section, i,
1302 &zero_address_frag));
1303 }
1304 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1305 &zero_address_frag));
1306 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1307 &zero_address_frag));
1308 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1309 &zero_address_frag));
1310 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1311 &zero_address_frag));
1312 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1313 &zero_address_frag));
1314 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1315 &zero_address_frag));
1316 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1317 &zero_address_frag));
1318
1319 mips_no_prev_insn (false);
1320
1321 mips_gprmask = 0;
1322 mips_cprmask[0] = 0;
1323 mips_cprmask[1] = 0;
1324 mips_cprmask[2] = 0;
1325 mips_cprmask[3] = 0;
1326
1327 /* set the default alignment for the text section (2**2) */
1328 record_alignment (text_section, 2);
1329
1330 if (USE_GLOBAL_POINTER_OPT)
1331 bfd_set_gp_size (stdoutput, g_switch_value);
1332
1333 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1334 {
1335 /* On a native system, sections must be aligned to 16 byte
1336 boundaries. When configured for an embedded ELF target, we
1337 don't bother. */
1338 if (strcmp (TARGET_OS, "elf") != 0)
1339 {
1340 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1341 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1342 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1343 }
1344
1345 /* Create a .reginfo section for register masks and a .mdebug
1346 section for debugging information. */
1347 {
1348 segT seg;
1349 subsegT subseg;
1350 flagword flags;
1351 segT sec;
1352
1353 seg = now_seg;
1354 subseg = now_subseg;
1355
1356 /* The ABI says this section should be loaded so that the
1357 running program can access it. However, we don't load it
1358 if we are configured for an embedded target */
1359 flags = SEC_READONLY | SEC_DATA;
1360 if (strcmp (TARGET_OS, "elf") != 0)
1361 flags |= SEC_ALLOC | SEC_LOAD;
1362
1363 if (! HAVE_NEWABI)
1364 {
1365 sec = subseg_new (".reginfo", (subsegT) 0);
1366
1367 (void) bfd_set_section_flags (stdoutput, sec, flags);
1368 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1369
1370 #ifdef OBJ_ELF
1371 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1372 #endif
1373 }
1374 else
1375 {
1376 /* The 64-bit ABI uses a .MIPS.options section rather than
1377 .reginfo section. */
1378 sec = subseg_new (".MIPS.options", (subsegT) 0);
1379 (void) bfd_set_section_flags (stdoutput, sec, flags);
1380 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1381
1382 #ifdef OBJ_ELF
1383 /* Set up the option header. */
1384 {
1385 Elf_Internal_Options opthdr;
1386 char *f;
1387
1388 opthdr.kind = ODK_REGINFO;
1389 opthdr.size = (sizeof (Elf_External_Options)
1390 + sizeof (Elf64_External_RegInfo));
1391 opthdr.section = 0;
1392 opthdr.info = 0;
1393 f = frag_more (sizeof (Elf_External_Options));
1394 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1395 (Elf_External_Options *) f);
1396
1397 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1398 }
1399 #endif
1400 }
1401
1402 if (ECOFF_DEBUGGING)
1403 {
1404 sec = subseg_new (".mdebug", (subsegT) 0);
1405 (void) bfd_set_section_flags (stdoutput, sec,
1406 SEC_HAS_CONTENTS | SEC_READONLY);
1407 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1408 }
1409
1410 #ifdef MIPS_STABS_ELF
1411 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1412 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1413 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1414 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1415 #endif
1416
1417 subseg_set (seg, subseg);
1418 }
1419 }
1420
1421 if (! ECOFF_DEBUGGING)
1422 md_obj_begin ();
1423 }
1424
1425 void
1426 md_mips_end ()
1427 {
1428 if (! ECOFF_DEBUGGING)
1429 md_obj_end ();
1430 }
1431
1432 void
1433 md_assemble (str)
1434 char *str;
1435 {
1436 struct mips_cl_insn insn;
1437 bfd_reloc_code_real_type unused_reloc[3]
1438 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1439
1440 imm_expr.X_op = O_absent;
1441 imm_unmatched_hi = false;
1442 offset_expr.X_op = O_absent;
1443 imm_reloc[0] = BFD_RELOC_UNUSED;
1444 imm_reloc[1] = BFD_RELOC_UNUSED;
1445 imm_reloc[2] = BFD_RELOC_UNUSED;
1446 offset_reloc[0] = BFD_RELOC_UNUSED;
1447 offset_reloc[1] = BFD_RELOC_UNUSED;
1448 offset_reloc[2] = BFD_RELOC_UNUSED;
1449
1450 if (mips_opts.mips16)
1451 mips16_ip (str, &insn);
1452 else
1453 {
1454 mips_ip (str, &insn);
1455 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1456 str, insn.insn_opcode));
1457 }
1458
1459 if (insn_error)
1460 {
1461 as_bad ("%s `%s'", insn_error, str);
1462 return;
1463 }
1464
1465 if (insn.insn_mo->pinfo == INSN_MACRO)
1466 {
1467 if (mips_opts.mips16)
1468 mips16_macro (&insn);
1469 else
1470 macro (&insn);
1471 }
1472 else
1473 {
1474 if (imm_expr.X_op != O_absent)
1475 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1476 else if (offset_expr.X_op != O_absent)
1477 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1478 else
1479 append_insn (NULL, &insn, NULL, unused_reloc, false);
1480 }
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 0, since it never changes. */
1500 if (class == MIPS_GR_REG && reg == 0)
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, unmatched_hi)
1628 char *place;
1629 struct mips_cl_insn *ip;
1630 expressionS *address_expr;
1631 bfd_reloc_code_real_type *reloc_type;
1632 boolean unmatched_hi;
1633 {
1634 register unsigned long prev_pinfo, pinfo;
1635 char *f;
1636 fixS *fixp[3];
1637 int nops = 0;
1638
1639 /* Mark instruction labels in mips16 mode. */
1640 if (mips_opts.mips16)
1641 mips16_mark_labels ();
1642
1643 prev_pinfo = prev_insn.insn_mo->pinfo;
1644 pinfo = ip->insn_mo->pinfo;
1645
1646 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1647 {
1648 int prev_prev_nop;
1649
1650 /* If the previous insn required any delay slots, see if we need
1651 to insert a NOP or two. There are eight kinds of possible
1652 hazards, of which an instruction can have at most one type.
1653 (1) a load from memory delay
1654 (2) a load from a coprocessor delay
1655 (3) an unconditional branch delay
1656 (4) a conditional branch delay
1657 (5) a move to coprocessor register delay
1658 (6) a load coprocessor register from memory delay
1659 (7) a coprocessor condition code delay
1660 (8) a HI/LO special register delay
1661
1662 There are a lot of optimizations we could do that we don't.
1663 In particular, we do not, in general, reorder instructions.
1664 If you use gcc with optimization, it will reorder
1665 instructions and generally do much more optimization then we
1666 do here; repeating all that work in the assembler would only
1667 benefit hand written assembly code, and does not seem worth
1668 it. */
1669
1670 /* This is how a NOP is emitted. */
1671 #define emit_nop() \
1672 (mips_opts.mips16 \
1673 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1674 : md_number_to_chars (frag_more (4), 0, 4))
1675
1676 /* The previous insn might require a delay slot, depending upon
1677 the contents of the current insn. */
1678 if (! mips_opts.mips16
1679 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1680 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1681 && ! cop_interlocks)
1682 || (! gpr_interlocks
1683 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1684 {
1685 /* A load from a coprocessor or from memory. All load
1686 delays delay the use of general register rt for one
1687 instruction on the r3000. The r6000 and r4000 use
1688 interlocks. */
1689 /* Itbl support may require additional care here. */
1690 know (prev_pinfo & INSN_WRITE_GPR_T);
1691 if (mips_optimize == 0
1692 || insn_uses_reg (ip,
1693 ((prev_insn.insn_opcode >> OP_SH_RT)
1694 & OP_MASK_RT),
1695 MIPS_GR_REG))
1696 ++nops;
1697 }
1698 else if (! mips_opts.mips16
1699 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1700 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1701 && ! cop_interlocks)
1702 || (mips_opts.isa == ISA_MIPS1
1703 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1704 {
1705 /* A generic coprocessor delay. The previous instruction
1706 modified a coprocessor general or control register. If
1707 it modified a control register, we need to avoid any
1708 coprocessor instruction (this is probably not always
1709 required, but it sometimes is). If it modified a general
1710 register, we avoid using that register.
1711
1712 On the r6000 and r4000 loading a coprocessor register
1713 from memory is interlocked, and does not require a delay.
1714
1715 This case is not handled very well. There is no special
1716 knowledge of CP0 handling, and the coprocessors other
1717 than the floating point unit are not distinguished at
1718 all. */
1719 /* Itbl support may require additional care here. FIXME!
1720 Need to modify this to include knowledge about
1721 user specified delays! */
1722 if (prev_pinfo & INSN_WRITE_FPR_T)
1723 {
1724 if (mips_optimize == 0
1725 || insn_uses_reg (ip,
1726 ((prev_insn.insn_opcode >> OP_SH_FT)
1727 & OP_MASK_FT),
1728 MIPS_FP_REG))
1729 ++nops;
1730 }
1731 else if (prev_pinfo & INSN_WRITE_FPR_S)
1732 {
1733 if (mips_optimize == 0
1734 || insn_uses_reg (ip,
1735 ((prev_insn.insn_opcode >> OP_SH_FS)
1736 & OP_MASK_FS),
1737 MIPS_FP_REG))
1738 ++nops;
1739 }
1740 else
1741 {
1742 /* We don't know exactly what the previous instruction
1743 does. If the current instruction uses a coprocessor
1744 register, we must insert a NOP. If previous
1745 instruction may set the condition codes, and the
1746 current instruction uses them, we must insert two
1747 NOPS. */
1748 /* Itbl support may require additional care here. */
1749 if (mips_optimize == 0
1750 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1751 && (pinfo & INSN_READ_COND_CODE)))
1752 nops += 2;
1753 else if (pinfo & INSN_COP)
1754 ++nops;
1755 }
1756 }
1757 else if (! mips_opts.mips16
1758 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1759 && (prev_pinfo & INSN_WRITE_COND_CODE)
1760 && ! cop_interlocks)
1761 {
1762 /* The previous instruction sets the coprocessor condition
1763 codes, but does not require a general coprocessor delay
1764 (this means it is a floating point comparison
1765 instruction). If this instruction uses the condition
1766 codes, we need to insert a single NOP. */
1767 /* Itbl support may require additional care here. */
1768 if (mips_optimize == 0
1769 || (pinfo & INSN_READ_COND_CODE))
1770 ++nops;
1771 }
1772
1773 /* If we're fixing up mfhi/mflo for the r7000 and the
1774 previous insn was an mfhi/mflo and the current insn
1775 reads the register that the mfhi/mflo wrote to, then
1776 insert two nops. */
1777
1778 else if (mips_7000_hilo_fix
1779 && MF_HILO_INSN (prev_pinfo)
1780 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1781 & OP_MASK_RD),
1782 MIPS_GR_REG))
1783 {
1784 nops += 2;
1785 }
1786
1787 /* If we're fixing up mfhi/mflo for the r7000 and the
1788 2nd previous insn was an mfhi/mflo and the current insn
1789 reads the register that the mfhi/mflo wrote to, then
1790 insert one nop. */
1791
1792 else if (mips_7000_hilo_fix
1793 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1794 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1795 & OP_MASK_RD),
1796 MIPS_GR_REG))
1797
1798 {
1799 nops += 1;
1800 }
1801
1802 else if (prev_pinfo & INSN_READ_LO)
1803 {
1804 /* The previous instruction reads the LO register; if the
1805 current instruction writes to the LO register, we must
1806 insert two NOPS. Some newer processors have interlocks.
1807 Also the tx39's multiply instructions can be exectuted
1808 immediatly after a read from HI/LO (without the delay),
1809 though the tx39's divide insns still do require the
1810 delay. */
1811 if (! (hilo_interlocks
1812 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1813 && (mips_optimize == 0
1814 || (pinfo & INSN_WRITE_LO)))
1815 nops += 2;
1816 /* Most mips16 branch insns don't have a delay slot.
1817 If a read from LO is immediately followed by a branch
1818 to a write to LO we have a read followed by a write
1819 less than 2 insns away. We assume the target of
1820 a branch might be a write to LO, and insert a nop
1821 between a read and an immediately following branch. */
1822 else if (mips_opts.mips16
1823 && (mips_optimize == 0
1824 || (pinfo & MIPS16_INSN_BRANCH)))
1825 nops += 1;
1826 }
1827 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1828 {
1829 /* The previous instruction reads the HI register; if the
1830 current instruction writes to the HI register, we must
1831 insert a NOP. Some newer processors have interlocks.
1832 Also the note tx39's multiply above. */
1833 if (! (hilo_interlocks
1834 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1835 && (mips_optimize == 0
1836 || (pinfo & INSN_WRITE_HI)))
1837 nops += 2;
1838 /* Most mips16 branch insns don't have a delay slot.
1839 If a read from HI is immediately followed by a branch
1840 to a write to HI we have a read followed by a write
1841 less than 2 insns away. We assume the target of
1842 a branch might be a write to HI, and insert a nop
1843 between a read and an immediately following branch. */
1844 else if (mips_opts.mips16
1845 && (mips_optimize == 0
1846 || (pinfo & MIPS16_INSN_BRANCH)))
1847 nops += 1;
1848 }
1849
1850 /* If the previous instruction was in a noreorder section, then
1851 we don't want to insert the nop after all. */
1852 /* Itbl support may require additional care here. */
1853 if (prev_insn_unreordered)
1854 nops = 0;
1855
1856 /* There are two cases which require two intervening
1857 instructions: 1) setting the condition codes using a move to
1858 coprocessor instruction which requires a general coprocessor
1859 delay and then reading the condition codes 2) reading the HI
1860 or LO register and then writing to it (except on processors
1861 which have interlocks). If we are not already emitting a NOP
1862 instruction, we must check for these cases compared to the
1863 instruction previous to the previous instruction. */
1864 if ((! mips_opts.mips16
1865 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1866 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1867 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1868 && (pinfo & INSN_READ_COND_CODE)
1869 && ! cop_interlocks)
1870 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1871 && (pinfo & INSN_WRITE_LO)
1872 && ! (hilo_interlocks
1873 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1874 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1875 && (pinfo & INSN_WRITE_HI)
1876 && ! (hilo_interlocks
1877 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1878 prev_prev_nop = 1;
1879 else
1880 prev_prev_nop = 0;
1881
1882 if (prev_prev_insn_unreordered)
1883 prev_prev_nop = 0;
1884
1885 if (prev_prev_nop && nops == 0)
1886 ++nops;
1887
1888 /* If we are being given a nop instruction, don't bother with
1889 one of the nops we would otherwise output. This will only
1890 happen when a nop instruction is used with mips_optimize set
1891 to 0. */
1892 if (nops > 0
1893 && ! mips_opts.noreorder
1894 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1895 --nops;
1896
1897 /* Now emit the right number of NOP instructions. */
1898 if (nops > 0 && ! mips_opts.noreorder)
1899 {
1900 fragS *old_frag;
1901 unsigned long old_frag_offset;
1902 int i;
1903 struct insn_label_list *l;
1904
1905 old_frag = frag_now;
1906 old_frag_offset = frag_now_fix ();
1907
1908 for (i = 0; i < nops; i++)
1909 emit_nop ();
1910
1911 if (listing)
1912 {
1913 listing_prev_line ();
1914 /* We may be at the start of a variant frag. In case we
1915 are, make sure there is enough space for the frag
1916 after the frags created by listing_prev_line. The
1917 argument to frag_grow here must be at least as large
1918 as the argument to all other calls to frag_grow in
1919 this file. We don't have to worry about being in the
1920 middle of a variant frag, because the variants insert
1921 all needed nop instructions themselves. */
1922 frag_grow (40);
1923 }
1924
1925 for (l = insn_labels; l != NULL; l = l->next)
1926 {
1927 valueT val;
1928
1929 assert (S_GET_SEGMENT (l->label) == now_seg);
1930 symbol_set_frag (l->label, frag_now);
1931 val = (valueT) frag_now_fix ();
1932 /* mips16 text labels are stored as odd. */
1933 if (mips_opts.mips16)
1934 val += 1;
1935 S_SET_VALUE (l->label, val);
1936 }
1937
1938 #ifndef NO_ECOFF_DEBUGGING
1939 if (ECOFF_DEBUGGING)
1940 ecoff_fix_loc (old_frag, old_frag_offset);
1941 #endif
1942 }
1943 else if (prev_nop_frag != NULL)
1944 {
1945 /* We have a frag holding nops we may be able to remove. If
1946 we don't need any nops, we can decrease the size of
1947 prev_nop_frag by the size of one instruction. If we do
1948 need some nops, we count them in prev_nops_required. */
1949 if (prev_nop_frag_since == 0)
1950 {
1951 if (nops == 0)
1952 {
1953 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1954 --prev_nop_frag_holds;
1955 }
1956 else
1957 prev_nop_frag_required += nops;
1958 }
1959 else
1960 {
1961 if (prev_prev_nop == 0)
1962 {
1963 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1964 --prev_nop_frag_holds;
1965 }
1966 else
1967 ++prev_nop_frag_required;
1968 }
1969
1970 if (prev_nop_frag_holds <= prev_nop_frag_required)
1971 prev_nop_frag = NULL;
1972
1973 ++prev_nop_frag_since;
1974
1975 /* Sanity check: by the time we reach the second instruction
1976 after prev_nop_frag, we should have used up all the nops
1977 one way or another. */
1978 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1979 }
1980 }
1981
1982 if (*reloc_type > BFD_RELOC_UNUSED)
1983 {
1984 /* We need to set up a variant frag. */
1985 assert (mips_opts.mips16 && address_expr != NULL);
1986 f = frag_var (rs_machine_dependent, 4, 0,
1987 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1988 mips16_small, mips16_ext,
1989 (prev_pinfo
1990 & INSN_UNCOND_BRANCH_DELAY),
1991 (*prev_insn_reloc_type
1992 == BFD_RELOC_MIPS16_JMP)),
1993 make_expr_symbol (address_expr), 0, NULL);
1994 }
1995 else if (place != NULL)
1996 f = place;
1997 else if (mips_opts.mips16
1998 && ! ip->use_extend
1999 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2000 {
2001 /* Make sure there is enough room to swap this instruction with
2002 a following jump instruction. */
2003 frag_grow (6);
2004 f = frag_more (2);
2005 }
2006 else
2007 {
2008 if (mips_opts.mips16
2009 && mips_opts.noreorder
2010 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2011 as_warn (_("extended instruction in delay slot"));
2012
2013 f = frag_more (4);
2014 }
2015
2016 fixp[0] = fixp[1] = fixp[2] = NULL;
2017 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2018 {
2019 if (address_expr->X_op == O_constant)
2020 {
2021 unsigned long tmp;
2022
2023 switch (*reloc_type)
2024 {
2025 case BFD_RELOC_32:
2026 ip->insn_opcode |= address_expr->X_add_number;
2027 break;
2028
2029 case BFD_RELOC_MIPS_HIGHEST:
2030 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2031 tmp >>= 16;
2032 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2033 break;
2034
2035 case BFD_RELOC_MIPS_HIGHER:
2036 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2037 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2038 break;
2039
2040 case BFD_RELOC_HI16_S:
2041 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2042 >> 16) & 0xffff;
2043 break;
2044
2045 case BFD_RELOC_HI16:
2046 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2047 break;
2048
2049 case BFD_RELOC_LO16:
2050 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2051 break;
2052
2053 case BFD_RELOC_MIPS_JMP:
2054 if ((address_expr->X_add_number & 3) != 0)
2055 as_bad (_("jump to misaligned address (0x%lx)"),
2056 (unsigned long) address_expr->X_add_number);
2057 if (address_expr->X_add_number & ~0xfffffff
2058 || address_expr->X_add_number > 0x7fffffc)
2059 as_bad (_("jump address range overflow (0x%lx)"),
2060 (unsigned long) address_expr->X_add_number);
2061 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2062 break;
2063
2064 case BFD_RELOC_MIPS16_JMP:
2065 if ((address_expr->X_add_number & 3) != 0)
2066 as_bad (_("jump to misaligned address (0x%lx)"),
2067 (unsigned long) address_expr->X_add_number);
2068 if (address_expr->X_add_number & ~0xfffffff
2069 || address_expr->X_add_number > 0x7fffffc)
2070 as_bad (_("jump address range overflow (0x%lx)"),
2071 (unsigned long) address_expr->X_add_number);
2072 ip->insn_opcode |=
2073 (((address_expr->X_add_number & 0x7c0000) << 3)
2074 | ((address_expr->X_add_number & 0xf800000) >> 7)
2075 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2076 break;
2077
2078 case BFD_RELOC_16_PCREL:
2079 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2080 break;
2081
2082 case BFD_RELOC_16_PCREL_S2:
2083 goto need_reloc;
2084
2085 default:
2086 internalError ();
2087 }
2088 }
2089 else
2090 {
2091 need_reloc:
2092 /* Don't generate a reloc if we are writing into a variant frag. */
2093 if (place == NULL)
2094 {
2095 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2096 address_expr,
2097 (*reloc_type == BFD_RELOC_16_PCREL
2098 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2099 reloc_type[0]);
2100
2101 /* These relocations can have an addend that won't fit in
2102 4 octets for 64bit assembly. */
2103 if (HAVE_64BIT_GPRS &&
2104 (*reloc_type == BFD_RELOC_16
2105 || *reloc_type == BFD_RELOC_32
2106 || *reloc_type == BFD_RELOC_MIPS_JMP
2107 || *reloc_type == BFD_RELOC_HI16_S
2108 || *reloc_type == BFD_RELOC_LO16
2109 || *reloc_type == BFD_RELOC_GPREL16
2110 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2111 || *reloc_type == BFD_RELOC_GPREL32
2112 || *reloc_type == BFD_RELOC_64
2113 || *reloc_type == BFD_RELOC_CTOR
2114 || *reloc_type == BFD_RELOC_MIPS_SUB
2115 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2116 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2117 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2118 || *reloc_type == BFD_RELOC_MIPS_REL16
2119 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2120 fixp[0]->fx_no_overflow = 1;
2121
2122 if (unmatched_hi)
2123 {
2124 struct mips_hi_fixup *hi_fixup;
2125
2126 assert (*reloc_type == BFD_RELOC_HI16_S);
2127 hi_fixup = ((struct mips_hi_fixup *)
2128 xmalloc (sizeof (struct mips_hi_fixup)));
2129 hi_fixup->fixp = fixp[0];
2130 hi_fixup->seg = now_seg;
2131 hi_fixup->next = mips_hi_fixup_list;
2132 mips_hi_fixup_list = hi_fixup;
2133 }
2134
2135 if (reloc_type[1] != BFD_RELOC_UNUSED)
2136 {
2137 /* FIXME: This symbol can be one of
2138 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2139 address_expr->X_op = O_absent;
2140 address_expr->X_add_symbol = 0;
2141 address_expr->X_add_number = 0;
2142
2143 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2144 4, address_expr, false,
2145 reloc_type[1]);
2146
2147 /* These relocations can have an addend that won't fit in
2148 4 octets for 64bit assembly. */
2149 if (HAVE_64BIT_GPRS &&
2150 (*reloc_type == BFD_RELOC_16
2151 || *reloc_type == BFD_RELOC_32
2152 || *reloc_type == BFD_RELOC_MIPS_JMP
2153 || *reloc_type == BFD_RELOC_HI16_S
2154 || *reloc_type == BFD_RELOC_LO16
2155 || *reloc_type == BFD_RELOC_GPREL16
2156 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2157 || *reloc_type == BFD_RELOC_GPREL32
2158 || *reloc_type == BFD_RELOC_64
2159 || *reloc_type == BFD_RELOC_CTOR
2160 || *reloc_type == BFD_RELOC_MIPS_SUB
2161 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2162 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2163 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2164 || *reloc_type == BFD_RELOC_MIPS_REL16
2165 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2166 fixp[1]->fx_no_overflow = 1;
2167
2168 if (reloc_type[2] != BFD_RELOC_UNUSED)
2169 {
2170 address_expr->X_op = O_absent;
2171 address_expr->X_add_symbol = 0;
2172 address_expr->X_add_number = 0;
2173
2174 fixp[2] = fix_new_exp (frag_now,
2175 f - frag_now->fr_literal, 4,
2176 address_expr, false,
2177 reloc_type[2]);
2178
2179 /* These relocations can have an addend that won't fit in
2180 4 octets for 64bit assembly. */
2181 if (HAVE_64BIT_GPRS &&
2182 (*reloc_type == BFD_RELOC_16
2183 || *reloc_type == BFD_RELOC_32
2184 || *reloc_type == BFD_RELOC_MIPS_JMP
2185 || *reloc_type == BFD_RELOC_HI16_S
2186 || *reloc_type == BFD_RELOC_LO16
2187 || *reloc_type == BFD_RELOC_GPREL16
2188 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2189 || *reloc_type == BFD_RELOC_GPREL32
2190 || *reloc_type == BFD_RELOC_64
2191 || *reloc_type == BFD_RELOC_CTOR
2192 || *reloc_type == BFD_RELOC_MIPS_SUB
2193 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2194 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2195 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2196 || *reloc_type == BFD_RELOC_MIPS_REL16
2197 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2198 fixp[2]->fx_no_overflow = 1;
2199 }
2200 }
2201 }
2202 }
2203 }
2204
2205 if (! mips_opts.mips16)
2206 md_number_to_chars (f, ip->insn_opcode, 4);
2207 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2208 {
2209 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2210 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2211 }
2212 else
2213 {
2214 if (ip->use_extend)
2215 {
2216 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2217 f += 2;
2218 }
2219 md_number_to_chars (f, ip->insn_opcode, 2);
2220 }
2221
2222 /* Update the register mask information. */
2223 if (! mips_opts.mips16)
2224 {
2225 if (pinfo & INSN_WRITE_GPR_D)
2226 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2227 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2228 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2229 if (pinfo & INSN_READ_GPR_S)
2230 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2231 if (pinfo & INSN_WRITE_GPR_31)
2232 mips_gprmask |= 1 << 31;
2233 if (pinfo & INSN_WRITE_FPR_D)
2234 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2235 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2236 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2237 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2238 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2239 if ((pinfo & INSN_READ_FPR_R) != 0)
2240 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2241 if (pinfo & INSN_COP)
2242 {
2243 /* We don't keep enough information to sort these cases out.
2244 The itbl support does keep this information however, although
2245 we currently don't support itbl fprmats as part of the cop
2246 instruction. May want to add this support in the future. */
2247 }
2248 /* Never set the bit for $0, which is always zero. */
2249 mips_gprmask &= ~1 << 0;
2250 }
2251 else
2252 {
2253 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2254 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2255 & MIPS16OP_MASK_RX);
2256 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2257 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2258 & MIPS16OP_MASK_RY);
2259 if (pinfo & MIPS16_INSN_WRITE_Z)
2260 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2261 & MIPS16OP_MASK_RZ);
2262 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2263 mips_gprmask |= 1 << TREG;
2264 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2265 mips_gprmask |= 1 << SP;
2266 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2267 mips_gprmask |= 1 << RA;
2268 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2269 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2270 if (pinfo & MIPS16_INSN_READ_Z)
2271 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2272 & MIPS16OP_MASK_MOVE32Z);
2273 if (pinfo & MIPS16_INSN_READ_GPR_X)
2274 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2275 & MIPS16OP_MASK_REGR32);
2276 }
2277
2278 if (place == NULL && ! mips_opts.noreorder)
2279 {
2280 /* Filling the branch delay slot is more complex. We try to
2281 switch the branch with the previous instruction, which we can
2282 do if the previous instruction does not set up a condition
2283 that the branch tests and if the branch is not itself the
2284 target of any branch. */
2285 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2286 || (pinfo & INSN_COND_BRANCH_DELAY))
2287 {
2288 if (mips_optimize < 2
2289 /* If we have seen .set volatile or .set nomove, don't
2290 optimize. */
2291 || mips_opts.nomove != 0
2292 /* If we had to emit any NOP instructions, then we
2293 already know we can not swap. */
2294 || nops != 0
2295 /* If we don't even know the previous insn, we can not
2296 swap. */
2297 || ! prev_insn_valid
2298 /* If the previous insn is already in a branch delay
2299 slot, then we can not swap. */
2300 || prev_insn_is_delay_slot
2301 /* If the previous previous insn was in a .set
2302 noreorder, we can't swap. Actually, the MIPS
2303 assembler will swap in this situation. However, gcc
2304 configured -with-gnu-as will generate code like
2305 .set noreorder
2306 lw $4,XXX
2307 .set reorder
2308 INSN
2309 bne $4,$0,foo
2310 in which we can not swap the bne and INSN. If gcc is
2311 not configured -with-gnu-as, it does not output the
2312 .set pseudo-ops. We don't have to check
2313 prev_insn_unreordered, because prev_insn_valid will
2314 be 0 in that case. We don't want to use
2315 prev_prev_insn_valid, because we do want to be able
2316 to swap at the start of a function. */
2317 || prev_prev_insn_unreordered
2318 /* If the branch is itself the target of a branch, we
2319 can not swap. We cheat on this; all we check for is
2320 whether there is a label on this instruction. If
2321 there are any branches to anything other than a
2322 label, users must use .set noreorder. */
2323 || insn_labels != NULL
2324 /* If the previous instruction is in a variant frag, we
2325 can not do the swap. This does not apply to the
2326 mips16, which uses variant frags for different
2327 purposes. */
2328 || (! mips_opts.mips16
2329 && prev_insn_frag->fr_type == rs_machine_dependent)
2330 /* If the branch reads the condition codes, we don't
2331 even try to swap, because in the sequence
2332 ctc1 $X,$31
2333 INSN
2334 INSN
2335 bc1t LABEL
2336 we can not swap, and I don't feel like handling that
2337 case. */
2338 || (! mips_opts.mips16
2339 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2340 && (pinfo & INSN_READ_COND_CODE))
2341 /* We can not swap with an instruction that requires a
2342 delay slot, becase the target of the branch might
2343 interfere with that instruction. */
2344 || (! mips_opts.mips16
2345 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2346 && (prev_pinfo
2347 /* Itbl support may require additional care here. */
2348 & (INSN_LOAD_COPROC_DELAY
2349 | INSN_COPROC_MOVE_DELAY
2350 | INSN_WRITE_COND_CODE)))
2351 || (! (hilo_interlocks
2352 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2353 && (prev_pinfo
2354 & (INSN_READ_LO
2355 | INSN_READ_HI)))
2356 || (! mips_opts.mips16
2357 && ! gpr_interlocks
2358 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2359 || (! mips_opts.mips16
2360 && mips_opts.isa == ISA_MIPS1
2361 /* Itbl support may require additional care here. */
2362 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2363 /* We can not swap with a branch instruction. */
2364 || (prev_pinfo
2365 & (INSN_UNCOND_BRANCH_DELAY
2366 | INSN_COND_BRANCH_DELAY
2367 | INSN_COND_BRANCH_LIKELY))
2368 /* We do not swap with a trap instruction, since it
2369 complicates trap handlers to have the trap
2370 instruction be in a delay slot. */
2371 || (prev_pinfo & INSN_TRAP)
2372 /* If the branch reads a register that the previous
2373 instruction sets, we can not swap. */
2374 || (! mips_opts.mips16
2375 && (prev_pinfo & INSN_WRITE_GPR_T)
2376 && insn_uses_reg (ip,
2377 ((prev_insn.insn_opcode >> OP_SH_RT)
2378 & OP_MASK_RT),
2379 MIPS_GR_REG))
2380 || (! mips_opts.mips16
2381 && (prev_pinfo & INSN_WRITE_GPR_D)
2382 && insn_uses_reg (ip,
2383 ((prev_insn.insn_opcode >> OP_SH_RD)
2384 & OP_MASK_RD),
2385 MIPS_GR_REG))
2386 || (mips_opts.mips16
2387 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2388 && insn_uses_reg (ip,
2389 ((prev_insn.insn_opcode
2390 >> MIPS16OP_SH_RX)
2391 & MIPS16OP_MASK_RX),
2392 MIPS16_REG))
2393 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2394 && insn_uses_reg (ip,
2395 ((prev_insn.insn_opcode
2396 >> MIPS16OP_SH_RY)
2397 & MIPS16OP_MASK_RY),
2398 MIPS16_REG))
2399 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2400 && insn_uses_reg (ip,
2401 ((prev_insn.insn_opcode
2402 >> MIPS16OP_SH_RZ)
2403 & MIPS16OP_MASK_RZ),
2404 MIPS16_REG))
2405 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2406 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2407 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2408 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2409 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2410 && insn_uses_reg (ip,
2411 MIPS16OP_EXTRACT_REG32R (prev_insn.
2412 insn_opcode),
2413 MIPS_GR_REG))))
2414 /* If the branch writes a register that the previous
2415 instruction sets, we can not swap (we know that
2416 branches write only to RD or to $31). */
2417 || (! mips_opts.mips16
2418 && (prev_pinfo & INSN_WRITE_GPR_T)
2419 && (((pinfo & INSN_WRITE_GPR_D)
2420 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2421 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2422 || ((pinfo & INSN_WRITE_GPR_31)
2423 && (((prev_insn.insn_opcode >> OP_SH_RT)
2424 & OP_MASK_RT)
2425 == 31))))
2426 || (! mips_opts.mips16
2427 && (prev_pinfo & INSN_WRITE_GPR_D)
2428 && (((pinfo & INSN_WRITE_GPR_D)
2429 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2430 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2431 || ((pinfo & INSN_WRITE_GPR_31)
2432 && (((prev_insn.insn_opcode >> OP_SH_RD)
2433 & OP_MASK_RD)
2434 == 31))))
2435 || (mips_opts.mips16
2436 && (pinfo & MIPS16_INSN_WRITE_31)
2437 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2438 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2439 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2440 == RA))))
2441 /* If the branch writes a register that the previous
2442 instruction reads, we can not swap (we know that
2443 branches only write to RD or to $31). */
2444 || (! mips_opts.mips16
2445 && (pinfo & INSN_WRITE_GPR_D)
2446 && insn_uses_reg (&prev_insn,
2447 ((ip->insn_opcode >> OP_SH_RD)
2448 & OP_MASK_RD),
2449 MIPS_GR_REG))
2450 || (! mips_opts.mips16
2451 && (pinfo & INSN_WRITE_GPR_31)
2452 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2453 || (mips_opts.mips16
2454 && (pinfo & MIPS16_INSN_WRITE_31)
2455 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2456 /* If we are generating embedded PIC code, the branch
2457 might be expanded into a sequence which uses $at, so
2458 we can't swap with an instruction which reads it. */
2459 || (mips_pic == EMBEDDED_PIC
2460 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2461 /* If the previous previous instruction has a load
2462 delay, and sets a register that the branch reads, we
2463 can not swap. */
2464 || (! mips_opts.mips16
2465 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2466 /* Itbl support may require additional care here. */
2467 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2468 || (! gpr_interlocks
2469 && (prev_prev_insn.insn_mo->pinfo
2470 & INSN_LOAD_MEMORY_DELAY)))
2471 && insn_uses_reg (ip,
2472 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2473 & OP_MASK_RT),
2474 MIPS_GR_REG))
2475 /* If one instruction sets a condition code and the
2476 other one uses a condition code, we can not swap. */
2477 || ((pinfo & INSN_READ_COND_CODE)
2478 && (prev_pinfo & INSN_WRITE_COND_CODE))
2479 || ((pinfo & INSN_WRITE_COND_CODE)
2480 && (prev_pinfo & INSN_READ_COND_CODE))
2481 /* If the previous instruction uses the PC, we can not
2482 swap. */
2483 || (mips_opts.mips16
2484 && (prev_pinfo & MIPS16_INSN_READ_PC))
2485 /* If the previous instruction was extended, we can not
2486 swap. */
2487 || (mips_opts.mips16 && prev_insn_extended)
2488 /* If the previous instruction had a fixup in mips16
2489 mode, we can not swap. This normally means that the
2490 previous instruction was a 4 byte branch anyhow. */
2491 || (mips_opts.mips16 && prev_insn_fixp[0])
2492 /* If the previous instruction is a sync, sync.l, or
2493 sync.p, we can not swap. */
2494 || (prev_pinfo & INSN_SYNC))
2495 {
2496 /* We could do even better for unconditional branches to
2497 portions of this object file; we could pick up the
2498 instruction at the destination, put it in the delay
2499 slot, and bump the destination address. */
2500 emit_nop ();
2501 /* Update the previous insn information. */
2502 prev_prev_insn = *ip;
2503 prev_insn.insn_mo = &dummy_opcode;
2504 }
2505 else
2506 {
2507 /* It looks like we can actually do the swap. */
2508 if (! mips_opts.mips16)
2509 {
2510 char *prev_f;
2511 char temp[4];
2512
2513 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2514 memcpy (temp, prev_f, 4);
2515 memcpy (prev_f, f, 4);
2516 memcpy (f, temp, 4);
2517 if (prev_insn_fixp[0])
2518 {
2519 prev_insn_fixp[0]->fx_frag = frag_now;
2520 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2521 }
2522 if (prev_insn_fixp[1])
2523 {
2524 prev_insn_fixp[1]->fx_frag = frag_now;
2525 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2526 }
2527 if (prev_insn_fixp[2])
2528 {
2529 prev_insn_fixp[2]->fx_frag = frag_now;
2530 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2531 }
2532 if (fixp[0])
2533 {
2534 fixp[0]->fx_frag = prev_insn_frag;
2535 fixp[0]->fx_where = prev_insn_where;
2536 }
2537 if (fixp[1])
2538 {
2539 fixp[1]->fx_frag = prev_insn_frag;
2540 fixp[1]->fx_where = prev_insn_where;
2541 }
2542 if (fixp[2])
2543 {
2544 fixp[2]->fx_frag = prev_insn_frag;
2545 fixp[2]->fx_where = prev_insn_where;
2546 }
2547 }
2548 else
2549 {
2550 char *prev_f;
2551 char temp[2];
2552
2553 assert (prev_insn_fixp[0] == NULL);
2554 assert (prev_insn_fixp[1] == NULL);
2555 assert (prev_insn_fixp[2] == NULL);
2556 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2557 memcpy (temp, prev_f, 2);
2558 memcpy (prev_f, f, 2);
2559 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2560 {
2561 assert (*reloc_type == BFD_RELOC_UNUSED);
2562 memcpy (f, temp, 2);
2563 }
2564 else
2565 {
2566 memcpy (f, f + 2, 2);
2567 memcpy (f + 2, temp, 2);
2568 }
2569 if (fixp[0])
2570 {
2571 fixp[0]->fx_frag = prev_insn_frag;
2572 fixp[0]->fx_where = prev_insn_where;
2573 }
2574 if (fixp[1])
2575 {
2576 fixp[1]->fx_frag = prev_insn_frag;
2577 fixp[1]->fx_where = prev_insn_where;
2578 }
2579 if (fixp[2])
2580 {
2581 fixp[2]->fx_frag = prev_insn_frag;
2582 fixp[2]->fx_where = prev_insn_where;
2583 }
2584 }
2585
2586 /* Update the previous insn information; leave prev_insn
2587 unchanged. */
2588 prev_prev_insn = *ip;
2589 }
2590 prev_insn_is_delay_slot = 1;
2591
2592 /* If that was an unconditional branch, forget the previous
2593 insn information. */
2594 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2595 {
2596 prev_prev_insn.insn_mo = &dummy_opcode;
2597 prev_insn.insn_mo = &dummy_opcode;
2598 }
2599
2600 prev_insn_fixp[0] = NULL;
2601 prev_insn_fixp[1] = NULL;
2602 prev_insn_fixp[2] = NULL;
2603 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2604 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2605 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2606 prev_insn_extended = 0;
2607 }
2608 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2609 {
2610 /* We don't yet optimize a branch likely. What we should do
2611 is look at the target, copy the instruction found there
2612 into the delay slot, and increment the branch to jump to
2613 the next instruction. */
2614 emit_nop ();
2615 /* Update the previous insn information. */
2616 prev_prev_insn = *ip;
2617 prev_insn.insn_mo = &dummy_opcode;
2618 prev_insn_fixp[0] = NULL;
2619 prev_insn_fixp[1] = NULL;
2620 prev_insn_fixp[2] = NULL;
2621 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2622 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2623 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2624 prev_insn_extended = 0;
2625 }
2626 else
2627 {
2628 /* Update the previous insn information. */
2629 if (nops > 0)
2630 prev_prev_insn.insn_mo = &dummy_opcode;
2631 else
2632 prev_prev_insn = prev_insn;
2633 prev_insn = *ip;
2634
2635 /* Any time we see a branch, we always fill the delay slot
2636 immediately; since this insn is not a branch, we know it
2637 is not in a delay slot. */
2638 prev_insn_is_delay_slot = 0;
2639
2640 prev_insn_fixp[0] = fixp[0];
2641 prev_insn_fixp[1] = fixp[1];
2642 prev_insn_fixp[2] = fixp[2];
2643 prev_insn_reloc_type[0] = reloc_type[0];
2644 prev_insn_reloc_type[1] = reloc_type[1];
2645 prev_insn_reloc_type[2] = reloc_type[2];
2646 if (mips_opts.mips16)
2647 prev_insn_extended = (ip->use_extend
2648 || *reloc_type > BFD_RELOC_UNUSED);
2649 }
2650
2651 prev_prev_insn_unreordered = prev_insn_unreordered;
2652 prev_insn_unreordered = 0;
2653 prev_insn_frag = frag_now;
2654 prev_insn_where = f - frag_now->fr_literal;
2655 prev_insn_valid = 1;
2656 }
2657 else if (place == NULL)
2658 {
2659 /* We need to record a bit of information even when we are not
2660 reordering, in order to determine the base address for mips16
2661 PC relative relocs. */
2662 prev_prev_insn = prev_insn;
2663 prev_insn = *ip;
2664 prev_insn_reloc_type[0] = reloc_type[0];
2665 prev_insn_reloc_type[1] = reloc_type[1];
2666 prev_insn_reloc_type[2] = reloc_type[2];
2667 prev_prev_insn_unreordered = prev_insn_unreordered;
2668 prev_insn_unreordered = 1;
2669 }
2670
2671 /* We just output an insn, so the next one doesn't have a label. */
2672 mips_clear_insn_labels ();
2673
2674 /* We must ensure that a fixup associated with an unmatched %hi
2675 reloc does not become a variant frag. Otherwise, the
2676 rearrangement of %hi relocs in frob_file may confuse
2677 tc_gen_reloc. */
2678 if (unmatched_hi)
2679 {
2680 frag_wane (frag_now);
2681 frag_new (0);
2682 }
2683 }
2684
2685 /* This function forgets that there was any previous instruction or
2686 label. If PRESERVE is non-zero, it remembers enough information to
2687 know whether nops are needed before a noreorder section. */
2688
2689 static void
2690 mips_no_prev_insn (preserve)
2691 int preserve;
2692 {
2693 if (! preserve)
2694 {
2695 prev_insn.insn_mo = &dummy_opcode;
2696 prev_prev_insn.insn_mo = &dummy_opcode;
2697 prev_nop_frag = NULL;
2698 prev_nop_frag_holds = 0;
2699 prev_nop_frag_required = 0;
2700 prev_nop_frag_since = 0;
2701 }
2702 prev_insn_valid = 0;
2703 prev_insn_is_delay_slot = 0;
2704 prev_insn_unreordered = 0;
2705 prev_insn_extended = 0;
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_prev_insn_unreordered = 0;
2710 mips_clear_insn_labels ();
2711 }
2712
2713 /* This function must be called whenever we turn on noreorder or emit
2714 something other than instructions. It inserts any NOPS which might
2715 be needed by the previous instruction, and clears the information
2716 kept for the previous instructions. The INSNS parameter is true if
2717 instructions are to follow. */
2718
2719 static void
2720 mips_emit_delays (insns)
2721 boolean insns;
2722 {
2723 if (! mips_opts.noreorder)
2724 {
2725 int nops;
2726
2727 nops = 0;
2728 if ((! mips_opts.mips16
2729 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2730 && (! cop_interlocks
2731 && (prev_insn.insn_mo->pinfo
2732 & (INSN_LOAD_COPROC_DELAY
2733 | INSN_COPROC_MOVE_DELAY
2734 | INSN_WRITE_COND_CODE))))
2735 || (! hilo_interlocks
2736 && (prev_insn.insn_mo->pinfo
2737 & (INSN_READ_LO
2738 | INSN_READ_HI)))
2739 || (! mips_opts.mips16
2740 && ! gpr_interlocks
2741 && (prev_insn.insn_mo->pinfo
2742 & INSN_LOAD_MEMORY_DELAY))
2743 || (! mips_opts.mips16
2744 && mips_opts.isa == ISA_MIPS1
2745 && (prev_insn.insn_mo->pinfo
2746 & INSN_COPROC_MEMORY_DELAY)))
2747 {
2748 /* Itbl support may require additional care here. */
2749 ++nops;
2750 if ((! mips_opts.mips16
2751 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2752 && (! cop_interlocks
2753 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2754 || (! hilo_interlocks
2755 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2756 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2757 ++nops;
2758
2759 if (prev_insn_unreordered)
2760 nops = 0;
2761 }
2762 else if ((! mips_opts.mips16
2763 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2764 && (! cop_interlocks
2765 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2766 || (! hilo_interlocks
2767 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2768 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2769 {
2770 /* Itbl support may require additional care here. */
2771 if (! prev_prev_insn_unreordered)
2772 ++nops;
2773 }
2774
2775 if (nops > 0)
2776 {
2777 struct insn_label_list *l;
2778
2779 if (insns)
2780 {
2781 /* Record the frag which holds the nop instructions, so
2782 that we can remove them if we don't need them. */
2783 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2784 prev_nop_frag = frag_now;
2785 prev_nop_frag_holds = nops;
2786 prev_nop_frag_required = 0;
2787 prev_nop_frag_since = 0;
2788 }
2789
2790 for (; nops > 0; --nops)
2791 emit_nop ();
2792
2793 if (insns)
2794 {
2795 /* Move on to a new frag, so that it is safe to simply
2796 decrease the size of prev_nop_frag. */
2797 frag_wane (frag_now);
2798 frag_new (0);
2799 }
2800
2801 for (l = insn_labels; l != NULL; l = l->next)
2802 {
2803 valueT val;
2804
2805 assert (S_GET_SEGMENT (l->label) == now_seg);
2806 symbol_set_frag (l->label, frag_now);
2807 val = (valueT) frag_now_fix ();
2808 /* mips16 text labels are stored as odd. */
2809 if (mips_opts.mips16)
2810 val += 1;
2811 S_SET_VALUE (l->label, val);
2812 }
2813 }
2814 }
2815
2816 /* Mark instruction labels in mips16 mode. */
2817 if (mips_opts.mips16 && insns)
2818 mips16_mark_labels ();
2819
2820 mips_no_prev_insn (insns);
2821 }
2822
2823 /* Build an instruction created by a macro expansion. This is passed
2824 a pointer to the count of instructions created so far, an
2825 expression, the name of the instruction to build, an operand format
2826 string, and corresponding arguments. */
2827
2828 #ifdef USE_STDARG
2829 static void
2830 macro_build (char *place,
2831 int *counter,
2832 expressionS * ep,
2833 const char *name,
2834 const char *fmt,
2835 ...)
2836 #else
2837 static void
2838 macro_build (place, counter, ep, name, fmt, va_alist)
2839 char *place;
2840 int *counter;
2841 expressionS *ep;
2842 const char *name;
2843 const char *fmt;
2844 va_dcl
2845 #endif
2846 {
2847 struct mips_cl_insn insn;
2848 bfd_reloc_code_real_type r[3];
2849 va_list args;
2850
2851 #ifdef USE_STDARG
2852 va_start (args, fmt);
2853 #else
2854 va_start (args);
2855 #endif
2856
2857 /*
2858 * If the macro is about to expand into a second instruction,
2859 * print a warning if needed. We need to pass ip as a parameter
2860 * to generate a better warning message here...
2861 */
2862 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2863 as_warn (_("Macro instruction expanded into multiple instructions"));
2864
2865 if (place == NULL)
2866 *counter += 1; /* bump instruction counter */
2867
2868 if (mips_opts.mips16)
2869 {
2870 mips16_macro_build (place, counter, ep, name, fmt, args);
2871 va_end (args);
2872 return;
2873 }
2874
2875 r[0] = BFD_RELOC_UNUSED;
2876 r[1] = BFD_RELOC_UNUSED;
2877 r[2] = BFD_RELOC_UNUSED;
2878 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2879 assert (insn.insn_mo);
2880 assert (strcmp (name, insn.insn_mo->name) == 0);
2881
2882 /* Search until we get a match for NAME. */
2883 while (1)
2884 {
2885 if (strcmp (fmt, insn.insn_mo->args) == 0
2886 && insn.insn_mo->pinfo != INSN_MACRO
2887 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2888 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2889 break;
2890
2891 ++insn.insn_mo;
2892 assert (insn.insn_mo->name);
2893 assert (strcmp (name, insn.insn_mo->name) == 0);
2894 }
2895
2896 insn.insn_opcode = insn.insn_mo->match;
2897 for (;;)
2898 {
2899 switch (*fmt++)
2900 {
2901 case '\0':
2902 break;
2903
2904 case ',':
2905 case '(':
2906 case ')':
2907 continue;
2908
2909 case 't':
2910 case 'w':
2911 case 'E':
2912 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2913 continue;
2914
2915 case 'c':
2916 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2917 continue;
2918
2919 case 'T':
2920 case 'W':
2921 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2922 continue;
2923
2924 case 'd':
2925 case 'G':
2926 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2927 continue;
2928
2929 case 'U':
2930 {
2931 int tmp = va_arg (args, int);
2932
2933 insn.insn_opcode |= tmp << OP_SH_RT;
2934 insn.insn_opcode |= tmp << OP_SH_RD;
2935 continue;
2936 }
2937
2938 case 'V':
2939 case 'S':
2940 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2941 continue;
2942
2943 case 'z':
2944 continue;
2945
2946 case '<':
2947 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2948 continue;
2949
2950 case 'D':
2951 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2952 continue;
2953
2954 case 'B':
2955 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2956 continue;
2957
2958 case 'J':
2959 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2960 continue;
2961
2962 case 'q':
2963 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2964 continue;
2965
2966 case 'b':
2967 case 's':
2968 case 'r':
2969 case 'v':
2970 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2971 continue;
2972
2973 case 'i':
2974 case 'j':
2975 case 'o':
2976 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2977 assert (*r == BFD_RELOC_GPREL16
2978 || *r == BFD_RELOC_MIPS_LITERAL
2979 || *r == BFD_RELOC_MIPS_HIGHER
2980 || *r == BFD_RELOC_HI16_S
2981 || *r == BFD_RELOC_LO16
2982 || *r == BFD_RELOC_MIPS_GOT16
2983 || *r == BFD_RELOC_MIPS_CALL16
2984 || *r == BFD_RELOC_MIPS_GOT_LO16
2985 || *r == BFD_RELOC_MIPS_CALL_LO16
2986 || (ep->X_op == O_subtract
2987 && *r == BFD_RELOC_PCREL_LO16));
2988 continue;
2989
2990 case 'u':
2991 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2992 assert (ep != NULL
2993 && (ep->X_op == O_constant
2994 || (ep->X_op == O_symbol
2995 && (*r == BFD_RELOC_MIPS_HIGHEST
2996 || *r == BFD_RELOC_HI16_S
2997 || *r == BFD_RELOC_HI16
2998 || *r == BFD_RELOC_GPREL16
2999 || *r == BFD_RELOC_MIPS_GOT_HI16
3000 || *r == BFD_RELOC_MIPS_CALL_HI16))
3001 || (ep->X_op == O_subtract
3002 && *r == BFD_RELOC_PCREL_HI16_S)));
3003 continue;
3004
3005 case 'p':
3006 assert (ep != NULL);
3007 /*
3008 * This allows macro() to pass an immediate expression for
3009 * creating short branches without creating a symbol.
3010 * Note that the expression still might come from the assembly
3011 * input, in which case the value is not checked for range nor
3012 * is a relocation entry generated (yuck).
3013 */
3014 if (ep->X_op == O_constant)
3015 {
3016 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3017 ep = NULL;
3018 }
3019 else
3020 if (mips_pic == EMBEDDED_PIC)
3021 *r = BFD_RELOC_16_PCREL_S2;
3022 else
3023 *r = BFD_RELOC_16_PCREL;
3024 continue;
3025
3026 case 'a':
3027 assert (ep != NULL);
3028 *r = BFD_RELOC_MIPS_JMP;
3029 continue;
3030
3031 case 'C':
3032 insn.insn_opcode |= va_arg (args, unsigned long);
3033 continue;
3034
3035 default:
3036 internalError ();
3037 }
3038 break;
3039 }
3040 va_end (args);
3041 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3042
3043 append_insn (place, &insn, ep, r, false);
3044 }
3045
3046 static void
3047 mips16_macro_build (place, counter, ep, name, fmt, args)
3048 char *place;
3049 int *counter ATTRIBUTE_UNUSED;
3050 expressionS *ep;
3051 const char *name;
3052 const char *fmt;
3053 va_list args;
3054 {
3055 struct mips_cl_insn insn;
3056 bfd_reloc_code_real_type r[3]
3057 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3058
3059 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3060 assert (insn.insn_mo);
3061 assert (strcmp (name, insn.insn_mo->name) == 0);
3062
3063 while (strcmp (fmt, insn.insn_mo->args) != 0
3064 || insn.insn_mo->pinfo == INSN_MACRO)
3065 {
3066 ++insn.insn_mo;
3067 assert (insn.insn_mo->name);
3068 assert (strcmp (name, insn.insn_mo->name) == 0);
3069 }
3070
3071 insn.insn_opcode = insn.insn_mo->match;
3072 insn.use_extend = false;
3073
3074 for (;;)
3075 {
3076 int c;
3077
3078 c = *fmt++;
3079 switch (c)
3080 {
3081 case '\0':
3082 break;
3083
3084 case ',':
3085 case '(':
3086 case ')':
3087 continue;
3088
3089 case 'y':
3090 case 'w':
3091 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3092 continue;
3093
3094 case 'x':
3095 case 'v':
3096 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3097 continue;
3098
3099 case 'z':
3100 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3101 continue;
3102
3103 case 'Z':
3104 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3105 continue;
3106
3107 case '0':
3108 case 'S':
3109 case 'P':
3110 case 'R':
3111 continue;
3112
3113 case 'X':
3114 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3115 continue;
3116
3117 case 'Y':
3118 {
3119 int regno;
3120
3121 regno = va_arg (args, int);
3122 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3123 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3124 }
3125 continue;
3126
3127 case '<':
3128 case '>':
3129 case '4':
3130 case '5':
3131 case 'H':
3132 case 'W':
3133 case 'D':
3134 case 'j':
3135 case '8':
3136 case 'V':
3137 case 'C':
3138 case 'U':
3139 case 'k':
3140 case 'K':
3141 case 'p':
3142 case 'q':
3143 {
3144 assert (ep != NULL);
3145
3146 if (ep->X_op != O_constant)
3147 *r = BFD_RELOC_UNUSED + c;
3148 else
3149 {
3150 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3151 false, &insn.insn_opcode, &insn.use_extend,
3152 &insn.extend);
3153 ep = NULL;
3154 *r = BFD_RELOC_UNUSED;
3155 }
3156 }
3157 continue;
3158
3159 case '6':
3160 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3161 continue;
3162 }
3163
3164 break;
3165 }
3166
3167 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3168
3169 append_insn (place, &insn, ep, r, false);
3170 }
3171
3172 /*
3173 * Generate a "lui" instruction.
3174 */
3175 static void
3176 macro_build_lui (place, counter, ep, regnum)
3177 char *place;
3178 int *counter;
3179 expressionS *ep;
3180 int regnum;
3181 {
3182 expressionS high_expr;
3183 struct mips_cl_insn insn;
3184 bfd_reloc_code_real_type r[3]
3185 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3186 CONST char *name = "lui";
3187 CONST char *fmt = "t,u";
3188
3189 assert (! mips_opts.mips16);
3190
3191 if (place == NULL)
3192 high_expr = *ep;
3193 else
3194 {
3195 high_expr.X_op = O_constant;
3196 high_expr.X_add_number = ep->X_add_number;
3197 }
3198
3199 if (high_expr.X_op == O_constant)
3200 {
3201 /* we can compute the instruction now without a relocation entry */
3202 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3203 >> 16) & 0xffff;
3204 *r = BFD_RELOC_UNUSED;
3205 }
3206 else if (! HAVE_NEWABI)
3207 {
3208 assert (ep->X_op == O_symbol);
3209 /* _gp_disp is a special case, used from s_cpload. */
3210 assert (mips_pic == NO_PIC
3211 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3212 *r = BFD_RELOC_HI16_S;
3213 }
3214
3215 /*
3216 * If the macro is about to expand into a second instruction,
3217 * print a warning if needed. We need to pass ip as a parameter
3218 * to generate a better warning message here...
3219 */
3220 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3221 as_warn (_("Macro instruction expanded into multiple instructions"));
3222
3223 if (place == NULL)
3224 *counter += 1; /* bump instruction counter */
3225
3226 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3227 assert (insn.insn_mo);
3228 assert (strcmp (name, insn.insn_mo->name) == 0);
3229 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3230
3231 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3232 if (*r == BFD_RELOC_UNUSED)
3233 {
3234 insn.insn_opcode |= high_expr.X_add_number;
3235 append_insn (place, &insn, NULL, r, false);
3236 }
3237 else
3238 append_insn (place, &insn, &high_expr, r, false);
3239 }
3240
3241 /* set_at()
3242 * Generates code to set the $at register to true (one)
3243 * if reg is less than the immediate expression.
3244 */
3245 static void
3246 set_at (counter, reg, unsignedp)
3247 int *counter;
3248 int reg;
3249 int unsignedp;
3250 {
3251 if (imm_expr.X_op == O_constant
3252 && imm_expr.X_add_number >= -0x8000
3253 && imm_expr.X_add_number < 0x8000)
3254 macro_build ((char *) NULL, counter, &imm_expr,
3255 unsignedp ? "sltiu" : "slti",
3256 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3257 else
3258 {
3259 load_register (counter, AT, &imm_expr, 0);
3260 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3261 unsignedp ? "sltu" : "slt",
3262 "d,v,t", AT, reg, AT);
3263 }
3264 }
3265
3266 /* Warn if an expression is not a constant. */
3267
3268 static void
3269 check_absolute_expr (ip, ex)
3270 struct mips_cl_insn *ip;
3271 expressionS *ex;
3272 {
3273 if (ex->X_op == O_big)
3274 as_bad (_("unsupported large constant"));
3275 else if (ex->X_op != O_constant)
3276 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3277 }
3278
3279 /* Count the leading zeroes by performing a binary chop. This is a
3280 bulky bit of source, but performance is a LOT better for the
3281 majority of values than a simple loop to count the bits:
3282 for (lcnt = 0; (lcnt < 32); lcnt++)
3283 if ((v) & (1 << (31 - lcnt)))
3284 break;
3285 However it is not code size friendly, and the gain will drop a bit
3286 on certain cached systems.
3287 */
3288 #define COUNT_TOP_ZEROES(v) \
3289 (((v) & ~0xffff) == 0 \
3290 ? ((v) & ~0xff) == 0 \
3291 ? ((v) & ~0xf) == 0 \
3292 ? ((v) & ~0x3) == 0 \
3293 ? ((v) & ~0x1) == 0 \
3294 ? !(v) \
3295 ? 32 \
3296 : 31 \
3297 : 30 \
3298 : ((v) & ~0x7) == 0 \
3299 ? 29 \
3300 : 28 \
3301 : ((v) & ~0x3f) == 0 \
3302 ? ((v) & ~0x1f) == 0 \
3303 ? 27 \
3304 : 26 \
3305 : ((v) & ~0x7f) == 0 \
3306 ? 25 \
3307 : 24 \
3308 : ((v) & ~0xfff) == 0 \
3309 ? ((v) & ~0x3ff) == 0 \
3310 ? ((v) & ~0x1ff) == 0 \
3311 ? 23 \
3312 : 22 \
3313 : ((v) & ~0x7ff) == 0 \
3314 ? 21 \
3315 : 20 \
3316 : ((v) & ~0x3fff) == 0 \
3317 ? ((v) & ~0x1fff) == 0 \
3318 ? 19 \
3319 : 18 \
3320 : ((v) & ~0x7fff) == 0 \
3321 ? 17 \
3322 : 16 \
3323 : ((v) & ~0xffffff) == 0 \
3324 ? ((v) & ~0xfffff) == 0 \
3325 ? ((v) & ~0x3ffff) == 0 \
3326 ? ((v) & ~0x1ffff) == 0 \
3327 ? 15 \
3328 : 14 \
3329 : ((v) & ~0x7ffff) == 0 \
3330 ? 13 \
3331 : 12 \
3332 : ((v) & ~0x3fffff) == 0 \
3333 ? ((v) & ~0x1fffff) == 0 \
3334 ? 11 \
3335 : 10 \
3336 : ((v) & ~0x7fffff) == 0 \
3337 ? 9 \
3338 : 8 \
3339 : ((v) & ~0xfffffff) == 0 \
3340 ? ((v) & ~0x3ffffff) == 0 \
3341 ? ((v) & ~0x1ffffff) == 0 \
3342 ? 7 \
3343 : 6 \
3344 : ((v) & ~0x7ffffff) == 0 \
3345 ? 5 \
3346 : 4 \
3347 : ((v) & ~0x3fffffff) == 0 \
3348 ? ((v) & ~0x1fffffff) == 0 \
3349 ? 3 \
3350 : 2 \
3351 : ((v) & ~0x7fffffff) == 0 \
3352 ? 1 \
3353 : 0)
3354
3355 /* load_register()
3356 * This routine generates the least number of instructions neccessary to load
3357 * an absolute expression value into a register.
3358 */
3359 static void
3360 load_register (counter, reg, ep, dbl)
3361 int *counter;
3362 int reg;
3363 expressionS *ep;
3364 int dbl;
3365 {
3366 int freg;
3367 expressionS hi32, lo32;
3368
3369 if (ep->X_op != O_big)
3370 {
3371 assert (ep->X_op == O_constant);
3372 if (ep->X_add_number < 0x8000
3373 && (ep->X_add_number >= 0
3374 || (ep->X_add_number >= -0x8000
3375 && (! dbl
3376 || ! ep->X_unsigned
3377 || sizeof (ep->X_add_number) > 4))))
3378 {
3379 /* We can handle 16 bit signed values with an addiu to
3380 $zero. No need to ever use daddiu here, since $zero and
3381 the result are always correct in 32 bit mode. */
3382 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3383 (int) BFD_RELOC_LO16);
3384 return;
3385 }
3386 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3387 {
3388 /* We can handle 16 bit unsigned values with an ori to
3389 $zero. */
3390 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3391 (int) BFD_RELOC_LO16);
3392 return;
3393 }
3394 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3395 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3396 == ~ (offsetT) 0x7fffffff))
3397 && (! dbl
3398 || ! ep->X_unsigned
3399 || sizeof (ep->X_add_number) > 4
3400 || (ep->X_add_number & 0x80000000) == 0))
3401 || ((HAVE_32BIT_GPRS || ! dbl)
3402 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3403 || (HAVE_32BIT_GPRS
3404 && ! dbl
3405 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3406 == ~ (offsetT) 0xffffffff)))
3407 {
3408 /* 32 bit values require an lui. */
3409 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3410 (int) BFD_RELOC_HI16);
3411 if ((ep->X_add_number & 0xffff) != 0)
3412 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3413 (int) BFD_RELOC_LO16);
3414 return;
3415 }
3416 }
3417
3418 /* The value is larger than 32 bits. */
3419
3420 if (HAVE_32BIT_GPRS)
3421 {
3422 as_bad (_("Number (0x%lx) larger than 32 bits"),
3423 (unsigned long) ep->X_add_number);
3424 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3425 (int) BFD_RELOC_LO16);
3426 return;
3427 }
3428
3429 if (ep->X_op != O_big)
3430 {
3431 hi32 = *ep;
3432 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3433 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3434 hi32.X_add_number &= 0xffffffff;
3435 lo32 = *ep;
3436 lo32.X_add_number &= 0xffffffff;
3437 }
3438 else
3439 {
3440 assert (ep->X_add_number > 2);
3441 if (ep->X_add_number == 3)
3442 generic_bignum[3] = 0;
3443 else if (ep->X_add_number > 4)
3444 as_bad (_("Number larger than 64 bits"));
3445 lo32.X_op = O_constant;
3446 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3447 hi32.X_op = O_constant;
3448 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3449 }
3450
3451 if (hi32.X_add_number == 0)
3452 freg = 0;
3453 else
3454 {
3455 int shift, bit;
3456 unsigned long hi, lo;
3457
3458 if (hi32.X_add_number == (offsetT) 0xffffffff)
3459 {
3460 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3461 {
3462 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3463 reg, 0, (int) BFD_RELOC_LO16);
3464 return;
3465 }
3466 if (lo32.X_add_number & 0x80000000)
3467 {
3468 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3469 (int) BFD_RELOC_HI16);
3470 if (lo32.X_add_number & 0xffff)
3471 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3472 reg, reg, (int) BFD_RELOC_LO16);
3473 return;
3474 }
3475 }
3476
3477 /* Check for 16bit shifted constant. We know that hi32 is
3478 non-zero, so start the mask on the first bit of the hi32
3479 value. */
3480 shift = 17;
3481 do
3482 {
3483 unsigned long himask, lomask;
3484
3485 if (shift < 32)
3486 {
3487 himask = 0xffff >> (32 - shift);
3488 lomask = (0xffff << shift) & 0xffffffff;
3489 }
3490 else
3491 {
3492 himask = 0xffff << (shift - 32);
3493 lomask = 0;
3494 }
3495 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3496 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3497 {
3498 expressionS tmp;
3499
3500 tmp.X_op = O_constant;
3501 if (shift < 32)
3502 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3503 | (lo32.X_add_number >> shift));
3504 else
3505 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3506 macro_build ((char *) NULL, counter, &tmp,
3507 "ori", "t,r,i", reg, 0,
3508 (int) BFD_RELOC_LO16);
3509 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3510 (shift >= 32) ? "dsll32" : "dsll",
3511 "d,w,<", reg, reg,
3512 (shift >= 32) ? shift - 32 : shift);
3513 return;
3514 }
3515 shift++;
3516 }
3517 while (shift <= (64 - 16));
3518
3519 /* Find the bit number of the lowest one bit, and store the
3520 shifted value in hi/lo. */
3521 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3522 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3523 if (lo != 0)
3524 {
3525 bit = 0;
3526 while ((lo & 1) == 0)
3527 {
3528 lo >>= 1;
3529 ++bit;
3530 }
3531 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3532 hi >>= bit;
3533 }
3534 else
3535 {
3536 bit = 32;
3537 while ((hi & 1) == 0)
3538 {
3539 hi >>= 1;
3540 ++bit;
3541 }
3542 lo = hi;
3543 hi = 0;
3544 }
3545
3546 /* Optimize if the shifted value is a (power of 2) - 1. */
3547 if ((hi == 0 && ((lo + 1) & lo) == 0)
3548 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3549 {
3550 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3551 if (shift != 0)
3552 {
3553 expressionS tmp;
3554
3555 /* This instruction will set the register to be all
3556 ones. */
3557 tmp.X_op = O_constant;
3558 tmp.X_add_number = (offsetT) -1;
3559 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3560 reg, 0, (int) BFD_RELOC_LO16);
3561 if (bit != 0)
3562 {
3563 bit += shift;
3564 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3565 (bit >= 32) ? "dsll32" : "dsll",
3566 "d,w,<", reg, reg,
3567 (bit >= 32) ? bit - 32 : bit);
3568 }
3569 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3570 (shift >= 32) ? "dsrl32" : "dsrl",
3571 "d,w,<", reg, reg,
3572 (shift >= 32) ? shift - 32 : shift);
3573 return;
3574 }
3575 }
3576
3577 /* Sign extend hi32 before calling load_register, because we can
3578 generally get better code when we load a sign extended value. */
3579 if ((hi32.X_add_number & 0x80000000) != 0)
3580 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3581 load_register (counter, reg, &hi32, 0);
3582 freg = reg;
3583 }
3584 if ((lo32.X_add_number & 0xffff0000) == 0)
3585 {
3586 if (freg != 0)
3587 {
3588 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3589 "dsll32", "d,w,<", reg, freg, 0);
3590 freg = reg;
3591 }
3592 }
3593 else
3594 {
3595 expressionS mid16;
3596
3597 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3598 {
3599 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3600 (int) BFD_RELOC_HI16);
3601 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3602 "dsrl32", "d,w,<", reg, reg, 0);
3603 return;
3604 }
3605
3606 if (freg != 0)
3607 {
3608 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3609 "d,w,<", reg, freg, 16);
3610 freg = reg;
3611 }
3612 mid16 = lo32;
3613 mid16.X_add_number >>= 16;
3614 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3615 freg, (int) BFD_RELOC_LO16);
3616 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3617 "d,w,<", reg, reg, 16);
3618 freg = reg;
3619 }
3620 if ((lo32.X_add_number & 0xffff) != 0)
3621 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3622 (int) BFD_RELOC_LO16);
3623 }
3624
3625 /* Load an address into a register. */
3626
3627 static void
3628 load_address (counter, reg, ep, dbl, used_at)
3629 int *counter;
3630 int reg;
3631 expressionS *ep;
3632 int dbl;
3633 int *used_at;
3634 {
3635 char *p;
3636
3637 if (ep->X_op != O_constant
3638 && ep->X_op != O_symbol)
3639 {
3640 as_bad (_("expression too complex"));
3641 ep->X_op = O_constant;
3642 }
3643
3644 if (ep->X_op == O_constant)
3645 {
3646 load_register (counter, reg, ep, dbl);
3647 return;
3648 }
3649
3650 if (mips_pic == NO_PIC)
3651 {
3652 /* If this is a reference to a GP relative symbol, we want
3653 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3654 Otherwise we want
3655 lui $reg,<sym> (BFD_RELOC_HI16_S)
3656 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3657 If we have an addend, we always use the latter form.
3658
3659 With 64bit address space and a usable $at we want
3660 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3661 lui $at,<sym> (BFD_RELOC_HI16_S)
3662 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3663 daddiu $at,<sym> (BFD_RELOC_LO16)
3664 dsll32 $reg,0
3665 dadd $reg,$reg,$at
3666
3667 If $at is already in use, we use an path which is suboptimal
3668 on superscalar processors.
3669 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3670 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3671 dsll $reg,16
3672 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3673 dsll $reg,16
3674 daddiu $reg,<sym> (BFD_RELOC_LO16)
3675 */
3676 if (dbl)
3677 {
3678 p = NULL;
3679
3680 /* We don't do GP optimization for now because RELAX_ENCODE can't
3681 hold the data for such large chunks. */
3682
3683 if (*used_at == 0)
3684 {
3685 macro_build (p, counter, ep, "lui", "t,u",
3686 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3687 macro_build (p, counter, ep, "lui", "t,u",
3688 AT, (int) BFD_RELOC_HI16_S);
3689 macro_build (p, counter, ep, "daddiu", "t,r,j",
3690 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3691 macro_build (p, counter, ep, "daddiu", "t,r,j",
3692 AT, AT, (int) BFD_RELOC_LO16);
3693 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3694 "d,w,<", reg, reg, 0);
3695 macro_build (p, counter, (expressionS *) NULL, "dadd",
3696 "d,v,t", reg, reg, AT);
3697 *used_at = 1;
3698 }
3699 else
3700 {
3701 macro_build (p, counter, ep, "lui", "t,u",
3702 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3703 macro_build (p, counter, ep, "daddiu", "t,r,j",
3704 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3705 macro_build (p, counter, (expressionS *) NULL, "dsll",
3706 "d,w,<", reg, reg, 16);
3707 macro_build (p, counter, ep, "daddiu", "t,r,j",
3708 reg, reg, (int) BFD_RELOC_HI16_S);
3709 macro_build (p, counter, (expressionS *) NULL, "dsll",
3710 "d,w,<", reg, reg, 16);
3711 macro_build (p, counter, ep, "daddiu", "t,r,j",
3712 reg, reg, (int) BFD_RELOC_LO16);
3713 }
3714 }
3715 else
3716 {
3717 p = NULL;
3718 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3719 && ! nopic_need_relax (ep->X_add_symbol, 1))
3720 {
3721 frag_grow (20);
3722 macro_build ((char *) NULL, counter, ep,
3723 dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3724 (int) BFD_RELOC_GPREL16);
3725 p = frag_var (rs_machine_dependent, 8, 0,
3726 RELAX_ENCODE (4, 8, 0, 4, 0,
3727 mips_opts.warn_about_macros),
3728 ep->X_add_symbol, 0, NULL);
3729 }
3730 macro_build_lui (p, counter, ep, reg);
3731 if (p != NULL)
3732 p += 4;
3733 macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3734 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3735 }
3736 }
3737 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3738 {
3739 expressionS ex;
3740
3741 /* If this is a reference to an external symbol, we want
3742 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3743 Otherwise we want
3744 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3745 nop
3746 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3747 If there is a constant, it must be added in after. */
3748 ex.X_add_number = ep->X_add_number;
3749 ep->X_add_number = 0;
3750 frag_grow (20);
3751 macro_build ((char *) NULL, counter, ep,
3752 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3753 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3754 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3755 p = frag_var (rs_machine_dependent, 4, 0,
3756 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3757 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3758 macro_build (p, counter, ep,
3759 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3760 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3761 if (ex.X_add_number != 0)
3762 {
3763 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3764 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3765 ex.X_op = O_constant;
3766 macro_build ((char *) NULL, counter, &ex,
3767 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3768 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3769 }
3770 }
3771 else if (mips_pic == SVR4_PIC)
3772 {
3773 expressionS ex;
3774 int off;
3775
3776 /* This is the large GOT case. If this is a reference to an
3777 external symbol, we want
3778 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3779 addu $reg,$reg,$gp
3780 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3781 Otherwise, for a reference to a local symbol, we want
3782 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3783 nop
3784 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3785 If there is a constant, it must be added in after. */
3786 ex.X_add_number = ep->X_add_number;
3787 ep->X_add_number = 0;
3788 if (reg_needs_delay (GP))
3789 off = 4;
3790 else
3791 off = 0;
3792 frag_grow (32);
3793 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3794 (int) BFD_RELOC_MIPS_GOT_HI16);
3795 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3796 dbl ? "daddu" : "addu", "d,v,t", reg, reg, GP);
3797 macro_build ((char *) NULL, counter, ep, dbl ? "ld" : "lw",
3798 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3799 p = frag_var (rs_machine_dependent, 12 + off, 0,
3800 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3801 mips_opts.warn_about_macros),
3802 ep->X_add_symbol, 0, NULL);
3803 if (off > 0)
3804 {
3805 /* We need a nop before loading from $gp. This special
3806 check is required because the lui which starts the main
3807 instruction stream does not refer to $gp, and so will not
3808 insert the nop which may be required. */
3809 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3810 p += 4;
3811 }
3812 macro_build (p, counter, ep, dbl ? "ld" : "lw",
3813 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3814 p += 4;
3815 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3816 p += 4;
3817 macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3818 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3819 if (ex.X_add_number != 0)
3820 {
3821 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3822 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3823 ex.X_op = O_constant;
3824 macro_build ((char *) NULL, counter, &ex, dbl ? "daddiu" : "addiu",
3825 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3826 }
3827 }
3828 else if (mips_pic == EMBEDDED_PIC)
3829 {
3830 /* We always do
3831 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3832 */
3833 macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3834 "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
3835 }
3836 else
3837 abort ();
3838 }
3839
3840 /* Move the contents of register SOURCE into register DEST. */
3841
3842 static void
3843 move_register (counter, dest, source)
3844 int *counter;
3845 int dest;
3846 int source;
3847 {
3848 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3849 HAVE_32BIT_GPRS ? "addu" : "daddu",
3850 "d,v,t", dest, source, 0);
3851 }
3852
3853 /*
3854 * Build macros
3855 * This routine implements the seemingly endless macro or synthesized
3856 * instructions and addressing modes in the mips assembly language. Many
3857 * of these macros are simple and are similar to each other. These could
3858 * probably be handled by some kind of table or grammer aproach instead of
3859 * this verbose method. Others are not simple macros but are more like
3860 * optimizing code generation.
3861 * One interesting optimization is when several store macros appear
3862 * consecutivly that would load AT with the upper half of the same address.
3863 * The ensuing load upper instructions are ommited. This implies some kind
3864 * of global optimization. We currently only optimize within a single macro.
3865 * For many of the load and store macros if the address is specified as a
3866 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3867 * first load register 'at' with zero and use it as the base register. The
3868 * mips assembler simply uses register $zero. Just one tiny optimization
3869 * we're missing.
3870 */
3871 static void
3872 macro (ip)
3873 struct mips_cl_insn *ip;
3874 {
3875 register int treg, sreg, dreg, breg;
3876 int tempreg;
3877 int mask;
3878 int icnt = 0;
3879 int used_at = 0;
3880 expressionS expr1;
3881 const char *s;
3882 const char *s2;
3883 const char *fmt;
3884 int likely = 0;
3885 int dbl = 0;
3886 int coproc = 0;
3887 int lr = 0;
3888 int imm = 0;
3889 offsetT maxnum;
3890 int off;
3891 bfd_reloc_code_real_type r;
3892 char *p;
3893 int hold_mips_optimize;
3894
3895 assert (! mips_opts.mips16);
3896
3897 treg = (ip->insn_opcode >> 16) & 0x1f;
3898 dreg = (ip->insn_opcode >> 11) & 0x1f;
3899 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3900 mask = ip->insn_mo->mask;
3901
3902 expr1.X_op = O_constant;
3903 expr1.X_op_symbol = NULL;
3904 expr1.X_add_symbol = NULL;
3905 expr1.X_add_number = 1;
3906
3907 switch (mask)
3908 {
3909 case M_DABS:
3910 dbl = 1;
3911 case M_ABS:
3912 /* bgez $a0,.+12
3913 move v0,$a0
3914 sub v0,$zero,$a0
3915 */
3916
3917 mips_emit_delays (true);
3918 ++mips_opts.noreorder;
3919 mips_any_noreorder = 1;
3920
3921 expr1.X_add_number = 8;
3922 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3923 if (dreg == sreg)
3924 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3925 0);
3926 else
3927 move_register (&icnt, dreg, sreg);
3928 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3929 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3930
3931 --mips_opts.noreorder;
3932 return;
3933
3934 case M_ADD_I:
3935 s = "addi";
3936 s2 = "add";
3937 goto do_addi;
3938 case M_ADDU_I:
3939 s = "addiu";
3940 s2 = "addu";
3941 goto do_addi;
3942 case M_DADD_I:
3943 dbl = 1;
3944 s = "daddi";
3945 s2 = "dadd";
3946 goto do_addi;
3947 case M_DADDU_I:
3948 dbl = 1;
3949 s = "daddiu";
3950 s2 = "daddu";
3951 do_addi:
3952 if (imm_expr.X_op == O_constant
3953 && imm_expr.X_add_number >= -0x8000
3954 && imm_expr.X_add_number < 0x8000)
3955 {
3956 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3957 (int) BFD_RELOC_LO16);
3958 return;
3959 }
3960 load_register (&icnt, AT, &imm_expr, dbl);
3961 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3962 treg, sreg, AT);
3963 break;
3964
3965 case M_AND_I:
3966 s = "andi";
3967 s2 = "and";
3968 goto do_bit;
3969 case M_OR_I:
3970 s = "ori";
3971 s2 = "or";
3972 goto do_bit;
3973 case M_NOR_I:
3974 s = "";
3975 s2 = "nor";
3976 goto do_bit;
3977 case M_XOR_I:
3978 s = "xori";
3979 s2 = "xor";
3980 do_bit:
3981 if (imm_expr.X_op == O_constant
3982 && imm_expr.X_add_number >= 0
3983 && imm_expr.X_add_number < 0x10000)
3984 {
3985 if (mask != M_NOR_I)
3986 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3987 sreg, (int) BFD_RELOC_LO16);
3988 else
3989 {
3990 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3991 treg, sreg, (int) BFD_RELOC_LO16);
3992 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3993 "d,v,t", treg, treg, 0);
3994 }
3995 return;
3996 }
3997
3998 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3999 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4000 treg, sreg, AT);
4001 break;
4002
4003 case M_BEQ_I:
4004 s = "beq";
4005 goto beq_i;
4006 case M_BEQL_I:
4007 s = "beql";
4008 likely = 1;
4009 goto beq_i;
4010 case M_BNE_I:
4011 s = "bne";
4012 goto beq_i;
4013 case M_BNEL_I:
4014 s = "bnel";
4015 likely = 1;
4016 beq_i:
4017 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4018 {
4019 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4020 0);
4021 return;
4022 }
4023 load_register (&icnt, AT, &imm_expr, 0);
4024 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4025 break;
4026
4027 case M_BGEL:
4028 likely = 1;
4029 case M_BGE:
4030 if (treg == 0)
4031 {
4032 macro_build ((char *) NULL, &icnt, &offset_expr,
4033 likely ? "bgezl" : "bgez", "s,p", sreg);
4034 return;
4035 }
4036 if (sreg == 0)
4037 {
4038 macro_build ((char *) NULL, &icnt, &offset_expr,
4039 likely ? "blezl" : "blez", "s,p", treg);
4040 return;
4041 }
4042 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4043 AT, sreg, treg);
4044 macro_build ((char *) NULL, &icnt, &offset_expr,
4045 likely ? "beql" : "beq", "s,t,p", AT, 0);
4046 break;
4047
4048 case M_BGTL_I:
4049 likely = 1;
4050 case M_BGT_I:
4051 /* check for > max integer */
4052 maxnum = 0x7fffffff;
4053 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4054 {
4055 maxnum <<= 16;
4056 maxnum |= 0xffff;
4057 maxnum <<= 16;
4058 maxnum |= 0xffff;
4059 }
4060 if (imm_expr.X_op == O_constant
4061 && imm_expr.X_add_number >= maxnum
4062 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4063 {
4064 do_false:
4065 /* result is always false */
4066 if (! likely)
4067 {
4068 if (warn_nops)
4069 as_warn (_("Branch %s is always false (nop)"),
4070 ip->insn_mo->name);
4071 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4072 "", 0);
4073 }
4074 else
4075 {
4076 if (warn_nops)
4077 as_warn (_("Branch likely %s is always false"),
4078 ip->insn_mo->name);
4079 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4080 "s,t,p", 0, 0);
4081 }
4082 return;
4083 }
4084 if (imm_expr.X_op != O_constant)
4085 as_bad (_("Unsupported large constant"));
4086 imm_expr.X_add_number++;
4087 /* FALLTHROUGH */
4088 case M_BGE_I:
4089 case M_BGEL_I:
4090 if (mask == M_BGEL_I)
4091 likely = 1;
4092 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4093 {
4094 macro_build ((char *) NULL, &icnt, &offset_expr,
4095 likely ? "bgezl" : "bgez", "s,p", sreg);
4096 return;
4097 }
4098 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4099 {
4100 macro_build ((char *) NULL, &icnt, &offset_expr,
4101 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4102 return;
4103 }
4104 maxnum = 0x7fffffff;
4105 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4106 {
4107 maxnum <<= 16;
4108 maxnum |= 0xffff;
4109 maxnum <<= 16;
4110 maxnum |= 0xffff;
4111 }
4112 maxnum = - maxnum - 1;
4113 if (imm_expr.X_op == O_constant
4114 && imm_expr.X_add_number <= maxnum
4115 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4116 {
4117 do_true:
4118 /* result is always true */
4119 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4120 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4121 return;
4122 }
4123 set_at (&icnt, sreg, 0);
4124 macro_build ((char *) NULL, &icnt, &offset_expr,
4125 likely ? "beql" : "beq", "s,t,p", AT, 0);
4126 break;
4127
4128 case M_BGEUL:
4129 likely = 1;
4130 case M_BGEU:
4131 if (treg == 0)
4132 goto do_true;
4133 if (sreg == 0)
4134 {
4135 macro_build ((char *) NULL, &icnt, &offset_expr,
4136 likely ? "beql" : "beq", "s,t,p", 0, treg);
4137 return;
4138 }
4139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4140 "d,v,t", AT, sreg, treg);
4141 macro_build ((char *) NULL, &icnt, &offset_expr,
4142 likely ? "beql" : "beq", "s,t,p", AT, 0);
4143 break;
4144
4145 case M_BGTUL_I:
4146 likely = 1;
4147 case M_BGTU_I:
4148 if (sreg == 0
4149 || (HAVE_32BIT_GPRS
4150 && imm_expr.X_op == O_constant
4151 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4152 goto do_false;
4153 if (imm_expr.X_op != O_constant)
4154 as_bad (_("Unsupported large constant"));
4155 imm_expr.X_add_number++;
4156 /* FALLTHROUGH */
4157 case M_BGEU_I:
4158 case M_BGEUL_I:
4159 if (mask == M_BGEUL_I)
4160 likely = 1;
4161 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4162 goto do_true;
4163 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4164 {
4165 macro_build ((char *) NULL, &icnt, &offset_expr,
4166 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4167 return;
4168 }
4169 set_at (&icnt, sreg, 1);
4170 macro_build ((char *) NULL, &icnt, &offset_expr,
4171 likely ? "beql" : "beq", "s,t,p", AT, 0);
4172 break;
4173
4174 case M_BGTL:
4175 likely = 1;
4176 case M_BGT:
4177 if (treg == 0)
4178 {
4179 macro_build ((char *) NULL, &icnt, &offset_expr,
4180 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4181 return;
4182 }
4183 if (sreg == 0)
4184 {
4185 macro_build ((char *) NULL, &icnt, &offset_expr,
4186 likely ? "bltzl" : "bltz", "s,p", treg);
4187 return;
4188 }
4189 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4190 AT, treg, sreg);
4191 macro_build ((char *) NULL, &icnt, &offset_expr,
4192 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4193 break;
4194
4195 case M_BGTUL:
4196 likely = 1;
4197 case M_BGTU:
4198 if (treg == 0)
4199 {
4200 macro_build ((char *) NULL, &icnt, &offset_expr,
4201 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4202 return;
4203 }
4204 if (sreg == 0)
4205 goto do_false;
4206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4207 "d,v,t", AT, treg, sreg);
4208 macro_build ((char *) NULL, &icnt, &offset_expr,
4209 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4210 break;
4211
4212 case M_BLEL:
4213 likely = 1;
4214 case M_BLE:
4215 if (treg == 0)
4216 {
4217 macro_build ((char *) NULL, &icnt, &offset_expr,
4218 likely ? "blezl" : "blez", "s,p", sreg);
4219 return;
4220 }
4221 if (sreg == 0)
4222 {
4223 macro_build ((char *) NULL, &icnt, &offset_expr,
4224 likely ? "bgezl" : "bgez", "s,p", treg);
4225 return;
4226 }
4227 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4228 AT, treg, sreg);
4229 macro_build ((char *) NULL, &icnt, &offset_expr,
4230 likely ? "beql" : "beq", "s,t,p", AT, 0);
4231 break;
4232
4233 case M_BLEL_I:
4234 likely = 1;
4235 case M_BLE_I:
4236 maxnum = 0x7fffffff;
4237 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4238 {
4239 maxnum <<= 16;
4240 maxnum |= 0xffff;
4241 maxnum <<= 16;
4242 maxnum |= 0xffff;
4243 }
4244 if (imm_expr.X_op == O_constant
4245 && imm_expr.X_add_number >= maxnum
4246 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4247 goto do_true;
4248 if (imm_expr.X_op != O_constant)
4249 as_bad (_("Unsupported large constant"));
4250 imm_expr.X_add_number++;
4251 /* FALLTHROUGH */
4252 case M_BLT_I:
4253 case M_BLTL_I:
4254 if (mask == M_BLTL_I)
4255 likely = 1;
4256 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4257 {
4258 macro_build ((char *) NULL, &icnt, &offset_expr,
4259 likely ? "bltzl" : "bltz", "s,p", sreg);
4260 return;
4261 }
4262 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4263 {
4264 macro_build ((char *) NULL, &icnt, &offset_expr,
4265 likely ? "blezl" : "blez", "s,p", sreg);
4266 return;
4267 }
4268 set_at (&icnt, sreg, 0);
4269 macro_build ((char *) NULL, &icnt, &offset_expr,
4270 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4271 break;
4272
4273 case M_BLEUL:
4274 likely = 1;
4275 case M_BLEU:
4276 if (treg == 0)
4277 {
4278 macro_build ((char *) NULL, &icnt, &offset_expr,
4279 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4280 return;
4281 }
4282 if (sreg == 0)
4283 goto do_true;
4284 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4285 "d,v,t", AT, treg, sreg);
4286 macro_build ((char *) NULL, &icnt, &offset_expr,
4287 likely ? "beql" : "beq", "s,t,p", AT, 0);
4288 break;
4289
4290 case M_BLEUL_I:
4291 likely = 1;
4292 case M_BLEU_I:
4293 if (sreg == 0
4294 || (HAVE_32BIT_GPRS
4295 && imm_expr.X_op == O_constant
4296 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4297 goto do_true;
4298 if (imm_expr.X_op != O_constant)
4299 as_bad (_("Unsupported large constant"));
4300 imm_expr.X_add_number++;
4301 /* FALLTHROUGH */
4302 case M_BLTU_I:
4303 case M_BLTUL_I:
4304 if (mask == M_BLTUL_I)
4305 likely = 1;
4306 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4307 goto do_false;
4308 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4309 {
4310 macro_build ((char *) NULL, &icnt, &offset_expr,
4311 likely ? "beql" : "beq",
4312 "s,t,p", sreg, 0);
4313 return;
4314 }
4315 set_at (&icnt, sreg, 1);
4316 macro_build ((char *) NULL, &icnt, &offset_expr,
4317 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4318 break;
4319
4320 case M_BLTL:
4321 likely = 1;
4322 case M_BLT:
4323 if (treg == 0)
4324 {
4325 macro_build ((char *) NULL, &icnt, &offset_expr,
4326 likely ? "bltzl" : "bltz", "s,p", sreg);
4327 return;
4328 }
4329 if (sreg == 0)
4330 {
4331 macro_build ((char *) NULL, &icnt, &offset_expr,
4332 likely ? "bgtzl" : "bgtz", "s,p", treg);
4333 return;
4334 }
4335 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4336 AT, sreg, treg);
4337 macro_build ((char *) NULL, &icnt, &offset_expr,
4338 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4339 break;
4340
4341 case M_BLTUL:
4342 likely = 1;
4343 case M_BLTU:
4344 if (treg == 0)
4345 goto do_false;
4346 if (sreg == 0)
4347 {
4348 macro_build ((char *) NULL, &icnt, &offset_expr,
4349 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4350 return;
4351 }
4352 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4353 "d,v,t", AT, sreg,
4354 treg);
4355 macro_build ((char *) NULL, &icnt, &offset_expr,
4356 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4357 break;
4358
4359 case M_DDIV_3:
4360 dbl = 1;
4361 case M_DIV_3:
4362 s = "mflo";
4363 goto do_div3;
4364 case M_DREM_3:
4365 dbl = 1;
4366 case M_REM_3:
4367 s = "mfhi";
4368 do_div3:
4369 if (treg == 0)
4370 {
4371 as_warn (_("Divide by zero."));
4372 if (mips_trap)
4373 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4374 "s,t", 0, 0);
4375 else
4376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4377 "c", 7);
4378 return;
4379 }
4380
4381 mips_emit_delays (true);
4382 ++mips_opts.noreorder;
4383 mips_any_noreorder = 1;
4384 if (mips_trap)
4385 {
4386 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4387 "s,t", treg, 0);
4388 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4389 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4390 }
4391 else
4392 {
4393 expr1.X_add_number = 8;
4394 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4395 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4396 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4397 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4398 "c", 7);
4399 }
4400 expr1.X_add_number = -1;
4401 macro_build ((char *) NULL, &icnt, &expr1,
4402 dbl ? "daddiu" : "addiu",
4403 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4404 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4405 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4406 if (dbl)
4407 {
4408 expr1.X_add_number = 1;
4409 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4410 (int) BFD_RELOC_LO16);
4411 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4412 "d,w,<", AT, AT, 31);
4413 }
4414 else
4415 {
4416 expr1.X_add_number = 0x80000000;
4417 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4418 (int) BFD_RELOC_HI16);
4419 }
4420 if (mips_trap)
4421 {
4422 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4423 "s,t", sreg, AT);
4424 /* We want to close the noreorder block as soon as possible, so
4425 that later insns are available for delay slot filling. */
4426 --mips_opts.noreorder;
4427 }
4428 else
4429 {
4430 expr1.X_add_number = 8;
4431 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4432 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4433 0);
4434
4435 /* We want to close the noreorder block as soon as possible, so
4436 that later insns are available for delay slot filling. */
4437 --mips_opts.noreorder;
4438
4439 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4440 "c", 6);
4441 }
4442 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4443 break;
4444
4445 case M_DIV_3I:
4446 s = "div";
4447 s2 = "mflo";
4448 goto do_divi;
4449 case M_DIVU_3I:
4450 s = "divu";
4451 s2 = "mflo";
4452 goto do_divi;
4453 case M_REM_3I:
4454 s = "div";
4455 s2 = "mfhi";
4456 goto do_divi;
4457 case M_REMU_3I:
4458 s = "divu";
4459 s2 = "mfhi";
4460 goto do_divi;
4461 case M_DDIV_3I:
4462 dbl = 1;
4463 s = "ddiv";
4464 s2 = "mflo";
4465 goto do_divi;
4466 case M_DDIVU_3I:
4467 dbl = 1;
4468 s = "ddivu";
4469 s2 = "mflo";
4470 goto do_divi;
4471 case M_DREM_3I:
4472 dbl = 1;
4473 s = "ddiv";
4474 s2 = "mfhi";
4475 goto do_divi;
4476 case M_DREMU_3I:
4477 dbl = 1;
4478 s = "ddivu";
4479 s2 = "mfhi";
4480 do_divi:
4481 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4482 {
4483 as_warn (_("Divide by zero."));
4484 if (mips_trap)
4485 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4486 "s,t", 0, 0);
4487 else
4488 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4489 "c", 7);
4490 return;
4491 }
4492 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4493 {
4494 if (strcmp (s2, "mflo") == 0)
4495 move_register (&icnt, dreg, sreg);
4496 else
4497 move_register (&icnt, dreg, 0);
4498 return;
4499 }
4500 if (imm_expr.X_op == O_constant
4501 && imm_expr.X_add_number == -1
4502 && s[strlen (s) - 1] != 'u')
4503 {
4504 if (strcmp (s2, "mflo") == 0)
4505 {
4506 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4507 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4508 }
4509 else
4510 move_register (&icnt, dreg, 0);
4511 return;
4512 }
4513
4514 load_register (&icnt, AT, &imm_expr, dbl);
4515 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4516 sreg, AT);
4517 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4518 break;
4519
4520 case M_DIVU_3:
4521 s = "divu";
4522 s2 = "mflo";
4523 goto do_divu3;
4524 case M_REMU_3:
4525 s = "divu";
4526 s2 = "mfhi";
4527 goto do_divu3;
4528 case M_DDIVU_3:
4529 s = "ddivu";
4530 s2 = "mflo";
4531 goto do_divu3;
4532 case M_DREMU_3:
4533 s = "ddivu";
4534 s2 = "mfhi";
4535 do_divu3:
4536 mips_emit_delays (true);
4537 ++mips_opts.noreorder;
4538 mips_any_noreorder = 1;
4539 if (mips_trap)
4540 {
4541 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4542 "s,t", treg, 0);
4543 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4544 sreg, treg);
4545 /* We want to close the noreorder block as soon as possible, so
4546 that later insns are available for delay slot filling. */
4547 --mips_opts.noreorder;
4548 }
4549 else
4550 {
4551 expr1.X_add_number = 8;
4552 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4553 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4554 sreg, treg);
4555
4556 /* We want to close the noreorder block as soon as possible, so
4557 that later insns are available for delay slot filling. */
4558 --mips_opts.noreorder;
4559 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4560 "c", 7);
4561 }
4562 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4563 return;
4564
4565 case M_DLA_AB:
4566 dbl = 1;
4567 case M_LA_AB:
4568 /* Load the address of a symbol into a register. If breg is not
4569 zero, we then add a base register to it. */
4570
4571 if (treg == breg)
4572 {
4573 tempreg = AT;
4574 used_at = 1;
4575 }
4576 else
4577 {
4578 tempreg = treg;
4579 used_at = 0;
4580 }
4581
4582 /* When generating embedded PIC code, we permit expressions of
4583 the form
4584 la $treg,foo-bar
4585 la $treg,foo-bar($breg)
4586 where bar is an address in the current section. These are used
4587 when getting the addresses of functions. We don't permit
4588 X_add_number to be non-zero, because if the symbol is
4589 external the relaxing code needs to know that any addend is
4590 purely the offset to X_op_symbol. */
4591 if (mips_pic == EMBEDDED_PIC
4592 && offset_expr.X_op == O_subtract
4593 && (symbol_constant_p (offset_expr.X_op_symbol)
4594 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4595 : (symbol_equated_p (offset_expr.X_op_symbol)
4596 && (S_GET_SEGMENT
4597 (symbol_get_value_expression (offset_expr.X_op_symbol)
4598 ->X_add_symbol)
4599 == now_seg)))
4600 && (offset_expr.X_add_number == 0
4601 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4602 {
4603 if (breg == 0)
4604 {
4605 tempreg = treg;
4606 used_at = 0;
4607 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4608 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4609 }
4610 else
4611 {
4612 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4613 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4614 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4615 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4616 "d,v,t", tempreg, tempreg, breg);
4617 }
4618 macro_build ((char *) NULL, &icnt, &offset_expr,
4619 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4620 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4621 if (! used_at)
4622 return;
4623 break;
4624 }
4625
4626 if (offset_expr.X_op != O_symbol
4627 && offset_expr.X_op != O_constant)
4628 {
4629 as_bad (_("expression too complex"));
4630 offset_expr.X_op = O_constant;
4631 }
4632
4633 if (offset_expr.X_op == O_constant)
4634 load_register (&icnt, tempreg, &offset_expr, dbl);
4635 else if (mips_pic == NO_PIC)
4636 {
4637 /* If this is a reference to a GP relative symbol, we want
4638 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4639 Otherwise we want
4640 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4641 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4642 If we have a constant, we need two instructions anyhow,
4643 so we may as well always use the latter form.
4644
4645 With 64bit address space and a usable $at we want
4646 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4647 lui $at,<sym> (BFD_RELOC_HI16_S)
4648 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4649 daddiu $at,<sym> (BFD_RELOC_LO16)
4650 dsll32 $tempreg,0
4651 dadd $tempreg,$tempreg,$at
4652
4653 If $at is already in use, we use an path which is suboptimal
4654 on superscalar processors.
4655 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4656 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4657 dsll $tempreg,16
4658 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4659 dsll $tempreg,16
4660 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4661 */
4662 p = NULL;
4663 if (HAVE_64BIT_ADDRESSES)
4664 {
4665 /* We don't do GP optimization for now because RELAX_ENCODE can't
4666 hold the data for such large chunks. */
4667
4668 if (used_at == 0)
4669 {
4670 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4671 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4672 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4673 AT, (int) BFD_RELOC_HI16_S);
4674 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4675 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4676 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4677 AT, AT, (int) BFD_RELOC_LO16);
4678 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4679 "d,w,<", tempreg, tempreg, 0);
4680 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4681 tempreg, tempreg, AT);
4682 used_at = 1;
4683 }
4684 else
4685 {
4686 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4687 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4688 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4689 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4690 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4691 tempreg, tempreg, 16);
4692 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4693 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4694 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4695 tempreg, tempreg, 16);
4696 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4697 tempreg, tempreg, (int) BFD_RELOC_LO16);
4698 }
4699 }
4700 else
4701 {
4702 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4703 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4704 {
4705 frag_grow (20);
4706 macro_build ((char *) NULL, &icnt, &offset_expr,
4707 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4708 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4709 p = frag_var (rs_machine_dependent, 8, 0,
4710 RELAX_ENCODE (4, 8, 0, 4, 0,
4711 mips_opts.warn_about_macros),
4712 offset_expr.X_add_symbol, 0, NULL);
4713 }
4714 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4715 if (p != NULL)
4716 p += 4;
4717 macro_build (p, &icnt, &offset_expr,
4718 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4719 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4720 }
4721 }
4722 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4723 {
4724 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4725
4726 /* If this is a reference to an external symbol, and there
4727 is no constant, we want
4728 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4729 or if tempreg is PIC_CALL_REG
4730 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4731 For a local symbol, we want
4732 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4733 nop
4734 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4735
4736 If we have a small constant, and this is a reference to
4737 an external symbol, we want
4738 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4739 nop
4740 addiu $tempreg,$tempreg,<constant>
4741 For a local symbol, we want the same instruction
4742 sequence, but we output a BFD_RELOC_LO16 reloc on the
4743 addiu instruction.
4744
4745 If we have a large constant, and this is a reference to
4746 an external symbol, we want
4747 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4748 lui $at,<hiconstant>
4749 addiu $at,$at,<loconstant>
4750 addu $tempreg,$tempreg,$at
4751 For a local symbol, we want the same instruction
4752 sequence, but we output a BFD_RELOC_LO16 reloc on the
4753 addiu instruction. */
4754 expr1.X_add_number = offset_expr.X_add_number;
4755 offset_expr.X_add_number = 0;
4756 frag_grow (32);
4757 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4758 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4759 macro_build ((char *) NULL, &icnt, &offset_expr, dbl ? "ld" : "lw",
4760 "t,o(b)", tempreg, lw_reloc_type, GP);
4761 if (expr1.X_add_number == 0)
4762 {
4763 int off;
4764
4765 if (breg == 0)
4766 off = 0;
4767 else
4768 {
4769 /* We're going to put in an addu instruction using
4770 tempreg, so we may as well insert the nop right
4771 now. */
4772 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4773 "nop", "");
4774 off = 4;
4775 }
4776 p = frag_var (rs_machine_dependent, 8 - off, 0,
4777 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4778 (breg == 0
4779 ? mips_opts.warn_about_macros
4780 : 0)),
4781 offset_expr.X_add_symbol, 0, NULL);
4782 if (breg == 0)
4783 {
4784 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4785 p += 4;
4786 }
4787 macro_build (p, &icnt, &expr1,
4788 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4789 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4790 /* FIXME: If breg == 0, and the next instruction uses
4791 $tempreg, then if this variant case is used an extra
4792 nop will be generated. */
4793 }
4794 else if (expr1.X_add_number >= -0x8000
4795 && expr1.X_add_number < 0x8000)
4796 {
4797 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4798 "nop", "");
4799 macro_build ((char *) NULL, &icnt, &expr1,
4800 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4801 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4802 frag_var (rs_machine_dependent, 0, 0,
4803 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4804 offset_expr.X_add_symbol, 0, NULL);
4805 }
4806 else
4807 {
4808 int off1;
4809
4810 /* If we are going to add in a base register, and the
4811 target register and the base register are the same,
4812 then we are using AT as a temporary register. Since
4813 we want to load the constant into AT, we add our
4814 current AT (from the global offset table) and the
4815 register into the register now, and pretend we were
4816 not using a base register. */
4817 if (breg != treg)
4818 off1 = 0;
4819 else
4820 {
4821 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4822 "nop", "");
4823 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4824 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4825 "d,v,t", treg, AT, breg);
4826 breg = 0;
4827 tempreg = treg;
4828 off1 = -8;
4829 }
4830
4831 /* Set mips_optimize around the lui instruction to avoid
4832 inserting an unnecessary nop after the lw. */
4833 hold_mips_optimize = mips_optimize;
4834 mips_optimize = 2;
4835 macro_build_lui (NULL, &icnt, &expr1, AT);
4836 mips_optimize = hold_mips_optimize;
4837
4838 macro_build ((char *) NULL, &icnt, &expr1,
4839 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4840 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4841 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4842 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4843 "d,v,t", tempreg, tempreg, AT);
4844 frag_var (rs_machine_dependent, 0, 0,
4845 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4846 offset_expr.X_add_symbol, 0, NULL);
4847 used_at = 1;
4848 }
4849 }
4850 else if (mips_pic == SVR4_PIC)
4851 {
4852 int gpdel;
4853 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4854 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4855
4856 /* This is the large GOT case. If this is a reference to an
4857 external symbol, and there is no constant, we want
4858 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4859 addu $tempreg,$tempreg,$gp
4860 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4861 or if tempreg is PIC_CALL_REG
4862 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4863 addu $tempreg,$tempreg,$gp
4864 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4865 For a local symbol, we want
4866 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4867 nop
4868 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4869
4870 If we have a small constant, and this is a reference to
4871 an external symbol, we want
4872 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4873 addu $tempreg,$tempreg,$gp
4874 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4875 nop
4876 addiu $tempreg,$tempreg,<constant>
4877 For a local symbol, we want
4878 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4879 nop
4880 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4881
4882 If we have a large constant, and this is a reference to
4883 an external symbol, we want
4884 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4885 addu $tempreg,$tempreg,$gp
4886 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4887 lui $at,<hiconstant>
4888 addiu $at,$at,<loconstant>
4889 addu $tempreg,$tempreg,$at
4890 For a local symbol, we want
4891 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4892 lui $at,<hiconstant>
4893 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4894 addu $tempreg,$tempreg,$at
4895 */
4896 expr1.X_add_number = offset_expr.X_add_number;
4897 offset_expr.X_add_number = 0;
4898 frag_grow (52);
4899 if (reg_needs_delay (GP))
4900 gpdel = 4;
4901 else
4902 gpdel = 0;
4903 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4904 {
4905 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4906 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4907 }
4908 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4909 tempreg, lui_reloc_type);
4910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4911 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4912 "d,v,t", tempreg, tempreg, GP);
4913 macro_build ((char *) NULL, &icnt, &offset_expr,
4914 dbl ? "ld" : "lw",
4915 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4916 if (expr1.X_add_number == 0)
4917 {
4918 int off;
4919
4920 if (breg == 0)
4921 off = 0;
4922 else
4923 {
4924 /* We're going to put in an addu instruction using
4925 tempreg, so we may as well insert the nop right
4926 now. */
4927 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4928 "nop", "");
4929 off = 4;
4930 }
4931
4932 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4933 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4934 8 + gpdel, 0,
4935 (breg == 0
4936 ? mips_opts.warn_about_macros
4937 : 0)),
4938 offset_expr.X_add_symbol, 0, NULL);
4939 }
4940 else if (expr1.X_add_number >= -0x8000
4941 && expr1.X_add_number < 0x8000)
4942 {
4943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4944 "nop", "");
4945 macro_build ((char *) NULL, &icnt, &expr1,
4946 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4947 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4948
4949 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4950 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4951 (breg == 0
4952 ? mips_opts.warn_about_macros
4953 : 0)),
4954 offset_expr.X_add_symbol, 0, NULL);
4955 }
4956 else
4957 {
4958 int adj, dreg;
4959
4960 /* If we are going to add in a base register, and the
4961 target register and the base register are the same,
4962 then we are using AT as a temporary register. Since
4963 we want to load the constant into AT, we add our
4964 current AT (from the global offset table) and the
4965 register into the register now, and pretend we were
4966 not using a base register. */
4967 if (breg != treg)
4968 {
4969 adj = 0;
4970 dreg = tempreg;
4971 }
4972 else
4973 {
4974 assert (tempreg == AT);
4975 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4976 "nop", "");
4977 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4978 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4979 "d,v,t", treg, AT, breg);
4980 dreg = treg;
4981 adj = 8;
4982 }
4983
4984 /* Set mips_optimize around the lui instruction to avoid
4985 inserting an unnecessary nop after the lw. */
4986 hold_mips_optimize = mips_optimize;
4987 mips_optimize = 2;
4988 macro_build_lui (NULL, &icnt, &expr1, AT);
4989 mips_optimize = hold_mips_optimize;
4990
4991 macro_build ((char *) NULL, &icnt, &expr1,
4992 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4993 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4995 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4996 "d,v,t", dreg, dreg, AT);
4997
4998 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4999 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5000 8 + gpdel, 0,
5001 (breg == 0
5002 ? mips_opts.warn_about_macros
5003 : 0)),
5004 offset_expr.X_add_symbol, 0, NULL);
5005
5006 used_at = 1;
5007 }
5008
5009 if (gpdel > 0)
5010 {
5011 /* This is needed because this instruction uses $gp, but
5012 the first instruction on the main stream does not. */
5013 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5014 p += 4;
5015 }
5016 macro_build (p, &icnt, &offset_expr,
5017 dbl ? "ld" : "lw",
5018 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5019 p += 4;
5020 if (expr1.X_add_number >= -0x8000
5021 && expr1.X_add_number < 0x8000)
5022 {
5023 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5024 p += 4;
5025 macro_build (p, &icnt, &expr1,
5026 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5027 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5028 /* FIXME: If add_number is 0, and there was no base
5029 register, the external symbol case ended with a load,
5030 so if the symbol turns out to not be external, and
5031 the next instruction uses tempreg, an unnecessary nop
5032 will be inserted. */
5033 }
5034 else
5035 {
5036 if (breg == treg)
5037 {
5038 /* We must add in the base register now, as in the
5039 external symbol case. */
5040 assert (tempreg == AT);
5041 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5042 p += 4;
5043 macro_build (p, &icnt, (expressionS *) NULL,
5044 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5045 "d,v,t", treg, AT, breg);
5046 p += 4;
5047 tempreg = treg;
5048 /* We set breg to 0 because we have arranged to add
5049 it in in both cases. */
5050 breg = 0;
5051 }
5052
5053 macro_build_lui (p, &icnt, &expr1, AT);
5054 p += 4;
5055 macro_build (p, &icnt, &expr1,
5056 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5057 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5058 p += 4;
5059 macro_build (p, &icnt, (expressionS *) NULL,
5060 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5061 "d,v,t", tempreg, tempreg, AT);
5062 p += 4;
5063 }
5064 }
5065 else if (mips_pic == EMBEDDED_PIC)
5066 {
5067 /* We use
5068 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5069 */
5070 macro_build ((char *) NULL, &icnt, &offset_expr,
5071 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5072 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
5073 }
5074 else
5075 abort ();
5076
5077 if (breg != 0)
5078 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5079 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5080 "d,v,t", treg, tempreg, breg);
5081
5082 if (! used_at)
5083 return;
5084
5085 break;
5086
5087 case M_J_A:
5088 /* The j instruction may not be used in PIC code, since it
5089 requires an absolute address. We convert it to a b
5090 instruction. */
5091 if (mips_pic == NO_PIC)
5092 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5093 else
5094 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5095 return;
5096
5097 /* The jal instructions must be handled as macros because when
5098 generating PIC code they expand to multi-instruction
5099 sequences. Normally they are simple instructions. */
5100 case M_JAL_1:
5101 dreg = RA;
5102 /* Fall through. */
5103 case M_JAL_2:
5104 if (mips_pic == NO_PIC
5105 || mips_pic == EMBEDDED_PIC)
5106 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5107 "d,s", dreg, sreg);
5108 else if (mips_pic == SVR4_PIC)
5109 {
5110 if (sreg != PIC_CALL_REG)
5111 as_warn (_("MIPS PIC call to register other than $25"));
5112
5113 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5114 "d,s", dreg, sreg);
5115 if (! HAVE_NEWABI)
5116 {
5117 if (mips_cprestore_offset < 0)
5118 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5119 else
5120 {
5121 if (! mips_frame_reg_valid)
5122 {
5123 as_warn (_("No .frame pseudo-op used in PIC code"));
5124 /* Quiet this warning. */
5125 mips_frame_reg_valid = 1;
5126 }
5127 if (! mips_cprestore_valid)
5128 {
5129 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5130 /* Quiet this warning. */
5131 mips_cprestore_valid = 1;
5132 }
5133 expr1.X_add_number = mips_cprestore_offset;
5134 macro_build ((char *) NULL, &icnt, &expr1,
5135 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5136 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5137 }
5138 }
5139 }
5140 else
5141 abort ();
5142
5143 return;
5144
5145 case M_JAL_A:
5146 if (mips_pic == NO_PIC)
5147 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5148 else if (mips_pic == SVR4_PIC)
5149 {
5150 /* If this is a reference to an external symbol, and we are
5151 using a small GOT, we want
5152 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5153 nop
5154 jalr $25
5155 nop
5156 lw $gp,cprestore($sp)
5157 The cprestore value is set using the .cprestore
5158 pseudo-op. If we are using a big GOT, we want
5159 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5160 addu $25,$25,$gp
5161 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5162 nop
5163 jalr $25
5164 nop
5165 lw $gp,cprestore($sp)
5166 If the symbol is not external, we want
5167 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5168 nop
5169 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5170 jalr $25
5171 nop
5172 lw $gp,cprestore($sp) */
5173 frag_grow (40);
5174 if (! mips_big_got)
5175 {
5176 macro_build ((char *) NULL, &icnt, &offset_expr,
5177 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5178 "t,o(b)", PIC_CALL_REG,
5179 (int) BFD_RELOC_MIPS_CALL16, GP);
5180 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5181 "nop", "");
5182 p = frag_var (rs_machine_dependent, 4, 0,
5183 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5184 offset_expr.X_add_symbol, 0, NULL);
5185 }
5186 else
5187 {
5188 int gpdel;
5189
5190 if (reg_needs_delay (GP))
5191 gpdel = 4;
5192 else
5193 gpdel = 0;
5194 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5195 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5196 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5197 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5198 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5199 macro_build ((char *) NULL, &icnt, &offset_expr,
5200 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5201 "t,o(b)", PIC_CALL_REG,
5202 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5203 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5204 "nop", "");
5205 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5206 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5207 0, 0),
5208 offset_expr.X_add_symbol, 0, NULL);
5209 if (gpdel > 0)
5210 {
5211 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5212 p += 4;
5213 }
5214 macro_build (p, &icnt, &offset_expr,
5215 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5216 "t,o(b)", PIC_CALL_REG,
5217 (int) BFD_RELOC_MIPS_GOT16, GP);
5218 p += 4;
5219 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5220 p += 4;
5221 }
5222 macro_build (p, &icnt, &offset_expr,
5223 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5224 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5225 (int) BFD_RELOC_LO16);
5226 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5227 "jalr", "s", PIC_CALL_REG);
5228 if (! HAVE_NEWABI)
5229 {
5230 if (mips_cprestore_offset < 0)
5231 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5232 else
5233 {
5234 if (! mips_frame_reg_valid)
5235 {
5236 as_warn (_("No .frame pseudo-op used in PIC code"));
5237 /* Quiet this warning. */
5238 mips_frame_reg_valid = 1;
5239 }
5240 if (! mips_cprestore_valid)
5241 {
5242 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5243 /* Quiet this warning. */
5244 mips_cprestore_valid = 1;
5245 }
5246 if (mips_opts.noreorder)
5247 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5248 "nop", "");
5249 expr1.X_add_number = mips_cprestore_offset;
5250 macro_build ((char *) NULL, &icnt, &expr1,
5251 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5252 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5253 }
5254 }
5255 }
5256 else if (mips_pic == EMBEDDED_PIC)
5257 {
5258 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5259 /* The linker may expand the call to a longer sequence which
5260 uses $at, so we must break rather than return. */
5261 break;
5262 }
5263 else
5264 abort ();
5265
5266 return;
5267
5268 case M_LB_AB:
5269 s = "lb";
5270 goto ld;
5271 case M_LBU_AB:
5272 s = "lbu";
5273 goto ld;
5274 case M_LH_AB:
5275 s = "lh";
5276 goto ld;
5277 case M_LHU_AB:
5278 s = "lhu";
5279 goto ld;
5280 case M_LW_AB:
5281 s = "lw";
5282 goto ld;
5283 case M_LWC0_AB:
5284 s = "lwc0";
5285 /* Itbl support may require additional care here. */
5286 coproc = 1;
5287 goto ld;
5288 case M_LWC1_AB:
5289 s = "lwc1";
5290 /* Itbl support may require additional care here. */
5291 coproc = 1;
5292 goto ld;
5293 case M_LWC2_AB:
5294 s = "lwc2";
5295 /* Itbl support may require additional care here. */
5296 coproc = 1;
5297 goto ld;
5298 case M_LWC3_AB:
5299 s = "lwc3";
5300 /* Itbl support may require additional care here. */
5301 coproc = 1;
5302 goto ld;
5303 case M_LWL_AB:
5304 s = "lwl";
5305 lr = 1;
5306 goto ld;
5307 case M_LWR_AB:
5308 s = "lwr";
5309 lr = 1;
5310 goto ld;
5311 case M_LDC1_AB:
5312 if (mips_arch == CPU_R4650)
5313 {
5314 as_bad (_("opcode not supported on this processor"));
5315 return;
5316 }
5317 s = "ldc1";
5318 /* Itbl support may require additional care here. */
5319 coproc = 1;
5320 goto ld;
5321 case M_LDC2_AB:
5322 s = "ldc2";
5323 /* Itbl support may require additional care here. */
5324 coproc = 1;
5325 goto ld;
5326 case M_LDC3_AB:
5327 s = "ldc3";
5328 /* Itbl support may require additional care here. */
5329 coproc = 1;
5330 goto ld;
5331 case M_LDL_AB:
5332 s = "ldl";
5333 lr = 1;
5334 goto ld;
5335 case M_LDR_AB:
5336 s = "ldr";
5337 lr = 1;
5338 goto ld;
5339 case M_LL_AB:
5340 s = "ll";
5341 goto ld;
5342 case M_LLD_AB:
5343 s = "lld";
5344 goto ld;
5345 case M_LWU_AB:
5346 s = "lwu";
5347 ld:
5348 if (breg == treg || coproc || lr)
5349 {
5350 tempreg = AT;
5351 used_at = 1;
5352 }
5353 else
5354 {
5355 tempreg = treg;
5356 used_at = 0;
5357 }
5358 goto ld_st;
5359 case M_SB_AB:
5360 s = "sb";
5361 goto st;
5362 case M_SH_AB:
5363 s = "sh";
5364 goto st;
5365 case M_SW_AB:
5366 s = "sw";
5367 goto st;
5368 case M_SWC0_AB:
5369 s = "swc0";
5370 /* Itbl support may require additional care here. */
5371 coproc = 1;
5372 goto st;
5373 case M_SWC1_AB:
5374 s = "swc1";
5375 /* Itbl support may require additional care here. */
5376 coproc = 1;
5377 goto st;
5378 case M_SWC2_AB:
5379 s = "swc2";
5380 /* Itbl support may require additional care here. */
5381 coproc = 1;
5382 goto st;
5383 case M_SWC3_AB:
5384 s = "swc3";
5385 /* Itbl support may require additional care here. */
5386 coproc = 1;
5387 goto st;
5388 case M_SWL_AB:
5389 s = "swl";
5390 goto st;
5391 case M_SWR_AB:
5392 s = "swr";
5393 goto st;
5394 case M_SC_AB:
5395 s = "sc";
5396 goto st;
5397 case M_SCD_AB:
5398 s = "scd";
5399 goto st;
5400 case M_SDC1_AB:
5401 if (mips_arch == CPU_R4650)
5402 {
5403 as_bad (_("opcode not supported on this processor"));
5404 return;
5405 }
5406 s = "sdc1";
5407 coproc = 1;
5408 /* Itbl support may require additional care here. */
5409 goto st;
5410 case M_SDC2_AB:
5411 s = "sdc2";
5412 /* Itbl support may require additional care here. */
5413 coproc = 1;
5414 goto st;
5415 case M_SDC3_AB:
5416 s = "sdc3";
5417 /* Itbl support may require additional care here. */
5418 coproc = 1;
5419 goto st;
5420 case M_SDL_AB:
5421 s = "sdl";
5422 goto st;
5423 case M_SDR_AB:
5424 s = "sdr";
5425 st:
5426 tempreg = AT;
5427 used_at = 1;
5428 ld_st:
5429 /* Itbl support may require additional care here. */
5430 if (mask == M_LWC1_AB
5431 || mask == M_SWC1_AB
5432 || mask == M_LDC1_AB
5433 || mask == M_SDC1_AB
5434 || mask == M_L_DAB
5435 || mask == M_S_DAB)
5436 fmt = "T,o(b)";
5437 else if (coproc)
5438 fmt = "E,o(b)";
5439 else
5440 fmt = "t,o(b)";
5441
5442 /* For embedded PIC, we allow loads where the offset is calculated
5443 by subtracting a symbol in the current segment from an unknown
5444 symbol, relative to a base register, e.g.:
5445 <op> $treg, <sym>-<localsym>($breg)
5446 This is used by the compiler for switch statements. */
5447 if (mips_pic == EMBEDDED_PIC
5448 && offset_expr.X_op == O_subtract
5449 && (symbol_constant_p (offset_expr.X_op_symbol)
5450 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5451 : (symbol_equated_p (offset_expr.X_op_symbol)
5452 && (S_GET_SEGMENT
5453 (symbol_get_value_expression (offset_expr.X_op_symbol)
5454 ->X_add_symbol)
5455 == now_seg)))
5456 && breg != 0
5457 && (offset_expr.X_add_number == 0
5458 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5459 {
5460 /* For this case, we output the instructions:
5461 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5462 addiu $tempreg,$tempreg,$breg
5463 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5464 If the relocation would fit entirely in 16 bits, it would be
5465 nice to emit:
5466 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5467 instead, but that seems quite difficult. */
5468 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5469 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5470 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5471 ((bfd_arch_bits_per_address (stdoutput) == 32
5472 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5473 ? "addu" : "daddu"),
5474 "d,v,t", tempreg, tempreg, breg);
5475 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5476 (int) BFD_RELOC_PCREL_LO16, tempreg);
5477 if (! used_at)
5478 return;
5479 break;
5480 }
5481
5482 if (offset_expr.X_op != O_constant
5483 && offset_expr.X_op != O_symbol)
5484 {
5485 as_bad (_("expression too complex"));
5486 offset_expr.X_op = O_constant;
5487 }
5488
5489 /* A constant expression in PIC code can be handled just as it
5490 is in non PIC code. */
5491 if (mips_pic == NO_PIC
5492 || offset_expr.X_op == O_constant)
5493 {
5494 /* If this is a reference to a GP relative symbol, and there
5495 is no base register, we want
5496 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5497 Otherwise, if there is no base register, we want
5498 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5499 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5500 If we have a constant, we need two instructions anyhow,
5501 so we always use the latter form.
5502
5503 If we have a base register, and this is a reference to a
5504 GP relative symbol, we want
5505 addu $tempreg,$breg,$gp
5506 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5507 Otherwise we want
5508 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5509 addu $tempreg,$tempreg,$breg
5510 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5511 With a constant we always use the latter case.
5512
5513 With 64bit address space and no base register and $at usable,
5514 we want
5515 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5516 lui $at,<sym> (BFD_RELOC_HI16_S)
5517 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5518 dsll32 $tempreg,0
5519 daddu $tempreg,$at
5520 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5521 If we have a base register, we want
5522 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5523 lui $at,<sym> (BFD_RELOC_HI16_S)
5524 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5525 daddu $at,$breg
5526 dsll32 $tempreg,0
5527 daddu $tempreg,$at
5528 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5529
5530 Without $at we can't generate the optimal path for superscalar
5531 processors here since this would require two temporary registers.
5532 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5533 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5534 dsll $tempreg,16
5535 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5536 dsll $tempreg,16
5537 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5538 If we have a base register, we want
5539 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5540 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5541 dsll $tempreg,16
5542 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5543 dsll $tempreg,16
5544 daddu $tempreg,$tempreg,$breg
5545 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5546 */
5547 if (HAVE_64BIT_ADDRESSES)
5548 {
5549 p = NULL;
5550
5551 /* We don't do GP optimization for now because RELAX_ENCODE can't
5552 hold the data for such large chunks. */
5553
5554 if (used_at == 0)
5555 {
5556 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5557 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5558 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5559 AT, (int) BFD_RELOC_HI16_S);
5560 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5561 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5562 if (breg != 0)
5563 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5564 "d,v,t", AT, AT, breg);
5565 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5566 "d,w,<", tempreg, tempreg, 0);
5567 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5568 "d,v,t", tempreg, tempreg, AT);
5569 macro_build (p, &icnt, &offset_expr, s,
5570 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5571 used_at = 1;
5572 }
5573 else
5574 {
5575 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5576 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5577 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5578 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5579 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5580 "d,w,<", tempreg, tempreg, 16);
5581 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5582 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5583 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5584 "d,w,<", tempreg, tempreg, 16);
5585 if (breg != 0)
5586 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5587 "d,v,t", tempreg, tempreg, breg);
5588 macro_build (p, &icnt, &offset_expr, s,
5589 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5590 }
5591
5592 return;
5593 }
5594
5595 if (breg == 0)
5596 {
5597 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5598 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5599 p = NULL;
5600 else
5601 {
5602 frag_grow (20);
5603 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5604 treg, (int) BFD_RELOC_GPREL16, GP);
5605 p = frag_var (rs_machine_dependent, 8, 0,
5606 RELAX_ENCODE (4, 8, 0, 4, 0,
5607 (mips_opts.warn_about_macros
5608 || (used_at
5609 && mips_opts.noat))),
5610 offset_expr.X_add_symbol, 0, NULL);
5611 used_at = 0;
5612 }
5613 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5614 if (p != NULL)
5615 p += 4;
5616 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5617 (int) BFD_RELOC_LO16, tempreg);
5618 }
5619 else
5620 {
5621 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5622 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5623 p = NULL;
5624 else
5625 {
5626 frag_grow (28);
5627 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5628 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5629 "d,v,t", tempreg, breg, GP);
5630 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5631 treg, (int) BFD_RELOC_GPREL16, tempreg);
5632 p = frag_var (rs_machine_dependent, 12, 0,
5633 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5634 offset_expr.X_add_symbol, 0, NULL);
5635 }
5636 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5637 if (p != NULL)
5638 p += 4;
5639 macro_build (p, &icnt, (expressionS *) NULL,
5640 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5641 "d,v,t", tempreg, tempreg, breg);
5642 if (p != NULL)
5643 p += 4;
5644 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5645 (int) BFD_RELOC_LO16, tempreg);
5646 }
5647 }
5648 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5649 {
5650 /* If this is a reference to an external symbol, we want
5651 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5652 nop
5653 <op> $treg,0($tempreg)
5654 Otherwise we want
5655 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5656 nop
5657 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5658 <op> $treg,0($tempreg)
5659 If there is a base register, we add it to $tempreg before
5660 the <op>. If there is a constant, we stick it in the
5661 <op> instruction. We don't handle constants larger than
5662 16 bits, because we have no way to load the upper 16 bits
5663 (actually, we could handle them for the subset of cases
5664 in which we are not using $at). */
5665 assert (offset_expr.X_op == O_symbol);
5666 expr1.X_add_number = offset_expr.X_add_number;
5667 offset_expr.X_add_number = 0;
5668 if (expr1.X_add_number < -0x8000
5669 || expr1.X_add_number >= 0x8000)
5670 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5671 frag_grow (20);
5672 macro_build ((char *) NULL, &icnt, &offset_expr,
5673 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5674 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5676 p = frag_var (rs_machine_dependent, 4, 0,
5677 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5678 offset_expr.X_add_symbol, 0, NULL);
5679 macro_build (p, &icnt, &offset_expr,
5680 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5681 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5682 if (breg != 0)
5683 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5684 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5685 "d,v,t", tempreg, tempreg, breg);
5686 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5687 (int) BFD_RELOC_LO16, tempreg);
5688 }
5689 else if (mips_pic == SVR4_PIC)
5690 {
5691 int gpdel;
5692
5693 /* If this is a reference to an external symbol, we want
5694 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5695 addu $tempreg,$tempreg,$gp
5696 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5697 <op> $treg,0($tempreg)
5698 Otherwise we want
5699 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5700 nop
5701 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5702 <op> $treg,0($tempreg)
5703 If there is a base register, we add it to $tempreg before
5704 the <op>. If there is a constant, we stick it in the
5705 <op> instruction. We don't handle constants larger than
5706 16 bits, because we have no way to load the upper 16 bits
5707 (actually, we could handle them for the subset of cases
5708 in which we are not using $at). */
5709 assert (offset_expr.X_op == O_symbol);
5710 expr1.X_add_number = offset_expr.X_add_number;
5711 offset_expr.X_add_number = 0;
5712 if (expr1.X_add_number < -0x8000
5713 || expr1.X_add_number >= 0x8000)
5714 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5715 if (reg_needs_delay (GP))
5716 gpdel = 4;
5717 else
5718 gpdel = 0;
5719 frag_grow (36);
5720 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5721 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5722 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5723 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5724 "d,v,t", tempreg, tempreg, GP);
5725 macro_build ((char *) NULL, &icnt, &offset_expr,
5726 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5727 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5728 tempreg);
5729 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5730 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5731 offset_expr.X_add_symbol, 0, NULL);
5732 if (gpdel > 0)
5733 {
5734 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5735 p += 4;
5736 }
5737 macro_build (p, &icnt, &offset_expr,
5738 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5739 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5740 p += 4;
5741 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5742 p += 4;
5743 macro_build (p, &icnt, &offset_expr,
5744 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5745 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5746 if (breg != 0)
5747 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5748 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5749 "d,v,t", tempreg, tempreg, breg);
5750 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5751 (int) BFD_RELOC_LO16, tempreg);
5752 }
5753 else if (mips_pic == EMBEDDED_PIC)
5754 {
5755 /* If there is no base register, we want
5756 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5757 If there is a base register, we want
5758 addu $tempreg,$breg,$gp
5759 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5760 */
5761 assert (offset_expr.X_op == O_symbol);
5762 if (breg == 0)
5763 {
5764 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5765 treg, (int) BFD_RELOC_GPREL16, GP);
5766 used_at = 0;
5767 }
5768 else
5769 {
5770 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5771 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5772 "d,v,t", tempreg, breg, GP);
5773 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5774 treg, (int) BFD_RELOC_GPREL16, tempreg);
5775 }
5776 }
5777 else
5778 abort ();
5779
5780 if (! used_at)
5781 return;
5782
5783 break;
5784
5785 case M_LI:
5786 case M_LI_S:
5787 load_register (&icnt, treg, &imm_expr, 0);
5788 return;
5789
5790 case M_DLI:
5791 load_register (&icnt, treg, &imm_expr, 1);
5792 return;
5793
5794 case M_LI_SS:
5795 if (imm_expr.X_op == O_constant)
5796 {
5797 load_register (&icnt, AT, &imm_expr, 0);
5798 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5799 "mtc1", "t,G", AT, treg);
5800 break;
5801 }
5802 else
5803 {
5804 assert (offset_expr.X_op == O_symbol
5805 && strcmp (segment_name (S_GET_SEGMENT
5806 (offset_expr.X_add_symbol)),
5807 ".lit4") == 0
5808 && offset_expr.X_add_number == 0);
5809 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5810 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5811 return;
5812 }
5813
5814 case M_LI_D:
5815 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5816 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5817 order 32 bits of the value and the low order 32 bits are either
5818 zero or in OFFSET_EXPR. */
5819 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5820 {
5821 if (HAVE_64BIT_GPRS)
5822 load_register (&icnt, treg, &imm_expr, 1);
5823 else
5824 {
5825 int hreg, lreg;
5826
5827 if (target_big_endian)
5828 {
5829 hreg = treg;
5830 lreg = treg + 1;
5831 }
5832 else
5833 {
5834 hreg = treg + 1;
5835 lreg = treg;
5836 }
5837
5838 if (hreg <= 31)
5839 load_register (&icnt, hreg, &imm_expr, 0);
5840 if (lreg <= 31)
5841 {
5842 if (offset_expr.X_op == O_absent)
5843 move_register (&icnt, lreg, 0);
5844 else
5845 {
5846 assert (offset_expr.X_op == O_constant);
5847 load_register (&icnt, lreg, &offset_expr, 0);
5848 }
5849 }
5850 }
5851 return;
5852 }
5853
5854 /* We know that sym is in the .rdata section. First we get the
5855 upper 16 bits of the address. */
5856 if (mips_pic == NO_PIC)
5857 {
5858 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5859 }
5860 else if (mips_pic == SVR4_PIC)
5861 {
5862 macro_build ((char *) NULL, &icnt, &offset_expr,
5863 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5864 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5865 }
5866 else if (mips_pic == EMBEDDED_PIC)
5867 {
5868 /* For embedded PIC we pick up the entire address off $gp in
5869 a single instruction. */
5870 macro_build ((char *) NULL, &icnt, &offset_expr,
5871 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5872 "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5873 offset_expr.X_op = O_constant;
5874 offset_expr.X_add_number = 0;
5875 }
5876 else
5877 abort ();
5878
5879 /* Now we load the register(s). */
5880 if (HAVE_64BIT_GPRS)
5881 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5882 treg, (int) BFD_RELOC_LO16, AT);
5883 else
5884 {
5885 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5886 treg, (int) BFD_RELOC_LO16, AT);
5887 if (treg != 31)
5888 {
5889 /* FIXME: How in the world do we deal with the possible
5890 overflow here? */
5891 offset_expr.X_add_number += 4;
5892 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5893 treg + 1, (int) BFD_RELOC_LO16, AT);
5894 }
5895 }
5896
5897 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5898 does not become a variant frag. */
5899 frag_wane (frag_now);
5900 frag_new (0);
5901
5902 break;
5903
5904 case M_LI_DD:
5905 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5906 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5907 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5908 the value and the low order 32 bits are either zero or in
5909 OFFSET_EXPR. */
5910 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5911 {
5912 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5913 if (HAVE_64BIT_FPRS)
5914 {
5915 assert (HAVE_64BIT_GPRS);
5916 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5917 "dmtc1", "t,S", AT, treg);
5918 }
5919 else
5920 {
5921 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5922 "mtc1", "t,G", AT, treg + 1);
5923 if (offset_expr.X_op == O_absent)
5924 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5925 "mtc1", "t,G", 0, treg);
5926 else
5927 {
5928 assert (offset_expr.X_op == O_constant);
5929 load_register (&icnt, AT, &offset_expr, 0);
5930 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5931 "mtc1", "t,G", AT, treg);
5932 }
5933 }
5934 break;
5935 }
5936
5937 assert (offset_expr.X_op == O_symbol
5938 && offset_expr.X_add_number == 0);
5939 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5940 if (strcmp (s, ".lit8") == 0)
5941 {
5942 if (mips_opts.isa != ISA_MIPS1)
5943 {
5944 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5945 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5946 return;
5947 }
5948 breg = GP;
5949 r = BFD_RELOC_MIPS_LITERAL;
5950 goto dob;
5951 }
5952 else
5953 {
5954 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5955 if (mips_pic == SVR4_PIC)
5956 macro_build ((char *) NULL, &icnt, &offset_expr,
5957 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5958 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5959 else
5960 {
5961 /* FIXME: This won't work for a 64 bit address. */
5962 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5963 }
5964
5965 if (mips_opts.isa != ISA_MIPS1)
5966 {
5967 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5968 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5969
5970 /* To avoid confusion in tc_gen_reloc, we must ensure
5971 that this does not become a variant frag. */
5972 frag_wane (frag_now);
5973 frag_new (0);
5974
5975 break;
5976 }
5977 breg = AT;
5978 r = BFD_RELOC_LO16;
5979 goto dob;
5980 }
5981
5982 case M_L_DOB:
5983 if (mips_arch == CPU_R4650)
5984 {
5985 as_bad (_("opcode not supported on this processor"));
5986 return;
5987 }
5988 /* Even on a big endian machine $fn comes before $fn+1. We have
5989 to adjust when loading from memory. */
5990 r = BFD_RELOC_LO16;
5991 dob:
5992 assert (mips_opts.isa == ISA_MIPS1);
5993 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5994 target_big_endian ? treg + 1 : treg,
5995 (int) r, breg);
5996 /* FIXME: A possible overflow which I don't know how to deal
5997 with. */
5998 offset_expr.X_add_number += 4;
5999 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6000 target_big_endian ? treg : treg + 1,
6001 (int) r, breg);
6002
6003 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6004 does not become a variant frag. */
6005 frag_wane (frag_now);
6006 frag_new (0);
6007
6008 if (breg != AT)
6009 return;
6010 break;
6011
6012 case M_L_DAB:
6013 /*
6014 * The MIPS assembler seems to check for X_add_number not
6015 * being double aligned and generating:
6016 * lui at,%hi(foo+1)
6017 * addu at,at,v1
6018 * addiu at,at,%lo(foo+1)
6019 * lwc1 f2,0(at)
6020 * lwc1 f3,4(at)
6021 * But, the resulting address is the same after relocation so why
6022 * generate the extra instruction?
6023 */
6024 if (mips_arch == CPU_R4650)
6025 {
6026 as_bad (_("opcode not supported on this processor"));
6027 return;
6028 }
6029 /* Itbl support may require additional care here. */
6030 coproc = 1;
6031 if (mips_opts.isa != ISA_MIPS1)
6032 {
6033 s = "ldc1";
6034 goto ld;
6035 }
6036
6037 s = "lwc1";
6038 fmt = "T,o(b)";
6039 goto ldd_std;
6040
6041 case M_S_DAB:
6042 if (mips_arch == CPU_R4650)
6043 {
6044 as_bad (_("opcode not supported on this processor"));
6045 return;
6046 }
6047
6048 if (mips_opts.isa != ISA_MIPS1)
6049 {
6050 s = "sdc1";
6051 goto st;
6052 }
6053
6054 s = "swc1";
6055 fmt = "T,o(b)";
6056 /* Itbl support may require additional care here. */
6057 coproc = 1;
6058 goto ldd_std;
6059
6060 case M_LD_AB:
6061 if (HAVE_64BIT_GPRS)
6062 {
6063 s = "ld";
6064 goto ld;
6065 }
6066
6067 s = "lw";
6068 fmt = "t,o(b)";
6069 goto ldd_std;
6070
6071 case M_SD_AB:
6072 if (HAVE_64BIT_GPRS)
6073 {
6074 s = "sd";
6075 goto st;
6076 }
6077
6078 s = "sw";
6079 fmt = "t,o(b)";
6080
6081 ldd_std:
6082 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6083 loads for the case of doing a pair of loads to simulate an 'ld'.
6084 This is not currently done by the compiler, and assembly coders
6085 writing embedded-pic code can cope. */
6086
6087 if (offset_expr.X_op != O_symbol
6088 && offset_expr.X_op != O_constant)
6089 {
6090 as_bad (_("expression too complex"));
6091 offset_expr.X_op = O_constant;
6092 }
6093
6094 /* Even on a big endian machine $fn comes before $fn+1. We have
6095 to adjust when loading from memory. We set coproc if we must
6096 load $fn+1 first. */
6097 /* Itbl support may require additional care here. */
6098 if (! target_big_endian)
6099 coproc = 0;
6100
6101 if (mips_pic == NO_PIC
6102 || offset_expr.X_op == O_constant)
6103 {
6104 /* If this is a reference to a GP relative symbol, we want
6105 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6106 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6107 If we have a base register, we use this
6108 addu $at,$breg,$gp
6109 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6110 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6111 If this is not a GP relative symbol, we want
6112 lui $at,<sym> (BFD_RELOC_HI16_S)
6113 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6114 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6115 If there is a base register, we add it to $at after the
6116 lui instruction. If there is a constant, we always use
6117 the last case. */
6118 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6119 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6120 {
6121 p = NULL;
6122 used_at = 1;
6123 }
6124 else
6125 {
6126 int off;
6127
6128 if (breg == 0)
6129 {
6130 frag_grow (28);
6131 tempreg = GP;
6132 off = 0;
6133 used_at = 0;
6134 }
6135 else
6136 {
6137 frag_grow (36);
6138 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6139 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6140 "d,v,t", AT, breg, GP);
6141 tempreg = AT;
6142 off = 4;
6143 used_at = 1;
6144 }
6145
6146 /* Itbl support may require additional care here. */
6147 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6148 coproc ? treg + 1 : treg,
6149 (int) BFD_RELOC_GPREL16, tempreg);
6150 offset_expr.X_add_number += 4;
6151
6152 /* Set mips_optimize to 2 to avoid inserting an
6153 undesired nop. */
6154 hold_mips_optimize = mips_optimize;
6155 mips_optimize = 2;
6156 /* Itbl support may require additional care here. */
6157 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6158 coproc ? treg : treg + 1,
6159 (int) BFD_RELOC_GPREL16, tempreg);
6160 mips_optimize = hold_mips_optimize;
6161
6162 p = frag_var (rs_machine_dependent, 12 + off, 0,
6163 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6164 used_at && mips_opts.noat),
6165 offset_expr.X_add_symbol, 0, NULL);
6166
6167 /* We just generated two relocs. When tc_gen_reloc
6168 handles this case, it will skip the first reloc and
6169 handle the second. The second reloc already has an
6170 extra addend of 4, which we added above. We must
6171 subtract it out, and then subtract another 4 to make
6172 the first reloc come out right. The second reloc
6173 will come out right because we are going to add 4 to
6174 offset_expr when we build its instruction below.
6175
6176 If we have a symbol, then we don't want to include
6177 the offset, because it will wind up being included
6178 when we generate the reloc. */
6179
6180 if (offset_expr.X_op == O_constant)
6181 offset_expr.X_add_number -= 8;
6182 else
6183 {
6184 offset_expr.X_add_number = -4;
6185 offset_expr.X_op = O_constant;
6186 }
6187 }
6188 macro_build_lui (p, &icnt, &offset_expr, AT);
6189 if (p != NULL)
6190 p += 4;
6191 if (breg != 0)
6192 {
6193 macro_build (p, &icnt, (expressionS *) NULL,
6194 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6195 "d,v,t", AT, breg, AT);
6196 if (p != NULL)
6197 p += 4;
6198 }
6199 /* Itbl support may require additional care here. */
6200 macro_build (p, &icnt, &offset_expr, s, fmt,
6201 coproc ? treg + 1 : treg,
6202 (int) BFD_RELOC_LO16, AT);
6203 if (p != NULL)
6204 p += 4;
6205 /* FIXME: How do we handle overflow here? */
6206 offset_expr.X_add_number += 4;
6207 /* Itbl support may require additional care here. */
6208 macro_build (p, &icnt, &offset_expr, s, fmt,
6209 coproc ? treg : treg + 1,
6210 (int) BFD_RELOC_LO16, AT);
6211 }
6212 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6213 {
6214 int off;
6215
6216 /* If this is a reference to an external symbol, we want
6217 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6218 nop
6219 <op> $treg,0($at)
6220 <op> $treg+1,4($at)
6221 Otherwise we want
6222 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6223 nop
6224 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6225 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6226 If there is a base register we add it to $at before the
6227 lwc1 instructions. If there is a constant we include it
6228 in the lwc1 instructions. */
6229 used_at = 1;
6230 expr1.X_add_number = offset_expr.X_add_number;
6231 offset_expr.X_add_number = 0;
6232 if (expr1.X_add_number < -0x8000
6233 || expr1.X_add_number >= 0x8000 - 4)
6234 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6235 if (breg == 0)
6236 off = 0;
6237 else
6238 off = 4;
6239 frag_grow (24 + off);
6240 macro_build ((char *) NULL, &icnt, &offset_expr,
6241 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6242 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6243 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6244 if (breg != 0)
6245 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6246 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6247 "d,v,t", AT, breg, AT);
6248 /* Itbl support may require additional care here. */
6249 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6250 coproc ? treg + 1 : treg,
6251 (int) BFD_RELOC_LO16, AT);
6252 expr1.X_add_number += 4;
6253
6254 /* Set mips_optimize to 2 to avoid inserting an undesired
6255 nop. */
6256 hold_mips_optimize = mips_optimize;
6257 mips_optimize = 2;
6258 /* Itbl support may require additional care here. */
6259 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6260 coproc ? treg : treg + 1,
6261 (int) BFD_RELOC_LO16, AT);
6262 mips_optimize = hold_mips_optimize;
6263
6264 (void) frag_var (rs_machine_dependent, 0, 0,
6265 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6266 offset_expr.X_add_symbol, 0, NULL);
6267 }
6268 else if (mips_pic == SVR4_PIC)
6269 {
6270 int gpdel, off;
6271
6272 /* If this is a reference to an external symbol, we want
6273 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6274 addu $at,$at,$gp
6275 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6276 nop
6277 <op> $treg,0($at)
6278 <op> $treg+1,4($at)
6279 Otherwise we want
6280 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6281 nop
6282 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6283 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6284 If there is a base register we add it to $at before the
6285 lwc1 instructions. If there is a constant we include it
6286 in the lwc1 instructions. */
6287 used_at = 1;
6288 expr1.X_add_number = offset_expr.X_add_number;
6289 offset_expr.X_add_number = 0;
6290 if (expr1.X_add_number < -0x8000
6291 || expr1.X_add_number >= 0x8000 - 4)
6292 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6293 if (reg_needs_delay (GP))
6294 gpdel = 4;
6295 else
6296 gpdel = 0;
6297 if (breg == 0)
6298 off = 0;
6299 else
6300 off = 4;
6301 frag_grow (56);
6302 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6303 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6304 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6305 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6306 "d,v,t", AT, AT, GP);
6307 macro_build ((char *) NULL, &icnt, &offset_expr,
6308 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6309 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6310 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6311 if (breg != 0)
6312 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6313 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6314 "d,v,t", AT, breg, AT);
6315 /* Itbl support may require additional care here. */
6316 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6317 coproc ? treg + 1 : treg,
6318 (int) BFD_RELOC_LO16, AT);
6319 expr1.X_add_number += 4;
6320
6321 /* Set mips_optimize to 2 to avoid inserting an undesired
6322 nop. */
6323 hold_mips_optimize = mips_optimize;
6324 mips_optimize = 2;
6325 /* Itbl support may require additional care here. */
6326 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6327 coproc ? treg : treg + 1,
6328 (int) BFD_RELOC_LO16, AT);
6329 mips_optimize = hold_mips_optimize;
6330 expr1.X_add_number -= 4;
6331
6332 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6333 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6334 8 + gpdel + off, 1, 0),
6335 offset_expr.X_add_symbol, 0, NULL);
6336 if (gpdel > 0)
6337 {
6338 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6339 p += 4;
6340 }
6341 macro_build (p, &icnt, &offset_expr,
6342 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6343 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6344 p += 4;
6345 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6346 p += 4;
6347 if (breg != 0)
6348 {
6349 macro_build (p, &icnt, (expressionS *) NULL,
6350 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6351 "d,v,t", AT, breg, AT);
6352 p += 4;
6353 }
6354 /* Itbl support may require additional care here. */
6355 macro_build (p, &icnt, &expr1, s, fmt,
6356 coproc ? treg + 1 : treg,
6357 (int) BFD_RELOC_LO16, AT);
6358 p += 4;
6359 expr1.X_add_number += 4;
6360
6361 /* Set mips_optimize to 2 to avoid inserting an undesired
6362 nop. */
6363 hold_mips_optimize = mips_optimize;
6364 mips_optimize = 2;
6365 /* Itbl support may require additional care here. */
6366 macro_build (p, &icnt, &expr1, s, fmt,
6367 coproc ? treg : treg + 1,
6368 (int) BFD_RELOC_LO16, AT);
6369 mips_optimize = hold_mips_optimize;
6370 }
6371 else if (mips_pic == EMBEDDED_PIC)
6372 {
6373 /* If there is no base register, we use
6374 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6375 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6376 If we have a base register, we use
6377 addu $at,$breg,$gp
6378 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6379 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6380 */
6381 if (breg == 0)
6382 {
6383 tempreg = GP;
6384 used_at = 0;
6385 }
6386 else
6387 {
6388 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6389 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6390 "d,v,t", AT, breg, GP);
6391 tempreg = AT;
6392 used_at = 1;
6393 }
6394
6395 /* Itbl support may require additional care here. */
6396 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6397 coproc ? treg + 1 : treg,
6398 (int) BFD_RELOC_GPREL16, tempreg);
6399 offset_expr.X_add_number += 4;
6400 /* Itbl support may require additional care here. */
6401 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6402 coproc ? treg : treg + 1,
6403 (int) BFD_RELOC_GPREL16, tempreg);
6404 }
6405 else
6406 abort ();
6407
6408 if (! used_at)
6409 return;
6410
6411 break;
6412
6413 case M_LD_OB:
6414 s = "lw";
6415 goto sd_ob;
6416 case M_SD_OB:
6417 s = "sw";
6418 sd_ob:
6419 assert (HAVE_32BIT_ADDRESSES);
6420 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6421 (int) BFD_RELOC_LO16, breg);
6422 offset_expr.X_add_number += 4;
6423 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6424 (int) BFD_RELOC_LO16, breg);
6425 return;
6426
6427 /* New code added to support COPZ instructions.
6428 This code builds table entries out of the macros in mip_opcodes.
6429 R4000 uses interlocks to handle coproc delays.
6430 Other chips (like the R3000) require nops to be inserted for delays.
6431
6432 FIXME: Currently, we require that the user handle delays.
6433 In order to fill delay slots for non-interlocked chips,
6434 we must have a way to specify delays based on the coprocessor.
6435 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6436 What are the side-effects of the cop instruction?
6437 What cache support might we have and what are its effects?
6438 Both coprocessor & memory require delays. how long???
6439 What registers are read/set/modified?
6440
6441 If an itbl is provided to interpret cop instructions,
6442 this knowledge can be encoded in the itbl spec. */
6443
6444 case M_COP0:
6445 s = "c0";
6446 goto copz;
6447 case M_COP1:
6448 s = "c1";
6449 goto copz;
6450 case M_COP2:
6451 s = "c2";
6452 goto copz;
6453 case M_COP3:
6454 s = "c3";
6455 copz:
6456 /* For now we just do C (same as Cz). The parameter will be
6457 stored in insn_opcode by mips_ip. */
6458 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6459 ip->insn_opcode);
6460 return;
6461
6462 case M_MOVE:
6463 move_register (&icnt, dreg, sreg);
6464 return;
6465
6466 #ifdef LOSING_COMPILER
6467 default:
6468 /* Try and see if this is a new itbl instruction.
6469 This code builds table entries out of the macros in mip_opcodes.
6470 FIXME: For now we just assemble the expression and pass it's
6471 value along as a 32-bit immediate.
6472 We may want to have the assembler assemble this value,
6473 so that we gain the assembler's knowledge of delay slots,
6474 symbols, etc.
6475 Would it be more efficient to use mask (id) here? */
6476 if (itbl_have_entries
6477 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6478 {
6479 s = ip->insn_mo->name;
6480 s2 = "cop3";
6481 coproc = ITBL_DECODE_PNUM (immed_expr);;
6482 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6483 return;
6484 }
6485 macro2 (ip);
6486 return;
6487 }
6488 if (mips_opts.noat)
6489 as_warn (_("Macro used $at after \".set noat\""));
6490 }
6491
6492 static void
6493 macro2 (ip)
6494 struct mips_cl_insn *ip;
6495 {
6496 register int treg, sreg, dreg, breg;
6497 int tempreg;
6498 int mask;
6499 int icnt = 0;
6500 int used_at;
6501 expressionS expr1;
6502 const char *s;
6503 const char *s2;
6504 const char *fmt;
6505 int likely = 0;
6506 int dbl = 0;
6507 int coproc = 0;
6508 int lr = 0;
6509 int imm = 0;
6510 int off;
6511 offsetT maxnum;
6512 bfd_reloc_code_real_type r;
6513 char *p;
6514
6515 treg = (ip->insn_opcode >> 16) & 0x1f;
6516 dreg = (ip->insn_opcode >> 11) & 0x1f;
6517 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6518 mask = ip->insn_mo->mask;
6519
6520 expr1.X_op = O_constant;
6521 expr1.X_op_symbol = NULL;
6522 expr1.X_add_symbol = NULL;
6523 expr1.X_add_number = 1;
6524
6525 switch (mask)
6526 {
6527 #endif /* LOSING_COMPILER */
6528
6529 case M_DMUL:
6530 dbl = 1;
6531 case M_MUL:
6532 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6533 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6534 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6535 dreg);
6536 return;
6537
6538 case M_DMUL_I:
6539 dbl = 1;
6540 case M_MUL_I:
6541 /* The MIPS assembler some times generates shifts and adds. I'm
6542 not trying to be that fancy. GCC should do this for us
6543 anyway. */
6544 load_register (&icnt, AT, &imm_expr, dbl);
6545 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6546 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6548 dreg);
6549 break;
6550
6551 case M_DMULO_I:
6552 dbl = 1;
6553 case M_MULO_I:
6554 imm = 1;
6555 goto do_mulo;
6556
6557 case M_DMULO:
6558 dbl = 1;
6559 case M_MULO:
6560 do_mulo:
6561 mips_emit_delays (true);
6562 ++mips_opts.noreorder;
6563 mips_any_noreorder = 1;
6564 if (imm)
6565 load_register (&icnt, AT, &imm_expr, dbl);
6566 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6567 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6569 dreg);
6570 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6571 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6572 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6573 AT);
6574 if (mips_trap)
6575 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6576 dreg, AT);
6577 else
6578 {
6579 expr1.X_add_number = 8;
6580 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6581 AT);
6582 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6583 0);
6584 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6585 "c", 6);
6586 }
6587 --mips_opts.noreorder;
6588 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6589 break;
6590
6591 case M_DMULOU_I:
6592 dbl = 1;
6593 case M_MULOU_I:
6594 imm = 1;
6595 goto do_mulou;
6596
6597 case M_DMULOU:
6598 dbl = 1;
6599 case M_MULOU:
6600 do_mulou:
6601 mips_emit_delays (true);
6602 ++mips_opts.noreorder;
6603 mips_any_noreorder = 1;
6604 if (imm)
6605 load_register (&icnt, AT, &imm_expr, dbl);
6606 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6607 dbl ? "dmultu" : "multu",
6608 "s,t", sreg, imm ? AT : treg);
6609 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6610 AT);
6611 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6612 dreg);
6613 if (mips_trap)
6614 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6615 AT, 0);
6616 else
6617 {
6618 expr1.X_add_number = 8;
6619 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6620 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6621 0);
6622 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6623 "c", 6);
6624 }
6625 --mips_opts.noreorder;
6626 break;
6627
6628 case M_ROL:
6629 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6630 "d,v,t", AT, 0, treg);
6631 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6632 "d,t,s", AT, sreg, AT);
6633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6634 "d,t,s", dreg, sreg, treg);
6635 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6636 "d,v,t", dreg, dreg, AT);
6637 break;
6638
6639 case M_ROL_I:
6640 if (imm_expr.X_op != O_constant)
6641 as_bad (_("rotate count too large"));
6642 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6643 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6644 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6645 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6646 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6647 dreg, dreg, AT);
6648 break;
6649
6650 case M_ROR:
6651 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6652 "d,v,t", AT, 0, treg);
6653 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6654 "d,t,s", AT, sreg, AT);
6655 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6656 "d,t,s", dreg, sreg, treg);
6657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6658 "d,v,t", dreg, dreg, AT);
6659 break;
6660
6661 case M_ROR_I:
6662 if (imm_expr.X_op != O_constant)
6663 as_bad (_("rotate count too large"));
6664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6665 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6667 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6668 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6669 dreg, dreg, AT);
6670 break;
6671
6672 case M_S_DOB:
6673 if (mips_arch == CPU_R4650)
6674 {
6675 as_bad (_("opcode not supported on this processor"));
6676 return;
6677 }
6678 assert (mips_opts.isa == ISA_MIPS1);
6679 /* Even on a big endian machine $fn comes before $fn+1. We have
6680 to adjust when storing to memory. */
6681 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6682 target_big_endian ? treg + 1 : treg,
6683 (int) BFD_RELOC_LO16, breg);
6684 offset_expr.X_add_number += 4;
6685 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6686 target_big_endian ? treg : treg + 1,
6687 (int) BFD_RELOC_LO16, breg);
6688 return;
6689
6690 case M_SEQ:
6691 if (sreg == 0)
6692 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6693 treg, (int) BFD_RELOC_LO16);
6694 else if (treg == 0)
6695 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6696 sreg, (int) BFD_RELOC_LO16);
6697 else
6698 {
6699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6700 "d,v,t", dreg, sreg, treg);
6701 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6702 dreg, (int) BFD_RELOC_LO16);
6703 }
6704 return;
6705
6706 case M_SEQ_I:
6707 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6708 {
6709 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6710 sreg, (int) BFD_RELOC_LO16);
6711 return;
6712 }
6713 if (sreg == 0)
6714 {
6715 as_warn (_("Instruction %s: result is always false"),
6716 ip->insn_mo->name);
6717 move_register (&icnt, dreg, 0);
6718 return;
6719 }
6720 if (imm_expr.X_op == O_constant
6721 && imm_expr.X_add_number >= 0
6722 && imm_expr.X_add_number < 0x10000)
6723 {
6724 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6725 sreg, (int) BFD_RELOC_LO16);
6726 used_at = 0;
6727 }
6728 else if (imm_expr.X_op == O_constant
6729 && imm_expr.X_add_number > -0x8000
6730 && imm_expr.X_add_number < 0)
6731 {
6732 imm_expr.X_add_number = -imm_expr.X_add_number;
6733 macro_build ((char *) NULL, &icnt, &imm_expr,
6734 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6735 "t,r,j", dreg, sreg,
6736 (int) BFD_RELOC_LO16);
6737 used_at = 0;
6738 }
6739 else
6740 {
6741 load_register (&icnt, AT, &imm_expr, 0);
6742 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6743 "d,v,t", dreg, sreg, AT);
6744 used_at = 1;
6745 }
6746 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6747 (int) BFD_RELOC_LO16);
6748 if (used_at)
6749 break;
6750 return;
6751
6752 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6753 s = "slt";
6754 goto sge;
6755 case M_SGEU:
6756 s = "sltu";
6757 sge:
6758 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6759 dreg, sreg, treg);
6760 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6761 (int) BFD_RELOC_LO16);
6762 return;
6763
6764 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6765 case M_SGEU_I:
6766 if (imm_expr.X_op == O_constant
6767 && imm_expr.X_add_number >= -0x8000
6768 && imm_expr.X_add_number < 0x8000)
6769 {
6770 macro_build ((char *) NULL, &icnt, &imm_expr,
6771 mask == M_SGE_I ? "slti" : "sltiu",
6772 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6773 used_at = 0;
6774 }
6775 else
6776 {
6777 load_register (&icnt, AT, &imm_expr, 0);
6778 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6779 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6780 AT);
6781 used_at = 1;
6782 }
6783 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6784 (int) BFD_RELOC_LO16);
6785 if (used_at)
6786 break;
6787 return;
6788
6789 case M_SGT: /* sreg > treg <==> treg < sreg */
6790 s = "slt";
6791 goto sgt;
6792 case M_SGTU:
6793 s = "sltu";
6794 sgt:
6795 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6796 dreg, treg, sreg);
6797 return;
6798
6799 case M_SGT_I: /* sreg > I <==> I < sreg */
6800 s = "slt";
6801 goto sgti;
6802 case M_SGTU_I:
6803 s = "sltu";
6804 sgti:
6805 load_register (&icnt, AT, &imm_expr, 0);
6806 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6807 dreg, AT, sreg);
6808 break;
6809
6810 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6811 s = "slt";
6812 goto sle;
6813 case M_SLEU:
6814 s = "sltu";
6815 sle:
6816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6817 dreg, treg, sreg);
6818 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6819 (int) BFD_RELOC_LO16);
6820 return;
6821
6822 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6823 s = "slt";
6824 goto slei;
6825 case M_SLEU_I:
6826 s = "sltu";
6827 slei:
6828 load_register (&icnt, AT, &imm_expr, 0);
6829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6830 dreg, AT, sreg);
6831 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6832 (int) BFD_RELOC_LO16);
6833 break;
6834
6835 case M_SLT_I:
6836 if (imm_expr.X_op == O_constant
6837 && imm_expr.X_add_number >= -0x8000
6838 && imm_expr.X_add_number < 0x8000)
6839 {
6840 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6841 dreg, sreg, (int) BFD_RELOC_LO16);
6842 return;
6843 }
6844 load_register (&icnt, AT, &imm_expr, 0);
6845 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6846 dreg, sreg, AT);
6847 break;
6848
6849 case M_SLTU_I:
6850 if (imm_expr.X_op == O_constant
6851 && imm_expr.X_add_number >= -0x8000
6852 && imm_expr.X_add_number < 0x8000)
6853 {
6854 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6855 dreg, sreg, (int) BFD_RELOC_LO16);
6856 return;
6857 }
6858 load_register (&icnt, AT, &imm_expr, 0);
6859 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6860 "d,v,t", dreg, sreg, AT);
6861 break;
6862
6863 case M_SNE:
6864 if (sreg == 0)
6865 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6866 "d,v,t", dreg, 0, treg);
6867 else if (treg == 0)
6868 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6869 "d,v,t", dreg, 0, sreg);
6870 else
6871 {
6872 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6873 "d,v,t", dreg, sreg, treg);
6874 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6875 "d,v,t", dreg, 0, dreg);
6876 }
6877 return;
6878
6879 case M_SNE_I:
6880 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6881 {
6882 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6883 "d,v,t", dreg, 0, sreg);
6884 return;
6885 }
6886 if (sreg == 0)
6887 {
6888 as_warn (_("Instruction %s: result is always true"),
6889 ip->insn_mo->name);
6890 macro_build ((char *) NULL, &icnt, &expr1,
6891 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6892 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6893 return;
6894 }
6895 if (imm_expr.X_op == O_constant
6896 && imm_expr.X_add_number >= 0
6897 && imm_expr.X_add_number < 0x10000)
6898 {
6899 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6900 dreg, sreg, (int) BFD_RELOC_LO16);
6901 used_at = 0;
6902 }
6903 else if (imm_expr.X_op == O_constant
6904 && imm_expr.X_add_number > -0x8000
6905 && imm_expr.X_add_number < 0)
6906 {
6907 imm_expr.X_add_number = -imm_expr.X_add_number;
6908 macro_build ((char *) NULL, &icnt, &imm_expr,
6909 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6910 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6911 used_at = 0;
6912 }
6913 else
6914 {
6915 load_register (&icnt, AT, &imm_expr, 0);
6916 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6917 "d,v,t", dreg, sreg, AT);
6918 used_at = 1;
6919 }
6920 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6921 "d,v,t", dreg, 0, dreg);
6922 if (used_at)
6923 break;
6924 return;
6925
6926 case M_DSUB_I:
6927 dbl = 1;
6928 case M_SUB_I:
6929 if (imm_expr.X_op == O_constant
6930 && imm_expr.X_add_number > -0x8000
6931 && imm_expr.X_add_number <= 0x8000)
6932 {
6933 imm_expr.X_add_number = -imm_expr.X_add_number;
6934 macro_build ((char *) NULL, &icnt, &imm_expr,
6935 dbl ? "daddi" : "addi",
6936 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6937 return;
6938 }
6939 load_register (&icnt, AT, &imm_expr, dbl);
6940 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6941 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6942 break;
6943
6944 case M_DSUBU_I:
6945 dbl = 1;
6946 case M_SUBU_I:
6947 if (imm_expr.X_op == O_constant
6948 && imm_expr.X_add_number > -0x8000
6949 && imm_expr.X_add_number <= 0x8000)
6950 {
6951 imm_expr.X_add_number = -imm_expr.X_add_number;
6952 macro_build ((char *) NULL, &icnt, &imm_expr,
6953 dbl ? "daddiu" : "addiu",
6954 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6955 return;
6956 }
6957 load_register (&icnt, AT, &imm_expr, dbl);
6958 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6959 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6960 break;
6961
6962 case M_TEQ_I:
6963 s = "teq";
6964 goto trap;
6965 case M_TGE_I:
6966 s = "tge";
6967 goto trap;
6968 case M_TGEU_I:
6969 s = "tgeu";
6970 goto trap;
6971 case M_TLT_I:
6972 s = "tlt";
6973 goto trap;
6974 case M_TLTU_I:
6975 s = "tltu";
6976 goto trap;
6977 case M_TNE_I:
6978 s = "tne";
6979 trap:
6980 load_register (&icnt, AT, &imm_expr, 0);
6981 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
6982 AT);
6983 break;
6984
6985 case M_TRUNCWS:
6986 case M_TRUNCWD:
6987 assert (mips_opts.isa == ISA_MIPS1);
6988 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6989 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6990
6991 /*
6992 * Is the double cfc1 instruction a bug in the mips assembler;
6993 * or is there a reason for it?
6994 */
6995 mips_emit_delays (true);
6996 ++mips_opts.noreorder;
6997 mips_any_noreorder = 1;
6998 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
6999 treg, 31);
7000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7001 treg, 31);
7002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7003 expr1.X_add_number = 3;
7004 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7005 (int) BFD_RELOC_LO16);
7006 expr1.X_add_number = 2;
7007 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7008 (int) BFD_RELOC_LO16);
7009 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7010 AT, 31);
7011 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7012 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7013 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7015 treg, 31);
7016 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7017 --mips_opts.noreorder;
7018 break;
7019
7020 case M_ULH:
7021 s = "lb";
7022 goto ulh;
7023 case M_ULHU:
7024 s = "lbu";
7025 ulh:
7026 if (offset_expr.X_add_number >= 0x7fff)
7027 as_bad (_("operand overflow"));
7028 /* avoid load delay */
7029 if (! target_big_endian)
7030 offset_expr.X_add_number += 1;
7031 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7032 (int) BFD_RELOC_LO16, breg);
7033 if (! target_big_endian)
7034 offset_expr.X_add_number -= 1;
7035 else
7036 offset_expr.X_add_number += 1;
7037 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7038 (int) BFD_RELOC_LO16, breg);
7039 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7040 treg, treg, 8);
7041 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7042 treg, treg, AT);
7043 break;
7044
7045 case M_ULD:
7046 s = "ldl";
7047 s2 = "ldr";
7048 off = 7;
7049 goto ulw;
7050 case M_ULW:
7051 s = "lwl";
7052 s2 = "lwr";
7053 off = 3;
7054 ulw:
7055 if (offset_expr.X_add_number >= 0x8000 - off)
7056 as_bad (_("operand overflow"));
7057 if (! target_big_endian)
7058 offset_expr.X_add_number += off;
7059 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7060 (int) BFD_RELOC_LO16, breg);
7061 if (! target_big_endian)
7062 offset_expr.X_add_number -= off;
7063 else
7064 offset_expr.X_add_number += off;
7065 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7066 (int) BFD_RELOC_LO16, breg);
7067 return;
7068
7069 case M_ULD_A:
7070 s = "ldl";
7071 s2 = "ldr";
7072 off = 7;
7073 goto ulwa;
7074 case M_ULW_A:
7075 s = "lwl";
7076 s2 = "lwr";
7077 off = 3;
7078 ulwa:
7079 used_at = 1;
7080 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7081 if (breg != 0)
7082 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7083 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7084 "d,v,t", AT, AT, breg);
7085 if (! target_big_endian)
7086 expr1.X_add_number = off;
7087 else
7088 expr1.X_add_number = 0;
7089 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7090 (int) BFD_RELOC_LO16, AT);
7091 if (! target_big_endian)
7092 expr1.X_add_number = 0;
7093 else
7094 expr1.X_add_number = off;
7095 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7096 (int) BFD_RELOC_LO16, AT);
7097 break;
7098
7099 case M_ULH_A:
7100 case M_ULHU_A:
7101 used_at = 1;
7102 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7103 if (breg != 0)
7104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7105 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7106 "d,v,t", AT, AT, breg);
7107 if (target_big_endian)
7108 expr1.X_add_number = 0;
7109 macro_build ((char *) NULL, &icnt, &expr1,
7110 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7111 (int) BFD_RELOC_LO16, AT);
7112 if (target_big_endian)
7113 expr1.X_add_number = 1;
7114 else
7115 expr1.X_add_number = 0;
7116 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7117 (int) BFD_RELOC_LO16, AT);
7118 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7119 treg, treg, 8);
7120 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7121 treg, treg, AT);
7122 break;
7123
7124 case M_USH:
7125 if (offset_expr.X_add_number >= 0x7fff)
7126 as_bad (_("operand overflow"));
7127 if (target_big_endian)
7128 offset_expr.X_add_number += 1;
7129 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7130 (int) BFD_RELOC_LO16, breg);
7131 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7132 AT, treg, 8);
7133 if (target_big_endian)
7134 offset_expr.X_add_number -= 1;
7135 else
7136 offset_expr.X_add_number += 1;
7137 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7138 (int) BFD_RELOC_LO16, breg);
7139 break;
7140
7141 case M_USD:
7142 s = "sdl";
7143 s2 = "sdr";
7144 off = 7;
7145 goto usw;
7146 case M_USW:
7147 s = "swl";
7148 s2 = "swr";
7149 off = 3;
7150 usw:
7151 if (offset_expr.X_add_number >= 0x8000 - off)
7152 as_bad (_("operand overflow"));
7153 if (! target_big_endian)
7154 offset_expr.X_add_number += off;
7155 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7156 (int) BFD_RELOC_LO16, breg);
7157 if (! target_big_endian)
7158 offset_expr.X_add_number -= off;
7159 else
7160 offset_expr.X_add_number += off;
7161 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7162 (int) BFD_RELOC_LO16, breg);
7163 return;
7164
7165 case M_USD_A:
7166 s = "sdl";
7167 s2 = "sdr";
7168 off = 7;
7169 goto uswa;
7170 case M_USW_A:
7171 s = "swl";
7172 s2 = "swr";
7173 off = 3;
7174 uswa:
7175 used_at = 1;
7176 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7177 if (breg != 0)
7178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7179 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7180 "d,v,t", AT, AT, breg);
7181 if (! target_big_endian)
7182 expr1.X_add_number = off;
7183 else
7184 expr1.X_add_number = 0;
7185 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7186 (int) BFD_RELOC_LO16, AT);
7187 if (! target_big_endian)
7188 expr1.X_add_number = 0;
7189 else
7190 expr1.X_add_number = off;
7191 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7192 (int) BFD_RELOC_LO16, AT);
7193 break;
7194
7195 case M_USH_A:
7196 used_at = 1;
7197 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7198 if (breg != 0)
7199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7200 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7201 "d,v,t", AT, AT, breg);
7202 if (! target_big_endian)
7203 expr1.X_add_number = 0;
7204 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7205 (int) BFD_RELOC_LO16, AT);
7206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7207 treg, treg, 8);
7208 if (! target_big_endian)
7209 expr1.X_add_number = 1;
7210 else
7211 expr1.X_add_number = 0;
7212 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7213 (int) BFD_RELOC_LO16, AT);
7214 if (! target_big_endian)
7215 expr1.X_add_number = 0;
7216 else
7217 expr1.X_add_number = 1;
7218 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7219 (int) BFD_RELOC_LO16, AT);
7220 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7221 treg, treg, 8);
7222 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7223 treg, treg, AT);
7224 break;
7225
7226 default:
7227 /* FIXME: Check if this is one of the itbl macros, since they
7228 are added dynamically. */
7229 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7230 break;
7231 }
7232 if (mips_opts.noat)
7233 as_warn (_("Macro used $at after \".set noat\""));
7234 }
7235
7236 /* Implement macros in mips16 mode. */
7237
7238 static void
7239 mips16_macro (ip)
7240 struct mips_cl_insn *ip;
7241 {
7242 int mask;
7243 int xreg, yreg, zreg, tmp;
7244 int icnt;
7245 expressionS expr1;
7246 int dbl;
7247 const char *s, *s2, *s3;
7248
7249 mask = ip->insn_mo->mask;
7250
7251 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7252 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7253 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7254
7255 icnt = 0;
7256
7257 expr1.X_op = O_constant;
7258 expr1.X_op_symbol = NULL;
7259 expr1.X_add_symbol = NULL;
7260 expr1.X_add_number = 1;
7261
7262 dbl = 0;
7263
7264 switch (mask)
7265 {
7266 default:
7267 internalError ();
7268
7269 case M_DDIV_3:
7270 dbl = 1;
7271 case M_DIV_3:
7272 s = "mflo";
7273 goto do_div3;
7274 case M_DREM_3:
7275 dbl = 1;
7276 case M_REM_3:
7277 s = "mfhi";
7278 do_div3:
7279 mips_emit_delays (true);
7280 ++mips_opts.noreorder;
7281 mips_any_noreorder = 1;
7282 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7283 dbl ? "ddiv" : "div",
7284 "0,x,y", xreg, yreg);
7285 expr1.X_add_number = 2;
7286 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7288 7);
7289
7290 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7291 since that causes an overflow. We should do that as well,
7292 but I don't see how to do the comparisons without a temporary
7293 register. */
7294 --mips_opts.noreorder;
7295 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7296 break;
7297
7298 case M_DIVU_3:
7299 s = "divu";
7300 s2 = "mflo";
7301 goto do_divu3;
7302 case M_REMU_3:
7303 s = "divu";
7304 s2 = "mfhi";
7305 goto do_divu3;
7306 case M_DDIVU_3:
7307 s = "ddivu";
7308 s2 = "mflo";
7309 goto do_divu3;
7310 case M_DREMU_3:
7311 s = "ddivu";
7312 s2 = "mfhi";
7313 do_divu3:
7314 mips_emit_delays (true);
7315 ++mips_opts.noreorder;
7316 mips_any_noreorder = 1;
7317 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7318 xreg, yreg);
7319 expr1.X_add_number = 2;
7320 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7321 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7322 "6", 7);
7323 --mips_opts.noreorder;
7324 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7325 break;
7326
7327 case M_DMUL:
7328 dbl = 1;
7329 case M_MUL:
7330 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7331 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7332 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7333 zreg);
7334 return;
7335
7336 case M_DSUBU_I:
7337 dbl = 1;
7338 goto do_subu;
7339 case M_SUBU_I:
7340 do_subu:
7341 if (imm_expr.X_op != O_constant)
7342 as_bad (_("Unsupported large constant"));
7343 imm_expr.X_add_number = -imm_expr.X_add_number;
7344 macro_build ((char *) NULL, &icnt, &imm_expr,
7345 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7346 break;
7347
7348 case M_SUBU_I_2:
7349 if (imm_expr.X_op != O_constant)
7350 as_bad (_("Unsupported large constant"));
7351 imm_expr.X_add_number = -imm_expr.X_add_number;
7352 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7353 "x,k", xreg);
7354 break;
7355
7356 case M_DSUBU_I_2:
7357 if (imm_expr.X_op != O_constant)
7358 as_bad (_("Unsupported large constant"));
7359 imm_expr.X_add_number = -imm_expr.X_add_number;
7360 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7361 "y,j", yreg);
7362 break;
7363
7364 case M_BEQ:
7365 s = "cmp";
7366 s2 = "bteqz";
7367 goto do_branch;
7368 case M_BNE:
7369 s = "cmp";
7370 s2 = "btnez";
7371 goto do_branch;
7372 case M_BLT:
7373 s = "slt";
7374 s2 = "btnez";
7375 goto do_branch;
7376 case M_BLTU:
7377 s = "sltu";
7378 s2 = "btnez";
7379 goto do_branch;
7380 case M_BLE:
7381 s = "slt";
7382 s2 = "bteqz";
7383 goto do_reverse_branch;
7384 case M_BLEU:
7385 s = "sltu";
7386 s2 = "bteqz";
7387 goto do_reverse_branch;
7388 case M_BGE:
7389 s = "slt";
7390 s2 = "bteqz";
7391 goto do_branch;
7392 case M_BGEU:
7393 s = "sltu";
7394 s2 = "bteqz";
7395 goto do_branch;
7396 case M_BGT:
7397 s = "slt";
7398 s2 = "btnez";
7399 goto do_reverse_branch;
7400 case M_BGTU:
7401 s = "sltu";
7402 s2 = "btnez";
7403
7404 do_reverse_branch:
7405 tmp = xreg;
7406 xreg = yreg;
7407 yreg = tmp;
7408
7409 do_branch:
7410 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7411 xreg, yreg);
7412 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7413 break;
7414
7415 case M_BEQ_I:
7416 s = "cmpi";
7417 s2 = "bteqz";
7418 s3 = "x,U";
7419 goto do_branch_i;
7420 case M_BNE_I:
7421 s = "cmpi";
7422 s2 = "btnez";
7423 s3 = "x,U";
7424 goto do_branch_i;
7425 case M_BLT_I:
7426 s = "slti";
7427 s2 = "btnez";
7428 s3 = "x,8";
7429 goto do_branch_i;
7430 case M_BLTU_I:
7431 s = "sltiu";
7432 s2 = "btnez";
7433 s3 = "x,8";
7434 goto do_branch_i;
7435 case M_BLE_I:
7436 s = "slti";
7437 s2 = "btnez";
7438 s3 = "x,8";
7439 goto do_addone_branch_i;
7440 case M_BLEU_I:
7441 s = "sltiu";
7442 s2 = "btnez";
7443 s3 = "x,8";
7444 goto do_addone_branch_i;
7445 case M_BGE_I:
7446 s = "slti";
7447 s2 = "bteqz";
7448 s3 = "x,8";
7449 goto do_branch_i;
7450 case M_BGEU_I:
7451 s = "sltiu";
7452 s2 = "bteqz";
7453 s3 = "x,8";
7454 goto do_branch_i;
7455 case M_BGT_I:
7456 s = "slti";
7457 s2 = "bteqz";
7458 s3 = "x,8";
7459 goto do_addone_branch_i;
7460 case M_BGTU_I:
7461 s = "sltiu";
7462 s2 = "bteqz";
7463 s3 = "x,8";
7464
7465 do_addone_branch_i:
7466 if (imm_expr.X_op != O_constant)
7467 as_bad (_("Unsupported large constant"));
7468 ++imm_expr.X_add_number;
7469
7470 do_branch_i:
7471 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7472 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7473 break;
7474
7475 case M_ABS:
7476 expr1.X_add_number = 0;
7477 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7478 if (xreg != yreg)
7479 move_register (&icnt, xreg, yreg);
7480 expr1.X_add_number = 2;
7481 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7482 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7483 "neg", "x,w", xreg, xreg);
7484 }
7485 }
7486
7487 /* For consistency checking, verify that all bits are specified either
7488 by the match/mask part of the instruction definition, or by the
7489 operand list. */
7490 static int
7491 validate_mips_insn (opc)
7492 const struct mips_opcode *opc;
7493 {
7494 const char *p = opc->args;
7495 char c;
7496 unsigned long used_bits = opc->mask;
7497
7498 if ((used_bits & opc->match) != opc->match)
7499 {
7500 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7501 opc->name, opc->args);
7502 return 0;
7503 }
7504 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7505 while (*p)
7506 switch (c = *p++)
7507 {
7508 case ',': break;
7509 case '(': break;
7510 case ')': break;
7511 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7512 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7513 case 'A': break;
7514 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7515 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7516 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7517 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7518 case 'F': break;
7519 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7520 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7521 case 'I': break;
7522 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7523 case 'L': break;
7524 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7525 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7526 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7527 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7528 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7529 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7530 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7531 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7532 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7533 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7534 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7535 case 'f': break;
7536 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7537 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7538 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7539 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7540 case 'l': break;
7541 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7542 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7543 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7544 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7545 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7546 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7547 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7548 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7549 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7550 case 'x': break;
7551 case 'z': break;
7552 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7553 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7554 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7555 default:
7556 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7557 c, opc->name, opc->args);
7558 return 0;
7559 }
7560 #undef USE_BITS
7561 if (used_bits != 0xffffffff)
7562 {
7563 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7564 ~used_bits & 0xffffffff, opc->name, opc->args);
7565 return 0;
7566 }
7567 return 1;
7568 }
7569
7570 /* This routine assembles an instruction into its binary format. As a
7571 side effect, it sets one of the global variables imm_reloc or
7572 offset_reloc to the type of relocation to do if one of the operands
7573 is an address expression. */
7574
7575 static void
7576 mips_ip (str, ip)
7577 char *str;
7578 struct mips_cl_insn *ip;
7579 {
7580 char *s;
7581 const char *args;
7582 char c = 0;
7583 struct mips_opcode *insn;
7584 char *argsStart;
7585 unsigned int regno;
7586 unsigned int lastregno = 0;
7587 char *s_reset;
7588 char save_c = 0;
7589 int full_opcode_match = 1;
7590
7591 insn_error = NULL;
7592
7593 /* If the instruction contains a '.', we first try to match an instruction
7594 including the '.'. Then we try again without the '.'. */
7595 insn = NULL;
7596 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7597 continue;
7598
7599 /* If we stopped on whitespace, then replace the whitespace with null for
7600 the call to hash_find. Save the character we replaced just in case we
7601 have to re-parse the instruction. */
7602 if (ISSPACE (*s))
7603 {
7604 save_c = *s;
7605 *s++ = '\0';
7606 }
7607
7608 insn = (struct mips_opcode *) hash_find (op_hash, str);
7609
7610 /* If we didn't find the instruction in the opcode table, try again, but
7611 this time with just the instruction up to, but not including the
7612 first '.'. */
7613 if (insn == NULL)
7614 {
7615 /* Restore the character we overwrite above (if any). */
7616 if (save_c)
7617 *(--s) = save_c;
7618
7619 /* Scan up to the first '.' or whitespace. */
7620 for (s = str;
7621 *s != '\0' && *s != '.' && !ISSPACE (*s);
7622 ++s)
7623 continue;
7624
7625 /* If we did not find a '.', then we can quit now. */
7626 if (*s != '.')
7627 {
7628 insn_error = "unrecognized opcode";
7629 return;
7630 }
7631
7632 /* Lookup the instruction in the hash table. */
7633 *s++ = '\0';
7634 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7635 {
7636 insn_error = "unrecognized opcode";
7637 return;
7638 }
7639
7640 full_opcode_match = 0;
7641 }
7642
7643 argsStart = s;
7644 for (;;)
7645 {
7646 boolean ok;
7647
7648 assert (strcmp (insn->name, str) == 0);
7649
7650 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7651 ok = true;
7652 else
7653 ok = false;
7654
7655 if (insn->pinfo != INSN_MACRO)
7656 {
7657 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7658 ok = false;
7659 }
7660
7661 if (! ok)
7662 {
7663 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7664 && strcmp (insn->name, insn[1].name) == 0)
7665 {
7666 ++insn;
7667 continue;
7668 }
7669 else
7670 {
7671 if (!insn_error)
7672 {
7673 static char buf[100];
7674 sprintf (buf,
7675 _("opcode not supported on this processor: %s (%s)"),
7676 mips_cpu_to_str (mips_arch),
7677 mips_isa_to_str (mips_opts.isa));
7678
7679 insn_error = buf;
7680 }
7681 if (save_c)
7682 *(--s) = save_c;
7683 return;
7684 }
7685 }
7686
7687 ip->insn_mo = insn;
7688 ip->insn_opcode = insn->match;
7689 insn_error = NULL;
7690 for (args = insn->args;; ++args)
7691 {
7692 s += strspn (s, " \t");
7693 switch (*args)
7694 {
7695 case '\0': /* end of args */
7696 if (*s == '\0')
7697 return;
7698 break;
7699
7700 case ',':
7701 if (*s++ == *args)
7702 continue;
7703 s--;
7704 switch (*++args)
7705 {
7706 case 'r':
7707 case 'v':
7708 ip->insn_opcode |= lastregno << OP_SH_RS;
7709 continue;
7710
7711 case 'w':
7712 ip->insn_opcode |= lastregno << OP_SH_RT;
7713 continue;
7714
7715 case 'W':
7716 ip->insn_opcode |= lastregno << OP_SH_FT;
7717 continue;
7718
7719 case 'V':
7720 ip->insn_opcode |= lastregno << OP_SH_FS;
7721 continue;
7722 }
7723 break;
7724
7725 case '(':
7726 /* Handle optional base register.
7727 Either the base register is omitted or
7728 we must have a left paren. */
7729 /* This is dependent on the next operand specifier
7730 is a base register specification. */
7731 assert (args[1] == 'b' || args[1] == '5'
7732 || args[1] == '-' || args[1] == '4');
7733 if (*s == '\0')
7734 return;
7735
7736 case ')': /* these must match exactly */
7737 if (*s++ == *args)
7738 continue;
7739 break;
7740
7741 case '<': /* must be at least one digit */
7742 /*
7743 * According to the manual, if the shift amount is greater
7744 * than 31 or less than 0, then the shift amount should be
7745 * mod 32. In reality the mips assembler issues an error.
7746 * We issue a warning and mask out all but the low 5 bits.
7747 */
7748 my_getExpression (&imm_expr, s);
7749 check_absolute_expr (ip, &imm_expr);
7750 if ((unsigned long) imm_expr.X_add_number > 31)
7751 {
7752 as_warn (_("Improper shift amount (%ld)"),
7753 (long) imm_expr.X_add_number);
7754 imm_expr.X_add_number &= OP_MASK_SHAMT;
7755 }
7756 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7757 imm_expr.X_op = O_absent;
7758 s = expr_end;
7759 continue;
7760
7761 case '>': /* shift amount minus 32 */
7762 my_getExpression (&imm_expr, s);
7763 check_absolute_expr (ip, &imm_expr);
7764 if ((unsigned long) imm_expr.X_add_number < 32
7765 || (unsigned long) imm_expr.X_add_number > 63)
7766 break;
7767 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7768 imm_expr.X_op = O_absent;
7769 s = expr_end;
7770 continue;
7771
7772 case 'k': /* cache code */
7773 case 'h': /* prefx code */
7774 my_getExpression (&imm_expr, s);
7775 check_absolute_expr (ip, &imm_expr);
7776 if ((unsigned long) imm_expr.X_add_number > 31)
7777 {
7778 as_warn (_("Invalid value for `%s' (%lu)"),
7779 ip->insn_mo->name,
7780 (unsigned long) imm_expr.X_add_number);
7781 imm_expr.X_add_number &= 0x1f;
7782 }
7783 if (*args == 'k')
7784 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7785 else
7786 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7787 imm_expr.X_op = O_absent;
7788 s = expr_end;
7789 continue;
7790
7791 case 'c': /* break code */
7792 my_getExpression (&imm_expr, s);
7793 check_absolute_expr (ip, &imm_expr);
7794 if ((unsigned) imm_expr.X_add_number > 1023)
7795 {
7796 as_warn (_("Illegal break code (%ld)"),
7797 (long) imm_expr.X_add_number);
7798 imm_expr.X_add_number &= OP_MASK_CODE;
7799 }
7800 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7801 imm_expr.X_op = O_absent;
7802 s = expr_end;
7803 continue;
7804
7805 case 'q': /* lower break code */
7806 my_getExpression (&imm_expr, s);
7807 check_absolute_expr (ip, &imm_expr);
7808 if ((unsigned) imm_expr.X_add_number > 1023)
7809 {
7810 as_warn (_("Illegal lower break code (%ld)"),
7811 (long) imm_expr.X_add_number);
7812 imm_expr.X_add_number &= OP_MASK_CODE2;
7813 }
7814 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7815 imm_expr.X_op = O_absent;
7816 s = expr_end;
7817 continue;
7818
7819 case 'B': /* 20-bit syscall/break code. */
7820 my_getExpression (&imm_expr, s);
7821 check_absolute_expr (ip, &imm_expr);
7822 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7823 as_warn (_("Illegal 20-bit code (%ld)"),
7824 (long) imm_expr.X_add_number);
7825 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7826 imm_expr.X_op = O_absent;
7827 s = expr_end;
7828 continue;
7829
7830 case 'C': /* Coprocessor code */
7831 my_getExpression (&imm_expr, s);
7832 check_absolute_expr (ip, &imm_expr);
7833 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7834 {
7835 as_warn (_("Coproccesor code > 25 bits (%ld)"),
7836 (long) imm_expr.X_add_number);
7837 imm_expr.X_add_number &= ((1<<25) - 1);
7838 }
7839 ip->insn_opcode |= imm_expr.X_add_number;
7840 imm_expr.X_op = O_absent;
7841 s = expr_end;
7842 continue;
7843
7844 case 'J': /* 19-bit wait code. */
7845 my_getExpression (&imm_expr, s);
7846 check_absolute_expr (ip, &imm_expr);
7847 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7848 as_warn (_("Illegal 19-bit code (%ld)"),
7849 (long) imm_expr.X_add_number);
7850 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7851 imm_expr.X_op = O_absent;
7852 s = expr_end;
7853 continue;
7854
7855 case 'P': /* Performance register */
7856 my_getExpression (&imm_expr, s);
7857 check_absolute_expr (ip, &imm_expr);
7858 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7859 {
7860 as_warn (_("Invalid performance register (%ld)"),
7861 (long) imm_expr.X_add_number);
7862 imm_expr.X_add_number &= OP_MASK_PERFREG;
7863 }
7864 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7865 imm_expr.X_op = O_absent;
7866 s = expr_end;
7867 continue;
7868
7869 case 'b': /* base register */
7870 case 'd': /* destination register */
7871 case 's': /* source register */
7872 case 't': /* target register */
7873 case 'r': /* both target and source */
7874 case 'v': /* both dest and source */
7875 case 'w': /* both dest and target */
7876 case 'E': /* coprocessor target register */
7877 case 'G': /* coprocessor destination register */
7878 case 'x': /* ignore register name */
7879 case 'z': /* must be zero register */
7880 case 'U': /* destination register (clo/clz). */
7881 s_reset = s;
7882 if (s[0] == '$')
7883 {
7884
7885 if (ISDIGIT (s[1]))
7886 {
7887 ++s;
7888 regno = 0;
7889 do
7890 {
7891 regno *= 10;
7892 regno += *s - '0';
7893 ++s;
7894 }
7895 while (ISDIGIT (*s));
7896 if (regno > 31)
7897 as_bad (_("Invalid register number (%d)"), regno);
7898 }
7899 else if (*args == 'E' || *args == 'G')
7900 goto notreg;
7901 else
7902 {
7903 if (s[1] == 'f' && s[2] == 'p')
7904 {
7905 s += 3;
7906 regno = FP;
7907 }
7908 else if (s[1] == 's' && s[2] == 'p')
7909 {
7910 s += 3;
7911 regno = SP;
7912 }
7913 else if (s[1] == 'g' && s[2] == 'p')
7914 {
7915 s += 3;
7916 regno = GP;
7917 }
7918 else if (s[1] == 'a' && s[2] == 't')
7919 {
7920 s += 3;
7921 regno = AT;
7922 }
7923 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7924 {
7925 s += 4;
7926 regno = KT0;
7927 }
7928 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7929 {
7930 s += 4;
7931 regno = KT1;
7932 }
7933 else if (itbl_have_entries)
7934 {
7935 char *p, *n;
7936 unsigned long r;
7937
7938 p = s + 1; /* advance past '$' */
7939 n = itbl_get_field (&p); /* n is name */
7940
7941 /* See if this is a register defined in an
7942 itbl entry. */
7943 if (itbl_get_reg_val (n, &r))
7944 {
7945 /* Get_field advances to the start of
7946 the next field, so we need to back
7947 rack to the end of the last field. */
7948 if (p)
7949 s = p - 1;
7950 else
7951 s = strchr (s, '\0');
7952 regno = r;
7953 }
7954 else
7955 goto notreg;
7956 }
7957 else
7958 goto notreg;
7959 }
7960 if (regno == AT
7961 && ! mips_opts.noat
7962 && *args != 'E'
7963 && *args != 'G')
7964 as_warn (_("Used $at without \".set noat\""));
7965 c = *args;
7966 if (*s == ' ')
7967 s++;
7968 if (args[1] != *s)
7969 {
7970 if (c == 'r' || c == 'v' || c == 'w')
7971 {
7972 regno = lastregno;
7973 s = s_reset;
7974 args++;
7975 }
7976 }
7977 /* 'z' only matches $0. */
7978 if (c == 'z' && regno != 0)
7979 break;
7980
7981 /* Now that we have assembled one operand, we use the args string
7982 * to figure out where it goes in the instruction. */
7983 switch (c)
7984 {
7985 case 'r':
7986 case 's':
7987 case 'v':
7988 case 'b':
7989 ip->insn_opcode |= regno << OP_SH_RS;
7990 break;
7991 case 'd':
7992 case 'G':
7993 ip->insn_opcode |= regno << OP_SH_RD;
7994 break;
7995 case 'U':
7996 ip->insn_opcode |= regno << OP_SH_RD;
7997 ip->insn_opcode |= regno << OP_SH_RT;
7998 break;
7999 case 'w':
8000 case 't':
8001 case 'E':
8002 ip->insn_opcode |= regno << OP_SH_RT;
8003 break;
8004 case 'x':
8005 /* This case exists because on the r3000 trunc
8006 expands into a macro which requires a gp
8007 register. On the r6000 or r4000 it is
8008 assembled into a single instruction which
8009 ignores the register. Thus the insn version
8010 is MIPS_ISA2 and uses 'x', and the macro
8011 version is MIPS_ISA1 and uses 't'. */
8012 break;
8013 case 'z':
8014 /* This case is for the div instruction, which
8015 acts differently if the destination argument
8016 is $0. This only matches $0, and is checked
8017 outside the switch. */
8018 break;
8019 case 'D':
8020 /* Itbl operand; not yet implemented. FIXME ?? */
8021 break;
8022 /* What about all other operands like 'i', which
8023 can be specified in the opcode table? */
8024 }
8025 lastregno = regno;
8026 continue;
8027 }
8028 notreg:
8029 switch (*args++)
8030 {
8031 case 'r':
8032 case 'v':
8033 ip->insn_opcode |= lastregno << OP_SH_RS;
8034 continue;
8035 case 'w':
8036 ip->insn_opcode |= lastregno << OP_SH_RT;
8037 continue;
8038 }
8039 break;
8040
8041 case 'D': /* floating point destination register */
8042 case 'S': /* floating point source register */
8043 case 'T': /* floating point target register */
8044 case 'R': /* floating point source register */
8045 case 'V':
8046 case 'W':
8047 s_reset = s;
8048 if (s[0] == '$' && s[1] == 'f'
8049 && ISDIGIT (s[2]))
8050 {
8051 s += 2;
8052 regno = 0;
8053 do
8054 {
8055 regno *= 10;
8056 regno += *s - '0';
8057 ++s;
8058 }
8059 while (ISDIGIT (*s));
8060
8061 if (regno > 31)
8062 as_bad (_("Invalid float register number (%d)"), regno);
8063
8064 if ((regno & 1) != 0
8065 && HAVE_32BIT_FPRS
8066 && ! (strcmp (str, "mtc1") == 0
8067 || strcmp (str, "mfc1") == 0
8068 || strcmp (str, "lwc1") == 0
8069 || strcmp (str, "swc1") == 0
8070 || strcmp (str, "l.s") == 0
8071 || strcmp (str, "s.s") == 0))
8072 as_warn (_("Float register should be even, was %d"),
8073 regno);
8074
8075 c = *args;
8076 if (*s == ' ')
8077 s++;
8078 if (args[1] != *s)
8079 {
8080 if (c == 'V' || c == 'W')
8081 {
8082 regno = lastregno;
8083 s = s_reset;
8084 args++;
8085 }
8086 }
8087 switch (c)
8088 {
8089 case 'D':
8090 ip->insn_opcode |= regno << OP_SH_FD;
8091 break;
8092 case 'V':
8093 case 'S':
8094 ip->insn_opcode |= regno << OP_SH_FS;
8095 break;
8096 case 'W':
8097 case 'T':
8098 ip->insn_opcode |= regno << OP_SH_FT;
8099 break;
8100 case 'R':
8101 ip->insn_opcode |= regno << OP_SH_FR;
8102 break;
8103 }
8104 lastregno = regno;
8105 continue;
8106 }
8107
8108 switch (*args++)
8109 {
8110 case 'V':
8111 ip->insn_opcode |= lastregno << OP_SH_FS;
8112 continue;
8113 case 'W':
8114 ip->insn_opcode |= lastregno << OP_SH_FT;
8115 continue;
8116 }
8117 break;
8118
8119 case 'I':
8120 my_getExpression (&imm_expr, s);
8121 if (imm_expr.X_op != O_big
8122 && imm_expr.X_op != O_constant)
8123 insn_error = _("absolute expression required");
8124 s = expr_end;
8125 continue;
8126
8127 case 'A':
8128 my_getExpression (&offset_expr, s);
8129 *imm_reloc = BFD_RELOC_32;
8130 s = expr_end;
8131 continue;
8132
8133 case 'F':
8134 case 'L':
8135 case 'f':
8136 case 'l':
8137 {
8138 int f64;
8139 int using_gprs;
8140 char *save_in;
8141 char *err;
8142 unsigned char temp[8];
8143 int len;
8144 unsigned int length;
8145 segT seg;
8146 subsegT subseg;
8147 char *p;
8148
8149 /* These only appear as the last operand in an
8150 instruction, and every instruction that accepts
8151 them in any variant accepts them in all variants.
8152 This means we don't have to worry about backing out
8153 any changes if the instruction does not match.
8154
8155 The difference between them is the size of the
8156 floating point constant and where it goes. For 'F'
8157 and 'L' the constant is 64 bits; for 'f' and 'l' it
8158 is 32 bits. Where the constant is placed is based
8159 on how the MIPS assembler does things:
8160 F -- .rdata
8161 L -- .lit8
8162 f -- immediate value
8163 l -- .lit4
8164
8165 The .lit4 and .lit8 sections are only used if
8166 permitted by the -G argument.
8167
8168 When generating embedded PIC code, we use the
8169 .lit8 section but not the .lit4 section (we can do
8170 .lit4 inline easily; we need to put .lit8
8171 somewhere in the data segment, and using .lit8
8172 permits the linker to eventually combine identical
8173 .lit8 entries).
8174
8175 The code below needs to know whether the target register
8176 is 32 or 64 bits wide. It relies on the fact 'f' and
8177 'F' are used with GPR-based instructions and 'l' and
8178 'L' are used with FPR-based instructions. */
8179
8180 f64 = *args == 'F' || *args == 'L';
8181 using_gprs = *args == 'F' || *args == 'f';
8182
8183 save_in = input_line_pointer;
8184 input_line_pointer = s;
8185 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8186 length = len;
8187 s = input_line_pointer;
8188 input_line_pointer = save_in;
8189 if (err != NULL && *err != '\0')
8190 {
8191 as_bad (_("Bad floating point constant: %s"), err);
8192 memset (temp, '\0', sizeof temp);
8193 length = f64 ? 8 : 4;
8194 }
8195
8196 assert (length == (unsigned) (f64 ? 8 : 4));
8197
8198 if (*args == 'f'
8199 || (*args == 'l'
8200 && (! USE_GLOBAL_POINTER_OPT
8201 || mips_pic == EMBEDDED_PIC
8202 || g_switch_value < 4
8203 || (temp[0] == 0 && temp[1] == 0)
8204 || (temp[2] == 0 && temp[3] == 0))))
8205 {
8206 imm_expr.X_op = O_constant;
8207 if (! target_big_endian)
8208 imm_expr.X_add_number = bfd_getl32 (temp);
8209 else
8210 imm_expr.X_add_number = bfd_getb32 (temp);
8211 }
8212 else if (length > 4
8213 && ! mips_disable_float_construction
8214 /* Constants can only be constructed in GPRs and
8215 copied to FPRs if the GPRs are at least as wide
8216 as the FPRs. Force the constant into memory if
8217 we are using 64-bit FPRs but the GPRs are only
8218 32 bits wide. */
8219 && (using_gprs
8220 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8221 && ((temp[0] == 0 && temp[1] == 0)
8222 || (temp[2] == 0 && temp[3] == 0))
8223 && ((temp[4] == 0 && temp[5] == 0)
8224 || (temp[6] == 0 && temp[7] == 0)))
8225 {
8226 /* The value is simple enough to load with a couple of
8227 instructions. If using 32-bit registers, set
8228 imm_expr to the high order 32 bits and offset_expr to
8229 the low order 32 bits. Otherwise, set imm_expr to
8230 the entire 64 bit constant. */
8231 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8232 {
8233 imm_expr.X_op = O_constant;
8234 offset_expr.X_op = O_constant;
8235 if (! target_big_endian)
8236 {
8237 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8238 offset_expr.X_add_number = bfd_getl32 (temp);
8239 }
8240 else
8241 {
8242 imm_expr.X_add_number = bfd_getb32 (temp);
8243 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8244 }
8245 if (offset_expr.X_add_number == 0)
8246 offset_expr.X_op = O_absent;
8247 }
8248 else if (sizeof (imm_expr.X_add_number) > 4)
8249 {
8250 imm_expr.X_op = O_constant;
8251 if (! target_big_endian)
8252 imm_expr.X_add_number = bfd_getl64 (temp);
8253 else
8254 imm_expr.X_add_number = bfd_getb64 (temp);
8255 }
8256 else
8257 {
8258 imm_expr.X_op = O_big;
8259 imm_expr.X_add_number = 4;
8260 if (! target_big_endian)
8261 {
8262 generic_bignum[0] = bfd_getl16 (temp);
8263 generic_bignum[1] = bfd_getl16 (temp + 2);
8264 generic_bignum[2] = bfd_getl16 (temp + 4);
8265 generic_bignum[3] = bfd_getl16 (temp + 6);
8266 }
8267 else
8268 {
8269 generic_bignum[0] = bfd_getb16 (temp + 6);
8270 generic_bignum[1] = bfd_getb16 (temp + 4);
8271 generic_bignum[2] = bfd_getb16 (temp + 2);
8272 generic_bignum[3] = bfd_getb16 (temp);
8273 }
8274 }
8275 }
8276 else
8277 {
8278 const char *newname;
8279 segT new_seg;
8280
8281 /* Switch to the right section. */
8282 seg = now_seg;
8283 subseg = now_subseg;
8284 switch (*args)
8285 {
8286 default: /* unused default case avoids warnings. */
8287 case 'L':
8288 newname = RDATA_SECTION_NAME;
8289 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8290 || mips_pic == EMBEDDED_PIC)
8291 newname = ".lit8";
8292 break;
8293 case 'F':
8294 if (mips_pic == EMBEDDED_PIC)
8295 newname = ".lit8";
8296 else
8297 newname = RDATA_SECTION_NAME;
8298 break;
8299 case 'l':
8300 assert (!USE_GLOBAL_POINTER_OPT
8301 || g_switch_value >= 4);
8302 newname = ".lit4";
8303 break;
8304 }
8305 new_seg = subseg_new (newname, (subsegT) 0);
8306 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8307 bfd_set_section_flags (stdoutput, new_seg,
8308 (SEC_ALLOC
8309 | SEC_LOAD
8310 | SEC_READONLY
8311 | SEC_DATA));
8312 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8313 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8314 && strcmp (TARGET_OS, "elf") != 0)
8315 record_alignment (new_seg, 4);
8316 else
8317 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8318 if (seg == now_seg)
8319 as_bad (_("Can't use floating point insn in this section"));
8320
8321 /* Set the argument to the current address in the
8322 section. */
8323 offset_expr.X_op = O_symbol;
8324 offset_expr.X_add_symbol =
8325 symbol_new ("L0\001", now_seg,
8326 (valueT) frag_now_fix (), frag_now);
8327 offset_expr.X_add_number = 0;
8328
8329 /* Put the floating point number into the section. */
8330 p = frag_more ((int) length);
8331 memcpy (p, temp, length);
8332
8333 /* Switch back to the original section. */
8334 subseg_set (seg, subseg);
8335 }
8336 }
8337 continue;
8338
8339 case 'i': /* 16 bit unsigned immediate */
8340 case 'j': /* 16 bit signed immediate */
8341 *imm_reloc = BFD_RELOC_LO16;
8342 c = my_getSmallExpression (&imm_expr, s);
8343 if (c != S_EX_NONE)
8344 {
8345 if (c != S_EX_LO)
8346 {
8347 if (imm_expr.X_op == O_constant)
8348 imm_expr.X_add_number =
8349 (imm_expr.X_add_number >> 16) & 0xffff;
8350 #ifdef OBJ_ELF
8351 else if (c == S_EX_HIGHEST)
8352 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8353 else if (c == S_EX_HIGHER)
8354 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8355 else if (c == S_EX_GP_REL)
8356 {
8357 /* This occurs in NewABI only. */
8358 c = my_getSmallExpression (&imm_expr, s);
8359 if (c != S_EX_NEG)
8360 as_bad (_("bad composition of relocations"));
8361 else
8362 {
8363 c = my_getSmallExpression (&imm_expr, s);
8364 if (c != S_EX_LO)
8365 as_bad (_("bad composition of relocations"));
8366 else
8367 {
8368 imm_reloc[0] = BFD_RELOC_GPREL16;
8369 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8370 imm_reloc[2] = BFD_RELOC_LO16;
8371 }
8372 }
8373 }
8374 #endif
8375 else if (c == S_EX_HI)
8376 {
8377 *imm_reloc = BFD_RELOC_HI16_S;
8378 imm_unmatched_hi = true;
8379 }
8380 else
8381 *imm_reloc = BFD_RELOC_HI16;
8382 }
8383 else if (imm_expr.X_op == O_constant)
8384 imm_expr.X_add_number &= 0xffff;
8385 }
8386 if (*args == 'i')
8387 {
8388 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8389 || ((imm_expr.X_add_number < 0
8390 || imm_expr.X_add_number >= 0x10000)
8391 && imm_expr.X_op == O_constant))
8392 {
8393 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8394 !strcmp (insn->name, insn[1].name))
8395 break;
8396 if (imm_expr.X_op == O_constant
8397 || imm_expr.X_op == O_big)
8398 as_bad (_("16 bit expression not in range 0..65535"));
8399 }
8400 }
8401 else
8402 {
8403 int more;
8404 offsetT max;
8405
8406 /* The upper bound should be 0x8000, but
8407 unfortunately the MIPS assembler accepts numbers
8408 from 0x8000 to 0xffff and sign extends them, and
8409 we want to be compatible. We only permit this
8410 extended range for an instruction which does not
8411 provide any further alternates, since those
8412 alternates may handle other cases. People should
8413 use the numbers they mean, rather than relying on
8414 a mysterious sign extension. */
8415 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8416 strcmp (insn->name, insn[1].name) == 0);
8417 if (more)
8418 max = 0x8000;
8419 else
8420 max = 0x10000;
8421 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8422 || ((imm_expr.X_add_number < -0x8000
8423 || imm_expr.X_add_number >= max)
8424 && imm_expr.X_op == O_constant)
8425 || (more
8426 && imm_expr.X_add_number < 0
8427 && HAVE_64BIT_GPRS
8428 && imm_expr.X_unsigned
8429 && sizeof (imm_expr.X_add_number) <= 4))
8430 {
8431 if (more)
8432 break;
8433 if (imm_expr.X_op == O_constant
8434 || imm_expr.X_op == O_big)
8435 as_bad (_("16 bit expression not in range -32768..32767"));
8436 }
8437 }
8438 s = expr_end;
8439 continue;
8440
8441 case 'o': /* 16 bit offset */
8442 c = my_getSmallExpression (&offset_expr, s);
8443
8444 /* If this value won't fit into a 16 bit offset, then go
8445 find a macro that will generate the 32 bit offset
8446 code pattern. */
8447 if (c == S_EX_NONE
8448 && (offset_expr.X_op != O_constant
8449 || offset_expr.X_add_number >= 0x8000
8450 || offset_expr.X_add_number < -0x8000))
8451 break;
8452
8453 if (c == S_EX_HI)
8454 {
8455 if (offset_expr.X_op != O_constant)
8456 break;
8457 offset_expr.X_add_number =
8458 (offset_expr.X_add_number >> 16) & 0xffff;
8459 }
8460 *offset_reloc = BFD_RELOC_LO16;
8461 s = expr_end;
8462 continue;
8463
8464 case 'p': /* pc relative offset */
8465 if (mips_pic == EMBEDDED_PIC)
8466 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8467 else
8468 *offset_reloc = BFD_RELOC_16_PCREL;
8469 my_getExpression (&offset_expr, s);
8470 s = expr_end;
8471 continue;
8472
8473 case 'u': /* upper 16 bits */
8474 c = my_getSmallExpression (&imm_expr, s);
8475 *imm_reloc = BFD_RELOC_LO16;
8476 if (c != S_EX_NONE)
8477 {
8478 if (c != S_EX_LO)
8479 {
8480 if (imm_expr.X_op == O_constant)
8481 imm_expr.X_add_number =
8482 (imm_expr.X_add_number >> 16) & 0xffff;
8483 else if (c == S_EX_HI)
8484 {
8485 *imm_reloc = BFD_RELOC_HI16_S;
8486 imm_unmatched_hi = true;
8487 }
8488 #ifdef OBJ_ELF
8489 else if (c == S_EX_HIGHEST)
8490 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8491 else if (c == S_EX_GP_REL)
8492 {
8493 /* This occurs in NewABI only. */
8494 c = my_getSmallExpression (&imm_expr, s);
8495 if (c != S_EX_NEG)
8496 as_bad (_("bad composition of relocations"));
8497 else
8498 {
8499 c = my_getSmallExpression (&imm_expr, s);
8500 if (c != S_EX_HI)
8501 as_bad (_("bad composition of relocations"));
8502 else
8503 {
8504 imm_reloc[0] = BFD_RELOC_GPREL16;
8505 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8506 imm_reloc[2] = BFD_RELOC_HI16_S;
8507 }
8508 }
8509 }
8510 #endif
8511 else
8512 *imm_reloc = BFD_RELOC_HI16;
8513 }
8514 else if (imm_expr.X_op == O_constant)
8515 imm_expr.X_add_number &= 0xffff;
8516 }
8517 if (imm_expr.X_op == O_constant
8518 && (imm_expr.X_add_number < 0
8519 || imm_expr.X_add_number >= 0x10000))
8520 as_bad (_("lui expression not in range 0..65535"));
8521 s = expr_end;
8522 continue;
8523
8524 case 'a': /* 26 bit address */
8525 my_getExpression (&offset_expr, s);
8526 s = expr_end;
8527 *offset_reloc = BFD_RELOC_MIPS_JMP;
8528 continue;
8529
8530 case 'N': /* 3 bit branch condition code */
8531 case 'M': /* 3 bit compare condition code */
8532 if (strncmp (s, "$fcc", 4) != 0)
8533 break;
8534 s += 4;
8535 regno = 0;
8536 do
8537 {
8538 regno *= 10;
8539 regno += *s - '0';
8540 ++s;
8541 }
8542 while (ISDIGIT (*s));
8543 if (regno > 7)
8544 as_bad (_("invalid condition code register $fcc%d"), regno);
8545 if (*args == 'N')
8546 ip->insn_opcode |= regno << OP_SH_BCC;
8547 else
8548 ip->insn_opcode |= regno << OP_SH_CCC;
8549 continue;
8550
8551 case 'H':
8552 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8553 s += 2;
8554 if (ISDIGIT (*s))
8555 {
8556 c = 0;
8557 do
8558 {
8559 c *= 10;
8560 c += *s - '0';
8561 ++s;
8562 }
8563 while (ISDIGIT (*s));
8564 }
8565 else
8566 c = 8; /* Invalid sel value. */
8567
8568 if (c > 7)
8569 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8570 ip->insn_opcode |= c;
8571 continue;
8572
8573 default:
8574 as_bad (_("bad char = '%c'\n"), *args);
8575 internalError ();
8576 }
8577 break;
8578 }
8579 /* Args don't match. */
8580 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8581 !strcmp (insn->name, insn[1].name))
8582 {
8583 ++insn;
8584 s = argsStart;
8585 insn_error = _("illegal operands");
8586 continue;
8587 }
8588 if (save_c)
8589 *(--s) = save_c;
8590 insn_error = _("illegal operands");
8591 return;
8592 }
8593 }
8594
8595 /* This routine assembles an instruction into its binary format when
8596 assembling for the mips16. As a side effect, it sets one of the
8597 global variables imm_reloc or offset_reloc to the type of
8598 relocation to do if one of the operands is an address expression.
8599 It also sets mips16_small and mips16_ext if the user explicitly
8600 requested a small or extended instruction. */
8601
8602 static void
8603 mips16_ip (str, ip)
8604 char *str;
8605 struct mips_cl_insn *ip;
8606 {
8607 char *s;
8608 const char *args;
8609 struct mips_opcode *insn;
8610 char *argsstart;
8611 unsigned int regno;
8612 unsigned int lastregno = 0;
8613 char *s_reset;
8614
8615 insn_error = NULL;
8616
8617 mips16_small = false;
8618 mips16_ext = false;
8619
8620 for (s = str; ISLOWER (*s); ++s)
8621 ;
8622 switch (*s)
8623 {
8624 case '\0':
8625 break;
8626
8627 case ' ':
8628 *s++ = '\0';
8629 break;
8630
8631 case '.':
8632 if (s[1] == 't' && s[2] == ' ')
8633 {
8634 *s = '\0';
8635 mips16_small = true;
8636 s += 3;
8637 break;
8638 }
8639 else if (s[1] == 'e' && s[2] == ' ')
8640 {
8641 *s = '\0';
8642 mips16_ext = true;
8643 s += 3;
8644 break;
8645 }
8646 /* Fall through. */
8647 default:
8648 insn_error = _("unknown opcode");
8649 return;
8650 }
8651
8652 if (mips_opts.noautoextend && ! mips16_ext)
8653 mips16_small = true;
8654
8655 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8656 {
8657 insn_error = _("unrecognized opcode");
8658 return;
8659 }
8660
8661 argsstart = s;
8662 for (;;)
8663 {
8664 assert (strcmp (insn->name, str) == 0);
8665
8666 ip->insn_mo = insn;
8667 ip->insn_opcode = insn->match;
8668 ip->use_extend = false;
8669 imm_expr.X_op = O_absent;
8670 imm_reloc[0] = BFD_RELOC_UNUSED;
8671 imm_reloc[1] = BFD_RELOC_UNUSED;
8672 imm_reloc[2] = BFD_RELOC_UNUSED;
8673 offset_expr.X_op = O_absent;
8674 offset_reloc[0] = BFD_RELOC_UNUSED;
8675 offset_reloc[1] = BFD_RELOC_UNUSED;
8676 offset_reloc[2] = BFD_RELOC_UNUSED;
8677 for (args = insn->args; 1; ++args)
8678 {
8679 int c;
8680
8681 if (*s == ' ')
8682 ++s;
8683
8684 /* In this switch statement we call break if we did not find
8685 a match, continue if we did find a match, or return if we
8686 are done. */
8687
8688 c = *args;
8689 switch (c)
8690 {
8691 case '\0':
8692 if (*s == '\0')
8693 {
8694 /* Stuff the immediate value in now, if we can. */
8695 if (imm_expr.X_op == O_constant
8696 && *imm_reloc > BFD_RELOC_UNUSED
8697 && insn->pinfo != INSN_MACRO)
8698 {
8699 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8700 imm_expr.X_add_number, true, mips16_small,
8701 mips16_ext, &ip->insn_opcode,
8702 &ip->use_extend, &ip->extend);
8703 imm_expr.X_op = O_absent;
8704 *imm_reloc = BFD_RELOC_UNUSED;
8705 }
8706
8707 return;
8708 }
8709 break;
8710
8711 case ',':
8712 if (*s++ == c)
8713 continue;
8714 s--;
8715 switch (*++args)
8716 {
8717 case 'v':
8718 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8719 continue;
8720 case 'w':
8721 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8722 continue;
8723 }
8724 break;
8725
8726 case '(':
8727 case ')':
8728 if (*s++ == c)
8729 continue;
8730 break;
8731
8732 case 'v':
8733 case 'w':
8734 if (s[0] != '$')
8735 {
8736 if (c == 'v')
8737 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8738 else
8739 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8740 ++args;
8741 continue;
8742 }
8743 /* Fall through. */
8744 case 'x':
8745 case 'y':
8746 case 'z':
8747 case 'Z':
8748 case '0':
8749 case 'S':
8750 case 'R':
8751 case 'X':
8752 case 'Y':
8753 if (s[0] != '$')
8754 break;
8755 s_reset = s;
8756 if (ISDIGIT (s[1]))
8757 {
8758 ++s;
8759 regno = 0;
8760 do
8761 {
8762 regno *= 10;
8763 regno += *s - '0';
8764 ++s;
8765 }
8766 while (ISDIGIT (*s));
8767 if (regno > 31)
8768 {
8769 as_bad (_("invalid register number (%d)"), regno);
8770 regno = 2;
8771 }
8772 }
8773 else
8774 {
8775 if (s[1] == 'f' && s[2] == 'p')
8776 {
8777 s += 3;
8778 regno = FP;
8779 }
8780 else if (s[1] == 's' && s[2] == 'p')
8781 {
8782 s += 3;
8783 regno = SP;
8784 }
8785 else if (s[1] == 'g' && s[2] == 'p')
8786 {
8787 s += 3;
8788 regno = GP;
8789 }
8790 else if (s[1] == 'a' && s[2] == 't')
8791 {
8792 s += 3;
8793 regno = AT;
8794 }
8795 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8796 {
8797 s += 4;
8798 regno = KT0;
8799 }
8800 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8801 {
8802 s += 4;
8803 regno = KT1;
8804 }
8805 else
8806 break;
8807 }
8808
8809 if (*s == ' ')
8810 ++s;
8811 if (args[1] != *s)
8812 {
8813 if (c == 'v' || c == 'w')
8814 {
8815 regno = mips16_to_32_reg_map[lastregno];
8816 s = s_reset;
8817 args++;
8818 }
8819 }
8820
8821 switch (c)
8822 {
8823 case 'x':
8824 case 'y':
8825 case 'z':
8826 case 'v':
8827 case 'w':
8828 case 'Z':
8829 regno = mips32_to_16_reg_map[regno];
8830 break;
8831
8832 case '0':
8833 if (regno != 0)
8834 regno = ILLEGAL_REG;
8835 break;
8836
8837 case 'S':
8838 if (regno != SP)
8839 regno = ILLEGAL_REG;
8840 break;
8841
8842 case 'R':
8843 if (regno != RA)
8844 regno = ILLEGAL_REG;
8845 break;
8846
8847 case 'X':
8848 case 'Y':
8849 if (regno == AT && ! mips_opts.noat)
8850 as_warn (_("used $at without \".set noat\""));
8851 break;
8852
8853 default:
8854 internalError ();
8855 }
8856
8857 if (regno == ILLEGAL_REG)
8858 break;
8859
8860 switch (c)
8861 {
8862 case 'x':
8863 case 'v':
8864 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8865 break;
8866 case 'y':
8867 case 'w':
8868 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8869 break;
8870 case 'z':
8871 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8872 break;
8873 case 'Z':
8874 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8875 case '0':
8876 case 'S':
8877 case 'R':
8878 break;
8879 case 'X':
8880 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8881 break;
8882 case 'Y':
8883 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8884 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8885 break;
8886 default:
8887 internalError ();
8888 }
8889
8890 lastregno = regno;
8891 continue;
8892
8893 case 'P':
8894 if (strncmp (s, "$pc", 3) == 0)
8895 {
8896 s += 3;
8897 continue;
8898 }
8899 break;
8900
8901 case '<':
8902 case '>':
8903 case '[':
8904 case ']':
8905 case '4':
8906 case '5':
8907 case 'H':
8908 case 'W':
8909 case 'D':
8910 case 'j':
8911 case '8':
8912 case 'V':
8913 case 'C':
8914 case 'U':
8915 case 'k':
8916 case 'K':
8917 if (s[0] == '%'
8918 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8919 {
8920 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8921 and generate the appropriate reloc. If the text
8922 inside %gprel is not a symbol name with an
8923 optional offset, then we generate a normal reloc
8924 and will probably fail later. */
8925 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8926 if (imm_expr.X_op == O_symbol)
8927 {
8928 mips16_ext = true;
8929 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8930 s = expr_end;
8931 ip->use_extend = true;
8932 ip->extend = 0;
8933 continue;
8934 }
8935 }
8936 else
8937 {
8938 /* Just pick up a normal expression. */
8939 my_getExpression (&imm_expr, s);
8940 }
8941
8942 if (imm_expr.X_op == O_register)
8943 {
8944 /* What we thought was an expression turned out to
8945 be a register. */
8946
8947 if (s[0] == '(' && args[1] == '(')
8948 {
8949 /* It looks like the expression was omitted
8950 before a register indirection, which means
8951 that the expression is implicitly zero. We
8952 still set up imm_expr, so that we handle
8953 explicit extensions correctly. */
8954 imm_expr.X_op = O_constant;
8955 imm_expr.X_add_number = 0;
8956 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8957 continue;
8958 }
8959
8960 break;
8961 }
8962
8963 /* We need to relax this instruction. */
8964 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8965 s = expr_end;
8966 continue;
8967
8968 case 'p':
8969 case 'q':
8970 case 'A':
8971 case 'B':
8972 case 'E':
8973 /* We use offset_reloc rather than imm_reloc for the PC
8974 relative operands. This lets macros with both
8975 immediate and address operands work correctly. */
8976 my_getExpression (&offset_expr, s);
8977
8978 if (offset_expr.X_op == O_register)
8979 break;
8980
8981 /* We need to relax this instruction. */
8982 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8983 s = expr_end;
8984 continue;
8985
8986 case '6': /* break code */
8987 my_getExpression (&imm_expr, s);
8988 check_absolute_expr (ip, &imm_expr);
8989 if ((unsigned long) imm_expr.X_add_number > 63)
8990 {
8991 as_warn (_("Invalid value for `%s' (%lu)"),
8992 ip->insn_mo->name,
8993 (unsigned long) imm_expr.X_add_number);
8994 imm_expr.X_add_number &= 0x3f;
8995 }
8996 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8997 imm_expr.X_op = O_absent;
8998 s = expr_end;
8999 continue;
9000
9001 case 'a': /* 26 bit address */
9002 my_getExpression (&offset_expr, s);
9003 s = expr_end;
9004 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9005 ip->insn_opcode <<= 16;
9006 continue;
9007
9008 case 'l': /* register list for entry macro */
9009 case 'L': /* register list for exit macro */
9010 {
9011 int mask;
9012
9013 if (c == 'l')
9014 mask = 0;
9015 else
9016 mask = 7 << 3;
9017 while (*s != '\0')
9018 {
9019 int freg, reg1, reg2;
9020
9021 while (*s == ' ' || *s == ',')
9022 ++s;
9023 if (*s != '$')
9024 {
9025 as_bad (_("can't parse register list"));
9026 break;
9027 }
9028 ++s;
9029 if (*s != 'f')
9030 freg = 0;
9031 else
9032 {
9033 freg = 1;
9034 ++s;
9035 }
9036 reg1 = 0;
9037 while (ISDIGIT (*s))
9038 {
9039 reg1 *= 10;
9040 reg1 += *s - '0';
9041 ++s;
9042 }
9043 if (*s == ' ')
9044 ++s;
9045 if (*s != '-')
9046 reg2 = reg1;
9047 else
9048 {
9049 ++s;
9050 if (*s != '$')
9051 break;
9052 ++s;
9053 if (freg)
9054 {
9055 if (*s == 'f')
9056 ++s;
9057 else
9058 {
9059 as_bad (_("invalid register list"));
9060 break;
9061 }
9062 }
9063 reg2 = 0;
9064 while (ISDIGIT (*s))
9065 {
9066 reg2 *= 10;
9067 reg2 += *s - '0';
9068 ++s;
9069 }
9070 }
9071 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9072 {
9073 mask &= ~ (7 << 3);
9074 mask |= 5 << 3;
9075 }
9076 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9077 {
9078 mask &= ~ (7 << 3);
9079 mask |= 6 << 3;
9080 }
9081 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9082 mask |= (reg2 - 3) << 3;
9083 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9084 mask |= (reg2 - 15) << 1;
9085 else if (reg1 == 31 && reg2 == 31)
9086 mask |= 1;
9087 else
9088 {
9089 as_bad (_("invalid register list"));
9090 break;
9091 }
9092 }
9093 /* The mask is filled in in the opcode table for the
9094 benefit of the disassembler. We remove it before
9095 applying the actual mask. */
9096 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9097 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9098 }
9099 continue;
9100
9101 case 'e': /* extend code */
9102 my_getExpression (&imm_expr, s);
9103 check_absolute_expr (ip, &imm_expr);
9104 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9105 {
9106 as_warn (_("Invalid value for `%s' (%lu)"),
9107 ip->insn_mo->name,
9108 (unsigned long) imm_expr.X_add_number);
9109 imm_expr.X_add_number &= 0x7ff;
9110 }
9111 ip->insn_opcode |= imm_expr.X_add_number;
9112 imm_expr.X_op = O_absent;
9113 s = expr_end;
9114 continue;
9115
9116 default:
9117 internalError ();
9118 }
9119 break;
9120 }
9121
9122 /* Args don't match. */
9123 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9124 strcmp (insn->name, insn[1].name) == 0)
9125 {
9126 ++insn;
9127 s = argsstart;
9128 continue;
9129 }
9130
9131 insn_error = _("illegal operands");
9132
9133 return;
9134 }
9135 }
9136
9137 /* This structure holds information we know about a mips16 immediate
9138 argument type. */
9139
9140 struct mips16_immed_operand
9141 {
9142 /* The type code used in the argument string in the opcode table. */
9143 int type;
9144 /* The number of bits in the short form of the opcode. */
9145 int nbits;
9146 /* The number of bits in the extended form of the opcode. */
9147 int extbits;
9148 /* The amount by which the short form is shifted when it is used;
9149 for example, the sw instruction has a shift count of 2. */
9150 int shift;
9151 /* The amount by which the short form is shifted when it is stored
9152 into the instruction code. */
9153 int op_shift;
9154 /* Non-zero if the short form is unsigned. */
9155 int unsp;
9156 /* Non-zero if the extended form is unsigned. */
9157 int extu;
9158 /* Non-zero if the value is PC relative. */
9159 int pcrel;
9160 };
9161
9162 /* The mips16 immediate operand types. */
9163
9164 static const struct mips16_immed_operand mips16_immed_operands[] =
9165 {
9166 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9167 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9168 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9169 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9170 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9171 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9172 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9173 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9174 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9175 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9176 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9177 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9178 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9179 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9180 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9181 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9182 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9183 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9184 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9185 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9186 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9187 };
9188
9189 #define MIPS16_NUM_IMMED \
9190 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9191
9192 /* Handle a mips16 instruction with an immediate value. This or's the
9193 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9194 whether an extended value is needed; if one is needed, it sets
9195 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9196 If SMALL is true, an unextended opcode was explicitly requested.
9197 If EXT is true, an extended opcode was explicitly requested. If
9198 WARN is true, warn if EXT does not match reality. */
9199
9200 static void
9201 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9202 extend)
9203 char *file;
9204 unsigned int line;
9205 int type;
9206 offsetT val;
9207 boolean warn;
9208 boolean small;
9209 boolean ext;
9210 unsigned long *insn;
9211 boolean *use_extend;
9212 unsigned short *extend;
9213 {
9214 register const struct mips16_immed_operand *op;
9215 int mintiny, maxtiny;
9216 boolean needext;
9217
9218 op = mips16_immed_operands;
9219 while (op->type != type)
9220 {
9221 ++op;
9222 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9223 }
9224
9225 if (op->unsp)
9226 {
9227 if (type == '<' || type == '>' || type == '[' || type == ']')
9228 {
9229 mintiny = 1;
9230 maxtiny = 1 << op->nbits;
9231 }
9232 else
9233 {
9234 mintiny = 0;
9235 maxtiny = (1 << op->nbits) - 1;
9236 }
9237 }
9238 else
9239 {
9240 mintiny = - (1 << (op->nbits - 1));
9241 maxtiny = (1 << (op->nbits - 1)) - 1;
9242 }
9243
9244 /* Branch offsets have an implicit 0 in the lowest bit. */
9245 if (type == 'p' || type == 'q')
9246 val /= 2;
9247
9248 if ((val & ((1 << op->shift) - 1)) != 0
9249 || val < (mintiny << op->shift)
9250 || val > (maxtiny << op->shift))
9251 needext = true;
9252 else
9253 needext = false;
9254
9255 if (warn && ext && ! needext)
9256 as_warn_where (file, line,
9257 _("extended operand requested but not required"));
9258 if (small && needext)
9259 as_bad_where (file, line, _("invalid unextended operand value"));
9260
9261 if (small || (! ext && ! needext))
9262 {
9263 int insnval;
9264
9265 *use_extend = false;
9266 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9267 insnval <<= op->op_shift;
9268 *insn |= insnval;
9269 }
9270 else
9271 {
9272 long minext, maxext;
9273 int extval;
9274
9275 if (op->extu)
9276 {
9277 minext = 0;
9278 maxext = (1 << op->extbits) - 1;
9279 }
9280 else
9281 {
9282 minext = - (1 << (op->extbits - 1));
9283 maxext = (1 << (op->extbits - 1)) - 1;
9284 }
9285 if (val < minext || val > maxext)
9286 as_bad_where (file, line,
9287 _("operand value out of range for instruction"));
9288
9289 *use_extend = true;
9290 if (op->extbits == 16)
9291 {
9292 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9293 val &= 0x1f;
9294 }
9295 else if (op->extbits == 15)
9296 {
9297 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9298 val &= 0xf;
9299 }
9300 else
9301 {
9302 extval = ((val & 0x1f) << 6) | (val & 0x20);
9303 val = 0;
9304 }
9305
9306 *extend = (unsigned short) extval;
9307 *insn |= val;
9308 }
9309 }
9310 \f
9311 static struct percent_op_match
9312 {
9313 const char *str;
9314 const enum small_ex_type type;
9315 } percent_op[] =
9316 {
9317 #ifdef OBJ_ELF
9318 {"%half", S_EX_HALF},
9319 #endif
9320 {"%hi", S_EX_HI},
9321 {"%lo", S_EX_LO},
9322 #ifdef OBJ_ELF
9323 {"%gp_rel", S_EX_GP_REL},
9324 {"%got", S_EX_GOT},
9325 {"%call16", S_EX_CALL16},
9326 {"%got_disp", S_EX_GOT_DISP},
9327 {"%got_page", S_EX_GOT_PAGE},
9328 {"%got_ofst", S_EX_GOT_OFST},
9329 {"%got_hi", S_EX_GOT_HI},
9330 {"%got_lo", S_EX_GOT_LO},
9331 {"%neg", S_EX_NEG},
9332 {"%higher", S_EX_HIGHER},
9333 {"%highest", S_EX_HIGHEST},
9334 {"%call_hi", S_EX_CALL_HI},
9335 {"%call_lo", S_EX_CALL_LO}
9336 #endif
9337 };
9338
9339 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9340 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9341 can be nested, this is handled by blanking the innermost, parsing the
9342 rest by subsequent calls. */
9343
9344 static int
9345 my_getSmallParser (str, len, nestlevel)
9346 char **str;
9347 unsigned int *len;
9348 int *nestlevel;
9349 {
9350 int type = S_EX_NONE;
9351
9352 *len = 0;
9353 *str += strspn (*str, " \t");
9354 if (**str == '(')
9355 {
9356 char *b = *str + 1 + strspn (*str + 1, " \t");
9357 char *e;
9358
9359 /* Check for base register. */
9360 if (b[0] == '$')
9361 {
9362 if (strchr (b, ')')
9363 && (e = b + strcspn (b, ") \t"))
9364 && e - b > 1 && e - b < 4)
9365 {
9366 if ((e - b == 3
9367 && ((b[1] == 'f' && b[2] == 'p')
9368 || (b[1] == 's' && b[2] == 'p')
9369 || (b[1] == 'g' && b[2] == 'p')
9370 || (b[1] == 'a' && b[2] == 't')
9371 || (ISDIGIT (b[1])
9372 && ISDIGIT (b[2]))))
9373 || (ISDIGIT (b[1])))
9374 {
9375 *len = strcspn (*str, ")") + 1;
9376 return S_EX_REGISTER;
9377 }
9378 }
9379 }
9380 else if (b[0] == '%')
9381 {
9382 *str = b;
9383 goto percent_op;
9384 }
9385
9386 /* Some other expression in the braces. */
9387 *len = strcspn (*str, ")") + 1;
9388 }
9389 /* Check for percent_op. */
9390 else if (*str[0] == '%')
9391 {
9392 char *tmp;
9393 unsigned int i;
9394
9395 percent_op:
9396 tmp = *str + 1;
9397 i = 0;
9398
9399 while (ISALPHA (*tmp) || *tmp == '_')
9400 {
9401 *tmp = TOLOWER (*tmp);
9402 tmp++;
9403 }
9404 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9405 {
9406 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9407 i++;
9408 else
9409 {
9410 type = percent_op[i].type;
9411
9412 /* Only %hi and %lo are allowed for OldABI. */
9413 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9414 return S_EX_NONE;
9415
9416 *len = strlen (percent_op[i].str);
9417 (*nestlevel)++;
9418 return type;
9419 }
9420 }
9421 }
9422
9423 /* Any other expression. */
9424 return S_EX_NONE;
9425 }
9426
9427 static int
9428 my_getSmallExpression (ep, str)
9429 expressionS *ep;
9430 char *str;
9431 {
9432 static char *oldstr = NULL;
9433 int c = S_EX_NONE;
9434 int oldc;
9435 int nest_level = 0;
9436 unsigned int len;
9437
9438 /* Don't update oldstr if the last call had nested percent_op's. */
9439 if (! oldstr)
9440 oldstr = str;
9441
9442 do
9443 {
9444 oldc = c;
9445 c = my_getSmallParser (&str, &len, &nest_level);
9446 if (c != S_EX_NONE && c != S_EX_REGISTER)
9447 str += len;
9448 }
9449 while (c != S_EX_NONE && c != S_EX_REGISTER);
9450
9451 /* A percent_op was encountered. */
9452 if (nest_level)
9453 {
9454 /* Don't try to get an expression if it is already blanked out. */
9455 if (*(str + strspn (str + 1, " )")) != ')')
9456 {
9457 char save;
9458
9459 save = *(str + len);
9460 *(str + len) = '\0';
9461 my_getExpression (ep, str);
9462 *(str + len) = save;
9463 }
9464 if (nest_level > 1)
9465 {
9466 /* blank out including the % sign. */
9467 char *p = strrchr (oldstr, '%');
9468 memset (p, ' ', str - p + len);
9469 str = oldstr;
9470 }
9471 else
9472 {
9473 expr_end = strchr (str, ')') + 1;
9474 }
9475 c = oldc;
9476 }
9477 else if (c == S_EX_NONE)
9478 {
9479 my_getExpression (ep, str);
9480 }
9481 else if (c == S_EX_REGISTER)
9482 {
9483 ep->X_op = O_constant;
9484 expr_end = str;
9485 ep->X_add_symbol = NULL;
9486 ep->X_op_symbol = NULL;
9487 ep->X_add_number = 0;
9488 }
9489 else
9490 {
9491 as_fatal(_("internal error"));
9492 }
9493
9494 if (nest_level <= 1)
9495 oldstr = NULL;
9496
9497 return c;
9498 }
9499
9500 static void
9501 my_getExpression (ep, str)
9502 expressionS *ep;
9503 char *str;
9504 {
9505 char *save_in;
9506 valueT val;
9507
9508 save_in = input_line_pointer;
9509 input_line_pointer = str;
9510 expression (ep);
9511 expr_end = input_line_pointer;
9512 input_line_pointer = save_in;
9513
9514 /* If we are in mips16 mode, and this is an expression based on `.',
9515 then we bump the value of the symbol by 1 since that is how other
9516 text symbols are handled. We don't bother to handle complex
9517 expressions, just `.' plus or minus a constant. */
9518 if (mips_opts.mips16
9519 && ep->X_op == O_symbol
9520 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9521 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9522 && symbol_get_frag (ep->X_add_symbol) == frag_now
9523 && symbol_constant_p (ep->X_add_symbol)
9524 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9525 S_SET_VALUE (ep->X_add_symbol, val + 1);
9526 }
9527
9528 /* Turn a string in input_line_pointer into a floating point constant
9529 of type TYPE, and store the appropriate bytes in *LITP. The number
9530 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9531 returned, or NULL on OK. */
9532
9533 char *
9534 md_atof (type, litP, sizeP)
9535 int type;
9536 char *litP;
9537 int *sizeP;
9538 {
9539 int prec;
9540 LITTLENUM_TYPE words[4];
9541 char *t;
9542 int i;
9543
9544 switch (type)
9545 {
9546 case 'f':
9547 prec = 2;
9548 break;
9549
9550 case 'd':
9551 prec = 4;
9552 break;
9553
9554 default:
9555 *sizeP = 0;
9556 return _("bad call to md_atof");
9557 }
9558
9559 t = atof_ieee (input_line_pointer, type, words);
9560 if (t)
9561 input_line_pointer = t;
9562
9563 *sizeP = prec * 2;
9564
9565 if (! target_big_endian)
9566 {
9567 for (i = prec - 1; i >= 0; i--)
9568 {
9569 md_number_to_chars (litP, (valueT) words[i], 2);
9570 litP += 2;
9571 }
9572 }
9573 else
9574 {
9575 for (i = 0; i < prec; i++)
9576 {
9577 md_number_to_chars (litP, (valueT) words[i], 2);
9578 litP += 2;
9579 }
9580 }
9581
9582 return NULL;
9583 }
9584
9585 void
9586 md_number_to_chars (buf, val, n)
9587 char *buf;
9588 valueT val;
9589 int n;
9590 {
9591 if (target_big_endian)
9592 number_to_chars_bigendian (buf, val, n);
9593 else
9594 number_to_chars_littleendian (buf, val, n);
9595 }
9596 \f
9597 #ifdef OBJ_ELF
9598 static int support_64bit_objects(void)
9599 {
9600 const char **list, **l;
9601
9602 list = bfd_target_list ();
9603 for (l = list; *l != NULL; l++)
9604 #ifdef TE_TMIPS
9605 /* This is traditional mips */
9606 if (strcmp (*l, "elf64-tradbigmips") == 0
9607 || strcmp (*l, "elf64-tradlittlemips") == 0)
9608 #else
9609 if (strcmp (*l, "elf64-bigmips") == 0
9610 || strcmp (*l, "elf64-littlemips") == 0)
9611 #endif
9612 break;
9613 free (list);
9614 return (*l != NULL);
9615 }
9616 #endif /* OBJ_ELF */
9617
9618 CONST char *md_shortopts = "nO::g::G:";
9619
9620 struct option md_longopts[] =
9621 {
9622 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9623 {"mips0", no_argument, NULL, OPTION_MIPS1},
9624 {"mips1", no_argument, NULL, OPTION_MIPS1},
9625 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9626 {"mips2", no_argument, NULL, OPTION_MIPS2},
9627 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9628 {"mips3", no_argument, NULL, OPTION_MIPS3},
9629 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9630 {"mips4", no_argument, NULL, OPTION_MIPS4},
9631 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9632 {"mips5", no_argument, NULL, OPTION_MIPS5},
9633 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9634 {"mips32", no_argument, NULL, OPTION_MIPS32},
9635 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9636 {"mips64", no_argument, NULL, OPTION_MIPS64},
9637 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9638 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9639 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9640 {"trap", no_argument, NULL, OPTION_TRAP},
9641 {"no-break", no_argument, NULL, OPTION_TRAP},
9642 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9643 {"break", no_argument, NULL, OPTION_BREAK},
9644 {"no-trap", no_argument, NULL, OPTION_BREAK},
9645 #define OPTION_EB (OPTION_MD_BASE + 11)
9646 {"EB", no_argument, NULL, OPTION_EB},
9647 #define OPTION_EL (OPTION_MD_BASE + 12)
9648 {"EL", no_argument, NULL, OPTION_EL},
9649 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9650 {"mips16", no_argument, NULL, OPTION_MIPS16},
9651 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9652 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9653 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9654 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9655 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9656 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9657 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9658 {"mfp32", no_argument, NULL, OPTION_FP32},
9659 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9660 {"mgp32", no_argument, NULL, OPTION_GP32},
9661 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9662 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9663 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9664 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9665 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9666 {"march", required_argument, NULL, OPTION_MARCH},
9667 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9668 {"mtune", required_argument, NULL, OPTION_MTUNE},
9669 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9670 {"mcpu", required_argument, NULL, OPTION_MCPU},
9671 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9672 {"m4650", no_argument, NULL, OPTION_M4650},
9673 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9674 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9675 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9676 {"m4010", no_argument, NULL, OPTION_M4010},
9677 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9678 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9679 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9680 {"m4100", no_argument, NULL, OPTION_M4100},
9681 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9682 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9683 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9684 {"m3900", no_argument, NULL, OPTION_M3900},
9685 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9686 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9687 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9688 {"mgp64", no_argument, NULL, OPTION_GP64},
9689 #ifdef OBJ_ELF
9690 #define OPTION_ELF_BASE (OPTION_MD_BASE + 33)
9691 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9692 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9693 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9694 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
9695 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
9696 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
9697 {"xgot", no_argument, NULL, OPTION_XGOT},
9698 #define OPTION_MABI (OPTION_ELF_BASE + 3)
9699 {"mabi", required_argument, NULL, OPTION_MABI},
9700 #define OPTION_32 (OPTION_ELF_BASE + 4)
9701 {"32", no_argument, NULL, OPTION_32},
9702 #define OPTION_N32 (OPTION_ELF_BASE + 5)
9703 {"n32", no_argument, NULL, OPTION_N32},
9704 #define OPTION_64 (OPTION_ELF_BASE + 6)
9705 {"64", no_argument, NULL, OPTION_64},
9706 #endif /* OBJ_ELF */
9707 {NULL, no_argument, NULL, 0}
9708 };
9709 size_t md_longopts_size = sizeof (md_longopts);
9710
9711 int
9712 md_parse_option (c, arg)
9713 int c;
9714 char *arg;
9715 {
9716 switch (c)
9717 {
9718 case OPTION_CONSTRUCT_FLOATS:
9719 mips_disable_float_construction = 0;
9720 break;
9721
9722 case OPTION_NO_CONSTRUCT_FLOATS:
9723 mips_disable_float_construction = 1;
9724 break;
9725
9726 case OPTION_TRAP:
9727 mips_trap = 1;
9728 break;
9729
9730 case OPTION_BREAK:
9731 mips_trap = 0;
9732 break;
9733
9734 case OPTION_EB:
9735 target_big_endian = 1;
9736 break;
9737
9738 case OPTION_EL:
9739 target_big_endian = 0;
9740 break;
9741
9742 case 'n':
9743 warn_nops = 1;
9744 break;
9745
9746 case 'O':
9747 if (arg && arg[1] == '0')
9748 mips_optimize = 1;
9749 else
9750 mips_optimize = 2;
9751 break;
9752
9753 case 'g':
9754 if (arg == NULL)
9755 mips_debug = 2;
9756 else
9757 mips_debug = atoi (arg);
9758 /* When the MIPS assembler sees -g or -g2, it does not do
9759 optimizations which limit full symbolic debugging. We take
9760 that to be equivalent to -O0. */
9761 if (mips_debug == 2)
9762 mips_optimize = 1;
9763 break;
9764
9765 case OPTION_MIPS1:
9766 mips_opts.isa = ISA_MIPS1;
9767 break;
9768
9769 case OPTION_MIPS2:
9770 mips_opts.isa = ISA_MIPS2;
9771 break;
9772
9773 case OPTION_MIPS3:
9774 mips_opts.isa = ISA_MIPS3;
9775 break;
9776
9777 case OPTION_MIPS4:
9778 mips_opts.isa = ISA_MIPS4;
9779 break;
9780
9781 case OPTION_MIPS5:
9782 mips_opts.isa = ISA_MIPS5;
9783 break;
9784
9785 case OPTION_MIPS32:
9786 mips_opts.isa = ISA_MIPS32;
9787 break;
9788
9789 case OPTION_MIPS64:
9790 mips_opts.isa = ISA_MIPS64;
9791 break;
9792
9793 case OPTION_MTUNE:
9794 case OPTION_MARCH:
9795 case OPTION_MCPU:
9796 {
9797 int cpu = CPU_UNKNOWN;
9798
9799 /* Identify the processor type. */
9800 if (strcasecmp (arg, "default") != 0)
9801 {
9802 const struct mips_cpu_info *ci;
9803
9804 ci = mips_cpu_info_from_name (arg);
9805 if (ci == NULL || ci->is_isa)
9806 {
9807 switch (c)
9808 {
9809 case OPTION_MTUNE:
9810 as_fatal (_("invalid architecture -mtune=%s"), arg);
9811 break;
9812 case OPTION_MARCH:
9813 as_fatal (_("invalid architecture -march=%s"), arg);
9814 break;
9815 case OPTION_MCPU:
9816 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9817 break;
9818 }
9819 }
9820 else
9821 cpu = ci->cpu;
9822 }
9823
9824 switch (c)
9825 {
9826 case OPTION_MTUNE:
9827 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9828 as_warn(_("A different -mtune= was already specified, is now "
9829 "-mtune=%s"), arg);
9830 mips_tune = cpu;
9831 break;
9832 case OPTION_MARCH:
9833 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9834 as_warn(_("A different -march= was already specified, is now "
9835 "-march=%s"), arg);
9836 mips_arch = cpu;
9837 break;
9838 case OPTION_MCPU:
9839 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9840 as_warn(_("A different -mcpu= was already specified, is now "
9841 "-mcpu=%s"), arg);
9842 mips_cpu = cpu;
9843 }
9844 }
9845 break;
9846
9847 case OPTION_M4650:
9848 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9849 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9850 as_warn(_("A different -march= or -mtune= was already specified, "
9851 "is now -m4650"));
9852 mips_arch = CPU_R4650;
9853 mips_tune = CPU_R4650;
9854 break;
9855
9856 case OPTION_NO_M4650:
9857 break;
9858
9859 case OPTION_M4010:
9860 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9861 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9862 as_warn(_("A different -march= or -mtune= was already specified, "
9863 "is now -m4010"));
9864 mips_arch = CPU_R4010;
9865 mips_tune = CPU_R4010;
9866 break;
9867
9868 case OPTION_NO_M4010:
9869 break;
9870
9871 case OPTION_M4100:
9872 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9873 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9874 as_warn(_("A different -march= or -mtune= was already specified, "
9875 "is now -m4100"));
9876 mips_arch = CPU_VR4100;
9877 mips_tune = CPU_VR4100;
9878 break;
9879
9880 case OPTION_NO_M4100:
9881 break;
9882
9883 case OPTION_M3900:
9884 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9885 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9886 as_warn(_("A different -march= or -mtune= was already specified, "
9887 "is now -m3900"));
9888 mips_arch = CPU_R3900;
9889 mips_tune = CPU_R3900;
9890 break;
9891
9892 case OPTION_NO_M3900:
9893 break;
9894
9895 case OPTION_MIPS16:
9896 mips_opts.mips16 = 1;
9897 mips_no_prev_insn (false);
9898 break;
9899
9900 case OPTION_NO_MIPS16:
9901 mips_opts.mips16 = 0;
9902 mips_no_prev_insn (false);
9903 break;
9904
9905 case OPTION_MEMBEDDED_PIC:
9906 mips_pic = EMBEDDED_PIC;
9907 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9908 {
9909 as_bad (_("-G may not be used with embedded PIC code"));
9910 return 0;
9911 }
9912 g_switch_value = 0x7fffffff;
9913 break;
9914
9915 #ifdef OBJ_ELF
9916 /* When generating ELF code, we permit -KPIC and -call_shared to
9917 select SVR4_PIC, and -non_shared to select no PIC. This is
9918 intended to be compatible with Irix 5. */
9919 case OPTION_CALL_SHARED:
9920 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9921 {
9922 as_bad (_("-call_shared is supported only for ELF format"));
9923 return 0;
9924 }
9925 mips_pic = SVR4_PIC;
9926 if (g_switch_seen && g_switch_value != 0)
9927 {
9928 as_bad (_("-G may not be used with SVR4 PIC code"));
9929 return 0;
9930 }
9931 g_switch_value = 0;
9932 break;
9933
9934 case OPTION_NON_SHARED:
9935 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9936 {
9937 as_bad (_("-non_shared is supported only for ELF format"));
9938 return 0;
9939 }
9940 mips_pic = NO_PIC;
9941 break;
9942
9943 /* The -xgot option tells the assembler to use 32 offsets when
9944 accessing the got in SVR4_PIC mode. It is for Irix
9945 compatibility. */
9946 case OPTION_XGOT:
9947 mips_big_got = 1;
9948 break;
9949 #endif /* OBJ_ELF */
9950
9951 case 'G':
9952 if (! USE_GLOBAL_POINTER_OPT)
9953 {
9954 as_bad (_("-G is not supported for this configuration"));
9955 return 0;
9956 }
9957 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9958 {
9959 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9960 return 0;
9961 }
9962 else
9963 g_switch_value = atoi (arg);
9964 g_switch_seen = 1;
9965 break;
9966
9967 #ifdef OBJ_ELF
9968 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
9969 and -mabi=64. */
9970 case OPTION_32:
9971 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9972 {
9973 as_bad (_("-32 is supported for ELF format only"));
9974 return 0;
9975 }
9976 mips_opts.abi = O32_ABI;
9977 break;
9978
9979 case OPTION_N32:
9980 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9981 {
9982 as_bad (_("-n32 is supported for ELF format only"));
9983 return 0;
9984 }
9985 mips_opts.abi = N32_ABI;
9986 break;
9987
9988 case OPTION_64:
9989 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9990 {
9991 as_bad (_("-64 is supported for ELF format only"));
9992 return 0;
9993 }
9994 mips_opts.abi = N64_ABI;
9995 if (! support_64bit_objects())
9996 as_fatal (_("No compiled in support for 64 bit object file format"));
9997 break;
9998 #endif /* OBJ_ELF */
9999
10000 case OPTION_GP32:
10001 file_mips_gp32 = 1;
10002 if (mips_opts.abi != O32_ABI)
10003 mips_opts.abi = NO_ABI;
10004 break;
10005
10006 case OPTION_GP64:
10007 file_mips_gp32 = 0;
10008 if (mips_opts.abi == O32_ABI)
10009 mips_opts.abi = NO_ABI;
10010 break;
10011
10012 case OPTION_FP32:
10013 file_mips_fp32 = 1;
10014 if (mips_opts.abi != O32_ABI)
10015 mips_opts.abi = NO_ABI;
10016 break;
10017
10018 #ifdef OBJ_ELF
10019 case OPTION_MABI:
10020 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10021 {
10022 as_bad (_("-mabi is supported for ELF format only"));
10023 return 0;
10024 }
10025 if (strcmp (arg, "32") == 0)
10026 mips_opts.abi = O32_ABI;
10027 else if (strcmp (arg, "o64") == 0)
10028 mips_opts.abi = O64_ABI;
10029 else if (strcmp (arg, "n32") == 0)
10030 mips_opts.abi = N32_ABI;
10031 else if (strcmp (arg, "64") == 0)
10032 {
10033 mips_opts.abi = N64_ABI;
10034 if (! support_64bit_objects())
10035 as_fatal (_("No compiled in support for 64 bit object file "
10036 "format"));
10037 }
10038 else if (strcmp (arg, "eabi") == 0)
10039 mips_opts.abi = EABI_ABI;
10040 else
10041 mips_opts.abi = NO_ABI;
10042 break;
10043 #endif /* OBJ_ELF */
10044
10045 case OPTION_M7000_HILO_FIX:
10046 mips_7000_hilo_fix = true;
10047 break;
10048
10049 case OPTION_NO_M7000_HILO_FIX:
10050 mips_7000_hilo_fix = false;
10051 break;
10052
10053 default:
10054 return 0;
10055 }
10056
10057 return 1;
10058 }
10059
10060 static void
10061 show (stream, string, col_p, first_p)
10062 FILE *stream;
10063 char *string;
10064 int *col_p;
10065 int *first_p;
10066 {
10067 if (*first_p)
10068 {
10069 fprintf (stream, "%24s", "");
10070 *col_p = 24;
10071 }
10072 else
10073 {
10074 fprintf (stream, ", ");
10075 *col_p += 2;
10076 }
10077
10078 if (*col_p + strlen (string) > 72)
10079 {
10080 fprintf (stream, "\n%24s", "");
10081 *col_p = 24;
10082 }
10083
10084 fprintf (stream, "%s", string);
10085 *col_p += strlen (string);
10086
10087 *first_p = 0;
10088 }
10089
10090 void
10091 md_show_usage (stream)
10092 FILE *stream;
10093 {
10094 int column, first;
10095
10096 fprintf (stream, _("\
10097 MIPS options:\n\
10098 -membedded-pic generate embedded position independent code\n\
10099 -EB generate big endian output\n\
10100 -EL generate little endian output\n\
10101 -g, -g2 do not remove unneeded NOPs or swap branches\n\
10102 -G NUM allow referencing objects up to NUM bytes\n\
10103 implicitly with the gp register [default 8]\n"));
10104 fprintf (stream, _("\
10105 -mips1 generate MIPS ISA I instructions\n\
10106 -mips2 generate MIPS ISA II instructions\n\
10107 -mips3 generate MIPS ISA III instructions\n\
10108 -mips4 generate MIPS ISA IV instructions\n\
10109 -mips5 generate MIPS ISA V instructions\n\
10110 -mips32 generate MIPS32 ISA instructions\n\
10111 -mips64 generate MIPS64 ISA instructions\n\
10112 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
10113
10114 first = 1;
10115
10116 show (stream, "2000", &column, &first);
10117 show (stream, "3000", &column, &first);
10118 show (stream, "3900", &column, &first);
10119 show (stream, "4000", &column, &first);
10120 show (stream, "4010", &column, &first);
10121 show (stream, "4100", &column, &first);
10122 show (stream, "4111", &column, &first);
10123 show (stream, "4300", &column, &first);
10124 show (stream, "4400", &column, &first);
10125 show (stream, "4600", &column, &first);
10126 show (stream, "4650", &column, &first);
10127 show (stream, "5000", &column, &first);
10128 show (stream, "5200", &column, &first);
10129 show (stream, "5230", &column, &first);
10130 show (stream, "5231", &column, &first);
10131 show (stream, "5261", &column, &first);
10132 show (stream, "5721", &column, &first);
10133 show (stream, "6000", &column, &first);
10134 show (stream, "8000", &column, &first);
10135 show (stream, "10000", &column, &first);
10136 show (stream, "12000", &column, &first);
10137 show (stream, "sb1", &column, &first);
10138 fputc ('\n', stream);
10139
10140 fprintf (stream, _("\
10141 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10142 -no-mCPU don't generate code specific to CPU.\n\
10143 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10144
10145 first = 1;
10146
10147 show (stream, "3900", &column, &first);
10148 show (stream, "4010", &column, &first);
10149 show (stream, "4100", &column, &first);
10150 show (stream, "4650", &column, &first);
10151 fputc ('\n', stream);
10152
10153 fprintf (stream, _("\
10154 -mips16 generate mips16 instructions\n\
10155 -no-mips16 do not generate mips16 instructions\n"));
10156 fprintf (stream, _("\
10157 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10158 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
10159 -O0 remove unneeded NOPs, do not swap branches\n\
10160 -O remove unneeded NOPs and swap branches\n\
10161 -n warn about NOPs generated from macros\n\
10162 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10163 --trap, --no-break trap exception on div by 0 and mult overflow\n\
10164 --break, --no-trap break exception on div by 0 and mult overflow\n"));
10165 #ifdef OBJ_ELF
10166 fprintf (stream, _("\
10167 -KPIC, -call_shared generate SVR4 position independent code\n\
10168 -non_shared do not generate position independent code\n\
10169 -xgot assume a 32 bit GOT\n\
10170 -mabi=ABI create ABI conformant object file for:\n"));
10171
10172 first = 1;
10173
10174 show (stream, "32", &column, &first);
10175 show (stream, "o64", &column, &first);
10176 show (stream, "n32", &column, &first);
10177 show (stream, "64", &column, &first);
10178 show (stream, "eabi", &column, &first);
10179
10180 fputc ('\n', stream);
10181
10182 fprintf (stream, _("\
10183 -32 create o32 ABI object file (default)\n\
10184 -n32 create n32 ABI object file\n\
10185 -64 create 64 ABI object file\n"));
10186 #endif
10187 }
10188 \f
10189 void
10190 mips_init_after_args ()
10191 {
10192 /* initialize opcodes */
10193 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10194 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10195 }
10196
10197 long
10198 md_pcrel_from (fixP)
10199 fixS *fixP;
10200 {
10201 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10202 && fixP->fx_addsy != (symbolS *) NULL
10203 && ! S_IS_DEFINED (fixP->fx_addsy))
10204 {
10205 /* This makes a branch to an undefined symbol be a branch to the
10206 current location. */
10207 if (mips_pic == EMBEDDED_PIC)
10208 return 4;
10209 else
10210 return 1;
10211 }
10212
10213 /* return the address of the delay slot */
10214 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10215 }
10216
10217 /* This is called before the symbol table is processed. In order to
10218 work with gcc when using mips-tfile, we must keep all local labels.
10219 However, in other cases, we want to discard them. If we were
10220 called with -g, but we didn't see any debugging information, it may
10221 mean that gcc is smuggling debugging information through to
10222 mips-tfile, in which case we must generate all local labels. */
10223
10224 void
10225 mips_frob_file_before_adjust ()
10226 {
10227 #ifndef NO_ECOFF_DEBUGGING
10228 if (ECOFF_DEBUGGING
10229 && mips_debug != 0
10230 && ! ecoff_debugging_seen)
10231 flag_keep_locals = 1;
10232 #endif
10233 }
10234
10235 /* Sort any unmatched HI16_S relocs so that they immediately precede
10236 the corresponding LO reloc. This is called before md_apply_fix3 and
10237 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10238 explicit use of the %hi modifier. */
10239
10240 void
10241 mips_frob_file ()
10242 {
10243 struct mips_hi_fixup *l;
10244
10245 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10246 {
10247 segment_info_type *seginfo;
10248 int pass;
10249
10250 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10251
10252 /* Check quickly whether the next fixup happens to be a matching
10253 %lo. */
10254 if (l->fixp->fx_next != NULL
10255 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10256 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10257 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10258 continue;
10259
10260 /* Look through the fixups for this segment for a matching %lo.
10261 When we find one, move the %hi just in front of it. We do
10262 this in two passes. In the first pass, we try to find a
10263 unique %lo. In the second pass, we permit multiple %hi
10264 relocs for a single %lo (this is a GNU extension). */
10265 seginfo = seg_info (l->seg);
10266 for (pass = 0; pass < 2; pass++)
10267 {
10268 fixS *f, *prev;
10269
10270 prev = NULL;
10271 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10272 {
10273 /* Check whether this is a %lo fixup which matches l->fixp. */
10274 if (f->fx_r_type == BFD_RELOC_LO16
10275 && f->fx_addsy == l->fixp->fx_addsy
10276 && f->fx_offset == l->fixp->fx_offset
10277 && (pass == 1
10278 || prev == NULL
10279 || prev->fx_r_type != BFD_RELOC_HI16_S
10280 || prev->fx_addsy != f->fx_addsy
10281 || prev->fx_offset != f->fx_offset))
10282 {
10283 fixS **pf;
10284
10285 /* Move l->fixp before f. */
10286 for (pf = &seginfo->fix_root;
10287 *pf != l->fixp;
10288 pf = &(*pf)->fx_next)
10289 assert (*pf != NULL);
10290
10291 *pf = l->fixp->fx_next;
10292
10293 l->fixp->fx_next = f;
10294 if (prev == NULL)
10295 seginfo->fix_root = l->fixp;
10296 else
10297 prev->fx_next = l->fixp;
10298
10299 break;
10300 }
10301
10302 prev = f;
10303 }
10304
10305 if (f != NULL)
10306 break;
10307
10308 #if 0 /* GCC code motion plus incomplete dead code elimination
10309 can leave a %hi without a %lo. */
10310 if (pass == 1)
10311 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10312 _("Unmatched %%hi reloc"));
10313 #endif
10314 }
10315 }
10316 }
10317
10318 /* When generating embedded PIC code we need to use a special
10319 relocation to represent the difference of two symbols in the .text
10320 section (switch tables use a difference of this sort). See
10321 include/coff/mips.h for details. This macro checks whether this
10322 fixup requires the special reloc. */
10323 #define SWITCH_TABLE(fixp) \
10324 ((fixp)->fx_r_type == BFD_RELOC_32 \
10325 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10326 && (fixp)->fx_addsy != NULL \
10327 && (fixp)->fx_subsy != NULL \
10328 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10329 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10330
10331 /* When generating embedded PIC code we must keep all PC relative
10332 relocations, in case the linker has to relax a call. We also need
10333 to keep relocations for switch table entries.
10334
10335 We may have combined relocations without symbols in the N32/N64 ABI.
10336 We have to prevent gas from dropping them. */
10337
10338 int
10339 mips_force_relocation (fixp)
10340 fixS *fixp;
10341 {
10342 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10343 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10344 return 1;
10345
10346 if (HAVE_NEWABI
10347 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10348 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10349 || fixp->fx_r_type == BFD_RELOC_HI16_S
10350 || fixp->fx_r_type == BFD_RELOC_LO16))
10351 return 1;
10352
10353 return (mips_pic == EMBEDDED_PIC
10354 && (fixp->fx_pcrel
10355 || SWITCH_TABLE (fixp)
10356 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10357 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10358 }
10359
10360 #ifdef OBJ_ELF
10361 static int
10362 mips_need_elf_addend_fixup (fixP)
10363 fixS *fixP;
10364 {
10365 return (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
10366 || ((S_IS_WEAK (fixP->fx_addsy)
10367 || S_IS_EXTERN (fixP->fx_addsy))
10368 && !S_IS_COMMON (fixP->fx_addsy))
10369 || (symbol_used_in_reloc_p (fixP->fx_addsy)
10370 && (((bfd_get_section_flags (stdoutput,
10371 S_GET_SEGMENT (fixP->fx_addsy))
10372 & SEC_LINK_ONCE) != 0)
10373 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10374 ".gnu.linkonce",
10375 sizeof (".gnu.linkonce") - 1))));
10376 }
10377 #endif
10378
10379 /* Apply a fixup to the object file. */
10380
10381 void
10382 md_apply_fix3 (fixP, valP, seg)
10383 fixS *fixP;
10384 valueT * valP;
10385 segT seg ATTRIBUTE_UNUSED;
10386 {
10387 unsigned char *buf;
10388 long insn;
10389 valueT value;
10390
10391 assert (fixP->fx_size == 4
10392 || fixP->fx_r_type == BFD_RELOC_16
10393 || fixP->fx_r_type == BFD_RELOC_32
10394 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10395 || fixP->fx_r_type == BFD_RELOC_HI16_S
10396 || fixP->fx_r_type == BFD_RELOC_LO16
10397 || fixP->fx_r_type == BFD_RELOC_GPREL16
10398 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10399 || fixP->fx_r_type == BFD_RELOC_GPREL32
10400 || fixP->fx_r_type == BFD_RELOC_64
10401 || fixP->fx_r_type == BFD_RELOC_CTOR
10402 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10403 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10404 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10405 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10406 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10407 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10408 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10409 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10410
10411 value = * valP;
10412
10413 /* If we aren't adjusting this fixup to be against the section
10414 symbol, we need to adjust the value. */
10415 #ifdef OBJ_ELF
10416 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10417 {
10418 if (mips_need_elf_addend_fixup (fixP))
10419 {
10420 valueT symval = S_GET_VALUE (fixP->fx_addsy);
10421
10422 value -= symval;
10423 if (value != 0 && ! fixP->fx_pcrel)
10424 {
10425 /* In this case, the bfd_install_relocation routine will
10426 incorrectly add the symbol value back in. We just want
10427 the addend to appear in the object file. */
10428 value -= symval;
10429
10430 /* Make sure the addend is still non-zero. If it became zero
10431 after the last operation, set it to a spurious value and
10432 subtract the same value from the object file's contents. */
10433 if (value == 0)
10434 {
10435 value = 8;
10436
10437 /* The in-place addends for LO16 relocations are signed;
10438 leave the matching HI16 in-place addends as zero. */
10439 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10440 {
10441 reloc_howto_type *howto;
10442 bfd_vma contents, mask, field;
10443
10444 howto = bfd_reloc_type_lookup (stdoutput,
10445 fixP->fx_r_type);
10446
10447 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10448 + fixP->fx_where,
10449 fixP->fx_size * 8,
10450 target_big_endian);
10451
10452 /* MASK has bits set where the relocation should go.
10453 FIELD is -value, shifted into the appropriate place
10454 for this relocation. */
10455 mask = 1 << (howto->bitsize - 1);
10456 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10457 field = (-value >> howto->rightshift) << howto->bitpos;
10458
10459 bfd_put_bits ((field & mask) | (contents & ~mask),
10460 fixP->fx_frag->fr_literal + fixP->fx_where,
10461 fixP->fx_size * 8,
10462 target_big_endian);
10463 }
10464 }
10465 }
10466 }
10467
10468 /* This code was generated using trial and error and so is
10469 fragile and not trustworthy. If you change it, you should
10470 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10471 they still pass. */
10472 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10473 {
10474 value += fixP->fx_frag->fr_address + fixP->fx_where;
10475
10476 /* BFD's REL handling, for MIPS, is _very_ weird.
10477 This gives the right results, but it can't possibly
10478 be the way things are supposed to work. */
10479 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10480 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10481 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10482 value += fixP->fx_frag->fr_address + fixP->fx_where;
10483 }
10484 }
10485 #endif
10486
10487 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
10488
10489 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10490 fixP->fx_done = 1;
10491
10492 switch (fixP->fx_r_type)
10493 {
10494 case BFD_RELOC_MIPS_JMP:
10495 case BFD_RELOC_MIPS_SHIFT5:
10496 case BFD_RELOC_MIPS_SHIFT6:
10497 case BFD_RELOC_MIPS_GOT_DISP:
10498 case BFD_RELOC_MIPS_GOT_PAGE:
10499 case BFD_RELOC_MIPS_GOT_OFST:
10500 case BFD_RELOC_MIPS_SUB:
10501 case BFD_RELOC_MIPS_INSERT_A:
10502 case BFD_RELOC_MIPS_INSERT_B:
10503 case BFD_RELOC_MIPS_DELETE:
10504 case BFD_RELOC_MIPS_HIGHEST:
10505 case BFD_RELOC_MIPS_HIGHER:
10506 case BFD_RELOC_MIPS_SCN_DISP:
10507 case BFD_RELOC_MIPS_REL16:
10508 case BFD_RELOC_MIPS_RELGOT:
10509 case BFD_RELOC_MIPS_JALR:
10510 case BFD_RELOC_HI16:
10511 case BFD_RELOC_HI16_S:
10512 case BFD_RELOC_GPREL16:
10513 case BFD_RELOC_MIPS_LITERAL:
10514 case BFD_RELOC_MIPS_CALL16:
10515 case BFD_RELOC_MIPS_GOT16:
10516 case BFD_RELOC_GPREL32:
10517 case BFD_RELOC_MIPS_GOT_HI16:
10518 case BFD_RELOC_MIPS_GOT_LO16:
10519 case BFD_RELOC_MIPS_CALL_HI16:
10520 case BFD_RELOC_MIPS_CALL_LO16:
10521 case BFD_RELOC_MIPS16_GPREL:
10522 if (fixP->fx_pcrel)
10523 as_bad_where (fixP->fx_file, fixP->fx_line,
10524 _("Invalid PC relative reloc"));
10525 /* Nothing needed to do. The value comes from the reloc entry */
10526 break;
10527
10528 case BFD_RELOC_MIPS16_JMP:
10529 /* We currently always generate a reloc against a symbol, which
10530 means that we don't want an addend even if the symbol is
10531 defined. */
10532 fixP->fx_addnumber = 0;
10533 break;
10534
10535 case BFD_RELOC_PCREL_HI16_S:
10536 /* The addend for this is tricky if it is internal, so we just
10537 do everything here rather than in bfd_install_relocation. */
10538 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10539 && !fixP->fx_done
10540 && value != 0)
10541 break;
10542 if (fixP->fx_addsy
10543 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10544 {
10545 /* For an external symbol adjust by the address to make it
10546 pcrel_offset. We use the address of the RELLO reloc
10547 which follows this one. */
10548 value += (fixP->fx_next->fx_frag->fr_address
10549 + fixP->fx_next->fx_where);
10550 }
10551 value = ((value + 0x8000) >> 16) & 0xffff;
10552 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10553 if (target_big_endian)
10554 buf += 2;
10555 md_number_to_chars (buf, value, 2);
10556 break;
10557
10558 case BFD_RELOC_PCREL_LO16:
10559 /* The addend for this is tricky if it is internal, so we just
10560 do everything here rather than in bfd_install_relocation. */
10561 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10562 && !fixP->fx_done
10563 && value != 0)
10564 break;
10565 if (fixP->fx_addsy
10566 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10567 value += fixP->fx_frag->fr_address + fixP->fx_where;
10568 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10569 if (target_big_endian)
10570 buf += 2;
10571 md_number_to_chars (buf, value, 2);
10572 break;
10573
10574 case BFD_RELOC_64:
10575 /* This is handled like BFD_RELOC_32, but we output a sign
10576 extended value if we are only 32 bits. */
10577 if (fixP->fx_done
10578 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10579 {
10580 if (8 <= sizeof (valueT))
10581 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10582 value, 8);
10583 else
10584 {
10585 long w1, w2;
10586 long hiv;
10587
10588 w1 = w2 = fixP->fx_where;
10589 if (target_big_endian)
10590 w1 += 4;
10591 else
10592 w2 += 4;
10593 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10594 if ((value & 0x80000000) != 0)
10595 hiv = 0xffffffff;
10596 else
10597 hiv = 0;
10598 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10599 }
10600 }
10601 break;
10602
10603 case BFD_RELOC_RVA:
10604 case BFD_RELOC_32:
10605 /* If we are deleting this reloc entry, we must fill in the
10606 value now. This can happen if we have a .word which is not
10607 resolved when it appears but is later defined. We also need
10608 to fill in the value if this is an embedded PIC switch table
10609 entry. */
10610 if (fixP->fx_done
10611 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10612 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10613 value, 4);
10614 break;
10615
10616 case BFD_RELOC_16:
10617 /* If we are deleting this reloc entry, we must fill in the
10618 value now. */
10619 assert (fixP->fx_size == 2);
10620 if (fixP->fx_done)
10621 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10622 value, 2);
10623 break;
10624
10625 case BFD_RELOC_LO16:
10626 /* When handling an embedded PIC switch statement, we can wind
10627 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10628 if (fixP->fx_done)
10629 {
10630 if (value + 0x8000 > 0xffff)
10631 as_bad_where (fixP->fx_file, fixP->fx_line,
10632 _("relocation overflow"));
10633 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10634 if (target_big_endian)
10635 buf += 2;
10636 md_number_to_chars (buf, value, 2);
10637 }
10638 break;
10639
10640 case BFD_RELOC_16_PCREL_S2:
10641 if ((value & 0x3) != 0)
10642 as_bad_where (fixP->fx_file, fixP->fx_line,
10643 _("Branch to odd address (%lx)"), (long) value);
10644
10645 /* Fall through. */
10646
10647 case BFD_RELOC_16_PCREL:
10648 /*
10649 * We need to save the bits in the instruction since fixup_segment()
10650 * might be deleting the relocation entry (i.e., a branch within
10651 * the current segment).
10652 */
10653 if (!fixP->fx_done && value != 0)
10654 break;
10655 /* If 'value' is zero, the remaining reloc code won't actually
10656 do the store, so it must be done here. This is probably
10657 a bug somewhere. */
10658 if (!fixP->fx_done)
10659 value -= fixP->fx_frag->fr_address + fixP->fx_where;
10660
10661 value = (offsetT) value >> 2;
10662
10663 /* update old instruction data */
10664 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10665 if (target_big_endian)
10666 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10667 else
10668 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10669
10670 if (value + 0x8000 <= 0xffff)
10671 insn |= value & 0xffff;
10672 else
10673 {
10674 /* The branch offset is too large. If this is an
10675 unconditional branch, and we are not generating PIC code,
10676 we can convert it to an absolute jump instruction. */
10677 if (mips_pic == NO_PIC
10678 && fixP->fx_done
10679 && fixP->fx_frag->fr_address >= text_section->vma
10680 && (fixP->fx_frag->fr_address
10681 < text_section->vma + text_section->_raw_size)
10682 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10683 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10684 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10685 {
10686 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10687 insn = 0x0c000000; /* jal */
10688 else
10689 insn = 0x08000000; /* j */
10690 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10691 fixP->fx_done = 0;
10692 fixP->fx_addsy = section_symbol (text_section);
10693 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10694 }
10695 else
10696 {
10697 /* FIXME. It would be possible in principle to handle
10698 conditional branches which overflow. They could be
10699 transformed into a branch around a jump. This would
10700 require setting up variant frags for each different
10701 branch type. The native MIPS assembler attempts to
10702 handle these cases, but it appears to do it
10703 incorrectly. */
10704 as_bad_where (fixP->fx_file, fixP->fx_line,
10705 _("Branch out of range"));
10706 }
10707 }
10708
10709 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10710 break;
10711
10712 case BFD_RELOC_VTABLE_INHERIT:
10713 fixP->fx_done = 0;
10714 if (fixP->fx_addsy
10715 && !S_IS_DEFINED (fixP->fx_addsy)
10716 && !S_IS_WEAK (fixP->fx_addsy))
10717 S_SET_WEAK (fixP->fx_addsy);
10718 break;
10719
10720 case BFD_RELOC_VTABLE_ENTRY:
10721 fixP->fx_done = 0;
10722 break;
10723
10724 default:
10725 internalError ();
10726 }
10727 }
10728
10729 #if 0
10730 void
10731 printInsn (oc)
10732 unsigned long oc;
10733 {
10734 const struct mips_opcode *p;
10735 int treg, sreg, dreg, shamt;
10736 short imm;
10737 const char *args;
10738 int i;
10739
10740 for (i = 0; i < NUMOPCODES; ++i)
10741 {
10742 p = &mips_opcodes[i];
10743 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10744 {
10745 printf ("%08lx %s\t", oc, p->name);
10746 treg = (oc >> 16) & 0x1f;
10747 sreg = (oc >> 21) & 0x1f;
10748 dreg = (oc >> 11) & 0x1f;
10749 shamt = (oc >> 6) & 0x1f;
10750 imm = oc;
10751 for (args = p->args;; ++args)
10752 {
10753 switch (*args)
10754 {
10755 case '\0':
10756 printf ("\n");
10757 break;
10758
10759 case ',':
10760 case '(':
10761 case ')':
10762 printf ("%c", *args);
10763 continue;
10764
10765 case 'r':
10766 assert (treg == sreg);
10767 printf ("$%d,$%d", treg, sreg);
10768 continue;
10769
10770 case 'd':
10771 case 'G':
10772 printf ("$%d", dreg);
10773 continue;
10774
10775 case 't':
10776 case 'E':
10777 printf ("$%d", treg);
10778 continue;
10779
10780 case 'k':
10781 printf ("0x%x", treg);
10782 continue;
10783
10784 case 'b':
10785 case 's':
10786 printf ("$%d", sreg);
10787 continue;
10788
10789 case 'a':
10790 printf ("0x%08lx", oc & 0x1ffffff);
10791 continue;
10792
10793 case 'i':
10794 case 'j':
10795 case 'o':
10796 case 'u':
10797 printf ("%d", imm);
10798 continue;
10799
10800 case '<':
10801 case '>':
10802 printf ("$%d", shamt);
10803 continue;
10804
10805 default:
10806 internalError ();
10807 }
10808 break;
10809 }
10810 return;
10811 }
10812 }
10813 printf (_("%08lx UNDEFINED\n"), oc);
10814 }
10815 #endif
10816
10817 static symbolS *
10818 get_symbol ()
10819 {
10820 int c;
10821 char *name;
10822 symbolS *p;
10823
10824 name = input_line_pointer;
10825 c = get_symbol_end ();
10826 p = (symbolS *) symbol_find_or_make (name);
10827 *input_line_pointer = c;
10828 return p;
10829 }
10830
10831 /* Align the current frag to a given power of two. The MIPS assembler
10832 also automatically adjusts any preceding label. */
10833
10834 static void
10835 mips_align (to, fill, label)
10836 int to;
10837 int fill;
10838 symbolS *label;
10839 {
10840 mips_emit_delays (false);
10841 frag_align (to, fill, 0);
10842 record_alignment (now_seg, to);
10843 if (label != NULL)
10844 {
10845 assert (S_GET_SEGMENT (label) == now_seg);
10846 symbol_set_frag (label, frag_now);
10847 S_SET_VALUE (label, (valueT) frag_now_fix ());
10848 }
10849 }
10850
10851 /* Align to a given power of two. .align 0 turns off the automatic
10852 alignment used by the data creating pseudo-ops. */
10853
10854 static void
10855 s_align (x)
10856 int x ATTRIBUTE_UNUSED;
10857 {
10858 register int temp;
10859 register long temp_fill;
10860 long max_alignment = 15;
10861
10862 /*
10863
10864 o Note that the assembler pulls down any immediately preceeding label
10865 to the aligned address.
10866 o It's not documented but auto alignment is reinstated by
10867 a .align pseudo instruction.
10868 o Note also that after auto alignment is turned off the mips assembler
10869 issues an error on attempt to assemble an improperly aligned data item.
10870 We don't.
10871
10872 */
10873
10874 temp = get_absolute_expression ();
10875 if (temp > max_alignment)
10876 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10877 else if (temp < 0)
10878 {
10879 as_warn (_("Alignment negative: 0 assumed."));
10880 temp = 0;
10881 }
10882 if (*input_line_pointer == ',')
10883 {
10884 input_line_pointer++;
10885 temp_fill = get_absolute_expression ();
10886 }
10887 else
10888 temp_fill = 0;
10889 if (temp)
10890 {
10891 auto_align = 1;
10892 mips_align (temp, (int) temp_fill,
10893 insn_labels != NULL ? insn_labels->label : NULL);
10894 }
10895 else
10896 {
10897 auto_align = 0;
10898 }
10899
10900 demand_empty_rest_of_line ();
10901 }
10902
10903 void
10904 mips_flush_pending_output ()
10905 {
10906 mips_emit_delays (false);
10907 mips_clear_insn_labels ();
10908 }
10909
10910 static void
10911 s_change_sec (sec)
10912 int sec;
10913 {
10914 segT seg;
10915
10916 /* When generating embedded PIC code, we only use the .text, .lit8,
10917 .sdata and .sbss sections. We change the .data and .rdata
10918 pseudo-ops to use .sdata. */
10919 if (mips_pic == EMBEDDED_PIC
10920 && (sec == 'd' || sec == 'r'))
10921 sec = 's';
10922
10923 #ifdef OBJ_ELF
10924 /* The ELF backend needs to know that we are changing sections, so
10925 that .previous works correctly. We could do something like check
10926 for an obj_section_change_hook macro, but that might be confusing
10927 as it would not be appropriate to use it in the section changing
10928 functions in read.c, since obj-elf.c intercepts those. FIXME:
10929 This should be cleaner, somehow. */
10930 obj_elf_section_change_hook ();
10931 #endif
10932
10933 mips_emit_delays (false);
10934 switch (sec)
10935 {
10936 case 't':
10937 s_text (0);
10938 break;
10939 case 'd':
10940 s_data (0);
10941 break;
10942 case 'b':
10943 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10944 demand_empty_rest_of_line ();
10945 break;
10946
10947 case 'r':
10948 if (USE_GLOBAL_POINTER_OPT)
10949 {
10950 seg = subseg_new (RDATA_SECTION_NAME,
10951 (subsegT) get_absolute_expression ());
10952 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10953 {
10954 bfd_set_section_flags (stdoutput, seg,
10955 (SEC_ALLOC
10956 | SEC_LOAD
10957 | SEC_READONLY
10958 | SEC_RELOC
10959 | SEC_DATA));
10960 if (strcmp (TARGET_OS, "elf") != 0)
10961 record_alignment (seg, 4);
10962 }
10963 demand_empty_rest_of_line ();
10964 }
10965 else
10966 {
10967 as_bad (_("No read only data section in this object file format"));
10968 demand_empty_rest_of_line ();
10969 return;
10970 }
10971 break;
10972
10973 case 's':
10974 if (USE_GLOBAL_POINTER_OPT)
10975 {
10976 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10977 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10978 {
10979 bfd_set_section_flags (stdoutput, seg,
10980 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10981 | SEC_DATA);
10982 if (strcmp (TARGET_OS, "elf") != 0)
10983 record_alignment (seg, 4);
10984 }
10985 demand_empty_rest_of_line ();
10986 break;
10987 }
10988 else
10989 {
10990 as_bad (_("Global pointers not supported; recompile -G 0"));
10991 demand_empty_rest_of_line ();
10992 return;
10993 }
10994 }
10995
10996 auto_align = 1;
10997 }
10998
10999 void
11000 mips_enable_auto_align ()
11001 {
11002 auto_align = 1;
11003 }
11004
11005 static void
11006 s_cons (log_size)
11007 int log_size;
11008 {
11009 symbolS *label;
11010
11011 label = insn_labels != NULL ? insn_labels->label : NULL;
11012 mips_emit_delays (false);
11013 if (log_size > 0 && auto_align)
11014 mips_align (log_size, 0, label);
11015 mips_clear_insn_labels ();
11016 cons (1 << log_size);
11017 }
11018
11019 static void
11020 s_float_cons (type)
11021 int type;
11022 {
11023 symbolS *label;
11024
11025 label = insn_labels != NULL ? insn_labels->label : NULL;
11026
11027 mips_emit_delays (false);
11028
11029 if (auto_align)
11030 {
11031 if (type == 'd')
11032 mips_align (3, 0, label);
11033 else
11034 mips_align (2, 0, label);
11035 }
11036
11037 mips_clear_insn_labels ();
11038
11039 float_cons (type);
11040 }
11041
11042 /* Handle .globl. We need to override it because on Irix 5 you are
11043 permitted to say
11044 .globl foo .text
11045 where foo is an undefined symbol, to mean that foo should be
11046 considered to be the address of a function. */
11047
11048 static void
11049 s_mips_globl (x)
11050 int x ATTRIBUTE_UNUSED;
11051 {
11052 char *name;
11053 int c;
11054 symbolS *symbolP;
11055 flagword flag;
11056
11057 name = input_line_pointer;
11058 c = get_symbol_end ();
11059 symbolP = symbol_find_or_make (name);
11060 *input_line_pointer = c;
11061 SKIP_WHITESPACE ();
11062
11063 /* On Irix 5, every global symbol that is not explicitly labelled as
11064 being a function is apparently labelled as being an object. */
11065 flag = BSF_OBJECT;
11066
11067 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11068 {
11069 char *secname;
11070 asection *sec;
11071
11072 secname = input_line_pointer;
11073 c = get_symbol_end ();
11074 sec = bfd_get_section_by_name (stdoutput, secname);
11075 if (sec == NULL)
11076 as_bad (_("%s: no such section"), secname);
11077 *input_line_pointer = c;
11078
11079 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11080 flag = BSF_FUNCTION;
11081 }
11082
11083 symbol_get_bfdsym (symbolP)->flags |= flag;
11084
11085 S_SET_EXTERNAL (symbolP);
11086 demand_empty_rest_of_line ();
11087 }
11088
11089 static void
11090 s_option (x)
11091 int x ATTRIBUTE_UNUSED;
11092 {
11093 char *opt;
11094 char c;
11095
11096 opt = input_line_pointer;
11097 c = get_symbol_end ();
11098
11099 if (*opt == 'O')
11100 {
11101 /* FIXME: What does this mean? */
11102 }
11103 else if (strncmp (opt, "pic", 3) == 0)
11104 {
11105 int i;
11106
11107 i = atoi (opt + 3);
11108 if (i == 0)
11109 mips_pic = NO_PIC;
11110 else if (i == 2)
11111 mips_pic = SVR4_PIC;
11112 else
11113 as_bad (_(".option pic%d not supported"), i);
11114
11115 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11116 {
11117 if (g_switch_seen && g_switch_value != 0)
11118 as_warn (_("-G may not be used with SVR4 PIC code"));
11119 g_switch_value = 0;
11120 bfd_set_gp_size (stdoutput, 0);
11121 }
11122 }
11123 else
11124 as_warn (_("Unrecognized option \"%s\""), opt);
11125
11126 *input_line_pointer = c;
11127 demand_empty_rest_of_line ();
11128 }
11129
11130 /* This structure is used to hold a stack of .set values. */
11131
11132 struct mips_option_stack
11133 {
11134 struct mips_option_stack *next;
11135 struct mips_set_options options;
11136 };
11137
11138 static struct mips_option_stack *mips_opts_stack;
11139
11140 /* Handle the .set pseudo-op. */
11141
11142 static void
11143 s_mipsset (x)
11144 int x ATTRIBUTE_UNUSED;
11145 {
11146 char *name = input_line_pointer, ch;
11147
11148 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11149 input_line_pointer++;
11150 ch = *input_line_pointer;
11151 *input_line_pointer = '\0';
11152
11153 if (strcmp (name, "reorder") == 0)
11154 {
11155 if (mips_opts.noreorder && prev_nop_frag != NULL)
11156 {
11157 /* If we still have pending nops, we can discard them. The
11158 usual nop handling will insert any that are still
11159 needed. */
11160 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11161 * (mips_opts.mips16 ? 2 : 4));
11162 prev_nop_frag = NULL;
11163 }
11164 mips_opts.noreorder = 0;
11165 }
11166 else if (strcmp (name, "noreorder") == 0)
11167 {
11168 mips_emit_delays (true);
11169 mips_opts.noreorder = 1;
11170 mips_any_noreorder = 1;
11171 }
11172 else if (strcmp (name, "at") == 0)
11173 {
11174 mips_opts.noat = 0;
11175 }
11176 else if (strcmp (name, "noat") == 0)
11177 {
11178 mips_opts.noat = 1;
11179 }
11180 else if (strcmp (name, "macro") == 0)
11181 {
11182 mips_opts.warn_about_macros = 0;
11183 }
11184 else if (strcmp (name, "nomacro") == 0)
11185 {
11186 if (mips_opts.noreorder == 0)
11187 as_bad (_("`noreorder' must be set before `nomacro'"));
11188 mips_opts.warn_about_macros = 1;
11189 }
11190 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11191 {
11192 mips_opts.nomove = 0;
11193 }
11194 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11195 {
11196 mips_opts.nomove = 1;
11197 }
11198 else if (strcmp (name, "bopt") == 0)
11199 {
11200 mips_opts.nobopt = 0;
11201 }
11202 else if (strcmp (name, "nobopt") == 0)
11203 {
11204 mips_opts.nobopt = 1;
11205 }
11206 else if (strcmp (name, "mips16") == 0
11207 || strcmp (name, "MIPS-16") == 0)
11208 mips_opts.mips16 = 1;
11209 else if (strcmp (name, "nomips16") == 0
11210 || strcmp (name, "noMIPS-16") == 0)
11211 mips_opts.mips16 = 0;
11212 else if (strncmp (name, "mips", 4) == 0)
11213 {
11214 int isa;
11215
11216 /* Permit the user to change the ISA on the fly. Needless to
11217 say, misuse can cause serious problems. */
11218 isa = atoi (name + 4);
11219 switch (isa)
11220 {
11221 case 0:
11222 mips_opts.gp32 = file_mips_gp32;
11223 mips_opts.fp32 = file_mips_fp32;
11224 mips_opts.abi = file_mips_abi;
11225 break;
11226 case 1:
11227 case 2:
11228 case 32:
11229 mips_opts.gp32 = 1;
11230 mips_opts.fp32 = 1;
11231 break;
11232 case 3:
11233 case 4:
11234 case 5:
11235 case 64:
11236 /* Loosen ABI register width restriction. */
11237 if (mips_opts.abi == O32_ABI)
11238 mips_opts.abi = NO_ABI;
11239 mips_opts.gp32 = 0;
11240 mips_opts.fp32 = 0;
11241 break;
11242 default:
11243 as_bad (_("unknown ISA level %s"), name + 4);
11244 break;
11245 }
11246
11247 switch (isa)
11248 {
11249 case 0: mips_opts.isa = file_mips_isa; break;
11250 case 1: mips_opts.isa = ISA_MIPS1; break;
11251 case 2: mips_opts.isa = ISA_MIPS2; break;
11252 case 3: mips_opts.isa = ISA_MIPS3; break;
11253 case 4: mips_opts.isa = ISA_MIPS4; break;
11254 case 5: mips_opts.isa = ISA_MIPS5; break;
11255 case 32: mips_opts.isa = ISA_MIPS32; break;
11256 case 64: mips_opts.isa = ISA_MIPS64; break;
11257 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11258 }
11259 }
11260 else if (strcmp (name, "autoextend") == 0)
11261 mips_opts.noautoextend = 0;
11262 else if (strcmp (name, "noautoextend") == 0)
11263 mips_opts.noautoextend = 1;
11264 else if (strcmp (name, "push") == 0)
11265 {
11266 struct mips_option_stack *s;
11267
11268 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11269 s->next = mips_opts_stack;
11270 s->options = mips_opts;
11271 mips_opts_stack = s;
11272 }
11273 else if (strcmp (name, "pop") == 0)
11274 {
11275 struct mips_option_stack *s;
11276
11277 s = mips_opts_stack;
11278 if (s == NULL)
11279 as_bad (_(".set pop with no .set push"));
11280 else
11281 {
11282 /* If we're changing the reorder mode we need to handle
11283 delay slots correctly. */
11284 if (s->options.noreorder && ! mips_opts.noreorder)
11285 mips_emit_delays (true);
11286 else if (! s->options.noreorder && mips_opts.noreorder)
11287 {
11288 if (prev_nop_frag != NULL)
11289 {
11290 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11291 * (mips_opts.mips16 ? 2 : 4));
11292 prev_nop_frag = NULL;
11293 }
11294 }
11295
11296 mips_opts = s->options;
11297 mips_opts_stack = s->next;
11298 free (s);
11299 }
11300 }
11301 else
11302 {
11303 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11304 }
11305 *input_line_pointer = ch;
11306 demand_empty_rest_of_line ();
11307 }
11308
11309 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11310 .option pic2. It means to generate SVR4 PIC calls. */
11311
11312 static void
11313 s_abicalls (ignore)
11314 int ignore ATTRIBUTE_UNUSED;
11315 {
11316 mips_pic = SVR4_PIC;
11317 if (USE_GLOBAL_POINTER_OPT)
11318 {
11319 if (g_switch_seen && g_switch_value != 0)
11320 as_warn (_("-G may not be used with SVR4 PIC code"));
11321 g_switch_value = 0;
11322 }
11323 bfd_set_gp_size (stdoutput, 0);
11324 demand_empty_rest_of_line ();
11325 }
11326
11327 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11328 PIC code. It sets the $gp register for the function based on the
11329 function address, which is in the register named in the argument.
11330 This uses a relocation against _gp_disp, which is handled specially
11331 by the linker. The result is:
11332 lui $gp,%hi(_gp_disp)
11333 addiu $gp,$gp,%lo(_gp_disp)
11334 addu $gp,$gp,.cpload argument
11335 The .cpload argument is normally $25 == $t9. */
11336
11337 static void
11338 s_cpload (ignore)
11339 int ignore ATTRIBUTE_UNUSED;
11340 {
11341 expressionS ex;
11342 int icnt = 0;
11343
11344 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11345 .cpload is ignored. */
11346 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11347 {
11348 s_ignore (0);
11349 return;
11350 }
11351
11352 /* .cpload should be in a .set noreorder section. */
11353 if (mips_opts.noreorder == 0)
11354 as_warn (_(".cpload not in noreorder section"));
11355
11356 ex.X_op = O_symbol;
11357 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11358 ex.X_op_symbol = NULL;
11359 ex.X_add_number = 0;
11360
11361 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11362 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11363
11364 macro_build_lui (NULL, &icnt, &ex, GP);
11365 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11366 (int) BFD_RELOC_LO16);
11367
11368 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11369 GP, GP, tc_get_register (0));
11370
11371 demand_empty_rest_of_line ();
11372 }
11373
11374 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11375 .cpsetup $reg1, offset|$reg2, label
11376
11377 If offset is given, this results in:
11378 sd $gp, offset($sp)
11379 lui $gp, %hi(%neg(%gp_rel(label)))
11380 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11381 addu $gp, $gp, $reg1
11382
11383 If $reg2 is given, this results in:
11384 daddu $reg2, $gp, $0
11385 lui $gp, %hi(%neg(%gp_rel(label)))
11386 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11387 addu $gp, $gp, $reg1
11388 */
11389 static void
11390 s_cpsetup (ignore)
11391 int ignore ATTRIBUTE_UNUSED;
11392 {
11393 expressionS ex_off;
11394 expressionS ex_sym;
11395 int reg1;
11396 int icnt = 0;
11397 char *sym;
11398
11399 /* If we are not generating SVR4 PIC code, .cpload is ignored.
11400 We also need NewABI support. */
11401 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11402 {
11403 s_ignore (0);
11404 return;
11405 }
11406
11407 reg1 = tc_get_register (0);
11408 SKIP_WHITESPACE ();
11409 if (*input_line_pointer != ',')
11410 {
11411 as_bad (_("missing argument separator ',' for .cpsetup"));
11412 return;
11413 }
11414 else
11415 input_line_pointer++;
11416 SKIP_WHITESPACE ();
11417 if (*input_line_pointer == '$')
11418 mips_cpreturn_register = tc_get_register (0);
11419 else
11420 mips_cpreturn_offset = get_absolute_expression ();
11421 SKIP_WHITESPACE ();
11422 if (*input_line_pointer != ',')
11423 {
11424 as_bad (_("missing argument separator ',' for .cpsetup"));
11425 return;
11426 }
11427 else
11428 input_line_pointer++;
11429 SKIP_WHITESPACE ();
11430 sym = input_line_pointer;
11431 while (ISALNUM (*input_line_pointer))
11432 input_line_pointer++;
11433 *input_line_pointer = 0;
11434
11435 ex_sym.X_op = O_symbol;
11436 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11437 ex_sym.X_op_symbol = NULL;
11438 ex_sym.X_add_number = 0;
11439
11440 if (mips_cpreturn_register == -1)
11441 {
11442 ex_off.X_op = O_constant;
11443 ex_off.X_add_symbol = NULL;
11444 ex_off.X_op_symbol = NULL;
11445 ex_off.X_add_number = mips_cpreturn_offset;
11446
11447 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11448 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11449 }
11450 else
11451 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11452 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11453
11454 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11455 (int) BFD_RELOC_GPREL16);
11456 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11457 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11458 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11459 NULL, 0, 0, BFD_RELOC_HI16_S);
11460 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11461 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11462 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11463 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11464 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11465 NULL, 0, 0, BFD_RELOC_LO16);
11466 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11467 "d,v,t", mips_gp_register, mips_gp_register, reg1);
11468
11469 demand_empty_rest_of_line ();
11470 }
11471
11472 static void
11473 s_cplocal (ignore)
11474 int ignore ATTRIBUTE_UNUSED;
11475 {
11476 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11477 .cplocal is ignored. */
11478 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11479 {
11480 s_ignore (0);
11481 return;
11482 }
11483
11484 mips_gp_register = tc_get_register (0);
11485 }
11486
11487 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11488 offset from $sp. The offset is remembered, and after making a PIC
11489 call $gp is restored from that location. */
11490
11491 static void
11492 s_cprestore (ignore)
11493 int ignore ATTRIBUTE_UNUSED;
11494 {
11495 expressionS ex;
11496 int icnt = 0;
11497
11498 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11499 .cprestore is ignored. */
11500 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11501 {
11502 s_ignore (0);
11503 return;
11504 }
11505
11506 mips_cprestore_offset = get_absolute_expression ();
11507 mips_cprestore_valid = 1;
11508
11509 ex.X_op = O_constant;
11510 ex.X_add_symbol = NULL;
11511 ex.X_op_symbol = NULL;
11512 ex.X_add_number = mips_cprestore_offset;
11513
11514 macro_build ((char *) NULL, &icnt, &ex,
11515 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11516 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11517
11518 demand_empty_rest_of_line ();
11519 }
11520
11521 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11522 was given in the preceeding .gpsetup, it results in:
11523 ld $gp, offset($sp)
11524
11525 If a register $reg2 was given there, it results in:
11526 daddiu $gp, $gp, $reg2
11527 */
11528 static void
11529 s_cpreturn (ignore)
11530 int ignore ATTRIBUTE_UNUSED;
11531 {
11532 expressionS ex;
11533 int icnt = 0;
11534
11535 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11536 We also need NewABI support. */
11537 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11538 {
11539 s_ignore (0);
11540 return;
11541 }
11542
11543 if (mips_cpreturn_register == -1)
11544 {
11545 ex.X_op = O_constant;
11546 ex.X_add_symbol = NULL;
11547 ex.X_op_symbol = NULL;
11548 ex.X_add_number = mips_cpreturn_offset;
11549
11550 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11551 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11552 }
11553 else
11554 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11555 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11556
11557 demand_empty_rest_of_line ();
11558 }
11559
11560 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11561 code. It sets the offset to use in gp_rel relocations. */
11562
11563 static void
11564 s_gpvalue (ignore)
11565 int ignore ATTRIBUTE_UNUSED;
11566 {
11567 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11568 We also need NewABI support. */
11569 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11570 {
11571 s_ignore (0);
11572 return;
11573 }
11574
11575 mips_cpreturn_offset = get_absolute_expression ();
11576
11577 demand_empty_rest_of_line ();
11578 }
11579
11580 /* Handle the .gpword pseudo-op. This is used when generating PIC
11581 code. It generates a 32 bit GP relative reloc. */
11582
11583 static void
11584 s_gpword (ignore)
11585 int ignore ATTRIBUTE_UNUSED;
11586 {
11587 symbolS *label;
11588 expressionS ex;
11589 char *p;
11590
11591 /* When not generating PIC code, this is treated as .word. */
11592 if (mips_pic != SVR4_PIC)
11593 {
11594 s_cons (2);
11595 return;
11596 }
11597
11598 label = insn_labels != NULL ? insn_labels->label : NULL;
11599 mips_emit_delays (true);
11600 if (auto_align)
11601 mips_align (2, 0, label);
11602 mips_clear_insn_labels ();
11603
11604 expression (&ex);
11605
11606 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11607 {
11608 as_bad (_("Unsupported use of .gpword"));
11609 ignore_rest_of_line ();
11610 }
11611
11612 p = frag_more (4);
11613 md_number_to_chars (p, (valueT) 0, 4);
11614 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11615 BFD_RELOC_GPREL32);
11616
11617 demand_empty_rest_of_line ();
11618 }
11619
11620 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11621 tables in SVR4 PIC code. */
11622
11623 static void
11624 s_cpadd (ignore)
11625 int ignore ATTRIBUTE_UNUSED;
11626 {
11627 int icnt = 0;
11628 int reg;
11629
11630 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11631 code. */
11632 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11633 {
11634 s_ignore (0);
11635 return;
11636 }
11637
11638 /* Add $gp to the register named as an argument. */
11639 reg = tc_get_register (0);
11640 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11641 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11642 "d,v,t", reg, reg, GP);
11643
11644 demand_empty_rest_of_line ();
11645 }
11646
11647 /* Handle the .insn pseudo-op. This marks instruction labels in
11648 mips16 mode. This permits the linker to handle them specially,
11649 such as generating jalx instructions when needed. We also make
11650 them odd for the duration of the assembly, in order to generate the
11651 right sort of code. We will make them even in the adjust_symtab
11652 routine, while leaving them marked. This is convenient for the
11653 debugger and the disassembler. The linker knows to make them odd
11654 again. */
11655
11656 static void
11657 s_insn (ignore)
11658 int ignore ATTRIBUTE_UNUSED;
11659 {
11660 if (mips_opts.mips16)
11661 mips16_mark_labels ();
11662
11663 demand_empty_rest_of_line ();
11664 }
11665
11666 /* Handle a .stabn directive. We need these in order to mark a label
11667 as being a mips16 text label correctly. Sometimes the compiler
11668 will emit a label, followed by a .stabn, and then switch sections.
11669 If the label and .stabn are in mips16 mode, then the label is
11670 really a mips16 text label. */
11671
11672 static void
11673 s_mips_stab (type)
11674 int type;
11675 {
11676 if (type == 'n' && mips_opts.mips16)
11677 mips16_mark_labels ();
11678
11679 s_stab (type);
11680 }
11681
11682 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11683 */
11684
11685 static void
11686 s_mips_weakext (ignore)
11687 int ignore ATTRIBUTE_UNUSED;
11688 {
11689 char *name;
11690 int c;
11691 symbolS *symbolP;
11692 expressionS exp;
11693
11694 name = input_line_pointer;
11695 c = get_symbol_end ();
11696 symbolP = symbol_find_or_make (name);
11697 S_SET_WEAK (symbolP);
11698 *input_line_pointer = c;
11699
11700 SKIP_WHITESPACE ();
11701
11702 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11703 {
11704 if (S_IS_DEFINED (symbolP))
11705 {
11706 as_bad ("ignoring attempt to redefine symbol %s",
11707 S_GET_NAME (symbolP));
11708 ignore_rest_of_line ();
11709 return;
11710 }
11711
11712 if (*input_line_pointer == ',')
11713 {
11714 ++input_line_pointer;
11715 SKIP_WHITESPACE ();
11716 }
11717
11718 expression (&exp);
11719 if (exp.X_op != O_symbol)
11720 {
11721 as_bad ("bad .weakext directive");
11722 ignore_rest_of_line();
11723 return;
11724 }
11725 symbol_set_value_expression (symbolP, &exp);
11726 }
11727
11728 demand_empty_rest_of_line ();
11729 }
11730
11731 /* Parse a register string into a number. Called from the ECOFF code
11732 to parse .frame. The argument is non-zero if this is the frame
11733 register, so that we can record it in mips_frame_reg. */
11734
11735 int
11736 tc_get_register (frame)
11737 int frame;
11738 {
11739 int reg;
11740
11741 SKIP_WHITESPACE ();
11742 if (*input_line_pointer++ != '$')
11743 {
11744 as_warn (_("expected `$'"));
11745 reg = 0;
11746 }
11747 else if (ISDIGIT (*input_line_pointer))
11748 {
11749 reg = get_absolute_expression ();
11750 if (reg < 0 || reg >= 32)
11751 {
11752 as_warn (_("Bad register number"));
11753 reg = 0;
11754 }
11755 }
11756 else
11757 {
11758 if (strncmp (input_line_pointer, "fp", 2) == 0)
11759 reg = FP;
11760 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11761 reg = SP;
11762 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11763 reg = GP;
11764 else if (strncmp (input_line_pointer, "at", 2) == 0)
11765 reg = AT;
11766 else
11767 {
11768 as_warn (_("Unrecognized register name"));
11769 reg = 0;
11770 }
11771 input_line_pointer += 2;
11772 }
11773 if (frame)
11774 {
11775 mips_frame_reg = reg != 0 ? reg : SP;
11776 mips_frame_reg_valid = 1;
11777 mips_cprestore_valid = 0;
11778 }
11779 return reg;
11780 }
11781
11782 valueT
11783 md_section_align (seg, addr)
11784 asection *seg;
11785 valueT addr;
11786 {
11787 int align = bfd_get_section_alignment (stdoutput, seg);
11788
11789 #ifdef OBJ_ELF
11790 /* We don't need to align ELF sections to the full alignment.
11791 However, Irix 5 may prefer that we align them at least to a 16
11792 byte boundary. We don't bother to align the sections if we are
11793 targeted for an embedded system. */
11794 if (strcmp (TARGET_OS, "elf") == 0)
11795 return addr;
11796 if (align > 4)
11797 align = 4;
11798 #endif
11799
11800 return ((addr + (1 << align) - 1) & (-1 << align));
11801 }
11802
11803 /* Utility routine, called from above as well. If called while the
11804 input file is still being read, it's only an approximation. (For
11805 example, a symbol may later become defined which appeared to be
11806 undefined earlier.) */
11807
11808 static int
11809 nopic_need_relax (sym, before_relaxing)
11810 symbolS *sym;
11811 int before_relaxing;
11812 {
11813 if (sym == 0)
11814 return 0;
11815
11816 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11817 {
11818 const char *symname;
11819 int change;
11820
11821 /* Find out whether this symbol can be referenced off the GP
11822 register. It can be if it is smaller than the -G size or if
11823 it is in the .sdata or .sbss section. Certain symbols can
11824 not be referenced off the GP, although it appears as though
11825 they can. */
11826 symname = S_GET_NAME (sym);
11827 if (symname != (const char *) NULL
11828 && (strcmp (symname, "eprol") == 0
11829 || strcmp (symname, "etext") == 0
11830 || strcmp (symname, "_gp") == 0
11831 || strcmp (symname, "edata") == 0
11832 || strcmp (symname, "_fbss") == 0
11833 || strcmp (symname, "_fdata") == 0
11834 || strcmp (symname, "_ftext") == 0
11835 || strcmp (symname, "end") == 0
11836 || strcmp (symname, "_gp_disp") == 0))
11837 change = 1;
11838 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11839 && (0
11840 #ifndef NO_ECOFF_DEBUGGING
11841 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11842 && (symbol_get_obj (sym)->ecoff_extern_size
11843 <= g_switch_value))
11844 #endif
11845 /* We must defer this decision until after the whole
11846 file has been read, since there might be a .extern
11847 after the first use of this symbol. */
11848 || (before_relaxing
11849 #ifndef NO_ECOFF_DEBUGGING
11850 && symbol_get_obj (sym)->ecoff_extern_size == 0
11851 #endif
11852 && S_GET_VALUE (sym) == 0)
11853 || (S_GET_VALUE (sym) != 0
11854 && S_GET_VALUE (sym) <= g_switch_value)))
11855 change = 0;
11856 else
11857 {
11858 const char *segname;
11859
11860 segname = segment_name (S_GET_SEGMENT (sym));
11861 assert (strcmp (segname, ".lit8") != 0
11862 && strcmp (segname, ".lit4") != 0);
11863 change = (strcmp (segname, ".sdata") != 0
11864 && strcmp (segname, ".sbss") != 0
11865 && strncmp (segname, ".sdata.", 7) != 0
11866 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11867 }
11868 return change;
11869 }
11870 else
11871 /* We are not optimizing for the GP register. */
11872 return 1;
11873 }
11874
11875 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11876 extended opcode. SEC is the section the frag is in. */
11877
11878 static int
11879 mips16_extended_frag (fragp, sec, stretch)
11880 fragS *fragp;
11881 asection *sec;
11882 long stretch;
11883 {
11884 int type;
11885 register const struct mips16_immed_operand *op;
11886 offsetT val;
11887 int mintiny, maxtiny;
11888 segT symsec;
11889 fragS *sym_frag;
11890
11891 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11892 return 0;
11893 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11894 return 1;
11895
11896 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11897 op = mips16_immed_operands;
11898 while (op->type != type)
11899 {
11900 ++op;
11901 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11902 }
11903
11904 if (op->unsp)
11905 {
11906 if (type == '<' || type == '>' || type == '[' || type == ']')
11907 {
11908 mintiny = 1;
11909 maxtiny = 1 << op->nbits;
11910 }
11911 else
11912 {
11913 mintiny = 0;
11914 maxtiny = (1 << op->nbits) - 1;
11915 }
11916 }
11917 else
11918 {
11919 mintiny = - (1 << (op->nbits - 1));
11920 maxtiny = (1 << (op->nbits - 1)) - 1;
11921 }
11922
11923 sym_frag = symbol_get_frag (fragp->fr_symbol);
11924 val = S_GET_VALUE (fragp->fr_symbol);
11925 symsec = S_GET_SEGMENT (fragp->fr_symbol);
11926
11927 if (op->pcrel)
11928 {
11929 addressT addr;
11930
11931 /* We won't have the section when we are called from
11932 mips_relax_frag. However, we will always have been called
11933 from md_estimate_size_before_relax first. If this is a
11934 branch to a different section, we mark it as such. If SEC is
11935 NULL, and the frag is not marked, then it must be a branch to
11936 the same section. */
11937 if (sec == NULL)
11938 {
11939 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
11940 return 1;
11941 }
11942 else
11943 {
11944 /* Must have been called from md_estimate_size_before_relax. */
11945 if (symsec != sec)
11946 {
11947 fragp->fr_subtype =
11948 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11949
11950 /* FIXME: We should support this, and let the linker
11951 catch branches and loads that are out of range. */
11952 as_bad_where (fragp->fr_file, fragp->fr_line,
11953 _("unsupported PC relative reference to different section"));
11954
11955 return 1;
11956 }
11957 if (fragp != sym_frag && sym_frag->fr_address == 0)
11958 /* Assume non-extended on the first relaxation pass.
11959 The address we have calculated will be bogus if this is
11960 a forward branch to another frag, as the forward frag
11961 will have fr_address == 0. */
11962 return 0;
11963 }
11964
11965 /* In this case, we know for sure that the symbol fragment is in
11966 the same section. If the relax_marker of the symbol fragment
11967 differs from the relax_marker of this fragment, we have not
11968 yet adjusted the symbol fragment fr_address. We want to add
11969 in STRETCH in order to get a better estimate of the address.
11970 This particularly matters because of the shift bits. */
11971 if (stretch != 0
11972 && sym_frag->relax_marker != fragp->relax_marker)
11973 {
11974 fragS *f;
11975
11976 /* Adjust stretch for any alignment frag. Note that if have
11977 been expanding the earlier code, the symbol may be
11978 defined in what appears to be an earlier frag. FIXME:
11979 This doesn't handle the fr_subtype field, which specifies
11980 a maximum number of bytes to skip when doing an
11981 alignment. */
11982 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
11983 {
11984 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
11985 {
11986 if (stretch < 0)
11987 stretch = - ((- stretch)
11988 & ~ ((1 << (int) f->fr_offset) - 1));
11989 else
11990 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
11991 if (stretch == 0)
11992 break;
11993 }
11994 }
11995 if (f != NULL)
11996 val += stretch;
11997 }
11998
11999 addr = fragp->fr_address + fragp->fr_fix;
12000
12001 /* The base address rules are complicated. The base address of
12002 a branch is the following instruction. The base address of a
12003 PC relative load or add is the instruction itself, but if it
12004 is in a delay slot (in which case it can not be extended) use
12005 the address of the instruction whose delay slot it is in. */
12006 if (type == 'p' || type == 'q')
12007 {
12008 addr += 2;
12009
12010 /* If we are currently assuming that this frag should be
12011 extended, then, the current address is two bytes
12012 higher. */
12013 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12014 addr += 2;
12015
12016 /* Ignore the low bit in the target, since it will be set
12017 for a text label. */
12018 if ((val & 1) != 0)
12019 --val;
12020 }
12021 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12022 addr -= 4;
12023 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12024 addr -= 2;
12025
12026 val -= addr & ~ ((1 << op->shift) - 1);
12027
12028 /* Branch offsets have an implicit 0 in the lowest bit. */
12029 if (type == 'p' || type == 'q')
12030 val /= 2;
12031
12032 /* If any of the shifted bits are set, we must use an extended
12033 opcode. If the address depends on the size of this
12034 instruction, this can lead to a loop, so we arrange to always
12035 use an extended opcode. We only check this when we are in
12036 the main relaxation loop, when SEC is NULL. */
12037 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12038 {
12039 fragp->fr_subtype =
12040 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12041 return 1;
12042 }
12043
12044 /* If we are about to mark a frag as extended because the value
12045 is precisely maxtiny + 1, then there is a chance of an
12046 infinite loop as in the following code:
12047 la $4,foo
12048 .skip 1020
12049 .align 2
12050 foo:
12051 In this case when the la is extended, foo is 0x3fc bytes
12052 away, so the la can be shrunk, but then foo is 0x400 away, so
12053 the la must be extended. To avoid this loop, we mark the
12054 frag as extended if it was small, and is about to become
12055 extended with a value of maxtiny + 1. */
12056 if (val == ((maxtiny + 1) << op->shift)
12057 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12058 && sec == NULL)
12059 {
12060 fragp->fr_subtype =
12061 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12062 return 1;
12063 }
12064 }
12065 else if (symsec != absolute_section && sec != NULL)
12066 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12067
12068 if ((val & ((1 << op->shift) - 1)) != 0
12069 || val < (mintiny << op->shift)
12070 || val > (maxtiny << op->shift))
12071 return 1;
12072 else
12073 return 0;
12074 }
12075
12076 /* Estimate the size of a frag before relaxing. Unless this is the
12077 mips16, we are not really relaxing here, and the final size is
12078 encoded in the subtype information. For the mips16, we have to
12079 decide whether we are using an extended opcode or not. */
12080
12081 int
12082 md_estimate_size_before_relax (fragp, segtype)
12083 fragS *fragp;
12084 asection *segtype;
12085 {
12086 int change = 0;
12087 boolean linkonce = false;
12088
12089 if (RELAX_MIPS16_P (fragp->fr_subtype))
12090 {
12091 if (mips16_extended_frag (fragp, segtype, 0))
12092 {
12093 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12094 return 4;
12095 }
12096 else
12097 {
12098 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12099 return 2;
12100 }
12101 }
12102
12103 if (mips_pic == NO_PIC)
12104 {
12105 change = nopic_need_relax (fragp->fr_symbol, 0);
12106 }
12107 else if (mips_pic == SVR4_PIC)
12108 {
12109 symbolS *sym;
12110 asection *symsec;
12111
12112 sym = fragp->fr_symbol;
12113
12114 /* Handle the case of a symbol equated to another symbol. */
12115 while (symbol_equated_reloc_p (sym))
12116 {
12117 symbolS *n;
12118
12119 /* It's possible to get a loop here in a badly written
12120 program. */
12121 n = symbol_get_value_expression (sym)->X_add_symbol;
12122 if (n == sym)
12123 break;
12124 sym = n;
12125 }
12126
12127 symsec = S_GET_SEGMENT (sym);
12128
12129 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12130 if (symsec != segtype && ! S_IS_LOCAL (sym))
12131 {
12132 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12133 != 0)
12134 linkonce = true;
12135
12136 /* The GNU toolchain uses an extension for ELF: a section
12137 beginning with the magic string .gnu.linkonce is a linkonce
12138 section. */
12139 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12140 sizeof ".gnu.linkonce" - 1) == 0)
12141 linkonce = true;
12142 }
12143
12144 /* This must duplicate the test in adjust_reloc_syms. */
12145 change = (symsec != &bfd_und_section
12146 && symsec != &bfd_abs_section
12147 && ! bfd_is_com_section (symsec)
12148 && !linkonce
12149 #ifdef OBJ_ELF
12150 /* A global or weak symbol is treated as external. */
12151 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
12152 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
12153 #endif
12154 );
12155 }
12156 else
12157 abort ();
12158
12159 if (change)
12160 {
12161 /* Record the offset to the first reloc in the fr_opcode field.
12162 This lets md_convert_frag and tc_gen_reloc know that the code
12163 must be expanded. */
12164 fragp->fr_opcode = (fragp->fr_literal
12165 + fragp->fr_fix
12166 - RELAX_OLD (fragp->fr_subtype)
12167 + RELAX_RELOC1 (fragp->fr_subtype));
12168 /* FIXME: This really needs as_warn_where. */
12169 if (RELAX_WARN (fragp->fr_subtype))
12170 as_warn (_("AT used after \".set noat\" or macro used after "
12171 "\".set nomacro\""));
12172
12173 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12174 }
12175
12176 return 0;
12177 }
12178
12179 /* This is called to see whether a reloc against a defined symbol
12180 should be converted into a reloc against a section. Don't adjust
12181 MIPS16 jump relocations, so we don't have to worry about the format
12182 of the offset in the .o file. Don't adjust relocations against
12183 mips16 symbols, so that the linker can find them if it needs to set
12184 up a stub. */
12185
12186 int
12187 mips_fix_adjustable (fixp)
12188 fixS *fixp;
12189 {
12190 #ifdef OBJ_ELF
12191 /* Prevent all adjustments to global symbols. */
12192 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12193 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12194 return 0;
12195 #endif
12196 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12197 return 0;
12198 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12199 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12200 return 0;
12201 if (fixp->fx_addsy == NULL)
12202 return 1;
12203 #ifdef OBJ_ELF
12204 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12205 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12206 && fixp->fx_subsy == NULL)
12207 return 0;
12208 #endif
12209 return 1;
12210 }
12211
12212 /* Translate internal representation of relocation info to BFD target
12213 format. */
12214
12215 arelent **
12216 tc_gen_reloc (section, fixp)
12217 asection *section ATTRIBUTE_UNUSED;
12218 fixS *fixp;
12219 {
12220 static arelent *retval[4];
12221 arelent *reloc;
12222 bfd_reloc_code_real_type code;
12223
12224 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12225 retval[1] = NULL;
12226
12227 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12228 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12229 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12230
12231 if (mips_pic == EMBEDDED_PIC
12232 && SWITCH_TABLE (fixp))
12233 {
12234 /* For a switch table entry we use a special reloc. The addend
12235 is actually the difference between the reloc address and the
12236 subtrahend. */
12237 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12238 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12239 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12240 fixp->fx_r_type = BFD_RELOC_GPREL32;
12241 }
12242 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12243 reloc->addend = fixp->fx_addnumber;
12244 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12245 {
12246 /* We use a special addend for an internal RELLO reloc. */
12247 if (symbol_section_p (fixp->fx_addsy))
12248 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12249 else
12250 reloc->addend = fixp->fx_addnumber + reloc->address;
12251 }
12252 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12253 {
12254 assert (fixp->fx_next != NULL
12255 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12256 /* We use a special addend for an internal RELHI reloc. The
12257 reloc is relative to the RELLO; adjust the addend
12258 accordingly. */
12259 if (symbol_section_p (fixp->fx_addsy))
12260 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12261 + fixp->fx_next->fx_where
12262 - S_GET_VALUE (fixp->fx_subsy));
12263 else
12264 reloc->addend = (fixp->fx_addnumber
12265 + fixp->fx_next->fx_frag->fr_address
12266 + fixp->fx_next->fx_where);
12267 }
12268 else
12269 {
12270 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12271 /* A gruesome hack which is a result of the gruesome gas reloc
12272 handling. */
12273 reloc->addend = reloc->address;
12274 else
12275 reloc->addend = -reloc->address;
12276 }
12277
12278 /* If this is a variant frag, we may need to adjust the existing
12279 reloc and generate a new one. */
12280 if (fixp->fx_frag->fr_opcode != NULL
12281 && (fixp->fx_r_type == BFD_RELOC_GPREL16
12282 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12283 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12284 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12285 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12286 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12287 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12288 && ! HAVE_NEWABI)
12289 {
12290 arelent *reloc2;
12291
12292 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12293
12294 /* If this is not the last reloc in this frag, then we have two
12295 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12296 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12297 the second one handle all of them. */
12298 if (fixp->fx_next != NULL
12299 && fixp->fx_frag == fixp->fx_next->fx_frag)
12300 {
12301 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12302 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12303 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12304 && (fixp->fx_next->fx_r_type
12305 == BFD_RELOC_MIPS_GOT_LO16))
12306 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12307 && (fixp->fx_next->fx_r_type
12308 == BFD_RELOC_MIPS_CALL_LO16)));
12309 retval[0] = NULL;
12310 return retval;
12311 }
12312
12313 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12314 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12315 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12316 retval[2] = NULL;
12317 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12318 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12319 reloc2->address = (reloc->address
12320 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12321 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12322 reloc2->addend = fixp->fx_addnumber;
12323 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12324 assert (reloc2->howto != NULL);
12325
12326 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12327 {
12328 arelent *reloc3;
12329
12330 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12331 retval[3] = NULL;
12332 *reloc3 = *reloc2;
12333 reloc3->address += 4;
12334 }
12335
12336 if (mips_pic == NO_PIC)
12337 {
12338 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12339 fixp->fx_r_type = BFD_RELOC_HI16_S;
12340 }
12341 else if (mips_pic == SVR4_PIC)
12342 {
12343 switch (fixp->fx_r_type)
12344 {
12345 default:
12346 abort ();
12347 case BFD_RELOC_MIPS_GOT16:
12348 break;
12349 case BFD_RELOC_MIPS_CALL16:
12350 case BFD_RELOC_MIPS_GOT_LO16:
12351 case BFD_RELOC_MIPS_CALL_LO16:
12352 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12353 break;
12354 }
12355 }
12356 else
12357 abort ();
12358 }
12359
12360 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12361 to be used in the relocation's section offset. */
12362 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12363 {
12364 reloc->address = reloc->addend;
12365 reloc->addend = 0;
12366 }
12367
12368 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12369 fixup_segment converted a non-PC relative reloc into a PC
12370 relative reloc. In such a case, we need to convert the reloc
12371 code. */
12372 code = fixp->fx_r_type;
12373 if (fixp->fx_pcrel)
12374 {
12375 switch (code)
12376 {
12377 case BFD_RELOC_8:
12378 code = BFD_RELOC_8_PCREL;
12379 break;
12380 case BFD_RELOC_16:
12381 code = BFD_RELOC_16_PCREL;
12382 break;
12383 case BFD_RELOC_32:
12384 code = BFD_RELOC_32_PCREL;
12385 break;
12386 case BFD_RELOC_64:
12387 code = BFD_RELOC_64_PCREL;
12388 break;
12389 case BFD_RELOC_8_PCREL:
12390 case BFD_RELOC_16_PCREL:
12391 case BFD_RELOC_32_PCREL:
12392 case BFD_RELOC_64_PCREL:
12393 case BFD_RELOC_16_PCREL_S2:
12394 case BFD_RELOC_PCREL_HI16_S:
12395 case BFD_RELOC_PCREL_LO16:
12396 break;
12397 default:
12398 as_bad_where (fixp->fx_file, fixp->fx_line,
12399 _("Cannot make %s relocation PC relative"),
12400 bfd_get_reloc_code_name (code));
12401 }
12402 }
12403
12404 #ifdef OBJ_ELF
12405 /* md_apply_fix3 has a double-subtraction hack to get
12406 bfd_install_relocation to behave nicely. GPREL relocations are
12407 handled correctly without this hack, so undo it here. We can't
12408 stop md_apply_fix3 from subtracting twice in the first place since
12409 the fake addend is required for variant frags above. */
12410 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12411 && code == BFD_RELOC_GPREL16
12412 && reloc->addend != 0
12413 && mips_need_elf_addend_fixup (fixp))
12414 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12415 #endif
12416
12417 /* To support a PC relative reloc when generating embedded PIC code
12418 for ECOFF, we use a Cygnus extension. We check for that here to
12419 make sure that we don't let such a reloc escape normally. */
12420 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12421 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12422 && code == BFD_RELOC_16_PCREL_S2
12423 && mips_pic != EMBEDDED_PIC)
12424 reloc->howto = NULL;
12425 else
12426 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12427
12428 if (reloc->howto == NULL)
12429 {
12430 as_bad_where (fixp->fx_file, fixp->fx_line,
12431 _("Can not represent %s relocation in this object file format"),
12432 bfd_get_reloc_code_name (code));
12433 retval[0] = NULL;
12434 }
12435
12436 return retval;
12437 }
12438
12439 /* Relax a machine dependent frag. This returns the amount by which
12440 the current size of the frag should change. */
12441
12442 int
12443 mips_relax_frag (fragp, stretch)
12444 fragS *fragp;
12445 long stretch;
12446 {
12447 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12448 return 0;
12449
12450 if (mips16_extended_frag (fragp, NULL, stretch))
12451 {
12452 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12453 return 0;
12454 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12455 return 2;
12456 }
12457 else
12458 {
12459 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12460 return 0;
12461 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12462 return -2;
12463 }
12464
12465 return 0;
12466 }
12467
12468 /* Convert a machine dependent frag. */
12469
12470 void
12471 md_convert_frag (abfd, asec, fragp)
12472 bfd *abfd ATTRIBUTE_UNUSED;
12473 segT asec;
12474 fragS *fragp;
12475 {
12476 int old, new;
12477 char *fixptr;
12478
12479 if (RELAX_MIPS16_P (fragp->fr_subtype))
12480 {
12481 int type;
12482 register const struct mips16_immed_operand *op;
12483 boolean small, ext;
12484 offsetT val;
12485 bfd_byte *buf;
12486 unsigned long insn;
12487 boolean use_extend;
12488 unsigned short extend;
12489
12490 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12491 op = mips16_immed_operands;
12492 while (op->type != type)
12493 ++op;
12494
12495 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12496 {
12497 small = false;
12498 ext = true;
12499 }
12500 else
12501 {
12502 small = true;
12503 ext = false;
12504 }
12505
12506 resolve_symbol_value (fragp->fr_symbol);
12507 val = S_GET_VALUE (fragp->fr_symbol);
12508 if (op->pcrel)
12509 {
12510 addressT addr;
12511
12512 addr = fragp->fr_address + fragp->fr_fix;
12513
12514 /* The rules for the base address of a PC relative reloc are
12515 complicated; see mips16_extended_frag. */
12516 if (type == 'p' || type == 'q')
12517 {
12518 addr += 2;
12519 if (ext)
12520 addr += 2;
12521 /* Ignore the low bit in the target, since it will be
12522 set for a text label. */
12523 if ((val & 1) != 0)
12524 --val;
12525 }
12526 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12527 addr -= 4;
12528 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12529 addr -= 2;
12530
12531 addr &= ~ (addressT) ((1 << op->shift) - 1);
12532 val -= addr;
12533
12534 /* Make sure the section winds up with the alignment we have
12535 assumed. */
12536 if (op->shift > 0)
12537 record_alignment (asec, op->shift);
12538 }
12539
12540 if (ext
12541 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12542 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12543 as_warn_where (fragp->fr_file, fragp->fr_line,
12544 _("extended instruction in delay slot"));
12545
12546 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12547
12548 if (target_big_endian)
12549 insn = bfd_getb16 (buf);
12550 else
12551 insn = bfd_getl16 (buf);
12552
12553 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12554 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12555 small, ext, &insn, &use_extend, &extend);
12556
12557 if (use_extend)
12558 {
12559 md_number_to_chars (buf, 0xf000 | extend, 2);
12560 fragp->fr_fix += 2;
12561 buf += 2;
12562 }
12563
12564 md_number_to_chars (buf, insn, 2);
12565 fragp->fr_fix += 2;
12566 buf += 2;
12567 }
12568 else
12569 {
12570 if (fragp->fr_opcode == NULL)
12571 return;
12572
12573 old = RELAX_OLD (fragp->fr_subtype);
12574 new = RELAX_NEW (fragp->fr_subtype);
12575 fixptr = fragp->fr_literal + fragp->fr_fix;
12576
12577 if (new > 0)
12578 memcpy (fixptr - old, fixptr, new);
12579
12580 fragp->fr_fix += new - old;
12581 }
12582 }
12583
12584 #ifdef OBJ_ELF
12585
12586 /* This function is called after the relocs have been generated.
12587 We've been storing mips16 text labels as odd. Here we convert them
12588 back to even for the convenience of the debugger. */
12589
12590 void
12591 mips_frob_file_after_relocs ()
12592 {
12593 asymbol **syms;
12594 unsigned int count, i;
12595
12596 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12597 return;
12598
12599 syms = bfd_get_outsymbols (stdoutput);
12600 count = bfd_get_symcount (stdoutput);
12601 for (i = 0; i < count; i++, syms++)
12602 {
12603 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12604 && ((*syms)->value & 1) != 0)
12605 {
12606 (*syms)->value &= ~1;
12607 /* If the symbol has an odd size, it was probably computed
12608 incorrectly, so adjust that as well. */
12609 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12610 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12611 }
12612 }
12613 }
12614
12615 #endif
12616
12617 /* This function is called whenever a label is defined. It is used
12618 when handling branch delays; if a branch has a label, we assume we
12619 can not move it. */
12620
12621 void
12622 mips_define_label (sym)
12623 symbolS *sym;
12624 {
12625 struct insn_label_list *l;
12626
12627 if (free_insn_labels == NULL)
12628 l = (struct insn_label_list *) xmalloc (sizeof *l);
12629 else
12630 {
12631 l = free_insn_labels;
12632 free_insn_labels = l->next;
12633 }
12634
12635 l->label = sym;
12636 l->next = insn_labels;
12637 insn_labels = l;
12638 }
12639 \f
12640 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12641
12642 /* Some special processing for a MIPS ELF file. */
12643
12644 void
12645 mips_elf_final_processing ()
12646 {
12647 /* Write out the register information. */
12648 if (! HAVE_NEWABI)
12649 {
12650 Elf32_RegInfo s;
12651
12652 s.ri_gprmask = mips_gprmask;
12653 s.ri_cprmask[0] = mips_cprmask[0];
12654 s.ri_cprmask[1] = mips_cprmask[1];
12655 s.ri_cprmask[2] = mips_cprmask[2];
12656 s.ri_cprmask[3] = mips_cprmask[3];
12657 /* The gp_value field is set by the MIPS ELF backend. */
12658
12659 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12660 ((Elf32_External_RegInfo *)
12661 mips_regmask_frag));
12662 }
12663 else
12664 {
12665 Elf64_Internal_RegInfo s;
12666
12667 s.ri_gprmask = mips_gprmask;
12668 s.ri_pad = 0;
12669 s.ri_cprmask[0] = mips_cprmask[0];
12670 s.ri_cprmask[1] = mips_cprmask[1];
12671 s.ri_cprmask[2] = mips_cprmask[2];
12672 s.ri_cprmask[3] = mips_cprmask[3];
12673 /* The gp_value field is set by the MIPS ELF backend. */
12674
12675 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12676 ((Elf64_External_RegInfo *)
12677 mips_regmask_frag));
12678 }
12679
12680 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12681 sort of BFD interface for this. */
12682 if (mips_any_noreorder)
12683 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12684 if (mips_pic != NO_PIC)
12685 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12686
12687 /* Set the MIPS ELF ABI flags. */
12688 if (file_mips_abi == NO_ABI)
12689 ;
12690 else if (file_mips_abi == O32_ABI)
12691 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12692 else if (file_mips_abi == O64_ABI)
12693 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12694 else if (file_mips_abi == EABI_ABI)
12695 {
12696 if (mips_eabi64)
12697 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12698 else
12699 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12700 }
12701 else if (file_mips_abi == N32_ABI)
12702 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12703
12704 /* Nothing to do for "64". */
12705
12706 if (mips_32bitmode)
12707 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12708 }
12709
12710 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12711 \f
12712 typedef struct proc {
12713 symbolS *isym;
12714 unsigned long reg_mask;
12715 unsigned long reg_offset;
12716 unsigned long fpreg_mask;
12717 unsigned long fpreg_offset;
12718 unsigned long frame_offset;
12719 unsigned long frame_reg;
12720 unsigned long pc_reg;
12721 } procS;
12722
12723 static procS cur_proc;
12724 static procS *cur_proc_ptr;
12725 static int numprocs;
12726
12727 /* Fill in an rs_align_code fragment. */
12728
12729 void
12730 mips_handle_align (fragp)
12731 fragS *fragp;
12732 {
12733 if (fragp->fr_type != rs_align_code)
12734 return;
12735
12736 if (mips_opts.mips16)
12737 {
12738 static const unsigned char be_nop[] = { 0x65, 0x00 };
12739 static const unsigned char le_nop[] = { 0x00, 0x65 };
12740
12741 int bytes;
12742 char *p;
12743
12744 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12745 p = fragp->fr_literal + fragp->fr_fix;
12746
12747 if (bytes & 1)
12748 {
12749 *p++ = 0;
12750 fragp->fr_fix += 1;
12751 }
12752
12753 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12754 fragp->fr_var = 2;
12755 }
12756
12757 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
12758 }
12759
12760 static void
12761 md_obj_begin ()
12762 {
12763 }
12764
12765 static void
12766 md_obj_end ()
12767 {
12768 /* check for premature end, nesting errors, etc */
12769 if (cur_proc_ptr)
12770 as_warn (_("missing .end at end of assembly"));
12771 }
12772
12773 static long
12774 get_number ()
12775 {
12776 int negative = 0;
12777 long val = 0;
12778
12779 if (*input_line_pointer == '-')
12780 {
12781 ++input_line_pointer;
12782 negative = 1;
12783 }
12784 if (!ISDIGIT (*input_line_pointer))
12785 as_bad (_("expected simple number"));
12786 if (input_line_pointer[0] == '0')
12787 {
12788 if (input_line_pointer[1] == 'x')
12789 {
12790 input_line_pointer += 2;
12791 while (ISXDIGIT (*input_line_pointer))
12792 {
12793 val <<= 4;
12794 val |= hex_value (*input_line_pointer++);
12795 }
12796 return negative ? -val : val;
12797 }
12798 else
12799 {
12800 ++input_line_pointer;
12801 while (ISDIGIT (*input_line_pointer))
12802 {
12803 val <<= 3;
12804 val |= *input_line_pointer++ - '0';
12805 }
12806 return negative ? -val : val;
12807 }
12808 }
12809 if (!ISDIGIT (*input_line_pointer))
12810 {
12811 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12812 *input_line_pointer, *input_line_pointer);
12813 as_warn (_("invalid number"));
12814 return -1;
12815 }
12816 while (ISDIGIT (*input_line_pointer))
12817 {
12818 val *= 10;
12819 val += *input_line_pointer++ - '0';
12820 }
12821 return negative ? -val : val;
12822 }
12823
12824 /* The .file directive; just like the usual .file directive, but there
12825 is an initial number which is the ECOFF file index. */
12826
12827 static void
12828 s_file (x)
12829 int x ATTRIBUTE_UNUSED;
12830 {
12831 int line;
12832
12833 line = get_number ();
12834 s_app_file (0);
12835 }
12836
12837 /* The .end directive. */
12838
12839 static void
12840 s_mips_end (x)
12841 int x ATTRIBUTE_UNUSED;
12842 {
12843 symbolS *p;
12844 int maybe_text;
12845
12846 /* Following functions need their own .frame and .cprestore directives. */
12847 mips_frame_reg_valid = 0;
12848 mips_cprestore_valid = 0;
12849
12850 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12851 {
12852 p = get_symbol ();
12853 demand_empty_rest_of_line ();
12854 }
12855 else
12856 p = NULL;
12857
12858 #ifdef BFD_ASSEMBLER
12859 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12860 maybe_text = 1;
12861 else
12862 maybe_text = 0;
12863 #else
12864 if (now_seg != data_section && now_seg != bss_section)
12865 maybe_text = 1;
12866 else
12867 maybe_text = 0;
12868 #endif
12869
12870 if (!maybe_text)
12871 as_warn (_(".end not in text section"));
12872
12873 if (!cur_proc_ptr)
12874 {
12875 as_warn (_(".end directive without a preceding .ent directive."));
12876 demand_empty_rest_of_line ();
12877 return;
12878 }
12879
12880 if (p != NULL)
12881 {
12882 assert (S_GET_NAME (p));
12883 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12884 as_warn (_(".end symbol does not match .ent symbol."));
12885 }
12886 else
12887 as_warn (_(".end directive missing or unknown symbol"));
12888
12889 #ifdef MIPS_STABS_ELF
12890 {
12891 segT saved_seg = now_seg;
12892 subsegT saved_subseg = now_subseg;
12893 valueT dot;
12894 expressionS exp;
12895 char *fragp;
12896
12897 dot = frag_now_fix ();
12898
12899 #ifdef md_flush_pending_output
12900 md_flush_pending_output ();
12901 #endif
12902
12903 assert (pdr_seg);
12904 subseg_set (pdr_seg, 0);
12905
12906 /* Write the symbol. */
12907 exp.X_op = O_symbol;
12908 exp.X_add_symbol = p;
12909 exp.X_add_number = 0;
12910 emit_expr (&exp, 4);
12911
12912 fragp = frag_more (7 * 4);
12913
12914 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
12915 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
12916 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12917 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12918 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12919 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12920 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
12921
12922 subseg_set (saved_seg, saved_subseg);
12923 }
12924 #endif
12925
12926 cur_proc_ptr = NULL;
12927 }
12928
12929 /* The .aent and .ent directives. */
12930
12931 static void
12932 s_mips_ent (aent)
12933 int aent;
12934 {
12935 int number = 0;
12936 symbolS *symbolP;
12937 int maybe_text;
12938
12939 symbolP = get_symbol ();
12940 if (*input_line_pointer == ',')
12941 input_line_pointer++;
12942 SKIP_WHITESPACE ();
12943 if (ISDIGIT (*input_line_pointer)
12944 || *input_line_pointer == '-')
12945 number = get_number ();
12946
12947 #ifdef BFD_ASSEMBLER
12948 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12949 maybe_text = 1;
12950 else
12951 maybe_text = 0;
12952 #else
12953 if (now_seg != data_section && now_seg != bss_section)
12954 maybe_text = 1;
12955 else
12956 maybe_text = 0;
12957 #endif
12958
12959 if (!maybe_text)
12960 as_warn (_(".ent or .aent not in text section."));
12961
12962 if (!aent && cur_proc_ptr)
12963 as_warn (_("missing .end"));
12964
12965 if (!aent)
12966 {
12967 /* This function needs its own .frame and .cprestore directives. */
12968 mips_frame_reg_valid = 0;
12969 mips_cprestore_valid = 0;
12970
12971 cur_proc_ptr = &cur_proc;
12972 memset (cur_proc_ptr, '\0', sizeof (procS));
12973
12974 cur_proc_ptr->isym = symbolP;
12975
12976 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
12977
12978 numprocs++;
12979 }
12980
12981 demand_empty_rest_of_line ();
12982 }
12983
12984 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
12985 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
12986 s_mips_frame is used so that we can set the PDR information correctly.
12987 We can't use the ecoff routines because they make reference to the ecoff
12988 symbol table (in the mdebug section). */
12989
12990 static void
12991 s_mips_frame (ignore)
12992 int ignore ATTRIBUTE_UNUSED;
12993 {
12994 #ifdef MIPS_STABS_ELF
12995
12996 long val;
12997
12998 if (cur_proc_ptr == (procS *) NULL)
12999 {
13000 as_warn (_(".frame outside of .ent"));
13001 demand_empty_rest_of_line ();
13002 return;
13003 }
13004
13005 cur_proc_ptr->frame_reg = tc_get_register (1);
13006
13007 SKIP_WHITESPACE ();
13008 if (*input_line_pointer++ != ','
13009 || get_absolute_expression_and_terminator (&val) != ',')
13010 {
13011 as_warn (_("Bad .frame directive"));
13012 --input_line_pointer;
13013 demand_empty_rest_of_line ();
13014 return;
13015 }
13016
13017 cur_proc_ptr->frame_offset = val;
13018 cur_proc_ptr->pc_reg = tc_get_register (0);
13019
13020 demand_empty_rest_of_line ();
13021 #else
13022 s_ignore (ignore);
13023 #endif /* MIPS_STABS_ELF */
13024 }
13025
13026 /* The .fmask and .mask directives. If the mdebug section is present
13027 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13028 embedded targets, s_mips_mask is used so that we can set the PDR
13029 information correctly. We can't use the ecoff routines because they
13030 make reference to the ecoff symbol table (in the mdebug section). */
13031
13032 static void
13033 s_mips_mask (reg_type)
13034 char reg_type;
13035 {
13036 #ifdef MIPS_STABS_ELF
13037 long mask, off;
13038
13039 if (cur_proc_ptr == (procS *) NULL)
13040 {
13041 as_warn (_(".mask/.fmask outside of .ent"));
13042 demand_empty_rest_of_line ();
13043 return;
13044 }
13045
13046 if (get_absolute_expression_and_terminator (&mask) != ',')
13047 {
13048 as_warn (_("Bad .mask/.fmask directive"));
13049 --input_line_pointer;
13050 demand_empty_rest_of_line ();
13051 return;
13052 }
13053
13054 off = get_absolute_expression ();
13055
13056 if (reg_type == 'F')
13057 {
13058 cur_proc_ptr->fpreg_mask = mask;
13059 cur_proc_ptr->fpreg_offset = off;
13060 }
13061 else
13062 {
13063 cur_proc_ptr->reg_mask = mask;
13064 cur_proc_ptr->reg_offset = off;
13065 }
13066
13067 demand_empty_rest_of_line ();
13068 #else
13069 s_ignore (reg_type);
13070 #endif /* MIPS_STABS_ELF */
13071 }
13072
13073 /* The .loc directive. */
13074
13075 #if 0
13076 static void
13077 s_loc (x)
13078 int x;
13079 {
13080 symbolS *symbolP;
13081 int lineno;
13082 int addroff;
13083
13084 assert (now_seg == text_section);
13085
13086 lineno = get_number ();
13087 addroff = frag_now_fix ();
13088
13089 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13090 S_SET_TYPE (symbolP, N_SLINE);
13091 S_SET_OTHER (symbolP, 0);
13092 S_SET_DESC (symbolP, lineno);
13093 symbolP->sy_segment = now_seg;
13094 }
13095 #endif
13096
13097 /* CPU name/ISA/number mapping table.
13098
13099 Entries are grouped by type. The first matching CPU or ISA entry
13100 gets chosen by CPU or ISA, so it should be the 'canonical' name
13101 for that type. Entries after that within the type are sorted
13102 alphabetically.
13103
13104 Case is ignored in comparison, so put the canonical entry in the
13105 appropriate case but everything else in lower case to ease eye pain. */
13106 static const struct mips_cpu_info mips_cpu_info_table[] =
13107 {
13108 /* MIPS1 ISA */
13109 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13110 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13111
13112 /* MIPS2 ISA */
13113 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13114
13115 /* MIPS3 ISA */
13116 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13117
13118 /* MIPS4 ISA */
13119 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13120
13121 /* MIPS5 ISA */
13122 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13123 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13124
13125 /* MIPS32 ISA */
13126 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
13127 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
13128 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
13129 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13130 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13131 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13132
13133 /* For historical reasons. */
13134 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
13135
13136 /* MIPS64 ISA */
13137 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
13138 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
13139 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13140 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
13141
13142 /* R2000 CPU */
13143 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13144 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13145 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13146 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13147
13148 /* R3000 CPU */
13149 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13150 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13151 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13152 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13153
13154 /* TX3900 CPU */
13155 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13156 { "3900", 0, ISA_MIPS1, CPU_R3900, },
13157 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
13158
13159 /* R4000 CPU */
13160 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13161 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13162 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13163 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13164
13165 /* R4010 CPU */
13166 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13167 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13168
13169 /* R4400 CPU */
13170 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13171 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13172
13173 /* R4600 CPU */
13174 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13175 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13176 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13177 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13178
13179 /* R4650 CPU */
13180 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13181 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13182
13183 /* R6000 CPU */
13184 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13185 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13186 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13187 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13188
13189 /* R8000 CPU */
13190 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13191 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13192 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13193 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13194
13195 /* R10000 CPU */
13196 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13197 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13198 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13199 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13200
13201 /* R12000 CPU */
13202 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13203 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13204 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13205 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13206
13207 /* VR4100 CPU */
13208 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13209 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13210 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13211 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13212
13213 /* VR4111 CPU */
13214 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13215 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13216 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13217 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13218
13219 /* VR4300 CPU */
13220 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13221 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13222 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13223 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13224
13225 /* VR5000 CPU */
13226 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13227 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13228 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13229 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13230 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13231 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
13232 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
13233 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
13234 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
13235 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
13236 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
13237 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
13238 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
13239 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
13240 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
13241 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13242 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13243
13244 /* Broadcom SB-1 CPU */
13245 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
13246 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
13247 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13248 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13249
13250 /* End marker. */
13251 { NULL, 0, 0, 0, },
13252 };
13253
13254 static const struct mips_cpu_info *
13255 mips_cpu_info_from_name (name)
13256 const char *name;
13257 {
13258 int i;
13259
13260 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13261 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13262 return (&mips_cpu_info_table[i]);
13263
13264 return NULL;
13265 }
13266
13267 static const struct mips_cpu_info *
13268 mips_cpu_info_from_isa (isa)
13269 int isa;
13270 {
13271 int i;
13272
13273 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13274 if (mips_cpu_info_table[i].is_isa
13275 && isa == mips_cpu_info_table[i].isa)
13276 return (&mips_cpu_info_table[i]);
13277
13278 return NULL;
13279 }
13280
13281 static const struct mips_cpu_info *
13282 mips_cpu_info_from_cpu (cpu)
13283 int cpu;
13284 {
13285 int i;
13286
13287 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13288 if (!mips_cpu_info_table[i].is_isa
13289 && cpu == mips_cpu_info_table[i].cpu)
13290 return (&mips_cpu_info_table[i]);
13291
13292 return NULL;
13293 }