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