f9b58117444b42d1cf62b480cb640a17c6c10daf
[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
30 #include <ctype.h>
31
32 #ifdef USE_STDARG
33 #include <stdarg.h>
34 #endif
35 #ifdef USE_VARARGS
36 #include <varargs.h>
37 #endif
38
39 #include "opcode/mips.h"
40 #include "itbl-ops.h"
41
42 #ifdef DEBUG
43 #define DBG(x) printf x
44 #else
45 #define DBG(x)
46 #endif
47
48 #ifdef OBJ_MAYBE_ELF
49 /* Clean up namespace so we can include obj-elf.h too. */
50 static int mips_output_flavor PARAMS ((void));
51 static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52 #undef OBJ_PROCESS_STAB
53 #undef OUTPUT_FLAVOR
54 #undef S_GET_ALIGN
55 #undef S_GET_SIZE
56 #undef S_SET_ALIGN
57 #undef S_SET_SIZE
58 #undef obj_frob_file
59 #undef obj_frob_file_after_relocs
60 #undef obj_frob_symbol
61 #undef obj_pop_insert
62 #undef obj_sec_sym_ok_for_reloc
63 #undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65 #include "obj-elf.h"
66 /* Fix any of them that we actually care about. */
67 #undef OUTPUT_FLAVOR
68 #define OUTPUT_FLAVOR mips_output_flavor()
69 #endif
70
71 #if defined (OBJ_ELF)
72 #include "elf/mips.h"
73 #endif
74
75 #ifndef ECOFF_DEBUGGING
76 #define NO_ECOFF_DEBUGGING
77 #define ECOFF_DEBUGGING 0
78 #endif
79
80 #include "ecoff.h"
81
82 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
83 static char *mips_regmask_frag;
84 #endif
85
86 #define AT 1
87 #define TREG 24
88 #define PIC_CALL_REG 25
89 #define KT0 26
90 #define KT1 27
91 #define GP 28
92 #define SP 29
93 #define FP 30
94 #define RA 31
95
96 #define ILLEGAL_REG (32)
97
98 /* Allow override of standard little-endian ECOFF format. */
99
100 #ifndef ECOFF_LITTLE_FORMAT
101 #define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
102 #endif
103
104 extern int target_big_endian;
105
106 /* 1 is we should use the 64 bit MIPS ELF ABI, 0 if we should use the
107 32 bit ABI. This has no meaning for ECOFF.
108 Note that the default is always 32 bit, even if "configured" for
109 64 bit [e.g. --target=mips64-elf]. */
110 static int mips_64;
111
112 /* The default target format to use. */
113
114 const char *
115 mips_target_format ()
116 {
117 switch (OUTPUT_FLAVOR)
118 {
119 case bfd_target_aout_flavour:
120 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
121 case bfd_target_ecoff_flavour:
122 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
123 case bfd_target_coff_flavour:
124 return "pe-mips";
125 case bfd_target_elf_flavour:
126 #ifdef TE_TMIPS
127 /* This is traditional mips */
128 return (target_big_endian
129 ? (mips_64 ? "elf64-tradbigmips" : "elf32-tradbigmips")
130 : (mips_64 ? "elf64-tradlittlemips" : "elf32-tradlittlemips"));
131 #else
132 return (target_big_endian
133 ? (mips_64 ? "elf64-bigmips" : "elf32-bigmips")
134 : (mips_64 ? "elf64-littlemips" : "elf32-littlemips"));
135 #endif
136 default:
137 abort ();
138 return NULL;
139 }
140 }
141
142 /* The name of the readonly data section. */
143 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
144 ? ".data" \
145 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
146 ? ".rdata" \
147 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
148 ? ".rdata" \
149 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
150 ? ".rodata" \
151 : (abort (), ""))
152
153 /* This is the set of options which may be modified by the .set
154 pseudo-op. We use a struct so that .set push and .set pop are more
155 reliable. */
156
157 struct mips_set_options
158 {
159 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
160 if it has not been initialized. Changed by `.set mipsN', and the
161 -mipsN command line option, and the default CPU. */
162 int isa;
163 /* Whether we are assembling for the mips16 processor. 0 if we are
164 not, 1 if we are, and -1 if the value has not been initialized.
165 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
166 -nomips16 command line options, and the default CPU. */
167 int mips16;
168 /* Non-zero if we should not reorder instructions. Changed by `.set
169 reorder' and `.set noreorder'. */
170 int noreorder;
171 /* Non-zero if we should not permit the $at ($1) register to be used
172 in instructions. Changed by `.set at' and `.set noat'. */
173 int noat;
174 /* Non-zero if we should warn when a macro instruction expands into
175 more than one machine instruction. Changed by `.set nomacro' and
176 `.set macro'. */
177 int warn_about_macros;
178 /* Non-zero if we should not move instructions. Changed by `.set
179 move', `.set volatile', `.set nomove', and `.set novolatile'. */
180 int nomove;
181 /* Non-zero if we should not optimize branches by moving the target
182 of the branch into the delay slot. Actually, we don't perform
183 this optimization anyhow. Changed by `.set bopt' and `.set
184 nobopt'. */
185 int nobopt;
186 /* Non-zero if we should not autoextend mips16 instructions.
187 Changed by `.set autoextend' and `.set noautoextend'. */
188 int noautoextend;
189 };
190
191 /* This is the struct we use to hold the current set of options. Note
192 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
193 -1 to indicate that they have not been initialized. */
194
195 static struct mips_set_options mips_opts =
196 {
197 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0
198 };
199
200 /* These variables are filled in with the masks of registers used.
201 The object format code reads them and puts them in the appropriate
202 place. */
203 unsigned long mips_gprmask;
204 unsigned long mips_cprmask[4];
205
206 /* MIPS ISA we are using for this output file. */
207 static int file_mips_isa = ISA_UNKNOWN;
208
209 /* The argument of the -mcpu= flag. Historical for code generation. */
210 static int mips_cpu = CPU_UNKNOWN;
211
212 /* The argument of the -march= flag. The architecture we are assembling. */
213 static int mips_arch = CPU_UNKNOWN;
214
215 /* The argument of the -mtune= flag. The architecture for which we
216 are optimizing. */
217 static int mips_tune = CPU_UNKNOWN;
218
219 /* The argument of the -mabi= flag. */
220 static char * mips_abi_string = NULL;
221
222 /* Whether we should mark the file EABI64 or EABI32. */
223 static int mips_eabi64 = 0;
224
225 /* If they asked for mips1 or mips2 and a cpu that is
226 mips3 or greater, then mark the object file 32BITMODE. */
227 static int mips_32bitmode = 0;
228
229 /* True if -mgp32 was passed. */
230 static int mips_gp32 = 0;
231
232 /* True if -mfp32 was passed. */
233 static int mips_fp32 = 0;
234
235 /* True if the selected ABI is defined for 32-bit registers only. */
236 static int mips_32bit_abi = 0;
237
238 /* Some ISA's have delay slots for instructions which read or write
239 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
240 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
241 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
242 delay slot in this ISA. The uses of this macro assume that any
243 ISA that has delay slots for one of these, has them for all. They
244 also assume that ISAs which don't have delays for these insns, don't
245 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
246 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
247 (ISA) == ISA_MIPS1 \
248 || (ISA) == ISA_MIPS2 \
249 || (ISA) == ISA_MIPS3 \
250 )
251
252 /* Return true if ISA supports 64 bit gp register instructions. */
253 #define ISA_HAS_64BIT_REGS(ISA) ( \
254 (ISA) == ISA_MIPS3 \
255 || (ISA) == ISA_MIPS4 \
256 || (ISA) == ISA_MIPS5 \
257 || (ISA) == ISA_MIPS64 \
258 )
259
260 #define HAVE_32BIT_GPRS \
261 (mips_gp32 \
262 || mips_32bit_abi \
263 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
264
265 #define HAVE_32BIT_FPRS \
266 (mips_fp32 \
267 || mips_32bit_abi \
268 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
269
270 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
271 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
272
273 #define HAVE_32BIT_ADDRESSES \
274 (HAVE_32BIT_GPRS \
275 || bfd_arch_bits_per_address (stdoutput) == 32)
276
277 /* Whether the processor uses hardware interlocks to protect
278 reads from the HI and LO registers, and thus does not
279 require nops to be inserted. */
280
281 #define hilo_interlocks (mips_arch == CPU_R4010 \
282 )
283
284 /* Whether the processor uses hardware interlocks to protect reads
285 from the GPRs, and thus does not require nops to be inserted. */
286 #define gpr_interlocks \
287 (mips_opts.isa != ISA_MIPS1 \
288 || mips_arch == CPU_R3900)
289
290 /* As with other "interlocks" this is used by hardware that has FP
291 (co-processor) interlocks. */
292 /* Itbl support may require additional care here. */
293 #define cop_interlocks (mips_arch == CPU_R4300 \
294 )
295
296 /* Is this a mfhi or mflo instruction? */
297 #define MF_HILO_INSN(PINFO) \
298 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
299
300 /* MIPS PIC level. */
301
302 enum mips_pic_level
303 {
304 /* Do not generate PIC code. */
305 NO_PIC,
306
307 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
308 not sure what it is supposed to do. */
309 IRIX4_PIC,
310
311 /* Generate PIC code as in the SVR4 MIPS ABI. */
312 SVR4_PIC,
313
314 /* Generate PIC code without using a global offset table: the data
315 segment has a maximum size of 64K, all data references are off
316 the $gp register, and all text references are PC relative. This
317 is used on some embedded systems. */
318 EMBEDDED_PIC
319 };
320
321 static enum mips_pic_level mips_pic;
322
323 /* Warn about all NOPS that the assembler generates. */
324 static int warn_nops = 0;
325
326 /* 1 if we should generate 32 bit offsets from the GP register in
327 SVR4_PIC mode. Currently has no meaning in other modes. */
328 static int mips_big_got;
329
330 /* 1 if trap instructions should used for overflow rather than break
331 instructions. */
332 static int mips_trap;
333
334 /* 1 if double width floating point constants should not be constructed
335 by a assembling two single width halves into two single width floating
336 point registers which just happen to alias the double width destination
337 register. On some architectures this aliasing can be disabled by a bit
338 in the status register, and the setting of this bit cannot be determined
339 automatically at assemble time. */
340 static int mips_disable_float_construction;
341
342 /* Non-zero if any .set noreorder directives were used. */
343
344 static int mips_any_noreorder;
345
346 /* Non-zero if nops should be inserted when the register referenced in
347 an mfhi/mflo instruction is read in the next two instructions. */
348 static int mips_7000_hilo_fix;
349
350 /* The size of the small data section. */
351 static unsigned int g_switch_value = 8;
352 /* Whether the -G option was used. */
353 static int g_switch_seen = 0;
354
355 #define N_RMASK 0xc4
356 #define N_VFP 0xd4
357
358 /* If we can determine in advance that GP optimization won't be
359 possible, we can skip the relaxation stuff that tries to produce
360 GP-relative references. This makes delay slot optimization work
361 better.
362
363 This function can only provide a guess, but it seems to work for
364 gcc output. It needs to guess right for gcc, otherwise gcc
365 will put what it thinks is a GP-relative instruction in a branch
366 delay slot.
367
368 I don't know if a fix is needed for the SVR4_PIC mode. I've only
369 fixed it for the non-PIC mode. KR 95/04/07 */
370 static int nopic_need_relax PARAMS ((symbolS *, int));
371
372 /* handle of the OPCODE hash table */
373 static struct hash_control *op_hash = NULL;
374
375 /* The opcode hash table we use for the mips16. */
376 static struct hash_control *mips16_op_hash = NULL;
377
378 /* This array holds the chars that always start a comment. If the
379 pre-processor is disabled, these aren't very useful */
380 const char comment_chars[] = "#";
381
382 /* This array holds the chars that only start a comment at the beginning of
383 a line. If the line seems to have the form '# 123 filename'
384 .line and .file directives will appear in the pre-processed output */
385 /* Note that input_file.c hand checks for '#' at the beginning of the
386 first line of the input file. This is because the compiler outputs
387 #NO_APP at the beginning of its output. */
388 /* Also note that C style comments are always supported. */
389 const char line_comment_chars[] = "#";
390
391 /* This array holds machine specific line separator characters. */
392 const char line_separator_chars[] = ";";
393
394 /* Chars that can be used to separate mant from exp in floating point nums */
395 const char EXP_CHARS[] = "eE";
396
397 /* Chars that mean this number is a floating point constant */
398 /* As in 0f12.456 */
399 /* or 0d1.2345e12 */
400 const char FLT_CHARS[] = "rRsSfFdDxXpP";
401
402 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
403 changed in read.c . Ideally it shouldn't have to know about it at all,
404 but nothing is ideal around here.
405 */
406
407 static char *insn_error;
408
409 static int auto_align = 1;
410
411 /* When outputting SVR4 PIC code, the assembler needs to know the
412 offset in the stack frame from which to restore the $gp register.
413 This is set by the .cprestore pseudo-op, and saved in this
414 variable. */
415 static offsetT mips_cprestore_offset = -1;
416
417 /* This is the register which holds the stack frame, as set by the
418 .frame pseudo-op. This is needed to implement .cprestore. */
419 static int mips_frame_reg = SP;
420
421 /* To output NOP instructions correctly, we need to keep information
422 about the previous two instructions. */
423
424 /* Whether we are optimizing. The default value of 2 means to remove
425 unneeded NOPs and swap branch instructions when possible. A value
426 of 1 means to not swap branches. A value of 0 means to always
427 insert NOPs. */
428 static int mips_optimize = 2;
429
430 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
431 equivalent to seeing no -g option at all. */
432 static int mips_debug = 0;
433
434 /* The previous instruction. */
435 static struct mips_cl_insn prev_insn;
436
437 /* The instruction before prev_insn. */
438 static struct mips_cl_insn prev_prev_insn;
439
440 /* If we don't want information for prev_insn or prev_prev_insn, we
441 point the insn_mo field at this dummy integer. */
442 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
443
444 /* Non-zero if prev_insn is valid. */
445 static int prev_insn_valid;
446
447 /* The frag for the previous instruction. */
448 static struct frag *prev_insn_frag;
449
450 /* The offset into prev_insn_frag for the previous instruction. */
451 static long prev_insn_where;
452
453 /* The reloc type for the previous instruction, if any. */
454 static bfd_reloc_code_real_type prev_insn_reloc_type;
455
456 /* The reloc for the previous instruction, if any. */
457 static fixS *prev_insn_fixp;
458
459 /* Non-zero if the previous instruction was in a delay slot. */
460 static int prev_insn_is_delay_slot;
461
462 /* Non-zero if the previous instruction was in a .set noreorder. */
463 static int prev_insn_unreordered;
464
465 /* Non-zero if the previous instruction uses an extend opcode (if
466 mips16). */
467 static int prev_insn_extended;
468
469 /* Non-zero if the previous previous instruction was in a .set
470 noreorder. */
471 static int prev_prev_insn_unreordered;
472
473 /* If this is set, it points to a frag holding nop instructions which
474 were inserted before the start of a noreorder section. If those
475 nops turn out to be unnecessary, the size of the frag can be
476 decreased. */
477 static fragS *prev_nop_frag;
478
479 /* The number of nop instructions we created in prev_nop_frag. */
480 static int prev_nop_frag_holds;
481
482 /* The number of nop instructions that we know we need in
483 prev_nop_frag. */
484 static int prev_nop_frag_required;
485
486 /* The number of instructions we've seen since prev_nop_frag. */
487 static int prev_nop_frag_since;
488
489 /* For ECOFF and ELF, relocations against symbols are done in two
490 parts, with a HI relocation and a LO relocation. Each relocation
491 has only 16 bits of space to store an addend. This means that in
492 order for the linker to handle carries correctly, it must be able
493 to locate both the HI and the LO relocation. This means that the
494 relocations must appear in order in the relocation table.
495
496 In order to implement this, we keep track of each unmatched HI
497 relocation. We then sort them so that they immediately precede the
498 corresponding LO relocation. */
499
500 struct mips_hi_fixup
501 {
502 /* Next HI fixup. */
503 struct mips_hi_fixup *next;
504 /* This fixup. */
505 fixS *fixp;
506 /* The section this fixup is in. */
507 segT seg;
508 };
509
510 /* The list of unmatched HI relocs. */
511
512 static struct mips_hi_fixup *mips_hi_fixup_list;
513
514 /* Map normal MIPS register numbers to mips16 register numbers. */
515
516 #define X ILLEGAL_REG
517 static const int mips32_to_16_reg_map[] =
518 {
519 X, X, 2, 3, 4, 5, 6, 7,
520 X, X, X, X, X, X, X, X,
521 0, 1, X, X, X, X, X, X,
522 X, X, X, X, X, X, X, X
523 };
524 #undef X
525
526 /* Map mips16 register numbers to normal MIPS register numbers. */
527
528 static const unsigned int mips16_to_32_reg_map[] =
529 {
530 16, 17, 2, 3, 4, 5, 6, 7
531 };
532 \f
533 /* Since the MIPS does not have multiple forms of PC relative
534 instructions, we do not have to do relaxing as is done on other
535 platforms. However, we do have to handle GP relative addressing
536 correctly, which turns out to be a similar problem.
537
538 Every macro that refers to a symbol can occur in (at least) two
539 forms, one with GP relative addressing and one without. For
540 example, loading a global variable into a register generally uses
541 a macro instruction like this:
542 lw $4,i
543 If i can be addressed off the GP register (this is true if it is in
544 the .sbss or .sdata section, or if it is known to be smaller than
545 the -G argument) this will generate the following instruction:
546 lw $4,i($gp)
547 This instruction will use a GPREL reloc. If i can not be addressed
548 off the GP register, the following instruction sequence will be used:
549 lui $at,i
550 lw $4,i($at)
551 In this case the first instruction will have a HI16 reloc, and the
552 second reloc will have a LO16 reloc. Both relocs will be against
553 the symbol i.
554
555 The issue here is that we may not know whether i is GP addressable
556 until after we see the instruction that uses it. Therefore, we
557 want to be able to choose the final instruction sequence only at
558 the end of the assembly. This is similar to the way other
559 platforms choose the size of a PC relative instruction only at the
560 end of assembly.
561
562 When generating position independent code we do not use GP
563 addressing in quite the same way, but the issue still arises as
564 external symbols and local symbols must be handled differently.
565
566 We handle these issues by actually generating both possible
567 instruction sequences. The longer one is put in a frag_var with
568 type rs_machine_dependent. We encode what to do with the frag in
569 the subtype field. We encode (1) the number of existing bytes to
570 replace, (2) the number of new bytes to use, (3) the offset from
571 the start of the existing bytes to the first reloc we must generate
572 (that is, the offset is applied from the start of the existing
573 bytes after they are replaced by the new bytes, if any), (4) the
574 offset from the start of the existing bytes to the second reloc,
575 (5) whether a third reloc is needed (the third reloc is always four
576 bytes after the second reloc), and (6) whether to warn if this
577 variant is used (this is sometimes needed if .set nomacro or .set
578 noat is in effect). All these numbers are reasonably small.
579
580 Generating two instruction sequences must be handled carefully to
581 ensure that delay slots are handled correctly. Fortunately, there
582 are a limited number of cases. When the second instruction
583 sequence is generated, append_insn is directed to maintain the
584 existing delay slot information, so it continues to apply to any
585 code after the second instruction sequence. This means that the
586 second instruction sequence must not impose any requirements not
587 required by the first instruction sequence.
588
589 These variant frags are then handled in functions called by the
590 machine independent code. md_estimate_size_before_relax returns
591 the final size of the frag. md_convert_frag sets up the final form
592 of the frag. tc_gen_reloc adjust the first reloc and adds a second
593 one if needed. */
594 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
595 ((relax_substateT) \
596 (((old) << 23) \
597 | ((new) << 16) \
598 | (((reloc1) + 64) << 9) \
599 | (((reloc2) + 64) << 2) \
600 | ((reloc3) ? (1 << 1) : 0) \
601 | ((warn) ? 1 : 0)))
602 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
603 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
604 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
605 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
606 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
607 #define RELAX_WARN(i) ((i) & 1)
608
609 /* For mips16 code, we use an entirely different form of relaxation.
610 mips16 supports two versions of most instructions which take
611 immediate values: a small one which takes some small value, and a
612 larger one which takes a 16 bit value. Since branches also follow
613 this pattern, relaxing these values is required.
614
615 We can assemble both mips16 and normal MIPS code in a single
616 object. Therefore, we need to support this type of relaxation at
617 the same time that we support the relaxation described above. We
618 use the high bit of the subtype field to distinguish these cases.
619
620 The information we store for this type of relaxation is the
621 argument code found in the opcode file for this relocation, whether
622 the user explicitly requested a small or extended form, and whether
623 the relocation is in a jump or jal delay slot. That tells us the
624 size of the value, and how it should be stored. We also store
625 whether the fragment is considered to be extended or not. We also
626 store whether this is known to be a branch to a different section,
627 whether we have tried to relax this frag yet, and whether we have
628 ever extended a PC relative fragment because of a shift count. */
629 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
630 (0x80000000 \
631 | ((type) & 0xff) \
632 | ((small) ? 0x100 : 0) \
633 | ((ext) ? 0x200 : 0) \
634 | ((dslot) ? 0x400 : 0) \
635 | ((jal_dslot) ? 0x800 : 0))
636 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
637 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
638 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
639 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
640 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
641 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
642 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
643 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
644 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
645 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
646 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
647 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
648 \f
649 /* Prototypes for static functions. */
650
651 #ifdef __STDC__
652 #define internalError() \
653 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
654 #else
655 #define internalError() as_fatal (_("MIPS internal Error"));
656 #endif
657
658 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
659
660 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
661 unsigned int reg, enum mips_regclass class));
662 static int reg_needs_delay PARAMS ((unsigned int));
663 static void mips16_mark_labels PARAMS ((void));
664 static void append_insn PARAMS ((char *place,
665 struct mips_cl_insn * ip,
666 expressionS * p,
667 bfd_reloc_code_real_type r,
668 boolean));
669 static void mips_no_prev_insn PARAMS ((int));
670 static void mips_emit_delays PARAMS ((boolean));
671 #ifdef USE_STDARG
672 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
673 const char *name, const char *fmt,
674 ...));
675 #else
676 static void macro_build ();
677 #endif
678 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
679 const char *, const char *,
680 va_list));
681 static void macro_build_lui PARAMS ((char *place, int *counter,
682 expressionS * ep, int regnum));
683 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
684 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
685 expressionS *));
686 static void load_register PARAMS ((int *, int, expressionS *, int));
687 static void load_address PARAMS ((int *counter, int reg, expressionS *ep));
688 static void move_register PARAMS ((int *, int, int));
689 static void macro PARAMS ((struct mips_cl_insn * ip));
690 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
691 #ifdef LOSING_COMPILER
692 static void macro2 PARAMS ((struct mips_cl_insn * ip));
693 #endif
694 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
695 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
696 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
697 boolean, boolean, unsigned long *,
698 boolean *, unsigned short *));
699 static int my_getSmallExpression PARAMS ((expressionS * ep, char *str));
700 static void my_getExpression PARAMS ((expressionS * ep, char *str));
701 static symbolS *get_symbol PARAMS ((void));
702 static void mips_align PARAMS ((int to, int fill, symbolS *label));
703 static void s_align PARAMS ((int));
704 static void s_change_sec PARAMS ((int));
705 static void s_cons PARAMS ((int));
706 static void s_float_cons PARAMS ((int));
707 static void s_mips_globl PARAMS ((int));
708 static void s_option PARAMS ((int));
709 static void s_mipsset PARAMS ((int));
710 static void s_abicalls PARAMS ((int));
711 static void s_cpload PARAMS ((int));
712 static void s_cprestore PARAMS ((int));
713 static void s_gpword PARAMS ((int));
714 static void s_cpadd PARAMS ((int));
715 static void s_insn PARAMS ((int));
716 static void md_obj_begin PARAMS ((void));
717 static void md_obj_end PARAMS ((void));
718 static long get_number PARAMS ((void));
719 static void s_mips_ent PARAMS ((int));
720 static void s_mips_end PARAMS ((int));
721 static void s_mips_frame PARAMS ((int));
722 static void s_mips_mask PARAMS ((int));
723 static void s_mips_stab PARAMS ((int));
724 static void s_mips_weakext PARAMS ((int));
725 static void s_file PARAMS ((int));
726 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
727 static const char *mips_isa_to_str PARAMS ((int));
728 static const char *mips_cpu_to_str PARAMS ((int));
729 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
730 static void show PARAMS ((FILE *, char *, int *, int *));
731
732 /* Return values of my_getSmallExpression() */
733
734 enum
735 {
736 S_EX_NONE = 0,
737 S_EX_LO,
738 S_EX_HI,
739 S_EX_HIGHER,
740 S_EX_HIGHEST,
741 S_EX_GPREL,
742 S_EX_NEG
743 };
744
745 /* Table and functions used to map between CPU/ISA names, and
746 ISA levels, and CPU numbers. */
747
748 struct mips_cpu_info
749 {
750 const char *name; /* CPU or ISA name. */
751 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
752 int isa; /* ISA level. */
753 int cpu; /* CPU number (default CPU if ISA). */
754 };
755
756 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
757 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
758 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
759 \f
760 /* Pseudo-op table.
761
762 The following pseudo-ops from the Kane and Heinrich MIPS book
763 should be defined here, but are currently unsupported: .alias,
764 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
765
766 The following pseudo-ops from the Kane and Heinrich MIPS book are
767 specific to the type of debugging information being generated, and
768 should be defined by the object format: .aent, .begin, .bend,
769 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
770 .vreg.
771
772 The following pseudo-ops from the Kane and Heinrich MIPS book are
773 not MIPS CPU specific, but are also not specific to the object file
774 format. This file is probably the best place to define them, but
775 they are not currently supported: .asm0, .endr, .lab, .repeat,
776 .struct. */
777
778 static const pseudo_typeS mips_pseudo_table[] =
779 {
780 /* MIPS specific pseudo-ops. */
781 {"option", s_option, 0},
782 {"set", s_mipsset, 0},
783 {"rdata", s_change_sec, 'r'},
784 {"sdata", s_change_sec, 's'},
785 {"livereg", s_ignore, 0},
786 {"abicalls", s_abicalls, 0},
787 {"cpload", s_cpload, 0},
788 {"cprestore", s_cprestore, 0},
789 {"gpword", s_gpword, 0},
790 {"cpadd", s_cpadd, 0},
791 {"insn", s_insn, 0},
792
793 /* Relatively generic pseudo-ops that happen to be used on MIPS
794 chips. */
795 {"asciiz", stringer, 1},
796 {"bss", s_change_sec, 'b'},
797 {"err", s_err, 0},
798 {"half", s_cons, 1},
799 {"dword", s_cons, 3},
800 {"weakext", s_mips_weakext, 0},
801
802 /* These pseudo-ops are defined in read.c, but must be overridden
803 here for one reason or another. */
804 {"align", s_align, 0},
805 {"byte", s_cons, 0},
806 {"data", s_change_sec, 'd'},
807 {"double", s_float_cons, 'd'},
808 {"float", s_float_cons, 'f'},
809 {"globl", s_mips_globl, 0},
810 {"global", s_mips_globl, 0},
811 {"hword", s_cons, 1},
812 {"int", s_cons, 2},
813 {"long", s_cons, 2},
814 {"octa", s_cons, 4},
815 {"quad", s_cons, 3},
816 {"short", s_cons, 1},
817 {"single", s_float_cons, 'f'},
818 {"stabn", s_mips_stab, 'n'},
819 {"text", s_change_sec, 't'},
820 {"word", s_cons, 2},
821
822 #ifdef MIPS_STABS_ELF
823 { "extern", ecoff_directive_extern, 0},
824 #endif
825
826 { NULL, NULL, 0 },
827 };
828
829 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
830 {
831 /* These pseudo-ops should be defined by the object file format.
832 However, a.out doesn't support them, so we have versions here. */
833 {"aent", s_mips_ent, 1},
834 {"bgnb", s_ignore, 0},
835 {"end", s_mips_end, 0},
836 {"endb", s_ignore, 0},
837 {"ent", s_mips_ent, 0},
838 {"file", s_file, 0},
839 {"fmask", s_mips_mask, 'F'},
840 {"frame", s_mips_frame, 0},
841 {"loc", s_ignore, 0},
842 {"mask", s_mips_mask, 'R'},
843 {"verstamp", s_ignore, 0},
844 { NULL, NULL, 0 },
845 };
846
847 extern void pop_insert PARAMS ((const pseudo_typeS *));
848
849 void
850 mips_pop_insert ()
851 {
852 pop_insert (mips_pseudo_table);
853 if (! ECOFF_DEBUGGING)
854 pop_insert (mips_nonecoff_pseudo_table);
855 }
856 \f
857 /* Symbols labelling the current insn. */
858
859 struct insn_label_list
860 {
861 struct insn_label_list *next;
862 symbolS *label;
863 };
864
865 static struct insn_label_list *insn_labels;
866 static struct insn_label_list *free_insn_labels;
867
868 static void mips_clear_insn_labels PARAMS ((void));
869
870 static inline void
871 mips_clear_insn_labels ()
872 {
873 register struct insn_label_list **pl;
874
875 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
876 ;
877 *pl = insn_labels;
878 insn_labels = NULL;
879 }
880 \f
881 static char *expr_end;
882
883 /* Expressions which appear in instructions. These are set by
884 mips_ip. */
885
886 static expressionS imm_expr;
887 static expressionS offset_expr;
888
889 /* Relocs associated with imm_expr and offset_expr. */
890
891 static bfd_reloc_code_real_type imm_reloc;
892 static bfd_reloc_code_real_type offset_reloc;
893
894 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
895
896 static boolean imm_unmatched_hi;
897
898 /* These are set by mips16_ip if an explicit extension is used. */
899
900 static boolean mips16_small, mips16_ext;
901
902 #ifdef MIPS_STABS_ELF
903 /* The pdr segment for per procedure frame/regmask info */
904
905 static segT pdr_seg;
906 #endif
907
908 static const char *
909 mips_isa_to_str (isa)
910 int isa;
911 {
912 const struct mips_cpu_info *ci;
913 static char s[20];
914
915 ci = mips_cpu_info_from_isa (isa);
916 if (ci != NULL)
917 return (ci->name);
918
919 sprintf (s, "ISA#%d", isa);
920 return s;
921 }
922
923 static const char *
924 mips_cpu_to_str (cpu)
925 int cpu;
926 {
927 const struct mips_cpu_info *ci;
928 static char s[16];
929
930 ci = mips_cpu_info_from_cpu (cpu);
931 if (ci != NULL)
932 return (ci->name);
933
934 sprintf (s, "CPU#%d", cpu);
935 return s;
936 }
937
938 /* This function is called once, at assembler startup time. It should
939 set up all the tables, etc. that the MD part of the assembler will need. */
940
941 void
942 md_begin ()
943 {
944 register const char *retval = NULL;
945 int i = 0;
946 const char *cpu;
947 char *a = NULL;
948 int broken = 0;
949 int mips_isa_from_cpu;
950 int target_cpu_had_mips16 = 0;
951 const struct mips_cpu_info *ci;
952
953 /* GP relative stuff not working for PE */
954 if (strncmp (TARGET_OS, "pe", 2) == 0
955 && g_switch_value != 0)
956 {
957 if (g_switch_seen)
958 as_bad (_("-G not supported in this configuration."));
959 g_switch_value = 0;
960 }
961
962 cpu = TARGET_CPU;
963 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
964 {
965 a = xmalloc (sizeof TARGET_CPU);
966 strcpy (a, TARGET_CPU);
967 a[(sizeof TARGET_CPU) - 3] = '\0';
968 cpu = a;
969 }
970
971 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
972 {
973 target_cpu_had_mips16 = 1;
974 cpu += sizeof "mips16" - 1;
975 }
976
977 if (mips_opts.mips16 < 0)
978 mips_opts.mips16 = target_cpu_had_mips16;
979
980 /* Backward compatibility for historic -mcpu= option. Check for
981 incompatible options, warn if -mcpu is used. */
982 if (mips_cpu != CPU_UNKNOWN
983 && mips_arch != CPU_UNKNOWN
984 && mips_cpu != mips_arch)
985 {
986 as_fatal (_("The -mcpu option can't be used together with -march. "
987 "Use -mtune instead of -mcpu."));
988 }
989
990 if (mips_cpu != CPU_UNKNOWN
991 && mips_tune != CPU_UNKNOWN
992 && mips_cpu != mips_tune)
993 {
994 as_fatal (_("The -mcpu option can't be used together with -mtune. "
995 "Use -march instead of -mcpu."));
996 }
997
998 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
999 {
1000 ci = mips_cpu_info_from_cpu (mips_cpu);
1001 assert (ci != NULL);
1002 mips_arch = ci->cpu;
1003 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1004 "-mtune instead."));
1005 }
1006
1007 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1008 specified on the command line, or some other value if one was.
1009 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1010 the command line, or will be set otherwise if one was. */
1011 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1012 {
1013 /* We have to check if the isa is the default isa of arch. Otherwise
1014 we'll get invalid object file headers. */
1015 ci = mips_cpu_info_from_cpu (mips_arch);
1016 assert (ci != NULL);
1017 if (mips_opts.isa != ci->isa)
1018 {
1019 /* This really should be an error instead of a warning, but old
1020 compilers only have -mcpu which sets both arch and tune. For
1021 now, we discard arch and preserve tune. */
1022 as_warn (_("The -march option is incompatible to -mipsN and "
1023 "therefore ignored."));
1024 if (mips_tune == CPU_UNKNOWN)
1025 mips_tune = mips_arch;
1026 ci = mips_cpu_info_from_isa (mips_opts.isa);
1027 assert (ci != NULL);
1028 mips_arch = ci->cpu;
1029 }
1030 }
1031 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1032 {
1033 /* We have ARCH, we need ISA. */
1034 ci = mips_cpu_info_from_cpu (mips_arch);
1035 assert (ci != NULL);
1036 mips_opts.isa = ci->isa;
1037 }
1038 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1039 {
1040 /* We have ISA, we need default ARCH. */
1041 ci = mips_cpu_info_from_isa (mips_opts.isa);
1042 assert (ci != NULL);
1043 mips_arch = ci->cpu;
1044 }
1045 else
1046 {
1047 /* We need to set both ISA and ARCH from target cpu. */
1048 ci = mips_cpu_info_from_name (cpu);
1049 if (ci == NULL)
1050 ci = mips_cpu_info_from_cpu (CPU_R3000);
1051 assert (ci != NULL);
1052 mips_opts.isa = ci->isa;
1053 mips_arch = ci->cpu;
1054 }
1055
1056 if (mips_tune == CPU_UNKNOWN)
1057 mips_tune = mips_arch;
1058
1059 ci = mips_cpu_info_from_cpu (mips_arch);
1060 assert (ci != NULL);
1061 mips_isa_from_cpu = ci->isa;
1062
1063 /* End of TARGET_CPU processing, get rid of malloced memory
1064 if necessary. */
1065 cpu = NULL;
1066 if (a != NULL)
1067 {
1068 free (a);
1069 a = NULL;
1070 }
1071
1072 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1073 as_bad (_("trap exception not supported at ISA 1"));
1074
1075 /* Set the EABI kind based on the ISA before the user gets
1076 to change the ISA with directives. This isn't really
1077 the best, but then neither is basing the abi on the isa. */
1078 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1079 && mips_abi_string
1080 && 0 == strcmp (mips_abi_string, "eabi"))
1081 mips_eabi64 = 1;
1082
1083 /* If they asked for mips1 or mips2 and a cpu that is
1084 mips3 or greater, then mark the object file 32BITMODE. */
1085 if (mips_isa_from_cpu != ISA_UNKNOWN
1086 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1087 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1088 mips_32bitmode = 1;
1089
1090 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1091 as_warn (_("Could not set architecture and machine"));
1092
1093 file_mips_isa = mips_opts.isa;
1094
1095 op_hash = hash_new ();
1096
1097 for (i = 0; i < NUMOPCODES;)
1098 {
1099 const char *name = mips_opcodes[i].name;
1100
1101 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1102 if (retval != NULL)
1103 {
1104 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1105 mips_opcodes[i].name, retval);
1106 /* Probably a memory allocation problem? Give up now. */
1107 as_fatal (_("Broken assembler. No assembly attempted."));
1108 }
1109 do
1110 {
1111 if (mips_opcodes[i].pinfo != INSN_MACRO)
1112 {
1113 if (!validate_mips_insn (&mips_opcodes[i]))
1114 broken = 1;
1115 }
1116 ++i;
1117 }
1118 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1119 }
1120
1121 mips16_op_hash = hash_new ();
1122
1123 i = 0;
1124 while (i < bfd_mips16_num_opcodes)
1125 {
1126 const char *name = mips16_opcodes[i].name;
1127
1128 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1129 if (retval != NULL)
1130 as_fatal (_("internal: can't hash `%s': %s"),
1131 mips16_opcodes[i].name, retval);
1132 do
1133 {
1134 if (mips16_opcodes[i].pinfo != INSN_MACRO
1135 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1136 != mips16_opcodes[i].match))
1137 {
1138 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1139 mips16_opcodes[i].name, mips16_opcodes[i].args);
1140 broken = 1;
1141 }
1142 ++i;
1143 }
1144 while (i < bfd_mips16_num_opcodes
1145 && strcmp (mips16_opcodes[i].name, name) == 0);
1146 }
1147
1148 if (broken)
1149 as_fatal (_("Broken assembler. No assembly attempted."));
1150
1151 /* We add all the general register names to the symbol table. This
1152 helps us detect invalid uses of them. */
1153 for (i = 0; i < 32; i++)
1154 {
1155 char buf[5];
1156
1157 sprintf (buf, "$%d", i);
1158 symbol_table_insert (symbol_new (buf, reg_section, i,
1159 &zero_address_frag));
1160 }
1161 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1162 &zero_address_frag));
1163 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1164 &zero_address_frag));
1165 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1166 &zero_address_frag));
1167 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1168 &zero_address_frag));
1169 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1170 &zero_address_frag));
1171 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1172 &zero_address_frag));
1173 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1174 &zero_address_frag));
1175
1176 mips_no_prev_insn (false);
1177
1178 mips_gprmask = 0;
1179 mips_cprmask[0] = 0;
1180 mips_cprmask[1] = 0;
1181 mips_cprmask[2] = 0;
1182 mips_cprmask[3] = 0;
1183
1184 /* set the default alignment for the text section (2**2) */
1185 record_alignment (text_section, 2);
1186
1187 if (USE_GLOBAL_POINTER_OPT)
1188 bfd_set_gp_size (stdoutput, g_switch_value);
1189
1190 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1191 {
1192 /* On a native system, sections must be aligned to 16 byte
1193 boundaries. When configured for an embedded ELF target, we
1194 don't bother. */
1195 if (strcmp (TARGET_OS, "elf") != 0)
1196 {
1197 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1198 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1199 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1200 }
1201
1202 /* Create a .reginfo section for register masks and a .mdebug
1203 section for debugging information. */
1204 {
1205 segT seg;
1206 subsegT subseg;
1207 flagword flags;
1208 segT sec;
1209
1210 seg = now_seg;
1211 subseg = now_subseg;
1212
1213 /* The ABI says this section should be loaded so that the
1214 running program can access it. However, we don't load it
1215 if we are configured for an embedded target */
1216 flags = SEC_READONLY | SEC_DATA;
1217 if (strcmp (TARGET_OS, "elf") != 0)
1218 flags |= SEC_ALLOC | SEC_LOAD;
1219
1220 if (! mips_64)
1221 {
1222 sec = subseg_new (".reginfo", (subsegT) 0);
1223
1224 (void) bfd_set_section_flags (stdoutput, sec, flags);
1225 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1226
1227 #ifdef OBJ_ELF
1228 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1229 #endif
1230 }
1231 else
1232 {
1233 /* The 64-bit ABI uses a .MIPS.options section rather than
1234 .reginfo section. */
1235 sec = subseg_new (".MIPS.options", (subsegT) 0);
1236 (void) bfd_set_section_flags (stdoutput, sec, flags);
1237 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1238
1239 #ifdef OBJ_ELF
1240 /* Set up the option header. */
1241 {
1242 Elf_Internal_Options opthdr;
1243 char *f;
1244
1245 opthdr.kind = ODK_REGINFO;
1246 opthdr.size = (sizeof (Elf_External_Options)
1247 + sizeof (Elf64_External_RegInfo));
1248 opthdr.section = 0;
1249 opthdr.info = 0;
1250 f = frag_more (sizeof (Elf_External_Options));
1251 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1252 (Elf_External_Options *) f);
1253
1254 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1255 }
1256 #endif
1257 }
1258
1259 if (ECOFF_DEBUGGING)
1260 {
1261 sec = subseg_new (".mdebug", (subsegT) 0);
1262 (void) bfd_set_section_flags (stdoutput, sec,
1263 SEC_HAS_CONTENTS | SEC_READONLY);
1264 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1265 }
1266
1267 #ifdef MIPS_STABS_ELF
1268 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1269 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1270 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1271 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1272 #endif
1273
1274 subseg_set (seg, subseg);
1275 }
1276 }
1277
1278 if (! ECOFF_DEBUGGING)
1279 md_obj_begin ();
1280 }
1281
1282 void
1283 md_mips_end ()
1284 {
1285 if (! ECOFF_DEBUGGING)
1286 md_obj_end ();
1287 }
1288
1289 void
1290 md_assemble (str)
1291 char *str;
1292 {
1293 struct mips_cl_insn insn;
1294
1295 imm_expr.X_op = O_absent;
1296 imm_reloc = BFD_RELOC_UNUSED;
1297 imm_unmatched_hi = false;
1298 offset_expr.X_op = O_absent;
1299 offset_reloc = BFD_RELOC_UNUSED;
1300
1301 if (mips_opts.mips16)
1302 mips16_ip (str, &insn);
1303 else
1304 {
1305 mips_ip (str, &insn);
1306 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1307 str, insn.insn_opcode));
1308 }
1309
1310 if (insn_error)
1311 {
1312 as_bad ("%s `%s'", insn_error, str);
1313 return;
1314 }
1315
1316 if (insn.insn_mo->pinfo == INSN_MACRO)
1317 {
1318 if (mips_opts.mips16)
1319 mips16_macro (&insn);
1320 else
1321 macro (&insn);
1322 }
1323 else
1324 {
1325 if (imm_expr.X_op != O_absent)
1326 append_insn ((char *) NULL, &insn, &imm_expr, imm_reloc,
1327 imm_unmatched_hi);
1328 else if (offset_expr.X_op != O_absent)
1329 append_insn ((char *) NULL, &insn, &offset_expr, offset_reloc, false);
1330 else
1331 append_insn ((char *) NULL, &insn, NULL, BFD_RELOC_UNUSED, false);
1332 }
1333 }
1334
1335 /* See whether instruction IP reads register REG. CLASS is the type
1336 of register. */
1337
1338 static int
1339 insn_uses_reg (ip, reg, class)
1340 struct mips_cl_insn *ip;
1341 unsigned int reg;
1342 enum mips_regclass class;
1343 {
1344 if (class == MIPS16_REG)
1345 {
1346 assert (mips_opts.mips16);
1347 reg = mips16_to_32_reg_map[reg];
1348 class = MIPS_GR_REG;
1349 }
1350
1351 /* Don't report on general register 0, since it never changes. */
1352 if (class == MIPS_GR_REG && reg == 0)
1353 return 0;
1354
1355 if (class == MIPS_FP_REG)
1356 {
1357 assert (! mips_opts.mips16);
1358 /* If we are called with either $f0 or $f1, we must check $f0.
1359 This is not optimal, because it will introduce an unnecessary
1360 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1361 need to distinguish reading both $f0 and $f1 or just one of
1362 them. Note that we don't have to check the other way,
1363 because there is no instruction that sets both $f0 and $f1
1364 and requires a delay. */
1365 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1366 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1367 == (reg &~ (unsigned) 1)))
1368 return 1;
1369 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1370 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1371 == (reg &~ (unsigned) 1)))
1372 return 1;
1373 }
1374 else if (! mips_opts.mips16)
1375 {
1376 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1377 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1378 return 1;
1379 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1380 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1381 return 1;
1382 }
1383 else
1384 {
1385 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1386 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1387 & MIPS16OP_MASK_RX)]
1388 == reg))
1389 return 1;
1390 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1391 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1392 & MIPS16OP_MASK_RY)]
1393 == reg))
1394 return 1;
1395 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1396 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1397 & MIPS16OP_MASK_MOVE32Z)]
1398 == reg))
1399 return 1;
1400 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1401 return 1;
1402 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1403 return 1;
1404 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1405 return 1;
1406 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1407 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1408 & MIPS16OP_MASK_REGR32) == reg)
1409 return 1;
1410 }
1411
1412 return 0;
1413 }
1414
1415 /* This function returns true if modifying a register requires a
1416 delay. */
1417
1418 static int
1419 reg_needs_delay (reg)
1420 unsigned int reg;
1421 {
1422 unsigned long prev_pinfo;
1423
1424 prev_pinfo = prev_insn.insn_mo->pinfo;
1425 if (! mips_opts.noreorder
1426 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1427 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1428 || (! gpr_interlocks
1429 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1430 {
1431 /* A load from a coprocessor or from memory. All load
1432 delays delay the use of general register rt for one
1433 instruction on the r3000. The r6000 and r4000 use
1434 interlocks. */
1435 /* Itbl support may require additional care here. */
1436 know (prev_pinfo & INSN_WRITE_GPR_T);
1437 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1438 return 1;
1439 }
1440
1441 return 0;
1442 }
1443
1444 /* Mark instruction labels in mips16 mode. This permits the linker to
1445 handle them specially, such as generating jalx instructions when
1446 needed. We also make them odd for the duration of the assembly, in
1447 order to generate the right sort of code. We will make them even
1448 in the adjust_symtab routine, while leaving them marked. This is
1449 convenient for the debugger and the disassembler. The linker knows
1450 to make them odd again. */
1451
1452 static void
1453 mips16_mark_labels ()
1454 {
1455 if (mips_opts.mips16)
1456 {
1457 struct insn_label_list *l;
1458 valueT val;
1459
1460 for (l = insn_labels; l != NULL; l = l->next)
1461 {
1462 #ifdef OBJ_ELF
1463 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1464 S_SET_OTHER (l->label, STO_MIPS16);
1465 #endif
1466 val = S_GET_VALUE (l->label);
1467 if ((val & 1) == 0)
1468 S_SET_VALUE (l->label, val + 1);
1469 }
1470 }
1471 }
1472
1473 /* Output an instruction. PLACE is where to put the instruction; if
1474 it is NULL, this uses frag_more to get room. IP is the instruction
1475 information. ADDRESS_EXPR is an operand of the instruction to be
1476 used with RELOC_TYPE. */
1477
1478 static void
1479 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1480 char *place;
1481 struct mips_cl_insn *ip;
1482 expressionS *address_expr;
1483 bfd_reloc_code_real_type reloc_type;
1484 boolean unmatched_hi;
1485 {
1486 register unsigned long prev_pinfo, pinfo;
1487 char *f;
1488 fixS *fixp;
1489 int nops = 0;
1490
1491 /* Mark instruction labels in mips16 mode. */
1492 if (mips_opts.mips16)
1493 mips16_mark_labels ();
1494
1495 prev_pinfo = prev_insn.insn_mo->pinfo;
1496 pinfo = ip->insn_mo->pinfo;
1497
1498 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1499 {
1500 int prev_prev_nop;
1501
1502 /* If the previous insn required any delay slots, see if we need
1503 to insert a NOP or two. There are eight kinds of possible
1504 hazards, of which an instruction can have at most one type.
1505 (1) a load from memory delay
1506 (2) a load from a coprocessor delay
1507 (3) an unconditional branch delay
1508 (4) a conditional branch delay
1509 (5) a move to coprocessor register delay
1510 (6) a load coprocessor register from memory delay
1511 (7) a coprocessor condition code delay
1512 (8) a HI/LO special register delay
1513
1514 There are a lot of optimizations we could do that we don't.
1515 In particular, we do not, in general, reorder instructions.
1516 If you use gcc with optimization, it will reorder
1517 instructions and generally do much more optimization then we
1518 do here; repeating all that work in the assembler would only
1519 benefit hand written assembly code, and does not seem worth
1520 it. */
1521
1522 /* This is how a NOP is emitted. */
1523 #define emit_nop() \
1524 (mips_opts.mips16 \
1525 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1526 : md_number_to_chars (frag_more (4), 0, 4))
1527
1528 /* The previous insn might require a delay slot, depending upon
1529 the contents of the current insn. */
1530 if (! mips_opts.mips16
1531 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1532 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1533 && ! cop_interlocks)
1534 || (! gpr_interlocks
1535 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1536 {
1537 /* A load from a coprocessor or from memory. All load
1538 delays delay the use of general register rt for one
1539 instruction on the r3000. The r6000 and r4000 use
1540 interlocks. */
1541 /* Itbl support may require additional care here. */
1542 know (prev_pinfo & INSN_WRITE_GPR_T);
1543 if (mips_optimize == 0
1544 || insn_uses_reg (ip,
1545 ((prev_insn.insn_opcode >> OP_SH_RT)
1546 & OP_MASK_RT),
1547 MIPS_GR_REG))
1548 ++nops;
1549 }
1550 else if (! mips_opts.mips16
1551 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1552 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1553 && ! cop_interlocks)
1554 || (mips_opts.isa == ISA_MIPS1
1555 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1556 {
1557 /* A generic coprocessor delay. The previous instruction
1558 modified a coprocessor general or control register. If
1559 it modified a control register, we need to avoid any
1560 coprocessor instruction (this is probably not always
1561 required, but it sometimes is). If it modified a general
1562 register, we avoid using that register.
1563
1564 On the r6000 and r4000 loading a coprocessor register
1565 from memory is interlocked, and does not require a delay.
1566
1567 This case is not handled very well. There is no special
1568 knowledge of CP0 handling, and the coprocessors other
1569 than the floating point unit are not distinguished at
1570 all. */
1571 /* Itbl support may require additional care here. FIXME!
1572 Need to modify this to include knowledge about
1573 user specified delays! */
1574 if (prev_pinfo & INSN_WRITE_FPR_T)
1575 {
1576 if (mips_optimize == 0
1577 || insn_uses_reg (ip,
1578 ((prev_insn.insn_opcode >> OP_SH_FT)
1579 & OP_MASK_FT),
1580 MIPS_FP_REG))
1581 ++nops;
1582 }
1583 else if (prev_pinfo & INSN_WRITE_FPR_S)
1584 {
1585 if (mips_optimize == 0
1586 || insn_uses_reg (ip,
1587 ((prev_insn.insn_opcode >> OP_SH_FS)
1588 & OP_MASK_FS),
1589 MIPS_FP_REG))
1590 ++nops;
1591 }
1592 else
1593 {
1594 /* We don't know exactly what the previous instruction
1595 does. If the current instruction uses a coprocessor
1596 register, we must insert a NOP. If previous
1597 instruction may set the condition codes, and the
1598 current instruction uses them, we must insert two
1599 NOPS. */
1600 /* Itbl support may require additional care here. */
1601 if (mips_optimize == 0
1602 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1603 && (pinfo & INSN_READ_COND_CODE)))
1604 nops += 2;
1605 else if (pinfo & INSN_COP)
1606 ++nops;
1607 }
1608 }
1609 else if (! mips_opts.mips16
1610 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1611 && (prev_pinfo & INSN_WRITE_COND_CODE)
1612 && ! cop_interlocks)
1613 {
1614 /* The previous instruction sets the coprocessor condition
1615 codes, but does not require a general coprocessor delay
1616 (this means it is a floating point comparison
1617 instruction). If this instruction uses the condition
1618 codes, we need to insert a single NOP. */
1619 /* Itbl support may require additional care here. */
1620 if (mips_optimize == 0
1621 || (pinfo & INSN_READ_COND_CODE))
1622 ++nops;
1623 }
1624
1625 /* If we're fixing up mfhi/mflo for the r7000 and the
1626 previous insn was an mfhi/mflo and the current insn
1627 reads the register that the mfhi/mflo wrote to, then
1628 insert two nops. */
1629
1630 else if (mips_7000_hilo_fix
1631 && MF_HILO_INSN (prev_pinfo)
1632 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1633 & OP_MASK_RD),
1634 MIPS_GR_REG))
1635 {
1636 nops += 2;
1637 }
1638
1639 /* If we're fixing up mfhi/mflo for the r7000 and the
1640 2nd previous insn was an mfhi/mflo and the current insn
1641 reads the register that the mfhi/mflo wrote to, then
1642 insert one nop. */
1643
1644 else if (mips_7000_hilo_fix
1645 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1646 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1647 & OP_MASK_RD),
1648 MIPS_GR_REG))
1649
1650 {
1651 nops += 1;
1652 }
1653
1654 else if (prev_pinfo & INSN_READ_LO)
1655 {
1656 /* The previous instruction reads the LO register; if the
1657 current instruction writes to the LO register, we must
1658 insert two NOPS. Some newer processors have interlocks.
1659 Also the tx39's multiply instructions can be exectuted
1660 immediatly after a read from HI/LO (without the delay),
1661 though the tx39's divide insns still do require the
1662 delay. */
1663 if (! (hilo_interlocks
1664 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1665 && (mips_optimize == 0
1666 || (pinfo & INSN_WRITE_LO)))
1667 nops += 2;
1668 /* Most mips16 branch insns don't have a delay slot.
1669 If a read from LO is immediately followed by a branch
1670 to a write to LO we have a read followed by a write
1671 less than 2 insns away. We assume the target of
1672 a branch might be a write to LO, and insert a nop
1673 between a read and an immediately following branch. */
1674 else if (mips_opts.mips16
1675 && (mips_optimize == 0
1676 || (pinfo & MIPS16_INSN_BRANCH)))
1677 nops += 1;
1678 }
1679 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1680 {
1681 /* The previous instruction reads the HI register; if the
1682 current instruction writes to the HI register, we must
1683 insert a NOP. Some newer processors have interlocks.
1684 Also the note tx39's multiply above. */
1685 if (! (hilo_interlocks
1686 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1687 && (mips_optimize == 0
1688 || (pinfo & INSN_WRITE_HI)))
1689 nops += 2;
1690 /* Most mips16 branch insns don't have a delay slot.
1691 If a read from HI is immediately followed by a branch
1692 to a write to HI we have a read followed by a write
1693 less than 2 insns away. We assume the target of
1694 a branch might be a write to HI, and insert a nop
1695 between a read and an immediately following branch. */
1696 else if (mips_opts.mips16
1697 && (mips_optimize == 0
1698 || (pinfo & MIPS16_INSN_BRANCH)))
1699 nops += 1;
1700 }
1701
1702 /* If the previous instruction was in a noreorder section, then
1703 we don't want to insert the nop after all. */
1704 /* Itbl support may require additional care here. */
1705 if (prev_insn_unreordered)
1706 nops = 0;
1707
1708 /* There are two cases which require two intervening
1709 instructions: 1) setting the condition codes using a move to
1710 coprocessor instruction which requires a general coprocessor
1711 delay and then reading the condition codes 2) reading the HI
1712 or LO register and then writing to it (except on processors
1713 which have interlocks). If we are not already emitting a NOP
1714 instruction, we must check for these cases compared to the
1715 instruction previous to the previous instruction. */
1716 if ((! mips_opts.mips16
1717 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1718 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1719 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1720 && (pinfo & INSN_READ_COND_CODE)
1721 && ! cop_interlocks)
1722 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1723 && (pinfo & INSN_WRITE_LO)
1724 && ! (hilo_interlocks
1725 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1726 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1727 && (pinfo & INSN_WRITE_HI)
1728 && ! (hilo_interlocks
1729 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1730 prev_prev_nop = 1;
1731 else
1732 prev_prev_nop = 0;
1733
1734 if (prev_prev_insn_unreordered)
1735 prev_prev_nop = 0;
1736
1737 if (prev_prev_nop && nops == 0)
1738 ++nops;
1739
1740 /* If we are being given a nop instruction, don't bother with
1741 one of the nops we would otherwise output. This will only
1742 happen when a nop instruction is used with mips_optimize set
1743 to 0. */
1744 if (nops > 0
1745 && ! mips_opts.noreorder
1746 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1747 --nops;
1748
1749 /* Now emit the right number of NOP instructions. */
1750 if (nops > 0 && ! mips_opts.noreorder)
1751 {
1752 fragS *old_frag;
1753 unsigned long old_frag_offset;
1754 int i;
1755 struct insn_label_list *l;
1756
1757 old_frag = frag_now;
1758 old_frag_offset = frag_now_fix ();
1759
1760 for (i = 0; i < nops; i++)
1761 emit_nop ();
1762
1763 if (listing)
1764 {
1765 listing_prev_line ();
1766 /* We may be at the start of a variant frag. In case we
1767 are, make sure there is enough space for the frag
1768 after the frags created by listing_prev_line. The
1769 argument to frag_grow here must be at least as large
1770 as the argument to all other calls to frag_grow in
1771 this file. We don't have to worry about being in the
1772 middle of a variant frag, because the variants insert
1773 all needed nop instructions themselves. */
1774 frag_grow (40);
1775 }
1776
1777 for (l = insn_labels; l != NULL; l = l->next)
1778 {
1779 valueT val;
1780
1781 assert (S_GET_SEGMENT (l->label) == now_seg);
1782 symbol_set_frag (l->label, frag_now);
1783 val = (valueT) frag_now_fix ();
1784 /* mips16 text labels are stored as odd. */
1785 if (mips_opts.mips16)
1786 val += 1;
1787 S_SET_VALUE (l->label, val);
1788 }
1789
1790 #ifndef NO_ECOFF_DEBUGGING
1791 if (ECOFF_DEBUGGING)
1792 ecoff_fix_loc (old_frag, old_frag_offset);
1793 #endif
1794 }
1795 else if (prev_nop_frag != NULL)
1796 {
1797 /* We have a frag holding nops we may be able to remove. If
1798 we don't need any nops, we can decrease the size of
1799 prev_nop_frag by the size of one instruction. If we do
1800 need some nops, we count them in prev_nops_required. */
1801 if (prev_nop_frag_since == 0)
1802 {
1803 if (nops == 0)
1804 {
1805 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1806 --prev_nop_frag_holds;
1807 }
1808 else
1809 prev_nop_frag_required += nops;
1810 }
1811 else
1812 {
1813 if (prev_prev_nop == 0)
1814 {
1815 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1816 --prev_nop_frag_holds;
1817 }
1818 else
1819 ++prev_nop_frag_required;
1820 }
1821
1822 if (prev_nop_frag_holds <= prev_nop_frag_required)
1823 prev_nop_frag = NULL;
1824
1825 ++prev_nop_frag_since;
1826
1827 /* Sanity check: by the time we reach the second instruction
1828 after prev_nop_frag, we should have used up all the nops
1829 one way or another. */
1830 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1831 }
1832 }
1833
1834 if (reloc_type > BFD_RELOC_UNUSED)
1835 {
1836 /* We need to set up a variant frag. */
1837 assert (mips_opts.mips16 && address_expr != NULL);
1838 f = frag_var (rs_machine_dependent, 4, 0,
1839 RELAX_MIPS16_ENCODE (reloc_type - BFD_RELOC_UNUSED,
1840 mips16_small, mips16_ext,
1841 (prev_pinfo
1842 & INSN_UNCOND_BRANCH_DELAY),
1843 (prev_insn_reloc_type
1844 == BFD_RELOC_MIPS16_JMP)),
1845 make_expr_symbol (address_expr), (offsetT) 0,
1846 (char *) NULL);
1847 }
1848 else if (place != NULL)
1849 f = place;
1850 else if (mips_opts.mips16
1851 && ! ip->use_extend
1852 && reloc_type != BFD_RELOC_MIPS16_JMP)
1853 {
1854 /* Make sure there is enough room to swap this instruction with
1855 a following jump instruction. */
1856 frag_grow (6);
1857 f = frag_more (2);
1858 }
1859 else
1860 {
1861 if (mips_opts.mips16
1862 && mips_opts.noreorder
1863 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1864 as_warn (_("extended instruction in delay slot"));
1865
1866 f = frag_more (4);
1867 }
1868
1869 fixp = NULL;
1870 if (address_expr != NULL && reloc_type < BFD_RELOC_UNUSED)
1871 {
1872 if (address_expr->X_op == O_constant)
1873 {
1874 switch (reloc_type)
1875 {
1876 case BFD_RELOC_32:
1877 ip->insn_opcode |= address_expr->X_add_number;
1878 break;
1879
1880 case BFD_RELOC_LO16:
1881 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1882 break;
1883
1884 case BFD_RELOC_MIPS_JMP:
1885 if ((address_expr->X_add_number & 3) != 0)
1886 as_bad (_("jump to misaligned address (0x%lx)"),
1887 (unsigned long) address_expr->X_add_number);
1888 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1889 break;
1890
1891 case BFD_RELOC_MIPS16_JMP:
1892 if ((address_expr->X_add_number & 3) != 0)
1893 as_bad (_("jump to misaligned address (0x%lx)"),
1894 (unsigned long) address_expr->X_add_number);
1895 ip->insn_opcode |=
1896 (((address_expr->X_add_number & 0x7c0000) << 3)
1897 | ((address_expr->X_add_number & 0xf800000) >> 7)
1898 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1899 break;
1900
1901 case BFD_RELOC_16_PCREL:
1902 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
1903 break;
1904
1905 case BFD_RELOC_16_PCREL_S2:
1906 goto need_reloc;
1907
1908 default:
1909 internalError ();
1910 }
1911 }
1912 else
1913 {
1914 need_reloc:
1915 /* Don't generate a reloc if we are writing into a variant
1916 frag. */
1917 if (place == NULL)
1918 {
1919 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1920 address_expr,
1921 (reloc_type == BFD_RELOC_16_PCREL
1922 || reloc_type == BFD_RELOC_16_PCREL_S2),
1923 reloc_type);
1924 if (unmatched_hi)
1925 {
1926 struct mips_hi_fixup *hi_fixup;
1927
1928 assert (reloc_type == BFD_RELOC_HI16_S);
1929 hi_fixup = ((struct mips_hi_fixup *)
1930 xmalloc (sizeof (struct mips_hi_fixup)));
1931 hi_fixup->fixp = fixp;
1932 hi_fixup->seg = now_seg;
1933 hi_fixup->next = mips_hi_fixup_list;
1934 mips_hi_fixup_list = hi_fixup;
1935 }
1936 }
1937 }
1938 }
1939
1940 if (! mips_opts.mips16)
1941 md_number_to_chars (f, ip->insn_opcode, 4);
1942 else if (reloc_type == BFD_RELOC_MIPS16_JMP)
1943 {
1944 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
1945 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
1946 }
1947 else
1948 {
1949 if (ip->use_extend)
1950 {
1951 md_number_to_chars (f, 0xf000 | ip->extend, 2);
1952 f += 2;
1953 }
1954 md_number_to_chars (f, ip->insn_opcode, 2);
1955 }
1956
1957 /* Update the register mask information. */
1958 if (! mips_opts.mips16)
1959 {
1960 if (pinfo & INSN_WRITE_GPR_D)
1961 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
1962 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
1963 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
1964 if (pinfo & INSN_READ_GPR_S)
1965 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
1966 if (pinfo & INSN_WRITE_GPR_31)
1967 mips_gprmask |= 1 << 31;
1968 if (pinfo & INSN_WRITE_FPR_D)
1969 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
1970 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
1971 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
1972 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
1973 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
1974 if ((pinfo & INSN_READ_FPR_R) != 0)
1975 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
1976 if (pinfo & INSN_COP)
1977 {
1978 /* We don't keep enough information to sort these cases out.
1979 The itbl support does keep this information however, although
1980 we currently don't support itbl fprmats as part of the cop
1981 instruction. May want to add this support in the future. */
1982 }
1983 /* Never set the bit for $0, which is always zero. */
1984 mips_gprmask &= ~1 << 0;
1985 }
1986 else
1987 {
1988 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
1989 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
1990 & MIPS16OP_MASK_RX);
1991 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
1992 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
1993 & MIPS16OP_MASK_RY);
1994 if (pinfo & MIPS16_INSN_WRITE_Z)
1995 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
1996 & MIPS16OP_MASK_RZ);
1997 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
1998 mips_gprmask |= 1 << TREG;
1999 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2000 mips_gprmask |= 1 << SP;
2001 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2002 mips_gprmask |= 1 << RA;
2003 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2004 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2005 if (pinfo & MIPS16_INSN_READ_Z)
2006 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2007 & MIPS16OP_MASK_MOVE32Z);
2008 if (pinfo & MIPS16_INSN_READ_GPR_X)
2009 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2010 & MIPS16OP_MASK_REGR32);
2011 }
2012
2013 if (place == NULL && ! mips_opts.noreorder)
2014 {
2015 /* Filling the branch delay slot is more complex. We try to
2016 switch the branch with the previous instruction, which we can
2017 do if the previous instruction does not set up a condition
2018 that the branch tests and if the branch is not itself the
2019 target of any branch. */
2020 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2021 || (pinfo & INSN_COND_BRANCH_DELAY))
2022 {
2023 if (mips_optimize < 2
2024 /* If we have seen .set volatile or .set nomove, don't
2025 optimize. */
2026 || mips_opts.nomove != 0
2027 /* If we had to emit any NOP instructions, then we
2028 already know we can not swap. */
2029 || nops != 0
2030 /* If we don't even know the previous insn, we can not
2031 swap. */
2032 || ! prev_insn_valid
2033 /* If the previous insn is already in a branch delay
2034 slot, then we can not swap. */
2035 || prev_insn_is_delay_slot
2036 /* If the previous previous insn was in a .set
2037 noreorder, we can't swap. Actually, the MIPS
2038 assembler will swap in this situation. However, gcc
2039 configured -with-gnu-as will generate code like
2040 .set noreorder
2041 lw $4,XXX
2042 .set reorder
2043 INSN
2044 bne $4,$0,foo
2045 in which we can not swap the bne and INSN. If gcc is
2046 not configured -with-gnu-as, it does not output the
2047 .set pseudo-ops. We don't have to check
2048 prev_insn_unreordered, because prev_insn_valid will
2049 be 0 in that case. We don't want to use
2050 prev_prev_insn_valid, because we do want to be able
2051 to swap at the start of a function. */
2052 || prev_prev_insn_unreordered
2053 /* If the branch is itself the target of a branch, we
2054 can not swap. We cheat on this; all we check for is
2055 whether there is a label on this instruction. If
2056 there are any branches to anything other than a
2057 label, users must use .set noreorder. */
2058 || insn_labels != NULL
2059 /* If the previous instruction is in a variant frag, we
2060 can not do the swap. This does not apply to the
2061 mips16, which uses variant frags for different
2062 purposes. */
2063 || (! mips_opts.mips16
2064 && prev_insn_frag->fr_type == rs_machine_dependent)
2065 /* If the branch reads the condition codes, we don't
2066 even try to swap, because in the sequence
2067 ctc1 $X,$31
2068 INSN
2069 INSN
2070 bc1t LABEL
2071 we can not swap, and I don't feel like handling that
2072 case. */
2073 || (! mips_opts.mips16
2074 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2075 && (pinfo & INSN_READ_COND_CODE))
2076 /* We can not swap with an instruction that requires a
2077 delay slot, becase the target of the branch might
2078 interfere with that instruction. */
2079 || (! mips_opts.mips16
2080 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2081 && (prev_pinfo
2082 /* Itbl support may require additional care here. */
2083 & (INSN_LOAD_COPROC_DELAY
2084 | INSN_COPROC_MOVE_DELAY
2085 | INSN_WRITE_COND_CODE)))
2086 || (! (hilo_interlocks
2087 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2088 && (prev_pinfo
2089 & (INSN_READ_LO
2090 | INSN_READ_HI)))
2091 || (! mips_opts.mips16
2092 && ! gpr_interlocks
2093 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2094 || (! mips_opts.mips16
2095 && mips_opts.isa == ISA_MIPS1
2096 /* Itbl support may require additional care here. */
2097 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2098 /* We can not swap with a branch instruction. */
2099 || (prev_pinfo
2100 & (INSN_UNCOND_BRANCH_DELAY
2101 | INSN_COND_BRANCH_DELAY
2102 | INSN_COND_BRANCH_LIKELY))
2103 /* We do not swap with a trap instruction, since it
2104 complicates trap handlers to have the trap
2105 instruction be in a delay slot. */
2106 || (prev_pinfo & INSN_TRAP)
2107 /* If the branch reads a register that the previous
2108 instruction sets, we can not swap. */
2109 || (! mips_opts.mips16
2110 && (prev_pinfo & INSN_WRITE_GPR_T)
2111 && insn_uses_reg (ip,
2112 ((prev_insn.insn_opcode >> OP_SH_RT)
2113 & OP_MASK_RT),
2114 MIPS_GR_REG))
2115 || (! mips_opts.mips16
2116 && (prev_pinfo & INSN_WRITE_GPR_D)
2117 && insn_uses_reg (ip,
2118 ((prev_insn.insn_opcode >> OP_SH_RD)
2119 & OP_MASK_RD),
2120 MIPS_GR_REG))
2121 || (mips_opts.mips16
2122 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2123 && insn_uses_reg (ip,
2124 ((prev_insn.insn_opcode
2125 >> MIPS16OP_SH_RX)
2126 & MIPS16OP_MASK_RX),
2127 MIPS16_REG))
2128 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2129 && insn_uses_reg (ip,
2130 ((prev_insn.insn_opcode
2131 >> MIPS16OP_SH_RY)
2132 & MIPS16OP_MASK_RY),
2133 MIPS16_REG))
2134 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2135 && insn_uses_reg (ip,
2136 ((prev_insn.insn_opcode
2137 >> MIPS16OP_SH_RZ)
2138 & MIPS16OP_MASK_RZ),
2139 MIPS16_REG))
2140 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2141 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2142 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2143 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2144 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2145 && insn_uses_reg (ip,
2146 MIPS16OP_EXTRACT_REG32R (prev_insn.
2147 insn_opcode),
2148 MIPS_GR_REG))))
2149 /* If the branch writes a register that the previous
2150 instruction sets, we can not swap (we know that
2151 branches write only to RD or to $31). */
2152 || (! mips_opts.mips16
2153 && (prev_pinfo & INSN_WRITE_GPR_T)
2154 && (((pinfo & INSN_WRITE_GPR_D)
2155 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2156 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2157 || ((pinfo & INSN_WRITE_GPR_31)
2158 && (((prev_insn.insn_opcode >> OP_SH_RT)
2159 & OP_MASK_RT)
2160 == 31))))
2161 || (! mips_opts.mips16
2162 && (prev_pinfo & INSN_WRITE_GPR_D)
2163 && (((pinfo & INSN_WRITE_GPR_D)
2164 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2165 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2166 || ((pinfo & INSN_WRITE_GPR_31)
2167 && (((prev_insn.insn_opcode >> OP_SH_RD)
2168 & OP_MASK_RD)
2169 == 31))))
2170 || (mips_opts.mips16
2171 && (pinfo & MIPS16_INSN_WRITE_31)
2172 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2173 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2174 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2175 == RA))))
2176 /* If the branch writes a register that the previous
2177 instruction reads, we can not swap (we know that
2178 branches only write to RD or to $31). */
2179 || (! mips_opts.mips16
2180 && (pinfo & INSN_WRITE_GPR_D)
2181 && insn_uses_reg (&prev_insn,
2182 ((ip->insn_opcode >> OP_SH_RD)
2183 & OP_MASK_RD),
2184 MIPS_GR_REG))
2185 || (! mips_opts.mips16
2186 && (pinfo & INSN_WRITE_GPR_31)
2187 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2188 || (mips_opts.mips16
2189 && (pinfo & MIPS16_INSN_WRITE_31)
2190 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2191 /* If we are generating embedded PIC code, the branch
2192 might be expanded into a sequence which uses $at, so
2193 we can't swap with an instruction which reads it. */
2194 || (mips_pic == EMBEDDED_PIC
2195 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2196 /* If the previous previous instruction has a load
2197 delay, and sets a register that the branch reads, we
2198 can not swap. */
2199 || (! mips_opts.mips16
2200 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2201 /* Itbl support may require additional care here. */
2202 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2203 || (! gpr_interlocks
2204 && (prev_prev_insn.insn_mo->pinfo
2205 & INSN_LOAD_MEMORY_DELAY)))
2206 && insn_uses_reg (ip,
2207 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2208 & OP_MASK_RT),
2209 MIPS_GR_REG))
2210 /* If one instruction sets a condition code and the
2211 other one uses a condition code, we can not swap. */
2212 || ((pinfo & INSN_READ_COND_CODE)
2213 && (prev_pinfo & INSN_WRITE_COND_CODE))
2214 || ((pinfo & INSN_WRITE_COND_CODE)
2215 && (prev_pinfo & INSN_READ_COND_CODE))
2216 /* If the previous instruction uses the PC, we can not
2217 swap. */
2218 || (mips_opts.mips16
2219 && (prev_pinfo & MIPS16_INSN_READ_PC))
2220 /* If the previous instruction was extended, we can not
2221 swap. */
2222 || (mips_opts.mips16 && prev_insn_extended)
2223 /* If the previous instruction had a fixup in mips16
2224 mode, we can not swap. This normally means that the
2225 previous instruction was a 4 byte branch anyhow. */
2226 || (mips_opts.mips16 && prev_insn_fixp)
2227 /* If the previous instruction is a sync, sync.l, or
2228 sync.p, we can not swap. */
2229 || (prev_pinfo & INSN_SYNC))
2230 {
2231 /* We could do even better for unconditional branches to
2232 portions of this object file; we could pick up the
2233 instruction at the destination, put it in the delay
2234 slot, and bump the destination address. */
2235 emit_nop ();
2236 /* Update the previous insn information. */
2237 prev_prev_insn = *ip;
2238 prev_insn.insn_mo = &dummy_opcode;
2239 }
2240 else
2241 {
2242 /* It looks like we can actually do the swap. */
2243 if (! mips_opts.mips16)
2244 {
2245 char *prev_f;
2246 char temp[4];
2247
2248 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2249 memcpy (temp, prev_f, 4);
2250 memcpy (prev_f, f, 4);
2251 memcpy (f, temp, 4);
2252 if (prev_insn_fixp)
2253 {
2254 prev_insn_fixp->fx_frag = frag_now;
2255 prev_insn_fixp->fx_where = f - frag_now->fr_literal;
2256 }
2257 if (fixp)
2258 {
2259 fixp->fx_frag = prev_insn_frag;
2260 fixp->fx_where = prev_insn_where;
2261 }
2262 }
2263 else
2264 {
2265 char *prev_f;
2266 char temp[2];
2267
2268 assert (prev_insn_fixp == NULL);
2269 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2270 memcpy (temp, prev_f, 2);
2271 memcpy (prev_f, f, 2);
2272 if (reloc_type != BFD_RELOC_MIPS16_JMP)
2273 {
2274 assert (reloc_type == BFD_RELOC_UNUSED);
2275 memcpy (f, temp, 2);
2276 }
2277 else
2278 {
2279 memcpy (f, f + 2, 2);
2280 memcpy (f + 2, temp, 2);
2281 }
2282 if (fixp)
2283 {
2284 fixp->fx_frag = prev_insn_frag;
2285 fixp->fx_where = prev_insn_where;
2286 }
2287 }
2288
2289 /* Update the previous insn information; leave prev_insn
2290 unchanged. */
2291 prev_prev_insn = *ip;
2292 }
2293 prev_insn_is_delay_slot = 1;
2294
2295 /* If that was an unconditional branch, forget the previous
2296 insn information. */
2297 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2298 {
2299 prev_prev_insn.insn_mo = &dummy_opcode;
2300 prev_insn.insn_mo = &dummy_opcode;
2301 }
2302
2303 prev_insn_fixp = NULL;
2304 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2305 prev_insn_extended = 0;
2306 }
2307 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2308 {
2309 /* We don't yet optimize a branch likely. What we should do
2310 is look at the target, copy the instruction found there
2311 into the delay slot, and increment the branch to jump to
2312 the next instruction. */
2313 emit_nop ();
2314 /* Update the previous insn information. */
2315 prev_prev_insn = *ip;
2316 prev_insn.insn_mo = &dummy_opcode;
2317 prev_insn_fixp = NULL;
2318 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2319 prev_insn_extended = 0;
2320 }
2321 else
2322 {
2323 /* Update the previous insn information. */
2324 if (nops > 0)
2325 prev_prev_insn.insn_mo = &dummy_opcode;
2326 else
2327 prev_prev_insn = prev_insn;
2328 prev_insn = *ip;
2329
2330 /* Any time we see a branch, we always fill the delay slot
2331 immediately; since this insn is not a branch, we know it
2332 is not in a delay slot. */
2333 prev_insn_is_delay_slot = 0;
2334
2335 prev_insn_fixp = fixp;
2336 prev_insn_reloc_type = reloc_type;
2337 if (mips_opts.mips16)
2338 prev_insn_extended = (ip->use_extend
2339 || reloc_type > BFD_RELOC_UNUSED);
2340 }
2341
2342 prev_prev_insn_unreordered = prev_insn_unreordered;
2343 prev_insn_unreordered = 0;
2344 prev_insn_frag = frag_now;
2345 prev_insn_where = f - frag_now->fr_literal;
2346 prev_insn_valid = 1;
2347 }
2348 else if (place == NULL)
2349 {
2350 /* We need to record a bit of information even when we are not
2351 reordering, in order to determine the base address for mips16
2352 PC relative relocs. */
2353 prev_prev_insn = prev_insn;
2354 prev_insn = *ip;
2355 prev_insn_reloc_type = reloc_type;
2356 prev_prev_insn_unreordered = prev_insn_unreordered;
2357 prev_insn_unreordered = 1;
2358 }
2359
2360 /* We just output an insn, so the next one doesn't have a label. */
2361 mips_clear_insn_labels ();
2362
2363 /* We must ensure that a fixup associated with an unmatched %hi
2364 reloc does not become a variant frag. Otherwise, the
2365 rearrangement of %hi relocs in frob_file may confuse
2366 tc_gen_reloc. */
2367 if (unmatched_hi)
2368 {
2369 frag_wane (frag_now);
2370 frag_new (0);
2371 }
2372 }
2373
2374 /* This function forgets that there was any previous instruction or
2375 label. If PRESERVE is non-zero, it remembers enough information to
2376 know whether nops are needed before a noreorder section. */
2377
2378 static void
2379 mips_no_prev_insn (preserve)
2380 int preserve;
2381 {
2382 if (! preserve)
2383 {
2384 prev_insn.insn_mo = &dummy_opcode;
2385 prev_prev_insn.insn_mo = &dummy_opcode;
2386 prev_nop_frag = NULL;
2387 prev_nop_frag_holds = 0;
2388 prev_nop_frag_required = 0;
2389 prev_nop_frag_since = 0;
2390 }
2391 prev_insn_valid = 0;
2392 prev_insn_is_delay_slot = 0;
2393 prev_insn_unreordered = 0;
2394 prev_insn_extended = 0;
2395 prev_insn_reloc_type = BFD_RELOC_UNUSED;
2396 prev_prev_insn_unreordered = 0;
2397 mips_clear_insn_labels ();
2398 }
2399
2400 /* This function must be called whenever we turn on noreorder or emit
2401 something other than instructions. It inserts any NOPS which might
2402 be needed by the previous instruction, and clears the information
2403 kept for the previous instructions. The INSNS parameter is true if
2404 instructions are to follow. */
2405
2406 static void
2407 mips_emit_delays (insns)
2408 boolean insns;
2409 {
2410 if (! mips_opts.noreorder)
2411 {
2412 int nops;
2413
2414 nops = 0;
2415 if ((! mips_opts.mips16
2416 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2417 && (! cop_interlocks
2418 && (prev_insn.insn_mo->pinfo
2419 & (INSN_LOAD_COPROC_DELAY
2420 | INSN_COPROC_MOVE_DELAY
2421 | INSN_WRITE_COND_CODE))))
2422 || (! hilo_interlocks
2423 && (prev_insn.insn_mo->pinfo
2424 & (INSN_READ_LO
2425 | INSN_READ_HI)))
2426 || (! mips_opts.mips16
2427 && ! gpr_interlocks
2428 && (prev_insn.insn_mo->pinfo
2429 & INSN_LOAD_MEMORY_DELAY))
2430 || (! mips_opts.mips16
2431 && mips_opts.isa == ISA_MIPS1
2432 && (prev_insn.insn_mo->pinfo
2433 & INSN_COPROC_MEMORY_DELAY)))
2434 {
2435 /* Itbl support may require additional care here. */
2436 ++nops;
2437 if ((! mips_opts.mips16
2438 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2439 && (! cop_interlocks
2440 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2441 || (! hilo_interlocks
2442 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2443 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2444 ++nops;
2445
2446 if (prev_insn_unreordered)
2447 nops = 0;
2448 }
2449 else if ((! mips_opts.mips16
2450 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2451 && (! cop_interlocks
2452 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2453 || (! hilo_interlocks
2454 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2455 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2456 {
2457 /* Itbl support may require additional care here. */
2458 if (! prev_prev_insn_unreordered)
2459 ++nops;
2460 }
2461
2462 if (nops > 0)
2463 {
2464 struct insn_label_list *l;
2465
2466 if (insns)
2467 {
2468 /* Record the frag which holds the nop instructions, so
2469 that we can remove them if we don't need them. */
2470 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2471 prev_nop_frag = frag_now;
2472 prev_nop_frag_holds = nops;
2473 prev_nop_frag_required = 0;
2474 prev_nop_frag_since = 0;
2475 }
2476
2477 for (; nops > 0; --nops)
2478 emit_nop ();
2479
2480 if (insns)
2481 {
2482 /* Move on to a new frag, so that it is safe to simply
2483 decrease the size of prev_nop_frag. */
2484 frag_wane (frag_now);
2485 frag_new (0);
2486 }
2487
2488 for (l = insn_labels; l != NULL; l = l->next)
2489 {
2490 valueT val;
2491
2492 assert (S_GET_SEGMENT (l->label) == now_seg);
2493 symbol_set_frag (l->label, frag_now);
2494 val = (valueT) frag_now_fix ();
2495 /* mips16 text labels are stored as odd. */
2496 if (mips_opts.mips16)
2497 val += 1;
2498 S_SET_VALUE (l->label, val);
2499 }
2500 }
2501 }
2502
2503 /* Mark instruction labels in mips16 mode. */
2504 if (mips_opts.mips16 && insns)
2505 mips16_mark_labels ();
2506
2507 mips_no_prev_insn (insns);
2508 }
2509
2510 /* Build an instruction created by a macro expansion. This is passed
2511 a pointer to the count of instructions created so far, an
2512 expression, the name of the instruction to build, an operand format
2513 string, and corresponding arguments. */
2514
2515 #ifdef USE_STDARG
2516 static void
2517 macro_build (char *place,
2518 int *counter,
2519 expressionS * ep,
2520 const char *name,
2521 const char *fmt,
2522 ...)
2523 #else
2524 static void
2525 macro_build (place, counter, ep, name, fmt, va_alist)
2526 char *place;
2527 int *counter;
2528 expressionS *ep;
2529 const char *name;
2530 const char *fmt;
2531 va_dcl
2532 #endif
2533 {
2534 struct mips_cl_insn insn;
2535 bfd_reloc_code_real_type r;
2536 va_list args;
2537
2538 #ifdef USE_STDARG
2539 va_start (args, fmt);
2540 #else
2541 va_start (args);
2542 #endif
2543
2544 /*
2545 * If the macro is about to expand into a second instruction,
2546 * print a warning if needed. We need to pass ip as a parameter
2547 * to generate a better warning message here...
2548 */
2549 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2550 as_warn (_("Macro instruction expanded into multiple instructions"));
2551
2552 if (place == NULL)
2553 *counter += 1; /* bump instruction counter */
2554
2555 if (mips_opts.mips16)
2556 {
2557 mips16_macro_build (place, counter, ep, name, fmt, args);
2558 va_end (args);
2559 return;
2560 }
2561
2562 r = BFD_RELOC_UNUSED;
2563 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2564 assert (insn.insn_mo);
2565 assert (strcmp (name, insn.insn_mo->name) == 0);
2566
2567 /* Search until we get a match for NAME. */
2568 while (1)
2569 {
2570 if (strcmp (fmt, insn.insn_mo->args) == 0
2571 && insn.insn_mo->pinfo != INSN_MACRO
2572 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2573 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2574 break;
2575
2576 ++insn.insn_mo;
2577 assert (insn.insn_mo->name);
2578 assert (strcmp (name, insn.insn_mo->name) == 0);
2579 }
2580
2581 insn.insn_opcode = insn.insn_mo->match;
2582 for (;;)
2583 {
2584 switch (*fmt++)
2585 {
2586 case '\0':
2587 break;
2588
2589 case ',':
2590 case '(':
2591 case ')':
2592 continue;
2593
2594 case 't':
2595 case 'w':
2596 case 'E':
2597 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2598 continue;
2599
2600 case 'c':
2601 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2602 continue;
2603
2604 case 'T':
2605 case 'W':
2606 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2607 continue;
2608
2609 case 'd':
2610 case 'G':
2611 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2612 continue;
2613
2614 case 'U':
2615 {
2616 int tmp = va_arg (args, int);
2617
2618 insn.insn_opcode |= tmp << OP_SH_RT;
2619 insn.insn_opcode |= tmp << OP_SH_RD;
2620 continue;
2621 }
2622
2623 case 'V':
2624 case 'S':
2625 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2626 continue;
2627
2628 case 'z':
2629 continue;
2630
2631 case '<':
2632 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2633 continue;
2634
2635 case 'D':
2636 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2637 continue;
2638
2639 case 'B':
2640 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2641 continue;
2642
2643 case 'J':
2644 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2645 continue;
2646
2647 case 'q':
2648 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2649 continue;
2650
2651 case 'b':
2652 case 's':
2653 case 'r':
2654 case 'v':
2655 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2656 continue;
2657
2658 case 'i':
2659 case 'j':
2660 case 'o':
2661 r = (bfd_reloc_code_real_type) va_arg (args, int);
2662 assert (r == BFD_RELOC_MIPS_GPREL
2663 || r == BFD_RELOC_MIPS_LITERAL
2664 || r == BFD_RELOC_LO16
2665 || r == BFD_RELOC_MIPS_GOT16
2666 || r == BFD_RELOC_MIPS_CALL16
2667 || r == BFD_RELOC_MIPS_GOT_LO16
2668 || r == BFD_RELOC_MIPS_CALL_LO16
2669 || (ep->X_op == O_subtract
2670 && r == BFD_RELOC_PCREL_LO16));
2671 continue;
2672
2673 case 'u':
2674 r = (bfd_reloc_code_real_type) va_arg (args, int);
2675 assert (ep != NULL
2676 && (ep->X_op == O_constant
2677 || (ep->X_op == O_symbol
2678 && (r == BFD_RELOC_HI16_S
2679 || r == BFD_RELOC_HI16
2680 || r == BFD_RELOC_MIPS_GOT_HI16
2681 || r == BFD_RELOC_MIPS_CALL_HI16))
2682 || (ep->X_op == O_subtract
2683 && r == BFD_RELOC_PCREL_HI16_S)));
2684 if (ep->X_op == O_constant)
2685 {
2686 insn.insn_opcode |= (ep->X_add_number >> 16) & 0xffff;
2687 ep = NULL;
2688 r = BFD_RELOC_UNUSED;
2689 }
2690 continue;
2691
2692 case 'p':
2693 assert (ep != NULL);
2694 /*
2695 * This allows macro() to pass an immediate expression for
2696 * creating short branches without creating a symbol.
2697 * Note that the expression still might come from the assembly
2698 * input, in which case the value is not checked for range nor
2699 * is a relocation entry generated (yuck).
2700 */
2701 if (ep->X_op == O_constant)
2702 {
2703 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2704 ep = NULL;
2705 }
2706 else
2707 if (mips_pic == EMBEDDED_PIC)
2708 r = BFD_RELOC_16_PCREL_S2;
2709 else
2710 r = BFD_RELOC_16_PCREL;
2711 continue;
2712
2713 case 'a':
2714 assert (ep != NULL);
2715 r = BFD_RELOC_MIPS_JMP;
2716 continue;
2717
2718 case 'C':
2719 insn.insn_opcode |= va_arg (args, unsigned long);
2720 continue;
2721
2722 default:
2723 internalError ();
2724 }
2725 break;
2726 }
2727 va_end (args);
2728 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2729
2730 append_insn (place, &insn, ep, r, false);
2731 }
2732
2733 static void
2734 mips16_macro_build (place, counter, ep, name, fmt, args)
2735 char *place;
2736 int *counter ATTRIBUTE_UNUSED;
2737 expressionS *ep;
2738 const char *name;
2739 const char *fmt;
2740 va_list args;
2741 {
2742 struct mips_cl_insn insn;
2743 bfd_reloc_code_real_type r;
2744
2745 r = BFD_RELOC_UNUSED;
2746 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2747 assert (insn.insn_mo);
2748 assert (strcmp (name, insn.insn_mo->name) == 0);
2749
2750 while (strcmp (fmt, insn.insn_mo->args) != 0
2751 || insn.insn_mo->pinfo == INSN_MACRO)
2752 {
2753 ++insn.insn_mo;
2754 assert (insn.insn_mo->name);
2755 assert (strcmp (name, insn.insn_mo->name) == 0);
2756 }
2757
2758 insn.insn_opcode = insn.insn_mo->match;
2759 insn.use_extend = false;
2760
2761 for (;;)
2762 {
2763 int c;
2764
2765 c = *fmt++;
2766 switch (c)
2767 {
2768 case '\0':
2769 break;
2770
2771 case ',':
2772 case '(':
2773 case ')':
2774 continue;
2775
2776 case 'y':
2777 case 'w':
2778 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2779 continue;
2780
2781 case 'x':
2782 case 'v':
2783 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2784 continue;
2785
2786 case 'z':
2787 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2788 continue;
2789
2790 case 'Z':
2791 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2792 continue;
2793
2794 case '0':
2795 case 'S':
2796 case 'P':
2797 case 'R':
2798 continue;
2799
2800 case 'X':
2801 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2802 continue;
2803
2804 case 'Y':
2805 {
2806 int regno;
2807
2808 regno = va_arg (args, int);
2809 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2810 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2811 }
2812 continue;
2813
2814 case '<':
2815 case '>':
2816 case '4':
2817 case '5':
2818 case 'H':
2819 case 'W':
2820 case 'D':
2821 case 'j':
2822 case '8':
2823 case 'V':
2824 case 'C':
2825 case 'U':
2826 case 'k':
2827 case 'K':
2828 case 'p':
2829 case 'q':
2830 {
2831 assert (ep != NULL);
2832
2833 if (ep->X_op != O_constant)
2834 r = BFD_RELOC_UNUSED + c;
2835 else
2836 {
2837 mips16_immed ((char *) NULL, 0, c, ep->X_add_number, false,
2838 false, false, &insn.insn_opcode,
2839 &insn.use_extend, &insn.extend);
2840 ep = NULL;
2841 r = BFD_RELOC_UNUSED;
2842 }
2843 }
2844 continue;
2845
2846 case '6':
2847 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2848 continue;
2849 }
2850
2851 break;
2852 }
2853
2854 assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2855
2856 append_insn (place, &insn, ep, r, false);
2857 }
2858
2859 /*
2860 * Generate a "lui" instruction.
2861 */
2862 static void
2863 macro_build_lui (place, counter, ep, regnum)
2864 char *place;
2865 int *counter;
2866 expressionS *ep;
2867 int regnum;
2868 {
2869 expressionS high_expr;
2870 struct mips_cl_insn insn;
2871 bfd_reloc_code_real_type r;
2872 CONST char *name = "lui";
2873 CONST char *fmt = "t,u";
2874
2875 assert (! mips_opts.mips16);
2876
2877 if (place == NULL)
2878 high_expr = *ep;
2879 else
2880 {
2881 high_expr.X_op = O_constant;
2882 high_expr.X_add_number = ep->X_add_number;
2883 }
2884
2885 if (high_expr.X_op == O_constant)
2886 {
2887 /* we can compute the instruction now without a relocation entry */
2888 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
2889 >> 16) & 0xffff;
2890 r = BFD_RELOC_UNUSED;
2891 }
2892 else
2893 {
2894 assert (ep->X_op == O_symbol);
2895 /* _gp_disp is a special case, used from s_cpload. */
2896 assert (mips_pic == NO_PIC
2897 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
2898 r = BFD_RELOC_HI16_S;
2899 }
2900
2901 /*
2902 * If the macro is about to expand into a second instruction,
2903 * print a warning if needed. We need to pass ip as a parameter
2904 * to generate a better warning message here...
2905 */
2906 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2907 as_warn (_("Macro instruction expanded into multiple instructions"));
2908
2909 if (place == NULL)
2910 *counter += 1; /* bump instruction counter */
2911
2912 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2913 assert (insn.insn_mo);
2914 assert (strcmp (name, insn.insn_mo->name) == 0);
2915 assert (strcmp (fmt, insn.insn_mo->args) == 0);
2916
2917 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
2918 if (r == BFD_RELOC_UNUSED)
2919 {
2920 insn.insn_opcode |= high_expr.X_add_number;
2921 append_insn (place, &insn, NULL, r, false);
2922 }
2923 else
2924 append_insn (place, &insn, &high_expr, r, false);
2925 }
2926
2927 /* set_at()
2928 * Generates code to set the $at register to true (one)
2929 * if reg is less than the immediate expression.
2930 */
2931 static void
2932 set_at (counter, reg, unsignedp)
2933 int *counter;
2934 int reg;
2935 int unsignedp;
2936 {
2937 if (imm_expr.X_op == O_constant
2938 && imm_expr.X_add_number >= -0x8000
2939 && imm_expr.X_add_number < 0x8000)
2940 macro_build ((char *) NULL, counter, &imm_expr,
2941 unsignedp ? "sltiu" : "slti",
2942 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
2943 else
2944 {
2945 load_register (counter, AT, &imm_expr, 0);
2946 macro_build ((char *) NULL, counter, NULL,
2947 unsignedp ? "sltu" : "slt",
2948 "d,v,t", AT, reg, AT);
2949 }
2950 }
2951
2952 /* Warn if an expression is not a constant. */
2953
2954 static void
2955 check_absolute_expr (ip, ex)
2956 struct mips_cl_insn *ip;
2957 expressionS *ex;
2958 {
2959 if (ex->X_op == O_big)
2960 as_bad (_("unsupported large constant"));
2961 else if (ex->X_op != O_constant)
2962 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
2963 }
2964
2965 /* Count the leading zeroes by performing a binary chop. This is a
2966 bulky bit of source, but performance is a LOT better for the
2967 majority of values than a simple loop to count the bits:
2968 for (lcnt = 0; (lcnt < 32); lcnt++)
2969 if ((v) & (1 << (31 - lcnt)))
2970 break;
2971 However it is not code size friendly, and the gain will drop a bit
2972 on certain cached systems.
2973 */
2974 #define COUNT_TOP_ZEROES(v) \
2975 (((v) & ~0xffff) == 0 \
2976 ? ((v) & ~0xff) == 0 \
2977 ? ((v) & ~0xf) == 0 \
2978 ? ((v) & ~0x3) == 0 \
2979 ? ((v) & ~0x1) == 0 \
2980 ? !(v) \
2981 ? 32 \
2982 : 31 \
2983 : 30 \
2984 : ((v) & ~0x7) == 0 \
2985 ? 29 \
2986 : 28 \
2987 : ((v) & ~0x3f) == 0 \
2988 ? ((v) & ~0x1f) == 0 \
2989 ? 27 \
2990 : 26 \
2991 : ((v) & ~0x7f) == 0 \
2992 ? 25 \
2993 : 24 \
2994 : ((v) & ~0xfff) == 0 \
2995 ? ((v) & ~0x3ff) == 0 \
2996 ? ((v) & ~0x1ff) == 0 \
2997 ? 23 \
2998 : 22 \
2999 : ((v) & ~0x7ff) == 0 \
3000 ? 21 \
3001 : 20 \
3002 : ((v) & ~0x3fff) == 0 \
3003 ? ((v) & ~0x1fff) == 0 \
3004 ? 19 \
3005 : 18 \
3006 : ((v) & ~0x7fff) == 0 \
3007 ? 17 \
3008 : 16 \
3009 : ((v) & ~0xffffff) == 0 \
3010 ? ((v) & ~0xfffff) == 0 \
3011 ? ((v) & ~0x3ffff) == 0 \
3012 ? ((v) & ~0x1ffff) == 0 \
3013 ? 15 \
3014 : 14 \
3015 : ((v) & ~0x7ffff) == 0 \
3016 ? 13 \
3017 : 12 \
3018 : ((v) & ~0x3fffff) == 0 \
3019 ? ((v) & ~0x1fffff) == 0 \
3020 ? 11 \
3021 : 10 \
3022 : ((v) & ~0x7fffff) == 0 \
3023 ? 9 \
3024 : 8 \
3025 : ((v) & ~0xfffffff) == 0 \
3026 ? ((v) & ~0x3ffffff) == 0 \
3027 ? ((v) & ~0x1ffffff) == 0 \
3028 ? 7 \
3029 : 6 \
3030 : ((v) & ~0x7ffffff) == 0 \
3031 ? 5 \
3032 : 4 \
3033 : ((v) & ~0x3fffffff) == 0 \
3034 ? ((v) & ~0x1fffffff) == 0 \
3035 ? 3 \
3036 : 2 \
3037 : ((v) & ~0x7fffffff) == 0 \
3038 ? 1 \
3039 : 0)
3040
3041 /* load_register()
3042 * This routine generates the least number of instructions neccessary to load
3043 * an absolute expression value into a register.
3044 */
3045 static void
3046 load_register (counter, reg, ep, dbl)
3047 int *counter;
3048 int reg;
3049 expressionS *ep;
3050 int dbl;
3051 {
3052 int freg;
3053 expressionS hi32, lo32;
3054
3055 if (ep->X_op != O_big)
3056 {
3057 assert (ep->X_op == O_constant);
3058 if (ep->X_add_number < 0x8000
3059 && (ep->X_add_number >= 0
3060 || (ep->X_add_number >= -0x8000
3061 && (! dbl
3062 || ! ep->X_unsigned
3063 || sizeof (ep->X_add_number) > 4))))
3064 {
3065 /* We can handle 16 bit signed values with an addiu to
3066 $zero. No need to ever use daddiu here, since $zero and
3067 the result are always correct in 32 bit mode. */
3068 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3069 (int) BFD_RELOC_LO16);
3070 return;
3071 }
3072 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3073 {
3074 /* We can handle 16 bit unsigned values with an ori to
3075 $zero. */
3076 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3077 (int) BFD_RELOC_LO16);
3078 return;
3079 }
3080 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3081 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3082 == ~ (offsetT) 0x7fffffff))
3083 && (! dbl
3084 || ! ep->X_unsigned
3085 || sizeof (ep->X_add_number) > 4
3086 || (ep->X_add_number & 0x80000000) == 0))
3087 || ((HAVE_32BIT_GPRS || ! dbl)
3088 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3089 || (HAVE_32BIT_GPRS
3090 && ! dbl
3091 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3092 == ~ (offsetT) 0xffffffff)))
3093 {
3094 /* 32 bit values require an lui. */
3095 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3096 (int) BFD_RELOC_HI16);
3097 if ((ep->X_add_number & 0xffff) != 0)
3098 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3099 (int) BFD_RELOC_LO16);
3100 return;
3101 }
3102 }
3103
3104 /* The value is larger than 32 bits. */
3105
3106 if (HAVE_32BIT_GPRS)
3107 {
3108 as_bad (_("Number larger than 32 bits"));
3109 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3110 (int) BFD_RELOC_LO16);
3111 return;
3112 }
3113
3114 if (ep->X_op != O_big)
3115 {
3116 hi32 = *ep;
3117 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3118 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3119 hi32.X_add_number &= 0xffffffff;
3120 lo32 = *ep;
3121 lo32.X_add_number &= 0xffffffff;
3122 }
3123 else
3124 {
3125 assert (ep->X_add_number > 2);
3126 if (ep->X_add_number == 3)
3127 generic_bignum[3] = 0;
3128 else if (ep->X_add_number > 4)
3129 as_bad (_("Number larger than 64 bits"));
3130 lo32.X_op = O_constant;
3131 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3132 hi32.X_op = O_constant;
3133 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3134 }
3135
3136 if (hi32.X_add_number == 0)
3137 freg = 0;
3138 else
3139 {
3140 int shift, bit;
3141 unsigned long hi, lo;
3142
3143 if (hi32.X_add_number == 0xffffffff)
3144 {
3145 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3146 {
3147 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3148 reg, 0, (int) BFD_RELOC_LO16);
3149 return;
3150 }
3151 if (lo32.X_add_number & 0x80000000)
3152 {
3153 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3154 (int) BFD_RELOC_HI16);
3155 if (lo32.X_add_number & 0xffff)
3156 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3157 reg, reg, (int) BFD_RELOC_LO16);
3158 return;
3159 }
3160 }
3161
3162 /* Check for 16bit shifted constant. We know that hi32 is
3163 non-zero, so start the mask on the first bit of the hi32
3164 value. */
3165 shift = 17;
3166 do
3167 {
3168 unsigned long himask, lomask;
3169
3170 if (shift < 32)
3171 {
3172 himask = 0xffff >> (32 - shift);
3173 lomask = (0xffff << shift) & 0xffffffff;
3174 }
3175 else
3176 {
3177 himask = 0xffff << (shift - 32);
3178 lomask = 0;
3179 }
3180 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3181 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3182 {
3183 expressionS tmp;
3184
3185 tmp.X_op = O_constant;
3186 if (shift < 32)
3187 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3188 | (lo32.X_add_number >> shift));
3189 else
3190 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3191 macro_build ((char *) NULL, counter, &tmp,
3192 "ori", "t,r,i", reg, 0,
3193 (int) BFD_RELOC_LO16);
3194 macro_build ((char *) NULL, counter, NULL,
3195 (shift >= 32) ? "dsll32" : "dsll",
3196 "d,w,<", reg, reg,
3197 (shift >= 32) ? shift - 32 : shift);
3198 return;
3199 }
3200 shift++;
3201 }
3202 while (shift <= (64 - 16));
3203
3204 /* Find the bit number of the lowest one bit, and store the
3205 shifted value in hi/lo. */
3206 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3207 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3208 if (lo != 0)
3209 {
3210 bit = 0;
3211 while ((lo & 1) == 0)
3212 {
3213 lo >>= 1;
3214 ++bit;
3215 }
3216 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3217 hi >>= bit;
3218 }
3219 else
3220 {
3221 bit = 32;
3222 while ((hi & 1) == 0)
3223 {
3224 hi >>= 1;
3225 ++bit;
3226 }
3227 lo = hi;
3228 hi = 0;
3229 }
3230
3231 /* Optimize if the shifted value is a (power of 2) - 1. */
3232 if ((hi == 0 && ((lo + 1) & lo) == 0)
3233 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3234 {
3235 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3236 if (shift != 0)
3237 {
3238 expressionS tmp;
3239
3240 /* This instruction will set the register to be all
3241 ones. */
3242 tmp.X_op = O_constant;
3243 tmp.X_add_number = (offsetT) -1;
3244 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3245 reg, 0, (int) BFD_RELOC_LO16);
3246 if (bit != 0)
3247 {
3248 bit += shift;
3249 macro_build ((char *) NULL, counter, NULL,
3250 (bit >= 32) ? "dsll32" : "dsll",
3251 "d,w,<", reg, reg,
3252 (bit >= 32) ? bit - 32 : bit);
3253 }
3254 macro_build ((char *) NULL, counter, NULL,
3255 (shift >= 32) ? "dsrl32" : "dsrl",
3256 "d,w,<", reg, reg,
3257 (shift >= 32) ? shift - 32 : shift);
3258 return;
3259 }
3260 }
3261
3262 /* Sign extend hi32 before calling load_register, because we can
3263 generally get better code when we load a sign extended value. */
3264 if ((hi32.X_add_number & 0x80000000) != 0)
3265 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3266 load_register (counter, reg, &hi32, 0);
3267 freg = reg;
3268 }
3269 if ((lo32.X_add_number & 0xffff0000) == 0)
3270 {
3271 if (freg != 0)
3272 {
3273 macro_build ((char *) NULL, counter, NULL, "dsll32", "d,w,<", reg,
3274 freg, 0);
3275 freg = reg;
3276 }
3277 }
3278 else
3279 {
3280 expressionS mid16;
3281
3282 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
3283 {
3284 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3285 (int) BFD_RELOC_HI16);
3286 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3287 reg, 0);
3288 return;
3289 }
3290
3291 if (freg != 0)
3292 {
3293 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3294 freg, 16);
3295 freg = reg;
3296 }
3297 mid16 = lo32;
3298 mid16.X_add_number >>= 16;
3299 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3300 freg, (int) BFD_RELOC_LO16);
3301 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3302 reg, 16);
3303 freg = reg;
3304 }
3305 if ((lo32.X_add_number & 0xffff) != 0)
3306 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3307 (int) BFD_RELOC_LO16);
3308 }
3309
3310 /* Load an address into a register. */
3311
3312 static void
3313 load_address (counter, reg, ep)
3314 int *counter;
3315 int reg;
3316 expressionS *ep;
3317 {
3318 char *p;
3319
3320 if (ep->X_op != O_constant
3321 && ep->X_op != O_symbol)
3322 {
3323 as_bad (_("expression too complex"));
3324 ep->X_op = O_constant;
3325 }
3326
3327 if (ep->X_op == O_constant)
3328 {
3329 load_register (counter, reg, ep, 0);
3330 return;
3331 }
3332
3333 if (mips_pic == NO_PIC)
3334 {
3335 /* If this is a reference to a GP relative symbol, we want
3336 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3337 Otherwise we want
3338 lui $reg,<sym> (BFD_RELOC_HI16_S)
3339 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3340 If we have an addend, we always use the latter form. */
3341 if ((valueT) ep->X_add_number > MAX_GPREL_OFFSET
3342 || nopic_need_relax (ep->X_add_symbol, 1))
3343 p = NULL;
3344 else
3345 {
3346 frag_grow (20);
3347 macro_build ((char *) NULL, counter, ep,
3348 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3349 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3350 p = frag_var (rs_machine_dependent, 8, 0,
3351 RELAX_ENCODE (4, 8, 0, 4, 0,
3352 mips_opts.warn_about_macros),
3353 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3354 }
3355 macro_build_lui (p, counter, ep, reg);
3356 if (p != NULL)
3357 p += 4;
3358 macro_build (p, counter, ep,
3359 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3360 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3361 }
3362 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3363 {
3364 expressionS ex;
3365
3366 /* If this is a reference to an external symbol, we want
3367 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3368 Otherwise we want
3369 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3370 nop
3371 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3372 If there is a constant, it must be added in after. */
3373 ex.X_add_number = ep->X_add_number;
3374 ep->X_add_number = 0;
3375 frag_grow (20);
3376 macro_build ((char *) NULL, counter, ep,
3377 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3378 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3379 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3380 p = frag_var (rs_machine_dependent, 4, 0,
3381 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3382 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3383 macro_build (p, counter, ep,
3384 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3385 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3386 if (ex.X_add_number != 0)
3387 {
3388 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3389 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3390 ex.X_op = O_constant;
3391 macro_build ((char *) NULL, counter, &ex,
3392 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3393 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3394 }
3395 }
3396 else if (mips_pic == SVR4_PIC)
3397 {
3398 expressionS ex;
3399 int off;
3400
3401 /* This is the large GOT case. If this is a reference to an
3402 external symbol, we want
3403 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3404 addu $reg,$reg,$gp
3405 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3406 Otherwise, for a reference to a local symbol, we want
3407 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3408 nop
3409 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3410 If there is a constant, it must be added in after. */
3411 ex.X_add_number = ep->X_add_number;
3412 ep->X_add_number = 0;
3413 if (reg_needs_delay (GP))
3414 off = 4;
3415 else
3416 off = 0;
3417 frag_grow (32);
3418 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3419 (int) BFD_RELOC_MIPS_GOT_HI16);
3420 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3421 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3422 "d,v,t", reg, reg, GP);
3423 macro_build ((char *) NULL, counter, ep,
3424 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3425 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3426 p = frag_var (rs_machine_dependent, 12 + off, 0,
3427 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3428 mips_opts.warn_about_macros),
3429 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3430 if (off > 0)
3431 {
3432 /* We need a nop before loading from $gp. This special
3433 check is required because the lui which starts the main
3434 instruction stream does not refer to $gp, and so will not
3435 insert the nop which may be required. */
3436 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3437 p += 4;
3438 }
3439 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3440 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3441 p += 4;
3442 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3443 p += 4;
3444 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3445 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3446 if (ex.X_add_number != 0)
3447 {
3448 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3449 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3450 ex.X_op = O_constant;
3451 macro_build ((char *) NULL, counter, &ex,
3452 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3453 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3454 }
3455 }
3456 else if (mips_pic == EMBEDDED_PIC)
3457 {
3458 /* We always do
3459 addiu $reg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
3460 */
3461 macro_build ((char *) NULL, counter, ep,
3462 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3463 "t,r,j", reg, GP, (int) BFD_RELOC_MIPS_GPREL);
3464 }
3465 else
3466 abort ();
3467 }
3468
3469 /* Move the contents of register SOURCE into register DEST. */
3470
3471 static void
3472 move_register (counter, dest, source)
3473 int *counter;
3474 int dest;
3475 int source;
3476 {
3477 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3478 HAVE_32BIT_GPRS ? "addu" : "daddu",
3479 "d,v,t", dest, source, 0);
3480 }
3481
3482 /*
3483 * Build macros
3484 * This routine implements the seemingly endless macro or synthesized
3485 * instructions and addressing modes in the mips assembly language. Many
3486 * of these macros are simple and are similar to each other. These could
3487 * probably be handled by some kind of table or grammer aproach instead of
3488 * this verbose method. Others are not simple macros but are more like
3489 * optimizing code generation.
3490 * One interesting optimization is when several store macros appear
3491 * consecutivly that would load AT with the upper half of the same address.
3492 * The ensuing load upper instructions are ommited. This implies some kind
3493 * of global optimization. We currently only optimize within a single macro.
3494 * For many of the load and store macros if the address is specified as a
3495 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3496 * first load register 'at' with zero and use it as the base register. The
3497 * mips assembler simply uses register $zero. Just one tiny optimization
3498 * we're missing.
3499 */
3500 static void
3501 macro (ip)
3502 struct mips_cl_insn *ip;
3503 {
3504 register int treg, sreg, dreg, breg;
3505 int tempreg;
3506 int mask;
3507 int icnt = 0;
3508 int used_at = 0;
3509 expressionS expr1;
3510 const char *s;
3511 const char *s2;
3512 const char *fmt;
3513 int likely = 0;
3514 int dbl = 0;
3515 int coproc = 0;
3516 int lr = 0;
3517 int imm = 0;
3518 offsetT maxnum;
3519 int off;
3520 bfd_reloc_code_real_type r;
3521 char *p;
3522 int hold_mips_optimize;
3523
3524 assert (! mips_opts.mips16);
3525
3526 treg = (ip->insn_opcode >> 16) & 0x1f;
3527 dreg = (ip->insn_opcode >> 11) & 0x1f;
3528 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3529 mask = ip->insn_mo->mask;
3530
3531 expr1.X_op = O_constant;
3532 expr1.X_op_symbol = NULL;
3533 expr1.X_add_symbol = NULL;
3534 expr1.X_add_number = 1;
3535
3536 switch (mask)
3537 {
3538 case M_DABS:
3539 dbl = 1;
3540 case M_ABS:
3541 /* bgez $a0,.+12
3542 move v0,$a0
3543 sub v0,$zero,$a0
3544 */
3545
3546 mips_emit_delays (true);
3547 ++mips_opts.noreorder;
3548 mips_any_noreorder = 1;
3549
3550 expr1.X_add_number = 8;
3551 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3552 if (dreg == sreg)
3553 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3554 else
3555 move_register (&icnt, dreg, sreg);
3556 macro_build ((char *) NULL, &icnt, NULL,
3557 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3558
3559 --mips_opts.noreorder;
3560 return;
3561
3562 case M_ADD_I:
3563 s = "addi";
3564 s2 = "add";
3565 goto do_addi;
3566 case M_ADDU_I:
3567 s = "addiu";
3568 s2 = "addu";
3569 goto do_addi;
3570 case M_DADD_I:
3571 dbl = 1;
3572 s = "daddi";
3573 s2 = "dadd";
3574 goto do_addi;
3575 case M_DADDU_I:
3576 dbl = 1;
3577 s = "daddiu";
3578 s2 = "daddu";
3579 do_addi:
3580 if (imm_expr.X_op == O_constant
3581 && imm_expr.X_add_number >= -0x8000
3582 && imm_expr.X_add_number < 0x8000)
3583 {
3584 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3585 (int) BFD_RELOC_LO16);
3586 return;
3587 }
3588 load_register (&icnt, AT, &imm_expr, dbl);
3589 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3590 break;
3591
3592 case M_AND_I:
3593 s = "andi";
3594 s2 = "and";
3595 goto do_bit;
3596 case M_OR_I:
3597 s = "ori";
3598 s2 = "or";
3599 goto do_bit;
3600 case M_NOR_I:
3601 s = "";
3602 s2 = "nor";
3603 goto do_bit;
3604 case M_XOR_I:
3605 s = "xori";
3606 s2 = "xor";
3607 do_bit:
3608 if (imm_expr.X_op == O_constant
3609 && imm_expr.X_add_number >= 0
3610 && imm_expr.X_add_number < 0x10000)
3611 {
3612 if (mask != M_NOR_I)
3613 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3614 sreg, (int) BFD_RELOC_LO16);
3615 else
3616 {
3617 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3618 treg, sreg, (int) BFD_RELOC_LO16);
3619 macro_build ((char *) NULL, &icnt, NULL, "nor", "d,v,t",
3620 treg, treg, 0);
3621 }
3622 return;
3623 }
3624
3625 load_register (&icnt, AT, &imm_expr, 0);
3626 macro_build ((char *) NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
3627 break;
3628
3629 case M_BEQ_I:
3630 s = "beq";
3631 goto beq_i;
3632 case M_BEQL_I:
3633 s = "beql";
3634 likely = 1;
3635 goto beq_i;
3636 case M_BNE_I:
3637 s = "bne";
3638 goto beq_i;
3639 case M_BNEL_I:
3640 s = "bnel";
3641 likely = 1;
3642 beq_i:
3643 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3644 {
3645 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3646 0);
3647 return;
3648 }
3649 load_register (&icnt, AT, &imm_expr, 0);
3650 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3651 break;
3652
3653 case M_BGEL:
3654 likely = 1;
3655 case M_BGE:
3656 if (treg == 0)
3657 {
3658 macro_build ((char *) NULL, &icnt, &offset_expr,
3659 likely ? "bgezl" : "bgez",
3660 "s,p", sreg);
3661 return;
3662 }
3663 if (sreg == 0)
3664 {
3665 macro_build ((char *) NULL, &icnt, &offset_expr,
3666 likely ? "blezl" : "blez",
3667 "s,p", treg);
3668 return;
3669 }
3670 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3671 macro_build ((char *) NULL, &icnt, &offset_expr,
3672 likely ? "beql" : "beq", "s,t,p", AT, 0);
3673 break;
3674
3675 case M_BGTL_I:
3676 likely = 1;
3677 case M_BGT_I:
3678 /* check for > max integer */
3679 maxnum = 0x7fffffff;
3680 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3681 {
3682 maxnum <<= 16;
3683 maxnum |= 0xffff;
3684 maxnum <<= 16;
3685 maxnum |= 0xffff;
3686 }
3687 if (imm_expr.X_op == O_constant
3688 && imm_expr.X_add_number >= maxnum
3689 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3690 {
3691 do_false:
3692 /* result is always false */
3693 if (! likely)
3694 {
3695 if (warn_nops)
3696 as_warn (_("Branch %s is always false (nop)"),
3697 ip->insn_mo->name);
3698 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
3699 }
3700 else
3701 {
3702 if (warn_nops)
3703 as_warn (_("Branch likely %s is always false"),
3704 ip->insn_mo->name);
3705 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3706 "s,t,p", 0, 0);
3707 }
3708 return;
3709 }
3710 if (imm_expr.X_op != O_constant)
3711 as_bad (_("Unsupported large constant"));
3712 imm_expr.X_add_number++;
3713 /* FALLTHROUGH */
3714 case M_BGE_I:
3715 case M_BGEL_I:
3716 if (mask == M_BGEL_I)
3717 likely = 1;
3718 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3719 {
3720 macro_build ((char *) NULL, &icnt, &offset_expr,
3721 likely ? "bgezl" : "bgez", "s,p", sreg);
3722 return;
3723 }
3724 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3725 {
3726 macro_build ((char *) NULL, &icnt, &offset_expr,
3727 likely ? "bgtzl" : "bgtz", "s,p", sreg);
3728 return;
3729 }
3730 maxnum = 0x7fffffff;
3731 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3732 {
3733 maxnum <<= 16;
3734 maxnum |= 0xffff;
3735 maxnum <<= 16;
3736 maxnum |= 0xffff;
3737 }
3738 maxnum = - maxnum - 1;
3739 if (imm_expr.X_op == O_constant
3740 && imm_expr.X_add_number <= maxnum
3741 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3742 {
3743 do_true:
3744 /* result is always true */
3745 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3746 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3747 return;
3748 }
3749 set_at (&icnt, sreg, 0);
3750 macro_build ((char *) NULL, &icnt, &offset_expr,
3751 likely ? "beql" : "beq", "s,t,p", AT, 0);
3752 break;
3753
3754 case M_BGEUL:
3755 likely = 1;
3756 case M_BGEU:
3757 if (treg == 0)
3758 goto do_true;
3759 if (sreg == 0)
3760 {
3761 macro_build ((char *) NULL, &icnt, &offset_expr,
3762 likely ? "beql" : "beq", "s,t,p", 0, treg);
3763 return;
3764 }
3765 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3766 treg);
3767 macro_build ((char *) NULL, &icnt, &offset_expr,
3768 likely ? "beql" : "beq", "s,t,p", AT, 0);
3769 break;
3770
3771 case M_BGTUL_I:
3772 likely = 1;
3773 case M_BGTU_I:
3774 if (sreg == 0
3775 || (HAVE_32BIT_GPRS
3776 && imm_expr.X_op == O_constant
3777 && imm_expr.X_add_number == 0xffffffff))
3778 goto do_false;
3779 if (imm_expr.X_op != O_constant)
3780 as_bad (_("Unsupported large constant"));
3781 imm_expr.X_add_number++;
3782 /* FALLTHROUGH */
3783 case M_BGEU_I:
3784 case M_BGEUL_I:
3785 if (mask == M_BGEUL_I)
3786 likely = 1;
3787 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3788 goto do_true;
3789 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3790 {
3791 macro_build ((char *) NULL, &icnt, &offset_expr,
3792 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
3793 return;
3794 }
3795 set_at (&icnt, sreg, 1);
3796 macro_build ((char *) NULL, &icnt, &offset_expr,
3797 likely ? "beql" : "beq", "s,t,p", AT, 0);
3798 break;
3799
3800 case M_BGTL:
3801 likely = 1;
3802 case M_BGT:
3803 if (treg == 0)
3804 {
3805 macro_build ((char *) NULL, &icnt, &offset_expr,
3806 likely ? "bgtzl" : "bgtz", "s,p", sreg);
3807 return;
3808 }
3809 if (sreg == 0)
3810 {
3811 macro_build ((char *) NULL, &icnt, &offset_expr,
3812 likely ? "bltzl" : "bltz", "s,p", treg);
3813 return;
3814 }
3815 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3816 macro_build ((char *) NULL, &icnt, &offset_expr,
3817 likely ? "bnel" : "bne", "s,t,p", AT, 0);
3818 break;
3819
3820 case M_BGTUL:
3821 likely = 1;
3822 case M_BGTU:
3823 if (treg == 0)
3824 {
3825 macro_build ((char *) NULL, &icnt, &offset_expr,
3826 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
3827 return;
3828 }
3829 if (sreg == 0)
3830 goto do_false;
3831 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3832 sreg);
3833 macro_build ((char *) NULL, &icnt, &offset_expr,
3834 likely ? "bnel" : "bne", "s,t,p", AT, 0);
3835 break;
3836
3837 case M_BLEL:
3838 likely = 1;
3839 case M_BLE:
3840 if (treg == 0)
3841 {
3842 macro_build ((char *) NULL, &icnt, &offset_expr,
3843 likely ? "blezl" : "blez", "s,p", sreg);
3844 return;
3845 }
3846 if (sreg == 0)
3847 {
3848 macro_build ((char *) NULL, &icnt, &offset_expr,
3849 likely ? "bgezl" : "bgez", "s,p", treg);
3850 return;
3851 }
3852 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
3853 macro_build ((char *) NULL, &icnt, &offset_expr,
3854 likely ? "beql" : "beq", "s,t,p", AT, 0);
3855 break;
3856
3857 case M_BLEL_I:
3858 likely = 1;
3859 case M_BLE_I:
3860 maxnum = 0x7fffffff;
3861 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3862 {
3863 maxnum <<= 16;
3864 maxnum |= 0xffff;
3865 maxnum <<= 16;
3866 maxnum |= 0xffff;
3867 }
3868 if (imm_expr.X_op == O_constant
3869 && imm_expr.X_add_number >= maxnum
3870 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3871 goto do_true;
3872 if (imm_expr.X_op != O_constant)
3873 as_bad (_("Unsupported large constant"));
3874 imm_expr.X_add_number++;
3875 /* FALLTHROUGH */
3876 case M_BLT_I:
3877 case M_BLTL_I:
3878 if (mask == M_BLTL_I)
3879 likely = 1;
3880 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3881 {
3882 macro_build ((char *) NULL, &icnt, &offset_expr,
3883 likely ? "bltzl" : "bltz", "s,p", sreg);
3884 return;
3885 }
3886 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3887 {
3888 macro_build ((char *) NULL, &icnt, &offset_expr,
3889 likely ? "blezl" : "blez", "s,p", sreg);
3890 return;
3891 }
3892 set_at (&icnt, sreg, 0);
3893 macro_build ((char *) NULL, &icnt, &offset_expr,
3894 likely ? "bnel" : "bne", "s,t,p", AT, 0);
3895 break;
3896
3897 case M_BLEUL:
3898 likely = 1;
3899 case M_BLEU:
3900 if (treg == 0)
3901 {
3902 macro_build ((char *) NULL, &icnt, &offset_expr,
3903 likely ? "beql" : "beq", "s,t,p", sreg, 0);
3904 return;
3905 }
3906 if (sreg == 0)
3907 goto do_true;
3908 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg,
3909 sreg);
3910 macro_build ((char *) NULL, &icnt, &offset_expr,
3911 likely ? "beql" : "beq", "s,t,p", AT, 0);
3912 break;
3913
3914 case M_BLEUL_I:
3915 likely = 1;
3916 case M_BLEU_I:
3917 if (sreg == 0
3918 || (HAVE_32BIT_GPRS
3919 && imm_expr.X_op == O_constant
3920 && imm_expr.X_add_number == 0xffffffff))
3921 goto do_true;
3922 if (imm_expr.X_op != O_constant)
3923 as_bad (_("Unsupported large constant"));
3924 imm_expr.X_add_number++;
3925 /* FALLTHROUGH */
3926 case M_BLTU_I:
3927 case M_BLTUL_I:
3928 if (mask == M_BLTUL_I)
3929 likely = 1;
3930 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3931 goto do_false;
3932 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3933 {
3934 macro_build ((char *) NULL, &icnt, &offset_expr,
3935 likely ? "beql" : "beq",
3936 "s,t,p", sreg, 0);
3937 return;
3938 }
3939 set_at (&icnt, sreg, 1);
3940 macro_build ((char *) NULL, &icnt, &offset_expr,
3941 likely ? "bnel" : "bne", "s,t,p", AT, 0);
3942 break;
3943
3944 case M_BLTL:
3945 likely = 1;
3946 case M_BLT:
3947 if (treg == 0)
3948 {
3949 macro_build ((char *) NULL, &icnt, &offset_expr,
3950 likely ? "bltzl" : "bltz", "s,p", sreg);
3951 return;
3952 }
3953 if (sreg == 0)
3954 {
3955 macro_build ((char *) NULL, &icnt, &offset_expr,
3956 likely ? "bgtzl" : "bgtz", "s,p", treg);
3957 return;
3958 }
3959 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
3960 macro_build ((char *) NULL, &icnt, &offset_expr,
3961 likely ? "bnel" : "bne", "s,t,p", AT, 0);
3962 break;
3963
3964 case M_BLTUL:
3965 likely = 1;
3966 case M_BLTU:
3967 if (treg == 0)
3968 goto do_false;
3969 if (sreg == 0)
3970 {
3971 macro_build ((char *) NULL, &icnt, &offset_expr,
3972 likely ? "bnel" : "bne", "s,t,p", 0, treg);
3973 return;
3974 }
3975 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg,
3976 treg);
3977 macro_build ((char *) NULL, &icnt, &offset_expr,
3978 likely ? "bnel" : "bne", "s,t,p", AT, 0);
3979 break;
3980
3981 case M_DDIV_3:
3982 dbl = 1;
3983 case M_DIV_3:
3984 s = "mflo";
3985 goto do_div3;
3986 case M_DREM_3:
3987 dbl = 1;
3988 case M_REM_3:
3989 s = "mfhi";
3990 do_div3:
3991 if (treg == 0)
3992 {
3993 as_warn (_("Divide by zero."));
3994 if (mips_trap)
3995 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
3996 else
3997 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
3998 return;
3999 }
4000
4001 mips_emit_delays (true);
4002 ++mips_opts.noreorder;
4003 mips_any_noreorder = 1;
4004 if (mips_trap)
4005 {
4006 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4007 macro_build ((char *) NULL, &icnt, NULL,
4008 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4009 }
4010 else
4011 {
4012 expr1.X_add_number = 8;
4013 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4014 macro_build ((char *) NULL, &icnt, NULL,
4015 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4016 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4017 }
4018 expr1.X_add_number = -1;
4019 macro_build ((char *) NULL, &icnt, &expr1,
4020 dbl ? "daddiu" : "addiu",
4021 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4022 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4023 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4024 if (dbl)
4025 {
4026 expr1.X_add_number = 1;
4027 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4028 (int) BFD_RELOC_LO16);
4029 macro_build ((char *) NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT,
4030 31);
4031 }
4032 else
4033 {
4034 expr1.X_add_number = 0x80000000;
4035 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4036 (int) BFD_RELOC_HI16);
4037 }
4038 if (mips_trap)
4039 {
4040 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", sreg, AT);
4041 /* We want to close the noreorder block as soon as possible, so
4042 that later insns are available for delay slot filling. */
4043 --mips_opts.noreorder;
4044 }
4045 else
4046 {
4047 expr1.X_add_number = 8;
4048 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4049 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
4050
4051 /* We want to close the noreorder block as soon as possible, so
4052 that later insns are available for delay slot filling. */
4053 --mips_opts.noreorder;
4054
4055 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
4056 }
4057 macro_build ((char *) NULL, &icnt, NULL, s, "d", dreg);
4058 break;
4059
4060 case M_DIV_3I:
4061 s = "div";
4062 s2 = "mflo";
4063 goto do_divi;
4064 case M_DIVU_3I:
4065 s = "divu";
4066 s2 = "mflo";
4067 goto do_divi;
4068 case M_REM_3I:
4069 s = "div";
4070 s2 = "mfhi";
4071 goto do_divi;
4072 case M_REMU_3I:
4073 s = "divu";
4074 s2 = "mfhi";
4075 goto do_divi;
4076 case M_DDIV_3I:
4077 dbl = 1;
4078 s = "ddiv";
4079 s2 = "mflo";
4080 goto do_divi;
4081 case M_DDIVU_3I:
4082 dbl = 1;
4083 s = "ddivu";
4084 s2 = "mflo";
4085 goto do_divi;
4086 case M_DREM_3I:
4087 dbl = 1;
4088 s = "ddiv";
4089 s2 = "mfhi";
4090 goto do_divi;
4091 case M_DREMU_3I:
4092 dbl = 1;
4093 s = "ddivu";
4094 s2 = "mfhi";
4095 do_divi:
4096 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4097 {
4098 as_warn (_("Divide by zero."));
4099 if (mips_trap)
4100 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", 0, 0);
4101 else
4102 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4103 return;
4104 }
4105 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4106 {
4107 if (strcmp (s2, "mflo") == 0)
4108 move_register (&icnt, dreg, sreg);
4109 else
4110 move_register (&icnt, dreg, 0);
4111 return;
4112 }
4113 if (imm_expr.X_op == O_constant
4114 && imm_expr.X_add_number == -1
4115 && s[strlen (s) - 1] != 'u')
4116 {
4117 if (strcmp (s2, "mflo") == 0)
4118 {
4119 macro_build ((char *) NULL, &icnt, NULL, dbl ? "dneg" : "neg",
4120 "d,w", dreg, sreg);
4121 }
4122 else
4123 move_register (&icnt, dreg, 0);
4124 return;
4125 }
4126
4127 load_register (&icnt, AT, &imm_expr, dbl);
4128 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4129 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4130 break;
4131
4132 case M_DIVU_3:
4133 s = "divu";
4134 s2 = "mflo";
4135 goto do_divu3;
4136 case M_REMU_3:
4137 s = "divu";
4138 s2 = "mfhi";
4139 goto do_divu3;
4140 case M_DDIVU_3:
4141 s = "ddivu";
4142 s2 = "mflo";
4143 goto do_divu3;
4144 case M_DREMU_3:
4145 s = "ddivu";
4146 s2 = "mfhi";
4147 do_divu3:
4148 mips_emit_delays (true);
4149 ++mips_opts.noreorder;
4150 mips_any_noreorder = 1;
4151 if (mips_trap)
4152 {
4153 macro_build ((char *) NULL, &icnt, NULL, "teq", "s,t", treg, 0);
4154 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4155 /* We want to close the noreorder block as soon as possible, so
4156 that later insns are available for delay slot filling. */
4157 --mips_opts.noreorder;
4158 }
4159 else
4160 {
4161 expr1.X_add_number = 8;
4162 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4163 macro_build ((char *) NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
4164
4165 /* We want to close the noreorder block as soon as possible, so
4166 that later insns are available for delay slot filling. */
4167 --mips_opts.noreorder;
4168 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 7);
4169 }
4170 macro_build ((char *) NULL, &icnt, NULL, s2, "d", dreg);
4171 return;
4172
4173 case M_DLA_AB:
4174 dbl = 1;
4175 case M_LA_AB:
4176 /* Load the address of a symbol into a register. If breg is not
4177 zero, we then add a base register to it. */
4178
4179 /* When generating embedded PIC code, we permit expressions of
4180 the form
4181 la $4,foo-bar
4182 where bar is an address in the current section. These are used
4183 when getting the addresses of functions. We don't permit
4184 X_add_number to be non-zero, because if the symbol is
4185 external the relaxing code needs to know that any addend is
4186 purely the offset to X_op_symbol. */
4187 if (mips_pic == EMBEDDED_PIC
4188 && offset_expr.X_op == O_subtract
4189 && (symbol_constant_p (offset_expr.X_op_symbol)
4190 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4191 : (symbol_equated_p (offset_expr.X_op_symbol)
4192 && (S_GET_SEGMENT
4193 (symbol_get_value_expression (offset_expr.X_op_symbol)
4194 ->X_add_symbol)
4195 == now_seg)))
4196 && breg == 0
4197 && (offset_expr.X_add_number == 0
4198 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4199 {
4200 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4201 treg, (int) BFD_RELOC_PCREL_HI16_S);
4202 macro_build ((char *) NULL, &icnt, &offset_expr,
4203 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4204 "t,r,j", treg, treg, (int) BFD_RELOC_PCREL_LO16);
4205 return;
4206 }
4207
4208 if (offset_expr.X_op != O_symbol
4209 && offset_expr.X_op != O_constant)
4210 {
4211 as_bad (_("expression too complex"));
4212 offset_expr.X_op = O_constant;
4213 }
4214
4215 if (treg == breg)
4216 {
4217 tempreg = AT;
4218 used_at = 1;
4219 }
4220 else
4221 {
4222 tempreg = treg;
4223 used_at = 0;
4224 }
4225
4226 if (offset_expr.X_op == O_constant)
4227 load_register (&icnt, tempreg, &offset_expr, dbl);
4228 else if (mips_pic == NO_PIC)
4229 {
4230 /* If this is a reference to an GP relative symbol, we want
4231 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4232 Otherwise we want
4233 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4234 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4235 If we have a constant, we need two instructions anyhow,
4236 so we may as well always use the latter form. */
4237 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
4238 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4239 p = NULL;
4240 else
4241 {
4242 frag_grow (20);
4243 macro_build ((char *) NULL, &icnt, &offset_expr,
4244 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4245 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4246 p = frag_var (rs_machine_dependent, 8, 0,
4247 RELAX_ENCODE (4, 8, 0, 4, 0,
4248 mips_opts.warn_about_macros),
4249 offset_expr.X_add_symbol, (offsetT) 0,
4250 (char *) NULL);
4251 }
4252 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4253 if (p != NULL)
4254 p += 4;
4255 macro_build (p, &icnt, &offset_expr,
4256 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4257 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4258 }
4259 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4260 {
4261 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4262
4263 /* If this is a reference to an external symbol, and there
4264 is no constant, we want
4265 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4266 or if tempreg is PIC_CALL_REG
4267 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4268 For a local symbol, we want
4269 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4270 nop
4271 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4272
4273 If we have a small constant, and this is a reference to
4274 an external symbol, we want
4275 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4276 nop
4277 addiu $tempreg,$tempreg,<constant>
4278 For a local symbol, we want the same instruction
4279 sequence, but we output a BFD_RELOC_LO16 reloc on the
4280 addiu instruction.
4281
4282 If we have a large constant, and this is a reference to
4283 an external symbol, we want
4284 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4285 lui $at,<hiconstant>
4286 addiu $at,$at,<loconstant>
4287 addu $tempreg,$tempreg,$at
4288 For a local symbol, we want the same instruction
4289 sequence, but we output a BFD_RELOC_LO16 reloc on the
4290 addiu instruction. */
4291 expr1.X_add_number = offset_expr.X_add_number;
4292 offset_expr.X_add_number = 0;
4293 frag_grow (32);
4294 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4295 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4296 macro_build ((char *) NULL, &icnt, &offset_expr,
4297 dbl ? "ld" : "lw",
4298 "t,o(b)", tempreg, lw_reloc_type, GP);
4299 if (expr1.X_add_number == 0)
4300 {
4301 int off;
4302
4303 if (breg == 0)
4304 off = 0;
4305 else
4306 {
4307 /* We're going to put in an addu instruction using
4308 tempreg, so we may as well insert the nop right
4309 now. */
4310 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4311 "nop", "");
4312 off = 4;
4313 }
4314 p = frag_var (rs_machine_dependent, 8 - off, 0,
4315 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4316 (breg == 0
4317 ? mips_opts.warn_about_macros
4318 : 0)),
4319 offset_expr.X_add_symbol, (offsetT) 0,
4320 (char *) NULL);
4321 if (breg == 0)
4322 {
4323 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4324 p += 4;
4325 }
4326 macro_build (p, &icnt, &expr1,
4327 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4328 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4329 /* FIXME: If breg == 0, and the next instruction uses
4330 $tempreg, then if this variant case is used an extra
4331 nop will be generated. */
4332 }
4333 else if (expr1.X_add_number >= -0x8000
4334 && expr1.X_add_number < 0x8000)
4335 {
4336 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4337 "nop", "");
4338 macro_build ((char *) NULL, &icnt, &expr1,
4339 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4340 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4341 (void) frag_var (rs_machine_dependent, 0, 0,
4342 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4343 offset_expr.X_add_symbol, (offsetT) 0,
4344 (char *) NULL);
4345 }
4346 else
4347 {
4348 int off1;
4349
4350 /* If we are going to add in a base register, and the
4351 target register and the base register are the same,
4352 then we are using AT as a temporary register. Since
4353 we want to load the constant into AT, we add our
4354 current AT (from the global offset table) and the
4355 register into the register now, and pretend we were
4356 not using a base register. */
4357 if (breg != treg)
4358 off1 = 0;
4359 else
4360 {
4361 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4362 "nop", "");
4363 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4364 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4365 "d,v,t", treg, AT, breg);
4366 breg = 0;
4367 tempreg = treg;
4368 off1 = -8;
4369 }
4370
4371 /* Set mips_optimize around the lui instruction to avoid
4372 inserting an unnecessary nop after the lw. */
4373 hold_mips_optimize = mips_optimize;
4374 mips_optimize = 2;
4375 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4376 mips_optimize = hold_mips_optimize;
4377
4378 macro_build ((char *) NULL, &icnt, &expr1,
4379 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4380 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4381 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4382 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4383 "d,v,t", tempreg, tempreg, AT);
4384 (void) frag_var (rs_machine_dependent, 0, 0,
4385 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4386 offset_expr.X_add_symbol, (offsetT) 0,
4387 (char *) NULL);
4388 used_at = 1;
4389 }
4390 }
4391 else if (mips_pic == SVR4_PIC)
4392 {
4393 int gpdel;
4394 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4395 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4396
4397 /* This is the large GOT case. If this is a reference to an
4398 external symbol, and there is no constant, we want
4399 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4400 addu $tempreg,$tempreg,$gp
4401 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4402 or if tempreg is PIC_CALL_REG
4403 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4404 addu $tempreg,$tempreg,$gp
4405 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4406 For a local symbol, we want
4407 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4408 nop
4409 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4410
4411 If we have a small constant, and this is a reference to
4412 an external symbol, we want
4413 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4414 addu $tempreg,$tempreg,$gp
4415 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4416 nop
4417 addiu $tempreg,$tempreg,<constant>
4418 For a local symbol, we want
4419 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4420 nop
4421 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4422
4423 If we have a large constant, and this is a reference to
4424 an external symbol, we want
4425 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4426 addu $tempreg,$tempreg,$gp
4427 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4428 lui $at,<hiconstant>
4429 addiu $at,$at,<loconstant>
4430 addu $tempreg,$tempreg,$at
4431 For a local symbol, we want
4432 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4433 lui $at,<hiconstant>
4434 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4435 addu $tempreg,$tempreg,$at
4436 */
4437 expr1.X_add_number = offset_expr.X_add_number;
4438 offset_expr.X_add_number = 0;
4439 frag_grow (52);
4440 if (reg_needs_delay (GP))
4441 gpdel = 4;
4442 else
4443 gpdel = 0;
4444 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4445 {
4446 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4447 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4448 }
4449 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4450 tempreg, lui_reloc_type);
4451 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4452 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4453 "d,v,t", tempreg, tempreg, GP);
4454 macro_build ((char *) NULL, &icnt, &offset_expr,
4455 dbl ? "ld" : "lw",
4456 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4457 if (expr1.X_add_number == 0)
4458 {
4459 int off;
4460
4461 if (breg == 0)
4462 off = 0;
4463 else
4464 {
4465 /* We're going to put in an addu instruction using
4466 tempreg, so we may as well insert the nop right
4467 now. */
4468 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4469 "nop", "");
4470 off = 4;
4471 }
4472
4473 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4474 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4475 8 + gpdel, 0,
4476 (breg == 0
4477 ? mips_opts.warn_about_macros
4478 : 0)),
4479 offset_expr.X_add_symbol, (offsetT) 0,
4480 (char *) NULL);
4481 }
4482 else if (expr1.X_add_number >= -0x8000
4483 && expr1.X_add_number < 0x8000)
4484 {
4485 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4486 "nop", "");
4487 macro_build ((char *) NULL, &icnt, &expr1,
4488 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4489 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4490
4491 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4492 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4493 (breg == 0
4494 ? mips_opts.warn_about_macros
4495 : 0)),
4496 offset_expr.X_add_symbol, (offsetT) 0,
4497 (char *) NULL);
4498 }
4499 else
4500 {
4501 int adj, dreg;
4502
4503 /* If we are going to add in a base register, and the
4504 target register and the base register are the same,
4505 then we are using AT as a temporary register. Since
4506 we want to load the constant into AT, we add our
4507 current AT (from the global offset table) and the
4508 register into the register now, and pretend we were
4509 not using a base register. */
4510 if (breg != treg)
4511 {
4512 adj = 0;
4513 dreg = tempreg;
4514 }
4515 else
4516 {
4517 assert (tempreg == AT);
4518 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4519 "nop", "");
4520 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4521 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4522 "d,v,t", treg, AT, breg);
4523 dreg = treg;
4524 adj = 8;
4525 }
4526
4527 /* Set mips_optimize around the lui instruction to avoid
4528 inserting an unnecessary nop after the lw. */
4529 hold_mips_optimize = mips_optimize;
4530 mips_optimize = 2;
4531 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
4532 mips_optimize = hold_mips_optimize;
4533
4534 macro_build ((char *) NULL, &icnt, &expr1,
4535 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4536 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4537 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4538 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4539 "d,v,t", dreg, dreg, AT);
4540
4541 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4542 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4543 8 + gpdel, 0,
4544 (breg == 0
4545 ? mips_opts.warn_about_macros
4546 : 0)),
4547 offset_expr.X_add_symbol, (offsetT) 0,
4548 (char *) NULL);
4549
4550 used_at = 1;
4551 }
4552
4553 if (gpdel > 0)
4554 {
4555 /* This is needed because this instruction uses $gp, but
4556 the first instruction on the main stream does not. */
4557 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4558 p += 4;
4559 }
4560 macro_build (p, &icnt, &offset_expr,
4561 dbl ? "ld" : "lw",
4562 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4563 p += 4;
4564 if (expr1.X_add_number >= -0x8000
4565 && expr1.X_add_number < 0x8000)
4566 {
4567 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4568 p += 4;
4569 macro_build (p, &icnt, &expr1,
4570 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4571 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4572 /* FIXME: If add_number is 0, and there was no base
4573 register, the external symbol case ended with a load,
4574 so if the symbol turns out to not be external, and
4575 the next instruction uses tempreg, an unnecessary nop
4576 will be inserted. */
4577 }
4578 else
4579 {
4580 if (breg == treg)
4581 {
4582 /* We must add in the base register now, as in the
4583 external symbol case. */
4584 assert (tempreg == AT);
4585 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4586 p += 4;
4587 macro_build (p, &icnt, (expressionS *) NULL,
4588 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4589 "d,v,t", treg, AT, breg);
4590 p += 4;
4591 tempreg = treg;
4592 /* We set breg to 0 because we have arranged to add
4593 it in in both cases. */
4594 breg = 0;
4595 }
4596
4597 macro_build_lui (p, &icnt, &expr1, AT);
4598 p += 4;
4599 macro_build (p, &icnt, &expr1,
4600 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4601 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4602 p += 4;
4603 macro_build (p, &icnt, (expressionS *) NULL,
4604 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4605 "d,v,t", tempreg, tempreg, AT);
4606 p += 4;
4607 }
4608 }
4609 else if (mips_pic == EMBEDDED_PIC)
4610 {
4611 /* We use
4612 addiu $tempreg,$gp,<sym> (BFD_RELOC_MIPS_GPREL)
4613 */
4614 macro_build ((char *) NULL, &icnt, &offset_expr,
4615 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4616 "t,r,j", tempreg, GP, (int) BFD_RELOC_MIPS_GPREL);
4617 }
4618 else
4619 abort ();
4620
4621 if (breg != 0)
4622 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4623 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4624 "d,v,t", treg, tempreg, breg);
4625
4626 if (! used_at)
4627 return;
4628
4629 break;
4630
4631 case M_J_A:
4632 /* The j instruction may not be used in PIC code, since it
4633 requires an absolute address. We convert it to a b
4634 instruction. */
4635 if (mips_pic == NO_PIC)
4636 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
4637 else
4638 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4639 return;
4640
4641 /* The jal instructions must be handled as macros because when
4642 generating PIC code they expand to multi-instruction
4643 sequences. Normally they are simple instructions. */
4644 case M_JAL_1:
4645 dreg = RA;
4646 /* Fall through. */
4647 case M_JAL_2:
4648 if (mips_pic == NO_PIC
4649 || mips_pic == EMBEDDED_PIC)
4650 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4651 "d,s", dreg, sreg);
4652 else if (mips_pic == SVR4_PIC)
4653 {
4654 if (sreg != PIC_CALL_REG)
4655 as_warn (_("MIPS PIC call to register other than $25"));
4656
4657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
4658 "d,s", dreg, sreg);
4659 if (mips_cprestore_offset < 0)
4660 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4661 else
4662 {
4663 expr1.X_add_number = mips_cprestore_offset;
4664 macro_build ((char *) NULL, &icnt, &expr1,
4665 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4666 "t,o(b)", GP, (int) BFD_RELOC_LO16, mips_frame_reg);
4667 }
4668 }
4669 else
4670 abort ();
4671
4672 return;
4673
4674 case M_JAL_A:
4675 if (mips_pic == NO_PIC)
4676 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
4677 else if (mips_pic == SVR4_PIC)
4678 {
4679 /* If this is a reference to an external symbol, and we are
4680 using a small GOT, we want
4681 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4682 nop
4683 jalr $25
4684 nop
4685 lw $gp,cprestore($sp)
4686 The cprestore value is set using the .cprestore
4687 pseudo-op. If we are using a big GOT, we want
4688 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4689 addu $25,$25,$gp
4690 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
4691 nop
4692 jalr $25
4693 nop
4694 lw $gp,cprestore($sp)
4695 If the symbol is not external, we want
4696 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4697 nop
4698 addiu $25,$25,<sym> (BFD_RELOC_LO16)
4699 jalr $25
4700 nop
4701 lw $gp,cprestore($sp) */
4702 frag_grow (40);
4703 if (! mips_big_got)
4704 {
4705 macro_build ((char *) NULL, &icnt, &offset_expr,
4706 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4707 "t,o(b)", PIC_CALL_REG,
4708 (int) BFD_RELOC_MIPS_CALL16, GP);
4709 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4710 "nop", "");
4711 p = frag_var (rs_machine_dependent, 4, 0,
4712 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
4713 offset_expr.X_add_symbol, (offsetT) 0,
4714 (char *) NULL);
4715 }
4716 else
4717 {
4718 int gpdel;
4719
4720 if (reg_needs_delay (GP))
4721 gpdel = 4;
4722 else
4723 gpdel = 0;
4724 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4725 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
4726 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4727 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4728 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
4729 macro_build ((char *) NULL, &icnt, &offset_expr,
4730 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4731 "t,o(b)", PIC_CALL_REG,
4732 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
4733 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4734 "nop", "");
4735 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4736 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
4737 0, 0),
4738 offset_expr.X_add_symbol, (offsetT) 0,
4739 (char *) NULL);
4740 if (gpdel > 0)
4741 {
4742 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4743 p += 4;
4744 }
4745 macro_build (p, &icnt, &offset_expr,
4746 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4747 "t,o(b)", PIC_CALL_REG,
4748 (int) BFD_RELOC_MIPS_GOT16, GP);
4749 p += 4;
4750 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4751 p += 4;
4752 }
4753 macro_build (p, &icnt, &offset_expr,
4754 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4755 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
4756 (int) BFD_RELOC_LO16);
4757 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4758 "jalr", "s", PIC_CALL_REG);
4759 if (mips_cprestore_offset < 0)
4760 as_warn (_("No .cprestore pseudo-op used in PIC code"));
4761 else
4762 {
4763 if (mips_opts.noreorder)
4764 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4765 "nop", "");
4766 expr1.X_add_number = mips_cprestore_offset;
4767 macro_build ((char *) NULL, &icnt, &expr1,
4768 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4769 "t,o(b)", GP, (int) BFD_RELOC_LO16,
4770 mips_frame_reg);
4771 }
4772 }
4773 else if (mips_pic == EMBEDDED_PIC)
4774 {
4775 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
4776 /* The linker may expand the call to a longer sequence which
4777 uses $at, so we must break rather than return. */
4778 break;
4779 }
4780 else
4781 abort ();
4782
4783 return;
4784
4785 case M_LB_AB:
4786 s = "lb";
4787 goto ld;
4788 case M_LBU_AB:
4789 s = "lbu";
4790 goto ld;
4791 case M_LH_AB:
4792 s = "lh";
4793 goto ld;
4794 case M_LHU_AB:
4795 s = "lhu";
4796 goto ld;
4797 case M_LW_AB:
4798 s = "lw";
4799 goto ld;
4800 case M_LWC0_AB:
4801 s = "lwc0";
4802 /* Itbl support may require additional care here. */
4803 coproc = 1;
4804 goto ld;
4805 case M_LWC1_AB:
4806 s = "lwc1";
4807 /* Itbl support may require additional care here. */
4808 coproc = 1;
4809 goto ld;
4810 case M_LWC2_AB:
4811 s = "lwc2";
4812 /* Itbl support may require additional care here. */
4813 coproc = 1;
4814 goto ld;
4815 case M_LWC3_AB:
4816 s = "lwc3";
4817 /* Itbl support may require additional care here. */
4818 coproc = 1;
4819 goto ld;
4820 case M_LWL_AB:
4821 s = "lwl";
4822 lr = 1;
4823 goto ld;
4824 case M_LWR_AB:
4825 s = "lwr";
4826 lr = 1;
4827 goto ld;
4828 case M_LDC1_AB:
4829 if (mips_arch == CPU_R4650)
4830 {
4831 as_bad (_("opcode not supported on this processor"));
4832 return;
4833 }
4834 s = "ldc1";
4835 /* Itbl support may require additional care here. */
4836 coproc = 1;
4837 goto ld;
4838 case M_LDC2_AB:
4839 s = "ldc2";
4840 /* Itbl support may require additional care here. */
4841 coproc = 1;
4842 goto ld;
4843 case M_LDC3_AB:
4844 s = "ldc3";
4845 /* Itbl support may require additional care here. */
4846 coproc = 1;
4847 goto ld;
4848 case M_LDL_AB:
4849 s = "ldl";
4850 lr = 1;
4851 goto ld;
4852 case M_LDR_AB:
4853 s = "ldr";
4854 lr = 1;
4855 goto ld;
4856 case M_LL_AB:
4857 s = "ll";
4858 goto ld;
4859 case M_LLD_AB:
4860 s = "lld";
4861 goto ld;
4862 case M_LWU_AB:
4863 s = "lwu";
4864 ld:
4865 if (breg == treg || coproc || lr)
4866 {
4867 tempreg = AT;
4868 used_at = 1;
4869 }
4870 else
4871 {
4872 tempreg = treg;
4873 used_at = 0;
4874 }
4875 goto ld_st;
4876 case M_SB_AB:
4877 s = "sb";
4878 goto st;
4879 case M_SH_AB:
4880 s = "sh";
4881 goto st;
4882 case M_SW_AB:
4883 s = "sw";
4884 goto st;
4885 case M_SWC0_AB:
4886 s = "swc0";
4887 /* Itbl support may require additional care here. */
4888 coproc = 1;
4889 goto st;
4890 case M_SWC1_AB:
4891 s = "swc1";
4892 /* Itbl support may require additional care here. */
4893 coproc = 1;
4894 goto st;
4895 case M_SWC2_AB:
4896 s = "swc2";
4897 /* Itbl support may require additional care here. */
4898 coproc = 1;
4899 goto st;
4900 case M_SWC3_AB:
4901 s = "swc3";
4902 /* Itbl support may require additional care here. */
4903 coproc = 1;
4904 goto st;
4905 case M_SWL_AB:
4906 s = "swl";
4907 goto st;
4908 case M_SWR_AB:
4909 s = "swr";
4910 goto st;
4911 case M_SC_AB:
4912 s = "sc";
4913 goto st;
4914 case M_SCD_AB:
4915 s = "scd";
4916 goto st;
4917 case M_SDC1_AB:
4918 if (mips_arch == CPU_R4650)
4919 {
4920 as_bad (_("opcode not supported on this processor"));
4921 return;
4922 }
4923 s = "sdc1";
4924 coproc = 1;
4925 /* Itbl support may require additional care here. */
4926 goto st;
4927 case M_SDC2_AB:
4928 s = "sdc2";
4929 /* Itbl support may require additional care here. */
4930 coproc = 1;
4931 goto st;
4932 case M_SDC3_AB:
4933 s = "sdc3";
4934 /* Itbl support may require additional care here. */
4935 coproc = 1;
4936 goto st;
4937 case M_SDL_AB:
4938 s = "sdl";
4939 goto st;
4940 case M_SDR_AB:
4941 s = "sdr";
4942 st:
4943 tempreg = AT;
4944 used_at = 1;
4945 ld_st:
4946 /* Itbl support may require additional care here. */
4947 if (mask == M_LWC1_AB
4948 || mask == M_SWC1_AB
4949 || mask == M_LDC1_AB
4950 || mask == M_SDC1_AB
4951 || mask == M_L_DAB
4952 || mask == M_S_DAB)
4953 fmt = "T,o(b)";
4954 else if (coproc)
4955 fmt = "E,o(b)";
4956 else
4957 fmt = "t,o(b)";
4958
4959 if (offset_expr.X_op != O_constant
4960 && offset_expr.X_op != O_symbol)
4961 {
4962 as_bad (_("expression too complex"));
4963 offset_expr.X_op = O_constant;
4964 }
4965
4966 /* A constant expression in PIC code can be handled just as it
4967 is in non PIC code. */
4968 if (mips_pic == NO_PIC
4969 || offset_expr.X_op == O_constant)
4970 {
4971 /* If this is a reference to a GP relative symbol, and there
4972 is no base register, we want
4973 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
4974 Otherwise, if there is no base register, we want
4975 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4976 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
4977 If we have a constant, we need two instructions anyhow,
4978 so we always use the latter form.
4979
4980 If we have a base register, and this is a reference to a
4981 GP relative symbol, we want
4982 addu $tempreg,$breg,$gp
4983 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
4984 Otherwise we want
4985 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4986 addu $tempreg,$tempreg,$breg
4987 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
4988 With a constant we always use the latter case. */
4989 if (breg == 0)
4990 {
4991 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
4992 || nopic_need_relax (offset_expr.X_add_symbol, 1))
4993 p = NULL;
4994 else
4995 {
4996 frag_grow (20);
4997 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
4998 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
4999 p = frag_var (rs_machine_dependent, 8, 0,
5000 RELAX_ENCODE (4, 8, 0, 4, 0,
5001 (mips_opts.warn_about_macros
5002 || (used_at
5003 && mips_opts.noat))),
5004 offset_expr.X_add_symbol, (offsetT) 0,
5005 (char *) NULL);
5006 used_at = 0;
5007 }
5008 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5009 if (p != NULL)
5010 p += 4;
5011 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5012 (int) BFD_RELOC_LO16, tempreg);
5013 }
5014 else
5015 {
5016 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5017 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5018 p = NULL;
5019 else
5020 {
5021 frag_grow (28);
5022 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5023 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5024 "d,v,t", tempreg, breg, GP);
5025 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5026 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5027 p = frag_var (rs_machine_dependent, 12, 0,
5028 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5029 offset_expr.X_add_symbol, (offsetT) 0,
5030 (char *) NULL);
5031 }
5032 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5033 if (p != NULL)
5034 p += 4;
5035 macro_build (p, &icnt, (expressionS *) NULL,
5036 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5037 "d,v,t", tempreg, tempreg, breg);
5038 if (p != NULL)
5039 p += 4;
5040 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5041 (int) BFD_RELOC_LO16, tempreg);
5042 }
5043 }
5044 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5045 {
5046 /* If this is a reference to an external symbol, we want
5047 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5048 nop
5049 <op> $treg,0($tempreg)
5050 Otherwise we want
5051 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5052 nop
5053 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5054 <op> $treg,0($tempreg)
5055 If there is a base register, we add it to $tempreg before
5056 the <op>. If there is a constant, we stick it in the
5057 <op> instruction. We don't handle constants larger than
5058 16 bits, because we have no way to load the upper 16 bits
5059 (actually, we could handle them for the subset of cases
5060 in which we are not using $at). */
5061 assert (offset_expr.X_op == O_symbol);
5062 expr1.X_add_number = offset_expr.X_add_number;
5063 offset_expr.X_add_number = 0;
5064 if (expr1.X_add_number < -0x8000
5065 || expr1.X_add_number >= 0x8000)
5066 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5067 frag_grow (20);
5068 macro_build ((char *) NULL, &icnt, &offset_expr,
5069 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5070 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5071 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5072 p = frag_var (rs_machine_dependent, 4, 0,
5073 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5074 offset_expr.X_add_symbol, (offsetT) 0,
5075 (char *) NULL);
5076 macro_build (p, &icnt, &offset_expr,
5077 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5078 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5079 if (breg != 0)
5080 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5081 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5082 "d,v,t", tempreg, tempreg, breg);
5083 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5084 (int) BFD_RELOC_LO16, tempreg);
5085 }
5086 else if (mips_pic == SVR4_PIC)
5087 {
5088 int gpdel;
5089
5090 /* If this is a reference to an external symbol, we want
5091 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5092 addu $tempreg,$tempreg,$gp
5093 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5094 <op> $treg,0($tempreg)
5095 Otherwise we want
5096 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5097 nop
5098 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5099 <op> $treg,0($tempreg)
5100 If there is a base register, we add it to $tempreg before
5101 the <op>. If there is a constant, we stick it in the
5102 <op> instruction. We don't handle constants larger than
5103 16 bits, because we have no way to load the upper 16 bits
5104 (actually, we could handle them for the subset of cases
5105 in which we are not using $at). */
5106 assert (offset_expr.X_op == O_symbol);
5107 expr1.X_add_number = offset_expr.X_add_number;
5108 offset_expr.X_add_number = 0;
5109 if (expr1.X_add_number < -0x8000
5110 || expr1.X_add_number >= 0x8000)
5111 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5112 if (reg_needs_delay (GP))
5113 gpdel = 4;
5114 else
5115 gpdel = 0;
5116 frag_grow (36);
5117 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5118 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5119 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5120 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5121 "d,v,t", tempreg, tempreg, GP);
5122 macro_build ((char *) NULL, &icnt, &offset_expr,
5123 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5124 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5125 tempreg);
5126 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5127 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5128 offset_expr.X_add_symbol, (offsetT) 0, (char *) NULL);
5129 if (gpdel > 0)
5130 {
5131 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5132 p += 4;
5133 }
5134 macro_build (p, &icnt, &offset_expr,
5135 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5136 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5137 p += 4;
5138 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5139 p += 4;
5140 macro_build (p, &icnt, &offset_expr,
5141 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5142 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5143 if (breg != 0)
5144 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5145 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5146 "d,v,t", tempreg, tempreg, breg);
5147 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5148 (int) BFD_RELOC_LO16, tempreg);
5149 }
5150 else if (mips_pic == EMBEDDED_PIC)
5151 {
5152 /* If there is no base register, we want
5153 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5154 If there is a base register, we want
5155 addu $tempreg,$breg,$gp
5156 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GPREL)
5157 */
5158 assert (offset_expr.X_op == O_symbol);
5159 if (breg == 0)
5160 {
5161 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5162 treg, (int) BFD_RELOC_MIPS_GPREL, GP);
5163 used_at = 0;
5164 }
5165 else
5166 {
5167 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5168 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5169 "d,v,t", tempreg, breg, GP);
5170 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5171 treg, (int) BFD_RELOC_MIPS_GPREL, tempreg);
5172 }
5173 }
5174 else
5175 abort ();
5176
5177 if (! used_at)
5178 return;
5179
5180 break;
5181
5182 case M_LI:
5183 case M_LI_S:
5184 load_register (&icnt, treg, &imm_expr, 0);
5185 return;
5186
5187 case M_DLI:
5188 load_register (&icnt, treg, &imm_expr, 1);
5189 return;
5190
5191 case M_LI_SS:
5192 if (imm_expr.X_op == O_constant)
5193 {
5194 load_register (&icnt, AT, &imm_expr, 0);
5195 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5196 "mtc1", "t,G", AT, treg);
5197 break;
5198 }
5199 else
5200 {
5201 assert (offset_expr.X_op == O_symbol
5202 && strcmp (segment_name (S_GET_SEGMENT
5203 (offset_expr.X_add_symbol)),
5204 ".lit4") == 0
5205 && offset_expr.X_add_number == 0);
5206 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5207 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5208 return;
5209 }
5210
5211 case M_LI_D:
5212 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5213 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5214 order 32 bits of the value and the low order 32 bits are either
5215 zero or in OFFSET_EXPR. */
5216 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5217 {
5218 if (HAVE_64BIT_GPRS)
5219 load_register (&icnt, treg, &imm_expr, 1);
5220 else
5221 {
5222 int hreg, lreg;
5223
5224 if (target_big_endian)
5225 {
5226 hreg = treg;
5227 lreg = treg + 1;
5228 }
5229 else
5230 {
5231 hreg = treg + 1;
5232 lreg = treg;
5233 }
5234
5235 if (hreg <= 31)
5236 load_register (&icnt, hreg, &imm_expr, 0);
5237 if (lreg <= 31)
5238 {
5239 if (offset_expr.X_op == O_absent)
5240 move_register (&icnt, lreg, 0);
5241 else
5242 {
5243 assert (offset_expr.X_op == O_constant);
5244 load_register (&icnt, lreg, &offset_expr, 0);
5245 }
5246 }
5247 }
5248 return;
5249 }
5250
5251 /* We know that sym is in the .rdata section. First we get the
5252 upper 16 bits of the address. */
5253 if (mips_pic == NO_PIC)
5254 {
5255 /* FIXME: This won't work for a 64 bit address. */
5256 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5257 }
5258 else if (mips_pic == SVR4_PIC)
5259 {
5260 macro_build ((char *) NULL, &icnt, &offset_expr,
5261 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5262 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5263 }
5264 else if (mips_pic == EMBEDDED_PIC)
5265 {
5266 /* For embedded PIC we pick up the entire address off $gp in
5267 a single instruction. */
5268 macro_build ((char *) NULL, &icnt, &offset_expr,
5269 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5270 "t,r,j", AT, GP, (int) BFD_RELOC_MIPS_GPREL);
5271 offset_expr.X_op = O_constant;
5272 offset_expr.X_add_number = 0;
5273 }
5274 else
5275 abort ();
5276
5277 /* Now we load the register(s). */
5278 if (HAVE_64BIT_GPRS)
5279 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5280 treg, (int) BFD_RELOC_LO16, AT);
5281 else
5282 {
5283 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5284 treg, (int) BFD_RELOC_LO16, AT);
5285 if (treg != 31)
5286 {
5287 /* FIXME: How in the world do we deal with the possible
5288 overflow here? */
5289 offset_expr.X_add_number += 4;
5290 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5291 treg + 1, (int) BFD_RELOC_LO16, AT);
5292 }
5293 }
5294
5295 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5296 does not become a variant frag. */
5297 frag_wane (frag_now);
5298 frag_new (0);
5299
5300 break;
5301
5302 case M_LI_DD:
5303 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5304 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5305 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5306 the value and the low order 32 bits are either zero or in
5307 OFFSET_EXPR. */
5308 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5309 {
5310 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5311 if (HAVE_64BIT_FPRS)
5312 {
5313 assert (HAVE_64BIT_GPRS);
5314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5315 "dmtc1", "t,S", AT, treg);
5316 }
5317 else
5318 {
5319 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5320 "mtc1", "t,G", AT, treg + 1);
5321 if (offset_expr.X_op == O_absent)
5322 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5323 "mtc1", "t,G", 0, treg);
5324 else
5325 {
5326 assert (offset_expr.X_op == O_constant);
5327 load_register (&icnt, AT, &offset_expr, 0);
5328 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5329 "mtc1", "t,G", AT, treg);
5330 }
5331 }
5332 break;
5333 }
5334
5335 assert (offset_expr.X_op == O_symbol
5336 && offset_expr.X_add_number == 0);
5337 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5338 if (strcmp (s, ".lit8") == 0)
5339 {
5340 if (mips_opts.isa != ISA_MIPS1)
5341 {
5342 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5343 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5344 return;
5345 }
5346 breg = GP;
5347 r = BFD_RELOC_MIPS_LITERAL;
5348 goto dob;
5349 }
5350 else
5351 {
5352 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5353 if (mips_pic == SVR4_PIC)
5354 macro_build ((char *) NULL, &icnt, &offset_expr,
5355 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5356 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5357 else
5358 {
5359 /* FIXME: This won't work for a 64 bit address. */
5360 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5361 }
5362
5363 if (mips_opts.isa != ISA_MIPS1)
5364 {
5365 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5366 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5367
5368 /* To avoid confusion in tc_gen_reloc, we must ensure
5369 that this does not become a variant frag. */
5370 frag_wane (frag_now);
5371 frag_new (0);
5372
5373 break;
5374 }
5375 breg = AT;
5376 r = BFD_RELOC_LO16;
5377 goto dob;
5378 }
5379
5380 case M_L_DOB:
5381 if (mips_arch == CPU_R4650)
5382 {
5383 as_bad (_("opcode not supported on this processor"));
5384 return;
5385 }
5386 /* Even on a big endian machine $fn comes before $fn+1. We have
5387 to adjust when loading from memory. */
5388 r = BFD_RELOC_LO16;
5389 dob:
5390 assert (mips_opts.isa == ISA_MIPS1);
5391 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5392 target_big_endian ? treg + 1 : treg,
5393 (int) r, breg);
5394 /* FIXME: A possible overflow which I don't know how to deal
5395 with. */
5396 offset_expr.X_add_number += 4;
5397 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5398 target_big_endian ? treg : treg + 1,
5399 (int) r, breg);
5400
5401 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5402 does not become a variant frag. */
5403 frag_wane (frag_now);
5404 frag_new (0);
5405
5406 if (breg != AT)
5407 return;
5408 break;
5409
5410 case M_L_DAB:
5411 /*
5412 * The MIPS assembler seems to check for X_add_number not
5413 * being double aligned and generating:
5414 * lui at,%hi(foo+1)
5415 * addu at,at,v1
5416 * addiu at,at,%lo(foo+1)
5417 * lwc1 f2,0(at)
5418 * lwc1 f3,4(at)
5419 * But, the resulting address is the same after relocation so why
5420 * generate the extra instruction?
5421 */
5422 if (mips_arch == CPU_R4650)
5423 {
5424 as_bad (_("opcode not supported on this processor"));
5425 return;
5426 }
5427 /* Itbl support may require additional care here. */
5428 coproc = 1;
5429 if (mips_opts.isa != ISA_MIPS1)
5430 {
5431 s = "ldc1";
5432 goto ld;
5433 }
5434
5435 s = "lwc1";
5436 fmt = "T,o(b)";
5437 goto ldd_std;
5438
5439 case M_S_DAB:
5440 if (mips_arch == CPU_R4650)
5441 {
5442 as_bad (_("opcode not supported on this processor"));
5443 return;
5444 }
5445
5446 if (mips_opts.isa != ISA_MIPS1)
5447 {
5448 s = "sdc1";
5449 goto st;
5450 }
5451
5452 s = "swc1";
5453 fmt = "T,o(b)";
5454 /* Itbl support may require additional care here. */
5455 coproc = 1;
5456 goto ldd_std;
5457
5458 case M_LD_AB:
5459 if (HAVE_64BIT_GPRS)
5460 {
5461 s = "ld";
5462 goto ld;
5463 }
5464
5465 s = "lw";
5466 fmt = "t,o(b)";
5467 goto ldd_std;
5468
5469 case M_SD_AB:
5470 if (HAVE_64BIT_GPRS)
5471 {
5472 s = "sd";
5473 goto st;
5474 }
5475
5476 s = "sw";
5477 fmt = "t,o(b)";
5478
5479 ldd_std:
5480 if (offset_expr.X_op != O_symbol
5481 && offset_expr.X_op != O_constant)
5482 {
5483 as_bad (_("expression too complex"));
5484 offset_expr.X_op = O_constant;
5485 }
5486
5487 /* Even on a big endian machine $fn comes before $fn+1. We have
5488 to adjust when loading from memory. We set coproc if we must
5489 load $fn+1 first. */
5490 /* Itbl support may require additional care here. */
5491 if (! target_big_endian)
5492 coproc = 0;
5493
5494 if (mips_pic == NO_PIC
5495 || offset_expr.X_op == O_constant)
5496 {
5497 /* If this is a reference to a GP relative symbol, we want
5498 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5499 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5500 If we have a base register, we use this
5501 addu $at,$breg,$gp
5502 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5503 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5504 If this is not a GP relative symbol, we want
5505 lui $at,<sym> (BFD_RELOC_HI16_S)
5506 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5507 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5508 If there is a base register, we add it to $at after the
5509 lui instruction. If there is a constant, we always use
5510 the last case. */
5511 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5512 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5513 {
5514 p = NULL;
5515 used_at = 1;
5516 }
5517 else
5518 {
5519 int off;
5520
5521 if (breg == 0)
5522 {
5523 frag_grow (28);
5524 tempreg = GP;
5525 off = 0;
5526 used_at = 0;
5527 }
5528 else
5529 {
5530 frag_grow (36);
5531 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5532 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5533 "d,v,t", AT, breg, GP);
5534 tempreg = AT;
5535 off = 4;
5536 used_at = 1;
5537 }
5538
5539 /* Itbl support may require additional care here. */
5540 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5541 coproc ? treg + 1 : treg,
5542 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5543 offset_expr.X_add_number += 4;
5544
5545 /* Set mips_optimize to 2 to avoid inserting an
5546 undesired nop. */
5547 hold_mips_optimize = mips_optimize;
5548 mips_optimize = 2;
5549 /* Itbl support may require additional care here. */
5550 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5551 coproc ? treg : treg + 1,
5552 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5553 mips_optimize = hold_mips_optimize;
5554
5555 p = frag_var (rs_machine_dependent, 12 + off, 0,
5556 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
5557 used_at && mips_opts.noat),
5558 offset_expr.X_add_symbol, (offsetT) 0,
5559 (char *) NULL);
5560
5561 /* We just generated two relocs. When tc_gen_reloc
5562 handles this case, it will skip the first reloc and
5563 handle the second. The second reloc already has an
5564 extra addend of 4, which we added above. We must
5565 subtract it out, and then subtract another 4 to make
5566 the first reloc come out right. The second reloc
5567 will come out right because we are going to add 4 to
5568 offset_expr when we build its instruction below.
5569
5570 If we have a symbol, then we don't want to include
5571 the offset, because it will wind up being included
5572 when we generate the reloc. */
5573
5574 if (offset_expr.X_op == O_constant)
5575 offset_expr.X_add_number -= 8;
5576 else
5577 {
5578 offset_expr.X_add_number = -4;
5579 offset_expr.X_op = O_constant;
5580 }
5581 }
5582 macro_build_lui (p, &icnt, &offset_expr, AT);
5583 if (p != NULL)
5584 p += 4;
5585 if (breg != 0)
5586 {
5587 macro_build (p, &icnt, (expressionS *) NULL,
5588 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5589 "d,v,t", AT, breg, AT);
5590 if (p != NULL)
5591 p += 4;
5592 }
5593 /* Itbl support may require additional care here. */
5594 macro_build (p, &icnt, &offset_expr, s, fmt,
5595 coproc ? treg + 1 : treg,
5596 (int) BFD_RELOC_LO16, AT);
5597 if (p != NULL)
5598 p += 4;
5599 /* FIXME: How do we handle overflow here? */
5600 offset_expr.X_add_number += 4;
5601 /* Itbl support may require additional care here. */
5602 macro_build (p, &icnt, &offset_expr, s, fmt,
5603 coproc ? treg : treg + 1,
5604 (int) BFD_RELOC_LO16, AT);
5605 }
5606 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5607 {
5608 int off;
5609
5610 /* If this is a reference to an external symbol, we want
5611 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5612 nop
5613 <op> $treg,0($at)
5614 <op> $treg+1,4($at)
5615 Otherwise we want
5616 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5617 nop
5618 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5619 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5620 If there is a base register we add it to $at before the
5621 lwc1 instructions. If there is a constant we include it
5622 in the lwc1 instructions. */
5623 used_at = 1;
5624 expr1.X_add_number = offset_expr.X_add_number;
5625 offset_expr.X_add_number = 0;
5626 if (expr1.X_add_number < -0x8000
5627 || expr1.X_add_number >= 0x8000 - 4)
5628 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5629 if (breg == 0)
5630 off = 0;
5631 else
5632 off = 4;
5633 frag_grow (24 + off);
5634 macro_build ((char *) NULL, &icnt, &offset_expr,
5635 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5636 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5637 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5638 if (breg != 0)
5639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5640 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5641 "d,v,t", AT, breg, AT);
5642 /* Itbl support may require additional care here. */
5643 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5644 coproc ? treg + 1 : treg,
5645 (int) BFD_RELOC_LO16, AT);
5646 expr1.X_add_number += 4;
5647
5648 /* Set mips_optimize to 2 to avoid inserting an undesired
5649 nop. */
5650 hold_mips_optimize = mips_optimize;
5651 mips_optimize = 2;
5652 /* Itbl support may require additional care here. */
5653 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5654 coproc ? treg : treg + 1,
5655 (int) BFD_RELOC_LO16, AT);
5656 mips_optimize = hold_mips_optimize;
5657
5658 (void) frag_var (rs_machine_dependent, 0, 0,
5659 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
5660 offset_expr.X_add_symbol, (offsetT) 0,
5661 (char *) NULL);
5662 }
5663 else if (mips_pic == SVR4_PIC)
5664 {
5665 int gpdel, off;
5666
5667 /* If this is a reference to an external symbol, we want
5668 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5669 addu $at,$at,$gp
5670 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
5671 nop
5672 <op> $treg,0($at)
5673 <op> $treg+1,4($at)
5674 Otherwise we want
5675 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5676 nop
5677 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
5678 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
5679 If there is a base register we add it to $at before the
5680 lwc1 instructions. If there is a constant we include it
5681 in the lwc1 instructions. */
5682 used_at = 1;
5683 expr1.X_add_number = offset_expr.X_add_number;
5684 offset_expr.X_add_number = 0;
5685 if (expr1.X_add_number < -0x8000
5686 || expr1.X_add_number >= 0x8000 - 4)
5687 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5688 if (reg_needs_delay (GP))
5689 gpdel = 4;
5690 else
5691 gpdel = 0;
5692 if (breg == 0)
5693 off = 0;
5694 else
5695 off = 4;
5696 frag_grow (56);
5697 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5698 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
5699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5700 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5701 "d,v,t", AT, AT, GP);
5702 macro_build ((char *) NULL, &icnt, &offset_expr,
5703 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5704 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
5705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5706 if (breg != 0)
5707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5708 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5709 "d,v,t", AT, breg, AT);
5710 /* Itbl support may require additional care here. */
5711 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5712 coproc ? treg + 1 : treg,
5713 (int) BFD_RELOC_LO16, AT);
5714 expr1.X_add_number += 4;
5715
5716 /* Set mips_optimize to 2 to avoid inserting an undesired
5717 nop. */
5718 hold_mips_optimize = mips_optimize;
5719 mips_optimize = 2;
5720 /* Itbl support may require additional care here. */
5721 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
5722 coproc ? treg : treg + 1,
5723 (int) BFD_RELOC_LO16, AT);
5724 mips_optimize = hold_mips_optimize;
5725 expr1.X_add_number -= 4;
5726
5727 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
5728 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
5729 8 + gpdel + off, 1, 0),
5730 offset_expr.X_add_symbol, (offsetT) 0,
5731 (char *) NULL);
5732 if (gpdel > 0)
5733 {
5734 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5735 p += 4;
5736 }
5737 macro_build (p, &icnt, &offset_expr,
5738 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5739 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5740 p += 4;
5741 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5742 p += 4;
5743 if (breg != 0)
5744 {
5745 macro_build (p, &icnt, (expressionS *) NULL,
5746 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5747 "d,v,t", AT, breg, AT);
5748 p += 4;
5749 }
5750 /* Itbl support may require additional care here. */
5751 macro_build (p, &icnt, &expr1, s, fmt,
5752 coproc ? treg + 1 : treg,
5753 (int) BFD_RELOC_LO16, AT);
5754 p += 4;
5755 expr1.X_add_number += 4;
5756
5757 /* Set mips_optimize to 2 to avoid inserting an undesired
5758 nop. */
5759 hold_mips_optimize = mips_optimize;
5760 mips_optimize = 2;
5761 /* Itbl support may require additional care here. */
5762 macro_build (p, &icnt, &expr1, s, fmt,
5763 coproc ? treg : treg + 1,
5764 (int) BFD_RELOC_LO16, AT);
5765 mips_optimize = hold_mips_optimize;
5766 }
5767 else if (mips_pic == EMBEDDED_PIC)
5768 {
5769 /* If there is no base register, we use
5770 <op> $treg,<sym>($gp) (BFD_RELOC_MIPS_GPREL)
5771 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_MIPS_GPREL)
5772 If we have a base register, we use
5773 addu $at,$breg,$gp
5774 <op> $treg,<sym>($at) (BFD_RELOC_MIPS_GPREL)
5775 <op> $treg+1,<sym>+4($at) (BFD_RELOC_MIPS_GPREL)
5776 */
5777 if (breg == 0)
5778 {
5779 tempreg = GP;
5780 used_at = 0;
5781 }
5782 else
5783 {
5784 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5785 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5786 "d,v,t", AT, breg, GP);
5787 tempreg = AT;
5788 used_at = 1;
5789 }
5790
5791 /* Itbl support may require additional care here. */
5792 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5793 coproc ? treg + 1 : treg,
5794 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5795 offset_expr.X_add_number += 4;
5796 /* Itbl support may require additional care here. */
5797 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5798 coproc ? treg : treg + 1,
5799 (int) BFD_RELOC_MIPS_GPREL, tempreg);
5800 }
5801 else
5802 abort ();
5803
5804 if (! used_at)
5805 return;
5806
5807 break;
5808
5809 case M_LD_OB:
5810 s = "lw";
5811 goto sd_ob;
5812 case M_SD_OB:
5813 s = "sw";
5814 sd_ob:
5815 assert (HAVE_32BIT_ADDRESSES);
5816 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
5817 (int) BFD_RELOC_LO16, breg);
5818 offset_expr.X_add_number += 4;
5819 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
5820 (int) BFD_RELOC_LO16, breg);
5821 return;
5822
5823 /* New code added to support COPZ instructions.
5824 This code builds table entries out of the macros in mip_opcodes.
5825 R4000 uses interlocks to handle coproc delays.
5826 Other chips (like the R3000) require nops to be inserted for delays.
5827
5828 FIXME: Currently, we require that the user handle delays.
5829 In order to fill delay slots for non-interlocked chips,
5830 we must have a way to specify delays based on the coprocessor.
5831 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
5832 What are the side-effects of the cop instruction?
5833 What cache support might we have and what are its effects?
5834 Both coprocessor & memory require delays. how long???
5835 What registers are read/set/modified?
5836
5837 If an itbl is provided to interpret cop instructions,
5838 this knowledge can be encoded in the itbl spec. */
5839
5840 case M_COP0:
5841 s = "c0";
5842 goto copz;
5843 case M_COP1:
5844 s = "c1";
5845 goto copz;
5846 case M_COP2:
5847 s = "c2";
5848 goto copz;
5849 case M_COP3:
5850 s = "c3";
5851 copz:
5852 /* For now we just do C (same as Cz). The parameter will be
5853 stored in insn_opcode by mips_ip. */
5854 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
5855 ip->insn_opcode);
5856 return;
5857
5858 case M_MOVE:
5859 move_register (&icnt, dreg, sreg);
5860 return;
5861
5862 #ifdef LOSING_COMPILER
5863 default:
5864 /* Try and see if this is a new itbl instruction.
5865 This code builds table entries out of the macros in mip_opcodes.
5866 FIXME: For now we just assemble the expression and pass it's
5867 value along as a 32-bit immediate.
5868 We may want to have the assembler assemble this value,
5869 so that we gain the assembler's knowledge of delay slots,
5870 symbols, etc.
5871 Would it be more efficient to use mask (id) here? */
5872 if (itbl_have_entries
5873 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
5874 {
5875 s = ip->insn_mo->name;
5876 s2 = "cop3";
5877 coproc = ITBL_DECODE_PNUM (immed_expr);;
5878 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
5879 return;
5880 }
5881 macro2 (ip);
5882 return;
5883 }
5884 if (mips_opts.noat)
5885 as_warn (_("Macro used $at after \".set noat\""));
5886 }
5887
5888 static void
5889 macro2 (ip)
5890 struct mips_cl_insn *ip;
5891 {
5892 register int treg, sreg, dreg, breg;
5893 int tempreg;
5894 int mask;
5895 int icnt = 0;
5896 int used_at;
5897 expressionS expr1;
5898 const char *s;
5899 const char *s2;
5900 const char *fmt;
5901 int likely = 0;
5902 int dbl = 0;
5903 int coproc = 0;
5904 int lr = 0;
5905 int imm = 0;
5906 int off;
5907 offsetT maxnum;
5908 bfd_reloc_code_real_type r;
5909 char *p;
5910
5911 treg = (ip->insn_opcode >> 16) & 0x1f;
5912 dreg = (ip->insn_opcode >> 11) & 0x1f;
5913 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
5914 mask = ip->insn_mo->mask;
5915
5916 expr1.X_op = O_constant;
5917 expr1.X_op_symbol = NULL;
5918 expr1.X_add_symbol = NULL;
5919 expr1.X_add_number = 1;
5920
5921 switch (mask)
5922 {
5923 #endif /* LOSING_COMPILER */
5924
5925 case M_DMUL:
5926 dbl = 1;
5927 case M_MUL:
5928 macro_build ((char *) NULL, &icnt, NULL,
5929 dbl ? "dmultu" : "multu",
5930 "s,t", sreg, treg);
5931 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5932 return;
5933
5934 case M_DMUL_I:
5935 dbl = 1;
5936 case M_MUL_I:
5937 /* The MIPS assembler some times generates shifts and adds. I'm
5938 not trying to be that fancy. GCC should do this for us
5939 anyway. */
5940 load_register (&icnt, AT, &imm_expr, dbl);
5941 macro_build ((char *) NULL, &icnt, NULL,
5942 dbl ? "dmult" : "mult", "s,t", sreg, AT);
5943 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5944 break;
5945
5946 case M_DMULO_I:
5947 dbl = 1;
5948 case M_MULO_I:
5949 imm = 1;
5950 goto do_mulo;
5951
5952 case M_DMULO:
5953 dbl = 1;
5954 case M_MULO:
5955 do_mulo:
5956 mips_emit_delays (true);
5957 ++mips_opts.noreorder;
5958 mips_any_noreorder = 1;
5959 if (imm)
5960 load_register (&icnt, AT, &imm_expr, dbl);
5961 macro_build ((char *) NULL, &icnt, NULL,
5962 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
5963 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5964 macro_build ((char *) NULL, &icnt, NULL,
5965 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
5966 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5967 if (mips_trap)
5968 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", dreg, AT);
5969 else
5970 {
5971 expr1.X_add_number = 8;
5972 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
5973 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
5974 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
5975 }
5976 --mips_opts.noreorder;
5977 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
5978 break;
5979
5980 case M_DMULOU_I:
5981 dbl = 1;
5982 case M_MULOU_I:
5983 imm = 1;
5984 goto do_mulou;
5985
5986 case M_DMULOU:
5987 dbl = 1;
5988 case M_MULOU:
5989 do_mulou:
5990 mips_emit_delays (true);
5991 ++mips_opts.noreorder;
5992 mips_any_noreorder = 1;
5993 if (imm)
5994 load_register (&icnt, AT, &imm_expr, dbl);
5995 macro_build ((char *) NULL, &icnt, NULL,
5996 dbl ? "dmultu" : "multu",
5997 "s,t", sreg, imm ? AT : treg);
5998 macro_build ((char *) NULL, &icnt, NULL, "mfhi", "d", AT);
5999 macro_build ((char *) NULL, &icnt, NULL, "mflo", "d", dreg);
6000 if (mips_trap)
6001 macro_build ((char *) NULL, &icnt, NULL, "tne", "s,t", AT, 0);
6002 else
6003 {
6004 expr1.X_add_number = 8;
6005 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6006 macro_build ((char *) NULL, &icnt, NULL, "nop", "", 0);
6007 macro_build ((char *) NULL, &icnt, NULL, "break", "c", 6);
6008 }
6009 --mips_opts.noreorder;
6010 break;
6011
6012 case M_ROL:
6013 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6014 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
6015 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg,
6016 treg);
6017 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6018 break;
6019
6020 case M_ROL_I:
6021 if (imm_expr.X_op != O_constant)
6022 as_bad (_("rotate count too large"));
6023 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg,
6024 (int) (imm_expr.X_add_number & 0x1f));
6025 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
6026 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6027 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6028 break;
6029
6030 case M_ROR:
6031 macro_build ((char *) NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
6032 macro_build ((char *) NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
6033 macro_build ((char *) NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg,
6034 treg);
6035 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6036 break;
6037
6038 case M_ROR_I:
6039 if (imm_expr.X_op != O_constant)
6040 as_bad (_("rotate count too large"));
6041 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg,
6042 (int) (imm_expr.X_add_number & 0x1f));
6043 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
6044 (int) ((0 - imm_expr.X_add_number) & 0x1f));
6045 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
6046 break;
6047
6048 case M_S_DOB:
6049 if (mips_arch == CPU_R4650)
6050 {
6051 as_bad (_("opcode not supported on this processor"));
6052 return;
6053 }
6054 assert (mips_opts.isa == ISA_MIPS1);
6055 /* Even on a big endian machine $fn comes before $fn+1. We have
6056 to adjust when storing to memory. */
6057 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6058 target_big_endian ? treg + 1 : treg,
6059 (int) BFD_RELOC_LO16, breg);
6060 offset_expr.X_add_number += 4;
6061 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6062 target_big_endian ? treg : treg + 1,
6063 (int) BFD_RELOC_LO16, breg);
6064 return;
6065
6066 case M_SEQ:
6067 if (sreg == 0)
6068 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6069 treg, (int) BFD_RELOC_LO16);
6070 else if (treg == 0)
6071 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6072 sreg, (int) BFD_RELOC_LO16);
6073 else
6074 {
6075 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6076 sreg, treg);
6077 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6078 dreg, (int) BFD_RELOC_LO16);
6079 }
6080 return;
6081
6082 case M_SEQ_I:
6083 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6084 {
6085 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6086 sreg, (int) BFD_RELOC_LO16);
6087 return;
6088 }
6089 if (sreg == 0)
6090 {
6091 as_warn (_("Instruction %s: result is always false"),
6092 ip->insn_mo->name);
6093 move_register (&icnt, dreg, 0);
6094 return;
6095 }
6096 if (imm_expr.X_op == O_constant
6097 && imm_expr.X_add_number >= 0
6098 && imm_expr.X_add_number < 0x10000)
6099 {
6100 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6101 sreg, (int) BFD_RELOC_LO16);
6102 used_at = 0;
6103 }
6104 else if (imm_expr.X_op == O_constant
6105 && imm_expr.X_add_number > -0x8000
6106 && imm_expr.X_add_number < 0)
6107 {
6108 imm_expr.X_add_number = -imm_expr.X_add_number;
6109 macro_build ((char *) NULL, &icnt, &imm_expr,
6110 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6111 "t,r,j", dreg, sreg,
6112 (int) BFD_RELOC_LO16);
6113 used_at = 0;
6114 }
6115 else
6116 {
6117 load_register (&icnt, AT, &imm_expr, 0);
6118 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6119 sreg, AT);
6120 used_at = 1;
6121 }
6122 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6123 (int) BFD_RELOC_LO16);
6124 if (used_at)
6125 break;
6126 return;
6127
6128 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6129 s = "slt";
6130 goto sge;
6131 case M_SGEU:
6132 s = "sltu";
6133 sge:
6134 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
6135 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6136 (int) BFD_RELOC_LO16);
6137 return;
6138
6139 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6140 case M_SGEU_I:
6141 if (imm_expr.X_op == O_constant
6142 && imm_expr.X_add_number >= -0x8000
6143 && imm_expr.X_add_number < 0x8000)
6144 {
6145 macro_build ((char *) NULL, &icnt, &imm_expr,
6146 mask == M_SGE_I ? "slti" : "sltiu",
6147 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6148 used_at = 0;
6149 }
6150 else
6151 {
6152 load_register (&icnt, AT, &imm_expr, 0);
6153 macro_build ((char *) NULL, &icnt, NULL,
6154 mask == M_SGE_I ? "slt" : "sltu",
6155 "d,v,t", dreg, sreg, AT);
6156 used_at = 1;
6157 }
6158 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6159 (int) BFD_RELOC_LO16);
6160 if (used_at)
6161 break;
6162 return;
6163
6164 case M_SGT: /* sreg > treg <==> treg < sreg */
6165 s = "slt";
6166 goto sgt;
6167 case M_SGTU:
6168 s = "sltu";
6169 sgt:
6170 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6171 return;
6172
6173 case M_SGT_I: /* sreg > I <==> I < sreg */
6174 s = "slt";
6175 goto sgti;
6176 case M_SGTU_I:
6177 s = "sltu";
6178 sgti:
6179 load_register (&icnt, AT, &imm_expr, 0);
6180 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6181 break;
6182
6183 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6184 s = "slt";
6185 goto sle;
6186 case M_SLEU:
6187 s = "sltu";
6188 sle:
6189 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
6190 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6191 (int) BFD_RELOC_LO16);
6192 return;
6193
6194 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6195 s = "slt";
6196 goto slei;
6197 case M_SLEU_I:
6198 s = "sltu";
6199 slei:
6200 load_register (&icnt, AT, &imm_expr, 0);
6201 macro_build ((char *) NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
6202 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6203 (int) BFD_RELOC_LO16);
6204 break;
6205
6206 case M_SLT_I:
6207 if (imm_expr.X_op == O_constant
6208 && imm_expr.X_add_number >= -0x8000
6209 && imm_expr.X_add_number < 0x8000)
6210 {
6211 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6212 dreg, sreg, (int) BFD_RELOC_LO16);
6213 return;
6214 }
6215 load_register (&icnt, AT, &imm_expr, 0);
6216 macro_build ((char *) NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
6217 break;
6218
6219 case M_SLTU_I:
6220 if (imm_expr.X_op == O_constant
6221 && imm_expr.X_add_number >= -0x8000
6222 && imm_expr.X_add_number < 0x8000)
6223 {
6224 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6225 dreg, sreg, (int) BFD_RELOC_LO16);
6226 return;
6227 }
6228 load_register (&icnt, AT, &imm_expr, 0);
6229 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg,
6230 AT);
6231 break;
6232
6233 case M_SNE:
6234 if (sreg == 0)
6235 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6236 treg);
6237 else if (treg == 0)
6238 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6239 sreg);
6240 else
6241 {
6242 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6243 sreg, treg);
6244 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6245 dreg);
6246 }
6247 return;
6248
6249 case M_SNE_I:
6250 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6251 {
6252 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0,
6253 sreg);
6254 return;
6255 }
6256 if (sreg == 0)
6257 {
6258 as_warn (_("Instruction %s: result is always true"),
6259 ip->insn_mo->name);
6260 macro_build ((char *) NULL, &icnt, &expr1,
6261 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6262 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6263 return;
6264 }
6265 if (imm_expr.X_op == O_constant
6266 && imm_expr.X_add_number >= 0
6267 && imm_expr.X_add_number < 0x10000)
6268 {
6269 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6270 dreg, sreg, (int) BFD_RELOC_LO16);
6271 used_at = 0;
6272 }
6273 else if (imm_expr.X_op == O_constant
6274 && imm_expr.X_add_number > -0x8000
6275 && imm_expr.X_add_number < 0)
6276 {
6277 imm_expr.X_add_number = -imm_expr.X_add_number;
6278 macro_build ((char *) NULL, &icnt, &imm_expr,
6279 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6280 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6281 used_at = 0;
6282 }
6283 else
6284 {
6285 load_register (&icnt, AT, &imm_expr, 0);
6286 macro_build ((char *) NULL, &icnt, NULL, "xor", "d,v,t", dreg,
6287 sreg, AT);
6288 used_at = 1;
6289 }
6290 macro_build ((char *) NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
6291 if (used_at)
6292 break;
6293 return;
6294
6295 case M_DSUB_I:
6296 dbl = 1;
6297 case M_SUB_I:
6298 if (imm_expr.X_op == O_constant
6299 && imm_expr.X_add_number > -0x8000
6300 && imm_expr.X_add_number <= 0x8000)
6301 {
6302 imm_expr.X_add_number = -imm_expr.X_add_number;
6303 macro_build ((char *) NULL, &icnt, &imm_expr,
6304 dbl ? "daddi" : "addi",
6305 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6306 return;
6307 }
6308 load_register (&icnt, AT, &imm_expr, dbl);
6309 macro_build ((char *) NULL, &icnt, NULL,
6310 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6311 break;
6312
6313 case M_DSUBU_I:
6314 dbl = 1;
6315 case M_SUBU_I:
6316 if (imm_expr.X_op == O_constant
6317 && imm_expr.X_add_number > -0x8000
6318 && imm_expr.X_add_number <= 0x8000)
6319 {
6320 imm_expr.X_add_number = -imm_expr.X_add_number;
6321 macro_build ((char *) NULL, &icnt, &imm_expr,
6322 dbl ? "daddiu" : "addiu",
6323 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6324 return;
6325 }
6326 load_register (&icnt, AT, &imm_expr, dbl);
6327 macro_build ((char *) NULL, &icnt, NULL,
6328 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6329 break;
6330
6331 case M_TEQ_I:
6332 s = "teq";
6333 goto trap;
6334 case M_TGE_I:
6335 s = "tge";
6336 goto trap;
6337 case M_TGEU_I:
6338 s = "tgeu";
6339 goto trap;
6340 case M_TLT_I:
6341 s = "tlt";
6342 goto trap;
6343 case M_TLTU_I:
6344 s = "tltu";
6345 goto trap;
6346 case M_TNE_I:
6347 s = "tne";
6348 trap:
6349 load_register (&icnt, AT, &imm_expr, 0);
6350 macro_build ((char *) NULL, &icnt, NULL, s, "s,t", sreg, AT);
6351 break;
6352
6353 case M_TRUNCWS:
6354 case M_TRUNCWD:
6355 assert (mips_opts.isa == ISA_MIPS1);
6356 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6357 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6358
6359 /*
6360 * Is the double cfc1 instruction a bug in the mips assembler;
6361 * or is there a reason for it?
6362 */
6363 mips_emit_delays (true);
6364 ++mips_opts.noreorder;
6365 mips_any_noreorder = 1;
6366 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6367 macro_build ((char *) NULL, &icnt, NULL, "cfc1", "t,G", treg, 31);
6368 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6369 expr1.X_add_number = 3;
6370 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6371 (int) BFD_RELOC_LO16);
6372 expr1.X_add_number = 2;
6373 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6374 (int) BFD_RELOC_LO16);
6375 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", AT, 31);
6376 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6377 macro_build ((char *) NULL, &icnt, NULL,
6378 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6379 macro_build ((char *) NULL, &icnt, NULL, "ctc1", "t,G", treg, 31);
6380 macro_build ((char *) NULL, &icnt, NULL, "nop", "");
6381 --mips_opts.noreorder;
6382 break;
6383
6384 case M_ULH:
6385 s = "lb";
6386 goto ulh;
6387 case M_ULHU:
6388 s = "lbu";
6389 ulh:
6390 if (offset_expr.X_add_number >= 0x7fff)
6391 as_bad (_("operand overflow"));
6392 /* avoid load delay */
6393 if (! target_big_endian)
6394 offset_expr.X_add_number += 1;
6395 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6396 (int) BFD_RELOC_LO16, breg);
6397 if (! target_big_endian)
6398 offset_expr.X_add_number -= 1;
6399 else
6400 offset_expr.X_add_number += 1;
6401 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6402 (int) BFD_RELOC_LO16, breg);
6403 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
6404 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
6405 break;
6406
6407 case M_ULD:
6408 s = "ldl";
6409 s2 = "ldr";
6410 off = 7;
6411 goto ulw;
6412 case M_ULW:
6413 s = "lwl";
6414 s2 = "lwr";
6415 off = 3;
6416 ulw:
6417 if (offset_expr.X_add_number >= 0x8000 - off)
6418 as_bad (_("operand overflow"));
6419 if (! target_big_endian)
6420 offset_expr.X_add_number += off;
6421 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6422 (int) BFD_RELOC_LO16, breg);
6423 if (! target_big_endian)
6424 offset_expr.X_add_number -= off;
6425 else
6426 offset_expr.X_add_number += off;
6427 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6428 (int) BFD_RELOC_LO16, breg);
6429 return;
6430
6431 case M_ULD_A:
6432 s = "ldl";
6433 s2 = "ldr";
6434 off = 7;
6435 goto ulwa;
6436 case M_ULW_A:
6437 s = "lwl";
6438 s2 = "lwr";
6439 off = 3;
6440 ulwa:
6441 load_address (&icnt, AT, &offset_expr);
6442 if (breg != 0)
6443 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6444 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6445 "d,v,t", AT, AT, breg);
6446 if (! target_big_endian)
6447 expr1.X_add_number = off;
6448 else
6449 expr1.X_add_number = 0;
6450 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6451 (int) BFD_RELOC_LO16, AT);
6452 if (! target_big_endian)
6453 expr1.X_add_number = 0;
6454 else
6455 expr1.X_add_number = off;
6456 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6457 (int) BFD_RELOC_LO16, AT);
6458 break;
6459
6460 case M_ULH_A:
6461 case M_ULHU_A:
6462 load_address (&icnt, AT, &offset_expr);
6463 if (breg != 0)
6464 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6465 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6466 "d,v,t", AT, AT, breg);
6467 if (target_big_endian)
6468 expr1.X_add_number = 0;
6469 macro_build ((char *) NULL, &icnt, &expr1,
6470 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
6471 (int) BFD_RELOC_LO16, AT);
6472 if (target_big_endian)
6473 expr1.X_add_number = 1;
6474 else
6475 expr1.X_add_number = 0;
6476 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6477 (int) BFD_RELOC_LO16, AT);
6478 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6479 treg, 8);
6480 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6481 treg, AT);
6482 break;
6483
6484 case M_USH:
6485 if (offset_expr.X_add_number >= 0x7fff)
6486 as_bad (_("operand overflow"));
6487 if (target_big_endian)
6488 offset_expr.X_add_number += 1;
6489 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
6490 (int) BFD_RELOC_LO16, breg);
6491 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
6492 if (target_big_endian)
6493 offset_expr.X_add_number -= 1;
6494 else
6495 offset_expr.X_add_number += 1;
6496 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
6497 (int) BFD_RELOC_LO16, breg);
6498 break;
6499
6500 case M_USD:
6501 s = "sdl";
6502 s2 = "sdr";
6503 off = 7;
6504 goto usw;
6505 case M_USW:
6506 s = "swl";
6507 s2 = "swr";
6508 off = 3;
6509 usw:
6510 if (offset_expr.X_add_number >= 0x8000 - off)
6511 as_bad (_("operand overflow"));
6512 if (! target_big_endian)
6513 offset_expr.X_add_number += off;
6514 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6515 (int) BFD_RELOC_LO16, breg);
6516 if (! target_big_endian)
6517 offset_expr.X_add_number -= off;
6518 else
6519 offset_expr.X_add_number += off;
6520 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6521 (int) BFD_RELOC_LO16, breg);
6522 return;
6523
6524 case M_USD_A:
6525 s = "sdl";
6526 s2 = "sdr";
6527 off = 7;
6528 goto uswa;
6529 case M_USW_A:
6530 s = "swl";
6531 s2 = "swr";
6532 off = 3;
6533 uswa:
6534 load_address (&icnt, AT, &offset_expr);
6535 if (breg != 0)
6536 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6537 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6538 "d,v,t", AT, AT, breg);
6539 if (! target_big_endian)
6540 expr1.X_add_number = off;
6541 else
6542 expr1.X_add_number = 0;
6543 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6544 (int) BFD_RELOC_LO16, AT);
6545 if (! target_big_endian)
6546 expr1.X_add_number = 0;
6547 else
6548 expr1.X_add_number = off;
6549 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6550 (int) BFD_RELOC_LO16, AT);
6551 break;
6552
6553 case M_USH_A:
6554 load_address (&icnt, AT, &offset_expr);
6555 if (breg != 0)
6556 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6557 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6558 "d,v,t", AT, AT, breg);
6559 if (! target_big_endian)
6560 expr1.X_add_number = 0;
6561 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6562 (int) BFD_RELOC_LO16, AT);
6563 macro_build ((char *) NULL, &icnt, NULL, "srl", "d,w,<", treg,
6564 treg, 8);
6565 if (! target_big_endian)
6566 expr1.X_add_number = 1;
6567 else
6568 expr1.X_add_number = 0;
6569 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
6570 (int) BFD_RELOC_LO16, AT);
6571 if (! target_big_endian)
6572 expr1.X_add_number = 0;
6573 else
6574 expr1.X_add_number = 1;
6575 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
6576 (int) BFD_RELOC_LO16, AT);
6577 macro_build ((char *) NULL, &icnt, NULL, "sll", "d,w,<", treg,
6578 treg, 8);
6579 macro_build ((char *) NULL, &icnt, NULL, "or", "d,v,t", treg,
6580 treg, AT);
6581 break;
6582
6583 default:
6584 /* FIXME: Check if this is one of the itbl macros, since they
6585 are added dynamically. */
6586 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
6587 break;
6588 }
6589 if (mips_opts.noat)
6590 as_warn (_("Macro used $at after \".set noat\""));
6591 }
6592
6593 /* Implement macros in mips16 mode. */
6594
6595 static void
6596 mips16_macro (ip)
6597 struct mips_cl_insn *ip;
6598 {
6599 int mask;
6600 int xreg, yreg, zreg, tmp;
6601 int icnt;
6602 expressionS expr1;
6603 int dbl;
6604 const char *s, *s2, *s3;
6605
6606 mask = ip->insn_mo->mask;
6607
6608 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
6609 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
6610 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
6611
6612 icnt = 0;
6613
6614 expr1.X_op = O_constant;
6615 expr1.X_op_symbol = NULL;
6616 expr1.X_add_symbol = NULL;
6617 expr1.X_add_number = 1;
6618
6619 dbl = 0;
6620
6621 switch (mask)
6622 {
6623 default:
6624 internalError ();
6625
6626 case M_DDIV_3:
6627 dbl = 1;
6628 case M_DIV_3:
6629 s = "mflo";
6630 goto do_div3;
6631 case M_DREM_3:
6632 dbl = 1;
6633 case M_REM_3:
6634 s = "mfhi";
6635 do_div3:
6636 mips_emit_delays (true);
6637 ++mips_opts.noreorder;
6638 mips_any_noreorder = 1;
6639 macro_build ((char *) NULL, &icnt, NULL,
6640 dbl ? "ddiv" : "div",
6641 "0,x,y", xreg, yreg);
6642 expr1.X_add_number = 2;
6643 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6644 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6645
6646 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
6647 since that causes an overflow. We should do that as well,
6648 but I don't see how to do the comparisons without a temporary
6649 register. */
6650 --mips_opts.noreorder;
6651 macro_build ((char *) NULL, &icnt, NULL, s, "x", zreg);
6652 break;
6653
6654 case M_DIVU_3:
6655 s = "divu";
6656 s2 = "mflo";
6657 goto do_divu3;
6658 case M_REMU_3:
6659 s = "divu";
6660 s2 = "mfhi";
6661 goto do_divu3;
6662 case M_DDIVU_3:
6663 s = "ddivu";
6664 s2 = "mflo";
6665 goto do_divu3;
6666 case M_DREMU_3:
6667 s = "ddivu";
6668 s2 = "mfhi";
6669 do_divu3:
6670 mips_emit_delays (true);
6671 ++mips_opts.noreorder;
6672 mips_any_noreorder = 1;
6673 macro_build ((char *) NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
6674 expr1.X_add_number = 2;
6675 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
6676 macro_build ((char *) NULL, &icnt, NULL, "break", "6", 7);
6677 --mips_opts.noreorder;
6678 macro_build ((char *) NULL, &icnt, NULL, s2, "x", zreg);
6679 break;
6680
6681 case M_DMUL:
6682 dbl = 1;
6683 case M_MUL:
6684 macro_build ((char *) NULL, &icnt, NULL,
6685 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
6686 macro_build ((char *) NULL, &icnt, NULL, "mflo", "x", zreg);
6687 return;
6688
6689 case M_DSUBU_I:
6690 dbl = 1;
6691 goto do_subu;
6692 case M_SUBU_I:
6693 do_subu:
6694 if (imm_expr.X_op != O_constant)
6695 as_bad (_("Unsupported large constant"));
6696 imm_expr.X_add_number = -imm_expr.X_add_number;
6697 macro_build ((char *) NULL, &icnt, &imm_expr,
6698 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
6699 break;
6700
6701 case M_SUBU_I_2:
6702 if (imm_expr.X_op != O_constant)
6703 as_bad (_("Unsupported large constant"));
6704 imm_expr.X_add_number = -imm_expr.X_add_number;
6705 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
6706 "x,k", xreg);
6707 break;
6708
6709 case M_DSUBU_I_2:
6710 if (imm_expr.X_op != O_constant)
6711 as_bad (_("Unsupported large constant"));
6712 imm_expr.X_add_number = -imm_expr.X_add_number;
6713 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
6714 "y,j", yreg);
6715 break;
6716
6717 case M_BEQ:
6718 s = "cmp";
6719 s2 = "bteqz";
6720 goto do_branch;
6721 case M_BNE:
6722 s = "cmp";
6723 s2 = "btnez";
6724 goto do_branch;
6725 case M_BLT:
6726 s = "slt";
6727 s2 = "btnez";
6728 goto do_branch;
6729 case M_BLTU:
6730 s = "sltu";
6731 s2 = "btnez";
6732 goto do_branch;
6733 case M_BLE:
6734 s = "slt";
6735 s2 = "bteqz";
6736 goto do_reverse_branch;
6737 case M_BLEU:
6738 s = "sltu";
6739 s2 = "bteqz";
6740 goto do_reverse_branch;
6741 case M_BGE:
6742 s = "slt";
6743 s2 = "bteqz";
6744 goto do_branch;
6745 case M_BGEU:
6746 s = "sltu";
6747 s2 = "bteqz";
6748 goto do_branch;
6749 case M_BGT:
6750 s = "slt";
6751 s2 = "btnez";
6752 goto do_reverse_branch;
6753 case M_BGTU:
6754 s = "sltu";
6755 s2 = "btnez";
6756
6757 do_reverse_branch:
6758 tmp = xreg;
6759 xreg = yreg;
6760 yreg = tmp;
6761
6762 do_branch:
6763 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
6764 xreg, yreg);
6765 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6766 break;
6767
6768 case M_BEQ_I:
6769 s = "cmpi";
6770 s2 = "bteqz";
6771 s3 = "x,U";
6772 goto do_branch_i;
6773 case M_BNE_I:
6774 s = "cmpi";
6775 s2 = "btnez";
6776 s3 = "x,U";
6777 goto do_branch_i;
6778 case M_BLT_I:
6779 s = "slti";
6780 s2 = "btnez";
6781 s3 = "x,8";
6782 goto do_branch_i;
6783 case M_BLTU_I:
6784 s = "sltiu";
6785 s2 = "btnez";
6786 s3 = "x,8";
6787 goto do_branch_i;
6788 case M_BLE_I:
6789 s = "slti";
6790 s2 = "btnez";
6791 s3 = "x,8";
6792 goto do_addone_branch_i;
6793 case M_BLEU_I:
6794 s = "sltiu";
6795 s2 = "btnez";
6796 s3 = "x,8";
6797 goto do_addone_branch_i;
6798 case M_BGE_I:
6799 s = "slti";
6800 s2 = "bteqz";
6801 s3 = "x,8";
6802 goto do_branch_i;
6803 case M_BGEU_I:
6804 s = "sltiu";
6805 s2 = "bteqz";
6806 s3 = "x,8";
6807 goto do_branch_i;
6808 case M_BGT_I:
6809 s = "slti";
6810 s2 = "bteqz";
6811 s3 = "x,8";
6812 goto do_addone_branch_i;
6813 case M_BGTU_I:
6814 s = "sltiu";
6815 s2 = "bteqz";
6816 s3 = "x,8";
6817
6818 do_addone_branch_i:
6819 if (imm_expr.X_op != O_constant)
6820 as_bad (_("Unsupported large constant"));
6821 ++imm_expr.X_add_number;
6822
6823 do_branch_i:
6824 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
6825 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
6826 break;
6827
6828 case M_ABS:
6829 expr1.X_add_number = 0;
6830 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
6831 if (xreg != yreg)
6832 move_register (&icnt, xreg, yreg);
6833 expr1.X_add_number = 2;
6834 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
6835 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6836 "neg", "x,w", xreg, xreg);
6837 }
6838 }
6839
6840 /* For consistency checking, verify that all bits are specified either
6841 by the match/mask part of the instruction definition, or by the
6842 operand list. */
6843 static int
6844 validate_mips_insn (opc)
6845 const struct mips_opcode *opc;
6846 {
6847 const char *p = opc->args;
6848 char c;
6849 unsigned long used_bits = opc->mask;
6850
6851 if ((used_bits & opc->match) != opc->match)
6852 {
6853 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
6854 opc->name, opc->args);
6855 return 0;
6856 }
6857 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
6858 while (*p)
6859 switch (c = *p++)
6860 {
6861 case ',': break;
6862 case '(': break;
6863 case ')': break;
6864 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
6865 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
6866 case 'A': break;
6867 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
6868 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
6869 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
6870 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6871 case 'F': break;
6872 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
6873 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
6874 case 'I': break;
6875 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
6876 case 'L': break;
6877 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
6878 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
6879 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
6880 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
6881 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
6882 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
6883 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
6884 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
6885 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6886 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
6887 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
6888 case 'f': break;
6889 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
6890 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
6891 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6892 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
6893 case 'l': break;
6894 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6895 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
6896 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
6897 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6898 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6899 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6900 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
6901 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
6902 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6903 case 'x': break;
6904 case 'z': break;
6905 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
6906 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
6907 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
6908 default:
6909 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
6910 c, opc->name, opc->args);
6911 return 0;
6912 }
6913 #undef USE_BITS
6914 if (used_bits != 0xffffffff)
6915 {
6916 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
6917 ~used_bits & 0xffffffff, opc->name, opc->args);
6918 return 0;
6919 }
6920 return 1;
6921 }
6922
6923 /* This routine assembles an instruction into its binary format. As a
6924 side effect, it sets one of the global variables imm_reloc or
6925 offset_reloc to the type of relocation to do if one of the operands
6926 is an address expression. */
6927
6928 static void
6929 mips_ip (str, ip)
6930 char *str;
6931 struct mips_cl_insn *ip;
6932 {
6933 char *s;
6934 const char *args;
6935 char c = 0;
6936 struct mips_opcode *insn;
6937 char *argsStart;
6938 unsigned int regno;
6939 unsigned int lastregno = 0;
6940 char *s_reset;
6941 char save_c = 0;
6942 int full_opcode_match = 1;
6943
6944 insn_error = NULL;
6945
6946 /* If the instruction contains a '.', we first try to match an instruction
6947 including the '.'. Then we try again without the '.'. */
6948 insn = NULL;
6949 for (s = str; *s != '\0' && !isspace ((unsigned char) *s); ++s)
6950 continue;
6951
6952 /* If we stopped on whitespace, then replace the whitespace with null for
6953 the call to hash_find. Save the character we replaced just in case we
6954 have to re-parse the instruction. */
6955 if (isspace ((unsigned char) *s))
6956 {
6957 save_c = *s;
6958 *s++ = '\0';
6959 }
6960
6961 insn = (struct mips_opcode *) hash_find (op_hash, str);
6962
6963 /* If we didn't find the instruction in the opcode table, try again, but
6964 this time with just the instruction up to, but not including the
6965 first '.'. */
6966 if (insn == NULL)
6967 {
6968 /* Restore the character we overwrite above (if any). */
6969 if (save_c)
6970 *(--s) = save_c;
6971
6972 /* Scan up to the first '.' or whitespace. */
6973 for (s = str; *s != '\0' && *s != '.' && !isspace ((unsigned char) *s); ++s)
6974 continue;
6975
6976 /* If we did not find a '.', then we can quit now. */
6977 if (*s != '.')
6978 {
6979 insn_error = "unrecognized opcode";
6980 return;
6981 }
6982
6983 /* Lookup the instruction in the hash table. */
6984 *s++ = '\0';
6985 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
6986 {
6987 insn_error = "unrecognized opcode";
6988 return;
6989 }
6990
6991 full_opcode_match = 0;
6992 }
6993
6994 argsStart = s;
6995 for (;;)
6996 {
6997 boolean ok;
6998
6999 assert (strcmp (insn->name, str) == 0);
7000
7001 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7002 ok = true;
7003 else
7004 ok = false;
7005
7006 if (insn->pinfo != INSN_MACRO)
7007 {
7008 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7009 ok = false;
7010 }
7011
7012 if (! ok)
7013 {
7014 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7015 && strcmp (insn->name, insn[1].name) == 0)
7016 {
7017 ++insn;
7018 continue;
7019 }
7020 else
7021 {
7022 if (!insn_error)
7023 {
7024 static char buf[100];
7025 sprintf (buf,
7026 _("opcode not supported on this processor: %s (%s)"),
7027 mips_cpu_to_str (mips_arch),
7028 mips_isa_to_str (mips_opts.isa));
7029
7030 insn_error = buf;
7031 }
7032 if (save_c)
7033 *(--s) = save_c;
7034 return;
7035 }
7036 }
7037
7038 ip->insn_mo = insn;
7039 ip->insn_opcode = insn->match;
7040 insn_error = NULL;
7041 for (args = insn->args;; ++args)
7042 {
7043 if (*s == ' ')
7044 ++s;
7045 switch (*args)
7046 {
7047 case '\0': /* end of args */
7048 if (*s == '\0')
7049 return;
7050 break;
7051
7052 case ',':
7053 if (*s++ == *args)
7054 continue;
7055 s--;
7056 switch (*++args)
7057 {
7058 case 'r':
7059 case 'v':
7060 ip->insn_opcode |= lastregno << OP_SH_RS;
7061 continue;
7062
7063 case 'w':
7064 ip->insn_opcode |= lastregno << OP_SH_RT;
7065 continue;
7066
7067 case 'W':
7068 ip->insn_opcode |= lastregno << OP_SH_FT;
7069 continue;
7070
7071 case 'V':
7072 ip->insn_opcode |= lastregno << OP_SH_FS;
7073 continue;
7074 }
7075 break;
7076
7077 case '(':
7078 /* Handle optional base register.
7079 Either the base register is omitted or
7080 we must have a left paren. */
7081 /* This is dependent on the next operand specifier
7082 is a base register specification. */
7083 assert (args[1] == 'b' || args[1] == '5'
7084 || args[1] == '-' || args[1] == '4');
7085 if (*s == '\0')
7086 return;
7087
7088 case ')': /* these must match exactly */
7089 if (*s++ == *args)
7090 continue;
7091 break;
7092
7093 case '<': /* must be at least one digit */
7094 /*
7095 * According to the manual, if the shift amount is greater
7096 * than 31 or less than 0 the the shift amount should be
7097 * mod 32. In reality the mips assembler issues an error.
7098 * We issue a warning and mask out all but the low 5 bits.
7099 */
7100 my_getExpression (&imm_expr, s);
7101 check_absolute_expr (ip, &imm_expr);
7102 if ((unsigned long) imm_expr.X_add_number > 31)
7103 {
7104 as_warn (_("Improper shift amount (%ld)"),
7105 (long) imm_expr.X_add_number);
7106 imm_expr.X_add_number &= OP_MASK_SHAMT;
7107 }
7108 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7109 imm_expr.X_op = O_absent;
7110 s = expr_end;
7111 continue;
7112
7113 case '>': /* shift amount minus 32 */
7114 my_getExpression (&imm_expr, s);
7115 check_absolute_expr (ip, &imm_expr);
7116 if ((unsigned long) imm_expr.X_add_number < 32
7117 || (unsigned long) imm_expr.X_add_number > 63)
7118 break;
7119 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7120 imm_expr.X_op = O_absent;
7121 s = expr_end;
7122 continue;
7123
7124 case 'k': /* cache code */
7125 case 'h': /* prefx code */
7126 my_getExpression (&imm_expr, s);
7127 check_absolute_expr (ip, &imm_expr);
7128 if ((unsigned long) imm_expr.X_add_number > 31)
7129 {
7130 as_warn (_("Invalid value for `%s' (%lu)"),
7131 ip->insn_mo->name,
7132 (unsigned long) imm_expr.X_add_number);
7133 imm_expr.X_add_number &= 0x1f;
7134 }
7135 if (*args == 'k')
7136 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7137 else
7138 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7139 imm_expr.X_op = O_absent;
7140 s = expr_end;
7141 continue;
7142
7143 case 'c': /* break code */
7144 my_getExpression (&imm_expr, s);
7145 check_absolute_expr (ip, &imm_expr);
7146 if ((unsigned) imm_expr.X_add_number > 1023)
7147 {
7148 as_warn (_("Illegal break code (%ld)"),
7149 (long) imm_expr.X_add_number);
7150 imm_expr.X_add_number &= OP_MASK_CODE;
7151 }
7152 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7153 imm_expr.X_op = O_absent;
7154 s = expr_end;
7155 continue;
7156
7157 case 'q': /* lower break code */
7158 my_getExpression (&imm_expr, s);
7159 check_absolute_expr (ip, &imm_expr);
7160 if ((unsigned) imm_expr.X_add_number > 1023)
7161 {
7162 as_warn (_("Illegal lower break code (%ld)"),
7163 (long) imm_expr.X_add_number);
7164 imm_expr.X_add_number &= OP_MASK_CODE2;
7165 }
7166 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7167 imm_expr.X_op = O_absent;
7168 s = expr_end;
7169 continue;
7170
7171 case 'B': /* 20-bit syscall/break code. */
7172 my_getExpression (&imm_expr, s);
7173 check_absolute_expr (ip, &imm_expr);
7174 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7175 as_warn (_("Illegal 20-bit code (%ld)"),
7176 (long) imm_expr.X_add_number);
7177 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7178 imm_expr.X_op = O_absent;
7179 s = expr_end;
7180 continue;
7181
7182 case 'C': /* Coprocessor code */
7183 my_getExpression (&imm_expr, s);
7184 check_absolute_expr (ip, &imm_expr);
7185 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7186 {
7187 as_warn (_("Coproccesor code > 25 bits (%ld)"),
7188 (long) imm_expr.X_add_number);
7189 imm_expr.X_add_number &= ((1<<25) - 1);
7190 }
7191 ip->insn_opcode |= imm_expr.X_add_number;
7192 imm_expr.X_op = O_absent;
7193 s = expr_end;
7194 continue;
7195
7196 case 'J': /* 19-bit wait code. */
7197 my_getExpression (&imm_expr, s);
7198 check_absolute_expr (ip, &imm_expr);
7199 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7200 as_warn (_("Illegal 19-bit code (%ld)"),
7201 (long) imm_expr.X_add_number);
7202 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7203 imm_expr.X_op = O_absent;
7204 s = expr_end;
7205 continue;
7206
7207 case 'P': /* Performance register */
7208 my_getExpression (&imm_expr, s);
7209 check_absolute_expr (ip, &imm_expr);
7210 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7211 {
7212 as_warn (_("Invalid performance register (%ld)"),
7213 (long) imm_expr.X_add_number);
7214 imm_expr.X_add_number &= OP_MASK_PERFREG;
7215 }
7216 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7217 imm_expr.X_op = O_absent;
7218 s = expr_end;
7219 continue;
7220
7221 case 'b': /* base register */
7222 case 'd': /* destination register */
7223 case 's': /* source register */
7224 case 't': /* target register */
7225 case 'r': /* both target and source */
7226 case 'v': /* both dest and source */
7227 case 'w': /* both dest and target */
7228 case 'E': /* coprocessor target register */
7229 case 'G': /* coprocessor destination register */
7230 case 'x': /* ignore register name */
7231 case 'z': /* must be zero register */
7232 case 'U': /* destination register (clo/clz). */
7233 s_reset = s;
7234 if (s[0] == '$')
7235 {
7236
7237 if (isdigit ((unsigned char) s[1]))
7238 {
7239 ++s;
7240 regno = 0;
7241 do
7242 {
7243 regno *= 10;
7244 regno += *s - '0';
7245 ++s;
7246 }
7247 while (isdigit ((unsigned char) *s));
7248 if (regno > 31)
7249 as_bad (_("Invalid register number (%d)"), regno);
7250 }
7251 else if (*args == 'E' || *args == 'G')
7252 goto notreg;
7253 else
7254 {
7255 if (s[1] == 'f' && s[2] == 'p')
7256 {
7257 s += 3;
7258 regno = FP;
7259 }
7260 else if (s[1] == 's' && s[2] == 'p')
7261 {
7262 s += 3;
7263 regno = SP;
7264 }
7265 else if (s[1] == 'g' && s[2] == 'p')
7266 {
7267 s += 3;
7268 regno = GP;
7269 }
7270 else if (s[1] == 'a' && s[2] == 't')
7271 {
7272 s += 3;
7273 regno = AT;
7274 }
7275 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7276 {
7277 s += 4;
7278 regno = KT0;
7279 }
7280 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7281 {
7282 s += 4;
7283 regno = KT1;
7284 }
7285 else if (itbl_have_entries)
7286 {
7287 char *p, *n;
7288 unsigned long r;
7289
7290 p = s + 1; /* advance past '$' */
7291 n = itbl_get_field (&p); /* n is name */
7292
7293 /* See if this is a register defined in an
7294 itbl entry. */
7295 if (itbl_get_reg_val (n, &r))
7296 {
7297 /* Get_field advances to the start of
7298 the next field, so we need to back
7299 rack to the end of the last field. */
7300 if (p)
7301 s = p - 1;
7302 else
7303 s = strchr (s, '\0');
7304 regno = r;
7305 }
7306 else
7307 goto notreg;
7308 }
7309 else
7310 goto notreg;
7311 }
7312 if (regno == AT
7313 && ! mips_opts.noat
7314 && *args != 'E'
7315 && *args != 'G')
7316 as_warn (_("Used $at without \".set noat\""));
7317 c = *args;
7318 if (*s == ' ')
7319 s++;
7320 if (args[1] != *s)
7321 {
7322 if (c == 'r' || c == 'v' || c == 'w')
7323 {
7324 regno = lastregno;
7325 s = s_reset;
7326 args++;
7327 }
7328 }
7329 /* 'z' only matches $0. */
7330 if (c == 'z' && regno != 0)
7331 break;
7332
7333 /* Now that we have assembled one operand, we use the args string
7334 * to figure out where it goes in the instruction. */
7335 switch (c)
7336 {
7337 case 'r':
7338 case 's':
7339 case 'v':
7340 case 'b':
7341 ip->insn_opcode |= regno << OP_SH_RS;
7342 break;
7343 case 'd':
7344 case 'G':
7345 ip->insn_opcode |= regno << OP_SH_RD;
7346 break;
7347 case 'U':
7348 ip->insn_opcode |= regno << OP_SH_RD;
7349 ip->insn_opcode |= regno << OP_SH_RT;
7350 break;
7351 case 'w':
7352 case 't':
7353 case 'E':
7354 ip->insn_opcode |= regno << OP_SH_RT;
7355 break;
7356 case 'x':
7357 /* This case exists because on the r3000 trunc
7358 expands into a macro which requires a gp
7359 register. On the r6000 or r4000 it is
7360 assembled into a single instruction which
7361 ignores the register. Thus the insn version
7362 is MIPS_ISA2 and uses 'x', and the macro
7363 version is MIPS_ISA1 and uses 't'. */
7364 break;
7365 case 'z':
7366 /* This case is for the div instruction, which
7367 acts differently if the destination argument
7368 is $0. This only matches $0, and is checked
7369 outside the switch. */
7370 break;
7371 case 'D':
7372 /* Itbl operand; not yet implemented. FIXME ?? */
7373 break;
7374 /* What about all other operands like 'i', which
7375 can be specified in the opcode table? */
7376 }
7377 lastregno = regno;
7378 continue;
7379 }
7380 notreg:
7381 switch (*args++)
7382 {
7383 case 'r':
7384 case 'v':
7385 ip->insn_opcode |= lastregno << OP_SH_RS;
7386 continue;
7387 case 'w':
7388 ip->insn_opcode |= lastregno << OP_SH_RT;
7389 continue;
7390 }
7391 break;
7392
7393 case 'D': /* floating point destination register */
7394 case 'S': /* floating point source register */
7395 case 'T': /* floating point target register */
7396 case 'R': /* floating point source register */
7397 case 'V':
7398 case 'W':
7399 s_reset = s;
7400 if (s[0] == '$' && s[1] == 'f' && isdigit ((unsigned char) s[2]))
7401 {
7402 s += 2;
7403 regno = 0;
7404 do
7405 {
7406 regno *= 10;
7407 regno += *s - '0';
7408 ++s;
7409 }
7410 while (isdigit ((unsigned char) *s));
7411
7412 if (regno > 31)
7413 as_bad (_("Invalid float register number (%d)"), regno);
7414
7415 if ((regno & 1) != 0
7416 && HAVE_32BIT_FPRS
7417 && ! (strcmp (str, "mtc1") == 0
7418 || strcmp (str, "mfc1") == 0
7419 || strcmp (str, "lwc1") == 0
7420 || strcmp (str, "swc1") == 0
7421 || strcmp (str, "l.s") == 0
7422 || strcmp (str, "s.s") == 0))
7423 as_warn (_("Float register should be even, was %d"),
7424 regno);
7425
7426 c = *args;
7427 if (*s == ' ')
7428 s++;
7429 if (args[1] != *s)
7430 {
7431 if (c == 'V' || c == 'W')
7432 {
7433 regno = lastregno;
7434 s = s_reset;
7435 args++;
7436 }
7437 }
7438 switch (c)
7439 {
7440 case 'D':
7441 ip->insn_opcode |= regno << OP_SH_FD;
7442 break;
7443 case 'V':
7444 case 'S':
7445 ip->insn_opcode |= regno << OP_SH_FS;
7446 break;
7447 case 'W':
7448 case 'T':
7449 ip->insn_opcode |= regno << OP_SH_FT;
7450 break;
7451 case 'R':
7452 ip->insn_opcode |= regno << OP_SH_FR;
7453 break;
7454 }
7455 lastregno = regno;
7456 continue;
7457 }
7458
7459 switch (*args++)
7460 {
7461 case 'V':
7462 ip->insn_opcode |= lastregno << OP_SH_FS;
7463 continue;
7464 case 'W':
7465 ip->insn_opcode |= lastregno << OP_SH_FT;
7466 continue;
7467 }
7468 break;
7469
7470 case 'I':
7471 my_getExpression (&imm_expr, s);
7472 if (imm_expr.X_op != O_big
7473 && imm_expr.X_op != O_constant)
7474 insn_error = _("absolute expression required");
7475 s = expr_end;
7476 continue;
7477
7478 case 'A':
7479 my_getExpression (&offset_expr, s);
7480 imm_reloc = BFD_RELOC_32;
7481 s = expr_end;
7482 continue;
7483
7484 case 'F':
7485 case 'L':
7486 case 'f':
7487 case 'l':
7488 {
7489 int f64;
7490 int using_gprs;
7491 char *save_in;
7492 char *err;
7493 unsigned char temp[8];
7494 int len;
7495 unsigned int length;
7496 segT seg;
7497 subsegT subseg;
7498 char *p;
7499
7500 /* These only appear as the last operand in an
7501 instruction, and every instruction that accepts
7502 them in any variant accepts them in all variants.
7503 This means we don't have to worry about backing out
7504 any changes if the instruction does not match.
7505
7506 The difference between them is the size of the
7507 floating point constant and where it goes. For 'F'
7508 and 'L' the constant is 64 bits; for 'f' and 'l' it
7509 is 32 bits. Where the constant is placed is based
7510 on how the MIPS assembler does things:
7511 F -- .rdata
7512 L -- .lit8
7513 f -- immediate value
7514 l -- .lit4
7515
7516 The .lit4 and .lit8 sections are only used if
7517 permitted by the -G argument.
7518
7519 When generating embedded PIC code, we use the
7520 .lit8 section but not the .lit4 section (we can do
7521 .lit4 inline easily; we need to put .lit8
7522 somewhere in the data segment, and using .lit8
7523 permits the linker to eventually combine identical
7524 .lit8 entries).
7525
7526 The code below needs to know whether the target register
7527 is 32 or 64 bits wide. It relies on the fact 'f' and
7528 'F' are used with GPR-based instructions and 'l' and
7529 'L' are used with FPR-based instructions. */
7530
7531 f64 = *args == 'F' || *args == 'L';
7532 using_gprs = *args == 'F' || *args == 'f';
7533
7534 save_in = input_line_pointer;
7535 input_line_pointer = s;
7536 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
7537 length = len;
7538 s = input_line_pointer;
7539 input_line_pointer = save_in;
7540 if (err != NULL && *err != '\0')
7541 {
7542 as_bad (_("Bad floating point constant: %s"), err);
7543 memset (temp, '\0', sizeof temp);
7544 length = f64 ? 8 : 4;
7545 }
7546
7547 assert (length == (unsigned) (f64 ? 8 : 4));
7548
7549 if (*args == 'f'
7550 || (*args == 'l'
7551 && (! USE_GLOBAL_POINTER_OPT
7552 || mips_pic == EMBEDDED_PIC
7553 || g_switch_value < 4
7554 || (temp[0] == 0 && temp[1] == 0)
7555 || (temp[2] == 0 && temp[3] == 0))))
7556 {
7557 imm_expr.X_op = O_constant;
7558 if (! target_big_endian)
7559 imm_expr.X_add_number = bfd_getl32 (temp);
7560 else
7561 imm_expr.X_add_number = bfd_getb32 (temp);
7562 }
7563 else if (length > 4
7564 && ! mips_disable_float_construction
7565 /* Constants can only be constructed in GPRs and
7566 copied to FPRs if the GPRs are at least as wide
7567 as the FPRs. Force the constant into memory if
7568 we are using 64-bit FPRs but the GPRs are only
7569 32 bits wide. */
7570 && (using_gprs
7571 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
7572 && ((temp[0] == 0 && temp[1] == 0)
7573 || (temp[2] == 0 && temp[3] == 0))
7574 && ((temp[4] == 0 && temp[5] == 0)
7575 || (temp[6] == 0 && temp[7] == 0)))
7576 {
7577 /* The value is simple enough to load with a couple of
7578 instructions. If using 32-bit registers, set
7579 imm_expr to the high order 32 bits and offset_expr to
7580 the low order 32 bits. Otherwise, set imm_expr to
7581 the entire 64 bit constant. */
7582 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
7583 {
7584 imm_expr.X_op = O_constant;
7585 offset_expr.X_op = O_constant;
7586 if (! target_big_endian)
7587 {
7588 imm_expr.X_add_number = bfd_getl32 (temp + 4);
7589 offset_expr.X_add_number = bfd_getl32 (temp);
7590 }
7591 else
7592 {
7593 imm_expr.X_add_number = bfd_getb32 (temp);
7594 offset_expr.X_add_number = bfd_getb32 (temp + 4);
7595 }
7596 if (offset_expr.X_add_number == 0)
7597 offset_expr.X_op = O_absent;
7598 }
7599 else if (sizeof (imm_expr.X_add_number) > 4)
7600 {
7601 imm_expr.X_op = O_constant;
7602 if (! target_big_endian)
7603 imm_expr.X_add_number = bfd_getl64 (temp);
7604 else
7605 imm_expr.X_add_number = bfd_getb64 (temp);
7606 }
7607 else
7608 {
7609 imm_expr.X_op = O_big;
7610 imm_expr.X_add_number = 4;
7611 if (! target_big_endian)
7612 {
7613 generic_bignum[0] = bfd_getl16 (temp);
7614 generic_bignum[1] = bfd_getl16 (temp + 2);
7615 generic_bignum[2] = bfd_getl16 (temp + 4);
7616 generic_bignum[3] = bfd_getl16 (temp + 6);
7617 }
7618 else
7619 {
7620 generic_bignum[0] = bfd_getb16 (temp + 6);
7621 generic_bignum[1] = bfd_getb16 (temp + 4);
7622 generic_bignum[2] = bfd_getb16 (temp + 2);
7623 generic_bignum[3] = bfd_getb16 (temp);
7624 }
7625 }
7626 }
7627 else
7628 {
7629 const char *newname;
7630 segT new_seg;
7631
7632 /* Switch to the right section. */
7633 seg = now_seg;
7634 subseg = now_subseg;
7635 switch (*args)
7636 {
7637 default: /* unused default case avoids warnings. */
7638 case 'L':
7639 newname = RDATA_SECTION_NAME;
7640 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
7641 || mips_pic == EMBEDDED_PIC)
7642 newname = ".lit8";
7643 break;
7644 case 'F':
7645 if (mips_pic == EMBEDDED_PIC)
7646 newname = ".lit8";
7647 else
7648 newname = RDATA_SECTION_NAME;
7649 break;
7650 case 'l':
7651 assert (!USE_GLOBAL_POINTER_OPT
7652 || g_switch_value >= 4);
7653 newname = ".lit4";
7654 break;
7655 }
7656 new_seg = subseg_new (newname, (subsegT) 0);
7657 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
7658 bfd_set_section_flags (stdoutput, new_seg,
7659 (SEC_ALLOC
7660 | SEC_LOAD
7661 | SEC_READONLY
7662 | SEC_DATA));
7663 frag_align (*args == 'l' ? 2 : 3, 0, 0);
7664 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
7665 && strcmp (TARGET_OS, "elf") != 0)
7666 record_alignment (new_seg, 4);
7667 else
7668 record_alignment (new_seg, *args == 'l' ? 2 : 3);
7669 if (seg == now_seg)
7670 as_bad (_("Can't use floating point insn in this section"));
7671
7672 /* Set the argument to the current address in the
7673 section. */
7674 offset_expr.X_op = O_symbol;
7675 offset_expr.X_add_symbol =
7676 symbol_new ("L0\001", now_seg,
7677 (valueT) frag_now_fix (), frag_now);
7678 offset_expr.X_add_number = 0;
7679
7680 /* Put the floating point number into the section. */
7681 p = frag_more ((int) length);
7682 memcpy (p, temp, length);
7683
7684 /* Switch back to the original section. */
7685 subseg_set (seg, subseg);
7686 }
7687 }
7688 continue;
7689
7690 case 'i': /* 16 bit unsigned immediate */
7691 case 'j': /* 16 bit signed immediate */
7692 imm_reloc = BFD_RELOC_LO16;
7693 c = my_getSmallExpression (&imm_expr, s);
7694 if (c != S_EX_NONE)
7695 {
7696 if (c != S_EX_LO)
7697 {
7698 if (imm_expr.X_op == O_constant)
7699 imm_expr.X_add_number =
7700 (imm_expr.X_add_number >> 16) & 0xffff;
7701 else if (c == S_EX_HIGHEST)
7702 imm_reloc = BFD_RELOC_MIPS_HIGHEST;
7703 else if (c == S_EX_HIGHER)
7704 imm_reloc = BFD_RELOC_MIPS_HIGHER;
7705 else if (c == S_EX_HI)
7706 {
7707 imm_reloc = BFD_RELOC_HI16_S;
7708 imm_unmatched_hi = true;
7709 }
7710 else
7711 imm_reloc = BFD_RELOC_HI16;
7712 }
7713 else if (imm_expr.X_op == O_constant)
7714 imm_expr.X_add_number &= 0xffff;
7715 }
7716 if (*args == 'i')
7717 {
7718 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
7719 || ((imm_expr.X_add_number < 0
7720 || imm_expr.X_add_number >= 0x10000)
7721 && imm_expr.X_op == O_constant))
7722 {
7723 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7724 !strcmp (insn->name, insn[1].name))
7725 break;
7726 if (imm_expr.X_op == O_constant
7727 || imm_expr.X_op == O_big)
7728 as_bad (_("16 bit expression not in range 0..65535"));
7729 }
7730 }
7731 else
7732 {
7733 int more;
7734 offsetT max;
7735
7736 /* The upper bound should be 0x8000, but
7737 unfortunately the MIPS assembler accepts numbers
7738 from 0x8000 to 0xffff and sign extends them, and
7739 we want to be compatible. We only permit this
7740 extended range for an instruction which does not
7741 provide any further alternates, since those
7742 alternates may handle other cases. People should
7743 use the numbers they mean, rather than relying on
7744 a mysterious sign extension. */
7745 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7746 strcmp (insn->name, insn[1].name) == 0);
7747 if (more)
7748 max = 0x8000;
7749 else
7750 max = 0x10000;
7751 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
7752 || ((imm_expr.X_add_number < -0x8000
7753 || imm_expr.X_add_number >= max)
7754 && imm_expr.X_op == O_constant)
7755 || (more
7756 && imm_expr.X_add_number < 0
7757 && HAVE_64BIT_GPRS
7758 && imm_expr.X_unsigned
7759 && sizeof (imm_expr.X_add_number) <= 4))
7760 {
7761 if (more)
7762 break;
7763 if (imm_expr.X_op == O_constant
7764 || imm_expr.X_op == O_big)
7765 as_bad (_("16 bit expression not in range -32768..32767"));
7766 }
7767 }
7768 s = expr_end;
7769 continue;
7770
7771 case 'o': /* 16 bit offset */
7772 c = my_getSmallExpression (&offset_expr, s);
7773
7774 /* If this value won't fit into a 16 bit offset, then go
7775 find a macro that will generate the 32 bit offset
7776 code pattern. As a special hack, we accept the
7777 difference of two local symbols as a constant. This
7778 is required to suppose embedded PIC switches, which
7779 use an instruction which looks like
7780 lw $4,$L12-$LS12($4)
7781 The problem with handling this in a more general
7782 fashion is that the macro function doesn't expect to
7783 see anything which can be handled in a single
7784 constant instruction. */
7785 if (c == S_EX_NONE
7786 && (offset_expr.X_op != O_constant
7787 || offset_expr.X_add_number >= 0x8000
7788 || offset_expr.X_add_number < -0x8000)
7789 && (mips_pic != EMBEDDED_PIC
7790 || offset_expr.X_op != O_subtract
7791 || (S_GET_SEGMENT (offset_expr.X_add_symbol)
7792 != S_GET_SEGMENT (offset_expr.X_op_symbol))))
7793 break;
7794
7795 if (c == S_EX_HI)
7796 {
7797 if (offset_expr.X_op != O_constant)
7798 break;
7799 offset_expr.X_add_number =
7800 (offset_expr.X_add_number >> 16) & 0xffff;
7801 }
7802 offset_reloc = BFD_RELOC_LO16;
7803 s = expr_end;
7804 continue;
7805
7806 case 'p': /* pc relative offset */
7807 if (mips_pic == EMBEDDED_PIC)
7808 offset_reloc = BFD_RELOC_16_PCREL_S2;
7809 else
7810 offset_reloc = BFD_RELOC_16_PCREL;
7811 my_getExpression (&offset_expr, s);
7812 s = expr_end;
7813 continue;
7814
7815 case 'u': /* upper 16 bits */
7816 c = my_getSmallExpression (&imm_expr, s);
7817 imm_reloc = BFD_RELOC_LO16;
7818 if (c != S_EX_NONE)
7819 {
7820 if (c != S_EX_LO)
7821 {
7822 if (imm_expr.X_op == O_constant)
7823 imm_expr.X_add_number =
7824 (imm_expr.X_add_number >> 16) & 0xffff;
7825 else if (c == S_EX_HIGHEST)
7826 imm_reloc = BFD_RELOC_MIPS_HIGHEST;
7827 else if (c == S_EX_HI)
7828 {
7829 imm_reloc = BFD_RELOC_HI16_S;
7830 imm_unmatched_hi = true;
7831 }
7832 else
7833 imm_reloc = BFD_RELOC_HI16;
7834 }
7835 else if (imm_expr.X_op == O_constant)
7836 imm_expr.X_add_number &= 0xffff;
7837 }
7838 if (imm_expr.X_op == O_constant
7839 && (imm_expr.X_add_number < 0
7840 || imm_expr.X_add_number >= 0x10000))
7841 as_bad (_("lui expression not in range 0..65535"));
7842 s = expr_end;
7843 continue;
7844
7845 case 'a': /* 26 bit address */
7846 my_getExpression (&offset_expr, s);
7847 s = expr_end;
7848 offset_reloc = BFD_RELOC_MIPS_JMP;
7849 continue;
7850
7851 case 'N': /* 3 bit branch condition code */
7852 case 'M': /* 3 bit compare condition code */
7853 if (strncmp (s, "$fcc", 4) != 0)
7854 break;
7855 s += 4;
7856 regno = 0;
7857 do
7858 {
7859 regno *= 10;
7860 regno += *s - '0';
7861 ++s;
7862 }
7863 while (isdigit ((unsigned char) *s));
7864 if (regno > 7)
7865 as_bad (_("invalid condition code register $fcc%d"), regno);
7866 if (*args == 'N')
7867 ip->insn_opcode |= regno << OP_SH_BCC;
7868 else
7869 ip->insn_opcode |= regno << OP_SH_CCC;
7870 continue;
7871
7872 case 'H':
7873 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
7874 s += 2;
7875 if (isdigit ((unsigned char) *s))
7876 {
7877 c = 0;
7878 do
7879 {
7880 c *= 10;
7881 c += *s - '0';
7882 ++s;
7883 }
7884 while (isdigit ((unsigned char) *s));
7885 }
7886 else
7887 c = 8; /* Invalid sel value. */
7888
7889 if (c > 7)
7890 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
7891 ip->insn_opcode |= c;
7892 continue;
7893
7894 default:
7895 as_bad (_("bad char = '%c'\n"), *args);
7896 internalError ();
7897 }
7898 break;
7899 }
7900 /* Args don't match. */
7901 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
7902 !strcmp (insn->name, insn[1].name))
7903 {
7904 ++insn;
7905 s = argsStart;
7906 insn_error = _("illegal operands");
7907 continue;
7908 }
7909 if (save_c)
7910 *(--s) = save_c;
7911 insn_error = _("illegal operands");
7912 return;
7913 }
7914 }
7915
7916 /* This routine assembles an instruction into its binary format when
7917 assembling for the mips16. As a side effect, it sets one of the
7918 global variables imm_reloc or offset_reloc to the type of
7919 relocation to do if one of the operands is an address expression.
7920 It also sets mips16_small and mips16_ext if the user explicitly
7921 requested a small or extended instruction. */
7922
7923 static void
7924 mips16_ip (str, ip)
7925 char *str;
7926 struct mips_cl_insn *ip;
7927 {
7928 char *s;
7929 const char *args;
7930 struct mips_opcode *insn;
7931 char *argsstart;
7932 unsigned int regno;
7933 unsigned int lastregno = 0;
7934 char *s_reset;
7935
7936 insn_error = NULL;
7937
7938 mips16_small = false;
7939 mips16_ext = false;
7940
7941 for (s = str; islower ((unsigned char) *s); ++s)
7942 ;
7943 switch (*s)
7944 {
7945 case '\0':
7946 break;
7947
7948 case ' ':
7949 *s++ = '\0';
7950 break;
7951
7952 case '.':
7953 if (s[1] == 't' && s[2] == ' ')
7954 {
7955 *s = '\0';
7956 mips16_small = true;
7957 s += 3;
7958 break;
7959 }
7960 else if (s[1] == 'e' && s[2] == ' ')
7961 {
7962 *s = '\0';
7963 mips16_ext = true;
7964 s += 3;
7965 break;
7966 }
7967 /* Fall through. */
7968 default:
7969 insn_error = _("unknown opcode");
7970 return;
7971 }
7972
7973 if (mips_opts.noautoextend && ! mips16_ext)
7974 mips16_small = true;
7975
7976 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
7977 {
7978 insn_error = _("unrecognized opcode");
7979 return;
7980 }
7981
7982 argsstart = s;
7983 for (;;)
7984 {
7985 assert (strcmp (insn->name, str) == 0);
7986
7987 ip->insn_mo = insn;
7988 ip->insn_opcode = insn->match;
7989 ip->use_extend = false;
7990 imm_expr.X_op = O_absent;
7991 imm_reloc = BFD_RELOC_UNUSED;
7992 offset_expr.X_op = O_absent;
7993 offset_reloc = BFD_RELOC_UNUSED;
7994 for (args = insn->args; 1; ++args)
7995 {
7996 int c;
7997
7998 if (*s == ' ')
7999 ++s;
8000
8001 /* In this switch statement we call break if we did not find
8002 a match, continue if we did find a match, or return if we
8003 are done. */
8004
8005 c = *args;
8006 switch (c)
8007 {
8008 case '\0':
8009 if (*s == '\0')
8010 {
8011 /* Stuff the immediate value in now, if we can. */
8012 if (imm_expr.X_op == O_constant
8013 && imm_reloc > BFD_RELOC_UNUSED
8014 && insn->pinfo != INSN_MACRO)
8015 {
8016 mips16_immed ((char *) NULL, 0,
8017 imm_reloc - BFD_RELOC_UNUSED,
8018 imm_expr.X_add_number, true, mips16_small,
8019 mips16_ext, &ip->insn_opcode,
8020 &ip->use_extend, &ip->extend);
8021 imm_expr.X_op = O_absent;
8022 imm_reloc = BFD_RELOC_UNUSED;
8023 }
8024
8025 return;
8026 }
8027 break;
8028
8029 case ',':
8030 if (*s++ == c)
8031 continue;
8032 s--;
8033 switch (*++args)
8034 {
8035 case 'v':
8036 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8037 continue;
8038 case 'w':
8039 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8040 continue;
8041 }
8042 break;
8043
8044 case '(':
8045 case ')':
8046 if (*s++ == c)
8047 continue;
8048 break;
8049
8050 case 'v':
8051 case 'w':
8052 if (s[0] != '$')
8053 {
8054 if (c == 'v')
8055 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8056 else
8057 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8058 ++args;
8059 continue;
8060 }
8061 /* Fall through. */
8062 case 'x':
8063 case 'y':
8064 case 'z':
8065 case 'Z':
8066 case '0':
8067 case 'S':
8068 case 'R':
8069 case 'X':
8070 case 'Y':
8071 if (s[0] != '$')
8072 break;
8073 s_reset = s;
8074 if (isdigit ((unsigned char) s[1]))
8075 {
8076 ++s;
8077 regno = 0;
8078 do
8079 {
8080 regno *= 10;
8081 regno += *s - '0';
8082 ++s;
8083 }
8084 while (isdigit ((unsigned char) *s));
8085 if (regno > 31)
8086 {
8087 as_bad (_("invalid register number (%d)"), regno);
8088 regno = 2;
8089 }
8090 }
8091 else
8092 {
8093 if (s[1] == 'f' && s[2] == 'p')
8094 {
8095 s += 3;
8096 regno = FP;
8097 }
8098 else if (s[1] == 's' && s[2] == 'p')
8099 {
8100 s += 3;
8101 regno = SP;
8102 }
8103 else if (s[1] == 'g' && s[2] == 'p')
8104 {
8105 s += 3;
8106 regno = GP;
8107 }
8108 else if (s[1] == 'a' && s[2] == 't')
8109 {
8110 s += 3;
8111 regno = AT;
8112 }
8113 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8114 {
8115 s += 4;
8116 regno = KT0;
8117 }
8118 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8119 {
8120 s += 4;
8121 regno = KT1;
8122 }
8123 else
8124 break;
8125 }
8126
8127 if (*s == ' ')
8128 ++s;
8129 if (args[1] != *s)
8130 {
8131 if (c == 'v' || c == 'w')
8132 {
8133 regno = mips16_to_32_reg_map[lastregno];
8134 s = s_reset;
8135 args++;
8136 }
8137 }
8138
8139 switch (c)
8140 {
8141 case 'x':
8142 case 'y':
8143 case 'z':
8144 case 'v':
8145 case 'w':
8146 case 'Z':
8147 regno = mips32_to_16_reg_map[regno];
8148 break;
8149
8150 case '0':
8151 if (regno != 0)
8152 regno = ILLEGAL_REG;
8153 break;
8154
8155 case 'S':
8156 if (regno != SP)
8157 regno = ILLEGAL_REG;
8158 break;
8159
8160 case 'R':
8161 if (regno != RA)
8162 regno = ILLEGAL_REG;
8163 break;
8164
8165 case 'X':
8166 case 'Y':
8167 if (regno == AT && ! mips_opts.noat)
8168 as_warn (_("used $at without \".set noat\""));
8169 break;
8170
8171 default:
8172 internalError ();
8173 }
8174
8175 if (regno == ILLEGAL_REG)
8176 break;
8177
8178 switch (c)
8179 {
8180 case 'x':
8181 case 'v':
8182 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8183 break;
8184 case 'y':
8185 case 'w':
8186 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8187 break;
8188 case 'z':
8189 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8190 break;
8191 case 'Z':
8192 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8193 case '0':
8194 case 'S':
8195 case 'R':
8196 break;
8197 case 'X':
8198 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8199 break;
8200 case 'Y':
8201 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8202 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8203 break;
8204 default:
8205 internalError ();
8206 }
8207
8208 lastregno = regno;
8209 continue;
8210
8211 case 'P':
8212 if (strncmp (s, "$pc", 3) == 0)
8213 {
8214 s += 3;
8215 continue;
8216 }
8217 break;
8218
8219 case '<':
8220 case '>':
8221 case '[':
8222 case ']':
8223 case '4':
8224 case '5':
8225 case 'H':
8226 case 'W':
8227 case 'D':
8228 case 'j':
8229 case '8':
8230 case 'V':
8231 case 'C':
8232 case 'U':
8233 case 'k':
8234 case 'K':
8235 if (s[0] == '%'
8236 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8237 {
8238 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8239 and generate the appropriate reloc. If the text
8240 inside %gprel is not a symbol name with an
8241 optional offset, then we generate a normal reloc
8242 and will probably fail later. */
8243 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8244 if (imm_expr.X_op == O_symbol)
8245 {
8246 mips16_ext = true;
8247 imm_reloc = BFD_RELOC_MIPS16_GPREL;
8248 s = expr_end;
8249 ip->use_extend = true;
8250 ip->extend = 0;
8251 continue;
8252 }
8253 }
8254 else
8255 {
8256 /* Just pick up a normal expression. */
8257 my_getExpression (&imm_expr, s);
8258 }
8259
8260 if (imm_expr.X_op == O_register)
8261 {
8262 /* What we thought was an expression turned out to
8263 be a register. */
8264
8265 if (s[0] == '(' && args[1] == '(')
8266 {
8267 /* It looks like the expression was omitted
8268 before a register indirection, which means
8269 that the expression is implicitly zero. We
8270 still set up imm_expr, so that we handle
8271 explicit extensions correctly. */
8272 imm_expr.X_op = O_constant;
8273 imm_expr.X_add_number = 0;
8274 imm_reloc = (int) BFD_RELOC_UNUSED + c;
8275 continue;
8276 }
8277
8278 break;
8279 }
8280
8281 /* We need to relax this instruction. */
8282 imm_reloc = (int) BFD_RELOC_UNUSED + c;
8283 s = expr_end;
8284 continue;
8285
8286 case 'p':
8287 case 'q':
8288 case 'A':
8289 case 'B':
8290 case 'E':
8291 /* We use offset_reloc rather than imm_reloc for the PC
8292 relative operands. This lets macros with both
8293 immediate and address operands work correctly. */
8294 my_getExpression (&offset_expr, s);
8295
8296 if (offset_expr.X_op == O_register)
8297 break;
8298
8299 /* We need to relax this instruction. */
8300 offset_reloc = (int) BFD_RELOC_UNUSED + c;
8301 s = expr_end;
8302 continue;
8303
8304 case '6': /* break code */
8305 my_getExpression (&imm_expr, s);
8306 check_absolute_expr (ip, &imm_expr);
8307 if ((unsigned long) imm_expr.X_add_number > 63)
8308 {
8309 as_warn (_("Invalid value for `%s' (%lu)"),
8310 ip->insn_mo->name,
8311 (unsigned long) imm_expr.X_add_number);
8312 imm_expr.X_add_number &= 0x3f;
8313 }
8314 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8315 imm_expr.X_op = O_absent;
8316 s = expr_end;
8317 continue;
8318
8319 case 'a': /* 26 bit address */
8320 my_getExpression (&offset_expr, s);
8321 s = expr_end;
8322 offset_reloc = BFD_RELOC_MIPS16_JMP;
8323 ip->insn_opcode <<= 16;
8324 continue;
8325
8326 case 'l': /* register list for entry macro */
8327 case 'L': /* register list for exit macro */
8328 {
8329 int mask;
8330
8331 if (c == 'l')
8332 mask = 0;
8333 else
8334 mask = 7 << 3;
8335 while (*s != '\0')
8336 {
8337 int freg, reg1, reg2;
8338
8339 while (*s == ' ' || *s == ',')
8340 ++s;
8341 if (*s != '$')
8342 {
8343 as_bad (_("can't parse register list"));
8344 break;
8345 }
8346 ++s;
8347 if (*s != 'f')
8348 freg = 0;
8349 else
8350 {
8351 freg = 1;
8352 ++s;
8353 }
8354 reg1 = 0;
8355 while (isdigit ((unsigned char) *s))
8356 {
8357 reg1 *= 10;
8358 reg1 += *s - '0';
8359 ++s;
8360 }
8361 if (*s == ' ')
8362 ++s;
8363 if (*s != '-')
8364 reg2 = reg1;
8365 else
8366 {
8367 ++s;
8368 if (*s != '$')
8369 break;
8370 ++s;
8371 if (freg)
8372 {
8373 if (*s == 'f')
8374 ++s;
8375 else
8376 {
8377 as_bad (_("invalid register list"));
8378 break;
8379 }
8380 }
8381 reg2 = 0;
8382 while (isdigit ((unsigned char) *s))
8383 {
8384 reg2 *= 10;
8385 reg2 += *s - '0';
8386 ++s;
8387 }
8388 }
8389 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
8390 {
8391 mask &= ~ (7 << 3);
8392 mask |= 5 << 3;
8393 }
8394 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
8395 {
8396 mask &= ~ (7 << 3);
8397 mask |= 6 << 3;
8398 }
8399 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
8400 mask |= (reg2 - 3) << 3;
8401 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
8402 mask |= (reg2 - 15) << 1;
8403 else if (reg1 == 31 && reg2 == 31)
8404 mask |= 1;
8405 else
8406 {
8407 as_bad (_("invalid register list"));
8408 break;
8409 }
8410 }
8411 /* The mask is filled in in the opcode table for the
8412 benefit of the disassembler. We remove it before
8413 applying the actual mask. */
8414 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
8415 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
8416 }
8417 continue;
8418
8419 case 'e': /* extend code */
8420 my_getExpression (&imm_expr, s);
8421 check_absolute_expr (ip, &imm_expr);
8422 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
8423 {
8424 as_warn (_("Invalid value for `%s' (%lu)"),
8425 ip->insn_mo->name,
8426 (unsigned long) imm_expr.X_add_number);
8427 imm_expr.X_add_number &= 0x7ff;
8428 }
8429 ip->insn_opcode |= imm_expr.X_add_number;
8430 imm_expr.X_op = O_absent;
8431 s = expr_end;
8432 continue;
8433
8434 default:
8435 internalError ();
8436 }
8437 break;
8438 }
8439
8440 /* Args don't match. */
8441 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
8442 strcmp (insn->name, insn[1].name) == 0)
8443 {
8444 ++insn;
8445 s = argsstart;
8446 continue;
8447 }
8448
8449 insn_error = _("illegal operands");
8450
8451 return;
8452 }
8453 }
8454
8455 /* This structure holds information we know about a mips16 immediate
8456 argument type. */
8457
8458 struct mips16_immed_operand
8459 {
8460 /* The type code used in the argument string in the opcode table. */
8461 int type;
8462 /* The number of bits in the short form of the opcode. */
8463 int nbits;
8464 /* The number of bits in the extended form of the opcode. */
8465 int extbits;
8466 /* The amount by which the short form is shifted when it is used;
8467 for example, the sw instruction has a shift count of 2. */
8468 int shift;
8469 /* The amount by which the short form is shifted when it is stored
8470 into the instruction code. */
8471 int op_shift;
8472 /* Non-zero if the short form is unsigned. */
8473 int unsp;
8474 /* Non-zero if the extended form is unsigned. */
8475 int extu;
8476 /* Non-zero if the value is PC relative. */
8477 int pcrel;
8478 };
8479
8480 /* The mips16 immediate operand types. */
8481
8482 static const struct mips16_immed_operand mips16_immed_operands[] =
8483 {
8484 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8485 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8486 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
8487 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
8488 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
8489 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
8490 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
8491 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
8492 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
8493 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
8494 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
8495 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
8496 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
8497 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
8498 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
8499 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
8500 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8501 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
8502 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
8503 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
8504 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
8505 };
8506
8507 #define MIPS16_NUM_IMMED \
8508 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
8509
8510 /* Handle a mips16 instruction with an immediate value. This or's the
8511 small immediate value into *INSN. It sets *USE_EXTEND to indicate
8512 whether an extended value is needed; if one is needed, it sets
8513 *EXTEND to the value. The argument type is TYPE. The value is VAL.
8514 If SMALL is true, an unextended opcode was explicitly requested.
8515 If EXT is true, an extended opcode was explicitly requested. If
8516 WARN is true, warn if EXT does not match reality. */
8517
8518 static void
8519 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
8520 extend)
8521 char *file;
8522 unsigned int line;
8523 int type;
8524 offsetT val;
8525 boolean warn;
8526 boolean small;
8527 boolean ext;
8528 unsigned long *insn;
8529 boolean *use_extend;
8530 unsigned short *extend;
8531 {
8532 register const struct mips16_immed_operand *op;
8533 int mintiny, maxtiny;
8534 boolean needext;
8535
8536 op = mips16_immed_operands;
8537 while (op->type != type)
8538 {
8539 ++op;
8540 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
8541 }
8542
8543 if (op->unsp)
8544 {
8545 if (type == '<' || type == '>' || type == '[' || type == ']')
8546 {
8547 mintiny = 1;
8548 maxtiny = 1 << op->nbits;
8549 }
8550 else
8551 {
8552 mintiny = 0;
8553 maxtiny = (1 << op->nbits) - 1;
8554 }
8555 }
8556 else
8557 {
8558 mintiny = - (1 << (op->nbits - 1));
8559 maxtiny = (1 << (op->nbits - 1)) - 1;
8560 }
8561
8562 /* Branch offsets have an implicit 0 in the lowest bit. */
8563 if (type == 'p' || type == 'q')
8564 val /= 2;
8565
8566 if ((val & ((1 << op->shift) - 1)) != 0
8567 || val < (mintiny << op->shift)
8568 || val > (maxtiny << op->shift))
8569 needext = true;
8570 else
8571 needext = false;
8572
8573 if (warn && ext && ! needext)
8574 as_warn_where (file, line,
8575 _("extended operand requested but not required"));
8576 if (small && needext)
8577 as_bad_where (file, line, _("invalid unextended operand value"));
8578
8579 if (small || (! ext && ! needext))
8580 {
8581 int insnval;
8582
8583 *use_extend = false;
8584 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
8585 insnval <<= op->op_shift;
8586 *insn |= insnval;
8587 }
8588 else
8589 {
8590 long minext, maxext;
8591 int extval;
8592
8593 if (op->extu)
8594 {
8595 minext = 0;
8596 maxext = (1 << op->extbits) - 1;
8597 }
8598 else
8599 {
8600 minext = - (1 << (op->extbits - 1));
8601 maxext = (1 << (op->extbits - 1)) - 1;
8602 }
8603 if (val < minext || val > maxext)
8604 as_bad_where (file, line,
8605 _("operand value out of range for instruction"));
8606
8607 *use_extend = true;
8608 if (op->extbits == 16)
8609 {
8610 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
8611 val &= 0x1f;
8612 }
8613 else if (op->extbits == 15)
8614 {
8615 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
8616 val &= 0xf;
8617 }
8618 else
8619 {
8620 extval = ((val & 0x1f) << 6) | (val & 0x20);
8621 val = 0;
8622 }
8623
8624 *extend = (unsigned short) extval;
8625 *insn |= val;
8626 }
8627 }
8628 \f
8629
8630 static int
8631 my_getSmallExpression (ep, str)
8632 expressionS *ep;
8633 char *str;
8634 {
8635 char *sp;
8636 char *oldstr = str;
8637 int c = S_EX_NONE;
8638
8639 if (*str == ' ')
8640 str++;
8641 if (*str == '(')
8642 c = S_EX_NONE;
8643 else if (str[0] == '%'
8644 && tolower(str[1]) == 'l'
8645 && tolower(str[2]) == 'o'
8646 && str[3] == '(')
8647 {
8648 c = S_EX_LO;
8649 str += sizeof ("%lo(") - 2;
8650 }
8651 else if (str[0] == '%'
8652 && tolower(str[1]) == 'h'
8653 && tolower(str[2]) == 'i'
8654 && str[3] == '(')
8655 {
8656 c = S_EX_HI;
8657 str += sizeof ("%hi(") - 2;
8658 }
8659 else if (str[0] == '%'
8660 && tolower(str[1]) == 'h'
8661 && tolower(str[2]) == 'i'
8662 && tolower(str[3]) == 'g'
8663 && tolower(str[4]) == 'h'
8664 && tolower(str[5]) == 'e'
8665 && tolower(str[6]) == 'r'
8666 && str[7] == '(')
8667 {
8668 c = S_EX_HIGHER;
8669 str += sizeof ("%higher(") - 2;
8670 }
8671 else if (str[0] == '%'
8672 && tolower(str[1]) == 'h'
8673 && tolower(str[2]) == 'i'
8674 && tolower(str[3]) == 'g'
8675 && tolower(str[4]) == 'h'
8676 && tolower(str[5]) == 'e'
8677 && tolower(str[6]) == 's'
8678 && tolower(str[7]) == 't'
8679 && str[8] == '(')
8680 {
8681 c = S_EX_HIGHEST;
8682 str += sizeof ("%highest(") - 2;
8683 }
8684 /* currently unsupported */
8685 #if 0
8686 else if (str[0] == '%'
8687 && tolower(str[1]) == 'g'
8688 && tolower(str[2]) == 'p'
8689 && tolower(str[3]) == '_'
8690 && tolower(str[4]) == 'r'
8691 && tolower(str[5]) == 'e'
8692 && tolower(str[6]) == 'l'
8693 && str[7] == '(')
8694 {
8695 c = S_EX_GPREL;
8696 str += sizeof ("%gp_rel(") - 2;
8697 }
8698 else if (str[0] == '%'
8699 && tolower(str[1]) == 'n'
8700 && tolower(str[2]) == 'e'
8701 && tolower(str[3]) == 'g'
8702 && str[4] == '(')
8703 {
8704 c = S_EX_NEG;
8705 str += sizeof ("%neg(") - 2;
8706 }
8707 #endif
8708 else
8709 {
8710 my_getExpression (ep, str);
8711 return c;
8712 }
8713
8714 /*
8715 * A small expression may be followed by a base register.
8716 * Scan to the end of this operand, and then back over a possible
8717 * base register. Then scan the small expression up to that
8718 * point. (Based on code in sparc.c...)
8719 */
8720 for (sp = str; *sp && *sp != ','; sp++)
8721 ;
8722 if (sp - 4 >= str && sp[-1] == ')')
8723 {
8724 if (isdigit ((unsigned char) sp[-2]))
8725 {
8726 for (sp -= 3; sp >= str && isdigit ((unsigned char) *sp); sp--)
8727 ;
8728 if (*sp == '$' && sp > str && sp[-1] == '(')
8729 {
8730 sp--;
8731 goto do_it;
8732 }
8733 }
8734 else if (sp - 5 >= str
8735 && sp[-5] == '('
8736 && sp[-4] == '$'
8737 && ((sp[-3] == 'f' && sp[-2] == 'p')
8738 || (sp[-3] == 's' && sp[-2] == 'p')
8739 || (sp[-3] == 'g' && sp[-2] == 'p')
8740 || (sp[-3] == 'a' && sp[-2] == 't')))
8741 {
8742 sp -= 5;
8743 do_it:
8744 if (sp == str)
8745 {
8746 /* no expression means zero offset */
8747 if (c != S_EX_NONE)
8748 {
8749 /* %xx(reg) is an error */
8750 ep->X_op = O_absent;
8751 expr_end = oldstr;
8752 }
8753 else
8754 {
8755 ep->X_op = O_constant;
8756 expr_end = sp;
8757 }
8758 ep->X_add_symbol = NULL;
8759 ep->X_op_symbol = NULL;
8760 ep->X_add_number = 0;
8761 }
8762 else
8763 {
8764 *sp = '\0';
8765 my_getExpression (ep, str);
8766 *sp = '(';
8767 }
8768 return c;
8769 }
8770 }
8771 my_getExpression (ep, str);
8772
8773 /* => %highest, %higher, %hi, %lo, %gprel, %neg encountered */
8774 return c;
8775 }
8776
8777 static void
8778 my_getExpression (ep, str)
8779 expressionS *ep;
8780 char *str;
8781 {
8782 char *save_in;
8783 valueT val;
8784
8785 save_in = input_line_pointer;
8786 input_line_pointer = str;
8787 expression (ep);
8788 expr_end = input_line_pointer;
8789 input_line_pointer = save_in;
8790
8791 /* If we are in mips16 mode, and this is an expression based on `.',
8792 then we bump the value of the symbol by 1 since that is how other
8793 text symbols are handled. We don't bother to handle complex
8794 expressions, just `.' plus or minus a constant. */
8795 if (mips_opts.mips16
8796 && ep->X_op == O_symbol
8797 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
8798 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
8799 && symbol_get_frag (ep->X_add_symbol) == frag_now
8800 && symbol_constant_p (ep->X_add_symbol)
8801 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
8802 S_SET_VALUE (ep->X_add_symbol, val + 1);
8803 }
8804
8805 /* Turn a string in input_line_pointer into a floating point constant
8806 of type TYPE, and store the appropriate bytes in *LITP. The number
8807 of LITTLENUMS emitted is stored in *SIZEP. An error message is
8808 returned, or NULL on OK. */
8809
8810 char *
8811 md_atof (type, litP, sizeP)
8812 int type;
8813 char *litP;
8814 int *sizeP;
8815 {
8816 int prec;
8817 LITTLENUM_TYPE words[4];
8818 char *t;
8819 int i;
8820
8821 switch (type)
8822 {
8823 case 'f':
8824 prec = 2;
8825 break;
8826
8827 case 'd':
8828 prec = 4;
8829 break;
8830
8831 default:
8832 *sizeP = 0;
8833 return _("bad call to md_atof");
8834 }
8835
8836 t = atof_ieee (input_line_pointer, type, words);
8837 if (t)
8838 input_line_pointer = t;
8839
8840 *sizeP = prec * 2;
8841
8842 if (! target_big_endian)
8843 {
8844 for (i = prec - 1; i >= 0; i--)
8845 {
8846 md_number_to_chars (litP, (valueT) words[i], 2);
8847 litP += 2;
8848 }
8849 }
8850 else
8851 {
8852 for (i = 0; i < prec; i++)
8853 {
8854 md_number_to_chars (litP, (valueT) words[i], 2);
8855 litP += 2;
8856 }
8857 }
8858
8859 return NULL;
8860 }
8861
8862 void
8863 md_number_to_chars (buf, val, n)
8864 char *buf;
8865 valueT val;
8866 int n;
8867 {
8868 if (target_big_endian)
8869 number_to_chars_bigendian (buf, val, n);
8870 else
8871 number_to_chars_littleendian (buf, val, n);
8872 }
8873 \f
8874 CONST char *md_shortopts = "nO::g::G:";
8875
8876 struct option md_longopts[] =
8877 {
8878 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
8879 {"mips0", no_argument, NULL, OPTION_MIPS1},
8880 {"mips1", no_argument, NULL, OPTION_MIPS1},
8881 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
8882 {"mips2", no_argument, NULL, OPTION_MIPS2},
8883 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
8884 {"mips3", no_argument, NULL, OPTION_MIPS3},
8885 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
8886 {"mips4", no_argument, NULL, OPTION_MIPS4},
8887 #define OPTION_MCPU (OPTION_MD_BASE + 5)
8888 {"mcpu", required_argument, NULL, OPTION_MCPU},
8889 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 6)
8890 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
8891 #define OPTION_TRAP (OPTION_MD_BASE + 7)
8892 {"trap", no_argument, NULL, OPTION_TRAP},
8893 {"no-break", no_argument, NULL, OPTION_TRAP},
8894 #define OPTION_BREAK (OPTION_MD_BASE + 8)
8895 {"break", no_argument, NULL, OPTION_BREAK},
8896 {"no-trap", no_argument, NULL, OPTION_BREAK},
8897 #define OPTION_EB (OPTION_MD_BASE + 9)
8898 {"EB", no_argument, NULL, OPTION_EB},
8899 #define OPTION_EL (OPTION_MD_BASE + 10)
8900 {"EL", no_argument, NULL, OPTION_EL},
8901 #define OPTION_M4650 (OPTION_MD_BASE + 11)
8902 {"m4650", no_argument, NULL, OPTION_M4650},
8903 #define OPTION_NO_M4650 (OPTION_MD_BASE + 12)
8904 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
8905 #define OPTION_M4010 (OPTION_MD_BASE + 13)
8906 {"m4010", no_argument, NULL, OPTION_M4010},
8907 #define OPTION_NO_M4010 (OPTION_MD_BASE + 14)
8908 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
8909 #define OPTION_M4100 (OPTION_MD_BASE + 15)
8910 {"m4100", no_argument, NULL, OPTION_M4100},
8911 #define OPTION_NO_M4100 (OPTION_MD_BASE + 16)
8912 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
8913 #define OPTION_MIPS16 (OPTION_MD_BASE + 17)
8914 {"mips16", no_argument, NULL, OPTION_MIPS16},
8915 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 18)
8916 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
8917 #define OPTION_M3900 (OPTION_MD_BASE + 19)
8918 {"m3900", no_argument, NULL, OPTION_M3900},
8919 #define OPTION_NO_M3900 (OPTION_MD_BASE + 20)
8920 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
8921 #define OPTION_MABI (OPTION_MD_BASE + 21)
8922 {"mabi", required_argument, NULL, OPTION_MABI},
8923 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 22)
8924 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
8925 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 23)
8926 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
8927 #define OPTION_GP32 (OPTION_MD_BASE + 24)
8928 {"mgp32", no_argument, NULL, OPTION_GP32},
8929 #define OPTION_GP64 (OPTION_MD_BASE + 25)
8930 {"mgp64", no_argument, NULL, OPTION_GP64},
8931 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 26)
8932 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
8933 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 27)
8934 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
8935 #define OPTION_MIPS32 (OPTION_MD_BASE + 28)
8936 {"mips32", no_argument, NULL, OPTION_MIPS32},
8937 #define OPTION_MIPS5 (OPTION_MD_BASE + 29)
8938 {"mips5", no_argument, NULL, OPTION_MIPS5},
8939 #define OPTION_MIPS64 (OPTION_MD_BASE + 30)
8940 {"mips64", no_argument, NULL, OPTION_MIPS64},
8941 #define OPTION_MARCH (OPTION_MD_BASE + 31)
8942 {"march", required_argument, NULL, OPTION_MARCH},
8943 #define OPTION_MTUNE (OPTION_MD_BASE + 32)
8944 {"mtune", required_argument, NULL, OPTION_MTUNE},
8945 #define OPTION_FP32 (OPTION_MD_BASE + 33)
8946 {"mfp32", no_argument, NULL, OPTION_FP32},
8947 #ifdef OBJ_ELF
8948 #define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
8949 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
8950 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
8951 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
8952 #define OPTION_32 (OPTION_ELF_BASE + 3)
8953 #define OPTION_64 (OPTION_ELF_BASE + 4)
8954 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
8955 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
8956 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
8957 {"xgot", no_argument, NULL, OPTION_XGOT},
8958 {"32", no_argument, NULL, OPTION_32},
8959 {"64", no_argument, NULL, OPTION_64},
8960 #endif
8961
8962 {NULL, no_argument, NULL, 0}
8963 };
8964 size_t md_longopts_size = sizeof (md_longopts);
8965
8966 int
8967 md_parse_option (c, arg)
8968 int c;
8969 char *arg;
8970 {
8971 switch (c)
8972 {
8973 case OPTION_CONSTRUCT_FLOATS:
8974 mips_disable_float_construction = 0;
8975 break;
8976
8977 case OPTION_NO_CONSTRUCT_FLOATS:
8978 mips_disable_float_construction = 1;
8979 break;
8980
8981 case OPTION_TRAP:
8982 mips_trap = 1;
8983 break;
8984
8985 case OPTION_BREAK:
8986 mips_trap = 0;
8987 break;
8988
8989 case OPTION_EB:
8990 target_big_endian = 1;
8991 break;
8992
8993 case OPTION_EL:
8994 target_big_endian = 0;
8995 break;
8996
8997 case 'n':
8998 warn_nops = 1;
8999 break;
9000
9001 case 'O':
9002 if (arg && arg[1] == '0')
9003 mips_optimize = 1;
9004 else
9005 mips_optimize = 2;
9006 break;
9007
9008 case 'g':
9009 if (arg == NULL)
9010 mips_debug = 2;
9011 else
9012 mips_debug = atoi (arg);
9013 /* When the MIPS assembler sees -g or -g2, it does not do
9014 optimizations which limit full symbolic debugging. We take
9015 that to be equivalent to -O0. */
9016 if (mips_debug == 2)
9017 mips_optimize = 1;
9018 break;
9019
9020 case OPTION_MIPS1:
9021 mips_opts.isa = ISA_MIPS1;
9022 break;
9023
9024 case OPTION_MIPS2:
9025 mips_opts.isa = ISA_MIPS2;
9026 break;
9027
9028 case OPTION_MIPS3:
9029 mips_opts.isa = ISA_MIPS3;
9030 break;
9031
9032 case OPTION_MIPS4:
9033 mips_opts.isa = ISA_MIPS4;
9034 break;
9035
9036 case OPTION_MIPS5:
9037 mips_opts.isa = ISA_MIPS5;
9038 break;
9039
9040 case OPTION_MIPS32:
9041 mips_opts.isa = ISA_MIPS32;
9042 break;
9043
9044 case OPTION_MIPS64:
9045 mips_opts.isa = ISA_MIPS64;
9046 break;
9047
9048 case OPTION_MTUNE:
9049 case OPTION_MARCH:
9050 case OPTION_MCPU:
9051 {
9052 int cpu = CPU_UNKNOWN;
9053
9054 /* Identify the processor type. */
9055 if (strcasecmp (arg, "default") != 0)
9056 {
9057 const struct mips_cpu_info *ci;
9058
9059 ci = mips_cpu_info_from_name (arg);
9060 if (ci == NULL || ci->is_isa)
9061 {
9062 switch (c)
9063 {
9064 case OPTION_MTUNE:
9065 as_fatal (_("invalid architecture -mtune=%s"), arg);
9066 break;
9067 case OPTION_MARCH:
9068 as_fatal (_("invalid architecture -march=%s"), arg);
9069 break;
9070 case OPTION_MCPU:
9071 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9072 break;
9073 }
9074 }
9075 else
9076 cpu = ci->cpu;
9077 }
9078
9079 switch (c)
9080 {
9081 case OPTION_MTUNE:
9082 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9083 as_warn(_("A different -mtune= was already specified, is now "
9084 "-mtune=%s"), arg);
9085 mips_tune = cpu;
9086 break;
9087 case OPTION_MARCH:
9088 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9089 as_warn(_("A different -march= was already specified, is now "
9090 "-march=%s"), arg);
9091 mips_arch = cpu;
9092 break;
9093 case OPTION_MCPU:
9094 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9095 as_warn(_("A different -mcpu= was already specified, is now "
9096 "-mcpu=%s"), arg);
9097 mips_cpu = cpu;
9098 }
9099 }
9100 break;
9101
9102 case OPTION_M4650:
9103 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9104 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9105 as_warn(_("A different -march= or -mtune= was already specified, "
9106 "is now -m4650"));
9107 mips_arch = CPU_R4650;
9108 mips_tune = CPU_R4650;
9109 break;
9110
9111 case OPTION_NO_M4650:
9112 break;
9113
9114 case OPTION_M4010:
9115 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9116 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9117 as_warn(_("A different -march= or -mtune= was already specified, "
9118 "is now -m4010"));
9119 mips_arch = CPU_R4010;
9120 mips_tune = CPU_R4010;
9121 break;
9122
9123 case OPTION_NO_M4010:
9124 break;
9125
9126 case OPTION_M4100:
9127 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9128 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9129 as_warn(_("A different -march= or -mtune= was already specified, "
9130 "is now -m4100"));
9131 mips_arch = CPU_VR4100;
9132 mips_tune = CPU_VR4100;
9133 break;
9134
9135 case OPTION_NO_M4100:
9136 break;
9137
9138 case OPTION_M3900:
9139 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9140 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9141 as_warn(_("A different -march= or -mtune= was already specified, "
9142 "is now -m3900"));
9143 mips_arch = CPU_R3900;
9144 mips_tune = CPU_R3900;
9145 break;
9146
9147 case OPTION_NO_M3900:
9148 break;
9149
9150 case OPTION_MIPS16:
9151 mips_opts.mips16 = 1;
9152 mips_no_prev_insn (false);
9153 break;
9154
9155 case OPTION_NO_MIPS16:
9156 mips_opts.mips16 = 0;
9157 mips_no_prev_insn (false);
9158 break;
9159
9160 case OPTION_MEMBEDDED_PIC:
9161 mips_pic = EMBEDDED_PIC;
9162 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9163 {
9164 as_bad (_("-G may not be used with embedded PIC code"));
9165 return 0;
9166 }
9167 g_switch_value = 0x7fffffff;
9168 break;
9169
9170 #ifdef OBJ_ELF
9171 /* When generating ELF code, we permit -KPIC and -call_shared to
9172 select SVR4_PIC, and -non_shared to select no PIC. This is
9173 intended to be compatible with Irix 5. */
9174 case OPTION_CALL_SHARED:
9175 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9176 {
9177 as_bad (_("-call_shared is supported only for ELF format"));
9178 return 0;
9179 }
9180 mips_pic = SVR4_PIC;
9181 if (g_switch_seen && g_switch_value != 0)
9182 {
9183 as_bad (_("-G may not be used with SVR4 PIC code"));
9184 return 0;
9185 }
9186 g_switch_value = 0;
9187 break;
9188
9189 case OPTION_NON_SHARED:
9190 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9191 {
9192 as_bad (_("-non_shared is supported only for ELF format"));
9193 return 0;
9194 }
9195 mips_pic = NO_PIC;
9196 break;
9197
9198 /* The -xgot option tells the assembler to use 32 offsets when
9199 accessing the got in SVR4_PIC mode. It is for Irix
9200 compatibility. */
9201 case OPTION_XGOT:
9202 mips_big_got = 1;
9203 break;
9204 #endif /* OBJ_ELF */
9205
9206 case 'G':
9207 if (! USE_GLOBAL_POINTER_OPT)
9208 {
9209 as_bad (_("-G is not supported for this configuration"));
9210 return 0;
9211 }
9212 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9213 {
9214 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9215 return 0;
9216 }
9217 else
9218 g_switch_value = atoi (arg);
9219 g_switch_seen = 1;
9220 break;
9221
9222 #ifdef OBJ_ELF
9223 /* The -32 and -64 options tell the assembler to output the 32
9224 bit or the 64 bit MIPS ELF format. */
9225 case OPTION_32:
9226 mips_64 = 0;
9227 break;
9228
9229 case OPTION_64:
9230 {
9231 const char **list, **l;
9232
9233 list = bfd_target_list ();
9234 for (l = list; *l != NULL; l++)
9235 #ifdef TE_TMIPS
9236 /* This is traditional mips */
9237 if (strcmp (*l, "elf64-tradbigmips") == 0
9238 || strcmp (*l, "elf64-tradlittlemips") == 0)
9239 #else
9240 if (strcmp (*l, "elf64-bigmips") == 0
9241 || strcmp (*l, "elf64-littlemips") == 0)
9242 #endif
9243 break;
9244 if (*l == NULL)
9245 as_fatal (_("No compiled in support for 64 bit object file format"));
9246 free (list);
9247 mips_64 = 1;
9248 }
9249 break;
9250 #endif /* OBJ_ELF */
9251
9252 case OPTION_GP32:
9253 mips_gp32 = 1;
9254 mips_64 = 0;
9255
9256 /* We deliberately don't allow "-gp32" to set the MIPS_32BITMODE
9257 flag in object files because to do so would make it
9258 impossible to link with libraries compiled without "-gp32".
9259 This is unnecessarily restrictive.
9260
9261 We could solve this problem by adding "-gp32" multilibs to
9262 gcc, but to set this flag before gcc is built with such
9263 multilibs will break too many systems. */
9264
9265 #if 0
9266 mips_32bitmode = 1;
9267 #endif
9268 break;
9269
9270 case OPTION_GP64:
9271 mips_gp32 = 0;
9272 mips_64 = 1;
9273 #if 0
9274 mips_32bitmode = 0;
9275 #endif
9276 break;
9277
9278 case OPTION_FP32:
9279 mips_fp32 = 1;
9280 break;
9281
9282 case OPTION_MABI:
9283 if (strcmp (arg, "32") == 0
9284 || strcmp (arg, "n32") == 0
9285 || strcmp (arg, "64") == 0
9286 || strcmp (arg, "o64") == 0
9287 || strcmp (arg, "eabi") == 0)
9288 {
9289 mips_abi_string = arg;
9290 mips_32bit_abi = (strcmp (arg, "32") == 0);
9291 }
9292 break;
9293
9294 case OPTION_M7000_HILO_FIX:
9295 mips_7000_hilo_fix = true;
9296 break;
9297
9298 case OPTION_NO_M7000_HILO_FIX:
9299 mips_7000_hilo_fix = false;
9300 break;
9301
9302 default:
9303 return 0;
9304 }
9305
9306 return 1;
9307 }
9308
9309 static void
9310 show (stream, string, col_p, first_p)
9311 FILE *stream;
9312 char *string;
9313 int *col_p;
9314 int *first_p;
9315 {
9316 if (*first_p)
9317 {
9318 fprintf (stream, "%24s", "");
9319 *col_p = 24;
9320 }
9321 else
9322 {
9323 fprintf (stream, ", ");
9324 *col_p += 2;
9325 }
9326
9327 if (*col_p + strlen (string) > 72)
9328 {
9329 fprintf (stream, "\n%24s", "");
9330 *col_p = 24;
9331 }
9332
9333 fprintf (stream, "%s", string);
9334 *col_p += strlen (string);
9335
9336 *first_p = 0;
9337 }
9338
9339 void
9340 md_show_usage (stream)
9341 FILE *stream;
9342 {
9343 int column, first;
9344
9345 fprintf (stream, _("\
9346 MIPS options:\n\
9347 -membedded-pic generate embedded position independent code\n\
9348 -EB generate big endian output\n\
9349 -EL generate little endian output\n\
9350 -g, -g2 do not remove unneeded NOPs or swap branches\n\
9351 -G NUM allow referencing objects up to NUM bytes\n\
9352 implicitly with the gp register [default 8]\n"));
9353 fprintf (stream, _("\
9354 -mips1 generate MIPS ISA I instructions\n\
9355 -mips2 generate MIPS ISA II instructions\n\
9356 -mips3 generate MIPS ISA III instructions\n\
9357 -mips4 generate MIPS ISA IV instructions\n\
9358 -mips5 generate MIPS ISA V instructions\n\
9359 -mips32 generate MIPS32 ISA instructions\n\
9360 -mips64 generate MIPS64 ISA instructions\n\
9361 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
9362
9363 first = 1;
9364
9365 show (stream, "2000", &column, &first);
9366 show (stream, "3000", &column, &first);
9367 show (stream, "3900", &column, &first);
9368 show (stream, "4000", &column, &first);
9369 show (stream, "4010", &column, &first);
9370 show (stream, "4100", &column, &first);
9371 show (stream, "4111", &column, &first);
9372 show (stream, "4300", &column, &first);
9373 show (stream, "4400", &column, &first);
9374 show (stream, "4600", &column, &first);
9375 show (stream, "4650", &column, &first);
9376 show (stream, "5000", &column, &first);
9377 show (stream, "5200", &column, &first);
9378 show (stream, "5230", &column, &first);
9379 show (stream, "5231", &column, &first);
9380 show (stream, "5261", &column, &first);
9381 show (stream, "5721", &column, &first);
9382 show (stream, "6000", &column, &first);
9383 show (stream, "8000", &column, &first);
9384 show (stream, "10000", &column, &first);
9385 show (stream, "12000", &column, &first);
9386 show (stream, "sb-1", &column, &first);
9387 fputc ('\n', stream);
9388
9389 fprintf (stream, _("\
9390 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
9391 -no-mCPU don't generate code specific to CPU.\n\
9392 For -mCPU and -no-mCPU, CPU must be one of:\n"));
9393
9394 first = 1;
9395
9396 show (stream, "3900", &column, &first);
9397 show (stream, "4010", &column, &first);
9398 show (stream, "4100", &column, &first);
9399 show (stream, "4650", &column, &first);
9400 fputc ('\n', stream);
9401
9402 fprintf (stream, _("\
9403 -mips16 generate mips16 instructions\n\
9404 -no-mips16 do not generate mips16 instructions\n"));
9405 fprintf (stream, _("\
9406 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
9407 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
9408 -O0 remove unneeded NOPs, do not swap branches\n\
9409 -O remove unneeded NOPs and swap branches\n\
9410 -n warn about NOPs generated from macros\n\
9411 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
9412 --trap, --no-break trap exception on div by 0 and mult overflow\n\
9413 --break, --no-trap break exception on div by 0 and mult overflow\n"));
9414 #ifdef OBJ_ELF
9415 fprintf (stream, _("\
9416 -KPIC, -call_shared generate SVR4 position independent code\n\
9417 -non_shared do not generate position independent code\n\
9418 -xgot assume a 32 bit GOT\n\
9419 -32 create 32 bit object file (default)\n\
9420 -64 create 64 bit object file\n"));
9421 #endif
9422 }
9423 \f
9424 void
9425 mips_init_after_args ()
9426 {
9427 /* initialize opcodes */
9428 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
9429 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
9430 }
9431
9432 long
9433 md_pcrel_from (fixP)
9434 fixS *fixP;
9435 {
9436 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
9437 && fixP->fx_addsy != (symbolS *) NULL
9438 && ! S_IS_DEFINED (fixP->fx_addsy))
9439 {
9440 if (mips_pic == EMBEDDED_PIC)
9441 {
9442 /* This makes a branch to an undefined symbol be a branch to the
9443 current location. */
9444 return 4;
9445 }
9446 else
9447 {
9448 return 1;
9449 }
9450 }
9451
9452 /* return the address of the delay slot */
9453 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
9454 }
9455
9456 /* This is called before the symbol table is processed. In order to
9457 work with gcc when using mips-tfile, we must keep all local labels.
9458 However, in other cases, we want to discard them. If we were
9459 called with -g, but we didn't see any debugging information, it may
9460 mean that gcc is smuggling debugging information through to
9461 mips-tfile, in which case we must generate all local labels. */
9462
9463 void
9464 mips_frob_file_before_adjust ()
9465 {
9466 #ifndef NO_ECOFF_DEBUGGING
9467 if (ECOFF_DEBUGGING
9468 && mips_debug != 0
9469 && ! ecoff_debugging_seen)
9470 flag_keep_locals = 1;
9471 #endif
9472 }
9473
9474 /* Sort any unmatched HI16_S relocs so that they immediately precede
9475 the corresponding LO reloc. This is called before md_apply_fix and
9476 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
9477 explicit use of the %hi modifier. */
9478
9479 void
9480 mips_frob_file ()
9481 {
9482 struct mips_hi_fixup *l;
9483
9484 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
9485 {
9486 segment_info_type *seginfo;
9487 int pass;
9488
9489 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
9490
9491 /* Check quickly whether the next fixup happens to be a matching
9492 %lo. */
9493 if (l->fixp->fx_next != NULL
9494 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
9495 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
9496 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
9497 continue;
9498
9499 /* Look through the fixups for this segment for a matching %lo.
9500 When we find one, move the %hi just in front of it. We do
9501 this in two passes. In the first pass, we try to find a
9502 unique %lo. In the second pass, we permit multiple %hi
9503 relocs for a single %lo (this is a GNU extension). */
9504 seginfo = seg_info (l->seg);
9505 for (pass = 0; pass < 2; pass++)
9506 {
9507 fixS *f, *prev;
9508
9509 prev = NULL;
9510 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
9511 {
9512 /* Check whether this is a %lo fixup which matches l->fixp. */
9513 if (f->fx_r_type == BFD_RELOC_LO16
9514 && f->fx_addsy == l->fixp->fx_addsy
9515 && f->fx_offset == l->fixp->fx_offset
9516 && (pass == 1
9517 || prev == NULL
9518 || prev->fx_r_type != BFD_RELOC_HI16_S
9519 || prev->fx_addsy != f->fx_addsy
9520 || prev->fx_offset != f->fx_offset))
9521 {
9522 fixS **pf;
9523
9524 /* Move l->fixp before f. */
9525 for (pf = &seginfo->fix_root;
9526 *pf != l->fixp;
9527 pf = &(*pf)->fx_next)
9528 assert (*pf != NULL);
9529
9530 *pf = l->fixp->fx_next;
9531
9532 l->fixp->fx_next = f;
9533 if (prev == NULL)
9534 seginfo->fix_root = l->fixp;
9535 else
9536 prev->fx_next = l->fixp;
9537
9538 break;
9539 }
9540
9541 prev = f;
9542 }
9543
9544 if (f != NULL)
9545 break;
9546
9547 #if 0 /* GCC code motion plus incomplete dead code elimination
9548 can leave a %hi without a %lo. */
9549 if (pass == 1)
9550 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
9551 _("Unmatched %%hi reloc"));
9552 #endif
9553 }
9554 }
9555 }
9556
9557 /* When generating embedded PIC code we need to use a special
9558 relocation to represent the difference of two symbols in the .text
9559 section (switch tables use a difference of this sort). See
9560 include/coff/mips.h for details. This macro checks whether this
9561 fixup requires the special reloc. */
9562 #define SWITCH_TABLE(fixp) \
9563 ((fixp)->fx_r_type == BFD_RELOC_32 \
9564 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
9565 && (fixp)->fx_addsy != NULL \
9566 && (fixp)->fx_subsy != NULL \
9567 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
9568 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
9569
9570 /* When generating embedded PIC code we must keep all PC relative
9571 relocations, in case the linker has to relax a call. We also need
9572 to keep relocations for switch table entries. */
9573
9574 int
9575 mips_force_relocation (fixp)
9576 fixS *fixp;
9577 {
9578 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9579 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
9580 return 1;
9581
9582 return (mips_pic == EMBEDDED_PIC
9583 && (fixp->fx_pcrel
9584 || SWITCH_TABLE (fixp)
9585 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
9586 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
9587 }
9588
9589 /* Apply a fixup to the object file. */
9590
9591 int
9592 md_apply_fix (fixP, valueP)
9593 fixS *fixP;
9594 valueT *valueP;
9595 {
9596 unsigned char *buf;
9597 long insn;
9598 valueT value;
9599
9600 assert (fixP->fx_size == 4
9601 || fixP->fx_r_type == BFD_RELOC_16
9602 || fixP->fx_r_type == BFD_RELOC_64
9603 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
9604 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
9605
9606 value = *valueP;
9607
9608 /* If we aren't adjusting this fixup to be against the section
9609 symbol, we need to adjust the value. */
9610 #ifdef OBJ_ELF
9611 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
9612 {
9613 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
9614 || ((S_IS_WEAK (fixP->fx_addsy)
9615 || S_IS_EXTERN (fixP->fx_addsy))
9616 && !S_IS_COMMON (fixP->fx_addsy))
9617 || (symbol_used_in_reloc_p (fixP->fx_addsy)
9618 && (((bfd_get_section_flags (stdoutput,
9619 S_GET_SEGMENT (fixP->fx_addsy))
9620 & SEC_LINK_ONCE) != 0)
9621 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
9622 ".gnu.linkonce",
9623 sizeof (".gnu.linkonce") - 1))))
9624
9625 {
9626 valueT symval = S_GET_VALUE (fixP->fx_addsy);
9627 value -= symval;
9628 if (value != 0
9629 && ! fixP->fx_pcrel
9630 && fixP->fx_r_type != BFD_RELOC_MIPS_GPREL)
9631 {
9632 /* In this case, the bfd_install_relocation routine will
9633 incorrectly add the symbol value back in. We just want
9634 the addend to appear in the object file. */
9635 value -= symval;
9636
9637 /* Make sure the addend is still non-zero. If it became zero
9638 after the last operation, set it to a spurious value and
9639 subtract the same value from the object file's contents. */
9640 if (value == 0)
9641 {
9642 value = 8;
9643
9644 /* The in-place addends for LO16 relocations are signed;
9645 leave the matching HI16 in-place addends as zero. */
9646 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
9647 {
9648 reloc_howto_type *howto;
9649 bfd_vma contents, mask, field;
9650
9651 howto = bfd_reloc_type_lookup (stdoutput,
9652 fixP->fx_r_type);
9653
9654 contents = bfd_get_bits (fixP->fx_frag->fr_literal
9655 + fixP->fx_where,
9656 fixP->fx_size * 8,
9657 target_big_endian);
9658
9659 /* MASK has bits set where the relocation should go.
9660 FIELD is -value, shifted into the appropriate place
9661 for this relocation. */
9662 mask = 1 << (howto->bitsize - 1);
9663 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
9664 field = (-value >> howto->rightshift) << howto->bitpos;
9665
9666 bfd_put_bits ((field & mask) | (contents & ~mask),
9667 fixP->fx_frag->fr_literal + fixP->fx_where,
9668 fixP->fx_size * 8,
9669 target_big_endian);
9670 }
9671 }
9672 }
9673 }
9674
9675 /* This code was generated using trial and error and so is
9676 fragile and not trustworthy. If you change it, you should
9677 rerun the elf-rel, elf-rel2, and empic testcases and ensure
9678 they still pass. */
9679 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
9680 {
9681 value += fixP->fx_frag->fr_address + fixP->fx_where;
9682
9683 /* BFD's REL handling, for MIPS, is _very_ weird.
9684 This gives the right results, but it can't possibly
9685 be the way things are supposed to work. */
9686 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
9687 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
9688 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
9689 value += fixP->fx_frag->fr_address + fixP->fx_where;
9690 }
9691 }
9692 #endif
9693
9694 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc */
9695
9696 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
9697 fixP->fx_done = 1;
9698
9699 switch (fixP->fx_r_type)
9700 {
9701 case BFD_RELOC_MIPS_JMP:
9702 case BFD_RELOC_HI16:
9703 case BFD_RELOC_HI16_S:
9704 case BFD_RELOC_MIPS_GPREL:
9705 case BFD_RELOC_MIPS_LITERAL:
9706 case BFD_RELOC_MIPS_CALL16:
9707 case BFD_RELOC_MIPS_GOT16:
9708 case BFD_RELOC_MIPS_GPREL32:
9709 case BFD_RELOC_MIPS_GOT_HI16:
9710 case BFD_RELOC_MIPS_GOT_LO16:
9711 case BFD_RELOC_MIPS_CALL_HI16:
9712 case BFD_RELOC_MIPS_CALL_LO16:
9713 case BFD_RELOC_MIPS16_GPREL:
9714 if (fixP->fx_pcrel)
9715 as_bad_where (fixP->fx_file, fixP->fx_line,
9716 _("Invalid PC relative reloc"));
9717 /* Nothing needed to do. The value comes from the reloc entry */
9718 break;
9719
9720 case BFD_RELOC_MIPS16_JMP:
9721 /* We currently always generate a reloc against a symbol, which
9722 means that we don't want an addend even if the symbol is
9723 defined. */
9724 fixP->fx_addnumber = 0;
9725 break;
9726
9727 case BFD_RELOC_PCREL_HI16_S:
9728 /* The addend for this is tricky if it is internal, so we just
9729 do everything here rather than in bfd_install_relocation. */
9730 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9731 && !fixP->fx_done
9732 && value != 0)
9733 break;
9734 if (fixP->fx_addsy
9735 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
9736 {
9737 /* For an external symbol adjust by the address to make it
9738 pcrel_offset. We use the address of the RELLO reloc
9739 which follows this one. */
9740 value += (fixP->fx_next->fx_frag->fr_address
9741 + fixP->fx_next->fx_where);
9742 }
9743 value = ((value + 0x8000) >> 16) & 0xffff;
9744 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9745 if (target_big_endian)
9746 buf += 2;
9747 md_number_to_chars (buf, value, 2);
9748 break;
9749
9750 case BFD_RELOC_PCREL_LO16:
9751 /* The addend for this is tricky if it is internal, so we just
9752 do everything here rather than in bfd_install_relocation. */
9753 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9754 && !fixP->fx_done
9755 && value != 0)
9756 break;
9757 if (fixP->fx_addsy
9758 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
9759 value += fixP->fx_frag->fr_address + fixP->fx_where;
9760 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9761 if (target_big_endian)
9762 buf += 2;
9763 md_number_to_chars (buf, value, 2);
9764 break;
9765
9766 case BFD_RELOC_64:
9767 /* This is handled like BFD_RELOC_32, but we output a sign
9768 extended value if we are only 32 bits. */
9769 if (fixP->fx_done
9770 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9771 {
9772 if (8 <= sizeof (valueT))
9773 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9774 value, 8);
9775 else
9776 {
9777 long w1, w2;
9778 long hiv;
9779
9780 w1 = w2 = fixP->fx_where;
9781 if (target_big_endian)
9782 w1 += 4;
9783 else
9784 w2 += 4;
9785 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
9786 if ((value & 0x80000000) != 0)
9787 hiv = 0xffffffff;
9788 else
9789 hiv = 0;
9790 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
9791 }
9792 }
9793 break;
9794
9795 case BFD_RELOC_RVA:
9796 case BFD_RELOC_32:
9797 /* If we are deleting this reloc entry, we must fill in the
9798 value now. This can happen if we have a .word which is not
9799 resolved when it appears but is later defined. We also need
9800 to fill in the value if this is an embedded PIC switch table
9801 entry. */
9802 if (fixP->fx_done
9803 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
9804 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9805 value, 4);
9806 break;
9807
9808 case BFD_RELOC_16:
9809 /* If we are deleting this reloc entry, we must fill in the
9810 value now. */
9811 assert (fixP->fx_size == 2);
9812 if (fixP->fx_done)
9813 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
9814 value, 2);
9815 break;
9816
9817 case BFD_RELOC_LO16:
9818 /* When handling an embedded PIC switch statement, we can wind
9819 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
9820 if (fixP->fx_done)
9821 {
9822 if (value + 0x8000 > 0xffff)
9823 as_bad_where (fixP->fx_file, fixP->fx_line,
9824 _("relocation overflow"));
9825 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
9826 if (target_big_endian)
9827 buf += 2;
9828 md_number_to_chars (buf, value, 2);
9829 }
9830 break;
9831
9832 case BFD_RELOC_16_PCREL_S2:
9833 if ((value & 0x3) != 0)
9834 as_bad_where (fixP->fx_file, fixP->fx_line,
9835 _("Branch to odd address (%lx)"), (long) value);
9836
9837 /* Fall through. */
9838
9839 case BFD_RELOC_16_PCREL:
9840 /*
9841 * We need to save the bits in the instruction since fixup_segment()
9842 * might be deleting the relocation entry (i.e., a branch within
9843 * the current segment).
9844 */
9845 if (!fixP->fx_done && value != 0)
9846 break;
9847 /* If 'value' is zero, the remaining reloc code won't actually
9848 do the store, so it must be done here. This is probably
9849 a bug somewhere. */
9850 if (!fixP->fx_done)
9851 value -= fixP->fx_frag->fr_address + fixP->fx_where;
9852
9853 value = (offsetT) value >> 2;
9854
9855 /* update old instruction data */
9856 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
9857 if (target_big_endian)
9858 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
9859 else
9860 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
9861
9862 if (value + 0x8000 <= 0xffff)
9863 insn |= value & 0xffff;
9864 else
9865 {
9866 /* The branch offset is too large. If this is an
9867 unconditional branch, and we are not generating PIC code,
9868 we can convert it to an absolute jump instruction. */
9869 if (mips_pic == NO_PIC
9870 && fixP->fx_done
9871 && fixP->fx_frag->fr_address >= text_section->vma
9872 && (fixP->fx_frag->fr_address
9873 < text_section->vma + text_section->_raw_size)
9874 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
9875 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
9876 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
9877 {
9878 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
9879 insn = 0x0c000000; /* jal */
9880 else
9881 insn = 0x08000000; /* j */
9882 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
9883 fixP->fx_done = 0;
9884 fixP->fx_addsy = section_symbol (text_section);
9885 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
9886 }
9887 else
9888 {
9889 /* FIXME. It would be possible in principle to handle
9890 conditional branches which overflow. They could be
9891 transformed into a branch around a jump. This would
9892 require setting up variant frags for each different
9893 branch type. The native MIPS assembler attempts to
9894 handle these cases, but it appears to do it
9895 incorrectly. */
9896 as_bad_where (fixP->fx_file, fixP->fx_line,
9897 _("Branch out of range"));
9898 }
9899 }
9900
9901 md_number_to_chars ((char *) buf, (valueT) insn, 4);
9902 break;
9903
9904 case BFD_RELOC_VTABLE_INHERIT:
9905 fixP->fx_done = 0;
9906 if (fixP->fx_addsy
9907 && !S_IS_DEFINED (fixP->fx_addsy)
9908 && !S_IS_WEAK (fixP->fx_addsy))
9909 S_SET_WEAK (fixP->fx_addsy);
9910 break;
9911
9912 case BFD_RELOC_VTABLE_ENTRY:
9913 fixP->fx_done = 0;
9914 break;
9915
9916 default:
9917 internalError ();
9918 }
9919
9920 return 1;
9921 }
9922
9923 #if 0
9924 void
9925 printInsn (oc)
9926 unsigned long oc;
9927 {
9928 const struct mips_opcode *p;
9929 int treg, sreg, dreg, shamt;
9930 short imm;
9931 const char *args;
9932 int i;
9933
9934 for (i = 0; i < NUMOPCODES; ++i)
9935 {
9936 p = &mips_opcodes[i];
9937 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
9938 {
9939 printf ("%08lx %s\t", oc, p->name);
9940 treg = (oc >> 16) & 0x1f;
9941 sreg = (oc >> 21) & 0x1f;
9942 dreg = (oc >> 11) & 0x1f;
9943 shamt = (oc >> 6) & 0x1f;
9944 imm = oc;
9945 for (args = p->args;; ++args)
9946 {
9947 switch (*args)
9948 {
9949 case '\0':
9950 printf ("\n");
9951 break;
9952
9953 case ',':
9954 case '(':
9955 case ')':
9956 printf ("%c", *args);
9957 continue;
9958
9959 case 'r':
9960 assert (treg == sreg);
9961 printf ("$%d,$%d", treg, sreg);
9962 continue;
9963
9964 case 'd':
9965 case 'G':
9966 printf ("$%d", dreg);
9967 continue;
9968
9969 case 't':
9970 case 'E':
9971 printf ("$%d", treg);
9972 continue;
9973
9974 case 'k':
9975 printf ("0x%x", treg);
9976 continue;
9977
9978 case 'b':
9979 case 's':
9980 printf ("$%d", sreg);
9981 continue;
9982
9983 case 'a':
9984 printf ("0x%08lx", oc & 0x1ffffff);
9985 continue;
9986
9987 case 'i':
9988 case 'j':
9989 case 'o':
9990 case 'u':
9991 printf ("%d", imm);
9992 continue;
9993
9994 case '<':
9995 case '>':
9996 printf ("$%d", shamt);
9997 continue;
9998
9999 default:
10000 internalError ();
10001 }
10002 break;
10003 }
10004 return;
10005 }
10006 }
10007 printf (_("%08lx UNDEFINED\n"), oc);
10008 }
10009 #endif
10010
10011 static symbolS *
10012 get_symbol ()
10013 {
10014 int c;
10015 char *name;
10016 symbolS *p;
10017
10018 name = input_line_pointer;
10019 c = get_symbol_end ();
10020 p = (symbolS *) symbol_find_or_make (name);
10021 *input_line_pointer = c;
10022 return p;
10023 }
10024
10025 /* Align the current frag to a given power of two. The MIPS assembler
10026 also automatically adjusts any preceding label. */
10027
10028 static void
10029 mips_align (to, fill, label)
10030 int to;
10031 int fill;
10032 symbolS *label;
10033 {
10034 mips_emit_delays (false);
10035 frag_align (to, fill, 0);
10036 record_alignment (now_seg, to);
10037 if (label != NULL)
10038 {
10039 assert (S_GET_SEGMENT (label) == now_seg);
10040 symbol_set_frag (label, frag_now);
10041 S_SET_VALUE (label, (valueT) frag_now_fix ());
10042 }
10043 }
10044
10045 /* Align to a given power of two. .align 0 turns off the automatic
10046 alignment used by the data creating pseudo-ops. */
10047
10048 static void
10049 s_align (x)
10050 int x ATTRIBUTE_UNUSED;
10051 {
10052 register int temp;
10053 register long temp_fill;
10054 long max_alignment = 15;
10055
10056 /*
10057
10058 o Note that the assembler pulls down any immediately preceeding label
10059 to the aligned address.
10060 o It's not documented but auto alignment is reinstated by
10061 a .align pseudo instruction.
10062 o Note also that after auto alignment is turned off the mips assembler
10063 issues an error on attempt to assemble an improperly aligned data item.
10064 We don't.
10065
10066 */
10067
10068 temp = get_absolute_expression ();
10069 if (temp > max_alignment)
10070 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10071 else if (temp < 0)
10072 {
10073 as_warn (_("Alignment negative: 0 assumed."));
10074 temp = 0;
10075 }
10076 if (*input_line_pointer == ',')
10077 {
10078 input_line_pointer++;
10079 temp_fill = get_absolute_expression ();
10080 }
10081 else
10082 temp_fill = 0;
10083 if (temp)
10084 {
10085 auto_align = 1;
10086 mips_align (temp, (int) temp_fill,
10087 insn_labels != NULL ? insn_labels->label : NULL);
10088 }
10089 else
10090 {
10091 auto_align = 0;
10092 }
10093
10094 demand_empty_rest_of_line ();
10095 }
10096
10097 void
10098 mips_flush_pending_output ()
10099 {
10100 mips_emit_delays (false);
10101 mips_clear_insn_labels ();
10102 }
10103
10104 static void
10105 s_change_sec (sec)
10106 int sec;
10107 {
10108 segT seg;
10109
10110 /* When generating embedded PIC code, we only use the .text, .lit8,
10111 .sdata and .sbss sections. We change the .data and .rdata
10112 pseudo-ops to use .sdata. */
10113 if (mips_pic == EMBEDDED_PIC
10114 && (sec == 'd' || sec == 'r'))
10115 sec = 's';
10116
10117 #ifdef OBJ_ELF
10118 /* The ELF backend needs to know that we are changing sections, so
10119 that .previous works correctly. We could do something like check
10120 for a obj_section_change_hook macro, but that might be confusing
10121 as it would not be appropriate to use it in the section changing
10122 functions in read.c, since obj-elf.c intercepts those. FIXME:
10123 This should be cleaner, somehow. */
10124 obj_elf_section_change_hook ();
10125 #endif
10126
10127 mips_emit_delays (false);
10128 switch (sec)
10129 {
10130 case 't':
10131 s_text (0);
10132 break;
10133 case 'd':
10134 s_data (0);
10135 break;
10136 case 'b':
10137 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10138 demand_empty_rest_of_line ();
10139 break;
10140
10141 case 'r':
10142 if (USE_GLOBAL_POINTER_OPT)
10143 {
10144 seg = subseg_new (RDATA_SECTION_NAME,
10145 (subsegT) get_absolute_expression ());
10146 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10147 {
10148 bfd_set_section_flags (stdoutput, seg,
10149 (SEC_ALLOC
10150 | SEC_LOAD
10151 | SEC_READONLY
10152 | SEC_RELOC
10153 | SEC_DATA));
10154 if (strcmp (TARGET_OS, "elf") != 0)
10155 record_alignment (seg, 4);
10156 }
10157 demand_empty_rest_of_line ();
10158 }
10159 else
10160 {
10161 as_bad (_("No read only data section in this object file format"));
10162 demand_empty_rest_of_line ();
10163 return;
10164 }
10165 break;
10166
10167 case 's':
10168 if (USE_GLOBAL_POINTER_OPT)
10169 {
10170 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10171 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10172 {
10173 bfd_set_section_flags (stdoutput, seg,
10174 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10175 | SEC_DATA);
10176 if (strcmp (TARGET_OS, "elf") != 0)
10177 record_alignment (seg, 4);
10178 }
10179 demand_empty_rest_of_line ();
10180 break;
10181 }
10182 else
10183 {
10184 as_bad (_("Global pointers not supported; recompile -G 0"));
10185 demand_empty_rest_of_line ();
10186 return;
10187 }
10188 }
10189
10190 auto_align = 1;
10191 }
10192
10193 void
10194 mips_enable_auto_align ()
10195 {
10196 auto_align = 1;
10197 }
10198
10199 static void
10200 s_cons (log_size)
10201 int log_size;
10202 {
10203 symbolS *label;
10204
10205 label = insn_labels != NULL ? insn_labels->label : NULL;
10206 mips_emit_delays (false);
10207 if (log_size > 0 && auto_align)
10208 mips_align (log_size, 0, label);
10209 mips_clear_insn_labels ();
10210 cons (1 << log_size);
10211 }
10212
10213 static void
10214 s_float_cons (type)
10215 int type;
10216 {
10217 symbolS *label;
10218
10219 label = insn_labels != NULL ? insn_labels->label : NULL;
10220
10221 mips_emit_delays (false);
10222
10223 if (auto_align)
10224 {
10225 if (type == 'd')
10226 mips_align (3, 0, label);
10227 else
10228 mips_align (2, 0, label);
10229 }
10230
10231 mips_clear_insn_labels ();
10232
10233 float_cons (type);
10234 }
10235
10236 /* Handle .globl. We need to override it because on Irix 5 you are
10237 permitted to say
10238 .globl foo .text
10239 where foo is an undefined symbol, to mean that foo should be
10240 considered to be the address of a function. */
10241
10242 static void
10243 s_mips_globl (x)
10244 int x ATTRIBUTE_UNUSED;
10245 {
10246 char *name;
10247 int c;
10248 symbolS *symbolP;
10249 flagword flag;
10250
10251 name = input_line_pointer;
10252 c = get_symbol_end ();
10253 symbolP = symbol_find_or_make (name);
10254 *input_line_pointer = c;
10255 SKIP_WHITESPACE ();
10256
10257 /* On Irix 5, every global symbol that is not explicitly labelled as
10258 being a function is apparently labelled as being an object. */
10259 flag = BSF_OBJECT;
10260
10261 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10262 {
10263 char *secname;
10264 asection *sec;
10265
10266 secname = input_line_pointer;
10267 c = get_symbol_end ();
10268 sec = bfd_get_section_by_name (stdoutput, secname);
10269 if (sec == NULL)
10270 as_bad (_("%s: no such section"), secname);
10271 *input_line_pointer = c;
10272
10273 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
10274 flag = BSF_FUNCTION;
10275 }
10276
10277 symbol_get_bfdsym (symbolP)->flags |= flag;
10278
10279 S_SET_EXTERNAL (symbolP);
10280 demand_empty_rest_of_line ();
10281 }
10282
10283 static void
10284 s_option (x)
10285 int x ATTRIBUTE_UNUSED;
10286 {
10287 char *opt;
10288 char c;
10289
10290 opt = input_line_pointer;
10291 c = get_symbol_end ();
10292
10293 if (*opt == 'O')
10294 {
10295 /* FIXME: What does this mean? */
10296 }
10297 else if (strncmp (opt, "pic", 3) == 0)
10298 {
10299 int i;
10300
10301 i = atoi (opt + 3);
10302 if (i == 0)
10303 mips_pic = NO_PIC;
10304 else if (i == 2)
10305 mips_pic = SVR4_PIC;
10306 else
10307 as_bad (_(".option pic%d not supported"), i);
10308
10309 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
10310 {
10311 if (g_switch_seen && g_switch_value != 0)
10312 as_warn (_("-G may not be used with SVR4 PIC code"));
10313 g_switch_value = 0;
10314 bfd_set_gp_size (stdoutput, 0);
10315 }
10316 }
10317 else
10318 as_warn (_("Unrecognized option \"%s\""), opt);
10319
10320 *input_line_pointer = c;
10321 demand_empty_rest_of_line ();
10322 }
10323
10324 /* This structure is used to hold a stack of .set values. */
10325
10326 struct mips_option_stack
10327 {
10328 struct mips_option_stack *next;
10329 struct mips_set_options options;
10330 };
10331
10332 static struct mips_option_stack *mips_opts_stack;
10333
10334 /* Handle the .set pseudo-op. */
10335
10336 static void
10337 s_mipsset (x)
10338 int x ATTRIBUTE_UNUSED;
10339 {
10340 char *name = input_line_pointer, ch;
10341
10342 while (!is_end_of_line[(unsigned char) *input_line_pointer])
10343 input_line_pointer++;
10344 ch = *input_line_pointer;
10345 *input_line_pointer = '\0';
10346
10347 if (strcmp (name, "reorder") == 0)
10348 {
10349 if (mips_opts.noreorder && prev_nop_frag != NULL)
10350 {
10351 /* If we still have pending nops, we can discard them. The
10352 usual nop handling will insert any that are still
10353 needed. */
10354 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10355 * (mips_opts.mips16 ? 2 : 4));
10356 prev_nop_frag = NULL;
10357 }
10358 mips_opts.noreorder = 0;
10359 }
10360 else if (strcmp (name, "noreorder") == 0)
10361 {
10362 mips_emit_delays (true);
10363 mips_opts.noreorder = 1;
10364 mips_any_noreorder = 1;
10365 }
10366 else if (strcmp (name, "at") == 0)
10367 {
10368 mips_opts.noat = 0;
10369 }
10370 else if (strcmp (name, "noat") == 0)
10371 {
10372 mips_opts.noat = 1;
10373 }
10374 else if (strcmp (name, "macro") == 0)
10375 {
10376 mips_opts.warn_about_macros = 0;
10377 }
10378 else if (strcmp (name, "nomacro") == 0)
10379 {
10380 if (mips_opts.noreorder == 0)
10381 as_bad (_("`noreorder' must be set before `nomacro'"));
10382 mips_opts.warn_about_macros = 1;
10383 }
10384 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
10385 {
10386 mips_opts.nomove = 0;
10387 }
10388 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
10389 {
10390 mips_opts.nomove = 1;
10391 }
10392 else if (strcmp (name, "bopt") == 0)
10393 {
10394 mips_opts.nobopt = 0;
10395 }
10396 else if (strcmp (name, "nobopt") == 0)
10397 {
10398 mips_opts.nobopt = 1;
10399 }
10400 else if (strcmp (name, "mips16") == 0
10401 || strcmp (name, "MIPS-16") == 0)
10402 mips_opts.mips16 = 1;
10403 else if (strcmp (name, "nomips16") == 0
10404 || strcmp (name, "noMIPS-16") == 0)
10405 mips_opts.mips16 = 0;
10406 else if (strncmp (name, "mips", 4) == 0)
10407 {
10408 int isa;
10409 static int saved_mips_gp32;
10410 static int saved_mips_fp32;
10411 static int saved_mips_32bit_abi;
10412 static int is_saved;
10413
10414 /* Permit the user to change the ISA on the fly. Needless to
10415 say, misuse can cause serious problems. */
10416 isa = atoi (name + 4);
10417 switch (isa)
10418 {
10419 case 0:
10420 mips_gp32 = saved_mips_gp32;
10421 mips_fp32 = saved_mips_fp32;
10422 mips_32bit_abi = saved_mips_32bit_abi;
10423 is_saved = 0;
10424 break;
10425 case 1:
10426 case 2:
10427 case 32:
10428 if (! is_saved)
10429 {
10430 saved_mips_gp32 = mips_gp32;
10431 saved_mips_fp32 = mips_fp32;
10432 saved_mips_32bit_abi = mips_32bit_abi;
10433 }
10434 mips_gp32 = 1;
10435 mips_fp32 = 1;
10436 is_saved = 1;
10437 break;
10438 case 3:
10439 case 4:
10440 case 5:
10441 case 64:
10442 if (! is_saved)
10443 {
10444 saved_mips_gp32 = mips_gp32;
10445 saved_mips_fp32 = mips_fp32;
10446 saved_mips_32bit_abi = mips_32bit_abi;
10447 }
10448 mips_gp32 = 0;
10449 mips_fp32 = 0;
10450 mips_32bit_abi = 0;
10451 is_saved = 1;
10452 break;
10453 default:
10454 as_bad (_("unknown ISA level"));
10455 break;
10456 }
10457
10458 switch (isa)
10459 {
10460 case 0: mips_opts.isa = file_mips_isa; break;
10461 case 1: mips_opts.isa = ISA_MIPS1; break;
10462 case 2: mips_opts.isa = ISA_MIPS2; break;
10463 case 3: mips_opts.isa = ISA_MIPS3; break;
10464 case 4: mips_opts.isa = ISA_MIPS4; break;
10465 case 5: mips_opts.isa = ISA_MIPS5; break;
10466 case 32: mips_opts.isa = ISA_MIPS32; break;
10467 case 64: mips_opts.isa = ISA_MIPS64; break;
10468 default: as_bad (_("unknown ISA level")); break;
10469 }
10470 }
10471 else if (strcmp (name, "autoextend") == 0)
10472 mips_opts.noautoextend = 0;
10473 else if (strcmp (name, "noautoextend") == 0)
10474 mips_opts.noautoextend = 1;
10475 else if (strcmp (name, "push") == 0)
10476 {
10477 struct mips_option_stack *s;
10478
10479 s = (struct mips_option_stack *) xmalloc (sizeof *s);
10480 s->next = mips_opts_stack;
10481 s->options = mips_opts;
10482 mips_opts_stack = s;
10483 }
10484 else if (strcmp (name, "pop") == 0)
10485 {
10486 struct mips_option_stack *s;
10487
10488 s = mips_opts_stack;
10489 if (s == NULL)
10490 as_bad (_(".set pop with no .set push"));
10491 else
10492 {
10493 /* If we're changing the reorder mode we need to handle
10494 delay slots correctly. */
10495 if (s->options.noreorder && ! mips_opts.noreorder)
10496 mips_emit_delays (true);
10497 else if (! s->options.noreorder && mips_opts.noreorder)
10498 {
10499 if (prev_nop_frag != NULL)
10500 {
10501 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
10502 * (mips_opts.mips16 ? 2 : 4));
10503 prev_nop_frag = NULL;
10504 }
10505 }
10506
10507 mips_opts = s->options;
10508 mips_opts_stack = s->next;
10509 free (s);
10510 }
10511 }
10512 else
10513 {
10514 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
10515 }
10516 *input_line_pointer = ch;
10517 demand_empty_rest_of_line ();
10518 }
10519
10520 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
10521 .option pic2. It means to generate SVR4 PIC calls. */
10522
10523 static void
10524 s_abicalls (ignore)
10525 int ignore ATTRIBUTE_UNUSED;
10526 {
10527 mips_pic = SVR4_PIC;
10528 if (USE_GLOBAL_POINTER_OPT)
10529 {
10530 if (g_switch_seen && g_switch_value != 0)
10531 as_warn (_("-G may not be used with SVR4 PIC code"));
10532 g_switch_value = 0;
10533 }
10534 bfd_set_gp_size (stdoutput, 0);
10535 demand_empty_rest_of_line ();
10536 }
10537
10538 /* Handle the .cpload pseudo-op. This is used when generating SVR4
10539 PIC code. It sets the $gp register for the function based on the
10540 function address, which is in the register named in the argument.
10541 This uses a relocation against _gp_disp, which is handled specially
10542 by the linker. The result is:
10543 lui $gp,%hi(_gp_disp)
10544 addiu $gp,$gp,%lo(_gp_disp)
10545 addu $gp,$gp,.cpload argument
10546 The .cpload argument is normally $25 == $t9. */
10547
10548 static void
10549 s_cpload (ignore)
10550 int ignore ATTRIBUTE_UNUSED;
10551 {
10552 expressionS ex;
10553 int icnt = 0;
10554
10555 /* If we are not generating SVR4 PIC code, .cpload is ignored. */
10556 if (mips_pic != SVR4_PIC)
10557 {
10558 s_ignore (0);
10559 return;
10560 }
10561
10562 /* .cpload should be a in .set noreorder section. */
10563 if (mips_opts.noreorder == 0)
10564 as_warn (_(".cpload not in noreorder section"));
10565
10566 ex.X_op = O_symbol;
10567 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
10568 ex.X_op_symbol = NULL;
10569 ex.X_add_number = 0;
10570
10571 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
10572 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
10573
10574 macro_build_lui ((char *) NULL, &icnt, &ex, GP);
10575 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
10576 (int) BFD_RELOC_LO16);
10577
10578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
10579 GP, GP, tc_get_register (0));
10580
10581 demand_empty_rest_of_line ();
10582 }
10583
10584 /* Handle the .cprestore pseudo-op. This stores $gp into a given
10585 offset from $sp. The offset is remembered, and after making a PIC
10586 call $gp is restored from that location. */
10587
10588 static void
10589 s_cprestore (ignore)
10590 int ignore ATTRIBUTE_UNUSED;
10591 {
10592 expressionS ex;
10593 int icnt = 0;
10594
10595 /* If we are not generating SVR4 PIC code, .cprestore is ignored. */
10596 if (mips_pic != SVR4_PIC)
10597 {
10598 s_ignore (0);
10599 return;
10600 }
10601
10602 mips_cprestore_offset = get_absolute_expression ();
10603
10604 ex.X_op = O_constant;
10605 ex.X_add_symbol = NULL;
10606 ex.X_op_symbol = NULL;
10607 ex.X_add_number = mips_cprestore_offset;
10608
10609 macro_build ((char *) NULL, &icnt, &ex,
10610 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
10611 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
10612
10613 demand_empty_rest_of_line ();
10614 }
10615
10616 /* Handle the .gpword pseudo-op. This is used when generating PIC
10617 code. It generates a 32 bit GP relative reloc. */
10618
10619 static void
10620 s_gpword (ignore)
10621 int ignore ATTRIBUTE_UNUSED;
10622 {
10623 symbolS *label;
10624 expressionS ex;
10625 char *p;
10626
10627 /* When not generating PIC code, this is treated as .word. */
10628 if (mips_pic != SVR4_PIC)
10629 {
10630 s_cons (2);
10631 return;
10632 }
10633
10634 label = insn_labels != NULL ? insn_labels->label : NULL;
10635 mips_emit_delays (true);
10636 if (auto_align)
10637 mips_align (2, 0, label);
10638 mips_clear_insn_labels ();
10639
10640 expression (&ex);
10641
10642 if (ex.X_op != O_symbol || ex.X_add_number != 0)
10643 {
10644 as_bad (_("Unsupported use of .gpword"));
10645 ignore_rest_of_line ();
10646 }
10647
10648 p = frag_more (4);
10649 md_number_to_chars (p, (valueT) 0, 4);
10650 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
10651 BFD_RELOC_MIPS_GPREL32);
10652
10653 demand_empty_rest_of_line ();
10654 }
10655
10656 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
10657 tables in SVR4 PIC code. */
10658
10659 static void
10660 s_cpadd (ignore)
10661 int ignore ATTRIBUTE_UNUSED;
10662 {
10663 int icnt = 0;
10664 int reg;
10665
10666 /* This is ignored when not generating SVR4 PIC code. */
10667 if (mips_pic != SVR4_PIC)
10668 {
10669 s_ignore (0);
10670 return;
10671 }
10672
10673 /* Add $gp to the register named as an argument. */
10674 reg = tc_get_register (0);
10675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
10676 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
10677 "d,v,t", reg, reg, GP);
10678
10679 demand_empty_rest_of_line ();
10680 }
10681
10682 /* Handle the .insn pseudo-op. This marks instruction labels in
10683 mips16 mode. This permits the linker to handle them specially,
10684 such as generating jalx instructions when needed. We also make
10685 them odd for the duration of the assembly, in order to generate the
10686 right sort of code. We will make them even in the adjust_symtab
10687 routine, while leaving them marked. This is convenient for the
10688 debugger and the disassembler. The linker knows to make them odd
10689 again. */
10690
10691 static void
10692 s_insn (ignore)
10693 int ignore ATTRIBUTE_UNUSED;
10694 {
10695 if (mips_opts.mips16)
10696 mips16_mark_labels ();
10697
10698 demand_empty_rest_of_line ();
10699 }
10700
10701 /* Handle a .stabn directive. We need these in order to mark a label
10702 as being a mips16 text label correctly. Sometimes the compiler
10703 will emit a label, followed by a .stabn, and then switch sections.
10704 If the label and .stabn are in mips16 mode, then the label is
10705 really a mips16 text label. */
10706
10707 static void
10708 s_mips_stab (type)
10709 int type;
10710 {
10711 if (type == 'n' && mips_opts.mips16)
10712 mips16_mark_labels ();
10713
10714 s_stab (type);
10715 }
10716
10717 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
10718 */
10719
10720 static void
10721 s_mips_weakext (ignore)
10722 int ignore ATTRIBUTE_UNUSED;
10723 {
10724 char *name;
10725 int c;
10726 symbolS *symbolP;
10727 expressionS exp;
10728
10729 name = input_line_pointer;
10730 c = get_symbol_end ();
10731 symbolP = symbol_find_or_make (name);
10732 S_SET_WEAK (symbolP);
10733 *input_line_pointer = c;
10734
10735 SKIP_WHITESPACE ();
10736
10737 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10738 {
10739 if (S_IS_DEFINED (symbolP))
10740 {
10741 as_bad ("Ignoring attempt to redefine symbol `%s'.",
10742 S_GET_NAME (symbolP));
10743 ignore_rest_of_line ();
10744 return;
10745 }
10746
10747 if (*input_line_pointer == ',')
10748 {
10749 ++input_line_pointer;
10750 SKIP_WHITESPACE ();
10751 }
10752
10753 expression (&exp);
10754 if (exp.X_op != O_symbol)
10755 {
10756 as_bad ("bad .weakext directive");
10757 ignore_rest_of_line();
10758 return;
10759 }
10760 symbol_set_value_expression (symbolP, &exp);
10761 }
10762
10763 demand_empty_rest_of_line ();
10764 }
10765
10766 /* Parse a register string into a number. Called from the ECOFF code
10767 to parse .frame. The argument is non-zero if this is the frame
10768 register, so that we can record it in mips_frame_reg. */
10769
10770 int
10771 tc_get_register (frame)
10772 int frame;
10773 {
10774 int reg;
10775
10776 SKIP_WHITESPACE ();
10777 if (*input_line_pointer++ != '$')
10778 {
10779 as_warn (_("expected `$'"));
10780 reg = 0;
10781 }
10782 else if (isdigit ((unsigned char) *input_line_pointer))
10783 {
10784 reg = get_absolute_expression ();
10785 if (reg < 0 || reg >= 32)
10786 {
10787 as_warn (_("Bad register number"));
10788 reg = 0;
10789 }
10790 }
10791 else
10792 {
10793 if (strncmp (input_line_pointer, "fp", 2) == 0)
10794 reg = FP;
10795 else if (strncmp (input_line_pointer, "sp", 2) == 0)
10796 reg = SP;
10797 else if (strncmp (input_line_pointer, "gp", 2) == 0)
10798 reg = GP;
10799 else if (strncmp (input_line_pointer, "at", 2) == 0)
10800 reg = AT;
10801 else
10802 {
10803 as_warn (_("Unrecognized register name"));
10804 reg = 0;
10805 }
10806 input_line_pointer += 2;
10807 }
10808 if (frame)
10809 mips_frame_reg = reg != 0 ? reg : SP;
10810 return reg;
10811 }
10812
10813 valueT
10814 md_section_align (seg, addr)
10815 asection *seg;
10816 valueT addr;
10817 {
10818 int align = bfd_get_section_alignment (stdoutput, seg);
10819
10820 #ifdef OBJ_ELF
10821 /* We don't need to align ELF sections to the full alignment.
10822 However, Irix 5 may prefer that we align them at least to a 16
10823 byte boundary. We don't bother to align the sections if we are
10824 targeted for an embedded system. */
10825 if (strcmp (TARGET_OS, "elf") == 0)
10826 return addr;
10827 if (align > 4)
10828 align = 4;
10829 #endif
10830
10831 return ((addr + (1 << align) - 1) & (-1 << align));
10832 }
10833
10834 /* Utility routine, called from above as well. If called while the
10835 input file is still being read, it's only an approximation. (For
10836 example, a symbol may later become defined which appeared to be
10837 undefined earlier.) */
10838
10839 static int
10840 nopic_need_relax (sym, before_relaxing)
10841 symbolS *sym;
10842 int before_relaxing;
10843 {
10844 if (sym == 0)
10845 return 0;
10846
10847 if (USE_GLOBAL_POINTER_OPT)
10848 {
10849 const char *symname;
10850 int change;
10851
10852 /* Find out whether this symbol can be referenced off the GP
10853 register. It can be if it is smaller than the -G size or if
10854 it is in the .sdata or .sbss section. Certain symbols can
10855 not be referenced off the GP, although it appears as though
10856 they can. */
10857 symname = S_GET_NAME (sym);
10858 if (symname != (const char *) NULL
10859 && (strcmp (symname, "eprol") == 0
10860 || strcmp (symname, "etext") == 0
10861 || strcmp (symname, "_gp") == 0
10862 || strcmp (symname, "edata") == 0
10863 || strcmp (symname, "_fbss") == 0
10864 || strcmp (symname, "_fdata") == 0
10865 || strcmp (symname, "_ftext") == 0
10866 || strcmp (symname, "end") == 0
10867 || strcmp (symname, "_gp_disp") == 0))
10868 change = 1;
10869 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
10870 && (0
10871 #ifndef NO_ECOFF_DEBUGGING
10872 || (symbol_get_obj (sym)->ecoff_extern_size != 0
10873 && (symbol_get_obj (sym)->ecoff_extern_size
10874 <= g_switch_value))
10875 #endif
10876 /* We must defer this decision until after the whole
10877 file has been read, since there might be a .extern
10878 after the first use of this symbol. */
10879 || (before_relaxing
10880 #ifndef NO_ECOFF_DEBUGGING
10881 && symbol_get_obj (sym)->ecoff_extern_size == 0
10882 #endif
10883 && S_GET_VALUE (sym) == 0)
10884 || (S_GET_VALUE (sym) != 0
10885 && S_GET_VALUE (sym) <= g_switch_value)))
10886 change = 0;
10887 else
10888 {
10889 const char *segname;
10890
10891 segname = segment_name (S_GET_SEGMENT (sym));
10892 assert (strcmp (segname, ".lit8") != 0
10893 && strcmp (segname, ".lit4") != 0);
10894 change = (strcmp (segname, ".sdata") != 0
10895 && strcmp (segname, ".sbss") != 0
10896 && strncmp (segname, ".sdata.", 7) != 0
10897 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
10898 }
10899 return change;
10900 }
10901 else
10902 /* We are not optimizing for the GP register. */
10903 return 1;
10904 }
10905
10906 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
10907 extended opcode. SEC is the section the frag is in. */
10908
10909 static int
10910 mips16_extended_frag (fragp, sec, stretch)
10911 fragS *fragp;
10912 asection *sec;
10913 long stretch;
10914 {
10915 int type;
10916 register const struct mips16_immed_operand *op;
10917 offsetT val;
10918 int mintiny, maxtiny;
10919 segT symsec;
10920 fragS *sym_frag;
10921
10922 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
10923 return 0;
10924 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
10925 return 1;
10926
10927 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
10928 op = mips16_immed_operands;
10929 while (op->type != type)
10930 {
10931 ++op;
10932 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10933 }
10934
10935 if (op->unsp)
10936 {
10937 if (type == '<' || type == '>' || type == '[' || type == ']')
10938 {
10939 mintiny = 1;
10940 maxtiny = 1 << op->nbits;
10941 }
10942 else
10943 {
10944 mintiny = 0;
10945 maxtiny = (1 << op->nbits) - 1;
10946 }
10947 }
10948 else
10949 {
10950 mintiny = - (1 << (op->nbits - 1));
10951 maxtiny = (1 << (op->nbits - 1)) - 1;
10952 }
10953
10954 sym_frag = symbol_get_frag (fragp->fr_symbol);
10955 val = S_GET_VALUE (fragp->fr_symbol);
10956 symsec = S_GET_SEGMENT (fragp->fr_symbol);
10957
10958 if (op->pcrel)
10959 {
10960 addressT addr;
10961
10962 /* We won't have the section when we are called from
10963 mips_relax_frag. However, we will always have been called
10964 from md_estimate_size_before_relax first. If this is a
10965 branch to a different section, we mark it as such. If SEC is
10966 NULL, and the frag is not marked, then it must be a branch to
10967 the same section. */
10968 if (sec == NULL)
10969 {
10970 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
10971 return 1;
10972 }
10973 else
10974 {
10975 /* Must have been called from md_estimate_size_before_relax. */
10976 if (symsec != sec)
10977 {
10978 fragp->fr_subtype =
10979 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
10980
10981 /* FIXME: We should support this, and let the linker
10982 catch branches and loads that are out of range. */
10983 as_bad_where (fragp->fr_file, fragp->fr_line,
10984 _("unsupported PC relative reference to different section"));
10985
10986 return 1;
10987 }
10988 if (fragp != sym_frag && sym_frag->fr_address == 0)
10989 /* Assume non-extended on the first relaxation pass.
10990 The address we have calculated will be bogus if this is
10991 a forward branch to another frag, as the forward frag
10992 will have fr_address == 0. */
10993 return 0;
10994 }
10995
10996 /* In this case, we know for sure that the symbol fragment is in
10997 the same section. If the relax_marker of the symbol fragment
10998 differs from the relax_marker of this fragment, we have not
10999 yet adjusted the symbol fragment fr_address. We want to add
11000 in STRETCH in order to get a better estimate of the address.
11001 This particularly matters because of the shift bits. */
11002 if (stretch != 0
11003 && sym_frag->relax_marker != fragp->relax_marker)
11004 {
11005 fragS *f;
11006
11007 /* Adjust stretch for any alignment frag. Note that if have
11008 been expanding the earlier code, the symbol may be
11009 defined in what appears to be an earlier frag. FIXME:
11010 This doesn't handle the fr_subtype field, which specifies
11011 a maximum number of bytes to skip when doing an
11012 alignment. */
11013 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
11014 {
11015 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
11016 {
11017 if (stretch < 0)
11018 stretch = - ((- stretch)
11019 & ~ ((1 << (int) f->fr_offset) - 1));
11020 else
11021 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
11022 if (stretch == 0)
11023 break;
11024 }
11025 }
11026 if (f != NULL)
11027 val += stretch;
11028 }
11029
11030 addr = fragp->fr_address + fragp->fr_fix;
11031
11032 /* The base address rules are complicated. The base address of
11033 a branch is the following instruction. The base address of a
11034 PC relative load or add is the instruction itself, but if it
11035 is in a delay slot (in which case it can not be extended) use
11036 the address of the instruction whose delay slot it is in. */
11037 if (type == 'p' || type == 'q')
11038 {
11039 addr += 2;
11040
11041 /* If we are currently assuming that this frag should be
11042 extended, then, the current address is two bytes
11043 higher. */
11044 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11045 addr += 2;
11046
11047 /* Ignore the low bit in the target, since it will be set
11048 for a text label. */
11049 if ((val & 1) != 0)
11050 --val;
11051 }
11052 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11053 addr -= 4;
11054 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11055 addr -= 2;
11056
11057 val -= addr & ~ ((1 << op->shift) - 1);
11058
11059 /* Branch offsets have an implicit 0 in the lowest bit. */
11060 if (type == 'p' || type == 'q')
11061 val /= 2;
11062
11063 /* If any of the shifted bits are set, we must use an extended
11064 opcode. If the address depends on the size of this
11065 instruction, this can lead to a loop, so we arrange to always
11066 use an extended opcode. We only check this when we are in
11067 the main relaxation loop, when SEC is NULL. */
11068 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
11069 {
11070 fragp->fr_subtype =
11071 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11072 return 1;
11073 }
11074
11075 /* If we are about to mark a frag as extended because the value
11076 is precisely maxtiny + 1, then there is a chance of an
11077 infinite loop as in the following code:
11078 la $4,foo
11079 .skip 1020
11080 .align 2
11081 foo:
11082 In this case when the la is extended, foo is 0x3fc bytes
11083 away, so the la can be shrunk, but then foo is 0x400 away, so
11084 the la must be extended. To avoid this loop, we mark the
11085 frag as extended if it was small, and is about to become
11086 extended with a value of maxtiny + 1. */
11087 if (val == ((maxtiny + 1) << op->shift)
11088 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
11089 && sec == NULL)
11090 {
11091 fragp->fr_subtype =
11092 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11093 return 1;
11094 }
11095 }
11096 else if (symsec != absolute_section && sec != NULL)
11097 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
11098
11099 if ((val & ((1 << op->shift) - 1)) != 0
11100 || val < (mintiny << op->shift)
11101 || val > (maxtiny << op->shift))
11102 return 1;
11103 else
11104 return 0;
11105 }
11106
11107 /* Estimate the size of a frag before relaxing. Unless this is the
11108 mips16, we are not really relaxing here, and the final size is
11109 encoded in the subtype information. For the mips16, we have to
11110 decide whether we are using an extended opcode or not. */
11111
11112 int
11113 md_estimate_size_before_relax (fragp, segtype)
11114 fragS *fragp;
11115 asection *segtype;
11116 {
11117 int change = 0;
11118 boolean linkonce = false;
11119
11120 if (RELAX_MIPS16_P (fragp->fr_subtype))
11121 {
11122 if (mips16_extended_frag (fragp, segtype, 0))
11123 {
11124 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11125 return 4;
11126 }
11127 else
11128 {
11129 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11130 return 2;
11131 }
11132 }
11133
11134 if (mips_pic == NO_PIC)
11135 {
11136 change = nopic_need_relax (fragp->fr_symbol, 0);
11137 }
11138 else if (mips_pic == SVR4_PIC)
11139 {
11140 symbolS *sym;
11141 asection *symsec;
11142
11143 sym = fragp->fr_symbol;
11144
11145 /* Handle the case of a symbol equated to another symbol. */
11146 while (symbol_equated_reloc_p (sym))
11147 {
11148 symbolS *n;
11149
11150 /* It's possible to get a loop here in a badly written
11151 program. */
11152 n = symbol_get_value_expression (sym)->X_add_symbol;
11153 if (n == sym)
11154 break;
11155 sym = n;
11156 }
11157
11158 symsec = S_GET_SEGMENT (sym);
11159
11160 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
11161 if (symsec != segtype && ! S_IS_LOCAL (sym))
11162 {
11163 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
11164 != 0)
11165 linkonce = true;
11166
11167 /* The GNU toolchain uses an extension for ELF: a section
11168 beginning with the magic string .gnu.linkonce is a linkonce
11169 section. */
11170 if (strncmp (segment_name (symsec), ".gnu.linkonce",
11171 sizeof ".gnu.linkonce" - 1) == 0)
11172 linkonce = true;
11173 }
11174
11175 /* This must duplicate the test in adjust_reloc_syms. */
11176 change = (symsec != &bfd_und_section
11177 && symsec != &bfd_abs_section
11178 && ! bfd_is_com_section (symsec)
11179 && !linkonce
11180 #ifdef OBJ_ELF
11181 /* A global or weak symbol is treated as external. */
11182 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
11183 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
11184 #endif
11185 );
11186 }
11187 else
11188 abort ();
11189
11190 if (change)
11191 {
11192 /* Record the offset to the first reloc in the fr_opcode field.
11193 This lets md_convert_frag and tc_gen_reloc know that the code
11194 must be expanded. */
11195 fragp->fr_opcode = (fragp->fr_literal
11196 + fragp->fr_fix
11197 - RELAX_OLD (fragp->fr_subtype)
11198 + RELAX_RELOC1 (fragp->fr_subtype));
11199 /* FIXME: This really needs as_warn_where. */
11200 if (RELAX_WARN (fragp->fr_subtype))
11201 as_warn (_("AT used after \".set noat\" or macro used after "
11202 "\".set nomacro\""));
11203
11204 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
11205 }
11206
11207 return 0;
11208 }
11209
11210 /* This is called to see whether a reloc against a defined symbol
11211 should be converted into a reloc against a section. Don't adjust
11212 MIPS16 jump relocations, so we don't have to worry about the format
11213 of the offset in the .o file. Don't adjust relocations against
11214 mips16 symbols, so that the linker can find them if it needs to set
11215 up a stub. */
11216
11217 int
11218 mips_fix_adjustable (fixp)
11219 fixS *fixp;
11220 {
11221 #ifdef OBJ_ELF
11222 /* Prevent all adjustments to global symbols. */
11223 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11224 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
11225 return 0;
11226 #endif
11227 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
11228 return 0;
11229 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
11230 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11231 return 0;
11232 if (fixp->fx_addsy == NULL)
11233 return 1;
11234 #ifdef OBJ_ELF
11235 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
11236 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
11237 && fixp->fx_subsy == NULL)
11238 return 0;
11239 #endif
11240 return 1;
11241 }
11242
11243 /* Translate internal representation of relocation info to BFD target
11244 format. */
11245
11246 arelent **
11247 tc_gen_reloc (section, fixp)
11248 asection *section ATTRIBUTE_UNUSED;
11249 fixS *fixp;
11250 {
11251 static arelent *retval[4];
11252 arelent *reloc;
11253 bfd_reloc_code_real_type code;
11254
11255 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
11256 retval[1] = NULL;
11257
11258 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11259 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
11260 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11261
11262 if (mips_pic == EMBEDDED_PIC
11263 && SWITCH_TABLE (fixp))
11264 {
11265 /* For a switch table entry we use a special reloc. The addend
11266 is actually the difference between the reloc address and the
11267 subtrahend. */
11268 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11269 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
11270 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
11271 fixp->fx_r_type = BFD_RELOC_GPREL32;
11272 }
11273 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11274 reloc->addend = fixp->fx_addnumber;
11275 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
11276 {
11277 /* We use a special addend for an internal RELLO reloc. */
11278 if (symbol_section_p (fixp->fx_addsy))
11279 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
11280 else
11281 reloc->addend = fixp->fx_addnumber + reloc->address;
11282 }
11283 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
11284 {
11285 assert (fixp->fx_next != NULL
11286 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
11287 /* We use a special addend for an internal RELHI reloc. The
11288 reloc is relative to the RELLO; adjust the addend
11289 accordingly. */
11290 if (symbol_section_p (fixp->fx_addsy))
11291 reloc->addend = (fixp->fx_next->fx_frag->fr_address
11292 + fixp->fx_next->fx_where
11293 - S_GET_VALUE (fixp->fx_subsy));
11294 else
11295 reloc->addend = (fixp->fx_addnumber
11296 + fixp->fx_next->fx_frag->fr_address
11297 + fixp->fx_next->fx_where);
11298 }
11299 else
11300 {
11301 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
11302 /* A gruesome hack which is a result of the gruesome gas reloc
11303 handling. */
11304 reloc->addend = reloc->address;
11305 else
11306 reloc->addend = -reloc->address;
11307 }
11308
11309 /* If this is a variant frag, we may need to adjust the existing
11310 reloc and generate a new one. */
11311 if (fixp->fx_frag->fr_opcode != NULL
11312 && (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11313 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11314 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
11315 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11316 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
11317 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11318 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16))
11319 {
11320 arelent *reloc2;
11321
11322 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
11323
11324 /* If this is not the last reloc in this frag, then we have two
11325 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
11326 CALL_HI16/CALL_LO16, both of which are being replaced. Let
11327 the second one handle all of them. */
11328 if (fixp->fx_next != NULL
11329 && fixp->fx_frag == fixp->fx_next->fx_frag)
11330 {
11331 assert ((fixp->fx_r_type == BFD_RELOC_MIPS_GPREL
11332 && fixp->fx_next->fx_r_type == BFD_RELOC_MIPS_GPREL)
11333 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
11334 && (fixp->fx_next->fx_r_type
11335 == BFD_RELOC_MIPS_GOT_LO16))
11336 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
11337 && (fixp->fx_next->fx_r_type
11338 == BFD_RELOC_MIPS_CALL_LO16)));
11339 retval[0] = NULL;
11340 return retval;
11341 }
11342
11343 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
11344 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
11345 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
11346 retval[2] = NULL;
11347 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
11348 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
11349 reloc2->address = (reloc->address
11350 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
11351 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
11352 reloc2->addend = fixp->fx_addnumber;
11353 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
11354 assert (reloc2->howto != NULL);
11355
11356 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
11357 {
11358 arelent *reloc3;
11359
11360 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
11361 retval[3] = NULL;
11362 *reloc3 = *reloc2;
11363 reloc3->address += 4;
11364 }
11365
11366 if (mips_pic == NO_PIC)
11367 {
11368 assert (fixp->fx_r_type == BFD_RELOC_MIPS_GPREL);
11369 fixp->fx_r_type = BFD_RELOC_HI16_S;
11370 }
11371 else if (mips_pic == SVR4_PIC)
11372 {
11373 switch (fixp->fx_r_type)
11374 {
11375 default:
11376 abort ();
11377 case BFD_RELOC_MIPS_GOT16:
11378 break;
11379 case BFD_RELOC_MIPS_CALL16:
11380 case BFD_RELOC_MIPS_GOT_LO16:
11381 case BFD_RELOC_MIPS_CALL_LO16:
11382 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
11383 break;
11384 }
11385 }
11386 else
11387 abort ();
11388 }
11389
11390 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
11391 to be used in the relocation's section offset. */
11392 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
11393 {
11394 reloc->address = reloc->addend;
11395 reloc->addend = 0;
11396 }
11397
11398 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
11399 fixup_segment converted a non-PC relative reloc into a PC
11400 relative reloc. In such a case, we need to convert the reloc
11401 code. */
11402 code = fixp->fx_r_type;
11403 if (fixp->fx_pcrel)
11404 {
11405 switch (code)
11406 {
11407 case BFD_RELOC_8:
11408 code = BFD_RELOC_8_PCREL;
11409 break;
11410 case BFD_RELOC_16:
11411 code = BFD_RELOC_16_PCREL;
11412 break;
11413 case BFD_RELOC_32:
11414 code = BFD_RELOC_32_PCREL;
11415 break;
11416 case BFD_RELOC_64:
11417 code = BFD_RELOC_64_PCREL;
11418 break;
11419 case BFD_RELOC_8_PCREL:
11420 case BFD_RELOC_16_PCREL:
11421 case BFD_RELOC_32_PCREL:
11422 case BFD_RELOC_64_PCREL:
11423 case BFD_RELOC_16_PCREL_S2:
11424 case BFD_RELOC_PCREL_HI16_S:
11425 case BFD_RELOC_PCREL_LO16:
11426 break;
11427 default:
11428 as_bad_where (fixp->fx_file, fixp->fx_line,
11429 _("Cannot make %s relocation PC relative"),
11430 bfd_get_reloc_code_name (code));
11431 }
11432 }
11433
11434 /* To support a PC relative reloc when generating embedded PIC code
11435 for ECOFF, we use a Cygnus extension. We check for that here to
11436 make sure that we don't let such a reloc escape normally. */
11437 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11438 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11439 && code == BFD_RELOC_16_PCREL_S2
11440 && mips_pic != EMBEDDED_PIC)
11441 reloc->howto = NULL;
11442 else
11443 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
11444
11445 if (reloc->howto == NULL)
11446 {
11447 as_bad_where (fixp->fx_file, fixp->fx_line,
11448 _("Can not represent %s relocation in this object file format"),
11449 bfd_get_reloc_code_name (code));
11450 retval[0] = NULL;
11451 }
11452
11453 return retval;
11454 }
11455
11456 /* Relax a machine dependent frag. This returns the amount by which
11457 the current size of the frag should change. */
11458
11459 int
11460 mips_relax_frag (fragp, stretch)
11461 fragS *fragp;
11462 long stretch;
11463 {
11464 if (! RELAX_MIPS16_P (fragp->fr_subtype))
11465 return 0;
11466
11467 if (mips16_extended_frag (fragp, (asection *) NULL, stretch))
11468 {
11469 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11470 return 0;
11471 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11472 return 2;
11473 }
11474 else
11475 {
11476 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11477 return 0;
11478 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11479 return -2;
11480 }
11481
11482 return 0;
11483 }
11484
11485 /* Convert a machine dependent frag. */
11486
11487 void
11488 md_convert_frag (abfd, asec, fragp)
11489 bfd *abfd ATTRIBUTE_UNUSED;
11490 segT asec;
11491 fragS *fragp;
11492 {
11493 int old, new;
11494 char *fixptr;
11495
11496 if (RELAX_MIPS16_P (fragp->fr_subtype))
11497 {
11498 int type;
11499 register const struct mips16_immed_operand *op;
11500 boolean small, ext;
11501 offsetT val;
11502 bfd_byte *buf;
11503 unsigned long insn;
11504 boolean use_extend;
11505 unsigned short extend;
11506
11507 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11508 op = mips16_immed_operands;
11509 while (op->type != type)
11510 ++op;
11511
11512 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11513 {
11514 small = false;
11515 ext = true;
11516 }
11517 else
11518 {
11519 small = true;
11520 ext = false;
11521 }
11522
11523 resolve_symbol_value (fragp->fr_symbol);
11524 val = S_GET_VALUE (fragp->fr_symbol);
11525 if (op->pcrel)
11526 {
11527 addressT addr;
11528
11529 addr = fragp->fr_address + fragp->fr_fix;
11530
11531 /* The rules for the base address of a PC relative reloc are
11532 complicated; see mips16_extended_frag. */
11533 if (type == 'p' || type == 'q')
11534 {
11535 addr += 2;
11536 if (ext)
11537 addr += 2;
11538 /* Ignore the low bit in the target, since it will be
11539 set for a text label. */
11540 if ((val & 1) != 0)
11541 --val;
11542 }
11543 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11544 addr -= 4;
11545 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11546 addr -= 2;
11547
11548 addr &= ~ (addressT) ((1 << op->shift) - 1);
11549 val -= addr;
11550
11551 /* Make sure the section winds up with the alignment we have
11552 assumed. */
11553 if (op->shift > 0)
11554 record_alignment (asec, op->shift);
11555 }
11556
11557 if (ext
11558 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
11559 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
11560 as_warn_where (fragp->fr_file, fragp->fr_line,
11561 _("extended instruction in delay slot"));
11562
11563 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
11564
11565 if (target_big_endian)
11566 insn = bfd_getb16 (buf);
11567 else
11568 insn = bfd_getl16 (buf);
11569
11570 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
11571 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
11572 small, ext, &insn, &use_extend, &extend);
11573
11574 if (use_extend)
11575 {
11576 md_number_to_chars (buf, 0xf000 | extend, 2);
11577 fragp->fr_fix += 2;
11578 buf += 2;
11579 }
11580
11581 md_number_to_chars (buf, insn, 2);
11582 fragp->fr_fix += 2;
11583 buf += 2;
11584 }
11585 else
11586 {
11587 if (fragp->fr_opcode == NULL)
11588 return;
11589
11590 old = RELAX_OLD (fragp->fr_subtype);
11591 new = RELAX_NEW (fragp->fr_subtype);
11592 fixptr = fragp->fr_literal + fragp->fr_fix;
11593
11594 if (new > 0)
11595 memcpy (fixptr - old, fixptr, new);
11596
11597 fragp->fr_fix += new - old;
11598 }
11599 }
11600
11601 #ifdef OBJ_ELF
11602
11603 /* This function is called after the relocs have been generated.
11604 We've been storing mips16 text labels as odd. Here we convert them
11605 back to even for the convenience of the debugger. */
11606
11607 void
11608 mips_frob_file_after_relocs ()
11609 {
11610 asymbol **syms;
11611 unsigned int count, i;
11612
11613 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11614 return;
11615
11616 syms = bfd_get_outsymbols (stdoutput);
11617 count = bfd_get_symcount (stdoutput);
11618 for (i = 0; i < count; i++, syms++)
11619 {
11620 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
11621 && ((*syms)->value & 1) != 0)
11622 {
11623 (*syms)->value &= ~1;
11624 /* If the symbol has an odd size, it was probably computed
11625 incorrectly, so adjust that as well. */
11626 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
11627 ++elf_symbol (*syms)->internal_elf_sym.st_size;
11628 }
11629 }
11630 }
11631
11632 #endif
11633
11634 /* This function is called whenever a label is defined. It is used
11635 when handling branch delays; if a branch has a label, we assume we
11636 can not move it. */
11637
11638 void
11639 mips_define_label (sym)
11640 symbolS *sym;
11641 {
11642 struct insn_label_list *l;
11643
11644 if (free_insn_labels == NULL)
11645 l = (struct insn_label_list *) xmalloc (sizeof *l);
11646 else
11647 {
11648 l = free_insn_labels;
11649 free_insn_labels = l->next;
11650 }
11651
11652 l->label = sym;
11653 l->next = insn_labels;
11654 insn_labels = l;
11655 }
11656 \f
11657 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
11658
11659 /* Some special processing for a MIPS ELF file. */
11660
11661 void
11662 mips_elf_final_processing ()
11663 {
11664 /* Write out the register information. */
11665 if (! mips_64)
11666 {
11667 Elf32_RegInfo s;
11668
11669 s.ri_gprmask = mips_gprmask;
11670 s.ri_cprmask[0] = mips_cprmask[0];
11671 s.ri_cprmask[1] = mips_cprmask[1];
11672 s.ri_cprmask[2] = mips_cprmask[2];
11673 s.ri_cprmask[3] = mips_cprmask[3];
11674 /* The gp_value field is set by the MIPS ELF backend. */
11675
11676 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
11677 ((Elf32_External_RegInfo *)
11678 mips_regmask_frag));
11679 }
11680 else
11681 {
11682 Elf64_Internal_RegInfo s;
11683
11684 s.ri_gprmask = mips_gprmask;
11685 s.ri_pad = 0;
11686 s.ri_cprmask[0] = mips_cprmask[0];
11687 s.ri_cprmask[1] = mips_cprmask[1];
11688 s.ri_cprmask[2] = mips_cprmask[2];
11689 s.ri_cprmask[3] = mips_cprmask[3];
11690 /* The gp_value field is set by the MIPS ELF backend. */
11691
11692 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
11693 ((Elf64_External_RegInfo *)
11694 mips_regmask_frag));
11695 }
11696
11697 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
11698 sort of BFD interface for this. */
11699 if (mips_any_noreorder)
11700 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
11701 if (mips_pic != NO_PIC)
11702 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
11703
11704 /* Set the MIPS ELF ABI flags. */
11705 if (mips_abi_string == NULL)
11706 ;
11707 else if (strcmp (mips_abi_string, "32") == 0)
11708 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
11709 else if (strcmp (mips_abi_string, "o64") == 0)
11710 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
11711 else if (strcmp (mips_abi_string, "eabi") == 0)
11712 {
11713 if (mips_eabi64)
11714 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
11715 else
11716 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
11717 }
11718 else if (strcmp (mips_abi_string, "n32") == 0)
11719 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
11720
11721 /* Nothing to do for "64". */
11722
11723 if (mips_32bitmode)
11724 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
11725 }
11726
11727 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
11728 \f
11729 typedef struct proc {
11730 symbolS *isym;
11731 unsigned long reg_mask;
11732 unsigned long reg_offset;
11733 unsigned long fpreg_mask;
11734 unsigned long fpreg_offset;
11735 unsigned long frame_offset;
11736 unsigned long frame_reg;
11737 unsigned long pc_reg;
11738 } procS;
11739
11740 static procS cur_proc;
11741 static procS *cur_proc_ptr;
11742 static int numprocs;
11743
11744 /* Fill in an rs_align_code fragment. */
11745
11746 void
11747 mips_handle_align (fragp)
11748 fragS *fragp;
11749 {
11750 if (fragp->fr_type != rs_align_code)
11751 return;
11752
11753 if (mips_opts.mips16)
11754 {
11755 static const unsigned char be_nop[] = { 0x65, 0x00 };
11756 static const unsigned char le_nop[] = { 0x00, 0x65 };
11757
11758 int bytes;
11759 char *p;
11760
11761 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
11762 p = fragp->fr_literal + fragp->fr_fix;
11763
11764 if (bytes & 1)
11765 {
11766 *p++ = 0;
11767 fragp->fr_fix += 1;
11768 }
11769
11770 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
11771 fragp->fr_var = 2;
11772 }
11773
11774 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
11775 }
11776
11777 static void
11778 md_obj_begin ()
11779 {
11780 }
11781
11782 static void
11783 md_obj_end ()
11784 {
11785 /* check for premature end, nesting errors, etc */
11786 if (cur_proc_ptr)
11787 as_warn (_("missing .end at end of assembly"));
11788 }
11789
11790 static long
11791 get_number ()
11792 {
11793 int negative = 0;
11794 long val = 0;
11795
11796 if (*input_line_pointer == '-')
11797 {
11798 ++input_line_pointer;
11799 negative = 1;
11800 }
11801 if (!isdigit ((unsigned char) *input_line_pointer))
11802 as_bad (_("Expected simple number."));
11803 if (input_line_pointer[0] == '0')
11804 {
11805 if (input_line_pointer[1] == 'x')
11806 {
11807 input_line_pointer += 2;
11808 while (isxdigit ((unsigned char) *input_line_pointer))
11809 {
11810 val <<= 4;
11811 val |= hex_value (*input_line_pointer++);
11812 }
11813 return negative ? -val : val;
11814 }
11815 else
11816 {
11817 ++input_line_pointer;
11818 while (isdigit ((unsigned char) *input_line_pointer))
11819 {
11820 val <<= 3;
11821 val |= *input_line_pointer++ - '0';
11822 }
11823 return negative ? -val : val;
11824 }
11825 }
11826 if (!isdigit ((unsigned char) *input_line_pointer))
11827 {
11828 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
11829 *input_line_pointer, *input_line_pointer);
11830 as_warn (_("Invalid number"));
11831 return -1;
11832 }
11833 while (isdigit ((unsigned char) *input_line_pointer))
11834 {
11835 val *= 10;
11836 val += *input_line_pointer++ - '0';
11837 }
11838 return negative ? -val : val;
11839 }
11840
11841 /* The .file directive; just like the usual .file directive, but there
11842 is an initial number which is the ECOFF file index. */
11843
11844 static void
11845 s_file (x)
11846 int x ATTRIBUTE_UNUSED;
11847 {
11848 int line;
11849
11850 line = get_number ();
11851 s_app_file (0);
11852 }
11853
11854 /* The .end directive. */
11855
11856 static void
11857 s_mips_end (x)
11858 int x ATTRIBUTE_UNUSED;
11859 {
11860 symbolS *p;
11861 int maybe_text;
11862
11863 if (!is_end_of_line[(unsigned char) *input_line_pointer])
11864 {
11865 p = get_symbol ();
11866 demand_empty_rest_of_line ();
11867 }
11868 else
11869 p = NULL;
11870
11871 #ifdef BFD_ASSEMBLER
11872 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11873 maybe_text = 1;
11874 else
11875 maybe_text = 0;
11876 #else
11877 if (now_seg != data_section && now_seg != bss_section)
11878 maybe_text = 1;
11879 else
11880 maybe_text = 0;
11881 #endif
11882
11883 if (!maybe_text)
11884 as_warn (_(".end not in text section"));
11885
11886 if (!cur_proc_ptr)
11887 {
11888 as_warn (_(".end directive without a preceding .ent directive."));
11889 demand_empty_rest_of_line ();
11890 return;
11891 }
11892
11893 if (p != NULL)
11894 {
11895 assert (S_GET_NAME (p));
11896 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
11897 as_warn (_(".end symbol does not match .ent symbol."));
11898 }
11899 else
11900 as_warn (_(".end directive missing or unknown symbol"));
11901
11902 #ifdef MIPS_STABS_ELF
11903 {
11904 segT saved_seg = now_seg;
11905 subsegT saved_subseg = now_subseg;
11906 valueT dot;
11907 expressionS exp;
11908 char *fragp;
11909
11910 dot = frag_now_fix ();
11911
11912 #ifdef md_flush_pending_output
11913 md_flush_pending_output ();
11914 #endif
11915
11916 assert (pdr_seg);
11917 subseg_set (pdr_seg, 0);
11918
11919 /* Write the symbol. */
11920 exp.X_op = O_symbol;
11921 exp.X_add_symbol = p;
11922 exp.X_add_number = 0;
11923 emit_expr (&exp, 4);
11924
11925 fragp = frag_more (7 * 4);
11926
11927 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
11928 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
11929 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
11930 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
11931 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
11932 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
11933 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
11934
11935 subseg_set (saved_seg, saved_subseg);
11936 }
11937 #endif
11938
11939 cur_proc_ptr = NULL;
11940 }
11941
11942 /* The .aent and .ent directives. */
11943
11944 static void
11945 s_mips_ent (aent)
11946 int aent;
11947 {
11948 int number = 0;
11949 symbolS *symbolP;
11950 int maybe_text;
11951
11952 symbolP = get_symbol ();
11953 if (*input_line_pointer == ',')
11954 input_line_pointer++;
11955 SKIP_WHITESPACE ();
11956 if (isdigit ((unsigned char) *input_line_pointer)
11957 || *input_line_pointer == '-')
11958 number = get_number ();
11959
11960 #ifdef BFD_ASSEMBLER
11961 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
11962 maybe_text = 1;
11963 else
11964 maybe_text = 0;
11965 #else
11966 if (now_seg != data_section && now_seg != bss_section)
11967 maybe_text = 1;
11968 else
11969 maybe_text = 0;
11970 #endif
11971
11972 if (!maybe_text)
11973 as_warn (_(".ent or .aent not in text section."));
11974
11975 if (!aent && cur_proc_ptr)
11976 as_warn (_("missing .end"));
11977
11978 if (!aent)
11979 {
11980 cur_proc_ptr = &cur_proc;
11981 memset (cur_proc_ptr, '\0', sizeof (procS));
11982
11983 cur_proc_ptr->isym = symbolP;
11984
11985 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
11986
11987 numprocs++;
11988 }
11989
11990 demand_empty_rest_of_line ();
11991 }
11992
11993 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
11994 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
11995 s_mips_frame is used so that we can set the PDR information correctly.
11996 We can't use the ecoff routines because they make reference to the ecoff
11997 symbol table (in the mdebug section). */
11998
11999 static void
12000 s_mips_frame (ignore)
12001 int ignore ATTRIBUTE_UNUSED;
12002 {
12003 #ifdef MIPS_STABS_ELF
12004
12005 long val;
12006
12007 if (cur_proc_ptr == (procS *) NULL)
12008 {
12009 as_warn (_(".frame outside of .ent"));
12010 demand_empty_rest_of_line ();
12011 return;
12012 }
12013
12014 cur_proc_ptr->frame_reg = tc_get_register (1);
12015
12016 SKIP_WHITESPACE ();
12017 if (*input_line_pointer++ != ','
12018 || get_absolute_expression_and_terminator (&val) != ',')
12019 {
12020 as_warn (_("Bad .frame directive"));
12021 --input_line_pointer;
12022 demand_empty_rest_of_line ();
12023 return;
12024 }
12025
12026 cur_proc_ptr->frame_offset = val;
12027 cur_proc_ptr->pc_reg = tc_get_register (0);
12028
12029 demand_empty_rest_of_line ();
12030 #else
12031 s_ignore (ignore);
12032 #endif /* MIPS_STABS_ELF */
12033 }
12034
12035 /* The .fmask and .mask directives. If the mdebug section is present
12036 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
12037 embedded targets, s_mips_mask is used so that we can set the PDR
12038 information correctly. We can't use the ecoff routines because they
12039 make reference to the ecoff symbol table (in the mdebug section). */
12040
12041 static void
12042 s_mips_mask (reg_type)
12043 char reg_type;
12044 {
12045 #ifdef MIPS_STABS_ELF
12046 long mask, off;
12047
12048 if (cur_proc_ptr == (procS *) NULL)
12049 {
12050 as_warn (_(".mask/.fmask outside of .ent"));
12051 demand_empty_rest_of_line ();
12052 return;
12053 }
12054
12055 if (get_absolute_expression_and_terminator (&mask) != ',')
12056 {
12057 as_warn (_("Bad .mask/.fmask directive"));
12058 --input_line_pointer;
12059 demand_empty_rest_of_line ();
12060 return;
12061 }
12062
12063 off = get_absolute_expression ();
12064
12065 if (reg_type == 'F')
12066 {
12067 cur_proc_ptr->fpreg_mask = mask;
12068 cur_proc_ptr->fpreg_offset = off;
12069 }
12070 else
12071 {
12072 cur_proc_ptr->reg_mask = mask;
12073 cur_proc_ptr->reg_offset = off;
12074 }
12075
12076 demand_empty_rest_of_line ();
12077 #else
12078 s_ignore (reg_type);
12079 #endif /* MIPS_STABS_ELF */
12080 }
12081
12082 /* The .loc directive. */
12083
12084 #if 0
12085 static void
12086 s_loc (x)
12087 int x;
12088 {
12089 symbolS *symbolP;
12090 int lineno;
12091 int addroff;
12092
12093 assert (now_seg == text_section);
12094
12095 lineno = get_number ();
12096 addroff = frag_now_fix ();
12097
12098 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
12099 S_SET_TYPE (symbolP, N_SLINE);
12100 S_SET_OTHER (symbolP, 0);
12101 S_SET_DESC (symbolP, lineno);
12102 symbolP->sy_segment = now_seg;
12103 }
12104 #endif
12105
12106 /* CPU name/ISA/number mapping table.
12107
12108 Entries are grouped by type. The first matching CPU or ISA entry
12109 gets chosen by CPU or ISA, so it should be the 'canonical' name
12110 for that type. Entries after that within the type are sorted
12111 alphabetically.
12112
12113 Case is ignored in comparison, so put the canonical entry in the
12114 appropriate case but everything else in lower case to ease eye pain. */
12115 static const struct mips_cpu_info mips_cpu_info_table[] =
12116 {
12117 /* MIPS1 ISA */
12118 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
12119 { "mips", 1, ISA_MIPS1, CPU_R3000, },
12120
12121 /* MIPS2 ISA */
12122 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
12123
12124 /* MIPS3 ISA */
12125 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
12126
12127 /* MIPS4 ISA */
12128 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
12129
12130 /* MIPS5 ISA */
12131 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
12132 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
12133
12134 /* MIPS32 ISA */
12135 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
12136 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
12137 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
12138 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
12139 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
12140 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
12141
12142 /* For historical reasons. */
12143 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
12144
12145 /* MIPS64 ISA */
12146 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
12147 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
12148 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
12149 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
12150
12151 /* R2000 CPU */
12152 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
12153 { "2000", 0, ISA_MIPS1, CPU_R2000, },
12154 { "2k", 0, ISA_MIPS1, CPU_R2000, },
12155 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
12156
12157 /* R3000 CPU */
12158 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
12159 { "3000", 0, ISA_MIPS1, CPU_R3000, },
12160 { "3k", 0, ISA_MIPS1, CPU_R3000, },
12161 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
12162
12163 /* TX3900 CPU */
12164 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
12165 { "3900", 0, ISA_MIPS1, CPU_R3900, },
12166 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
12167
12168 /* R4000 CPU */
12169 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
12170 { "4000", 0, ISA_MIPS3, CPU_R4000, },
12171 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
12172 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
12173
12174 /* R4010 CPU */
12175 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
12176 { "4010", 0, ISA_MIPS2, CPU_R4010, },
12177
12178 /* R4400 CPU */
12179 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
12180 { "4400", 0, ISA_MIPS3, CPU_R4400, },
12181
12182 /* R4600 CPU */
12183 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
12184 { "4600", 0, ISA_MIPS3, CPU_R4600, },
12185 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
12186 { "orion", 0, ISA_MIPS3, CPU_R4600, },
12187
12188 /* R4650 CPU */
12189 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
12190 { "4650", 0, ISA_MIPS3, CPU_R4650, },
12191
12192 /* R6000 CPU */
12193 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
12194 { "6000", 0, ISA_MIPS2, CPU_R6000, },
12195 { "6k", 0, ISA_MIPS2, CPU_R6000, },
12196 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
12197
12198 /* R8000 CPU */
12199 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
12200 { "8000", 0, ISA_MIPS4, CPU_R8000, },
12201 { "8k", 0, ISA_MIPS4, CPU_R8000, },
12202 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
12203
12204 /* R10000 CPU */
12205 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
12206 { "10000", 0, ISA_MIPS4, CPU_R10000, },
12207 { "10k", 0, ISA_MIPS4, CPU_R10000, },
12208 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
12209
12210 /* R12000 CPU */
12211 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
12212 { "12000", 0, ISA_MIPS4, CPU_R12000, },
12213 { "12k", 0, ISA_MIPS4, CPU_R12000, },
12214 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
12215
12216 /* VR4100 CPU */
12217 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
12218 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
12219 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
12220 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
12221
12222 /* VR4111 CPU */
12223 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
12224 { "4111", 0, ISA_MIPS3, CPU_R4111, },
12225 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
12226 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
12227
12228 /* VR4300 CPU */
12229 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
12230 { "4300", 0, ISA_MIPS3, CPU_R4300, },
12231 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
12232 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
12233
12234 /* VR5000 CPU */
12235 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
12236 { "5000", 0, ISA_MIPS4, CPU_R5000, },
12237 { "5k", 0, ISA_MIPS4, CPU_R5000, },
12238 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
12239 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
12240 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
12241 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
12242 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
12243 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
12244 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
12245 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
12246 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
12247 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
12248 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
12249 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
12250 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
12251 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
12252
12253 /* SiByte SB-1 CPU */
12254 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
12255 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
12256 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
12257 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
12258
12259 /* End marker. */
12260 { NULL, 0, 0, 0, },
12261 };
12262
12263 static const struct mips_cpu_info *
12264 mips_cpu_info_from_name (name)
12265 const char *name;
12266 {
12267 int i;
12268
12269 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12270 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
12271 return (&mips_cpu_info_table[i]);
12272
12273 return NULL;
12274 }
12275
12276 static const struct mips_cpu_info *
12277 mips_cpu_info_from_isa (isa)
12278 int isa;
12279 {
12280 int i;
12281
12282 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12283 if (mips_cpu_info_table[i].is_isa
12284 && isa == mips_cpu_info_table[i].isa)
12285 return (&mips_cpu_info_table[i]);
12286
12287 return NULL;
12288 }
12289
12290 static const struct mips_cpu_info *
12291 mips_cpu_info_from_cpu (cpu)
12292 int cpu;
12293 {
12294 int i;
12295
12296 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
12297 if (!mips_cpu_info_table[i].is_isa
12298 && cpu == mips_cpu_info_table[i].cpu)
12299 return (&mips_cpu_info_table[i]);
12300
12301 return NULL;
12302 }
This page took 0.525969 seconds and 4 git commands to generate.