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