* tc-pdp11.c: Use VAX float format support for PDP-11 target.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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, HAVE_64BIT_GPRS);
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, HAVE_64BIT_GPRS);
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 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
4620 "d,v,t", tempreg, tempreg, breg);
4621 }
4622 macro_build ((char *) NULL, &icnt, &offset_expr,
4623 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
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,
4639 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4640 ? (dbl || HAVE_64BIT_ADDRESSES)
4641 : HAVE_64BIT_ADDRESSES));
4642 else if (mips_pic == NO_PIC)
4643 {
4644 /* If this is a reference to a GP relative symbol, we want
4645 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4646 Otherwise we want
4647 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4648 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4649 If we have a constant, we need two instructions anyhow,
4650 so we may as well always use the latter form.
4651
4652 With 64bit address space and a usable $at we want
4653 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4654 lui $at,<sym> (BFD_RELOC_HI16_S)
4655 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4656 daddiu $at,<sym> (BFD_RELOC_LO16)
4657 dsll32 $tempreg,0
4658 dadd $tempreg,$tempreg,$at
4659
4660 If $at is already in use, we use an path which is suboptimal
4661 on superscalar processors.
4662 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4663 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4664 dsll $tempreg,16
4665 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4666 dsll $tempreg,16
4667 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4668 */
4669 p = NULL;
4670 if (HAVE_64BIT_ADDRESSES)
4671 {
4672 /* We don't do GP optimization for now because RELAX_ENCODE can't
4673 hold the data for such large chunks. */
4674
4675 if (used_at == 0)
4676 {
4677 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4678 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4679 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4680 AT, (int) BFD_RELOC_HI16_S);
4681 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4682 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4683 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4684 AT, AT, (int) BFD_RELOC_LO16);
4685 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4686 "d,w,<", tempreg, tempreg, 0);
4687 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4688 tempreg, tempreg, AT);
4689 used_at = 1;
4690 }
4691 else
4692 {
4693 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4694 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4695 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4696 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4697 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4698 tempreg, tempreg, 16);
4699 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4700 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4701 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4702 tempreg, tempreg, 16);
4703 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4704 tempreg, tempreg, (int) BFD_RELOC_LO16);
4705 }
4706 }
4707 else
4708 {
4709 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4710 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4711 {
4712 frag_grow (20);
4713 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
4714 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4715 p = frag_var (rs_machine_dependent, 8, 0,
4716 RELAX_ENCODE (4, 8, 0, 4, 0,
4717 mips_opts.warn_about_macros),
4718 offset_expr.X_add_symbol, 0, NULL);
4719 }
4720 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4721 if (p != NULL)
4722 p += 4;
4723 macro_build (p, &icnt, &offset_expr, "addiu",
4724 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4725 }
4726 }
4727 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4728 {
4729 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4730
4731 /* If this is a reference to an external symbol, and there
4732 is no constant, we want
4733 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4734 or if tempreg is PIC_CALL_REG
4735 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4736 For a local symbol, we want
4737 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4738 nop
4739 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4740
4741 If we have a small constant, and this is a reference to
4742 an external symbol, we want
4743 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4744 nop
4745 addiu $tempreg,$tempreg,<constant>
4746 For a local symbol, we want the same instruction
4747 sequence, but we output a BFD_RELOC_LO16 reloc on the
4748 addiu instruction.
4749
4750 If we have a large constant, and this is a reference to
4751 an external symbol, we want
4752 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4753 lui $at,<hiconstant>
4754 addiu $at,$at,<loconstant>
4755 addu $tempreg,$tempreg,$at
4756 For a local symbol, we want the same instruction
4757 sequence, but we output a BFD_RELOC_LO16 reloc on the
4758 addiu instruction. */
4759 expr1.X_add_number = offset_expr.X_add_number;
4760 offset_expr.X_add_number = 0;
4761 frag_grow (32);
4762 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4763 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4764 macro_build ((char *) NULL, &icnt, &offset_expr,
4765 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4766 "t,o(b)", tempreg, lw_reloc_type, GP);
4767 if (expr1.X_add_number == 0)
4768 {
4769 int off;
4770
4771 if (breg == 0)
4772 off = 0;
4773 else
4774 {
4775 /* We're going to put in an addu instruction using
4776 tempreg, so we may as well insert the nop right
4777 now. */
4778 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4779 "nop", "");
4780 off = 4;
4781 }
4782 p = frag_var (rs_machine_dependent, 8 - off, 0,
4783 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4784 (breg == 0
4785 ? mips_opts.warn_about_macros
4786 : 0)),
4787 offset_expr.X_add_symbol, 0, NULL);
4788 if (breg == 0)
4789 {
4790 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4791 p += 4;
4792 }
4793 macro_build (p, &icnt, &expr1,
4794 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4795 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4796 /* FIXME: If breg == 0, and the next instruction uses
4797 $tempreg, then if this variant case is used an extra
4798 nop will be generated. */
4799 }
4800 else if (expr1.X_add_number >= -0x8000
4801 && expr1.X_add_number < 0x8000)
4802 {
4803 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4804 "nop", "");
4805 macro_build ((char *) NULL, &icnt, &expr1,
4806 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4807 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4808 frag_var (rs_machine_dependent, 0, 0,
4809 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4810 offset_expr.X_add_symbol, 0, NULL);
4811 }
4812 else
4813 {
4814 int off1;
4815
4816 /* If we are going to add in a base register, and the
4817 target register and the base register are the same,
4818 then we are using AT as a temporary register. Since
4819 we want to load the constant into AT, we add our
4820 current AT (from the global offset table) and the
4821 register into the register now, and pretend we were
4822 not using a base register. */
4823 if (breg != treg)
4824 off1 = 0;
4825 else
4826 {
4827 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4828 "nop", "");
4829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4830 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4831 "d,v,t", treg, AT, breg);
4832 breg = 0;
4833 tempreg = treg;
4834 off1 = -8;
4835 }
4836
4837 /* Set mips_optimize around the lui instruction to avoid
4838 inserting an unnecessary nop after the lw. */
4839 hold_mips_optimize = mips_optimize;
4840 mips_optimize = 2;
4841 macro_build_lui (NULL, &icnt, &expr1, AT);
4842 mips_optimize = hold_mips_optimize;
4843
4844 macro_build ((char *) NULL, &icnt, &expr1,
4845 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4846 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4847 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4848 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4849 "d,v,t", tempreg, tempreg, AT);
4850 frag_var (rs_machine_dependent, 0, 0,
4851 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4852 offset_expr.X_add_symbol, 0, NULL);
4853 used_at = 1;
4854 }
4855 }
4856 else if (mips_pic == SVR4_PIC)
4857 {
4858 int gpdel;
4859 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4860 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4861
4862 /* This is the large GOT case. If this is a reference to an
4863 external symbol, and there is no constant, we want
4864 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4865 addu $tempreg,$tempreg,$gp
4866 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4867 or if tempreg is PIC_CALL_REG
4868 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4869 addu $tempreg,$tempreg,$gp
4870 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4871 For a local symbol, we want
4872 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4873 nop
4874 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4875
4876 If we have a small constant, and this is a reference to
4877 an external symbol, we want
4878 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4879 addu $tempreg,$tempreg,$gp
4880 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4881 nop
4882 addiu $tempreg,$tempreg,<constant>
4883 For a local symbol, we want
4884 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4885 nop
4886 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4887
4888 If we have a large constant, and this is a reference to
4889 an external symbol, we want
4890 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4891 addu $tempreg,$tempreg,$gp
4892 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4893 lui $at,<hiconstant>
4894 addiu $at,$at,<loconstant>
4895 addu $tempreg,$tempreg,$at
4896 For a local symbol, we want
4897 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4898 lui $at,<hiconstant>
4899 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4900 addu $tempreg,$tempreg,$at
4901 */
4902 expr1.X_add_number = offset_expr.X_add_number;
4903 offset_expr.X_add_number = 0;
4904 frag_grow (52);
4905 if (reg_needs_delay (GP))
4906 gpdel = 4;
4907 else
4908 gpdel = 0;
4909 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4910 {
4911 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4912 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4913 }
4914 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4915 tempreg, lui_reloc_type);
4916 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4917 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4918 "d,v,t", tempreg, tempreg, GP);
4919 macro_build ((char *) NULL, &icnt, &offset_expr,
4920 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4921 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4922 if (expr1.X_add_number == 0)
4923 {
4924 int off;
4925
4926 if (breg == 0)
4927 off = 0;
4928 else
4929 {
4930 /* We're going to put in an addu instruction using
4931 tempreg, so we may as well insert the nop right
4932 now. */
4933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4934 "nop", "");
4935 off = 4;
4936 }
4937
4938 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4939 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4940 8 + gpdel, 0,
4941 (breg == 0
4942 ? mips_opts.warn_about_macros
4943 : 0)),
4944 offset_expr.X_add_symbol, 0, NULL);
4945 }
4946 else if (expr1.X_add_number >= -0x8000
4947 && expr1.X_add_number < 0x8000)
4948 {
4949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4950 "nop", "");
4951 macro_build ((char *) NULL, &icnt, &expr1,
4952 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4953 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4954
4955 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4956 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4957 (breg == 0
4958 ? mips_opts.warn_about_macros
4959 : 0)),
4960 offset_expr.X_add_symbol, 0, NULL);
4961 }
4962 else
4963 {
4964 int adj, dreg;
4965
4966 /* If we are going to add in a base register, and the
4967 target register and the base register are the same,
4968 then we are using AT as a temporary register. Since
4969 we want to load the constant into AT, we add our
4970 current AT (from the global offset table) and the
4971 register into the register now, and pretend we were
4972 not using a base register. */
4973 if (breg != treg)
4974 {
4975 adj = 0;
4976 dreg = tempreg;
4977 }
4978 else
4979 {
4980 assert (tempreg == AT);
4981 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4982 "nop", "");
4983 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4984 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4985 "d,v,t", treg, AT, breg);
4986 dreg = treg;
4987 adj = 8;
4988 }
4989
4990 /* Set mips_optimize around the lui instruction to avoid
4991 inserting an unnecessary nop after the lw. */
4992 hold_mips_optimize = mips_optimize;
4993 mips_optimize = 2;
4994 macro_build_lui (NULL, &icnt, &expr1, AT);
4995 mips_optimize = hold_mips_optimize;
4996
4997 macro_build ((char *) NULL, &icnt, &expr1,
4998 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4999 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5001 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5002 "d,v,t", dreg, dreg, AT);
5003
5004 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5005 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5006 8 + gpdel, 0,
5007 (breg == 0
5008 ? mips_opts.warn_about_macros
5009 : 0)),
5010 offset_expr.X_add_symbol, 0, NULL);
5011
5012 used_at = 1;
5013 }
5014
5015 if (gpdel > 0)
5016 {
5017 /* This is needed because this instruction uses $gp, but
5018 the first instruction on the main stream does not. */
5019 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5020 p += 4;
5021 }
5022 macro_build (p, &icnt, &offset_expr,
5023 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5024 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5025 p += 4;
5026 if (expr1.X_add_number >= -0x8000
5027 && expr1.X_add_number < 0x8000)
5028 {
5029 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5030 p += 4;
5031 macro_build (p, &icnt, &expr1,
5032 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5033 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5034 /* FIXME: If add_number is 0, and there was no base
5035 register, the external symbol case ended with a load,
5036 so if the symbol turns out to not be external, and
5037 the next instruction uses tempreg, an unnecessary nop
5038 will be inserted. */
5039 }
5040 else
5041 {
5042 if (breg == treg)
5043 {
5044 /* We must add in the base register now, as in the
5045 external symbol case. */
5046 assert (tempreg == AT);
5047 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5048 p += 4;
5049 macro_build (p, &icnt, (expressionS *) NULL,
5050 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5051 "d,v,t", treg, AT, breg);
5052 p += 4;
5053 tempreg = treg;
5054 /* We set breg to 0 because we have arranged to add
5055 it in in both cases. */
5056 breg = 0;
5057 }
5058
5059 macro_build_lui (p, &icnt, &expr1, AT);
5060 p += 4;
5061 macro_build (p, &icnt, &expr1,
5062 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5063 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5064 p += 4;
5065 macro_build (p, &icnt, (expressionS *) NULL,
5066 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5067 "d,v,t", tempreg, tempreg, AT);
5068 p += 4;
5069 }
5070 }
5071 else if (mips_pic == EMBEDDED_PIC)
5072 {
5073 /* We use
5074 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
5075 */
5076 macro_build ((char *) NULL, &icnt, &offset_expr,
5077 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5078 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
5079 }
5080 else
5081 abort ();
5082
5083 if (breg != 0)
5084 {
5085 char *s;
5086
5087 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5088 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5089 else
5090 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5091
5092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5093 "d,v,t", treg, tempreg, breg);
5094 }
5095
5096 if (! used_at)
5097 return;
5098
5099 break;
5100
5101 case M_J_A:
5102 /* The j instruction may not be used in PIC code, since it
5103 requires an absolute address. We convert it to a b
5104 instruction. */
5105 if (mips_pic == NO_PIC)
5106 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5107 else
5108 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5109 return;
5110
5111 /* The jal instructions must be handled as macros because when
5112 generating PIC code they expand to multi-instruction
5113 sequences. Normally they are simple instructions. */
5114 case M_JAL_1:
5115 dreg = RA;
5116 /* Fall through. */
5117 case M_JAL_2:
5118 if (mips_pic == NO_PIC
5119 || mips_pic == EMBEDDED_PIC)
5120 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5121 "d,s", dreg, sreg);
5122 else if (mips_pic == SVR4_PIC)
5123 {
5124 if (sreg != PIC_CALL_REG)
5125 as_warn (_("MIPS PIC call to register other than $25"));
5126
5127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5128 "d,s", dreg, sreg);
5129 if (! HAVE_NEWABI)
5130 {
5131 if (mips_cprestore_offset < 0)
5132 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5133 else
5134 {
5135 if (! mips_frame_reg_valid)
5136 {
5137 as_warn (_("No .frame pseudo-op used in PIC code"));
5138 /* Quiet this warning. */
5139 mips_frame_reg_valid = 1;
5140 }
5141 if (! mips_cprestore_valid)
5142 {
5143 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5144 /* Quiet this warning. */
5145 mips_cprestore_valid = 1;
5146 }
5147 expr1.X_add_number = mips_cprestore_offset;
5148 macro_build ((char *) NULL, &icnt, &expr1,
5149 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5150 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5151 }
5152 }
5153 }
5154 else
5155 abort ();
5156
5157 return;
5158
5159 case M_JAL_A:
5160 if (mips_pic == NO_PIC)
5161 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5162 else if (mips_pic == SVR4_PIC)
5163 {
5164 /* If this is a reference to an external symbol, and we are
5165 using a small GOT, we want
5166 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5167 nop
5168 jalr $25
5169 nop
5170 lw $gp,cprestore($sp)
5171 The cprestore value is set using the .cprestore
5172 pseudo-op. If we are using a big GOT, we want
5173 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5174 addu $25,$25,$gp
5175 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5176 nop
5177 jalr $25
5178 nop
5179 lw $gp,cprestore($sp)
5180 If the symbol is not external, we want
5181 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5182 nop
5183 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5184 jalr $25
5185 nop
5186 lw $gp,cprestore($sp) */
5187 frag_grow (40);
5188 if (! mips_big_got)
5189 {
5190 macro_build ((char *) NULL, &icnt, &offset_expr,
5191 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5192 "t,o(b)", PIC_CALL_REG,
5193 (int) BFD_RELOC_MIPS_CALL16, GP);
5194 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5195 "nop", "");
5196 p = frag_var (rs_machine_dependent, 4, 0,
5197 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5198 offset_expr.X_add_symbol, 0, NULL);
5199 }
5200 else
5201 {
5202 int gpdel;
5203
5204 if (reg_needs_delay (GP))
5205 gpdel = 4;
5206 else
5207 gpdel = 0;
5208 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5209 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5211 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5212 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5213 macro_build ((char *) NULL, &icnt, &offset_expr,
5214 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5215 "t,o(b)", PIC_CALL_REG,
5216 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5217 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5218 "nop", "");
5219 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5220 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5221 0, 0),
5222 offset_expr.X_add_symbol, 0, NULL);
5223 if (gpdel > 0)
5224 {
5225 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5226 p += 4;
5227 }
5228 macro_build (p, &icnt, &offset_expr,
5229 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5230 "t,o(b)", PIC_CALL_REG,
5231 (int) BFD_RELOC_MIPS_GOT16, GP);
5232 p += 4;
5233 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5234 p += 4;
5235 }
5236 macro_build (p, &icnt, &offset_expr,
5237 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5238 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5239 (int) BFD_RELOC_LO16);
5240 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5241 "jalr", "s", PIC_CALL_REG);
5242 if (! HAVE_NEWABI)
5243 {
5244 if (mips_cprestore_offset < 0)
5245 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5246 else
5247 {
5248 if (! mips_frame_reg_valid)
5249 {
5250 as_warn (_("No .frame pseudo-op used in PIC code"));
5251 /* Quiet this warning. */
5252 mips_frame_reg_valid = 1;
5253 }
5254 if (! mips_cprestore_valid)
5255 {
5256 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5257 /* Quiet this warning. */
5258 mips_cprestore_valid = 1;
5259 }
5260 if (mips_opts.noreorder)
5261 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5262 "nop", "");
5263 expr1.X_add_number = mips_cprestore_offset;
5264 macro_build ((char *) NULL, &icnt, &expr1,
5265 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5266 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5267 }
5268 }
5269 }
5270 else if (mips_pic == EMBEDDED_PIC)
5271 {
5272 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5273 /* The linker may expand the call to a longer sequence which
5274 uses $at, so we must break rather than return. */
5275 break;
5276 }
5277 else
5278 abort ();
5279
5280 return;
5281
5282 case M_LB_AB:
5283 s = "lb";
5284 goto ld;
5285 case M_LBU_AB:
5286 s = "lbu";
5287 goto ld;
5288 case M_LH_AB:
5289 s = "lh";
5290 goto ld;
5291 case M_LHU_AB:
5292 s = "lhu";
5293 goto ld;
5294 case M_LW_AB:
5295 s = "lw";
5296 goto ld;
5297 case M_LWC0_AB:
5298 s = "lwc0";
5299 /* Itbl support may require additional care here. */
5300 coproc = 1;
5301 goto ld;
5302 case M_LWC1_AB:
5303 s = "lwc1";
5304 /* Itbl support may require additional care here. */
5305 coproc = 1;
5306 goto ld;
5307 case M_LWC2_AB:
5308 s = "lwc2";
5309 /* Itbl support may require additional care here. */
5310 coproc = 1;
5311 goto ld;
5312 case M_LWC3_AB:
5313 s = "lwc3";
5314 /* Itbl support may require additional care here. */
5315 coproc = 1;
5316 goto ld;
5317 case M_LWL_AB:
5318 s = "lwl";
5319 lr = 1;
5320 goto ld;
5321 case M_LWR_AB:
5322 s = "lwr";
5323 lr = 1;
5324 goto ld;
5325 case M_LDC1_AB:
5326 if (mips_arch == CPU_R4650)
5327 {
5328 as_bad (_("opcode not supported on this processor"));
5329 return;
5330 }
5331 s = "ldc1";
5332 /* Itbl support may require additional care here. */
5333 coproc = 1;
5334 goto ld;
5335 case M_LDC2_AB:
5336 s = "ldc2";
5337 /* Itbl support may require additional care here. */
5338 coproc = 1;
5339 goto ld;
5340 case M_LDC3_AB:
5341 s = "ldc3";
5342 /* Itbl support may require additional care here. */
5343 coproc = 1;
5344 goto ld;
5345 case M_LDL_AB:
5346 s = "ldl";
5347 lr = 1;
5348 goto ld;
5349 case M_LDR_AB:
5350 s = "ldr";
5351 lr = 1;
5352 goto ld;
5353 case M_LL_AB:
5354 s = "ll";
5355 goto ld;
5356 case M_LLD_AB:
5357 s = "lld";
5358 goto ld;
5359 case M_LWU_AB:
5360 s = "lwu";
5361 ld:
5362 if (breg == treg || coproc || lr)
5363 {
5364 tempreg = AT;
5365 used_at = 1;
5366 }
5367 else
5368 {
5369 tempreg = treg;
5370 used_at = 0;
5371 }
5372 goto ld_st;
5373 case M_SB_AB:
5374 s = "sb";
5375 goto st;
5376 case M_SH_AB:
5377 s = "sh";
5378 goto st;
5379 case M_SW_AB:
5380 s = "sw";
5381 goto st;
5382 case M_SWC0_AB:
5383 s = "swc0";
5384 /* Itbl support may require additional care here. */
5385 coproc = 1;
5386 goto st;
5387 case M_SWC1_AB:
5388 s = "swc1";
5389 /* Itbl support may require additional care here. */
5390 coproc = 1;
5391 goto st;
5392 case M_SWC2_AB:
5393 s = "swc2";
5394 /* Itbl support may require additional care here. */
5395 coproc = 1;
5396 goto st;
5397 case M_SWC3_AB:
5398 s = "swc3";
5399 /* Itbl support may require additional care here. */
5400 coproc = 1;
5401 goto st;
5402 case M_SWL_AB:
5403 s = "swl";
5404 goto st;
5405 case M_SWR_AB:
5406 s = "swr";
5407 goto st;
5408 case M_SC_AB:
5409 s = "sc";
5410 goto st;
5411 case M_SCD_AB:
5412 s = "scd";
5413 goto st;
5414 case M_SDC1_AB:
5415 if (mips_arch == CPU_R4650)
5416 {
5417 as_bad (_("opcode not supported on this processor"));
5418 return;
5419 }
5420 s = "sdc1";
5421 coproc = 1;
5422 /* Itbl support may require additional care here. */
5423 goto st;
5424 case M_SDC2_AB:
5425 s = "sdc2";
5426 /* Itbl support may require additional care here. */
5427 coproc = 1;
5428 goto st;
5429 case M_SDC3_AB:
5430 s = "sdc3";
5431 /* Itbl support may require additional care here. */
5432 coproc = 1;
5433 goto st;
5434 case M_SDL_AB:
5435 s = "sdl";
5436 goto st;
5437 case M_SDR_AB:
5438 s = "sdr";
5439 st:
5440 tempreg = AT;
5441 used_at = 1;
5442 ld_st:
5443 /* Itbl support may require additional care here. */
5444 if (mask == M_LWC1_AB
5445 || mask == M_SWC1_AB
5446 || mask == M_LDC1_AB
5447 || mask == M_SDC1_AB
5448 || mask == M_L_DAB
5449 || mask == M_S_DAB)
5450 fmt = "T,o(b)";
5451 else if (coproc)
5452 fmt = "E,o(b)";
5453 else
5454 fmt = "t,o(b)";
5455
5456 /* For embedded PIC, we allow loads where the offset is calculated
5457 by subtracting a symbol in the current segment from an unknown
5458 symbol, relative to a base register, e.g.:
5459 <op> $treg, <sym>-<localsym>($breg)
5460 This is used by the compiler for switch statements. */
5461 if (mips_pic == EMBEDDED_PIC
5462 && offset_expr.X_op == O_subtract
5463 && (symbol_constant_p (offset_expr.X_op_symbol)
5464 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5465 : (symbol_equated_p (offset_expr.X_op_symbol)
5466 && (S_GET_SEGMENT
5467 (symbol_get_value_expression (offset_expr.X_op_symbol)
5468 ->X_add_symbol)
5469 == now_seg)))
5470 && breg != 0
5471 && (offset_expr.X_add_number == 0
5472 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5473 {
5474 /* For this case, we output the instructions:
5475 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5476 addiu $tempreg,$tempreg,$breg
5477 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5478 If the relocation would fit entirely in 16 bits, it would be
5479 nice to emit:
5480 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5481 instead, but that seems quite difficult. */
5482 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5483 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5484 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5485 ((bfd_arch_bits_per_address (stdoutput) == 32
5486 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5487 ? "addu" : "daddu"),
5488 "d,v,t", tempreg, tempreg, breg);
5489 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5490 (int) BFD_RELOC_PCREL_LO16, tempreg);
5491 if (! used_at)
5492 return;
5493 break;
5494 }
5495
5496 if (offset_expr.X_op != O_constant
5497 && offset_expr.X_op != O_symbol)
5498 {
5499 as_bad (_("expression too complex"));
5500 offset_expr.X_op = O_constant;
5501 }
5502
5503 /* A constant expression in PIC code can be handled just as it
5504 is in non PIC code. */
5505 if (mips_pic == NO_PIC
5506 || offset_expr.X_op == O_constant)
5507 {
5508 /* If this is a reference to a GP relative symbol, and there
5509 is no base register, we want
5510 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5511 Otherwise, if there is no base register, we want
5512 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5513 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5514 If we have a constant, we need two instructions anyhow,
5515 so we always use the latter form.
5516
5517 If we have a base register, and this is a reference to a
5518 GP relative symbol, we want
5519 addu $tempreg,$breg,$gp
5520 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5521 Otherwise we want
5522 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5523 addu $tempreg,$tempreg,$breg
5524 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5525 With a constant we always use the latter case.
5526
5527 With 64bit address space and no base register and $at usable,
5528 we want
5529 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5530 lui $at,<sym> (BFD_RELOC_HI16_S)
5531 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5532 dsll32 $tempreg,0
5533 daddu $tempreg,$at
5534 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5535 If we have a base register, we want
5536 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5537 lui $at,<sym> (BFD_RELOC_HI16_S)
5538 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5539 daddu $at,$breg
5540 dsll32 $tempreg,0
5541 daddu $tempreg,$at
5542 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5543
5544 Without $at we can't generate the optimal path for superscalar
5545 processors here since this would require two temporary registers.
5546 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5547 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5548 dsll $tempreg,16
5549 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5550 dsll $tempreg,16
5551 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5552 If we have a base register, we want
5553 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5554 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5555 dsll $tempreg,16
5556 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5557 dsll $tempreg,16
5558 daddu $tempreg,$tempreg,$breg
5559 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5560
5561 If we have 64-bit addresses, as an optimization, for
5562 addresses which are 32-bit constants (e.g. kseg0/kseg1
5563 addresses) we fall back to the 32-bit address generation
5564 mechanism since it is more efficient. This code should
5565 probably attempt to generate 64-bit constants more
5566 efficiently in general.
5567 */
5568 if (HAVE_64BIT_ADDRESSES
5569 && !(offset_expr.X_op == O_constant
5570 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
5571 {
5572 p = NULL;
5573
5574 /* We don't do GP optimization for now because RELAX_ENCODE can't
5575 hold the data for such large chunks. */
5576
5577 if (used_at == 0)
5578 {
5579 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5580 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5581 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5582 AT, (int) BFD_RELOC_HI16_S);
5583 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5584 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5585 if (breg != 0)
5586 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5587 "d,v,t", AT, AT, breg);
5588 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5589 "d,w,<", tempreg, tempreg, 0);
5590 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5591 "d,v,t", tempreg, tempreg, AT);
5592 macro_build (p, &icnt, &offset_expr, s,
5593 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5594 used_at = 1;
5595 }
5596 else
5597 {
5598 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5599 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5600 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5601 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5602 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5603 "d,w,<", tempreg, tempreg, 16);
5604 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5605 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5606 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5607 "d,w,<", tempreg, tempreg, 16);
5608 if (breg != 0)
5609 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5610 "d,v,t", tempreg, tempreg, breg);
5611 macro_build (p, &icnt, &offset_expr, s,
5612 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5613 }
5614
5615 return;
5616 }
5617
5618 if (breg == 0)
5619 {
5620 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5621 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5622 p = NULL;
5623 else
5624 {
5625 frag_grow (20);
5626 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5627 treg, (int) BFD_RELOC_GPREL16, GP);
5628 p = frag_var (rs_machine_dependent, 8, 0,
5629 RELAX_ENCODE (4, 8, 0, 4, 0,
5630 (mips_opts.warn_about_macros
5631 || (used_at
5632 && mips_opts.noat))),
5633 offset_expr.X_add_symbol, 0, NULL);
5634 used_at = 0;
5635 }
5636 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5637 if (p != NULL)
5638 p += 4;
5639 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5640 (int) BFD_RELOC_LO16, tempreg);
5641 }
5642 else
5643 {
5644 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5645 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5646 p = NULL;
5647 else
5648 {
5649 frag_grow (28);
5650 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5651 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5652 "d,v,t", tempreg, breg, GP);
5653 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5654 treg, (int) BFD_RELOC_GPREL16, tempreg);
5655 p = frag_var (rs_machine_dependent, 12, 0,
5656 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5657 offset_expr.X_add_symbol, 0, NULL);
5658 }
5659 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5660 if (p != NULL)
5661 p += 4;
5662 macro_build (p, &icnt, (expressionS *) NULL,
5663 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5664 "d,v,t", tempreg, tempreg, breg);
5665 if (p != NULL)
5666 p += 4;
5667 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5668 (int) BFD_RELOC_LO16, tempreg);
5669 }
5670 }
5671 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5672 {
5673 /* If this is a reference to an external symbol, we want
5674 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5675 nop
5676 <op> $treg,0($tempreg)
5677 Otherwise we want
5678 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5679 nop
5680 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5681 <op> $treg,0($tempreg)
5682 If there is a base register, we add it to $tempreg before
5683 the <op>. If there is a constant, we stick it in the
5684 <op> instruction. We don't handle constants larger than
5685 16 bits, because we have no way to load the upper 16 bits
5686 (actually, we could handle them for the subset of cases
5687 in which we are not using $at). */
5688 assert (offset_expr.X_op == O_symbol);
5689 expr1.X_add_number = offset_expr.X_add_number;
5690 offset_expr.X_add_number = 0;
5691 if (expr1.X_add_number < -0x8000
5692 || expr1.X_add_number >= 0x8000)
5693 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5694 frag_grow (20);
5695 macro_build ((char *) NULL, &icnt, &offset_expr,
5696 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5697 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5699 p = frag_var (rs_machine_dependent, 4, 0,
5700 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5701 offset_expr.X_add_symbol, 0, NULL);
5702 macro_build (p, &icnt, &offset_expr,
5703 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5704 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5705 if (breg != 0)
5706 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5707 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5708 "d,v,t", tempreg, tempreg, breg);
5709 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5710 (int) BFD_RELOC_LO16, tempreg);
5711 }
5712 else if (mips_pic == SVR4_PIC)
5713 {
5714 int gpdel;
5715
5716 /* If this is a reference to an external symbol, we want
5717 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5718 addu $tempreg,$tempreg,$gp
5719 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5720 <op> $treg,0($tempreg)
5721 Otherwise we want
5722 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5723 nop
5724 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5725 <op> $treg,0($tempreg)
5726 If there is a base register, we add it to $tempreg before
5727 the <op>. If there is a constant, we stick it in the
5728 <op> instruction. We don't handle constants larger than
5729 16 bits, because we have no way to load the upper 16 bits
5730 (actually, we could handle them for the subset of cases
5731 in which we are not using $at). */
5732 assert (offset_expr.X_op == O_symbol);
5733 expr1.X_add_number = offset_expr.X_add_number;
5734 offset_expr.X_add_number = 0;
5735 if (expr1.X_add_number < -0x8000
5736 || expr1.X_add_number >= 0x8000)
5737 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5738 if (reg_needs_delay (GP))
5739 gpdel = 4;
5740 else
5741 gpdel = 0;
5742 frag_grow (36);
5743 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5744 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5745 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5746 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5747 "d,v,t", tempreg, tempreg, GP);
5748 macro_build ((char *) NULL, &icnt, &offset_expr,
5749 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5750 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5751 tempreg);
5752 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5753 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5754 offset_expr.X_add_symbol, 0, NULL);
5755 if (gpdel > 0)
5756 {
5757 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5758 p += 4;
5759 }
5760 macro_build (p, &icnt, &offset_expr,
5761 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5762 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5763 p += 4;
5764 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5765 p += 4;
5766 macro_build (p, &icnt, &offset_expr,
5767 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5768 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5769 if (breg != 0)
5770 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5771 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5772 "d,v,t", tempreg, tempreg, breg);
5773 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5774 (int) BFD_RELOC_LO16, tempreg);
5775 }
5776 else if (mips_pic == EMBEDDED_PIC)
5777 {
5778 /* If there is no base register, we want
5779 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5780 If there is a base register, we want
5781 addu $tempreg,$breg,$gp
5782 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5783 */
5784 assert (offset_expr.X_op == O_symbol);
5785 if (breg == 0)
5786 {
5787 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5788 treg, (int) BFD_RELOC_GPREL16, GP);
5789 used_at = 0;
5790 }
5791 else
5792 {
5793 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5794 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5795 "d,v,t", tempreg, breg, GP);
5796 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5797 treg, (int) BFD_RELOC_GPREL16, tempreg);
5798 }
5799 }
5800 else
5801 abort ();
5802
5803 if (! used_at)
5804 return;
5805
5806 break;
5807
5808 case M_LI:
5809 case M_LI_S:
5810 load_register (&icnt, treg, &imm_expr, 0);
5811 return;
5812
5813 case M_DLI:
5814 load_register (&icnt, treg, &imm_expr, 1);
5815 return;
5816
5817 case M_LI_SS:
5818 if (imm_expr.X_op == O_constant)
5819 {
5820 load_register (&icnt, AT, &imm_expr, 0);
5821 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5822 "mtc1", "t,G", AT, treg);
5823 break;
5824 }
5825 else
5826 {
5827 assert (offset_expr.X_op == O_symbol
5828 && strcmp (segment_name (S_GET_SEGMENT
5829 (offset_expr.X_add_symbol)),
5830 ".lit4") == 0
5831 && offset_expr.X_add_number == 0);
5832 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5833 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5834 return;
5835 }
5836
5837 case M_LI_D:
5838 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5839 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5840 order 32 bits of the value and the low order 32 bits are either
5841 zero or in OFFSET_EXPR. */
5842 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5843 {
5844 if (HAVE_64BIT_GPRS)
5845 load_register (&icnt, treg, &imm_expr, 1);
5846 else
5847 {
5848 int hreg, lreg;
5849
5850 if (target_big_endian)
5851 {
5852 hreg = treg;
5853 lreg = treg + 1;
5854 }
5855 else
5856 {
5857 hreg = treg + 1;
5858 lreg = treg;
5859 }
5860
5861 if (hreg <= 31)
5862 load_register (&icnt, hreg, &imm_expr, 0);
5863 if (lreg <= 31)
5864 {
5865 if (offset_expr.X_op == O_absent)
5866 move_register (&icnt, lreg, 0);
5867 else
5868 {
5869 assert (offset_expr.X_op == O_constant);
5870 load_register (&icnt, lreg, &offset_expr, 0);
5871 }
5872 }
5873 }
5874 return;
5875 }
5876
5877 /* We know that sym is in the .rdata section. First we get the
5878 upper 16 bits of the address. */
5879 if (mips_pic == NO_PIC)
5880 {
5881 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5882 }
5883 else if (mips_pic == SVR4_PIC)
5884 {
5885 macro_build ((char *) NULL, &icnt, &offset_expr,
5886 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5887 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5888 }
5889 else if (mips_pic == EMBEDDED_PIC)
5890 {
5891 /* For embedded PIC we pick up the entire address off $gp in
5892 a single instruction. */
5893 macro_build ((char *) NULL, &icnt, &offset_expr,
5894 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5895 "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5896 offset_expr.X_op = O_constant;
5897 offset_expr.X_add_number = 0;
5898 }
5899 else
5900 abort ();
5901
5902 /* Now we load the register(s). */
5903 if (HAVE_64BIT_GPRS)
5904 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5905 treg, (int) BFD_RELOC_LO16, AT);
5906 else
5907 {
5908 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5909 treg, (int) BFD_RELOC_LO16, AT);
5910 if (treg != 31)
5911 {
5912 /* FIXME: How in the world do we deal with the possible
5913 overflow here? */
5914 offset_expr.X_add_number += 4;
5915 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5916 treg + 1, (int) BFD_RELOC_LO16, AT);
5917 }
5918 }
5919
5920 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5921 does not become a variant frag. */
5922 frag_wane (frag_now);
5923 frag_new (0);
5924
5925 break;
5926
5927 case M_LI_DD:
5928 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5929 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5930 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5931 the value and the low order 32 bits are either zero or in
5932 OFFSET_EXPR. */
5933 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5934 {
5935 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5936 if (HAVE_64BIT_FPRS)
5937 {
5938 assert (HAVE_64BIT_GPRS);
5939 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5940 "dmtc1", "t,S", AT, treg);
5941 }
5942 else
5943 {
5944 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5945 "mtc1", "t,G", AT, treg + 1);
5946 if (offset_expr.X_op == O_absent)
5947 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5948 "mtc1", "t,G", 0, treg);
5949 else
5950 {
5951 assert (offset_expr.X_op == O_constant);
5952 load_register (&icnt, AT, &offset_expr, 0);
5953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5954 "mtc1", "t,G", AT, treg);
5955 }
5956 }
5957 break;
5958 }
5959
5960 assert (offset_expr.X_op == O_symbol
5961 && offset_expr.X_add_number == 0);
5962 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5963 if (strcmp (s, ".lit8") == 0)
5964 {
5965 if (mips_opts.isa != ISA_MIPS1)
5966 {
5967 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5968 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5969 return;
5970 }
5971 breg = GP;
5972 r = BFD_RELOC_MIPS_LITERAL;
5973 goto dob;
5974 }
5975 else
5976 {
5977 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5978 if (mips_pic == SVR4_PIC)
5979 macro_build ((char *) NULL, &icnt, &offset_expr,
5980 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5981 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5982 else
5983 {
5984 /* FIXME: This won't work for a 64 bit address. */
5985 macro_build_lui (NULL, &icnt, &offset_expr, AT);
5986 }
5987
5988 if (mips_opts.isa != ISA_MIPS1)
5989 {
5990 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5991 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5992
5993 /* To avoid confusion in tc_gen_reloc, we must ensure
5994 that this does not become a variant frag. */
5995 frag_wane (frag_now);
5996 frag_new (0);
5997
5998 break;
5999 }
6000 breg = AT;
6001 r = BFD_RELOC_LO16;
6002 goto dob;
6003 }
6004
6005 case M_L_DOB:
6006 if (mips_arch == CPU_R4650)
6007 {
6008 as_bad (_("opcode not supported on this processor"));
6009 return;
6010 }
6011 /* Even on a big endian machine $fn comes before $fn+1. We have
6012 to adjust when loading from memory. */
6013 r = BFD_RELOC_LO16;
6014 dob:
6015 assert (mips_opts.isa == ISA_MIPS1);
6016 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6017 target_big_endian ? treg + 1 : treg,
6018 (int) r, breg);
6019 /* FIXME: A possible overflow which I don't know how to deal
6020 with. */
6021 offset_expr.X_add_number += 4;
6022 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6023 target_big_endian ? treg : treg + 1,
6024 (int) r, breg);
6025
6026 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6027 does not become a variant frag. */
6028 frag_wane (frag_now);
6029 frag_new (0);
6030
6031 if (breg != AT)
6032 return;
6033 break;
6034
6035 case M_L_DAB:
6036 /*
6037 * The MIPS assembler seems to check for X_add_number not
6038 * being double aligned and generating:
6039 * lui at,%hi(foo+1)
6040 * addu at,at,v1
6041 * addiu at,at,%lo(foo+1)
6042 * lwc1 f2,0(at)
6043 * lwc1 f3,4(at)
6044 * But, the resulting address is the same after relocation so why
6045 * generate the extra instruction?
6046 */
6047 if (mips_arch == CPU_R4650)
6048 {
6049 as_bad (_("opcode not supported on this processor"));
6050 return;
6051 }
6052 /* Itbl support may require additional care here. */
6053 coproc = 1;
6054 if (mips_opts.isa != ISA_MIPS1)
6055 {
6056 s = "ldc1";
6057 goto ld;
6058 }
6059
6060 s = "lwc1";
6061 fmt = "T,o(b)";
6062 goto ldd_std;
6063
6064 case M_S_DAB:
6065 if (mips_arch == CPU_R4650)
6066 {
6067 as_bad (_("opcode not supported on this processor"));
6068 return;
6069 }
6070
6071 if (mips_opts.isa != ISA_MIPS1)
6072 {
6073 s = "sdc1";
6074 goto st;
6075 }
6076
6077 s = "swc1";
6078 fmt = "T,o(b)";
6079 /* Itbl support may require additional care here. */
6080 coproc = 1;
6081 goto ldd_std;
6082
6083 case M_LD_AB:
6084 if (HAVE_64BIT_GPRS)
6085 {
6086 s = "ld";
6087 goto ld;
6088 }
6089
6090 s = "lw";
6091 fmt = "t,o(b)";
6092 goto ldd_std;
6093
6094 case M_SD_AB:
6095 if (HAVE_64BIT_GPRS)
6096 {
6097 s = "sd";
6098 goto st;
6099 }
6100
6101 s = "sw";
6102 fmt = "t,o(b)";
6103
6104 ldd_std:
6105 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6106 loads for the case of doing a pair of loads to simulate an 'ld'.
6107 This is not currently done by the compiler, and assembly coders
6108 writing embedded-pic code can cope. */
6109
6110 if (offset_expr.X_op != O_symbol
6111 && offset_expr.X_op != O_constant)
6112 {
6113 as_bad (_("expression too complex"));
6114 offset_expr.X_op = O_constant;
6115 }
6116
6117 /* Even on a big endian machine $fn comes before $fn+1. We have
6118 to adjust when loading from memory. We set coproc if we must
6119 load $fn+1 first. */
6120 /* Itbl support may require additional care here. */
6121 if (! target_big_endian)
6122 coproc = 0;
6123
6124 if (mips_pic == NO_PIC
6125 || offset_expr.X_op == O_constant)
6126 {
6127 /* If this is a reference to a GP relative symbol, we want
6128 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6129 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6130 If we have a base register, we use this
6131 addu $at,$breg,$gp
6132 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6133 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6134 If this is not a GP relative symbol, we want
6135 lui $at,<sym> (BFD_RELOC_HI16_S)
6136 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6137 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6138 If there is a base register, we add it to $at after the
6139 lui instruction. If there is a constant, we always use
6140 the last case. */
6141 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6142 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6143 {
6144 p = NULL;
6145 used_at = 1;
6146 }
6147 else
6148 {
6149 int off;
6150
6151 if (breg == 0)
6152 {
6153 frag_grow (28);
6154 tempreg = GP;
6155 off = 0;
6156 used_at = 0;
6157 }
6158 else
6159 {
6160 frag_grow (36);
6161 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6162 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6163 "d,v,t", AT, breg, GP);
6164 tempreg = AT;
6165 off = 4;
6166 used_at = 1;
6167 }
6168
6169 /* Itbl support may require additional care here. */
6170 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6171 coproc ? treg + 1 : treg,
6172 (int) BFD_RELOC_GPREL16, tempreg);
6173 offset_expr.X_add_number += 4;
6174
6175 /* Set mips_optimize to 2 to avoid inserting an
6176 undesired nop. */
6177 hold_mips_optimize = mips_optimize;
6178 mips_optimize = 2;
6179 /* Itbl support may require additional care here. */
6180 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6181 coproc ? treg : treg + 1,
6182 (int) BFD_RELOC_GPREL16, tempreg);
6183 mips_optimize = hold_mips_optimize;
6184
6185 p = frag_var (rs_machine_dependent, 12 + off, 0,
6186 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6187 used_at && mips_opts.noat),
6188 offset_expr.X_add_symbol, 0, NULL);
6189
6190 /* We just generated two relocs. When tc_gen_reloc
6191 handles this case, it will skip the first reloc and
6192 handle the second. The second reloc already has an
6193 extra addend of 4, which we added above. We must
6194 subtract it out, and then subtract another 4 to make
6195 the first reloc come out right. The second reloc
6196 will come out right because we are going to add 4 to
6197 offset_expr when we build its instruction below.
6198
6199 If we have a symbol, then we don't want to include
6200 the offset, because it will wind up being included
6201 when we generate the reloc. */
6202
6203 if (offset_expr.X_op == O_constant)
6204 offset_expr.X_add_number -= 8;
6205 else
6206 {
6207 offset_expr.X_add_number = -4;
6208 offset_expr.X_op = O_constant;
6209 }
6210 }
6211 macro_build_lui (p, &icnt, &offset_expr, AT);
6212 if (p != NULL)
6213 p += 4;
6214 if (breg != 0)
6215 {
6216 macro_build (p, &icnt, (expressionS *) NULL,
6217 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6218 "d,v,t", AT, breg, AT);
6219 if (p != NULL)
6220 p += 4;
6221 }
6222 /* Itbl support may require additional care here. */
6223 macro_build (p, &icnt, &offset_expr, s, fmt,
6224 coproc ? treg + 1 : treg,
6225 (int) BFD_RELOC_LO16, AT);
6226 if (p != NULL)
6227 p += 4;
6228 /* FIXME: How do we handle overflow here? */
6229 offset_expr.X_add_number += 4;
6230 /* Itbl support may require additional care here. */
6231 macro_build (p, &icnt, &offset_expr, s, fmt,
6232 coproc ? treg : treg + 1,
6233 (int) BFD_RELOC_LO16, AT);
6234 }
6235 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6236 {
6237 int off;
6238
6239 /* If this is a reference to an external symbol, we want
6240 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6241 nop
6242 <op> $treg,0($at)
6243 <op> $treg+1,4($at)
6244 Otherwise we want
6245 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6246 nop
6247 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6248 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6249 If there is a base register we add it to $at before the
6250 lwc1 instructions. If there is a constant we include it
6251 in the lwc1 instructions. */
6252 used_at = 1;
6253 expr1.X_add_number = offset_expr.X_add_number;
6254 offset_expr.X_add_number = 0;
6255 if (expr1.X_add_number < -0x8000
6256 || expr1.X_add_number >= 0x8000 - 4)
6257 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6258 if (breg == 0)
6259 off = 0;
6260 else
6261 off = 4;
6262 frag_grow (24 + off);
6263 macro_build ((char *) NULL, &icnt, &offset_expr,
6264 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6265 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6267 if (breg != 0)
6268 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6269 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6270 "d,v,t", AT, breg, AT);
6271 /* Itbl support may require additional care here. */
6272 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6273 coproc ? treg + 1 : treg,
6274 (int) BFD_RELOC_LO16, AT);
6275 expr1.X_add_number += 4;
6276
6277 /* Set mips_optimize to 2 to avoid inserting an undesired
6278 nop. */
6279 hold_mips_optimize = mips_optimize;
6280 mips_optimize = 2;
6281 /* Itbl support may require additional care here. */
6282 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6283 coproc ? treg : treg + 1,
6284 (int) BFD_RELOC_LO16, AT);
6285 mips_optimize = hold_mips_optimize;
6286
6287 (void) frag_var (rs_machine_dependent, 0, 0,
6288 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6289 offset_expr.X_add_symbol, 0, NULL);
6290 }
6291 else if (mips_pic == SVR4_PIC)
6292 {
6293 int gpdel, off;
6294
6295 /* If this is a reference to an external symbol, we want
6296 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6297 addu $at,$at,$gp
6298 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6299 nop
6300 <op> $treg,0($at)
6301 <op> $treg+1,4($at)
6302 Otherwise we want
6303 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6304 nop
6305 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6306 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6307 If there is a base register we add it to $at before the
6308 lwc1 instructions. If there is a constant we include it
6309 in the lwc1 instructions. */
6310 used_at = 1;
6311 expr1.X_add_number = offset_expr.X_add_number;
6312 offset_expr.X_add_number = 0;
6313 if (expr1.X_add_number < -0x8000
6314 || expr1.X_add_number >= 0x8000 - 4)
6315 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6316 if (reg_needs_delay (GP))
6317 gpdel = 4;
6318 else
6319 gpdel = 0;
6320 if (breg == 0)
6321 off = 0;
6322 else
6323 off = 4;
6324 frag_grow (56);
6325 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6326 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6327 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6328 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6329 "d,v,t", AT, AT, GP);
6330 macro_build ((char *) NULL, &icnt, &offset_expr,
6331 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6332 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6333 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6334 if (breg != 0)
6335 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6336 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6337 "d,v,t", AT, breg, AT);
6338 /* Itbl support may require additional care here. */
6339 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6340 coproc ? treg + 1 : treg,
6341 (int) BFD_RELOC_LO16, AT);
6342 expr1.X_add_number += 4;
6343
6344 /* Set mips_optimize to 2 to avoid inserting an undesired
6345 nop. */
6346 hold_mips_optimize = mips_optimize;
6347 mips_optimize = 2;
6348 /* Itbl support may require additional care here. */
6349 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6350 coproc ? treg : treg + 1,
6351 (int) BFD_RELOC_LO16, AT);
6352 mips_optimize = hold_mips_optimize;
6353 expr1.X_add_number -= 4;
6354
6355 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6356 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6357 8 + gpdel + off, 1, 0),
6358 offset_expr.X_add_symbol, 0, NULL);
6359 if (gpdel > 0)
6360 {
6361 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6362 p += 4;
6363 }
6364 macro_build (p, &icnt, &offset_expr,
6365 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6366 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6367 p += 4;
6368 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6369 p += 4;
6370 if (breg != 0)
6371 {
6372 macro_build (p, &icnt, (expressionS *) NULL,
6373 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6374 "d,v,t", AT, breg, AT);
6375 p += 4;
6376 }
6377 /* Itbl support may require additional care here. */
6378 macro_build (p, &icnt, &expr1, s, fmt,
6379 coproc ? treg + 1 : treg,
6380 (int) BFD_RELOC_LO16, AT);
6381 p += 4;
6382 expr1.X_add_number += 4;
6383
6384 /* Set mips_optimize to 2 to avoid inserting an undesired
6385 nop. */
6386 hold_mips_optimize = mips_optimize;
6387 mips_optimize = 2;
6388 /* Itbl support may require additional care here. */
6389 macro_build (p, &icnt, &expr1, s, fmt,
6390 coproc ? treg : treg + 1,
6391 (int) BFD_RELOC_LO16, AT);
6392 mips_optimize = hold_mips_optimize;
6393 }
6394 else if (mips_pic == EMBEDDED_PIC)
6395 {
6396 /* If there is no base register, we use
6397 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6398 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6399 If we have a base register, we use
6400 addu $at,$breg,$gp
6401 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6402 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6403 */
6404 if (breg == 0)
6405 {
6406 tempreg = GP;
6407 used_at = 0;
6408 }
6409 else
6410 {
6411 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6412 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6413 "d,v,t", AT, breg, GP);
6414 tempreg = AT;
6415 used_at = 1;
6416 }
6417
6418 /* Itbl support may require additional care here. */
6419 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6420 coproc ? treg + 1 : treg,
6421 (int) BFD_RELOC_GPREL16, tempreg);
6422 offset_expr.X_add_number += 4;
6423 /* Itbl support may require additional care here. */
6424 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6425 coproc ? treg : treg + 1,
6426 (int) BFD_RELOC_GPREL16, tempreg);
6427 }
6428 else
6429 abort ();
6430
6431 if (! used_at)
6432 return;
6433
6434 break;
6435
6436 case M_LD_OB:
6437 s = "lw";
6438 goto sd_ob;
6439 case M_SD_OB:
6440 s = "sw";
6441 sd_ob:
6442 assert (HAVE_32BIT_ADDRESSES);
6443 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6444 (int) BFD_RELOC_LO16, breg);
6445 offset_expr.X_add_number += 4;
6446 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6447 (int) BFD_RELOC_LO16, breg);
6448 return;
6449
6450 /* New code added to support COPZ instructions.
6451 This code builds table entries out of the macros in mip_opcodes.
6452 R4000 uses interlocks to handle coproc delays.
6453 Other chips (like the R3000) require nops to be inserted for delays.
6454
6455 FIXME: Currently, we require that the user handle delays.
6456 In order to fill delay slots for non-interlocked chips,
6457 we must have a way to specify delays based on the coprocessor.
6458 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6459 What are the side-effects of the cop instruction?
6460 What cache support might we have and what are its effects?
6461 Both coprocessor & memory require delays. how long???
6462 What registers are read/set/modified?
6463
6464 If an itbl is provided to interpret cop instructions,
6465 this knowledge can be encoded in the itbl spec. */
6466
6467 case M_COP0:
6468 s = "c0";
6469 goto copz;
6470 case M_COP1:
6471 s = "c1";
6472 goto copz;
6473 case M_COP2:
6474 s = "c2";
6475 goto copz;
6476 case M_COP3:
6477 s = "c3";
6478 copz:
6479 /* For now we just do C (same as Cz). The parameter will be
6480 stored in insn_opcode by mips_ip. */
6481 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6482 ip->insn_opcode);
6483 return;
6484
6485 case M_MOVE:
6486 move_register (&icnt, dreg, sreg);
6487 return;
6488
6489 #ifdef LOSING_COMPILER
6490 default:
6491 /* Try and see if this is a new itbl instruction.
6492 This code builds table entries out of the macros in mip_opcodes.
6493 FIXME: For now we just assemble the expression and pass it's
6494 value along as a 32-bit immediate.
6495 We may want to have the assembler assemble this value,
6496 so that we gain the assembler's knowledge of delay slots,
6497 symbols, etc.
6498 Would it be more efficient to use mask (id) here? */
6499 if (itbl_have_entries
6500 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6501 {
6502 s = ip->insn_mo->name;
6503 s2 = "cop3";
6504 coproc = ITBL_DECODE_PNUM (immed_expr);;
6505 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6506 return;
6507 }
6508 macro2 (ip);
6509 return;
6510 }
6511 if (mips_opts.noat)
6512 as_warn (_("Macro used $at after \".set noat\""));
6513 }
6514
6515 static void
6516 macro2 (ip)
6517 struct mips_cl_insn *ip;
6518 {
6519 register int treg, sreg, dreg, breg;
6520 int tempreg;
6521 int mask;
6522 int icnt = 0;
6523 int used_at;
6524 expressionS expr1;
6525 const char *s;
6526 const char *s2;
6527 const char *fmt;
6528 int likely = 0;
6529 int dbl = 0;
6530 int coproc = 0;
6531 int lr = 0;
6532 int imm = 0;
6533 int off;
6534 offsetT maxnum;
6535 bfd_reloc_code_real_type r;
6536 char *p;
6537
6538 treg = (ip->insn_opcode >> 16) & 0x1f;
6539 dreg = (ip->insn_opcode >> 11) & 0x1f;
6540 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6541 mask = ip->insn_mo->mask;
6542
6543 expr1.X_op = O_constant;
6544 expr1.X_op_symbol = NULL;
6545 expr1.X_add_symbol = NULL;
6546 expr1.X_add_number = 1;
6547
6548 switch (mask)
6549 {
6550 #endif /* LOSING_COMPILER */
6551
6552 case M_DMUL:
6553 dbl = 1;
6554 case M_MUL:
6555 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6556 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6558 dreg);
6559 return;
6560
6561 case M_DMUL_I:
6562 dbl = 1;
6563 case M_MUL_I:
6564 /* The MIPS assembler some times generates shifts and adds. I'm
6565 not trying to be that fancy. GCC should do this for us
6566 anyway. */
6567 load_register (&icnt, AT, &imm_expr, dbl);
6568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6569 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6570 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6571 dreg);
6572 break;
6573
6574 case M_DMULO_I:
6575 dbl = 1;
6576 case M_MULO_I:
6577 imm = 1;
6578 goto do_mulo;
6579
6580 case M_DMULO:
6581 dbl = 1;
6582 case M_MULO:
6583 do_mulo:
6584 mips_emit_delays (true);
6585 ++mips_opts.noreorder;
6586 mips_any_noreorder = 1;
6587 if (imm)
6588 load_register (&icnt, AT, &imm_expr, dbl);
6589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6590 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6592 dreg);
6593 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6594 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6595 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6596 AT);
6597 if (mips_trap)
6598 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6599 dreg, AT);
6600 else
6601 {
6602 expr1.X_add_number = 8;
6603 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6604 AT);
6605 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6606 0);
6607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6608 "c", 6);
6609 }
6610 --mips_opts.noreorder;
6611 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6612 break;
6613
6614 case M_DMULOU_I:
6615 dbl = 1;
6616 case M_MULOU_I:
6617 imm = 1;
6618 goto do_mulou;
6619
6620 case M_DMULOU:
6621 dbl = 1;
6622 case M_MULOU:
6623 do_mulou:
6624 mips_emit_delays (true);
6625 ++mips_opts.noreorder;
6626 mips_any_noreorder = 1;
6627 if (imm)
6628 load_register (&icnt, AT, &imm_expr, dbl);
6629 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6630 dbl ? "dmultu" : "multu",
6631 "s,t", sreg, imm ? AT : treg);
6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6633 AT);
6634 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6635 dreg);
6636 if (mips_trap)
6637 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6638 AT, 0);
6639 else
6640 {
6641 expr1.X_add_number = 8;
6642 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6644 0);
6645 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6646 "c", 6);
6647 }
6648 --mips_opts.noreorder;
6649 break;
6650
6651 case M_ROL:
6652 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6653 "d,v,t", AT, 0, treg);
6654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6655 "d,t,s", AT, sreg, AT);
6656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6657 "d,t,s", dreg, sreg, treg);
6658 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6659 "d,v,t", dreg, dreg, AT);
6660 break;
6661
6662 case M_ROL_I:
6663 if (imm_expr.X_op != O_constant)
6664 as_bad (_("rotate count too large"));
6665 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6666 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6667 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6668 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6669 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6670 dreg, dreg, AT);
6671 break;
6672
6673 case M_ROR:
6674 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6675 "d,v,t", AT, 0, treg);
6676 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6677 "d,t,s", AT, sreg, AT);
6678 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6679 "d,t,s", dreg, sreg, treg);
6680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6681 "d,v,t", dreg, dreg, AT);
6682 break;
6683
6684 case M_ROR_I:
6685 if (imm_expr.X_op != O_constant)
6686 as_bad (_("rotate count too large"));
6687 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6688 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6689 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6690 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6691 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6692 dreg, dreg, AT);
6693 break;
6694
6695 case M_S_DOB:
6696 if (mips_arch == CPU_R4650)
6697 {
6698 as_bad (_("opcode not supported on this processor"));
6699 return;
6700 }
6701 assert (mips_opts.isa == ISA_MIPS1);
6702 /* Even on a big endian machine $fn comes before $fn+1. We have
6703 to adjust when storing to memory. */
6704 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6705 target_big_endian ? treg + 1 : treg,
6706 (int) BFD_RELOC_LO16, breg);
6707 offset_expr.X_add_number += 4;
6708 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6709 target_big_endian ? treg : treg + 1,
6710 (int) BFD_RELOC_LO16, breg);
6711 return;
6712
6713 case M_SEQ:
6714 if (sreg == 0)
6715 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6716 treg, (int) BFD_RELOC_LO16);
6717 else if (treg == 0)
6718 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6719 sreg, (int) BFD_RELOC_LO16);
6720 else
6721 {
6722 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6723 "d,v,t", dreg, sreg, treg);
6724 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6725 dreg, (int) BFD_RELOC_LO16);
6726 }
6727 return;
6728
6729 case M_SEQ_I:
6730 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6731 {
6732 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6733 sreg, (int) BFD_RELOC_LO16);
6734 return;
6735 }
6736 if (sreg == 0)
6737 {
6738 as_warn (_("Instruction %s: result is always false"),
6739 ip->insn_mo->name);
6740 move_register (&icnt, dreg, 0);
6741 return;
6742 }
6743 if (imm_expr.X_op == O_constant
6744 && imm_expr.X_add_number >= 0
6745 && imm_expr.X_add_number < 0x10000)
6746 {
6747 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6748 sreg, (int) BFD_RELOC_LO16);
6749 used_at = 0;
6750 }
6751 else if (imm_expr.X_op == O_constant
6752 && imm_expr.X_add_number > -0x8000
6753 && imm_expr.X_add_number < 0)
6754 {
6755 imm_expr.X_add_number = -imm_expr.X_add_number;
6756 macro_build ((char *) NULL, &icnt, &imm_expr,
6757 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6758 "t,r,j", dreg, sreg,
6759 (int) BFD_RELOC_LO16);
6760 used_at = 0;
6761 }
6762 else
6763 {
6764 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6765 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6766 "d,v,t", dreg, sreg, AT);
6767 used_at = 1;
6768 }
6769 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6770 (int) BFD_RELOC_LO16);
6771 if (used_at)
6772 break;
6773 return;
6774
6775 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6776 s = "slt";
6777 goto sge;
6778 case M_SGEU:
6779 s = "sltu";
6780 sge:
6781 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6782 dreg, sreg, treg);
6783 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6784 (int) BFD_RELOC_LO16);
6785 return;
6786
6787 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6788 case M_SGEU_I:
6789 if (imm_expr.X_op == O_constant
6790 && imm_expr.X_add_number >= -0x8000
6791 && imm_expr.X_add_number < 0x8000)
6792 {
6793 macro_build ((char *) NULL, &icnt, &imm_expr,
6794 mask == M_SGE_I ? "slti" : "sltiu",
6795 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6796 used_at = 0;
6797 }
6798 else
6799 {
6800 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6801 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6802 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6803 AT);
6804 used_at = 1;
6805 }
6806 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6807 (int) BFD_RELOC_LO16);
6808 if (used_at)
6809 break;
6810 return;
6811
6812 case M_SGT: /* sreg > treg <==> treg < sreg */
6813 s = "slt";
6814 goto sgt;
6815 case M_SGTU:
6816 s = "sltu";
6817 sgt:
6818 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6819 dreg, treg, sreg);
6820 return;
6821
6822 case M_SGT_I: /* sreg > I <==> I < sreg */
6823 s = "slt";
6824 goto sgti;
6825 case M_SGTU_I:
6826 s = "sltu";
6827 sgti:
6828 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6830 dreg, AT, sreg);
6831 break;
6832
6833 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6834 s = "slt";
6835 goto sle;
6836 case M_SLEU:
6837 s = "sltu";
6838 sle:
6839 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6840 dreg, treg, sreg);
6841 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6842 (int) BFD_RELOC_LO16);
6843 return;
6844
6845 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6846 s = "slt";
6847 goto slei;
6848 case M_SLEU_I:
6849 s = "sltu";
6850 slei:
6851 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6852 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6853 dreg, AT, sreg);
6854 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6855 (int) BFD_RELOC_LO16);
6856 break;
6857
6858 case M_SLT_I:
6859 if (imm_expr.X_op == O_constant
6860 && imm_expr.X_add_number >= -0x8000
6861 && imm_expr.X_add_number < 0x8000)
6862 {
6863 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6864 dreg, sreg, (int) BFD_RELOC_LO16);
6865 return;
6866 }
6867 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6868 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6869 dreg, sreg, AT);
6870 break;
6871
6872 case M_SLTU_I:
6873 if (imm_expr.X_op == O_constant
6874 && imm_expr.X_add_number >= -0x8000
6875 && imm_expr.X_add_number < 0x8000)
6876 {
6877 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6878 dreg, sreg, (int) BFD_RELOC_LO16);
6879 return;
6880 }
6881 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6882 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6883 "d,v,t", dreg, sreg, AT);
6884 break;
6885
6886 case M_SNE:
6887 if (sreg == 0)
6888 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6889 "d,v,t", dreg, 0, treg);
6890 else if (treg == 0)
6891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6892 "d,v,t", dreg, 0, sreg);
6893 else
6894 {
6895 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6896 "d,v,t", dreg, sreg, treg);
6897 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6898 "d,v,t", dreg, 0, dreg);
6899 }
6900 return;
6901
6902 case M_SNE_I:
6903 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6904 {
6905 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6906 "d,v,t", dreg, 0, sreg);
6907 return;
6908 }
6909 if (sreg == 0)
6910 {
6911 as_warn (_("Instruction %s: result is always true"),
6912 ip->insn_mo->name);
6913 macro_build ((char *) NULL, &icnt, &expr1,
6914 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6915 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6916 return;
6917 }
6918 if (imm_expr.X_op == O_constant
6919 && imm_expr.X_add_number >= 0
6920 && imm_expr.X_add_number < 0x10000)
6921 {
6922 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6923 dreg, sreg, (int) BFD_RELOC_LO16);
6924 used_at = 0;
6925 }
6926 else if (imm_expr.X_op == O_constant
6927 && imm_expr.X_add_number > -0x8000
6928 && imm_expr.X_add_number < 0)
6929 {
6930 imm_expr.X_add_number = -imm_expr.X_add_number;
6931 macro_build ((char *) NULL, &icnt, &imm_expr,
6932 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6933 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6934 used_at = 0;
6935 }
6936 else
6937 {
6938 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
6939 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6940 "d,v,t", dreg, sreg, AT);
6941 used_at = 1;
6942 }
6943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6944 "d,v,t", dreg, 0, dreg);
6945 if (used_at)
6946 break;
6947 return;
6948
6949 case M_DSUB_I:
6950 dbl = 1;
6951 case M_SUB_I:
6952 if (imm_expr.X_op == O_constant
6953 && imm_expr.X_add_number > -0x8000
6954 && imm_expr.X_add_number <= 0x8000)
6955 {
6956 imm_expr.X_add_number = -imm_expr.X_add_number;
6957 macro_build ((char *) NULL, &icnt, &imm_expr,
6958 dbl ? "daddi" : "addi",
6959 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6960 return;
6961 }
6962 load_register (&icnt, AT, &imm_expr, dbl);
6963 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6964 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6965 break;
6966
6967 case M_DSUBU_I:
6968 dbl = 1;
6969 case M_SUBU_I:
6970 if (imm_expr.X_op == O_constant
6971 && imm_expr.X_add_number > -0x8000
6972 && imm_expr.X_add_number <= 0x8000)
6973 {
6974 imm_expr.X_add_number = -imm_expr.X_add_number;
6975 macro_build ((char *) NULL, &icnt, &imm_expr,
6976 dbl ? "daddiu" : "addiu",
6977 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6978 return;
6979 }
6980 load_register (&icnt, AT, &imm_expr, dbl);
6981 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6982 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6983 break;
6984
6985 case M_TEQ_I:
6986 s = "teq";
6987 goto trap;
6988 case M_TGE_I:
6989 s = "tge";
6990 goto trap;
6991 case M_TGEU_I:
6992 s = "tgeu";
6993 goto trap;
6994 case M_TLT_I:
6995 s = "tlt";
6996 goto trap;
6997 case M_TLTU_I:
6998 s = "tltu";
6999 goto trap;
7000 case M_TNE_I:
7001 s = "tne";
7002 trap:
7003 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
7004 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7005 AT);
7006 break;
7007
7008 case M_TRUNCWS:
7009 case M_TRUNCWD:
7010 assert (mips_opts.isa == ISA_MIPS1);
7011 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7012 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7013
7014 /*
7015 * Is the double cfc1 instruction a bug in the mips assembler;
7016 * or is there a reason for it?
7017 */
7018 mips_emit_delays (true);
7019 ++mips_opts.noreorder;
7020 mips_any_noreorder = 1;
7021 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7022 treg, 31);
7023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7024 treg, 31);
7025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7026 expr1.X_add_number = 3;
7027 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7028 (int) BFD_RELOC_LO16);
7029 expr1.X_add_number = 2;
7030 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7031 (int) BFD_RELOC_LO16);
7032 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7033 AT, 31);
7034 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7035 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7036 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
7037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7038 treg, 31);
7039 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7040 --mips_opts.noreorder;
7041 break;
7042
7043 case M_ULH:
7044 s = "lb";
7045 goto ulh;
7046 case M_ULHU:
7047 s = "lbu";
7048 ulh:
7049 if (offset_expr.X_add_number >= 0x7fff)
7050 as_bad (_("operand overflow"));
7051 /* avoid load delay */
7052 if (! target_big_endian)
7053 offset_expr.X_add_number += 1;
7054 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7055 (int) BFD_RELOC_LO16, breg);
7056 if (! target_big_endian)
7057 offset_expr.X_add_number -= 1;
7058 else
7059 offset_expr.X_add_number += 1;
7060 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7061 (int) BFD_RELOC_LO16, breg);
7062 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7063 treg, treg, 8);
7064 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7065 treg, treg, AT);
7066 break;
7067
7068 case M_ULD:
7069 s = "ldl";
7070 s2 = "ldr";
7071 off = 7;
7072 goto ulw;
7073 case M_ULW:
7074 s = "lwl";
7075 s2 = "lwr";
7076 off = 3;
7077 ulw:
7078 if (offset_expr.X_add_number >= 0x8000 - off)
7079 as_bad (_("operand overflow"));
7080 if (! target_big_endian)
7081 offset_expr.X_add_number += off;
7082 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7083 (int) BFD_RELOC_LO16, breg);
7084 if (! target_big_endian)
7085 offset_expr.X_add_number -= off;
7086 else
7087 offset_expr.X_add_number += off;
7088 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7089 (int) BFD_RELOC_LO16, breg);
7090 return;
7091
7092 case M_ULD_A:
7093 s = "ldl";
7094 s2 = "ldr";
7095 off = 7;
7096 goto ulwa;
7097 case M_ULW_A:
7098 s = "lwl";
7099 s2 = "lwr";
7100 off = 3;
7101 ulwa:
7102 used_at = 1;
7103 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7104 if (breg != 0)
7105 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7106 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7107 "d,v,t", AT, AT, breg);
7108 if (! target_big_endian)
7109 expr1.X_add_number = off;
7110 else
7111 expr1.X_add_number = 0;
7112 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7113 (int) BFD_RELOC_LO16, AT);
7114 if (! target_big_endian)
7115 expr1.X_add_number = 0;
7116 else
7117 expr1.X_add_number = off;
7118 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7119 (int) BFD_RELOC_LO16, AT);
7120 break;
7121
7122 case M_ULH_A:
7123 case M_ULHU_A:
7124 used_at = 1;
7125 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7126 if (breg != 0)
7127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7128 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7129 "d,v,t", AT, AT, breg);
7130 if (target_big_endian)
7131 expr1.X_add_number = 0;
7132 macro_build ((char *) NULL, &icnt, &expr1,
7133 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7134 (int) BFD_RELOC_LO16, AT);
7135 if (target_big_endian)
7136 expr1.X_add_number = 1;
7137 else
7138 expr1.X_add_number = 0;
7139 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7140 (int) BFD_RELOC_LO16, AT);
7141 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7142 treg, treg, 8);
7143 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7144 treg, treg, AT);
7145 break;
7146
7147 case M_USH:
7148 if (offset_expr.X_add_number >= 0x7fff)
7149 as_bad (_("operand overflow"));
7150 if (target_big_endian)
7151 offset_expr.X_add_number += 1;
7152 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7153 (int) BFD_RELOC_LO16, breg);
7154 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7155 AT, treg, 8);
7156 if (target_big_endian)
7157 offset_expr.X_add_number -= 1;
7158 else
7159 offset_expr.X_add_number += 1;
7160 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7161 (int) BFD_RELOC_LO16, breg);
7162 break;
7163
7164 case M_USD:
7165 s = "sdl";
7166 s2 = "sdr";
7167 off = 7;
7168 goto usw;
7169 case M_USW:
7170 s = "swl";
7171 s2 = "swr";
7172 off = 3;
7173 usw:
7174 if (offset_expr.X_add_number >= 0x8000 - off)
7175 as_bad (_("operand overflow"));
7176 if (! target_big_endian)
7177 offset_expr.X_add_number += off;
7178 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7179 (int) BFD_RELOC_LO16, breg);
7180 if (! target_big_endian)
7181 offset_expr.X_add_number -= off;
7182 else
7183 offset_expr.X_add_number += off;
7184 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7185 (int) BFD_RELOC_LO16, breg);
7186 return;
7187
7188 case M_USD_A:
7189 s = "sdl";
7190 s2 = "sdr";
7191 off = 7;
7192 goto uswa;
7193 case M_USW_A:
7194 s = "swl";
7195 s2 = "swr";
7196 off = 3;
7197 uswa:
7198 used_at = 1;
7199 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7200 if (breg != 0)
7201 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7202 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7203 "d,v,t", AT, AT, breg);
7204 if (! target_big_endian)
7205 expr1.X_add_number = off;
7206 else
7207 expr1.X_add_number = 0;
7208 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7209 (int) BFD_RELOC_LO16, AT);
7210 if (! target_big_endian)
7211 expr1.X_add_number = 0;
7212 else
7213 expr1.X_add_number = off;
7214 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7215 (int) BFD_RELOC_LO16, AT);
7216 break;
7217
7218 case M_USH_A:
7219 used_at = 1;
7220 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7221 if (breg != 0)
7222 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7223 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7224 "d,v,t", AT, AT, breg);
7225 if (! target_big_endian)
7226 expr1.X_add_number = 0;
7227 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7228 (int) BFD_RELOC_LO16, AT);
7229 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7230 treg, treg, 8);
7231 if (! target_big_endian)
7232 expr1.X_add_number = 1;
7233 else
7234 expr1.X_add_number = 0;
7235 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7236 (int) BFD_RELOC_LO16, AT);
7237 if (! target_big_endian)
7238 expr1.X_add_number = 0;
7239 else
7240 expr1.X_add_number = 1;
7241 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7242 (int) BFD_RELOC_LO16, AT);
7243 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7244 treg, treg, 8);
7245 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7246 treg, treg, AT);
7247 break;
7248
7249 default:
7250 /* FIXME: Check if this is one of the itbl macros, since they
7251 are added dynamically. */
7252 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7253 break;
7254 }
7255 if (mips_opts.noat)
7256 as_warn (_("Macro used $at after \".set noat\""));
7257 }
7258
7259 /* Implement macros in mips16 mode. */
7260
7261 static void
7262 mips16_macro (ip)
7263 struct mips_cl_insn *ip;
7264 {
7265 int mask;
7266 int xreg, yreg, zreg, tmp;
7267 int icnt;
7268 expressionS expr1;
7269 int dbl;
7270 const char *s, *s2, *s3;
7271
7272 mask = ip->insn_mo->mask;
7273
7274 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7275 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7276 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7277
7278 icnt = 0;
7279
7280 expr1.X_op = O_constant;
7281 expr1.X_op_symbol = NULL;
7282 expr1.X_add_symbol = NULL;
7283 expr1.X_add_number = 1;
7284
7285 dbl = 0;
7286
7287 switch (mask)
7288 {
7289 default:
7290 internalError ();
7291
7292 case M_DDIV_3:
7293 dbl = 1;
7294 case M_DIV_3:
7295 s = "mflo";
7296 goto do_div3;
7297 case M_DREM_3:
7298 dbl = 1;
7299 case M_REM_3:
7300 s = "mfhi";
7301 do_div3:
7302 mips_emit_delays (true);
7303 ++mips_opts.noreorder;
7304 mips_any_noreorder = 1;
7305 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7306 dbl ? "ddiv" : "div",
7307 "0,x,y", xreg, yreg);
7308 expr1.X_add_number = 2;
7309 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7310 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7311 7);
7312
7313 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7314 since that causes an overflow. We should do that as well,
7315 but I don't see how to do the comparisons without a temporary
7316 register. */
7317 --mips_opts.noreorder;
7318 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7319 break;
7320
7321 case M_DIVU_3:
7322 s = "divu";
7323 s2 = "mflo";
7324 goto do_divu3;
7325 case M_REMU_3:
7326 s = "divu";
7327 s2 = "mfhi";
7328 goto do_divu3;
7329 case M_DDIVU_3:
7330 s = "ddivu";
7331 s2 = "mflo";
7332 goto do_divu3;
7333 case M_DREMU_3:
7334 s = "ddivu";
7335 s2 = "mfhi";
7336 do_divu3:
7337 mips_emit_delays (true);
7338 ++mips_opts.noreorder;
7339 mips_any_noreorder = 1;
7340 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7341 xreg, yreg);
7342 expr1.X_add_number = 2;
7343 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7344 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7345 "6", 7);
7346 --mips_opts.noreorder;
7347 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7348 break;
7349
7350 case M_DMUL:
7351 dbl = 1;
7352 case M_MUL:
7353 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7354 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7355 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7356 zreg);
7357 return;
7358
7359 case M_DSUBU_I:
7360 dbl = 1;
7361 goto do_subu;
7362 case M_SUBU_I:
7363 do_subu:
7364 if (imm_expr.X_op != O_constant)
7365 as_bad (_("Unsupported large constant"));
7366 imm_expr.X_add_number = -imm_expr.X_add_number;
7367 macro_build ((char *) NULL, &icnt, &imm_expr,
7368 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7369 break;
7370
7371 case M_SUBU_I_2:
7372 if (imm_expr.X_op != O_constant)
7373 as_bad (_("Unsupported large constant"));
7374 imm_expr.X_add_number = -imm_expr.X_add_number;
7375 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7376 "x,k", xreg);
7377 break;
7378
7379 case M_DSUBU_I_2:
7380 if (imm_expr.X_op != O_constant)
7381 as_bad (_("Unsupported large constant"));
7382 imm_expr.X_add_number = -imm_expr.X_add_number;
7383 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7384 "y,j", yreg);
7385 break;
7386
7387 case M_BEQ:
7388 s = "cmp";
7389 s2 = "bteqz";
7390 goto do_branch;
7391 case M_BNE:
7392 s = "cmp";
7393 s2 = "btnez";
7394 goto do_branch;
7395 case M_BLT:
7396 s = "slt";
7397 s2 = "btnez";
7398 goto do_branch;
7399 case M_BLTU:
7400 s = "sltu";
7401 s2 = "btnez";
7402 goto do_branch;
7403 case M_BLE:
7404 s = "slt";
7405 s2 = "bteqz";
7406 goto do_reverse_branch;
7407 case M_BLEU:
7408 s = "sltu";
7409 s2 = "bteqz";
7410 goto do_reverse_branch;
7411 case M_BGE:
7412 s = "slt";
7413 s2 = "bteqz";
7414 goto do_branch;
7415 case M_BGEU:
7416 s = "sltu";
7417 s2 = "bteqz";
7418 goto do_branch;
7419 case M_BGT:
7420 s = "slt";
7421 s2 = "btnez";
7422 goto do_reverse_branch;
7423 case M_BGTU:
7424 s = "sltu";
7425 s2 = "btnez";
7426
7427 do_reverse_branch:
7428 tmp = xreg;
7429 xreg = yreg;
7430 yreg = tmp;
7431
7432 do_branch:
7433 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7434 xreg, yreg);
7435 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7436 break;
7437
7438 case M_BEQ_I:
7439 s = "cmpi";
7440 s2 = "bteqz";
7441 s3 = "x,U";
7442 goto do_branch_i;
7443 case M_BNE_I:
7444 s = "cmpi";
7445 s2 = "btnez";
7446 s3 = "x,U";
7447 goto do_branch_i;
7448 case M_BLT_I:
7449 s = "slti";
7450 s2 = "btnez";
7451 s3 = "x,8";
7452 goto do_branch_i;
7453 case M_BLTU_I:
7454 s = "sltiu";
7455 s2 = "btnez";
7456 s3 = "x,8";
7457 goto do_branch_i;
7458 case M_BLE_I:
7459 s = "slti";
7460 s2 = "btnez";
7461 s3 = "x,8";
7462 goto do_addone_branch_i;
7463 case M_BLEU_I:
7464 s = "sltiu";
7465 s2 = "btnez";
7466 s3 = "x,8";
7467 goto do_addone_branch_i;
7468 case M_BGE_I:
7469 s = "slti";
7470 s2 = "bteqz";
7471 s3 = "x,8";
7472 goto do_branch_i;
7473 case M_BGEU_I:
7474 s = "sltiu";
7475 s2 = "bteqz";
7476 s3 = "x,8";
7477 goto do_branch_i;
7478 case M_BGT_I:
7479 s = "slti";
7480 s2 = "bteqz";
7481 s3 = "x,8";
7482 goto do_addone_branch_i;
7483 case M_BGTU_I:
7484 s = "sltiu";
7485 s2 = "bteqz";
7486 s3 = "x,8";
7487
7488 do_addone_branch_i:
7489 if (imm_expr.X_op != O_constant)
7490 as_bad (_("Unsupported large constant"));
7491 ++imm_expr.X_add_number;
7492
7493 do_branch_i:
7494 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7495 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7496 break;
7497
7498 case M_ABS:
7499 expr1.X_add_number = 0;
7500 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7501 if (xreg != yreg)
7502 move_register (&icnt, xreg, yreg);
7503 expr1.X_add_number = 2;
7504 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7505 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7506 "neg", "x,w", xreg, xreg);
7507 }
7508 }
7509
7510 /* For consistency checking, verify that all bits are specified either
7511 by the match/mask part of the instruction definition, or by the
7512 operand list. */
7513 static int
7514 validate_mips_insn (opc)
7515 const struct mips_opcode *opc;
7516 {
7517 const char *p = opc->args;
7518 char c;
7519 unsigned long used_bits = opc->mask;
7520
7521 if ((used_bits & opc->match) != opc->match)
7522 {
7523 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7524 opc->name, opc->args);
7525 return 0;
7526 }
7527 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7528 while (*p)
7529 switch (c = *p++)
7530 {
7531 case ',': break;
7532 case '(': break;
7533 case ')': break;
7534 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7535 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7536 case 'A': break;
7537 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7538 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7539 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7540 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7541 case 'F': break;
7542 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7543 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7544 case 'I': break;
7545 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7546 case 'L': break;
7547 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7548 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7549 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7550 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7551 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7552 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7553 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7554 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7555 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7556 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7557 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7558 case 'f': break;
7559 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7560 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7561 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7562 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7563 case 'l': break;
7564 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7565 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7566 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7567 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7568 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7569 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7570 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7571 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7572 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7573 case 'x': break;
7574 case 'z': break;
7575 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7576 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7577 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7578 default:
7579 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7580 c, opc->name, opc->args);
7581 return 0;
7582 }
7583 #undef USE_BITS
7584 if (used_bits != 0xffffffff)
7585 {
7586 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7587 ~used_bits & 0xffffffff, opc->name, opc->args);
7588 return 0;
7589 }
7590 return 1;
7591 }
7592
7593 /* This routine assembles an instruction into its binary format. As a
7594 side effect, it sets one of the global variables imm_reloc or
7595 offset_reloc to the type of relocation to do if one of the operands
7596 is an address expression. */
7597
7598 static void
7599 mips_ip (str, ip)
7600 char *str;
7601 struct mips_cl_insn *ip;
7602 {
7603 char *s;
7604 const char *args;
7605 char c = 0;
7606 struct mips_opcode *insn;
7607 char *argsStart;
7608 unsigned int regno;
7609 unsigned int lastregno = 0;
7610 char *s_reset;
7611 char save_c = 0;
7612 int full_opcode_match = 1;
7613
7614 insn_error = NULL;
7615
7616 /* If the instruction contains a '.', we first try to match an instruction
7617 including the '.'. Then we try again without the '.'. */
7618 insn = NULL;
7619 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7620 continue;
7621
7622 /* If we stopped on whitespace, then replace the whitespace with null for
7623 the call to hash_find. Save the character we replaced just in case we
7624 have to re-parse the instruction. */
7625 if (ISSPACE (*s))
7626 {
7627 save_c = *s;
7628 *s++ = '\0';
7629 }
7630
7631 insn = (struct mips_opcode *) hash_find (op_hash, str);
7632
7633 /* If we didn't find the instruction in the opcode table, try again, but
7634 this time with just the instruction up to, but not including the
7635 first '.'. */
7636 if (insn == NULL)
7637 {
7638 /* Restore the character we overwrite above (if any). */
7639 if (save_c)
7640 *(--s) = save_c;
7641
7642 /* Scan up to the first '.' or whitespace. */
7643 for (s = str;
7644 *s != '\0' && *s != '.' && !ISSPACE (*s);
7645 ++s)
7646 continue;
7647
7648 /* If we did not find a '.', then we can quit now. */
7649 if (*s != '.')
7650 {
7651 insn_error = "unrecognized opcode";
7652 return;
7653 }
7654
7655 /* Lookup the instruction in the hash table. */
7656 *s++ = '\0';
7657 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7658 {
7659 insn_error = "unrecognized opcode";
7660 return;
7661 }
7662
7663 full_opcode_match = 0;
7664 }
7665
7666 argsStart = s;
7667 for (;;)
7668 {
7669 boolean ok;
7670
7671 assert (strcmp (insn->name, str) == 0);
7672
7673 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7674 ok = true;
7675 else
7676 ok = false;
7677
7678 if (insn->pinfo != INSN_MACRO)
7679 {
7680 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7681 ok = false;
7682 }
7683
7684 if (! ok)
7685 {
7686 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7687 && strcmp (insn->name, insn[1].name) == 0)
7688 {
7689 ++insn;
7690 continue;
7691 }
7692 else
7693 {
7694 if (!insn_error)
7695 {
7696 static char buf[100];
7697 sprintf (buf,
7698 _("opcode not supported on this processor: %s (%s)"),
7699 mips_cpu_to_str (mips_arch),
7700 mips_isa_to_str (mips_opts.isa));
7701
7702 insn_error = buf;
7703 }
7704 if (save_c)
7705 *(--s) = save_c;
7706 return;
7707 }
7708 }
7709
7710 ip->insn_mo = insn;
7711 ip->insn_opcode = insn->match;
7712 insn_error = NULL;
7713 for (args = insn->args;; ++args)
7714 {
7715 s += strspn (s, " \t");
7716 switch (*args)
7717 {
7718 case '\0': /* end of args */
7719 if (*s == '\0')
7720 return;
7721 break;
7722
7723 case ',':
7724 if (*s++ == *args)
7725 continue;
7726 s--;
7727 switch (*++args)
7728 {
7729 case 'r':
7730 case 'v':
7731 ip->insn_opcode |= lastregno << OP_SH_RS;
7732 continue;
7733
7734 case 'w':
7735 ip->insn_opcode |= lastregno << OP_SH_RT;
7736 continue;
7737
7738 case 'W':
7739 ip->insn_opcode |= lastregno << OP_SH_FT;
7740 continue;
7741
7742 case 'V':
7743 ip->insn_opcode |= lastregno << OP_SH_FS;
7744 continue;
7745 }
7746 break;
7747
7748 case '(':
7749 /* Handle optional base register.
7750 Either the base register is omitted or
7751 we must have a left paren. */
7752 /* This is dependent on the next operand specifier
7753 is a base register specification. */
7754 assert (args[1] == 'b' || args[1] == '5'
7755 || args[1] == '-' || args[1] == '4');
7756 if (*s == '\0')
7757 return;
7758
7759 case ')': /* these must match exactly */
7760 if (*s++ == *args)
7761 continue;
7762 break;
7763
7764 case '<': /* must be at least one digit */
7765 /*
7766 * According to the manual, if the shift amount is greater
7767 * than 31 or less than 0, then the shift amount should be
7768 * mod 32. In reality the mips assembler issues an error.
7769 * We issue a warning and mask out all but the low 5 bits.
7770 */
7771 my_getExpression (&imm_expr, s);
7772 check_absolute_expr (ip, &imm_expr);
7773 if ((unsigned long) imm_expr.X_add_number > 31)
7774 {
7775 as_warn (_("Improper shift amount (%ld)"),
7776 (long) imm_expr.X_add_number);
7777 imm_expr.X_add_number &= OP_MASK_SHAMT;
7778 }
7779 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7780 imm_expr.X_op = O_absent;
7781 s = expr_end;
7782 continue;
7783
7784 case '>': /* shift amount minus 32 */
7785 my_getExpression (&imm_expr, s);
7786 check_absolute_expr (ip, &imm_expr);
7787 if ((unsigned long) imm_expr.X_add_number < 32
7788 || (unsigned long) imm_expr.X_add_number > 63)
7789 break;
7790 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7791 imm_expr.X_op = O_absent;
7792 s = expr_end;
7793 continue;
7794
7795 case 'k': /* cache code */
7796 case 'h': /* prefx code */
7797 my_getExpression (&imm_expr, s);
7798 check_absolute_expr (ip, &imm_expr);
7799 if ((unsigned long) imm_expr.X_add_number > 31)
7800 {
7801 as_warn (_("Invalid value for `%s' (%lu)"),
7802 ip->insn_mo->name,
7803 (unsigned long) imm_expr.X_add_number);
7804 imm_expr.X_add_number &= 0x1f;
7805 }
7806 if (*args == 'k')
7807 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7808 else
7809 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7810 imm_expr.X_op = O_absent;
7811 s = expr_end;
7812 continue;
7813
7814 case 'c': /* break code */
7815 my_getExpression (&imm_expr, s);
7816 check_absolute_expr (ip, &imm_expr);
7817 if ((unsigned) imm_expr.X_add_number > 1023)
7818 {
7819 as_warn (_("Illegal break code (%ld)"),
7820 (long) imm_expr.X_add_number);
7821 imm_expr.X_add_number &= OP_MASK_CODE;
7822 }
7823 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7824 imm_expr.X_op = O_absent;
7825 s = expr_end;
7826 continue;
7827
7828 case 'q': /* lower break code */
7829 my_getExpression (&imm_expr, s);
7830 check_absolute_expr (ip, &imm_expr);
7831 if ((unsigned) imm_expr.X_add_number > 1023)
7832 {
7833 as_warn (_("Illegal lower break code (%ld)"),
7834 (long) imm_expr.X_add_number);
7835 imm_expr.X_add_number &= OP_MASK_CODE2;
7836 }
7837 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7838 imm_expr.X_op = O_absent;
7839 s = expr_end;
7840 continue;
7841
7842 case 'B': /* 20-bit syscall/break code. */
7843 my_getExpression (&imm_expr, s);
7844 check_absolute_expr (ip, &imm_expr);
7845 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7846 as_warn (_("Illegal 20-bit code (%ld)"),
7847 (long) imm_expr.X_add_number);
7848 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7849 imm_expr.X_op = O_absent;
7850 s = expr_end;
7851 continue;
7852
7853 case 'C': /* Coprocessor code */
7854 my_getExpression (&imm_expr, s);
7855 check_absolute_expr (ip, &imm_expr);
7856 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7857 {
7858 as_warn (_("Coproccesor code > 25 bits (%ld)"),
7859 (long) imm_expr.X_add_number);
7860 imm_expr.X_add_number &= ((1<<25) - 1);
7861 }
7862 ip->insn_opcode |= imm_expr.X_add_number;
7863 imm_expr.X_op = O_absent;
7864 s = expr_end;
7865 continue;
7866
7867 case 'J': /* 19-bit wait code. */
7868 my_getExpression (&imm_expr, s);
7869 check_absolute_expr (ip, &imm_expr);
7870 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7871 as_warn (_("Illegal 19-bit code (%ld)"),
7872 (long) imm_expr.X_add_number);
7873 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7874 imm_expr.X_op = O_absent;
7875 s = expr_end;
7876 continue;
7877
7878 case 'P': /* Performance register */
7879 my_getExpression (&imm_expr, s);
7880 check_absolute_expr (ip, &imm_expr);
7881 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7882 {
7883 as_warn (_("Invalid performance register (%ld)"),
7884 (long) imm_expr.X_add_number);
7885 imm_expr.X_add_number &= OP_MASK_PERFREG;
7886 }
7887 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7888 imm_expr.X_op = O_absent;
7889 s = expr_end;
7890 continue;
7891
7892 case 'b': /* base register */
7893 case 'd': /* destination register */
7894 case 's': /* source register */
7895 case 't': /* target register */
7896 case 'r': /* both target and source */
7897 case 'v': /* both dest and source */
7898 case 'w': /* both dest and target */
7899 case 'E': /* coprocessor target register */
7900 case 'G': /* coprocessor destination register */
7901 case 'x': /* ignore register name */
7902 case 'z': /* must be zero register */
7903 case 'U': /* destination register (clo/clz). */
7904 s_reset = s;
7905 if (s[0] == '$')
7906 {
7907
7908 if (ISDIGIT (s[1]))
7909 {
7910 ++s;
7911 regno = 0;
7912 do
7913 {
7914 regno *= 10;
7915 regno += *s - '0';
7916 ++s;
7917 }
7918 while (ISDIGIT (*s));
7919 if (regno > 31)
7920 as_bad (_("Invalid register number (%d)"), regno);
7921 }
7922 else if (*args == 'E' || *args == 'G')
7923 goto notreg;
7924 else
7925 {
7926 if (s[1] == 'f' && s[2] == 'p')
7927 {
7928 s += 3;
7929 regno = FP;
7930 }
7931 else if (s[1] == 's' && s[2] == 'p')
7932 {
7933 s += 3;
7934 regno = SP;
7935 }
7936 else if (s[1] == 'g' && s[2] == 'p')
7937 {
7938 s += 3;
7939 regno = GP;
7940 }
7941 else if (s[1] == 'a' && s[2] == 't')
7942 {
7943 s += 3;
7944 regno = AT;
7945 }
7946 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7947 {
7948 s += 4;
7949 regno = KT0;
7950 }
7951 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7952 {
7953 s += 4;
7954 regno = KT1;
7955 }
7956 else if (itbl_have_entries)
7957 {
7958 char *p, *n;
7959 unsigned long r;
7960
7961 p = s + 1; /* advance past '$' */
7962 n = itbl_get_field (&p); /* n is name */
7963
7964 /* See if this is a register defined in an
7965 itbl entry. */
7966 if (itbl_get_reg_val (n, &r))
7967 {
7968 /* Get_field advances to the start of
7969 the next field, so we need to back
7970 rack to the end of the last field. */
7971 if (p)
7972 s = p - 1;
7973 else
7974 s = strchr (s, '\0');
7975 regno = r;
7976 }
7977 else
7978 goto notreg;
7979 }
7980 else
7981 goto notreg;
7982 }
7983 if (regno == AT
7984 && ! mips_opts.noat
7985 && *args != 'E'
7986 && *args != 'G')
7987 as_warn (_("Used $at without \".set noat\""));
7988 c = *args;
7989 if (*s == ' ')
7990 s++;
7991 if (args[1] != *s)
7992 {
7993 if (c == 'r' || c == 'v' || c == 'w')
7994 {
7995 regno = lastregno;
7996 s = s_reset;
7997 args++;
7998 }
7999 }
8000 /* 'z' only matches $0. */
8001 if (c == 'z' && regno != 0)
8002 break;
8003
8004 /* Now that we have assembled one operand, we use the args string
8005 * to figure out where it goes in the instruction. */
8006 switch (c)
8007 {
8008 case 'r':
8009 case 's':
8010 case 'v':
8011 case 'b':
8012 ip->insn_opcode |= regno << OP_SH_RS;
8013 break;
8014 case 'd':
8015 case 'G':
8016 ip->insn_opcode |= regno << OP_SH_RD;
8017 break;
8018 case 'U':
8019 ip->insn_opcode |= regno << OP_SH_RD;
8020 ip->insn_opcode |= regno << OP_SH_RT;
8021 break;
8022 case 'w':
8023 case 't':
8024 case 'E':
8025 ip->insn_opcode |= regno << OP_SH_RT;
8026 break;
8027 case 'x':
8028 /* This case exists because on the r3000 trunc
8029 expands into a macro which requires a gp
8030 register. On the r6000 or r4000 it is
8031 assembled into a single instruction which
8032 ignores the register. Thus the insn version
8033 is MIPS_ISA2 and uses 'x', and the macro
8034 version is MIPS_ISA1 and uses 't'. */
8035 break;
8036 case 'z':
8037 /* This case is for the div instruction, which
8038 acts differently if the destination argument
8039 is $0. This only matches $0, and is checked
8040 outside the switch. */
8041 break;
8042 case 'D':
8043 /* Itbl operand; not yet implemented. FIXME ?? */
8044 break;
8045 /* What about all other operands like 'i', which
8046 can be specified in the opcode table? */
8047 }
8048 lastregno = regno;
8049 continue;
8050 }
8051 notreg:
8052 switch (*args++)
8053 {
8054 case 'r':
8055 case 'v':
8056 ip->insn_opcode |= lastregno << OP_SH_RS;
8057 continue;
8058 case 'w':
8059 ip->insn_opcode |= lastregno << OP_SH_RT;
8060 continue;
8061 }
8062 break;
8063
8064 case 'D': /* floating point destination register */
8065 case 'S': /* floating point source register */
8066 case 'T': /* floating point target register */
8067 case 'R': /* floating point source register */
8068 case 'V':
8069 case 'W':
8070 s_reset = s;
8071 if (s[0] == '$' && s[1] == 'f'
8072 && ISDIGIT (s[2]))
8073 {
8074 s += 2;
8075 regno = 0;
8076 do
8077 {
8078 regno *= 10;
8079 regno += *s - '0';
8080 ++s;
8081 }
8082 while (ISDIGIT (*s));
8083
8084 if (regno > 31)
8085 as_bad (_("Invalid float register number (%d)"), regno);
8086
8087 if ((regno & 1) != 0
8088 && HAVE_32BIT_FPRS
8089 && ! (strcmp (str, "mtc1") == 0
8090 || strcmp (str, "mfc1") == 0
8091 || strcmp (str, "lwc1") == 0
8092 || strcmp (str, "swc1") == 0
8093 || strcmp (str, "l.s") == 0
8094 || strcmp (str, "s.s") == 0))
8095 as_warn (_("Float register should be even, was %d"),
8096 regno);
8097
8098 c = *args;
8099 if (*s == ' ')
8100 s++;
8101 if (args[1] != *s)
8102 {
8103 if (c == 'V' || c == 'W')
8104 {
8105 regno = lastregno;
8106 s = s_reset;
8107 args++;
8108 }
8109 }
8110 switch (c)
8111 {
8112 case 'D':
8113 ip->insn_opcode |= regno << OP_SH_FD;
8114 break;
8115 case 'V':
8116 case 'S':
8117 ip->insn_opcode |= regno << OP_SH_FS;
8118 break;
8119 case 'W':
8120 case 'T':
8121 ip->insn_opcode |= regno << OP_SH_FT;
8122 break;
8123 case 'R':
8124 ip->insn_opcode |= regno << OP_SH_FR;
8125 break;
8126 }
8127 lastregno = regno;
8128 continue;
8129 }
8130
8131 switch (*args++)
8132 {
8133 case 'V':
8134 ip->insn_opcode |= lastregno << OP_SH_FS;
8135 continue;
8136 case 'W':
8137 ip->insn_opcode |= lastregno << OP_SH_FT;
8138 continue;
8139 }
8140 break;
8141
8142 case 'I':
8143 my_getExpression (&imm_expr, s);
8144 if (imm_expr.X_op != O_big
8145 && imm_expr.X_op != O_constant)
8146 insn_error = _("absolute expression required");
8147 s = expr_end;
8148 continue;
8149
8150 case 'A':
8151 my_getExpression (&offset_expr, s);
8152 *imm_reloc = BFD_RELOC_32;
8153 s = expr_end;
8154 continue;
8155
8156 case 'F':
8157 case 'L':
8158 case 'f':
8159 case 'l':
8160 {
8161 int f64;
8162 int using_gprs;
8163 char *save_in;
8164 char *err;
8165 unsigned char temp[8];
8166 int len;
8167 unsigned int length;
8168 segT seg;
8169 subsegT subseg;
8170 char *p;
8171
8172 /* These only appear as the last operand in an
8173 instruction, and every instruction that accepts
8174 them in any variant accepts them in all variants.
8175 This means we don't have to worry about backing out
8176 any changes if the instruction does not match.
8177
8178 The difference between them is the size of the
8179 floating point constant and where it goes. For 'F'
8180 and 'L' the constant is 64 bits; for 'f' and 'l' it
8181 is 32 bits. Where the constant is placed is based
8182 on how the MIPS assembler does things:
8183 F -- .rdata
8184 L -- .lit8
8185 f -- immediate value
8186 l -- .lit4
8187
8188 The .lit4 and .lit8 sections are only used if
8189 permitted by the -G argument.
8190
8191 When generating embedded PIC code, we use the
8192 .lit8 section but not the .lit4 section (we can do
8193 .lit4 inline easily; we need to put .lit8
8194 somewhere in the data segment, and using .lit8
8195 permits the linker to eventually combine identical
8196 .lit8 entries).
8197
8198 The code below needs to know whether the target register
8199 is 32 or 64 bits wide. It relies on the fact 'f' and
8200 'F' are used with GPR-based instructions and 'l' and
8201 'L' are used with FPR-based instructions. */
8202
8203 f64 = *args == 'F' || *args == 'L';
8204 using_gprs = *args == 'F' || *args == 'f';
8205
8206 save_in = input_line_pointer;
8207 input_line_pointer = s;
8208 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8209 length = len;
8210 s = input_line_pointer;
8211 input_line_pointer = save_in;
8212 if (err != NULL && *err != '\0')
8213 {
8214 as_bad (_("Bad floating point constant: %s"), err);
8215 memset (temp, '\0', sizeof temp);
8216 length = f64 ? 8 : 4;
8217 }
8218
8219 assert (length == (unsigned) (f64 ? 8 : 4));
8220
8221 if (*args == 'f'
8222 || (*args == 'l'
8223 && (! USE_GLOBAL_POINTER_OPT
8224 || mips_pic == EMBEDDED_PIC
8225 || g_switch_value < 4
8226 || (temp[0] == 0 && temp[1] == 0)
8227 || (temp[2] == 0 && temp[3] == 0))))
8228 {
8229 imm_expr.X_op = O_constant;
8230 if (! target_big_endian)
8231 imm_expr.X_add_number = bfd_getl32 (temp);
8232 else
8233 imm_expr.X_add_number = bfd_getb32 (temp);
8234 }
8235 else if (length > 4
8236 && ! mips_disable_float_construction
8237 /* Constants can only be constructed in GPRs and
8238 copied to FPRs if the GPRs are at least as wide
8239 as the FPRs. Force the constant into memory if
8240 we are using 64-bit FPRs but the GPRs are only
8241 32 bits wide. */
8242 && (using_gprs
8243 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8244 && ((temp[0] == 0 && temp[1] == 0)
8245 || (temp[2] == 0 && temp[3] == 0))
8246 && ((temp[4] == 0 && temp[5] == 0)
8247 || (temp[6] == 0 && temp[7] == 0)))
8248 {
8249 /* The value is simple enough to load with a couple of
8250 instructions. If using 32-bit registers, set
8251 imm_expr to the high order 32 bits and offset_expr to
8252 the low order 32 bits. Otherwise, set imm_expr to
8253 the entire 64 bit constant. */
8254 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8255 {
8256 imm_expr.X_op = O_constant;
8257 offset_expr.X_op = O_constant;
8258 if (! target_big_endian)
8259 {
8260 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8261 offset_expr.X_add_number = bfd_getl32 (temp);
8262 }
8263 else
8264 {
8265 imm_expr.X_add_number = bfd_getb32 (temp);
8266 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8267 }
8268 if (offset_expr.X_add_number == 0)
8269 offset_expr.X_op = O_absent;
8270 }
8271 else if (sizeof (imm_expr.X_add_number) > 4)
8272 {
8273 imm_expr.X_op = O_constant;
8274 if (! target_big_endian)
8275 imm_expr.X_add_number = bfd_getl64 (temp);
8276 else
8277 imm_expr.X_add_number = bfd_getb64 (temp);
8278 }
8279 else
8280 {
8281 imm_expr.X_op = O_big;
8282 imm_expr.X_add_number = 4;
8283 if (! target_big_endian)
8284 {
8285 generic_bignum[0] = bfd_getl16 (temp);
8286 generic_bignum[1] = bfd_getl16 (temp + 2);
8287 generic_bignum[2] = bfd_getl16 (temp + 4);
8288 generic_bignum[3] = bfd_getl16 (temp + 6);
8289 }
8290 else
8291 {
8292 generic_bignum[0] = bfd_getb16 (temp + 6);
8293 generic_bignum[1] = bfd_getb16 (temp + 4);
8294 generic_bignum[2] = bfd_getb16 (temp + 2);
8295 generic_bignum[3] = bfd_getb16 (temp);
8296 }
8297 }
8298 }
8299 else
8300 {
8301 const char *newname;
8302 segT new_seg;
8303
8304 /* Switch to the right section. */
8305 seg = now_seg;
8306 subseg = now_subseg;
8307 switch (*args)
8308 {
8309 default: /* unused default case avoids warnings. */
8310 case 'L':
8311 newname = RDATA_SECTION_NAME;
8312 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8313 || mips_pic == EMBEDDED_PIC)
8314 newname = ".lit8";
8315 break;
8316 case 'F':
8317 if (mips_pic == EMBEDDED_PIC)
8318 newname = ".lit8";
8319 else
8320 newname = RDATA_SECTION_NAME;
8321 break;
8322 case 'l':
8323 assert (!USE_GLOBAL_POINTER_OPT
8324 || g_switch_value >= 4);
8325 newname = ".lit4";
8326 break;
8327 }
8328 new_seg = subseg_new (newname, (subsegT) 0);
8329 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8330 bfd_set_section_flags (stdoutput, new_seg,
8331 (SEC_ALLOC
8332 | SEC_LOAD
8333 | SEC_READONLY
8334 | SEC_DATA));
8335 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8336 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8337 && strcmp (TARGET_OS, "elf") != 0)
8338 record_alignment (new_seg, 4);
8339 else
8340 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8341 if (seg == now_seg)
8342 as_bad (_("Can't use floating point insn in this section"));
8343
8344 /* Set the argument to the current address in the
8345 section. */
8346 offset_expr.X_op = O_symbol;
8347 offset_expr.X_add_symbol =
8348 symbol_new ("L0\001", now_seg,
8349 (valueT) frag_now_fix (), frag_now);
8350 offset_expr.X_add_number = 0;
8351
8352 /* Put the floating point number into the section. */
8353 p = frag_more ((int) length);
8354 memcpy (p, temp, length);
8355
8356 /* Switch back to the original section. */
8357 subseg_set (seg, subseg);
8358 }
8359 }
8360 continue;
8361
8362 case 'i': /* 16 bit unsigned immediate */
8363 case 'j': /* 16 bit signed immediate */
8364 *imm_reloc = BFD_RELOC_LO16;
8365 c = my_getSmallExpression (&imm_expr, s);
8366 if (c != S_EX_NONE)
8367 {
8368 if (c != S_EX_LO)
8369 {
8370 if (imm_expr.X_op == O_constant)
8371 imm_expr.X_add_number =
8372 (imm_expr.X_add_number >> 16) & 0xffff;
8373 #ifdef OBJ_ELF
8374 else if (c == S_EX_HIGHEST)
8375 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8376 else if (c == S_EX_HIGHER)
8377 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8378 else if (c == S_EX_GP_REL)
8379 {
8380 /* This occurs in NewABI only. */
8381 c = my_getSmallExpression (&imm_expr, s);
8382 if (c != S_EX_NEG)
8383 as_bad (_("bad composition of relocations"));
8384 else
8385 {
8386 c = my_getSmallExpression (&imm_expr, s);
8387 if (c != S_EX_LO)
8388 as_bad (_("bad composition of relocations"));
8389 else
8390 {
8391 imm_reloc[0] = BFD_RELOC_GPREL16;
8392 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8393 imm_reloc[2] = BFD_RELOC_LO16;
8394 }
8395 }
8396 }
8397 #endif
8398 else if (c == S_EX_HI)
8399 {
8400 *imm_reloc = BFD_RELOC_HI16_S;
8401 imm_unmatched_hi = true;
8402 }
8403 else
8404 *imm_reloc = BFD_RELOC_HI16;
8405 }
8406 else if (imm_expr.X_op == O_constant)
8407 imm_expr.X_add_number &= 0xffff;
8408 }
8409 if (*args == 'i')
8410 {
8411 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8412 || ((imm_expr.X_add_number < 0
8413 || imm_expr.X_add_number >= 0x10000)
8414 && imm_expr.X_op == O_constant))
8415 {
8416 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8417 !strcmp (insn->name, insn[1].name))
8418 break;
8419 if (imm_expr.X_op == O_constant
8420 || imm_expr.X_op == O_big)
8421 as_bad (_("16 bit expression not in range 0..65535"));
8422 }
8423 }
8424 else
8425 {
8426 int more;
8427 offsetT max;
8428
8429 /* The upper bound should be 0x8000, but
8430 unfortunately the MIPS assembler accepts numbers
8431 from 0x8000 to 0xffff and sign extends them, and
8432 we want to be compatible. We only permit this
8433 extended range for an instruction which does not
8434 provide any further alternates, since those
8435 alternates may handle other cases. People should
8436 use the numbers they mean, rather than relying on
8437 a mysterious sign extension. */
8438 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8439 strcmp (insn->name, insn[1].name) == 0);
8440 if (more)
8441 max = 0x8000;
8442 else
8443 max = 0x10000;
8444 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8445 || ((imm_expr.X_add_number < -0x8000
8446 || imm_expr.X_add_number >= max)
8447 && imm_expr.X_op == O_constant)
8448 || (more
8449 && imm_expr.X_add_number < 0
8450 && HAVE_64BIT_GPRS
8451 && imm_expr.X_unsigned
8452 && sizeof (imm_expr.X_add_number) <= 4))
8453 {
8454 if (more)
8455 break;
8456 if (imm_expr.X_op == O_constant
8457 || imm_expr.X_op == O_big)
8458 as_bad (_("16 bit expression not in range -32768..32767"));
8459 }
8460 }
8461 s = expr_end;
8462 continue;
8463
8464 case 'o': /* 16 bit offset */
8465 c = my_getSmallExpression (&offset_expr, s);
8466
8467 /* If this value won't fit into a 16 bit offset, then go
8468 find a macro that will generate the 32 bit offset
8469 code pattern. */
8470 if (c == S_EX_NONE
8471 && (offset_expr.X_op != O_constant
8472 || offset_expr.X_add_number >= 0x8000
8473 || offset_expr.X_add_number < -0x8000))
8474 break;
8475
8476 if (c == S_EX_HI)
8477 {
8478 if (offset_expr.X_op != O_constant)
8479 break;
8480 offset_expr.X_add_number =
8481 (offset_expr.X_add_number >> 16) & 0xffff;
8482 }
8483 *offset_reloc = BFD_RELOC_LO16;
8484 s = expr_end;
8485 continue;
8486
8487 case 'p': /* pc relative offset */
8488 if (mips_pic == EMBEDDED_PIC)
8489 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8490 else
8491 *offset_reloc = BFD_RELOC_16_PCREL;
8492 my_getExpression (&offset_expr, s);
8493 s = expr_end;
8494 continue;
8495
8496 case 'u': /* upper 16 bits */
8497 c = my_getSmallExpression (&imm_expr, s);
8498 *imm_reloc = BFD_RELOC_LO16;
8499 if (c != S_EX_NONE)
8500 {
8501 if (c != S_EX_LO)
8502 {
8503 if (imm_expr.X_op == O_constant)
8504 imm_expr.X_add_number =
8505 (imm_expr.X_add_number >> 16) & 0xffff;
8506 else if (c == S_EX_HI)
8507 {
8508 *imm_reloc = BFD_RELOC_HI16_S;
8509 imm_unmatched_hi = true;
8510 }
8511 #ifdef OBJ_ELF
8512 else if (c == S_EX_HIGHEST)
8513 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8514 else if (c == S_EX_GP_REL)
8515 {
8516 /* This occurs in NewABI only. */
8517 c = my_getSmallExpression (&imm_expr, s);
8518 if (c != S_EX_NEG)
8519 as_bad (_("bad composition of relocations"));
8520 else
8521 {
8522 c = my_getSmallExpression (&imm_expr, s);
8523 if (c != S_EX_HI)
8524 as_bad (_("bad composition of relocations"));
8525 else
8526 {
8527 imm_reloc[0] = BFD_RELOC_GPREL16;
8528 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8529 imm_reloc[2] = BFD_RELOC_HI16_S;
8530 }
8531 }
8532 }
8533 #endif
8534 else
8535 *imm_reloc = BFD_RELOC_HI16;
8536 }
8537 else if (imm_expr.X_op == O_constant)
8538 imm_expr.X_add_number &= 0xffff;
8539 }
8540 if (imm_expr.X_op == O_constant
8541 && (imm_expr.X_add_number < 0
8542 || imm_expr.X_add_number >= 0x10000))
8543 as_bad (_("lui expression not in range 0..65535"));
8544 s = expr_end;
8545 continue;
8546
8547 case 'a': /* 26 bit address */
8548 my_getExpression (&offset_expr, s);
8549 s = expr_end;
8550 *offset_reloc = BFD_RELOC_MIPS_JMP;
8551 continue;
8552
8553 case 'N': /* 3 bit branch condition code */
8554 case 'M': /* 3 bit compare condition code */
8555 if (strncmp (s, "$fcc", 4) != 0)
8556 break;
8557 s += 4;
8558 regno = 0;
8559 do
8560 {
8561 regno *= 10;
8562 regno += *s - '0';
8563 ++s;
8564 }
8565 while (ISDIGIT (*s));
8566 if (regno > 7)
8567 as_bad (_("invalid condition code register $fcc%d"), regno);
8568 if (*args == 'N')
8569 ip->insn_opcode |= regno << OP_SH_BCC;
8570 else
8571 ip->insn_opcode |= regno << OP_SH_CCC;
8572 continue;
8573
8574 case 'H':
8575 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8576 s += 2;
8577 if (ISDIGIT (*s))
8578 {
8579 c = 0;
8580 do
8581 {
8582 c *= 10;
8583 c += *s - '0';
8584 ++s;
8585 }
8586 while (ISDIGIT (*s));
8587 }
8588 else
8589 c = 8; /* Invalid sel value. */
8590
8591 if (c > 7)
8592 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8593 ip->insn_opcode |= c;
8594 continue;
8595
8596 default:
8597 as_bad (_("bad char = '%c'\n"), *args);
8598 internalError ();
8599 }
8600 break;
8601 }
8602 /* Args don't match. */
8603 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8604 !strcmp (insn->name, insn[1].name))
8605 {
8606 ++insn;
8607 s = argsStart;
8608 insn_error = _("illegal operands");
8609 continue;
8610 }
8611 if (save_c)
8612 *(--s) = save_c;
8613 insn_error = _("illegal operands");
8614 return;
8615 }
8616 }
8617
8618 /* This routine assembles an instruction into its binary format when
8619 assembling for the mips16. As a side effect, it sets one of the
8620 global variables imm_reloc or offset_reloc to the type of
8621 relocation to do if one of the operands is an address expression.
8622 It also sets mips16_small and mips16_ext if the user explicitly
8623 requested a small or extended instruction. */
8624
8625 static void
8626 mips16_ip (str, ip)
8627 char *str;
8628 struct mips_cl_insn *ip;
8629 {
8630 char *s;
8631 const char *args;
8632 struct mips_opcode *insn;
8633 char *argsstart;
8634 unsigned int regno;
8635 unsigned int lastregno = 0;
8636 char *s_reset;
8637
8638 insn_error = NULL;
8639
8640 mips16_small = false;
8641 mips16_ext = false;
8642
8643 for (s = str; ISLOWER (*s); ++s)
8644 ;
8645 switch (*s)
8646 {
8647 case '\0':
8648 break;
8649
8650 case ' ':
8651 *s++ = '\0';
8652 break;
8653
8654 case '.':
8655 if (s[1] == 't' && s[2] == ' ')
8656 {
8657 *s = '\0';
8658 mips16_small = true;
8659 s += 3;
8660 break;
8661 }
8662 else if (s[1] == 'e' && s[2] == ' ')
8663 {
8664 *s = '\0';
8665 mips16_ext = true;
8666 s += 3;
8667 break;
8668 }
8669 /* Fall through. */
8670 default:
8671 insn_error = _("unknown opcode");
8672 return;
8673 }
8674
8675 if (mips_opts.noautoextend && ! mips16_ext)
8676 mips16_small = true;
8677
8678 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8679 {
8680 insn_error = _("unrecognized opcode");
8681 return;
8682 }
8683
8684 argsstart = s;
8685 for (;;)
8686 {
8687 assert (strcmp (insn->name, str) == 0);
8688
8689 ip->insn_mo = insn;
8690 ip->insn_opcode = insn->match;
8691 ip->use_extend = false;
8692 imm_expr.X_op = O_absent;
8693 imm_reloc[0] = BFD_RELOC_UNUSED;
8694 imm_reloc[1] = BFD_RELOC_UNUSED;
8695 imm_reloc[2] = BFD_RELOC_UNUSED;
8696 offset_expr.X_op = O_absent;
8697 offset_reloc[0] = BFD_RELOC_UNUSED;
8698 offset_reloc[1] = BFD_RELOC_UNUSED;
8699 offset_reloc[2] = BFD_RELOC_UNUSED;
8700 for (args = insn->args; 1; ++args)
8701 {
8702 int c;
8703
8704 if (*s == ' ')
8705 ++s;
8706
8707 /* In this switch statement we call break if we did not find
8708 a match, continue if we did find a match, or return if we
8709 are done. */
8710
8711 c = *args;
8712 switch (c)
8713 {
8714 case '\0':
8715 if (*s == '\0')
8716 {
8717 /* Stuff the immediate value in now, if we can. */
8718 if (imm_expr.X_op == O_constant
8719 && *imm_reloc > BFD_RELOC_UNUSED
8720 && insn->pinfo != INSN_MACRO)
8721 {
8722 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8723 imm_expr.X_add_number, true, mips16_small,
8724 mips16_ext, &ip->insn_opcode,
8725 &ip->use_extend, &ip->extend);
8726 imm_expr.X_op = O_absent;
8727 *imm_reloc = BFD_RELOC_UNUSED;
8728 }
8729
8730 return;
8731 }
8732 break;
8733
8734 case ',':
8735 if (*s++ == c)
8736 continue;
8737 s--;
8738 switch (*++args)
8739 {
8740 case 'v':
8741 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8742 continue;
8743 case 'w':
8744 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8745 continue;
8746 }
8747 break;
8748
8749 case '(':
8750 case ')':
8751 if (*s++ == c)
8752 continue;
8753 break;
8754
8755 case 'v':
8756 case 'w':
8757 if (s[0] != '$')
8758 {
8759 if (c == 'v')
8760 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8761 else
8762 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8763 ++args;
8764 continue;
8765 }
8766 /* Fall through. */
8767 case 'x':
8768 case 'y':
8769 case 'z':
8770 case 'Z':
8771 case '0':
8772 case 'S':
8773 case 'R':
8774 case 'X':
8775 case 'Y':
8776 if (s[0] != '$')
8777 break;
8778 s_reset = s;
8779 if (ISDIGIT (s[1]))
8780 {
8781 ++s;
8782 regno = 0;
8783 do
8784 {
8785 regno *= 10;
8786 regno += *s - '0';
8787 ++s;
8788 }
8789 while (ISDIGIT (*s));
8790 if (regno > 31)
8791 {
8792 as_bad (_("invalid register number (%d)"), regno);
8793 regno = 2;
8794 }
8795 }
8796 else
8797 {
8798 if (s[1] == 'f' && s[2] == 'p')
8799 {
8800 s += 3;
8801 regno = FP;
8802 }
8803 else if (s[1] == 's' && s[2] == 'p')
8804 {
8805 s += 3;
8806 regno = SP;
8807 }
8808 else if (s[1] == 'g' && s[2] == 'p')
8809 {
8810 s += 3;
8811 regno = GP;
8812 }
8813 else if (s[1] == 'a' && s[2] == 't')
8814 {
8815 s += 3;
8816 regno = AT;
8817 }
8818 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8819 {
8820 s += 4;
8821 regno = KT0;
8822 }
8823 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8824 {
8825 s += 4;
8826 regno = KT1;
8827 }
8828 else
8829 break;
8830 }
8831
8832 if (*s == ' ')
8833 ++s;
8834 if (args[1] != *s)
8835 {
8836 if (c == 'v' || c == 'w')
8837 {
8838 regno = mips16_to_32_reg_map[lastregno];
8839 s = s_reset;
8840 args++;
8841 }
8842 }
8843
8844 switch (c)
8845 {
8846 case 'x':
8847 case 'y':
8848 case 'z':
8849 case 'v':
8850 case 'w':
8851 case 'Z':
8852 regno = mips32_to_16_reg_map[regno];
8853 break;
8854
8855 case '0':
8856 if (regno != 0)
8857 regno = ILLEGAL_REG;
8858 break;
8859
8860 case 'S':
8861 if (regno != SP)
8862 regno = ILLEGAL_REG;
8863 break;
8864
8865 case 'R':
8866 if (regno != RA)
8867 regno = ILLEGAL_REG;
8868 break;
8869
8870 case 'X':
8871 case 'Y':
8872 if (regno == AT && ! mips_opts.noat)
8873 as_warn (_("used $at without \".set noat\""));
8874 break;
8875
8876 default:
8877 internalError ();
8878 }
8879
8880 if (regno == ILLEGAL_REG)
8881 break;
8882
8883 switch (c)
8884 {
8885 case 'x':
8886 case 'v':
8887 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8888 break;
8889 case 'y':
8890 case 'w':
8891 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8892 break;
8893 case 'z':
8894 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8895 break;
8896 case 'Z':
8897 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8898 case '0':
8899 case 'S':
8900 case 'R':
8901 break;
8902 case 'X':
8903 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8904 break;
8905 case 'Y':
8906 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8907 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8908 break;
8909 default:
8910 internalError ();
8911 }
8912
8913 lastregno = regno;
8914 continue;
8915
8916 case 'P':
8917 if (strncmp (s, "$pc", 3) == 0)
8918 {
8919 s += 3;
8920 continue;
8921 }
8922 break;
8923
8924 case '<':
8925 case '>':
8926 case '[':
8927 case ']':
8928 case '4':
8929 case '5':
8930 case 'H':
8931 case 'W':
8932 case 'D':
8933 case 'j':
8934 case '8':
8935 case 'V':
8936 case 'C':
8937 case 'U':
8938 case 'k':
8939 case 'K':
8940 if (s[0] == '%'
8941 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8942 {
8943 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8944 and generate the appropriate reloc. If the text
8945 inside %gprel is not a symbol name with an
8946 optional offset, then we generate a normal reloc
8947 and will probably fail later. */
8948 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8949 if (imm_expr.X_op == O_symbol)
8950 {
8951 mips16_ext = true;
8952 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8953 s = expr_end;
8954 ip->use_extend = true;
8955 ip->extend = 0;
8956 continue;
8957 }
8958 }
8959 else
8960 {
8961 /* Just pick up a normal expression. */
8962 my_getExpression (&imm_expr, s);
8963 }
8964
8965 if (imm_expr.X_op == O_register)
8966 {
8967 /* What we thought was an expression turned out to
8968 be a register. */
8969
8970 if (s[0] == '(' && args[1] == '(')
8971 {
8972 /* It looks like the expression was omitted
8973 before a register indirection, which means
8974 that the expression is implicitly zero. We
8975 still set up imm_expr, so that we handle
8976 explicit extensions correctly. */
8977 imm_expr.X_op = O_constant;
8978 imm_expr.X_add_number = 0;
8979 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8980 continue;
8981 }
8982
8983 break;
8984 }
8985
8986 /* We need to relax this instruction. */
8987 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8988 s = expr_end;
8989 continue;
8990
8991 case 'p':
8992 case 'q':
8993 case 'A':
8994 case 'B':
8995 case 'E':
8996 /* We use offset_reloc rather than imm_reloc for the PC
8997 relative operands. This lets macros with both
8998 immediate and address operands work correctly. */
8999 my_getExpression (&offset_expr, s);
9000
9001 if (offset_expr.X_op == O_register)
9002 break;
9003
9004 /* We need to relax this instruction. */
9005 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
9006 s = expr_end;
9007 continue;
9008
9009 case '6': /* break code */
9010 my_getExpression (&imm_expr, s);
9011 check_absolute_expr (ip, &imm_expr);
9012 if ((unsigned long) imm_expr.X_add_number > 63)
9013 {
9014 as_warn (_("Invalid value for `%s' (%lu)"),
9015 ip->insn_mo->name,
9016 (unsigned long) imm_expr.X_add_number);
9017 imm_expr.X_add_number &= 0x3f;
9018 }
9019 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9020 imm_expr.X_op = O_absent;
9021 s = expr_end;
9022 continue;
9023
9024 case 'a': /* 26 bit address */
9025 my_getExpression (&offset_expr, s);
9026 s = expr_end;
9027 *offset_reloc = BFD_RELOC_MIPS16_JMP;
9028 ip->insn_opcode <<= 16;
9029 continue;
9030
9031 case 'l': /* register list for entry macro */
9032 case 'L': /* register list for exit macro */
9033 {
9034 int mask;
9035
9036 if (c == 'l')
9037 mask = 0;
9038 else
9039 mask = 7 << 3;
9040 while (*s != '\0')
9041 {
9042 int freg, reg1, reg2;
9043
9044 while (*s == ' ' || *s == ',')
9045 ++s;
9046 if (*s != '$')
9047 {
9048 as_bad (_("can't parse register list"));
9049 break;
9050 }
9051 ++s;
9052 if (*s != 'f')
9053 freg = 0;
9054 else
9055 {
9056 freg = 1;
9057 ++s;
9058 }
9059 reg1 = 0;
9060 while (ISDIGIT (*s))
9061 {
9062 reg1 *= 10;
9063 reg1 += *s - '0';
9064 ++s;
9065 }
9066 if (*s == ' ')
9067 ++s;
9068 if (*s != '-')
9069 reg2 = reg1;
9070 else
9071 {
9072 ++s;
9073 if (*s != '$')
9074 break;
9075 ++s;
9076 if (freg)
9077 {
9078 if (*s == 'f')
9079 ++s;
9080 else
9081 {
9082 as_bad (_("invalid register list"));
9083 break;
9084 }
9085 }
9086 reg2 = 0;
9087 while (ISDIGIT (*s))
9088 {
9089 reg2 *= 10;
9090 reg2 += *s - '0';
9091 ++s;
9092 }
9093 }
9094 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9095 {
9096 mask &= ~ (7 << 3);
9097 mask |= 5 << 3;
9098 }
9099 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9100 {
9101 mask &= ~ (7 << 3);
9102 mask |= 6 << 3;
9103 }
9104 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9105 mask |= (reg2 - 3) << 3;
9106 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9107 mask |= (reg2 - 15) << 1;
9108 else if (reg1 == 31 && reg2 == 31)
9109 mask |= 1;
9110 else
9111 {
9112 as_bad (_("invalid register list"));
9113 break;
9114 }
9115 }
9116 /* The mask is filled in in the opcode table for the
9117 benefit of the disassembler. We remove it before
9118 applying the actual mask. */
9119 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9120 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9121 }
9122 continue;
9123
9124 case 'e': /* extend code */
9125 my_getExpression (&imm_expr, s);
9126 check_absolute_expr (ip, &imm_expr);
9127 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9128 {
9129 as_warn (_("Invalid value for `%s' (%lu)"),
9130 ip->insn_mo->name,
9131 (unsigned long) imm_expr.X_add_number);
9132 imm_expr.X_add_number &= 0x7ff;
9133 }
9134 ip->insn_opcode |= imm_expr.X_add_number;
9135 imm_expr.X_op = O_absent;
9136 s = expr_end;
9137 continue;
9138
9139 default:
9140 internalError ();
9141 }
9142 break;
9143 }
9144
9145 /* Args don't match. */
9146 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9147 strcmp (insn->name, insn[1].name) == 0)
9148 {
9149 ++insn;
9150 s = argsstart;
9151 continue;
9152 }
9153
9154 insn_error = _("illegal operands");
9155
9156 return;
9157 }
9158 }
9159
9160 /* This structure holds information we know about a mips16 immediate
9161 argument type. */
9162
9163 struct mips16_immed_operand
9164 {
9165 /* The type code used in the argument string in the opcode table. */
9166 int type;
9167 /* The number of bits in the short form of the opcode. */
9168 int nbits;
9169 /* The number of bits in the extended form of the opcode. */
9170 int extbits;
9171 /* The amount by which the short form is shifted when it is used;
9172 for example, the sw instruction has a shift count of 2. */
9173 int shift;
9174 /* The amount by which the short form is shifted when it is stored
9175 into the instruction code. */
9176 int op_shift;
9177 /* Non-zero if the short form is unsigned. */
9178 int unsp;
9179 /* Non-zero if the extended form is unsigned. */
9180 int extu;
9181 /* Non-zero if the value is PC relative. */
9182 int pcrel;
9183 };
9184
9185 /* The mips16 immediate operand types. */
9186
9187 static const struct mips16_immed_operand mips16_immed_operands[] =
9188 {
9189 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9190 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9191 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9192 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9193 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9194 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9195 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9196 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9197 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9198 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9199 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9200 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9201 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9202 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9203 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9204 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9205 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9206 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9207 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9208 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9209 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9210 };
9211
9212 #define MIPS16_NUM_IMMED \
9213 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9214
9215 /* Handle a mips16 instruction with an immediate value. This or's the
9216 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9217 whether an extended value is needed; if one is needed, it sets
9218 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9219 If SMALL is true, an unextended opcode was explicitly requested.
9220 If EXT is true, an extended opcode was explicitly requested. If
9221 WARN is true, warn if EXT does not match reality. */
9222
9223 static void
9224 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9225 extend)
9226 char *file;
9227 unsigned int line;
9228 int type;
9229 offsetT val;
9230 boolean warn;
9231 boolean small;
9232 boolean ext;
9233 unsigned long *insn;
9234 boolean *use_extend;
9235 unsigned short *extend;
9236 {
9237 register const struct mips16_immed_operand *op;
9238 int mintiny, maxtiny;
9239 boolean needext;
9240
9241 op = mips16_immed_operands;
9242 while (op->type != type)
9243 {
9244 ++op;
9245 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9246 }
9247
9248 if (op->unsp)
9249 {
9250 if (type == '<' || type == '>' || type == '[' || type == ']')
9251 {
9252 mintiny = 1;
9253 maxtiny = 1 << op->nbits;
9254 }
9255 else
9256 {
9257 mintiny = 0;
9258 maxtiny = (1 << op->nbits) - 1;
9259 }
9260 }
9261 else
9262 {
9263 mintiny = - (1 << (op->nbits - 1));
9264 maxtiny = (1 << (op->nbits - 1)) - 1;
9265 }
9266
9267 /* Branch offsets have an implicit 0 in the lowest bit. */
9268 if (type == 'p' || type == 'q')
9269 val /= 2;
9270
9271 if ((val & ((1 << op->shift) - 1)) != 0
9272 || val < (mintiny << op->shift)
9273 || val > (maxtiny << op->shift))
9274 needext = true;
9275 else
9276 needext = false;
9277
9278 if (warn && ext && ! needext)
9279 as_warn_where (file, line,
9280 _("extended operand requested but not required"));
9281 if (small && needext)
9282 as_bad_where (file, line, _("invalid unextended operand value"));
9283
9284 if (small || (! ext && ! needext))
9285 {
9286 int insnval;
9287
9288 *use_extend = false;
9289 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9290 insnval <<= op->op_shift;
9291 *insn |= insnval;
9292 }
9293 else
9294 {
9295 long minext, maxext;
9296 int extval;
9297
9298 if (op->extu)
9299 {
9300 minext = 0;
9301 maxext = (1 << op->extbits) - 1;
9302 }
9303 else
9304 {
9305 minext = - (1 << (op->extbits - 1));
9306 maxext = (1 << (op->extbits - 1)) - 1;
9307 }
9308 if (val < minext || val > maxext)
9309 as_bad_where (file, line,
9310 _("operand value out of range for instruction"));
9311
9312 *use_extend = true;
9313 if (op->extbits == 16)
9314 {
9315 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9316 val &= 0x1f;
9317 }
9318 else if (op->extbits == 15)
9319 {
9320 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9321 val &= 0xf;
9322 }
9323 else
9324 {
9325 extval = ((val & 0x1f) << 6) | (val & 0x20);
9326 val = 0;
9327 }
9328
9329 *extend = (unsigned short) extval;
9330 *insn |= val;
9331 }
9332 }
9333 \f
9334 static struct percent_op_match
9335 {
9336 const char *str;
9337 const enum small_ex_type type;
9338 } percent_op[] =
9339 {
9340 {"%lo", S_EX_LO},
9341 #ifdef OBJ_ELF
9342 {"%call_hi", S_EX_CALL_HI},
9343 {"%call_lo", S_EX_CALL_LO},
9344 {"%call16", S_EX_CALL16},
9345 {"%got_disp", S_EX_GOT_DISP},
9346 {"%got_page", S_EX_GOT_PAGE},
9347 {"%got_ofst", S_EX_GOT_OFST},
9348 {"%got_hi", S_EX_GOT_HI},
9349 {"%got_lo", S_EX_GOT_LO},
9350 {"%got", S_EX_GOT},
9351 {"%gp_rel", S_EX_GP_REL},
9352 {"%half", S_EX_HALF},
9353 {"%highest", S_EX_HIGHEST},
9354 {"%higher", S_EX_HIGHER},
9355 {"%neg", S_EX_NEG},
9356 #endif
9357 {"%hi", S_EX_HI}
9358 };
9359
9360 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9361 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9362 can be nested, this is handled by blanking the innermost, parsing the
9363 rest by subsequent calls. */
9364
9365 static int
9366 my_getSmallParser (str, len, nestlevel)
9367 char **str;
9368 unsigned int *len;
9369 int *nestlevel;
9370 {
9371 *len = 0;
9372 *str += strspn (*str, " \t");
9373 /* Check for expression in parentheses. */
9374 if (**str == '(')
9375 {
9376 char *b = *str + 1 + strspn (*str + 1, " \t");
9377 char *e;
9378
9379 /* Check for base register. */
9380 if (b[0] == '$')
9381 {
9382 if (strchr (b, ')')
9383 && (e = b + strcspn (b, ") \t"))
9384 && e - b > 1 && e - b < 4)
9385 {
9386 if ((e - b == 3
9387 && ((b[1] == 'f' && b[2] == 'p')
9388 || (b[1] == 's' && b[2] == 'p')
9389 || (b[1] == 'g' && b[2] == 'p')
9390 || (b[1] == 'a' && b[2] == 't')
9391 || (ISDIGIT (b[1])
9392 && ISDIGIT (b[2]))))
9393 || (ISDIGIT (b[1])))
9394 {
9395 *len = strcspn (*str, ")") + 1;
9396 return S_EX_REGISTER;
9397 }
9398 }
9399 }
9400 /* Check for percent_op (in parentheses). */
9401 else if (b[0] == '%')
9402 {
9403 *str = b;
9404 return my_getPercentOp (str, len, nestlevel);
9405 }
9406
9407 /* Some other expression in the parentheses, which can contain
9408 parentheses itself. Attempt to find the matching one. */
9409 {
9410 int pcnt = 1;
9411 char *s;
9412
9413 *len = 1;
9414 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9415 {
9416 if (*s == '(')
9417 pcnt++;
9418 else if (*s == ')')
9419 pcnt--;
9420 }
9421 }
9422 }
9423 /* Check for percent_op (outside of parentheses). */
9424 else if (*str[0] == '%')
9425 return my_getPercentOp (str, len, nestlevel);
9426
9427 /* Any other expression. */
9428 return S_EX_NONE;
9429 }
9430
9431 static int
9432 my_getPercentOp (str, len, nestlevel)
9433 char **str;
9434 unsigned int *len;
9435 int *nestlevel;
9436 {
9437 char *tmp = *str + 1;
9438 unsigned int i = 0;
9439
9440 while (ISALPHA (*tmp) || *tmp == '_')
9441 {
9442 *tmp = TOLOWER (*tmp);
9443 tmp++;
9444 }
9445 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9446 {
9447 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9448 i++;
9449 else
9450 {
9451 int type = percent_op[i].type;
9452
9453 /* Only %hi and %lo are allowed for OldABI. */
9454 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9455 return S_EX_NONE;
9456
9457 *len = strlen (percent_op[i].str);
9458 (*nestlevel)++;
9459 return type;
9460 }
9461 }
9462 return S_EX_NONE;
9463 }
9464
9465 static int
9466 my_getSmallExpression (ep, str)
9467 expressionS *ep;
9468 char *str;
9469 {
9470 static char *oldstr = NULL;
9471 int c = S_EX_NONE;
9472 int oldc;
9473 int nestlevel = -1;
9474 unsigned int len;
9475
9476 /* Don't update oldstr if the last call had nested percent_op's. We need
9477 it to parse the outer ones later. */
9478 if (! oldstr)
9479 oldstr = str;
9480
9481 do
9482 {
9483 oldc = c;
9484 c = my_getSmallParser (&str, &len, &nestlevel);
9485 if (c != S_EX_NONE && c != S_EX_REGISTER)
9486 str += len;
9487 }
9488 while (c != S_EX_NONE && c != S_EX_REGISTER);
9489
9490 if (nestlevel >= 0)
9491 {
9492 /* A percent_op was encountered. Don't try to get an expression if
9493 it is already blanked out. */
9494 if (*(str + strspn (str + 1, " )")) != ')')
9495 {
9496 char save;
9497
9498 /* Let my_getExpression() stop at the closing parenthesis. */
9499 save = *(str + len);
9500 *(str + len) = '\0';
9501 my_getExpression (ep, str);
9502 *(str + len) = save;
9503 }
9504 if (nestlevel > 0)
9505 {
9506 /* Blank out including the % sign and the proper matching
9507 parenthesis. */
9508 int pcnt = 1;
9509 char *s = strrchr (oldstr, '%');
9510 char *end;
9511
9512 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9513 {
9514 if (*end == '(')
9515 pcnt++;
9516 else if (*end == ')')
9517 pcnt--;
9518 }
9519
9520 memset (s, ' ', end - s);
9521 str = oldstr;
9522 }
9523 else
9524 expr_end = str + len;
9525
9526 c = oldc;
9527 }
9528 else if (c == S_EX_NONE)
9529 {
9530 my_getExpression (ep, str);
9531 }
9532 else if (c == S_EX_REGISTER)
9533 {
9534 ep->X_op = O_constant;
9535 expr_end = str;
9536 ep->X_add_symbol = NULL;
9537 ep->X_op_symbol = NULL;
9538 ep->X_add_number = 0;
9539 }
9540 else
9541 {
9542 as_fatal(_("internal error"));
9543 }
9544
9545 if (nestlevel <= 0)
9546 /* All percent_op's have been handled. */
9547 oldstr = NULL;
9548
9549 return c;
9550 }
9551
9552 static void
9553 my_getExpression (ep, str)
9554 expressionS *ep;
9555 char *str;
9556 {
9557 char *save_in;
9558 valueT val;
9559
9560 save_in = input_line_pointer;
9561 input_line_pointer = str;
9562 expression (ep);
9563 expr_end = input_line_pointer;
9564 input_line_pointer = save_in;
9565
9566 /* If we are in mips16 mode, and this is an expression based on `.',
9567 then we bump the value of the symbol by 1 since that is how other
9568 text symbols are handled. We don't bother to handle complex
9569 expressions, just `.' plus or minus a constant. */
9570 if (mips_opts.mips16
9571 && ep->X_op == O_symbol
9572 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9573 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9574 && symbol_get_frag (ep->X_add_symbol) == frag_now
9575 && symbol_constant_p (ep->X_add_symbol)
9576 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9577 S_SET_VALUE (ep->X_add_symbol, val + 1);
9578 }
9579
9580 /* Turn a string in input_line_pointer into a floating point constant
9581 of type TYPE, and store the appropriate bytes in *LITP. The number
9582 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9583 returned, or NULL on OK. */
9584
9585 char *
9586 md_atof (type, litP, sizeP)
9587 int type;
9588 char *litP;
9589 int *sizeP;
9590 {
9591 int prec;
9592 LITTLENUM_TYPE words[4];
9593 char *t;
9594 int i;
9595
9596 switch (type)
9597 {
9598 case 'f':
9599 prec = 2;
9600 break;
9601
9602 case 'd':
9603 prec = 4;
9604 break;
9605
9606 default:
9607 *sizeP = 0;
9608 return _("bad call to md_atof");
9609 }
9610
9611 t = atof_ieee (input_line_pointer, type, words);
9612 if (t)
9613 input_line_pointer = t;
9614
9615 *sizeP = prec * 2;
9616
9617 if (! target_big_endian)
9618 {
9619 for (i = prec - 1; i >= 0; i--)
9620 {
9621 md_number_to_chars (litP, (valueT) words[i], 2);
9622 litP += 2;
9623 }
9624 }
9625 else
9626 {
9627 for (i = 0; i < prec; i++)
9628 {
9629 md_number_to_chars (litP, (valueT) words[i], 2);
9630 litP += 2;
9631 }
9632 }
9633
9634 return NULL;
9635 }
9636
9637 void
9638 md_number_to_chars (buf, val, n)
9639 char *buf;
9640 valueT val;
9641 int n;
9642 {
9643 if (target_big_endian)
9644 number_to_chars_bigendian (buf, val, n);
9645 else
9646 number_to_chars_littleendian (buf, val, n);
9647 }
9648 \f
9649 #ifdef OBJ_ELF
9650 static int support_64bit_objects(void)
9651 {
9652 const char **list, **l;
9653
9654 list = bfd_target_list ();
9655 for (l = list; *l != NULL; l++)
9656 #ifdef TE_TMIPS
9657 /* This is traditional mips */
9658 if (strcmp (*l, "elf64-tradbigmips") == 0
9659 || strcmp (*l, "elf64-tradlittlemips") == 0)
9660 #else
9661 if (strcmp (*l, "elf64-bigmips") == 0
9662 || strcmp (*l, "elf64-littlemips") == 0)
9663 #endif
9664 break;
9665 free (list);
9666 return (*l != NULL);
9667 }
9668 #endif /* OBJ_ELF */
9669
9670 CONST char *md_shortopts = "nO::g::G:";
9671
9672 struct option md_longopts[] =
9673 {
9674 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9675 {"mips0", no_argument, NULL, OPTION_MIPS1},
9676 {"mips1", no_argument, NULL, OPTION_MIPS1},
9677 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9678 {"mips2", no_argument, NULL, OPTION_MIPS2},
9679 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9680 {"mips3", no_argument, NULL, OPTION_MIPS3},
9681 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9682 {"mips4", no_argument, NULL, OPTION_MIPS4},
9683 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9684 {"mips5", no_argument, NULL, OPTION_MIPS5},
9685 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9686 {"mips32", no_argument, NULL, OPTION_MIPS32},
9687 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9688 {"mips64", no_argument, NULL, OPTION_MIPS64},
9689 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9690 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9691 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9692 {"trap", no_argument, NULL, OPTION_TRAP},
9693 {"no-break", no_argument, NULL, OPTION_TRAP},
9694 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9695 {"break", no_argument, NULL, OPTION_BREAK},
9696 {"no-trap", no_argument, NULL, OPTION_BREAK},
9697 #define OPTION_EB (OPTION_MD_BASE + 11)
9698 {"EB", no_argument, NULL, OPTION_EB},
9699 #define OPTION_EL (OPTION_MD_BASE + 12)
9700 {"EL", no_argument, NULL, OPTION_EL},
9701 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9702 {"mips16", no_argument, NULL, OPTION_MIPS16},
9703 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9704 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9705 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9706 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9707 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9708 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9709 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9710 {"mfp32", no_argument, NULL, OPTION_FP32},
9711 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9712 {"mgp32", no_argument, NULL, OPTION_GP32},
9713 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9714 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9715 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9716 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9717 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9718 {"march", required_argument, NULL, OPTION_MARCH},
9719 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9720 {"mtune", required_argument, NULL, OPTION_MTUNE},
9721 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9722 {"mcpu", required_argument, NULL, OPTION_MCPU},
9723 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9724 {"m4650", no_argument, NULL, OPTION_M4650},
9725 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9726 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9727 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9728 {"m4010", no_argument, NULL, OPTION_M4010},
9729 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9730 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9731 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9732 {"m4100", no_argument, NULL, OPTION_M4100},
9733 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9734 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9735 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9736 {"m3900", no_argument, NULL, OPTION_M3900},
9737 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9738 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9739 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9740 {"mgp64", no_argument, NULL, OPTION_GP64},
9741 #ifdef OBJ_ELF
9742 #define OPTION_ELF_BASE (OPTION_MD_BASE + 33)
9743 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9744 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9745 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9746 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
9747 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
9748 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
9749 {"xgot", no_argument, NULL, OPTION_XGOT},
9750 #define OPTION_MABI (OPTION_ELF_BASE + 3)
9751 {"mabi", required_argument, NULL, OPTION_MABI},
9752 #define OPTION_32 (OPTION_ELF_BASE + 4)
9753 {"32", no_argument, NULL, OPTION_32},
9754 #define OPTION_N32 (OPTION_ELF_BASE + 5)
9755 {"n32", no_argument, NULL, OPTION_N32},
9756 #define OPTION_64 (OPTION_ELF_BASE + 6)
9757 {"64", no_argument, NULL, OPTION_64},
9758 #endif /* OBJ_ELF */
9759 {NULL, no_argument, NULL, 0}
9760 };
9761 size_t md_longopts_size = sizeof (md_longopts);
9762
9763 int
9764 md_parse_option (c, arg)
9765 int c;
9766 char *arg;
9767 {
9768 switch (c)
9769 {
9770 case OPTION_CONSTRUCT_FLOATS:
9771 mips_disable_float_construction = 0;
9772 break;
9773
9774 case OPTION_NO_CONSTRUCT_FLOATS:
9775 mips_disable_float_construction = 1;
9776 break;
9777
9778 case OPTION_TRAP:
9779 mips_trap = 1;
9780 break;
9781
9782 case OPTION_BREAK:
9783 mips_trap = 0;
9784 break;
9785
9786 case OPTION_EB:
9787 target_big_endian = 1;
9788 break;
9789
9790 case OPTION_EL:
9791 target_big_endian = 0;
9792 break;
9793
9794 case 'n':
9795 warn_nops = 1;
9796 break;
9797
9798 case 'O':
9799 if (arg && arg[1] == '0')
9800 mips_optimize = 1;
9801 else
9802 mips_optimize = 2;
9803 break;
9804
9805 case 'g':
9806 if (arg == NULL)
9807 mips_debug = 2;
9808 else
9809 mips_debug = atoi (arg);
9810 /* When the MIPS assembler sees -g or -g2, it does not do
9811 optimizations which limit full symbolic debugging. We take
9812 that to be equivalent to -O0. */
9813 if (mips_debug == 2)
9814 mips_optimize = 1;
9815 break;
9816
9817 case OPTION_MIPS1:
9818 mips_opts.isa = ISA_MIPS1;
9819 break;
9820
9821 case OPTION_MIPS2:
9822 mips_opts.isa = ISA_MIPS2;
9823 break;
9824
9825 case OPTION_MIPS3:
9826 mips_opts.isa = ISA_MIPS3;
9827 break;
9828
9829 case OPTION_MIPS4:
9830 mips_opts.isa = ISA_MIPS4;
9831 break;
9832
9833 case OPTION_MIPS5:
9834 mips_opts.isa = ISA_MIPS5;
9835 break;
9836
9837 case OPTION_MIPS32:
9838 mips_opts.isa = ISA_MIPS32;
9839 break;
9840
9841 case OPTION_MIPS64:
9842 mips_opts.isa = ISA_MIPS64;
9843 break;
9844
9845 case OPTION_MTUNE:
9846 case OPTION_MARCH:
9847 case OPTION_MCPU:
9848 {
9849 int cpu = CPU_UNKNOWN;
9850
9851 /* Identify the processor type. */
9852 if (strcasecmp (arg, "default") != 0)
9853 {
9854 const struct mips_cpu_info *ci;
9855
9856 ci = mips_cpu_info_from_name (arg);
9857 if (ci == NULL || ci->is_isa)
9858 {
9859 switch (c)
9860 {
9861 case OPTION_MTUNE:
9862 as_fatal (_("invalid architecture -mtune=%s"), arg);
9863 break;
9864 case OPTION_MARCH:
9865 as_fatal (_("invalid architecture -march=%s"), arg);
9866 break;
9867 case OPTION_MCPU:
9868 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9869 break;
9870 }
9871 }
9872 else
9873 cpu = ci->cpu;
9874 }
9875
9876 switch (c)
9877 {
9878 case OPTION_MTUNE:
9879 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9880 as_warn(_("A different -mtune= was already specified, is now "
9881 "-mtune=%s"), arg);
9882 mips_tune = cpu;
9883 break;
9884 case OPTION_MARCH:
9885 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9886 as_warn(_("A different -march= was already specified, is now "
9887 "-march=%s"), arg);
9888 mips_arch = cpu;
9889 break;
9890 case OPTION_MCPU:
9891 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9892 as_warn(_("A different -mcpu= was already specified, is now "
9893 "-mcpu=%s"), arg);
9894 mips_cpu = cpu;
9895 }
9896 }
9897 break;
9898
9899 case OPTION_M4650:
9900 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9901 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9902 as_warn(_("A different -march= or -mtune= was already specified, "
9903 "is now -m4650"));
9904 mips_arch = CPU_R4650;
9905 mips_tune = CPU_R4650;
9906 break;
9907
9908 case OPTION_NO_M4650:
9909 break;
9910
9911 case OPTION_M4010:
9912 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9913 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9914 as_warn(_("A different -march= or -mtune= was already specified, "
9915 "is now -m4010"));
9916 mips_arch = CPU_R4010;
9917 mips_tune = CPU_R4010;
9918 break;
9919
9920 case OPTION_NO_M4010:
9921 break;
9922
9923 case OPTION_M4100:
9924 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9925 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9926 as_warn(_("A different -march= or -mtune= was already specified, "
9927 "is now -m4100"));
9928 mips_arch = CPU_VR4100;
9929 mips_tune = CPU_VR4100;
9930 break;
9931
9932 case OPTION_NO_M4100:
9933 break;
9934
9935 case OPTION_M3900:
9936 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9937 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9938 as_warn(_("A different -march= or -mtune= was already specified, "
9939 "is now -m3900"));
9940 mips_arch = CPU_R3900;
9941 mips_tune = CPU_R3900;
9942 break;
9943
9944 case OPTION_NO_M3900:
9945 break;
9946
9947 case OPTION_MIPS16:
9948 mips_opts.mips16 = 1;
9949 mips_no_prev_insn (false);
9950 break;
9951
9952 case OPTION_NO_MIPS16:
9953 mips_opts.mips16 = 0;
9954 mips_no_prev_insn (false);
9955 break;
9956
9957 case OPTION_MEMBEDDED_PIC:
9958 mips_pic = EMBEDDED_PIC;
9959 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9960 {
9961 as_bad (_("-G may not be used with embedded PIC code"));
9962 return 0;
9963 }
9964 g_switch_value = 0x7fffffff;
9965 break;
9966
9967 #ifdef OBJ_ELF
9968 /* When generating ELF code, we permit -KPIC and -call_shared to
9969 select SVR4_PIC, and -non_shared to select no PIC. This is
9970 intended to be compatible with Irix 5. */
9971 case OPTION_CALL_SHARED:
9972 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9973 {
9974 as_bad (_("-call_shared is supported only for ELF format"));
9975 return 0;
9976 }
9977 mips_pic = SVR4_PIC;
9978 if (g_switch_seen && g_switch_value != 0)
9979 {
9980 as_bad (_("-G may not be used with SVR4 PIC code"));
9981 return 0;
9982 }
9983 g_switch_value = 0;
9984 break;
9985
9986 case OPTION_NON_SHARED:
9987 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9988 {
9989 as_bad (_("-non_shared is supported only for ELF format"));
9990 return 0;
9991 }
9992 mips_pic = NO_PIC;
9993 break;
9994
9995 /* The -xgot option tells the assembler to use 32 offsets when
9996 accessing the got in SVR4_PIC mode. It is for Irix
9997 compatibility. */
9998 case OPTION_XGOT:
9999 mips_big_got = 1;
10000 break;
10001 #endif /* OBJ_ELF */
10002
10003 case 'G':
10004 if (! USE_GLOBAL_POINTER_OPT)
10005 {
10006 as_bad (_("-G is not supported for this configuration"));
10007 return 0;
10008 }
10009 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10010 {
10011 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10012 return 0;
10013 }
10014 else
10015 g_switch_value = atoi (arg);
10016 g_switch_seen = 1;
10017 break;
10018
10019 #ifdef OBJ_ELF
10020 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10021 and -mabi=64. */
10022 case OPTION_32:
10023 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10024 {
10025 as_bad (_("-32 is supported for ELF format only"));
10026 return 0;
10027 }
10028 mips_opts.abi = O32_ABI;
10029 break;
10030
10031 case OPTION_N32:
10032 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10033 {
10034 as_bad (_("-n32 is supported for ELF format only"));
10035 return 0;
10036 }
10037 mips_opts.abi = N32_ABI;
10038 break;
10039
10040 case OPTION_64:
10041 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10042 {
10043 as_bad (_("-64 is supported for ELF format only"));
10044 return 0;
10045 }
10046 mips_opts.abi = N64_ABI;
10047 if (! support_64bit_objects())
10048 as_fatal (_("No compiled in support for 64 bit object file format"));
10049 break;
10050 #endif /* OBJ_ELF */
10051
10052 case OPTION_GP32:
10053 file_mips_gp32 = 1;
10054 if (mips_opts.abi != O32_ABI)
10055 mips_opts.abi = NO_ABI;
10056 break;
10057
10058 case OPTION_GP64:
10059 file_mips_gp32 = 0;
10060 if (mips_opts.abi == O32_ABI)
10061 mips_opts.abi = NO_ABI;
10062 break;
10063
10064 case OPTION_FP32:
10065 file_mips_fp32 = 1;
10066 if (mips_opts.abi != O32_ABI)
10067 mips_opts.abi = NO_ABI;
10068 break;
10069
10070 #ifdef OBJ_ELF
10071 case OPTION_MABI:
10072 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10073 {
10074 as_bad (_("-mabi is supported for ELF format only"));
10075 return 0;
10076 }
10077 if (strcmp (arg, "32") == 0)
10078 mips_opts.abi = O32_ABI;
10079 else if (strcmp (arg, "o64") == 0)
10080 mips_opts.abi = O64_ABI;
10081 else if (strcmp (arg, "n32") == 0)
10082 mips_opts.abi = N32_ABI;
10083 else if (strcmp (arg, "64") == 0)
10084 {
10085 mips_opts.abi = N64_ABI;
10086 if (! support_64bit_objects())
10087 as_fatal (_("No compiled in support for 64 bit object file "
10088 "format"));
10089 }
10090 else if (strcmp (arg, "eabi") == 0)
10091 mips_opts.abi = EABI_ABI;
10092 else
10093 {
10094 as_fatal (_("invalid abi -mabi=%s"), arg);
10095 return 0;
10096 }
10097 break;
10098 #endif /* OBJ_ELF */
10099
10100 case OPTION_M7000_HILO_FIX:
10101 mips_7000_hilo_fix = true;
10102 break;
10103
10104 case OPTION_NO_M7000_HILO_FIX:
10105 mips_7000_hilo_fix = false;
10106 break;
10107
10108 default:
10109 return 0;
10110 }
10111
10112 return 1;
10113 }
10114
10115 static void
10116 show (stream, string, col_p, first_p)
10117 FILE *stream;
10118 char *string;
10119 int *col_p;
10120 int *first_p;
10121 {
10122 if (*first_p)
10123 {
10124 fprintf (stream, "%24s", "");
10125 *col_p = 24;
10126 }
10127 else
10128 {
10129 fprintf (stream, ", ");
10130 *col_p += 2;
10131 }
10132
10133 if (*col_p + strlen (string) > 72)
10134 {
10135 fprintf (stream, "\n%24s", "");
10136 *col_p = 24;
10137 }
10138
10139 fprintf (stream, "%s", string);
10140 *col_p += strlen (string);
10141
10142 *first_p = 0;
10143 }
10144
10145 void
10146 md_show_usage (stream)
10147 FILE *stream;
10148 {
10149 int column, first;
10150
10151 fprintf (stream, _("\
10152 MIPS options:\n\
10153 -membedded-pic generate embedded position independent code\n\
10154 -EB generate big endian output\n\
10155 -EL generate little endian output\n\
10156 -g, -g2 do not remove unneeded NOPs or swap branches\n\
10157 -G NUM allow referencing objects up to NUM bytes\n\
10158 implicitly with the gp register [default 8]\n"));
10159 fprintf (stream, _("\
10160 -mips1 generate MIPS ISA I instructions\n\
10161 -mips2 generate MIPS ISA II instructions\n\
10162 -mips3 generate MIPS ISA III instructions\n\
10163 -mips4 generate MIPS ISA IV instructions\n\
10164 -mips5 generate MIPS ISA V instructions\n\
10165 -mips32 generate MIPS32 ISA instructions\n\
10166 -mips64 generate MIPS64 ISA instructions\n\
10167 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
10168
10169 first = 1;
10170
10171 show (stream, "2000", &column, &first);
10172 show (stream, "3000", &column, &first);
10173 show (stream, "3900", &column, &first);
10174 show (stream, "4000", &column, &first);
10175 show (stream, "4010", &column, &first);
10176 show (stream, "4100", &column, &first);
10177 show (stream, "4111", &column, &first);
10178 show (stream, "4300", &column, &first);
10179 show (stream, "4400", &column, &first);
10180 show (stream, "4600", &column, &first);
10181 show (stream, "4650", &column, &first);
10182 show (stream, "5000", &column, &first);
10183 show (stream, "5200", &column, &first);
10184 show (stream, "5230", &column, &first);
10185 show (stream, "5231", &column, &first);
10186 show (stream, "5261", &column, &first);
10187 show (stream, "5721", &column, &first);
10188 show (stream, "6000", &column, &first);
10189 show (stream, "8000", &column, &first);
10190 show (stream, "10000", &column, &first);
10191 show (stream, "12000", &column, &first);
10192 show (stream, "sb1", &column, &first);
10193 fputc ('\n', stream);
10194
10195 fprintf (stream, _("\
10196 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10197 -no-mCPU don't generate code specific to CPU.\n\
10198 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10199
10200 first = 1;
10201
10202 show (stream, "3900", &column, &first);
10203 show (stream, "4010", &column, &first);
10204 show (stream, "4100", &column, &first);
10205 show (stream, "4650", &column, &first);
10206 fputc ('\n', stream);
10207
10208 fprintf (stream, _("\
10209 -mips16 generate mips16 instructions\n\
10210 -no-mips16 do not generate mips16 instructions\n"));
10211 fprintf (stream, _("\
10212 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10213 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
10214 -O0 remove unneeded NOPs, do not swap branches\n\
10215 -O remove unneeded NOPs and swap branches\n\
10216 -n warn about NOPs generated from macros\n\
10217 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10218 --trap, --no-break trap exception on div by 0 and mult overflow\n\
10219 --break, --no-trap break exception on div by 0 and mult overflow\n"));
10220 #ifdef OBJ_ELF
10221 fprintf (stream, _("\
10222 -KPIC, -call_shared generate SVR4 position independent code\n\
10223 -non_shared do not generate position independent code\n\
10224 -xgot assume a 32 bit GOT\n\
10225 -mabi=ABI create ABI conformant object file for:\n"));
10226
10227 first = 1;
10228
10229 show (stream, "32", &column, &first);
10230 show (stream, "o64", &column, &first);
10231 show (stream, "n32", &column, &first);
10232 show (stream, "64", &column, &first);
10233 show (stream, "eabi", &column, &first);
10234
10235 fputc ('\n', stream);
10236
10237 fprintf (stream, _("\
10238 -32 create o32 ABI object file (default)\n\
10239 -n32 create n32 ABI object file\n\
10240 -64 create 64 ABI object file\n"));
10241 #endif
10242 }
10243 \f
10244 void
10245 mips_init_after_args ()
10246 {
10247 /* initialize opcodes */
10248 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10249 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10250 }
10251
10252 long
10253 md_pcrel_from (fixP)
10254 fixS *fixP;
10255 {
10256 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10257 && fixP->fx_addsy != (symbolS *) NULL
10258 && ! S_IS_DEFINED (fixP->fx_addsy))
10259 {
10260 /* This makes a branch to an undefined symbol be a branch to the
10261 current location. */
10262 if (mips_pic == EMBEDDED_PIC)
10263 return 4;
10264 else
10265 return 1;
10266 }
10267
10268 /* return the address of the delay slot */
10269 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10270 }
10271
10272 /* This is called before the symbol table is processed. In order to
10273 work with gcc when using mips-tfile, we must keep all local labels.
10274 However, in other cases, we want to discard them. If we were
10275 called with -g, but we didn't see any debugging information, it may
10276 mean that gcc is smuggling debugging information through to
10277 mips-tfile, in which case we must generate all local labels. */
10278
10279 void
10280 mips_frob_file_before_adjust ()
10281 {
10282 #ifndef NO_ECOFF_DEBUGGING
10283 if (ECOFF_DEBUGGING
10284 && mips_debug != 0
10285 && ! ecoff_debugging_seen)
10286 flag_keep_locals = 1;
10287 #endif
10288 }
10289
10290 /* Sort any unmatched HI16_S relocs so that they immediately precede
10291 the corresponding LO reloc. This is called before md_apply_fix3 and
10292 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10293 explicit use of the %hi modifier. */
10294
10295 void
10296 mips_frob_file ()
10297 {
10298 struct mips_hi_fixup *l;
10299
10300 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10301 {
10302 segment_info_type *seginfo;
10303 int pass;
10304
10305 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10306
10307 /* Check quickly whether the next fixup happens to be a matching
10308 %lo. */
10309 if (l->fixp->fx_next != NULL
10310 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10311 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10312 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10313 continue;
10314
10315 /* Look through the fixups for this segment for a matching %lo.
10316 When we find one, move the %hi just in front of it. We do
10317 this in two passes. In the first pass, we try to find a
10318 unique %lo. In the second pass, we permit multiple %hi
10319 relocs for a single %lo (this is a GNU extension). */
10320 seginfo = seg_info (l->seg);
10321 for (pass = 0; pass < 2; pass++)
10322 {
10323 fixS *f, *prev;
10324
10325 prev = NULL;
10326 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10327 {
10328 /* Check whether this is a %lo fixup which matches l->fixp. */
10329 if (f->fx_r_type == BFD_RELOC_LO16
10330 && f->fx_addsy == l->fixp->fx_addsy
10331 && f->fx_offset == l->fixp->fx_offset
10332 && (pass == 1
10333 || prev == NULL
10334 || prev->fx_r_type != BFD_RELOC_HI16_S
10335 || prev->fx_addsy != f->fx_addsy
10336 || prev->fx_offset != f->fx_offset))
10337 {
10338 fixS **pf;
10339
10340 /* Move l->fixp before f. */
10341 for (pf = &seginfo->fix_root;
10342 *pf != l->fixp;
10343 pf = &(*pf)->fx_next)
10344 assert (*pf != NULL);
10345
10346 *pf = l->fixp->fx_next;
10347
10348 l->fixp->fx_next = f;
10349 if (prev == NULL)
10350 seginfo->fix_root = l->fixp;
10351 else
10352 prev->fx_next = l->fixp;
10353
10354 break;
10355 }
10356
10357 prev = f;
10358 }
10359
10360 if (f != NULL)
10361 break;
10362
10363 #if 0 /* GCC code motion plus incomplete dead code elimination
10364 can leave a %hi without a %lo. */
10365 if (pass == 1)
10366 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10367 _("Unmatched %%hi reloc"));
10368 #endif
10369 }
10370 }
10371 }
10372
10373 /* When generating embedded PIC code we need to use a special
10374 relocation to represent the difference of two symbols in the .text
10375 section (switch tables use a difference of this sort). See
10376 include/coff/mips.h for details. This macro checks whether this
10377 fixup requires the special reloc. */
10378 #define SWITCH_TABLE(fixp) \
10379 ((fixp)->fx_r_type == BFD_RELOC_32 \
10380 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10381 && (fixp)->fx_addsy != NULL \
10382 && (fixp)->fx_subsy != NULL \
10383 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10384 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10385
10386 /* When generating embedded PIC code we must keep all PC relative
10387 relocations, in case the linker has to relax a call. We also need
10388 to keep relocations for switch table entries.
10389
10390 We may have combined relocations without symbols in the N32/N64 ABI.
10391 We have to prevent gas from dropping them. */
10392
10393 int
10394 mips_force_relocation (fixp)
10395 fixS *fixp;
10396 {
10397 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10398 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10399 return 1;
10400
10401 if (HAVE_NEWABI
10402 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10403 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10404 || fixp->fx_r_type == BFD_RELOC_HI16_S
10405 || fixp->fx_r_type == BFD_RELOC_LO16))
10406 return 1;
10407
10408 return (mips_pic == EMBEDDED_PIC
10409 && (fixp->fx_pcrel
10410 || SWITCH_TABLE (fixp)
10411 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10412 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10413 }
10414
10415 #ifdef OBJ_ELF
10416 static int
10417 mips_need_elf_addend_fixup (fixP)
10418 fixS *fixP;
10419 {
10420 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10421 return 1;
10422 if (mips_pic == EMBEDDED_PIC
10423 && S_IS_WEAK (fixP->fx_addsy))
10424 return 1;
10425 if (mips_pic != EMBEDDED_PIC
10426 && (S_IS_WEAK (fixP->fx_addsy)
10427 || S_IS_EXTERN (fixP->fx_addsy))
10428 && !S_IS_COMMON (fixP->fx_addsy))
10429 return 1;
10430 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10431 && (((bfd_get_section_flags (stdoutput,
10432 S_GET_SEGMENT (fixP->fx_addsy))
10433 & SEC_LINK_ONCE) != 0)
10434 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10435 ".gnu.linkonce",
10436 sizeof (".gnu.linkonce") - 1)))
10437 return 1;
10438 return 0;
10439 }
10440 #endif
10441
10442 /* Apply a fixup to the object file. */
10443
10444 void
10445 md_apply_fix3 (fixP, valP, seg)
10446 fixS *fixP;
10447 valueT * valP;
10448 segT seg ATTRIBUTE_UNUSED;
10449 {
10450 unsigned char *buf;
10451 long insn;
10452 valueT value;
10453
10454 assert (fixP->fx_size == 4
10455 || fixP->fx_r_type == BFD_RELOC_16
10456 || fixP->fx_r_type == BFD_RELOC_32
10457 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10458 || fixP->fx_r_type == BFD_RELOC_HI16_S
10459 || fixP->fx_r_type == BFD_RELOC_LO16
10460 || fixP->fx_r_type == BFD_RELOC_GPREL16
10461 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10462 || fixP->fx_r_type == BFD_RELOC_GPREL32
10463 || fixP->fx_r_type == BFD_RELOC_64
10464 || fixP->fx_r_type == BFD_RELOC_CTOR
10465 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10466 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10467 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10468 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10469 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10470 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10471 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10472 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10473
10474 value = * valP;
10475
10476 /* If we aren't adjusting this fixup to be against the section
10477 symbol, we need to adjust the value. */
10478 #ifdef OBJ_ELF
10479 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10480 {
10481 if (mips_need_elf_addend_fixup (fixP))
10482 {
10483 valueT symval = S_GET_VALUE (fixP->fx_addsy);
10484
10485 value -= symval;
10486 if (value != 0 && ! fixP->fx_pcrel)
10487 {
10488 /* In this case, the bfd_install_relocation routine will
10489 incorrectly add the symbol value back in. We just want
10490 the addend to appear in the object file. */
10491 value -= symval;
10492
10493 /* Make sure the addend is still non-zero. If it became zero
10494 after the last operation, set it to a spurious value and
10495 subtract the same value from the object file's contents. */
10496 if (value == 0)
10497 {
10498 value = 8;
10499
10500 /* The in-place addends for LO16 relocations are signed;
10501 leave the matching HI16 in-place addends as zero. */
10502 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10503 {
10504 reloc_howto_type *howto;
10505 bfd_vma contents, mask, field;
10506
10507 howto = bfd_reloc_type_lookup (stdoutput,
10508 fixP->fx_r_type);
10509
10510 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10511 + fixP->fx_where,
10512 fixP->fx_size * 8,
10513 target_big_endian);
10514
10515 /* MASK has bits set where the relocation should go.
10516 FIELD is -value, shifted into the appropriate place
10517 for this relocation. */
10518 mask = 1 << (howto->bitsize - 1);
10519 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10520 field = (-value >> howto->rightshift) << howto->bitpos;
10521
10522 bfd_put_bits ((field & mask) | (contents & ~mask),
10523 fixP->fx_frag->fr_literal + fixP->fx_where,
10524 fixP->fx_size * 8,
10525 target_big_endian);
10526 }
10527 }
10528 }
10529 }
10530
10531 /* This code was generated using trial and error and so is
10532 fragile and not trustworthy. If you change it, you should
10533 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10534 they still pass. */
10535 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10536 {
10537 value += fixP->fx_frag->fr_address + fixP->fx_where;
10538
10539 /* BFD's REL handling, for MIPS, is _very_ weird.
10540 This gives the right results, but it can't possibly
10541 be the way things are supposed to work. */
10542 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10543 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10544 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10545 value += fixP->fx_frag->fr_address + fixP->fx_where;
10546 }
10547 }
10548 #endif
10549
10550 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
10551
10552 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10553 fixP->fx_done = 1;
10554
10555 switch (fixP->fx_r_type)
10556 {
10557 case BFD_RELOC_MIPS_JMP:
10558 case BFD_RELOC_MIPS_SHIFT5:
10559 case BFD_RELOC_MIPS_SHIFT6:
10560 case BFD_RELOC_MIPS_GOT_DISP:
10561 case BFD_RELOC_MIPS_GOT_PAGE:
10562 case BFD_RELOC_MIPS_GOT_OFST:
10563 case BFD_RELOC_MIPS_SUB:
10564 case BFD_RELOC_MIPS_INSERT_A:
10565 case BFD_RELOC_MIPS_INSERT_B:
10566 case BFD_RELOC_MIPS_DELETE:
10567 case BFD_RELOC_MIPS_HIGHEST:
10568 case BFD_RELOC_MIPS_HIGHER:
10569 case BFD_RELOC_MIPS_SCN_DISP:
10570 case BFD_RELOC_MIPS_REL16:
10571 case BFD_RELOC_MIPS_RELGOT:
10572 case BFD_RELOC_MIPS_JALR:
10573 case BFD_RELOC_HI16:
10574 case BFD_RELOC_HI16_S:
10575 case BFD_RELOC_GPREL16:
10576 case BFD_RELOC_MIPS_LITERAL:
10577 case BFD_RELOC_MIPS_CALL16:
10578 case BFD_RELOC_MIPS_GOT16:
10579 case BFD_RELOC_GPREL32:
10580 case BFD_RELOC_MIPS_GOT_HI16:
10581 case BFD_RELOC_MIPS_GOT_LO16:
10582 case BFD_RELOC_MIPS_CALL_HI16:
10583 case BFD_RELOC_MIPS_CALL_LO16:
10584 case BFD_RELOC_MIPS16_GPREL:
10585 if (fixP->fx_pcrel)
10586 as_bad_where (fixP->fx_file, fixP->fx_line,
10587 _("Invalid PC relative reloc"));
10588 /* Nothing needed to do. The value comes from the reloc entry */
10589 break;
10590
10591 case BFD_RELOC_MIPS16_JMP:
10592 /* We currently always generate a reloc against a symbol, which
10593 means that we don't want an addend even if the symbol is
10594 defined. */
10595 fixP->fx_addnumber = 0;
10596 break;
10597
10598 case BFD_RELOC_PCREL_HI16_S:
10599 /* The addend for this is tricky if it is internal, so we just
10600 do everything here rather than in bfd_install_relocation. */
10601 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10602 && !fixP->fx_done
10603 && value != 0)
10604 break;
10605 if (fixP->fx_addsy
10606 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10607 {
10608 /* For an external symbol adjust by the address to make it
10609 pcrel_offset. We use the address of the RELLO reloc
10610 which follows this one. */
10611 value += (fixP->fx_next->fx_frag->fr_address
10612 + fixP->fx_next->fx_where);
10613 }
10614 value = ((value + 0x8000) >> 16) & 0xffff;
10615 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10616 if (target_big_endian)
10617 buf += 2;
10618 md_number_to_chars (buf, value, 2);
10619 break;
10620
10621 case BFD_RELOC_PCREL_LO16:
10622 /* The addend for this is tricky if it is internal, so we just
10623 do everything here rather than in bfd_install_relocation. */
10624 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10625 && !fixP->fx_done
10626 && value != 0)
10627 break;
10628 if (fixP->fx_addsy
10629 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10630 value += fixP->fx_frag->fr_address + fixP->fx_where;
10631 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10632 if (target_big_endian)
10633 buf += 2;
10634 md_number_to_chars (buf, value, 2);
10635 break;
10636
10637 case BFD_RELOC_64:
10638 /* This is handled like BFD_RELOC_32, but we output a sign
10639 extended value if we are only 32 bits. */
10640 if (fixP->fx_done
10641 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10642 {
10643 if (8 <= sizeof (valueT))
10644 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10645 value, 8);
10646 else
10647 {
10648 long w1, w2;
10649 long hiv;
10650
10651 w1 = w2 = fixP->fx_where;
10652 if (target_big_endian)
10653 w1 += 4;
10654 else
10655 w2 += 4;
10656 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10657 if ((value & 0x80000000) != 0)
10658 hiv = 0xffffffff;
10659 else
10660 hiv = 0;
10661 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10662 }
10663 }
10664 break;
10665
10666 case BFD_RELOC_RVA:
10667 case BFD_RELOC_32:
10668 /* If we are deleting this reloc entry, we must fill in the
10669 value now. This can happen if we have a .word which is not
10670 resolved when it appears but is later defined. We also need
10671 to fill in the value if this is an embedded PIC switch table
10672 entry. */
10673 if (fixP->fx_done
10674 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10675 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10676 value, 4);
10677 break;
10678
10679 case BFD_RELOC_16:
10680 /* If we are deleting this reloc entry, we must fill in the
10681 value now. */
10682 assert (fixP->fx_size == 2);
10683 if (fixP->fx_done)
10684 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10685 value, 2);
10686 break;
10687
10688 case BFD_RELOC_LO16:
10689 /* When handling an embedded PIC switch statement, we can wind
10690 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10691 if (fixP->fx_done)
10692 {
10693 if (value + 0x8000 > 0xffff)
10694 as_bad_where (fixP->fx_file, fixP->fx_line,
10695 _("relocation overflow"));
10696 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10697 if (target_big_endian)
10698 buf += 2;
10699 md_number_to_chars (buf, value, 2);
10700 }
10701 break;
10702
10703 case BFD_RELOC_16_PCREL_S2:
10704 if ((value & 0x3) != 0)
10705 as_bad_where (fixP->fx_file, fixP->fx_line,
10706 _("Branch to odd address (%lx)"), (long) value);
10707
10708 /* Fall through. */
10709
10710 case BFD_RELOC_16_PCREL:
10711 /*
10712 * We need to save the bits in the instruction since fixup_segment()
10713 * might be deleting the relocation entry (i.e., a branch within
10714 * the current segment).
10715 */
10716 if (!fixP->fx_done && value != 0)
10717 break;
10718 /* If 'value' is zero, the remaining reloc code won't actually
10719 do the store, so it must be done here. This is probably
10720 a bug somewhere. */
10721 if (!fixP->fx_done
10722 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10723 || fixP->fx_addsy == NULL /* ??? */
10724 || ! S_IS_DEFINED (fixP->fx_addsy)))
10725 value -= fixP->fx_frag->fr_address + fixP->fx_where;
10726
10727 value = (offsetT) value >> 2;
10728
10729 /* update old instruction data */
10730 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10731 if (target_big_endian)
10732 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10733 else
10734 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10735
10736 if (value + 0x8000 <= 0xffff)
10737 insn |= value & 0xffff;
10738 else
10739 {
10740 /* The branch offset is too large. If this is an
10741 unconditional branch, and we are not generating PIC code,
10742 we can convert it to an absolute jump instruction. */
10743 if (mips_pic == NO_PIC
10744 && fixP->fx_done
10745 && fixP->fx_frag->fr_address >= text_section->vma
10746 && (fixP->fx_frag->fr_address
10747 < text_section->vma + text_section->_raw_size)
10748 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10749 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10750 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10751 {
10752 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10753 insn = 0x0c000000; /* jal */
10754 else
10755 insn = 0x08000000; /* j */
10756 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10757 fixP->fx_done = 0;
10758 fixP->fx_addsy = section_symbol (text_section);
10759 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10760 }
10761 else
10762 {
10763 /* FIXME. It would be possible in principle to handle
10764 conditional branches which overflow. They could be
10765 transformed into a branch around a jump. This would
10766 require setting up variant frags for each different
10767 branch type. The native MIPS assembler attempts to
10768 handle these cases, but it appears to do it
10769 incorrectly. */
10770 as_bad_where (fixP->fx_file, fixP->fx_line,
10771 _("Branch out of range"));
10772 }
10773 }
10774
10775 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10776 break;
10777
10778 case BFD_RELOC_VTABLE_INHERIT:
10779 fixP->fx_done = 0;
10780 if (fixP->fx_addsy
10781 && !S_IS_DEFINED (fixP->fx_addsy)
10782 && !S_IS_WEAK (fixP->fx_addsy))
10783 S_SET_WEAK (fixP->fx_addsy);
10784 break;
10785
10786 case BFD_RELOC_VTABLE_ENTRY:
10787 fixP->fx_done = 0;
10788 break;
10789
10790 default:
10791 internalError ();
10792 }
10793 }
10794
10795 #if 0
10796 void
10797 printInsn (oc)
10798 unsigned long oc;
10799 {
10800 const struct mips_opcode *p;
10801 int treg, sreg, dreg, shamt;
10802 short imm;
10803 const char *args;
10804 int i;
10805
10806 for (i = 0; i < NUMOPCODES; ++i)
10807 {
10808 p = &mips_opcodes[i];
10809 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10810 {
10811 printf ("%08lx %s\t", oc, p->name);
10812 treg = (oc >> 16) & 0x1f;
10813 sreg = (oc >> 21) & 0x1f;
10814 dreg = (oc >> 11) & 0x1f;
10815 shamt = (oc >> 6) & 0x1f;
10816 imm = oc;
10817 for (args = p->args;; ++args)
10818 {
10819 switch (*args)
10820 {
10821 case '\0':
10822 printf ("\n");
10823 break;
10824
10825 case ',':
10826 case '(':
10827 case ')':
10828 printf ("%c", *args);
10829 continue;
10830
10831 case 'r':
10832 assert (treg == sreg);
10833 printf ("$%d,$%d", treg, sreg);
10834 continue;
10835
10836 case 'd':
10837 case 'G':
10838 printf ("$%d", dreg);
10839 continue;
10840
10841 case 't':
10842 case 'E':
10843 printf ("$%d", treg);
10844 continue;
10845
10846 case 'k':
10847 printf ("0x%x", treg);
10848 continue;
10849
10850 case 'b':
10851 case 's':
10852 printf ("$%d", sreg);
10853 continue;
10854
10855 case 'a':
10856 printf ("0x%08lx", oc & 0x1ffffff);
10857 continue;
10858
10859 case 'i':
10860 case 'j':
10861 case 'o':
10862 case 'u':
10863 printf ("%d", imm);
10864 continue;
10865
10866 case '<':
10867 case '>':
10868 printf ("$%d", shamt);
10869 continue;
10870
10871 default:
10872 internalError ();
10873 }
10874 break;
10875 }
10876 return;
10877 }
10878 }
10879 printf (_("%08lx UNDEFINED\n"), oc);
10880 }
10881 #endif
10882
10883 static symbolS *
10884 get_symbol ()
10885 {
10886 int c;
10887 char *name;
10888 symbolS *p;
10889
10890 name = input_line_pointer;
10891 c = get_symbol_end ();
10892 p = (symbolS *) symbol_find_or_make (name);
10893 *input_line_pointer = c;
10894 return p;
10895 }
10896
10897 /* Align the current frag to a given power of two. The MIPS assembler
10898 also automatically adjusts any preceding label. */
10899
10900 static void
10901 mips_align (to, fill, label)
10902 int to;
10903 int fill;
10904 symbolS *label;
10905 {
10906 mips_emit_delays (false);
10907 frag_align (to, fill, 0);
10908 record_alignment (now_seg, to);
10909 if (label != NULL)
10910 {
10911 assert (S_GET_SEGMENT (label) == now_seg);
10912 symbol_set_frag (label, frag_now);
10913 S_SET_VALUE (label, (valueT) frag_now_fix ());
10914 }
10915 }
10916
10917 /* Align to a given power of two. .align 0 turns off the automatic
10918 alignment used by the data creating pseudo-ops. */
10919
10920 static void
10921 s_align (x)
10922 int x ATTRIBUTE_UNUSED;
10923 {
10924 register int temp;
10925 register long temp_fill;
10926 long max_alignment = 15;
10927
10928 /*
10929
10930 o Note that the assembler pulls down any immediately preceeding label
10931 to the aligned address.
10932 o It's not documented but auto alignment is reinstated by
10933 a .align pseudo instruction.
10934 o Note also that after auto alignment is turned off the mips assembler
10935 issues an error on attempt to assemble an improperly aligned data item.
10936 We don't.
10937
10938 */
10939
10940 temp = get_absolute_expression ();
10941 if (temp > max_alignment)
10942 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10943 else if (temp < 0)
10944 {
10945 as_warn (_("Alignment negative: 0 assumed."));
10946 temp = 0;
10947 }
10948 if (*input_line_pointer == ',')
10949 {
10950 input_line_pointer++;
10951 temp_fill = get_absolute_expression ();
10952 }
10953 else
10954 temp_fill = 0;
10955 if (temp)
10956 {
10957 auto_align = 1;
10958 mips_align (temp, (int) temp_fill,
10959 insn_labels != NULL ? insn_labels->label : NULL);
10960 }
10961 else
10962 {
10963 auto_align = 0;
10964 }
10965
10966 demand_empty_rest_of_line ();
10967 }
10968
10969 void
10970 mips_flush_pending_output ()
10971 {
10972 mips_emit_delays (false);
10973 mips_clear_insn_labels ();
10974 }
10975
10976 static void
10977 s_change_sec (sec)
10978 int sec;
10979 {
10980 segT seg;
10981
10982 /* When generating embedded PIC code, we only use the .text, .lit8,
10983 .sdata and .sbss sections. We change the .data and .rdata
10984 pseudo-ops to use .sdata. */
10985 if (mips_pic == EMBEDDED_PIC
10986 && (sec == 'd' || sec == 'r'))
10987 sec = 's';
10988
10989 #ifdef OBJ_ELF
10990 /* The ELF backend needs to know that we are changing sections, so
10991 that .previous works correctly. We could do something like check
10992 for an obj_section_change_hook macro, but that might be confusing
10993 as it would not be appropriate to use it in the section changing
10994 functions in read.c, since obj-elf.c intercepts those. FIXME:
10995 This should be cleaner, somehow. */
10996 obj_elf_section_change_hook ();
10997 #endif
10998
10999 mips_emit_delays (false);
11000 switch (sec)
11001 {
11002 case 't':
11003 s_text (0);
11004 break;
11005 case 'd':
11006 s_data (0);
11007 break;
11008 case 'b':
11009 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11010 demand_empty_rest_of_line ();
11011 break;
11012
11013 case 'r':
11014 if (USE_GLOBAL_POINTER_OPT)
11015 {
11016 seg = subseg_new (RDATA_SECTION_NAME,
11017 (subsegT) get_absolute_expression ());
11018 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11019 {
11020 bfd_set_section_flags (stdoutput, seg,
11021 (SEC_ALLOC
11022 | SEC_LOAD
11023 | SEC_READONLY
11024 | SEC_RELOC
11025 | SEC_DATA));
11026 if (strcmp (TARGET_OS, "elf") != 0)
11027 record_alignment (seg, 4);
11028 }
11029 demand_empty_rest_of_line ();
11030 }
11031 else
11032 {
11033 as_bad (_("No read only data section in this object file format"));
11034 demand_empty_rest_of_line ();
11035 return;
11036 }
11037 break;
11038
11039 case 's':
11040 if (USE_GLOBAL_POINTER_OPT)
11041 {
11042 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11043 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11044 {
11045 bfd_set_section_flags (stdoutput, seg,
11046 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11047 | SEC_DATA);
11048 if (strcmp (TARGET_OS, "elf") != 0)
11049 record_alignment (seg, 4);
11050 }
11051 demand_empty_rest_of_line ();
11052 break;
11053 }
11054 else
11055 {
11056 as_bad (_("Global pointers not supported; recompile -G 0"));
11057 demand_empty_rest_of_line ();
11058 return;
11059 }
11060 }
11061
11062 auto_align = 1;
11063 }
11064
11065 void
11066 mips_enable_auto_align ()
11067 {
11068 auto_align = 1;
11069 }
11070
11071 static void
11072 s_cons (log_size)
11073 int log_size;
11074 {
11075 symbolS *label;
11076
11077 label = insn_labels != NULL ? insn_labels->label : NULL;
11078 mips_emit_delays (false);
11079 if (log_size > 0 && auto_align)
11080 mips_align (log_size, 0, label);
11081 mips_clear_insn_labels ();
11082 cons (1 << log_size);
11083 }
11084
11085 static void
11086 s_float_cons (type)
11087 int type;
11088 {
11089 symbolS *label;
11090
11091 label = insn_labels != NULL ? insn_labels->label : NULL;
11092
11093 mips_emit_delays (false);
11094
11095 if (auto_align)
11096 {
11097 if (type == 'd')
11098 mips_align (3, 0, label);
11099 else
11100 mips_align (2, 0, label);
11101 }
11102
11103 mips_clear_insn_labels ();
11104
11105 float_cons (type);
11106 }
11107
11108 /* Handle .globl. We need to override it because on Irix 5 you are
11109 permitted to say
11110 .globl foo .text
11111 where foo is an undefined symbol, to mean that foo should be
11112 considered to be the address of a function. */
11113
11114 static void
11115 s_mips_globl (x)
11116 int x ATTRIBUTE_UNUSED;
11117 {
11118 char *name;
11119 int c;
11120 symbolS *symbolP;
11121 flagword flag;
11122
11123 name = input_line_pointer;
11124 c = get_symbol_end ();
11125 symbolP = symbol_find_or_make (name);
11126 *input_line_pointer = c;
11127 SKIP_WHITESPACE ();
11128
11129 /* On Irix 5, every global symbol that is not explicitly labelled as
11130 being a function is apparently labelled as being an object. */
11131 flag = BSF_OBJECT;
11132
11133 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11134 {
11135 char *secname;
11136 asection *sec;
11137
11138 secname = input_line_pointer;
11139 c = get_symbol_end ();
11140 sec = bfd_get_section_by_name (stdoutput, secname);
11141 if (sec == NULL)
11142 as_bad (_("%s: no such section"), secname);
11143 *input_line_pointer = c;
11144
11145 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11146 flag = BSF_FUNCTION;
11147 }
11148
11149 symbol_get_bfdsym (symbolP)->flags |= flag;
11150
11151 S_SET_EXTERNAL (symbolP);
11152 demand_empty_rest_of_line ();
11153 }
11154
11155 static void
11156 s_option (x)
11157 int x ATTRIBUTE_UNUSED;
11158 {
11159 char *opt;
11160 char c;
11161
11162 opt = input_line_pointer;
11163 c = get_symbol_end ();
11164
11165 if (*opt == 'O')
11166 {
11167 /* FIXME: What does this mean? */
11168 }
11169 else if (strncmp (opt, "pic", 3) == 0)
11170 {
11171 int i;
11172
11173 i = atoi (opt + 3);
11174 if (i == 0)
11175 mips_pic = NO_PIC;
11176 else if (i == 2)
11177 mips_pic = SVR4_PIC;
11178 else
11179 as_bad (_(".option pic%d not supported"), i);
11180
11181 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11182 {
11183 if (g_switch_seen && g_switch_value != 0)
11184 as_warn (_("-G may not be used with SVR4 PIC code"));
11185 g_switch_value = 0;
11186 bfd_set_gp_size (stdoutput, 0);
11187 }
11188 }
11189 else
11190 as_warn (_("Unrecognized option \"%s\""), opt);
11191
11192 *input_line_pointer = c;
11193 demand_empty_rest_of_line ();
11194 }
11195
11196 /* This structure is used to hold a stack of .set values. */
11197
11198 struct mips_option_stack
11199 {
11200 struct mips_option_stack *next;
11201 struct mips_set_options options;
11202 };
11203
11204 static struct mips_option_stack *mips_opts_stack;
11205
11206 /* Handle the .set pseudo-op. */
11207
11208 static void
11209 s_mipsset (x)
11210 int x ATTRIBUTE_UNUSED;
11211 {
11212 char *name = input_line_pointer, ch;
11213
11214 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11215 input_line_pointer++;
11216 ch = *input_line_pointer;
11217 *input_line_pointer = '\0';
11218
11219 if (strcmp (name, "reorder") == 0)
11220 {
11221 if (mips_opts.noreorder && prev_nop_frag != NULL)
11222 {
11223 /* If we still have pending nops, we can discard them. The
11224 usual nop handling will insert any that are still
11225 needed. */
11226 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11227 * (mips_opts.mips16 ? 2 : 4));
11228 prev_nop_frag = NULL;
11229 }
11230 mips_opts.noreorder = 0;
11231 }
11232 else if (strcmp (name, "noreorder") == 0)
11233 {
11234 mips_emit_delays (true);
11235 mips_opts.noreorder = 1;
11236 mips_any_noreorder = 1;
11237 }
11238 else if (strcmp (name, "at") == 0)
11239 {
11240 mips_opts.noat = 0;
11241 }
11242 else if (strcmp (name, "noat") == 0)
11243 {
11244 mips_opts.noat = 1;
11245 }
11246 else if (strcmp (name, "macro") == 0)
11247 {
11248 mips_opts.warn_about_macros = 0;
11249 }
11250 else if (strcmp (name, "nomacro") == 0)
11251 {
11252 if (mips_opts.noreorder == 0)
11253 as_bad (_("`noreorder' must be set before `nomacro'"));
11254 mips_opts.warn_about_macros = 1;
11255 }
11256 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11257 {
11258 mips_opts.nomove = 0;
11259 }
11260 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11261 {
11262 mips_opts.nomove = 1;
11263 }
11264 else if (strcmp (name, "bopt") == 0)
11265 {
11266 mips_opts.nobopt = 0;
11267 }
11268 else if (strcmp (name, "nobopt") == 0)
11269 {
11270 mips_opts.nobopt = 1;
11271 }
11272 else if (strcmp (name, "mips16") == 0
11273 || strcmp (name, "MIPS-16") == 0)
11274 mips_opts.mips16 = 1;
11275 else if (strcmp (name, "nomips16") == 0
11276 || strcmp (name, "noMIPS-16") == 0)
11277 mips_opts.mips16 = 0;
11278 else if (strncmp (name, "mips", 4) == 0)
11279 {
11280 int isa;
11281
11282 /* Permit the user to change the ISA on the fly. Needless to
11283 say, misuse can cause serious problems. */
11284 isa = atoi (name + 4);
11285 switch (isa)
11286 {
11287 case 0:
11288 mips_opts.gp32 = file_mips_gp32;
11289 mips_opts.fp32 = file_mips_fp32;
11290 mips_opts.abi = file_mips_abi;
11291 break;
11292 case 1:
11293 case 2:
11294 case 32:
11295 mips_opts.gp32 = 1;
11296 mips_opts.fp32 = 1;
11297 break;
11298 case 3:
11299 case 4:
11300 case 5:
11301 case 64:
11302 /* Loosen ABI register width restriction. */
11303 if (mips_opts.abi == O32_ABI)
11304 mips_opts.abi = NO_ABI;
11305 mips_opts.gp32 = 0;
11306 mips_opts.fp32 = 0;
11307 break;
11308 default:
11309 as_bad (_("unknown ISA level %s"), name + 4);
11310 break;
11311 }
11312
11313 switch (isa)
11314 {
11315 case 0: mips_opts.isa = file_mips_isa; break;
11316 case 1: mips_opts.isa = ISA_MIPS1; break;
11317 case 2: mips_opts.isa = ISA_MIPS2; break;
11318 case 3: mips_opts.isa = ISA_MIPS3; break;
11319 case 4: mips_opts.isa = ISA_MIPS4; break;
11320 case 5: mips_opts.isa = ISA_MIPS5; break;
11321 case 32: mips_opts.isa = ISA_MIPS32; break;
11322 case 64: mips_opts.isa = ISA_MIPS64; break;
11323 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11324 }
11325 }
11326 else if (strcmp (name, "autoextend") == 0)
11327 mips_opts.noautoextend = 0;
11328 else if (strcmp (name, "noautoextend") == 0)
11329 mips_opts.noautoextend = 1;
11330 else if (strcmp (name, "push") == 0)
11331 {
11332 struct mips_option_stack *s;
11333
11334 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11335 s->next = mips_opts_stack;
11336 s->options = mips_opts;
11337 mips_opts_stack = s;
11338 }
11339 else if (strcmp (name, "pop") == 0)
11340 {
11341 struct mips_option_stack *s;
11342
11343 s = mips_opts_stack;
11344 if (s == NULL)
11345 as_bad (_(".set pop with no .set push"));
11346 else
11347 {
11348 /* If we're changing the reorder mode we need to handle
11349 delay slots correctly. */
11350 if (s->options.noreorder && ! mips_opts.noreorder)
11351 mips_emit_delays (true);
11352 else if (! s->options.noreorder && mips_opts.noreorder)
11353 {
11354 if (prev_nop_frag != NULL)
11355 {
11356 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11357 * (mips_opts.mips16 ? 2 : 4));
11358 prev_nop_frag = NULL;
11359 }
11360 }
11361
11362 mips_opts = s->options;
11363 mips_opts_stack = s->next;
11364 free (s);
11365 }
11366 }
11367 else
11368 {
11369 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11370 }
11371 *input_line_pointer = ch;
11372 demand_empty_rest_of_line ();
11373 }
11374
11375 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11376 .option pic2. It means to generate SVR4 PIC calls. */
11377
11378 static void
11379 s_abicalls (ignore)
11380 int ignore ATTRIBUTE_UNUSED;
11381 {
11382 mips_pic = SVR4_PIC;
11383 if (USE_GLOBAL_POINTER_OPT)
11384 {
11385 if (g_switch_seen && g_switch_value != 0)
11386 as_warn (_("-G may not be used with SVR4 PIC code"));
11387 g_switch_value = 0;
11388 }
11389 bfd_set_gp_size (stdoutput, 0);
11390 demand_empty_rest_of_line ();
11391 }
11392
11393 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11394 PIC code. It sets the $gp register for the function based on the
11395 function address, which is in the register named in the argument.
11396 This uses a relocation against _gp_disp, which is handled specially
11397 by the linker. The result is:
11398 lui $gp,%hi(_gp_disp)
11399 addiu $gp,$gp,%lo(_gp_disp)
11400 addu $gp,$gp,.cpload argument
11401 The .cpload argument is normally $25 == $t9. */
11402
11403 static void
11404 s_cpload (ignore)
11405 int ignore ATTRIBUTE_UNUSED;
11406 {
11407 expressionS ex;
11408 int icnt = 0;
11409
11410 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11411 .cpload is ignored. */
11412 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11413 {
11414 s_ignore (0);
11415 return;
11416 }
11417
11418 /* .cpload should be in a .set noreorder section. */
11419 if (mips_opts.noreorder == 0)
11420 as_warn (_(".cpload not in noreorder section"));
11421
11422 ex.X_op = O_symbol;
11423 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11424 ex.X_op_symbol = NULL;
11425 ex.X_add_number = 0;
11426
11427 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11428 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11429
11430 macro_build_lui (NULL, &icnt, &ex, GP);
11431 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11432 (int) BFD_RELOC_LO16);
11433
11434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11435 GP, GP, tc_get_register (0));
11436
11437 demand_empty_rest_of_line ();
11438 }
11439
11440 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11441 .cpsetup $reg1, offset|$reg2, label
11442
11443 If offset is given, this results in:
11444 sd $gp, offset($sp)
11445 lui $gp, %hi(%neg(%gp_rel(label)))
11446 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11447 addu $gp, $gp, $reg1
11448
11449 If $reg2 is given, this results in:
11450 daddu $reg2, $gp, $0
11451 lui $gp, %hi(%neg(%gp_rel(label)))
11452 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11453 addu $gp, $gp, $reg1
11454 */
11455 static void
11456 s_cpsetup (ignore)
11457 int ignore ATTRIBUTE_UNUSED;
11458 {
11459 expressionS ex_off;
11460 expressionS ex_sym;
11461 int reg1;
11462 int icnt = 0;
11463 char *sym;
11464
11465 /* If we are not generating SVR4 PIC code, .cpload is ignored.
11466 We also need NewABI support. */
11467 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11468 {
11469 s_ignore (0);
11470 return;
11471 }
11472
11473 reg1 = tc_get_register (0);
11474 SKIP_WHITESPACE ();
11475 if (*input_line_pointer != ',')
11476 {
11477 as_bad (_("missing argument separator ',' for .cpsetup"));
11478 return;
11479 }
11480 else
11481 input_line_pointer++;
11482 SKIP_WHITESPACE ();
11483 if (*input_line_pointer == '$')
11484 mips_cpreturn_register = tc_get_register (0);
11485 else
11486 mips_cpreturn_offset = get_absolute_expression ();
11487 SKIP_WHITESPACE ();
11488 if (*input_line_pointer != ',')
11489 {
11490 as_bad (_("missing argument separator ',' for .cpsetup"));
11491 return;
11492 }
11493 else
11494 input_line_pointer++;
11495 SKIP_WHITESPACE ();
11496 sym = input_line_pointer;
11497 while (ISALNUM (*input_line_pointer))
11498 input_line_pointer++;
11499 *input_line_pointer = 0;
11500
11501 ex_sym.X_op = O_symbol;
11502 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11503 ex_sym.X_op_symbol = NULL;
11504 ex_sym.X_add_number = 0;
11505
11506 if (mips_cpreturn_register == -1)
11507 {
11508 ex_off.X_op = O_constant;
11509 ex_off.X_add_symbol = NULL;
11510 ex_off.X_op_symbol = NULL;
11511 ex_off.X_add_number = mips_cpreturn_offset;
11512
11513 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11514 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11515 }
11516 else
11517 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11518 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11519
11520 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11521 (int) BFD_RELOC_GPREL16);
11522 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11523 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11524 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11525 NULL, 0, 0, BFD_RELOC_HI16_S);
11526 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11527 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11528 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11529 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11530 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11531 NULL, 0, 0, BFD_RELOC_LO16);
11532 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11533 "d,v,t", mips_gp_register, mips_gp_register, reg1);
11534
11535 demand_empty_rest_of_line ();
11536 }
11537
11538 static void
11539 s_cplocal (ignore)
11540 int ignore ATTRIBUTE_UNUSED;
11541 {
11542 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11543 .cplocal is ignored. */
11544 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11545 {
11546 s_ignore (0);
11547 return;
11548 }
11549
11550 mips_gp_register = tc_get_register (0);
11551 }
11552
11553 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11554 offset from $sp. The offset is remembered, and after making a PIC
11555 call $gp is restored from that location. */
11556
11557 static void
11558 s_cprestore (ignore)
11559 int ignore ATTRIBUTE_UNUSED;
11560 {
11561 expressionS ex;
11562 int icnt = 0;
11563
11564 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11565 .cprestore is ignored. */
11566 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11567 {
11568 s_ignore (0);
11569 return;
11570 }
11571
11572 mips_cprestore_offset = get_absolute_expression ();
11573 mips_cprestore_valid = 1;
11574
11575 ex.X_op = O_constant;
11576 ex.X_add_symbol = NULL;
11577 ex.X_op_symbol = NULL;
11578 ex.X_add_number = mips_cprestore_offset;
11579
11580 macro_build ((char *) NULL, &icnt, &ex,
11581 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11582 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11583
11584 demand_empty_rest_of_line ();
11585 }
11586
11587 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11588 was given in the preceeding .gpsetup, it results in:
11589 ld $gp, offset($sp)
11590
11591 If a register $reg2 was given there, it results in:
11592 daddiu $gp, $gp, $reg2
11593 */
11594 static void
11595 s_cpreturn (ignore)
11596 int ignore ATTRIBUTE_UNUSED;
11597 {
11598 expressionS ex;
11599 int icnt = 0;
11600
11601 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11602 We also need NewABI support. */
11603 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11604 {
11605 s_ignore (0);
11606 return;
11607 }
11608
11609 if (mips_cpreturn_register == -1)
11610 {
11611 ex.X_op = O_constant;
11612 ex.X_add_symbol = NULL;
11613 ex.X_op_symbol = NULL;
11614 ex.X_add_number = mips_cpreturn_offset;
11615
11616 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11617 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11618 }
11619 else
11620 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11621 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11622
11623 demand_empty_rest_of_line ();
11624 }
11625
11626 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11627 code. It sets the offset to use in gp_rel relocations. */
11628
11629 static void
11630 s_gpvalue (ignore)
11631 int ignore ATTRIBUTE_UNUSED;
11632 {
11633 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11634 We also need NewABI support. */
11635 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11636 {
11637 s_ignore (0);
11638 return;
11639 }
11640
11641 mips_cpreturn_offset = get_absolute_expression ();
11642
11643 demand_empty_rest_of_line ();
11644 }
11645
11646 /* Handle the .gpword pseudo-op. This is used when generating PIC
11647 code. It generates a 32 bit GP relative reloc. */
11648
11649 static void
11650 s_gpword (ignore)
11651 int ignore ATTRIBUTE_UNUSED;
11652 {
11653 symbolS *label;
11654 expressionS ex;
11655 char *p;
11656
11657 /* When not generating PIC code, this is treated as .word. */
11658 if (mips_pic != SVR4_PIC)
11659 {
11660 s_cons (2);
11661 return;
11662 }
11663
11664 label = insn_labels != NULL ? insn_labels->label : NULL;
11665 mips_emit_delays (true);
11666 if (auto_align)
11667 mips_align (2, 0, label);
11668 mips_clear_insn_labels ();
11669
11670 expression (&ex);
11671
11672 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11673 {
11674 as_bad (_("Unsupported use of .gpword"));
11675 ignore_rest_of_line ();
11676 }
11677
11678 p = frag_more (4);
11679 md_number_to_chars (p, (valueT) 0, 4);
11680 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11681 BFD_RELOC_GPREL32);
11682
11683 demand_empty_rest_of_line ();
11684 }
11685
11686 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11687 tables in SVR4 PIC code. */
11688
11689 static void
11690 s_cpadd (ignore)
11691 int ignore ATTRIBUTE_UNUSED;
11692 {
11693 int icnt = 0;
11694 int reg;
11695
11696 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11697 code. */
11698 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11699 {
11700 s_ignore (0);
11701 return;
11702 }
11703
11704 /* Add $gp to the register named as an argument. */
11705 reg = tc_get_register (0);
11706 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11707 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11708 "d,v,t", reg, reg, GP);
11709
11710 demand_empty_rest_of_line ();
11711 }
11712
11713 /* Handle the .insn pseudo-op. This marks instruction labels in
11714 mips16 mode. This permits the linker to handle them specially,
11715 such as generating jalx instructions when needed. We also make
11716 them odd for the duration of the assembly, in order to generate the
11717 right sort of code. We will make them even in the adjust_symtab
11718 routine, while leaving them marked. This is convenient for the
11719 debugger and the disassembler. The linker knows to make them odd
11720 again. */
11721
11722 static void
11723 s_insn (ignore)
11724 int ignore ATTRIBUTE_UNUSED;
11725 {
11726 if (mips_opts.mips16)
11727 mips16_mark_labels ();
11728
11729 demand_empty_rest_of_line ();
11730 }
11731
11732 /* Handle a .stabn directive. We need these in order to mark a label
11733 as being a mips16 text label correctly. Sometimes the compiler
11734 will emit a label, followed by a .stabn, and then switch sections.
11735 If the label and .stabn are in mips16 mode, then the label is
11736 really a mips16 text label. */
11737
11738 static void
11739 s_mips_stab (type)
11740 int type;
11741 {
11742 if (type == 'n' && mips_opts.mips16)
11743 mips16_mark_labels ();
11744
11745 s_stab (type);
11746 }
11747
11748 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11749 */
11750
11751 static void
11752 s_mips_weakext (ignore)
11753 int ignore ATTRIBUTE_UNUSED;
11754 {
11755 char *name;
11756 int c;
11757 symbolS *symbolP;
11758 expressionS exp;
11759
11760 name = input_line_pointer;
11761 c = get_symbol_end ();
11762 symbolP = symbol_find_or_make (name);
11763 S_SET_WEAK (symbolP);
11764 *input_line_pointer = c;
11765
11766 SKIP_WHITESPACE ();
11767
11768 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11769 {
11770 if (S_IS_DEFINED (symbolP))
11771 {
11772 as_bad ("ignoring attempt to redefine symbol %s",
11773 S_GET_NAME (symbolP));
11774 ignore_rest_of_line ();
11775 return;
11776 }
11777
11778 if (*input_line_pointer == ',')
11779 {
11780 ++input_line_pointer;
11781 SKIP_WHITESPACE ();
11782 }
11783
11784 expression (&exp);
11785 if (exp.X_op != O_symbol)
11786 {
11787 as_bad ("bad .weakext directive");
11788 ignore_rest_of_line();
11789 return;
11790 }
11791 symbol_set_value_expression (symbolP, &exp);
11792 }
11793
11794 demand_empty_rest_of_line ();
11795 }
11796
11797 /* Parse a register string into a number. Called from the ECOFF code
11798 to parse .frame. The argument is non-zero if this is the frame
11799 register, so that we can record it in mips_frame_reg. */
11800
11801 int
11802 tc_get_register (frame)
11803 int frame;
11804 {
11805 int reg;
11806
11807 SKIP_WHITESPACE ();
11808 if (*input_line_pointer++ != '$')
11809 {
11810 as_warn (_("expected `$'"));
11811 reg = 0;
11812 }
11813 else if (ISDIGIT (*input_line_pointer))
11814 {
11815 reg = get_absolute_expression ();
11816 if (reg < 0 || reg >= 32)
11817 {
11818 as_warn (_("Bad register number"));
11819 reg = 0;
11820 }
11821 }
11822 else
11823 {
11824 if (strncmp (input_line_pointer, "fp", 2) == 0)
11825 reg = FP;
11826 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11827 reg = SP;
11828 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11829 reg = GP;
11830 else if (strncmp (input_line_pointer, "at", 2) == 0)
11831 reg = AT;
11832 else
11833 {
11834 as_warn (_("Unrecognized register name"));
11835 reg = 0;
11836 }
11837 input_line_pointer += 2;
11838 }
11839 if (frame)
11840 {
11841 mips_frame_reg = reg != 0 ? reg : SP;
11842 mips_frame_reg_valid = 1;
11843 mips_cprestore_valid = 0;
11844 }
11845 return reg;
11846 }
11847
11848 valueT
11849 md_section_align (seg, addr)
11850 asection *seg;
11851 valueT addr;
11852 {
11853 int align = bfd_get_section_alignment (stdoutput, seg);
11854
11855 #ifdef OBJ_ELF
11856 /* We don't need to align ELF sections to the full alignment.
11857 However, Irix 5 may prefer that we align them at least to a 16
11858 byte boundary. We don't bother to align the sections if we are
11859 targeted for an embedded system. */
11860 if (strcmp (TARGET_OS, "elf") == 0)
11861 return addr;
11862 if (align > 4)
11863 align = 4;
11864 #endif
11865
11866 return ((addr + (1 << align) - 1) & (-1 << align));
11867 }
11868
11869 /* Utility routine, called from above as well. If called while the
11870 input file is still being read, it's only an approximation. (For
11871 example, a symbol may later become defined which appeared to be
11872 undefined earlier.) */
11873
11874 static int
11875 nopic_need_relax (sym, before_relaxing)
11876 symbolS *sym;
11877 int before_relaxing;
11878 {
11879 if (sym == 0)
11880 return 0;
11881
11882 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11883 {
11884 const char *symname;
11885 int change;
11886
11887 /* Find out whether this symbol can be referenced off the GP
11888 register. It can be if it is smaller than the -G size or if
11889 it is in the .sdata or .sbss section. Certain symbols can
11890 not be referenced off the GP, although it appears as though
11891 they can. */
11892 symname = S_GET_NAME (sym);
11893 if (symname != (const char *) NULL
11894 && (strcmp (symname, "eprol") == 0
11895 || strcmp (symname, "etext") == 0
11896 || strcmp (symname, "_gp") == 0
11897 || strcmp (symname, "edata") == 0
11898 || strcmp (symname, "_fbss") == 0
11899 || strcmp (symname, "_fdata") == 0
11900 || strcmp (symname, "_ftext") == 0
11901 || strcmp (symname, "end") == 0
11902 || strcmp (symname, "_gp_disp") == 0))
11903 change = 1;
11904 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11905 && (0
11906 #ifndef NO_ECOFF_DEBUGGING
11907 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11908 && (symbol_get_obj (sym)->ecoff_extern_size
11909 <= g_switch_value))
11910 #endif
11911 /* We must defer this decision until after the whole
11912 file has been read, since there might be a .extern
11913 after the first use of this symbol. */
11914 || (before_relaxing
11915 #ifndef NO_ECOFF_DEBUGGING
11916 && symbol_get_obj (sym)->ecoff_extern_size == 0
11917 #endif
11918 && S_GET_VALUE (sym) == 0)
11919 || (S_GET_VALUE (sym) != 0
11920 && S_GET_VALUE (sym) <= g_switch_value)))
11921 change = 0;
11922 else
11923 {
11924 const char *segname;
11925
11926 segname = segment_name (S_GET_SEGMENT (sym));
11927 assert (strcmp (segname, ".lit8") != 0
11928 && strcmp (segname, ".lit4") != 0);
11929 change = (strcmp (segname, ".sdata") != 0
11930 && strcmp (segname, ".sbss") != 0
11931 && strncmp (segname, ".sdata.", 7) != 0
11932 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11933 }
11934 return change;
11935 }
11936 else
11937 /* We are not optimizing for the GP register. */
11938 return 1;
11939 }
11940
11941 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11942 extended opcode. SEC is the section the frag is in. */
11943
11944 static int
11945 mips16_extended_frag (fragp, sec, stretch)
11946 fragS *fragp;
11947 asection *sec;
11948 long stretch;
11949 {
11950 int type;
11951 register const struct mips16_immed_operand *op;
11952 offsetT val;
11953 int mintiny, maxtiny;
11954 segT symsec;
11955 fragS *sym_frag;
11956
11957 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11958 return 0;
11959 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11960 return 1;
11961
11962 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11963 op = mips16_immed_operands;
11964 while (op->type != type)
11965 {
11966 ++op;
11967 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11968 }
11969
11970 if (op->unsp)
11971 {
11972 if (type == '<' || type == '>' || type == '[' || type == ']')
11973 {
11974 mintiny = 1;
11975 maxtiny = 1 << op->nbits;
11976 }
11977 else
11978 {
11979 mintiny = 0;
11980 maxtiny = (1 << op->nbits) - 1;
11981 }
11982 }
11983 else
11984 {
11985 mintiny = - (1 << (op->nbits - 1));
11986 maxtiny = (1 << (op->nbits - 1)) - 1;
11987 }
11988
11989 sym_frag = symbol_get_frag (fragp->fr_symbol);
11990 val = S_GET_VALUE (fragp->fr_symbol);
11991 symsec = S_GET_SEGMENT (fragp->fr_symbol);
11992
11993 if (op->pcrel)
11994 {
11995 addressT addr;
11996
11997 /* We won't have the section when we are called from
11998 mips_relax_frag. However, we will always have been called
11999 from md_estimate_size_before_relax first. If this is a
12000 branch to a different section, we mark it as such. If SEC is
12001 NULL, and the frag is not marked, then it must be a branch to
12002 the same section. */
12003 if (sec == NULL)
12004 {
12005 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12006 return 1;
12007 }
12008 else
12009 {
12010 /* Must have been called from md_estimate_size_before_relax. */
12011 if (symsec != sec)
12012 {
12013 fragp->fr_subtype =
12014 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12015
12016 /* FIXME: We should support this, and let the linker
12017 catch branches and loads that are out of range. */
12018 as_bad_where (fragp->fr_file, fragp->fr_line,
12019 _("unsupported PC relative reference to different section"));
12020
12021 return 1;
12022 }
12023 if (fragp != sym_frag && sym_frag->fr_address == 0)
12024 /* Assume non-extended on the first relaxation pass.
12025 The address we have calculated will be bogus if this is
12026 a forward branch to another frag, as the forward frag
12027 will have fr_address == 0. */
12028 return 0;
12029 }
12030
12031 /* In this case, we know for sure that the symbol fragment is in
12032 the same section. If the relax_marker of the symbol fragment
12033 differs from the relax_marker of this fragment, we have not
12034 yet adjusted the symbol fragment fr_address. We want to add
12035 in STRETCH in order to get a better estimate of the address.
12036 This particularly matters because of the shift bits. */
12037 if (stretch != 0
12038 && sym_frag->relax_marker != fragp->relax_marker)
12039 {
12040 fragS *f;
12041
12042 /* Adjust stretch for any alignment frag. Note that if have
12043 been expanding the earlier code, the symbol may be
12044 defined in what appears to be an earlier frag. FIXME:
12045 This doesn't handle the fr_subtype field, which specifies
12046 a maximum number of bytes to skip when doing an
12047 alignment. */
12048 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
12049 {
12050 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12051 {
12052 if (stretch < 0)
12053 stretch = - ((- stretch)
12054 & ~ ((1 << (int) f->fr_offset) - 1));
12055 else
12056 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12057 if (stretch == 0)
12058 break;
12059 }
12060 }
12061 if (f != NULL)
12062 val += stretch;
12063 }
12064
12065 addr = fragp->fr_address + fragp->fr_fix;
12066
12067 /* The base address rules are complicated. The base address of
12068 a branch is the following instruction. The base address of a
12069 PC relative load or add is the instruction itself, but if it
12070 is in a delay slot (in which case it can not be extended) use
12071 the address of the instruction whose delay slot it is in. */
12072 if (type == 'p' || type == 'q')
12073 {
12074 addr += 2;
12075
12076 /* If we are currently assuming that this frag should be
12077 extended, then, the current address is two bytes
12078 higher. */
12079 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12080 addr += 2;
12081
12082 /* Ignore the low bit in the target, since it will be set
12083 for a text label. */
12084 if ((val & 1) != 0)
12085 --val;
12086 }
12087 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12088 addr -= 4;
12089 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12090 addr -= 2;
12091
12092 val -= addr & ~ ((1 << op->shift) - 1);
12093
12094 /* Branch offsets have an implicit 0 in the lowest bit. */
12095 if (type == 'p' || type == 'q')
12096 val /= 2;
12097
12098 /* If any of the shifted bits are set, we must use an extended
12099 opcode. If the address depends on the size of this
12100 instruction, this can lead to a loop, so we arrange to always
12101 use an extended opcode. We only check this when we are in
12102 the main relaxation loop, when SEC is NULL. */
12103 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12104 {
12105 fragp->fr_subtype =
12106 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12107 return 1;
12108 }
12109
12110 /* If we are about to mark a frag as extended because the value
12111 is precisely maxtiny + 1, then there is a chance of an
12112 infinite loop as in the following code:
12113 la $4,foo
12114 .skip 1020
12115 .align 2
12116 foo:
12117 In this case when the la is extended, foo is 0x3fc bytes
12118 away, so the la can be shrunk, but then foo is 0x400 away, so
12119 the la must be extended. To avoid this loop, we mark the
12120 frag as extended if it was small, and is about to become
12121 extended with a value of maxtiny + 1. */
12122 if (val == ((maxtiny + 1) << op->shift)
12123 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12124 && sec == NULL)
12125 {
12126 fragp->fr_subtype =
12127 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12128 return 1;
12129 }
12130 }
12131 else if (symsec != absolute_section && sec != NULL)
12132 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12133
12134 if ((val & ((1 << op->shift) - 1)) != 0
12135 || val < (mintiny << op->shift)
12136 || val > (maxtiny << op->shift))
12137 return 1;
12138 else
12139 return 0;
12140 }
12141
12142 /* Estimate the size of a frag before relaxing. Unless this is the
12143 mips16, we are not really relaxing here, and the final size is
12144 encoded in the subtype information. For the mips16, we have to
12145 decide whether we are using an extended opcode or not. */
12146
12147 int
12148 md_estimate_size_before_relax (fragp, segtype)
12149 fragS *fragp;
12150 asection *segtype;
12151 {
12152 int change = 0;
12153 boolean linkonce = false;
12154
12155 if (RELAX_MIPS16_P (fragp->fr_subtype))
12156 {
12157 if (mips16_extended_frag (fragp, segtype, 0))
12158 {
12159 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12160 return 4;
12161 }
12162 else
12163 {
12164 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12165 return 2;
12166 }
12167 }
12168
12169 if (mips_pic == NO_PIC)
12170 {
12171 change = nopic_need_relax (fragp->fr_symbol, 0);
12172 }
12173 else if (mips_pic == SVR4_PIC)
12174 {
12175 symbolS *sym;
12176 asection *symsec;
12177
12178 sym = fragp->fr_symbol;
12179
12180 /* Handle the case of a symbol equated to another symbol. */
12181 while (symbol_equated_reloc_p (sym))
12182 {
12183 symbolS *n;
12184
12185 /* It's possible to get a loop here in a badly written
12186 program. */
12187 n = symbol_get_value_expression (sym)->X_add_symbol;
12188 if (n == sym)
12189 break;
12190 sym = n;
12191 }
12192
12193 symsec = S_GET_SEGMENT (sym);
12194
12195 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12196 if (symsec != segtype && ! S_IS_LOCAL (sym))
12197 {
12198 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12199 != 0)
12200 linkonce = true;
12201
12202 /* The GNU toolchain uses an extension for ELF: a section
12203 beginning with the magic string .gnu.linkonce is a linkonce
12204 section. */
12205 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12206 sizeof ".gnu.linkonce" - 1) == 0)
12207 linkonce = true;
12208 }
12209
12210 /* This must duplicate the test in adjust_reloc_syms. */
12211 change = (symsec != &bfd_und_section
12212 && symsec != &bfd_abs_section
12213 && ! bfd_is_com_section (symsec)
12214 && !linkonce
12215 #ifdef OBJ_ELF
12216 /* A global or weak symbol is treated as external. */
12217 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12218 || (! S_IS_WEAK (sym)
12219 && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
12220 #endif
12221 );
12222 }
12223 else
12224 abort ();
12225
12226 if (change)
12227 {
12228 /* Record the offset to the first reloc in the fr_opcode field.
12229 This lets md_convert_frag and tc_gen_reloc know that the code
12230 must be expanded. */
12231 fragp->fr_opcode = (fragp->fr_literal
12232 + fragp->fr_fix
12233 - RELAX_OLD (fragp->fr_subtype)
12234 + RELAX_RELOC1 (fragp->fr_subtype));
12235 /* FIXME: This really needs as_warn_where. */
12236 if (RELAX_WARN (fragp->fr_subtype))
12237 as_warn (_("AT used after \".set noat\" or macro used after "
12238 "\".set nomacro\""));
12239
12240 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12241 }
12242
12243 return 0;
12244 }
12245
12246 /* This is called to see whether a reloc against a defined symbol
12247 should be converted into a reloc against a section. Don't adjust
12248 MIPS16 jump relocations, so we don't have to worry about the format
12249 of the offset in the .o file. Don't adjust relocations against
12250 mips16 symbols, so that the linker can find them if it needs to set
12251 up a stub. */
12252
12253 int
12254 mips_fix_adjustable (fixp)
12255 fixS *fixp;
12256 {
12257 #ifdef OBJ_ELF
12258 /* Prevent all adjustments to global symbols. */
12259 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12260 && mips_pic != EMBEDDED_PIC
12261 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12262 return 0;
12263 #endif
12264 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12265 return 0;
12266 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12267 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12268 return 0;
12269 if (fixp->fx_addsy == NULL)
12270 return 1;
12271 #ifdef OBJ_ELF
12272 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12273 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12274 && fixp->fx_subsy == NULL)
12275 return 0;
12276 #endif
12277 return 1;
12278 }
12279
12280 /* Translate internal representation of relocation info to BFD target
12281 format. */
12282
12283 arelent **
12284 tc_gen_reloc (section, fixp)
12285 asection *section ATTRIBUTE_UNUSED;
12286 fixS *fixp;
12287 {
12288 static arelent *retval[4];
12289 arelent *reloc;
12290 bfd_reloc_code_real_type code;
12291
12292 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12293 retval[1] = NULL;
12294
12295 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12296 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12297 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12298
12299 if (mips_pic == EMBEDDED_PIC
12300 && SWITCH_TABLE (fixp))
12301 {
12302 /* For a switch table entry we use a special reloc. The addend
12303 is actually the difference between the reloc address and the
12304 subtrahend. */
12305 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12306 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12307 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12308 fixp->fx_r_type = BFD_RELOC_GPREL32;
12309 }
12310 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12311 {
12312 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12313 reloc->addend = fixp->fx_addnumber;
12314 else
12315 {
12316 /* We use a special addend for an internal RELLO reloc. */
12317 if (symbol_section_p (fixp->fx_addsy))
12318 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12319 else
12320 reloc->addend = fixp->fx_addnumber + reloc->address;
12321 }
12322 }
12323 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12324 {
12325 assert (fixp->fx_next != NULL
12326 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12327
12328 /* The reloc is relative to the RELLO; adjust the addend
12329 accordingly. */
12330 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12331 reloc->addend = fixp->fx_next->fx_addnumber;
12332 else
12333 {
12334 /* We use a special addend for an internal RELHI reloc. */
12335 if (symbol_section_p (fixp->fx_addsy))
12336 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12337 + fixp->fx_next->fx_where
12338 - S_GET_VALUE (fixp->fx_subsy));
12339 else
12340 reloc->addend = (fixp->fx_addnumber
12341 + fixp->fx_next->fx_frag->fr_address
12342 + fixp->fx_next->fx_where);
12343 }
12344 }
12345 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12346 reloc->addend = fixp->fx_addnumber;
12347 else
12348 {
12349 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12350 /* A gruesome hack which is a result of the gruesome gas reloc
12351 handling. */
12352 reloc->addend = reloc->address;
12353 else
12354 reloc->addend = -reloc->address;
12355 }
12356
12357 /* If this is a variant frag, we may need to adjust the existing
12358 reloc and generate a new one. */
12359 if (fixp->fx_frag->fr_opcode != NULL
12360 && (fixp->fx_r_type == BFD_RELOC_GPREL16
12361 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12362 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12363 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12364 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12365 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12366 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12367 && ! HAVE_NEWABI)
12368 {
12369 arelent *reloc2;
12370
12371 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12372
12373 /* If this is not the last reloc in this frag, then we have two
12374 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12375 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12376 the second one handle all of them. */
12377 if (fixp->fx_next != NULL
12378 && fixp->fx_frag == fixp->fx_next->fx_frag)
12379 {
12380 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12381 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12382 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12383 && (fixp->fx_next->fx_r_type
12384 == BFD_RELOC_MIPS_GOT_LO16))
12385 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12386 && (fixp->fx_next->fx_r_type
12387 == BFD_RELOC_MIPS_CALL_LO16)));
12388 retval[0] = NULL;
12389 return retval;
12390 }
12391
12392 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12393 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12394 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12395 retval[2] = NULL;
12396 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12397 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12398 reloc2->address = (reloc->address
12399 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12400 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12401 reloc2->addend = fixp->fx_addnumber;
12402 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12403 assert (reloc2->howto != NULL);
12404
12405 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12406 {
12407 arelent *reloc3;
12408
12409 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12410 retval[3] = NULL;
12411 *reloc3 = *reloc2;
12412 reloc3->address += 4;
12413 }
12414
12415 if (mips_pic == NO_PIC)
12416 {
12417 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12418 fixp->fx_r_type = BFD_RELOC_HI16_S;
12419 }
12420 else if (mips_pic == SVR4_PIC)
12421 {
12422 switch (fixp->fx_r_type)
12423 {
12424 default:
12425 abort ();
12426 case BFD_RELOC_MIPS_GOT16:
12427 break;
12428 case BFD_RELOC_MIPS_CALL16:
12429 case BFD_RELOC_MIPS_GOT_LO16:
12430 case BFD_RELOC_MIPS_CALL_LO16:
12431 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12432 break;
12433 }
12434 }
12435 else
12436 abort ();
12437 }
12438
12439 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12440 to be used in the relocation's section offset. */
12441 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12442 {
12443 reloc->address = reloc->addend;
12444 reloc->addend = 0;
12445 }
12446
12447 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12448 fixup_segment converted a non-PC relative reloc into a PC
12449 relative reloc. In such a case, we need to convert the reloc
12450 code. */
12451 code = fixp->fx_r_type;
12452 if (fixp->fx_pcrel)
12453 {
12454 switch (code)
12455 {
12456 case BFD_RELOC_8:
12457 code = BFD_RELOC_8_PCREL;
12458 break;
12459 case BFD_RELOC_16:
12460 code = BFD_RELOC_16_PCREL;
12461 break;
12462 case BFD_RELOC_32:
12463 code = BFD_RELOC_32_PCREL;
12464 break;
12465 case BFD_RELOC_64:
12466 code = BFD_RELOC_64_PCREL;
12467 break;
12468 case BFD_RELOC_8_PCREL:
12469 case BFD_RELOC_16_PCREL:
12470 case BFD_RELOC_32_PCREL:
12471 case BFD_RELOC_64_PCREL:
12472 case BFD_RELOC_16_PCREL_S2:
12473 case BFD_RELOC_PCREL_HI16_S:
12474 case BFD_RELOC_PCREL_LO16:
12475 break;
12476 default:
12477 as_bad_where (fixp->fx_file, fixp->fx_line,
12478 _("Cannot make %s relocation PC relative"),
12479 bfd_get_reloc_code_name (code));
12480 }
12481 }
12482
12483 #ifdef OBJ_ELF
12484 /* md_apply_fix3 has a double-subtraction hack to get
12485 bfd_install_relocation to behave nicely. GPREL relocations are
12486 handled correctly without this hack, so undo it here. We can't
12487 stop md_apply_fix3 from subtracting twice in the first place since
12488 the fake addend is required for variant frags above. */
12489 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12490 && code == BFD_RELOC_GPREL16
12491 && reloc->addend != 0
12492 && mips_need_elf_addend_fixup (fixp))
12493 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12494 #endif
12495
12496 /* To support a PC relative reloc when generating embedded PIC code
12497 for ECOFF, we use a Cygnus extension. We check for that here to
12498 make sure that we don't let such a reloc escape normally. */
12499 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12500 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12501 && code == BFD_RELOC_16_PCREL_S2
12502 && mips_pic != EMBEDDED_PIC)
12503 reloc->howto = NULL;
12504 else
12505 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12506
12507 if (reloc->howto == NULL)
12508 {
12509 as_bad_where (fixp->fx_file, fixp->fx_line,
12510 _("Can not represent %s relocation in this object file format"),
12511 bfd_get_reloc_code_name (code));
12512 retval[0] = NULL;
12513 }
12514
12515 return retval;
12516 }
12517
12518 /* Relax a machine dependent frag. This returns the amount by which
12519 the current size of the frag should change. */
12520
12521 int
12522 mips_relax_frag (fragp, stretch)
12523 fragS *fragp;
12524 long stretch;
12525 {
12526 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12527 return 0;
12528
12529 if (mips16_extended_frag (fragp, NULL, stretch))
12530 {
12531 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12532 return 0;
12533 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12534 return 2;
12535 }
12536 else
12537 {
12538 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12539 return 0;
12540 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12541 return -2;
12542 }
12543
12544 return 0;
12545 }
12546
12547 /* Convert a machine dependent frag. */
12548
12549 void
12550 md_convert_frag (abfd, asec, fragp)
12551 bfd *abfd ATTRIBUTE_UNUSED;
12552 segT asec;
12553 fragS *fragp;
12554 {
12555 int old, new;
12556 char *fixptr;
12557
12558 if (RELAX_MIPS16_P (fragp->fr_subtype))
12559 {
12560 int type;
12561 register const struct mips16_immed_operand *op;
12562 boolean small, ext;
12563 offsetT val;
12564 bfd_byte *buf;
12565 unsigned long insn;
12566 boolean use_extend;
12567 unsigned short extend;
12568
12569 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12570 op = mips16_immed_operands;
12571 while (op->type != type)
12572 ++op;
12573
12574 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12575 {
12576 small = false;
12577 ext = true;
12578 }
12579 else
12580 {
12581 small = true;
12582 ext = false;
12583 }
12584
12585 resolve_symbol_value (fragp->fr_symbol);
12586 val = S_GET_VALUE (fragp->fr_symbol);
12587 if (op->pcrel)
12588 {
12589 addressT addr;
12590
12591 addr = fragp->fr_address + fragp->fr_fix;
12592
12593 /* The rules for the base address of a PC relative reloc are
12594 complicated; see mips16_extended_frag. */
12595 if (type == 'p' || type == 'q')
12596 {
12597 addr += 2;
12598 if (ext)
12599 addr += 2;
12600 /* Ignore the low bit in the target, since it will be
12601 set for a text label. */
12602 if ((val & 1) != 0)
12603 --val;
12604 }
12605 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12606 addr -= 4;
12607 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12608 addr -= 2;
12609
12610 addr &= ~ (addressT) ((1 << op->shift) - 1);
12611 val -= addr;
12612
12613 /* Make sure the section winds up with the alignment we have
12614 assumed. */
12615 if (op->shift > 0)
12616 record_alignment (asec, op->shift);
12617 }
12618
12619 if (ext
12620 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12621 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12622 as_warn_where (fragp->fr_file, fragp->fr_line,
12623 _("extended instruction in delay slot"));
12624
12625 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12626
12627 if (target_big_endian)
12628 insn = bfd_getb16 (buf);
12629 else
12630 insn = bfd_getl16 (buf);
12631
12632 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12633 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12634 small, ext, &insn, &use_extend, &extend);
12635
12636 if (use_extend)
12637 {
12638 md_number_to_chars (buf, 0xf000 | extend, 2);
12639 fragp->fr_fix += 2;
12640 buf += 2;
12641 }
12642
12643 md_number_to_chars (buf, insn, 2);
12644 fragp->fr_fix += 2;
12645 buf += 2;
12646 }
12647 else
12648 {
12649 if (fragp->fr_opcode == NULL)
12650 return;
12651
12652 old = RELAX_OLD (fragp->fr_subtype);
12653 new = RELAX_NEW (fragp->fr_subtype);
12654 fixptr = fragp->fr_literal + fragp->fr_fix;
12655
12656 if (new > 0)
12657 memcpy (fixptr - old, fixptr, new);
12658
12659 fragp->fr_fix += new - old;
12660 }
12661 }
12662
12663 #ifdef OBJ_ELF
12664
12665 /* This function is called after the relocs have been generated.
12666 We've been storing mips16 text labels as odd. Here we convert them
12667 back to even for the convenience of the debugger. */
12668
12669 void
12670 mips_frob_file_after_relocs ()
12671 {
12672 asymbol **syms;
12673 unsigned int count, i;
12674
12675 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12676 return;
12677
12678 syms = bfd_get_outsymbols (stdoutput);
12679 count = bfd_get_symcount (stdoutput);
12680 for (i = 0; i < count; i++, syms++)
12681 {
12682 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12683 && ((*syms)->value & 1) != 0)
12684 {
12685 (*syms)->value &= ~1;
12686 /* If the symbol has an odd size, it was probably computed
12687 incorrectly, so adjust that as well. */
12688 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12689 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12690 }
12691 }
12692 }
12693
12694 #endif
12695
12696 /* This function is called whenever a label is defined. It is used
12697 when handling branch delays; if a branch has a label, we assume we
12698 can not move it. */
12699
12700 void
12701 mips_define_label (sym)
12702 symbolS *sym;
12703 {
12704 struct insn_label_list *l;
12705
12706 if (free_insn_labels == NULL)
12707 l = (struct insn_label_list *) xmalloc (sizeof *l);
12708 else
12709 {
12710 l = free_insn_labels;
12711 free_insn_labels = l->next;
12712 }
12713
12714 l->label = sym;
12715 l->next = insn_labels;
12716 insn_labels = l;
12717 }
12718 \f
12719 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12720
12721 /* Some special processing for a MIPS ELF file. */
12722
12723 void
12724 mips_elf_final_processing ()
12725 {
12726 /* Write out the register information. */
12727 if (! HAVE_NEWABI)
12728 {
12729 Elf32_RegInfo s;
12730
12731 s.ri_gprmask = mips_gprmask;
12732 s.ri_cprmask[0] = mips_cprmask[0];
12733 s.ri_cprmask[1] = mips_cprmask[1];
12734 s.ri_cprmask[2] = mips_cprmask[2];
12735 s.ri_cprmask[3] = mips_cprmask[3];
12736 /* The gp_value field is set by the MIPS ELF backend. */
12737
12738 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12739 ((Elf32_External_RegInfo *)
12740 mips_regmask_frag));
12741 }
12742 else
12743 {
12744 Elf64_Internal_RegInfo s;
12745
12746 s.ri_gprmask = mips_gprmask;
12747 s.ri_pad = 0;
12748 s.ri_cprmask[0] = mips_cprmask[0];
12749 s.ri_cprmask[1] = mips_cprmask[1];
12750 s.ri_cprmask[2] = mips_cprmask[2];
12751 s.ri_cprmask[3] = mips_cprmask[3];
12752 /* The gp_value field is set by the MIPS ELF backend. */
12753
12754 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12755 ((Elf64_External_RegInfo *)
12756 mips_regmask_frag));
12757 }
12758
12759 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12760 sort of BFD interface for this. */
12761 if (mips_any_noreorder)
12762 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12763 if (mips_pic != NO_PIC)
12764 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12765
12766 /* Set the MIPS ELF ABI flags. */
12767 if (file_mips_abi == NO_ABI)
12768 ;
12769 else if (file_mips_abi == O32_ABI)
12770 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12771 else if (file_mips_abi == O64_ABI)
12772 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12773 else if (file_mips_abi == EABI_ABI)
12774 {
12775 if (mips_eabi64)
12776 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12777 else
12778 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12779 }
12780 else if (file_mips_abi == N32_ABI)
12781 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12782
12783 /* Nothing to do for "64". */
12784
12785 if (mips_32bitmode)
12786 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12787 }
12788
12789 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12790 \f
12791 typedef struct proc {
12792 symbolS *isym;
12793 unsigned long reg_mask;
12794 unsigned long reg_offset;
12795 unsigned long fpreg_mask;
12796 unsigned long fpreg_offset;
12797 unsigned long frame_offset;
12798 unsigned long frame_reg;
12799 unsigned long pc_reg;
12800 } procS;
12801
12802 static procS cur_proc;
12803 static procS *cur_proc_ptr;
12804 static int numprocs;
12805
12806 /* Fill in an rs_align_code fragment. */
12807
12808 void
12809 mips_handle_align (fragp)
12810 fragS *fragp;
12811 {
12812 if (fragp->fr_type != rs_align_code)
12813 return;
12814
12815 if (mips_opts.mips16)
12816 {
12817 static const unsigned char be_nop[] = { 0x65, 0x00 };
12818 static const unsigned char le_nop[] = { 0x00, 0x65 };
12819
12820 int bytes;
12821 char *p;
12822
12823 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12824 p = fragp->fr_literal + fragp->fr_fix;
12825
12826 if (bytes & 1)
12827 {
12828 *p++ = 0;
12829 fragp->fr_fix += 1;
12830 }
12831
12832 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12833 fragp->fr_var = 2;
12834 }
12835
12836 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
12837 }
12838
12839 static void
12840 md_obj_begin ()
12841 {
12842 }
12843
12844 static void
12845 md_obj_end ()
12846 {
12847 /* check for premature end, nesting errors, etc */
12848 if (cur_proc_ptr)
12849 as_warn (_("missing .end at end of assembly"));
12850 }
12851
12852 static long
12853 get_number ()
12854 {
12855 int negative = 0;
12856 long val = 0;
12857
12858 if (*input_line_pointer == '-')
12859 {
12860 ++input_line_pointer;
12861 negative = 1;
12862 }
12863 if (!ISDIGIT (*input_line_pointer))
12864 as_bad (_("expected simple number"));
12865 if (input_line_pointer[0] == '0')
12866 {
12867 if (input_line_pointer[1] == 'x')
12868 {
12869 input_line_pointer += 2;
12870 while (ISXDIGIT (*input_line_pointer))
12871 {
12872 val <<= 4;
12873 val |= hex_value (*input_line_pointer++);
12874 }
12875 return negative ? -val : val;
12876 }
12877 else
12878 {
12879 ++input_line_pointer;
12880 while (ISDIGIT (*input_line_pointer))
12881 {
12882 val <<= 3;
12883 val |= *input_line_pointer++ - '0';
12884 }
12885 return negative ? -val : val;
12886 }
12887 }
12888 if (!ISDIGIT (*input_line_pointer))
12889 {
12890 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12891 *input_line_pointer, *input_line_pointer);
12892 as_warn (_("invalid number"));
12893 return -1;
12894 }
12895 while (ISDIGIT (*input_line_pointer))
12896 {
12897 val *= 10;
12898 val += *input_line_pointer++ - '0';
12899 }
12900 return negative ? -val : val;
12901 }
12902
12903 /* The .file directive; just like the usual .file directive, but there
12904 is an initial number which is the ECOFF file index. */
12905
12906 static void
12907 s_file (x)
12908 int x ATTRIBUTE_UNUSED;
12909 {
12910 int line;
12911
12912 line = get_number ();
12913 s_app_file (0);
12914 }
12915
12916 /* The .end directive. */
12917
12918 static void
12919 s_mips_end (x)
12920 int x ATTRIBUTE_UNUSED;
12921 {
12922 symbolS *p;
12923 int maybe_text;
12924
12925 /* Following functions need their own .frame and .cprestore directives. */
12926 mips_frame_reg_valid = 0;
12927 mips_cprestore_valid = 0;
12928
12929 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12930 {
12931 p = get_symbol ();
12932 demand_empty_rest_of_line ();
12933 }
12934 else
12935 p = NULL;
12936
12937 #ifdef BFD_ASSEMBLER
12938 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12939 maybe_text = 1;
12940 else
12941 maybe_text = 0;
12942 #else
12943 if (now_seg != data_section && now_seg != bss_section)
12944 maybe_text = 1;
12945 else
12946 maybe_text = 0;
12947 #endif
12948
12949 if (!maybe_text)
12950 as_warn (_(".end not in text section"));
12951
12952 if (!cur_proc_ptr)
12953 {
12954 as_warn (_(".end directive without a preceding .ent directive."));
12955 demand_empty_rest_of_line ();
12956 return;
12957 }
12958
12959 if (p != NULL)
12960 {
12961 assert (S_GET_NAME (p));
12962 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12963 as_warn (_(".end symbol does not match .ent symbol."));
12964 }
12965 else
12966 as_warn (_(".end directive missing or unknown symbol"));
12967
12968 #ifdef MIPS_STABS_ELF
12969 {
12970 segT saved_seg = now_seg;
12971 subsegT saved_subseg = now_subseg;
12972 valueT dot;
12973 expressionS exp;
12974 char *fragp;
12975
12976 dot = frag_now_fix ();
12977
12978 #ifdef md_flush_pending_output
12979 md_flush_pending_output ();
12980 #endif
12981
12982 assert (pdr_seg);
12983 subseg_set (pdr_seg, 0);
12984
12985 /* Write the symbol. */
12986 exp.X_op = O_symbol;
12987 exp.X_add_symbol = p;
12988 exp.X_add_number = 0;
12989 emit_expr (&exp, 4);
12990
12991 fragp = frag_more (7 * 4);
12992
12993 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
12994 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
12995 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12996 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12997 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12998 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12999 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
13000
13001 subseg_set (saved_seg, saved_subseg);
13002 }
13003 #endif
13004
13005 cur_proc_ptr = NULL;
13006 }
13007
13008 /* The .aent and .ent directives. */
13009
13010 static void
13011 s_mips_ent (aent)
13012 int aent;
13013 {
13014 int number = 0;
13015 symbolS *symbolP;
13016 int maybe_text;
13017
13018 symbolP = get_symbol ();
13019 if (*input_line_pointer == ',')
13020 input_line_pointer++;
13021 SKIP_WHITESPACE ();
13022 if (ISDIGIT (*input_line_pointer)
13023 || *input_line_pointer == '-')
13024 number = get_number ();
13025
13026 #ifdef BFD_ASSEMBLER
13027 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13028 maybe_text = 1;
13029 else
13030 maybe_text = 0;
13031 #else
13032 if (now_seg != data_section && now_seg != bss_section)
13033 maybe_text = 1;
13034 else
13035 maybe_text = 0;
13036 #endif
13037
13038 if (!maybe_text)
13039 as_warn (_(".ent or .aent not in text section."));
13040
13041 if (!aent && cur_proc_ptr)
13042 as_warn (_("missing .end"));
13043
13044 if (!aent)
13045 {
13046 /* This function needs its own .frame and .cprestore directives. */
13047 mips_frame_reg_valid = 0;
13048 mips_cprestore_valid = 0;
13049
13050 cur_proc_ptr = &cur_proc;
13051 memset (cur_proc_ptr, '\0', sizeof (procS));
13052
13053 cur_proc_ptr->isym = symbolP;
13054
13055 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
13056
13057 numprocs++;
13058 }
13059
13060 demand_empty_rest_of_line ();
13061 }
13062
13063 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
13064 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
13065 s_mips_frame is used so that we can set the PDR information correctly.
13066 We can't use the ecoff routines because they make reference to the ecoff
13067 symbol table (in the mdebug section). */
13068
13069 static void
13070 s_mips_frame (ignore)
13071 int ignore ATTRIBUTE_UNUSED;
13072 {
13073 #ifdef MIPS_STABS_ELF
13074
13075 long val;
13076
13077 if (cur_proc_ptr == (procS *) NULL)
13078 {
13079 as_warn (_(".frame outside of .ent"));
13080 demand_empty_rest_of_line ();
13081 return;
13082 }
13083
13084 cur_proc_ptr->frame_reg = tc_get_register (1);
13085
13086 SKIP_WHITESPACE ();
13087 if (*input_line_pointer++ != ','
13088 || get_absolute_expression_and_terminator (&val) != ',')
13089 {
13090 as_warn (_("Bad .frame directive"));
13091 --input_line_pointer;
13092 demand_empty_rest_of_line ();
13093 return;
13094 }
13095
13096 cur_proc_ptr->frame_offset = val;
13097 cur_proc_ptr->pc_reg = tc_get_register (0);
13098
13099 demand_empty_rest_of_line ();
13100 #else
13101 s_ignore (ignore);
13102 #endif /* MIPS_STABS_ELF */
13103 }
13104
13105 /* The .fmask and .mask directives. If the mdebug section is present
13106 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
13107 embedded targets, s_mips_mask is used so that we can set the PDR
13108 information correctly. We can't use the ecoff routines because they
13109 make reference to the ecoff symbol table (in the mdebug section). */
13110
13111 static void
13112 s_mips_mask (reg_type)
13113 char reg_type;
13114 {
13115 #ifdef MIPS_STABS_ELF
13116 long mask, off;
13117
13118 if (cur_proc_ptr == (procS *) NULL)
13119 {
13120 as_warn (_(".mask/.fmask outside of .ent"));
13121 demand_empty_rest_of_line ();
13122 return;
13123 }
13124
13125 if (get_absolute_expression_and_terminator (&mask) != ',')
13126 {
13127 as_warn (_("Bad .mask/.fmask directive"));
13128 --input_line_pointer;
13129 demand_empty_rest_of_line ();
13130 return;
13131 }
13132
13133 off = get_absolute_expression ();
13134
13135 if (reg_type == 'F')
13136 {
13137 cur_proc_ptr->fpreg_mask = mask;
13138 cur_proc_ptr->fpreg_offset = off;
13139 }
13140 else
13141 {
13142 cur_proc_ptr->reg_mask = mask;
13143 cur_proc_ptr->reg_offset = off;
13144 }
13145
13146 demand_empty_rest_of_line ();
13147 #else
13148 s_ignore (reg_type);
13149 #endif /* MIPS_STABS_ELF */
13150 }
13151
13152 /* The .loc directive. */
13153
13154 #if 0
13155 static void
13156 s_loc (x)
13157 int x;
13158 {
13159 symbolS *symbolP;
13160 int lineno;
13161 int addroff;
13162
13163 assert (now_seg == text_section);
13164
13165 lineno = get_number ();
13166 addroff = frag_now_fix ();
13167
13168 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13169 S_SET_TYPE (symbolP, N_SLINE);
13170 S_SET_OTHER (symbolP, 0);
13171 S_SET_DESC (symbolP, lineno);
13172 symbolP->sy_segment = now_seg;
13173 }
13174 #endif
13175
13176 /* CPU name/ISA/number mapping table.
13177
13178 Entries are grouped by type. The first matching CPU or ISA entry
13179 gets chosen by CPU or ISA, so it should be the 'canonical' name
13180 for that type. Entries after that within the type are sorted
13181 alphabetically.
13182
13183 Case is ignored in comparison, so put the canonical entry in the
13184 appropriate case but everything else in lower case to ease eye pain. */
13185 static const struct mips_cpu_info mips_cpu_info_table[] =
13186 {
13187 /* MIPS1 ISA */
13188 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13189 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13190
13191 /* MIPS2 ISA */
13192 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13193
13194 /* MIPS3 ISA */
13195 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13196
13197 /* MIPS4 ISA */
13198 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13199
13200 /* MIPS5 ISA */
13201 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13202 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13203
13204 /* MIPS32 ISA */
13205 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
13206 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
13207 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
13208 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13209 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13210 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13211
13212 /* For historical reasons. */
13213 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
13214
13215 /* MIPS64 ISA */
13216 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
13217 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
13218 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13219 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
13220
13221 /* R2000 CPU */
13222 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13223 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13224 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13225 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13226
13227 /* R3000 CPU */
13228 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13229 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13230 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13231 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13232
13233 /* TX3900 CPU */
13234 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13235 { "3900", 0, ISA_MIPS1, CPU_R3900, },
13236 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
13237
13238 /* R4000 CPU */
13239 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13240 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13241 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13242 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13243
13244 /* R4010 CPU */
13245 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13246 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13247
13248 /* R4400 CPU */
13249 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13250 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13251
13252 /* R4600 CPU */
13253 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13254 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13255 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13256 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13257
13258 /* R4650 CPU */
13259 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13260 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13261
13262 /* R6000 CPU */
13263 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13264 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13265 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13266 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13267
13268 /* R8000 CPU */
13269 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13270 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13271 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13272 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13273
13274 /* R10000 CPU */
13275 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13276 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13277 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13278 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13279
13280 /* R12000 CPU */
13281 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13282 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13283 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13284 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13285
13286 /* VR4100 CPU */
13287 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13288 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13289 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13290 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13291
13292 /* VR4111 CPU */
13293 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13294 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13295 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13296 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13297
13298 /* VR4300 CPU */
13299 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13300 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13301 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13302 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13303
13304 /* VR5000 CPU */
13305 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13306 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13307 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13308 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13309 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13310 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
13311 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
13312 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
13313 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
13314 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
13315 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
13316 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
13317 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
13318 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
13319 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
13320 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13321 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13322
13323 /* Broadcom SB-1 CPU */
13324 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
13325 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
13326 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13327 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13328
13329 /* End marker. */
13330 { NULL, 0, 0, 0, },
13331 };
13332
13333 static const struct mips_cpu_info *
13334 mips_cpu_info_from_name (name)
13335 const char *name;
13336 {
13337 int i;
13338
13339 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13340 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13341 return (&mips_cpu_info_table[i]);
13342
13343 return NULL;
13344 }
13345
13346 static const struct mips_cpu_info *
13347 mips_cpu_info_from_isa (isa)
13348 int isa;
13349 {
13350 int i;
13351
13352 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13353 if (mips_cpu_info_table[i].is_isa
13354 && isa == mips_cpu_info_table[i].isa)
13355 return (&mips_cpu_info_table[i]);
13356
13357 return NULL;
13358 }
13359
13360 static const struct mips_cpu_info *
13361 mips_cpu_info_from_cpu (cpu)
13362 int cpu;
13363 {
13364 int i;
13365
13366 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13367 if (!mips_cpu_info_table[i].is_isa
13368 && cpu == mips_cpu_info_table[i].cpu)
13369 return (&mips_cpu_info_table[i]);
13370
13371 return NULL;
13372 }
This page took 0.31849 seconds and 4 git commands to generate.