2002-02-08 Chris Demetriou <cgd@broadcom.com>
[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_getPercentOp PARAMS ((char **, unsigned int *, int *));
706 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
707 static int my_getSmallExpression PARAMS ((expressionS *, char *));
708 static void my_getExpression PARAMS ((expressionS *, char *));
709 #ifdef OBJ_ELF
710 static int support_64bit_objects PARAMS((void));
711 #endif
712 static symbolS *get_symbol PARAMS ((void));
713 static void mips_align PARAMS ((int to, int fill, symbolS *label));
714 static void s_align PARAMS ((int));
715 static void s_change_sec PARAMS ((int));
716 static void s_cons PARAMS ((int));
717 static void s_float_cons PARAMS ((int));
718 static void s_mips_globl PARAMS ((int));
719 static void s_option PARAMS ((int));
720 static void s_mipsset PARAMS ((int));
721 static void s_abicalls PARAMS ((int));
722 static void s_cpload PARAMS ((int));
723 static void s_cpsetup PARAMS ((int));
724 static void s_cplocal PARAMS ((int));
725 static void s_cprestore PARAMS ((int));
726 static void s_cpreturn PARAMS ((int));
727 static void s_gpvalue PARAMS ((int));
728 static void s_gpword PARAMS ((int));
729 static void s_cpadd PARAMS ((int));
730 static void s_insn PARAMS ((int));
731 static void md_obj_begin PARAMS ((void));
732 static void md_obj_end PARAMS ((void));
733 static long get_number PARAMS ((void));
734 static void s_mips_ent PARAMS ((int));
735 static void s_mips_end PARAMS ((int));
736 static void s_mips_frame PARAMS ((int));
737 static void s_mips_mask PARAMS ((int));
738 static void s_mips_stab PARAMS ((int));
739 static void s_mips_weakext PARAMS ((int));
740 static void s_file PARAMS ((int));
741 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
742 static const char *mips_isa_to_str PARAMS ((int));
743 static const char *mips_cpu_to_str PARAMS ((int));
744 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
745 static void show PARAMS ((FILE *, char *, int *, int *));
746 #ifdef OBJ_ELF
747 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
748 #endif
749
750 /* Return values of my_getSmallExpression(). */
751
752 enum small_ex_type
753 {
754 S_EX_NONE = 0,
755 S_EX_REGISTER,
756
757 /* Direct relocation creation by %percent_op(). */
758 S_EX_HALF,
759 S_EX_HI,
760 S_EX_LO,
761 S_EX_GP_REL,
762 S_EX_GOT,
763 S_EX_CALL16,
764 S_EX_GOT_DISP,
765 S_EX_GOT_PAGE,
766 S_EX_GOT_OFST,
767 S_EX_GOT_HI,
768 S_EX_GOT_LO,
769 S_EX_NEG,
770 S_EX_HIGHER,
771 S_EX_HIGHEST,
772 S_EX_CALL_HI,
773 S_EX_CALL_LO
774 };
775
776 /* Table and functions used to map between CPU/ISA names, and
777 ISA levels, and CPU numbers. */
778
779 struct mips_cpu_info
780 {
781 const char *name; /* CPU or ISA name. */
782 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
783 int isa; /* ISA level. */
784 int cpu; /* CPU number (default CPU if ISA). */
785 };
786
787 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
788 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
789 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
790 \f
791 /* Pseudo-op table.
792
793 The following pseudo-ops from the Kane and Heinrich MIPS book
794 should be defined here, but are currently unsupported: .alias,
795 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
796
797 The following pseudo-ops from the Kane and Heinrich MIPS book are
798 specific to the type of debugging information being generated, and
799 should be defined by the object format: .aent, .begin, .bend,
800 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
801 .vreg.
802
803 The following pseudo-ops from the Kane and Heinrich MIPS book are
804 not MIPS CPU specific, but are also not specific to the object file
805 format. This file is probably the best place to define them, but
806 they are not currently supported: .asm0, .endr, .lab, .repeat,
807 .struct. */
808
809 static const pseudo_typeS mips_pseudo_table[] =
810 {
811 /* MIPS specific pseudo-ops. */
812 {"option", s_option, 0},
813 {"set", s_mipsset, 0},
814 {"rdata", s_change_sec, 'r'},
815 {"sdata", s_change_sec, 's'},
816 {"livereg", s_ignore, 0},
817 {"abicalls", s_abicalls, 0},
818 {"cpload", s_cpload, 0},
819 {"cpsetup", s_cpsetup, 0},
820 {"cplocal", s_cplocal, 0},
821 {"cprestore", s_cprestore, 0},
822 {"cpreturn", s_cpreturn, 0},
823 {"gpvalue", s_gpvalue, 0},
824 {"gpword", s_gpword, 0},
825 {"cpadd", s_cpadd, 0},
826 {"insn", s_insn, 0},
827
828 /* Relatively generic pseudo-ops that happen to be used on MIPS
829 chips. */
830 {"asciiz", stringer, 1},
831 {"bss", s_change_sec, 'b'},
832 {"err", s_err, 0},
833 {"half", s_cons, 1},
834 {"dword", s_cons, 3},
835 {"weakext", s_mips_weakext, 0},
836
837 /* These pseudo-ops are defined in read.c, but must be overridden
838 here for one reason or another. */
839 {"align", s_align, 0},
840 {"byte", s_cons, 0},
841 {"data", s_change_sec, 'd'},
842 {"double", s_float_cons, 'd'},
843 {"float", s_float_cons, 'f'},
844 {"globl", s_mips_globl, 0},
845 {"global", s_mips_globl, 0},
846 {"hword", s_cons, 1},
847 {"int", s_cons, 2},
848 {"long", s_cons, 2},
849 {"octa", s_cons, 4},
850 {"quad", s_cons, 3},
851 {"short", s_cons, 1},
852 {"single", s_float_cons, 'f'},
853 {"stabn", s_mips_stab, 'n'},
854 {"text", s_change_sec, 't'},
855 {"word", s_cons, 2},
856
857 #ifdef MIPS_STABS_ELF
858 { "extern", ecoff_directive_extern, 0},
859 #endif
860
861 { NULL, NULL, 0 },
862 };
863
864 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
865 {
866 /* These pseudo-ops should be defined by the object file format.
867 However, a.out doesn't support them, so we have versions here. */
868 {"aent", s_mips_ent, 1},
869 {"bgnb", s_ignore, 0},
870 {"end", s_mips_end, 0},
871 {"endb", s_ignore, 0},
872 {"ent", s_mips_ent, 0},
873 {"file", s_file, 0},
874 {"fmask", s_mips_mask, 'F'},
875 {"frame", s_mips_frame, 0},
876 {"loc", s_ignore, 0},
877 {"mask", s_mips_mask, 'R'},
878 {"verstamp", s_ignore, 0},
879 { NULL, NULL, 0 },
880 };
881
882 extern void pop_insert PARAMS ((const pseudo_typeS *));
883
884 void
885 mips_pop_insert ()
886 {
887 pop_insert (mips_pseudo_table);
888 if (! ECOFF_DEBUGGING)
889 pop_insert (mips_nonecoff_pseudo_table);
890 }
891 \f
892 /* Symbols labelling the current insn. */
893
894 struct insn_label_list
895 {
896 struct insn_label_list *next;
897 symbolS *label;
898 };
899
900 static struct insn_label_list *insn_labels;
901 static struct insn_label_list *free_insn_labels;
902
903 static void mips_clear_insn_labels PARAMS ((void));
904
905 static inline void
906 mips_clear_insn_labels ()
907 {
908 register struct insn_label_list **pl;
909
910 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
911 ;
912 *pl = insn_labels;
913 insn_labels = NULL;
914 }
915 \f
916 static char *expr_end;
917
918 /* Expressions which appear in instructions. These are set by
919 mips_ip. */
920
921 static expressionS imm_expr;
922 static expressionS offset_expr;
923
924 /* Relocs associated with imm_expr and offset_expr. */
925
926 static bfd_reloc_code_real_type imm_reloc[3]
927 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
928 static bfd_reloc_code_real_type offset_reloc[3]
929 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
930
931 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
932
933 static boolean imm_unmatched_hi;
934
935 /* These are set by mips16_ip if an explicit extension is used. */
936
937 static boolean mips16_small, mips16_ext;
938
939 #ifdef MIPS_STABS_ELF
940 /* The pdr segment for per procedure frame/regmask info */
941
942 static segT pdr_seg;
943 #endif
944
945 static const char *
946 mips_isa_to_str (isa)
947 int isa;
948 {
949 const struct mips_cpu_info *ci;
950 static char s[20];
951
952 ci = mips_cpu_info_from_isa (isa);
953 if (ci != NULL)
954 return (ci->name);
955
956 sprintf (s, "ISA#%d", isa);
957 return s;
958 }
959
960 static const char *
961 mips_cpu_to_str (cpu)
962 int cpu;
963 {
964 const struct mips_cpu_info *ci;
965 static char s[16];
966
967 ci = mips_cpu_info_from_cpu (cpu);
968 if (ci != NULL)
969 return (ci->name);
970
971 sprintf (s, "CPU#%d", cpu);
972 return s;
973 }
974
975 /* The default target format to use. */
976
977 const char *
978 mips_target_format ()
979 {
980 switch (OUTPUT_FLAVOR)
981 {
982 case bfd_target_aout_flavour:
983 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
984 case bfd_target_ecoff_flavour:
985 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
986 case bfd_target_coff_flavour:
987 return "pe-mips";
988 case bfd_target_elf_flavour:
989 #ifdef TE_TMIPS
990 /* This is traditional mips */
991 return (target_big_endian
992 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
993 : "elf32-tradbigmips")
994 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
995 : "elf32-tradlittlemips"));
996 #else
997 return (target_big_endian
998 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
999 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1000 : "elf32-littlemips"));
1001 #endif
1002 default:
1003 abort ();
1004 return NULL;
1005 }
1006 }
1007
1008 /* This function is called once, at assembler startup time. It should
1009 set up all the tables, etc. that the MD part of the assembler will need. */
1010
1011 void
1012 md_begin ()
1013 {
1014 register const char *retval = NULL;
1015 int i = 0;
1016 const char *cpu;
1017 char *a = NULL;
1018 int broken = 0;
1019 int mips_isa_from_cpu;
1020 int target_cpu_had_mips16 = 0;
1021 const struct mips_cpu_info *ci;
1022
1023 /* GP relative stuff not working for PE */
1024 if (strncmp (TARGET_OS, "pe", 2) == 0
1025 && g_switch_value != 0)
1026 {
1027 if (g_switch_seen)
1028 as_bad (_("-G not supported in this configuration."));
1029 g_switch_value = 0;
1030 }
1031
1032 cpu = TARGET_CPU;
1033 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1034 {
1035 a = xmalloc (sizeof TARGET_CPU);
1036 strcpy (a, TARGET_CPU);
1037 a[(sizeof TARGET_CPU) - 3] = '\0';
1038 cpu = a;
1039 }
1040
1041 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1042 {
1043 target_cpu_had_mips16 = 1;
1044 cpu += sizeof "mips16" - 1;
1045 }
1046
1047 if (mips_opts.mips16 < 0)
1048 mips_opts.mips16 = target_cpu_had_mips16;
1049
1050 /* Backward compatibility for historic -mcpu= option. Check for
1051 incompatible options, warn if -mcpu is used. */
1052 if (mips_cpu != CPU_UNKNOWN
1053 && mips_arch != CPU_UNKNOWN
1054 && mips_cpu != mips_arch)
1055 {
1056 as_fatal (_("The -mcpu option can't be used together with -march. "
1057 "Use -mtune instead of -mcpu."));
1058 }
1059
1060 if (mips_cpu != CPU_UNKNOWN
1061 && mips_tune != CPU_UNKNOWN
1062 && mips_cpu != mips_tune)
1063 {
1064 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1065 "Use -march instead of -mcpu."));
1066 }
1067
1068 #if 1
1069 /* For backward compatibility, let -mipsN set various defaults. */
1070 /* This code should go away, to be replaced with something rather more
1071 draconian. Until GCC 3.1 has been released for some reasonable
1072 amount of time, however, we need to support this. */
1073 if (mips_opts.isa != ISA_UNKNOWN)
1074 {
1075 /* Translate -mipsN to the appropriate settings of file_mips_gp32
1076 and file_mips_fp32. Tag binaries as using the mipsN ISA. */
1077 if (file_mips_gp32 < 0)
1078 {
1079 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1080 file_mips_gp32 = 0;
1081 else
1082 file_mips_gp32 = 1;
1083 }
1084 if (file_mips_fp32 < 0)
1085 {
1086 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1087 file_mips_fp32 = 0;
1088 else
1089 file_mips_fp32 = 1;
1090 }
1091
1092 ci = mips_cpu_info_from_isa (mips_opts.isa);
1093 assert (ci != NULL);
1094 /* -mipsN has higher priority than -mcpu but lower than -march. */
1095 if (mips_arch == CPU_UNKNOWN)
1096 mips_arch = ci->cpu;
1097
1098 /* Default mips_abi. */
1099 if (mips_opts.abi == NO_ABI)
1100 {
1101 if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1102 mips_opts.abi = O32_ABI;
1103 else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1104 mips_opts.abi = O64_ABI;
1105 }
1106 }
1107
1108 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1109 {
1110 ci = mips_cpu_info_from_cpu (mips_cpu);
1111 assert (ci != NULL);
1112 mips_arch = ci->cpu;
1113 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1114 "-mtune instead."));
1115 }
1116
1117 /* Set tune from -mcpu, not from -mipsN. */
1118 if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1119 {
1120 ci = mips_cpu_info_from_cpu (mips_cpu);
1121 assert (ci != NULL);
1122 mips_tune = ci->cpu;
1123 }
1124
1125 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1126 specified on the command line, or some other value if one was.
1127 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1128 the command line, or will be set otherwise if one was. */
1129
1130 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1131 /* Handled above. */;
1132 #else
1133 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1134 {
1135 ci = mips_cpu_info_from_cpu (mips_cpu);
1136 assert (ci != NULL);
1137 mips_arch = ci->cpu;
1138 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1139 "-mtune instead."));
1140 }
1141
1142 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1143 specified on the command line, or some other value if one was.
1144 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1145 the command line, or will be set otherwise if one was. */
1146
1147 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1148 {
1149 /* We have to check if the isa is the default isa of arch. Otherwise
1150 we'll get invalid object file headers. */
1151 ci = mips_cpu_info_from_cpu (mips_arch);
1152 assert (ci != NULL);
1153 if (mips_opts.isa != ci->isa)
1154 {
1155 /* This really should be an error instead of a warning, but old
1156 compilers only have -mcpu which sets both arch and tune. For
1157 now, we discard arch and preserve tune. */
1158 as_warn (_("The -march option is incompatible to -mipsN and "
1159 "therefore ignored."));
1160 if (mips_tune == CPU_UNKNOWN)
1161 mips_tune = mips_arch;
1162 ci = mips_cpu_info_from_isa (mips_opts.isa);
1163 assert (ci != NULL);
1164 mips_arch = ci->cpu;
1165 }
1166 }
1167 #endif
1168 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1169 {
1170 /* We have ARCH, we need ISA. */
1171 ci = mips_cpu_info_from_cpu (mips_arch);
1172 assert (ci != NULL);
1173 mips_opts.isa = ci->isa;
1174 }
1175 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1176 {
1177 /* We have ISA, we need default ARCH. */
1178 ci = mips_cpu_info_from_isa (mips_opts.isa);
1179 assert (ci != NULL);
1180 mips_arch = ci->cpu;
1181 }
1182 else
1183 {
1184 /* We need to set both ISA and ARCH from target cpu. */
1185 ci = mips_cpu_info_from_name (cpu);
1186 if (ci == NULL)
1187 ci = mips_cpu_info_from_cpu (CPU_R3000);
1188 assert (ci != NULL);
1189 mips_opts.isa = ci->isa;
1190 mips_arch = ci->cpu;
1191 }
1192
1193 if (mips_tune == CPU_UNKNOWN)
1194 mips_tune = mips_arch;
1195
1196 ci = mips_cpu_info_from_cpu (mips_arch);
1197 assert (ci != NULL);
1198 mips_isa_from_cpu = ci->isa;
1199
1200 /* End of TARGET_CPU processing, get rid of malloced memory
1201 if necessary. */
1202 cpu = NULL;
1203 if (a != NULL)
1204 {
1205 free (a);
1206 a = NULL;
1207 }
1208
1209 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1210 as_bad (_("trap exception not supported at ISA 1"));
1211
1212 /* Set the EABI kind based on the ISA before the user gets
1213 to change the ISA with directives. This isn't really
1214 the best, but then neither is basing the abi on the isa. */
1215 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1216 && mips_opts.abi == EABI_ABI)
1217 mips_eabi64 = 1;
1218
1219 /* If they asked for mips1 or mips2 and a cpu that is
1220 mips3 or greater, then mark the object file 32BITMODE. */
1221 if (mips_isa_from_cpu != ISA_UNKNOWN
1222 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1223 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1224 mips_32bitmode = 1;
1225
1226 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1227 as_warn (_("Could not set architecture and machine"));
1228
1229 if (file_mips_gp32 < 0)
1230 file_mips_gp32 = 0;
1231 if (file_mips_fp32 < 0)
1232 file_mips_fp32 = 0;
1233
1234 file_mips_isa = mips_opts.isa;
1235 file_mips_abi = mips_opts.abi;
1236 mips_opts.gp32 = file_mips_gp32;
1237 mips_opts.fp32 = file_mips_fp32;
1238
1239 op_hash = hash_new ();
1240
1241 for (i = 0; i < NUMOPCODES;)
1242 {
1243 const char *name = mips_opcodes[i].name;
1244
1245 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1246 if (retval != NULL)
1247 {
1248 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1249 mips_opcodes[i].name, retval);
1250 /* Probably a memory allocation problem? Give up now. */
1251 as_fatal (_("Broken assembler. No assembly attempted."));
1252 }
1253 do
1254 {
1255 if (mips_opcodes[i].pinfo != INSN_MACRO)
1256 {
1257 if (!validate_mips_insn (&mips_opcodes[i]))
1258 broken = 1;
1259 }
1260 ++i;
1261 }
1262 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1263 }
1264
1265 mips16_op_hash = hash_new ();
1266
1267 i = 0;
1268 while (i < bfd_mips16_num_opcodes)
1269 {
1270 const char *name = mips16_opcodes[i].name;
1271
1272 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1273 if (retval != NULL)
1274 as_fatal (_("internal: can't hash `%s': %s"),
1275 mips16_opcodes[i].name, retval);
1276 do
1277 {
1278 if (mips16_opcodes[i].pinfo != INSN_MACRO
1279 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1280 != mips16_opcodes[i].match))
1281 {
1282 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1283 mips16_opcodes[i].name, mips16_opcodes[i].args);
1284 broken = 1;
1285 }
1286 ++i;
1287 }
1288 while (i < bfd_mips16_num_opcodes
1289 && strcmp (mips16_opcodes[i].name, name) == 0);
1290 }
1291
1292 if (broken)
1293 as_fatal (_("Broken assembler. No assembly attempted."));
1294
1295 /* We add all the general register names to the symbol table. This
1296 helps us detect invalid uses of them. */
1297 for (i = 0; i < 32; i++)
1298 {
1299 char buf[5];
1300
1301 sprintf (buf, "$%d", i);
1302 symbol_table_insert (symbol_new (buf, reg_section, i,
1303 &zero_address_frag));
1304 }
1305 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1306 &zero_address_frag));
1307 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1308 &zero_address_frag));
1309 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1310 &zero_address_frag));
1311 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1312 &zero_address_frag));
1313 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1314 &zero_address_frag));
1315 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1316 &zero_address_frag));
1317 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1318 &zero_address_frag));
1319
1320 mips_no_prev_insn (false);
1321
1322 mips_gprmask = 0;
1323 mips_cprmask[0] = 0;
1324 mips_cprmask[1] = 0;
1325 mips_cprmask[2] = 0;
1326 mips_cprmask[3] = 0;
1327
1328 /* set the default alignment for the text section (2**2) */
1329 record_alignment (text_section, 2);
1330
1331 if (USE_GLOBAL_POINTER_OPT)
1332 bfd_set_gp_size (stdoutput, g_switch_value);
1333
1334 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1335 {
1336 /* On a native system, sections must be aligned to 16 byte
1337 boundaries. When configured for an embedded ELF target, we
1338 don't bother. */
1339 if (strcmp (TARGET_OS, "elf") != 0)
1340 {
1341 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1342 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1343 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1344 }
1345
1346 /* Create a .reginfo section for register masks and a .mdebug
1347 section for debugging information. */
1348 {
1349 segT seg;
1350 subsegT subseg;
1351 flagword flags;
1352 segT sec;
1353
1354 seg = now_seg;
1355 subseg = now_subseg;
1356
1357 /* The ABI says this section should be loaded so that the
1358 running program can access it. However, we don't load it
1359 if we are configured for an embedded target */
1360 flags = SEC_READONLY | SEC_DATA;
1361 if (strcmp (TARGET_OS, "elf") != 0)
1362 flags |= SEC_ALLOC | SEC_LOAD;
1363
1364 if (! HAVE_NEWABI)
1365 {
1366 sec = subseg_new (".reginfo", (subsegT) 0);
1367
1368 (void) bfd_set_section_flags (stdoutput, sec, flags);
1369 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1370
1371 #ifdef OBJ_ELF
1372 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1373 #endif
1374 }
1375 else
1376 {
1377 /* The 64-bit ABI uses a .MIPS.options section rather than
1378 .reginfo section. */
1379 sec = subseg_new (".MIPS.options", (subsegT) 0);
1380 (void) bfd_set_section_flags (stdoutput, sec, flags);
1381 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1382
1383 #ifdef OBJ_ELF
1384 /* Set up the option header. */
1385 {
1386 Elf_Internal_Options opthdr;
1387 char *f;
1388
1389 opthdr.kind = ODK_REGINFO;
1390 opthdr.size = (sizeof (Elf_External_Options)
1391 + sizeof (Elf64_External_RegInfo));
1392 opthdr.section = 0;
1393 opthdr.info = 0;
1394 f = frag_more (sizeof (Elf_External_Options));
1395 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1396 (Elf_External_Options *) f);
1397
1398 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1399 }
1400 #endif
1401 }
1402
1403 if (ECOFF_DEBUGGING)
1404 {
1405 sec = subseg_new (".mdebug", (subsegT) 0);
1406 (void) bfd_set_section_flags (stdoutput, sec,
1407 SEC_HAS_CONTENTS | SEC_READONLY);
1408 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1409 }
1410
1411 #ifdef MIPS_STABS_ELF
1412 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1413 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1414 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1415 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1416 #endif
1417
1418 subseg_set (seg, subseg);
1419 }
1420 }
1421
1422 if (! ECOFF_DEBUGGING)
1423 md_obj_begin ();
1424 }
1425
1426 void
1427 md_mips_end ()
1428 {
1429 if (! ECOFF_DEBUGGING)
1430 md_obj_end ();
1431 }
1432
1433 void
1434 md_assemble (str)
1435 char *str;
1436 {
1437 struct mips_cl_insn insn;
1438 bfd_reloc_code_real_type unused_reloc[3]
1439 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1440
1441 imm_expr.X_op = O_absent;
1442 imm_unmatched_hi = false;
1443 offset_expr.X_op = O_absent;
1444 imm_reloc[0] = BFD_RELOC_UNUSED;
1445 imm_reloc[1] = BFD_RELOC_UNUSED;
1446 imm_reloc[2] = BFD_RELOC_UNUSED;
1447 offset_reloc[0] = BFD_RELOC_UNUSED;
1448 offset_reloc[1] = BFD_RELOC_UNUSED;
1449 offset_reloc[2] = BFD_RELOC_UNUSED;
1450
1451 if (mips_opts.mips16)
1452 mips16_ip (str, &insn);
1453 else
1454 {
1455 mips_ip (str, &insn);
1456 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1457 str, insn.insn_opcode));
1458 }
1459
1460 if (insn_error)
1461 {
1462 as_bad ("%s `%s'", insn_error, str);
1463 return;
1464 }
1465
1466 if (insn.insn_mo->pinfo == INSN_MACRO)
1467 {
1468 if (mips_opts.mips16)
1469 mips16_macro (&insn);
1470 else
1471 macro (&insn);
1472 }
1473 else
1474 {
1475 if (imm_expr.X_op != O_absent)
1476 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1477 else if (offset_expr.X_op != O_absent)
1478 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1479 else
1480 append_insn (NULL, &insn, NULL, unused_reloc, false);
1481 }
1482 }
1483
1484 /* See whether instruction IP reads register REG. CLASS is the type
1485 of register. */
1486
1487 static int
1488 insn_uses_reg (ip, reg, class)
1489 struct mips_cl_insn *ip;
1490 unsigned int reg;
1491 enum mips_regclass class;
1492 {
1493 if (class == MIPS16_REG)
1494 {
1495 assert (mips_opts.mips16);
1496 reg = mips16_to_32_reg_map[reg];
1497 class = MIPS_GR_REG;
1498 }
1499
1500 /* Don't report on general register 0, since it never changes. */
1501 if (class == MIPS_GR_REG && reg == 0)
1502 return 0;
1503
1504 if (class == MIPS_FP_REG)
1505 {
1506 assert (! mips_opts.mips16);
1507 /* If we are called with either $f0 or $f1, we must check $f0.
1508 This is not optimal, because it will introduce an unnecessary
1509 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1510 need to distinguish reading both $f0 and $f1 or just one of
1511 them. Note that we don't have to check the other way,
1512 because there is no instruction that sets both $f0 and $f1
1513 and requires a delay. */
1514 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1515 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1516 == (reg &~ (unsigned) 1)))
1517 return 1;
1518 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1519 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1520 == (reg &~ (unsigned) 1)))
1521 return 1;
1522 }
1523 else if (! mips_opts.mips16)
1524 {
1525 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1526 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1527 return 1;
1528 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1529 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1530 return 1;
1531 }
1532 else
1533 {
1534 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1535 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1536 & MIPS16OP_MASK_RX)]
1537 == reg))
1538 return 1;
1539 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1540 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1541 & MIPS16OP_MASK_RY)]
1542 == reg))
1543 return 1;
1544 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1545 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1546 & MIPS16OP_MASK_MOVE32Z)]
1547 == reg))
1548 return 1;
1549 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1550 return 1;
1551 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1552 return 1;
1553 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1554 return 1;
1555 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1556 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1557 & MIPS16OP_MASK_REGR32) == reg)
1558 return 1;
1559 }
1560
1561 return 0;
1562 }
1563
1564 /* This function returns true if modifying a register requires a
1565 delay. */
1566
1567 static int
1568 reg_needs_delay (reg)
1569 unsigned int reg;
1570 {
1571 unsigned long prev_pinfo;
1572
1573 prev_pinfo = prev_insn.insn_mo->pinfo;
1574 if (! mips_opts.noreorder
1575 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1576 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1577 || (! gpr_interlocks
1578 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1579 {
1580 /* A load from a coprocessor or from memory. All load
1581 delays delay the use of general register rt for one
1582 instruction on the r3000. The r6000 and r4000 use
1583 interlocks. */
1584 /* Itbl support may require additional care here. */
1585 know (prev_pinfo & INSN_WRITE_GPR_T);
1586 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1587 return 1;
1588 }
1589
1590 return 0;
1591 }
1592
1593 /* Mark instruction labels in mips16 mode. This permits the linker to
1594 handle them specially, such as generating jalx instructions when
1595 needed. We also make them odd for the duration of the assembly, in
1596 order to generate the right sort of code. We will make them even
1597 in the adjust_symtab routine, while leaving them marked. This is
1598 convenient for the debugger and the disassembler. The linker knows
1599 to make them odd again. */
1600
1601 static void
1602 mips16_mark_labels ()
1603 {
1604 if (mips_opts.mips16)
1605 {
1606 struct insn_label_list *l;
1607 valueT val;
1608
1609 for (l = insn_labels; l != NULL; l = l->next)
1610 {
1611 #ifdef OBJ_ELF
1612 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1613 S_SET_OTHER (l->label, STO_MIPS16);
1614 #endif
1615 val = S_GET_VALUE (l->label);
1616 if ((val & 1) == 0)
1617 S_SET_VALUE (l->label, val + 1);
1618 }
1619 }
1620 }
1621
1622 /* Output an instruction. PLACE is where to put the instruction; if
1623 it is NULL, this uses frag_more to get room. IP is the instruction
1624 information. ADDRESS_EXPR is an operand of the instruction to be
1625 used with RELOC_TYPE. */
1626
1627 static void
1628 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1629 char *place;
1630 struct mips_cl_insn *ip;
1631 expressionS *address_expr;
1632 bfd_reloc_code_real_type *reloc_type;
1633 boolean unmatched_hi;
1634 {
1635 register unsigned long prev_pinfo, pinfo;
1636 char *f;
1637 fixS *fixp[3];
1638 int nops = 0;
1639
1640 /* Mark instruction labels in mips16 mode. */
1641 if (mips_opts.mips16)
1642 mips16_mark_labels ();
1643
1644 prev_pinfo = prev_insn.insn_mo->pinfo;
1645 pinfo = ip->insn_mo->pinfo;
1646
1647 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1648 {
1649 int prev_prev_nop;
1650
1651 /* If the previous insn required any delay slots, see if we need
1652 to insert a NOP or two. There are eight kinds of possible
1653 hazards, of which an instruction can have at most one type.
1654 (1) a load from memory delay
1655 (2) a load from a coprocessor delay
1656 (3) an unconditional branch delay
1657 (4) a conditional branch delay
1658 (5) a move to coprocessor register delay
1659 (6) a load coprocessor register from memory delay
1660 (7) a coprocessor condition code delay
1661 (8) a HI/LO special register delay
1662
1663 There are a lot of optimizations we could do that we don't.
1664 In particular, we do not, in general, reorder instructions.
1665 If you use gcc with optimization, it will reorder
1666 instructions and generally do much more optimization then we
1667 do here; repeating all that work in the assembler would only
1668 benefit hand written assembly code, and does not seem worth
1669 it. */
1670
1671 /* This is how a NOP is emitted. */
1672 #define emit_nop() \
1673 (mips_opts.mips16 \
1674 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1675 : md_number_to_chars (frag_more (4), 0, 4))
1676
1677 /* The previous insn might require a delay slot, depending upon
1678 the contents of the current insn. */
1679 if (! mips_opts.mips16
1680 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1681 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1682 && ! cop_interlocks)
1683 || (! gpr_interlocks
1684 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1685 {
1686 /* A load from a coprocessor or from memory. All load
1687 delays delay the use of general register rt for one
1688 instruction on the r3000. The r6000 and r4000 use
1689 interlocks. */
1690 /* Itbl support may require additional care here. */
1691 know (prev_pinfo & INSN_WRITE_GPR_T);
1692 if (mips_optimize == 0
1693 || insn_uses_reg (ip,
1694 ((prev_insn.insn_opcode >> OP_SH_RT)
1695 & OP_MASK_RT),
1696 MIPS_GR_REG))
1697 ++nops;
1698 }
1699 else if (! mips_opts.mips16
1700 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1701 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1702 && ! cop_interlocks)
1703 || (mips_opts.isa == ISA_MIPS1
1704 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1705 {
1706 /* A generic coprocessor delay. The previous instruction
1707 modified a coprocessor general or control register. If
1708 it modified a control register, we need to avoid any
1709 coprocessor instruction (this is probably not always
1710 required, but it sometimes is). If it modified a general
1711 register, we avoid using that register.
1712
1713 On the r6000 and r4000 loading a coprocessor register
1714 from memory is interlocked, and does not require a delay.
1715
1716 This case is not handled very well. There is no special
1717 knowledge of CP0 handling, and the coprocessors other
1718 than the floating point unit are not distinguished at
1719 all. */
1720 /* Itbl support may require additional care here. FIXME!
1721 Need to modify this to include knowledge about
1722 user specified delays! */
1723 if (prev_pinfo & INSN_WRITE_FPR_T)
1724 {
1725 if (mips_optimize == 0
1726 || insn_uses_reg (ip,
1727 ((prev_insn.insn_opcode >> OP_SH_FT)
1728 & OP_MASK_FT),
1729 MIPS_FP_REG))
1730 ++nops;
1731 }
1732 else if (prev_pinfo & INSN_WRITE_FPR_S)
1733 {
1734 if (mips_optimize == 0
1735 || insn_uses_reg (ip,
1736 ((prev_insn.insn_opcode >> OP_SH_FS)
1737 & OP_MASK_FS),
1738 MIPS_FP_REG))
1739 ++nops;
1740 }
1741 else
1742 {
1743 /* We don't know exactly what the previous instruction
1744 does. If the current instruction uses a coprocessor
1745 register, we must insert a NOP. If previous
1746 instruction may set the condition codes, and the
1747 current instruction uses them, we must insert two
1748 NOPS. */
1749 /* Itbl support may require additional care here. */
1750 if (mips_optimize == 0
1751 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1752 && (pinfo & INSN_READ_COND_CODE)))
1753 nops += 2;
1754 else if (pinfo & INSN_COP)
1755 ++nops;
1756 }
1757 }
1758 else if (! mips_opts.mips16
1759 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1760 && (prev_pinfo & INSN_WRITE_COND_CODE)
1761 && ! cop_interlocks)
1762 {
1763 /* The previous instruction sets the coprocessor condition
1764 codes, but does not require a general coprocessor delay
1765 (this means it is a floating point comparison
1766 instruction). If this instruction uses the condition
1767 codes, we need to insert a single NOP. */
1768 /* Itbl support may require additional care here. */
1769 if (mips_optimize == 0
1770 || (pinfo & INSN_READ_COND_CODE))
1771 ++nops;
1772 }
1773
1774 /* If we're fixing up mfhi/mflo for the r7000 and the
1775 previous insn was an mfhi/mflo and the current insn
1776 reads the register that the mfhi/mflo wrote to, then
1777 insert two nops. */
1778
1779 else if (mips_7000_hilo_fix
1780 && MF_HILO_INSN (prev_pinfo)
1781 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1782 & OP_MASK_RD),
1783 MIPS_GR_REG))
1784 {
1785 nops += 2;
1786 }
1787
1788 /* If we're fixing up mfhi/mflo for the r7000 and the
1789 2nd previous insn was an mfhi/mflo and the current insn
1790 reads the register that the mfhi/mflo wrote to, then
1791 insert one nop. */
1792
1793 else if (mips_7000_hilo_fix
1794 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1795 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1796 & OP_MASK_RD),
1797 MIPS_GR_REG))
1798
1799 {
1800 nops += 1;
1801 }
1802
1803 else if (prev_pinfo & INSN_READ_LO)
1804 {
1805 /* The previous instruction reads the LO register; if the
1806 current instruction writes to the LO register, we must
1807 insert two NOPS. Some newer processors have interlocks.
1808 Also the tx39's multiply instructions can be exectuted
1809 immediatly after a read from HI/LO (without the delay),
1810 though the tx39's divide insns still do require the
1811 delay. */
1812 if (! (hilo_interlocks
1813 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1814 && (mips_optimize == 0
1815 || (pinfo & INSN_WRITE_LO)))
1816 nops += 2;
1817 /* Most mips16 branch insns don't have a delay slot.
1818 If a read from LO is immediately followed by a branch
1819 to a write to LO we have a read followed by a write
1820 less than 2 insns away. We assume the target of
1821 a branch might be a write to LO, and insert a nop
1822 between a read and an immediately following branch. */
1823 else if (mips_opts.mips16
1824 && (mips_optimize == 0
1825 || (pinfo & MIPS16_INSN_BRANCH)))
1826 nops += 1;
1827 }
1828 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1829 {
1830 /* The previous instruction reads the HI register; if the
1831 current instruction writes to the HI register, we must
1832 insert a NOP. Some newer processors have interlocks.
1833 Also the note tx39's multiply above. */
1834 if (! (hilo_interlocks
1835 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1836 && (mips_optimize == 0
1837 || (pinfo & INSN_WRITE_HI)))
1838 nops += 2;
1839 /* Most mips16 branch insns don't have a delay slot.
1840 If a read from HI is immediately followed by a branch
1841 to a write to HI we have a read followed by a write
1842 less than 2 insns away. We assume the target of
1843 a branch might be a write to HI, and insert a nop
1844 between a read and an immediately following branch. */
1845 else if (mips_opts.mips16
1846 && (mips_optimize == 0
1847 || (pinfo & MIPS16_INSN_BRANCH)))
1848 nops += 1;
1849 }
1850
1851 /* If the previous instruction was in a noreorder section, then
1852 we don't want to insert the nop after all. */
1853 /* Itbl support may require additional care here. */
1854 if (prev_insn_unreordered)
1855 nops = 0;
1856
1857 /* There are two cases which require two intervening
1858 instructions: 1) setting the condition codes using a move to
1859 coprocessor instruction which requires a general coprocessor
1860 delay and then reading the condition codes 2) reading the HI
1861 or LO register and then writing to it (except on processors
1862 which have interlocks). If we are not already emitting a NOP
1863 instruction, we must check for these cases compared to the
1864 instruction previous to the previous instruction. */
1865 if ((! mips_opts.mips16
1866 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1867 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1868 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1869 && (pinfo & INSN_READ_COND_CODE)
1870 && ! cop_interlocks)
1871 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1872 && (pinfo & INSN_WRITE_LO)
1873 && ! (hilo_interlocks
1874 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1875 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1876 && (pinfo & INSN_WRITE_HI)
1877 && ! (hilo_interlocks
1878 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1879 prev_prev_nop = 1;
1880 else
1881 prev_prev_nop = 0;
1882
1883 if (prev_prev_insn_unreordered)
1884 prev_prev_nop = 0;
1885
1886 if (prev_prev_nop && nops == 0)
1887 ++nops;
1888
1889 /* If we are being given a nop instruction, don't bother with
1890 one of the nops we would otherwise output. This will only
1891 happen when a nop instruction is used with mips_optimize set
1892 to 0. */
1893 if (nops > 0
1894 && ! mips_opts.noreorder
1895 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1896 --nops;
1897
1898 /* Now emit the right number of NOP instructions. */
1899 if (nops > 0 && ! mips_opts.noreorder)
1900 {
1901 fragS *old_frag;
1902 unsigned long old_frag_offset;
1903 int i;
1904 struct insn_label_list *l;
1905
1906 old_frag = frag_now;
1907 old_frag_offset = frag_now_fix ();
1908
1909 for (i = 0; i < nops; i++)
1910 emit_nop ();
1911
1912 if (listing)
1913 {
1914 listing_prev_line ();
1915 /* We may be at the start of a variant frag. In case we
1916 are, make sure there is enough space for the frag
1917 after the frags created by listing_prev_line. The
1918 argument to frag_grow here must be at least as large
1919 as the argument to all other calls to frag_grow in
1920 this file. We don't have to worry about being in the
1921 middle of a variant frag, because the variants insert
1922 all needed nop instructions themselves. */
1923 frag_grow (40);
1924 }
1925
1926 for (l = insn_labels; l != NULL; l = l->next)
1927 {
1928 valueT val;
1929
1930 assert (S_GET_SEGMENT (l->label) == now_seg);
1931 symbol_set_frag (l->label, frag_now);
1932 val = (valueT) frag_now_fix ();
1933 /* mips16 text labels are stored as odd. */
1934 if (mips_opts.mips16)
1935 val += 1;
1936 S_SET_VALUE (l->label, val);
1937 }
1938
1939 #ifndef NO_ECOFF_DEBUGGING
1940 if (ECOFF_DEBUGGING)
1941 ecoff_fix_loc (old_frag, old_frag_offset);
1942 #endif
1943 }
1944 else if (prev_nop_frag != NULL)
1945 {
1946 /* We have a frag holding nops we may be able to remove. If
1947 we don't need any nops, we can decrease the size of
1948 prev_nop_frag by the size of one instruction. If we do
1949 need some nops, we count them in prev_nops_required. */
1950 if (prev_nop_frag_since == 0)
1951 {
1952 if (nops == 0)
1953 {
1954 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1955 --prev_nop_frag_holds;
1956 }
1957 else
1958 prev_nop_frag_required += nops;
1959 }
1960 else
1961 {
1962 if (prev_prev_nop == 0)
1963 {
1964 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1965 --prev_nop_frag_holds;
1966 }
1967 else
1968 ++prev_nop_frag_required;
1969 }
1970
1971 if (prev_nop_frag_holds <= prev_nop_frag_required)
1972 prev_nop_frag = NULL;
1973
1974 ++prev_nop_frag_since;
1975
1976 /* Sanity check: by the time we reach the second instruction
1977 after prev_nop_frag, we should have used up all the nops
1978 one way or another. */
1979 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1980 }
1981 }
1982
1983 if (*reloc_type > BFD_RELOC_UNUSED)
1984 {
1985 /* We need to set up a variant frag. */
1986 assert (mips_opts.mips16 && address_expr != NULL);
1987 f = frag_var (rs_machine_dependent, 4, 0,
1988 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1989 mips16_small, mips16_ext,
1990 (prev_pinfo
1991 & INSN_UNCOND_BRANCH_DELAY),
1992 (*prev_insn_reloc_type
1993 == BFD_RELOC_MIPS16_JMP)),
1994 make_expr_symbol (address_expr), 0, NULL);
1995 }
1996 else if (place != NULL)
1997 f = place;
1998 else if (mips_opts.mips16
1999 && ! ip->use_extend
2000 && *reloc_type != BFD_RELOC_MIPS16_JMP)
2001 {
2002 /* Make sure there is enough room to swap this instruction with
2003 a following jump instruction. */
2004 frag_grow (6);
2005 f = frag_more (2);
2006 }
2007 else
2008 {
2009 if (mips_opts.mips16
2010 && mips_opts.noreorder
2011 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2012 as_warn (_("extended instruction in delay slot"));
2013
2014 f = frag_more (4);
2015 }
2016
2017 fixp[0] = fixp[1] = fixp[2] = NULL;
2018 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
2019 {
2020 if (address_expr->X_op == O_constant)
2021 {
2022 unsigned long tmp;
2023
2024 switch (*reloc_type)
2025 {
2026 case BFD_RELOC_32:
2027 ip->insn_opcode |= address_expr->X_add_number;
2028 break;
2029
2030 case BFD_RELOC_MIPS_HIGHEST:
2031 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2032 tmp >>= 16;
2033 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2034 break;
2035
2036 case BFD_RELOC_MIPS_HIGHER:
2037 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2038 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2039 break;
2040
2041 case BFD_RELOC_HI16_S:
2042 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2043 >> 16) & 0xffff;
2044 break;
2045
2046 case BFD_RELOC_HI16:
2047 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2048 break;
2049
2050 case BFD_RELOC_LO16:
2051 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2052 break;
2053
2054 case BFD_RELOC_MIPS_JMP:
2055 if ((address_expr->X_add_number & 3) != 0)
2056 as_bad (_("jump to misaligned address (0x%lx)"),
2057 (unsigned long) address_expr->X_add_number);
2058 if (address_expr->X_add_number & ~0xfffffff
2059 || address_expr->X_add_number > 0x7fffffc)
2060 as_bad (_("jump address range overflow (0x%lx)"),
2061 (unsigned long) address_expr->X_add_number);
2062 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2063 break;
2064
2065 case BFD_RELOC_MIPS16_JMP:
2066 if ((address_expr->X_add_number & 3) != 0)
2067 as_bad (_("jump to misaligned address (0x%lx)"),
2068 (unsigned long) address_expr->X_add_number);
2069 if (address_expr->X_add_number & ~0xfffffff
2070 || address_expr->X_add_number > 0x7fffffc)
2071 as_bad (_("jump address range overflow (0x%lx)"),
2072 (unsigned long) address_expr->X_add_number);
2073 ip->insn_opcode |=
2074 (((address_expr->X_add_number & 0x7c0000) << 3)
2075 | ((address_expr->X_add_number & 0xf800000) >> 7)
2076 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2077 break;
2078
2079 case BFD_RELOC_16_PCREL:
2080 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2081 break;
2082
2083 case BFD_RELOC_16_PCREL_S2:
2084 goto need_reloc;
2085
2086 default:
2087 internalError ();
2088 }
2089 }
2090 else
2091 {
2092 need_reloc:
2093 /* Don't generate a reloc if we are writing into a variant frag. */
2094 if (place == NULL)
2095 {
2096 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2097 address_expr,
2098 (*reloc_type == BFD_RELOC_16_PCREL
2099 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2100 reloc_type[0]);
2101
2102 /* These relocations can have an addend that won't fit in
2103 4 octets for 64bit assembly. */
2104 if (HAVE_64BIT_GPRS &&
2105 (*reloc_type == BFD_RELOC_16
2106 || *reloc_type == BFD_RELOC_32
2107 || *reloc_type == BFD_RELOC_MIPS_JMP
2108 || *reloc_type == BFD_RELOC_HI16_S
2109 || *reloc_type == BFD_RELOC_LO16
2110 || *reloc_type == BFD_RELOC_GPREL16
2111 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2112 || *reloc_type == BFD_RELOC_GPREL32
2113 || *reloc_type == BFD_RELOC_64
2114 || *reloc_type == BFD_RELOC_CTOR
2115 || *reloc_type == BFD_RELOC_MIPS_SUB
2116 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2117 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2118 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2119 || *reloc_type == BFD_RELOC_MIPS_REL16
2120 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2121 fixp[0]->fx_no_overflow = 1;
2122
2123 if (unmatched_hi)
2124 {
2125 struct mips_hi_fixup *hi_fixup;
2126
2127 assert (*reloc_type == BFD_RELOC_HI16_S);
2128 hi_fixup = ((struct mips_hi_fixup *)
2129 xmalloc (sizeof (struct mips_hi_fixup)));
2130 hi_fixup->fixp = fixp[0];
2131 hi_fixup->seg = now_seg;
2132 hi_fixup->next = mips_hi_fixup_list;
2133 mips_hi_fixup_list = hi_fixup;
2134 }
2135
2136 if (reloc_type[1] != BFD_RELOC_UNUSED)
2137 {
2138 /* FIXME: This symbol can be one of
2139 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2140 address_expr->X_op = O_absent;
2141 address_expr->X_add_symbol = 0;
2142 address_expr->X_add_number = 0;
2143
2144 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2145 4, address_expr, false,
2146 reloc_type[1]);
2147
2148 /* These relocations can have an addend that won't fit in
2149 4 octets for 64bit assembly. */
2150 if (HAVE_64BIT_GPRS &&
2151 (*reloc_type == BFD_RELOC_16
2152 || *reloc_type == BFD_RELOC_32
2153 || *reloc_type == BFD_RELOC_MIPS_JMP
2154 || *reloc_type == BFD_RELOC_HI16_S
2155 || *reloc_type == BFD_RELOC_LO16
2156 || *reloc_type == BFD_RELOC_GPREL16
2157 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2158 || *reloc_type == BFD_RELOC_GPREL32
2159 || *reloc_type == BFD_RELOC_64
2160 || *reloc_type == BFD_RELOC_CTOR
2161 || *reloc_type == BFD_RELOC_MIPS_SUB
2162 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2163 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2164 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2165 || *reloc_type == BFD_RELOC_MIPS_REL16
2166 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2167 fixp[1]->fx_no_overflow = 1;
2168
2169 if (reloc_type[2] != BFD_RELOC_UNUSED)
2170 {
2171 address_expr->X_op = O_absent;
2172 address_expr->X_add_symbol = 0;
2173 address_expr->X_add_number = 0;
2174
2175 fixp[2] = fix_new_exp (frag_now,
2176 f - frag_now->fr_literal, 4,
2177 address_expr, false,
2178 reloc_type[2]);
2179
2180 /* These relocations can have an addend that won't fit in
2181 4 octets for 64bit assembly. */
2182 if (HAVE_64BIT_GPRS &&
2183 (*reloc_type == BFD_RELOC_16
2184 || *reloc_type == BFD_RELOC_32
2185 || *reloc_type == BFD_RELOC_MIPS_JMP
2186 || *reloc_type == BFD_RELOC_HI16_S
2187 || *reloc_type == BFD_RELOC_LO16
2188 || *reloc_type == BFD_RELOC_GPREL16
2189 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2190 || *reloc_type == BFD_RELOC_GPREL32
2191 || *reloc_type == BFD_RELOC_64
2192 || *reloc_type == BFD_RELOC_CTOR
2193 || *reloc_type == BFD_RELOC_MIPS_SUB
2194 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2195 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2196 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2197 || *reloc_type == BFD_RELOC_MIPS_REL16
2198 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2199 fixp[2]->fx_no_overflow = 1;
2200 }
2201 }
2202 }
2203 }
2204 }
2205
2206 if (! mips_opts.mips16)
2207 md_number_to_chars (f, ip->insn_opcode, 4);
2208 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2209 {
2210 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2211 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2212 }
2213 else
2214 {
2215 if (ip->use_extend)
2216 {
2217 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2218 f += 2;
2219 }
2220 md_number_to_chars (f, ip->insn_opcode, 2);
2221 }
2222
2223 /* Update the register mask information. */
2224 if (! mips_opts.mips16)
2225 {
2226 if (pinfo & INSN_WRITE_GPR_D)
2227 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2228 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2229 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2230 if (pinfo & INSN_READ_GPR_S)
2231 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2232 if (pinfo & INSN_WRITE_GPR_31)
2233 mips_gprmask |= 1 << 31;
2234 if (pinfo & INSN_WRITE_FPR_D)
2235 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2236 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2237 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2238 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2239 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2240 if ((pinfo & INSN_READ_FPR_R) != 0)
2241 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2242 if (pinfo & INSN_COP)
2243 {
2244 /* We don't keep enough information to sort these cases out.
2245 The itbl support does keep this information however, although
2246 we currently don't support itbl fprmats as part of the cop
2247 instruction. May want to add this support in the future. */
2248 }
2249 /* Never set the bit for $0, which is always zero. */
2250 mips_gprmask &= ~1 << 0;
2251 }
2252 else
2253 {
2254 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2255 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2256 & MIPS16OP_MASK_RX);
2257 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2258 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2259 & MIPS16OP_MASK_RY);
2260 if (pinfo & MIPS16_INSN_WRITE_Z)
2261 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2262 & MIPS16OP_MASK_RZ);
2263 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2264 mips_gprmask |= 1 << TREG;
2265 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2266 mips_gprmask |= 1 << SP;
2267 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2268 mips_gprmask |= 1 << RA;
2269 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2270 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2271 if (pinfo & MIPS16_INSN_READ_Z)
2272 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2273 & MIPS16OP_MASK_MOVE32Z);
2274 if (pinfo & MIPS16_INSN_READ_GPR_X)
2275 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2276 & MIPS16OP_MASK_REGR32);
2277 }
2278
2279 if (place == NULL && ! mips_opts.noreorder)
2280 {
2281 /* Filling the branch delay slot is more complex. We try to
2282 switch the branch with the previous instruction, which we can
2283 do if the previous instruction does not set up a condition
2284 that the branch tests and if the branch is not itself the
2285 target of any branch. */
2286 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2287 || (pinfo & INSN_COND_BRANCH_DELAY))
2288 {
2289 if (mips_optimize < 2
2290 /* If we have seen .set volatile or .set nomove, don't
2291 optimize. */
2292 || mips_opts.nomove != 0
2293 /* If we had to emit any NOP instructions, then we
2294 already know we can not swap. */
2295 || nops != 0
2296 /* If we don't even know the previous insn, we can not
2297 swap. */
2298 || ! prev_insn_valid
2299 /* If the previous insn is already in a branch delay
2300 slot, then we can not swap. */
2301 || prev_insn_is_delay_slot
2302 /* If the previous previous insn was in a .set
2303 noreorder, we can't swap. Actually, the MIPS
2304 assembler will swap in this situation. However, gcc
2305 configured -with-gnu-as will generate code like
2306 .set noreorder
2307 lw $4,XXX
2308 .set reorder
2309 INSN
2310 bne $4,$0,foo
2311 in which we can not swap the bne and INSN. If gcc is
2312 not configured -with-gnu-as, it does not output the
2313 .set pseudo-ops. We don't have to check
2314 prev_insn_unreordered, because prev_insn_valid will
2315 be 0 in that case. We don't want to use
2316 prev_prev_insn_valid, because we do want to be able
2317 to swap at the start of a function. */
2318 || prev_prev_insn_unreordered
2319 /* If the branch is itself the target of a branch, we
2320 can not swap. We cheat on this; all we check for is
2321 whether there is a label on this instruction. If
2322 there are any branches to anything other than a
2323 label, users must use .set noreorder. */
2324 || insn_labels != NULL
2325 /* If the previous instruction is in a variant frag, we
2326 can not do the swap. This does not apply to the
2327 mips16, which uses variant frags for different
2328 purposes. */
2329 || (! mips_opts.mips16
2330 && prev_insn_frag->fr_type == rs_machine_dependent)
2331 /* If the branch reads the condition codes, we don't
2332 even try to swap, because in the sequence
2333 ctc1 $X,$31
2334 INSN
2335 INSN
2336 bc1t LABEL
2337 we can not swap, and I don't feel like handling that
2338 case. */
2339 || (! mips_opts.mips16
2340 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2341 && (pinfo & INSN_READ_COND_CODE))
2342 /* We can not swap with an instruction that requires a
2343 delay slot, becase the target of the branch might
2344 interfere with that instruction. */
2345 || (! mips_opts.mips16
2346 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2347 && (prev_pinfo
2348 /* Itbl support may require additional care here. */
2349 & (INSN_LOAD_COPROC_DELAY
2350 | INSN_COPROC_MOVE_DELAY
2351 | INSN_WRITE_COND_CODE)))
2352 || (! (hilo_interlocks
2353 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2354 && (prev_pinfo
2355 & (INSN_READ_LO
2356 | INSN_READ_HI)))
2357 || (! mips_opts.mips16
2358 && ! gpr_interlocks
2359 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2360 || (! mips_opts.mips16
2361 && mips_opts.isa == ISA_MIPS1
2362 /* Itbl support may require additional care here. */
2363 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2364 /* We can not swap with a branch instruction. */
2365 || (prev_pinfo
2366 & (INSN_UNCOND_BRANCH_DELAY
2367 | INSN_COND_BRANCH_DELAY
2368 | INSN_COND_BRANCH_LIKELY))
2369 /* We do not swap with a trap instruction, since it
2370 complicates trap handlers to have the trap
2371 instruction be in a delay slot. */
2372 || (prev_pinfo & INSN_TRAP)
2373 /* If the branch reads a register that the previous
2374 instruction sets, we can not swap. */
2375 || (! mips_opts.mips16
2376 && (prev_pinfo & INSN_WRITE_GPR_T)
2377 && insn_uses_reg (ip,
2378 ((prev_insn.insn_opcode >> OP_SH_RT)
2379 & OP_MASK_RT),
2380 MIPS_GR_REG))
2381 || (! mips_opts.mips16
2382 && (prev_pinfo & INSN_WRITE_GPR_D)
2383 && insn_uses_reg (ip,
2384 ((prev_insn.insn_opcode >> OP_SH_RD)
2385 & OP_MASK_RD),
2386 MIPS_GR_REG))
2387 || (mips_opts.mips16
2388 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2389 && insn_uses_reg (ip,
2390 ((prev_insn.insn_opcode
2391 >> MIPS16OP_SH_RX)
2392 & MIPS16OP_MASK_RX),
2393 MIPS16_REG))
2394 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2395 && insn_uses_reg (ip,
2396 ((prev_insn.insn_opcode
2397 >> MIPS16OP_SH_RY)
2398 & MIPS16OP_MASK_RY),
2399 MIPS16_REG))
2400 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2401 && insn_uses_reg (ip,
2402 ((prev_insn.insn_opcode
2403 >> MIPS16OP_SH_RZ)
2404 & MIPS16OP_MASK_RZ),
2405 MIPS16_REG))
2406 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2407 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2408 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2409 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2410 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2411 && insn_uses_reg (ip,
2412 MIPS16OP_EXTRACT_REG32R (prev_insn.
2413 insn_opcode),
2414 MIPS_GR_REG))))
2415 /* If the branch writes a register that the previous
2416 instruction sets, we can not swap (we know that
2417 branches write only to RD or to $31). */
2418 || (! mips_opts.mips16
2419 && (prev_pinfo & INSN_WRITE_GPR_T)
2420 && (((pinfo & INSN_WRITE_GPR_D)
2421 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2422 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2423 || ((pinfo & INSN_WRITE_GPR_31)
2424 && (((prev_insn.insn_opcode >> OP_SH_RT)
2425 & OP_MASK_RT)
2426 == 31))))
2427 || (! mips_opts.mips16
2428 && (prev_pinfo & INSN_WRITE_GPR_D)
2429 && (((pinfo & INSN_WRITE_GPR_D)
2430 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2431 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2432 || ((pinfo & INSN_WRITE_GPR_31)
2433 && (((prev_insn.insn_opcode >> OP_SH_RD)
2434 & OP_MASK_RD)
2435 == 31))))
2436 || (mips_opts.mips16
2437 && (pinfo & MIPS16_INSN_WRITE_31)
2438 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2439 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2440 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2441 == RA))))
2442 /* If the branch writes a register that the previous
2443 instruction reads, we can not swap (we know that
2444 branches only write to RD or to $31). */
2445 || (! mips_opts.mips16
2446 && (pinfo & INSN_WRITE_GPR_D)
2447 && insn_uses_reg (&prev_insn,
2448 ((ip->insn_opcode >> OP_SH_RD)
2449 & OP_MASK_RD),
2450 MIPS_GR_REG))
2451 || (! mips_opts.mips16
2452 && (pinfo & INSN_WRITE_GPR_31)
2453 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2454 || (mips_opts.mips16
2455 && (pinfo & MIPS16_INSN_WRITE_31)
2456 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2457 /* If we are generating embedded PIC code, the branch
2458 might be expanded into a sequence which uses $at, so
2459 we can't swap with an instruction which reads it. */
2460 || (mips_pic == EMBEDDED_PIC
2461 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2462 /* If the previous previous instruction has a load
2463 delay, and sets a register that the branch reads, we
2464 can not swap. */
2465 || (! mips_opts.mips16
2466 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2467 /* Itbl support may require additional care here. */
2468 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2469 || (! gpr_interlocks
2470 && (prev_prev_insn.insn_mo->pinfo
2471 & INSN_LOAD_MEMORY_DELAY)))
2472 && insn_uses_reg (ip,
2473 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2474 & OP_MASK_RT),
2475 MIPS_GR_REG))
2476 /* If one instruction sets a condition code and the
2477 other one uses a condition code, we can not swap. */
2478 || ((pinfo & INSN_READ_COND_CODE)
2479 && (prev_pinfo & INSN_WRITE_COND_CODE))
2480 || ((pinfo & INSN_WRITE_COND_CODE)
2481 && (prev_pinfo & INSN_READ_COND_CODE))
2482 /* If the previous instruction uses the PC, we can not
2483 swap. */
2484 || (mips_opts.mips16
2485 && (prev_pinfo & MIPS16_INSN_READ_PC))
2486 /* If the previous instruction was extended, we can not
2487 swap. */
2488 || (mips_opts.mips16 && prev_insn_extended)
2489 /* If the previous instruction had a fixup in mips16
2490 mode, we can not swap. This normally means that the
2491 previous instruction was a 4 byte branch anyhow. */
2492 || (mips_opts.mips16 && prev_insn_fixp[0])
2493 /* If the previous instruction is a sync, sync.l, or
2494 sync.p, we can not swap. */
2495 || (prev_pinfo & INSN_SYNC))
2496 {
2497 /* We could do even better for unconditional branches to
2498 portions of this object file; we could pick up the
2499 instruction at the destination, put it in the delay
2500 slot, and bump the destination address. */
2501 emit_nop ();
2502 /* Update the previous insn information. */
2503 prev_prev_insn = *ip;
2504 prev_insn.insn_mo = &dummy_opcode;
2505 }
2506 else
2507 {
2508 /* It looks like we can actually do the swap. */
2509 if (! mips_opts.mips16)
2510 {
2511 char *prev_f;
2512 char temp[4];
2513
2514 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2515 memcpy (temp, prev_f, 4);
2516 memcpy (prev_f, f, 4);
2517 memcpy (f, temp, 4);
2518 if (prev_insn_fixp[0])
2519 {
2520 prev_insn_fixp[0]->fx_frag = frag_now;
2521 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2522 }
2523 if (prev_insn_fixp[1])
2524 {
2525 prev_insn_fixp[1]->fx_frag = frag_now;
2526 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2527 }
2528 if (prev_insn_fixp[2])
2529 {
2530 prev_insn_fixp[2]->fx_frag = frag_now;
2531 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2532 }
2533 if (fixp[0])
2534 {
2535 fixp[0]->fx_frag = prev_insn_frag;
2536 fixp[0]->fx_where = prev_insn_where;
2537 }
2538 if (fixp[1])
2539 {
2540 fixp[1]->fx_frag = prev_insn_frag;
2541 fixp[1]->fx_where = prev_insn_where;
2542 }
2543 if (fixp[2])
2544 {
2545 fixp[2]->fx_frag = prev_insn_frag;
2546 fixp[2]->fx_where = prev_insn_where;
2547 }
2548 }
2549 else
2550 {
2551 char *prev_f;
2552 char temp[2];
2553
2554 assert (prev_insn_fixp[0] == NULL);
2555 assert (prev_insn_fixp[1] == NULL);
2556 assert (prev_insn_fixp[2] == NULL);
2557 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2558 memcpy (temp, prev_f, 2);
2559 memcpy (prev_f, f, 2);
2560 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2561 {
2562 assert (*reloc_type == BFD_RELOC_UNUSED);
2563 memcpy (f, temp, 2);
2564 }
2565 else
2566 {
2567 memcpy (f, f + 2, 2);
2568 memcpy (f + 2, temp, 2);
2569 }
2570 if (fixp[0])
2571 {
2572 fixp[0]->fx_frag = prev_insn_frag;
2573 fixp[0]->fx_where = prev_insn_where;
2574 }
2575 if (fixp[1])
2576 {
2577 fixp[1]->fx_frag = prev_insn_frag;
2578 fixp[1]->fx_where = prev_insn_where;
2579 }
2580 if (fixp[2])
2581 {
2582 fixp[2]->fx_frag = prev_insn_frag;
2583 fixp[2]->fx_where = prev_insn_where;
2584 }
2585 }
2586
2587 /* Update the previous insn information; leave prev_insn
2588 unchanged. */
2589 prev_prev_insn = *ip;
2590 }
2591 prev_insn_is_delay_slot = 1;
2592
2593 /* If that was an unconditional branch, forget the previous
2594 insn information. */
2595 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2596 {
2597 prev_prev_insn.insn_mo = &dummy_opcode;
2598 prev_insn.insn_mo = &dummy_opcode;
2599 }
2600
2601 prev_insn_fixp[0] = NULL;
2602 prev_insn_fixp[1] = NULL;
2603 prev_insn_fixp[2] = NULL;
2604 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2605 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2606 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2607 prev_insn_extended = 0;
2608 }
2609 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2610 {
2611 /* We don't yet optimize a branch likely. What we should do
2612 is look at the target, copy the instruction found there
2613 into the delay slot, and increment the branch to jump to
2614 the next instruction. */
2615 emit_nop ();
2616 /* Update the previous insn information. */
2617 prev_prev_insn = *ip;
2618 prev_insn.insn_mo = &dummy_opcode;
2619 prev_insn_fixp[0] = NULL;
2620 prev_insn_fixp[1] = NULL;
2621 prev_insn_fixp[2] = NULL;
2622 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2623 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2624 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2625 prev_insn_extended = 0;
2626 }
2627 else
2628 {
2629 /* Update the previous insn information. */
2630 if (nops > 0)
2631 prev_prev_insn.insn_mo = &dummy_opcode;
2632 else
2633 prev_prev_insn = prev_insn;
2634 prev_insn = *ip;
2635
2636 /* Any time we see a branch, we always fill the delay slot
2637 immediately; since this insn is not a branch, we know it
2638 is not in a delay slot. */
2639 prev_insn_is_delay_slot = 0;
2640
2641 prev_insn_fixp[0] = fixp[0];
2642 prev_insn_fixp[1] = fixp[1];
2643 prev_insn_fixp[2] = fixp[2];
2644 prev_insn_reloc_type[0] = reloc_type[0];
2645 prev_insn_reloc_type[1] = reloc_type[1];
2646 prev_insn_reloc_type[2] = reloc_type[2];
2647 if (mips_opts.mips16)
2648 prev_insn_extended = (ip->use_extend
2649 || *reloc_type > BFD_RELOC_UNUSED);
2650 }
2651
2652 prev_prev_insn_unreordered = prev_insn_unreordered;
2653 prev_insn_unreordered = 0;
2654 prev_insn_frag = frag_now;
2655 prev_insn_where = f - frag_now->fr_literal;
2656 prev_insn_valid = 1;
2657 }
2658 else if (place == NULL)
2659 {
2660 /* We need to record a bit of information even when we are not
2661 reordering, in order to determine the base address for mips16
2662 PC relative relocs. */
2663 prev_prev_insn = prev_insn;
2664 prev_insn = *ip;
2665 prev_insn_reloc_type[0] = reloc_type[0];
2666 prev_insn_reloc_type[1] = reloc_type[1];
2667 prev_insn_reloc_type[2] = reloc_type[2];
2668 prev_prev_insn_unreordered = prev_insn_unreordered;
2669 prev_insn_unreordered = 1;
2670 }
2671
2672 /* We just output an insn, so the next one doesn't have a label. */
2673 mips_clear_insn_labels ();
2674
2675 /* We must ensure that a fixup associated with an unmatched %hi
2676 reloc does not become a variant frag. Otherwise, the
2677 rearrangement of %hi relocs in frob_file may confuse
2678 tc_gen_reloc. */
2679 if (unmatched_hi)
2680 {
2681 frag_wane (frag_now);
2682 frag_new (0);
2683 }
2684 }
2685
2686 /* This function forgets that there was any previous instruction or
2687 label. If PRESERVE is non-zero, it remembers enough information to
2688 know whether nops are needed before a noreorder section. */
2689
2690 static void
2691 mips_no_prev_insn (preserve)
2692 int preserve;
2693 {
2694 if (! preserve)
2695 {
2696 prev_insn.insn_mo = &dummy_opcode;
2697 prev_prev_insn.insn_mo = &dummy_opcode;
2698 prev_nop_frag = NULL;
2699 prev_nop_frag_holds = 0;
2700 prev_nop_frag_required = 0;
2701 prev_nop_frag_since = 0;
2702 }
2703 prev_insn_valid = 0;
2704 prev_insn_is_delay_slot = 0;
2705 prev_insn_unreordered = 0;
2706 prev_insn_extended = 0;
2707 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2708 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2709 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2710 prev_prev_insn_unreordered = 0;
2711 mips_clear_insn_labels ();
2712 }
2713
2714 /* This function must be called whenever we turn on noreorder or emit
2715 something other than instructions. It inserts any NOPS which might
2716 be needed by the previous instruction, and clears the information
2717 kept for the previous instructions. The INSNS parameter is true if
2718 instructions are to follow. */
2719
2720 static void
2721 mips_emit_delays (insns)
2722 boolean insns;
2723 {
2724 if (! mips_opts.noreorder)
2725 {
2726 int nops;
2727
2728 nops = 0;
2729 if ((! mips_opts.mips16
2730 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2731 && (! cop_interlocks
2732 && (prev_insn.insn_mo->pinfo
2733 & (INSN_LOAD_COPROC_DELAY
2734 | INSN_COPROC_MOVE_DELAY
2735 | INSN_WRITE_COND_CODE))))
2736 || (! hilo_interlocks
2737 && (prev_insn.insn_mo->pinfo
2738 & (INSN_READ_LO
2739 | INSN_READ_HI)))
2740 || (! mips_opts.mips16
2741 && ! gpr_interlocks
2742 && (prev_insn.insn_mo->pinfo
2743 & INSN_LOAD_MEMORY_DELAY))
2744 || (! mips_opts.mips16
2745 && mips_opts.isa == ISA_MIPS1
2746 && (prev_insn.insn_mo->pinfo
2747 & INSN_COPROC_MEMORY_DELAY)))
2748 {
2749 /* Itbl support may require additional care here. */
2750 ++nops;
2751 if ((! mips_opts.mips16
2752 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2753 && (! cop_interlocks
2754 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2755 || (! hilo_interlocks
2756 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2757 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2758 ++nops;
2759
2760 if (prev_insn_unreordered)
2761 nops = 0;
2762 }
2763 else if ((! mips_opts.mips16
2764 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2765 && (! cop_interlocks
2766 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2767 || (! hilo_interlocks
2768 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2769 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2770 {
2771 /* Itbl support may require additional care here. */
2772 if (! prev_prev_insn_unreordered)
2773 ++nops;
2774 }
2775
2776 if (nops > 0)
2777 {
2778 struct insn_label_list *l;
2779
2780 if (insns)
2781 {
2782 /* Record the frag which holds the nop instructions, so
2783 that we can remove them if we don't need them. */
2784 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2785 prev_nop_frag = frag_now;
2786 prev_nop_frag_holds = nops;
2787 prev_nop_frag_required = 0;
2788 prev_nop_frag_since = 0;
2789 }
2790
2791 for (; nops > 0; --nops)
2792 emit_nop ();
2793
2794 if (insns)
2795 {
2796 /* Move on to a new frag, so that it is safe to simply
2797 decrease the size of prev_nop_frag. */
2798 frag_wane (frag_now);
2799 frag_new (0);
2800 }
2801
2802 for (l = insn_labels; l != NULL; l = l->next)
2803 {
2804 valueT val;
2805
2806 assert (S_GET_SEGMENT (l->label) == now_seg);
2807 symbol_set_frag (l->label, frag_now);
2808 val = (valueT) frag_now_fix ();
2809 /* mips16 text labels are stored as odd. */
2810 if (mips_opts.mips16)
2811 val += 1;
2812 S_SET_VALUE (l->label, val);
2813 }
2814 }
2815 }
2816
2817 /* Mark instruction labels in mips16 mode. */
2818 if (mips_opts.mips16 && insns)
2819 mips16_mark_labels ();
2820
2821 mips_no_prev_insn (insns);
2822 }
2823
2824 /* Build an instruction created by a macro expansion. This is passed
2825 a pointer to the count of instructions created so far, an
2826 expression, the name of the instruction to build, an operand format
2827 string, and corresponding arguments. */
2828
2829 #ifdef USE_STDARG
2830 static void
2831 macro_build (char *place,
2832 int *counter,
2833 expressionS * ep,
2834 const char *name,
2835 const char *fmt,
2836 ...)
2837 #else
2838 static void
2839 macro_build (place, counter, ep, name, fmt, va_alist)
2840 char *place;
2841 int *counter;
2842 expressionS *ep;
2843 const char *name;
2844 const char *fmt;
2845 va_dcl
2846 #endif
2847 {
2848 struct mips_cl_insn insn;
2849 bfd_reloc_code_real_type r[3];
2850 va_list args;
2851
2852 #ifdef USE_STDARG
2853 va_start (args, fmt);
2854 #else
2855 va_start (args);
2856 #endif
2857
2858 /*
2859 * If the macro is about to expand into a second instruction,
2860 * print a warning if needed. We need to pass ip as a parameter
2861 * to generate a better warning message here...
2862 */
2863 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2864 as_warn (_("Macro instruction expanded into multiple instructions"));
2865
2866 if (place == NULL)
2867 *counter += 1; /* bump instruction counter */
2868
2869 if (mips_opts.mips16)
2870 {
2871 mips16_macro_build (place, counter, ep, name, fmt, args);
2872 va_end (args);
2873 return;
2874 }
2875
2876 r[0] = BFD_RELOC_UNUSED;
2877 r[1] = BFD_RELOC_UNUSED;
2878 r[2] = BFD_RELOC_UNUSED;
2879 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2880 assert (insn.insn_mo);
2881 assert (strcmp (name, insn.insn_mo->name) == 0);
2882
2883 /* Search until we get a match for NAME. */
2884 while (1)
2885 {
2886 if (strcmp (fmt, insn.insn_mo->args) == 0
2887 && insn.insn_mo->pinfo != INSN_MACRO
2888 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2889 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2890 break;
2891
2892 ++insn.insn_mo;
2893 assert (insn.insn_mo->name);
2894 assert (strcmp (name, insn.insn_mo->name) == 0);
2895 }
2896
2897 insn.insn_opcode = insn.insn_mo->match;
2898 for (;;)
2899 {
2900 switch (*fmt++)
2901 {
2902 case '\0':
2903 break;
2904
2905 case ',':
2906 case '(':
2907 case ')':
2908 continue;
2909
2910 case 't':
2911 case 'w':
2912 case 'E':
2913 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2914 continue;
2915
2916 case 'c':
2917 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2918 continue;
2919
2920 case 'T':
2921 case 'W':
2922 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2923 continue;
2924
2925 case 'd':
2926 case 'G':
2927 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2928 continue;
2929
2930 case 'U':
2931 {
2932 int tmp = va_arg (args, int);
2933
2934 insn.insn_opcode |= tmp << OP_SH_RT;
2935 insn.insn_opcode |= tmp << OP_SH_RD;
2936 continue;
2937 }
2938
2939 case 'V':
2940 case 'S':
2941 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2942 continue;
2943
2944 case 'z':
2945 continue;
2946
2947 case '<':
2948 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2949 continue;
2950
2951 case 'D':
2952 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2953 continue;
2954
2955 case 'B':
2956 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2957 continue;
2958
2959 case 'J':
2960 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2961 continue;
2962
2963 case 'q':
2964 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2965 continue;
2966
2967 case 'b':
2968 case 's':
2969 case 'r':
2970 case 'v':
2971 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2972 continue;
2973
2974 case 'i':
2975 case 'j':
2976 case 'o':
2977 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2978 assert (*r == BFD_RELOC_GPREL16
2979 || *r == BFD_RELOC_MIPS_LITERAL
2980 || *r == BFD_RELOC_MIPS_HIGHER
2981 || *r == BFD_RELOC_HI16_S
2982 || *r == BFD_RELOC_LO16
2983 || *r == BFD_RELOC_MIPS_GOT16
2984 || *r == BFD_RELOC_MIPS_CALL16
2985 || *r == BFD_RELOC_MIPS_GOT_LO16
2986 || *r == BFD_RELOC_MIPS_CALL_LO16
2987 || (ep->X_op == O_subtract
2988 && *r == BFD_RELOC_PCREL_LO16));
2989 continue;
2990
2991 case 'u':
2992 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2993 assert (ep != NULL
2994 && (ep->X_op == O_constant
2995 || (ep->X_op == O_symbol
2996 && (*r == BFD_RELOC_MIPS_HIGHEST
2997 || *r == BFD_RELOC_HI16_S
2998 || *r == BFD_RELOC_HI16
2999 || *r == BFD_RELOC_GPREL16
3000 || *r == BFD_RELOC_MIPS_GOT_HI16
3001 || *r == BFD_RELOC_MIPS_CALL_HI16))
3002 || (ep->X_op == O_subtract
3003 && *r == BFD_RELOC_PCREL_HI16_S)));
3004 continue;
3005
3006 case 'p':
3007 assert (ep != NULL);
3008 /*
3009 * This allows macro() to pass an immediate expression for
3010 * creating short branches without creating a symbol.
3011 * Note that the expression still might come from the assembly
3012 * input, in which case the value is not checked for range nor
3013 * is a relocation entry generated (yuck).
3014 */
3015 if (ep->X_op == O_constant)
3016 {
3017 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3018 ep = NULL;
3019 }
3020 else
3021 if (mips_pic == EMBEDDED_PIC)
3022 *r = BFD_RELOC_16_PCREL_S2;
3023 else
3024 *r = BFD_RELOC_16_PCREL;
3025 continue;
3026
3027 case 'a':
3028 assert (ep != NULL);
3029 *r = BFD_RELOC_MIPS_JMP;
3030 continue;
3031
3032 case 'C':
3033 insn.insn_opcode |= va_arg (args, unsigned long);
3034 continue;
3035
3036 default:
3037 internalError ();
3038 }
3039 break;
3040 }
3041 va_end (args);
3042 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3043
3044 append_insn (place, &insn, ep, r, false);
3045 }
3046
3047 static void
3048 mips16_macro_build (place, counter, ep, name, fmt, args)
3049 char *place;
3050 int *counter ATTRIBUTE_UNUSED;
3051 expressionS *ep;
3052 const char *name;
3053 const char *fmt;
3054 va_list args;
3055 {
3056 struct mips_cl_insn insn;
3057 bfd_reloc_code_real_type r[3]
3058 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3059
3060 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3061 assert (insn.insn_mo);
3062 assert (strcmp (name, insn.insn_mo->name) == 0);
3063
3064 while (strcmp (fmt, insn.insn_mo->args) != 0
3065 || insn.insn_mo->pinfo == INSN_MACRO)
3066 {
3067 ++insn.insn_mo;
3068 assert (insn.insn_mo->name);
3069 assert (strcmp (name, insn.insn_mo->name) == 0);
3070 }
3071
3072 insn.insn_opcode = insn.insn_mo->match;
3073 insn.use_extend = false;
3074
3075 for (;;)
3076 {
3077 int c;
3078
3079 c = *fmt++;
3080 switch (c)
3081 {
3082 case '\0':
3083 break;
3084
3085 case ',':
3086 case '(':
3087 case ')':
3088 continue;
3089
3090 case 'y':
3091 case 'w':
3092 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3093 continue;
3094
3095 case 'x':
3096 case 'v':
3097 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3098 continue;
3099
3100 case 'z':
3101 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3102 continue;
3103
3104 case 'Z':
3105 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3106 continue;
3107
3108 case '0':
3109 case 'S':
3110 case 'P':
3111 case 'R':
3112 continue;
3113
3114 case 'X':
3115 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3116 continue;
3117
3118 case 'Y':
3119 {
3120 int regno;
3121
3122 regno = va_arg (args, int);
3123 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3124 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3125 }
3126 continue;
3127
3128 case '<':
3129 case '>':
3130 case '4':
3131 case '5':
3132 case 'H':
3133 case 'W':
3134 case 'D':
3135 case 'j':
3136 case '8':
3137 case 'V':
3138 case 'C':
3139 case 'U':
3140 case 'k':
3141 case 'K':
3142 case 'p':
3143 case 'q':
3144 {
3145 assert (ep != NULL);
3146
3147 if (ep->X_op != O_constant)
3148 *r = BFD_RELOC_UNUSED + c;
3149 else
3150 {
3151 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3152 false, &insn.insn_opcode, &insn.use_extend,
3153 &insn.extend);
3154 ep = NULL;
3155 *r = BFD_RELOC_UNUSED;
3156 }
3157 }
3158 continue;
3159
3160 case '6':
3161 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3162 continue;
3163 }
3164
3165 break;
3166 }
3167
3168 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3169
3170 append_insn (place, &insn, ep, r, false);
3171 }
3172
3173 /*
3174 * Generate a "lui" instruction.
3175 */
3176 static void
3177 macro_build_lui (place, counter, ep, regnum)
3178 char *place;
3179 int *counter;
3180 expressionS *ep;
3181 int regnum;
3182 {
3183 expressionS high_expr;
3184 struct mips_cl_insn insn;
3185 bfd_reloc_code_real_type r[3]
3186 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3187 CONST char *name = "lui";
3188 CONST char *fmt = "t,u";
3189
3190 assert (! mips_opts.mips16);
3191
3192 if (place == NULL)
3193 high_expr = *ep;
3194 else
3195 {
3196 high_expr.X_op = O_constant;
3197 high_expr.X_add_number = ep->X_add_number;
3198 }
3199
3200 if (high_expr.X_op == O_constant)
3201 {
3202 /* we can compute the instruction now without a relocation entry */
3203 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3204 >> 16) & 0xffff;
3205 *r = BFD_RELOC_UNUSED;
3206 }
3207 else if (! HAVE_NEWABI)
3208 {
3209 assert (ep->X_op == O_symbol);
3210 /* _gp_disp is a special case, used from s_cpload. */
3211 assert (mips_pic == NO_PIC
3212 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3213 *r = BFD_RELOC_HI16_S;
3214 }
3215
3216 /*
3217 * If the macro is about to expand into a second instruction,
3218 * print a warning if needed. We need to pass ip as a parameter
3219 * to generate a better warning message here...
3220 */
3221 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3222 as_warn (_("Macro instruction expanded into multiple instructions"));
3223
3224 if (place == NULL)
3225 *counter += 1; /* bump instruction counter */
3226
3227 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3228 assert (insn.insn_mo);
3229 assert (strcmp (name, insn.insn_mo->name) == 0);
3230 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3231
3232 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3233 if (*r == BFD_RELOC_UNUSED)
3234 {
3235 insn.insn_opcode |= high_expr.X_add_number;
3236 append_insn (place, &insn, NULL, r, false);
3237 }
3238 else
3239 append_insn (place, &insn, &high_expr, r, false);
3240 }
3241
3242 /* set_at()
3243 * Generates code to set the $at register to true (one)
3244 * if reg is less than the immediate expression.
3245 */
3246 static void
3247 set_at (counter, reg, unsignedp)
3248 int *counter;
3249 int reg;
3250 int unsignedp;
3251 {
3252 if (imm_expr.X_op == O_constant
3253 && imm_expr.X_add_number >= -0x8000
3254 && imm_expr.X_add_number < 0x8000)
3255 macro_build ((char *) NULL, counter, &imm_expr,
3256 unsignedp ? "sltiu" : "slti",
3257 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3258 else
3259 {
3260 load_register (counter, AT, &imm_expr, 0);
3261 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3262 unsignedp ? "sltu" : "slt",
3263 "d,v,t", AT, reg, AT);
3264 }
3265 }
3266
3267 /* Warn if an expression is not a constant. */
3268
3269 static void
3270 check_absolute_expr (ip, ex)
3271 struct mips_cl_insn *ip;
3272 expressionS *ex;
3273 {
3274 if (ex->X_op == O_big)
3275 as_bad (_("unsupported large constant"));
3276 else if (ex->X_op != O_constant)
3277 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3278 }
3279
3280 /* Count the leading zeroes by performing a binary chop. This is a
3281 bulky bit of source, but performance is a LOT better for the
3282 majority of values than a simple loop to count the bits:
3283 for (lcnt = 0; (lcnt < 32); lcnt++)
3284 if ((v) & (1 << (31 - lcnt)))
3285 break;
3286 However it is not code size friendly, and the gain will drop a bit
3287 on certain cached systems.
3288 */
3289 #define COUNT_TOP_ZEROES(v) \
3290 (((v) & ~0xffff) == 0 \
3291 ? ((v) & ~0xff) == 0 \
3292 ? ((v) & ~0xf) == 0 \
3293 ? ((v) & ~0x3) == 0 \
3294 ? ((v) & ~0x1) == 0 \
3295 ? !(v) \
3296 ? 32 \
3297 : 31 \
3298 : 30 \
3299 : ((v) & ~0x7) == 0 \
3300 ? 29 \
3301 : 28 \
3302 : ((v) & ~0x3f) == 0 \
3303 ? ((v) & ~0x1f) == 0 \
3304 ? 27 \
3305 : 26 \
3306 : ((v) & ~0x7f) == 0 \
3307 ? 25 \
3308 : 24 \
3309 : ((v) & ~0xfff) == 0 \
3310 ? ((v) & ~0x3ff) == 0 \
3311 ? ((v) & ~0x1ff) == 0 \
3312 ? 23 \
3313 : 22 \
3314 : ((v) & ~0x7ff) == 0 \
3315 ? 21 \
3316 : 20 \
3317 : ((v) & ~0x3fff) == 0 \
3318 ? ((v) & ~0x1fff) == 0 \
3319 ? 19 \
3320 : 18 \
3321 : ((v) & ~0x7fff) == 0 \
3322 ? 17 \
3323 : 16 \
3324 : ((v) & ~0xffffff) == 0 \
3325 ? ((v) & ~0xfffff) == 0 \
3326 ? ((v) & ~0x3ffff) == 0 \
3327 ? ((v) & ~0x1ffff) == 0 \
3328 ? 15 \
3329 : 14 \
3330 : ((v) & ~0x7ffff) == 0 \
3331 ? 13 \
3332 : 12 \
3333 : ((v) & ~0x3fffff) == 0 \
3334 ? ((v) & ~0x1fffff) == 0 \
3335 ? 11 \
3336 : 10 \
3337 : ((v) & ~0x7fffff) == 0 \
3338 ? 9 \
3339 : 8 \
3340 : ((v) & ~0xfffffff) == 0 \
3341 ? ((v) & ~0x3ffffff) == 0 \
3342 ? ((v) & ~0x1ffffff) == 0 \
3343 ? 7 \
3344 : 6 \
3345 : ((v) & ~0x7ffffff) == 0 \
3346 ? 5 \
3347 : 4 \
3348 : ((v) & ~0x3fffffff) == 0 \
3349 ? ((v) & ~0x1fffffff) == 0 \
3350 ? 3 \
3351 : 2 \
3352 : ((v) & ~0x7fffffff) == 0 \
3353 ? 1 \
3354 : 0)
3355
3356 /* Is the given value a sign-extended 32-bit value? */
3357 #define IS_SEXT_32BIT_NUM(x) \
3358 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3359 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3360
3361 /* load_register()
3362 * This routine generates the least number of instructions neccessary to load
3363 * an absolute expression value into a register.
3364 */
3365 static void
3366 load_register (counter, reg, ep, dbl)
3367 int *counter;
3368 int reg;
3369 expressionS *ep;
3370 int dbl;
3371 {
3372 int freg;
3373 expressionS hi32, lo32;
3374
3375 if (ep->X_op != O_big)
3376 {
3377 assert (ep->X_op == O_constant);
3378 if (ep->X_add_number < 0x8000
3379 && (ep->X_add_number >= 0
3380 || (ep->X_add_number >= -0x8000
3381 && (! dbl
3382 || ! ep->X_unsigned
3383 || sizeof (ep->X_add_number) > 4))))
3384 {
3385 /* We can handle 16 bit signed values with an addiu to
3386 $zero. No need to ever use daddiu here, since $zero and
3387 the result are always correct in 32 bit mode. */
3388 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3389 (int) BFD_RELOC_LO16);
3390 return;
3391 }
3392 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3393 {
3394 /* We can handle 16 bit unsigned values with an ori to
3395 $zero. */
3396 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3397 (int) BFD_RELOC_LO16);
3398 return;
3399 }
3400 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
3401 && (! dbl
3402 || ! ep->X_unsigned
3403 || sizeof (ep->X_add_number) > 4
3404 || (ep->X_add_number & 0x80000000) == 0))
3405 || ((HAVE_32BIT_GPRS || ! dbl)
3406 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3407 || (HAVE_32BIT_GPRS
3408 && ! dbl
3409 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3410 == ~ (offsetT) 0xffffffff)))
3411 {
3412 /* 32 bit values require an lui. */
3413 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3414 (int) BFD_RELOC_HI16);
3415 if ((ep->X_add_number & 0xffff) != 0)
3416 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3417 (int) BFD_RELOC_LO16);
3418 return;
3419 }
3420 }
3421
3422 /* The value is larger than 32 bits. */
3423
3424 if (HAVE_32BIT_GPRS)
3425 {
3426 as_bad (_("Number (0x%lx) larger than 32 bits"),
3427 (unsigned long) ep->X_add_number);
3428 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3429 (int) BFD_RELOC_LO16);
3430 return;
3431 }
3432
3433 if (ep->X_op != O_big)
3434 {
3435 hi32 = *ep;
3436 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3437 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3438 hi32.X_add_number &= 0xffffffff;
3439 lo32 = *ep;
3440 lo32.X_add_number &= 0xffffffff;
3441 }
3442 else
3443 {
3444 assert (ep->X_add_number > 2);
3445 if (ep->X_add_number == 3)
3446 generic_bignum[3] = 0;
3447 else if (ep->X_add_number > 4)
3448 as_bad (_("Number larger than 64 bits"));
3449 lo32.X_op = O_constant;
3450 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3451 hi32.X_op = O_constant;
3452 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3453 }
3454
3455 if (hi32.X_add_number == 0)
3456 freg = 0;
3457 else
3458 {
3459 int shift, bit;
3460 unsigned long hi, lo;
3461
3462 if (hi32.X_add_number == (offsetT) 0xffffffff)
3463 {
3464 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3465 {
3466 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3467 reg, 0, (int) BFD_RELOC_LO16);
3468 return;
3469 }
3470 if (lo32.X_add_number & 0x80000000)
3471 {
3472 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3473 (int) BFD_RELOC_HI16);
3474 if (lo32.X_add_number & 0xffff)
3475 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3476 reg, reg, (int) BFD_RELOC_LO16);
3477 return;
3478 }
3479 }
3480
3481 /* Check for 16bit shifted constant. We know that hi32 is
3482 non-zero, so start the mask on the first bit of the hi32
3483 value. */
3484 shift = 17;
3485 do
3486 {
3487 unsigned long himask, lomask;
3488
3489 if (shift < 32)
3490 {
3491 himask = 0xffff >> (32 - shift);
3492 lomask = (0xffff << shift) & 0xffffffff;
3493 }
3494 else
3495 {
3496 himask = 0xffff << (shift - 32);
3497 lomask = 0;
3498 }
3499 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3500 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3501 {
3502 expressionS tmp;
3503
3504 tmp.X_op = O_constant;
3505 if (shift < 32)
3506 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3507 | (lo32.X_add_number >> shift));
3508 else
3509 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3510 macro_build ((char *) NULL, counter, &tmp,
3511 "ori", "t,r,i", reg, 0,
3512 (int) BFD_RELOC_LO16);
3513 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3514 (shift >= 32) ? "dsll32" : "dsll",
3515 "d,w,<", reg, reg,
3516 (shift >= 32) ? shift - 32 : shift);
3517 return;
3518 }
3519 shift++;
3520 }
3521 while (shift <= (64 - 16));
3522
3523 /* Find the bit number of the lowest one bit, and store the
3524 shifted value in hi/lo. */
3525 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3526 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3527 if (lo != 0)
3528 {
3529 bit = 0;
3530 while ((lo & 1) == 0)
3531 {
3532 lo >>= 1;
3533 ++bit;
3534 }
3535 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3536 hi >>= bit;
3537 }
3538 else
3539 {
3540 bit = 32;
3541 while ((hi & 1) == 0)
3542 {
3543 hi >>= 1;
3544 ++bit;
3545 }
3546 lo = hi;
3547 hi = 0;
3548 }
3549
3550 /* Optimize if the shifted value is a (power of 2) - 1. */
3551 if ((hi == 0 && ((lo + 1) & lo) == 0)
3552 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3553 {
3554 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3555 if (shift != 0)
3556 {
3557 expressionS tmp;
3558
3559 /* This instruction will set the register to be all
3560 ones. */
3561 tmp.X_op = O_constant;
3562 tmp.X_add_number = (offsetT) -1;
3563 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3564 reg, 0, (int) BFD_RELOC_LO16);
3565 if (bit != 0)
3566 {
3567 bit += shift;
3568 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3569 (bit >= 32) ? "dsll32" : "dsll",
3570 "d,w,<", reg, reg,
3571 (bit >= 32) ? bit - 32 : bit);
3572 }
3573 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3574 (shift >= 32) ? "dsrl32" : "dsrl",
3575 "d,w,<", reg, reg,
3576 (shift >= 32) ? shift - 32 : shift);
3577 return;
3578 }
3579 }
3580
3581 /* Sign extend hi32 before calling load_register, because we can
3582 generally get better code when we load a sign extended value. */
3583 if ((hi32.X_add_number & 0x80000000) != 0)
3584 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3585 load_register (counter, reg, &hi32, 0);
3586 freg = reg;
3587 }
3588 if ((lo32.X_add_number & 0xffff0000) == 0)
3589 {
3590 if (freg != 0)
3591 {
3592 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3593 "dsll32", "d,w,<", reg, freg, 0);
3594 freg = reg;
3595 }
3596 }
3597 else
3598 {
3599 expressionS mid16;
3600
3601 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
3602 {
3603 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3604 (int) BFD_RELOC_HI16);
3605 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3606 "dsrl32", "d,w,<", reg, reg, 0);
3607 return;
3608 }
3609
3610 if (freg != 0)
3611 {
3612 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3613 "d,w,<", reg, freg, 16);
3614 freg = reg;
3615 }
3616 mid16 = lo32;
3617 mid16.X_add_number >>= 16;
3618 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3619 freg, (int) BFD_RELOC_LO16);
3620 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3621 "d,w,<", reg, reg, 16);
3622 freg = reg;
3623 }
3624 if ((lo32.X_add_number & 0xffff) != 0)
3625 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3626 (int) BFD_RELOC_LO16);
3627 }
3628
3629 /* Load an address into a register. */
3630
3631 static void
3632 load_address (counter, reg, ep, dbl, used_at)
3633 int *counter;
3634 int reg;
3635 expressionS *ep;
3636 int dbl;
3637 int *used_at;
3638 {
3639 char *p;
3640
3641 if (ep->X_op != O_constant
3642 && ep->X_op != O_symbol)
3643 {
3644 as_bad (_("expression too complex"));
3645 ep->X_op = O_constant;
3646 }
3647
3648 if (ep->X_op == O_constant)
3649 {
3650 load_register (counter, reg, ep, dbl);
3651 return;
3652 }
3653
3654 if (mips_pic == NO_PIC)
3655 {
3656 /* If this is a reference to a GP relative symbol, we want
3657 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3658 Otherwise we want
3659 lui $reg,<sym> (BFD_RELOC_HI16_S)
3660 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3661 If we have an addend, we always use the latter form.
3662
3663 With 64bit address space and a usable $at we want
3664 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3665 lui $at,<sym> (BFD_RELOC_HI16_S)
3666 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3667 daddiu $at,<sym> (BFD_RELOC_LO16)
3668 dsll32 $reg,0
3669 dadd $reg,$reg,$at
3670
3671 If $at is already in use, we use an path which is suboptimal
3672 on superscalar processors.
3673 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3674 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3675 dsll $reg,16
3676 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3677 dsll $reg,16
3678 daddiu $reg,<sym> (BFD_RELOC_LO16)
3679 */
3680 if (dbl)
3681 {
3682 p = NULL;
3683
3684 /* We don't do GP optimization for now because RELAX_ENCODE can't
3685 hold the data for such large chunks. */
3686
3687 if (*used_at == 0)
3688 {
3689 macro_build (p, counter, ep, "lui", "t,u",
3690 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3691 macro_build (p, counter, ep, "lui", "t,u",
3692 AT, (int) BFD_RELOC_HI16_S);
3693 macro_build (p, counter, ep, "daddiu", "t,r,j",
3694 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3695 macro_build (p, counter, ep, "daddiu", "t,r,j",
3696 AT, AT, (int) BFD_RELOC_LO16);
3697 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3698 "d,w,<", reg, reg, 0);
3699 macro_build (p, counter, (expressionS *) NULL, "dadd",
3700 "d,v,t", reg, reg, AT);
3701 *used_at = 1;
3702 }
3703 else
3704 {
3705 macro_build (p, counter, ep, "lui", "t,u",
3706 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3707 macro_build (p, counter, ep, "daddiu", "t,r,j",
3708 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
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_HI16_S);
3713 macro_build (p, counter, (expressionS *) NULL, "dsll",
3714 "d,w,<", reg, reg, 16);
3715 macro_build (p, counter, ep, "daddiu", "t,r,j",
3716 reg, reg, (int) BFD_RELOC_LO16);
3717 }
3718 }
3719 else
3720 {
3721 p = NULL;
3722 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3723 && ! nopic_need_relax (ep->X_add_symbol, 1))
3724 {
3725 frag_grow (20);
3726 macro_build ((char *) NULL, counter, ep,
3727 dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3728 (int) BFD_RELOC_GPREL16);
3729 p = frag_var (rs_machine_dependent, 8, 0,
3730 RELAX_ENCODE (4, 8, 0, 4, 0,
3731 mips_opts.warn_about_macros),
3732 ep->X_add_symbol, 0, NULL);
3733 }
3734 macro_build_lui (p, counter, ep, reg);
3735 if (p != NULL)
3736 p += 4;
3737 macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3738 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3739 }
3740 }
3741 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3742 {
3743 expressionS ex;
3744
3745 /* If this is a reference to an external symbol, we want
3746 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3747 Otherwise we want
3748 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3749 nop
3750 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3751 If there is a constant, it must be added in after. */
3752 ex.X_add_number = ep->X_add_number;
3753 ep->X_add_number = 0;
3754 frag_grow (20);
3755 macro_build ((char *) NULL, counter, ep,
3756 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3757 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3758 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3759 p = frag_var (rs_machine_dependent, 4, 0,
3760 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3761 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3762 macro_build (p, counter, ep,
3763 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3764 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3765 if (ex.X_add_number != 0)
3766 {
3767 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3768 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3769 ex.X_op = O_constant;
3770 macro_build ((char *) NULL, counter, &ex,
3771 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3772 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3773 }
3774 }
3775 else if (mips_pic == SVR4_PIC)
3776 {
3777 expressionS ex;
3778 int off;
3779
3780 /* This is the large GOT case. If this is a reference to an
3781 external symbol, we want
3782 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3783 addu $reg,$reg,$gp
3784 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3785 Otherwise, for a reference to a local symbol, we want
3786 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3787 nop
3788 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3789 If there is a constant, it must be added in after. */
3790 ex.X_add_number = ep->X_add_number;
3791 ep->X_add_number = 0;
3792 if (reg_needs_delay (GP))
3793 off = 4;
3794 else
3795 off = 0;
3796 frag_grow (32);
3797 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3798 (int) BFD_RELOC_MIPS_GOT_HI16);
3799 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3800 dbl ? "daddu" : "addu", "d,v,t", reg, reg, GP);
3801 macro_build ((char *) NULL, counter, ep, dbl ? "ld" : "lw",
3802 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3803 p = frag_var (rs_machine_dependent, 12 + off, 0,
3804 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3805 mips_opts.warn_about_macros),
3806 ep->X_add_symbol, 0, NULL);
3807 if (off > 0)
3808 {
3809 /* We need a nop before loading from $gp. This special
3810 check is required because the lui which starts the main
3811 instruction stream does not refer to $gp, and so will not
3812 insert the nop which may be required. */
3813 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3814 p += 4;
3815 }
3816 macro_build (p, counter, ep, dbl ? "ld" : "lw",
3817 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3818 p += 4;
3819 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3820 p += 4;
3821 macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
3822 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3823 if (ex.X_add_number != 0)
3824 {
3825 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3826 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3827 ex.X_op = O_constant;
3828 macro_build ((char *) NULL, counter, &ex, dbl ? "daddiu" : "addiu",
3829 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3830 }
3831 }
3832 else if (mips_pic == EMBEDDED_PIC)
3833 {
3834 /* We always do
3835 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3836 */
3837 macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3838 "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
3839 }
3840 else
3841 abort ();
3842 }
3843
3844 /* Move the contents of register SOURCE into register DEST. */
3845
3846 static void
3847 move_register (counter, dest, source)
3848 int *counter;
3849 int dest;
3850 int source;
3851 {
3852 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3853 HAVE_32BIT_GPRS ? "addu" : "daddu",
3854 "d,v,t", dest, source, 0);
3855 }
3856
3857 /*
3858 * Build macros
3859 * This routine implements the seemingly endless macro or synthesized
3860 * instructions and addressing modes in the mips assembly language. Many
3861 * of these macros are simple and are similar to each other. These could
3862 * probably be handled by some kind of table or grammer aproach instead of
3863 * this verbose method. Others are not simple macros but are more like
3864 * optimizing code generation.
3865 * One interesting optimization is when several store macros appear
3866 * consecutivly that would load AT with the upper half of the same address.
3867 * The ensuing load upper instructions are ommited. This implies some kind
3868 * of global optimization. We currently only optimize within a single macro.
3869 * For many of the load and store macros if the address is specified as a
3870 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3871 * first load register 'at' with zero and use it as the base register. The
3872 * mips assembler simply uses register $zero. Just one tiny optimization
3873 * we're missing.
3874 */
3875 static void
3876 macro (ip)
3877 struct mips_cl_insn *ip;
3878 {
3879 register int treg, sreg, dreg, breg;
3880 int tempreg;
3881 int mask;
3882 int icnt = 0;
3883 int used_at = 0;
3884 expressionS expr1;
3885 const char *s;
3886 const char *s2;
3887 const char *fmt;
3888 int likely = 0;
3889 int dbl = 0;
3890 int coproc = 0;
3891 int lr = 0;
3892 int imm = 0;
3893 offsetT maxnum;
3894 int off;
3895 bfd_reloc_code_real_type r;
3896 char *p;
3897 int hold_mips_optimize;
3898
3899 assert (! mips_opts.mips16);
3900
3901 treg = (ip->insn_opcode >> 16) & 0x1f;
3902 dreg = (ip->insn_opcode >> 11) & 0x1f;
3903 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3904 mask = ip->insn_mo->mask;
3905
3906 expr1.X_op = O_constant;
3907 expr1.X_op_symbol = NULL;
3908 expr1.X_add_symbol = NULL;
3909 expr1.X_add_number = 1;
3910
3911 switch (mask)
3912 {
3913 case M_DABS:
3914 dbl = 1;
3915 case M_ABS:
3916 /* bgez $a0,.+12
3917 move v0,$a0
3918 sub v0,$zero,$a0
3919 */
3920
3921 mips_emit_delays (true);
3922 ++mips_opts.noreorder;
3923 mips_any_noreorder = 1;
3924
3925 expr1.X_add_number = 8;
3926 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3927 if (dreg == sreg)
3928 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3929 0);
3930 else
3931 move_register (&icnt, dreg, sreg);
3932 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3933 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3934
3935 --mips_opts.noreorder;
3936 return;
3937
3938 case M_ADD_I:
3939 s = "addi";
3940 s2 = "add";
3941 goto do_addi;
3942 case M_ADDU_I:
3943 s = "addiu";
3944 s2 = "addu";
3945 goto do_addi;
3946 case M_DADD_I:
3947 dbl = 1;
3948 s = "daddi";
3949 s2 = "dadd";
3950 goto do_addi;
3951 case M_DADDU_I:
3952 dbl = 1;
3953 s = "daddiu";
3954 s2 = "daddu";
3955 do_addi:
3956 if (imm_expr.X_op == O_constant
3957 && imm_expr.X_add_number >= -0x8000
3958 && imm_expr.X_add_number < 0x8000)
3959 {
3960 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3961 (int) BFD_RELOC_LO16);
3962 return;
3963 }
3964 load_register (&icnt, AT, &imm_expr, dbl);
3965 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3966 treg, sreg, AT);
3967 break;
3968
3969 case M_AND_I:
3970 s = "andi";
3971 s2 = "and";
3972 goto do_bit;
3973 case M_OR_I:
3974 s = "ori";
3975 s2 = "or";
3976 goto do_bit;
3977 case M_NOR_I:
3978 s = "";
3979 s2 = "nor";
3980 goto do_bit;
3981 case M_XOR_I:
3982 s = "xori";
3983 s2 = "xor";
3984 do_bit:
3985 if (imm_expr.X_op == O_constant
3986 && imm_expr.X_add_number >= 0
3987 && imm_expr.X_add_number < 0x10000)
3988 {
3989 if (mask != M_NOR_I)
3990 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3991 sreg, (int) BFD_RELOC_LO16);
3992 else
3993 {
3994 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3995 treg, sreg, (int) BFD_RELOC_LO16);
3996 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3997 "d,v,t", treg, treg, 0);
3998 }
3999 return;
4000 }
4001
4002 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
4003 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4004 treg, sreg, AT);
4005 break;
4006
4007 case M_BEQ_I:
4008 s = "beq";
4009 goto beq_i;
4010 case M_BEQL_I:
4011 s = "beql";
4012 likely = 1;
4013 goto beq_i;
4014 case M_BNE_I:
4015 s = "bne";
4016 goto beq_i;
4017 case M_BNEL_I:
4018 s = "bnel";
4019 likely = 1;
4020 beq_i:
4021 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4022 {
4023 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4024 0);
4025 return;
4026 }
4027 load_register (&icnt, AT, &imm_expr, 0);
4028 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4029 break;
4030
4031 case M_BGEL:
4032 likely = 1;
4033 case M_BGE:
4034 if (treg == 0)
4035 {
4036 macro_build ((char *) NULL, &icnt, &offset_expr,
4037 likely ? "bgezl" : "bgez", "s,p", sreg);
4038 return;
4039 }
4040 if (sreg == 0)
4041 {
4042 macro_build ((char *) NULL, &icnt, &offset_expr,
4043 likely ? "blezl" : "blez", "s,p", treg);
4044 return;
4045 }
4046 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4047 AT, sreg, treg);
4048 macro_build ((char *) NULL, &icnt, &offset_expr,
4049 likely ? "beql" : "beq", "s,t,p", AT, 0);
4050 break;
4051
4052 case M_BGTL_I:
4053 likely = 1;
4054 case M_BGT_I:
4055 /* check for > max integer */
4056 maxnum = 0x7fffffff;
4057 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4058 {
4059 maxnum <<= 16;
4060 maxnum |= 0xffff;
4061 maxnum <<= 16;
4062 maxnum |= 0xffff;
4063 }
4064 if (imm_expr.X_op == O_constant
4065 && imm_expr.X_add_number >= maxnum
4066 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4067 {
4068 do_false:
4069 /* result is always false */
4070 if (! likely)
4071 {
4072 if (warn_nops)
4073 as_warn (_("Branch %s is always false (nop)"),
4074 ip->insn_mo->name);
4075 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4076 "", 0);
4077 }
4078 else
4079 {
4080 if (warn_nops)
4081 as_warn (_("Branch likely %s is always false"),
4082 ip->insn_mo->name);
4083 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4084 "s,t,p", 0, 0);
4085 }
4086 return;
4087 }
4088 if (imm_expr.X_op != O_constant)
4089 as_bad (_("Unsupported large constant"));
4090 imm_expr.X_add_number++;
4091 /* FALLTHROUGH */
4092 case M_BGE_I:
4093 case M_BGEL_I:
4094 if (mask == M_BGEL_I)
4095 likely = 1;
4096 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4097 {
4098 macro_build ((char *) NULL, &icnt, &offset_expr,
4099 likely ? "bgezl" : "bgez", "s,p", sreg);
4100 return;
4101 }
4102 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4103 {
4104 macro_build ((char *) NULL, &icnt, &offset_expr,
4105 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4106 return;
4107 }
4108 maxnum = 0x7fffffff;
4109 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4110 {
4111 maxnum <<= 16;
4112 maxnum |= 0xffff;
4113 maxnum <<= 16;
4114 maxnum |= 0xffff;
4115 }
4116 maxnum = - maxnum - 1;
4117 if (imm_expr.X_op == O_constant
4118 && imm_expr.X_add_number <= maxnum
4119 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4120 {
4121 do_true:
4122 /* result is always true */
4123 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4124 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4125 return;
4126 }
4127 set_at (&icnt, sreg, 0);
4128 macro_build ((char *) NULL, &icnt, &offset_expr,
4129 likely ? "beql" : "beq", "s,t,p", AT, 0);
4130 break;
4131
4132 case M_BGEUL:
4133 likely = 1;
4134 case M_BGEU:
4135 if (treg == 0)
4136 goto do_true;
4137 if (sreg == 0)
4138 {
4139 macro_build ((char *) NULL, &icnt, &offset_expr,
4140 likely ? "beql" : "beq", "s,t,p", 0, treg);
4141 return;
4142 }
4143 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4144 "d,v,t", AT, sreg, treg);
4145 macro_build ((char *) NULL, &icnt, &offset_expr,
4146 likely ? "beql" : "beq", "s,t,p", AT, 0);
4147 break;
4148
4149 case M_BGTUL_I:
4150 likely = 1;
4151 case M_BGTU_I:
4152 if (sreg == 0
4153 || (HAVE_32BIT_GPRS
4154 && imm_expr.X_op == O_constant
4155 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4156 goto do_false;
4157 if (imm_expr.X_op != O_constant)
4158 as_bad (_("Unsupported large constant"));
4159 imm_expr.X_add_number++;
4160 /* FALLTHROUGH */
4161 case M_BGEU_I:
4162 case M_BGEUL_I:
4163 if (mask == M_BGEUL_I)
4164 likely = 1;
4165 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4166 goto do_true;
4167 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4168 {
4169 macro_build ((char *) NULL, &icnt, &offset_expr,
4170 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4171 return;
4172 }
4173 set_at (&icnt, sreg, 1);
4174 macro_build ((char *) NULL, &icnt, &offset_expr,
4175 likely ? "beql" : "beq", "s,t,p", AT, 0);
4176 break;
4177
4178 case M_BGTL:
4179 likely = 1;
4180 case M_BGT:
4181 if (treg == 0)
4182 {
4183 macro_build ((char *) NULL, &icnt, &offset_expr,
4184 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4185 return;
4186 }
4187 if (sreg == 0)
4188 {
4189 macro_build ((char *) NULL, &icnt, &offset_expr,
4190 likely ? "bltzl" : "bltz", "s,p", treg);
4191 return;
4192 }
4193 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4194 AT, treg, sreg);
4195 macro_build ((char *) NULL, &icnt, &offset_expr,
4196 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4197 break;
4198
4199 case M_BGTUL:
4200 likely = 1;
4201 case M_BGTU:
4202 if (treg == 0)
4203 {
4204 macro_build ((char *) NULL, &icnt, &offset_expr,
4205 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4206 return;
4207 }
4208 if (sreg == 0)
4209 goto do_false;
4210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4211 "d,v,t", AT, treg, sreg);
4212 macro_build ((char *) NULL, &icnt, &offset_expr,
4213 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4214 break;
4215
4216 case M_BLEL:
4217 likely = 1;
4218 case M_BLE:
4219 if (treg == 0)
4220 {
4221 macro_build ((char *) NULL, &icnt, &offset_expr,
4222 likely ? "blezl" : "blez", "s,p", sreg);
4223 return;
4224 }
4225 if (sreg == 0)
4226 {
4227 macro_build ((char *) NULL, &icnt, &offset_expr,
4228 likely ? "bgezl" : "bgez", "s,p", treg);
4229 return;
4230 }
4231 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4232 AT, treg, sreg);
4233 macro_build ((char *) NULL, &icnt, &offset_expr,
4234 likely ? "beql" : "beq", "s,t,p", AT, 0);
4235 break;
4236
4237 case M_BLEL_I:
4238 likely = 1;
4239 case M_BLE_I:
4240 maxnum = 0x7fffffff;
4241 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4242 {
4243 maxnum <<= 16;
4244 maxnum |= 0xffff;
4245 maxnum <<= 16;
4246 maxnum |= 0xffff;
4247 }
4248 if (imm_expr.X_op == O_constant
4249 && imm_expr.X_add_number >= maxnum
4250 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4251 goto do_true;
4252 if (imm_expr.X_op != O_constant)
4253 as_bad (_("Unsupported large constant"));
4254 imm_expr.X_add_number++;
4255 /* FALLTHROUGH */
4256 case M_BLT_I:
4257 case M_BLTL_I:
4258 if (mask == M_BLTL_I)
4259 likely = 1;
4260 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4261 {
4262 macro_build ((char *) NULL, &icnt, &offset_expr,
4263 likely ? "bltzl" : "bltz", "s,p", sreg);
4264 return;
4265 }
4266 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4267 {
4268 macro_build ((char *) NULL, &icnt, &offset_expr,
4269 likely ? "blezl" : "blez", "s,p", sreg);
4270 return;
4271 }
4272 set_at (&icnt, sreg, 0);
4273 macro_build ((char *) NULL, &icnt, &offset_expr,
4274 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4275 break;
4276
4277 case M_BLEUL:
4278 likely = 1;
4279 case M_BLEU:
4280 if (treg == 0)
4281 {
4282 macro_build ((char *) NULL, &icnt, &offset_expr,
4283 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4284 return;
4285 }
4286 if (sreg == 0)
4287 goto do_true;
4288 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4289 "d,v,t", AT, treg, sreg);
4290 macro_build ((char *) NULL, &icnt, &offset_expr,
4291 likely ? "beql" : "beq", "s,t,p", AT, 0);
4292 break;
4293
4294 case M_BLEUL_I:
4295 likely = 1;
4296 case M_BLEU_I:
4297 if (sreg == 0
4298 || (HAVE_32BIT_GPRS
4299 && imm_expr.X_op == O_constant
4300 && imm_expr.X_add_number == (offsetT) 0xffffffff))
4301 goto do_true;
4302 if (imm_expr.X_op != O_constant)
4303 as_bad (_("Unsupported large constant"));
4304 imm_expr.X_add_number++;
4305 /* FALLTHROUGH */
4306 case M_BLTU_I:
4307 case M_BLTUL_I:
4308 if (mask == M_BLTUL_I)
4309 likely = 1;
4310 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4311 goto do_false;
4312 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4313 {
4314 macro_build ((char *) NULL, &icnt, &offset_expr,
4315 likely ? "beql" : "beq",
4316 "s,t,p", sreg, 0);
4317 return;
4318 }
4319 set_at (&icnt, sreg, 1);
4320 macro_build ((char *) NULL, &icnt, &offset_expr,
4321 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4322 break;
4323
4324 case M_BLTL:
4325 likely = 1;
4326 case M_BLT:
4327 if (treg == 0)
4328 {
4329 macro_build ((char *) NULL, &icnt, &offset_expr,
4330 likely ? "bltzl" : "bltz", "s,p", sreg);
4331 return;
4332 }
4333 if (sreg == 0)
4334 {
4335 macro_build ((char *) NULL, &icnt, &offset_expr,
4336 likely ? "bgtzl" : "bgtz", "s,p", treg);
4337 return;
4338 }
4339 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4340 AT, sreg, treg);
4341 macro_build ((char *) NULL, &icnt, &offset_expr,
4342 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4343 break;
4344
4345 case M_BLTUL:
4346 likely = 1;
4347 case M_BLTU:
4348 if (treg == 0)
4349 goto do_false;
4350 if (sreg == 0)
4351 {
4352 macro_build ((char *) NULL, &icnt, &offset_expr,
4353 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4354 return;
4355 }
4356 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4357 "d,v,t", AT, sreg,
4358 treg);
4359 macro_build ((char *) NULL, &icnt, &offset_expr,
4360 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4361 break;
4362
4363 case M_DDIV_3:
4364 dbl = 1;
4365 case M_DIV_3:
4366 s = "mflo";
4367 goto do_div3;
4368 case M_DREM_3:
4369 dbl = 1;
4370 case M_REM_3:
4371 s = "mfhi";
4372 do_div3:
4373 if (treg == 0)
4374 {
4375 as_warn (_("Divide by zero."));
4376 if (mips_trap)
4377 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4378 "s,t", 0, 0);
4379 else
4380 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4381 "c", 7);
4382 return;
4383 }
4384
4385 mips_emit_delays (true);
4386 ++mips_opts.noreorder;
4387 mips_any_noreorder = 1;
4388 if (mips_trap)
4389 {
4390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4391 "s,t", treg, 0);
4392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4393 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4394 }
4395 else
4396 {
4397 expr1.X_add_number = 8;
4398 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4399 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4400 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4401 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4402 "c", 7);
4403 }
4404 expr1.X_add_number = -1;
4405 macro_build ((char *) NULL, &icnt, &expr1,
4406 dbl ? "daddiu" : "addiu",
4407 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4408 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4409 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4410 if (dbl)
4411 {
4412 expr1.X_add_number = 1;
4413 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4414 (int) BFD_RELOC_LO16);
4415 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4416 "d,w,<", AT, AT, 31);
4417 }
4418 else
4419 {
4420 expr1.X_add_number = 0x80000000;
4421 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4422 (int) BFD_RELOC_HI16);
4423 }
4424 if (mips_trap)
4425 {
4426 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4427 "s,t", sreg, AT);
4428 /* We want to close the noreorder block as soon as possible, so
4429 that later insns are available for delay slot filling. */
4430 --mips_opts.noreorder;
4431 }
4432 else
4433 {
4434 expr1.X_add_number = 8;
4435 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4436 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4437 0);
4438
4439 /* We want to close the noreorder block as soon as possible, so
4440 that later insns are available for delay slot filling. */
4441 --mips_opts.noreorder;
4442
4443 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4444 "c", 6);
4445 }
4446 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4447 break;
4448
4449 case M_DIV_3I:
4450 s = "div";
4451 s2 = "mflo";
4452 goto do_divi;
4453 case M_DIVU_3I:
4454 s = "divu";
4455 s2 = "mflo";
4456 goto do_divi;
4457 case M_REM_3I:
4458 s = "div";
4459 s2 = "mfhi";
4460 goto do_divi;
4461 case M_REMU_3I:
4462 s = "divu";
4463 s2 = "mfhi";
4464 goto do_divi;
4465 case M_DDIV_3I:
4466 dbl = 1;
4467 s = "ddiv";
4468 s2 = "mflo";
4469 goto do_divi;
4470 case M_DDIVU_3I:
4471 dbl = 1;
4472 s = "ddivu";
4473 s2 = "mflo";
4474 goto do_divi;
4475 case M_DREM_3I:
4476 dbl = 1;
4477 s = "ddiv";
4478 s2 = "mfhi";
4479 goto do_divi;
4480 case M_DREMU_3I:
4481 dbl = 1;
4482 s = "ddivu";
4483 s2 = "mfhi";
4484 do_divi:
4485 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4486 {
4487 as_warn (_("Divide by zero."));
4488 if (mips_trap)
4489 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4490 "s,t", 0, 0);
4491 else
4492 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4493 "c", 7);
4494 return;
4495 }
4496 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4497 {
4498 if (strcmp (s2, "mflo") == 0)
4499 move_register (&icnt, dreg, sreg);
4500 else
4501 move_register (&icnt, dreg, 0);
4502 return;
4503 }
4504 if (imm_expr.X_op == O_constant
4505 && imm_expr.X_add_number == -1
4506 && s[strlen (s) - 1] != 'u')
4507 {
4508 if (strcmp (s2, "mflo") == 0)
4509 {
4510 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4511 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4512 }
4513 else
4514 move_register (&icnt, dreg, 0);
4515 return;
4516 }
4517
4518 load_register (&icnt, AT, &imm_expr, dbl);
4519 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4520 sreg, AT);
4521 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4522 break;
4523
4524 case M_DIVU_3:
4525 s = "divu";
4526 s2 = "mflo";
4527 goto do_divu3;
4528 case M_REMU_3:
4529 s = "divu";
4530 s2 = "mfhi";
4531 goto do_divu3;
4532 case M_DDIVU_3:
4533 s = "ddivu";
4534 s2 = "mflo";
4535 goto do_divu3;
4536 case M_DREMU_3:
4537 s = "ddivu";
4538 s2 = "mfhi";
4539 do_divu3:
4540 mips_emit_delays (true);
4541 ++mips_opts.noreorder;
4542 mips_any_noreorder = 1;
4543 if (mips_trap)
4544 {
4545 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4546 "s,t", treg, 0);
4547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4548 sreg, treg);
4549 /* We want to close the noreorder block as soon as possible, so
4550 that later insns are available for delay slot filling. */
4551 --mips_opts.noreorder;
4552 }
4553 else
4554 {
4555 expr1.X_add_number = 8;
4556 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4558 sreg, treg);
4559
4560 /* We want to close the noreorder block as soon as possible, so
4561 that later insns are available for delay slot filling. */
4562 --mips_opts.noreorder;
4563 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4564 "c", 7);
4565 }
4566 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4567 return;
4568
4569 case M_DLA_AB:
4570 dbl = 1;
4571 case M_LA_AB:
4572 /* Load the address of a symbol into a register. If breg is not
4573 zero, we then add a base register to it. */
4574
4575 if (treg == breg)
4576 {
4577 tempreg = AT;
4578 used_at = 1;
4579 }
4580 else
4581 {
4582 tempreg = treg;
4583 used_at = 0;
4584 }
4585
4586 /* When generating embedded PIC code, we permit expressions of
4587 the form
4588 la $treg,foo-bar
4589 la $treg,foo-bar($breg)
4590 where bar is an address in the current section. These are used
4591 when getting the addresses of functions. We don't permit
4592 X_add_number to be non-zero, because if the symbol is
4593 external the relaxing code needs to know that any addend is
4594 purely the offset to X_op_symbol. */
4595 if (mips_pic == EMBEDDED_PIC
4596 && offset_expr.X_op == O_subtract
4597 && (symbol_constant_p (offset_expr.X_op_symbol)
4598 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4599 : (symbol_equated_p (offset_expr.X_op_symbol)
4600 && (S_GET_SEGMENT
4601 (symbol_get_value_expression (offset_expr.X_op_symbol)
4602 ->X_add_symbol)
4603 == now_seg)))
4604 && (offset_expr.X_add_number == 0
4605 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4606 {
4607 if (breg == 0)
4608 {
4609 tempreg = treg;
4610 used_at = 0;
4611 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4612 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4613 }
4614 else
4615 {
4616 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4617 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4618 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4619 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4620 "d,v,t", tempreg, tempreg, breg);
4621 }
4622 macro_build ((char *) NULL, &icnt, &offset_expr,
4623 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4624 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4625 if (! used_at)
4626 return;
4627 break;
4628 }
4629
4630 if (offset_expr.X_op != O_symbol
4631 && offset_expr.X_op != O_constant)
4632 {
4633 as_bad (_("expression too complex"));
4634 offset_expr.X_op = O_constant;
4635 }
4636
4637 if (offset_expr.X_op == O_constant)
4638 load_register (&icnt, tempreg, &offset_expr, dbl);
4639 else if (mips_pic == NO_PIC)
4640 {
4641 /* If this is a reference to a GP relative symbol, we want
4642 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4643 Otherwise we want
4644 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4645 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4646 If we have a constant, we need two instructions anyhow,
4647 so we may as well always use the latter form.
4648
4649 With 64bit address space and a usable $at we want
4650 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4651 lui $at,<sym> (BFD_RELOC_HI16_S)
4652 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4653 daddiu $at,<sym> (BFD_RELOC_LO16)
4654 dsll32 $tempreg,0
4655 dadd $tempreg,$tempreg,$at
4656
4657 If $at is already in use, we use an path which is suboptimal
4658 on superscalar processors.
4659 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4660 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4661 dsll $tempreg,16
4662 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4663 dsll $tempreg,16
4664 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4665 */
4666 p = NULL;
4667 if (HAVE_64BIT_ADDRESSES)
4668 {
4669 /* We don't do GP optimization for now because RELAX_ENCODE can't
4670 hold the data for such large chunks. */
4671
4672 if (used_at == 0)
4673 {
4674 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4675 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4676 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4677 AT, (int) BFD_RELOC_HI16_S);
4678 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4679 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4680 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4681 AT, AT, (int) BFD_RELOC_LO16);
4682 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4683 "d,w,<", tempreg, tempreg, 0);
4684 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4685 tempreg, tempreg, AT);
4686 used_at = 1;
4687 }
4688 else
4689 {
4690 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4691 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4692 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4693 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
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_HI16_S);
4698 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4699 tempreg, tempreg, 16);
4700 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4701 tempreg, tempreg, (int) BFD_RELOC_LO16);
4702 }
4703 }
4704 else
4705 {
4706 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4707 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4708 {
4709 frag_grow (20);
4710 macro_build ((char *) NULL, &icnt, &offset_expr,
4711 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4712 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4713 p = frag_var (rs_machine_dependent, 8, 0,
4714 RELAX_ENCODE (4, 8, 0, 4, 0,
4715 mips_opts.warn_about_macros),
4716 offset_expr.X_add_symbol, 0, NULL);
4717 }
4718 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4719 if (p != NULL)
4720 p += 4;
4721 macro_build (p, &icnt, &offset_expr,
4722 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4723 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4724 }
4725 }
4726 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4727 {
4728 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4729
4730 /* If this is a reference to an external symbol, and there
4731 is no constant, we want
4732 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4733 or if tempreg is PIC_CALL_REG
4734 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4735 For a local symbol, we want
4736 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4737 nop
4738 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4739
4740 If we have a small constant, and this is a reference to
4741 an external symbol, we want
4742 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4743 nop
4744 addiu $tempreg,$tempreg,<constant>
4745 For a local symbol, we want the same instruction
4746 sequence, but we output a BFD_RELOC_LO16 reloc on the
4747 addiu instruction.
4748
4749 If we have a large constant, and this is a reference to
4750 an external symbol, we want
4751 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4752 lui $at,<hiconstant>
4753 addiu $at,$at,<loconstant>
4754 addu $tempreg,$tempreg,$at
4755 For a local symbol, we want the same instruction
4756 sequence, but we output a BFD_RELOC_LO16 reloc on the
4757 addiu instruction. */
4758 expr1.X_add_number = offset_expr.X_add_number;
4759 offset_expr.X_add_number = 0;
4760 frag_grow (32);
4761 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4762 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4763 macro_build ((char *) NULL, &icnt, &offset_expr, dbl ? "ld" : "lw",
4764 "t,o(b)", tempreg, lw_reloc_type, GP);
4765 if (expr1.X_add_number == 0)
4766 {
4767 int off;
4768
4769 if (breg == 0)
4770 off = 0;
4771 else
4772 {
4773 /* We're going to put in an addu instruction using
4774 tempreg, so we may as well insert the nop right
4775 now. */
4776 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4777 "nop", "");
4778 off = 4;
4779 }
4780 p = frag_var (rs_machine_dependent, 8 - off, 0,
4781 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4782 (breg == 0
4783 ? mips_opts.warn_about_macros
4784 : 0)),
4785 offset_expr.X_add_symbol, 0, NULL);
4786 if (breg == 0)
4787 {
4788 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4789 p += 4;
4790 }
4791 macro_build (p, &icnt, &expr1,
4792 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4793 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4794 /* FIXME: If breg == 0, and the next instruction uses
4795 $tempreg, then if this variant case is used an extra
4796 nop will be generated. */
4797 }
4798 else if (expr1.X_add_number >= -0x8000
4799 && expr1.X_add_number < 0x8000)
4800 {
4801 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4802 "nop", "");
4803 macro_build ((char *) NULL, &icnt, &expr1,
4804 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4805 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4806 frag_var (rs_machine_dependent, 0, 0,
4807 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4808 offset_expr.X_add_symbol, 0, NULL);
4809 }
4810 else
4811 {
4812 int off1;
4813
4814 /* If we are going to add in a base register, and the
4815 target register and the base register are the same,
4816 then we are using AT as a temporary register. Since
4817 we want to load the constant into AT, we add our
4818 current AT (from the global offset table) and the
4819 register into the register now, and pretend we were
4820 not using a base register. */
4821 if (breg != treg)
4822 off1 = 0;
4823 else
4824 {
4825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4826 "nop", "");
4827 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4828 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4829 "d,v,t", treg, AT, breg);
4830 breg = 0;
4831 tempreg = treg;
4832 off1 = -8;
4833 }
4834
4835 /* Set mips_optimize around the lui instruction to avoid
4836 inserting an unnecessary nop after the lw. */
4837 hold_mips_optimize = mips_optimize;
4838 mips_optimize = 2;
4839 macro_build_lui (NULL, &icnt, &expr1, AT);
4840 mips_optimize = hold_mips_optimize;
4841
4842 macro_build ((char *) NULL, &icnt, &expr1,
4843 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4844 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4845 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4846 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4847 "d,v,t", tempreg, tempreg, AT);
4848 frag_var (rs_machine_dependent, 0, 0,
4849 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4850 offset_expr.X_add_symbol, 0, NULL);
4851 used_at = 1;
4852 }
4853 }
4854 else if (mips_pic == SVR4_PIC)
4855 {
4856 int gpdel;
4857 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4858 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4859
4860 /* This is the large GOT case. If this is a reference to an
4861 external symbol, and there is no constant, we want
4862 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4863 addu $tempreg,$tempreg,$gp
4864 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4865 or if tempreg is PIC_CALL_REG
4866 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4867 addu $tempreg,$tempreg,$gp
4868 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4869 For a local symbol, we want
4870 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4871 nop
4872 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4873
4874 If we have a small constant, and this is a reference to
4875 an external symbol, we want
4876 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4877 addu $tempreg,$tempreg,$gp
4878 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4879 nop
4880 addiu $tempreg,$tempreg,<constant>
4881 For a local symbol, we want
4882 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4883 nop
4884 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4885
4886 If we have a large constant, and this is a reference to
4887 an external symbol, we want
4888 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4889 addu $tempreg,$tempreg,$gp
4890 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4891 lui $at,<hiconstant>
4892 addiu $at,$at,<loconstant>
4893 addu $tempreg,$tempreg,$at
4894 For a local symbol, we want
4895 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4896 lui $at,<hiconstant>
4897 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4898 addu $tempreg,$tempreg,$at
4899 */
4900 expr1.X_add_number = offset_expr.X_add_number;
4901 offset_expr.X_add_number = 0;
4902 frag_grow (52);
4903 if (reg_needs_delay (GP))
4904 gpdel = 4;
4905 else
4906 gpdel = 0;
4907 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4908 {
4909 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4910 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4911 }
4912 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4913 tempreg, lui_reloc_type);
4914 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4915 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4916 "d,v,t", tempreg, tempreg, GP);
4917 macro_build ((char *) NULL, &icnt, &offset_expr,
4918 dbl ? "ld" : "lw",
4919 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4920 if (expr1.X_add_number == 0)
4921 {
4922 int off;
4923
4924 if (breg == 0)
4925 off = 0;
4926 else
4927 {
4928 /* We're going to put in an addu instruction using
4929 tempreg, so we may as well insert the nop right
4930 now. */
4931 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4932 "nop", "");
4933 off = 4;
4934 }
4935
4936 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4937 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4938 8 + gpdel, 0,
4939 (breg == 0
4940 ? mips_opts.warn_about_macros
4941 : 0)),
4942 offset_expr.X_add_symbol, 0, NULL);
4943 }
4944 else if (expr1.X_add_number >= -0x8000
4945 && expr1.X_add_number < 0x8000)
4946 {
4947 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4948 "nop", "");
4949 macro_build ((char *) NULL, &icnt, &expr1,
4950 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4951 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4952
4953 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4954 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4955 (breg == 0
4956 ? mips_opts.warn_about_macros
4957 : 0)),
4958 offset_expr.X_add_symbol, 0, NULL);
4959 }
4960 else
4961 {
4962 int adj, dreg;
4963
4964 /* If we are going to add in a base register, and the
4965 target register and the base register are the same,
4966 then we are using AT as a temporary register. Since
4967 we want to load the constant into AT, we add our
4968 current AT (from the global offset table) and the
4969 register into the register now, and pretend we were
4970 not using a base register. */
4971 if (breg != treg)
4972 {
4973 adj = 0;
4974 dreg = tempreg;
4975 }
4976 else
4977 {
4978 assert (tempreg == AT);
4979 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4980 "nop", "");
4981 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4982 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4983 "d,v,t", treg, AT, breg);
4984 dreg = treg;
4985 adj = 8;
4986 }
4987
4988 /* Set mips_optimize around the lui instruction to avoid
4989 inserting an unnecessary nop after the lw. */
4990 hold_mips_optimize = mips_optimize;
4991 mips_optimize = 2;
4992 macro_build_lui (NULL, &icnt, &expr1, AT);
4993 mips_optimize = hold_mips_optimize;
4994
4995 macro_build ((char *) NULL, &icnt, &expr1,
4996 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4997 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4998 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4999 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5000 "d,v,t", dreg, dreg, AT);
5001
5002 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5003 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5004 8 + gpdel, 0,
5005 (breg == 0
5006 ? mips_opts.warn_about_macros
5007 : 0)),
5008 offset_expr.X_add_symbol, 0, NULL);
5009
5010 used_at = 1;
5011 }
5012
5013 if (gpdel > 0)
5014 {
5015 /* This is needed because this instruction uses $gp, but
5016 the first instruction on the main stream does not. */
5017 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5018 p += 4;
5019 }
5020 macro_build (p, &icnt, &offset_expr,
5021 dbl ? "ld" : "lw",
5022 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5023 p += 4;
5024 if (expr1.X_add_number >= -0x8000
5025 && expr1.X_add_number < 0x8000)
5026 {
5027 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5028 p += 4;
5029 macro_build (p, &icnt, &expr1,
5030 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5031 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5032 /* FIXME: If add_number is 0, and there was no base
5033 register, the external symbol case ended with a load,
5034 so if the symbol turns out to not be external, and
5035 the next instruction uses tempreg, an unnecessary nop
5036 will be inserted. */
5037 }
5038 else
5039 {
5040 if (breg == treg)
5041 {
5042 /* We must add in the base register now, as in the
5043 external symbol case. */
5044 assert (tempreg == AT);
5045 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5046 p += 4;
5047 macro_build (p, &icnt, (expressionS *) NULL,
5048 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5049 "d,v,t", treg, AT, breg);
5050 p += 4;
5051 tempreg = treg;
5052 /* We set breg to 0 because we have arranged to add
5053 it in in both cases. */
5054 breg = 0;
5055 }
5056
5057 macro_build_lui (p, &icnt, &expr1, AT);
5058 p += 4;
5059 macro_build (p, &icnt, &expr1,
5060 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5061 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5062 p += 4;
5063 macro_build (p, &icnt, (expressionS *) NULL,
5064 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5065 "d,v,t", tempreg, tempreg, AT);
5066 p += 4;
5067 }
5068 }
5069 else if (mips_pic == EMBEDDED_PIC)
5070 {
5071 /* We use
5072 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5073 */
5074 macro_build ((char *) NULL, &icnt, &offset_expr,
5075 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5076 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
5077 }
5078 else
5079 abort ();
5080
5081 if (breg != 0)
5082 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5083 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5084 "d,v,t", treg, tempreg, breg);
5085
5086 if (! used_at)
5087 return;
5088
5089 break;
5090
5091 case M_J_A:
5092 /* The j instruction may not be used in PIC code, since it
5093 requires an absolute address. We convert it to a b
5094 instruction. */
5095 if (mips_pic == NO_PIC)
5096 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5097 else
5098 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5099 return;
5100
5101 /* The jal instructions must be handled as macros because when
5102 generating PIC code they expand to multi-instruction
5103 sequences. Normally they are simple instructions. */
5104 case M_JAL_1:
5105 dreg = RA;
5106 /* Fall through. */
5107 case M_JAL_2:
5108 if (mips_pic == NO_PIC
5109 || mips_pic == EMBEDDED_PIC)
5110 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5111 "d,s", dreg, sreg);
5112 else if (mips_pic == SVR4_PIC)
5113 {
5114 if (sreg != PIC_CALL_REG)
5115 as_warn (_("MIPS PIC call to register other than $25"));
5116
5117 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5118 "d,s", dreg, sreg);
5119 if (! HAVE_NEWABI)
5120 {
5121 if (mips_cprestore_offset < 0)
5122 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5123 else
5124 {
5125 if (! mips_frame_reg_valid)
5126 {
5127 as_warn (_("No .frame pseudo-op used in PIC code"));
5128 /* Quiet this warning. */
5129 mips_frame_reg_valid = 1;
5130 }
5131 if (! mips_cprestore_valid)
5132 {
5133 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5134 /* Quiet this warning. */
5135 mips_cprestore_valid = 1;
5136 }
5137 expr1.X_add_number = mips_cprestore_offset;
5138 macro_build ((char *) NULL, &icnt, &expr1,
5139 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5140 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5141 }
5142 }
5143 }
5144 else
5145 abort ();
5146
5147 return;
5148
5149 case M_JAL_A:
5150 if (mips_pic == NO_PIC)
5151 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5152 else if (mips_pic == SVR4_PIC)
5153 {
5154 /* If this is a reference to an external symbol, and we are
5155 using a small GOT, we want
5156 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5157 nop
5158 jalr $25
5159 nop
5160 lw $gp,cprestore($sp)
5161 The cprestore value is set using the .cprestore
5162 pseudo-op. If we are using a big GOT, we want
5163 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5164 addu $25,$25,$gp
5165 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5166 nop
5167 jalr $25
5168 nop
5169 lw $gp,cprestore($sp)
5170 If the symbol is not external, we want
5171 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5172 nop
5173 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5174 jalr $25
5175 nop
5176 lw $gp,cprestore($sp) */
5177 frag_grow (40);
5178 if (! mips_big_got)
5179 {
5180 macro_build ((char *) NULL, &icnt, &offset_expr,
5181 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5182 "t,o(b)", PIC_CALL_REG,
5183 (int) BFD_RELOC_MIPS_CALL16, GP);
5184 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5185 "nop", "");
5186 p = frag_var (rs_machine_dependent, 4, 0,
5187 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5188 offset_expr.X_add_symbol, 0, NULL);
5189 }
5190 else
5191 {
5192 int gpdel;
5193
5194 if (reg_needs_delay (GP))
5195 gpdel = 4;
5196 else
5197 gpdel = 0;
5198 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5199 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5200 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5201 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5202 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5203 macro_build ((char *) NULL, &icnt, &offset_expr,
5204 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5205 "t,o(b)", PIC_CALL_REG,
5206 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5207 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5208 "nop", "");
5209 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5210 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5211 0, 0),
5212 offset_expr.X_add_symbol, 0, NULL);
5213 if (gpdel > 0)
5214 {
5215 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5216 p += 4;
5217 }
5218 macro_build (p, &icnt, &offset_expr,
5219 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5220 "t,o(b)", PIC_CALL_REG,
5221 (int) BFD_RELOC_MIPS_GOT16, GP);
5222 p += 4;
5223 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5224 p += 4;
5225 }
5226 macro_build (p, &icnt, &offset_expr,
5227 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5228 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5229 (int) BFD_RELOC_LO16);
5230 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5231 "jalr", "s", PIC_CALL_REG);
5232 if (! HAVE_NEWABI)
5233 {
5234 if (mips_cprestore_offset < 0)
5235 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5236 else
5237 {
5238 if (! mips_frame_reg_valid)
5239 {
5240 as_warn (_("No .frame pseudo-op used in PIC code"));
5241 /* Quiet this warning. */
5242 mips_frame_reg_valid = 1;
5243 }
5244 if (! mips_cprestore_valid)
5245 {
5246 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5247 /* Quiet this warning. */
5248 mips_cprestore_valid = 1;
5249 }
5250 if (mips_opts.noreorder)
5251 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5252 "nop", "");
5253 expr1.X_add_number = mips_cprestore_offset;
5254 macro_build ((char *) NULL, &icnt, &expr1,
5255 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5256 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5257 }
5258 }
5259 }
5260 else if (mips_pic == EMBEDDED_PIC)
5261 {
5262 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5263 /* The linker may expand the call to a longer sequence which
5264 uses $at, so we must break rather than return. */
5265 break;
5266 }
5267 else
5268 abort ();
5269
5270 return;
5271
5272 case M_LB_AB:
5273 s = "lb";
5274 goto ld;
5275 case M_LBU_AB:
5276 s = "lbu";
5277 goto ld;
5278 case M_LH_AB:
5279 s = "lh";
5280 goto ld;
5281 case M_LHU_AB:
5282 s = "lhu";
5283 goto ld;
5284 case M_LW_AB:
5285 s = "lw";
5286 goto ld;
5287 case M_LWC0_AB:
5288 s = "lwc0";
5289 /* Itbl support may require additional care here. */
5290 coproc = 1;
5291 goto ld;
5292 case M_LWC1_AB:
5293 s = "lwc1";
5294 /* Itbl support may require additional care here. */
5295 coproc = 1;
5296 goto ld;
5297 case M_LWC2_AB:
5298 s = "lwc2";
5299 /* Itbl support may require additional care here. */
5300 coproc = 1;
5301 goto ld;
5302 case M_LWC3_AB:
5303 s = "lwc3";
5304 /* Itbl support may require additional care here. */
5305 coproc = 1;
5306 goto ld;
5307 case M_LWL_AB:
5308 s = "lwl";
5309 lr = 1;
5310 goto ld;
5311 case M_LWR_AB:
5312 s = "lwr";
5313 lr = 1;
5314 goto ld;
5315 case M_LDC1_AB:
5316 if (mips_arch == CPU_R4650)
5317 {
5318 as_bad (_("opcode not supported on this processor"));
5319 return;
5320 }
5321 s = "ldc1";
5322 /* Itbl support may require additional care here. */
5323 coproc = 1;
5324 goto ld;
5325 case M_LDC2_AB:
5326 s = "ldc2";
5327 /* Itbl support may require additional care here. */
5328 coproc = 1;
5329 goto ld;
5330 case M_LDC3_AB:
5331 s = "ldc3";
5332 /* Itbl support may require additional care here. */
5333 coproc = 1;
5334 goto ld;
5335 case M_LDL_AB:
5336 s = "ldl";
5337 lr = 1;
5338 goto ld;
5339 case M_LDR_AB:
5340 s = "ldr";
5341 lr = 1;
5342 goto ld;
5343 case M_LL_AB:
5344 s = "ll";
5345 goto ld;
5346 case M_LLD_AB:
5347 s = "lld";
5348 goto ld;
5349 case M_LWU_AB:
5350 s = "lwu";
5351 ld:
5352 if (breg == treg || coproc || lr)
5353 {
5354 tempreg = AT;
5355 used_at = 1;
5356 }
5357 else
5358 {
5359 tempreg = treg;
5360 used_at = 0;
5361 }
5362 goto ld_st;
5363 case M_SB_AB:
5364 s = "sb";
5365 goto st;
5366 case M_SH_AB:
5367 s = "sh";
5368 goto st;
5369 case M_SW_AB:
5370 s = "sw";
5371 goto st;
5372 case M_SWC0_AB:
5373 s = "swc0";
5374 /* Itbl support may require additional care here. */
5375 coproc = 1;
5376 goto st;
5377 case M_SWC1_AB:
5378 s = "swc1";
5379 /* Itbl support may require additional care here. */
5380 coproc = 1;
5381 goto st;
5382 case M_SWC2_AB:
5383 s = "swc2";
5384 /* Itbl support may require additional care here. */
5385 coproc = 1;
5386 goto st;
5387 case M_SWC3_AB:
5388 s = "swc3";
5389 /* Itbl support may require additional care here. */
5390 coproc = 1;
5391 goto st;
5392 case M_SWL_AB:
5393 s = "swl";
5394 goto st;
5395 case M_SWR_AB:
5396 s = "swr";
5397 goto st;
5398 case M_SC_AB:
5399 s = "sc";
5400 goto st;
5401 case M_SCD_AB:
5402 s = "scd";
5403 goto st;
5404 case M_SDC1_AB:
5405 if (mips_arch == CPU_R4650)
5406 {
5407 as_bad (_("opcode not supported on this processor"));
5408 return;
5409 }
5410 s = "sdc1";
5411 coproc = 1;
5412 /* Itbl support may require additional care here. */
5413 goto st;
5414 case M_SDC2_AB:
5415 s = "sdc2";
5416 /* Itbl support may require additional care here. */
5417 coproc = 1;
5418 goto st;
5419 case M_SDC3_AB:
5420 s = "sdc3";
5421 /* Itbl support may require additional care here. */
5422 coproc = 1;
5423 goto st;
5424 case M_SDL_AB:
5425 s = "sdl";
5426 goto st;
5427 case M_SDR_AB:
5428 s = "sdr";
5429 st:
5430 tempreg = AT;
5431 used_at = 1;
5432 ld_st:
5433 /* Itbl support may require additional care here. */
5434 if (mask == M_LWC1_AB
5435 || mask == M_SWC1_AB
5436 || mask == M_LDC1_AB
5437 || mask == M_SDC1_AB
5438 || mask == M_L_DAB
5439 || mask == M_S_DAB)
5440 fmt = "T,o(b)";
5441 else if (coproc)
5442 fmt = "E,o(b)";
5443 else
5444 fmt = "t,o(b)";
5445
5446 /* For embedded PIC, we allow loads where the offset is calculated
5447 by subtracting a symbol in the current segment from an unknown
5448 symbol, relative to a base register, e.g.:
5449 <op> $treg, <sym>-<localsym>($breg)
5450 This is used by the compiler for switch statements. */
5451 if (mips_pic == EMBEDDED_PIC
5452 && offset_expr.X_op == O_subtract
5453 && (symbol_constant_p (offset_expr.X_op_symbol)
5454 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5455 : (symbol_equated_p (offset_expr.X_op_symbol)
5456 && (S_GET_SEGMENT
5457 (symbol_get_value_expression (offset_expr.X_op_symbol)
5458 ->X_add_symbol)
5459 == now_seg)))
5460 && breg != 0
5461 && (offset_expr.X_add_number == 0
5462 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5463 {
5464 /* For this case, we output the instructions:
5465 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5466 addiu $tempreg,$tempreg,$breg
5467 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5468 If the relocation would fit entirely in 16 bits, it would be
5469 nice to emit:
5470 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5471 instead, but that seems quite difficult. */
5472 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5473 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5475 ((bfd_arch_bits_per_address (stdoutput) == 32
5476 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5477 ? "addu" : "daddu"),
5478 "d,v,t", tempreg, tempreg, breg);
5479 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5480 (int) BFD_RELOC_PCREL_LO16, tempreg);
5481 if (! used_at)
5482 return;
5483 break;
5484 }
5485
5486 if (offset_expr.X_op != O_constant
5487 && offset_expr.X_op != O_symbol)
5488 {
5489 as_bad (_("expression too complex"));
5490 offset_expr.X_op = O_constant;
5491 }
5492
5493 /* A constant expression in PIC code can be handled just as it
5494 is in non PIC code. */
5495 if (mips_pic == NO_PIC
5496 || offset_expr.X_op == O_constant)
5497 {
5498 /* If this is a reference to a GP relative symbol, and there
5499 is no base register, we want
5500 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5501 Otherwise, if there is no base register, we want
5502 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5503 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5504 If we have a constant, we need two instructions anyhow,
5505 so we always use the latter form.
5506
5507 If we have a base register, and this is a reference to a
5508 GP relative symbol, we want
5509 addu $tempreg,$breg,$gp
5510 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5511 Otherwise we want
5512 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5513 addu $tempreg,$tempreg,$breg
5514 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5515 With a constant we always use the latter case.
5516
5517 With 64bit address space and no base register and $at usable,
5518 we want
5519 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5520 lui $at,<sym> (BFD_RELOC_HI16_S)
5521 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5522 dsll32 $tempreg,0
5523 daddu $tempreg,$at
5524 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5525 If we have a base register, we want
5526 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5527 lui $at,<sym> (BFD_RELOC_HI16_S)
5528 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5529 daddu $at,$breg
5530 dsll32 $tempreg,0
5531 daddu $tempreg,$at
5532 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5533
5534 Without $at we can't generate the optimal path for superscalar
5535 processors here since this would require two temporary registers.
5536 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5537 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5538 dsll $tempreg,16
5539 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5540 dsll $tempreg,16
5541 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5542 If we have a base register, we want
5543 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5544 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5545 dsll $tempreg,16
5546 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5547 dsll $tempreg,16
5548 daddu $tempreg,$tempreg,$breg
5549 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5550
5551 If we have 64-bit addresses, as an optimization, for
5552 addresses which are 32-bit constants (e.g. kseg0/kseg1
5553 addresses) we fall back to the 32-bit address generation
5554 mechanism since it is more efficient. This code should
5555 probably attempt to generate 64-bit constants more
5556 efficiently in general.
5557 */
5558 if (HAVE_64BIT_ADDRESSES
5559 && !(offset_expr.X_op == O_constant
5560 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5561 {
5562 p = NULL;
5563
5564 /* We don't do GP optimization for now because RELAX_ENCODE can't
5565 hold the data for such large chunks. */
5566
5567 if (used_at == 0)
5568 {
5569 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5570 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5571 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5572 AT, (int) BFD_RELOC_HI16_S);
5573 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5574 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5575 if (breg != 0)
5576 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5577 "d,v,t", AT, AT, breg);
5578 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5579 "d,w,<", tempreg, tempreg, 0);
5580 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5581 "d,v,t", tempreg, tempreg, AT);
5582 macro_build (p, &icnt, &offset_expr, s,
5583 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5584 used_at = 1;
5585 }
5586 else
5587 {
5588 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5589 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5590 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5591 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5592 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5593 "d,w,<", tempreg, tempreg, 16);
5594 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5595 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5596 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5597 "d,w,<", tempreg, tempreg, 16);
5598 if (breg != 0)
5599 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5600 "d,v,t", tempreg, tempreg, breg);
5601 macro_build (p, &icnt, &offset_expr, s,
5602 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5603 }
5604
5605 return;
5606 }
5607
5608 if (breg == 0)
5609 {
5610 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5611 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5612 p = NULL;
5613 else
5614 {
5615 frag_grow (20);
5616 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5617 treg, (int) BFD_RELOC_GPREL16, GP);
5618 p = frag_var (rs_machine_dependent, 8, 0,
5619 RELAX_ENCODE (4, 8, 0, 4, 0,
5620 (mips_opts.warn_about_macros
5621 || (used_at
5622 && mips_opts.noat))),
5623 offset_expr.X_add_symbol, 0, NULL);
5624 used_at = 0;
5625 }
5626 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5627 if (p != NULL)
5628 p += 4;
5629 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5630 (int) BFD_RELOC_LO16, tempreg);
5631 }
5632 else
5633 {
5634 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5635 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5636 p = NULL;
5637 else
5638 {
5639 frag_grow (28);
5640 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5641 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5642 "d,v,t", tempreg, breg, GP);
5643 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5644 treg, (int) BFD_RELOC_GPREL16, tempreg);
5645 p = frag_var (rs_machine_dependent, 12, 0,
5646 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5647 offset_expr.X_add_symbol, 0, NULL);
5648 }
5649 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5650 if (p != NULL)
5651 p += 4;
5652 macro_build (p, &icnt, (expressionS *) NULL,
5653 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5654 "d,v,t", tempreg, tempreg, breg);
5655 if (p != NULL)
5656 p += 4;
5657 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5658 (int) BFD_RELOC_LO16, tempreg);
5659 }
5660 }
5661 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5662 {
5663 /* If this is a reference to an external symbol, we want
5664 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5665 nop
5666 <op> $treg,0($tempreg)
5667 Otherwise we want
5668 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5669 nop
5670 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5671 <op> $treg,0($tempreg)
5672 If there is a base register, we add it to $tempreg before
5673 the <op>. If there is a constant, we stick it in the
5674 <op> instruction. We don't handle constants larger than
5675 16 bits, because we have no way to load the upper 16 bits
5676 (actually, we could handle them for the subset of cases
5677 in which we are not using $at). */
5678 assert (offset_expr.X_op == O_symbol);
5679 expr1.X_add_number = offset_expr.X_add_number;
5680 offset_expr.X_add_number = 0;
5681 if (expr1.X_add_number < -0x8000
5682 || expr1.X_add_number >= 0x8000)
5683 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5684 frag_grow (20);
5685 macro_build ((char *) NULL, &icnt, &offset_expr,
5686 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5687 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5689 p = frag_var (rs_machine_dependent, 4, 0,
5690 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5691 offset_expr.X_add_symbol, 0, NULL);
5692 macro_build (p, &icnt, &offset_expr,
5693 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5694 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5695 if (breg != 0)
5696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5697 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5698 "d,v,t", tempreg, tempreg, breg);
5699 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5700 (int) BFD_RELOC_LO16, tempreg);
5701 }
5702 else if (mips_pic == SVR4_PIC)
5703 {
5704 int gpdel;
5705
5706 /* If this is a reference to an external symbol, we want
5707 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5708 addu $tempreg,$tempreg,$gp
5709 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5710 <op> $treg,0($tempreg)
5711 Otherwise we want
5712 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5713 nop
5714 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5715 <op> $treg,0($tempreg)
5716 If there is a base register, we add it to $tempreg before
5717 the <op>. If there is a constant, we stick it in the
5718 <op> instruction. We don't handle constants larger than
5719 16 bits, because we have no way to load the upper 16 bits
5720 (actually, we could handle them for the subset of cases
5721 in which we are not using $at). */
5722 assert (offset_expr.X_op == O_symbol);
5723 expr1.X_add_number = offset_expr.X_add_number;
5724 offset_expr.X_add_number = 0;
5725 if (expr1.X_add_number < -0x8000
5726 || expr1.X_add_number >= 0x8000)
5727 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5728 if (reg_needs_delay (GP))
5729 gpdel = 4;
5730 else
5731 gpdel = 0;
5732 frag_grow (36);
5733 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5734 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5735 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5736 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5737 "d,v,t", tempreg, tempreg, GP);
5738 macro_build ((char *) NULL, &icnt, &offset_expr,
5739 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5740 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5741 tempreg);
5742 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5743 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5744 offset_expr.X_add_symbol, 0, NULL);
5745 if (gpdel > 0)
5746 {
5747 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5748 p += 4;
5749 }
5750 macro_build (p, &icnt, &offset_expr,
5751 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5752 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5753 p += 4;
5754 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5755 p += 4;
5756 macro_build (p, &icnt, &offset_expr,
5757 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5758 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5759 if (breg != 0)
5760 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5761 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5762 "d,v,t", tempreg, tempreg, breg);
5763 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5764 (int) BFD_RELOC_LO16, tempreg);
5765 }
5766 else if (mips_pic == EMBEDDED_PIC)
5767 {
5768 /* If there is no base register, we want
5769 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5770 If there is a base register, we want
5771 addu $tempreg,$breg,$gp
5772 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5773 */
5774 assert (offset_expr.X_op == O_symbol);
5775 if (breg == 0)
5776 {
5777 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5778 treg, (int) BFD_RELOC_GPREL16, GP);
5779 used_at = 0;
5780 }
5781 else
5782 {
5783 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5784 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5785 "d,v,t", tempreg, breg, GP);
5786 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5787 treg, (int) BFD_RELOC_GPREL16, tempreg);
5788 }
5789 }
5790 else
5791 abort ();
5792
5793 if (! used_at)
5794 return;
5795
5796 break;
5797
5798 case M_LI:
5799 case M_LI_S:
5800 load_register (&icnt, treg, &imm_expr, 0);
5801 return;
5802
5803 case M_DLI:
5804 load_register (&icnt, treg, &imm_expr, 1);
5805 return;
5806
5807 case M_LI_SS:
5808 if (imm_expr.X_op == O_constant)
5809 {
5810 load_register (&icnt, AT, &imm_expr, 0);
5811 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5812 "mtc1", "t,G", AT, treg);
5813 break;
5814 }
5815 else
5816 {
5817 assert (offset_expr.X_op == O_symbol
5818 && strcmp (segment_name (S_GET_SEGMENT
5819 (offset_expr.X_add_symbol)),
5820 ".lit4") == 0
5821 && offset_expr.X_add_number == 0);
5822 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5823 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5824 return;
5825 }
5826
5827 case M_LI_D:
5828 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5829 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5830 order 32 bits of the value and the low order 32 bits are either
5831 zero or in OFFSET_EXPR. */
5832 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5833 {
5834 if (HAVE_64BIT_GPRS)
5835 load_register (&icnt, treg, &imm_expr, 1);
5836 else
5837 {
5838 int hreg, lreg;
5839
5840 if (target_big_endian)
5841 {
5842 hreg = treg;
5843 lreg = treg + 1;
5844 }
5845 else
5846 {
5847 hreg = treg + 1;
5848 lreg = treg;
5849 }
5850
5851 if (hreg <= 31)
5852 load_register (&icnt, hreg, &imm_expr, 0);
5853 if (lreg <= 31)
5854 {
5855 if (offset_expr.X_op == O_absent)
5856 move_register (&icnt, lreg, 0);
5857 else
5858 {
5859 assert (offset_expr.X_op == O_constant);
5860 load_register (&icnt, lreg, &offset_expr, 0);
5861 }
5862 }
5863 }
5864 return;
5865 }
5866
5867 /* We know that sym is in the .rdata section. First we get the
5868 upper 16 bits of the address. */
5869 if (mips_pic == NO_PIC)
5870 {
5871 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5872 }
5873 else if (mips_pic == SVR4_PIC)
5874 {
5875 macro_build ((char *) NULL, &icnt, &offset_expr,
5876 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5877 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5878 }
5879 else if (mips_pic == EMBEDDED_PIC)
5880 {
5881 /* For embedded PIC we pick up the entire address off $gp in
5882 a single instruction. */
5883 macro_build ((char *) NULL, &icnt, &offset_expr,
5884 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5885 "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5886 offset_expr.X_op = O_constant;
5887 offset_expr.X_add_number = 0;
5888 }
5889 else
5890 abort ();
5891
5892 /* Now we load the register(s). */
5893 if (HAVE_64BIT_GPRS)
5894 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5895 treg, (int) BFD_RELOC_LO16, AT);
5896 else
5897 {
5898 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5899 treg, (int) BFD_RELOC_LO16, AT);
5900 if (treg != 31)
5901 {
5902 /* FIXME: How in the world do we deal with the possible
5903 overflow here? */
5904 offset_expr.X_add_number += 4;
5905 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5906 treg + 1, (int) BFD_RELOC_LO16, AT);
5907 }
5908 }
5909
5910 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5911 does not become a variant frag. */
5912 frag_wane (frag_now);
5913 frag_new (0);
5914
5915 break;
5916
5917 case M_LI_DD:
5918 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5919 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5920 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5921 the value and the low order 32 bits are either zero or in
5922 OFFSET_EXPR. */
5923 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5924 {
5925 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5926 if (HAVE_64BIT_FPRS)
5927 {
5928 assert (HAVE_64BIT_GPRS);
5929 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5930 "dmtc1", "t,S", AT, treg);
5931 }
5932 else
5933 {
5934 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5935 "mtc1", "t,G", AT, treg + 1);
5936 if (offset_expr.X_op == O_absent)
5937 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5938 "mtc1", "t,G", 0, treg);
5939 else
5940 {
5941 assert (offset_expr.X_op == O_constant);
5942 load_register (&icnt, AT, &offset_expr, 0);
5943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5944 "mtc1", "t,G", AT, treg);
5945 }
5946 }
5947 break;
5948 }
5949
5950 assert (offset_expr.X_op == O_symbol
5951 && offset_expr.X_add_number == 0);
5952 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5953 if (strcmp (s, ".lit8") == 0)
5954 {
5955 if (mips_opts.isa != ISA_MIPS1)
5956 {
5957 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5958 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5959 return;
5960 }
5961 breg = GP;
5962 r = BFD_RELOC_MIPS_LITERAL;
5963 goto dob;
5964 }
5965 else
5966 {
5967 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5968 if (mips_pic == SVR4_PIC)
5969 macro_build ((char *) NULL, &icnt, &offset_expr,
5970 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5971 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5972 else
5973 {
5974 /* FIXME: This won't work for a 64 bit address. */
5975 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5976 }
5977
5978 if (mips_opts.isa != ISA_MIPS1)
5979 {
5980 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5981 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5982
5983 /* To avoid confusion in tc_gen_reloc, we must ensure
5984 that this does not become a variant frag. */
5985 frag_wane (frag_now);
5986 frag_new (0);
5987
5988 break;
5989 }
5990 breg = AT;
5991 r = BFD_RELOC_LO16;
5992 goto dob;
5993 }
5994
5995 case M_L_DOB:
5996 if (mips_arch == CPU_R4650)
5997 {
5998 as_bad (_("opcode not supported on this processor"));
5999 return;
6000 }
6001 /* Even on a big endian machine $fn comes before $fn+1. We have
6002 to adjust when loading from memory. */
6003 r = BFD_RELOC_LO16;
6004 dob:
6005 assert (mips_opts.isa == ISA_MIPS1);
6006 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6007 target_big_endian ? treg + 1 : treg,
6008 (int) r, breg);
6009 /* FIXME: A possible overflow which I don't know how to deal
6010 with. */
6011 offset_expr.X_add_number += 4;
6012 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6013 target_big_endian ? treg : treg + 1,
6014 (int) r, breg);
6015
6016 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6017 does not become a variant frag. */
6018 frag_wane (frag_now);
6019 frag_new (0);
6020
6021 if (breg != AT)
6022 return;
6023 break;
6024
6025 case M_L_DAB:
6026 /*
6027 * The MIPS assembler seems to check for X_add_number not
6028 * being double aligned and generating:
6029 * lui at,%hi(foo+1)
6030 * addu at,at,v1
6031 * addiu at,at,%lo(foo+1)
6032 * lwc1 f2,0(at)
6033 * lwc1 f3,4(at)
6034 * But, the resulting address is the same after relocation so why
6035 * generate the extra instruction?
6036 */
6037 if (mips_arch == CPU_R4650)
6038 {
6039 as_bad (_("opcode not supported on this processor"));
6040 return;
6041 }
6042 /* Itbl support may require additional care here. */
6043 coproc = 1;
6044 if (mips_opts.isa != ISA_MIPS1)
6045 {
6046 s = "ldc1";
6047 goto ld;
6048 }
6049
6050 s = "lwc1";
6051 fmt = "T,o(b)";
6052 goto ldd_std;
6053
6054 case M_S_DAB:
6055 if (mips_arch == CPU_R4650)
6056 {
6057 as_bad (_("opcode not supported on this processor"));
6058 return;
6059 }
6060
6061 if (mips_opts.isa != ISA_MIPS1)
6062 {
6063 s = "sdc1";
6064 goto st;
6065 }
6066
6067 s = "swc1";
6068 fmt = "T,o(b)";
6069 /* Itbl support may require additional care here. */
6070 coproc = 1;
6071 goto ldd_std;
6072
6073 case M_LD_AB:
6074 if (HAVE_64BIT_GPRS)
6075 {
6076 s = "ld";
6077 goto ld;
6078 }
6079
6080 s = "lw";
6081 fmt = "t,o(b)";
6082 goto ldd_std;
6083
6084 case M_SD_AB:
6085 if (HAVE_64BIT_GPRS)
6086 {
6087 s = "sd";
6088 goto st;
6089 }
6090
6091 s = "sw";
6092 fmt = "t,o(b)";
6093
6094 ldd_std:
6095 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6096 loads for the case of doing a pair of loads to simulate an 'ld'.
6097 This is not currently done by the compiler, and assembly coders
6098 writing embedded-pic code can cope. */
6099
6100 if (offset_expr.X_op != O_symbol
6101 && offset_expr.X_op != O_constant)
6102 {
6103 as_bad (_("expression too complex"));
6104 offset_expr.X_op = O_constant;
6105 }
6106
6107 /* Even on a big endian machine $fn comes before $fn+1. We have
6108 to adjust when loading from memory. We set coproc if we must
6109 load $fn+1 first. */
6110 /* Itbl support may require additional care here. */
6111 if (! target_big_endian)
6112 coproc = 0;
6113
6114 if (mips_pic == NO_PIC
6115 || offset_expr.X_op == O_constant)
6116 {
6117 /* If this is a reference to a GP relative symbol, we want
6118 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6119 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6120 If we have a base register, we use this
6121 addu $at,$breg,$gp
6122 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6123 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6124 If this is not a GP relative symbol, we want
6125 lui $at,<sym> (BFD_RELOC_HI16_S)
6126 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6127 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6128 If there is a base register, we add it to $at after the
6129 lui instruction. If there is a constant, we always use
6130 the last case. */
6131 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6132 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6133 {
6134 p = NULL;
6135 used_at = 1;
6136 }
6137 else
6138 {
6139 int off;
6140
6141 if (breg == 0)
6142 {
6143 frag_grow (28);
6144 tempreg = GP;
6145 off = 0;
6146 used_at = 0;
6147 }
6148 else
6149 {
6150 frag_grow (36);
6151 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6152 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6153 "d,v,t", AT, breg, GP);
6154 tempreg = AT;
6155 off = 4;
6156 used_at = 1;
6157 }
6158
6159 /* Itbl support may require additional care here. */
6160 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6161 coproc ? treg + 1 : treg,
6162 (int) BFD_RELOC_GPREL16, tempreg);
6163 offset_expr.X_add_number += 4;
6164
6165 /* Set mips_optimize to 2 to avoid inserting an
6166 undesired nop. */
6167 hold_mips_optimize = mips_optimize;
6168 mips_optimize = 2;
6169 /* Itbl support may require additional care here. */
6170 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6171 coproc ? treg : treg + 1,
6172 (int) BFD_RELOC_GPREL16, tempreg);
6173 mips_optimize = hold_mips_optimize;
6174
6175 p = frag_var (rs_machine_dependent, 12 + off, 0,
6176 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6177 used_at && mips_opts.noat),
6178 offset_expr.X_add_symbol, 0, NULL);
6179
6180 /* We just generated two relocs. When tc_gen_reloc
6181 handles this case, it will skip the first reloc and
6182 handle the second. The second reloc already has an
6183 extra addend of 4, which we added above. We must
6184 subtract it out, and then subtract another 4 to make
6185 the first reloc come out right. The second reloc
6186 will come out right because we are going to add 4 to
6187 offset_expr when we build its instruction below.
6188
6189 If we have a symbol, then we don't want to include
6190 the offset, because it will wind up being included
6191 when we generate the reloc. */
6192
6193 if (offset_expr.X_op == O_constant)
6194 offset_expr.X_add_number -= 8;
6195 else
6196 {
6197 offset_expr.X_add_number = -4;
6198 offset_expr.X_op = O_constant;
6199 }
6200 }
6201 macro_build_lui (p, &icnt, &offset_expr, AT);
6202 if (p != NULL)
6203 p += 4;
6204 if (breg != 0)
6205 {
6206 macro_build (p, &icnt, (expressionS *) NULL,
6207 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6208 "d,v,t", AT, breg, AT);
6209 if (p != NULL)
6210 p += 4;
6211 }
6212 /* Itbl support may require additional care here. */
6213 macro_build (p, &icnt, &offset_expr, s, fmt,
6214 coproc ? treg + 1 : treg,
6215 (int) BFD_RELOC_LO16, AT);
6216 if (p != NULL)
6217 p += 4;
6218 /* FIXME: How do we handle overflow here? */
6219 offset_expr.X_add_number += 4;
6220 /* Itbl support may require additional care here. */
6221 macro_build (p, &icnt, &offset_expr, s, fmt,
6222 coproc ? treg : treg + 1,
6223 (int) BFD_RELOC_LO16, AT);
6224 }
6225 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6226 {
6227 int off;
6228
6229 /* If this is a reference to an external symbol, we want
6230 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6231 nop
6232 <op> $treg,0($at)
6233 <op> $treg+1,4($at)
6234 Otherwise we want
6235 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6236 nop
6237 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6238 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6239 If there is a base register we add it to $at before the
6240 lwc1 instructions. If there is a constant we include it
6241 in the lwc1 instructions. */
6242 used_at = 1;
6243 expr1.X_add_number = offset_expr.X_add_number;
6244 offset_expr.X_add_number = 0;
6245 if (expr1.X_add_number < -0x8000
6246 || expr1.X_add_number >= 0x8000 - 4)
6247 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6248 if (breg == 0)
6249 off = 0;
6250 else
6251 off = 4;
6252 frag_grow (24 + off);
6253 macro_build ((char *) NULL, &icnt, &offset_expr,
6254 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6255 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6256 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6257 if (breg != 0)
6258 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6259 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6260 "d,v,t", AT, breg, AT);
6261 /* Itbl support may require additional care here. */
6262 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6263 coproc ? treg + 1 : treg,
6264 (int) BFD_RELOC_LO16, AT);
6265 expr1.X_add_number += 4;
6266
6267 /* Set mips_optimize to 2 to avoid inserting an undesired
6268 nop. */
6269 hold_mips_optimize = mips_optimize;
6270 mips_optimize = 2;
6271 /* Itbl support may require additional care here. */
6272 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6273 coproc ? treg : treg + 1,
6274 (int) BFD_RELOC_LO16, AT);
6275 mips_optimize = hold_mips_optimize;
6276
6277 (void) frag_var (rs_machine_dependent, 0, 0,
6278 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6279 offset_expr.X_add_symbol, 0, NULL);
6280 }
6281 else if (mips_pic == SVR4_PIC)
6282 {
6283 int gpdel, off;
6284
6285 /* If this is a reference to an external symbol, we want
6286 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6287 addu $at,$at,$gp
6288 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6289 nop
6290 <op> $treg,0($at)
6291 <op> $treg+1,4($at)
6292 Otherwise we want
6293 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6294 nop
6295 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6296 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6297 If there is a base register we add it to $at before the
6298 lwc1 instructions. If there is a constant we include it
6299 in the lwc1 instructions. */
6300 used_at = 1;
6301 expr1.X_add_number = offset_expr.X_add_number;
6302 offset_expr.X_add_number = 0;
6303 if (expr1.X_add_number < -0x8000
6304 || expr1.X_add_number >= 0x8000 - 4)
6305 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6306 if (reg_needs_delay (GP))
6307 gpdel = 4;
6308 else
6309 gpdel = 0;
6310 if (breg == 0)
6311 off = 0;
6312 else
6313 off = 4;
6314 frag_grow (56);
6315 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6316 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6317 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6318 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6319 "d,v,t", AT, AT, GP);
6320 macro_build ((char *) NULL, &icnt, &offset_expr,
6321 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6322 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6323 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6324 if (breg != 0)
6325 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6326 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6327 "d,v,t", AT, breg, AT);
6328 /* Itbl support may require additional care here. */
6329 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6330 coproc ? treg + 1 : treg,
6331 (int) BFD_RELOC_LO16, AT);
6332 expr1.X_add_number += 4;
6333
6334 /* Set mips_optimize to 2 to avoid inserting an undesired
6335 nop. */
6336 hold_mips_optimize = mips_optimize;
6337 mips_optimize = 2;
6338 /* Itbl support may require additional care here. */
6339 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6340 coproc ? treg : treg + 1,
6341 (int) BFD_RELOC_LO16, AT);
6342 mips_optimize = hold_mips_optimize;
6343 expr1.X_add_number -= 4;
6344
6345 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6346 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6347 8 + gpdel + off, 1, 0),
6348 offset_expr.X_add_symbol, 0, NULL);
6349 if (gpdel > 0)
6350 {
6351 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6352 p += 4;
6353 }
6354 macro_build (p, &icnt, &offset_expr,
6355 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6356 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6357 p += 4;
6358 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6359 p += 4;
6360 if (breg != 0)
6361 {
6362 macro_build (p, &icnt, (expressionS *) NULL,
6363 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6364 "d,v,t", AT, breg, AT);
6365 p += 4;
6366 }
6367 /* Itbl support may require additional care here. */
6368 macro_build (p, &icnt, &expr1, s, fmt,
6369 coproc ? treg + 1 : treg,
6370 (int) BFD_RELOC_LO16, AT);
6371 p += 4;
6372 expr1.X_add_number += 4;
6373
6374 /* Set mips_optimize to 2 to avoid inserting an undesired
6375 nop. */
6376 hold_mips_optimize = mips_optimize;
6377 mips_optimize = 2;
6378 /* Itbl support may require additional care here. */
6379 macro_build (p, &icnt, &expr1, s, fmt,
6380 coproc ? treg : treg + 1,
6381 (int) BFD_RELOC_LO16, AT);
6382 mips_optimize = hold_mips_optimize;
6383 }
6384 else if (mips_pic == EMBEDDED_PIC)
6385 {
6386 /* If there is no base register, we use
6387 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6388 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6389 If we have a base register, we use
6390 addu $at,$breg,$gp
6391 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6392 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6393 */
6394 if (breg == 0)
6395 {
6396 tempreg = GP;
6397 used_at = 0;
6398 }
6399 else
6400 {
6401 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6402 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6403 "d,v,t", AT, breg, GP);
6404 tempreg = AT;
6405 used_at = 1;
6406 }
6407
6408 /* Itbl support may require additional care here. */
6409 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6410 coproc ? treg + 1 : treg,
6411 (int) BFD_RELOC_GPREL16, tempreg);
6412 offset_expr.X_add_number += 4;
6413 /* Itbl support may require additional care here. */
6414 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6415 coproc ? treg : treg + 1,
6416 (int) BFD_RELOC_GPREL16, tempreg);
6417 }
6418 else
6419 abort ();
6420
6421 if (! used_at)
6422 return;
6423
6424 break;
6425
6426 case M_LD_OB:
6427 s = "lw";
6428 goto sd_ob;
6429 case M_SD_OB:
6430 s = "sw";
6431 sd_ob:
6432 assert (HAVE_32BIT_ADDRESSES);
6433 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6434 (int) BFD_RELOC_LO16, breg);
6435 offset_expr.X_add_number += 4;
6436 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6437 (int) BFD_RELOC_LO16, breg);
6438 return;
6439
6440 /* New code added to support COPZ instructions.
6441 This code builds table entries out of the macros in mip_opcodes.
6442 R4000 uses interlocks to handle coproc delays.
6443 Other chips (like the R3000) require nops to be inserted for delays.
6444
6445 FIXME: Currently, we require that the user handle delays.
6446 In order to fill delay slots for non-interlocked chips,
6447 we must have a way to specify delays based on the coprocessor.
6448 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6449 What are the side-effects of the cop instruction?
6450 What cache support might we have and what are its effects?
6451 Both coprocessor & memory require delays. how long???
6452 What registers are read/set/modified?
6453
6454 If an itbl is provided to interpret cop instructions,
6455 this knowledge can be encoded in the itbl spec. */
6456
6457 case M_COP0:
6458 s = "c0";
6459 goto copz;
6460 case M_COP1:
6461 s = "c1";
6462 goto copz;
6463 case M_COP2:
6464 s = "c2";
6465 goto copz;
6466 case M_COP3:
6467 s = "c3";
6468 copz:
6469 /* For now we just do C (same as Cz). The parameter will be
6470 stored in insn_opcode by mips_ip. */
6471 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6472 ip->insn_opcode);
6473 return;
6474
6475 case M_MOVE:
6476 move_register (&icnt, dreg, sreg);
6477 return;
6478
6479 #ifdef LOSING_COMPILER
6480 default:
6481 /* Try and see if this is a new itbl instruction.
6482 This code builds table entries out of the macros in mip_opcodes.
6483 FIXME: For now we just assemble the expression and pass it's
6484 value along as a 32-bit immediate.
6485 We may want to have the assembler assemble this value,
6486 so that we gain the assembler's knowledge of delay slots,
6487 symbols, etc.
6488 Would it be more efficient to use mask (id) here? */
6489 if (itbl_have_entries
6490 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6491 {
6492 s = ip->insn_mo->name;
6493 s2 = "cop3";
6494 coproc = ITBL_DECODE_PNUM (immed_expr);;
6495 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6496 return;
6497 }
6498 macro2 (ip);
6499 return;
6500 }
6501 if (mips_opts.noat)
6502 as_warn (_("Macro used $at after \".set noat\""));
6503 }
6504
6505 static void
6506 macro2 (ip)
6507 struct mips_cl_insn *ip;
6508 {
6509 register int treg, sreg, dreg, breg;
6510 int tempreg;
6511 int mask;
6512 int icnt = 0;
6513 int used_at;
6514 expressionS expr1;
6515 const char *s;
6516 const char *s2;
6517 const char *fmt;
6518 int likely = 0;
6519 int dbl = 0;
6520 int coproc = 0;
6521 int lr = 0;
6522 int imm = 0;
6523 int off;
6524 offsetT maxnum;
6525 bfd_reloc_code_real_type r;
6526 char *p;
6527
6528 treg = (ip->insn_opcode >> 16) & 0x1f;
6529 dreg = (ip->insn_opcode >> 11) & 0x1f;
6530 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6531 mask = ip->insn_mo->mask;
6532
6533 expr1.X_op = O_constant;
6534 expr1.X_op_symbol = NULL;
6535 expr1.X_add_symbol = NULL;
6536 expr1.X_add_number = 1;
6537
6538 switch (mask)
6539 {
6540 #endif /* LOSING_COMPILER */
6541
6542 case M_DMUL:
6543 dbl = 1;
6544 case M_MUL:
6545 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6546 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6548 dreg);
6549 return;
6550
6551 case M_DMUL_I:
6552 dbl = 1;
6553 case M_MUL_I:
6554 /* The MIPS assembler some times generates shifts and adds. I'm
6555 not trying to be that fancy. GCC should do this for us
6556 anyway. */
6557 load_register (&icnt, AT, &imm_expr, dbl);
6558 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6559 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6560 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6561 dreg);
6562 break;
6563
6564 case M_DMULO_I:
6565 dbl = 1;
6566 case M_MULO_I:
6567 imm = 1;
6568 goto do_mulo;
6569
6570 case M_DMULO:
6571 dbl = 1;
6572 case M_MULO:
6573 do_mulo:
6574 mips_emit_delays (true);
6575 ++mips_opts.noreorder;
6576 mips_any_noreorder = 1;
6577 if (imm)
6578 load_register (&icnt, AT, &imm_expr, dbl);
6579 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6580 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6581 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6582 dreg);
6583 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6584 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6585 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6586 AT);
6587 if (mips_trap)
6588 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6589 dreg, AT);
6590 else
6591 {
6592 expr1.X_add_number = 8;
6593 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6594 AT);
6595 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6596 0);
6597 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6598 "c", 6);
6599 }
6600 --mips_opts.noreorder;
6601 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6602 break;
6603
6604 case M_DMULOU_I:
6605 dbl = 1;
6606 case M_MULOU_I:
6607 imm = 1;
6608 goto do_mulou;
6609
6610 case M_DMULOU:
6611 dbl = 1;
6612 case M_MULOU:
6613 do_mulou:
6614 mips_emit_delays (true);
6615 ++mips_opts.noreorder;
6616 mips_any_noreorder = 1;
6617 if (imm)
6618 load_register (&icnt, AT, &imm_expr, dbl);
6619 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6620 dbl ? "dmultu" : "multu",
6621 "s,t", sreg, imm ? AT : treg);
6622 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6623 AT);
6624 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6625 dreg);
6626 if (mips_trap)
6627 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6628 AT, 0);
6629 else
6630 {
6631 expr1.X_add_number = 8;
6632 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6634 0);
6635 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6636 "c", 6);
6637 }
6638 --mips_opts.noreorder;
6639 break;
6640
6641 case M_ROL:
6642 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6643 "d,v,t", AT, 0, treg);
6644 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6645 "d,t,s", AT, sreg, AT);
6646 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6647 "d,t,s", dreg, sreg, treg);
6648 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6649 "d,v,t", dreg, dreg, AT);
6650 break;
6651
6652 case M_ROL_I:
6653 if (imm_expr.X_op != O_constant)
6654 as_bad (_("rotate count too large"));
6655 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6656 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6658 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6659 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6660 dreg, dreg, AT);
6661 break;
6662
6663 case M_ROR:
6664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6665 "d,v,t", AT, 0, treg);
6666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6667 "d,t,s", AT, sreg, AT);
6668 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6669 "d,t,s", dreg, sreg, treg);
6670 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6671 "d,v,t", dreg, dreg, AT);
6672 break;
6673
6674 case M_ROR_I:
6675 if (imm_expr.X_op != O_constant)
6676 as_bad (_("rotate count too large"));
6677 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6678 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6679 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6680 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6681 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6682 dreg, dreg, AT);
6683 break;
6684
6685 case M_S_DOB:
6686 if (mips_arch == CPU_R4650)
6687 {
6688 as_bad (_("opcode not supported on this processor"));
6689 return;
6690 }
6691 assert (mips_opts.isa == ISA_MIPS1);
6692 /* Even on a big endian machine $fn comes before $fn+1. We have
6693 to adjust when storing to memory. */
6694 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6695 target_big_endian ? treg + 1 : treg,
6696 (int) BFD_RELOC_LO16, breg);
6697 offset_expr.X_add_number += 4;
6698 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6699 target_big_endian ? treg : treg + 1,
6700 (int) BFD_RELOC_LO16, breg);
6701 return;
6702
6703 case M_SEQ:
6704 if (sreg == 0)
6705 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6706 treg, (int) BFD_RELOC_LO16);
6707 else if (treg == 0)
6708 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6709 sreg, (int) BFD_RELOC_LO16);
6710 else
6711 {
6712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6713 "d,v,t", dreg, sreg, treg);
6714 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6715 dreg, (int) BFD_RELOC_LO16);
6716 }
6717 return;
6718
6719 case M_SEQ_I:
6720 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6721 {
6722 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6723 sreg, (int) BFD_RELOC_LO16);
6724 return;
6725 }
6726 if (sreg == 0)
6727 {
6728 as_warn (_("Instruction %s: result is always false"),
6729 ip->insn_mo->name);
6730 move_register (&icnt, dreg, 0);
6731 return;
6732 }
6733 if (imm_expr.X_op == O_constant
6734 && imm_expr.X_add_number >= 0
6735 && imm_expr.X_add_number < 0x10000)
6736 {
6737 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6738 sreg, (int) BFD_RELOC_LO16);
6739 used_at = 0;
6740 }
6741 else if (imm_expr.X_op == O_constant
6742 && imm_expr.X_add_number > -0x8000
6743 && imm_expr.X_add_number < 0)
6744 {
6745 imm_expr.X_add_number = -imm_expr.X_add_number;
6746 macro_build ((char *) NULL, &icnt, &imm_expr,
6747 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6748 "t,r,j", dreg, sreg,
6749 (int) BFD_RELOC_LO16);
6750 used_at = 0;
6751 }
6752 else
6753 {
6754 load_register (&icnt, AT, &imm_expr, 0);
6755 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6756 "d,v,t", dreg, sreg, AT);
6757 used_at = 1;
6758 }
6759 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6760 (int) BFD_RELOC_LO16);
6761 if (used_at)
6762 break;
6763 return;
6764
6765 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6766 s = "slt";
6767 goto sge;
6768 case M_SGEU:
6769 s = "sltu";
6770 sge:
6771 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6772 dreg, sreg, treg);
6773 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6774 (int) BFD_RELOC_LO16);
6775 return;
6776
6777 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6778 case M_SGEU_I:
6779 if (imm_expr.X_op == O_constant
6780 && imm_expr.X_add_number >= -0x8000
6781 && imm_expr.X_add_number < 0x8000)
6782 {
6783 macro_build ((char *) NULL, &icnt, &imm_expr,
6784 mask == M_SGE_I ? "slti" : "sltiu",
6785 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6786 used_at = 0;
6787 }
6788 else
6789 {
6790 load_register (&icnt, AT, &imm_expr, 0);
6791 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6792 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6793 AT);
6794 used_at = 1;
6795 }
6796 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6797 (int) BFD_RELOC_LO16);
6798 if (used_at)
6799 break;
6800 return;
6801
6802 case M_SGT: /* sreg > treg <==> treg < sreg */
6803 s = "slt";
6804 goto sgt;
6805 case M_SGTU:
6806 s = "sltu";
6807 sgt:
6808 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6809 dreg, treg, sreg);
6810 return;
6811
6812 case M_SGT_I: /* sreg > I <==> I < sreg */
6813 s = "slt";
6814 goto sgti;
6815 case M_SGTU_I:
6816 s = "sltu";
6817 sgti:
6818 load_register (&icnt, AT, &imm_expr, 0);
6819 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6820 dreg, AT, sreg);
6821 break;
6822
6823 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6824 s = "slt";
6825 goto sle;
6826 case M_SLEU:
6827 s = "sltu";
6828 sle:
6829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6830 dreg, treg, sreg);
6831 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6832 (int) BFD_RELOC_LO16);
6833 return;
6834
6835 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6836 s = "slt";
6837 goto slei;
6838 case M_SLEU_I:
6839 s = "sltu";
6840 slei:
6841 load_register (&icnt, AT, &imm_expr, 0);
6842 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6843 dreg, AT, sreg);
6844 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6845 (int) BFD_RELOC_LO16);
6846 break;
6847
6848 case M_SLT_I:
6849 if (imm_expr.X_op == O_constant
6850 && imm_expr.X_add_number >= -0x8000
6851 && imm_expr.X_add_number < 0x8000)
6852 {
6853 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6854 dreg, sreg, (int) BFD_RELOC_LO16);
6855 return;
6856 }
6857 load_register (&icnt, AT, &imm_expr, 0);
6858 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6859 dreg, sreg, AT);
6860 break;
6861
6862 case M_SLTU_I:
6863 if (imm_expr.X_op == O_constant
6864 && imm_expr.X_add_number >= -0x8000
6865 && imm_expr.X_add_number < 0x8000)
6866 {
6867 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6868 dreg, sreg, (int) BFD_RELOC_LO16);
6869 return;
6870 }
6871 load_register (&icnt, AT, &imm_expr, 0);
6872 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6873 "d,v,t", dreg, sreg, AT);
6874 break;
6875
6876 case M_SNE:
6877 if (sreg == 0)
6878 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6879 "d,v,t", dreg, 0, treg);
6880 else if (treg == 0)
6881 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6882 "d,v,t", dreg, 0, sreg);
6883 else
6884 {
6885 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6886 "d,v,t", dreg, sreg, treg);
6887 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6888 "d,v,t", dreg, 0, dreg);
6889 }
6890 return;
6891
6892 case M_SNE_I:
6893 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6894 {
6895 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6896 "d,v,t", dreg, 0, sreg);
6897 return;
6898 }
6899 if (sreg == 0)
6900 {
6901 as_warn (_("Instruction %s: result is always true"),
6902 ip->insn_mo->name);
6903 macro_build ((char *) NULL, &icnt, &expr1,
6904 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6905 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6906 return;
6907 }
6908 if (imm_expr.X_op == O_constant
6909 && imm_expr.X_add_number >= 0
6910 && imm_expr.X_add_number < 0x10000)
6911 {
6912 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6913 dreg, sreg, (int) BFD_RELOC_LO16);
6914 used_at = 0;
6915 }
6916 else if (imm_expr.X_op == O_constant
6917 && imm_expr.X_add_number > -0x8000
6918 && imm_expr.X_add_number < 0)
6919 {
6920 imm_expr.X_add_number = -imm_expr.X_add_number;
6921 macro_build ((char *) NULL, &icnt, &imm_expr,
6922 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6923 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6924 used_at = 0;
6925 }
6926 else
6927 {
6928 load_register (&icnt, AT, &imm_expr, 0);
6929 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6930 "d,v,t", dreg, sreg, AT);
6931 used_at = 1;
6932 }
6933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6934 "d,v,t", dreg, 0, dreg);
6935 if (used_at)
6936 break;
6937 return;
6938
6939 case M_DSUB_I:
6940 dbl = 1;
6941 case M_SUB_I:
6942 if (imm_expr.X_op == O_constant
6943 && imm_expr.X_add_number > -0x8000
6944 && imm_expr.X_add_number <= 0x8000)
6945 {
6946 imm_expr.X_add_number = -imm_expr.X_add_number;
6947 macro_build ((char *) NULL, &icnt, &imm_expr,
6948 dbl ? "daddi" : "addi",
6949 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6950 return;
6951 }
6952 load_register (&icnt, AT, &imm_expr, dbl);
6953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6954 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6955 break;
6956
6957 case M_DSUBU_I:
6958 dbl = 1;
6959 case M_SUBU_I:
6960 if (imm_expr.X_op == O_constant
6961 && imm_expr.X_add_number > -0x8000
6962 && imm_expr.X_add_number <= 0x8000)
6963 {
6964 imm_expr.X_add_number = -imm_expr.X_add_number;
6965 macro_build ((char *) NULL, &icnt, &imm_expr,
6966 dbl ? "daddiu" : "addiu",
6967 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6968 return;
6969 }
6970 load_register (&icnt, AT, &imm_expr, dbl);
6971 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6972 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6973 break;
6974
6975 case M_TEQ_I:
6976 s = "teq";
6977 goto trap;
6978 case M_TGE_I:
6979 s = "tge";
6980 goto trap;
6981 case M_TGEU_I:
6982 s = "tgeu";
6983 goto trap;
6984 case M_TLT_I:
6985 s = "tlt";
6986 goto trap;
6987 case M_TLTU_I:
6988 s = "tltu";
6989 goto trap;
6990 case M_TNE_I:
6991 s = "tne";
6992 trap:
6993 load_register (&icnt, AT, &imm_expr, 0);
6994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
6995 AT);
6996 break;
6997
6998 case M_TRUNCWS:
6999 case M_TRUNCWD:
7000 assert (mips_opts.isa == ISA_MIPS1);
7001 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7002 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7003
7004 /*
7005 * Is the double cfc1 instruction a bug in the mips assembler;
7006 * or is there a reason for it?
7007 */
7008 mips_emit_delays (true);
7009 ++mips_opts.noreorder;
7010 mips_any_noreorder = 1;
7011 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7012 treg, 31);
7013 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7014 treg, 31);
7015 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7016 expr1.X_add_number = 3;
7017 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7018 (int) BFD_RELOC_LO16);
7019 expr1.X_add_number = 2;
7020 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7021 (int) BFD_RELOC_LO16);
7022 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7023 AT, 31);
7024 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7026 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7027 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7028 treg, 31);
7029 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7030 --mips_opts.noreorder;
7031 break;
7032
7033 case M_ULH:
7034 s = "lb";
7035 goto ulh;
7036 case M_ULHU:
7037 s = "lbu";
7038 ulh:
7039 if (offset_expr.X_add_number >= 0x7fff)
7040 as_bad (_("operand overflow"));
7041 /* avoid load delay */
7042 if (! target_big_endian)
7043 offset_expr.X_add_number += 1;
7044 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7045 (int) BFD_RELOC_LO16, breg);
7046 if (! target_big_endian)
7047 offset_expr.X_add_number -= 1;
7048 else
7049 offset_expr.X_add_number += 1;
7050 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7051 (int) BFD_RELOC_LO16, breg);
7052 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7053 treg, treg, 8);
7054 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7055 treg, treg, AT);
7056 break;
7057
7058 case M_ULD:
7059 s = "ldl";
7060 s2 = "ldr";
7061 off = 7;
7062 goto ulw;
7063 case M_ULW:
7064 s = "lwl";
7065 s2 = "lwr";
7066 off = 3;
7067 ulw:
7068 if (offset_expr.X_add_number >= 0x8000 - off)
7069 as_bad (_("operand overflow"));
7070 if (! target_big_endian)
7071 offset_expr.X_add_number += off;
7072 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7073 (int) BFD_RELOC_LO16, breg);
7074 if (! target_big_endian)
7075 offset_expr.X_add_number -= off;
7076 else
7077 offset_expr.X_add_number += off;
7078 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7079 (int) BFD_RELOC_LO16, breg);
7080 return;
7081
7082 case M_ULD_A:
7083 s = "ldl";
7084 s2 = "ldr";
7085 off = 7;
7086 goto ulwa;
7087 case M_ULW_A:
7088 s = "lwl";
7089 s2 = "lwr";
7090 off = 3;
7091 ulwa:
7092 used_at = 1;
7093 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7094 if (breg != 0)
7095 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7096 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7097 "d,v,t", AT, AT, breg);
7098 if (! target_big_endian)
7099 expr1.X_add_number = off;
7100 else
7101 expr1.X_add_number = 0;
7102 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7103 (int) BFD_RELOC_LO16, AT);
7104 if (! target_big_endian)
7105 expr1.X_add_number = 0;
7106 else
7107 expr1.X_add_number = off;
7108 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7109 (int) BFD_RELOC_LO16, AT);
7110 break;
7111
7112 case M_ULH_A:
7113 case M_ULHU_A:
7114 used_at = 1;
7115 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7116 if (breg != 0)
7117 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7118 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7119 "d,v,t", AT, AT, breg);
7120 if (target_big_endian)
7121 expr1.X_add_number = 0;
7122 macro_build ((char *) NULL, &icnt, &expr1,
7123 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7124 (int) BFD_RELOC_LO16, AT);
7125 if (target_big_endian)
7126 expr1.X_add_number = 1;
7127 else
7128 expr1.X_add_number = 0;
7129 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7130 (int) BFD_RELOC_LO16, AT);
7131 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7132 treg, treg, 8);
7133 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7134 treg, treg, AT);
7135 break;
7136
7137 case M_USH:
7138 if (offset_expr.X_add_number >= 0x7fff)
7139 as_bad (_("operand overflow"));
7140 if (target_big_endian)
7141 offset_expr.X_add_number += 1;
7142 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7143 (int) BFD_RELOC_LO16, breg);
7144 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7145 AT, treg, 8);
7146 if (target_big_endian)
7147 offset_expr.X_add_number -= 1;
7148 else
7149 offset_expr.X_add_number += 1;
7150 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7151 (int) BFD_RELOC_LO16, breg);
7152 break;
7153
7154 case M_USD:
7155 s = "sdl";
7156 s2 = "sdr";
7157 off = 7;
7158 goto usw;
7159 case M_USW:
7160 s = "swl";
7161 s2 = "swr";
7162 off = 3;
7163 usw:
7164 if (offset_expr.X_add_number >= 0x8000 - off)
7165 as_bad (_("operand overflow"));
7166 if (! target_big_endian)
7167 offset_expr.X_add_number += off;
7168 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7169 (int) BFD_RELOC_LO16, breg);
7170 if (! target_big_endian)
7171 offset_expr.X_add_number -= off;
7172 else
7173 offset_expr.X_add_number += off;
7174 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7175 (int) BFD_RELOC_LO16, breg);
7176 return;
7177
7178 case M_USD_A:
7179 s = "sdl";
7180 s2 = "sdr";
7181 off = 7;
7182 goto uswa;
7183 case M_USW_A:
7184 s = "swl";
7185 s2 = "swr";
7186 off = 3;
7187 uswa:
7188 used_at = 1;
7189 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7190 if (breg != 0)
7191 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7192 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7193 "d,v,t", AT, AT, breg);
7194 if (! target_big_endian)
7195 expr1.X_add_number = off;
7196 else
7197 expr1.X_add_number = 0;
7198 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7199 (int) BFD_RELOC_LO16, AT);
7200 if (! target_big_endian)
7201 expr1.X_add_number = 0;
7202 else
7203 expr1.X_add_number = off;
7204 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7205 (int) BFD_RELOC_LO16, AT);
7206 break;
7207
7208 case M_USH_A:
7209 used_at = 1;
7210 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7211 if (breg != 0)
7212 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7213 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7214 "d,v,t", AT, AT, breg);
7215 if (! target_big_endian)
7216 expr1.X_add_number = 0;
7217 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7218 (int) BFD_RELOC_LO16, AT);
7219 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7220 treg, treg, 8);
7221 if (! target_big_endian)
7222 expr1.X_add_number = 1;
7223 else
7224 expr1.X_add_number = 0;
7225 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7226 (int) BFD_RELOC_LO16, AT);
7227 if (! target_big_endian)
7228 expr1.X_add_number = 0;
7229 else
7230 expr1.X_add_number = 1;
7231 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7232 (int) BFD_RELOC_LO16, AT);
7233 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7234 treg, treg, 8);
7235 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7236 treg, treg, AT);
7237 break;
7238
7239 default:
7240 /* FIXME: Check if this is one of the itbl macros, since they
7241 are added dynamically. */
7242 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7243 break;
7244 }
7245 if (mips_opts.noat)
7246 as_warn (_("Macro used $at after \".set noat\""));
7247 }
7248
7249 /* Implement macros in mips16 mode. */
7250
7251 static void
7252 mips16_macro (ip)
7253 struct mips_cl_insn *ip;
7254 {
7255 int mask;
7256 int xreg, yreg, zreg, tmp;
7257 int icnt;
7258 expressionS expr1;
7259 int dbl;
7260 const char *s, *s2, *s3;
7261
7262 mask = ip->insn_mo->mask;
7263
7264 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7265 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7266 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7267
7268 icnt = 0;
7269
7270 expr1.X_op = O_constant;
7271 expr1.X_op_symbol = NULL;
7272 expr1.X_add_symbol = NULL;
7273 expr1.X_add_number = 1;
7274
7275 dbl = 0;
7276
7277 switch (mask)
7278 {
7279 default:
7280 internalError ();
7281
7282 case M_DDIV_3:
7283 dbl = 1;
7284 case M_DIV_3:
7285 s = "mflo";
7286 goto do_div3;
7287 case M_DREM_3:
7288 dbl = 1;
7289 case M_REM_3:
7290 s = "mfhi";
7291 do_div3:
7292 mips_emit_delays (true);
7293 ++mips_opts.noreorder;
7294 mips_any_noreorder = 1;
7295 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7296 dbl ? "ddiv" : "div",
7297 "0,x,y", xreg, yreg);
7298 expr1.X_add_number = 2;
7299 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7300 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7301 7);
7302
7303 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7304 since that causes an overflow. We should do that as well,
7305 but I don't see how to do the comparisons without a temporary
7306 register. */
7307 --mips_opts.noreorder;
7308 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7309 break;
7310
7311 case M_DIVU_3:
7312 s = "divu";
7313 s2 = "mflo";
7314 goto do_divu3;
7315 case M_REMU_3:
7316 s = "divu";
7317 s2 = "mfhi";
7318 goto do_divu3;
7319 case M_DDIVU_3:
7320 s = "ddivu";
7321 s2 = "mflo";
7322 goto do_divu3;
7323 case M_DREMU_3:
7324 s = "ddivu";
7325 s2 = "mfhi";
7326 do_divu3:
7327 mips_emit_delays (true);
7328 ++mips_opts.noreorder;
7329 mips_any_noreorder = 1;
7330 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7331 xreg, yreg);
7332 expr1.X_add_number = 2;
7333 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7334 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7335 "6", 7);
7336 --mips_opts.noreorder;
7337 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7338 break;
7339
7340 case M_DMUL:
7341 dbl = 1;
7342 case M_MUL:
7343 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7344 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7345 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7346 zreg);
7347 return;
7348
7349 case M_DSUBU_I:
7350 dbl = 1;
7351 goto do_subu;
7352 case M_SUBU_I:
7353 do_subu:
7354 if (imm_expr.X_op != O_constant)
7355 as_bad (_("Unsupported large constant"));
7356 imm_expr.X_add_number = -imm_expr.X_add_number;
7357 macro_build ((char *) NULL, &icnt, &imm_expr,
7358 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7359 break;
7360
7361 case M_SUBU_I_2:
7362 if (imm_expr.X_op != O_constant)
7363 as_bad (_("Unsupported large constant"));
7364 imm_expr.X_add_number = -imm_expr.X_add_number;
7365 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7366 "x,k", xreg);
7367 break;
7368
7369 case M_DSUBU_I_2:
7370 if (imm_expr.X_op != O_constant)
7371 as_bad (_("Unsupported large constant"));
7372 imm_expr.X_add_number = -imm_expr.X_add_number;
7373 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7374 "y,j", yreg);
7375 break;
7376
7377 case M_BEQ:
7378 s = "cmp";
7379 s2 = "bteqz";
7380 goto do_branch;
7381 case M_BNE:
7382 s = "cmp";
7383 s2 = "btnez";
7384 goto do_branch;
7385 case M_BLT:
7386 s = "slt";
7387 s2 = "btnez";
7388 goto do_branch;
7389 case M_BLTU:
7390 s = "sltu";
7391 s2 = "btnez";
7392 goto do_branch;
7393 case M_BLE:
7394 s = "slt";
7395 s2 = "bteqz";
7396 goto do_reverse_branch;
7397 case M_BLEU:
7398 s = "sltu";
7399 s2 = "bteqz";
7400 goto do_reverse_branch;
7401 case M_BGE:
7402 s = "slt";
7403 s2 = "bteqz";
7404 goto do_branch;
7405 case M_BGEU:
7406 s = "sltu";
7407 s2 = "bteqz";
7408 goto do_branch;
7409 case M_BGT:
7410 s = "slt";
7411 s2 = "btnez";
7412 goto do_reverse_branch;
7413 case M_BGTU:
7414 s = "sltu";
7415 s2 = "btnez";
7416
7417 do_reverse_branch:
7418 tmp = xreg;
7419 xreg = yreg;
7420 yreg = tmp;
7421
7422 do_branch:
7423 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7424 xreg, yreg);
7425 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7426 break;
7427
7428 case M_BEQ_I:
7429 s = "cmpi";
7430 s2 = "bteqz";
7431 s3 = "x,U";
7432 goto do_branch_i;
7433 case M_BNE_I:
7434 s = "cmpi";
7435 s2 = "btnez";
7436 s3 = "x,U";
7437 goto do_branch_i;
7438 case M_BLT_I:
7439 s = "slti";
7440 s2 = "btnez";
7441 s3 = "x,8";
7442 goto do_branch_i;
7443 case M_BLTU_I:
7444 s = "sltiu";
7445 s2 = "btnez";
7446 s3 = "x,8";
7447 goto do_branch_i;
7448 case M_BLE_I:
7449 s = "slti";
7450 s2 = "btnez";
7451 s3 = "x,8";
7452 goto do_addone_branch_i;
7453 case M_BLEU_I:
7454 s = "sltiu";
7455 s2 = "btnez";
7456 s3 = "x,8";
7457 goto do_addone_branch_i;
7458 case M_BGE_I:
7459 s = "slti";
7460 s2 = "bteqz";
7461 s3 = "x,8";
7462 goto do_branch_i;
7463 case M_BGEU_I:
7464 s = "sltiu";
7465 s2 = "bteqz";
7466 s3 = "x,8";
7467 goto do_branch_i;
7468 case M_BGT_I:
7469 s = "slti";
7470 s2 = "bteqz";
7471 s3 = "x,8";
7472 goto do_addone_branch_i;
7473 case M_BGTU_I:
7474 s = "sltiu";
7475 s2 = "bteqz";
7476 s3 = "x,8";
7477
7478 do_addone_branch_i:
7479 if (imm_expr.X_op != O_constant)
7480 as_bad (_("Unsupported large constant"));
7481 ++imm_expr.X_add_number;
7482
7483 do_branch_i:
7484 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7485 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7486 break;
7487
7488 case M_ABS:
7489 expr1.X_add_number = 0;
7490 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7491 if (xreg != yreg)
7492 move_register (&icnt, xreg, yreg);
7493 expr1.X_add_number = 2;
7494 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7495 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7496 "neg", "x,w", xreg, xreg);
7497 }
7498 }
7499
7500 /* For consistency checking, verify that all bits are specified either
7501 by the match/mask part of the instruction definition, or by the
7502 operand list. */
7503 static int
7504 validate_mips_insn (opc)
7505 const struct mips_opcode *opc;
7506 {
7507 const char *p = opc->args;
7508 char c;
7509 unsigned long used_bits = opc->mask;
7510
7511 if ((used_bits & opc->match) != opc->match)
7512 {
7513 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7514 opc->name, opc->args);
7515 return 0;
7516 }
7517 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7518 while (*p)
7519 switch (c = *p++)
7520 {
7521 case ',': break;
7522 case '(': break;
7523 case ')': break;
7524 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7525 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7526 case 'A': break;
7527 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7528 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7529 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7530 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7531 case 'F': break;
7532 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7533 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7534 case 'I': break;
7535 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7536 case 'L': break;
7537 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7538 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7539 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7540 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7541 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7542 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7543 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7544 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7545 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7546 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7547 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7548 case 'f': break;
7549 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7550 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7551 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7552 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7553 case 'l': break;
7554 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7555 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7556 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7557 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7558 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7559 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7560 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7561 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7562 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7563 case 'x': break;
7564 case 'z': break;
7565 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7566 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7567 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7568 default:
7569 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7570 c, opc->name, opc->args);
7571 return 0;
7572 }
7573 #undef USE_BITS
7574 if (used_bits != 0xffffffff)
7575 {
7576 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7577 ~used_bits & 0xffffffff, opc->name, opc->args);
7578 return 0;
7579 }
7580 return 1;
7581 }
7582
7583 /* This routine assembles an instruction into its binary format. As a
7584 side effect, it sets one of the global variables imm_reloc or
7585 offset_reloc to the type of relocation to do if one of the operands
7586 is an address expression. */
7587
7588 static void
7589 mips_ip (str, ip)
7590 char *str;
7591 struct mips_cl_insn *ip;
7592 {
7593 char *s;
7594 const char *args;
7595 char c = 0;
7596 struct mips_opcode *insn;
7597 char *argsStart;
7598 unsigned int regno;
7599 unsigned int lastregno = 0;
7600 char *s_reset;
7601 char save_c = 0;
7602 int full_opcode_match = 1;
7603
7604 insn_error = NULL;
7605
7606 /* If the instruction contains a '.', we first try to match an instruction
7607 including the '.'. Then we try again without the '.'. */
7608 insn = NULL;
7609 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7610 continue;
7611
7612 /* If we stopped on whitespace, then replace the whitespace with null for
7613 the call to hash_find. Save the character we replaced just in case we
7614 have to re-parse the instruction. */
7615 if (ISSPACE (*s))
7616 {
7617 save_c = *s;
7618 *s++ = '\0';
7619 }
7620
7621 insn = (struct mips_opcode *) hash_find (op_hash, str);
7622
7623 /* If we didn't find the instruction in the opcode table, try again, but
7624 this time with just the instruction up to, but not including the
7625 first '.'. */
7626 if (insn == NULL)
7627 {
7628 /* Restore the character we overwrite above (if any). */
7629 if (save_c)
7630 *(--s) = save_c;
7631
7632 /* Scan up to the first '.' or whitespace. */
7633 for (s = str;
7634 *s != '\0' && *s != '.' && !ISSPACE (*s);
7635 ++s)
7636 continue;
7637
7638 /* If we did not find a '.', then we can quit now. */
7639 if (*s != '.')
7640 {
7641 insn_error = "unrecognized opcode";
7642 return;
7643 }
7644
7645 /* Lookup the instruction in the hash table. */
7646 *s++ = '\0';
7647 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7648 {
7649 insn_error = "unrecognized opcode";
7650 return;
7651 }
7652
7653 full_opcode_match = 0;
7654 }
7655
7656 argsStart = s;
7657 for (;;)
7658 {
7659 boolean ok;
7660
7661 assert (strcmp (insn->name, str) == 0);
7662
7663 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7664 ok = true;
7665 else
7666 ok = false;
7667
7668 if (insn->pinfo != INSN_MACRO)
7669 {
7670 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7671 ok = false;
7672 }
7673
7674 if (! ok)
7675 {
7676 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7677 && strcmp (insn->name, insn[1].name) == 0)
7678 {
7679 ++insn;
7680 continue;
7681 }
7682 else
7683 {
7684 if (!insn_error)
7685 {
7686 static char buf[100];
7687 sprintf (buf,
7688 _("opcode not supported on this processor: %s (%s)"),
7689 mips_cpu_to_str (mips_arch),
7690 mips_isa_to_str (mips_opts.isa));
7691
7692 insn_error = buf;
7693 }
7694 if (save_c)
7695 *(--s) = save_c;
7696 return;
7697 }
7698 }
7699
7700 ip->insn_mo = insn;
7701 ip->insn_opcode = insn->match;
7702 insn_error = NULL;
7703 for (args = insn->args;; ++args)
7704 {
7705 s += strspn (s, " \t");
7706 switch (*args)
7707 {
7708 case '\0': /* end of args */
7709 if (*s == '\0')
7710 return;
7711 break;
7712
7713 case ',':
7714 if (*s++ == *args)
7715 continue;
7716 s--;
7717 switch (*++args)
7718 {
7719 case 'r':
7720 case 'v':
7721 ip->insn_opcode |= lastregno << OP_SH_RS;
7722 continue;
7723
7724 case 'w':
7725 ip->insn_opcode |= lastregno << OP_SH_RT;
7726 continue;
7727
7728 case 'W':
7729 ip->insn_opcode |= lastregno << OP_SH_FT;
7730 continue;
7731
7732 case 'V':
7733 ip->insn_opcode |= lastregno << OP_SH_FS;
7734 continue;
7735 }
7736 break;
7737
7738 case '(':
7739 /* Handle optional base register.
7740 Either the base register is omitted or
7741 we must have a left paren. */
7742 /* This is dependent on the next operand specifier
7743 is a base register specification. */
7744 assert (args[1] == 'b' || args[1] == '5'
7745 || args[1] == '-' || args[1] == '4');
7746 if (*s == '\0')
7747 return;
7748
7749 case ')': /* these must match exactly */
7750 if (*s++ == *args)
7751 continue;
7752 break;
7753
7754 case '<': /* must be at least one digit */
7755 /*
7756 * According to the manual, if the shift amount is greater
7757 * than 31 or less than 0, then the shift amount should be
7758 * mod 32. In reality the mips assembler issues an error.
7759 * We issue a warning and mask out all but the low 5 bits.
7760 */
7761 my_getExpression (&imm_expr, s);
7762 check_absolute_expr (ip, &imm_expr);
7763 if ((unsigned long) imm_expr.X_add_number > 31)
7764 {
7765 as_warn (_("Improper shift amount (%ld)"),
7766 (long) imm_expr.X_add_number);
7767 imm_expr.X_add_number &= OP_MASK_SHAMT;
7768 }
7769 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7770 imm_expr.X_op = O_absent;
7771 s = expr_end;
7772 continue;
7773
7774 case '>': /* shift amount minus 32 */
7775 my_getExpression (&imm_expr, s);
7776 check_absolute_expr (ip, &imm_expr);
7777 if ((unsigned long) imm_expr.X_add_number < 32
7778 || (unsigned long) imm_expr.X_add_number > 63)
7779 break;
7780 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7781 imm_expr.X_op = O_absent;
7782 s = expr_end;
7783 continue;
7784
7785 case 'k': /* cache code */
7786 case 'h': /* prefx code */
7787 my_getExpression (&imm_expr, s);
7788 check_absolute_expr (ip, &imm_expr);
7789 if ((unsigned long) imm_expr.X_add_number > 31)
7790 {
7791 as_warn (_("Invalid value for `%s' (%lu)"),
7792 ip->insn_mo->name,
7793 (unsigned long) imm_expr.X_add_number);
7794 imm_expr.X_add_number &= 0x1f;
7795 }
7796 if (*args == 'k')
7797 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7798 else
7799 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7800 imm_expr.X_op = O_absent;
7801 s = expr_end;
7802 continue;
7803
7804 case 'c': /* break code */
7805 my_getExpression (&imm_expr, s);
7806 check_absolute_expr (ip, &imm_expr);
7807 if ((unsigned) imm_expr.X_add_number > 1023)
7808 {
7809 as_warn (_("Illegal break code (%ld)"),
7810 (long) imm_expr.X_add_number);
7811 imm_expr.X_add_number &= OP_MASK_CODE;
7812 }
7813 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7814 imm_expr.X_op = O_absent;
7815 s = expr_end;
7816 continue;
7817
7818 case 'q': /* lower break code */
7819 my_getExpression (&imm_expr, s);
7820 check_absolute_expr (ip, &imm_expr);
7821 if ((unsigned) imm_expr.X_add_number > 1023)
7822 {
7823 as_warn (_("Illegal lower break code (%ld)"),
7824 (long) imm_expr.X_add_number);
7825 imm_expr.X_add_number &= OP_MASK_CODE2;
7826 }
7827 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7828 imm_expr.X_op = O_absent;
7829 s = expr_end;
7830 continue;
7831
7832 case 'B': /* 20-bit syscall/break code. */
7833 my_getExpression (&imm_expr, s);
7834 check_absolute_expr (ip, &imm_expr);
7835 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7836 as_warn (_("Illegal 20-bit code (%ld)"),
7837 (long) imm_expr.X_add_number);
7838 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7839 imm_expr.X_op = O_absent;
7840 s = expr_end;
7841 continue;
7842
7843 case 'C': /* Coprocessor code */
7844 my_getExpression (&imm_expr, s);
7845 check_absolute_expr (ip, &imm_expr);
7846 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7847 {
7848 as_warn (_("Coproccesor code > 25 bits (%ld)"),
7849 (long) imm_expr.X_add_number);
7850 imm_expr.X_add_number &= ((1<<25) - 1);
7851 }
7852 ip->insn_opcode |= imm_expr.X_add_number;
7853 imm_expr.X_op = O_absent;
7854 s = expr_end;
7855 continue;
7856
7857 case 'J': /* 19-bit wait code. */
7858 my_getExpression (&imm_expr, s);
7859 check_absolute_expr (ip, &imm_expr);
7860 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7861 as_warn (_("Illegal 19-bit code (%ld)"),
7862 (long) imm_expr.X_add_number);
7863 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7864 imm_expr.X_op = O_absent;
7865 s = expr_end;
7866 continue;
7867
7868 case 'P': /* Performance register */
7869 my_getExpression (&imm_expr, s);
7870 check_absolute_expr (ip, &imm_expr);
7871 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7872 {
7873 as_warn (_("Invalid performance register (%ld)"),
7874 (long) imm_expr.X_add_number);
7875 imm_expr.X_add_number &= OP_MASK_PERFREG;
7876 }
7877 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7878 imm_expr.X_op = O_absent;
7879 s = expr_end;
7880 continue;
7881
7882 case 'b': /* base register */
7883 case 'd': /* destination register */
7884 case 's': /* source register */
7885 case 't': /* target register */
7886 case 'r': /* both target and source */
7887 case 'v': /* both dest and source */
7888 case 'w': /* both dest and target */
7889 case 'E': /* coprocessor target register */
7890 case 'G': /* coprocessor destination register */
7891 case 'x': /* ignore register name */
7892 case 'z': /* must be zero register */
7893 case 'U': /* destination register (clo/clz). */
7894 s_reset = s;
7895 if (s[0] == '$')
7896 {
7897
7898 if (ISDIGIT (s[1]))
7899 {
7900 ++s;
7901 regno = 0;
7902 do
7903 {
7904 regno *= 10;
7905 regno += *s - '0';
7906 ++s;
7907 }
7908 while (ISDIGIT (*s));
7909 if (regno > 31)
7910 as_bad (_("Invalid register number (%d)"), regno);
7911 }
7912 else if (*args == 'E' || *args == 'G')
7913 goto notreg;
7914 else
7915 {
7916 if (s[1] == 'f' && s[2] == 'p')
7917 {
7918 s += 3;
7919 regno = FP;
7920 }
7921 else if (s[1] == 's' && s[2] == 'p')
7922 {
7923 s += 3;
7924 regno = SP;
7925 }
7926 else if (s[1] == 'g' && s[2] == 'p')
7927 {
7928 s += 3;
7929 regno = GP;
7930 }
7931 else if (s[1] == 'a' && s[2] == 't')
7932 {
7933 s += 3;
7934 regno = AT;
7935 }
7936 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7937 {
7938 s += 4;
7939 regno = KT0;
7940 }
7941 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7942 {
7943 s += 4;
7944 regno = KT1;
7945 }
7946 else if (itbl_have_entries)
7947 {
7948 char *p, *n;
7949 unsigned long r;
7950
7951 p = s + 1; /* advance past '$' */
7952 n = itbl_get_field (&p); /* n is name */
7953
7954 /* See if this is a register defined in an
7955 itbl entry. */
7956 if (itbl_get_reg_val (n, &r))
7957 {
7958 /* Get_field advances to the start of
7959 the next field, so we need to back
7960 rack to the end of the last field. */
7961 if (p)
7962 s = p - 1;
7963 else
7964 s = strchr (s, '\0');
7965 regno = r;
7966 }
7967 else
7968 goto notreg;
7969 }
7970 else
7971 goto notreg;
7972 }
7973 if (regno == AT
7974 && ! mips_opts.noat
7975 && *args != 'E'
7976 && *args != 'G')
7977 as_warn (_("Used $at without \".set noat\""));
7978 c = *args;
7979 if (*s == ' ')
7980 s++;
7981 if (args[1] != *s)
7982 {
7983 if (c == 'r' || c == 'v' || c == 'w')
7984 {
7985 regno = lastregno;
7986 s = s_reset;
7987 args++;
7988 }
7989 }
7990 /* 'z' only matches $0. */
7991 if (c == 'z' && regno != 0)
7992 break;
7993
7994 /* Now that we have assembled one operand, we use the args string
7995 * to figure out where it goes in the instruction. */
7996 switch (c)
7997 {
7998 case 'r':
7999 case 's':
8000 case 'v':
8001 case 'b':
8002 ip->insn_opcode |= regno << OP_SH_RS;
8003 break;
8004 case 'd':
8005 case 'G':
8006 ip->insn_opcode |= regno << OP_SH_RD;
8007 break;
8008 case 'U':
8009 ip->insn_opcode |= regno << OP_SH_RD;
8010 ip->insn_opcode |= regno << OP_SH_RT;
8011 break;
8012 case 'w':
8013 case 't':
8014 case 'E':
8015 ip->insn_opcode |= regno << OP_SH_RT;
8016 break;
8017 case 'x':
8018 /* This case exists because on the r3000 trunc
8019 expands into a macro which requires a gp
8020 register. On the r6000 or r4000 it is
8021 assembled into a single instruction which
8022 ignores the register. Thus the insn version
8023 is MIPS_ISA2 and uses 'x', and the macro
8024 version is MIPS_ISA1 and uses 't'. */
8025 break;
8026 case 'z':
8027 /* This case is for the div instruction, which
8028 acts differently if the destination argument
8029 is $0. This only matches $0, and is checked
8030 outside the switch. */
8031 break;
8032 case 'D':
8033 /* Itbl operand; not yet implemented. FIXME ?? */
8034 break;
8035 /* What about all other operands like 'i', which
8036 can be specified in the opcode table? */
8037 }
8038 lastregno = regno;
8039 continue;
8040 }
8041 notreg:
8042 switch (*args++)
8043 {
8044 case 'r':
8045 case 'v':
8046 ip->insn_opcode |= lastregno << OP_SH_RS;
8047 continue;
8048 case 'w':
8049 ip->insn_opcode |= lastregno << OP_SH_RT;
8050 continue;
8051 }
8052 break;
8053
8054 case 'D': /* floating point destination register */
8055 case 'S': /* floating point source register */
8056 case 'T': /* floating point target register */
8057 case 'R': /* floating point source register */
8058 case 'V':
8059 case 'W':
8060 s_reset = s;
8061 if (s[0] == '$' && s[1] == 'f'
8062 && ISDIGIT (s[2]))
8063 {
8064 s += 2;
8065 regno = 0;
8066 do
8067 {
8068 regno *= 10;
8069 regno += *s - '0';
8070 ++s;
8071 }
8072 while (ISDIGIT (*s));
8073
8074 if (regno > 31)
8075 as_bad (_("Invalid float register number (%d)"), regno);
8076
8077 if ((regno & 1) != 0
8078 && HAVE_32BIT_FPRS
8079 && ! (strcmp (str, "mtc1") == 0
8080 || strcmp (str, "mfc1") == 0
8081 || strcmp (str, "lwc1") == 0
8082 || strcmp (str, "swc1") == 0
8083 || strcmp (str, "l.s") == 0
8084 || strcmp (str, "s.s") == 0))
8085 as_warn (_("Float register should be even, was %d"),
8086 regno);
8087
8088 c = *args;
8089 if (*s == ' ')
8090 s++;
8091 if (args[1] != *s)
8092 {
8093 if (c == 'V' || c == 'W')
8094 {
8095 regno = lastregno;
8096 s = s_reset;
8097 args++;
8098 }
8099 }
8100 switch (c)
8101 {
8102 case 'D':
8103 ip->insn_opcode |= regno << OP_SH_FD;
8104 break;
8105 case 'V':
8106 case 'S':
8107 ip->insn_opcode |= regno << OP_SH_FS;
8108 break;
8109 case 'W':
8110 case 'T':
8111 ip->insn_opcode |= regno << OP_SH_FT;
8112 break;
8113 case 'R':
8114 ip->insn_opcode |= regno << OP_SH_FR;
8115 break;
8116 }
8117 lastregno = regno;
8118 continue;
8119 }
8120
8121 switch (*args++)
8122 {
8123 case 'V':
8124 ip->insn_opcode |= lastregno << OP_SH_FS;
8125 continue;
8126 case 'W':
8127 ip->insn_opcode |= lastregno << OP_SH_FT;
8128 continue;
8129 }
8130 break;
8131
8132 case 'I':
8133 my_getExpression (&imm_expr, s);
8134 if (imm_expr.X_op != O_big
8135 && imm_expr.X_op != O_constant)
8136 insn_error = _("absolute expression required");
8137 s = expr_end;
8138 continue;
8139
8140 case 'A':
8141 my_getExpression (&offset_expr, s);
8142 *imm_reloc = BFD_RELOC_32;
8143 s = expr_end;
8144 continue;
8145
8146 case 'F':
8147 case 'L':
8148 case 'f':
8149 case 'l':
8150 {
8151 int f64;
8152 int using_gprs;
8153 char *save_in;
8154 char *err;
8155 unsigned char temp[8];
8156 int len;
8157 unsigned int length;
8158 segT seg;
8159 subsegT subseg;
8160 char *p;
8161
8162 /* These only appear as the last operand in an
8163 instruction, and every instruction that accepts
8164 them in any variant accepts them in all variants.
8165 This means we don't have to worry about backing out
8166 any changes if the instruction does not match.
8167
8168 The difference between them is the size of the
8169 floating point constant and where it goes. For 'F'
8170 and 'L' the constant is 64 bits; for 'f' and 'l' it
8171 is 32 bits. Where the constant is placed is based
8172 on how the MIPS assembler does things:
8173 F -- .rdata
8174 L -- .lit8
8175 f -- immediate value
8176 l -- .lit4
8177
8178 The .lit4 and .lit8 sections are only used if
8179 permitted by the -G argument.
8180
8181 When generating embedded PIC code, we use the
8182 .lit8 section but not the .lit4 section (we can do
8183 .lit4 inline easily; we need to put .lit8
8184 somewhere in the data segment, and using .lit8
8185 permits the linker to eventually combine identical
8186 .lit8 entries).
8187
8188 The code below needs to know whether the target register
8189 is 32 or 64 bits wide. It relies on the fact 'f' and
8190 'F' are used with GPR-based instructions and 'l' and
8191 'L' are used with FPR-based instructions. */
8192
8193 f64 = *args == 'F' || *args == 'L';
8194 using_gprs = *args == 'F' || *args == 'f';
8195
8196 save_in = input_line_pointer;
8197 input_line_pointer = s;
8198 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8199 length = len;
8200 s = input_line_pointer;
8201 input_line_pointer = save_in;
8202 if (err != NULL && *err != '\0')
8203 {
8204 as_bad (_("Bad floating point constant: %s"), err);
8205 memset (temp, '\0', sizeof temp);
8206 length = f64 ? 8 : 4;
8207 }
8208
8209 assert (length == (unsigned) (f64 ? 8 : 4));
8210
8211 if (*args == 'f'
8212 || (*args == 'l'
8213 && (! USE_GLOBAL_POINTER_OPT
8214 || mips_pic == EMBEDDED_PIC
8215 || g_switch_value < 4
8216 || (temp[0] == 0 && temp[1] == 0)
8217 || (temp[2] == 0 && temp[3] == 0))))
8218 {
8219 imm_expr.X_op = O_constant;
8220 if (! target_big_endian)
8221 imm_expr.X_add_number = bfd_getl32 (temp);
8222 else
8223 imm_expr.X_add_number = bfd_getb32 (temp);
8224 }
8225 else if (length > 4
8226 && ! mips_disable_float_construction
8227 /* Constants can only be constructed in GPRs and
8228 copied to FPRs if the GPRs are at least as wide
8229 as the FPRs. Force the constant into memory if
8230 we are using 64-bit FPRs but the GPRs are only
8231 32 bits wide. */
8232 && (using_gprs
8233 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8234 && ((temp[0] == 0 && temp[1] == 0)
8235 || (temp[2] == 0 && temp[3] == 0))
8236 && ((temp[4] == 0 && temp[5] == 0)
8237 || (temp[6] == 0 && temp[7] == 0)))
8238 {
8239 /* The value is simple enough to load with a couple of
8240 instructions. If using 32-bit registers, set
8241 imm_expr to the high order 32 bits and offset_expr to
8242 the low order 32 bits. Otherwise, set imm_expr to
8243 the entire 64 bit constant. */
8244 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8245 {
8246 imm_expr.X_op = O_constant;
8247 offset_expr.X_op = O_constant;
8248 if (! target_big_endian)
8249 {
8250 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8251 offset_expr.X_add_number = bfd_getl32 (temp);
8252 }
8253 else
8254 {
8255 imm_expr.X_add_number = bfd_getb32 (temp);
8256 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8257 }
8258 if (offset_expr.X_add_number == 0)
8259 offset_expr.X_op = O_absent;
8260 }
8261 else if (sizeof (imm_expr.X_add_number) > 4)
8262 {
8263 imm_expr.X_op = O_constant;
8264 if (! target_big_endian)
8265 imm_expr.X_add_number = bfd_getl64 (temp);
8266 else
8267 imm_expr.X_add_number = bfd_getb64 (temp);
8268 }
8269 else
8270 {
8271 imm_expr.X_op = O_big;
8272 imm_expr.X_add_number = 4;
8273 if (! target_big_endian)
8274 {
8275 generic_bignum[0] = bfd_getl16 (temp);
8276 generic_bignum[1] = bfd_getl16 (temp + 2);
8277 generic_bignum[2] = bfd_getl16 (temp + 4);
8278 generic_bignum[3] = bfd_getl16 (temp + 6);
8279 }
8280 else
8281 {
8282 generic_bignum[0] = bfd_getb16 (temp + 6);
8283 generic_bignum[1] = bfd_getb16 (temp + 4);
8284 generic_bignum[2] = bfd_getb16 (temp + 2);
8285 generic_bignum[3] = bfd_getb16 (temp);
8286 }
8287 }
8288 }
8289 else
8290 {
8291 const char *newname;
8292 segT new_seg;
8293
8294 /* Switch to the right section. */
8295 seg = now_seg;
8296 subseg = now_subseg;
8297 switch (*args)
8298 {
8299 default: /* unused default case avoids warnings. */
8300 case 'L':
8301 newname = RDATA_SECTION_NAME;
8302 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8303 || mips_pic == EMBEDDED_PIC)
8304 newname = ".lit8";
8305 break;
8306 case 'F':
8307 if (mips_pic == EMBEDDED_PIC)
8308 newname = ".lit8";
8309 else
8310 newname = RDATA_SECTION_NAME;
8311 break;
8312 case 'l':
8313 assert (!USE_GLOBAL_POINTER_OPT
8314 || g_switch_value >= 4);
8315 newname = ".lit4";
8316 break;
8317 }
8318 new_seg = subseg_new (newname, (subsegT) 0);
8319 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8320 bfd_set_section_flags (stdoutput, new_seg,
8321 (SEC_ALLOC
8322 | SEC_LOAD
8323 | SEC_READONLY
8324 | SEC_DATA));
8325 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8326 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8327 && strcmp (TARGET_OS, "elf") != 0)
8328 record_alignment (new_seg, 4);
8329 else
8330 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8331 if (seg == now_seg)
8332 as_bad (_("Can't use floating point insn in this section"));
8333
8334 /* Set the argument to the current address in the
8335 section. */
8336 offset_expr.X_op = O_symbol;
8337 offset_expr.X_add_symbol =
8338 symbol_new ("L0\001", now_seg,
8339 (valueT) frag_now_fix (), frag_now);
8340 offset_expr.X_add_number = 0;
8341
8342 /* Put the floating point number into the section. */
8343 p = frag_more ((int) length);
8344 memcpy (p, temp, length);
8345
8346 /* Switch back to the original section. */
8347 subseg_set (seg, subseg);
8348 }
8349 }
8350 continue;
8351
8352 case 'i': /* 16 bit unsigned immediate */
8353 case 'j': /* 16 bit signed immediate */
8354 *imm_reloc = BFD_RELOC_LO16;
8355 c = my_getSmallExpression (&imm_expr, s);
8356 if (c != S_EX_NONE)
8357 {
8358 if (c != S_EX_LO)
8359 {
8360 if (imm_expr.X_op == O_constant)
8361 imm_expr.X_add_number =
8362 (imm_expr.X_add_number >> 16) & 0xffff;
8363 #ifdef OBJ_ELF
8364 else if (c == S_EX_HIGHEST)
8365 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8366 else if (c == S_EX_HIGHER)
8367 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8368 else if (c == S_EX_GP_REL)
8369 {
8370 /* This occurs in NewABI only. */
8371 c = my_getSmallExpression (&imm_expr, s);
8372 if (c != S_EX_NEG)
8373 as_bad (_("bad composition of relocations"));
8374 else
8375 {
8376 c = my_getSmallExpression (&imm_expr, s);
8377 if (c != S_EX_LO)
8378 as_bad (_("bad composition of relocations"));
8379 else
8380 {
8381 imm_reloc[0] = BFD_RELOC_GPREL16;
8382 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8383 imm_reloc[2] = BFD_RELOC_LO16;
8384 }
8385 }
8386 }
8387 #endif
8388 else if (c == S_EX_HI)
8389 {
8390 *imm_reloc = BFD_RELOC_HI16_S;
8391 imm_unmatched_hi = true;
8392 }
8393 else
8394 *imm_reloc = BFD_RELOC_HI16;
8395 }
8396 else if (imm_expr.X_op == O_constant)
8397 imm_expr.X_add_number &= 0xffff;
8398 }
8399 if (*args == 'i')
8400 {
8401 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8402 || ((imm_expr.X_add_number < 0
8403 || imm_expr.X_add_number >= 0x10000)
8404 && imm_expr.X_op == O_constant))
8405 {
8406 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8407 !strcmp (insn->name, insn[1].name))
8408 break;
8409 if (imm_expr.X_op == O_constant
8410 || imm_expr.X_op == O_big)
8411 as_bad (_("16 bit expression not in range 0..65535"));
8412 }
8413 }
8414 else
8415 {
8416 int more;
8417 offsetT max;
8418
8419 /* The upper bound should be 0x8000, but
8420 unfortunately the MIPS assembler accepts numbers
8421 from 0x8000 to 0xffff and sign extends them, and
8422 we want to be compatible. We only permit this
8423 extended range for an instruction which does not
8424 provide any further alternates, since those
8425 alternates may handle other cases. People should
8426 use the numbers they mean, rather than relying on
8427 a mysterious sign extension. */
8428 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8429 strcmp (insn->name, insn[1].name) == 0);
8430 if (more)
8431 max = 0x8000;
8432 else
8433 max = 0x10000;
8434 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8435 || ((imm_expr.X_add_number < -0x8000
8436 || imm_expr.X_add_number >= max)
8437 && imm_expr.X_op == O_constant)
8438 || (more
8439 && imm_expr.X_add_number < 0
8440 && HAVE_64BIT_GPRS
8441 && imm_expr.X_unsigned
8442 && sizeof (imm_expr.X_add_number) <= 4))
8443 {
8444 if (more)
8445 break;
8446 if (imm_expr.X_op == O_constant
8447 || imm_expr.X_op == O_big)
8448 as_bad (_("16 bit expression not in range -32768..32767"));
8449 }
8450 }
8451 s = expr_end;
8452 continue;
8453
8454 case 'o': /* 16 bit offset */
8455 c = my_getSmallExpression (&offset_expr, s);
8456
8457 /* If this value won't fit into a 16 bit offset, then go
8458 find a macro that will generate the 32 bit offset
8459 code pattern. */
8460 if (c == S_EX_NONE
8461 && (offset_expr.X_op != O_constant
8462 || offset_expr.X_add_number >= 0x8000
8463 || offset_expr.X_add_number < -0x8000))
8464 break;
8465
8466 if (c == S_EX_HI)
8467 {
8468 if (offset_expr.X_op != O_constant)
8469 break;
8470 offset_expr.X_add_number =
8471 (offset_expr.X_add_number >> 16) & 0xffff;
8472 }
8473 *offset_reloc = BFD_RELOC_LO16;
8474 s = expr_end;
8475 continue;
8476
8477 case 'p': /* pc relative offset */
8478 if (mips_pic == EMBEDDED_PIC)
8479 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8480 else
8481 *offset_reloc = BFD_RELOC_16_PCREL;
8482 my_getExpression (&offset_expr, s);
8483 s = expr_end;
8484 continue;
8485
8486 case 'u': /* upper 16 bits */
8487 c = my_getSmallExpression (&imm_expr, s);
8488 *imm_reloc = BFD_RELOC_LO16;
8489 if (c != S_EX_NONE)
8490 {
8491 if (c != S_EX_LO)
8492 {
8493 if (imm_expr.X_op == O_constant)
8494 imm_expr.X_add_number =
8495 (imm_expr.X_add_number >> 16) & 0xffff;
8496 else if (c == S_EX_HI)
8497 {
8498 *imm_reloc = BFD_RELOC_HI16_S;
8499 imm_unmatched_hi = true;
8500 }
8501 #ifdef OBJ_ELF
8502 else if (c == S_EX_HIGHEST)
8503 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8504 else if (c == S_EX_GP_REL)
8505 {
8506 /* This occurs in NewABI only. */
8507 c = my_getSmallExpression (&imm_expr, s);
8508 if (c != S_EX_NEG)
8509 as_bad (_("bad composition of relocations"));
8510 else
8511 {
8512 c = my_getSmallExpression (&imm_expr, s);
8513 if (c != S_EX_HI)
8514 as_bad (_("bad composition of relocations"));
8515 else
8516 {
8517 imm_reloc[0] = BFD_RELOC_GPREL16;
8518 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8519 imm_reloc[2] = BFD_RELOC_HI16_S;
8520 }
8521 }
8522 }
8523 #endif
8524 else
8525 *imm_reloc = BFD_RELOC_HI16;
8526 }
8527 else if (imm_expr.X_op == O_constant)
8528 imm_expr.X_add_number &= 0xffff;
8529 }
8530 if (imm_expr.X_op == O_constant
8531 && (imm_expr.X_add_number < 0
8532 || imm_expr.X_add_number >= 0x10000))
8533 as_bad (_("lui expression not in range 0..65535"));
8534 s = expr_end;
8535 continue;
8536
8537 case 'a': /* 26 bit address */
8538 my_getExpression (&offset_expr, s);
8539 s = expr_end;
8540 *offset_reloc = BFD_RELOC_MIPS_JMP;
8541 continue;
8542
8543 case 'N': /* 3 bit branch condition code */
8544 case 'M': /* 3 bit compare condition code */
8545 if (strncmp (s, "$fcc", 4) != 0)
8546 break;
8547 s += 4;
8548 regno = 0;
8549 do
8550 {
8551 regno *= 10;
8552 regno += *s - '0';
8553 ++s;
8554 }
8555 while (ISDIGIT (*s));
8556 if (regno > 7)
8557 as_bad (_("invalid condition code register $fcc%d"), regno);
8558 if (*args == 'N')
8559 ip->insn_opcode |= regno << OP_SH_BCC;
8560 else
8561 ip->insn_opcode |= regno << OP_SH_CCC;
8562 continue;
8563
8564 case 'H':
8565 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8566 s += 2;
8567 if (ISDIGIT (*s))
8568 {
8569 c = 0;
8570 do
8571 {
8572 c *= 10;
8573 c += *s - '0';
8574 ++s;
8575 }
8576 while (ISDIGIT (*s));
8577 }
8578 else
8579 c = 8; /* Invalid sel value. */
8580
8581 if (c > 7)
8582 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8583 ip->insn_opcode |= c;
8584 continue;
8585
8586 default:
8587 as_bad (_("bad char = '%c'\n"), *args);
8588 internalError ();
8589 }
8590 break;
8591 }
8592 /* Args don't match. */
8593 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8594 !strcmp (insn->name, insn[1].name))
8595 {
8596 ++insn;
8597 s = argsStart;
8598 insn_error = _("illegal operands");
8599 continue;
8600 }
8601 if (save_c)
8602 *(--s) = save_c;
8603 insn_error = _("illegal operands");
8604 return;
8605 }
8606 }
8607
8608 /* This routine assembles an instruction into its binary format when
8609 assembling for the mips16. As a side effect, it sets one of the
8610 global variables imm_reloc or offset_reloc to the type of
8611 relocation to do if one of the operands is an address expression.
8612 It also sets mips16_small and mips16_ext if the user explicitly
8613 requested a small or extended instruction. */
8614
8615 static void
8616 mips16_ip (str, ip)
8617 char *str;
8618 struct mips_cl_insn *ip;
8619 {
8620 char *s;
8621 const char *args;
8622 struct mips_opcode *insn;
8623 char *argsstart;
8624 unsigned int regno;
8625 unsigned int lastregno = 0;
8626 char *s_reset;
8627
8628 insn_error = NULL;
8629
8630 mips16_small = false;
8631 mips16_ext = false;
8632
8633 for (s = str; ISLOWER (*s); ++s)
8634 ;
8635 switch (*s)
8636 {
8637 case '\0':
8638 break;
8639
8640 case ' ':
8641 *s++ = '\0';
8642 break;
8643
8644 case '.':
8645 if (s[1] == 't' && s[2] == ' ')
8646 {
8647 *s = '\0';
8648 mips16_small = true;
8649 s += 3;
8650 break;
8651 }
8652 else if (s[1] == 'e' && s[2] == ' ')
8653 {
8654 *s = '\0';
8655 mips16_ext = true;
8656 s += 3;
8657 break;
8658 }
8659 /* Fall through. */
8660 default:
8661 insn_error = _("unknown opcode");
8662 return;
8663 }
8664
8665 if (mips_opts.noautoextend && ! mips16_ext)
8666 mips16_small = true;
8667
8668 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8669 {
8670 insn_error = _("unrecognized opcode");
8671 return;
8672 }
8673
8674 argsstart = s;
8675 for (;;)
8676 {
8677 assert (strcmp (insn->name, str) == 0);
8678
8679 ip->insn_mo = insn;
8680 ip->insn_opcode = insn->match;
8681 ip->use_extend = false;
8682 imm_expr.X_op = O_absent;
8683 imm_reloc[0] = BFD_RELOC_UNUSED;
8684 imm_reloc[1] = BFD_RELOC_UNUSED;
8685 imm_reloc[2] = BFD_RELOC_UNUSED;
8686 offset_expr.X_op = O_absent;
8687 offset_reloc[0] = BFD_RELOC_UNUSED;
8688 offset_reloc[1] = BFD_RELOC_UNUSED;
8689 offset_reloc[2] = BFD_RELOC_UNUSED;
8690 for (args = insn->args; 1; ++args)
8691 {
8692 int c;
8693
8694 if (*s == ' ')
8695 ++s;
8696
8697 /* In this switch statement we call break if we did not find
8698 a match, continue if we did find a match, or return if we
8699 are done. */
8700
8701 c = *args;
8702 switch (c)
8703 {
8704 case '\0':
8705 if (*s == '\0')
8706 {
8707 /* Stuff the immediate value in now, if we can. */
8708 if (imm_expr.X_op == O_constant
8709 && *imm_reloc > BFD_RELOC_UNUSED
8710 && insn->pinfo != INSN_MACRO)
8711 {
8712 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8713 imm_expr.X_add_number, true, mips16_small,
8714 mips16_ext, &ip->insn_opcode,
8715 &ip->use_extend, &ip->extend);
8716 imm_expr.X_op = O_absent;
8717 *imm_reloc = BFD_RELOC_UNUSED;
8718 }
8719
8720 return;
8721 }
8722 break;
8723
8724 case ',':
8725 if (*s++ == c)
8726 continue;
8727 s--;
8728 switch (*++args)
8729 {
8730 case 'v':
8731 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8732 continue;
8733 case 'w':
8734 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8735 continue;
8736 }
8737 break;
8738
8739 case '(':
8740 case ')':
8741 if (*s++ == c)
8742 continue;
8743 break;
8744
8745 case 'v':
8746 case 'w':
8747 if (s[0] != '$')
8748 {
8749 if (c == 'v')
8750 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8751 else
8752 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8753 ++args;
8754 continue;
8755 }
8756 /* Fall through. */
8757 case 'x':
8758 case 'y':
8759 case 'z':
8760 case 'Z':
8761 case '0':
8762 case 'S':
8763 case 'R':
8764 case 'X':
8765 case 'Y':
8766 if (s[0] != '$')
8767 break;
8768 s_reset = s;
8769 if (ISDIGIT (s[1]))
8770 {
8771 ++s;
8772 regno = 0;
8773 do
8774 {
8775 regno *= 10;
8776 regno += *s - '0';
8777 ++s;
8778 }
8779 while (ISDIGIT (*s));
8780 if (regno > 31)
8781 {
8782 as_bad (_("invalid register number (%d)"), regno);
8783 regno = 2;
8784 }
8785 }
8786 else
8787 {
8788 if (s[1] == 'f' && s[2] == 'p')
8789 {
8790 s += 3;
8791 regno = FP;
8792 }
8793 else if (s[1] == 's' && s[2] == 'p')
8794 {
8795 s += 3;
8796 regno = SP;
8797 }
8798 else if (s[1] == 'g' && s[2] == 'p')
8799 {
8800 s += 3;
8801 regno = GP;
8802 }
8803 else if (s[1] == 'a' && s[2] == 't')
8804 {
8805 s += 3;
8806 regno = AT;
8807 }
8808 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8809 {
8810 s += 4;
8811 regno = KT0;
8812 }
8813 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8814 {
8815 s += 4;
8816 regno = KT1;
8817 }
8818 else
8819 break;
8820 }
8821
8822 if (*s == ' ')
8823 ++s;
8824 if (args[1] != *s)
8825 {
8826 if (c == 'v' || c == 'w')
8827 {
8828 regno = mips16_to_32_reg_map[lastregno];
8829 s = s_reset;
8830 args++;
8831 }
8832 }
8833
8834 switch (c)
8835 {
8836 case 'x':
8837 case 'y':
8838 case 'z':
8839 case 'v':
8840 case 'w':
8841 case 'Z':
8842 regno = mips32_to_16_reg_map[regno];
8843 break;
8844
8845 case '0':
8846 if (regno != 0)
8847 regno = ILLEGAL_REG;
8848 break;
8849
8850 case 'S':
8851 if (regno != SP)
8852 regno = ILLEGAL_REG;
8853 break;
8854
8855 case 'R':
8856 if (regno != RA)
8857 regno = ILLEGAL_REG;
8858 break;
8859
8860 case 'X':
8861 case 'Y':
8862 if (regno == AT && ! mips_opts.noat)
8863 as_warn (_("used $at without \".set noat\""));
8864 break;
8865
8866 default:
8867 internalError ();
8868 }
8869
8870 if (regno == ILLEGAL_REG)
8871 break;
8872
8873 switch (c)
8874 {
8875 case 'x':
8876 case 'v':
8877 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8878 break;
8879 case 'y':
8880 case 'w':
8881 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8882 break;
8883 case 'z':
8884 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8885 break;
8886 case 'Z':
8887 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8888 case '0':
8889 case 'S':
8890 case 'R':
8891 break;
8892 case 'X':
8893 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8894 break;
8895 case 'Y':
8896 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8897 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8898 break;
8899 default:
8900 internalError ();
8901 }
8902
8903 lastregno = regno;
8904 continue;
8905
8906 case 'P':
8907 if (strncmp (s, "$pc", 3) == 0)
8908 {
8909 s += 3;
8910 continue;
8911 }
8912 break;
8913
8914 case '<':
8915 case '>':
8916 case '[':
8917 case ']':
8918 case '4':
8919 case '5':
8920 case 'H':
8921 case 'W':
8922 case 'D':
8923 case 'j':
8924 case '8':
8925 case 'V':
8926 case 'C':
8927 case 'U':
8928 case 'k':
8929 case 'K':
8930 if (s[0] == '%'
8931 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8932 {
8933 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8934 and generate the appropriate reloc. If the text
8935 inside %gprel is not a symbol name with an
8936 optional offset, then we generate a normal reloc
8937 and will probably fail later. */
8938 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8939 if (imm_expr.X_op == O_symbol)
8940 {
8941 mips16_ext = true;
8942 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8943 s = expr_end;
8944 ip->use_extend = true;
8945 ip->extend = 0;
8946 continue;
8947 }
8948 }
8949 else
8950 {
8951 /* Just pick up a normal expression. */
8952 my_getExpression (&imm_expr, s);
8953 }
8954
8955 if (imm_expr.X_op == O_register)
8956 {
8957 /* What we thought was an expression turned out to
8958 be a register. */
8959
8960 if (s[0] == '(' && args[1] == '(')
8961 {
8962 /* It looks like the expression was omitted
8963 before a register indirection, which means
8964 that the expression is implicitly zero. We
8965 still set up imm_expr, so that we handle
8966 explicit extensions correctly. */
8967 imm_expr.X_op = O_constant;
8968 imm_expr.X_add_number = 0;
8969 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8970 continue;
8971 }
8972
8973 break;
8974 }
8975
8976 /* We need to relax this instruction. */
8977 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8978 s = expr_end;
8979 continue;
8980
8981 case 'p':
8982 case 'q':
8983 case 'A':
8984 case 'B':
8985 case 'E':
8986 /* We use offset_reloc rather than imm_reloc for the PC
8987 relative operands. This lets macros with both
8988 immediate and address operands work correctly. */
8989 my_getExpression (&offset_expr, s);
8990
8991 if (offset_expr.X_op == O_register)
8992 break;
8993
8994 /* We need to relax this instruction. */
8995 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8996 s = expr_end;
8997 continue;
8998
8999 case '6': /* break code */
9000 my_getExpression (&imm_expr, s);
9001 check_absolute_expr (ip, &imm_expr);
9002 if ((unsigned long) imm_expr.X_add_number > 63)
9003 {
9004 as_warn (_("Invalid value for `%s' (%lu)"),
9005 ip->insn_mo->name,
9006 (unsigned long) imm_expr.X_add_number);
9007 imm_expr.X_add_number &= 0x3f;
9008 }
9009 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9010 imm_expr.X_op = O_absent;
9011 s = expr_end;
9012 continue;
9013
9014 case 'a': /* 26 bit address */
9015 my_getExpression (&offset_expr, s);
9016 s = expr_end;
9017 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9018 ip->insn_opcode <<= 16;
9019 continue;
9020
9021 case 'l': /* register list for entry macro */
9022 case 'L': /* register list for exit macro */
9023 {
9024 int mask;
9025
9026 if (c == 'l')
9027 mask = 0;
9028 else
9029 mask = 7 << 3;
9030 while (*s != '\0')
9031 {
9032 int freg, reg1, reg2;
9033
9034 while (*s == ' ' || *s == ',')
9035 ++s;
9036 if (*s != '$')
9037 {
9038 as_bad (_("can't parse register list"));
9039 break;
9040 }
9041 ++s;
9042 if (*s != 'f')
9043 freg = 0;
9044 else
9045 {
9046 freg = 1;
9047 ++s;
9048 }
9049 reg1 = 0;
9050 while (ISDIGIT (*s))
9051 {
9052 reg1 *= 10;
9053 reg1 += *s - '0';
9054 ++s;
9055 }
9056 if (*s == ' ')
9057 ++s;
9058 if (*s != '-')
9059 reg2 = reg1;
9060 else
9061 {
9062 ++s;
9063 if (*s != '$')
9064 break;
9065 ++s;
9066 if (freg)
9067 {
9068 if (*s == 'f')
9069 ++s;
9070 else
9071 {
9072 as_bad (_("invalid register list"));
9073 break;
9074 }
9075 }
9076 reg2 = 0;
9077 while (ISDIGIT (*s))
9078 {
9079 reg2 *= 10;
9080 reg2 += *s - '0';
9081 ++s;
9082 }
9083 }
9084 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9085 {
9086 mask &= ~ (7 << 3);
9087 mask |= 5 << 3;
9088 }
9089 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9090 {
9091 mask &= ~ (7 << 3);
9092 mask |= 6 << 3;
9093 }
9094 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9095 mask |= (reg2 - 3) << 3;
9096 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9097 mask |= (reg2 - 15) << 1;
9098 else if (reg1 == 31 && reg2 == 31)
9099 mask |= 1;
9100 else
9101 {
9102 as_bad (_("invalid register list"));
9103 break;
9104 }
9105 }
9106 /* The mask is filled in in the opcode table for the
9107 benefit of the disassembler. We remove it before
9108 applying the actual mask. */
9109 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9110 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9111 }
9112 continue;
9113
9114 case 'e': /* extend code */
9115 my_getExpression (&imm_expr, s);
9116 check_absolute_expr (ip, &imm_expr);
9117 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9118 {
9119 as_warn (_("Invalid value for `%s' (%lu)"),
9120 ip->insn_mo->name,
9121 (unsigned long) imm_expr.X_add_number);
9122 imm_expr.X_add_number &= 0x7ff;
9123 }
9124 ip->insn_opcode |= imm_expr.X_add_number;
9125 imm_expr.X_op = O_absent;
9126 s = expr_end;
9127 continue;
9128
9129 default:
9130 internalError ();
9131 }
9132 break;
9133 }
9134
9135 /* Args don't match. */
9136 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9137 strcmp (insn->name, insn[1].name) == 0)
9138 {
9139 ++insn;
9140 s = argsstart;
9141 continue;
9142 }
9143
9144 insn_error = _("illegal operands");
9145
9146 return;
9147 }
9148 }
9149
9150 /* This structure holds information we know about a mips16 immediate
9151 argument type. */
9152
9153 struct mips16_immed_operand
9154 {
9155 /* The type code used in the argument string in the opcode table. */
9156 int type;
9157 /* The number of bits in the short form of the opcode. */
9158 int nbits;
9159 /* The number of bits in the extended form of the opcode. */
9160 int extbits;
9161 /* The amount by which the short form is shifted when it is used;
9162 for example, the sw instruction has a shift count of 2. */
9163 int shift;
9164 /* The amount by which the short form is shifted when it is stored
9165 into the instruction code. */
9166 int op_shift;
9167 /* Non-zero if the short form is unsigned. */
9168 int unsp;
9169 /* Non-zero if the extended form is unsigned. */
9170 int extu;
9171 /* Non-zero if the value is PC relative. */
9172 int pcrel;
9173 };
9174
9175 /* The mips16 immediate operand types. */
9176
9177 static const struct mips16_immed_operand mips16_immed_operands[] =
9178 {
9179 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9180 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9181 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9182 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9183 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9184 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9185 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9186 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9187 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9188 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9189 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9190 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9191 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9192 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9193 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9194 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9195 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9196 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9197 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9198 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9199 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9200 };
9201
9202 #define MIPS16_NUM_IMMED \
9203 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9204
9205 /* Handle a mips16 instruction with an immediate value. This or's the
9206 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9207 whether an extended value is needed; if one is needed, it sets
9208 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9209 If SMALL is true, an unextended opcode was explicitly requested.
9210 If EXT is true, an extended opcode was explicitly requested. If
9211 WARN is true, warn if EXT does not match reality. */
9212
9213 static void
9214 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9215 extend)
9216 char *file;
9217 unsigned int line;
9218 int type;
9219 offsetT val;
9220 boolean warn;
9221 boolean small;
9222 boolean ext;
9223 unsigned long *insn;
9224 boolean *use_extend;
9225 unsigned short *extend;
9226 {
9227 register const struct mips16_immed_operand *op;
9228 int mintiny, maxtiny;
9229 boolean needext;
9230
9231 op = mips16_immed_operands;
9232 while (op->type != type)
9233 {
9234 ++op;
9235 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9236 }
9237
9238 if (op->unsp)
9239 {
9240 if (type == '<' || type == '>' || type == '[' || type == ']')
9241 {
9242 mintiny = 1;
9243 maxtiny = 1 << op->nbits;
9244 }
9245 else
9246 {
9247 mintiny = 0;
9248 maxtiny = (1 << op->nbits) - 1;
9249 }
9250 }
9251 else
9252 {
9253 mintiny = - (1 << (op->nbits - 1));
9254 maxtiny = (1 << (op->nbits - 1)) - 1;
9255 }
9256
9257 /* Branch offsets have an implicit 0 in the lowest bit. */
9258 if (type == 'p' || type == 'q')
9259 val /= 2;
9260
9261 if ((val & ((1 << op->shift) - 1)) != 0
9262 || val < (mintiny << op->shift)
9263 || val > (maxtiny << op->shift))
9264 needext = true;
9265 else
9266 needext = false;
9267
9268 if (warn && ext && ! needext)
9269 as_warn_where (file, line,
9270 _("extended operand requested but not required"));
9271 if (small && needext)
9272 as_bad_where (file, line, _("invalid unextended operand value"));
9273
9274 if (small || (! ext && ! needext))
9275 {
9276 int insnval;
9277
9278 *use_extend = false;
9279 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9280 insnval <<= op->op_shift;
9281 *insn |= insnval;
9282 }
9283 else
9284 {
9285 long minext, maxext;
9286 int extval;
9287
9288 if (op->extu)
9289 {
9290 minext = 0;
9291 maxext = (1 << op->extbits) - 1;
9292 }
9293 else
9294 {
9295 minext = - (1 << (op->extbits - 1));
9296 maxext = (1 << (op->extbits - 1)) - 1;
9297 }
9298 if (val < minext || val > maxext)
9299 as_bad_where (file, line,
9300 _("operand value out of range for instruction"));
9301
9302 *use_extend = true;
9303 if (op->extbits == 16)
9304 {
9305 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9306 val &= 0x1f;
9307 }
9308 else if (op->extbits == 15)
9309 {
9310 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9311 val &= 0xf;
9312 }
9313 else
9314 {
9315 extval = ((val & 0x1f) << 6) | (val & 0x20);
9316 val = 0;
9317 }
9318
9319 *extend = (unsigned short) extval;
9320 *insn |= val;
9321 }
9322 }
9323 \f
9324 static struct percent_op_match
9325 {
9326 const char *str;
9327 const enum small_ex_type type;
9328 } percent_op[] =
9329 {
9330 {"%lo", S_EX_LO},
9331 #ifdef OBJ_ELF
9332 {"%call_hi", S_EX_CALL_HI},
9333 {"%call_lo", S_EX_CALL_LO},
9334 {"%call16", S_EX_CALL16},
9335 {"%got_disp", S_EX_GOT_DISP},
9336 {"%got_page", S_EX_GOT_PAGE},
9337 {"%got_ofst", S_EX_GOT_OFST},
9338 {"%got_hi", S_EX_GOT_HI},
9339 {"%got_lo", S_EX_GOT_LO},
9340 {"%got", S_EX_GOT},
9341 {"%gp_rel", S_EX_GP_REL},
9342 {"%half", S_EX_HALF},
9343 {"%highest", S_EX_HIGHEST},
9344 {"%higher", S_EX_HIGHER},
9345 {"%neg", S_EX_NEG},
9346 #endif
9347 {"%hi", S_EX_HI}
9348 };
9349
9350 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9351 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9352 can be nested, this is handled by blanking the innermost, parsing the
9353 rest by subsequent calls. */
9354
9355 static int
9356 my_getSmallParser (str, len, nestlevel)
9357 char **str;
9358 unsigned int *len;
9359 int *nestlevel;
9360 {
9361 *len = 0;
9362 *str += strspn (*str, " \t");
9363 /* Check for expression in parentheses. */
9364 if (**str == '(')
9365 {
9366 char *b = *str + 1 + strspn (*str + 1, " \t");
9367 char *e;
9368
9369 /* Check for base register. */
9370 if (b[0] == '$')
9371 {
9372 if (strchr (b, ')')
9373 && (e = b + strcspn (b, ") \t"))
9374 && e - b > 1 && e - b < 4)
9375 {
9376 if ((e - b == 3
9377 && ((b[1] == 'f' && b[2] == 'p')
9378 || (b[1] == 's' && b[2] == 'p')
9379 || (b[1] == 'g' && b[2] == 'p')
9380 || (b[1] == 'a' && b[2] == 't')
9381 || (ISDIGIT (b[1])
9382 && ISDIGIT (b[2]))))
9383 || (ISDIGIT (b[1])))
9384 {
9385 *len = strcspn (*str, ")") + 1;
9386 return S_EX_REGISTER;
9387 }
9388 }
9389 }
9390 /* Check for percent_op (in parentheses). */
9391 else if (b[0] == '%')
9392 {
9393 *str = b;
9394 return my_getPercentOp (str, len, nestlevel);
9395 }
9396
9397 /* Some other expression in the parentheses, which can contain
9398 parentheses itself. Attempt to find the matching one. */
9399 {
9400 int pcnt = 1;
9401 char *s;
9402
9403 *len = 1;
9404 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9405 {
9406 if (*s == '(')
9407 pcnt++;
9408 else if (*s == ')')
9409 pcnt--;
9410 }
9411 }
9412 }
9413 /* Check for percent_op (outside of parentheses). */
9414 else if (*str[0] == '%')
9415 return my_getPercentOp (str, len, nestlevel);
9416
9417 /* Any other expression. */
9418 return S_EX_NONE;
9419 }
9420
9421 static int
9422 my_getPercentOp (str, len, nestlevel)
9423 char **str;
9424 unsigned int *len;
9425 int *nestlevel;
9426 {
9427 char *tmp = *str + 1;
9428 unsigned int i = 0;
9429
9430 while (ISALPHA (*tmp) || *tmp == '_')
9431 {
9432 *tmp = TOLOWER (*tmp);
9433 tmp++;
9434 }
9435 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9436 {
9437 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9438 i++;
9439 else
9440 {
9441 int type = percent_op[i].type;
9442
9443 /* Only %hi and %lo are allowed for OldABI. */
9444 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9445 return S_EX_NONE;
9446
9447 *len = strlen (percent_op[i].str);
9448 (*nestlevel)++;
9449 return type;
9450 }
9451 }
9452 return S_EX_NONE;
9453 }
9454
9455 static int
9456 my_getSmallExpression (ep, str)
9457 expressionS *ep;
9458 char *str;
9459 {
9460 static char *oldstr = NULL;
9461 int c = S_EX_NONE;
9462 int oldc;
9463 int nestlevel = -1;
9464 unsigned int len;
9465
9466 /* Don't update oldstr if the last call had nested percent_op's. We need
9467 it to parse the outer ones later. */
9468 if (! oldstr)
9469 oldstr = str;
9470
9471 do
9472 {
9473 oldc = c;
9474 c = my_getSmallParser (&str, &len, &nestlevel);
9475 if (c != S_EX_NONE && c != S_EX_REGISTER)
9476 str += len;
9477 }
9478 while (c != S_EX_NONE && c != S_EX_REGISTER);
9479
9480 if (nestlevel >= 0)
9481 {
9482 /* A percent_op was encountered. Don't try to get an expression if
9483 it is already blanked out. */
9484 if (*(str + strspn (str + 1, " )")) != ')')
9485 {
9486 char save;
9487
9488 /* Let my_getExpression() stop at the closing parenthesis. */
9489 save = *(str + len);
9490 *(str + len) = '\0';
9491 my_getExpression (ep, str);
9492 *(str + len) = save;
9493 }
9494 if (nestlevel > 0)
9495 {
9496 /* Blank out including the % sign and the proper matching
9497 parenthesis. */
9498 int pcnt = 1;
9499 char *s = strrchr (oldstr, '%');
9500 char *end;
9501
9502 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9503 {
9504 if (*end == '(')
9505 pcnt++;
9506 else if (*end == ')')
9507 pcnt--;
9508 }
9509
9510 memset (s, ' ', end - s);
9511 str = oldstr;
9512 }
9513 else
9514 expr_end = str + len;
9515
9516 c = oldc;
9517 }
9518 else if (c == S_EX_NONE)
9519 {
9520 my_getExpression (ep, str);
9521 }
9522 else if (c == S_EX_REGISTER)
9523 {
9524 ep->X_op = O_constant;
9525 expr_end = str;
9526 ep->X_add_symbol = NULL;
9527 ep->X_op_symbol = NULL;
9528 ep->X_add_number = 0;
9529 }
9530 else
9531 {
9532 as_fatal(_("internal error"));
9533 }
9534
9535 if (nestlevel <= 0)
9536 /* All percent_op's have been handled. */
9537 oldstr = NULL;
9538
9539 return c;
9540 }
9541
9542 static void
9543 my_getExpression (ep, str)
9544 expressionS *ep;
9545 char *str;
9546 {
9547 char *save_in;
9548 valueT val;
9549
9550 save_in = input_line_pointer;
9551 input_line_pointer = str;
9552 expression (ep);
9553 expr_end = input_line_pointer;
9554 input_line_pointer = save_in;
9555
9556 /* If we are in mips16 mode, and this is an expression based on `.',
9557 then we bump the value of the symbol by 1 since that is how other
9558 text symbols are handled. We don't bother to handle complex
9559 expressions, just `.' plus or minus a constant. */
9560 if (mips_opts.mips16
9561 && ep->X_op == O_symbol
9562 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9563 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9564 && symbol_get_frag (ep->X_add_symbol) == frag_now
9565 && symbol_constant_p (ep->X_add_symbol)
9566 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9567 S_SET_VALUE (ep->X_add_symbol, val + 1);
9568 }
9569
9570 /* Turn a string in input_line_pointer into a floating point constant
9571 of type TYPE, and store the appropriate bytes in *LITP. The number
9572 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9573 returned, or NULL on OK. */
9574
9575 char *
9576 md_atof (type, litP, sizeP)
9577 int type;
9578 char *litP;
9579 int *sizeP;
9580 {
9581 int prec;
9582 LITTLENUM_TYPE words[4];
9583 char *t;
9584 int i;
9585
9586 switch (type)
9587 {
9588 case 'f':
9589 prec = 2;
9590 break;
9591
9592 case 'd':
9593 prec = 4;
9594 break;
9595
9596 default:
9597 *sizeP = 0;
9598 return _("bad call to md_atof");
9599 }
9600
9601 t = atof_ieee (input_line_pointer, type, words);
9602 if (t)
9603 input_line_pointer = t;
9604
9605 *sizeP = prec * 2;
9606
9607 if (! target_big_endian)
9608 {
9609 for (i = prec - 1; i >= 0; i--)
9610 {
9611 md_number_to_chars (litP, (valueT) words[i], 2);
9612 litP += 2;
9613 }
9614 }
9615 else
9616 {
9617 for (i = 0; i < prec; i++)
9618 {
9619 md_number_to_chars (litP, (valueT) words[i], 2);
9620 litP += 2;
9621 }
9622 }
9623
9624 return NULL;
9625 }
9626
9627 void
9628 md_number_to_chars (buf, val, n)
9629 char *buf;
9630 valueT val;
9631 int n;
9632 {
9633 if (target_big_endian)
9634 number_to_chars_bigendian (buf, val, n);
9635 else
9636 number_to_chars_littleendian (buf, val, n);
9637 }
9638 \f
9639 #ifdef OBJ_ELF
9640 static int support_64bit_objects(void)
9641 {
9642 const char **list, **l;
9643
9644 list = bfd_target_list ();
9645 for (l = list; *l != NULL; l++)
9646 #ifdef TE_TMIPS
9647 /* This is traditional mips */
9648 if (strcmp (*l, "elf64-tradbigmips") == 0
9649 || strcmp (*l, "elf64-tradlittlemips") == 0)
9650 #else
9651 if (strcmp (*l, "elf64-bigmips") == 0
9652 || strcmp (*l, "elf64-littlemips") == 0)
9653 #endif
9654 break;
9655 free (list);
9656 return (*l != NULL);
9657 }
9658 #endif /* OBJ_ELF */
9659
9660 CONST char *md_shortopts = "nO::g::G:";
9661
9662 struct option md_longopts[] =
9663 {
9664 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9665 {"mips0", no_argument, NULL, OPTION_MIPS1},
9666 {"mips1", no_argument, NULL, OPTION_MIPS1},
9667 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9668 {"mips2", no_argument, NULL, OPTION_MIPS2},
9669 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9670 {"mips3", no_argument, NULL, OPTION_MIPS3},
9671 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9672 {"mips4", no_argument, NULL, OPTION_MIPS4},
9673 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9674 {"mips5", no_argument, NULL, OPTION_MIPS5},
9675 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9676 {"mips32", no_argument, NULL, OPTION_MIPS32},
9677 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9678 {"mips64", no_argument, NULL, OPTION_MIPS64},
9679 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9680 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9681 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9682 {"trap", no_argument, NULL, OPTION_TRAP},
9683 {"no-break", no_argument, NULL, OPTION_TRAP},
9684 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9685 {"break", no_argument, NULL, OPTION_BREAK},
9686 {"no-trap", no_argument, NULL, OPTION_BREAK},
9687 #define OPTION_EB (OPTION_MD_BASE + 11)
9688 {"EB", no_argument, NULL, OPTION_EB},
9689 #define OPTION_EL (OPTION_MD_BASE + 12)
9690 {"EL", no_argument, NULL, OPTION_EL},
9691 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9692 {"mips16", no_argument, NULL, OPTION_MIPS16},
9693 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9694 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9695 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9696 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9697 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9698 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9699 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9700 {"mfp32", no_argument, NULL, OPTION_FP32},
9701 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9702 {"mgp32", no_argument, NULL, OPTION_GP32},
9703 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9704 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9705 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9706 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9707 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9708 {"march", required_argument, NULL, OPTION_MARCH},
9709 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9710 {"mtune", required_argument, NULL, OPTION_MTUNE},
9711 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9712 {"mcpu", required_argument, NULL, OPTION_MCPU},
9713 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9714 {"m4650", no_argument, NULL, OPTION_M4650},
9715 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9716 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9717 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9718 {"m4010", no_argument, NULL, OPTION_M4010},
9719 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9720 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9721 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9722 {"m4100", no_argument, NULL, OPTION_M4100},
9723 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9724 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9725 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9726 {"m3900", no_argument, NULL, OPTION_M3900},
9727 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9728 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9729 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9730 {"mgp64", no_argument, NULL, OPTION_GP64},
9731 #ifdef OBJ_ELF
9732 #define OPTION_ELF_BASE (OPTION_MD_BASE + 33)
9733 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9734 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9735 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9736 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
9737 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
9738 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
9739 {"xgot", no_argument, NULL, OPTION_XGOT},
9740 #define OPTION_MABI (OPTION_ELF_BASE + 3)
9741 {"mabi", required_argument, NULL, OPTION_MABI},
9742 #define OPTION_32 (OPTION_ELF_BASE + 4)
9743 {"32", no_argument, NULL, OPTION_32},
9744 #define OPTION_N32 (OPTION_ELF_BASE + 5)
9745 {"n32", no_argument, NULL, OPTION_N32},
9746 #define OPTION_64 (OPTION_ELF_BASE + 6)
9747 {"64", no_argument, NULL, OPTION_64},
9748 #endif /* OBJ_ELF */
9749 {NULL, no_argument, NULL, 0}
9750 };
9751 size_t md_longopts_size = sizeof (md_longopts);
9752
9753 int
9754 md_parse_option (c, arg)
9755 int c;
9756 char *arg;
9757 {
9758 switch (c)
9759 {
9760 case OPTION_CONSTRUCT_FLOATS:
9761 mips_disable_float_construction = 0;
9762 break;
9763
9764 case OPTION_NO_CONSTRUCT_FLOATS:
9765 mips_disable_float_construction = 1;
9766 break;
9767
9768 case OPTION_TRAP:
9769 mips_trap = 1;
9770 break;
9771
9772 case OPTION_BREAK:
9773 mips_trap = 0;
9774 break;
9775
9776 case OPTION_EB:
9777 target_big_endian = 1;
9778 break;
9779
9780 case OPTION_EL:
9781 target_big_endian = 0;
9782 break;
9783
9784 case 'n':
9785 warn_nops = 1;
9786 break;
9787
9788 case 'O':
9789 if (arg && arg[1] == '0')
9790 mips_optimize = 1;
9791 else
9792 mips_optimize = 2;
9793 break;
9794
9795 case 'g':
9796 if (arg == NULL)
9797 mips_debug = 2;
9798 else
9799 mips_debug = atoi (arg);
9800 /* When the MIPS assembler sees -g or -g2, it does not do
9801 optimizations which limit full symbolic debugging. We take
9802 that to be equivalent to -O0. */
9803 if (mips_debug == 2)
9804 mips_optimize = 1;
9805 break;
9806
9807 case OPTION_MIPS1:
9808 mips_opts.isa = ISA_MIPS1;
9809 break;
9810
9811 case OPTION_MIPS2:
9812 mips_opts.isa = ISA_MIPS2;
9813 break;
9814
9815 case OPTION_MIPS3:
9816 mips_opts.isa = ISA_MIPS3;
9817 break;
9818
9819 case OPTION_MIPS4:
9820 mips_opts.isa = ISA_MIPS4;
9821 break;
9822
9823 case OPTION_MIPS5:
9824 mips_opts.isa = ISA_MIPS5;
9825 break;
9826
9827 case OPTION_MIPS32:
9828 mips_opts.isa = ISA_MIPS32;
9829 break;
9830
9831 case OPTION_MIPS64:
9832 mips_opts.isa = ISA_MIPS64;
9833 break;
9834
9835 case OPTION_MTUNE:
9836 case OPTION_MARCH:
9837 case OPTION_MCPU:
9838 {
9839 int cpu = CPU_UNKNOWN;
9840
9841 /* Identify the processor type. */
9842 if (strcasecmp (arg, "default") != 0)
9843 {
9844 const struct mips_cpu_info *ci;
9845
9846 ci = mips_cpu_info_from_name (arg);
9847 if (ci == NULL || ci->is_isa)
9848 {
9849 switch (c)
9850 {
9851 case OPTION_MTUNE:
9852 as_fatal (_("invalid architecture -mtune=%s"), arg);
9853 break;
9854 case OPTION_MARCH:
9855 as_fatal (_("invalid architecture -march=%s"), arg);
9856 break;
9857 case OPTION_MCPU:
9858 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9859 break;
9860 }
9861 }
9862 else
9863 cpu = ci->cpu;
9864 }
9865
9866 switch (c)
9867 {
9868 case OPTION_MTUNE:
9869 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9870 as_warn(_("A different -mtune= was already specified, is now "
9871 "-mtune=%s"), arg);
9872 mips_tune = cpu;
9873 break;
9874 case OPTION_MARCH:
9875 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9876 as_warn(_("A different -march= was already specified, is now "
9877 "-march=%s"), arg);
9878 mips_arch = cpu;
9879 break;
9880 case OPTION_MCPU:
9881 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9882 as_warn(_("A different -mcpu= was already specified, is now "
9883 "-mcpu=%s"), arg);
9884 mips_cpu = cpu;
9885 }
9886 }
9887 break;
9888
9889 case OPTION_M4650:
9890 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9891 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9892 as_warn(_("A different -march= or -mtune= was already specified, "
9893 "is now -m4650"));
9894 mips_arch = CPU_R4650;
9895 mips_tune = CPU_R4650;
9896 break;
9897
9898 case OPTION_NO_M4650:
9899 break;
9900
9901 case OPTION_M4010:
9902 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9903 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9904 as_warn(_("A different -march= or -mtune= was already specified, "
9905 "is now -m4010"));
9906 mips_arch = CPU_R4010;
9907 mips_tune = CPU_R4010;
9908 break;
9909
9910 case OPTION_NO_M4010:
9911 break;
9912
9913 case OPTION_M4100:
9914 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9915 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9916 as_warn(_("A different -march= or -mtune= was already specified, "
9917 "is now -m4100"));
9918 mips_arch = CPU_VR4100;
9919 mips_tune = CPU_VR4100;
9920 break;
9921
9922 case OPTION_NO_M4100:
9923 break;
9924
9925 case OPTION_M3900:
9926 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9927 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9928 as_warn(_("A different -march= or -mtune= was already specified, "
9929 "is now -m3900"));
9930 mips_arch = CPU_R3900;
9931 mips_tune = CPU_R3900;
9932 break;
9933
9934 case OPTION_NO_M3900:
9935 break;
9936
9937 case OPTION_MIPS16:
9938 mips_opts.mips16 = 1;
9939 mips_no_prev_insn (false);
9940 break;
9941
9942 case OPTION_NO_MIPS16:
9943 mips_opts.mips16 = 0;
9944 mips_no_prev_insn (false);
9945 break;
9946
9947 case OPTION_MEMBEDDED_PIC:
9948 mips_pic = EMBEDDED_PIC;
9949 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9950 {
9951 as_bad (_("-G may not be used with embedded PIC code"));
9952 return 0;
9953 }
9954 g_switch_value = 0x7fffffff;
9955 break;
9956
9957 #ifdef OBJ_ELF
9958 /* When generating ELF code, we permit -KPIC and -call_shared to
9959 select SVR4_PIC, and -non_shared to select no PIC. This is
9960 intended to be compatible with Irix 5. */
9961 case OPTION_CALL_SHARED:
9962 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9963 {
9964 as_bad (_("-call_shared is supported only for ELF format"));
9965 return 0;
9966 }
9967 mips_pic = SVR4_PIC;
9968 if (g_switch_seen && g_switch_value != 0)
9969 {
9970 as_bad (_("-G may not be used with SVR4 PIC code"));
9971 return 0;
9972 }
9973 g_switch_value = 0;
9974 break;
9975
9976 case OPTION_NON_SHARED:
9977 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9978 {
9979 as_bad (_("-non_shared is supported only for ELF format"));
9980 return 0;
9981 }
9982 mips_pic = NO_PIC;
9983 break;
9984
9985 /* The -xgot option tells the assembler to use 32 offsets when
9986 accessing the got in SVR4_PIC mode. It is for Irix
9987 compatibility. */
9988 case OPTION_XGOT:
9989 mips_big_got = 1;
9990 break;
9991 #endif /* OBJ_ELF */
9992
9993 case 'G':
9994 if (! USE_GLOBAL_POINTER_OPT)
9995 {
9996 as_bad (_("-G is not supported for this configuration"));
9997 return 0;
9998 }
9999 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10000 {
10001 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10002 return 0;
10003 }
10004 else
10005 g_switch_value = atoi (arg);
10006 g_switch_seen = 1;
10007 break;
10008
10009 #ifdef OBJ_ELF
10010 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10011 and -mabi=64. */
10012 case OPTION_32:
10013 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10014 {
10015 as_bad (_("-32 is supported for ELF format only"));
10016 return 0;
10017 }
10018 mips_opts.abi = O32_ABI;
10019 break;
10020
10021 case OPTION_N32:
10022 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10023 {
10024 as_bad (_("-n32 is supported for ELF format only"));
10025 return 0;
10026 }
10027 mips_opts.abi = N32_ABI;
10028 break;
10029
10030 case OPTION_64:
10031 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10032 {
10033 as_bad (_("-64 is supported for ELF format only"));
10034 return 0;
10035 }
10036 mips_opts.abi = N64_ABI;
10037 if (! support_64bit_objects())
10038 as_fatal (_("No compiled in support for 64 bit object file format"));
10039 break;
10040 #endif /* OBJ_ELF */
10041
10042 case OPTION_GP32:
10043 file_mips_gp32 = 1;
10044 if (mips_opts.abi != O32_ABI)
10045 mips_opts.abi = NO_ABI;
10046 break;
10047
10048 case OPTION_GP64:
10049 file_mips_gp32 = 0;
10050 if (mips_opts.abi == O32_ABI)
10051 mips_opts.abi = NO_ABI;
10052 break;
10053
10054 case OPTION_FP32:
10055 file_mips_fp32 = 1;
10056 if (mips_opts.abi != O32_ABI)
10057 mips_opts.abi = NO_ABI;
10058 break;
10059
10060 #ifdef OBJ_ELF
10061 case OPTION_MABI:
10062 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10063 {
10064 as_bad (_("-mabi is supported for ELF format only"));
10065 return 0;
10066 }
10067 if (strcmp (arg, "32") == 0)
10068 mips_opts.abi = O32_ABI;
10069 else if (strcmp (arg, "o64") == 0)
10070 mips_opts.abi = O64_ABI;
10071 else if (strcmp (arg, "n32") == 0)
10072 mips_opts.abi = N32_ABI;
10073 else if (strcmp (arg, "64") == 0)
10074 {
10075 mips_opts.abi = N64_ABI;
10076 if (! support_64bit_objects())
10077 as_fatal (_("No compiled in support for 64 bit object file "
10078 "format"));
10079 }
10080 else if (strcmp (arg, "eabi") == 0)
10081 mips_opts.abi = EABI_ABI;
10082 else
10083 mips_opts.abi = NO_ABI;
10084 break;
10085 #endif /* OBJ_ELF */
10086
10087 case OPTION_M7000_HILO_FIX:
10088 mips_7000_hilo_fix = true;
10089 break;
10090
10091 case OPTION_NO_M7000_HILO_FIX:
10092 mips_7000_hilo_fix = false;
10093 break;
10094
10095 default:
10096 return 0;
10097 }
10098
10099 return 1;
10100 }
10101
10102 static void
10103 show (stream, string, col_p, first_p)
10104 FILE *stream;
10105 char *string;
10106 int *col_p;
10107 int *first_p;
10108 {
10109 if (*first_p)
10110 {
10111 fprintf (stream, "%24s", "");
10112 *col_p = 24;
10113 }
10114 else
10115 {
10116 fprintf (stream, ", ");
10117 *col_p += 2;
10118 }
10119
10120 if (*col_p + strlen (string) > 72)
10121 {
10122 fprintf (stream, "\n%24s", "");
10123 *col_p = 24;
10124 }
10125
10126 fprintf (stream, "%s", string);
10127 *col_p += strlen (string);
10128
10129 *first_p = 0;
10130 }
10131
10132 void
10133 md_show_usage (stream)
10134 FILE *stream;
10135 {
10136 int column, first;
10137
10138 fprintf (stream, _("\
10139 MIPS options:\n\
10140 -membedded-pic generate embedded position independent code\n\
10141 -EB generate big endian output\n\
10142 -EL generate little endian output\n\
10143 -g, -g2 do not remove unneeded NOPs or swap branches\n\
10144 -G NUM allow referencing objects up to NUM bytes\n\
10145 implicitly with the gp register [default 8]\n"));
10146 fprintf (stream, _("\
10147 -mips1 generate MIPS ISA I instructions\n\
10148 -mips2 generate MIPS ISA II instructions\n\
10149 -mips3 generate MIPS ISA III instructions\n\
10150 -mips4 generate MIPS ISA IV instructions\n\
10151 -mips5 generate MIPS ISA V instructions\n\
10152 -mips32 generate MIPS32 ISA instructions\n\
10153 -mips64 generate MIPS64 ISA instructions\n\
10154 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
10155
10156 first = 1;
10157
10158 show (stream, "2000", &column, &first);
10159 show (stream, "3000", &column, &first);
10160 show (stream, "3900", &column, &first);
10161 show (stream, "4000", &column, &first);
10162 show (stream, "4010", &column, &first);
10163 show (stream, "4100", &column, &first);
10164 show (stream, "4111", &column, &first);
10165 show (stream, "4300", &column, &first);
10166 show (stream, "4400", &column, &first);
10167 show (stream, "4600", &column, &first);
10168 show (stream, "4650", &column, &first);
10169 show (stream, "5000", &column, &first);
10170 show (stream, "5200", &column, &first);
10171 show (stream, "5230", &column, &first);
10172 show (stream, "5231", &column, &first);
10173 show (stream, "5261", &column, &first);
10174 show (stream, "5721", &column, &first);
10175 show (stream, "6000", &column, &first);
10176 show (stream, "8000", &column, &first);
10177 show (stream, "10000", &column, &first);
10178 show (stream, "12000", &column, &first);
10179 show (stream, "sb1", &column, &first);
10180 fputc ('\n', stream);
10181
10182 fprintf (stream, _("\
10183 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10184 -no-mCPU don't generate code specific to CPU.\n\
10185 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10186
10187 first = 1;
10188
10189 show (stream, "3900", &column, &first);
10190 show (stream, "4010", &column, &first);
10191 show (stream, "4100", &column, &first);
10192 show (stream, "4650", &column, &first);
10193 fputc ('\n', stream);
10194
10195 fprintf (stream, _("\
10196 -mips16 generate mips16 instructions\n\
10197 -no-mips16 do not generate mips16 instructions\n"));
10198 fprintf (stream, _("\
10199 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10200 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
10201 -O0 remove unneeded NOPs, do not swap branches\n\
10202 -O remove unneeded NOPs and swap branches\n\
10203 -n warn about NOPs generated from macros\n\
10204 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10205 --trap, --no-break trap exception on div by 0 and mult overflow\n\
10206 --break, --no-trap break exception on div by 0 and mult overflow\n"));
10207 #ifdef OBJ_ELF
10208 fprintf (stream, _("\
10209 -KPIC, -call_shared generate SVR4 position independent code\n\
10210 -non_shared do not generate position independent code\n\
10211 -xgot assume a 32 bit GOT\n\
10212 -mabi=ABI create ABI conformant object file for:\n"));
10213
10214 first = 1;
10215
10216 show (stream, "32", &column, &first);
10217 show (stream, "o64", &column, &first);
10218 show (stream, "n32", &column, &first);
10219 show (stream, "64", &column, &first);
10220 show (stream, "eabi", &column, &first);
10221
10222 fputc ('\n', stream);
10223
10224 fprintf (stream, _("\
10225 -32 create o32 ABI object file (default)\n\
10226 -n32 create n32 ABI object file\n\
10227 -64 create 64 ABI object file\n"));
10228 #endif
10229 }
10230 \f
10231 void
10232 mips_init_after_args ()
10233 {
10234 /* initialize opcodes */
10235 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10236 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10237 }
10238
10239 long
10240 md_pcrel_from (fixP)
10241 fixS *fixP;
10242 {
10243 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10244 && fixP->fx_addsy != (symbolS *) NULL
10245 && ! S_IS_DEFINED (fixP->fx_addsy))
10246 {
10247 /* This makes a branch to an undefined symbol be a branch to the
10248 current location. */
10249 if (mips_pic == EMBEDDED_PIC)
10250 return 4;
10251 else
10252 return 1;
10253 }
10254
10255 /* return the address of the delay slot */
10256 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10257 }
10258
10259 /* This is called before the symbol table is processed. In order to
10260 work with gcc when using mips-tfile, we must keep all local labels.
10261 However, in other cases, we want to discard them. If we were
10262 called with -g, but we didn't see any debugging information, it may
10263 mean that gcc is smuggling debugging information through to
10264 mips-tfile, in which case we must generate all local labels. */
10265
10266 void
10267 mips_frob_file_before_adjust ()
10268 {
10269 #ifndef NO_ECOFF_DEBUGGING
10270 if (ECOFF_DEBUGGING
10271 && mips_debug != 0
10272 && ! ecoff_debugging_seen)
10273 flag_keep_locals = 1;
10274 #endif
10275 }
10276
10277 /* Sort any unmatched HI16_S relocs so that they immediately precede
10278 the corresponding LO reloc. This is called before md_apply_fix3 and
10279 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10280 explicit use of the %hi modifier. */
10281
10282 void
10283 mips_frob_file ()
10284 {
10285 struct mips_hi_fixup *l;
10286
10287 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10288 {
10289 segment_info_type *seginfo;
10290 int pass;
10291
10292 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10293
10294 /* Check quickly whether the next fixup happens to be a matching
10295 %lo. */
10296 if (l->fixp->fx_next != NULL
10297 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10298 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10299 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10300 continue;
10301
10302 /* Look through the fixups for this segment for a matching %lo.
10303 When we find one, move the %hi just in front of it. We do
10304 this in two passes. In the first pass, we try to find a
10305 unique %lo. In the second pass, we permit multiple %hi
10306 relocs for a single %lo (this is a GNU extension). */
10307 seginfo = seg_info (l->seg);
10308 for (pass = 0; pass < 2; pass++)
10309 {
10310 fixS *f, *prev;
10311
10312 prev = NULL;
10313 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10314 {
10315 /* Check whether this is a %lo fixup which matches l->fixp. */
10316 if (f->fx_r_type == BFD_RELOC_LO16
10317 && f->fx_addsy == l->fixp->fx_addsy
10318 && f->fx_offset == l->fixp->fx_offset
10319 && (pass == 1
10320 || prev == NULL
10321 || prev->fx_r_type != BFD_RELOC_HI16_S
10322 || prev->fx_addsy != f->fx_addsy
10323 || prev->fx_offset != f->fx_offset))
10324 {
10325 fixS **pf;
10326
10327 /* Move l->fixp before f. */
10328 for (pf = &seginfo->fix_root;
10329 *pf != l->fixp;
10330 pf = &(*pf)->fx_next)
10331 assert (*pf != NULL);
10332
10333 *pf = l->fixp->fx_next;
10334
10335 l->fixp->fx_next = f;
10336 if (prev == NULL)
10337 seginfo->fix_root = l->fixp;
10338 else
10339 prev->fx_next = l->fixp;
10340
10341 break;
10342 }
10343
10344 prev = f;
10345 }
10346
10347 if (f != NULL)
10348 break;
10349
10350 #if 0 /* GCC code motion plus incomplete dead code elimination
10351 can leave a %hi without a %lo. */
10352 if (pass == 1)
10353 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10354 _("Unmatched %%hi reloc"));
10355 #endif
10356 }
10357 }
10358 }
10359
10360 /* When generating embedded PIC code we need to use a special
10361 relocation to represent the difference of two symbols in the .text
10362 section (switch tables use a difference of this sort). See
10363 include/coff/mips.h for details. This macro checks whether this
10364 fixup requires the special reloc. */
10365 #define SWITCH_TABLE(fixp) \
10366 ((fixp)->fx_r_type == BFD_RELOC_32 \
10367 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10368 && (fixp)->fx_addsy != NULL \
10369 && (fixp)->fx_subsy != NULL \
10370 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10371 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10372
10373 /* When generating embedded PIC code we must keep all PC relative
10374 relocations, in case the linker has to relax a call. We also need
10375 to keep relocations for switch table entries.
10376
10377 We may have combined relocations without symbols in the N32/N64 ABI.
10378 We have to prevent gas from dropping them. */
10379
10380 int
10381 mips_force_relocation (fixp)
10382 fixS *fixp;
10383 {
10384 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10385 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10386 return 1;
10387
10388 if (HAVE_NEWABI
10389 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10390 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10391 || fixp->fx_r_type == BFD_RELOC_HI16_S
10392 || fixp->fx_r_type == BFD_RELOC_LO16))
10393 return 1;
10394
10395 return (mips_pic == EMBEDDED_PIC
10396 && (fixp->fx_pcrel
10397 || SWITCH_TABLE (fixp)
10398 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10399 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10400 }
10401
10402 #ifdef OBJ_ELF
10403 static int
10404 mips_need_elf_addend_fixup (fixP)
10405 fixS *fixP;
10406 {
10407 return (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
10408 || ((S_IS_WEAK (fixP->fx_addsy)
10409 || S_IS_EXTERN (fixP->fx_addsy))
10410 && !S_IS_COMMON (fixP->fx_addsy))
10411 || (symbol_used_in_reloc_p (fixP->fx_addsy)
10412 && (((bfd_get_section_flags (stdoutput,
10413 S_GET_SEGMENT (fixP->fx_addsy))
10414 & SEC_LINK_ONCE) != 0)
10415 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10416 ".gnu.linkonce",
10417 sizeof (".gnu.linkonce") - 1))));
10418 }
10419 #endif
10420
10421 /* Apply a fixup to the object file. */
10422
10423 void
10424 md_apply_fix3 (fixP, valP, seg)
10425 fixS *fixP;
10426 valueT * valP;
10427 segT seg ATTRIBUTE_UNUSED;
10428 {
10429 unsigned char *buf;
10430 long insn;
10431 valueT value;
10432
10433 assert (fixP->fx_size == 4
10434 || fixP->fx_r_type == BFD_RELOC_16
10435 || fixP->fx_r_type == BFD_RELOC_32
10436 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10437 || fixP->fx_r_type == BFD_RELOC_HI16_S
10438 || fixP->fx_r_type == BFD_RELOC_LO16
10439 || fixP->fx_r_type == BFD_RELOC_GPREL16
10440 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10441 || fixP->fx_r_type == BFD_RELOC_GPREL32
10442 || fixP->fx_r_type == BFD_RELOC_64
10443 || fixP->fx_r_type == BFD_RELOC_CTOR
10444 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10445 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10446 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10447 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10448 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10449 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10450 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10451 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10452
10453 value = * valP;
10454
10455 /* If we aren't adjusting this fixup to be against the section
10456 symbol, we need to adjust the value. */
10457 #ifdef OBJ_ELF
10458 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10459 {
10460 if (mips_need_elf_addend_fixup (fixP))
10461 {
10462 valueT symval = S_GET_VALUE (fixP->fx_addsy);
10463
10464 value -= symval;
10465 if (value != 0 && ! fixP->fx_pcrel)
10466 {
10467 /* In this case, the bfd_install_relocation routine will
10468 incorrectly add the symbol value back in. We just want
10469 the addend to appear in the object file. */
10470 value -= symval;
10471
10472 /* Make sure the addend is still non-zero. If it became zero
10473 after the last operation, set it to a spurious value and
10474 subtract the same value from the object file's contents. */
10475 if (value == 0)
10476 {
10477 value = 8;
10478
10479 /* The in-place addends for LO16 relocations are signed;
10480 leave the matching HI16 in-place addends as zero. */
10481 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10482 {
10483 reloc_howto_type *howto;
10484 bfd_vma contents, mask, field;
10485
10486 howto = bfd_reloc_type_lookup (stdoutput,
10487 fixP->fx_r_type);
10488
10489 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10490 + fixP->fx_where,
10491 fixP->fx_size * 8,
10492 target_big_endian);
10493
10494 /* MASK has bits set where the relocation should go.
10495 FIELD is -value, shifted into the appropriate place
10496 for this relocation. */
10497 mask = 1 << (howto->bitsize - 1);
10498 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10499 field = (-value >> howto->rightshift) << howto->bitpos;
10500
10501 bfd_put_bits ((field & mask) | (contents & ~mask),
10502 fixP->fx_frag->fr_literal + fixP->fx_where,
10503 fixP->fx_size * 8,
10504 target_big_endian);
10505 }
10506 }
10507 }
10508 }
10509
10510 /* This code was generated using trial and error and so is
10511 fragile and not trustworthy. If you change it, you should
10512 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10513 they still pass. */
10514 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10515 {
10516 value += fixP->fx_frag->fr_address + fixP->fx_where;
10517
10518 /* BFD's REL handling, for MIPS, is _very_ weird.
10519 This gives the right results, but it can't possibly
10520 be the way things are supposed to work. */
10521 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10522 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10523 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10524 value += fixP->fx_frag->fr_address + fixP->fx_where;
10525 }
10526 }
10527 #endif
10528
10529 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
10530
10531 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10532 fixP->fx_done = 1;
10533
10534 switch (fixP->fx_r_type)
10535 {
10536 case BFD_RELOC_MIPS_JMP:
10537 case BFD_RELOC_MIPS_SHIFT5:
10538 case BFD_RELOC_MIPS_SHIFT6:
10539 case BFD_RELOC_MIPS_GOT_DISP:
10540 case BFD_RELOC_MIPS_GOT_PAGE:
10541 case BFD_RELOC_MIPS_GOT_OFST:
10542 case BFD_RELOC_MIPS_SUB:
10543 case BFD_RELOC_MIPS_INSERT_A:
10544 case BFD_RELOC_MIPS_INSERT_B:
10545 case BFD_RELOC_MIPS_DELETE:
10546 case BFD_RELOC_MIPS_HIGHEST:
10547 case BFD_RELOC_MIPS_HIGHER:
10548 case BFD_RELOC_MIPS_SCN_DISP:
10549 case BFD_RELOC_MIPS_REL16:
10550 case BFD_RELOC_MIPS_RELGOT:
10551 case BFD_RELOC_MIPS_JALR:
10552 case BFD_RELOC_HI16:
10553 case BFD_RELOC_HI16_S:
10554 case BFD_RELOC_GPREL16:
10555 case BFD_RELOC_MIPS_LITERAL:
10556 case BFD_RELOC_MIPS_CALL16:
10557 case BFD_RELOC_MIPS_GOT16:
10558 case BFD_RELOC_GPREL32:
10559 case BFD_RELOC_MIPS_GOT_HI16:
10560 case BFD_RELOC_MIPS_GOT_LO16:
10561 case BFD_RELOC_MIPS_CALL_HI16:
10562 case BFD_RELOC_MIPS_CALL_LO16:
10563 case BFD_RELOC_MIPS16_GPREL:
10564 if (fixP->fx_pcrel)
10565 as_bad_where (fixP->fx_file, fixP->fx_line,
10566 _("Invalid PC relative reloc"));
10567 /* Nothing needed to do. The value comes from the reloc entry */
10568 break;
10569
10570 case BFD_RELOC_MIPS16_JMP:
10571 /* We currently always generate a reloc against a symbol, which
10572 means that we don't want an addend even if the symbol is
10573 defined. */
10574 fixP->fx_addnumber = 0;
10575 break;
10576
10577 case BFD_RELOC_PCREL_HI16_S:
10578 /* The addend for this is tricky if it is internal, so we just
10579 do everything here rather than in bfd_install_relocation. */
10580 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10581 && !fixP->fx_done
10582 && value != 0)
10583 break;
10584 if (fixP->fx_addsy
10585 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10586 {
10587 /* For an external symbol adjust by the address to make it
10588 pcrel_offset. We use the address of the RELLO reloc
10589 which follows this one. */
10590 value += (fixP->fx_next->fx_frag->fr_address
10591 + fixP->fx_next->fx_where);
10592 }
10593 value = ((value + 0x8000) >> 16) & 0xffff;
10594 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10595 if (target_big_endian)
10596 buf += 2;
10597 md_number_to_chars (buf, value, 2);
10598 break;
10599
10600 case BFD_RELOC_PCREL_LO16:
10601 /* The addend for this is tricky if it is internal, so we just
10602 do everything here rather than in bfd_install_relocation. */
10603 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10604 && !fixP->fx_done
10605 && value != 0)
10606 break;
10607 if (fixP->fx_addsy
10608 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10609 value += fixP->fx_frag->fr_address + fixP->fx_where;
10610 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10611 if (target_big_endian)
10612 buf += 2;
10613 md_number_to_chars (buf, value, 2);
10614 break;
10615
10616 case BFD_RELOC_64:
10617 /* This is handled like BFD_RELOC_32, but we output a sign
10618 extended value if we are only 32 bits. */
10619 if (fixP->fx_done
10620 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10621 {
10622 if (8 <= sizeof (valueT))
10623 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10624 value, 8);
10625 else
10626 {
10627 long w1, w2;
10628 long hiv;
10629
10630 w1 = w2 = fixP->fx_where;
10631 if (target_big_endian)
10632 w1 += 4;
10633 else
10634 w2 += 4;
10635 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10636 if ((value & 0x80000000) != 0)
10637 hiv = 0xffffffff;
10638 else
10639 hiv = 0;
10640 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10641 }
10642 }
10643 break;
10644
10645 case BFD_RELOC_RVA:
10646 case BFD_RELOC_32:
10647 /* If we are deleting this reloc entry, we must fill in the
10648 value now. This can happen if we have a .word which is not
10649 resolved when it appears but is later defined. We also need
10650 to fill in the value if this is an embedded PIC switch table
10651 entry. */
10652 if (fixP->fx_done
10653 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10654 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10655 value, 4);
10656 break;
10657
10658 case BFD_RELOC_16:
10659 /* If we are deleting this reloc entry, we must fill in the
10660 value now. */
10661 assert (fixP->fx_size == 2);
10662 if (fixP->fx_done)
10663 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10664 value, 2);
10665 break;
10666
10667 case BFD_RELOC_LO16:
10668 /* When handling an embedded PIC switch statement, we can wind
10669 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10670 if (fixP->fx_done)
10671 {
10672 if (value + 0x8000 > 0xffff)
10673 as_bad_where (fixP->fx_file, fixP->fx_line,
10674 _("relocation overflow"));
10675 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10676 if (target_big_endian)
10677 buf += 2;
10678 md_number_to_chars (buf, value, 2);
10679 }
10680 break;
10681
10682 case BFD_RELOC_16_PCREL_S2:
10683 if ((value & 0x3) != 0)
10684 as_bad_where (fixP->fx_file, fixP->fx_line,
10685 _("Branch to odd address (%lx)"), (long) value);
10686
10687 /* Fall through. */
10688
10689 case BFD_RELOC_16_PCREL:
10690 /*
10691 * We need to save the bits in the instruction since fixup_segment()
10692 * might be deleting the relocation entry (i.e., a branch within
10693 * the current segment).
10694 */
10695 if (!fixP->fx_done && value != 0)
10696 break;
10697 /* If 'value' is zero, the remaining reloc code won't actually
10698 do the store, so it must be done here. This is probably
10699 a bug somewhere. */
10700 if (!fixP->fx_done)
10701 value -= fixP->fx_frag->fr_address + fixP->fx_where;
10702
10703 value = (offsetT) value >> 2;
10704
10705 /* update old instruction data */
10706 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10707 if (target_big_endian)
10708 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10709 else
10710 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10711
10712 if (value + 0x8000 <= 0xffff)
10713 insn |= value & 0xffff;
10714 else
10715 {
10716 /* The branch offset is too large. If this is an
10717 unconditional branch, and we are not generating PIC code,
10718 we can convert it to an absolute jump instruction. */
10719 if (mips_pic == NO_PIC
10720 && fixP->fx_done
10721 && fixP->fx_frag->fr_address >= text_section->vma
10722 && (fixP->fx_frag->fr_address
10723 < text_section->vma + text_section->_raw_size)
10724 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10725 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10726 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10727 {
10728 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10729 insn = 0x0c000000; /* jal */
10730 else
10731 insn = 0x08000000; /* j */
10732 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10733 fixP->fx_done = 0;
10734 fixP->fx_addsy = section_symbol (text_section);
10735 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10736 }
10737 else
10738 {
10739 /* FIXME. It would be possible in principle to handle
10740 conditional branches which overflow. They could be
10741 transformed into a branch around a jump. This would
10742 require setting up variant frags for each different
10743 branch type. The native MIPS assembler attempts to
10744 handle these cases, but it appears to do it
10745 incorrectly. */
10746 as_bad_where (fixP->fx_file, fixP->fx_line,
10747 _("Branch out of range"));
10748 }
10749 }
10750
10751 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10752 break;
10753
10754 case BFD_RELOC_VTABLE_INHERIT:
10755 fixP->fx_done = 0;
10756 if (fixP->fx_addsy
10757 && !S_IS_DEFINED (fixP->fx_addsy)
10758 && !S_IS_WEAK (fixP->fx_addsy))
10759 S_SET_WEAK (fixP->fx_addsy);
10760 break;
10761
10762 case BFD_RELOC_VTABLE_ENTRY:
10763 fixP->fx_done = 0;
10764 break;
10765
10766 default:
10767 internalError ();
10768 }
10769 }
10770
10771 #if 0
10772 void
10773 printInsn (oc)
10774 unsigned long oc;
10775 {
10776 const struct mips_opcode *p;
10777 int treg, sreg, dreg, shamt;
10778 short imm;
10779 const char *args;
10780 int i;
10781
10782 for (i = 0; i < NUMOPCODES; ++i)
10783 {
10784 p = &mips_opcodes[i];
10785 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10786 {
10787 printf ("%08lx %s\t", oc, p->name);
10788 treg = (oc >> 16) & 0x1f;
10789 sreg = (oc >> 21) & 0x1f;
10790 dreg = (oc >> 11) & 0x1f;
10791 shamt = (oc >> 6) & 0x1f;
10792 imm = oc;
10793 for (args = p->args;; ++args)
10794 {
10795 switch (*args)
10796 {
10797 case '\0':
10798 printf ("\n");
10799 break;
10800
10801 case ',':
10802 case '(':
10803 case ')':
10804 printf ("%c", *args);
10805 continue;
10806
10807 case 'r':
10808 assert (treg == sreg);
10809 printf ("$%d,$%d", treg, sreg);
10810 continue;
10811
10812 case 'd':
10813 case 'G':
10814 printf ("$%d", dreg);
10815 continue;
10816
10817 case 't':
10818 case 'E':
10819 printf ("$%d", treg);
10820 continue;
10821
10822 case 'k':
10823 printf ("0x%x", treg);
10824 continue;
10825
10826 case 'b':
10827 case 's':
10828 printf ("$%d", sreg);
10829 continue;
10830
10831 case 'a':
10832 printf ("0x%08lx", oc & 0x1ffffff);
10833 continue;
10834
10835 case 'i':
10836 case 'j':
10837 case 'o':
10838 case 'u':
10839 printf ("%d", imm);
10840 continue;
10841
10842 case '<':
10843 case '>':
10844 printf ("$%d", shamt);
10845 continue;
10846
10847 default:
10848 internalError ();
10849 }
10850 break;
10851 }
10852 return;
10853 }
10854 }
10855 printf (_("%08lx UNDEFINED\n"), oc);
10856 }
10857 #endif
10858
10859 static symbolS *
10860 get_symbol ()
10861 {
10862 int c;
10863 char *name;
10864 symbolS *p;
10865
10866 name = input_line_pointer;
10867 c = get_symbol_end ();
10868 p = (symbolS *) symbol_find_or_make (name);
10869 *input_line_pointer = c;
10870 return p;
10871 }
10872
10873 /* Align the current frag to a given power of two. The MIPS assembler
10874 also automatically adjusts any preceding label. */
10875
10876 static void
10877 mips_align (to, fill, label)
10878 int to;
10879 int fill;
10880 symbolS *label;
10881 {
10882 mips_emit_delays (false);
10883 frag_align (to, fill, 0);
10884 record_alignment (now_seg, to);
10885 if (label != NULL)
10886 {
10887 assert (S_GET_SEGMENT (label) == now_seg);
10888 symbol_set_frag (label, frag_now);
10889 S_SET_VALUE (label, (valueT) frag_now_fix ());
10890 }
10891 }
10892
10893 /* Align to a given power of two. .align 0 turns off the automatic
10894 alignment used by the data creating pseudo-ops. */
10895
10896 static void
10897 s_align (x)
10898 int x ATTRIBUTE_UNUSED;
10899 {
10900 register int temp;
10901 register long temp_fill;
10902 long max_alignment = 15;
10903
10904 /*
10905
10906 o Note that the assembler pulls down any immediately preceeding label
10907 to the aligned address.
10908 o It's not documented but auto alignment is reinstated by
10909 a .align pseudo instruction.
10910 o Note also that after auto alignment is turned off the mips assembler
10911 issues an error on attempt to assemble an improperly aligned data item.
10912 We don't.
10913
10914 */
10915
10916 temp = get_absolute_expression ();
10917 if (temp > max_alignment)
10918 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10919 else if (temp < 0)
10920 {
10921 as_warn (_("Alignment negative: 0 assumed."));
10922 temp = 0;
10923 }
10924 if (*input_line_pointer == ',')
10925 {
10926 input_line_pointer++;
10927 temp_fill = get_absolute_expression ();
10928 }
10929 else
10930 temp_fill = 0;
10931 if (temp)
10932 {
10933 auto_align = 1;
10934 mips_align (temp, (int) temp_fill,
10935 insn_labels != NULL ? insn_labels->label : NULL);
10936 }
10937 else
10938 {
10939 auto_align = 0;
10940 }
10941
10942 demand_empty_rest_of_line ();
10943 }
10944
10945 void
10946 mips_flush_pending_output ()
10947 {
10948 mips_emit_delays (false);
10949 mips_clear_insn_labels ();
10950 }
10951
10952 static void
10953 s_change_sec (sec)
10954 int sec;
10955 {
10956 segT seg;
10957
10958 /* When generating embedded PIC code, we only use the .text, .lit8,
10959 .sdata and .sbss sections. We change the .data and .rdata
10960 pseudo-ops to use .sdata. */
10961 if (mips_pic == EMBEDDED_PIC
10962 && (sec == 'd' || sec == 'r'))
10963 sec = 's';
10964
10965 #ifdef OBJ_ELF
10966 /* The ELF backend needs to know that we are changing sections, so
10967 that .previous works correctly. We could do something like check
10968 for an obj_section_change_hook macro, but that might be confusing
10969 as it would not be appropriate to use it in the section changing
10970 functions in read.c, since obj-elf.c intercepts those. FIXME:
10971 This should be cleaner, somehow. */
10972 obj_elf_section_change_hook ();
10973 #endif
10974
10975 mips_emit_delays (false);
10976 switch (sec)
10977 {
10978 case 't':
10979 s_text (0);
10980 break;
10981 case 'd':
10982 s_data (0);
10983 break;
10984 case 'b':
10985 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10986 demand_empty_rest_of_line ();
10987 break;
10988
10989 case 'r':
10990 if (USE_GLOBAL_POINTER_OPT)
10991 {
10992 seg = subseg_new (RDATA_SECTION_NAME,
10993 (subsegT) get_absolute_expression ());
10994 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10995 {
10996 bfd_set_section_flags (stdoutput, seg,
10997 (SEC_ALLOC
10998 | SEC_LOAD
10999 | SEC_READONLY
11000 | SEC_RELOC
11001 | SEC_DATA));
11002 if (strcmp (TARGET_OS, "elf") != 0)
11003 record_alignment (seg, 4);
11004 }
11005 demand_empty_rest_of_line ();
11006 }
11007 else
11008 {
11009 as_bad (_("No read only data section in this object file format"));
11010 demand_empty_rest_of_line ();
11011 return;
11012 }
11013 break;
11014
11015 case 's':
11016 if (USE_GLOBAL_POINTER_OPT)
11017 {
11018 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11019 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11020 {
11021 bfd_set_section_flags (stdoutput, seg,
11022 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11023 | SEC_DATA);
11024 if (strcmp (TARGET_OS, "elf") != 0)
11025 record_alignment (seg, 4);
11026 }
11027 demand_empty_rest_of_line ();
11028 break;
11029 }
11030 else
11031 {
11032 as_bad (_("Global pointers not supported; recompile -G 0"));
11033 demand_empty_rest_of_line ();
11034 return;
11035 }
11036 }
11037
11038 auto_align = 1;
11039 }
11040
11041 void
11042 mips_enable_auto_align ()
11043 {
11044 auto_align = 1;
11045 }
11046
11047 static void
11048 s_cons (log_size)
11049 int log_size;
11050 {
11051 symbolS *label;
11052
11053 label = insn_labels != NULL ? insn_labels->label : NULL;
11054 mips_emit_delays (false);
11055 if (log_size > 0 && auto_align)
11056 mips_align (log_size, 0, label);
11057 mips_clear_insn_labels ();
11058 cons (1 << log_size);
11059 }
11060
11061 static void
11062 s_float_cons (type)
11063 int type;
11064 {
11065 symbolS *label;
11066
11067 label = insn_labels != NULL ? insn_labels->label : NULL;
11068
11069 mips_emit_delays (false);
11070
11071 if (auto_align)
11072 {
11073 if (type == 'd')
11074 mips_align (3, 0, label);
11075 else
11076 mips_align (2, 0, label);
11077 }
11078
11079 mips_clear_insn_labels ();
11080
11081 float_cons (type);
11082 }
11083
11084 /* Handle .globl. We need to override it because on Irix 5 you are
11085 permitted to say
11086 .globl foo .text
11087 where foo is an undefined symbol, to mean that foo should be
11088 considered to be the address of a function. */
11089
11090 static void
11091 s_mips_globl (x)
11092 int x ATTRIBUTE_UNUSED;
11093 {
11094 char *name;
11095 int c;
11096 symbolS *symbolP;
11097 flagword flag;
11098
11099 name = input_line_pointer;
11100 c = get_symbol_end ();
11101 symbolP = symbol_find_or_make (name);
11102 *input_line_pointer = c;
11103 SKIP_WHITESPACE ();
11104
11105 /* On Irix 5, every global symbol that is not explicitly labelled as
11106 being a function is apparently labelled as being an object. */
11107 flag = BSF_OBJECT;
11108
11109 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11110 {
11111 char *secname;
11112 asection *sec;
11113
11114 secname = input_line_pointer;
11115 c = get_symbol_end ();
11116 sec = bfd_get_section_by_name (stdoutput, secname);
11117 if (sec == NULL)
11118 as_bad (_("%s: no such section"), secname);
11119 *input_line_pointer = c;
11120
11121 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11122 flag = BSF_FUNCTION;
11123 }
11124
11125 symbol_get_bfdsym (symbolP)->flags |= flag;
11126
11127 S_SET_EXTERNAL (symbolP);
11128 demand_empty_rest_of_line ();
11129 }
11130
11131 static void
11132 s_option (x)
11133 int x ATTRIBUTE_UNUSED;
11134 {
11135 char *opt;
11136 char c;
11137
11138 opt = input_line_pointer;
11139 c = get_symbol_end ();
11140
11141 if (*opt == 'O')
11142 {
11143 /* FIXME: What does this mean? */
11144 }
11145 else if (strncmp (opt, "pic", 3) == 0)
11146 {
11147 int i;
11148
11149 i = atoi (opt + 3);
11150 if (i == 0)
11151 mips_pic = NO_PIC;
11152 else if (i == 2)
11153 mips_pic = SVR4_PIC;
11154 else
11155 as_bad (_(".option pic%d not supported"), i);
11156
11157 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11158 {
11159 if (g_switch_seen && g_switch_value != 0)
11160 as_warn (_("-G may not be used with SVR4 PIC code"));
11161 g_switch_value = 0;
11162 bfd_set_gp_size (stdoutput, 0);
11163 }
11164 }
11165 else
11166 as_warn (_("Unrecognized option \"%s\""), opt);
11167
11168 *input_line_pointer = c;
11169 demand_empty_rest_of_line ();
11170 }
11171
11172 /* This structure is used to hold a stack of .set values. */
11173
11174 struct mips_option_stack
11175 {
11176 struct mips_option_stack *next;
11177 struct mips_set_options options;
11178 };
11179
11180 static struct mips_option_stack *mips_opts_stack;
11181
11182 /* Handle the .set pseudo-op. */
11183
11184 static void
11185 s_mipsset (x)
11186 int x ATTRIBUTE_UNUSED;
11187 {
11188 char *name = input_line_pointer, ch;
11189
11190 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11191 input_line_pointer++;
11192 ch = *input_line_pointer;
11193 *input_line_pointer = '\0';
11194
11195 if (strcmp (name, "reorder") == 0)
11196 {
11197 if (mips_opts.noreorder && prev_nop_frag != NULL)
11198 {
11199 /* If we still have pending nops, we can discard them. The
11200 usual nop handling will insert any that are still
11201 needed. */
11202 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11203 * (mips_opts.mips16 ? 2 : 4));
11204 prev_nop_frag = NULL;
11205 }
11206 mips_opts.noreorder = 0;
11207 }
11208 else if (strcmp (name, "noreorder") == 0)
11209 {
11210 mips_emit_delays (true);
11211 mips_opts.noreorder = 1;
11212 mips_any_noreorder = 1;
11213 }
11214 else if (strcmp (name, "at") == 0)
11215 {
11216 mips_opts.noat = 0;
11217 }
11218 else if (strcmp (name, "noat") == 0)
11219 {
11220 mips_opts.noat = 1;
11221 }
11222 else if (strcmp (name, "macro") == 0)
11223 {
11224 mips_opts.warn_about_macros = 0;
11225 }
11226 else if (strcmp (name, "nomacro") == 0)
11227 {
11228 if (mips_opts.noreorder == 0)
11229 as_bad (_("`noreorder' must be set before `nomacro'"));
11230 mips_opts.warn_about_macros = 1;
11231 }
11232 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11233 {
11234 mips_opts.nomove = 0;
11235 }
11236 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11237 {
11238 mips_opts.nomove = 1;
11239 }
11240 else if (strcmp (name, "bopt") == 0)
11241 {
11242 mips_opts.nobopt = 0;
11243 }
11244 else if (strcmp (name, "nobopt") == 0)
11245 {
11246 mips_opts.nobopt = 1;
11247 }
11248 else if (strcmp (name, "mips16") == 0
11249 || strcmp (name, "MIPS-16") == 0)
11250 mips_opts.mips16 = 1;
11251 else if (strcmp (name, "nomips16") == 0
11252 || strcmp (name, "noMIPS-16") == 0)
11253 mips_opts.mips16 = 0;
11254 else if (strncmp (name, "mips", 4) == 0)
11255 {
11256 int isa;
11257
11258 /* Permit the user to change the ISA on the fly. Needless to
11259 say, misuse can cause serious problems. */
11260 isa = atoi (name + 4);
11261 switch (isa)
11262 {
11263 case 0:
11264 mips_opts.gp32 = file_mips_gp32;
11265 mips_opts.fp32 = file_mips_fp32;
11266 mips_opts.abi = file_mips_abi;
11267 break;
11268 case 1:
11269 case 2:
11270 case 32:
11271 mips_opts.gp32 = 1;
11272 mips_opts.fp32 = 1;
11273 break;
11274 case 3:
11275 case 4:
11276 case 5:
11277 case 64:
11278 /* Loosen ABI register width restriction. */
11279 if (mips_opts.abi == O32_ABI)
11280 mips_opts.abi = NO_ABI;
11281 mips_opts.gp32 = 0;
11282 mips_opts.fp32 = 0;
11283 break;
11284 default:
11285 as_bad (_("unknown ISA level %s"), name + 4);
11286 break;
11287 }
11288
11289 switch (isa)
11290 {
11291 case 0: mips_opts.isa = file_mips_isa; break;
11292 case 1: mips_opts.isa = ISA_MIPS1; break;
11293 case 2: mips_opts.isa = ISA_MIPS2; break;
11294 case 3: mips_opts.isa = ISA_MIPS3; break;
11295 case 4: mips_opts.isa = ISA_MIPS4; break;
11296 case 5: mips_opts.isa = ISA_MIPS5; break;
11297 case 32: mips_opts.isa = ISA_MIPS32; break;
11298 case 64: mips_opts.isa = ISA_MIPS64; break;
11299 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11300 }
11301 }
11302 else if (strcmp (name, "autoextend") == 0)
11303 mips_opts.noautoextend = 0;
11304 else if (strcmp (name, "noautoextend") == 0)
11305 mips_opts.noautoextend = 1;
11306 else if (strcmp (name, "push") == 0)
11307 {
11308 struct mips_option_stack *s;
11309
11310 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11311 s->next = mips_opts_stack;
11312 s->options = mips_opts;
11313 mips_opts_stack = s;
11314 }
11315 else if (strcmp (name, "pop") == 0)
11316 {
11317 struct mips_option_stack *s;
11318
11319 s = mips_opts_stack;
11320 if (s == NULL)
11321 as_bad (_(".set pop with no .set push"));
11322 else
11323 {
11324 /* If we're changing the reorder mode we need to handle
11325 delay slots correctly. */
11326 if (s->options.noreorder && ! mips_opts.noreorder)
11327 mips_emit_delays (true);
11328 else if (! s->options.noreorder && mips_opts.noreorder)
11329 {
11330 if (prev_nop_frag != NULL)
11331 {
11332 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11333 * (mips_opts.mips16 ? 2 : 4));
11334 prev_nop_frag = NULL;
11335 }
11336 }
11337
11338 mips_opts = s->options;
11339 mips_opts_stack = s->next;
11340 free (s);
11341 }
11342 }
11343 else
11344 {
11345 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11346 }
11347 *input_line_pointer = ch;
11348 demand_empty_rest_of_line ();
11349 }
11350
11351 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11352 .option pic2. It means to generate SVR4 PIC calls. */
11353
11354 static void
11355 s_abicalls (ignore)
11356 int ignore ATTRIBUTE_UNUSED;
11357 {
11358 mips_pic = SVR4_PIC;
11359 if (USE_GLOBAL_POINTER_OPT)
11360 {
11361 if (g_switch_seen && g_switch_value != 0)
11362 as_warn (_("-G may not be used with SVR4 PIC code"));
11363 g_switch_value = 0;
11364 }
11365 bfd_set_gp_size (stdoutput, 0);
11366 demand_empty_rest_of_line ();
11367 }
11368
11369 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11370 PIC code. It sets the $gp register for the function based on the
11371 function address, which is in the register named in the argument.
11372 This uses a relocation against _gp_disp, which is handled specially
11373 by the linker. The result is:
11374 lui $gp,%hi(_gp_disp)
11375 addiu $gp,$gp,%lo(_gp_disp)
11376 addu $gp,$gp,.cpload argument
11377 The .cpload argument is normally $25 == $t9. */
11378
11379 static void
11380 s_cpload (ignore)
11381 int ignore ATTRIBUTE_UNUSED;
11382 {
11383 expressionS ex;
11384 int icnt = 0;
11385
11386 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11387 .cpload is ignored. */
11388 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11389 {
11390 s_ignore (0);
11391 return;
11392 }
11393
11394 /* .cpload should be in a .set noreorder section. */
11395 if (mips_opts.noreorder == 0)
11396 as_warn (_(".cpload not in noreorder section"));
11397
11398 ex.X_op = O_symbol;
11399 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11400 ex.X_op_symbol = NULL;
11401 ex.X_add_number = 0;
11402
11403 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11404 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11405
11406 macro_build_lui (NULL, &icnt, &ex, GP);
11407 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11408 (int) BFD_RELOC_LO16);
11409
11410 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11411 GP, GP, tc_get_register (0));
11412
11413 demand_empty_rest_of_line ();
11414 }
11415
11416 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11417 .cpsetup $reg1, offset|$reg2, label
11418
11419 If offset is given, this results in:
11420 sd $gp, offset($sp)
11421 lui $gp, %hi(%neg(%gp_rel(label)))
11422 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11423 addu $gp, $gp, $reg1
11424
11425 If $reg2 is given, this results in:
11426 daddu $reg2, $gp, $0
11427 lui $gp, %hi(%neg(%gp_rel(label)))
11428 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11429 addu $gp, $gp, $reg1
11430 */
11431 static void
11432 s_cpsetup (ignore)
11433 int ignore ATTRIBUTE_UNUSED;
11434 {
11435 expressionS ex_off;
11436 expressionS ex_sym;
11437 int reg1;
11438 int icnt = 0;
11439 char *sym;
11440
11441 /* If we are not generating SVR4 PIC code, .cpload is ignored.
11442 We also need NewABI support. */
11443 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11444 {
11445 s_ignore (0);
11446 return;
11447 }
11448
11449 reg1 = tc_get_register (0);
11450 SKIP_WHITESPACE ();
11451 if (*input_line_pointer != ',')
11452 {
11453 as_bad (_("missing argument separator ',' for .cpsetup"));
11454 return;
11455 }
11456 else
11457 input_line_pointer++;
11458 SKIP_WHITESPACE ();
11459 if (*input_line_pointer == '$')
11460 mips_cpreturn_register = tc_get_register (0);
11461 else
11462 mips_cpreturn_offset = get_absolute_expression ();
11463 SKIP_WHITESPACE ();
11464 if (*input_line_pointer != ',')
11465 {
11466 as_bad (_("missing argument separator ',' for .cpsetup"));
11467 return;
11468 }
11469 else
11470 input_line_pointer++;
11471 SKIP_WHITESPACE ();
11472 sym = input_line_pointer;
11473 while (ISALNUM (*input_line_pointer))
11474 input_line_pointer++;
11475 *input_line_pointer = 0;
11476
11477 ex_sym.X_op = O_symbol;
11478 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11479 ex_sym.X_op_symbol = NULL;
11480 ex_sym.X_add_number = 0;
11481
11482 if (mips_cpreturn_register == -1)
11483 {
11484 ex_off.X_op = O_constant;
11485 ex_off.X_add_symbol = NULL;
11486 ex_off.X_op_symbol = NULL;
11487 ex_off.X_add_number = mips_cpreturn_offset;
11488
11489 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11490 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11491 }
11492 else
11493 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11494 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11495
11496 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11497 (int) BFD_RELOC_GPREL16);
11498 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11499 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11500 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11501 NULL, 0, 0, BFD_RELOC_HI16_S);
11502 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11503 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11504 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11505 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11506 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11507 NULL, 0, 0, BFD_RELOC_LO16);
11508 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11509 "d,v,t", mips_gp_register, mips_gp_register, reg1);
11510
11511 demand_empty_rest_of_line ();
11512 }
11513
11514 static void
11515 s_cplocal (ignore)
11516 int ignore ATTRIBUTE_UNUSED;
11517 {
11518 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11519 .cplocal is ignored. */
11520 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11521 {
11522 s_ignore (0);
11523 return;
11524 }
11525
11526 mips_gp_register = tc_get_register (0);
11527 }
11528
11529 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11530 offset from $sp. The offset is remembered, and after making a PIC
11531 call $gp is restored from that location. */
11532
11533 static void
11534 s_cprestore (ignore)
11535 int ignore ATTRIBUTE_UNUSED;
11536 {
11537 expressionS ex;
11538 int icnt = 0;
11539
11540 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11541 .cprestore is ignored. */
11542 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11543 {
11544 s_ignore (0);
11545 return;
11546 }
11547
11548 mips_cprestore_offset = get_absolute_expression ();
11549 mips_cprestore_valid = 1;
11550
11551 ex.X_op = O_constant;
11552 ex.X_add_symbol = NULL;
11553 ex.X_op_symbol = NULL;
11554 ex.X_add_number = mips_cprestore_offset;
11555
11556 macro_build ((char *) NULL, &icnt, &ex,
11557 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11558 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11559
11560 demand_empty_rest_of_line ();
11561 }
11562
11563 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11564 was given in the preceeding .gpsetup, it results in:
11565 ld $gp, offset($sp)
11566
11567 If a register $reg2 was given there, it results in:
11568 daddiu $gp, $gp, $reg2
11569 */
11570 static void
11571 s_cpreturn (ignore)
11572 int ignore ATTRIBUTE_UNUSED;
11573 {
11574 expressionS ex;
11575 int icnt = 0;
11576
11577 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11578 We also need NewABI support. */
11579 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11580 {
11581 s_ignore (0);
11582 return;
11583 }
11584
11585 if (mips_cpreturn_register == -1)
11586 {
11587 ex.X_op = O_constant;
11588 ex.X_add_symbol = NULL;
11589 ex.X_op_symbol = NULL;
11590 ex.X_add_number = mips_cpreturn_offset;
11591
11592 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11593 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11594 }
11595 else
11596 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11597 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11598
11599 demand_empty_rest_of_line ();
11600 }
11601
11602 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11603 code. It sets the offset to use in gp_rel relocations. */
11604
11605 static void
11606 s_gpvalue (ignore)
11607 int ignore ATTRIBUTE_UNUSED;
11608 {
11609 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11610 We also need NewABI support. */
11611 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11612 {
11613 s_ignore (0);
11614 return;
11615 }
11616
11617 mips_cpreturn_offset = get_absolute_expression ();
11618
11619 demand_empty_rest_of_line ();
11620 }
11621
11622 /* Handle the .gpword pseudo-op. This is used when generating PIC
11623 code. It generates a 32 bit GP relative reloc. */
11624
11625 static void
11626 s_gpword (ignore)
11627 int ignore ATTRIBUTE_UNUSED;
11628 {
11629 symbolS *label;
11630 expressionS ex;
11631 char *p;
11632
11633 /* When not generating PIC code, this is treated as .word. */
11634 if (mips_pic != SVR4_PIC)
11635 {
11636 s_cons (2);
11637 return;
11638 }
11639
11640 label = insn_labels != NULL ? insn_labels->label : NULL;
11641 mips_emit_delays (true);
11642 if (auto_align)
11643 mips_align (2, 0, label);
11644 mips_clear_insn_labels ();
11645
11646 expression (&ex);
11647
11648 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11649 {
11650 as_bad (_("Unsupported use of .gpword"));
11651 ignore_rest_of_line ();
11652 }
11653
11654 p = frag_more (4);
11655 md_number_to_chars (p, (valueT) 0, 4);
11656 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11657 BFD_RELOC_GPREL32);
11658
11659 demand_empty_rest_of_line ();
11660 }
11661
11662 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11663 tables in SVR4 PIC code. */
11664
11665 static void
11666 s_cpadd (ignore)
11667 int ignore ATTRIBUTE_UNUSED;
11668 {
11669 int icnt = 0;
11670 int reg;
11671
11672 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11673 code. */
11674 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11675 {
11676 s_ignore (0);
11677 return;
11678 }
11679
11680 /* Add $gp to the register named as an argument. */
11681 reg = tc_get_register (0);
11682 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11683 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11684 "d,v,t", reg, reg, GP);
11685
11686 demand_empty_rest_of_line ();
11687 }
11688
11689 /* Handle the .insn pseudo-op. This marks instruction labels in
11690 mips16 mode. This permits the linker to handle them specially,
11691 such as generating jalx instructions when needed. We also make
11692 them odd for the duration of the assembly, in order to generate the
11693 right sort of code. We will make them even in the adjust_symtab
11694 routine, while leaving them marked. This is convenient for the
11695 debugger and the disassembler. The linker knows to make them odd
11696 again. */
11697
11698 static void
11699 s_insn (ignore)
11700 int ignore ATTRIBUTE_UNUSED;
11701 {
11702 if (mips_opts.mips16)
11703 mips16_mark_labels ();
11704
11705 demand_empty_rest_of_line ();
11706 }
11707
11708 /* Handle a .stabn directive. We need these in order to mark a label
11709 as being a mips16 text label correctly. Sometimes the compiler
11710 will emit a label, followed by a .stabn, and then switch sections.
11711 If the label and .stabn are in mips16 mode, then the label is
11712 really a mips16 text label. */
11713
11714 static void
11715 s_mips_stab (type)
11716 int type;
11717 {
11718 if (type == 'n' && mips_opts.mips16)
11719 mips16_mark_labels ();
11720
11721 s_stab (type);
11722 }
11723
11724 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11725 */
11726
11727 static void
11728 s_mips_weakext (ignore)
11729 int ignore ATTRIBUTE_UNUSED;
11730 {
11731 char *name;
11732 int c;
11733 symbolS *symbolP;
11734 expressionS exp;
11735
11736 name = input_line_pointer;
11737 c = get_symbol_end ();
11738 symbolP = symbol_find_or_make (name);
11739 S_SET_WEAK (symbolP);
11740 *input_line_pointer = c;
11741
11742 SKIP_WHITESPACE ();
11743
11744 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11745 {
11746 if (S_IS_DEFINED (symbolP))
11747 {
11748 as_bad ("ignoring attempt to redefine symbol %s",
11749 S_GET_NAME (symbolP));
11750 ignore_rest_of_line ();
11751 return;
11752 }
11753
11754 if (*input_line_pointer == ',')
11755 {
11756 ++input_line_pointer;
11757 SKIP_WHITESPACE ();
11758 }
11759
11760 expression (&exp);
11761 if (exp.X_op != O_symbol)
11762 {
11763 as_bad ("bad .weakext directive");
11764 ignore_rest_of_line();
11765 return;
11766 }
11767 symbol_set_value_expression (symbolP, &exp);
11768 }
11769
11770 demand_empty_rest_of_line ();
11771 }
11772
11773 /* Parse a register string into a number. Called from the ECOFF code
11774 to parse .frame. The argument is non-zero if this is the frame
11775 register, so that we can record it in mips_frame_reg. */
11776
11777 int
11778 tc_get_register (frame)
11779 int frame;
11780 {
11781 int reg;
11782
11783 SKIP_WHITESPACE ();
11784 if (*input_line_pointer++ != '$')
11785 {
11786 as_warn (_("expected `$'"));
11787 reg = 0;
11788 }
11789 else if (ISDIGIT (*input_line_pointer))
11790 {
11791 reg = get_absolute_expression ();
11792 if (reg < 0 || reg >= 32)
11793 {
11794 as_warn (_("Bad register number"));
11795 reg = 0;
11796 }
11797 }
11798 else
11799 {
11800 if (strncmp (input_line_pointer, "fp", 2) == 0)
11801 reg = FP;
11802 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11803 reg = SP;
11804 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11805 reg = GP;
11806 else if (strncmp (input_line_pointer, "at", 2) == 0)
11807 reg = AT;
11808 else
11809 {
11810 as_warn (_("Unrecognized register name"));
11811 reg = 0;
11812 }
11813 input_line_pointer += 2;
11814 }
11815 if (frame)
11816 {
11817 mips_frame_reg = reg != 0 ? reg : SP;
11818 mips_frame_reg_valid = 1;
11819 mips_cprestore_valid = 0;
11820 }
11821 return reg;
11822 }
11823
11824 valueT
11825 md_section_align (seg, addr)
11826 asection *seg;
11827 valueT addr;
11828 {
11829 int align = bfd_get_section_alignment (stdoutput, seg);
11830
11831 #ifdef OBJ_ELF
11832 /* We don't need to align ELF sections to the full alignment.
11833 However, Irix 5 may prefer that we align them at least to a 16
11834 byte boundary. We don't bother to align the sections if we are
11835 targeted for an embedded system. */
11836 if (strcmp (TARGET_OS, "elf") == 0)
11837 return addr;
11838 if (align > 4)
11839 align = 4;
11840 #endif
11841
11842 return ((addr + (1 << align) - 1) & (-1 << align));
11843 }
11844
11845 /* Utility routine, called from above as well. If called while the
11846 input file is still being read, it's only an approximation. (For
11847 example, a symbol may later become defined which appeared to be
11848 undefined earlier.) */
11849
11850 static int
11851 nopic_need_relax (sym, before_relaxing)
11852 symbolS *sym;
11853 int before_relaxing;
11854 {
11855 if (sym == 0)
11856 return 0;
11857
11858 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11859 {
11860 const char *symname;
11861 int change;
11862
11863 /* Find out whether this symbol can be referenced off the GP
11864 register. It can be if it is smaller than the -G size or if
11865 it is in the .sdata or .sbss section. Certain symbols can
11866 not be referenced off the GP, although it appears as though
11867 they can. */
11868 symname = S_GET_NAME (sym);
11869 if (symname != (const char *) NULL
11870 && (strcmp (symname, "eprol") == 0
11871 || strcmp (symname, "etext") == 0
11872 || strcmp (symname, "_gp") == 0
11873 || strcmp (symname, "edata") == 0
11874 || strcmp (symname, "_fbss") == 0
11875 || strcmp (symname, "_fdata") == 0
11876 || strcmp (symname, "_ftext") == 0
11877 || strcmp (symname, "end") == 0
11878 || strcmp (symname, "_gp_disp") == 0))
11879 change = 1;
11880 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11881 && (0
11882 #ifndef NO_ECOFF_DEBUGGING
11883 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11884 && (symbol_get_obj (sym)->ecoff_extern_size
11885 <= g_switch_value))
11886 #endif
11887 /* We must defer this decision until after the whole
11888 file has been read, since there might be a .extern
11889 after the first use of this symbol. */
11890 || (before_relaxing
11891 #ifndef NO_ECOFF_DEBUGGING
11892 && symbol_get_obj (sym)->ecoff_extern_size == 0
11893 #endif
11894 && S_GET_VALUE (sym) == 0)
11895 || (S_GET_VALUE (sym) != 0
11896 && S_GET_VALUE (sym) <= g_switch_value)))
11897 change = 0;
11898 else
11899 {
11900 const char *segname;
11901
11902 segname = segment_name (S_GET_SEGMENT (sym));
11903 assert (strcmp (segname, ".lit8") != 0
11904 && strcmp (segname, ".lit4") != 0);
11905 change = (strcmp (segname, ".sdata") != 0
11906 && strcmp (segname, ".sbss") != 0
11907 && strncmp (segname, ".sdata.", 7) != 0
11908 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11909 }
11910 return change;
11911 }
11912 else
11913 /* We are not optimizing for the GP register. */
11914 return 1;
11915 }
11916
11917 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11918 extended opcode. SEC is the section the frag is in. */
11919
11920 static int
11921 mips16_extended_frag (fragp, sec, stretch)
11922 fragS *fragp;
11923 asection *sec;
11924 long stretch;
11925 {
11926 int type;
11927 register const struct mips16_immed_operand *op;
11928 offsetT val;
11929 int mintiny, maxtiny;
11930 segT symsec;
11931 fragS *sym_frag;
11932
11933 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11934 return 0;
11935 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11936 return 1;
11937
11938 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11939 op = mips16_immed_operands;
11940 while (op->type != type)
11941 {
11942 ++op;
11943 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11944 }
11945
11946 if (op->unsp)
11947 {
11948 if (type == '<' || type == '>' || type == '[' || type == ']')
11949 {
11950 mintiny = 1;
11951 maxtiny = 1 << op->nbits;
11952 }
11953 else
11954 {
11955 mintiny = 0;
11956 maxtiny = (1 << op->nbits) - 1;
11957 }
11958 }
11959 else
11960 {
11961 mintiny = - (1 << (op->nbits - 1));
11962 maxtiny = (1 << (op->nbits - 1)) - 1;
11963 }
11964
11965 sym_frag = symbol_get_frag (fragp->fr_symbol);
11966 val = S_GET_VALUE (fragp->fr_symbol);
11967 symsec = S_GET_SEGMENT (fragp->fr_symbol);
11968
11969 if (op->pcrel)
11970 {
11971 addressT addr;
11972
11973 /* We won't have the section when we are called from
11974 mips_relax_frag. However, we will always have been called
11975 from md_estimate_size_before_relax first. If this is a
11976 branch to a different section, we mark it as such. If SEC is
11977 NULL, and the frag is not marked, then it must be a branch to
11978 the same section. */
11979 if (sec == NULL)
11980 {
11981 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
11982 return 1;
11983 }
11984 else
11985 {
11986 /* Must have been called from md_estimate_size_before_relax. */
11987 if (symsec != sec)
11988 {
11989 fragp->fr_subtype =
11990 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11991
11992 /* FIXME: We should support this, and let the linker
11993 catch branches and loads that are out of range. */
11994 as_bad_where (fragp->fr_file, fragp->fr_line,
11995 _("unsupported PC relative reference to different section"));
11996
11997 return 1;
11998 }
11999 if (fragp != sym_frag && sym_frag->fr_address == 0)
12000 /* Assume non-extended on the first relaxation pass.
12001 The address we have calculated will be bogus if this is
12002 a forward branch to another frag, as the forward frag
12003 will have fr_address == 0. */
12004 return 0;
12005 }
12006
12007 /* In this case, we know for sure that the symbol fragment is in
12008 the same section. If the relax_marker of the symbol fragment
12009 differs from the relax_marker of this fragment, we have not
12010 yet adjusted the symbol fragment fr_address. We want to add
12011 in STRETCH in order to get a better estimate of the address.
12012 This particularly matters because of the shift bits. */
12013 if (stretch != 0
12014 && sym_frag->relax_marker != fragp->relax_marker)
12015 {
12016 fragS *f;
12017
12018 /* Adjust stretch for any alignment frag. Note that if have
12019 been expanding the earlier code, the symbol may be
12020 defined in what appears to be an earlier frag. FIXME:
12021 This doesn't handle the fr_subtype field, which specifies
12022 a maximum number of bytes to skip when doing an
12023 alignment. */
12024 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12025 {
12026 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12027 {
12028 if (stretch < 0)
12029 stretch = - ((- stretch)
12030 & ~ ((1 << (int) f->fr_offset) - 1));
12031 else
12032 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12033 if (stretch == 0)
12034 break;
12035 }
12036 }
12037 if (f != NULL)
12038 val += stretch;
12039 }
12040
12041 addr = fragp->fr_address + fragp->fr_fix;
12042
12043 /* The base address rules are complicated. The base address of
12044 a branch is the following instruction. The base address of a
12045 PC relative load or add is the instruction itself, but if it
12046 is in a delay slot (in which case it can not be extended) use
12047 the address of the instruction whose delay slot it is in. */
12048 if (type == 'p' || type == 'q')
12049 {
12050 addr += 2;
12051
12052 /* If we are currently assuming that this frag should be
12053 extended, then, the current address is two bytes
12054 higher. */
12055 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12056 addr += 2;
12057
12058 /* Ignore the low bit in the target, since it will be set
12059 for a text label. */
12060 if ((val & 1) != 0)
12061 --val;
12062 }
12063 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12064 addr -= 4;
12065 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12066 addr -= 2;
12067
12068 val -= addr & ~ ((1 << op->shift) - 1);
12069
12070 /* Branch offsets have an implicit 0 in the lowest bit. */
12071 if (type == 'p' || type == 'q')
12072 val /= 2;
12073
12074 /* If any of the shifted bits are set, we must use an extended
12075 opcode. If the address depends on the size of this
12076 instruction, this can lead to a loop, so we arrange to always
12077 use an extended opcode. We only check this when we are in
12078 the main relaxation loop, when SEC is NULL. */
12079 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12080 {
12081 fragp->fr_subtype =
12082 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12083 return 1;
12084 }
12085
12086 /* If we are about to mark a frag as extended because the value
12087 is precisely maxtiny + 1, then there is a chance of an
12088 infinite loop as in the following code:
12089 la $4,foo
12090 .skip 1020
12091 .align 2
12092 foo:
12093 In this case when the la is extended, foo is 0x3fc bytes
12094 away, so the la can be shrunk, but then foo is 0x400 away, so
12095 the la must be extended. To avoid this loop, we mark the
12096 frag as extended if it was small, and is about to become
12097 extended with a value of maxtiny + 1. */
12098 if (val == ((maxtiny + 1) << op->shift)
12099 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12100 && sec == NULL)
12101 {
12102 fragp->fr_subtype =
12103 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12104 return 1;
12105 }
12106 }
12107 else if (symsec != absolute_section && sec != NULL)
12108 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12109
12110 if ((val & ((1 << op->shift) - 1)) != 0
12111 || val < (mintiny << op->shift)
12112 || val > (maxtiny << op->shift))
12113 return 1;
12114 else
12115 return 0;
12116 }
12117
12118 /* Estimate the size of a frag before relaxing. Unless this is the
12119 mips16, we are not really relaxing here, and the final size is
12120 encoded in the subtype information. For the mips16, we have to
12121 decide whether we are using an extended opcode or not. */
12122
12123 int
12124 md_estimate_size_before_relax (fragp, segtype)
12125 fragS *fragp;
12126 asection *segtype;
12127 {
12128 int change = 0;
12129 boolean linkonce = false;
12130
12131 if (RELAX_MIPS16_P (fragp->fr_subtype))
12132 {
12133 if (mips16_extended_frag (fragp, segtype, 0))
12134 {
12135 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12136 return 4;
12137 }
12138 else
12139 {
12140 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12141 return 2;
12142 }
12143 }
12144
12145 if (mips_pic == NO_PIC)
12146 {
12147 change = nopic_need_relax (fragp->fr_symbol, 0);
12148 }
12149 else if (mips_pic == SVR4_PIC)
12150 {
12151 symbolS *sym;
12152 asection *symsec;
12153
12154 sym = fragp->fr_symbol;
12155
12156 /* Handle the case of a symbol equated to another symbol. */
12157 while (symbol_equated_reloc_p (sym))
12158 {
12159 symbolS *n;
12160
12161 /* It's possible to get a loop here in a badly written
12162 program. */
12163 n = symbol_get_value_expression (sym)->X_add_symbol;
12164 if (n == sym)
12165 break;
12166 sym = n;
12167 }
12168
12169 symsec = S_GET_SEGMENT (sym);
12170
12171 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12172 if (symsec != segtype && ! S_IS_LOCAL (sym))
12173 {
12174 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12175 != 0)
12176 linkonce = true;
12177
12178 /* The GNU toolchain uses an extension for ELF: a section
12179 beginning with the magic string .gnu.linkonce is a linkonce
12180 section. */
12181 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12182 sizeof ".gnu.linkonce" - 1) == 0)
12183 linkonce = true;
12184 }
12185
12186 /* This must duplicate the test in adjust_reloc_syms. */
12187 change = (symsec != &bfd_und_section
12188 && symsec != &bfd_abs_section
12189 && ! bfd_is_com_section (symsec)
12190 && !linkonce
12191 #ifdef OBJ_ELF
12192 /* A global or weak symbol is treated as external. */
12193 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
12194 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
12195 #endif
12196 );
12197 }
12198 else
12199 abort ();
12200
12201 if (change)
12202 {
12203 /* Record the offset to the first reloc in the fr_opcode field.
12204 This lets md_convert_frag and tc_gen_reloc know that the code
12205 must be expanded. */
12206 fragp->fr_opcode = (fragp->fr_literal
12207 + fragp->fr_fix
12208 - RELAX_OLD (fragp->fr_subtype)
12209 + RELAX_RELOC1 (fragp->fr_subtype));
12210 /* FIXME: This really needs as_warn_where. */
12211 if (RELAX_WARN (fragp->fr_subtype))
12212 as_warn (_("AT used after \".set noat\" or macro used after "
12213 "\".set nomacro\""));
12214
12215 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12216 }
12217
12218 return 0;
12219 }
12220
12221 /* This is called to see whether a reloc against a defined symbol
12222 should be converted into a reloc against a section. Don't adjust
12223 MIPS16 jump relocations, so we don't have to worry about the format
12224 of the offset in the .o file. Don't adjust relocations against
12225 mips16 symbols, so that the linker can find them if it needs to set
12226 up a stub. */
12227
12228 int
12229 mips_fix_adjustable (fixp)
12230 fixS *fixp;
12231 {
12232 #ifdef OBJ_ELF
12233 /* Prevent all adjustments to global symbols. */
12234 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12235 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12236 return 0;
12237 #endif
12238 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12239 return 0;
12240 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12241 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12242 return 0;
12243 if (fixp->fx_addsy == NULL)
12244 return 1;
12245 #ifdef OBJ_ELF
12246 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12247 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12248 && fixp->fx_subsy == NULL)
12249 return 0;
12250 #endif
12251 return 1;
12252 }
12253
12254 /* Translate internal representation of relocation info to BFD target
12255 format. */
12256
12257 arelent **
12258 tc_gen_reloc (section, fixp)
12259 asection *section ATTRIBUTE_UNUSED;
12260 fixS *fixp;
12261 {
12262 static arelent *retval[4];
12263 arelent *reloc;
12264 bfd_reloc_code_real_type code;
12265
12266 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12267 retval[1] = NULL;
12268
12269 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12270 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12271 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12272
12273 if (mips_pic == EMBEDDED_PIC
12274 && SWITCH_TABLE (fixp))
12275 {
12276 /* For a switch table entry we use a special reloc. The addend
12277 is actually the difference between the reloc address and the
12278 subtrahend. */
12279 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12280 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12281 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12282 fixp->fx_r_type = BFD_RELOC_GPREL32;
12283 }
12284 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12285 {
12286 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12287 reloc->addend = fixp->fx_addnumber;
12288 else
12289 {
12290 /* We use a special addend for an internal RELLO reloc. */
12291 if (symbol_section_p (fixp->fx_addsy))
12292 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12293 else
12294 reloc->addend = fixp->fx_addnumber + reloc->address;
12295 }
12296 }
12297 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12298 {
12299 assert (fixp->fx_next != NULL
12300 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12301
12302 /* The reloc is relative to the RELLO; adjust the addend
12303 accordingly. */
12304 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12305 reloc->addend = fixp->fx_next->fx_addnumber;
12306 else
12307 {
12308 /* We use a special addend for an internal RELHI reloc. */
12309 if (symbol_section_p (fixp->fx_addsy))
12310 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12311 + fixp->fx_next->fx_where
12312 - S_GET_VALUE (fixp->fx_subsy));
12313 else
12314 reloc->addend = (fixp->fx_addnumber
12315 + fixp->fx_next->fx_frag->fr_address
12316 + fixp->fx_next->fx_where);
12317 }
12318 }
12319 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12320 reloc->addend = fixp->fx_addnumber;
12321 else
12322 {
12323 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12324 /* A gruesome hack which is a result of the gruesome gas reloc
12325 handling. */
12326 reloc->addend = reloc->address;
12327 else
12328 reloc->addend = -reloc->address;
12329 }
12330
12331 /* If this is a variant frag, we may need to adjust the existing
12332 reloc and generate a new one. */
12333 if (fixp->fx_frag->fr_opcode != NULL
12334 && (fixp->fx_r_type == BFD_RELOC_GPREL16
12335 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12336 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12337 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12338 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12339 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12340 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12341 && ! HAVE_NEWABI)
12342 {
12343 arelent *reloc2;
12344
12345 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12346
12347 /* If this is not the last reloc in this frag, then we have two
12348 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12349 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12350 the second one handle all of them. */
12351 if (fixp->fx_next != NULL
12352 && fixp->fx_frag == fixp->fx_next->fx_frag)
12353 {
12354 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12355 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12356 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12357 && (fixp->fx_next->fx_r_type
12358 == BFD_RELOC_MIPS_GOT_LO16))
12359 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12360 && (fixp->fx_next->fx_r_type
12361 == BFD_RELOC_MIPS_CALL_LO16)));
12362 retval[0] = NULL;
12363 return retval;
12364 }
12365
12366 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12367 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12368 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12369 retval[2] = NULL;
12370 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12371 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12372 reloc2->address = (reloc->address
12373 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12374 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12375 reloc2->addend = fixp->fx_addnumber;
12376 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12377 assert (reloc2->howto != NULL);
12378
12379 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12380 {
12381 arelent *reloc3;
12382
12383 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12384 retval[3] = NULL;
12385 *reloc3 = *reloc2;
12386 reloc3->address += 4;
12387 }
12388
12389 if (mips_pic == NO_PIC)
12390 {
12391 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12392 fixp->fx_r_type = BFD_RELOC_HI16_S;
12393 }
12394 else if (mips_pic == SVR4_PIC)
12395 {
12396 switch (fixp->fx_r_type)
12397 {
12398 default:
12399 abort ();
12400 case BFD_RELOC_MIPS_GOT16:
12401 break;
12402 case BFD_RELOC_MIPS_CALL16:
12403 case BFD_RELOC_MIPS_GOT_LO16:
12404 case BFD_RELOC_MIPS_CALL_LO16:
12405 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12406 break;
12407 }
12408 }
12409 else
12410 abort ();
12411 }
12412
12413 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12414 to be used in the relocation's section offset. */
12415 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12416 {
12417 reloc->address = reloc->addend;
12418 reloc->addend = 0;
12419 }
12420
12421 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12422 fixup_segment converted a non-PC relative reloc into a PC
12423 relative reloc. In such a case, we need to convert the reloc
12424 code. */
12425 code = fixp->fx_r_type;
12426 if (fixp->fx_pcrel)
12427 {
12428 switch (code)
12429 {
12430 case BFD_RELOC_8:
12431 code = BFD_RELOC_8_PCREL;
12432 break;
12433 case BFD_RELOC_16:
12434 code = BFD_RELOC_16_PCREL;
12435 break;
12436 case BFD_RELOC_32:
12437 code = BFD_RELOC_32_PCREL;
12438 break;
12439 case BFD_RELOC_64:
12440 code = BFD_RELOC_64_PCREL;
12441 break;
12442 case BFD_RELOC_8_PCREL:
12443 case BFD_RELOC_16_PCREL:
12444 case BFD_RELOC_32_PCREL:
12445 case BFD_RELOC_64_PCREL:
12446 case BFD_RELOC_16_PCREL_S2:
12447 case BFD_RELOC_PCREL_HI16_S:
12448 case BFD_RELOC_PCREL_LO16:
12449 break;
12450 default:
12451 as_bad_where (fixp->fx_file, fixp->fx_line,
12452 _("Cannot make %s relocation PC relative"),
12453 bfd_get_reloc_code_name (code));
12454 }
12455 }
12456
12457 #ifdef OBJ_ELF
12458 /* md_apply_fix3 has a double-subtraction hack to get
12459 bfd_install_relocation to behave nicely. GPREL relocations are
12460 handled correctly without this hack, so undo it here. We can't
12461 stop md_apply_fix3 from subtracting twice in the first place since
12462 the fake addend is required for variant frags above. */
12463 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12464 && code == BFD_RELOC_GPREL16
12465 && reloc->addend != 0
12466 && mips_need_elf_addend_fixup (fixp))
12467 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12468 #endif
12469
12470 /* To support a PC relative reloc when generating embedded PIC code
12471 for ECOFF, we use a Cygnus extension. We check for that here to
12472 make sure that we don't let such a reloc escape normally. */
12473 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12474 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12475 && code == BFD_RELOC_16_PCREL_S2
12476 && mips_pic != EMBEDDED_PIC)
12477 reloc->howto = NULL;
12478 else
12479 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12480
12481 if (reloc->howto == NULL)
12482 {
12483 as_bad_where (fixp->fx_file, fixp->fx_line,
12484 _("Can not represent %s relocation in this object file format"),
12485 bfd_get_reloc_code_name (code));
12486 retval[0] = NULL;
12487 }
12488
12489 return retval;
12490 }
12491
12492 /* Relax a machine dependent frag. This returns the amount by which
12493 the current size of the frag should change. */
12494
12495 int
12496 mips_relax_frag (fragp, stretch)
12497 fragS *fragp;
12498 long stretch;
12499 {
12500 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12501 return 0;
12502
12503 if (mips16_extended_frag (fragp, NULL, stretch))
12504 {
12505 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12506 return 0;
12507 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12508 return 2;
12509 }
12510 else
12511 {
12512 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12513 return 0;
12514 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12515 return -2;
12516 }
12517
12518 return 0;
12519 }
12520
12521 /* Convert a machine dependent frag. */
12522
12523 void
12524 md_convert_frag (abfd, asec, fragp)
12525 bfd *abfd ATTRIBUTE_UNUSED;
12526 segT asec;
12527 fragS *fragp;
12528 {
12529 int old, new;
12530 char *fixptr;
12531
12532 if (RELAX_MIPS16_P (fragp->fr_subtype))
12533 {
12534 int type;
12535 register const struct mips16_immed_operand *op;
12536 boolean small, ext;
12537 offsetT val;
12538 bfd_byte *buf;
12539 unsigned long insn;
12540 boolean use_extend;
12541 unsigned short extend;
12542
12543 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12544 op = mips16_immed_operands;
12545 while (op->type != type)
12546 ++op;
12547
12548 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12549 {
12550 small = false;
12551 ext = true;
12552 }
12553 else
12554 {
12555 small = true;
12556 ext = false;
12557 }
12558
12559 resolve_symbol_value (fragp->fr_symbol);
12560 val = S_GET_VALUE (fragp->fr_symbol);
12561 if (op->pcrel)
12562 {
12563 addressT addr;
12564
12565 addr = fragp->fr_address + fragp->fr_fix;
12566
12567 /* The rules for the base address of a PC relative reloc are
12568 complicated; see mips16_extended_frag. */
12569 if (type == 'p' || type == 'q')
12570 {
12571 addr += 2;
12572 if (ext)
12573 addr += 2;
12574 /* Ignore the low bit in the target, since it will be
12575 set for a text label. */
12576 if ((val & 1) != 0)
12577 --val;
12578 }
12579 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12580 addr -= 4;
12581 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12582 addr -= 2;
12583
12584 addr &= ~ (addressT) ((1 << op->shift) - 1);
12585 val -= addr;
12586
12587 /* Make sure the section winds up with the alignment we have
12588 assumed. */
12589 if (op->shift > 0)
12590 record_alignment (asec, op->shift);
12591 }
12592
12593 if (ext
12594 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12595 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12596 as_warn_where (fragp->fr_file, fragp->fr_line,
12597 _("extended instruction in delay slot"));
12598
12599 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12600
12601 if (target_big_endian)
12602 insn = bfd_getb16 (buf);
12603 else
12604 insn = bfd_getl16 (buf);
12605
12606 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12607 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12608 small, ext, &insn, &use_extend, &extend);
12609
12610 if (use_extend)
12611 {
12612 md_number_to_chars (buf, 0xf000 | extend, 2);
12613 fragp->fr_fix += 2;
12614 buf += 2;
12615 }
12616
12617 md_number_to_chars (buf, insn, 2);
12618 fragp->fr_fix += 2;
12619 buf += 2;
12620 }
12621 else
12622 {
12623 if (fragp->fr_opcode == NULL)
12624 return;
12625
12626 old = RELAX_OLD (fragp->fr_subtype);
12627 new = RELAX_NEW (fragp->fr_subtype);
12628 fixptr = fragp->fr_literal + fragp->fr_fix;
12629
12630 if (new > 0)
12631 memcpy (fixptr - old, fixptr, new);
12632
12633 fragp->fr_fix += new - old;
12634 }
12635 }
12636
12637 #ifdef OBJ_ELF
12638
12639 /* This function is called after the relocs have been generated.
12640 We've been storing mips16 text labels as odd. Here we convert them
12641 back to even for the convenience of the debugger. */
12642
12643 void
12644 mips_frob_file_after_relocs ()
12645 {
12646 asymbol **syms;
12647 unsigned int count, i;
12648
12649 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12650 return;
12651
12652 syms = bfd_get_outsymbols (stdoutput);
12653 count = bfd_get_symcount (stdoutput);
12654 for (i = 0; i < count; i++, syms++)
12655 {
12656 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12657 && ((*syms)->value & 1) != 0)
12658 {
12659 (*syms)->value &= ~1;
12660 /* If the symbol has an odd size, it was probably computed
12661 incorrectly, so adjust that as well. */
12662 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12663 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12664 }
12665 }
12666 }
12667
12668 #endif
12669
12670 /* This function is called whenever a label is defined. It is used
12671 when handling branch delays; if a branch has a label, we assume we
12672 can not move it. */
12673
12674 void
12675 mips_define_label (sym)
12676 symbolS *sym;
12677 {
12678 struct insn_label_list *l;
12679
12680 if (free_insn_labels == NULL)
12681 l = (struct insn_label_list *) xmalloc (sizeof *l);
12682 else
12683 {
12684 l = free_insn_labels;
12685 free_insn_labels = l->next;
12686 }
12687
12688 l->label = sym;
12689 l->next = insn_labels;
12690 insn_labels = l;
12691 }
12692 \f
12693 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12694
12695 /* Some special processing for a MIPS ELF file. */
12696
12697 void
12698 mips_elf_final_processing ()
12699 {
12700 /* Write out the register information. */
12701 if (! HAVE_NEWABI)
12702 {
12703 Elf32_RegInfo s;
12704
12705 s.ri_gprmask = mips_gprmask;
12706 s.ri_cprmask[0] = mips_cprmask[0];
12707 s.ri_cprmask[1] = mips_cprmask[1];
12708 s.ri_cprmask[2] = mips_cprmask[2];
12709 s.ri_cprmask[3] = mips_cprmask[3];
12710 /* The gp_value field is set by the MIPS ELF backend. */
12711
12712 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12713 ((Elf32_External_RegInfo *)
12714 mips_regmask_frag));
12715 }
12716 else
12717 {
12718 Elf64_Internal_RegInfo s;
12719
12720 s.ri_gprmask = mips_gprmask;
12721 s.ri_pad = 0;
12722 s.ri_cprmask[0] = mips_cprmask[0];
12723 s.ri_cprmask[1] = mips_cprmask[1];
12724 s.ri_cprmask[2] = mips_cprmask[2];
12725 s.ri_cprmask[3] = mips_cprmask[3];
12726 /* The gp_value field is set by the MIPS ELF backend. */
12727
12728 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12729 ((Elf64_External_RegInfo *)
12730 mips_regmask_frag));
12731 }
12732
12733 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12734 sort of BFD interface for this. */
12735 if (mips_any_noreorder)
12736 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12737 if (mips_pic != NO_PIC)
12738 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12739
12740 /* Set the MIPS ELF ABI flags. */
12741 if (file_mips_abi == NO_ABI)
12742 ;
12743 else if (file_mips_abi == O32_ABI)
12744 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12745 else if (file_mips_abi == O64_ABI)
12746 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12747 else if (file_mips_abi == EABI_ABI)
12748 {
12749 if (mips_eabi64)
12750 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12751 else
12752 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12753 }
12754 else if (file_mips_abi == N32_ABI)
12755 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12756
12757 /* Nothing to do for "64". */
12758
12759 if (mips_32bitmode)
12760 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12761 }
12762
12763 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12764 \f
12765 typedef struct proc {
12766 symbolS *isym;
12767 unsigned long reg_mask;
12768 unsigned long reg_offset;
12769 unsigned long fpreg_mask;
12770 unsigned long fpreg_offset;
12771 unsigned long frame_offset;
12772 unsigned long frame_reg;
12773 unsigned long pc_reg;
12774 } procS;
12775
12776 static procS cur_proc;
12777 static procS *cur_proc_ptr;
12778 static int numprocs;
12779
12780 /* Fill in an rs_align_code fragment. */
12781
12782 void
12783 mips_handle_align (fragp)
12784 fragS *fragp;
12785 {
12786 if (fragp->fr_type != rs_align_code)
12787 return;
12788
12789 if (mips_opts.mips16)
12790 {
12791 static const unsigned char be_nop[] = { 0x65, 0x00 };
12792 static const unsigned char le_nop[] = { 0x00, 0x65 };
12793
12794 int bytes;
12795 char *p;
12796
12797 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12798 p = fragp->fr_literal + fragp->fr_fix;
12799
12800 if (bytes & 1)
12801 {
12802 *p++ = 0;
12803 fragp->fr_fix += 1;
12804 }
12805
12806 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12807 fragp->fr_var = 2;
12808 }
12809
12810 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
12811 }
12812
12813 static void
12814 md_obj_begin ()
12815 {
12816 }
12817
12818 static void
12819 md_obj_end ()
12820 {
12821 /* check for premature end, nesting errors, etc */
12822 if (cur_proc_ptr)
12823 as_warn (_("missing .end at end of assembly"));
12824 }
12825
12826 static long
12827 get_number ()
12828 {
12829 int negative = 0;
12830 long val = 0;
12831
12832 if (*input_line_pointer == '-')
12833 {
12834 ++input_line_pointer;
12835 negative = 1;
12836 }
12837 if (!ISDIGIT (*input_line_pointer))
12838 as_bad (_("expected simple number"));
12839 if (input_line_pointer[0] == '0')
12840 {
12841 if (input_line_pointer[1] == 'x')
12842 {
12843 input_line_pointer += 2;
12844 while (ISXDIGIT (*input_line_pointer))
12845 {
12846 val <<= 4;
12847 val |= hex_value (*input_line_pointer++);
12848 }
12849 return negative ? -val : val;
12850 }
12851 else
12852 {
12853 ++input_line_pointer;
12854 while (ISDIGIT (*input_line_pointer))
12855 {
12856 val <<= 3;
12857 val |= *input_line_pointer++ - '0';
12858 }
12859 return negative ? -val : val;
12860 }
12861 }
12862 if (!ISDIGIT (*input_line_pointer))
12863 {
12864 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12865 *input_line_pointer, *input_line_pointer);
12866 as_warn (_("invalid number"));
12867 return -1;
12868 }
12869 while (ISDIGIT (*input_line_pointer))
12870 {
12871 val *= 10;
12872 val += *input_line_pointer++ - '0';
12873 }
12874 return negative ? -val : val;
12875 }
12876
12877 /* The .file directive; just like the usual .file directive, but there
12878 is an initial number which is the ECOFF file index. */
12879
12880 static void
12881 s_file (x)
12882 int x ATTRIBUTE_UNUSED;
12883 {
12884 int line;
12885
12886 line = get_number ();
12887 s_app_file (0);
12888 }
12889
12890 /* The .end directive. */
12891
12892 static void
12893 s_mips_end (x)
12894 int x ATTRIBUTE_UNUSED;
12895 {
12896 symbolS *p;
12897 int maybe_text;
12898
12899 /* Following functions need their own .frame and .cprestore directives. */
12900 mips_frame_reg_valid = 0;
12901 mips_cprestore_valid = 0;
12902
12903 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12904 {
12905 p = get_symbol ();
12906 demand_empty_rest_of_line ();
12907 }
12908 else
12909 p = NULL;
12910
12911 #ifdef BFD_ASSEMBLER
12912 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12913 maybe_text = 1;
12914 else
12915 maybe_text = 0;
12916 #else
12917 if (now_seg != data_section && now_seg != bss_section)
12918 maybe_text = 1;
12919 else
12920 maybe_text = 0;
12921 #endif
12922
12923 if (!maybe_text)
12924 as_warn (_(".end not in text section"));
12925
12926 if (!cur_proc_ptr)
12927 {
12928 as_warn (_(".end directive without a preceding .ent directive."));
12929 demand_empty_rest_of_line ();
12930 return;
12931 }
12932
12933 if (p != NULL)
12934 {
12935 assert (S_GET_NAME (p));
12936 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12937 as_warn (_(".end symbol does not match .ent symbol."));
12938 }
12939 else
12940 as_warn (_(".end directive missing or unknown symbol"));
12941
12942 #ifdef MIPS_STABS_ELF
12943 {
12944 segT saved_seg = now_seg;
12945 subsegT saved_subseg = now_subseg;
12946 valueT dot;
12947 expressionS exp;
12948 char *fragp;
12949
12950 dot = frag_now_fix ();
12951
12952 #ifdef md_flush_pending_output
12953 md_flush_pending_output ();
12954 #endif
12955
12956 assert (pdr_seg);
12957 subseg_set (pdr_seg, 0);
12958
12959 /* Write the symbol. */
12960 exp.X_op = O_symbol;
12961 exp.X_add_symbol = p;
12962 exp.X_add_number = 0;
12963 emit_expr (&exp, 4);
12964
12965 fragp = frag_more (7 * 4);
12966
12967 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
12968 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
12969 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12970 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12971 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12972 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12973 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
12974
12975 subseg_set (saved_seg, saved_subseg);
12976 }
12977 #endif
12978
12979 cur_proc_ptr = NULL;
12980 }
12981
12982 /* The .aent and .ent directives. */
12983
12984 static void
12985 s_mips_ent (aent)
12986 int aent;
12987 {
12988 int number = 0;
12989 symbolS *symbolP;
12990 int maybe_text;
12991
12992 symbolP = get_symbol ();
12993 if (*input_line_pointer == ',')
12994 input_line_pointer++;
12995 SKIP_WHITESPACE ();
12996 if (ISDIGIT (*input_line_pointer)
12997 || *input_line_pointer == '-')
12998 number = get_number ();
12999
13000 #ifdef BFD_ASSEMBLER
13001 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13002 maybe_text = 1;
13003 else
13004 maybe_text = 0;
13005 #else
13006 if (now_seg != data_section && now_seg != bss_section)
13007 maybe_text = 1;
13008 else
13009 maybe_text = 0;
13010 #endif
13011
13012 if (!maybe_text)
13013 as_warn (_(".ent or .aent not in text section."));
13014
13015 if (!aent && cur_proc_ptr)
13016 as_warn (_("missing .end"));
13017
13018 if (!aent)
13019 {
13020 /* This function needs its own .frame and .cprestore directives. */
13021 mips_frame_reg_valid = 0;
13022 mips_cprestore_valid = 0;
13023
13024 cur_proc_ptr = &cur_proc;
13025 memset (cur_proc_ptr, '\0', sizeof (procS));
13026
13027 cur_proc_ptr->isym = symbolP;
13028
13029 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13030
13031 numprocs++;
13032 }
13033
13034 demand_empty_rest_of_line ();
13035 }
13036
13037 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13038 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13039 s_mips_frame is used so that we can set the PDR information correctly.
13040 We can't use the ecoff routines because they make reference to the ecoff
13041 symbol table (in the mdebug section). */
13042
13043 static void
13044 s_mips_frame (ignore)
13045 int ignore ATTRIBUTE_UNUSED;
13046 {
13047 #ifdef MIPS_STABS_ELF
13048
13049 long val;
13050
13051 if (cur_proc_ptr == (procS *) NULL)
13052 {
13053 as_warn (_(".frame outside of .ent"));
13054 demand_empty_rest_of_line ();
13055 return;
13056 }
13057
13058 cur_proc_ptr->frame_reg = tc_get_register (1);
13059
13060 SKIP_WHITESPACE ();
13061 if (*input_line_pointer++ != ','
13062 || get_absolute_expression_and_terminator (&val) != ',')
13063 {
13064 as_warn (_("Bad .frame directive"));
13065 --input_line_pointer;
13066 demand_empty_rest_of_line ();
13067 return;
13068 }
13069
13070 cur_proc_ptr->frame_offset = val;
13071 cur_proc_ptr->pc_reg = tc_get_register (0);
13072
13073 demand_empty_rest_of_line ();
13074 #else
13075 s_ignore (ignore);
13076 #endif /* MIPS_STABS_ELF */
13077 }
13078
13079 /* The .fmask and .mask directives. If the mdebug section is present
13080 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13081 embedded targets, s_mips_mask is used so that we can set the PDR
13082 information correctly. We can't use the ecoff routines because they
13083 make reference to the ecoff symbol table (in the mdebug section). */
13084
13085 static void
13086 s_mips_mask (reg_type)
13087 char reg_type;
13088 {
13089 #ifdef MIPS_STABS_ELF
13090 long mask, off;
13091
13092 if (cur_proc_ptr == (procS *) NULL)
13093 {
13094 as_warn (_(".mask/.fmask outside of .ent"));
13095 demand_empty_rest_of_line ();
13096 return;
13097 }
13098
13099 if (get_absolute_expression_and_terminator (&mask) != ',')
13100 {
13101 as_warn (_("Bad .mask/.fmask directive"));
13102 --input_line_pointer;
13103 demand_empty_rest_of_line ();
13104 return;
13105 }
13106
13107 off = get_absolute_expression ();
13108
13109 if (reg_type == 'F')
13110 {
13111 cur_proc_ptr->fpreg_mask = mask;
13112 cur_proc_ptr->fpreg_offset = off;
13113 }
13114 else
13115 {
13116 cur_proc_ptr->reg_mask = mask;
13117 cur_proc_ptr->reg_offset = off;
13118 }
13119
13120 demand_empty_rest_of_line ();
13121 #else
13122 s_ignore (reg_type);
13123 #endif /* MIPS_STABS_ELF */
13124 }
13125
13126 /* The .loc directive. */
13127
13128 #if 0
13129 static void
13130 s_loc (x)
13131 int x;
13132 {
13133 symbolS *symbolP;
13134 int lineno;
13135 int addroff;
13136
13137 assert (now_seg == text_section);
13138
13139 lineno = get_number ();
13140 addroff = frag_now_fix ();
13141
13142 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13143 S_SET_TYPE (symbolP, N_SLINE);
13144 S_SET_OTHER (symbolP, 0);
13145 S_SET_DESC (symbolP, lineno);
13146 symbolP->sy_segment = now_seg;
13147 }
13148 #endif
13149
13150 /* CPU name/ISA/number mapping table.
13151
13152 Entries are grouped by type. The first matching CPU or ISA entry
13153 gets chosen by CPU or ISA, so it should be the 'canonical' name
13154 for that type. Entries after that within the type are sorted
13155 alphabetically.
13156
13157 Case is ignored in comparison, so put the canonical entry in the
13158 appropriate case but everything else in lower case to ease eye pain. */
13159 static const struct mips_cpu_info mips_cpu_info_table[] =
13160 {
13161 /* MIPS1 ISA */
13162 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13163 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13164
13165 /* MIPS2 ISA */
13166 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13167
13168 /* MIPS3 ISA */
13169 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13170
13171 /* MIPS4 ISA */
13172 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13173
13174 /* MIPS5 ISA */
13175 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13176 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13177
13178 /* MIPS32 ISA */
13179 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
13180 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
13181 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
13182 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13183 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13184 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13185
13186 /* For historical reasons. */
13187 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
13188
13189 /* MIPS64 ISA */
13190 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
13191 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
13192 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13193 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
13194
13195 /* R2000 CPU */
13196 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13197 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13198 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13199 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13200
13201 /* R3000 CPU */
13202 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13203 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13204 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13205 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13206
13207 /* TX3900 CPU */
13208 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13209 { "3900", 0, ISA_MIPS1, CPU_R3900, },
13210 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
13211
13212 /* R4000 CPU */
13213 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13214 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13215 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13216 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13217
13218 /* R4010 CPU */
13219 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13220 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13221
13222 /* R4400 CPU */
13223 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13224 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13225
13226 /* R4600 CPU */
13227 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13228 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13229 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13230 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13231
13232 /* R4650 CPU */
13233 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13234 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13235
13236 /* R6000 CPU */
13237 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13238 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13239 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13240 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13241
13242 /* R8000 CPU */
13243 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13244 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13245 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13246 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13247
13248 /* R10000 CPU */
13249 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13250 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13251 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13252 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13253
13254 /* R12000 CPU */
13255 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13256 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13257 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13258 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13259
13260 /* VR4100 CPU */
13261 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13262 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13263 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13264 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13265
13266 /* VR4111 CPU */
13267 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13268 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13269 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13270 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13271
13272 /* VR4300 CPU */
13273 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13274 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13275 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13276 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13277
13278 /* VR5000 CPU */
13279 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13280 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13281 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13282 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13283 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13284 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
13285 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
13286 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
13287 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
13288 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
13289 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
13290 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
13291 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
13292 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
13293 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
13294 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13295 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13296
13297 /* Broadcom SB-1 CPU */
13298 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
13299 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
13300 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13301 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13302
13303 /* End marker. */
13304 { NULL, 0, 0, 0, },
13305 };
13306
13307 static const struct mips_cpu_info *
13308 mips_cpu_info_from_name (name)
13309 const char *name;
13310 {
13311 int i;
13312
13313 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13314 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13315 return (&mips_cpu_info_table[i]);
13316
13317 return NULL;
13318 }
13319
13320 static const struct mips_cpu_info *
13321 mips_cpu_info_from_isa (isa)
13322 int isa;
13323 {
13324 int i;
13325
13326 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13327 if (mips_cpu_info_table[i].is_isa
13328 && isa == mips_cpu_info_table[i].isa)
13329 return (&mips_cpu_info_table[i]);
13330
13331 return NULL;
13332 }
13333
13334 static const struct mips_cpu_info *
13335 mips_cpu_info_from_cpu (cpu)
13336 int cpu;
13337 {
13338 int i;
13339
13340 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13341 if (!mips_cpu_info_table[i].is_isa
13342 && cpu == mips_cpu_info_table[i].cpu)
13343 return (&mips_cpu_info_table[i]);
13344
13345 return NULL;
13346 }