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