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