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