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