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