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