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