* coff-mips.c (mips_bfd_reloc_type_lookup): Replace
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
1 /* tc-mips.c -- assemble code for a MIPS chip.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26 #include "as.h"
27 #include "config.h"
28 #include "subsegs.h"
29 #include "safe-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 /* The name of the readonly data section. */
106 #define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107 ? ".data" \
108 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109 ? ".rdata" \
110 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111 ? ".rdata" \
112 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113 ? ".rodata" \
114 : (abort (), ""))
115
116 /* The ABI to use. */
117 enum mips_abi_level
118 {
119 NO_ABI = 0,
120 O32_ABI,
121 O64_ABI,
122 N32_ABI,
123 N64_ABI,
124 EABI_ABI
125 };
126
127 /* MIPS ABI we are using for this output file. */
128 static enum mips_abi_level file_mips_abi = NO_ABI;
129
130 /* This is the set of options which may be modified by the .set
131 pseudo-op. We use a struct so that .set push and .set pop are more
132 reliable. */
133
134 struct mips_set_options
135 {
136 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
137 if it has not been initialized. Changed by `.set mipsN', and the
138 -mipsN command line option, and the default CPU. */
139 int isa;
140 /* Whether we are assembling for the mips16 processor. 0 if we are
141 not, 1 if we are, and -1 if the value has not been initialized.
142 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
143 -nomips16 command line options, and the default CPU. */
144 int mips16;
145 /* Non-zero if we should not reorder instructions. Changed by `.set
146 reorder' and `.set noreorder'. */
147 int noreorder;
148 /* Non-zero if we should not permit the $at ($1) register to be used
149 in instructions. Changed by `.set at' and `.set noat'. */
150 int noat;
151 /* Non-zero if we should warn when a macro instruction expands into
152 more than one machine instruction. Changed by `.set nomacro' and
153 `.set macro'. */
154 int warn_about_macros;
155 /* Non-zero if we should not move instructions. Changed by `.set
156 move', `.set volatile', `.set nomove', and `.set novolatile'. */
157 int nomove;
158 /* Non-zero if we should not optimize branches by moving the target
159 of the branch into the delay slot. Actually, we don't perform
160 this optimization anyhow. Changed by `.set bopt' and `.set
161 nobopt'. */
162 int nobopt;
163 /* Non-zero if we should not autoextend mips16 instructions.
164 Changed by `.set autoextend' and `.set noautoextend'. */
165 int noautoextend;
166 /* Restrict general purpose registers and floating point registers
167 to 32 bit. This is initially determined when -mgp32 or -mfp32
168 is passed but can changed if the assembler code uses .set mipsN. */
169 int gp32;
170 int fp32;
171 /* The ABI currently in use. This is changed by .set mipsN to loosen
172 restrictions and doesn't affect the whole file. */
173 enum mips_abi_level abi;
174 };
175
176 /* True if -mgp32 was passed. */
177 static int file_mips_gp32 = 0;
178
179 /* True if -mfp32 was passed. */
180 static int file_mips_fp32 = 0;
181
182 /* This is the struct we use to hold the current set of options. Note
183 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
184 -1 to indicate that they have not been initialized. */
185
186 static struct mips_set_options mips_opts =
187 {
188 ISA_UNKNOWN, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
189 };
190
191 /* These variables are filled in with the masks of registers used.
192 The object format code reads them and puts them in the appropriate
193 place. */
194 unsigned long mips_gprmask;
195 unsigned long mips_cprmask[4];
196
197 /* MIPS ISA we are using for this output file. */
198 static int file_mips_isa = ISA_UNKNOWN;
199
200 /* The argument of the -mcpu= flag. Historical for code generation. */
201 static int mips_cpu = CPU_UNKNOWN;
202
203 /* The argument of the -march= flag. The architecture we are assembling. */
204 static int mips_arch = CPU_UNKNOWN;
205
206 /* The argument of the -mtune= flag. The architecture for which we
207 are optimizing. */
208 static int mips_tune = CPU_UNKNOWN;
209
210 /* Whether we should mark the file EABI64 or EABI32. */
211 static int mips_eabi64 = 0;
212
213 /* If they asked for mips1 or mips2 and a cpu that is
214 mips3 or greater, then mark the object file 32BITMODE. */
215 static int mips_32bitmode = 0;
216
217 /* Some ISA's have delay slots for instructions which read or write
218 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
219 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
220 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
221 delay slot in this ISA. The uses of this macro assume that any
222 ISA that has delay slots for one of these, has them for all. They
223 also assume that ISAs which don't have delays for these insns, don't
224 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
225 #define ISA_HAS_COPROC_DELAYS(ISA) ( \
226 (ISA) == ISA_MIPS1 \
227 || (ISA) == ISA_MIPS2 \
228 || (ISA) == ISA_MIPS3 \
229 )
230
231 /* Return true if ISA supports 64 bit gp register instructions. */
232 #define ISA_HAS_64BIT_REGS(ISA) ( \
233 (ISA) == ISA_MIPS3 \
234 || (ISA) == ISA_MIPS4 \
235 || (ISA) == ISA_MIPS5 \
236 || (ISA) == ISA_MIPS64 \
237 )
238
239 #define HAVE_32BIT_GPRS \
240 (mips_opts.gp32 \
241 || mips_opts.abi == O32_ABI \
242 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
243
244 #define HAVE_32BIT_FPRS \
245 (mips_opts.fp32 \
246 || mips_opts.abi == O32_ABI \
247 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
248
249 #define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
250 #define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
251
252 #define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
253
254 #define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
255
256 /* We can only have 64bit addresses if the object file format
257 supports it. */
258 #define HAVE_32BIT_ADDRESSES \
259 (HAVE_32BIT_GPRS \
260 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
261 || ! HAVE_64BIT_OBJECTS) \
262 && mips_pic != EMBEDDED_PIC))
263
264 #define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
265
266 /* Whether the processor uses hardware interlocks to protect
267 reads from the HI and LO registers, and thus does not
268 require nops to be inserted. */
269
270 #define hilo_interlocks (mips_arch == CPU_R4010 \
271 || mips_arch == CPU_SB1 \
272 )
273
274 /* Whether the processor uses hardware interlocks to protect reads
275 from the GPRs, and thus does not require nops to be inserted. */
276 #define gpr_interlocks \
277 (mips_opts.isa != ISA_MIPS1 \
278 || mips_arch == CPU_R3900)
279
280 /* As with other "interlocks" this is used by hardware that has FP
281 (co-processor) interlocks. */
282 /* Itbl support may require additional care here. */
283 #define cop_interlocks (mips_arch == CPU_R4300 \
284 || mips_arch == CPU_SB1 \
285 )
286
287 /* Is this a mfhi or mflo instruction? */
288 #define MF_HILO_INSN(PINFO) \
289 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
290
291 /* MIPS PIC level. */
292
293 enum mips_pic_level
294 {
295 /* Do not generate PIC code. */
296 NO_PIC,
297
298 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
299 not sure what it is supposed to do. */
300 IRIX4_PIC,
301
302 /* Generate PIC code as in the SVR4 MIPS ABI. */
303 SVR4_PIC,
304
305 /* Generate PIC code without using a global offset table: the data
306 segment has a maximum size of 64K, all data references are off
307 the $gp register, and all text references are PC relative. This
308 is used on some embedded systems. */
309 EMBEDDED_PIC
310 };
311
312 static enum mips_pic_level mips_pic;
313
314 /* Warn about all NOPS that the assembler generates. */
315 static int warn_nops = 0;
316
317 /* 1 if we should generate 32 bit offsets from the GP register in
318 SVR4_PIC mode. Currently has no meaning in other modes. */
319 static int mips_big_got;
320
321 /* 1 if trap instructions should used for overflow rather than break
322 instructions. */
323 static int mips_trap;
324
325 /* 1 if double width floating point constants should not be constructed
326 by assembling two single width halves into two single width floating
327 point registers which just happen to alias the double width destination
328 register. On some architectures this aliasing can be disabled by a bit
329 in the status register, and the setting of this bit cannot be determined
330 automatically at assemble time. */
331 static int mips_disable_float_construction;
332
333 /* Non-zero if any .set noreorder directives were used. */
334
335 static int mips_any_noreorder;
336
337 /* Non-zero if nops should be inserted when the register referenced in
338 an mfhi/mflo instruction is read in the next two instructions. */
339 static int mips_7000_hilo_fix;
340
341 /* The size of the small data section. */
342 static unsigned int g_switch_value = 8;
343 /* Whether the -G option was used. */
344 static int g_switch_seen = 0;
345
346 #define N_RMASK 0xc4
347 #define N_VFP 0xd4
348
349 /* If we can determine in advance that GP optimization won't be
350 possible, we can skip the relaxation stuff that tries to produce
351 GP-relative references. This makes delay slot optimization work
352 better.
353
354 This function can only provide a guess, but it seems to work for
355 gcc output. It needs to guess right for gcc, otherwise gcc
356 will put what it thinks is a GP-relative instruction in a branch
357 delay slot.
358
359 I don't know if a fix is needed for the SVR4_PIC mode. I've only
360 fixed it for the non-PIC mode. KR 95/04/07 */
361 static int nopic_need_relax PARAMS ((symbolS *, int));
362
363 /* handle of the OPCODE hash table */
364 static struct hash_control *op_hash = NULL;
365
366 /* The opcode hash table we use for the mips16. */
367 static struct hash_control *mips16_op_hash = NULL;
368
369 /* This array holds the chars that always start a comment. If the
370 pre-processor is disabled, these aren't very useful */
371 const char comment_chars[] = "#";
372
373 /* This array holds the chars that only start a comment at the beginning of
374 a line. If the line seems to have the form '# 123 filename'
375 .line and .file directives will appear in the pre-processed output */
376 /* Note that input_file.c hand checks for '#' at the beginning of the
377 first line of the input file. This is because the compiler outputs
378 #NO_APP at the beginning of its output. */
379 /* Also note that C style comments are always supported. */
380 const char line_comment_chars[] = "#";
381
382 /* This array holds machine specific line separator characters. */
383 const char line_separator_chars[] = ";";
384
385 /* Chars that can be used to separate mant from exp in floating point nums */
386 const char EXP_CHARS[] = "eE";
387
388 /* Chars that mean this number is a floating point constant */
389 /* As in 0f12.456 */
390 /* or 0d1.2345e12 */
391 const char FLT_CHARS[] = "rRsSfFdDxXpP";
392
393 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
394 changed in read.c . Ideally it shouldn't have to know about it at all,
395 but nothing is ideal around here.
396 */
397
398 static char *insn_error;
399
400 static int auto_align = 1;
401
402 /* When outputting SVR4 PIC code, the assembler needs to know the
403 offset in the stack frame from which to restore the $gp register.
404 This is set by the .cprestore pseudo-op, and saved in this
405 variable. */
406 static offsetT mips_cprestore_offset = -1;
407
408 /* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
409 more optimizations, it can use a register value instead of a memory-saved
410 offset and even an other than GP as global pointer. */
411 static offsetT mips_cpreturn_offset = -1;
412 static int mips_cpreturn_register = -1;
413 static int mips_gp_register = GP;
414
415 /* This is the register which holds the stack frame, as set by the
416 .frame pseudo-op. This is needed to implement .cprestore. */
417 static int mips_frame_reg = SP;
418
419 /* To output NOP instructions correctly, we need to keep information
420 about the previous two instructions. */
421
422 /* Whether we are optimizing. The default value of 2 means to remove
423 unneeded NOPs and swap branch instructions when possible. A value
424 of 1 means to not swap branches. A value of 0 means to always
425 insert NOPs. */
426 static int mips_optimize = 2;
427
428 /* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
429 equivalent to seeing no -g option at all. */
430 static int mips_debug = 0;
431
432 /* The previous instruction. */
433 static struct mips_cl_insn prev_insn;
434
435 /* The instruction before prev_insn. */
436 static struct mips_cl_insn prev_prev_insn;
437
438 /* If we don't want information for prev_insn or prev_prev_insn, we
439 point the insn_mo field at this dummy integer. */
440 static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
441
442 /* Non-zero if prev_insn is valid. */
443 static int prev_insn_valid;
444
445 /* The frag for the previous instruction. */
446 static struct frag *prev_insn_frag;
447
448 /* The offset into prev_insn_frag for the previous instruction. */
449 static long prev_insn_where;
450
451 /* The reloc type for the previous instruction, if any. */
452 static bfd_reloc_code_real_type prev_insn_reloc_type[3];
453
454 /* The reloc for the previous instruction, if any. */
455 static fixS *prev_insn_fixp[3];
456
457 /* Non-zero if the previous instruction was in a delay slot. */
458 static int prev_insn_is_delay_slot;
459
460 /* Non-zero if the previous instruction was in a .set noreorder. */
461 static int prev_insn_unreordered;
462
463 /* Non-zero if the previous instruction uses an extend opcode (if
464 mips16). */
465 static int prev_insn_extended;
466
467 /* Non-zero if the previous previous instruction was in a .set
468 noreorder. */
469 static int prev_prev_insn_unreordered;
470
471 /* If this is set, it points to a frag holding nop instructions which
472 were inserted before the start of a noreorder section. If those
473 nops turn out to be unnecessary, the size of the frag can be
474 decreased. */
475 static fragS *prev_nop_frag;
476
477 /* The number of nop instructions we created in prev_nop_frag. */
478 static int prev_nop_frag_holds;
479
480 /* The number of nop instructions that we know we need in
481 prev_nop_frag. */
482 static int prev_nop_frag_required;
483
484 /* The number of instructions we've seen since prev_nop_frag. */
485 static int prev_nop_frag_since;
486
487 /* For ECOFF and ELF, relocations against symbols are done in two
488 parts, with a HI relocation and a LO relocation. Each relocation
489 has only 16 bits of space to store an addend. This means that in
490 order for the linker to handle carries correctly, it must be able
491 to locate both the HI and the LO relocation. This means that the
492 relocations must appear in order in the relocation table.
493
494 In order to implement this, we keep track of each unmatched HI
495 relocation. We then sort them so that they immediately precede the
496 corresponding LO relocation. */
497
498 struct mips_hi_fixup
499 {
500 /* Next HI fixup. */
501 struct mips_hi_fixup *next;
502 /* This fixup. */
503 fixS *fixp;
504 /* The section this fixup is in. */
505 segT seg;
506 };
507
508 /* The list of unmatched HI relocs. */
509
510 static struct mips_hi_fixup *mips_hi_fixup_list;
511
512 /* Map normal MIPS register numbers to mips16 register numbers. */
513
514 #define X ILLEGAL_REG
515 static const int mips32_to_16_reg_map[] =
516 {
517 X, X, 2, 3, 4, 5, 6, 7,
518 X, X, X, X, X, X, X, X,
519 0, 1, X, X, X, X, X, X,
520 X, X, X, X, X, X, X, X
521 };
522 #undef X
523
524 /* Map mips16 register numbers to normal MIPS register numbers. */
525
526 static const unsigned int mips16_to_32_reg_map[] =
527 {
528 16, 17, 2, 3, 4, 5, 6, 7
529 };
530 \f
531 /* Since the MIPS does not have multiple forms of PC relative
532 instructions, we do not have to do relaxing as is done on other
533 platforms. However, we do have to handle GP relative addressing
534 correctly, which turns out to be a similar problem.
535
536 Every macro that refers to a symbol can occur in (at least) two
537 forms, one with GP relative addressing and one without. For
538 example, loading a global variable into a register generally uses
539 a macro instruction like this:
540 lw $4,i
541 If i can be addressed off the GP register (this is true if it is in
542 the .sbss or .sdata section, or if it is known to be smaller than
543 the -G argument) this will generate the following instruction:
544 lw $4,i($gp)
545 This instruction will use a GPREL reloc. If i can not be addressed
546 off the GP register, the following instruction sequence will be used:
547 lui $at,i
548 lw $4,i($at)
549 In this case the first instruction will have a HI16 reloc, and the
550 second reloc will have a LO16 reloc. Both relocs will be against
551 the symbol i.
552
553 The issue here is that we may not know whether i is GP addressable
554 until after we see the instruction that uses it. Therefore, we
555 want to be able to choose the final instruction sequence only at
556 the end of the assembly. This is similar to the way other
557 platforms choose the size of a PC relative instruction only at the
558 end of assembly.
559
560 When generating position independent code we do not use GP
561 addressing in quite the same way, but the issue still arises as
562 external symbols and local symbols must be handled differently.
563
564 We handle these issues by actually generating both possible
565 instruction sequences. The longer one is put in a frag_var with
566 type rs_machine_dependent. We encode what to do with the frag in
567 the subtype field. We encode (1) the number of existing bytes to
568 replace, (2) the number of new bytes to use, (3) the offset from
569 the start of the existing bytes to the first reloc we must generate
570 (that is, the offset is applied from the start of the existing
571 bytes after they are replaced by the new bytes, if any), (4) the
572 offset from the start of the existing bytes to the second reloc,
573 (5) whether a third reloc is needed (the third reloc is always four
574 bytes after the second reloc), and (6) whether to warn if this
575 variant is used (this is sometimes needed if .set nomacro or .set
576 noat is in effect). All these numbers are reasonably small.
577
578 Generating two instruction sequences must be handled carefully to
579 ensure that delay slots are handled correctly. Fortunately, there
580 are a limited number of cases. When the second instruction
581 sequence is generated, append_insn is directed to maintain the
582 existing delay slot information, so it continues to apply to any
583 code after the second instruction sequence. This means that the
584 second instruction sequence must not impose any requirements not
585 required by the first instruction sequence.
586
587 These variant frags are then handled in functions called by the
588 machine independent code. md_estimate_size_before_relax returns
589 the final size of the frag. md_convert_frag sets up the final form
590 of the frag. tc_gen_reloc adjust the first reloc and adds a second
591 one if needed. */
592 #define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
593 ((relax_substateT) \
594 (((old) << 23) \
595 | ((new) << 16) \
596 | (((reloc1) + 64) << 9) \
597 | (((reloc2) + 64) << 2) \
598 | ((reloc3) ? (1 << 1) : 0) \
599 | ((warn) ? 1 : 0)))
600 #define RELAX_OLD(i) (((i) >> 23) & 0x7f)
601 #define RELAX_NEW(i) (((i) >> 16) & 0x7f)
602 #define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
603 #define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
604 #define RELAX_RELOC3(i) (((i) >> 1) & 1)
605 #define RELAX_WARN(i) ((i) & 1)
606
607 /* For mips16 code, we use an entirely different form of relaxation.
608 mips16 supports two versions of most instructions which take
609 immediate values: a small one which takes some small value, and a
610 larger one which takes a 16 bit value. Since branches also follow
611 this pattern, relaxing these values is required.
612
613 We can assemble both mips16 and normal MIPS code in a single
614 object. Therefore, we need to support this type of relaxation at
615 the same time that we support the relaxation described above. We
616 use the high bit of the subtype field to distinguish these cases.
617
618 The information we store for this type of relaxation is the
619 argument code found in the opcode file for this relocation, whether
620 the user explicitly requested a small or extended form, and whether
621 the relocation is in a jump or jal delay slot. That tells us the
622 size of the value, and how it should be stored. We also store
623 whether the fragment is considered to be extended or not. We also
624 store whether this is known to be a branch to a different section,
625 whether we have tried to relax this frag yet, and whether we have
626 ever extended a PC relative fragment because of a shift count. */
627 #define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
628 (0x80000000 \
629 | ((type) & 0xff) \
630 | ((small) ? 0x100 : 0) \
631 | ((ext) ? 0x200 : 0) \
632 | ((dslot) ? 0x400 : 0) \
633 | ((jal_dslot) ? 0x800 : 0))
634 #define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
635 #define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
636 #define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
637 #define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
638 #define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
639 #define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
640 #define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
641 #define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
642 #define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
643 #define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
644 #define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
645 #define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
646 \f
647 /* Prototypes for static functions. */
648
649 #ifdef __STDC__
650 #define internalError() \
651 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
652 #else
653 #define internalError() as_fatal (_("MIPS internal Error"));
654 #endif
655
656 enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
657
658 static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
659 unsigned int reg, enum mips_regclass class));
660 static int reg_needs_delay PARAMS ((unsigned int));
661 static void mips16_mark_labels PARAMS ((void));
662 static void append_insn PARAMS ((char *place,
663 struct mips_cl_insn * ip,
664 expressionS * p,
665 bfd_reloc_code_real_type *r,
666 boolean));
667 static void mips_no_prev_insn PARAMS ((int));
668 static void mips_emit_delays PARAMS ((boolean));
669 #ifdef USE_STDARG
670 static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
671 const char *name, const char *fmt,
672 ...));
673 #else
674 static void macro_build ();
675 #endif
676 static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
677 const char *, const char *,
678 va_list));
679 static void macro_build_lui PARAMS ((char *place, int *counter,
680 expressionS * ep, int regnum));
681 static void set_at PARAMS ((int *counter, int reg, int unsignedp));
682 static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
683 expressionS *));
684 static void load_register PARAMS ((int *, int, expressionS *, int));
685 static void load_address PARAMS ((int *, int, expressionS *, int, int *));
686 static void move_register PARAMS ((int *, int, int));
687 static void macro PARAMS ((struct mips_cl_insn * ip));
688 static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
689 #ifdef LOSING_COMPILER
690 static void macro2 PARAMS ((struct mips_cl_insn * ip));
691 #endif
692 static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
693 static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
694 static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
695 boolean, boolean, unsigned long *,
696 boolean *, unsigned short *));
697 static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
698 static int my_getSmallExpression PARAMS ((expressionS *, char *));
699 static void my_getExpression PARAMS ((expressionS *, char *));
700 #ifdef OBJ_ELF
701 static int support_64bit_objects PARAMS((void));
702 #endif
703 static symbolS *get_symbol PARAMS ((void));
704 static void mips_align PARAMS ((int to, int fill, symbolS *label));
705 static void s_align PARAMS ((int));
706 static void s_change_sec PARAMS ((int));
707 static void s_cons PARAMS ((int));
708 static void s_float_cons PARAMS ((int));
709 static void s_mips_globl PARAMS ((int));
710 static void s_option PARAMS ((int));
711 static void s_mipsset PARAMS ((int));
712 static void s_abicalls PARAMS ((int));
713 static void s_cpload PARAMS ((int));
714 static void s_cpsetup PARAMS ((int));
715 static void s_cplocal PARAMS ((int));
716 static void s_cprestore PARAMS ((int));
717 static void s_cpreturn PARAMS ((int));
718 static void s_gpvalue PARAMS ((int));
719 static void s_gpword PARAMS ((int));
720 static void s_cpadd PARAMS ((int));
721 static void s_insn PARAMS ((int));
722 static void md_obj_begin PARAMS ((void));
723 static void md_obj_end PARAMS ((void));
724 static long get_number PARAMS ((void));
725 static void s_mips_ent PARAMS ((int));
726 static void s_mips_end PARAMS ((int));
727 static void s_mips_frame PARAMS ((int));
728 static void s_mips_mask PARAMS ((int));
729 static void s_mips_stab PARAMS ((int));
730 static void s_mips_weakext PARAMS ((int));
731 static void s_file PARAMS ((int));
732 static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
733 static const char *mips_isa_to_str PARAMS ((int));
734 static const char *mips_cpu_to_str PARAMS ((int));
735 static int validate_mips_insn PARAMS ((const struct mips_opcode *));
736 static void show PARAMS ((FILE *, char *, int *, int *));
737 #ifdef OBJ_ELF
738 static int mips_need_elf_addend_fixup PARAMS ((fixS *));
739 #endif
740
741 /* Return values of my_getSmallExpression(). */
742
743 enum small_ex_type
744 {
745 S_EX_NONE = 0,
746 S_EX_REGISTER,
747
748 /* Direct relocation creation by %percent_op(). */
749 S_EX_HALF,
750 S_EX_HI,
751 S_EX_LO,
752 S_EX_GP_REL,
753 S_EX_GOT,
754 S_EX_CALL16,
755 S_EX_GOT_DISP,
756 S_EX_GOT_PAGE,
757 S_EX_GOT_OFST,
758 S_EX_GOT_HI,
759 S_EX_GOT_LO,
760 S_EX_NEG,
761 S_EX_HIGHER,
762 S_EX_HIGHEST,
763 S_EX_CALL_HI,
764 S_EX_CALL_LO
765 };
766
767 /* Table and functions used to map between CPU/ISA names, and
768 ISA levels, and CPU numbers. */
769
770 struct mips_cpu_info
771 {
772 const char *name; /* CPU or ISA name. */
773 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
774 int isa; /* ISA level. */
775 int cpu; /* CPU number (default CPU if ISA). */
776 };
777
778 static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
779 static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
780 static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
781 \f
782 /* Pseudo-op table.
783
784 The following pseudo-ops from the Kane and Heinrich MIPS book
785 should be defined here, but are currently unsupported: .alias,
786 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
787
788 The following pseudo-ops from the Kane and Heinrich MIPS book are
789 specific to the type of debugging information being generated, and
790 should be defined by the object format: .aent, .begin, .bend,
791 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
792 .vreg.
793
794 The following pseudo-ops from the Kane and Heinrich MIPS book are
795 not MIPS CPU specific, but are also not specific to the object file
796 format. This file is probably the best place to define them, but
797 they are not currently supported: .asm0, .endr, .lab, .repeat,
798 .struct. */
799
800 static const pseudo_typeS mips_pseudo_table[] =
801 {
802 /* MIPS specific pseudo-ops. */
803 {"option", s_option, 0},
804 {"set", s_mipsset, 0},
805 {"rdata", s_change_sec, 'r'},
806 {"sdata", s_change_sec, 's'},
807 {"livereg", s_ignore, 0},
808 {"abicalls", s_abicalls, 0},
809 {"cpload", s_cpload, 0},
810 {"cpsetup", s_cpsetup, 0},
811 {"cplocal", s_cplocal, 0},
812 {"cprestore", s_cprestore, 0},
813 {"cpreturn", s_cpreturn, 0},
814 {"gpvalue", s_gpvalue, 0},
815 {"gpword", s_gpword, 0},
816 {"cpadd", s_cpadd, 0},
817 {"insn", s_insn, 0},
818
819 /* Relatively generic pseudo-ops that happen to be used on MIPS
820 chips. */
821 {"asciiz", stringer, 1},
822 {"bss", s_change_sec, 'b'},
823 {"err", s_err, 0},
824 {"half", s_cons, 1},
825 {"dword", s_cons, 3},
826 {"weakext", s_mips_weakext, 0},
827
828 /* These pseudo-ops are defined in read.c, but must be overridden
829 here for one reason or another. */
830 {"align", s_align, 0},
831 {"byte", s_cons, 0},
832 {"data", s_change_sec, 'd'},
833 {"double", s_float_cons, 'd'},
834 {"float", s_float_cons, 'f'},
835 {"globl", s_mips_globl, 0},
836 {"global", s_mips_globl, 0},
837 {"hword", s_cons, 1},
838 {"int", s_cons, 2},
839 {"long", s_cons, 2},
840 {"octa", s_cons, 4},
841 {"quad", s_cons, 3},
842 {"short", s_cons, 1},
843 {"single", s_float_cons, 'f'},
844 {"stabn", s_mips_stab, 'n'},
845 {"text", s_change_sec, 't'},
846 {"word", s_cons, 2},
847
848 #ifdef MIPS_STABS_ELF
849 { "extern", ecoff_directive_extern, 0},
850 #endif
851
852 { NULL, NULL, 0 },
853 };
854
855 static const pseudo_typeS mips_nonecoff_pseudo_table[] =
856 {
857 /* These pseudo-ops should be defined by the object file format.
858 However, a.out doesn't support them, so we have versions here. */
859 {"aent", s_mips_ent, 1},
860 {"bgnb", s_ignore, 0},
861 {"end", s_mips_end, 0},
862 {"endb", s_ignore, 0},
863 {"ent", s_mips_ent, 0},
864 {"file", s_file, 0},
865 {"fmask", s_mips_mask, 'F'},
866 {"frame", s_mips_frame, 0},
867 {"loc", s_ignore, 0},
868 {"mask", s_mips_mask, 'R'},
869 {"verstamp", s_ignore, 0},
870 { NULL, NULL, 0 },
871 };
872
873 extern void pop_insert PARAMS ((const pseudo_typeS *));
874
875 void
876 mips_pop_insert ()
877 {
878 pop_insert (mips_pseudo_table);
879 if (! ECOFF_DEBUGGING)
880 pop_insert (mips_nonecoff_pseudo_table);
881 }
882 \f
883 /* Symbols labelling the current insn. */
884
885 struct insn_label_list
886 {
887 struct insn_label_list *next;
888 symbolS *label;
889 };
890
891 static struct insn_label_list *insn_labels;
892 static struct insn_label_list *free_insn_labels;
893
894 static void mips_clear_insn_labels PARAMS ((void));
895
896 static inline void
897 mips_clear_insn_labels ()
898 {
899 register struct insn_label_list **pl;
900
901 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
902 ;
903 *pl = insn_labels;
904 insn_labels = NULL;
905 }
906 \f
907 static char *expr_end;
908
909 /* Expressions which appear in instructions. These are set by
910 mips_ip. */
911
912 static expressionS imm_expr;
913 static expressionS offset_expr;
914
915 /* Relocs associated with imm_expr and offset_expr. */
916
917 static bfd_reloc_code_real_type imm_reloc[3]
918 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
919 static bfd_reloc_code_real_type offset_reloc[3]
920 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
921
922 /* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
923
924 static boolean imm_unmatched_hi;
925
926 /* These are set by mips16_ip if an explicit extension is used. */
927
928 static boolean mips16_small, mips16_ext;
929
930 #ifdef MIPS_STABS_ELF
931 /* The pdr segment for per procedure frame/regmask info */
932
933 static segT pdr_seg;
934 #endif
935
936 static const char *
937 mips_isa_to_str (isa)
938 int isa;
939 {
940 const struct mips_cpu_info *ci;
941 static char s[20];
942
943 ci = mips_cpu_info_from_isa (isa);
944 if (ci != NULL)
945 return (ci->name);
946
947 sprintf (s, "ISA#%d", isa);
948 return s;
949 }
950
951 static const char *
952 mips_cpu_to_str (cpu)
953 int cpu;
954 {
955 const struct mips_cpu_info *ci;
956 static char s[16];
957
958 ci = mips_cpu_info_from_cpu (cpu);
959 if (ci != NULL)
960 return (ci->name);
961
962 sprintf (s, "CPU#%d", cpu);
963 return s;
964 }
965
966 /* The default target format to use. */
967
968 const char *
969 mips_target_format ()
970 {
971 switch (OUTPUT_FLAVOR)
972 {
973 case bfd_target_aout_flavour:
974 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
975 case bfd_target_ecoff_flavour:
976 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
977 case bfd_target_coff_flavour:
978 return "pe-mips";
979 case bfd_target_elf_flavour:
980 #ifdef TE_TMIPS
981 /* This is traditional mips */
982 return (target_big_endian
983 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
984 : "elf32-tradbigmips")
985 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
986 : "elf32-tradlittlemips"));
987 #else
988 return (target_big_endian
989 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
990 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
991 : "elf32-littlemips"));
992 #endif
993 default:
994 abort ();
995 return NULL;
996 }
997 }
998
999 /* This function is called once, at assembler startup time. It should
1000 set up all the tables, etc. that the MD part of the assembler will need. */
1001
1002 void
1003 md_begin ()
1004 {
1005 register const char *retval = NULL;
1006 int i = 0;
1007 const char *cpu;
1008 char *a = NULL;
1009 int broken = 0;
1010 int mips_isa_from_cpu;
1011 int target_cpu_had_mips16 = 0;
1012 const struct mips_cpu_info *ci;
1013
1014 /* GP relative stuff not working for PE */
1015 if (strncmp (TARGET_OS, "pe", 2) == 0
1016 && g_switch_value != 0)
1017 {
1018 if (g_switch_seen)
1019 as_bad (_("-G not supported in this configuration."));
1020 g_switch_value = 0;
1021 }
1022
1023 cpu = TARGET_CPU;
1024 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1025 {
1026 a = xmalloc (sizeof TARGET_CPU);
1027 strcpy (a, TARGET_CPU);
1028 a[(sizeof TARGET_CPU) - 3] = '\0';
1029 cpu = a;
1030 }
1031
1032 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
1033 {
1034 target_cpu_had_mips16 = 1;
1035 cpu += sizeof "mips16" - 1;
1036 }
1037
1038 if (mips_opts.mips16 < 0)
1039 mips_opts.mips16 = target_cpu_had_mips16;
1040
1041 /* Backward compatibility for historic -mcpu= option. Check for
1042 incompatible options, warn if -mcpu is used. */
1043 if (mips_cpu != CPU_UNKNOWN
1044 && mips_arch != CPU_UNKNOWN
1045 && mips_cpu != mips_arch)
1046 {
1047 as_fatal (_("The -mcpu option can't be used together with -march. "
1048 "Use -mtune instead of -mcpu."));
1049 }
1050
1051 if (mips_cpu != CPU_UNKNOWN
1052 && mips_tune != CPU_UNKNOWN
1053 && mips_cpu != mips_tune)
1054 {
1055 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1056 "Use -march instead of -mcpu."));
1057 }
1058
1059 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1060 {
1061 ci = mips_cpu_info_from_cpu (mips_cpu);
1062 assert (ci != NULL);
1063 mips_arch = ci->cpu;
1064 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1065 "-mtune instead."));
1066 }
1067
1068 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1069 specified on the command line, or some other value if one was.
1070 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1071 the command line, or will be set otherwise if one was. */
1072 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1073 {
1074 /* We have to check if the isa is the default isa of arch. Otherwise
1075 we'll get invalid object file headers. */
1076 ci = mips_cpu_info_from_cpu (mips_arch);
1077 assert (ci != NULL);
1078 if (mips_opts.isa != ci->isa)
1079 {
1080 /* This really should be an error instead of a warning, but old
1081 compilers only have -mcpu which sets both arch and tune. For
1082 now, we discard arch and preserve tune. */
1083 as_warn (_("The -march option is incompatible to -mipsN and "
1084 "therefore ignored."));
1085 if (mips_tune == CPU_UNKNOWN)
1086 mips_tune = mips_arch;
1087 ci = mips_cpu_info_from_isa (mips_opts.isa);
1088 assert (ci != NULL);
1089 mips_arch = ci->cpu;
1090 }
1091 }
1092 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
1093 {
1094 /* We have ARCH, we need ISA. */
1095 ci = mips_cpu_info_from_cpu (mips_arch);
1096 assert (ci != NULL);
1097 mips_opts.isa = ci->isa;
1098 }
1099 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1100 {
1101 /* We have ISA, we need default ARCH. */
1102 ci = mips_cpu_info_from_isa (mips_opts.isa);
1103 assert (ci != NULL);
1104 mips_arch = ci->cpu;
1105 }
1106 else
1107 {
1108 /* We need to set both ISA and ARCH from target cpu. */
1109 ci = mips_cpu_info_from_name (cpu);
1110 if (ci == NULL)
1111 ci = mips_cpu_info_from_cpu (CPU_R3000);
1112 assert (ci != NULL);
1113 mips_opts.isa = ci->isa;
1114 mips_arch = ci->cpu;
1115 }
1116
1117 if (mips_tune == CPU_UNKNOWN)
1118 mips_tune = mips_arch;
1119
1120 ci = mips_cpu_info_from_cpu (mips_arch);
1121 assert (ci != NULL);
1122 mips_isa_from_cpu = ci->isa;
1123
1124 /* End of TARGET_CPU processing, get rid of malloced memory
1125 if necessary. */
1126 cpu = NULL;
1127 if (a != NULL)
1128 {
1129 free (a);
1130 a = NULL;
1131 }
1132
1133 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
1134 as_bad (_("trap exception not supported at ISA 1"));
1135
1136 /* Set the EABI kind based on the ISA before the user gets
1137 to change the ISA with directives. This isn't really
1138 the best, but then neither is basing the abi on the isa. */
1139 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
1140 && mips_opts.abi == EABI_ABI)
1141 mips_eabi64 = 1;
1142
1143 /* If they asked for mips1 or mips2 and a cpu that is
1144 mips3 or greater, then mark the object file 32BITMODE. */
1145 if (mips_isa_from_cpu != ISA_UNKNOWN
1146 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1147 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1148 mips_32bitmode = 1;
1149
1150 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
1151 as_warn (_("Could not set architecture and machine"));
1152
1153 file_mips_isa = mips_opts.isa;
1154 file_mips_abi = mips_opts.abi;
1155 mips_opts.gp32 = file_mips_gp32;
1156 mips_opts.fp32 = file_mips_fp32;
1157
1158 op_hash = hash_new ();
1159
1160 for (i = 0; i < NUMOPCODES;)
1161 {
1162 const char *name = mips_opcodes[i].name;
1163
1164 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1165 if (retval != NULL)
1166 {
1167 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1168 mips_opcodes[i].name, retval);
1169 /* Probably a memory allocation problem? Give up now. */
1170 as_fatal (_("Broken assembler. No assembly attempted."));
1171 }
1172 do
1173 {
1174 if (mips_opcodes[i].pinfo != INSN_MACRO)
1175 {
1176 if (!validate_mips_insn (&mips_opcodes[i]))
1177 broken = 1;
1178 }
1179 ++i;
1180 }
1181 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1182 }
1183
1184 mips16_op_hash = hash_new ();
1185
1186 i = 0;
1187 while (i < bfd_mips16_num_opcodes)
1188 {
1189 const char *name = mips16_opcodes[i].name;
1190
1191 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1192 if (retval != NULL)
1193 as_fatal (_("internal: can't hash `%s': %s"),
1194 mips16_opcodes[i].name, retval);
1195 do
1196 {
1197 if (mips16_opcodes[i].pinfo != INSN_MACRO
1198 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1199 != mips16_opcodes[i].match))
1200 {
1201 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1202 mips16_opcodes[i].name, mips16_opcodes[i].args);
1203 broken = 1;
1204 }
1205 ++i;
1206 }
1207 while (i < bfd_mips16_num_opcodes
1208 && strcmp (mips16_opcodes[i].name, name) == 0);
1209 }
1210
1211 if (broken)
1212 as_fatal (_("Broken assembler. No assembly attempted."));
1213
1214 /* We add all the general register names to the symbol table. This
1215 helps us detect invalid uses of them. */
1216 for (i = 0; i < 32; i++)
1217 {
1218 char buf[5];
1219
1220 sprintf (buf, "$%d", i);
1221 symbol_table_insert (symbol_new (buf, reg_section, i,
1222 &zero_address_frag));
1223 }
1224 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1225 &zero_address_frag));
1226 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1227 &zero_address_frag));
1228 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1229 &zero_address_frag));
1230 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1231 &zero_address_frag));
1232 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1233 &zero_address_frag));
1234 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1235 &zero_address_frag));
1236 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1237 &zero_address_frag));
1238
1239 mips_no_prev_insn (false);
1240
1241 mips_gprmask = 0;
1242 mips_cprmask[0] = 0;
1243 mips_cprmask[1] = 0;
1244 mips_cprmask[2] = 0;
1245 mips_cprmask[3] = 0;
1246
1247 /* set the default alignment for the text section (2**2) */
1248 record_alignment (text_section, 2);
1249
1250 if (USE_GLOBAL_POINTER_OPT)
1251 bfd_set_gp_size (stdoutput, g_switch_value);
1252
1253 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1254 {
1255 /* On a native system, sections must be aligned to 16 byte
1256 boundaries. When configured for an embedded ELF target, we
1257 don't bother. */
1258 if (strcmp (TARGET_OS, "elf") != 0)
1259 {
1260 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1261 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1262 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1263 }
1264
1265 /* Create a .reginfo section for register masks and a .mdebug
1266 section for debugging information. */
1267 {
1268 segT seg;
1269 subsegT subseg;
1270 flagword flags;
1271 segT sec;
1272
1273 seg = now_seg;
1274 subseg = now_subseg;
1275
1276 /* The ABI says this section should be loaded so that the
1277 running program can access it. However, we don't load it
1278 if we are configured for an embedded target */
1279 flags = SEC_READONLY | SEC_DATA;
1280 if (strcmp (TARGET_OS, "elf") != 0)
1281 flags |= SEC_ALLOC | SEC_LOAD;
1282
1283 if (! HAVE_NEWABI)
1284 {
1285 sec = subseg_new (".reginfo", (subsegT) 0);
1286
1287 (void) bfd_set_section_flags (stdoutput, sec, flags);
1288 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1289
1290 #ifdef OBJ_ELF
1291 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1292 #endif
1293 }
1294 else
1295 {
1296 /* The 64-bit ABI uses a .MIPS.options section rather than
1297 .reginfo section. */
1298 sec = subseg_new (".MIPS.options", (subsegT) 0);
1299 (void) bfd_set_section_flags (stdoutput, sec, flags);
1300 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1301
1302 #ifdef OBJ_ELF
1303 /* Set up the option header. */
1304 {
1305 Elf_Internal_Options opthdr;
1306 char *f;
1307
1308 opthdr.kind = ODK_REGINFO;
1309 opthdr.size = (sizeof (Elf_External_Options)
1310 + sizeof (Elf64_External_RegInfo));
1311 opthdr.section = 0;
1312 opthdr.info = 0;
1313 f = frag_more (sizeof (Elf_External_Options));
1314 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1315 (Elf_External_Options *) f);
1316
1317 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1318 }
1319 #endif
1320 }
1321
1322 if (ECOFF_DEBUGGING)
1323 {
1324 sec = subseg_new (".mdebug", (subsegT) 0);
1325 (void) bfd_set_section_flags (stdoutput, sec,
1326 SEC_HAS_CONTENTS | SEC_READONLY);
1327 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1328 }
1329
1330 #ifdef MIPS_STABS_ELF
1331 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1332 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1333 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1334 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1335 #endif
1336
1337 subseg_set (seg, subseg);
1338 }
1339 }
1340
1341 if (! ECOFF_DEBUGGING)
1342 md_obj_begin ();
1343 }
1344
1345 void
1346 md_mips_end ()
1347 {
1348 if (! ECOFF_DEBUGGING)
1349 md_obj_end ();
1350 }
1351
1352 void
1353 md_assemble (str)
1354 char *str;
1355 {
1356 struct mips_cl_insn insn;
1357 bfd_reloc_code_real_type unused_reloc[3]
1358 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1359
1360 imm_expr.X_op = O_absent;
1361 imm_unmatched_hi = false;
1362 offset_expr.X_op = O_absent;
1363 imm_reloc[0] = BFD_RELOC_UNUSED;
1364 imm_reloc[1] = BFD_RELOC_UNUSED;
1365 imm_reloc[2] = BFD_RELOC_UNUSED;
1366 offset_reloc[0] = BFD_RELOC_UNUSED;
1367 offset_reloc[1] = BFD_RELOC_UNUSED;
1368 offset_reloc[2] = BFD_RELOC_UNUSED;
1369
1370 if (mips_opts.mips16)
1371 mips16_ip (str, &insn);
1372 else
1373 {
1374 mips_ip (str, &insn);
1375 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1376 str, insn.insn_opcode));
1377 }
1378
1379 if (insn_error)
1380 {
1381 as_bad ("%s `%s'", insn_error, str);
1382 return;
1383 }
1384
1385 if (insn.insn_mo->pinfo == INSN_MACRO)
1386 {
1387 if (mips_opts.mips16)
1388 mips16_macro (&insn);
1389 else
1390 macro (&insn);
1391 }
1392 else
1393 {
1394 if (imm_expr.X_op != O_absent)
1395 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
1396 else if (offset_expr.X_op != O_absent)
1397 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
1398 else
1399 append_insn (NULL, &insn, NULL, unused_reloc, false);
1400 }
1401 }
1402
1403 /* See whether instruction IP reads register REG. CLASS is the type
1404 of register. */
1405
1406 static int
1407 insn_uses_reg (ip, reg, class)
1408 struct mips_cl_insn *ip;
1409 unsigned int reg;
1410 enum mips_regclass class;
1411 {
1412 if (class == MIPS16_REG)
1413 {
1414 assert (mips_opts.mips16);
1415 reg = mips16_to_32_reg_map[reg];
1416 class = MIPS_GR_REG;
1417 }
1418
1419 /* Don't report on general register 0, since it never changes. */
1420 if (class == MIPS_GR_REG && reg == 0)
1421 return 0;
1422
1423 if (class == MIPS_FP_REG)
1424 {
1425 assert (! mips_opts.mips16);
1426 /* If we are called with either $f0 or $f1, we must check $f0.
1427 This is not optimal, because it will introduce an unnecessary
1428 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1429 need to distinguish reading both $f0 and $f1 or just one of
1430 them. Note that we don't have to check the other way,
1431 because there is no instruction that sets both $f0 and $f1
1432 and requires a delay. */
1433 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1434 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1435 == (reg &~ (unsigned) 1)))
1436 return 1;
1437 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1438 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1439 == (reg &~ (unsigned) 1)))
1440 return 1;
1441 }
1442 else if (! mips_opts.mips16)
1443 {
1444 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1445 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1446 return 1;
1447 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1448 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1449 return 1;
1450 }
1451 else
1452 {
1453 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1454 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1455 & MIPS16OP_MASK_RX)]
1456 == reg))
1457 return 1;
1458 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1459 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1460 & MIPS16OP_MASK_RY)]
1461 == reg))
1462 return 1;
1463 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1464 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1465 & MIPS16OP_MASK_MOVE32Z)]
1466 == reg))
1467 return 1;
1468 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1469 return 1;
1470 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1471 return 1;
1472 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1473 return 1;
1474 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1475 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1476 & MIPS16OP_MASK_REGR32) == reg)
1477 return 1;
1478 }
1479
1480 return 0;
1481 }
1482
1483 /* This function returns true if modifying a register requires a
1484 delay. */
1485
1486 static int
1487 reg_needs_delay (reg)
1488 unsigned int reg;
1489 {
1490 unsigned long prev_pinfo;
1491
1492 prev_pinfo = prev_insn.insn_mo->pinfo;
1493 if (! mips_opts.noreorder
1494 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1495 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1496 || (! gpr_interlocks
1497 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1498 {
1499 /* A load from a coprocessor or from memory. All load
1500 delays delay the use of general register rt for one
1501 instruction on the r3000. The r6000 and r4000 use
1502 interlocks. */
1503 /* Itbl support may require additional care here. */
1504 know (prev_pinfo & INSN_WRITE_GPR_T);
1505 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1506 return 1;
1507 }
1508
1509 return 0;
1510 }
1511
1512 /* Mark instruction labels in mips16 mode. This permits the linker to
1513 handle them specially, such as generating jalx instructions when
1514 needed. We also make them odd for the duration of the assembly, in
1515 order to generate the right sort of code. We will make them even
1516 in the adjust_symtab routine, while leaving them marked. This is
1517 convenient for the debugger and the disassembler. The linker knows
1518 to make them odd again. */
1519
1520 static void
1521 mips16_mark_labels ()
1522 {
1523 if (mips_opts.mips16)
1524 {
1525 struct insn_label_list *l;
1526 valueT val;
1527
1528 for (l = insn_labels; l != NULL; l = l->next)
1529 {
1530 #ifdef OBJ_ELF
1531 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1532 S_SET_OTHER (l->label, STO_MIPS16);
1533 #endif
1534 val = S_GET_VALUE (l->label);
1535 if ((val & 1) == 0)
1536 S_SET_VALUE (l->label, val + 1);
1537 }
1538 }
1539 }
1540
1541 /* Output an instruction. PLACE is where to put the instruction; if
1542 it is NULL, this uses frag_more to get room. IP is the instruction
1543 information. ADDRESS_EXPR is an operand of the instruction to be
1544 used with RELOC_TYPE. */
1545
1546 static void
1547 append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1548 char *place;
1549 struct mips_cl_insn *ip;
1550 expressionS *address_expr;
1551 bfd_reloc_code_real_type *reloc_type;
1552 boolean unmatched_hi;
1553 {
1554 register unsigned long prev_pinfo, pinfo;
1555 char *f;
1556 fixS *fixp[3];
1557 int nops = 0;
1558
1559 /* Mark instruction labels in mips16 mode. */
1560 if (mips_opts.mips16)
1561 mips16_mark_labels ();
1562
1563 prev_pinfo = prev_insn.insn_mo->pinfo;
1564 pinfo = ip->insn_mo->pinfo;
1565
1566 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1567 {
1568 int prev_prev_nop;
1569
1570 /* If the previous insn required any delay slots, see if we need
1571 to insert a NOP or two. There are eight kinds of possible
1572 hazards, of which an instruction can have at most one type.
1573 (1) a load from memory delay
1574 (2) a load from a coprocessor delay
1575 (3) an unconditional branch delay
1576 (4) a conditional branch delay
1577 (5) a move to coprocessor register delay
1578 (6) a load coprocessor register from memory delay
1579 (7) a coprocessor condition code delay
1580 (8) a HI/LO special register delay
1581
1582 There are a lot of optimizations we could do that we don't.
1583 In particular, we do not, in general, reorder instructions.
1584 If you use gcc with optimization, it will reorder
1585 instructions and generally do much more optimization then we
1586 do here; repeating all that work in the assembler would only
1587 benefit hand written assembly code, and does not seem worth
1588 it. */
1589
1590 /* This is how a NOP is emitted. */
1591 #define emit_nop() \
1592 (mips_opts.mips16 \
1593 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1594 : md_number_to_chars (frag_more (4), 0, 4))
1595
1596 /* The previous insn might require a delay slot, depending upon
1597 the contents of the current insn. */
1598 if (! mips_opts.mips16
1599 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1600 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1601 && ! cop_interlocks)
1602 || (! gpr_interlocks
1603 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1604 {
1605 /* A load from a coprocessor or from memory. All load
1606 delays delay the use of general register rt for one
1607 instruction on the r3000. The r6000 and r4000 use
1608 interlocks. */
1609 /* Itbl support may require additional care here. */
1610 know (prev_pinfo & INSN_WRITE_GPR_T);
1611 if (mips_optimize == 0
1612 || insn_uses_reg (ip,
1613 ((prev_insn.insn_opcode >> OP_SH_RT)
1614 & OP_MASK_RT),
1615 MIPS_GR_REG))
1616 ++nops;
1617 }
1618 else if (! mips_opts.mips16
1619 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1620 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
1621 && ! cop_interlocks)
1622 || (mips_opts.isa == ISA_MIPS1
1623 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1624 {
1625 /* A generic coprocessor delay. The previous instruction
1626 modified a coprocessor general or control register. If
1627 it modified a control register, we need to avoid any
1628 coprocessor instruction (this is probably not always
1629 required, but it sometimes is). If it modified a general
1630 register, we avoid using that register.
1631
1632 On the r6000 and r4000 loading a coprocessor register
1633 from memory is interlocked, and does not require a delay.
1634
1635 This case is not handled very well. There is no special
1636 knowledge of CP0 handling, and the coprocessors other
1637 than the floating point unit are not distinguished at
1638 all. */
1639 /* Itbl support may require additional care here. FIXME!
1640 Need to modify this to include knowledge about
1641 user specified delays! */
1642 if (prev_pinfo & INSN_WRITE_FPR_T)
1643 {
1644 if (mips_optimize == 0
1645 || insn_uses_reg (ip,
1646 ((prev_insn.insn_opcode >> OP_SH_FT)
1647 & OP_MASK_FT),
1648 MIPS_FP_REG))
1649 ++nops;
1650 }
1651 else if (prev_pinfo & INSN_WRITE_FPR_S)
1652 {
1653 if (mips_optimize == 0
1654 || insn_uses_reg (ip,
1655 ((prev_insn.insn_opcode >> OP_SH_FS)
1656 & OP_MASK_FS),
1657 MIPS_FP_REG))
1658 ++nops;
1659 }
1660 else
1661 {
1662 /* We don't know exactly what the previous instruction
1663 does. If the current instruction uses a coprocessor
1664 register, we must insert a NOP. If previous
1665 instruction may set the condition codes, and the
1666 current instruction uses them, we must insert two
1667 NOPS. */
1668 /* Itbl support may require additional care here. */
1669 if (mips_optimize == 0
1670 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1671 && (pinfo & INSN_READ_COND_CODE)))
1672 nops += 2;
1673 else if (pinfo & INSN_COP)
1674 ++nops;
1675 }
1676 }
1677 else if (! mips_opts.mips16
1678 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1679 && (prev_pinfo & INSN_WRITE_COND_CODE)
1680 && ! cop_interlocks)
1681 {
1682 /* The previous instruction sets the coprocessor condition
1683 codes, but does not require a general coprocessor delay
1684 (this means it is a floating point comparison
1685 instruction). If this instruction uses the condition
1686 codes, we need to insert a single NOP. */
1687 /* Itbl support may require additional care here. */
1688 if (mips_optimize == 0
1689 || (pinfo & INSN_READ_COND_CODE))
1690 ++nops;
1691 }
1692
1693 /* If we're fixing up mfhi/mflo for the r7000 and the
1694 previous insn was an mfhi/mflo and the current insn
1695 reads the register that the mfhi/mflo wrote to, then
1696 insert two nops. */
1697
1698 else if (mips_7000_hilo_fix
1699 && MF_HILO_INSN (prev_pinfo)
1700 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
1701 & OP_MASK_RD),
1702 MIPS_GR_REG))
1703 {
1704 nops += 2;
1705 }
1706
1707 /* If we're fixing up mfhi/mflo for the r7000 and the
1708 2nd previous insn was an mfhi/mflo and the current insn
1709 reads the register that the mfhi/mflo wrote to, then
1710 insert one nop. */
1711
1712 else if (mips_7000_hilo_fix
1713 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1714 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1715 & OP_MASK_RD),
1716 MIPS_GR_REG))
1717
1718 {
1719 nops += 1;
1720 }
1721
1722 else if (prev_pinfo & INSN_READ_LO)
1723 {
1724 /* The previous instruction reads the LO register; if the
1725 current instruction writes to the LO register, we must
1726 insert two NOPS. Some newer processors have interlocks.
1727 Also the tx39's multiply instructions can be exectuted
1728 immediatly after a read from HI/LO (without the delay),
1729 though the tx39's divide insns still do require the
1730 delay. */
1731 if (! (hilo_interlocks
1732 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1733 && (mips_optimize == 0
1734 || (pinfo & INSN_WRITE_LO)))
1735 nops += 2;
1736 /* Most mips16 branch insns don't have a delay slot.
1737 If a read from LO is immediately followed by a branch
1738 to a write to LO we have a read followed by a write
1739 less than 2 insns away. We assume the target of
1740 a branch might be a write to LO, and insert a nop
1741 between a read and an immediately following branch. */
1742 else if (mips_opts.mips16
1743 && (mips_optimize == 0
1744 || (pinfo & MIPS16_INSN_BRANCH)))
1745 nops += 1;
1746 }
1747 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1748 {
1749 /* The previous instruction reads the HI register; if the
1750 current instruction writes to the HI register, we must
1751 insert a NOP. Some newer processors have interlocks.
1752 Also the note tx39's multiply above. */
1753 if (! (hilo_interlocks
1754 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
1755 && (mips_optimize == 0
1756 || (pinfo & INSN_WRITE_HI)))
1757 nops += 2;
1758 /* Most mips16 branch insns don't have a delay slot.
1759 If a read from HI is immediately followed by a branch
1760 to a write to HI we have a read followed by a write
1761 less than 2 insns away. We assume the target of
1762 a branch might be a write to HI, and insert a nop
1763 between a read and an immediately following branch. */
1764 else if (mips_opts.mips16
1765 && (mips_optimize == 0
1766 || (pinfo & MIPS16_INSN_BRANCH)))
1767 nops += 1;
1768 }
1769
1770 /* If the previous instruction was in a noreorder section, then
1771 we don't want to insert the nop after all. */
1772 /* Itbl support may require additional care here. */
1773 if (prev_insn_unreordered)
1774 nops = 0;
1775
1776 /* There are two cases which require two intervening
1777 instructions: 1) setting the condition codes using a move to
1778 coprocessor instruction which requires a general coprocessor
1779 delay and then reading the condition codes 2) reading the HI
1780 or LO register and then writing to it (except on processors
1781 which have interlocks). If we are not already emitting a NOP
1782 instruction, we must check for these cases compared to the
1783 instruction previous to the previous instruction. */
1784 if ((! mips_opts.mips16
1785 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
1786 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1787 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1788 && (pinfo & INSN_READ_COND_CODE)
1789 && ! cop_interlocks)
1790 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1791 && (pinfo & INSN_WRITE_LO)
1792 && ! (hilo_interlocks
1793 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
1794 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1795 && (pinfo & INSN_WRITE_HI)
1796 && ! (hilo_interlocks
1797 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
1798 prev_prev_nop = 1;
1799 else
1800 prev_prev_nop = 0;
1801
1802 if (prev_prev_insn_unreordered)
1803 prev_prev_nop = 0;
1804
1805 if (prev_prev_nop && nops == 0)
1806 ++nops;
1807
1808 /* If we are being given a nop instruction, don't bother with
1809 one of the nops we would otherwise output. This will only
1810 happen when a nop instruction is used with mips_optimize set
1811 to 0. */
1812 if (nops > 0
1813 && ! mips_opts.noreorder
1814 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
1815 --nops;
1816
1817 /* Now emit the right number of NOP instructions. */
1818 if (nops > 0 && ! mips_opts.noreorder)
1819 {
1820 fragS *old_frag;
1821 unsigned long old_frag_offset;
1822 int i;
1823 struct insn_label_list *l;
1824
1825 old_frag = frag_now;
1826 old_frag_offset = frag_now_fix ();
1827
1828 for (i = 0; i < nops; i++)
1829 emit_nop ();
1830
1831 if (listing)
1832 {
1833 listing_prev_line ();
1834 /* We may be at the start of a variant frag. In case we
1835 are, make sure there is enough space for the frag
1836 after the frags created by listing_prev_line. The
1837 argument to frag_grow here must be at least as large
1838 as the argument to all other calls to frag_grow in
1839 this file. We don't have to worry about being in the
1840 middle of a variant frag, because the variants insert
1841 all needed nop instructions themselves. */
1842 frag_grow (40);
1843 }
1844
1845 for (l = insn_labels; l != NULL; l = l->next)
1846 {
1847 valueT val;
1848
1849 assert (S_GET_SEGMENT (l->label) == now_seg);
1850 symbol_set_frag (l->label, frag_now);
1851 val = (valueT) frag_now_fix ();
1852 /* mips16 text labels are stored as odd. */
1853 if (mips_opts.mips16)
1854 val += 1;
1855 S_SET_VALUE (l->label, val);
1856 }
1857
1858 #ifndef NO_ECOFF_DEBUGGING
1859 if (ECOFF_DEBUGGING)
1860 ecoff_fix_loc (old_frag, old_frag_offset);
1861 #endif
1862 }
1863 else if (prev_nop_frag != NULL)
1864 {
1865 /* We have a frag holding nops we may be able to remove. If
1866 we don't need any nops, we can decrease the size of
1867 prev_nop_frag by the size of one instruction. If we do
1868 need some nops, we count them in prev_nops_required. */
1869 if (prev_nop_frag_since == 0)
1870 {
1871 if (nops == 0)
1872 {
1873 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1874 --prev_nop_frag_holds;
1875 }
1876 else
1877 prev_nop_frag_required += nops;
1878 }
1879 else
1880 {
1881 if (prev_prev_nop == 0)
1882 {
1883 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1884 --prev_nop_frag_holds;
1885 }
1886 else
1887 ++prev_nop_frag_required;
1888 }
1889
1890 if (prev_nop_frag_holds <= prev_nop_frag_required)
1891 prev_nop_frag = NULL;
1892
1893 ++prev_nop_frag_since;
1894
1895 /* Sanity check: by the time we reach the second instruction
1896 after prev_nop_frag, we should have used up all the nops
1897 one way or another. */
1898 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1899 }
1900 }
1901
1902 if (*reloc_type > BFD_RELOC_UNUSED)
1903 {
1904 /* We need to set up a variant frag. */
1905 assert (mips_opts.mips16 && address_expr != NULL);
1906 f = frag_var (rs_machine_dependent, 4, 0,
1907 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
1908 mips16_small, mips16_ext,
1909 (prev_pinfo
1910 & INSN_UNCOND_BRANCH_DELAY),
1911 (*prev_insn_reloc_type
1912 == BFD_RELOC_MIPS16_JMP)),
1913 make_expr_symbol (address_expr), 0, NULL);
1914 }
1915 else if (place != NULL)
1916 f = place;
1917 else if (mips_opts.mips16
1918 && ! ip->use_extend
1919 && *reloc_type != BFD_RELOC_MIPS16_JMP)
1920 {
1921 /* Make sure there is enough room to swap this instruction with
1922 a following jump instruction. */
1923 frag_grow (6);
1924 f = frag_more (2);
1925 }
1926 else
1927 {
1928 if (mips_opts.mips16
1929 && mips_opts.noreorder
1930 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1931 as_warn (_("extended instruction in delay slot"));
1932
1933 f = frag_more (4);
1934 }
1935
1936 fixp[0] = fixp[1] = fixp[2] = NULL;
1937 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
1938 {
1939 if (address_expr->X_op == O_constant)
1940 {
1941 unsigned long tmp;
1942
1943 switch (*reloc_type)
1944 {
1945 case BFD_RELOC_32:
1946 ip->insn_opcode |= address_expr->X_add_number;
1947 break;
1948
1949 case BFD_RELOC_MIPS_HIGHEST:
1950 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1951 tmp >>= 16;
1952 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1953 break;
1954
1955 case BFD_RELOC_MIPS_HIGHER:
1956 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1957 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1958 break;
1959
1960 case BFD_RELOC_HI16_S:
1961 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1962 >> 16) & 0xffff;
1963 break;
1964
1965 case BFD_RELOC_HI16:
1966 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1967 break;
1968
1969 case BFD_RELOC_LO16:
1970 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1971 break;
1972
1973 case BFD_RELOC_MIPS_JMP:
1974 if ((address_expr->X_add_number & 3) != 0)
1975 as_bad (_("jump to misaligned address (0x%lx)"),
1976 (unsigned long) address_expr->X_add_number);
1977 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1978 break;
1979
1980 case BFD_RELOC_MIPS16_JMP:
1981 if ((address_expr->X_add_number & 3) != 0)
1982 as_bad (_("jump to misaligned address (0x%lx)"),
1983 (unsigned long) address_expr->X_add_number);
1984 ip->insn_opcode |=
1985 (((address_expr->X_add_number & 0x7c0000) << 3)
1986 | ((address_expr->X_add_number & 0xf800000) >> 7)
1987 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1988 break;
1989
1990 case BFD_RELOC_16_PCREL:
1991 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1992 break;
1993
1994 case BFD_RELOC_16_PCREL_S2:
1995 goto need_reloc;
1996
1997 default:
1998 internalError ();
1999 }
2000 }
2001 else
2002 {
2003 need_reloc:
2004 /* Don't generate a reloc if we are writing into a variant frag. */
2005 if (place == NULL)
2006 {
2007 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2008 address_expr,
2009 (*reloc_type == BFD_RELOC_16_PCREL
2010 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2011 reloc_type[0]);
2012
2013 /* These relocations can have an addend that won't fit in
2014 4 octets for 64bit assembly. */
2015 if (HAVE_64BIT_GPRS &&
2016 (*reloc_type == BFD_RELOC_16
2017 || *reloc_type == BFD_RELOC_32
2018 || *reloc_type == BFD_RELOC_MIPS_JMP
2019 || *reloc_type == BFD_RELOC_HI16_S
2020 || *reloc_type == BFD_RELOC_LO16
2021 || *reloc_type == BFD_RELOC_GPREL16
2022 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2023 || *reloc_type == BFD_RELOC_GPREL32
2024 || *reloc_type == BFD_RELOC_64
2025 || *reloc_type == BFD_RELOC_CTOR
2026 || *reloc_type == BFD_RELOC_MIPS_SUB
2027 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2028 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2029 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2030 || *reloc_type == BFD_RELOC_MIPS_REL16
2031 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2032 fixp[0]->fx_no_overflow = 1;
2033
2034 if (unmatched_hi)
2035 {
2036 struct mips_hi_fixup *hi_fixup;
2037
2038 assert (*reloc_type == BFD_RELOC_HI16_S);
2039 hi_fixup = ((struct mips_hi_fixup *)
2040 xmalloc (sizeof (struct mips_hi_fixup)));
2041 hi_fixup->fixp = fixp[0];
2042 hi_fixup->seg = now_seg;
2043 hi_fixup->next = mips_hi_fixup_list;
2044 mips_hi_fixup_list = hi_fixup;
2045 }
2046
2047 if (reloc_type[1] != BFD_RELOC_UNUSED)
2048 {
2049 /* FIXME: This symbol can be one of
2050 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2051 address_expr->X_op = O_absent;
2052 address_expr->X_add_symbol = 0;
2053 address_expr->X_add_number = 0;
2054
2055 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2056 4, address_expr, false,
2057 reloc_type[1]);
2058
2059 /* These relocations can have an addend that won't fit in
2060 4 octets for 64bit assembly. */
2061 if (HAVE_64BIT_GPRS &&
2062 (*reloc_type == BFD_RELOC_16
2063 || *reloc_type == BFD_RELOC_32
2064 || *reloc_type == BFD_RELOC_MIPS_JMP
2065 || *reloc_type == BFD_RELOC_HI16_S
2066 || *reloc_type == BFD_RELOC_LO16
2067 || *reloc_type == BFD_RELOC_GPREL16
2068 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2069 || *reloc_type == BFD_RELOC_GPREL32
2070 || *reloc_type == BFD_RELOC_64
2071 || *reloc_type == BFD_RELOC_CTOR
2072 || *reloc_type == BFD_RELOC_MIPS_SUB
2073 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2074 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2075 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2076 || *reloc_type == BFD_RELOC_MIPS_REL16
2077 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2078 fixp[1]->fx_no_overflow = 1;
2079
2080 if (reloc_type[2] != BFD_RELOC_UNUSED)
2081 {
2082 address_expr->X_op = O_absent;
2083 address_expr->X_add_symbol = 0;
2084 address_expr->X_add_number = 0;
2085
2086 fixp[2] = fix_new_exp (frag_now,
2087 f - frag_now->fr_literal, 4,
2088 address_expr, false,
2089 reloc_type[2]);
2090
2091 /* These relocations can have an addend that won't fit in
2092 4 octets for 64bit assembly. */
2093 if (HAVE_64BIT_GPRS &&
2094 (*reloc_type == BFD_RELOC_16
2095 || *reloc_type == BFD_RELOC_32
2096 || *reloc_type == BFD_RELOC_MIPS_JMP
2097 || *reloc_type == BFD_RELOC_HI16_S
2098 || *reloc_type == BFD_RELOC_LO16
2099 || *reloc_type == BFD_RELOC_GPREL16
2100 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2101 || *reloc_type == BFD_RELOC_GPREL32
2102 || *reloc_type == BFD_RELOC_64
2103 || *reloc_type == BFD_RELOC_CTOR
2104 || *reloc_type == BFD_RELOC_MIPS_SUB
2105 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2106 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2107 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2108 || *reloc_type == BFD_RELOC_MIPS_REL16
2109 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2110 fixp[2]->fx_no_overflow = 1;
2111 }
2112 }
2113 }
2114 }
2115 }
2116
2117 if (! mips_opts.mips16)
2118 md_number_to_chars (f, ip->insn_opcode, 4);
2119 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
2120 {
2121 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2122 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2123 }
2124 else
2125 {
2126 if (ip->use_extend)
2127 {
2128 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2129 f += 2;
2130 }
2131 md_number_to_chars (f, ip->insn_opcode, 2);
2132 }
2133
2134 /* Update the register mask information. */
2135 if (! mips_opts.mips16)
2136 {
2137 if (pinfo & INSN_WRITE_GPR_D)
2138 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2139 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2140 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2141 if (pinfo & INSN_READ_GPR_S)
2142 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2143 if (pinfo & INSN_WRITE_GPR_31)
2144 mips_gprmask |= 1 << 31;
2145 if (pinfo & INSN_WRITE_FPR_D)
2146 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2147 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2148 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2149 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2150 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2151 if ((pinfo & INSN_READ_FPR_R) != 0)
2152 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2153 if (pinfo & INSN_COP)
2154 {
2155 /* We don't keep enough information to sort these cases out.
2156 The itbl support does keep this information however, although
2157 we currently don't support itbl fprmats as part of the cop
2158 instruction. May want to add this support in the future. */
2159 }
2160 /* Never set the bit for $0, which is always zero. */
2161 mips_gprmask &= ~1 << 0;
2162 }
2163 else
2164 {
2165 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2166 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2167 & MIPS16OP_MASK_RX);
2168 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2169 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2170 & MIPS16OP_MASK_RY);
2171 if (pinfo & MIPS16_INSN_WRITE_Z)
2172 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2173 & MIPS16OP_MASK_RZ);
2174 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2175 mips_gprmask |= 1 << TREG;
2176 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2177 mips_gprmask |= 1 << SP;
2178 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2179 mips_gprmask |= 1 << RA;
2180 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2181 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2182 if (pinfo & MIPS16_INSN_READ_Z)
2183 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2184 & MIPS16OP_MASK_MOVE32Z);
2185 if (pinfo & MIPS16_INSN_READ_GPR_X)
2186 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2187 & MIPS16OP_MASK_REGR32);
2188 }
2189
2190 if (place == NULL && ! mips_opts.noreorder)
2191 {
2192 /* Filling the branch delay slot is more complex. We try to
2193 switch the branch with the previous instruction, which we can
2194 do if the previous instruction does not set up a condition
2195 that the branch tests and if the branch is not itself the
2196 target of any branch. */
2197 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2198 || (pinfo & INSN_COND_BRANCH_DELAY))
2199 {
2200 if (mips_optimize < 2
2201 /* If we have seen .set volatile or .set nomove, don't
2202 optimize. */
2203 || mips_opts.nomove != 0
2204 /* If we had to emit any NOP instructions, then we
2205 already know we can not swap. */
2206 || nops != 0
2207 /* If we don't even know the previous insn, we can not
2208 swap. */
2209 || ! prev_insn_valid
2210 /* If the previous insn is already in a branch delay
2211 slot, then we can not swap. */
2212 || prev_insn_is_delay_slot
2213 /* If the previous previous insn was in a .set
2214 noreorder, we can't swap. Actually, the MIPS
2215 assembler will swap in this situation. However, gcc
2216 configured -with-gnu-as will generate code like
2217 .set noreorder
2218 lw $4,XXX
2219 .set reorder
2220 INSN
2221 bne $4,$0,foo
2222 in which we can not swap the bne and INSN. If gcc is
2223 not configured -with-gnu-as, it does not output the
2224 .set pseudo-ops. We don't have to check
2225 prev_insn_unreordered, because prev_insn_valid will
2226 be 0 in that case. We don't want to use
2227 prev_prev_insn_valid, because we do want to be able
2228 to swap at the start of a function. */
2229 || prev_prev_insn_unreordered
2230 /* If the branch is itself the target of a branch, we
2231 can not swap. We cheat on this; all we check for is
2232 whether there is a label on this instruction. If
2233 there are any branches to anything other than a
2234 label, users must use .set noreorder. */
2235 || insn_labels != NULL
2236 /* If the previous instruction is in a variant frag, we
2237 can not do the swap. This does not apply to the
2238 mips16, which uses variant frags for different
2239 purposes. */
2240 || (! mips_opts.mips16
2241 && prev_insn_frag->fr_type == rs_machine_dependent)
2242 /* If the branch reads the condition codes, we don't
2243 even try to swap, because in the sequence
2244 ctc1 $X,$31
2245 INSN
2246 INSN
2247 bc1t LABEL
2248 we can not swap, and I don't feel like handling that
2249 case. */
2250 || (! mips_opts.mips16
2251 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2252 && (pinfo & INSN_READ_COND_CODE))
2253 /* We can not swap with an instruction that requires a
2254 delay slot, becase the target of the branch might
2255 interfere with that instruction. */
2256 || (! mips_opts.mips16
2257 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2258 && (prev_pinfo
2259 /* Itbl support may require additional care here. */
2260 & (INSN_LOAD_COPROC_DELAY
2261 | INSN_COPROC_MOVE_DELAY
2262 | INSN_WRITE_COND_CODE)))
2263 || (! (hilo_interlocks
2264 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
2265 && (prev_pinfo
2266 & (INSN_READ_LO
2267 | INSN_READ_HI)))
2268 || (! mips_opts.mips16
2269 && ! gpr_interlocks
2270 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2271 || (! mips_opts.mips16
2272 && mips_opts.isa == ISA_MIPS1
2273 /* Itbl support may require additional care here. */
2274 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2275 /* We can not swap with a branch instruction. */
2276 || (prev_pinfo
2277 & (INSN_UNCOND_BRANCH_DELAY
2278 | INSN_COND_BRANCH_DELAY
2279 | INSN_COND_BRANCH_LIKELY))
2280 /* We do not swap with a trap instruction, since it
2281 complicates trap handlers to have the trap
2282 instruction be in a delay slot. */
2283 || (prev_pinfo & INSN_TRAP)
2284 /* If the branch reads a register that the previous
2285 instruction sets, we can not swap. */
2286 || (! mips_opts.mips16
2287 && (prev_pinfo & INSN_WRITE_GPR_T)
2288 && insn_uses_reg (ip,
2289 ((prev_insn.insn_opcode >> OP_SH_RT)
2290 & OP_MASK_RT),
2291 MIPS_GR_REG))
2292 || (! mips_opts.mips16
2293 && (prev_pinfo & INSN_WRITE_GPR_D)
2294 && insn_uses_reg (ip,
2295 ((prev_insn.insn_opcode >> OP_SH_RD)
2296 & OP_MASK_RD),
2297 MIPS_GR_REG))
2298 || (mips_opts.mips16
2299 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2300 && insn_uses_reg (ip,
2301 ((prev_insn.insn_opcode
2302 >> MIPS16OP_SH_RX)
2303 & MIPS16OP_MASK_RX),
2304 MIPS16_REG))
2305 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2306 && insn_uses_reg (ip,
2307 ((prev_insn.insn_opcode
2308 >> MIPS16OP_SH_RY)
2309 & MIPS16OP_MASK_RY),
2310 MIPS16_REG))
2311 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2312 && insn_uses_reg (ip,
2313 ((prev_insn.insn_opcode
2314 >> MIPS16OP_SH_RZ)
2315 & MIPS16OP_MASK_RZ),
2316 MIPS16_REG))
2317 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2318 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2319 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2320 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2321 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2322 && insn_uses_reg (ip,
2323 MIPS16OP_EXTRACT_REG32R (prev_insn.
2324 insn_opcode),
2325 MIPS_GR_REG))))
2326 /* If the branch writes a register that the previous
2327 instruction sets, we can not swap (we know that
2328 branches write only to RD or to $31). */
2329 || (! mips_opts.mips16
2330 && (prev_pinfo & INSN_WRITE_GPR_T)
2331 && (((pinfo & INSN_WRITE_GPR_D)
2332 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2333 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2334 || ((pinfo & INSN_WRITE_GPR_31)
2335 && (((prev_insn.insn_opcode >> OP_SH_RT)
2336 & OP_MASK_RT)
2337 == 31))))
2338 || (! mips_opts.mips16
2339 && (prev_pinfo & INSN_WRITE_GPR_D)
2340 && (((pinfo & INSN_WRITE_GPR_D)
2341 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2342 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2343 || ((pinfo & INSN_WRITE_GPR_31)
2344 && (((prev_insn.insn_opcode >> OP_SH_RD)
2345 & OP_MASK_RD)
2346 == 31))))
2347 || (mips_opts.mips16
2348 && (pinfo & MIPS16_INSN_WRITE_31)
2349 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2350 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2351 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2352 == RA))))
2353 /* If the branch writes a register that the previous
2354 instruction reads, we can not swap (we know that
2355 branches only write to RD or to $31). */
2356 || (! mips_opts.mips16
2357 && (pinfo & INSN_WRITE_GPR_D)
2358 && insn_uses_reg (&prev_insn,
2359 ((ip->insn_opcode >> OP_SH_RD)
2360 & OP_MASK_RD),
2361 MIPS_GR_REG))
2362 || (! mips_opts.mips16
2363 && (pinfo & INSN_WRITE_GPR_31)
2364 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2365 || (mips_opts.mips16
2366 && (pinfo & MIPS16_INSN_WRITE_31)
2367 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2368 /* If we are generating embedded PIC code, the branch
2369 might be expanded into a sequence which uses $at, so
2370 we can't swap with an instruction which reads it. */
2371 || (mips_pic == EMBEDDED_PIC
2372 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2373 /* If the previous previous instruction has a load
2374 delay, and sets a register that the branch reads, we
2375 can not swap. */
2376 || (! mips_opts.mips16
2377 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2378 /* Itbl support may require additional care here. */
2379 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2380 || (! gpr_interlocks
2381 && (prev_prev_insn.insn_mo->pinfo
2382 & INSN_LOAD_MEMORY_DELAY)))
2383 && insn_uses_reg (ip,
2384 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2385 & OP_MASK_RT),
2386 MIPS_GR_REG))
2387 /* If one instruction sets a condition code and the
2388 other one uses a condition code, we can not swap. */
2389 || ((pinfo & INSN_READ_COND_CODE)
2390 && (prev_pinfo & INSN_WRITE_COND_CODE))
2391 || ((pinfo & INSN_WRITE_COND_CODE)
2392 && (prev_pinfo & INSN_READ_COND_CODE))
2393 /* If the previous instruction uses the PC, we can not
2394 swap. */
2395 || (mips_opts.mips16
2396 && (prev_pinfo & MIPS16_INSN_READ_PC))
2397 /* If the previous instruction was extended, we can not
2398 swap. */
2399 || (mips_opts.mips16 && prev_insn_extended)
2400 /* If the previous instruction had a fixup in mips16
2401 mode, we can not swap. This normally means that the
2402 previous instruction was a 4 byte branch anyhow. */
2403 || (mips_opts.mips16 && prev_insn_fixp[0])
2404 /* If the previous instruction is a sync, sync.l, or
2405 sync.p, we can not swap. */
2406 || (prev_pinfo & INSN_SYNC))
2407 {
2408 /* We could do even better for unconditional branches to
2409 portions of this object file; we could pick up the
2410 instruction at the destination, put it in the delay
2411 slot, and bump the destination address. */
2412 emit_nop ();
2413 /* Update the previous insn information. */
2414 prev_prev_insn = *ip;
2415 prev_insn.insn_mo = &dummy_opcode;
2416 }
2417 else
2418 {
2419 /* It looks like we can actually do the swap. */
2420 if (! mips_opts.mips16)
2421 {
2422 char *prev_f;
2423 char temp[4];
2424
2425 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2426 memcpy (temp, prev_f, 4);
2427 memcpy (prev_f, f, 4);
2428 memcpy (f, temp, 4);
2429 if (prev_insn_fixp[0])
2430 {
2431 prev_insn_fixp[0]->fx_frag = frag_now;
2432 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2433 }
2434 if (prev_insn_fixp[1])
2435 {
2436 prev_insn_fixp[1]->fx_frag = frag_now;
2437 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2438 }
2439 if (prev_insn_fixp[2])
2440 {
2441 prev_insn_fixp[2]->fx_frag = frag_now;
2442 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
2443 }
2444 if (fixp[0])
2445 {
2446 fixp[0]->fx_frag = prev_insn_frag;
2447 fixp[0]->fx_where = prev_insn_where;
2448 }
2449 if (fixp[1])
2450 {
2451 fixp[1]->fx_frag = prev_insn_frag;
2452 fixp[1]->fx_where = prev_insn_where;
2453 }
2454 if (fixp[2])
2455 {
2456 fixp[2]->fx_frag = prev_insn_frag;
2457 fixp[2]->fx_where = prev_insn_where;
2458 }
2459 }
2460 else
2461 {
2462 char *prev_f;
2463 char temp[2];
2464
2465 assert (prev_insn_fixp[0] == NULL);
2466 assert (prev_insn_fixp[1] == NULL);
2467 assert (prev_insn_fixp[2] == NULL);
2468 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2469 memcpy (temp, prev_f, 2);
2470 memcpy (prev_f, f, 2);
2471 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
2472 {
2473 assert (*reloc_type == BFD_RELOC_UNUSED);
2474 memcpy (f, temp, 2);
2475 }
2476 else
2477 {
2478 memcpy (f, f + 2, 2);
2479 memcpy (f + 2, temp, 2);
2480 }
2481 if (fixp[0])
2482 {
2483 fixp[0]->fx_frag = prev_insn_frag;
2484 fixp[0]->fx_where = prev_insn_where;
2485 }
2486 if (fixp[1])
2487 {
2488 fixp[1]->fx_frag = prev_insn_frag;
2489 fixp[1]->fx_where = prev_insn_where;
2490 }
2491 if (fixp[2])
2492 {
2493 fixp[2]->fx_frag = prev_insn_frag;
2494 fixp[2]->fx_where = prev_insn_where;
2495 }
2496 }
2497
2498 /* Update the previous insn information; leave prev_insn
2499 unchanged. */
2500 prev_prev_insn = *ip;
2501 }
2502 prev_insn_is_delay_slot = 1;
2503
2504 /* If that was an unconditional branch, forget the previous
2505 insn information. */
2506 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2507 {
2508 prev_prev_insn.insn_mo = &dummy_opcode;
2509 prev_insn.insn_mo = &dummy_opcode;
2510 }
2511
2512 prev_insn_fixp[0] = NULL;
2513 prev_insn_fixp[1] = NULL;
2514 prev_insn_fixp[2] = NULL;
2515 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2516 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2517 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2518 prev_insn_extended = 0;
2519 }
2520 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2521 {
2522 /* We don't yet optimize a branch likely. What we should do
2523 is look at the target, copy the instruction found there
2524 into the delay slot, and increment the branch to jump to
2525 the next instruction. */
2526 emit_nop ();
2527 /* Update the previous insn information. */
2528 prev_prev_insn = *ip;
2529 prev_insn.insn_mo = &dummy_opcode;
2530 prev_insn_fixp[0] = NULL;
2531 prev_insn_fixp[1] = NULL;
2532 prev_insn_fixp[2] = NULL;
2533 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2534 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2535 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2536 prev_insn_extended = 0;
2537 }
2538 else
2539 {
2540 /* Update the previous insn information. */
2541 if (nops > 0)
2542 prev_prev_insn.insn_mo = &dummy_opcode;
2543 else
2544 prev_prev_insn = prev_insn;
2545 prev_insn = *ip;
2546
2547 /* Any time we see a branch, we always fill the delay slot
2548 immediately; since this insn is not a branch, we know it
2549 is not in a delay slot. */
2550 prev_insn_is_delay_slot = 0;
2551
2552 prev_insn_fixp[0] = fixp[0];
2553 prev_insn_fixp[1] = fixp[1];
2554 prev_insn_fixp[2] = fixp[2];
2555 prev_insn_reloc_type[0] = reloc_type[0];
2556 prev_insn_reloc_type[1] = reloc_type[1];
2557 prev_insn_reloc_type[2] = reloc_type[2];
2558 if (mips_opts.mips16)
2559 prev_insn_extended = (ip->use_extend
2560 || *reloc_type > BFD_RELOC_UNUSED);
2561 }
2562
2563 prev_prev_insn_unreordered = prev_insn_unreordered;
2564 prev_insn_unreordered = 0;
2565 prev_insn_frag = frag_now;
2566 prev_insn_where = f - frag_now->fr_literal;
2567 prev_insn_valid = 1;
2568 }
2569 else if (place == NULL)
2570 {
2571 /* We need to record a bit of information even when we are not
2572 reordering, in order to determine the base address for mips16
2573 PC relative relocs. */
2574 prev_prev_insn = prev_insn;
2575 prev_insn = *ip;
2576 prev_insn_reloc_type[0] = reloc_type[0];
2577 prev_insn_reloc_type[1] = reloc_type[1];
2578 prev_insn_reloc_type[2] = reloc_type[2];
2579 prev_prev_insn_unreordered = prev_insn_unreordered;
2580 prev_insn_unreordered = 1;
2581 }
2582
2583 /* We just output an insn, so the next one doesn't have a label. */
2584 mips_clear_insn_labels ();
2585
2586 /* We must ensure that a fixup associated with an unmatched %hi
2587 reloc does not become a variant frag. Otherwise, the
2588 rearrangement of %hi relocs in frob_file may confuse
2589 tc_gen_reloc. */
2590 if (unmatched_hi)
2591 {
2592 frag_wane (frag_now);
2593 frag_new (0);
2594 }
2595 }
2596
2597 /* This function forgets that there was any previous instruction or
2598 label. If PRESERVE is non-zero, it remembers enough information to
2599 know whether nops are needed before a noreorder section. */
2600
2601 static void
2602 mips_no_prev_insn (preserve)
2603 int preserve;
2604 {
2605 if (! preserve)
2606 {
2607 prev_insn.insn_mo = &dummy_opcode;
2608 prev_prev_insn.insn_mo = &dummy_opcode;
2609 prev_nop_frag = NULL;
2610 prev_nop_frag_holds = 0;
2611 prev_nop_frag_required = 0;
2612 prev_nop_frag_since = 0;
2613 }
2614 prev_insn_valid = 0;
2615 prev_insn_is_delay_slot = 0;
2616 prev_insn_unreordered = 0;
2617 prev_insn_extended = 0;
2618 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2619 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2620 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
2621 prev_prev_insn_unreordered = 0;
2622 mips_clear_insn_labels ();
2623 }
2624
2625 /* This function must be called whenever we turn on noreorder or emit
2626 something other than instructions. It inserts any NOPS which might
2627 be needed by the previous instruction, and clears the information
2628 kept for the previous instructions. The INSNS parameter is true if
2629 instructions are to follow. */
2630
2631 static void
2632 mips_emit_delays (insns)
2633 boolean insns;
2634 {
2635 if (! mips_opts.noreorder)
2636 {
2637 int nops;
2638
2639 nops = 0;
2640 if ((! mips_opts.mips16
2641 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2642 && (! cop_interlocks
2643 && (prev_insn.insn_mo->pinfo
2644 & (INSN_LOAD_COPROC_DELAY
2645 | INSN_COPROC_MOVE_DELAY
2646 | INSN_WRITE_COND_CODE))))
2647 || (! hilo_interlocks
2648 && (prev_insn.insn_mo->pinfo
2649 & (INSN_READ_LO
2650 | INSN_READ_HI)))
2651 || (! mips_opts.mips16
2652 && ! gpr_interlocks
2653 && (prev_insn.insn_mo->pinfo
2654 & INSN_LOAD_MEMORY_DELAY))
2655 || (! mips_opts.mips16
2656 && mips_opts.isa == ISA_MIPS1
2657 && (prev_insn.insn_mo->pinfo
2658 & INSN_COPROC_MEMORY_DELAY)))
2659 {
2660 /* Itbl support may require additional care here. */
2661 ++nops;
2662 if ((! mips_opts.mips16
2663 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2664 && (! cop_interlocks
2665 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2666 || (! hilo_interlocks
2667 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2668 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2669 ++nops;
2670
2671 if (prev_insn_unreordered)
2672 nops = 0;
2673 }
2674 else if ((! mips_opts.mips16
2675 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
2676 && (! cop_interlocks
2677 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2678 || (! hilo_interlocks
2679 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2680 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2681 {
2682 /* Itbl support may require additional care here. */
2683 if (! prev_prev_insn_unreordered)
2684 ++nops;
2685 }
2686
2687 if (nops > 0)
2688 {
2689 struct insn_label_list *l;
2690
2691 if (insns)
2692 {
2693 /* Record the frag which holds the nop instructions, so
2694 that we can remove them if we don't need them. */
2695 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2696 prev_nop_frag = frag_now;
2697 prev_nop_frag_holds = nops;
2698 prev_nop_frag_required = 0;
2699 prev_nop_frag_since = 0;
2700 }
2701
2702 for (; nops > 0; --nops)
2703 emit_nop ();
2704
2705 if (insns)
2706 {
2707 /* Move on to a new frag, so that it is safe to simply
2708 decrease the size of prev_nop_frag. */
2709 frag_wane (frag_now);
2710 frag_new (0);
2711 }
2712
2713 for (l = insn_labels; l != NULL; l = l->next)
2714 {
2715 valueT val;
2716
2717 assert (S_GET_SEGMENT (l->label) == now_seg);
2718 symbol_set_frag (l->label, frag_now);
2719 val = (valueT) frag_now_fix ();
2720 /* mips16 text labels are stored as odd. */
2721 if (mips_opts.mips16)
2722 val += 1;
2723 S_SET_VALUE (l->label, val);
2724 }
2725 }
2726 }
2727
2728 /* Mark instruction labels in mips16 mode. */
2729 if (mips_opts.mips16 && insns)
2730 mips16_mark_labels ();
2731
2732 mips_no_prev_insn (insns);
2733 }
2734
2735 /* Build an instruction created by a macro expansion. This is passed
2736 a pointer to the count of instructions created so far, an
2737 expression, the name of the instruction to build, an operand format
2738 string, and corresponding arguments. */
2739
2740 #ifdef USE_STDARG
2741 static void
2742 macro_build (char *place,
2743 int *counter,
2744 expressionS * ep,
2745 const char *name,
2746 const char *fmt,
2747 ...)
2748 #else
2749 static void
2750 macro_build (place, counter, ep, name, fmt, va_alist)
2751 char *place;
2752 int *counter;
2753 expressionS *ep;
2754 const char *name;
2755 const char *fmt;
2756 va_dcl
2757 #endif
2758 {
2759 struct mips_cl_insn insn;
2760 bfd_reloc_code_real_type r[3];
2761 va_list args;
2762
2763 #ifdef USE_STDARG
2764 va_start (args, fmt);
2765 #else
2766 va_start (args);
2767 #endif
2768
2769 /*
2770 * If the macro is about to expand into a second instruction,
2771 * print a warning if needed. We need to pass ip as a parameter
2772 * to generate a better warning message here...
2773 */
2774 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2775 as_warn (_("Macro instruction expanded into multiple instructions"));
2776
2777 if (place == NULL)
2778 *counter += 1; /* bump instruction counter */
2779
2780 if (mips_opts.mips16)
2781 {
2782 mips16_macro_build (place, counter, ep, name, fmt, args);
2783 va_end (args);
2784 return;
2785 }
2786
2787 r[0] = BFD_RELOC_UNUSED;
2788 r[1] = BFD_RELOC_UNUSED;
2789 r[2] = BFD_RELOC_UNUSED;
2790 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2791 assert (insn.insn_mo);
2792 assert (strcmp (name, insn.insn_mo->name) == 0);
2793
2794 /* Search until we get a match for NAME. */
2795 while (1)
2796 {
2797 if (strcmp (fmt, insn.insn_mo->args) == 0
2798 && insn.insn_mo->pinfo != INSN_MACRO
2799 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
2800 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
2801 break;
2802
2803 ++insn.insn_mo;
2804 assert (insn.insn_mo->name);
2805 assert (strcmp (name, insn.insn_mo->name) == 0);
2806 }
2807
2808 insn.insn_opcode = insn.insn_mo->match;
2809 for (;;)
2810 {
2811 switch (*fmt++)
2812 {
2813 case '\0':
2814 break;
2815
2816 case ',':
2817 case '(':
2818 case ')':
2819 continue;
2820
2821 case 't':
2822 case 'w':
2823 case 'E':
2824 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
2825 continue;
2826
2827 case 'c':
2828 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2829 continue;
2830
2831 case 'T':
2832 case 'W':
2833 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
2834 continue;
2835
2836 case 'd':
2837 case 'G':
2838 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
2839 continue;
2840
2841 case 'U':
2842 {
2843 int tmp = va_arg (args, int);
2844
2845 insn.insn_opcode |= tmp << OP_SH_RT;
2846 insn.insn_opcode |= tmp << OP_SH_RD;
2847 continue;
2848 }
2849
2850 case 'V':
2851 case 'S':
2852 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
2853 continue;
2854
2855 case 'z':
2856 continue;
2857
2858 case '<':
2859 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
2860 continue;
2861
2862 case 'D':
2863 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
2864 continue;
2865
2866 case 'B':
2867 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
2868 continue;
2869
2870 case 'J':
2871 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
2872 continue;
2873
2874 case 'q':
2875 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
2876 continue;
2877
2878 case 'b':
2879 case 's':
2880 case 'r':
2881 case 'v':
2882 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
2883 continue;
2884
2885 case 'i':
2886 case 'j':
2887 case 'o':
2888 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2889 assert (*r == BFD_RELOC_GPREL16
2890 || *r == BFD_RELOC_MIPS_LITERAL
2891 || *r == BFD_RELOC_MIPS_HIGHER
2892 || *r == BFD_RELOC_HI16_S
2893 || *r == BFD_RELOC_LO16
2894 || *r == BFD_RELOC_MIPS_GOT16
2895 || *r == BFD_RELOC_MIPS_CALL16
2896 || *r == BFD_RELOC_MIPS_GOT_LO16
2897 || *r == BFD_RELOC_MIPS_CALL_LO16
2898 || (ep->X_op == O_subtract
2899 && *r == BFD_RELOC_PCREL_LO16));
2900 continue;
2901
2902 case 'u':
2903 *r = (bfd_reloc_code_real_type) va_arg (args, int);
2904 assert (ep != NULL
2905 && (ep->X_op == O_constant
2906 || (ep->X_op == O_symbol
2907 && (*r == BFD_RELOC_MIPS_HIGHEST
2908 || *r == BFD_RELOC_HI16_S
2909 || *r == BFD_RELOC_HI16
2910 || *r == BFD_RELOC_GPREL16
2911 || *r == BFD_RELOC_MIPS_GOT_HI16
2912 || *r == BFD_RELOC_MIPS_CALL_HI16))
2913 || (ep->X_op == O_subtract
2914 && *r == BFD_RELOC_PCREL_HI16_S)));
2915 continue;
2916
2917 case 'p':
2918 assert (ep != NULL);
2919 /*
2920 * This allows macro() to pass an immediate expression for
2921 * creating short branches without creating a symbol.
2922 * Note that the expression still might come from the assembly
2923 * input, in which case the value is not checked for range nor
2924 * is a relocation entry generated (yuck).
2925 */
2926 if (ep->X_op == O_constant)
2927 {
2928 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2929 ep = NULL;
2930 }
2931 else
2932 if (mips_pic == EMBEDDED_PIC)
2933 *r = BFD_RELOC_16_PCREL_S2;
2934 else
2935 *r = BFD_RELOC_16_PCREL;
2936 continue;
2937
2938 case 'a':
2939 assert (ep != NULL);
2940 *r = BFD_RELOC_MIPS_JMP;
2941 continue;
2942
2943 case 'C':
2944 insn.insn_opcode |= va_arg (args, unsigned long);
2945 continue;
2946
2947 default:
2948 internalError ();
2949 }
2950 break;
2951 }
2952 va_end (args);
2953 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2954
2955 append_insn (place, &insn, ep, r, false);
2956 }
2957
2958 static void
2959 mips16_macro_build (place, counter, ep, name, fmt, args)
2960 char *place;
2961 int *counter ATTRIBUTE_UNUSED;
2962 expressionS *ep;
2963 const char *name;
2964 const char *fmt;
2965 va_list args;
2966 {
2967 struct mips_cl_insn insn;
2968 bfd_reloc_code_real_type r[3]
2969 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
2970
2971 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2972 assert (insn.insn_mo);
2973 assert (strcmp (name, insn.insn_mo->name) == 0);
2974
2975 while (strcmp (fmt, insn.insn_mo->args) != 0
2976 || insn.insn_mo->pinfo == INSN_MACRO)
2977 {
2978 ++insn.insn_mo;
2979 assert (insn.insn_mo->name);
2980 assert (strcmp (name, insn.insn_mo->name) == 0);
2981 }
2982
2983 insn.insn_opcode = insn.insn_mo->match;
2984 insn.use_extend = false;
2985
2986 for (;;)
2987 {
2988 int c;
2989
2990 c = *fmt++;
2991 switch (c)
2992 {
2993 case '\0':
2994 break;
2995
2996 case ',':
2997 case '(':
2998 case ')':
2999 continue;
3000
3001 case 'y':
3002 case 'w':
3003 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3004 continue;
3005
3006 case 'x':
3007 case 'v':
3008 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3009 continue;
3010
3011 case 'z':
3012 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3013 continue;
3014
3015 case 'Z':
3016 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3017 continue;
3018
3019 case '0':
3020 case 'S':
3021 case 'P':
3022 case 'R':
3023 continue;
3024
3025 case 'X':
3026 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3027 continue;
3028
3029 case 'Y':
3030 {
3031 int regno;
3032
3033 regno = va_arg (args, int);
3034 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3035 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3036 }
3037 continue;
3038
3039 case '<':
3040 case '>':
3041 case '4':
3042 case '5':
3043 case 'H':
3044 case 'W':
3045 case 'D':
3046 case 'j':
3047 case '8':
3048 case 'V':
3049 case 'C':
3050 case 'U':
3051 case 'k':
3052 case 'K':
3053 case 'p':
3054 case 'q':
3055 {
3056 assert (ep != NULL);
3057
3058 if (ep->X_op != O_constant)
3059 *r = BFD_RELOC_UNUSED + c;
3060 else
3061 {
3062 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3063 false, &insn.insn_opcode, &insn.use_extend,
3064 &insn.extend);
3065 ep = NULL;
3066 *r = BFD_RELOC_UNUSED;
3067 }
3068 }
3069 continue;
3070
3071 case '6':
3072 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3073 continue;
3074 }
3075
3076 break;
3077 }
3078
3079 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
3080
3081 append_insn (place, &insn, ep, r, false);
3082 }
3083
3084 /*
3085 * Generate a "lui" instruction.
3086 */
3087 static void
3088 macro_build_lui (place, counter, ep, regnum)
3089 char *place;
3090 int *counter;
3091 expressionS *ep;
3092 int regnum;
3093 {
3094 expressionS high_expr;
3095 struct mips_cl_insn insn;
3096 bfd_reloc_code_real_type r[3]
3097 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
3098 CONST char *name = "lui";
3099 CONST char *fmt = "t,u";
3100
3101 assert (! mips_opts.mips16);
3102
3103 if (place == NULL)
3104 high_expr = *ep;
3105 else
3106 {
3107 high_expr.X_op = O_constant;
3108 high_expr.X_add_number = ep->X_add_number;
3109 }
3110
3111 if (high_expr.X_op == O_constant)
3112 {
3113 /* we can compute the instruction now without a relocation entry */
3114 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3115 >> 16) & 0xffff;
3116 *r = BFD_RELOC_UNUSED;
3117 }
3118 else if (! HAVE_NEWABI)
3119 {
3120 assert (ep->X_op == O_symbol);
3121 /* _gp_disp is a special case, used from s_cpload. */
3122 assert (mips_pic == NO_PIC
3123 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
3124 *r = BFD_RELOC_HI16_S;
3125 }
3126
3127 /*
3128 * If the macro is about to expand into a second instruction,
3129 * print a warning if needed. We need to pass ip as a parameter
3130 * to generate a better warning message here...
3131 */
3132 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3133 as_warn (_("Macro instruction expanded into multiple instructions"));
3134
3135 if (place == NULL)
3136 *counter += 1; /* bump instruction counter */
3137
3138 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3139 assert (insn.insn_mo);
3140 assert (strcmp (name, insn.insn_mo->name) == 0);
3141 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3142
3143 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
3144 if (*r == BFD_RELOC_UNUSED)
3145 {
3146 insn.insn_opcode |= high_expr.X_add_number;
3147 append_insn (place, &insn, NULL, r, false);
3148 }
3149 else
3150 append_insn (place, &insn, &high_expr, r, false);
3151 }
3152
3153 /* set_at()
3154 * Generates code to set the $at register to true (one)
3155 * if reg is less than the immediate expression.
3156 */
3157 static void
3158 set_at (counter, reg, unsignedp)
3159 int *counter;
3160 int reg;
3161 int unsignedp;
3162 {
3163 if (imm_expr.X_op == O_constant
3164 && imm_expr.X_add_number >= -0x8000
3165 && imm_expr.X_add_number < 0x8000)
3166 macro_build ((char *) NULL, counter, &imm_expr,
3167 unsignedp ? "sltiu" : "slti",
3168 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3169 else
3170 {
3171 load_register (counter, AT, &imm_expr, 0);
3172 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3173 unsignedp ? "sltu" : "slt",
3174 "d,v,t", AT, reg, AT);
3175 }
3176 }
3177
3178 /* Warn if an expression is not a constant. */
3179
3180 static void
3181 check_absolute_expr (ip, ex)
3182 struct mips_cl_insn *ip;
3183 expressionS *ex;
3184 {
3185 if (ex->X_op == O_big)
3186 as_bad (_("unsupported large constant"));
3187 else if (ex->X_op != O_constant)
3188 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3189 }
3190
3191 /* Count the leading zeroes by performing a binary chop. This is a
3192 bulky bit of source, but performance is a LOT better for the
3193 majority of values than a simple loop to count the bits:
3194 for (lcnt = 0; (lcnt < 32); lcnt++)
3195 if ((v) & (1 << (31 - lcnt)))
3196 break;
3197 However it is not code size friendly, and the gain will drop a bit
3198 on certain cached systems.
3199 */
3200 #define COUNT_TOP_ZEROES(v) \
3201 (((v) & ~0xffff) == 0 \
3202 ? ((v) & ~0xff) == 0 \
3203 ? ((v) & ~0xf) == 0 \
3204 ? ((v) & ~0x3) == 0 \
3205 ? ((v) & ~0x1) == 0 \
3206 ? !(v) \
3207 ? 32 \
3208 : 31 \
3209 : 30 \
3210 : ((v) & ~0x7) == 0 \
3211 ? 29 \
3212 : 28 \
3213 : ((v) & ~0x3f) == 0 \
3214 ? ((v) & ~0x1f) == 0 \
3215 ? 27 \
3216 : 26 \
3217 : ((v) & ~0x7f) == 0 \
3218 ? 25 \
3219 : 24 \
3220 : ((v) & ~0xfff) == 0 \
3221 ? ((v) & ~0x3ff) == 0 \
3222 ? ((v) & ~0x1ff) == 0 \
3223 ? 23 \
3224 : 22 \
3225 : ((v) & ~0x7ff) == 0 \
3226 ? 21 \
3227 : 20 \
3228 : ((v) & ~0x3fff) == 0 \
3229 ? ((v) & ~0x1fff) == 0 \
3230 ? 19 \
3231 : 18 \
3232 : ((v) & ~0x7fff) == 0 \
3233 ? 17 \
3234 : 16 \
3235 : ((v) & ~0xffffff) == 0 \
3236 ? ((v) & ~0xfffff) == 0 \
3237 ? ((v) & ~0x3ffff) == 0 \
3238 ? ((v) & ~0x1ffff) == 0 \
3239 ? 15 \
3240 : 14 \
3241 : ((v) & ~0x7ffff) == 0 \
3242 ? 13 \
3243 : 12 \
3244 : ((v) & ~0x3fffff) == 0 \
3245 ? ((v) & ~0x1fffff) == 0 \
3246 ? 11 \
3247 : 10 \
3248 : ((v) & ~0x7fffff) == 0 \
3249 ? 9 \
3250 : 8 \
3251 : ((v) & ~0xfffffff) == 0 \
3252 ? ((v) & ~0x3ffffff) == 0 \
3253 ? ((v) & ~0x1ffffff) == 0 \
3254 ? 7 \
3255 : 6 \
3256 : ((v) & ~0x7ffffff) == 0 \
3257 ? 5 \
3258 : 4 \
3259 : ((v) & ~0x3fffffff) == 0 \
3260 ? ((v) & ~0x1fffffff) == 0 \
3261 ? 3 \
3262 : 2 \
3263 : ((v) & ~0x7fffffff) == 0 \
3264 ? 1 \
3265 : 0)
3266
3267 /* load_register()
3268 * This routine generates the least number of instructions neccessary to load
3269 * an absolute expression value into a register.
3270 */
3271 static void
3272 load_register (counter, reg, ep, dbl)
3273 int *counter;
3274 int reg;
3275 expressionS *ep;
3276 int dbl;
3277 {
3278 int freg;
3279 expressionS hi32, lo32;
3280
3281 if (ep->X_op != O_big)
3282 {
3283 assert (ep->X_op == O_constant);
3284 if (ep->X_add_number < 0x8000
3285 && (ep->X_add_number >= 0
3286 || (ep->X_add_number >= -0x8000
3287 && (! dbl
3288 || ! ep->X_unsigned
3289 || sizeof (ep->X_add_number) > 4))))
3290 {
3291 /* We can handle 16 bit signed values with an addiu to
3292 $zero. No need to ever use daddiu here, since $zero and
3293 the result are always correct in 32 bit mode. */
3294 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3295 (int) BFD_RELOC_LO16);
3296 return;
3297 }
3298 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3299 {
3300 /* We can handle 16 bit unsigned values with an ori to
3301 $zero. */
3302 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3303 (int) BFD_RELOC_LO16);
3304 return;
3305 }
3306 else if ((((ep->X_add_number &~ (offsetT) 0x7fffffff) == 0
3307 || ((ep->X_add_number &~ (offsetT) 0x7fffffff)
3308 == ~ (offsetT) 0x7fffffff))
3309 && (! dbl
3310 || ! ep->X_unsigned
3311 || sizeof (ep->X_add_number) > 4
3312 || (ep->X_add_number & 0x80000000) == 0))
3313 || ((HAVE_32BIT_GPRS || ! dbl)
3314 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
3315 || (HAVE_32BIT_GPRS
3316 && ! dbl
3317 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3318 == ~ (offsetT) 0xffffffff)))
3319 {
3320 /* 32 bit values require an lui. */
3321 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3322 (int) BFD_RELOC_HI16);
3323 if ((ep->X_add_number & 0xffff) != 0)
3324 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3325 (int) BFD_RELOC_LO16);
3326 return;
3327 }
3328 }
3329
3330 /* The value is larger than 32 bits. */
3331
3332 if (HAVE_32BIT_GPRS)
3333 {
3334 as_bad (_("Number larger than 32 bits"));
3335 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3336 (int) BFD_RELOC_LO16);
3337 return;
3338 }
3339
3340 if (ep->X_op != O_big)
3341 {
3342 hi32 = *ep;
3343 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3344 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3345 hi32.X_add_number &= 0xffffffff;
3346 lo32 = *ep;
3347 lo32.X_add_number &= 0xffffffff;
3348 }
3349 else
3350 {
3351 assert (ep->X_add_number > 2);
3352 if (ep->X_add_number == 3)
3353 generic_bignum[3] = 0;
3354 else if (ep->X_add_number > 4)
3355 as_bad (_("Number larger than 64 bits"));
3356 lo32.X_op = O_constant;
3357 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3358 hi32.X_op = O_constant;
3359 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3360 }
3361
3362 if (hi32.X_add_number == 0)
3363 freg = 0;
3364 else
3365 {
3366 int shift, bit;
3367 unsigned long hi, lo;
3368
3369 if (hi32.X_add_number == 0xffffffff)
3370 {
3371 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3372 {
3373 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
3374 reg, 0, (int) BFD_RELOC_LO16);
3375 return;
3376 }
3377 if (lo32.X_add_number & 0x80000000)
3378 {
3379 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3380 (int) BFD_RELOC_HI16);
3381 if (lo32.X_add_number & 0xffff)
3382 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3383 reg, reg, (int) BFD_RELOC_LO16);
3384 return;
3385 }
3386 }
3387
3388 /* Check for 16bit shifted constant. We know that hi32 is
3389 non-zero, so start the mask on the first bit of the hi32
3390 value. */
3391 shift = 17;
3392 do
3393 {
3394 unsigned long himask, lomask;
3395
3396 if (shift < 32)
3397 {
3398 himask = 0xffff >> (32 - shift);
3399 lomask = (0xffff << shift) & 0xffffffff;
3400 }
3401 else
3402 {
3403 himask = 0xffff << (shift - 32);
3404 lomask = 0;
3405 }
3406 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3407 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3408 {
3409 expressionS tmp;
3410
3411 tmp.X_op = O_constant;
3412 if (shift < 32)
3413 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3414 | (lo32.X_add_number >> shift));
3415 else
3416 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3417 macro_build ((char *) NULL, counter, &tmp,
3418 "ori", "t,r,i", reg, 0,
3419 (int) BFD_RELOC_LO16);
3420 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3421 (shift >= 32) ? "dsll32" : "dsll",
3422 "d,w,<", reg, reg,
3423 (shift >= 32) ? shift - 32 : shift);
3424 return;
3425 }
3426 shift++;
3427 }
3428 while (shift <= (64 - 16));
3429
3430 /* Find the bit number of the lowest one bit, and store the
3431 shifted value in hi/lo. */
3432 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3433 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3434 if (lo != 0)
3435 {
3436 bit = 0;
3437 while ((lo & 1) == 0)
3438 {
3439 lo >>= 1;
3440 ++bit;
3441 }
3442 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3443 hi >>= bit;
3444 }
3445 else
3446 {
3447 bit = 32;
3448 while ((hi & 1) == 0)
3449 {
3450 hi >>= 1;
3451 ++bit;
3452 }
3453 lo = hi;
3454 hi = 0;
3455 }
3456
3457 /* Optimize if the shifted value is a (power of 2) - 1. */
3458 if ((hi == 0 && ((lo + 1) & lo) == 0)
3459 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
3460 {
3461 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
3462 if (shift != 0)
3463 {
3464 expressionS tmp;
3465
3466 /* This instruction will set the register to be all
3467 ones. */
3468 tmp.X_op = O_constant;
3469 tmp.X_add_number = (offsetT) -1;
3470 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
3471 reg, 0, (int) BFD_RELOC_LO16);
3472 if (bit != 0)
3473 {
3474 bit += shift;
3475 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3476 (bit >= 32) ? "dsll32" : "dsll",
3477 "d,w,<", reg, reg,
3478 (bit >= 32) ? bit - 32 : bit);
3479 }
3480 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3481 (shift >= 32) ? "dsrl32" : "dsrl",
3482 "d,w,<", reg, reg,
3483 (shift >= 32) ? shift - 32 : shift);
3484 return;
3485 }
3486 }
3487
3488 /* Sign extend hi32 before calling load_register, because we can
3489 generally get better code when we load a sign extended value. */
3490 if ((hi32.X_add_number & 0x80000000) != 0)
3491 hi32.X_add_number |= ~(offsetT) 0xffffffff;
3492 load_register (counter, reg, &hi32, 0);
3493 freg = reg;
3494 }
3495 if ((lo32.X_add_number & 0xffff0000) == 0)
3496 {
3497 if (freg != 0)
3498 {
3499 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3500 "dsll32", "d,w,<", reg, freg, 0);
3501 freg = reg;
3502 }
3503 }
3504 else
3505 {
3506 expressionS mid16;
3507
3508 if ((freg == 0) && (lo32.X_add_number == 0xffffffff))
3509 {
3510 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3511 (int) BFD_RELOC_HI16);
3512 macro_build ((char *) NULL, counter, NULL, "dsrl32", "d,w,<", reg,
3513 reg, 0);
3514 return;
3515 }
3516
3517 if (freg != 0)
3518 {
3519 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3520 freg, 16);
3521 freg = reg;
3522 }
3523 mid16 = lo32;
3524 mid16.X_add_number >>= 16;
3525 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3526 freg, (int) BFD_RELOC_LO16);
3527 macro_build ((char *) NULL, counter, NULL, "dsll", "d,w,<", reg,
3528 reg, 16);
3529 freg = reg;
3530 }
3531 if ((lo32.X_add_number & 0xffff) != 0)
3532 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3533 (int) BFD_RELOC_LO16);
3534 }
3535
3536 /* Load an address into a register. */
3537
3538 static void
3539 load_address (counter, reg, ep, dbl, used_at)
3540 int *counter;
3541 int reg;
3542 expressionS *ep;
3543 int dbl;
3544 int *used_at;
3545 {
3546 char *p;
3547
3548 if (ep->X_op != O_constant
3549 && ep->X_op != O_symbol)
3550 {
3551 as_bad (_("expression too complex"));
3552 ep->X_op = O_constant;
3553 }
3554
3555 if (ep->X_op == O_constant)
3556 {
3557 load_register (counter, reg, ep, dbl);
3558 return;
3559 }
3560
3561 if (mips_pic == NO_PIC)
3562 {
3563 /* If this is a reference to a GP relative symbol, we want
3564 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3565 Otherwise we want
3566 lui $reg,<sym> (BFD_RELOC_HI16_S)
3567 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3568 If we have an addend, we always use the latter form.
3569
3570 With 64bit address space and a usable $at we want
3571 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3572 lui $at,<sym> (BFD_RELOC_HI16_S)
3573 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3574 daddiu $at,<sym> (BFD_RELOC_LO16)
3575 dsll32 $reg,0
3576 dadd $reg,$reg,$at
3577
3578 If $at is already in use, we use an path which is suboptimal
3579 on superscalar processors.
3580 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3581 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3582 dsll $reg,16
3583 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3584 dsll $reg,16
3585 daddiu $reg,<sym> (BFD_RELOC_LO16)
3586 */
3587 if (HAVE_64BIT_ADDRESSES)
3588 {
3589 p = NULL;
3590
3591 /* We don't do GP optimization for now because RELAX_ENCODE can't
3592 hold the data for such large chunks. */
3593
3594 if (*used_at == 0)
3595 {
3596 macro_build (p, counter, ep, "lui", "t,u",
3597 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3598 macro_build (p, counter, ep, "lui", "t,u",
3599 AT, (int) BFD_RELOC_HI16_S);
3600 macro_build (p, counter, ep, "daddiu", "t,r,j",
3601 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3602 macro_build (p, counter, ep, "daddiu", "t,r,j",
3603 AT, AT, (int) BFD_RELOC_LO16);
3604 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3605 "d,w,<", reg, reg, 0);
3606 macro_build (p, counter, (expressionS *) NULL, "dadd",
3607 "d,v,t", reg, reg, AT);
3608 *used_at = 1;
3609 }
3610 else
3611 {
3612 macro_build (p, counter, ep, "lui", "t,u",
3613 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3614 macro_build (p, counter, ep, "daddiu", "t,r,j",
3615 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3616 macro_build (p, counter, (expressionS *) NULL, "dsll",
3617 "d,w,<", reg, reg, 16);
3618 macro_build (p, counter, ep, "daddiu", "t,r,j",
3619 reg, reg, (int) BFD_RELOC_HI16_S);
3620 macro_build (p, counter, (expressionS *) NULL, "dsll",
3621 "d,w,<", reg, reg, 16);
3622 macro_build (p, counter, ep, "daddiu", "t,r,j",
3623 reg, reg, (int) BFD_RELOC_LO16);
3624 }
3625 }
3626 else
3627 {
3628 p = NULL;
3629 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3630 && ! nopic_need_relax (ep->X_add_symbol, 1))
3631 {
3632 frag_grow (20);
3633 macro_build ((char *) NULL, counter, ep,
3634 dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3635 (int) BFD_RELOC_GPREL16);
3636 p = frag_var (rs_machine_dependent, 8, 0,
3637 RELAX_ENCODE (4, 8, 0, 4, 0,
3638 mips_opts.warn_about_macros),
3639 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3640 }
3641 macro_build_lui (p, counter, ep, reg);
3642 if (p != NULL)
3643 p += 4;
3644 macro_build (p, counter, ep,
3645 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3646 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3647 }
3648 }
3649 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3650 {
3651 expressionS ex;
3652
3653 /* If this is a reference to an external symbol, we want
3654 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3655 Otherwise we want
3656 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3657 nop
3658 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3659 If there is a constant, it must be added in after. */
3660 ex.X_add_number = ep->X_add_number;
3661 ep->X_add_number = 0;
3662 frag_grow (20);
3663 macro_build ((char *) NULL, counter, ep,
3664 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3665 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3666 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3667 p = frag_var (rs_machine_dependent, 4, 0,
3668 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3669 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3670 macro_build (p, counter, ep,
3671 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3672 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3673 if (ex.X_add_number != 0)
3674 {
3675 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3676 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3677 ex.X_op = O_constant;
3678 macro_build ((char *) NULL, counter, &ex,
3679 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3680 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3681 }
3682 }
3683 else if (mips_pic == SVR4_PIC)
3684 {
3685 expressionS ex;
3686 int off;
3687
3688 /* This is the large GOT case. If this is a reference to an
3689 external symbol, we want
3690 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3691 addu $reg,$reg,$gp
3692 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3693 Otherwise, for a reference to a local symbol, we want
3694 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3695 nop
3696 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3697 If there is a constant, it must be added in after. */
3698 ex.X_add_number = ep->X_add_number;
3699 ep->X_add_number = 0;
3700 if (reg_needs_delay (GP))
3701 off = 4;
3702 else
3703 off = 0;
3704 frag_grow (32);
3705 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3706 (int) BFD_RELOC_MIPS_GOT_HI16);
3707 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3708 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3709 "d,v,t", reg, reg, GP);
3710 macro_build ((char *) NULL, counter, ep,
3711 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3712 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3713 p = frag_var (rs_machine_dependent, 12 + off, 0,
3714 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3715 mips_opts.warn_about_macros),
3716 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3717 if (off > 0)
3718 {
3719 /* We need a nop before loading from $gp. This special
3720 check is required because the lui which starts the main
3721 instruction stream does not refer to $gp, and so will not
3722 insert the nop which may be required. */
3723 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3724 p += 4;
3725 }
3726 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3727 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3728 p += 4;
3729 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3730 p += 4;
3731 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3732 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3733 if (ex.X_add_number != 0)
3734 {
3735 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3736 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3737 ex.X_op = O_constant;
3738 macro_build ((char *) NULL, counter, &ex,
3739 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3740 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3741 }
3742 }
3743 else if (mips_pic == EMBEDDED_PIC)
3744 {
3745 /* We always do
3746 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
3747 */
3748 macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3749 "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
3750 }
3751 else
3752 abort ();
3753 }
3754
3755 /* Move the contents of register SOURCE into register DEST. */
3756
3757 static void
3758 move_register (counter, dest, source)
3759 int *counter;
3760 int dest;
3761 int source;
3762 {
3763 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3764 HAVE_32BIT_GPRS ? "addu" : "daddu",
3765 "d,v,t", dest, source, 0);
3766 }
3767
3768 /*
3769 * Build macros
3770 * This routine implements the seemingly endless macro or synthesized
3771 * instructions and addressing modes in the mips assembly language. Many
3772 * of these macros are simple and are similar to each other. These could
3773 * probably be handled by some kind of table or grammer aproach instead of
3774 * this verbose method. Others are not simple macros but are more like
3775 * optimizing code generation.
3776 * One interesting optimization is when several store macros appear
3777 * consecutivly that would load AT with the upper half of the same address.
3778 * The ensuing load upper instructions are ommited. This implies some kind
3779 * of global optimization. We currently only optimize within a single macro.
3780 * For many of the load and store macros if the address is specified as a
3781 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3782 * first load register 'at' with zero and use it as the base register. The
3783 * mips assembler simply uses register $zero. Just one tiny optimization
3784 * we're missing.
3785 */
3786 static void
3787 macro (ip)
3788 struct mips_cl_insn *ip;
3789 {
3790 register int treg, sreg, dreg, breg;
3791 int tempreg;
3792 int mask;
3793 int icnt = 0;
3794 int used_at = 0;
3795 expressionS expr1;
3796 const char *s;
3797 const char *s2;
3798 const char *fmt;
3799 int likely = 0;
3800 int dbl = 0;
3801 int coproc = 0;
3802 int lr = 0;
3803 int imm = 0;
3804 offsetT maxnum;
3805 int off;
3806 bfd_reloc_code_real_type r;
3807 char *p;
3808 int hold_mips_optimize;
3809
3810 assert (! mips_opts.mips16);
3811
3812 treg = (ip->insn_opcode >> 16) & 0x1f;
3813 dreg = (ip->insn_opcode >> 11) & 0x1f;
3814 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3815 mask = ip->insn_mo->mask;
3816
3817 expr1.X_op = O_constant;
3818 expr1.X_op_symbol = NULL;
3819 expr1.X_add_symbol = NULL;
3820 expr1.X_add_number = 1;
3821
3822 switch (mask)
3823 {
3824 case M_DABS:
3825 dbl = 1;
3826 case M_ABS:
3827 /* bgez $a0,.+12
3828 move v0,$a0
3829 sub v0,$zero,$a0
3830 */
3831
3832 mips_emit_delays (true);
3833 ++mips_opts.noreorder;
3834 mips_any_noreorder = 1;
3835
3836 expr1.X_add_number = 8;
3837 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3838 if (dreg == sreg)
3839 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3840 0);
3841 else
3842 move_register (&icnt, dreg, sreg);
3843 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
3844 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
3845
3846 --mips_opts.noreorder;
3847 return;
3848
3849 case M_ADD_I:
3850 s = "addi";
3851 s2 = "add";
3852 goto do_addi;
3853 case M_ADDU_I:
3854 s = "addiu";
3855 s2 = "addu";
3856 goto do_addi;
3857 case M_DADD_I:
3858 dbl = 1;
3859 s = "daddi";
3860 s2 = "dadd";
3861 goto do_addi;
3862 case M_DADDU_I:
3863 dbl = 1;
3864 s = "daddiu";
3865 s2 = "daddu";
3866 do_addi:
3867 if (imm_expr.X_op == O_constant
3868 && imm_expr.X_add_number >= -0x8000
3869 && imm_expr.X_add_number < 0x8000)
3870 {
3871 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3872 (int) BFD_RELOC_LO16);
3873 return;
3874 }
3875 load_register (&icnt, AT, &imm_expr, dbl);
3876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3877 treg, sreg, AT);
3878 break;
3879
3880 case M_AND_I:
3881 s = "andi";
3882 s2 = "and";
3883 goto do_bit;
3884 case M_OR_I:
3885 s = "ori";
3886 s2 = "or";
3887 goto do_bit;
3888 case M_NOR_I:
3889 s = "";
3890 s2 = "nor";
3891 goto do_bit;
3892 case M_XOR_I:
3893 s = "xori";
3894 s2 = "xor";
3895 do_bit:
3896 if (imm_expr.X_op == O_constant
3897 && imm_expr.X_add_number >= 0
3898 && imm_expr.X_add_number < 0x10000)
3899 {
3900 if (mask != M_NOR_I)
3901 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3902 sreg, (int) BFD_RELOC_LO16);
3903 else
3904 {
3905 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3906 treg, sreg, (int) BFD_RELOC_LO16);
3907 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3908 "d,v,t", treg, treg, 0);
3909 }
3910 return;
3911 }
3912
3913 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
3914 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3915 treg, sreg, AT);
3916 break;
3917
3918 case M_BEQ_I:
3919 s = "beq";
3920 goto beq_i;
3921 case M_BEQL_I:
3922 s = "beql";
3923 likely = 1;
3924 goto beq_i;
3925 case M_BNE_I:
3926 s = "bne";
3927 goto beq_i;
3928 case M_BNEL_I:
3929 s = "bnel";
3930 likely = 1;
3931 beq_i:
3932 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3933 {
3934 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3935 0);
3936 return;
3937 }
3938 load_register (&icnt, AT, &imm_expr, 0);
3939 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3940 break;
3941
3942 case M_BGEL:
3943 likely = 1;
3944 case M_BGE:
3945 if (treg == 0)
3946 {
3947 macro_build ((char *) NULL, &icnt, &offset_expr,
3948 likely ? "bgezl" : "bgez", "s,p", sreg);
3949 return;
3950 }
3951 if (sreg == 0)
3952 {
3953 macro_build ((char *) NULL, &icnt, &offset_expr,
3954 likely ? "blezl" : "blez", "s,p", treg);
3955 return;
3956 }
3957 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3958 AT, sreg, treg);
3959 macro_build ((char *) NULL, &icnt, &offset_expr,
3960 likely ? "beql" : "beq", "s,t,p", AT, 0);
3961 break;
3962
3963 case M_BGTL_I:
3964 likely = 1;
3965 case M_BGT_I:
3966 /* check for > max integer */
3967 maxnum = 0x7fffffff;
3968 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
3969 {
3970 maxnum <<= 16;
3971 maxnum |= 0xffff;
3972 maxnum <<= 16;
3973 maxnum |= 0xffff;
3974 }
3975 if (imm_expr.X_op == O_constant
3976 && imm_expr.X_add_number >= maxnum
3977 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
3978 {
3979 do_false:
3980 /* result is always false */
3981 if (! likely)
3982 {
3983 if (warn_nops)
3984 as_warn (_("Branch %s is always false (nop)"),
3985 ip->insn_mo->name);
3986 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3987 "", 0);
3988 }
3989 else
3990 {
3991 if (warn_nops)
3992 as_warn (_("Branch likely %s is always false"),
3993 ip->insn_mo->name);
3994 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3995 "s,t,p", 0, 0);
3996 }
3997 return;
3998 }
3999 if (imm_expr.X_op != O_constant)
4000 as_bad (_("Unsupported large constant"));
4001 imm_expr.X_add_number++;
4002 /* FALLTHROUGH */
4003 case M_BGE_I:
4004 case M_BGEL_I:
4005 if (mask == M_BGEL_I)
4006 likely = 1;
4007 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4008 {
4009 macro_build ((char *) NULL, &icnt, &offset_expr,
4010 likely ? "bgezl" : "bgez", "s,p", sreg);
4011 return;
4012 }
4013 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4014 {
4015 macro_build ((char *) NULL, &icnt, &offset_expr,
4016 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4017 return;
4018 }
4019 maxnum = 0x7fffffff;
4020 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4021 {
4022 maxnum <<= 16;
4023 maxnum |= 0xffff;
4024 maxnum <<= 16;
4025 maxnum |= 0xffff;
4026 }
4027 maxnum = - maxnum - 1;
4028 if (imm_expr.X_op == O_constant
4029 && imm_expr.X_add_number <= maxnum
4030 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4031 {
4032 do_true:
4033 /* result is always true */
4034 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4035 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4036 return;
4037 }
4038 set_at (&icnt, sreg, 0);
4039 macro_build ((char *) NULL, &icnt, &offset_expr,
4040 likely ? "beql" : "beq", "s,t,p", AT, 0);
4041 break;
4042
4043 case M_BGEUL:
4044 likely = 1;
4045 case M_BGEU:
4046 if (treg == 0)
4047 goto do_true;
4048 if (sreg == 0)
4049 {
4050 macro_build ((char *) NULL, &icnt, &offset_expr,
4051 likely ? "beql" : "beq", "s,t,p", 0, treg);
4052 return;
4053 }
4054 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4055 "d,v,t", AT, sreg, treg);
4056 macro_build ((char *) NULL, &icnt, &offset_expr,
4057 likely ? "beql" : "beq", "s,t,p", AT, 0);
4058 break;
4059
4060 case M_BGTUL_I:
4061 likely = 1;
4062 case M_BGTU_I:
4063 if (sreg == 0
4064 || (HAVE_32BIT_GPRS
4065 && imm_expr.X_op == O_constant
4066 && imm_expr.X_add_number == 0xffffffff))
4067 goto do_false;
4068 if (imm_expr.X_op != O_constant)
4069 as_bad (_("Unsupported large constant"));
4070 imm_expr.X_add_number++;
4071 /* FALLTHROUGH */
4072 case M_BGEU_I:
4073 case M_BGEUL_I:
4074 if (mask == M_BGEUL_I)
4075 likely = 1;
4076 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4077 goto do_true;
4078 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4079 {
4080 macro_build ((char *) NULL, &icnt, &offset_expr,
4081 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4082 return;
4083 }
4084 set_at (&icnt, sreg, 1);
4085 macro_build ((char *) NULL, &icnt, &offset_expr,
4086 likely ? "beql" : "beq", "s,t,p", AT, 0);
4087 break;
4088
4089 case M_BGTL:
4090 likely = 1;
4091 case M_BGT:
4092 if (treg == 0)
4093 {
4094 macro_build ((char *) NULL, &icnt, &offset_expr,
4095 likely ? "bgtzl" : "bgtz", "s,p", sreg);
4096 return;
4097 }
4098 if (sreg == 0)
4099 {
4100 macro_build ((char *) NULL, &icnt, &offset_expr,
4101 likely ? "bltzl" : "bltz", "s,p", treg);
4102 return;
4103 }
4104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4105 AT, treg, sreg);
4106 macro_build ((char *) NULL, &icnt, &offset_expr,
4107 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4108 break;
4109
4110 case M_BGTUL:
4111 likely = 1;
4112 case M_BGTU:
4113 if (treg == 0)
4114 {
4115 macro_build ((char *) NULL, &icnt, &offset_expr,
4116 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
4117 return;
4118 }
4119 if (sreg == 0)
4120 goto do_false;
4121 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4122 "d,v,t", AT, treg, sreg);
4123 macro_build ((char *) NULL, &icnt, &offset_expr,
4124 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4125 break;
4126
4127 case M_BLEL:
4128 likely = 1;
4129 case M_BLE:
4130 if (treg == 0)
4131 {
4132 macro_build ((char *) NULL, &icnt, &offset_expr,
4133 likely ? "blezl" : "blez", "s,p", sreg);
4134 return;
4135 }
4136 if (sreg == 0)
4137 {
4138 macro_build ((char *) NULL, &icnt, &offset_expr,
4139 likely ? "bgezl" : "bgez", "s,p", treg);
4140 return;
4141 }
4142 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4143 AT, treg, sreg);
4144 macro_build ((char *) NULL, &icnt, &offset_expr,
4145 likely ? "beql" : "beq", "s,t,p", AT, 0);
4146 break;
4147
4148 case M_BLEL_I:
4149 likely = 1;
4150 case M_BLE_I:
4151 maxnum = 0x7fffffff;
4152 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
4153 {
4154 maxnum <<= 16;
4155 maxnum |= 0xffff;
4156 maxnum <<= 16;
4157 maxnum |= 0xffff;
4158 }
4159 if (imm_expr.X_op == O_constant
4160 && imm_expr.X_add_number >= maxnum
4161 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
4162 goto do_true;
4163 if (imm_expr.X_op != O_constant)
4164 as_bad (_("Unsupported large constant"));
4165 imm_expr.X_add_number++;
4166 /* FALLTHROUGH */
4167 case M_BLT_I:
4168 case M_BLTL_I:
4169 if (mask == M_BLTL_I)
4170 likely = 1;
4171 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4172 {
4173 macro_build ((char *) NULL, &icnt, &offset_expr,
4174 likely ? "bltzl" : "bltz", "s,p", sreg);
4175 return;
4176 }
4177 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4178 {
4179 macro_build ((char *) NULL, &icnt, &offset_expr,
4180 likely ? "blezl" : "blez", "s,p", sreg);
4181 return;
4182 }
4183 set_at (&icnt, sreg, 0);
4184 macro_build ((char *) NULL, &icnt, &offset_expr,
4185 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4186 break;
4187
4188 case M_BLEUL:
4189 likely = 1;
4190 case M_BLEU:
4191 if (treg == 0)
4192 {
4193 macro_build ((char *) NULL, &icnt, &offset_expr,
4194 likely ? "beql" : "beq", "s,t,p", sreg, 0);
4195 return;
4196 }
4197 if (sreg == 0)
4198 goto do_true;
4199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4200 "d,v,t", AT, treg, sreg);
4201 macro_build ((char *) NULL, &icnt, &offset_expr,
4202 likely ? "beql" : "beq", "s,t,p", AT, 0);
4203 break;
4204
4205 case M_BLEUL_I:
4206 likely = 1;
4207 case M_BLEU_I:
4208 if (sreg == 0
4209 || (HAVE_32BIT_GPRS
4210 && imm_expr.X_op == O_constant
4211 && imm_expr.X_add_number == 0xffffffff))
4212 goto do_true;
4213 if (imm_expr.X_op != O_constant)
4214 as_bad (_("Unsupported large constant"));
4215 imm_expr.X_add_number++;
4216 /* FALLTHROUGH */
4217 case M_BLTU_I:
4218 case M_BLTUL_I:
4219 if (mask == M_BLTUL_I)
4220 likely = 1;
4221 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4222 goto do_false;
4223 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4224 {
4225 macro_build ((char *) NULL, &icnt, &offset_expr,
4226 likely ? "beql" : "beq",
4227 "s,t,p", sreg, 0);
4228 return;
4229 }
4230 set_at (&icnt, sreg, 1);
4231 macro_build ((char *) NULL, &icnt, &offset_expr,
4232 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4233 break;
4234
4235 case M_BLTL:
4236 likely = 1;
4237 case M_BLT:
4238 if (treg == 0)
4239 {
4240 macro_build ((char *) NULL, &icnt, &offset_expr,
4241 likely ? "bltzl" : "bltz", "s,p", sreg);
4242 return;
4243 }
4244 if (sreg == 0)
4245 {
4246 macro_build ((char *) NULL, &icnt, &offset_expr,
4247 likely ? "bgtzl" : "bgtz", "s,p", treg);
4248 return;
4249 }
4250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4251 AT, sreg, treg);
4252 macro_build ((char *) NULL, &icnt, &offset_expr,
4253 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4254 break;
4255
4256 case M_BLTUL:
4257 likely = 1;
4258 case M_BLTU:
4259 if (treg == 0)
4260 goto do_false;
4261 if (sreg == 0)
4262 {
4263 macro_build ((char *) NULL, &icnt, &offset_expr,
4264 likely ? "bnel" : "bne", "s,t,p", 0, treg);
4265 return;
4266 }
4267 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4268 "d,v,t", AT, sreg,
4269 treg);
4270 macro_build ((char *) NULL, &icnt, &offset_expr,
4271 likely ? "bnel" : "bne", "s,t,p", AT, 0);
4272 break;
4273
4274 case M_DDIV_3:
4275 dbl = 1;
4276 case M_DIV_3:
4277 s = "mflo";
4278 goto do_div3;
4279 case M_DREM_3:
4280 dbl = 1;
4281 case M_REM_3:
4282 s = "mfhi";
4283 do_div3:
4284 if (treg == 0)
4285 {
4286 as_warn (_("Divide by zero."));
4287 if (mips_trap)
4288 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4289 "s,t", 0, 0);
4290 else
4291 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4292 "c", 7);
4293 return;
4294 }
4295
4296 mips_emit_delays (true);
4297 ++mips_opts.noreorder;
4298 mips_any_noreorder = 1;
4299 if (mips_trap)
4300 {
4301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4302 "s,t", treg, 0);
4303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4304 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4305 }
4306 else
4307 {
4308 expr1.X_add_number = 8;
4309 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4310 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4311 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4312 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4313 "c", 7);
4314 }
4315 expr1.X_add_number = -1;
4316 macro_build ((char *) NULL, &icnt, &expr1,
4317 dbl ? "daddiu" : "addiu",
4318 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4319 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4320 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4321 if (dbl)
4322 {
4323 expr1.X_add_number = 1;
4324 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4325 (int) BFD_RELOC_LO16);
4326 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4327 "d,w,<", AT, AT, 31);
4328 }
4329 else
4330 {
4331 expr1.X_add_number = 0x80000000;
4332 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4333 (int) BFD_RELOC_HI16);
4334 }
4335 if (mips_trap)
4336 {
4337 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4338 "s,t", sreg, AT);
4339 /* We want to close the noreorder block as soon as possible, so
4340 that later insns are available for delay slot filling. */
4341 --mips_opts.noreorder;
4342 }
4343 else
4344 {
4345 expr1.X_add_number = 8;
4346 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4347 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4348 0);
4349
4350 /* We want to close the noreorder block as soon as possible, so
4351 that later insns are available for delay slot filling. */
4352 --mips_opts.noreorder;
4353
4354 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4355 "c", 6);
4356 }
4357 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
4358 break;
4359
4360 case M_DIV_3I:
4361 s = "div";
4362 s2 = "mflo";
4363 goto do_divi;
4364 case M_DIVU_3I:
4365 s = "divu";
4366 s2 = "mflo";
4367 goto do_divi;
4368 case M_REM_3I:
4369 s = "div";
4370 s2 = "mfhi";
4371 goto do_divi;
4372 case M_REMU_3I:
4373 s = "divu";
4374 s2 = "mfhi";
4375 goto do_divi;
4376 case M_DDIV_3I:
4377 dbl = 1;
4378 s = "ddiv";
4379 s2 = "mflo";
4380 goto do_divi;
4381 case M_DDIVU_3I:
4382 dbl = 1;
4383 s = "ddivu";
4384 s2 = "mflo";
4385 goto do_divi;
4386 case M_DREM_3I:
4387 dbl = 1;
4388 s = "ddiv";
4389 s2 = "mfhi";
4390 goto do_divi;
4391 case M_DREMU_3I:
4392 dbl = 1;
4393 s = "ddivu";
4394 s2 = "mfhi";
4395 do_divi:
4396 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4397 {
4398 as_warn (_("Divide by zero."));
4399 if (mips_trap)
4400 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4401 "s,t", 0, 0);
4402 else
4403 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4404 "c", 7);
4405 return;
4406 }
4407 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4408 {
4409 if (strcmp (s2, "mflo") == 0)
4410 move_register (&icnt, dreg, sreg);
4411 else
4412 move_register (&icnt, dreg, 0);
4413 return;
4414 }
4415 if (imm_expr.X_op == O_constant
4416 && imm_expr.X_add_number == -1
4417 && s[strlen (s) - 1] != 'u')
4418 {
4419 if (strcmp (s2, "mflo") == 0)
4420 {
4421 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4422 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
4423 }
4424 else
4425 move_register (&icnt, dreg, 0);
4426 return;
4427 }
4428
4429 load_register (&icnt, AT, &imm_expr, dbl);
4430 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4431 sreg, AT);
4432 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4433 break;
4434
4435 case M_DIVU_3:
4436 s = "divu";
4437 s2 = "mflo";
4438 goto do_divu3;
4439 case M_REMU_3:
4440 s = "divu";
4441 s2 = "mfhi";
4442 goto do_divu3;
4443 case M_DDIVU_3:
4444 s = "ddivu";
4445 s2 = "mflo";
4446 goto do_divu3;
4447 case M_DREMU_3:
4448 s = "ddivu";
4449 s2 = "mfhi";
4450 do_divu3:
4451 mips_emit_delays (true);
4452 ++mips_opts.noreorder;
4453 mips_any_noreorder = 1;
4454 if (mips_trap)
4455 {
4456 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4457 "s,t", treg, 0);
4458 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4459 sreg, treg);
4460 /* We want to close the noreorder block as soon as possible, so
4461 that later insns are available for delay slot filling. */
4462 --mips_opts.noreorder;
4463 }
4464 else
4465 {
4466 expr1.X_add_number = 8;
4467 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4468 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4469 sreg, treg);
4470
4471 /* We want to close the noreorder block as soon as possible, so
4472 that later insns are available for delay slot filling. */
4473 --mips_opts.noreorder;
4474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4475 "c", 7);
4476 }
4477 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
4478 return;
4479
4480 case M_DLA_AB:
4481 dbl = 1;
4482 case M_LA_AB:
4483 /* Load the address of a symbol into a register. If breg is not
4484 zero, we then add a base register to it. */
4485
4486 if (treg == breg)
4487 {
4488 tempreg = AT;
4489 used_at = 1;
4490 }
4491 else
4492 {
4493 tempreg = treg;
4494 used_at = 0;
4495 }
4496
4497 /* When generating embedded PIC code, we permit expressions of
4498 the form
4499 la $treg,foo-bar
4500 la $treg,foo-bar($breg)
4501 where bar is an address in the current section. These are used
4502 when getting the addresses of functions. We don't permit
4503 X_add_number to be non-zero, because if the symbol is
4504 external the relaxing code needs to know that any addend is
4505 purely the offset to X_op_symbol. */
4506 if (mips_pic == EMBEDDED_PIC
4507 && offset_expr.X_op == O_subtract
4508 && (symbol_constant_p (offset_expr.X_op_symbol)
4509 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
4510 : (symbol_equated_p (offset_expr.X_op_symbol)
4511 && (S_GET_SEGMENT
4512 (symbol_get_value_expression (offset_expr.X_op_symbol)
4513 ->X_add_symbol)
4514 == now_seg)))
4515 && (offset_expr.X_add_number == 0
4516 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
4517 {
4518 if (breg == 0)
4519 {
4520 tempreg = treg;
4521 used_at = 0;
4522 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4523 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4524 }
4525 else
4526 {
4527 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4528 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4529 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4530 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4531 "d,v,t", tempreg, tempreg, breg);
4532 }
4533 macro_build ((char *) NULL, &icnt, &offset_expr,
4534 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4535 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4536 if (! used_at)
4537 return;
4538 break;
4539 }
4540
4541 if (offset_expr.X_op != O_symbol
4542 && offset_expr.X_op != O_constant)
4543 {
4544 as_bad (_("expression too complex"));
4545 offset_expr.X_op = O_constant;
4546 }
4547
4548 if (offset_expr.X_op == O_constant)
4549 load_register (&icnt, tempreg, &offset_expr, dbl);
4550 else if (mips_pic == NO_PIC)
4551 {
4552 /* If this is a reference to a GP relative symbol, we want
4553 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4554 Otherwise we want
4555 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4556 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4557 If we have a constant, we need two instructions anyhow,
4558 so we may as well always use the latter form.
4559
4560 With 64bit address space and a usable $at we want
4561 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4562 lui $at,<sym> (BFD_RELOC_HI16_S)
4563 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4564 daddiu $at,<sym> (BFD_RELOC_LO16)
4565 dsll32 $tempreg,0
4566 dadd $tempreg,$tempreg,$at
4567
4568 If $at is already in use, we use an path which is suboptimal
4569 on superscalar processors.
4570 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4571 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4572 dsll $tempreg,16
4573 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4574 dsll $tempreg,16
4575 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4576 */
4577 p = NULL;
4578 if (HAVE_64BIT_ADDRESSES)
4579 {
4580 /* We don't do GP optimization for now because RELAX_ENCODE can't
4581 hold the data for such large chunks. */
4582
4583 if (used_at == 0)
4584 {
4585 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4586 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4587 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4588 AT, (int) BFD_RELOC_HI16_S);
4589 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4590 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4591 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4592 AT, AT, (int) BFD_RELOC_LO16);
4593 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4594 "d,w,<", tempreg, tempreg, 0);
4595 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4596 tempreg, tempreg, AT);
4597 used_at = 1;
4598 }
4599 else
4600 {
4601 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4602 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4603 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4604 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4605 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4606 tempreg, tempreg, 16);
4607 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4608 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4609 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4610 tempreg, tempreg, 16);
4611 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4612 tempreg, tempreg, (int) BFD_RELOC_LO16);
4613 }
4614 }
4615 else
4616 {
4617 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4618 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4619 {
4620 frag_grow (20);
4621 macro_build ((char *) NULL, &icnt, &offset_expr,
4622 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4623 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4624 p = frag_var (rs_machine_dependent, 8, 0,
4625 RELAX_ENCODE (4, 8, 0, 4, 0,
4626 mips_opts.warn_about_macros),
4627 offset_expr.X_add_symbol, (offsetT) 0,
4628 (char *) NULL);
4629 }
4630 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4631 if (p != NULL)
4632 p += 4;
4633 macro_build (p, &icnt, &offset_expr,
4634 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4635 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4636 }
4637 }
4638 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4639 {
4640 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4641
4642 /* If this is a reference to an external symbol, and there
4643 is no constant, we want
4644 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4645 or if tempreg is PIC_CALL_REG
4646 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4647 For a local symbol, we want
4648 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4649 nop
4650 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4651
4652 If we have a small constant, and this is a reference to
4653 an external symbol, we want
4654 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4655 nop
4656 addiu $tempreg,$tempreg,<constant>
4657 For a local symbol, we want the same instruction
4658 sequence, but we output a BFD_RELOC_LO16 reloc on the
4659 addiu instruction.
4660
4661 If we have a large constant, and this is a reference to
4662 an external symbol, we want
4663 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4664 lui $at,<hiconstant>
4665 addiu $at,$at,<loconstant>
4666 addu $tempreg,$tempreg,$at
4667 For a local symbol, we want the same instruction
4668 sequence, but we output a BFD_RELOC_LO16 reloc on the
4669 addiu instruction. */
4670 expr1.X_add_number = offset_expr.X_add_number;
4671 offset_expr.X_add_number = 0;
4672 frag_grow (32);
4673 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4674 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4675 macro_build ((char *) NULL, &icnt, &offset_expr, dbl ? "ld" : "lw",
4676 "t,o(b)", tempreg, lw_reloc_type, GP);
4677 if (expr1.X_add_number == 0)
4678 {
4679 int off;
4680
4681 if (breg == 0)
4682 off = 0;
4683 else
4684 {
4685 /* We're going to put in an addu instruction using
4686 tempreg, so we may as well insert the nop right
4687 now. */
4688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4689 "nop", "");
4690 off = 4;
4691 }
4692 p = frag_var (rs_machine_dependent, 8 - off, 0,
4693 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4694 (breg == 0
4695 ? mips_opts.warn_about_macros
4696 : 0)),
4697 offset_expr.X_add_symbol, 0, NULL);
4698 if (breg == 0)
4699 {
4700 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4701 p += 4;
4702 }
4703 macro_build (p, &icnt, &expr1,
4704 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4705 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4706 /* FIXME: If breg == 0, and the next instruction uses
4707 $tempreg, then if this variant case is used an extra
4708 nop will be generated. */
4709 }
4710 else if (expr1.X_add_number >= -0x8000
4711 && expr1.X_add_number < 0x8000)
4712 {
4713 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4714 "nop", "");
4715 macro_build ((char *) NULL, &icnt, &expr1,
4716 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4717 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4718 frag_var (rs_machine_dependent, 0, 0,
4719 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4720 offset_expr.X_add_symbol, 0, NULL);
4721 }
4722 else
4723 {
4724 int off1;
4725
4726 /* If we are going to add in a base register, and the
4727 target register and the base register are the same,
4728 then we are using AT as a temporary register. Since
4729 we want to load the constant into AT, we add our
4730 current AT (from the global offset table) and the
4731 register into the register now, and pretend we were
4732 not using a base register. */
4733 if (breg != treg)
4734 off1 = 0;
4735 else
4736 {
4737 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4738 "nop", "");
4739 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4740 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4741 "d,v,t", treg, AT, breg);
4742 breg = 0;
4743 tempreg = treg;
4744 off1 = -8;
4745 }
4746
4747 /* Set mips_optimize around the lui instruction to avoid
4748 inserting an unnecessary nop after the lw. */
4749 hold_mips_optimize = mips_optimize;
4750 mips_optimize = 2;
4751 macro_build_lui (NULL, &icnt, &expr1, AT);
4752 mips_optimize = hold_mips_optimize;
4753
4754 macro_build ((char *) NULL, &icnt, &expr1,
4755 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4756 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4757 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4758 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4759 "d,v,t", tempreg, tempreg, AT);
4760 frag_var (rs_machine_dependent, 0, 0,
4761 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4762 offset_expr.X_add_symbol, 0, NULL);
4763 used_at = 1;
4764 }
4765 }
4766 else if (mips_pic == SVR4_PIC)
4767 {
4768 int gpdel;
4769 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4770 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
4771
4772 /* This is the large GOT case. If this is a reference to an
4773 external symbol, and there is no constant, we want
4774 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4775 addu $tempreg,$tempreg,$gp
4776 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4777 or if tempreg is PIC_CALL_REG
4778 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4779 addu $tempreg,$tempreg,$gp
4780 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
4781 For a local symbol, we want
4782 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4783 nop
4784 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4785
4786 If we have a small constant, and this is a reference to
4787 an external symbol, we want
4788 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4789 addu $tempreg,$tempreg,$gp
4790 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4791 nop
4792 addiu $tempreg,$tempreg,<constant>
4793 For a local symbol, we want
4794 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4795 nop
4796 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4797
4798 If we have a large constant, and this is a reference to
4799 an external symbol, we want
4800 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4801 addu $tempreg,$tempreg,$gp
4802 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4803 lui $at,<hiconstant>
4804 addiu $at,$at,<loconstant>
4805 addu $tempreg,$tempreg,$at
4806 For a local symbol, we want
4807 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4808 lui $at,<hiconstant>
4809 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4810 addu $tempreg,$tempreg,$at
4811 */
4812 expr1.X_add_number = offset_expr.X_add_number;
4813 offset_expr.X_add_number = 0;
4814 frag_grow (52);
4815 if (reg_needs_delay (GP))
4816 gpdel = 4;
4817 else
4818 gpdel = 0;
4819 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4820 {
4821 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4822 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4823 }
4824 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4825 tempreg, lui_reloc_type);
4826 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4827 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4828 "d,v,t", tempreg, tempreg, GP);
4829 macro_build ((char *) NULL, &icnt, &offset_expr,
4830 dbl ? "ld" : "lw",
4831 "t,o(b)", tempreg, lw_reloc_type, tempreg);
4832 if (expr1.X_add_number == 0)
4833 {
4834 int off;
4835
4836 if (breg == 0)
4837 off = 0;
4838 else
4839 {
4840 /* We're going to put in an addu instruction using
4841 tempreg, so we may as well insert the nop right
4842 now. */
4843 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4844 "nop", "");
4845 off = 4;
4846 }
4847
4848 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4849 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4850 8 + gpdel, 0,
4851 (breg == 0
4852 ? mips_opts.warn_about_macros
4853 : 0)),
4854 offset_expr.X_add_symbol, 0, NULL);
4855 }
4856 else if (expr1.X_add_number >= -0x8000
4857 && expr1.X_add_number < 0x8000)
4858 {
4859 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4860 "nop", "");
4861 macro_build ((char *) NULL, &icnt, &expr1,
4862 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4863 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4864
4865 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4866 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4867 (breg == 0
4868 ? mips_opts.warn_about_macros
4869 : 0)),
4870 offset_expr.X_add_symbol, 0, NULL);
4871 }
4872 else
4873 {
4874 int adj, dreg;
4875
4876 /* If we are going to add in a base register, and the
4877 target register and the base register are the same,
4878 then we are using AT as a temporary register. Since
4879 we want to load the constant into AT, we add our
4880 current AT (from the global offset table) and the
4881 register into the register now, and pretend we were
4882 not using a base register. */
4883 if (breg != treg)
4884 {
4885 adj = 0;
4886 dreg = tempreg;
4887 }
4888 else
4889 {
4890 assert (tempreg == AT);
4891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4892 "nop", "");
4893 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4894 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4895 "d,v,t", treg, AT, breg);
4896 dreg = treg;
4897 adj = 8;
4898 }
4899
4900 /* Set mips_optimize around the lui instruction to avoid
4901 inserting an unnecessary nop after the lw. */
4902 hold_mips_optimize = mips_optimize;
4903 mips_optimize = 2;
4904 macro_build_lui (NULL, &icnt, &expr1, AT);
4905 mips_optimize = hold_mips_optimize;
4906
4907 macro_build ((char *) NULL, &icnt, &expr1,
4908 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4909 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4911 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4912 "d,v,t", dreg, dreg, AT);
4913
4914 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4915 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4916 8 + gpdel, 0,
4917 (breg == 0
4918 ? mips_opts.warn_about_macros
4919 : 0)),
4920 offset_expr.X_add_symbol, 0, NULL);
4921
4922 used_at = 1;
4923 }
4924
4925 if (gpdel > 0)
4926 {
4927 /* This is needed because this instruction uses $gp, but
4928 the first instruction on the main stream does not. */
4929 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4930 p += 4;
4931 }
4932 macro_build (p, &icnt, &offset_expr,
4933 dbl ? "ld" : "lw",
4934 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
4935 p += 4;
4936 if (expr1.X_add_number >= -0x8000
4937 && expr1.X_add_number < 0x8000)
4938 {
4939 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4940 p += 4;
4941 macro_build (p, &icnt, &expr1,
4942 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4943 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4944 /* FIXME: If add_number is 0, and there was no base
4945 register, the external symbol case ended with a load,
4946 so if the symbol turns out to not be external, and
4947 the next instruction uses tempreg, an unnecessary nop
4948 will be inserted. */
4949 }
4950 else
4951 {
4952 if (breg == treg)
4953 {
4954 /* We must add in the base register now, as in the
4955 external symbol case. */
4956 assert (tempreg == AT);
4957 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4958 p += 4;
4959 macro_build (p, &icnt, (expressionS *) NULL,
4960 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4961 "d,v,t", treg, AT, breg);
4962 p += 4;
4963 tempreg = treg;
4964 /* We set breg to 0 because we have arranged to add
4965 it in in both cases. */
4966 breg = 0;
4967 }
4968
4969 macro_build_lui (p, &icnt, &expr1, AT);
4970 p += 4;
4971 macro_build (p, &icnt, &expr1,
4972 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4973 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4974 p += 4;
4975 macro_build (p, &icnt, (expressionS *) NULL,
4976 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4977 "d,v,t", tempreg, tempreg, AT);
4978 p += 4;
4979 }
4980 }
4981 else if (mips_pic == EMBEDDED_PIC)
4982 {
4983 /* We use
4984 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
4985 */
4986 macro_build ((char *) NULL, &icnt, &offset_expr,
4987 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4988 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
4989 }
4990 else
4991 abort ();
4992
4993 if (breg != 0)
4994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4995 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4996 "d,v,t", treg, tempreg, breg);
4997
4998 if (! used_at)
4999 return;
5000
5001 break;
5002
5003 case M_J_A:
5004 /* The j instruction may not be used in PIC code, since it
5005 requires an absolute address. We convert it to a b
5006 instruction. */
5007 if (mips_pic == NO_PIC)
5008 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5009 else
5010 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5011 return;
5012
5013 /* The jal instructions must be handled as macros because when
5014 generating PIC code they expand to multi-instruction
5015 sequences. Normally they are simple instructions. */
5016 case M_JAL_1:
5017 dreg = RA;
5018 /* Fall through. */
5019 case M_JAL_2:
5020 if (mips_pic == NO_PIC
5021 || mips_pic == EMBEDDED_PIC)
5022 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5023 "d,s", dreg, sreg);
5024 else if (mips_pic == SVR4_PIC)
5025 {
5026 if (sreg != PIC_CALL_REG)
5027 as_warn (_("MIPS PIC call to register other than $25"));
5028
5029 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5030 "d,s", dreg, sreg);
5031 if (! HAVE_NEWABI)
5032 {
5033 if (mips_cprestore_offset < 0)
5034 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5035 else
5036 {
5037 expr1.X_add_number = mips_cprestore_offset;
5038 macro_build ((char *) NULL, &icnt, &expr1,
5039 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5040 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5041 }
5042 }
5043 }
5044 else
5045 abort ();
5046
5047 return;
5048
5049 case M_JAL_A:
5050 if (mips_pic == NO_PIC)
5051 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5052 else if (mips_pic == SVR4_PIC)
5053 {
5054 /* If this is a reference to an external symbol, and we are
5055 using a small GOT, we want
5056 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5057 nop
5058 jalr $25
5059 nop
5060 lw $gp,cprestore($sp)
5061 The cprestore value is set using the .cprestore
5062 pseudo-op. If we are using a big GOT, we want
5063 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5064 addu $25,$25,$gp
5065 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5066 nop
5067 jalr $25
5068 nop
5069 lw $gp,cprestore($sp)
5070 If the symbol is not external, we want
5071 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5072 nop
5073 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5074 jalr $25
5075 nop
5076 lw $gp,cprestore($sp) */
5077 frag_grow (40);
5078 if (! mips_big_got)
5079 {
5080 macro_build ((char *) NULL, &icnt, &offset_expr,
5081 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5082 "t,o(b)", PIC_CALL_REG,
5083 (int) BFD_RELOC_MIPS_CALL16, GP);
5084 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5085 "nop", "");
5086 p = frag_var (rs_machine_dependent, 4, 0,
5087 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5088 offset_expr.X_add_symbol, 0, NULL);
5089 }
5090 else
5091 {
5092 int gpdel;
5093
5094 if (reg_needs_delay (GP))
5095 gpdel = 4;
5096 else
5097 gpdel = 0;
5098 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5099 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5100 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5101 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5102 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5103 macro_build ((char *) NULL, &icnt, &offset_expr,
5104 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5105 "t,o(b)", PIC_CALL_REG,
5106 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5107 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5108 "nop", "");
5109 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5110 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5111 0, 0),
5112 offset_expr.X_add_symbol, 0, NULL);
5113 if (gpdel > 0)
5114 {
5115 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5116 p += 4;
5117 }
5118 macro_build (p, &icnt, &offset_expr,
5119 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5120 "t,o(b)", PIC_CALL_REG,
5121 (int) BFD_RELOC_MIPS_GOT16, GP);
5122 p += 4;
5123 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5124 p += 4;
5125 }
5126 macro_build (p, &icnt, &offset_expr,
5127 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5128 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5129 (int) BFD_RELOC_LO16);
5130 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5131 "jalr", "s", PIC_CALL_REG);
5132 if (! HAVE_NEWABI)
5133 {
5134 if (mips_cprestore_offset < 0)
5135 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5136 else
5137 {
5138 if (mips_opts.noreorder)
5139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5140 "nop", "");
5141 expr1.X_add_number = mips_cprestore_offset;
5142 macro_build ((char *) NULL, &icnt, &expr1,
5143 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5144 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5145 }
5146 }
5147 }
5148 else if (mips_pic == EMBEDDED_PIC)
5149 {
5150 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5151 /* The linker may expand the call to a longer sequence which
5152 uses $at, so we must break rather than return. */
5153 break;
5154 }
5155 else
5156 abort ();
5157
5158 return;
5159
5160 case M_LB_AB:
5161 s = "lb";
5162 goto ld;
5163 case M_LBU_AB:
5164 s = "lbu";
5165 goto ld;
5166 case M_LH_AB:
5167 s = "lh";
5168 goto ld;
5169 case M_LHU_AB:
5170 s = "lhu";
5171 goto ld;
5172 case M_LW_AB:
5173 s = "lw";
5174 goto ld;
5175 case M_LWC0_AB:
5176 s = "lwc0";
5177 /* Itbl support may require additional care here. */
5178 coproc = 1;
5179 goto ld;
5180 case M_LWC1_AB:
5181 s = "lwc1";
5182 /* Itbl support may require additional care here. */
5183 coproc = 1;
5184 goto ld;
5185 case M_LWC2_AB:
5186 s = "lwc2";
5187 /* Itbl support may require additional care here. */
5188 coproc = 1;
5189 goto ld;
5190 case M_LWC3_AB:
5191 s = "lwc3";
5192 /* Itbl support may require additional care here. */
5193 coproc = 1;
5194 goto ld;
5195 case M_LWL_AB:
5196 s = "lwl";
5197 lr = 1;
5198 goto ld;
5199 case M_LWR_AB:
5200 s = "lwr";
5201 lr = 1;
5202 goto ld;
5203 case M_LDC1_AB:
5204 if (mips_arch == CPU_R4650)
5205 {
5206 as_bad (_("opcode not supported on this processor"));
5207 return;
5208 }
5209 s = "ldc1";
5210 /* Itbl support may require additional care here. */
5211 coproc = 1;
5212 goto ld;
5213 case M_LDC2_AB:
5214 s = "ldc2";
5215 /* Itbl support may require additional care here. */
5216 coproc = 1;
5217 goto ld;
5218 case M_LDC3_AB:
5219 s = "ldc3";
5220 /* Itbl support may require additional care here. */
5221 coproc = 1;
5222 goto ld;
5223 case M_LDL_AB:
5224 s = "ldl";
5225 lr = 1;
5226 goto ld;
5227 case M_LDR_AB:
5228 s = "ldr";
5229 lr = 1;
5230 goto ld;
5231 case M_LL_AB:
5232 s = "ll";
5233 goto ld;
5234 case M_LLD_AB:
5235 s = "lld";
5236 goto ld;
5237 case M_LWU_AB:
5238 s = "lwu";
5239 ld:
5240 if (breg == treg || coproc || lr)
5241 {
5242 tempreg = AT;
5243 used_at = 1;
5244 }
5245 else
5246 {
5247 tempreg = treg;
5248 used_at = 0;
5249 }
5250 goto ld_st;
5251 case M_SB_AB:
5252 s = "sb";
5253 goto st;
5254 case M_SH_AB:
5255 s = "sh";
5256 goto st;
5257 case M_SW_AB:
5258 s = "sw";
5259 goto st;
5260 case M_SWC0_AB:
5261 s = "swc0";
5262 /* Itbl support may require additional care here. */
5263 coproc = 1;
5264 goto st;
5265 case M_SWC1_AB:
5266 s = "swc1";
5267 /* Itbl support may require additional care here. */
5268 coproc = 1;
5269 goto st;
5270 case M_SWC2_AB:
5271 s = "swc2";
5272 /* Itbl support may require additional care here. */
5273 coproc = 1;
5274 goto st;
5275 case M_SWC3_AB:
5276 s = "swc3";
5277 /* Itbl support may require additional care here. */
5278 coproc = 1;
5279 goto st;
5280 case M_SWL_AB:
5281 s = "swl";
5282 goto st;
5283 case M_SWR_AB:
5284 s = "swr";
5285 goto st;
5286 case M_SC_AB:
5287 s = "sc";
5288 goto st;
5289 case M_SCD_AB:
5290 s = "scd";
5291 goto st;
5292 case M_SDC1_AB:
5293 if (mips_arch == CPU_R4650)
5294 {
5295 as_bad (_("opcode not supported on this processor"));
5296 return;
5297 }
5298 s = "sdc1";
5299 coproc = 1;
5300 /* Itbl support may require additional care here. */
5301 goto st;
5302 case M_SDC2_AB:
5303 s = "sdc2";
5304 /* Itbl support may require additional care here. */
5305 coproc = 1;
5306 goto st;
5307 case M_SDC3_AB:
5308 s = "sdc3";
5309 /* Itbl support may require additional care here. */
5310 coproc = 1;
5311 goto st;
5312 case M_SDL_AB:
5313 s = "sdl";
5314 goto st;
5315 case M_SDR_AB:
5316 s = "sdr";
5317 st:
5318 tempreg = AT;
5319 used_at = 1;
5320 ld_st:
5321 /* Itbl support may require additional care here. */
5322 if (mask == M_LWC1_AB
5323 || mask == M_SWC1_AB
5324 || mask == M_LDC1_AB
5325 || mask == M_SDC1_AB
5326 || mask == M_L_DAB
5327 || mask == M_S_DAB)
5328 fmt = "T,o(b)";
5329 else if (coproc)
5330 fmt = "E,o(b)";
5331 else
5332 fmt = "t,o(b)";
5333
5334 /* For embedded PIC, we allow loads where the offset is calculated
5335 by subtracting a symbol in the current segment from an unknown
5336 symbol, relative to a base register, e.g.:
5337 <op> $treg, <sym>-<localsym>($breg)
5338 This is used by the compiler for switch statements. */
5339 if (mips_pic == EMBEDDED_PIC
5340 && offset_expr.X_op == O_subtract
5341 && (symbol_constant_p (offset_expr.X_op_symbol)
5342 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5343 : (symbol_equated_p (offset_expr.X_op_symbol)
5344 && (S_GET_SEGMENT
5345 (symbol_get_value_expression (offset_expr.X_op_symbol)
5346 ->X_add_symbol)
5347 == now_seg)))
5348 && breg != 0
5349 && (offset_expr.X_add_number == 0
5350 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5351 {
5352 /* For this case, we output the instructions:
5353 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5354 addiu $tempreg,$tempreg,$breg
5355 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5356 If the relocation would fit entirely in 16 bits, it would be
5357 nice to emit:
5358 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5359 instead, but that seems quite difficult. */
5360 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5361 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5362 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5363 ((bfd_arch_bits_per_address (stdoutput) == 32
5364 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5365 ? "addu" : "daddu"),
5366 "d,v,t", tempreg, tempreg, breg);
5367 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5368 (int) BFD_RELOC_PCREL_LO16, tempreg);
5369 if (! used_at)
5370 return;
5371 break;
5372 }
5373
5374 if (offset_expr.X_op != O_constant
5375 && offset_expr.X_op != O_symbol)
5376 {
5377 as_bad (_("expression too complex"));
5378 offset_expr.X_op = O_constant;
5379 }
5380
5381 /* A constant expression in PIC code can be handled just as it
5382 is in non PIC code. */
5383 if (mips_pic == NO_PIC
5384 || offset_expr.X_op == O_constant)
5385 {
5386 /* If this is a reference to a GP relative symbol, and there
5387 is no base register, we want
5388 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5389 Otherwise, if there is no base register, we want
5390 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5391 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5392 If we have a constant, we need two instructions anyhow,
5393 so we always use the latter form.
5394
5395 If we have a base register, and this is a reference to a
5396 GP relative symbol, we want
5397 addu $tempreg,$breg,$gp
5398 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5399 Otherwise we want
5400 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5401 addu $tempreg,$tempreg,$breg
5402 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5403 With a constant we always use the latter case.
5404
5405 With 64bit address space and no base register and $at usable,
5406 we want
5407 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5408 lui $at,<sym> (BFD_RELOC_HI16_S)
5409 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5410 dsll32 $tempreg,0
5411 daddu $tempreg,$at
5412 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5413 If we have a base register, we want
5414 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5415 lui $at,<sym> (BFD_RELOC_HI16_S)
5416 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5417 daddu $at,$breg
5418 dsll32 $tempreg,0
5419 daddu $tempreg,$at
5420 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5421
5422 Without $at we can't generate the optimal path for superscalar
5423 processors here since this would require two temporary registers.
5424 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5425 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5426 dsll $tempreg,16
5427 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5428 dsll $tempreg,16
5429 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5430 If we have a base register, we want
5431 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5432 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5433 dsll $tempreg,16
5434 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5435 dsll $tempreg,16
5436 daddu $tempreg,$tempreg,$breg
5437 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5438 */
5439 if (HAVE_64BIT_ADDRESSES)
5440 {
5441 p = NULL;
5442
5443 /* We don't do GP optimization for now because RELAX_ENCODE can't
5444 hold the data for such large chunks. */
5445
5446 if (used_at == 0)
5447 {
5448 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5449 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5450 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5451 AT, (int) BFD_RELOC_HI16_S);
5452 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5453 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5454 if (breg != 0)
5455 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5456 "d,v,t", AT, AT, breg);
5457 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5458 "d,w,<", tempreg, tempreg, 0);
5459 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5460 "d,v,t", tempreg, tempreg, AT);
5461 macro_build (p, &icnt, &offset_expr, s,
5462 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5463 used_at = 1;
5464 }
5465 else
5466 {
5467 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5468 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5469 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5470 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5471 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5472 "d,w,<", tempreg, tempreg, 16);
5473 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5474 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5475 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5476 "d,w,<", tempreg, tempreg, 16);
5477 if (breg != 0)
5478 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5479 "d,v,t", tempreg, tempreg, breg);
5480 macro_build (p, &icnt, &offset_expr, s,
5481 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5482 }
5483
5484 return;
5485 }
5486
5487 if (breg == 0)
5488 {
5489 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5490 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5491 p = NULL;
5492 else
5493 {
5494 frag_grow (20);
5495 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5496 treg, (int) BFD_RELOC_GPREL16, GP);
5497 p = frag_var (rs_machine_dependent, 8, 0,
5498 RELAX_ENCODE (4, 8, 0, 4, 0,
5499 (mips_opts.warn_about_macros
5500 || (used_at
5501 && mips_opts.noat))),
5502 offset_expr.X_add_symbol, (offsetT) 0,
5503 (char *) NULL);
5504 used_at = 0;
5505 }
5506 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5507 if (p != NULL)
5508 p += 4;
5509 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5510 (int) BFD_RELOC_LO16, tempreg);
5511 }
5512 else
5513 {
5514 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
5515 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5516 p = NULL;
5517 else
5518 {
5519 frag_grow (28);
5520 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5521 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5522 "d,v,t", tempreg, breg, GP);
5523 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5524 treg, (int) BFD_RELOC_GPREL16, tempreg);
5525 p = frag_var (rs_machine_dependent, 12, 0,
5526 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
5527 offset_expr.X_add_symbol, (offsetT) 0,
5528 (char *) NULL);
5529 }
5530 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5531 if (p != NULL)
5532 p += 4;
5533 macro_build (p, &icnt, (expressionS *) NULL,
5534 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5535 "d,v,t", tempreg, tempreg, breg);
5536 if (p != NULL)
5537 p += 4;
5538 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5539 (int) BFD_RELOC_LO16, tempreg);
5540 }
5541 }
5542 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5543 {
5544 /* If this is a reference to an external symbol, we want
5545 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5546 nop
5547 <op> $treg,0($tempreg)
5548 Otherwise we want
5549 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5550 nop
5551 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5552 <op> $treg,0($tempreg)
5553 If there is a base register, we add it to $tempreg before
5554 the <op>. If there is a constant, we stick it in the
5555 <op> instruction. We don't handle constants larger than
5556 16 bits, because we have no way to load the upper 16 bits
5557 (actually, we could handle them for the subset of cases
5558 in which we are not using $at). */
5559 assert (offset_expr.X_op == O_symbol);
5560 expr1.X_add_number = offset_expr.X_add_number;
5561 offset_expr.X_add_number = 0;
5562 if (expr1.X_add_number < -0x8000
5563 || expr1.X_add_number >= 0x8000)
5564 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5565 frag_grow (20);
5566 macro_build ((char *) NULL, &icnt, &offset_expr,
5567 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5568 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5569 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
5570 p = frag_var (rs_machine_dependent, 4, 0,
5571 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5572 offset_expr.X_add_symbol, 0, NULL);
5573 macro_build (p, &icnt, &offset_expr,
5574 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5575 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5576 if (breg != 0)
5577 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5578 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5579 "d,v,t", tempreg, tempreg, breg);
5580 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5581 (int) BFD_RELOC_LO16, tempreg);
5582 }
5583 else if (mips_pic == SVR4_PIC)
5584 {
5585 int gpdel;
5586
5587 /* If this is a reference to an external symbol, we want
5588 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5589 addu $tempreg,$tempreg,$gp
5590 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5591 <op> $treg,0($tempreg)
5592 Otherwise we want
5593 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5594 nop
5595 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5596 <op> $treg,0($tempreg)
5597 If there is a base register, we add it to $tempreg before
5598 the <op>. If there is a constant, we stick it in the
5599 <op> instruction. We don't handle constants larger than
5600 16 bits, because we have no way to load the upper 16 bits
5601 (actually, we could handle them for the subset of cases
5602 in which we are not using $at). */
5603 assert (offset_expr.X_op == O_symbol);
5604 expr1.X_add_number = offset_expr.X_add_number;
5605 offset_expr.X_add_number = 0;
5606 if (expr1.X_add_number < -0x8000
5607 || expr1.X_add_number >= 0x8000)
5608 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5609 if (reg_needs_delay (GP))
5610 gpdel = 4;
5611 else
5612 gpdel = 0;
5613 frag_grow (36);
5614 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5615 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5616 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5617 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5618 "d,v,t", tempreg, tempreg, GP);
5619 macro_build ((char *) NULL, &icnt, &offset_expr,
5620 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5621 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5622 tempreg);
5623 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5624 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
5625 offset_expr.X_add_symbol, 0, NULL);
5626 if (gpdel > 0)
5627 {
5628 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5629 p += 4;
5630 }
5631 macro_build (p, &icnt, &offset_expr,
5632 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5633 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5634 p += 4;
5635 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5636 p += 4;
5637 macro_build (p, &icnt, &offset_expr,
5638 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5639 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5640 if (breg != 0)
5641 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5642 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5643 "d,v,t", tempreg, tempreg, breg);
5644 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5645 (int) BFD_RELOC_LO16, tempreg);
5646 }
5647 else if (mips_pic == EMBEDDED_PIC)
5648 {
5649 /* If there is no base register, we want
5650 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
5651 If there is a base register, we want
5652 addu $tempreg,$breg,$gp
5653 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
5654 */
5655 assert (offset_expr.X_op == O_symbol);
5656 if (breg == 0)
5657 {
5658 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5659 treg, (int) BFD_RELOC_GPREL16, GP);
5660 used_at = 0;
5661 }
5662 else
5663 {
5664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5665 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5666 "d,v,t", tempreg, breg, GP);
5667 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
5668 treg, (int) BFD_RELOC_GPREL16, tempreg);
5669 }
5670 }
5671 else
5672 abort ();
5673
5674 if (! used_at)
5675 return;
5676
5677 break;
5678
5679 case M_LI:
5680 case M_LI_S:
5681 load_register (&icnt, treg, &imm_expr, 0);
5682 return;
5683
5684 case M_DLI:
5685 load_register (&icnt, treg, &imm_expr, 1);
5686 return;
5687
5688 case M_LI_SS:
5689 if (imm_expr.X_op == O_constant)
5690 {
5691 load_register (&icnt, AT, &imm_expr, 0);
5692 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5693 "mtc1", "t,G", AT, treg);
5694 break;
5695 }
5696 else
5697 {
5698 assert (offset_expr.X_op == O_symbol
5699 && strcmp (segment_name (S_GET_SEGMENT
5700 (offset_expr.X_add_symbol)),
5701 ".lit4") == 0
5702 && offset_expr.X_add_number == 0);
5703 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5704 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5705 return;
5706 }
5707
5708 case M_LI_D:
5709 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5710 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5711 order 32 bits of the value and the low order 32 bits are either
5712 zero or in OFFSET_EXPR. */
5713 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5714 {
5715 if (HAVE_64BIT_GPRS)
5716 load_register (&icnt, treg, &imm_expr, 1);
5717 else
5718 {
5719 int hreg, lreg;
5720
5721 if (target_big_endian)
5722 {
5723 hreg = treg;
5724 lreg = treg + 1;
5725 }
5726 else
5727 {
5728 hreg = treg + 1;
5729 lreg = treg;
5730 }
5731
5732 if (hreg <= 31)
5733 load_register (&icnt, hreg, &imm_expr, 0);
5734 if (lreg <= 31)
5735 {
5736 if (offset_expr.X_op == O_absent)
5737 move_register (&icnt, lreg, 0);
5738 else
5739 {
5740 assert (offset_expr.X_op == O_constant);
5741 load_register (&icnt, lreg, &offset_expr, 0);
5742 }
5743 }
5744 }
5745 return;
5746 }
5747
5748 /* We know that sym is in the .rdata section. First we get the
5749 upper 16 bits of the address. */
5750 if (mips_pic == NO_PIC)
5751 {
5752 /* FIXME: This won't work for a 64 bit address. */
5753 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5754 }
5755 else if (mips_pic == SVR4_PIC)
5756 {
5757 macro_build ((char *) NULL, &icnt, &offset_expr,
5758 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5759 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5760 }
5761 else if (mips_pic == EMBEDDED_PIC)
5762 {
5763 /* For embedded PIC we pick up the entire address off $gp in
5764 a single instruction. */
5765 macro_build ((char *) NULL, &icnt, &offset_expr,
5766 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5767 "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
5768 offset_expr.X_op = O_constant;
5769 offset_expr.X_add_number = 0;
5770 }
5771 else
5772 abort ();
5773
5774 /* Now we load the register(s). */
5775 if (HAVE_64BIT_GPRS)
5776 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5777 treg, (int) BFD_RELOC_LO16, AT);
5778 else
5779 {
5780 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5781 treg, (int) BFD_RELOC_LO16, AT);
5782 if (treg != 31)
5783 {
5784 /* FIXME: How in the world do we deal with the possible
5785 overflow here? */
5786 offset_expr.X_add_number += 4;
5787 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5788 treg + 1, (int) BFD_RELOC_LO16, AT);
5789 }
5790 }
5791
5792 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5793 does not become a variant frag. */
5794 frag_wane (frag_now);
5795 frag_new (0);
5796
5797 break;
5798
5799 case M_LI_DD:
5800 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5801 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5802 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5803 the value and the low order 32 bits are either zero or in
5804 OFFSET_EXPR. */
5805 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5806 {
5807 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5808 if (HAVE_64BIT_FPRS)
5809 {
5810 assert (HAVE_64BIT_GPRS);
5811 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5812 "dmtc1", "t,S", AT, treg);
5813 }
5814 else
5815 {
5816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5817 "mtc1", "t,G", AT, treg + 1);
5818 if (offset_expr.X_op == O_absent)
5819 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5820 "mtc1", "t,G", 0, treg);
5821 else
5822 {
5823 assert (offset_expr.X_op == O_constant);
5824 load_register (&icnt, AT, &offset_expr, 0);
5825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5826 "mtc1", "t,G", AT, treg);
5827 }
5828 }
5829 break;
5830 }
5831
5832 assert (offset_expr.X_op == O_symbol
5833 && offset_expr.X_add_number == 0);
5834 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5835 if (strcmp (s, ".lit8") == 0)
5836 {
5837 if (mips_opts.isa != ISA_MIPS1)
5838 {
5839 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5840 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5841 return;
5842 }
5843 breg = GP;
5844 r = BFD_RELOC_MIPS_LITERAL;
5845 goto dob;
5846 }
5847 else
5848 {
5849 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5850 if (mips_pic == SVR4_PIC)
5851 macro_build ((char *) NULL, &icnt, &offset_expr,
5852 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5853 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5854 else
5855 {
5856 /* FIXME: This won't work for a 64 bit address. */
5857 macro_build_lui ((char *) NULL, &icnt, &offset_expr, AT);
5858 }
5859
5860 if (mips_opts.isa != ISA_MIPS1)
5861 {
5862 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5863 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5864
5865 /* To avoid confusion in tc_gen_reloc, we must ensure
5866 that this does not become a variant frag. */
5867 frag_wane (frag_now);
5868 frag_new (0);
5869
5870 break;
5871 }
5872 breg = AT;
5873 r = BFD_RELOC_LO16;
5874 goto dob;
5875 }
5876
5877 case M_L_DOB:
5878 if (mips_arch == CPU_R4650)
5879 {
5880 as_bad (_("opcode not supported on this processor"));
5881 return;
5882 }
5883 /* Even on a big endian machine $fn comes before $fn+1. We have
5884 to adjust when loading from memory. */
5885 r = BFD_RELOC_LO16;
5886 dob:
5887 assert (mips_opts.isa == ISA_MIPS1);
5888 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5889 target_big_endian ? treg + 1 : treg,
5890 (int) r, breg);
5891 /* FIXME: A possible overflow which I don't know how to deal
5892 with. */
5893 offset_expr.X_add_number += 4;
5894 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5895 target_big_endian ? treg : treg + 1,
5896 (int) r, breg);
5897
5898 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5899 does not become a variant frag. */
5900 frag_wane (frag_now);
5901 frag_new (0);
5902
5903 if (breg != AT)
5904 return;
5905 break;
5906
5907 case M_L_DAB:
5908 /*
5909 * The MIPS assembler seems to check for X_add_number not
5910 * being double aligned and generating:
5911 * lui at,%hi(foo+1)
5912 * addu at,at,v1
5913 * addiu at,at,%lo(foo+1)
5914 * lwc1 f2,0(at)
5915 * lwc1 f3,4(at)
5916 * But, the resulting address is the same after relocation so why
5917 * generate the extra instruction?
5918 */
5919 if (mips_arch == CPU_R4650)
5920 {
5921 as_bad (_("opcode not supported on this processor"));
5922 return;
5923 }
5924 /* Itbl support may require additional care here. */
5925 coproc = 1;
5926 if (mips_opts.isa != ISA_MIPS1)
5927 {
5928 s = "ldc1";
5929 goto ld;
5930 }
5931
5932 s = "lwc1";
5933 fmt = "T,o(b)";
5934 goto ldd_std;
5935
5936 case M_S_DAB:
5937 if (mips_arch == CPU_R4650)
5938 {
5939 as_bad (_("opcode not supported on this processor"));
5940 return;
5941 }
5942
5943 if (mips_opts.isa != ISA_MIPS1)
5944 {
5945 s = "sdc1";
5946 goto st;
5947 }
5948
5949 s = "swc1";
5950 fmt = "T,o(b)";
5951 /* Itbl support may require additional care here. */
5952 coproc = 1;
5953 goto ldd_std;
5954
5955 case M_LD_AB:
5956 if (HAVE_64BIT_GPRS)
5957 {
5958 s = "ld";
5959 goto ld;
5960 }
5961
5962 s = "lw";
5963 fmt = "t,o(b)";
5964 goto ldd_std;
5965
5966 case M_SD_AB:
5967 if (HAVE_64BIT_GPRS)
5968 {
5969 s = "sd";
5970 goto st;
5971 }
5972
5973 s = "sw";
5974 fmt = "t,o(b)";
5975
5976 ldd_std:
5977 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
5978 loads for the case of doing a pair of loads to simulate an 'ld'.
5979 This is not currently done by the compiler, and assembly coders
5980 writing embedded-pic code can cope. */
5981
5982 if (offset_expr.X_op != O_symbol
5983 && offset_expr.X_op != O_constant)
5984 {
5985 as_bad (_("expression too complex"));
5986 offset_expr.X_op = O_constant;
5987 }
5988
5989 /* Even on a big endian machine $fn comes before $fn+1. We have
5990 to adjust when loading from memory. We set coproc if we must
5991 load $fn+1 first. */
5992 /* Itbl support may require additional care here. */
5993 if (! target_big_endian)
5994 coproc = 0;
5995
5996 if (mips_pic == NO_PIC
5997 || offset_expr.X_op == O_constant)
5998 {
5999 /* If this is a reference to a GP relative symbol, we want
6000 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6001 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6002 If we have a base register, we use this
6003 addu $at,$breg,$gp
6004 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6005 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6006 If this is not a GP relative symbol, we want
6007 lui $at,<sym> (BFD_RELOC_HI16_S)
6008 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6009 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6010 If there is a base register, we add it to $at after the
6011 lui instruction. If there is a constant, we always use
6012 the last case. */
6013 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
6014 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6015 {
6016 p = NULL;
6017 used_at = 1;
6018 }
6019 else
6020 {
6021 int off;
6022
6023 if (breg == 0)
6024 {
6025 frag_grow (28);
6026 tempreg = GP;
6027 off = 0;
6028 used_at = 0;
6029 }
6030 else
6031 {
6032 frag_grow (36);
6033 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6034 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6035 "d,v,t", AT, breg, GP);
6036 tempreg = AT;
6037 off = 4;
6038 used_at = 1;
6039 }
6040
6041 /* Itbl support may require additional care here. */
6042 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6043 coproc ? treg + 1 : treg,
6044 (int) BFD_RELOC_GPREL16, tempreg);
6045 offset_expr.X_add_number += 4;
6046
6047 /* Set mips_optimize to 2 to avoid inserting an
6048 undesired nop. */
6049 hold_mips_optimize = mips_optimize;
6050 mips_optimize = 2;
6051 /* Itbl support may require additional care here. */
6052 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6053 coproc ? treg : treg + 1,
6054 (int) BFD_RELOC_GPREL16, tempreg);
6055 mips_optimize = hold_mips_optimize;
6056
6057 p = frag_var (rs_machine_dependent, 12 + off, 0,
6058 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6059 used_at && mips_opts.noat),
6060 offset_expr.X_add_symbol, (offsetT) 0,
6061 (char *) NULL);
6062
6063 /* We just generated two relocs. When tc_gen_reloc
6064 handles this case, it will skip the first reloc and
6065 handle the second. The second reloc already has an
6066 extra addend of 4, which we added above. We must
6067 subtract it out, and then subtract another 4 to make
6068 the first reloc come out right. The second reloc
6069 will come out right because we are going to add 4 to
6070 offset_expr when we build its instruction below.
6071
6072 If we have a symbol, then we don't want to include
6073 the offset, because it will wind up being included
6074 when we generate the reloc. */
6075
6076 if (offset_expr.X_op == O_constant)
6077 offset_expr.X_add_number -= 8;
6078 else
6079 {
6080 offset_expr.X_add_number = -4;
6081 offset_expr.X_op = O_constant;
6082 }
6083 }
6084 macro_build_lui (p, &icnt, &offset_expr, AT);
6085 if (p != NULL)
6086 p += 4;
6087 if (breg != 0)
6088 {
6089 macro_build (p, &icnt, (expressionS *) NULL,
6090 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6091 "d,v,t", AT, breg, AT);
6092 if (p != NULL)
6093 p += 4;
6094 }
6095 /* Itbl support may require additional care here. */
6096 macro_build (p, &icnt, &offset_expr, s, fmt,
6097 coproc ? treg + 1 : treg,
6098 (int) BFD_RELOC_LO16, AT);
6099 if (p != NULL)
6100 p += 4;
6101 /* FIXME: How do we handle overflow here? */
6102 offset_expr.X_add_number += 4;
6103 /* Itbl support may require additional care here. */
6104 macro_build (p, &icnt, &offset_expr, s, fmt,
6105 coproc ? treg : treg + 1,
6106 (int) BFD_RELOC_LO16, AT);
6107 }
6108 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6109 {
6110 int off;
6111
6112 /* If this is a reference to an external symbol, we want
6113 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6114 nop
6115 <op> $treg,0($at)
6116 <op> $treg+1,4($at)
6117 Otherwise we want
6118 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6119 nop
6120 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6121 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6122 If there is a base register we add it to $at before the
6123 lwc1 instructions. If there is a constant we include it
6124 in the lwc1 instructions. */
6125 used_at = 1;
6126 expr1.X_add_number = offset_expr.X_add_number;
6127 offset_expr.X_add_number = 0;
6128 if (expr1.X_add_number < -0x8000
6129 || expr1.X_add_number >= 0x8000 - 4)
6130 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6131 if (breg == 0)
6132 off = 0;
6133 else
6134 off = 4;
6135 frag_grow (24 + off);
6136 macro_build ((char *) NULL, &icnt, &offset_expr,
6137 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6138 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6140 if (breg != 0)
6141 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6142 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6143 "d,v,t", AT, breg, AT);
6144 /* Itbl support may require additional care here. */
6145 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6146 coproc ? treg + 1 : treg,
6147 (int) BFD_RELOC_LO16, AT);
6148 expr1.X_add_number += 4;
6149
6150 /* Set mips_optimize to 2 to avoid inserting an undesired
6151 nop. */
6152 hold_mips_optimize = mips_optimize;
6153 mips_optimize = 2;
6154 /* Itbl support may require additional care here. */
6155 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6156 coproc ? treg : treg + 1,
6157 (int) BFD_RELOC_LO16, AT);
6158 mips_optimize = hold_mips_optimize;
6159
6160 (void) frag_var (rs_machine_dependent, 0, 0,
6161 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
6162 offset_expr.X_add_symbol, 0, NULL);
6163 }
6164 else if (mips_pic == SVR4_PIC)
6165 {
6166 int gpdel, off;
6167
6168 /* If this is a reference to an external symbol, we want
6169 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6170 addu $at,$at,$gp
6171 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6172 nop
6173 <op> $treg,0($at)
6174 <op> $treg+1,4($at)
6175 Otherwise we want
6176 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6177 nop
6178 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6179 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6180 If there is a base register we add it to $at before the
6181 lwc1 instructions. If there is a constant we include it
6182 in the lwc1 instructions. */
6183 used_at = 1;
6184 expr1.X_add_number = offset_expr.X_add_number;
6185 offset_expr.X_add_number = 0;
6186 if (expr1.X_add_number < -0x8000
6187 || expr1.X_add_number >= 0x8000 - 4)
6188 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6189 if (reg_needs_delay (GP))
6190 gpdel = 4;
6191 else
6192 gpdel = 0;
6193 if (breg == 0)
6194 off = 0;
6195 else
6196 off = 4;
6197 frag_grow (56);
6198 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6199 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6200 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6201 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6202 "d,v,t", AT, AT, GP);
6203 macro_build ((char *) NULL, &icnt, &offset_expr,
6204 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6205 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6207 if (breg != 0)
6208 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6209 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6210 "d,v,t", AT, breg, AT);
6211 /* Itbl support may require additional care here. */
6212 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6213 coproc ? treg + 1 : treg,
6214 (int) BFD_RELOC_LO16, AT);
6215 expr1.X_add_number += 4;
6216
6217 /* Set mips_optimize to 2 to avoid inserting an undesired
6218 nop. */
6219 hold_mips_optimize = mips_optimize;
6220 mips_optimize = 2;
6221 /* Itbl support may require additional care here. */
6222 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6223 coproc ? treg : treg + 1,
6224 (int) BFD_RELOC_LO16, AT);
6225 mips_optimize = hold_mips_optimize;
6226 expr1.X_add_number -= 4;
6227
6228 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6229 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6230 8 + gpdel + off, 1, 0),
6231 offset_expr.X_add_symbol, 0, NULL);
6232 if (gpdel > 0)
6233 {
6234 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6235 p += 4;
6236 }
6237 macro_build (p, &icnt, &offset_expr,
6238 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6239 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6240 p += 4;
6241 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6242 p += 4;
6243 if (breg != 0)
6244 {
6245 macro_build (p, &icnt, (expressionS *) NULL,
6246 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6247 "d,v,t", AT, breg, AT);
6248 p += 4;
6249 }
6250 /* Itbl support may require additional care here. */
6251 macro_build (p, &icnt, &expr1, s, fmt,
6252 coproc ? treg + 1 : treg,
6253 (int) BFD_RELOC_LO16, AT);
6254 p += 4;
6255 expr1.X_add_number += 4;
6256
6257 /* Set mips_optimize to 2 to avoid inserting an undesired
6258 nop. */
6259 hold_mips_optimize = mips_optimize;
6260 mips_optimize = 2;
6261 /* Itbl support may require additional care here. */
6262 macro_build (p, &icnt, &expr1, s, fmt,
6263 coproc ? treg : treg + 1,
6264 (int) BFD_RELOC_LO16, AT);
6265 mips_optimize = hold_mips_optimize;
6266 }
6267 else if (mips_pic == EMBEDDED_PIC)
6268 {
6269 /* If there is no base register, we use
6270 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6271 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
6272 If we have a base register, we use
6273 addu $at,$breg,$gp
6274 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6275 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
6276 */
6277 if (breg == 0)
6278 {
6279 tempreg = GP;
6280 used_at = 0;
6281 }
6282 else
6283 {
6284 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6285 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6286 "d,v,t", AT, breg, GP);
6287 tempreg = AT;
6288 used_at = 1;
6289 }
6290
6291 /* Itbl support may require additional care here. */
6292 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6293 coproc ? treg + 1 : treg,
6294 (int) BFD_RELOC_GPREL16, tempreg);
6295 offset_expr.X_add_number += 4;
6296 /* Itbl support may require additional care here. */
6297 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6298 coproc ? treg : treg + 1,
6299 (int) BFD_RELOC_GPREL16, tempreg);
6300 }
6301 else
6302 abort ();
6303
6304 if (! used_at)
6305 return;
6306
6307 break;
6308
6309 case M_LD_OB:
6310 s = "lw";
6311 goto sd_ob;
6312 case M_SD_OB:
6313 s = "sw";
6314 sd_ob:
6315 assert (HAVE_32BIT_ADDRESSES);
6316 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6317 (int) BFD_RELOC_LO16, breg);
6318 offset_expr.X_add_number += 4;
6319 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6320 (int) BFD_RELOC_LO16, breg);
6321 return;
6322
6323 /* New code added to support COPZ instructions.
6324 This code builds table entries out of the macros in mip_opcodes.
6325 R4000 uses interlocks to handle coproc delays.
6326 Other chips (like the R3000) require nops to be inserted for delays.
6327
6328 FIXME: Currently, we require that the user handle delays.
6329 In order to fill delay slots for non-interlocked chips,
6330 we must have a way to specify delays based on the coprocessor.
6331 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6332 What are the side-effects of the cop instruction?
6333 What cache support might we have and what are its effects?
6334 Both coprocessor & memory require delays. how long???
6335 What registers are read/set/modified?
6336
6337 If an itbl is provided to interpret cop instructions,
6338 this knowledge can be encoded in the itbl spec. */
6339
6340 case M_COP0:
6341 s = "c0";
6342 goto copz;
6343 case M_COP1:
6344 s = "c1";
6345 goto copz;
6346 case M_COP2:
6347 s = "c2";
6348 goto copz;
6349 case M_COP3:
6350 s = "c3";
6351 copz:
6352 /* For now we just do C (same as Cz). The parameter will be
6353 stored in insn_opcode by mips_ip. */
6354 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6355 ip->insn_opcode);
6356 return;
6357
6358 case M_MOVE:
6359 move_register (&icnt, dreg, sreg);
6360 return;
6361
6362 #ifdef LOSING_COMPILER
6363 default:
6364 /* Try and see if this is a new itbl instruction.
6365 This code builds table entries out of the macros in mip_opcodes.
6366 FIXME: For now we just assemble the expression and pass it's
6367 value along as a 32-bit immediate.
6368 We may want to have the assembler assemble this value,
6369 so that we gain the assembler's knowledge of delay slots,
6370 symbols, etc.
6371 Would it be more efficient to use mask (id) here? */
6372 if (itbl_have_entries
6373 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
6374 {
6375 s = ip->insn_mo->name;
6376 s2 = "cop3";
6377 coproc = ITBL_DECODE_PNUM (immed_expr);;
6378 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6379 return;
6380 }
6381 macro2 (ip);
6382 return;
6383 }
6384 if (mips_opts.noat)
6385 as_warn (_("Macro used $at after \".set noat\""));
6386 }
6387
6388 static void
6389 macro2 (ip)
6390 struct mips_cl_insn *ip;
6391 {
6392 register int treg, sreg, dreg, breg;
6393 int tempreg;
6394 int mask;
6395 int icnt = 0;
6396 int used_at;
6397 expressionS expr1;
6398 const char *s;
6399 const char *s2;
6400 const char *fmt;
6401 int likely = 0;
6402 int dbl = 0;
6403 int coproc = 0;
6404 int lr = 0;
6405 int imm = 0;
6406 int off;
6407 offsetT maxnum;
6408 bfd_reloc_code_real_type r;
6409 char *p;
6410
6411 treg = (ip->insn_opcode >> 16) & 0x1f;
6412 dreg = (ip->insn_opcode >> 11) & 0x1f;
6413 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6414 mask = ip->insn_mo->mask;
6415
6416 expr1.X_op = O_constant;
6417 expr1.X_op_symbol = NULL;
6418 expr1.X_add_symbol = NULL;
6419 expr1.X_add_number = 1;
6420
6421 switch (mask)
6422 {
6423 #endif /* LOSING_COMPILER */
6424
6425 case M_DMUL:
6426 dbl = 1;
6427 case M_MUL:
6428 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6429 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6430 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6431 dreg);
6432 return;
6433
6434 case M_DMUL_I:
6435 dbl = 1;
6436 case M_MUL_I:
6437 /* The MIPS assembler some times generates shifts and adds. I'm
6438 not trying to be that fancy. GCC should do this for us
6439 anyway. */
6440 load_register (&icnt, AT, &imm_expr, dbl);
6441 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6442 dbl ? "dmult" : "mult", "s,t", sreg, AT);
6443 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6444 dreg);
6445 break;
6446
6447 case M_DMULO_I:
6448 dbl = 1;
6449 case M_MULO_I:
6450 imm = 1;
6451 goto do_mulo;
6452
6453 case M_DMULO:
6454 dbl = 1;
6455 case M_MULO:
6456 do_mulo:
6457 mips_emit_delays (true);
6458 ++mips_opts.noreorder;
6459 mips_any_noreorder = 1;
6460 if (imm)
6461 load_register (&icnt, AT, &imm_expr, dbl);
6462 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6463 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6464 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6465 dreg);
6466 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6467 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
6468 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6469 AT);
6470 if (mips_trap)
6471 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6472 dreg, AT);
6473 else
6474 {
6475 expr1.X_add_number = 8;
6476 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6477 AT);
6478 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6479 0);
6480 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6481 "c", 6);
6482 }
6483 --mips_opts.noreorder;
6484 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
6485 break;
6486
6487 case M_DMULOU_I:
6488 dbl = 1;
6489 case M_MULOU_I:
6490 imm = 1;
6491 goto do_mulou;
6492
6493 case M_DMULOU:
6494 dbl = 1;
6495 case M_MULOU:
6496 do_mulou:
6497 mips_emit_delays (true);
6498 ++mips_opts.noreorder;
6499 mips_any_noreorder = 1;
6500 if (imm)
6501 load_register (&icnt, AT, &imm_expr, dbl);
6502 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6503 dbl ? "dmultu" : "multu",
6504 "s,t", sreg, imm ? AT : treg);
6505 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6506 AT);
6507 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6508 dreg);
6509 if (mips_trap)
6510 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6511 AT, 0);
6512 else
6513 {
6514 expr1.X_add_number = 8;
6515 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
6516 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6517 0);
6518 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6519 "c", 6);
6520 }
6521 --mips_opts.noreorder;
6522 break;
6523
6524 case M_ROL:
6525 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6526 "d,v,t", AT, 0, treg);
6527 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6528 "d,t,s", AT, sreg, AT);
6529 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6530 "d,t,s", dreg, sreg, treg);
6531 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6532 "d,v,t", dreg, dreg, AT);
6533 break;
6534
6535 case M_ROL_I:
6536 if (imm_expr.X_op != O_constant)
6537 as_bad (_("rotate count too large"));
6538 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6539 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6540 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6541 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6542 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6543 dreg, dreg, AT);
6544 break;
6545
6546 case M_ROR:
6547 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6548 "d,v,t", AT, 0, treg);
6549 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6550 "d,t,s", AT, sreg, AT);
6551 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6552 "d,t,s", dreg, sreg, treg);
6553 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6554 "d,v,t", dreg, dreg, AT);
6555 break;
6556
6557 case M_ROR_I:
6558 if (imm_expr.X_op != O_constant)
6559 as_bad (_("rotate count too large"));
6560 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6561 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6562 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6563 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6564 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6565 dreg, dreg, AT);
6566 break;
6567
6568 case M_S_DOB:
6569 if (mips_arch == CPU_R4650)
6570 {
6571 as_bad (_("opcode not supported on this processor"));
6572 return;
6573 }
6574 assert (mips_opts.isa == ISA_MIPS1);
6575 /* Even on a big endian machine $fn comes before $fn+1. We have
6576 to adjust when storing to memory. */
6577 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6578 target_big_endian ? treg + 1 : treg,
6579 (int) BFD_RELOC_LO16, breg);
6580 offset_expr.X_add_number += 4;
6581 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6582 target_big_endian ? treg : treg + 1,
6583 (int) BFD_RELOC_LO16, breg);
6584 return;
6585
6586 case M_SEQ:
6587 if (sreg == 0)
6588 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6589 treg, (int) BFD_RELOC_LO16);
6590 else if (treg == 0)
6591 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6592 sreg, (int) BFD_RELOC_LO16);
6593 else
6594 {
6595 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6596 "d,v,t", dreg, sreg, treg);
6597 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6598 dreg, (int) BFD_RELOC_LO16);
6599 }
6600 return;
6601
6602 case M_SEQ_I:
6603 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6604 {
6605 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6606 sreg, (int) BFD_RELOC_LO16);
6607 return;
6608 }
6609 if (sreg == 0)
6610 {
6611 as_warn (_("Instruction %s: result is always false"),
6612 ip->insn_mo->name);
6613 move_register (&icnt, dreg, 0);
6614 return;
6615 }
6616 if (imm_expr.X_op == O_constant
6617 && imm_expr.X_add_number >= 0
6618 && imm_expr.X_add_number < 0x10000)
6619 {
6620 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6621 sreg, (int) BFD_RELOC_LO16);
6622 used_at = 0;
6623 }
6624 else if (imm_expr.X_op == O_constant
6625 && imm_expr.X_add_number > -0x8000
6626 && imm_expr.X_add_number < 0)
6627 {
6628 imm_expr.X_add_number = -imm_expr.X_add_number;
6629 macro_build ((char *) NULL, &icnt, &imm_expr,
6630 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6631 "t,r,j", dreg, sreg,
6632 (int) BFD_RELOC_LO16);
6633 used_at = 0;
6634 }
6635 else
6636 {
6637 load_register (&icnt, AT, &imm_expr, 0);
6638 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6639 "d,v,t", dreg, sreg, AT);
6640 used_at = 1;
6641 }
6642 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6643 (int) BFD_RELOC_LO16);
6644 if (used_at)
6645 break;
6646 return;
6647
6648 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6649 s = "slt";
6650 goto sge;
6651 case M_SGEU:
6652 s = "sltu";
6653 sge:
6654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6655 dreg, sreg, treg);
6656 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6657 (int) BFD_RELOC_LO16);
6658 return;
6659
6660 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6661 case M_SGEU_I:
6662 if (imm_expr.X_op == O_constant
6663 && imm_expr.X_add_number >= -0x8000
6664 && imm_expr.X_add_number < 0x8000)
6665 {
6666 macro_build ((char *) NULL, &icnt, &imm_expr,
6667 mask == M_SGE_I ? "slti" : "sltiu",
6668 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6669 used_at = 0;
6670 }
6671 else
6672 {
6673 load_register (&icnt, AT, &imm_expr, 0);
6674 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6675 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6676 AT);
6677 used_at = 1;
6678 }
6679 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6680 (int) BFD_RELOC_LO16);
6681 if (used_at)
6682 break;
6683 return;
6684
6685 case M_SGT: /* sreg > treg <==> treg < sreg */
6686 s = "slt";
6687 goto sgt;
6688 case M_SGTU:
6689 s = "sltu";
6690 sgt:
6691 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6692 dreg, treg, sreg);
6693 return;
6694
6695 case M_SGT_I: /* sreg > I <==> I < sreg */
6696 s = "slt";
6697 goto sgti;
6698 case M_SGTU_I:
6699 s = "sltu";
6700 sgti:
6701 load_register (&icnt, AT, &imm_expr, 0);
6702 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6703 dreg, AT, sreg);
6704 break;
6705
6706 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
6707 s = "slt";
6708 goto sle;
6709 case M_SLEU:
6710 s = "sltu";
6711 sle:
6712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6713 dreg, treg, sreg);
6714 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6715 (int) BFD_RELOC_LO16);
6716 return;
6717
6718 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
6719 s = "slt";
6720 goto slei;
6721 case M_SLEU_I:
6722 s = "sltu";
6723 slei:
6724 load_register (&icnt, AT, &imm_expr, 0);
6725 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6726 dreg, AT, sreg);
6727 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6728 (int) BFD_RELOC_LO16);
6729 break;
6730
6731 case M_SLT_I:
6732 if (imm_expr.X_op == O_constant
6733 && imm_expr.X_add_number >= -0x8000
6734 && imm_expr.X_add_number < 0x8000)
6735 {
6736 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6737 dreg, sreg, (int) BFD_RELOC_LO16);
6738 return;
6739 }
6740 load_register (&icnt, AT, &imm_expr, 0);
6741 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6742 dreg, sreg, AT);
6743 break;
6744
6745 case M_SLTU_I:
6746 if (imm_expr.X_op == O_constant
6747 && imm_expr.X_add_number >= -0x8000
6748 && imm_expr.X_add_number < 0x8000)
6749 {
6750 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6751 dreg, sreg, (int) BFD_RELOC_LO16);
6752 return;
6753 }
6754 load_register (&icnt, AT, &imm_expr, 0);
6755 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6756 "d,v,t", dreg, sreg, AT);
6757 break;
6758
6759 case M_SNE:
6760 if (sreg == 0)
6761 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6762 "d,v,t", dreg, 0, treg);
6763 else if (treg == 0)
6764 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6765 "d,v,t", dreg, 0, sreg);
6766 else
6767 {
6768 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6769 "d,v,t", dreg, sreg, treg);
6770 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6771 "d,v,t", dreg, 0, dreg);
6772 }
6773 return;
6774
6775 case M_SNE_I:
6776 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6777 {
6778 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6779 "d,v,t", dreg, 0, sreg);
6780 return;
6781 }
6782 if (sreg == 0)
6783 {
6784 as_warn (_("Instruction %s: result is always true"),
6785 ip->insn_mo->name);
6786 macro_build ((char *) NULL, &icnt, &expr1,
6787 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6788 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6789 return;
6790 }
6791 if (imm_expr.X_op == O_constant
6792 && imm_expr.X_add_number >= 0
6793 && imm_expr.X_add_number < 0x10000)
6794 {
6795 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6796 dreg, sreg, (int) BFD_RELOC_LO16);
6797 used_at = 0;
6798 }
6799 else if (imm_expr.X_op == O_constant
6800 && imm_expr.X_add_number > -0x8000
6801 && imm_expr.X_add_number < 0)
6802 {
6803 imm_expr.X_add_number = -imm_expr.X_add_number;
6804 macro_build ((char *) NULL, &icnt, &imm_expr,
6805 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
6806 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6807 used_at = 0;
6808 }
6809 else
6810 {
6811 load_register (&icnt, AT, &imm_expr, 0);
6812 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6813 "d,v,t", dreg, sreg, AT);
6814 used_at = 1;
6815 }
6816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6817 "d,v,t", dreg, 0, dreg);
6818 if (used_at)
6819 break;
6820 return;
6821
6822 case M_DSUB_I:
6823 dbl = 1;
6824 case M_SUB_I:
6825 if (imm_expr.X_op == O_constant
6826 && imm_expr.X_add_number > -0x8000
6827 && imm_expr.X_add_number <= 0x8000)
6828 {
6829 imm_expr.X_add_number = -imm_expr.X_add_number;
6830 macro_build ((char *) NULL, &icnt, &imm_expr,
6831 dbl ? "daddi" : "addi",
6832 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6833 return;
6834 }
6835 load_register (&icnt, AT, &imm_expr, dbl);
6836 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6837 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
6838 break;
6839
6840 case M_DSUBU_I:
6841 dbl = 1;
6842 case M_SUBU_I:
6843 if (imm_expr.X_op == O_constant
6844 && imm_expr.X_add_number > -0x8000
6845 && imm_expr.X_add_number <= 0x8000)
6846 {
6847 imm_expr.X_add_number = -imm_expr.X_add_number;
6848 macro_build ((char *) NULL, &icnt, &imm_expr,
6849 dbl ? "daddiu" : "addiu",
6850 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6851 return;
6852 }
6853 load_register (&icnt, AT, &imm_expr, dbl);
6854 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6855 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
6856 break;
6857
6858 case M_TEQ_I:
6859 s = "teq";
6860 goto trap;
6861 case M_TGE_I:
6862 s = "tge";
6863 goto trap;
6864 case M_TGEU_I:
6865 s = "tgeu";
6866 goto trap;
6867 case M_TLT_I:
6868 s = "tlt";
6869 goto trap;
6870 case M_TLTU_I:
6871 s = "tltu";
6872 goto trap;
6873 case M_TNE_I:
6874 s = "tne";
6875 trap:
6876 load_register (&icnt, AT, &imm_expr, 0);
6877 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
6878 AT);
6879 break;
6880
6881 case M_TRUNCWS:
6882 case M_TRUNCWD:
6883 assert (mips_opts.isa == ISA_MIPS1);
6884 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
6885 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
6886
6887 /*
6888 * Is the double cfc1 instruction a bug in the mips assembler;
6889 * or is there a reason for it?
6890 */
6891 mips_emit_delays (true);
6892 ++mips_opts.noreorder;
6893 mips_any_noreorder = 1;
6894 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
6895 treg, 31);
6896 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
6897 treg, 31);
6898 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6899 expr1.X_add_number = 3;
6900 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
6901 (int) BFD_RELOC_LO16);
6902 expr1.X_add_number = 2;
6903 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
6904 (int) BFD_RELOC_LO16);
6905 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
6906 AT, 31);
6907 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6908 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6909 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
6910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
6911 treg, 31);
6912 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6913 --mips_opts.noreorder;
6914 break;
6915
6916 case M_ULH:
6917 s = "lb";
6918 goto ulh;
6919 case M_ULHU:
6920 s = "lbu";
6921 ulh:
6922 if (offset_expr.X_add_number >= 0x7fff)
6923 as_bad (_("operand overflow"));
6924 /* avoid load delay */
6925 if (! target_big_endian)
6926 offset_expr.X_add_number += 1;
6927 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6928 (int) BFD_RELOC_LO16, breg);
6929 if (! target_big_endian)
6930 offset_expr.X_add_number -= 1;
6931 else
6932 offset_expr.X_add_number += 1;
6933 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
6934 (int) BFD_RELOC_LO16, breg);
6935 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6936 treg, treg, 8);
6937 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6938 treg, treg, AT);
6939 break;
6940
6941 case M_ULD:
6942 s = "ldl";
6943 s2 = "ldr";
6944 off = 7;
6945 goto ulw;
6946 case M_ULW:
6947 s = "lwl";
6948 s2 = "lwr";
6949 off = 3;
6950 ulw:
6951 if (offset_expr.X_add_number >= 0x8000 - off)
6952 as_bad (_("operand overflow"));
6953 if (! target_big_endian)
6954 offset_expr.X_add_number += off;
6955 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6956 (int) BFD_RELOC_LO16, breg);
6957 if (! target_big_endian)
6958 offset_expr.X_add_number -= off;
6959 else
6960 offset_expr.X_add_number += off;
6961 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
6962 (int) BFD_RELOC_LO16, breg);
6963 return;
6964
6965 case M_ULD_A:
6966 s = "ldl";
6967 s2 = "ldr";
6968 off = 7;
6969 goto ulwa;
6970 case M_ULW_A:
6971 s = "lwl";
6972 s2 = "lwr";
6973 off = 3;
6974 ulwa:
6975 used_at = 1;
6976 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
6977 if (breg != 0)
6978 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6979 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
6980 "d,v,t", AT, AT, breg);
6981 if (! target_big_endian)
6982 expr1.X_add_number = off;
6983 else
6984 expr1.X_add_number = 0;
6985 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
6986 (int) BFD_RELOC_LO16, AT);
6987 if (! target_big_endian)
6988 expr1.X_add_number = 0;
6989 else
6990 expr1.X_add_number = off;
6991 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
6992 (int) BFD_RELOC_LO16, AT);
6993 break;
6994
6995 case M_ULH_A:
6996 case M_ULHU_A:
6997 used_at = 1;
6998 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
6999 if (breg != 0)
7000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7001 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7002 "d,v,t", AT, AT, breg);
7003 if (target_big_endian)
7004 expr1.X_add_number = 0;
7005 macro_build ((char *) NULL, &icnt, &expr1,
7006 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7007 (int) BFD_RELOC_LO16, AT);
7008 if (target_big_endian)
7009 expr1.X_add_number = 1;
7010 else
7011 expr1.X_add_number = 0;
7012 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7013 (int) BFD_RELOC_LO16, AT);
7014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7015 treg, treg, 8);
7016 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7017 treg, treg, AT);
7018 break;
7019
7020 case M_USH:
7021 if (offset_expr.X_add_number >= 0x7fff)
7022 as_bad (_("operand overflow"));
7023 if (target_big_endian)
7024 offset_expr.X_add_number += 1;
7025 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7026 (int) BFD_RELOC_LO16, breg);
7027 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7028 AT, treg, 8);
7029 if (target_big_endian)
7030 offset_expr.X_add_number -= 1;
7031 else
7032 offset_expr.X_add_number += 1;
7033 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7034 (int) BFD_RELOC_LO16, breg);
7035 break;
7036
7037 case M_USD:
7038 s = "sdl";
7039 s2 = "sdr";
7040 off = 7;
7041 goto usw;
7042 case M_USW:
7043 s = "swl";
7044 s2 = "swr";
7045 off = 3;
7046 usw:
7047 if (offset_expr.X_add_number >= 0x8000 - off)
7048 as_bad (_("operand overflow"));
7049 if (! target_big_endian)
7050 offset_expr.X_add_number += off;
7051 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7052 (int) BFD_RELOC_LO16, breg);
7053 if (! target_big_endian)
7054 offset_expr.X_add_number -= off;
7055 else
7056 offset_expr.X_add_number += off;
7057 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7058 (int) BFD_RELOC_LO16, breg);
7059 return;
7060
7061 case M_USD_A:
7062 s = "sdl";
7063 s2 = "sdr";
7064 off = 7;
7065 goto uswa;
7066 case M_USW_A:
7067 s = "swl";
7068 s2 = "swr";
7069 off = 3;
7070 uswa:
7071 used_at = 1;
7072 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7073 if (breg != 0)
7074 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7075 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7076 "d,v,t", AT, AT, breg);
7077 if (! target_big_endian)
7078 expr1.X_add_number = off;
7079 else
7080 expr1.X_add_number = 0;
7081 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7082 (int) BFD_RELOC_LO16, AT);
7083 if (! target_big_endian)
7084 expr1.X_add_number = 0;
7085 else
7086 expr1.X_add_number = off;
7087 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7088 (int) BFD_RELOC_LO16, AT);
7089 break;
7090
7091 case M_USH_A:
7092 used_at = 1;
7093 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
7094 if (breg != 0)
7095 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7096 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
7097 "d,v,t", AT, AT, breg);
7098 if (! target_big_endian)
7099 expr1.X_add_number = 0;
7100 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7101 (int) BFD_RELOC_LO16, AT);
7102 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7103 treg, treg, 8);
7104 if (! target_big_endian)
7105 expr1.X_add_number = 1;
7106 else
7107 expr1.X_add_number = 0;
7108 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7109 (int) BFD_RELOC_LO16, AT);
7110 if (! target_big_endian)
7111 expr1.X_add_number = 0;
7112 else
7113 expr1.X_add_number = 1;
7114 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7115 (int) BFD_RELOC_LO16, AT);
7116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7117 treg, treg, 8);
7118 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7119 treg, treg, AT);
7120 break;
7121
7122 default:
7123 /* FIXME: Check if this is one of the itbl macros, since they
7124 are added dynamically. */
7125 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7126 break;
7127 }
7128 if (mips_opts.noat)
7129 as_warn (_("Macro used $at after \".set noat\""));
7130 }
7131
7132 /* Implement macros in mips16 mode. */
7133
7134 static void
7135 mips16_macro (ip)
7136 struct mips_cl_insn *ip;
7137 {
7138 int mask;
7139 int xreg, yreg, zreg, tmp;
7140 int icnt;
7141 expressionS expr1;
7142 int dbl;
7143 const char *s, *s2, *s3;
7144
7145 mask = ip->insn_mo->mask;
7146
7147 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7148 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7149 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7150
7151 icnt = 0;
7152
7153 expr1.X_op = O_constant;
7154 expr1.X_op_symbol = NULL;
7155 expr1.X_add_symbol = NULL;
7156 expr1.X_add_number = 1;
7157
7158 dbl = 0;
7159
7160 switch (mask)
7161 {
7162 default:
7163 internalError ();
7164
7165 case M_DDIV_3:
7166 dbl = 1;
7167 case M_DIV_3:
7168 s = "mflo";
7169 goto do_div3;
7170 case M_DREM_3:
7171 dbl = 1;
7172 case M_REM_3:
7173 s = "mfhi";
7174 do_div3:
7175 mips_emit_delays (true);
7176 ++mips_opts.noreorder;
7177 mips_any_noreorder = 1;
7178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7179 dbl ? "ddiv" : "div",
7180 "0,x,y", xreg, yreg);
7181 expr1.X_add_number = 2;
7182 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7183 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7184 7);
7185
7186 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7187 since that causes an overflow. We should do that as well,
7188 but I don't see how to do the comparisons without a temporary
7189 register. */
7190 --mips_opts.noreorder;
7191 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
7192 break;
7193
7194 case M_DIVU_3:
7195 s = "divu";
7196 s2 = "mflo";
7197 goto do_divu3;
7198 case M_REMU_3:
7199 s = "divu";
7200 s2 = "mfhi";
7201 goto do_divu3;
7202 case M_DDIVU_3:
7203 s = "ddivu";
7204 s2 = "mflo";
7205 goto do_divu3;
7206 case M_DREMU_3:
7207 s = "ddivu";
7208 s2 = "mfhi";
7209 do_divu3:
7210 mips_emit_delays (true);
7211 ++mips_opts.noreorder;
7212 mips_any_noreorder = 1;
7213 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7214 xreg, yreg);
7215 expr1.X_add_number = 2;
7216 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7217 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7218 "6", 7);
7219 --mips_opts.noreorder;
7220 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
7221 break;
7222
7223 case M_DMUL:
7224 dbl = 1;
7225 case M_MUL:
7226 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7227 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7228 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7229 zreg);
7230 return;
7231
7232 case M_DSUBU_I:
7233 dbl = 1;
7234 goto do_subu;
7235 case M_SUBU_I:
7236 do_subu:
7237 if (imm_expr.X_op != O_constant)
7238 as_bad (_("Unsupported large constant"));
7239 imm_expr.X_add_number = -imm_expr.X_add_number;
7240 macro_build ((char *) NULL, &icnt, &imm_expr,
7241 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
7242 break;
7243
7244 case M_SUBU_I_2:
7245 if (imm_expr.X_op != O_constant)
7246 as_bad (_("Unsupported large constant"));
7247 imm_expr.X_add_number = -imm_expr.X_add_number;
7248 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7249 "x,k", xreg);
7250 break;
7251
7252 case M_DSUBU_I_2:
7253 if (imm_expr.X_op != O_constant)
7254 as_bad (_("Unsupported large constant"));
7255 imm_expr.X_add_number = -imm_expr.X_add_number;
7256 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7257 "y,j", yreg);
7258 break;
7259
7260 case M_BEQ:
7261 s = "cmp";
7262 s2 = "bteqz";
7263 goto do_branch;
7264 case M_BNE:
7265 s = "cmp";
7266 s2 = "btnez";
7267 goto do_branch;
7268 case M_BLT:
7269 s = "slt";
7270 s2 = "btnez";
7271 goto do_branch;
7272 case M_BLTU:
7273 s = "sltu";
7274 s2 = "btnez";
7275 goto do_branch;
7276 case M_BLE:
7277 s = "slt";
7278 s2 = "bteqz";
7279 goto do_reverse_branch;
7280 case M_BLEU:
7281 s = "sltu";
7282 s2 = "bteqz";
7283 goto do_reverse_branch;
7284 case M_BGE:
7285 s = "slt";
7286 s2 = "bteqz";
7287 goto do_branch;
7288 case M_BGEU:
7289 s = "sltu";
7290 s2 = "bteqz";
7291 goto do_branch;
7292 case M_BGT:
7293 s = "slt";
7294 s2 = "btnez";
7295 goto do_reverse_branch;
7296 case M_BGTU:
7297 s = "sltu";
7298 s2 = "btnez";
7299
7300 do_reverse_branch:
7301 tmp = xreg;
7302 xreg = yreg;
7303 yreg = tmp;
7304
7305 do_branch:
7306 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7307 xreg, yreg);
7308 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7309 break;
7310
7311 case M_BEQ_I:
7312 s = "cmpi";
7313 s2 = "bteqz";
7314 s3 = "x,U";
7315 goto do_branch_i;
7316 case M_BNE_I:
7317 s = "cmpi";
7318 s2 = "btnez";
7319 s3 = "x,U";
7320 goto do_branch_i;
7321 case M_BLT_I:
7322 s = "slti";
7323 s2 = "btnez";
7324 s3 = "x,8";
7325 goto do_branch_i;
7326 case M_BLTU_I:
7327 s = "sltiu";
7328 s2 = "btnez";
7329 s3 = "x,8";
7330 goto do_branch_i;
7331 case M_BLE_I:
7332 s = "slti";
7333 s2 = "btnez";
7334 s3 = "x,8";
7335 goto do_addone_branch_i;
7336 case M_BLEU_I:
7337 s = "sltiu";
7338 s2 = "btnez";
7339 s3 = "x,8";
7340 goto do_addone_branch_i;
7341 case M_BGE_I:
7342 s = "slti";
7343 s2 = "bteqz";
7344 s3 = "x,8";
7345 goto do_branch_i;
7346 case M_BGEU_I:
7347 s = "sltiu";
7348 s2 = "bteqz";
7349 s3 = "x,8";
7350 goto do_branch_i;
7351 case M_BGT_I:
7352 s = "slti";
7353 s2 = "bteqz";
7354 s3 = "x,8";
7355 goto do_addone_branch_i;
7356 case M_BGTU_I:
7357 s = "sltiu";
7358 s2 = "bteqz";
7359 s3 = "x,8";
7360
7361 do_addone_branch_i:
7362 if (imm_expr.X_op != O_constant)
7363 as_bad (_("Unsupported large constant"));
7364 ++imm_expr.X_add_number;
7365
7366 do_branch_i:
7367 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7368 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7369 break;
7370
7371 case M_ABS:
7372 expr1.X_add_number = 0;
7373 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7374 if (xreg != yreg)
7375 move_register (&icnt, xreg, yreg);
7376 expr1.X_add_number = 2;
7377 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7379 "neg", "x,w", xreg, xreg);
7380 }
7381 }
7382
7383 /* For consistency checking, verify that all bits are specified either
7384 by the match/mask part of the instruction definition, or by the
7385 operand list. */
7386 static int
7387 validate_mips_insn (opc)
7388 const struct mips_opcode *opc;
7389 {
7390 const char *p = opc->args;
7391 char c;
7392 unsigned long used_bits = opc->mask;
7393
7394 if ((used_bits & opc->match) != opc->match)
7395 {
7396 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7397 opc->name, opc->args);
7398 return 0;
7399 }
7400 #define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7401 while (*p)
7402 switch (c = *p++)
7403 {
7404 case ',': break;
7405 case '(': break;
7406 case ')': break;
7407 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7408 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7409 case 'A': break;
7410 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
7411 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7412 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7413 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7414 case 'F': break;
7415 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7416 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
7417 case 'I': break;
7418 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
7419 case 'L': break;
7420 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7421 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7422 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7423 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7424 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7425 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7426 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7427 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7428 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7429 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7430 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7431 case 'f': break;
7432 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7433 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7434 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7435 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7436 case 'l': break;
7437 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7438 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7439 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7440 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7441 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7442 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7443 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7444 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7445 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7446 case 'x': break;
7447 case 'z': break;
7448 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
7449 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7450 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7451 default:
7452 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7453 c, opc->name, opc->args);
7454 return 0;
7455 }
7456 #undef USE_BITS
7457 if (used_bits != 0xffffffff)
7458 {
7459 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7460 ~used_bits & 0xffffffff, opc->name, opc->args);
7461 return 0;
7462 }
7463 return 1;
7464 }
7465
7466 /* This routine assembles an instruction into its binary format. As a
7467 side effect, it sets one of the global variables imm_reloc or
7468 offset_reloc to the type of relocation to do if one of the operands
7469 is an address expression. */
7470
7471 static void
7472 mips_ip (str, ip)
7473 char *str;
7474 struct mips_cl_insn *ip;
7475 {
7476 char *s;
7477 const char *args;
7478 char c = 0;
7479 struct mips_opcode *insn;
7480 char *argsStart;
7481 unsigned int regno;
7482 unsigned int lastregno = 0;
7483 char *s_reset;
7484 char save_c = 0;
7485 int full_opcode_match = 1;
7486
7487 insn_error = NULL;
7488
7489 /* If the instruction contains a '.', we first try to match an instruction
7490 including the '.'. Then we try again without the '.'. */
7491 insn = NULL;
7492 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
7493 continue;
7494
7495 /* If we stopped on whitespace, then replace the whitespace with null for
7496 the call to hash_find. Save the character we replaced just in case we
7497 have to re-parse the instruction. */
7498 if (ISSPACE (*s))
7499 {
7500 save_c = *s;
7501 *s++ = '\0';
7502 }
7503
7504 insn = (struct mips_opcode *) hash_find (op_hash, str);
7505
7506 /* If we didn't find the instruction in the opcode table, try again, but
7507 this time with just the instruction up to, but not including the
7508 first '.'. */
7509 if (insn == NULL)
7510 {
7511 /* Restore the character we overwrite above (if any). */
7512 if (save_c)
7513 *(--s) = save_c;
7514
7515 /* Scan up to the first '.' or whitespace. */
7516 for (s = str;
7517 *s != '\0' && *s != '.' && !ISSPACE (*s);
7518 ++s)
7519 continue;
7520
7521 /* If we did not find a '.', then we can quit now. */
7522 if (*s != '.')
7523 {
7524 insn_error = "unrecognized opcode";
7525 return;
7526 }
7527
7528 /* Lookup the instruction in the hash table. */
7529 *s++ = '\0';
7530 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7531 {
7532 insn_error = "unrecognized opcode";
7533 return;
7534 }
7535
7536 full_opcode_match = 0;
7537 }
7538
7539 argsStart = s;
7540 for (;;)
7541 {
7542 boolean ok;
7543
7544 assert (strcmp (insn->name, str) == 0);
7545
7546 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_arch))
7547 ok = true;
7548 else
7549 ok = false;
7550
7551 if (insn->pinfo != INSN_MACRO)
7552 {
7553 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
7554 ok = false;
7555 }
7556
7557 if (! ok)
7558 {
7559 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7560 && strcmp (insn->name, insn[1].name) == 0)
7561 {
7562 ++insn;
7563 continue;
7564 }
7565 else
7566 {
7567 if (!insn_error)
7568 {
7569 static char buf[100];
7570 sprintf (buf,
7571 _("opcode not supported on this processor: %s (%s)"),
7572 mips_cpu_to_str (mips_arch),
7573 mips_isa_to_str (mips_opts.isa));
7574
7575 insn_error = buf;
7576 }
7577 if (save_c)
7578 *(--s) = save_c;
7579 return;
7580 }
7581 }
7582
7583 ip->insn_mo = insn;
7584 ip->insn_opcode = insn->match;
7585 insn_error = NULL;
7586 for (args = insn->args;; ++args)
7587 {
7588 s += strspn (s, " \t");
7589 switch (*args)
7590 {
7591 case '\0': /* end of args */
7592 if (*s == '\0')
7593 return;
7594 break;
7595
7596 case ',':
7597 if (*s++ == *args)
7598 continue;
7599 s--;
7600 switch (*++args)
7601 {
7602 case 'r':
7603 case 'v':
7604 ip->insn_opcode |= lastregno << OP_SH_RS;
7605 continue;
7606
7607 case 'w':
7608 ip->insn_opcode |= lastregno << OP_SH_RT;
7609 continue;
7610
7611 case 'W':
7612 ip->insn_opcode |= lastregno << OP_SH_FT;
7613 continue;
7614
7615 case 'V':
7616 ip->insn_opcode |= lastregno << OP_SH_FS;
7617 continue;
7618 }
7619 break;
7620
7621 case '(':
7622 /* Handle optional base register.
7623 Either the base register is omitted or
7624 we must have a left paren. */
7625 /* This is dependent on the next operand specifier
7626 is a base register specification. */
7627 assert (args[1] == 'b' || args[1] == '5'
7628 || args[1] == '-' || args[1] == '4');
7629 if (*s == '\0')
7630 return;
7631
7632 case ')': /* these must match exactly */
7633 if (*s++ == *args)
7634 continue;
7635 break;
7636
7637 case '<': /* must be at least one digit */
7638 /*
7639 * According to the manual, if the shift amount is greater
7640 * than 31 or less than 0, then the shift amount should be
7641 * mod 32. In reality the mips assembler issues an error.
7642 * We issue a warning and mask out all but the low 5 bits.
7643 */
7644 my_getExpression (&imm_expr, s);
7645 check_absolute_expr (ip, &imm_expr);
7646 if ((unsigned long) imm_expr.X_add_number > 31)
7647 {
7648 as_warn (_("Improper shift amount (%ld)"),
7649 (long) imm_expr.X_add_number);
7650 imm_expr.X_add_number &= OP_MASK_SHAMT;
7651 }
7652 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
7653 imm_expr.X_op = O_absent;
7654 s = expr_end;
7655 continue;
7656
7657 case '>': /* shift amount minus 32 */
7658 my_getExpression (&imm_expr, s);
7659 check_absolute_expr (ip, &imm_expr);
7660 if ((unsigned long) imm_expr.X_add_number < 32
7661 || (unsigned long) imm_expr.X_add_number > 63)
7662 break;
7663 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
7664 imm_expr.X_op = O_absent;
7665 s = expr_end;
7666 continue;
7667
7668 case 'k': /* cache code */
7669 case 'h': /* prefx code */
7670 my_getExpression (&imm_expr, s);
7671 check_absolute_expr (ip, &imm_expr);
7672 if ((unsigned long) imm_expr.X_add_number > 31)
7673 {
7674 as_warn (_("Invalid value for `%s' (%lu)"),
7675 ip->insn_mo->name,
7676 (unsigned long) imm_expr.X_add_number);
7677 imm_expr.X_add_number &= 0x1f;
7678 }
7679 if (*args == 'k')
7680 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7681 else
7682 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7683 imm_expr.X_op = O_absent;
7684 s = expr_end;
7685 continue;
7686
7687 case 'c': /* break code */
7688 my_getExpression (&imm_expr, s);
7689 check_absolute_expr (ip, &imm_expr);
7690 if ((unsigned) imm_expr.X_add_number > 1023)
7691 {
7692 as_warn (_("Illegal break code (%ld)"),
7693 (long) imm_expr.X_add_number);
7694 imm_expr.X_add_number &= OP_MASK_CODE;
7695 }
7696 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
7697 imm_expr.X_op = O_absent;
7698 s = expr_end;
7699 continue;
7700
7701 case 'q': /* lower break code */
7702 my_getExpression (&imm_expr, s);
7703 check_absolute_expr (ip, &imm_expr);
7704 if ((unsigned) imm_expr.X_add_number > 1023)
7705 {
7706 as_warn (_("Illegal lower break code (%ld)"),
7707 (long) imm_expr.X_add_number);
7708 imm_expr.X_add_number &= OP_MASK_CODE2;
7709 }
7710 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
7711 imm_expr.X_op = O_absent;
7712 s = expr_end;
7713 continue;
7714
7715 case 'B': /* 20-bit syscall/break code. */
7716 my_getExpression (&imm_expr, s);
7717 check_absolute_expr (ip, &imm_expr);
7718 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
7719 as_warn (_("Illegal 20-bit code (%ld)"),
7720 (long) imm_expr.X_add_number);
7721 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
7722 imm_expr.X_op = O_absent;
7723 s = expr_end;
7724 continue;
7725
7726 case 'C': /* Coprocessor code */
7727 my_getExpression (&imm_expr, s);
7728 check_absolute_expr (ip, &imm_expr);
7729 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
7730 {
7731 as_warn (_("Coproccesor code > 25 bits (%ld)"),
7732 (long) imm_expr.X_add_number);
7733 imm_expr.X_add_number &= ((1<<25) - 1);
7734 }
7735 ip->insn_opcode |= imm_expr.X_add_number;
7736 imm_expr.X_op = O_absent;
7737 s = expr_end;
7738 continue;
7739
7740 case 'J': /* 19-bit wait code. */
7741 my_getExpression (&imm_expr, s);
7742 check_absolute_expr (ip, &imm_expr);
7743 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
7744 as_warn (_("Illegal 19-bit code (%ld)"),
7745 (long) imm_expr.X_add_number);
7746 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
7747 imm_expr.X_op = O_absent;
7748 s = expr_end;
7749 continue;
7750
7751 case 'P': /* Performance register */
7752 my_getExpression (&imm_expr, s);
7753 check_absolute_expr (ip, &imm_expr);
7754 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
7755 {
7756 as_warn (_("Invalid performance register (%ld)"),
7757 (long) imm_expr.X_add_number);
7758 imm_expr.X_add_number &= OP_MASK_PERFREG;
7759 }
7760 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
7761 imm_expr.X_op = O_absent;
7762 s = expr_end;
7763 continue;
7764
7765 case 'b': /* base register */
7766 case 'd': /* destination register */
7767 case 's': /* source register */
7768 case 't': /* target register */
7769 case 'r': /* both target and source */
7770 case 'v': /* both dest and source */
7771 case 'w': /* both dest and target */
7772 case 'E': /* coprocessor target register */
7773 case 'G': /* coprocessor destination register */
7774 case 'x': /* ignore register name */
7775 case 'z': /* must be zero register */
7776 case 'U': /* destination register (clo/clz). */
7777 s_reset = s;
7778 if (s[0] == '$')
7779 {
7780
7781 if (ISDIGIT (s[1]))
7782 {
7783 ++s;
7784 regno = 0;
7785 do
7786 {
7787 regno *= 10;
7788 regno += *s - '0';
7789 ++s;
7790 }
7791 while (ISDIGIT (*s));
7792 if (regno > 31)
7793 as_bad (_("Invalid register number (%d)"), regno);
7794 }
7795 else if (*args == 'E' || *args == 'G')
7796 goto notreg;
7797 else
7798 {
7799 if (s[1] == 'f' && s[2] == 'p')
7800 {
7801 s += 3;
7802 regno = FP;
7803 }
7804 else if (s[1] == 's' && s[2] == 'p')
7805 {
7806 s += 3;
7807 regno = SP;
7808 }
7809 else if (s[1] == 'g' && s[2] == 'p')
7810 {
7811 s += 3;
7812 regno = GP;
7813 }
7814 else if (s[1] == 'a' && s[2] == 't')
7815 {
7816 s += 3;
7817 regno = AT;
7818 }
7819 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7820 {
7821 s += 4;
7822 regno = KT0;
7823 }
7824 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7825 {
7826 s += 4;
7827 regno = KT1;
7828 }
7829 else if (itbl_have_entries)
7830 {
7831 char *p, *n;
7832 unsigned long r;
7833
7834 p = s + 1; /* advance past '$' */
7835 n = itbl_get_field (&p); /* n is name */
7836
7837 /* See if this is a register defined in an
7838 itbl entry. */
7839 if (itbl_get_reg_val (n, &r))
7840 {
7841 /* Get_field advances to the start of
7842 the next field, so we need to back
7843 rack to the end of the last field. */
7844 if (p)
7845 s = p - 1;
7846 else
7847 s = strchr (s, '\0');
7848 regno = r;
7849 }
7850 else
7851 goto notreg;
7852 }
7853 else
7854 goto notreg;
7855 }
7856 if (regno == AT
7857 && ! mips_opts.noat
7858 && *args != 'E'
7859 && *args != 'G')
7860 as_warn (_("Used $at without \".set noat\""));
7861 c = *args;
7862 if (*s == ' ')
7863 s++;
7864 if (args[1] != *s)
7865 {
7866 if (c == 'r' || c == 'v' || c == 'w')
7867 {
7868 regno = lastregno;
7869 s = s_reset;
7870 args++;
7871 }
7872 }
7873 /* 'z' only matches $0. */
7874 if (c == 'z' && regno != 0)
7875 break;
7876
7877 /* Now that we have assembled one operand, we use the args string
7878 * to figure out where it goes in the instruction. */
7879 switch (c)
7880 {
7881 case 'r':
7882 case 's':
7883 case 'v':
7884 case 'b':
7885 ip->insn_opcode |= regno << OP_SH_RS;
7886 break;
7887 case 'd':
7888 case 'G':
7889 ip->insn_opcode |= regno << OP_SH_RD;
7890 break;
7891 case 'U':
7892 ip->insn_opcode |= regno << OP_SH_RD;
7893 ip->insn_opcode |= regno << OP_SH_RT;
7894 break;
7895 case 'w':
7896 case 't':
7897 case 'E':
7898 ip->insn_opcode |= regno << OP_SH_RT;
7899 break;
7900 case 'x':
7901 /* This case exists because on the r3000 trunc
7902 expands into a macro which requires a gp
7903 register. On the r6000 or r4000 it is
7904 assembled into a single instruction which
7905 ignores the register. Thus the insn version
7906 is MIPS_ISA2 and uses 'x', and the macro
7907 version is MIPS_ISA1 and uses 't'. */
7908 break;
7909 case 'z':
7910 /* This case is for the div instruction, which
7911 acts differently if the destination argument
7912 is $0. This only matches $0, and is checked
7913 outside the switch. */
7914 break;
7915 case 'D':
7916 /* Itbl operand; not yet implemented. FIXME ?? */
7917 break;
7918 /* What about all other operands like 'i', which
7919 can be specified in the opcode table? */
7920 }
7921 lastregno = regno;
7922 continue;
7923 }
7924 notreg:
7925 switch (*args++)
7926 {
7927 case 'r':
7928 case 'v':
7929 ip->insn_opcode |= lastregno << OP_SH_RS;
7930 continue;
7931 case 'w':
7932 ip->insn_opcode |= lastregno << OP_SH_RT;
7933 continue;
7934 }
7935 break;
7936
7937 case 'D': /* floating point destination register */
7938 case 'S': /* floating point source register */
7939 case 'T': /* floating point target register */
7940 case 'R': /* floating point source register */
7941 case 'V':
7942 case 'W':
7943 s_reset = s;
7944 if (s[0] == '$' && s[1] == 'f'
7945 && ISDIGIT (s[2]))
7946 {
7947 s += 2;
7948 regno = 0;
7949 do
7950 {
7951 regno *= 10;
7952 regno += *s - '0';
7953 ++s;
7954 }
7955 while (ISDIGIT (*s));
7956
7957 if (regno > 31)
7958 as_bad (_("Invalid float register number (%d)"), regno);
7959
7960 if ((regno & 1) != 0
7961 && HAVE_32BIT_FPRS
7962 && ! (strcmp (str, "mtc1") == 0
7963 || strcmp (str, "mfc1") == 0
7964 || strcmp (str, "lwc1") == 0
7965 || strcmp (str, "swc1") == 0
7966 || strcmp (str, "l.s") == 0
7967 || strcmp (str, "s.s") == 0))
7968 as_warn (_("Float register should be even, was %d"),
7969 regno);
7970
7971 c = *args;
7972 if (*s == ' ')
7973 s++;
7974 if (args[1] != *s)
7975 {
7976 if (c == 'V' || c == 'W')
7977 {
7978 regno = lastregno;
7979 s = s_reset;
7980 args++;
7981 }
7982 }
7983 switch (c)
7984 {
7985 case 'D':
7986 ip->insn_opcode |= regno << OP_SH_FD;
7987 break;
7988 case 'V':
7989 case 'S':
7990 ip->insn_opcode |= regno << OP_SH_FS;
7991 break;
7992 case 'W':
7993 case 'T':
7994 ip->insn_opcode |= regno << OP_SH_FT;
7995 break;
7996 case 'R':
7997 ip->insn_opcode |= regno << OP_SH_FR;
7998 break;
7999 }
8000 lastregno = regno;
8001 continue;
8002 }
8003
8004 switch (*args++)
8005 {
8006 case 'V':
8007 ip->insn_opcode |= lastregno << OP_SH_FS;
8008 continue;
8009 case 'W':
8010 ip->insn_opcode |= lastregno << OP_SH_FT;
8011 continue;
8012 }
8013 break;
8014
8015 case 'I':
8016 my_getExpression (&imm_expr, s);
8017 if (imm_expr.X_op != O_big
8018 && imm_expr.X_op != O_constant)
8019 insn_error = _("absolute expression required");
8020 s = expr_end;
8021 continue;
8022
8023 case 'A':
8024 my_getExpression (&offset_expr, s);
8025 *imm_reloc = BFD_RELOC_32;
8026 s = expr_end;
8027 continue;
8028
8029 case 'F':
8030 case 'L':
8031 case 'f':
8032 case 'l':
8033 {
8034 int f64;
8035 int using_gprs;
8036 char *save_in;
8037 char *err;
8038 unsigned char temp[8];
8039 int len;
8040 unsigned int length;
8041 segT seg;
8042 subsegT subseg;
8043 char *p;
8044
8045 /* These only appear as the last operand in an
8046 instruction, and every instruction that accepts
8047 them in any variant accepts them in all variants.
8048 This means we don't have to worry about backing out
8049 any changes if the instruction does not match.
8050
8051 The difference between them is the size of the
8052 floating point constant and where it goes. For 'F'
8053 and 'L' the constant is 64 bits; for 'f' and 'l' it
8054 is 32 bits. Where the constant is placed is based
8055 on how the MIPS assembler does things:
8056 F -- .rdata
8057 L -- .lit8
8058 f -- immediate value
8059 l -- .lit4
8060
8061 The .lit4 and .lit8 sections are only used if
8062 permitted by the -G argument.
8063
8064 When generating embedded PIC code, we use the
8065 .lit8 section but not the .lit4 section (we can do
8066 .lit4 inline easily; we need to put .lit8
8067 somewhere in the data segment, and using .lit8
8068 permits the linker to eventually combine identical
8069 .lit8 entries).
8070
8071 The code below needs to know whether the target register
8072 is 32 or 64 bits wide. It relies on the fact 'f' and
8073 'F' are used with GPR-based instructions and 'l' and
8074 'L' are used with FPR-based instructions. */
8075
8076 f64 = *args == 'F' || *args == 'L';
8077 using_gprs = *args == 'F' || *args == 'f';
8078
8079 save_in = input_line_pointer;
8080 input_line_pointer = s;
8081 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8082 length = len;
8083 s = input_line_pointer;
8084 input_line_pointer = save_in;
8085 if (err != NULL && *err != '\0')
8086 {
8087 as_bad (_("Bad floating point constant: %s"), err);
8088 memset (temp, '\0', sizeof temp);
8089 length = f64 ? 8 : 4;
8090 }
8091
8092 assert (length == (unsigned) (f64 ? 8 : 4));
8093
8094 if (*args == 'f'
8095 || (*args == 'l'
8096 && (! USE_GLOBAL_POINTER_OPT
8097 || mips_pic == EMBEDDED_PIC
8098 || g_switch_value < 4
8099 || (temp[0] == 0 && temp[1] == 0)
8100 || (temp[2] == 0 && temp[3] == 0))))
8101 {
8102 imm_expr.X_op = O_constant;
8103 if (! target_big_endian)
8104 imm_expr.X_add_number = bfd_getl32 (temp);
8105 else
8106 imm_expr.X_add_number = bfd_getb32 (temp);
8107 }
8108 else if (length > 4
8109 && ! mips_disable_float_construction
8110 /* Constants can only be constructed in GPRs and
8111 copied to FPRs if the GPRs are at least as wide
8112 as the FPRs. Force the constant into memory if
8113 we are using 64-bit FPRs but the GPRs are only
8114 32 bits wide. */
8115 && (using_gprs
8116 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
8117 && ((temp[0] == 0 && temp[1] == 0)
8118 || (temp[2] == 0 && temp[3] == 0))
8119 && ((temp[4] == 0 && temp[5] == 0)
8120 || (temp[6] == 0 && temp[7] == 0)))
8121 {
8122 /* The value is simple enough to load with a couple of
8123 instructions. If using 32-bit registers, set
8124 imm_expr to the high order 32 bits and offset_expr to
8125 the low order 32 bits. Otherwise, set imm_expr to
8126 the entire 64 bit constant. */
8127 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
8128 {
8129 imm_expr.X_op = O_constant;
8130 offset_expr.X_op = O_constant;
8131 if (! target_big_endian)
8132 {
8133 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8134 offset_expr.X_add_number = bfd_getl32 (temp);
8135 }
8136 else
8137 {
8138 imm_expr.X_add_number = bfd_getb32 (temp);
8139 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8140 }
8141 if (offset_expr.X_add_number == 0)
8142 offset_expr.X_op = O_absent;
8143 }
8144 else if (sizeof (imm_expr.X_add_number) > 4)
8145 {
8146 imm_expr.X_op = O_constant;
8147 if (! target_big_endian)
8148 imm_expr.X_add_number = bfd_getl64 (temp);
8149 else
8150 imm_expr.X_add_number = bfd_getb64 (temp);
8151 }
8152 else
8153 {
8154 imm_expr.X_op = O_big;
8155 imm_expr.X_add_number = 4;
8156 if (! target_big_endian)
8157 {
8158 generic_bignum[0] = bfd_getl16 (temp);
8159 generic_bignum[1] = bfd_getl16 (temp + 2);
8160 generic_bignum[2] = bfd_getl16 (temp + 4);
8161 generic_bignum[3] = bfd_getl16 (temp + 6);
8162 }
8163 else
8164 {
8165 generic_bignum[0] = bfd_getb16 (temp + 6);
8166 generic_bignum[1] = bfd_getb16 (temp + 4);
8167 generic_bignum[2] = bfd_getb16 (temp + 2);
8168 generic_bignum[3] = bfd_getb16 (temp);
8169 }
8170 }
8171 }
8172 else
8173 {
8174 const char *newname;
8175 segT new_seg;
8176
8177 /* Switch to the right section. */
8178 seg = now_seg;
8179 subseg = now_subseg;
8180 switch (*args)
8181 {
8182 default: /* unused default case avoids warnings. */
8183 case 'L':
8184 newname = RDATA_SECTION_NAME;
8185 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8186 || mips_pic == EMBEDDED_PIC)
8187 newname = ".lit8";
8188 break;
8189 case 'F':
8190 if (mips_pic == EMBEDDED_PIC)
8191 newname = ".lit8";
8192 else
8193 newname = RDATA_SECTION_NAME;
8194 break;
8195 case 'l':
8196 assert (!USE_GLOBAL_POINTER_OPT
8197 || g_switch_value >= 4);
8198 newname = ".lit4";
8199 break;
8200 }
8201 new_seg = subseg_new (newname, (subsegT) 0);
8202 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8203 bfd_set_section_flags (stdoutput, new_seg,
8204 (SEC_ALLOC
8205 | SEC_LOAD
8206 | SEC_READONLY
8207 | SEC_DATA));
8208 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8209 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8210 && strcmp (TARGET_OS, "elf") != 0)
8211 record_alignment (new_seg, 4);
8212 else
8213 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8214 if (seg == now_seg)
8215 as_bad (_("Can't use floating point insn in this section"));
8216
8217 /* Set the argument to the current address in the
8218 section. */
8219 offset_expr.X_op = O_symbol;
8220 offset_expr.X_add_symbol =
8221 symbol_new ("L0\001", now_seg,
8222 (valueT) frag_now_fix (), frag_now);
8223 offset_expr.X_add_number = 0;
8224
8225 /* Put the floating point number into the section. */
8226 p = frag_more ((int) length);
8227 memcpy (p, temp, length);
8228
8229 /* Switch back to the original section. */
8230 subseg_set (seg, subseg);
8231 }
8232 }
8233 continue;
8234
8235 case 'i': /* 16 bit unsigned immediate */
8236 case 'j': /* 16 bit signed immediate */
8237 *imm_reloc = BFD_RELOC_LO16;
8238 c = my_getSmallExpression (&imm_expr, s);
8239 if (c != S_EX_NONE)
8240 {
8241 if (c != S_EX_LO)
8242 {
8243 if (imm_expr.X_op == O_constant)
8244 imm_expr.X_add_number =
8245 (imm_expr.X_add_number >> 16) & 0xffff;
8246 #ifdef OBJ_ELF
8247 else if (c == S_EX_HIGHEST)
8248 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8249 else if (c == S_EX_HIGHER)
8250 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
8251 else if (c == S_EX_GP_REL)
8252 {
8253 /* This occurs in NewABI only. */
8254 c = my_getSmallExpression (&imm_expr, s);
8255 if (c != S_EX_NEG)
8256 as_bad (_("bad composition of relocations"));
8257 else
8258 {
8259 c = my_getSmallExpression (&imm_expr, s);
8260 if (c != S_EX_LO)
8261 as_bad (_("bad composition of relocations"));
8262 else
8263 {
8264 imm_reloc[0] = BFD_RELOC_GPREL16;
8265 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8266 imm_reloc[2] = BFD_RELOC_LO16;
8267 }
8268 }
8269 }
8270 #endif
8271 else if (c == S_EX_HI)
8272 {
8273 *imm_reloc = BFD_RELOC_HI16_S;
8274 imm_unmatched_hi = true;
8275 }
8276 else
8277 *imm_reloc = BFD_RELOC_HI16;
8278 }
8279 else if (imm_expr.X_op == O_constant)
8280 imm_expr.X_add_number &= 0xffff;
8281 }
8282 if (*args == 'i')
8283 {
8284 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8285 || ((imm_expr.X_add_number < 0
8286 || imm_expr.X_add_number >= 0x10000)
8287 && imm_expr.X_op == O_constant))
8288 {
8289 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8290 !strcmp (insn->name, insn[1].name))
8291 break;
8292 if (imm_expr.X_op == O_constant
8293 || imm_expr.X_op == O_big)
8294 as_bad (_("16 bit expression not in range 0..65535"));
8295 }
8296 }
8297 else
8298 {
8299 int more;
8300 offsetT max;
8301
8302 /* The upper bound should be 0x8000, but
8303 unfortunately the MIPS assembler accepts numbers
8304 from 0x8000 to 0xffff and sign extends them, and
8305 we want to be compatible. We only permit this
8306 extended range for an instruction which does not
8307 provide any further alternates, since those
8308 alternates may handle other cases. People should
8309 use the numbers they mean, rather than relying on
8310 a mysterious sign extension. */
8311 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8312 strcmp (insn->name, insn[1].name) == 0);
8313 if (more)
8314 max = 0x8000;
8315 else
8316 max = 0x10000;
8317 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
8318 || ((imm_expr.X_add_number < -0x8000
8319 || imm_expr.X_add_number >= max)
8320 && imm_expr.X_op == O_constant)
8321 || (more
8322 && imm_expr.X_add_number < 0
8323 && HAVE_64BIT_GPRS
8324 && imm_expr.X_unsigned
8325 && sizeof (imm_expr.X_add_number) <= 4))
8326 {
8327 if (more)
8328 break;
8329 if (imm_expr.X_op == O_constant
8330 || imm_expr.X_op == O_big)
8331 as_bad (_("16 bit expression not in range -32768..32767"));
8332 }
8333 }
8334 s = expr_end;
8335 continue;
8336
8337 case 'o': /* 16 bit offset */
8338 c = my_getSmallExpression (&offset_expr, s);
8339
8340 /* If this value won't fit into a 16 bit offset, then go
8341 find a macro that will generate the 32 bit offset
8342 code pattern. */
8343 if (c == S_EX_NONE
8344 && (offset_expr.X_op != O_constant
8345 || offset_expr.X_add_number >= 0x8000
8346 || offset_expr.X_add_number < -0x8000))
8347 break;
8348
8349 if (c == S_EX_HI)
8350 {
8351 if (offset_expr.X_op != O_constant)
8352 break;
8353 offset_expr.X_add_number =
8354 (offset_expr.X_add_number >> 16) & 0xffff;
8355 }
8356 *offset_reloc = BFD_RELOC_LO16;
8357 s = expr_end;
8358 continue;
8359
8360 case 'p': /* pc relative offset */
8361 if (mips_pic == EMBEDDED_PIC)
8362 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8363 else
8364 *offset_reloc = BFD_RELOC_16_PCREL;
8365 my_getExpression (&offset_expr, s);
8366 s = expr_end;
8367 continue;
8368
8369 case 'u': /* upper 16 bits */
8370 c = my_getSmallExpression (&imm_expr, s);
8371 *imm_reloc = BFD_RELOC_LO16;
8372 if (c != S_EX_NONE)
8373 {
8374 if (c != S_EX_LO)
8375 {
8376 if (imm_expr.X_op == O_constant)
8377 imm_expr.X_add_number =
8378 (imm_expr.X_add_number >> 16) & 0xffff;
8379 else if (c == S_EX_HI)
8380 {
8381 *imm_reloc = BFD_RELOC_HI16_S;
8382 imm_unmatched_hi = true;
8383 }
8384 #ifdef OBJ_ELF
8385 else if (c == S_EX_HIGHEST)
8386 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
8387 else if (c == S_EX_GP_REL)
8388 {
8389 /* This occurs in NewABI only. */
8390 c = my_getSmallExpression (&imm_expr, s);
8391 if (c != S_EX_NEG)
8392 as_bad (_("bad composition of relocations"));
8393 else
8394 {
8395 c = my_getSmallExpression (&imm_expr, s);
8396 if (c != S_EX_HI)
8397 as_bad (_("bad composition of relocations"));
8398 else
8399 {
8400 imm_reloc[0] = BFD_RELOC_GPREL16;
8401 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8402 imm_reloc[2] = BFD_RELOC_HI16_S;
8403 }
8404 }
8405 }
8406 #endif
8407 else
8408 *imm_reloc = BFD_RELOC_HI16;
8409 }
8410 else if (imm_expr.X_op == O_constant)
8411 imm_expr.X_add_number &= 0xffff;
8412 }
8413 if (imm_expr.X_op == O_constant
8414 && (imm_expr.X_add_number < 0
8415 || imm_expr.X_add_number >= 0x10000))
8416 as_bad (_("lui expression not in range 0..65535"));
8417 s = expr_end;
8418 continue;
8419
8420 case 'a': /* 26 bit address */
8421 my_getExpression (&offset_expr, s);
8422 s = expr_end;
8423 *offset_reloc = BFD_RELOC_MIPS_JMP;
8424 continue;
8425
8426 case 'N': /* 3 bit branch condition code */
8427 case 'M': /* 3 bit compare condition code */
8428 if (strncmp (s, "$fcc", 4) != 0)
8429 break;
8430 s += 4;
8431 regno = 0;
8432 do
8433 {
8434 regno *= 10;
8435 regno += *s - '0';
8436 ++s;
8437 }
8438 while (ISDIGIT (*s));
8439 if (regno > 7)
8440 as_bad (_("invalid condition code register $fcc%d"), regno);
8441 if (*args == 'N')
8442 ip->insn_opcode |= regno << OP_SH_BCC;
8443 else
8444 ip->insn_opcode |= regno << OP_SH_CCC;
8445 continue;
8446
8447 case 'H':
8448 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8449 s += 2;
8450 if (ISDIGIT (*s))
8451 {
8452 c = 0;
8453 do
8454 {
8455 c *= 10;
8456 c += *s - '0';
8457 ++s;
8458 }
8459 while (ISDIGIT (*s));
8460 }
8461 else
8462 c = 8; /* Invalid sel value. */
8463
8464 if (c > 7)
8465 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8466 ip->insn_opcode |= c;
8467 continue;
8468
8469 default:
8470 as_bad (_("bad char = '%c'\n"), *args);
8471 internalError ();
8472 }
8473 break;
8474 }
8475 /* Args don't match. */
8476 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8477 !strcmp (insn->name, insn[1].name))
8478 {
8479 ++insn;
8480 s = argsStart;
8481 insn_error = _("illegal operands");
8482 continue;
8483 }
8484 if (save_c)
8485 *(--s) = save_c;
8486 insn_error = _("illegal operands");
8487 return;
8488 }
8489 }
8490
8491 /* This routine assembles an instruction into its binary format when
8492 assembling for the mips16. As a side effect, it sets one of the
8493 global variables imm_reloc or offset_reloc to the type of
8494 relocation to do if one of the operands is an address expression.
8495 It also sets mips16_small and mips16_ext if the user explicitly
8496 requested a small or extended instruction. */
8497
8498 static void
8499 mips16_ip (str, ip)
8500 char *str;
8501 struct mips_cl_insn *ip;
8502 {
8503 char *s;
8504 const char *args;
8505 struct mips_opcode *insn;
8506 char *argsstart;
8507 unsigned int regno;
8508 unsigned int lastregno = 0;
8509 char *s_reset;
8510
8511 insn_error = NULL;
8512
8513 mips16_small = false;
8514 mips16_ext = false;
8515
8516 for (s = str; ISLOWER (*s); ++s)
8517 ;
8518 switch (*s)
8519 {
8520 case '\0':
8521 break;
8522
8523 case ' ':
8524 *s++ = '\0';
8525 break;
8526
8527 case '.':
8528 if (s[1] == 't' && s[2] == ' ')
8529 {
8530 *s = '\0';
8531 mips16_small = true;
8532 s += 3;
8533 break;
8534 }
8535 else if (s[1] == 'e' && s[2] == ' ')
8536 {
8537 *s = '\0';
8538 mips16_ext = true;
8539 s += 3;
8540 break;
8541 }
8542 /* Fall through. */
8543 default:
8544 insn_error = _("unknown opcode");
8545 return;
8546 }
8547
8548 if (mips_opts.noautoextend && ! mips16_ext)
8549 mips16_small = true;
8550
8551 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8552 {
8553 insn_error = _("unrecognized opcode");
8554 return;
8555 }
8556
8557 argsstart = s;
8558 for (;;)
8559 {
8560 assert (strcmp (insn->name, str) == 0);
8561
8562 ip->insn_mo = insn;
8563 ip->insn_opcode = insn->match;
8564 ip->use_extend = false;
8565 imm_expr.X_op = O_absent;
8566 imm_reloc[0] = BFD_RELOC_UNUSED;
8567 imm_reloc[1] = BFD_RELOC_UNUSED;
8568 imm_reloc[2] = BFD_RELOC_UNUSED;
8569 offset_expr.X_op = O_absent;
8570 offset_reloc[0] = BFD_RELOC_UNUSED;
8571 offset_reloc[1] = BFD_RELOC_UNUSED;
8572 offset_reloc[2] = BFD_RELOC_UNUSED;
8573 for (args = insn->args; 1; ++args)
8574 {
8575 int c;
8576
8577 if (*s == ' ')
8578 ++s;
8579
8580 /* In this switch statement we call break if we did not find
8581 a match, continue if we did find a match, or return if we
8582 are done. */
8583
8584 c = *args;
8585 switch (c)
8586 {
8587 case '\0':
8588 if (*s == '\0')
8589 {
8590 /* Stuff the immediate value in now, if we can. */
8591 if (imm_expr.X_op == O_constant
8592 && *imm_reloc > BFD_RELOC_UNUSED
8593 && insn->pinfo != INSN_MACRO)
8594 {
8595 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
8596 imm_expr.X_add_number, true, mips16_small,
8597 mips16_ext, &ip->insn_opcode,
8598 &ip->use_extend, &ip->extend);
8599 imm_expr.X_op = O_absent;
8600 *imm_reloc = BFD_RELOC_UNUSED;
8601 }
8602
8603 return;
8604 }
8605 break;
8606
8607 case ',':
8608 if (*s++ == c)
8609 continue;
8610 s--;
8611 switch (*++args)
8612 {
8613 case 'v':
8614 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8615 continue;
8616 case 'w':
8617 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8618 continue;
8619 }
8620 break;
8621
8622 case '(':
8623 case ')':
8624 if (*s++ == c)
8625 continue;
8626 break;
8627
8628 case 'v':
8629 case 'w':
8630 if (s[0] != '$')
8631 {
8632 if (c == 'v')
8633 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8634 else
8635 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8636 ++args;
8637 continue;
8638 }
8639 /* Fall through. */
8640 case 'x':
8641 case 'y':
8642 case 'z':
8643 case 'Z':
8644 case '0':
8645 case 'S':
8646 case 'R':
8647 case 'X':
8648 case 'Y':
8649 if (s[0] != '$')
8650 break;
8651 s_reset = s;
8652 if (ISDIGIT (s[1]))
8653 {
8654 ++s;
8655 regno = 0;
8656 do
8657 {
8658 regno *= 10;
8659 regno += *s - '0';
8660 ++s;
8661 }
8662 while (ISDIGIT (*s));
8663 if (regno > 31)
8664 {
8665 as_bad (_("invalid register number (%d)"), regno);
8666 regno = 2;
8667 }
8668 }
8669 else
8670 {
8671 if (s[1] == 'f' && s[2] == 'p')
8672 {
8673 s += 3;
8674 regno = FP;
8675 }
8676 else if (s[1] == 's' && s[2] == 'p')
8677 {
8678 s += 3;
8679 regno = SP;
8680 }
8681 else if (s[1] == 'g' && s[2] == 'p')
8682 {
8683 s += 3;
8684 regno = GP;
8685 }
8686 else if (s[1] == 'a' && s[2] == 't')
8687 {
8688 s += 3;
8689 regno = AT;
8690 }
8691 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8692 {
8693 s += 4;
8694 regno = KT0;
8695 }
8696 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8697 {
8698 s += 4;
8699 regno = KT1;
8700 }
8701 else
8702 break;
8703 }
8704
8705 if (*s == ' ')
8706 ++s;
8707 if (args[1] != *s)
8708 {
8709 if (c == 'v' || c == 'w')
8710 {
8711 regno = mips16_to_32_reg_map[lastregno];
8712 s = s_reset;
8713 args++;
8714 }
8715 }
8716
8717 switch (c)
8718 {
8719 case 'x':
8720 case 'y':
8721 case 'z':
8722 case 'v':
8723 case 'w':
8724 case 'Z':
8725 regno = mips32_to_16_reg_map[regno];
8726 break;
8727
8728 case '0':
8729 if (regno != 0)
8730 regno = ILLEGAL_REG;
8731 break;
8732
8733 case 'S':
8734 if (regno != SP)
8735 regno = ILLEGAL_REG;
8736 break;
8737
8738 case 'R':
8739 if (regno != RA)
8740 regno = ILLEGAL_REG;
8741 break;
8742
8743 case 'X':
8744 case 'Y':
8745 if (regno == AT && ! mips_opts.noat)
8746 as_warn (_("used $at without \".set noat\""));
8747 break;
8748
8749 default:
8750 internalError ();
8751 }
8752
8753 if (regno == ILLEGAL_REG)
8754 break;
8755
8756 switch (c)
8757 {
8758 case 'x':
8759 case 'v':
8760 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8761 break;
8762 case 'y':
8763 case 'w':
8764 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8765 break;
8766 case 'z':
8767 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8768 break;
8769 case 'Z':
8770 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8771 case '0':
8772 case 'S':
8773 case 'R':
8774 break;
8775 case 'X':
8776 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8777 break;
8778 case 'Y':
8779 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8780 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8781 break;
8782 default:
8783 internalError ();
8784 }
8785
8786 lastregno = regno;
8787 continue;
8788
8789 case 'P':
8790 if (strncmp (s, "$pc", 3) == 0)
8791 {
8792 s += 3;
8793 continue;
8794 }
8795 break;
8796
8797 case '<':
8798 case '>':
8799 case '[':
8800 case ']':
8801 case '4':
8802 case '5':
8803 case 'H':
8804 case 'W':
8805 case 'D':
8806 case 'j':
8807 case '8':
8808 case 'V':
8809 case 'C':
8810 case 'U':
8811 case 'k':
8812 case 'K':
8813 if (s[0] == '%'
8814 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8815 {
8816 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8817 and generate the appropriate reloc. If the text
8818 inside %gprel is not a symbol name with an
8819 optional offset, then we generate a normal reloc
8820 and will probably fail later. */
8821 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8822 if (imm_expr.X_op == O_symbol)
8823 {
8824 mips16_ext = true;
8825 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
8826 s = expr_end;
8827 ip->use_extend = true;
8828 ip->extend = 0;
8829 continue;
8830 }
8831 }
8832 else
8833 {
8834 /* Just pick up a normal expression. */
8835 my_getExpression (&imm_expr, s);
8836 }
8837
8838 if (imm_expr.X_op == O_register)
8839 {
8840 /* What we thought was an expression turned out to
8841 be a register. */
8842
8843 if (s[0] == '(' && args[1] == '(')
8844 {
8845 /* It looks like the expression was omitted
8846 before a register indirection, which means
8847 that the expression is implicitly zero. We
8848 still set up imm_expr, so that we handle
8849 explicit extensions correctly. */
8850 imm_expr.X_op = O_constant;
8851 imm_expr.X_add_number = 0;
8852 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8853 continue;
8854 }
8855
8856 break;
8857 }
8858
8859 /* We need to relax this instruction. */
8860 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
8861 s = expr_end;
8862 continue;
8863
8864 case 'p':
8865 case 'q':
8866 case 'A':
8867 case 'B':
8868 case 'E':
8869 /* We use offset_reloc rather than imm_reloc for the PC
8870 relative operands. This lets macros with both
8871 immediate and address operands work correctly. */
8872 my_getExpression (&offset_expr, s);
8873
8874 if (offset_expr.X_op == O_register)
8875 break;
8876
8877 /* We need to relax this instruction. */
8878 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8879 s = expr_end;
8880 continue;
8881
8882 case '6': /* break code */
8883 my_getExpression (&imm_expr, s);
8884 check_absolute_expr (ip, &imm_expr);
8885 if ((unsigned long) imm_expr.X_add_number > 63)
8886 {
8887 as_warn (_("Invalid value for `%s' (%lu)"),
8888 ip->insn_mo->name,
8889 (unsigned long) imm_expr.X_add_number);
8890 imm_expr.X_add_number &= 0x3f;
8891 }
8892 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
8893 imm_expr.X_op = O_absent;
8894 s = expr_end;
8895 continue;
8896
8897 case 'a': /* 26 bit address */
8898 my_getExpression (&offset_expr, s);
8899 s = expr_end;
8900 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8901 ip->insn_opcode <<= 16;
8902 continue;
8903
8904 case 'l': /* register list for entry macro */
8905 case 'L': /* register list for exit macro */
8906 {
8907 int mask;
8908
8909 if (c == 'l')
8910 mask = 0;
8911 else
8912 mask = 7 << 3;
8913 while (*s != '\0')
8914 {
8915 int freg, reg1, reg2;
8916
8917 while (*s == ' ' || *s == ',')
8918 ++s;
8919 if (*s != '$')
8920 {
8921 as_bad (_("can't parse register list"));
8922 break;
8923 }
8924 ++s;
8925 if (*s != 'f')
8926 freg = 0;
8927 else
8928 {
8929 freg = 1;
8930 ++s;
8931 }
8932 reg1 = 0;
8933 while (ISDIGIT (*s))
8934 {
8935 reg1 *= 10;
8936 reg1 += *s - '0';
8937 ++s;
8938 }
8939 if (*s == ' ')
8940 ++s;
8941 if (*s != '-')
8942 reg2 = reg1;
8943 else
8944 {
8945 ++s;
8946 if (*s != '$')
8947 break;
8948 ++s;
8949 if (freg)
8950 {
8951 if (*s == 'f')
8952 ++s;
8953 else
8954 {
8955 as_bad (_("invalid register list"));
8956 break;
8957 }
8958 }
8959 reg2 = 0;
8960 while (ISDIGIT (*s))
8961 {
8962 reg2 *= 10;
8963 reg2 += *s - '0';
8964 ++s;
8965 }
8966 }
8967 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
8968 {
8969 mask &= ~ (7 << 3);
8970 mask |= 5 << 3;
8971 }
8972 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
8973 {
8974 mask &= ~ (7 << 3);
8975 mask |= 6 << 3;
8976 }
8977 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
8978 mask |= (reg2 - 3) << 3;
8979 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
8980 mask |= (reg2 - 15) << 1;
8981 else if (reg1 == 31 && reg2 == 31)
8982 mask |= 1;
8983 else
8984 {
8985 as_bad (_("invalid register list"));
8986 break;
8987 }
8988 }
8989 /* The mask is filled in in the opcode table for the
8990 benefit of the disassembler. We remove it before
8991 applying the actual mask. */
8992 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
8993 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
8994 }
8995 continue;
8996
8997 case 'e': /* extend code */
8998 my_getExpression (&imm_expr, s);
8999 check_absolute_expr (ip, &imm_expr);
9000 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9001 {
9002 as_warn (_("Invalid value for `%s' (%lu)"),
9003 ip->insn_mo->name,
9004 (unsigned long) imm_expr.X_add_number);
9005 imm_expr.X_add_number &= 0x7ff;
9006 }
9007 ip->insn_opcode |= imm_expr.X_add_number;
9008 imm_expr.X_op = O_absent;
9009 s = expr_end;
9010 continue;
9011
9012 default:
9013 internalError ();
9014 }
9015 break;
9016 }
9017
9018 /* Args don't match. */
9019 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9020 strcmp (insn->name, insn[1].name) == 0)
9021 {
9022 ++insn;
9023 s = argsstart;
9024 continue;
9025 }
9026
9027 insn_error = _("illegal operands");
9028
9029 return;
9030 }
9031 }
9032
9033 /* This structure holds information we know about a mips16 immediate
9034 argument type. */
9035
9036 struct mips16_immed_operand
9037 {
9038 /* The type code used in the argument string in the opcode table. */
9039 int type;
9040 /* The number of bits in the short form of the opcode. */
9041 int nbits;
9042 /* The number of bits in the extended form of the opcode. */
9043 int extbits;
9044 /* The amount by which the short form is shifted when it is used;
9045 for example, the sw instruction has a shift count of 2. */
9046 int shift;
9047 /* The amount by which the short form is shifted when it is stored
9048 into the instruction code. */
9049 int op_shift;
9050 /* Non-zero if the short form is unsigned. */
9051 int unsp;
9052 /* Non-zero if the extended form is unsigned. */
9053 int extu;
9054 /* Non-zero if the value is PC relative. */
9055 int pcrel;
9056 };
9057
9058 /* The mips16 immediate operand types. */
9059
9060 static const struct mips16_immed_operand mips16_immed_operands[] =
9061 {
9062 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9063 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9064 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9065 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9066 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9067 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9068 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9069 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9070 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9071 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9072 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9073 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9074 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9075 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9076 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9077 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9078 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9079 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9080 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9081 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9082 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9083 };
9084
9085 #define MIPS16_NUM_IMMED \
9086 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9087
9088 /* Handle a mips16 instruction with an immediate value. This or's the
9089 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9090 whether an extended value is needed; if one is needed, it sets
9091 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9092 If SMALL is true, an unextended opcode was explicitly requested.
9093 If EXT is true, an extended opcode was explicitly requested. If
9094 WARN is true, warn if EXT does not match reality. */
9095
9096 static void
9097 mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9098 extend)
9099 char *file;
9100 unsigned int line;
9101 int type;
9102 offsetT val;
9103 boolean warn;
9104 boolean small;
9105 boolean ext;
9106 unsigned long *insn;
9107 boolean *use_extend;
9108 unsigned short *extend;
9109 {
9110 register const struct mips16_immed_operand *op;
9111 int mintiny, maxtiny;
9112 boolean needext;
9113
9114 op = mips16_immed_operands;
9115 while (op->type != type)
9116 {
9117 ++op;
9118 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9119 }
9120
9121 if (op->unsp)
9122 {
9123 if (type == '<' || type == '>' || type == '[' || type == ']')
9124 {
9125 mintiny = 1;
9126 maxtiny = 1 << op->nbits;
9127 }
9128 else
9129 {
9130 mintiny = 0;
9131 maxtiny = (1 << op->nbits) - 1;
9132 }
9133 }
9134 else
9135 {
9136 mintiny = - (1 << (op->nbits - 1));
9137 maxtiny = (1 << (op->nbits - 1)) - 1;
9138 }
9139
9140 /* Branch offsets have an implicit 0 in the lowest bit. */
9141 if (type == 'p' || type == 'q')
9142 val /= 2;
9143
9144 if ((val & ((1 << op->shift) - 1)) != 0
9145 || val < (mintiny << op->shift)
9146 || val > (maxtiny << op->shift))
9147 needext = true;
9148 else
9149 needext = false;
9150
9151 if (warn && ext && ! needext)
9152 as_warn_where (file, line,
9153 _("extended operand requested but not required"));
9154 if (small && needext)
9155 as_bad_where (file, line, _("invalid unextended operand value"));
9156
9157 if (small || (! ext && ! needext))
9158 {
9159 int insnval;
9160
9161 *use_extend = false;
9162 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9163 insnval <<= op->op_shift;
9164 *insn |= insnval;
9165 }
9166 else
9167 {
9168 long minext, maxext;
9169 int extval;
9170
9171 if (op->extu)
9172 {
9173 minext = 0;
9174 maxext = (1 << op->extbits) - 1;
9175 }
9176 else
9177 {
9178 minext = - (1 << (op->extbits - 1));
9179 maxext = (1 << (op->extbits - 1)) - 1;
9180 }
9181 if (val < minext || val > maxext)
9182 as_bad_where (file, line,
9183 _("operand value out of range for instruction"));
9184
9185 *use_extend = true;
9186 if (op->extbits == 16)
9187 {
9188 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9189 val &= 0x1f;
9190 }
9191 else if (op->extbits == 15)
9192 {
9193 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9194 val &= 0xf;
9195 }
9196 else
9197 {
9198 extval = ((val & 0x1f) << 6) | (val & 0x20);
9199 val = 0;
9200 }
9201
9202 *extend = (unsigned short) extval;
9203 *insn |= val;
9204 }
9205 }
9206 \f
9207 static struct percent_op_match
9208 {
9209 const char *str;
9210 const enum small_ex_type type;
9211 } percent_op[] =
9212 {
9213 #ifdef OBJ_ELF
9214 {"%half", S_EX_HALF},
9215 #endif
9216 {"%hi", S_EX_HI},
9217 {"%lo", S_EX_LO},
9218 #ifdef OBJ_ELF
9219 {"%gp_rel", S_EX_GP_REL},
9220 {"%got", S_EX_GOT},
9221 {"%call16", S_EX_CALL16},
9222 {"%got_disp", S_EX_GOT_DISP},
9223 {"%got_page", S_EX_GOT_PAGE},
9224 {"%got_ofst", S_EX_GOT_OFST},
9225 {"%got_hi", S_EX_GOT_HI},
9226 {"%got_lo", S_EX_GOT_LO},
9227 {"%neg", S_EX_NEG},
9228 {"%higher", S_EX_HIGHER},
9229 {"%highest", S_EX_HIGHEST},
9230 {"%call_hi", S_EX_CALL_HI},
9231 {"%call_lo", S_EX_CALL_LO}
9232 #endif
9233 };
9234
9235 /* Parse small expression input. STR gets adjusted to eat up whitespace.
9236 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9237 can be nested, this is handled by blanking the innermost, parsing the
9238 rest by subsequent calls. */
9239
9240 static int
9241 my_getSmallParser (str, len, nestlevel)
9242 char **str;
9243 unsigned int *len;
9244 int *nestlevel;
9245 {
9246 int type = S_EX_NONE;
9247
9248 *len = 0;
9249 *str += strspn (*str, " \t");
9250 if (**str == '(')
9251 {
9252 char *b = *str + 1 + strspn (*str + 1, " \t");
9253 char *e;
9254
9255 /* Check for base register. */
9256 if (b[0] == '$')
9257 {
9258 if (strchr (b, ')')
9259 && (e = b + strcspn (b, ") \t"))
9260 && e - b > 1 && e - b < 4)
9261 {
9262 if ((e - b == 3
9263 && ((b[1] == 'f' && b[2] == 'p')
9264 || (b[1] == 's' && b[2] == 'p')
9265 || (b[1] == 'g' && b[2] == 'p')
9266 || (b[1] == 'a' && b[2] == 't')
9267 || (ISDIGIT (b[1])
9268 && ISDIGIT (b[2]))))
9269 || (ISDIGIT (b[1])))
9270 {
9271 *len = strcspn (*str, ")") + 1;
9272 return S_EX_REGISTER;
9273 }
9274 }
9275 }
9276 else if (b[0] == '%')
9277 {
9278 *str = b;
9279 goto percent_op;
9280 }
9281
9282 /* Some other expression in the braces. */
9283 *len = strcspn (*str, ")") + 1;
9284 }
9285 /* Check for percent_op. */
9286 else if (*str[0] == '%')
9287 {
9288 char *tmp;
9289 unsigned int i;
9290
9291 percent_op:
9292 tmp = *str + 1;
9293 i = 0;
9294
9295 while (ISALPHA (*tmp) || *tmp == '_')
9296 {
9297 *tmp = TOLOWER (*tmp);
9298 tmp++;
9299 }
9300 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9301 {
9302 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9303 i++;
9304 else
9305 {
9306 type = percent_op[i].type;
9307
9308 /* Only %hi and %lo are allowed for OldABI. */
9309 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9310 return S_EX_NONE;
9311
9312 *len = strlen (percent_op[i].str);
9313 (*nestlevel)++;
9314 return type;
9315 }
9316 }
9317 }
9318
9319 /* Any other expression. */
9320 return S_EX_NONE;
9321 }
9322
9323 static int
9324 my_getSmallExpression (ep, str)
9325 expressionS *ep;
9326 char *str;
9327 {
9328 static char *oldstr = NULL;
9329 int c = S_EX_NONE;
9330 int oldc;
9331 int nest_level = 0;
9332 unsigned int len;
9333
9334 /* Don't update oldstr if the last call had nested percent_op's. */
9335 if (! oldstr)
9336 oldstr = str;
9337
9338 do
9339 {
9340 oldc = c;
9341 c = my_getSmallParser (&str, &len, &nest_level);
9342 if (c != S_EX_NONE && c != S_EX_REGISTER)
9343 str += len;
9344 }
9345 while (c != S_EX_NONE && c != S_EX_REGISTER);
9346
9347 /* A percent_op was encountered. */
9348 if (nest_level)
9349 {
9350 /* Don't try to get an expression if it is already blanked out. */
9351 if (*(str + strspn (str + 1, " )")) != ')')
9352 {
9353 char save;
9354
9355 save = *(str + len);
9356 *(str + len) = '\0';
9357 my_getExpression (ep, str);
9358 *(str + len) = save;
9359 }
9360 if (nest_level > 1)
9361 {
9362 /* blank out including the % sign. */
9363 char *p = strrchr (oldstr, '%');
9364 memset (p, ' ', str - p + len);
9365 str = oldstr;
9366 }
9367 else
9368 {
9369 expr_end = strchr (str, ')') + 1;
9370 }
9371 c = oldc;
9372 }
9373 else if (c == S_EX_NONE)
9374 {
9375 my_getExpression (ep, str);
9376 }
9377 else if (c == S_EX_REGISTER)
9378 {
9379 ep->X_op = O_constant;
9380 expr_end = str;
9381 ep->X_add_symbol = NULL;
9382 ep->X_op_symbol = NULL;
9383 ep->X_add_number = 0;
9384 }
9385 else
9386 {
9387 as_fatal(_("internal error"));
9388 }
9389
9390 if (nest_level <= 1)
9391 oldstr = NULL;
9392
9393 return c;
9394 }
9395
9396 static void
9397 my_getExpression (ep, str)
9398 expressionS *ep;
9399 char *str;
9400 {
9401 char *save_in;
9402 valueT val;
9403
9404 save_in = input_line_pointer;
9405 input_line_pointer = str;
9406 expression (ep);
9407 expr_end = input_line_pointer;
9408 input_line_pointer = save_in;
9409
9410 /* If we are in mips16 mode, and this is an expression based on `.',
9411 then we bump the value of the symbol by 1 since that is how other
9412 text symbols are handled. We don't bother to handle complex
9413 expressions, just `.' plus or minus a constant. */
9414 if (mips_opts.mips16
9415 && ep->X_op == O_symbol
9416 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9417 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
9418 && symbol_get_frag (ep->X_add_symbol) == frag_now
9419 && symbol_constant_p (ep->X_add_symbol)
9420 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9421 S_SET_VALUE (ep->X_add_symbol, val + 1);
9422 }
9423
9424 /* Turn a string in input_line_pointer into a floating point constant
9425 of type TYPE, and store the appropriate bytes in *LITP. The number
9426 of LITTLENUMS emitted is stored in *SIZEP. An error message is
9427 returned, or NULL on OK. */
9428
9429 char *
9430 md_atof (type, litP, sizeP)
9431 int type;
9432 char *litP;
9433 int *sizeP;
9434 {
9435 int prec;
9436 LITTLENUM_TYPE words[4];
9437 char *t;
9438 int i;
9439
9440 switch (type)
9441 {
9442 case 'f':
9443 prec = 2;
9444 break;
9445
9446 case 'd':
9447 prec = 4;
9448 break;
9449
9450 default:
9451 *sizeP = 0;
9452 return _("bad call to md_atof");
9453 }
9454
9455 t = atof_ieee (input_line_pointer, type, words);
9456 if (t)
9457 input_line_pointer = t;
9458
9459 *sizeP = prec * 2;
9460
9461 if (! target_big_endian)
9462 {
9463 for (i = prec - 1; i >= 0; i--)
9464 {
9465 md_number_to_chars (litP, (valueT) words[i], 2);
9466 litP += 2;
9467 }
9468 }
9469 else
9470 {
9471 for (i = 0; i < prec; i++)
9472 {
9473 md_number_to_chars (litP, (valueT) words[i], 2);
9474 litP += 2;
9475 }
9476 }
9477
9478 return NULL;
9479 }
9480
9481 void
9482 md_number_to_chars (buf, val, n)
9483 char *buf;
9484 valueT val;
9485 int n;
9486 {
9487 if (target_big_endian)
9488 number_to_chars_bigendian (buf, val, n);
9489 else
9490 number_to_chars_littleendian (buf, val, n);
9491 }
9492 \f
9493 #ifdef OBJ_ELF
9494 static int support_64bit_objects(void)
9495 {
9496 const char **list, **l;
9497
9498 list = bfd_target_list ();
9499 for (l = list; *l != NULL; l++)
9500 #ifdef TE_TMIPS
9501 /* This is traditional mips */
9502 if (strcmp (*l, "elf64-tradbigmips") == 0
9503 || strcmp (*l, "elf64-tradlittlemips") == 0)
9504 #else
9505 if (strcmp (*l, "elf64-bigmips") == 0
9506 || strcmp (*l, "elf64-littlemips") == 0)
9507 #endif
9508 break;
9509 free (list);
9510 return (*l != NULL);
9511 }
9512 #endif /* OBJ_ELF */
9513
9514 CONST char *md_shortopts = "nO::g::G:";
9515
9516 struct option md_longopts[] =
9517 {
9518 #define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9519 {"mips0", no_argument, NULL, OPTION_MIPS1},
9520 {"mips1", no_argument, NULL, OPTION_MIPS1},
9521 #define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9522 {"mips2", no_argument, NULL, OPTION_MIPS2},
9523 #define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9524 {"mips3", no_argument, NULL, OPTION_MIPS3},
9525 #define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9526 {"mips4", no_argument, NULL, OPTION_MIPS4},
9527 #define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9528 {"mips5", no_argument, NULL, OPTION_MIPS5},
9529 #define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9530 {"mips32", no_argument, NULL, OPTION_MIPS32},
9531 #define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9532 {"mips64", no_argument, NULL, OPTION_MIPS64},
9533 #define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
9534 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
9535 #define OPTION_TRAP (OPTION_MD_BASE + 9)
9536 {"trap", no_argument, NULL, OPTION_TRAP},
9537 {"no-break", no_argument, NULL, OPTION_TRAP},
9538 #define OPTION_BREAK (OPTION_MD_BASE + 10)
9539 {"break", no_argument, NULL, OPTION_BREAK},
9540 {"no-trap", no_argument, NULL, OPTION_BREAK},
9541 #define OPTION_EB (OPTION_MD_BASE + 11)
9542 {"EB", no_argument, NULL, OPTION_EB},
9543 #define OPTION_EL (OPTION_MD_BASE + 12)
9544 {"EL", no_argument, NULL, OPTION_EL},
9545 #define OPTION_MIPS16 (OPTION_MD_BASE + 13)
9546 {"mips16", no_argument, NULL, OPTION_MIPS16},
9547 #define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
9548 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
9549 #define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
9550 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9551 #define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
9552 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
9553 #define OPTION_FP32 (OPTION_MD_BASE + 17)
9554 {"mfp32", no_argument, NULL, OPTION_FP32},
9555 #define OPTION_GP32 (OPTION_MD_BASE + 18)
9556 {"mgp32", no_argument, NULL, OPTION_GP32},
9557 #define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
9558 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
9559 #define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
9560 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
9561 #define OPTION_MARCH (OPTION_MD_BASE + 21)
9562 {"march", required_argument, NULL, OPTION_MARCH},
9563 #define OPTION_MTUNE (OPTION_MD_BASE + 22)
9564 {"mtune", required_argument, NULL, OPTION_MTUNE},
9565 #define OPTION_MCPU (OPTION_MD_BASE + 23)
9566 {"mcpu", required_argument, NULL, OPTION_MCPU},
9567 #define OPTION_M4650 (OPTION_MD_BASE + 24)
9568 {"m4650", no_argument, NULL, OPTION_M4650},
9569 #define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9570 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9571 #define OPTION_M4010 (OPTION_MD_BASE + 26)
9572 {"m4010", no_argument, NULL, OPTION_M4010},
9573 #define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9574 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9575 #define OPTION_M4100 (OPTION_MD_BASE + 28)
9576 {"m4100", no_argument, NULL, OPTION_M4100},
9577 #define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9578 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9579 #define OPTION_M3900 (OPTION_MD_BASE + 30)
9580 {"m3900", no_argument, NULL, OPTION_M3900},
9581 #define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9582 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9583 #define OPTION_GP64 (OPTION_MD_BASE + 32)
9584 {"mgp64", no_argument, NULL, OPTION_GP64},
9585 #ifdef OBJ_ELF
9586 #define OPTION_ELF_BASE (OPTION_MD_BASE + 33)
9587 #define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
9588 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9589 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
9590 #define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
9591 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
9592 #define OPTION_XGOT (OPTION_ELF_BASE + 2)
9593 {"xgot", no_argument, NULL, OPTION_XGOT},
9594 #define OPTION_MABI (OPTION_ELF_BASE + 3)
9595 {"mabi", required_argument, NULL, OPTION_MABI},
9596 #define OPTION_32 (OPTION_ELF_BASE + 4)
9597 {"32", no_argument, NULL, OPTION_32},
9598 #define OPTION_N32 (OPTION_ELF_BASE + 5)
9599 {"n32", no_argument, NULL, OPTION_N32},
9600 #define OPTION_64 (OPTION_ELF_BASE + 6)
9601 {"64", no_argument, NULL, OPTION_64},
9602 #endif /* OBJ_ELF */
9603 {NULL, no_argument, NULL, 0}
9604 };
9605 size_t md_longopts_size = sizeof (md_longopts);
9606
9607 int
9608 md_parse_option (c, arg)
9609 int c;
9610 char *arg;
9611 {
9612 switch (c)
9613 {
9614 case OPTION_CONSTRUCT_FLOATS:
9615 mips_disable_float_construction = 0;
9616 break;
9617
9618 case OPTION_NO_CONSTRUCT_FLOATS:
9619 mips_disable_float_construction = 1;
9620 break;
9621
9622 case OPTION_TRAP:
9623 mips_trap = 1;
9624 break;
9625
9626 case OPTION_BREAK:
9627 mips_trap = 0;
9628 break;
9629
9630 case OPTION_EB:
9631 target_big_endian = 1;
9632 break;
9633
9634 case OPTION_EL:
9635 target_big_endian = 0;
9636 break;
9637
9638 case 'n':
9639 warn_nops = 1;
9640 break;
9641
9642 case 'O':
9643 if (arg && arg[1] == '0')
9644 mips_optimize = 1;
9645 else
9646 mips_optimize = 2;
9647 break;
9648
9649 case 'g':
9650 if (arg == NULL)
9651 mips_debug = 2;
9652 else
9653 mips_debug = atoi (arg);
9654 /* When the MIPS assembler sees -g or -g2, it does not do
9655 optimizations which limit full symbolic debugging. We take
9656 that to be equivalent to -O0. */
9657 if (mips_debug == 2)
9658 mips_optimize = 1;
9659 break;
9660
9661 case OPTION_MIPS1:
9662 mips_opts.isa = ISA_MIPS1;
9663 break;
9664
9665 case OPTION_MIPS2:
9666 mips_opts.isa = ISA_MIPS2;
9667 break;
9668
9669 case OPTION_MIPS3:
9670 mips_opts.isa = ISA_MIPS3;
9671 break;
9672
9673 case OPTION_MIPS4:
9674 mips_opts.isa = ISA_MIPS4;
9675 break;
9676
9677 case OPTION_MIPS5:
9678 mips_opts.isa = ISA_MIPS5;
9679 break;
9680
9681 case OPTION_MIPS32:
9682 mips_opts.isa = ISA_MIPS32;
9683 break;
9684
9685 case OPTION_MIPS64:
9686 mips_opts.isa = ISA_MIPS64;
9687 break;
9688
9689 case OPTION_MTUNE:
9690 case OPTION_MARCH:
9691 case OPTION_MCPU:
9692 {
9693 int cpu = CPU_UNKNOWN;
9694
9695 /* Identify the processor type. */
9696 if (strcasecmp (arg, "default") != 0)
9697 {
9698 const struct mips_cpu_info *ci;
9699
9700 ci = mips_cpu_info_from_name (arg);
9701 if (ci == NULL || ci->is_isa)
9702 {
9703 switch (c)
9704 {
9705 case OPTION_MTUNE:
9706 as_fatal (_("invalid architecture -mtune=%s"), arg);
9707 break;
9708 case OPTION_MARCH:
9709 as_fatal (_("invalid architecture -march=%s"), arg);
9710 break;
9711 case OPTION_MCPU:
9712 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9713 break;
9714 }
9715 }
9716 else
9717 cpu = ci->cpu;
9718 }
9719
9720 switch (c)
9721 {
9722 case OPTION_MTUNE:
9723 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9724 as_warn(_("A different -mtune= was already specified, is now "
9725 "-mtune=%s"), arg);
9726 mips_tune = cpu;
9727 break;
9728 case OPTION_MARCH:
9729 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9730 as_warn(_("A different -march= was already specified, is now "
9731 "-march=%s"), arg);
9732 mips_arch = cpu;
9733 break;
9734 case OPTION_MCPU:
9735 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9736 as_warn(_("A different -mcpu= was already specified, is now "
9737 "-mcpu=%s"), arg);
9738 mips_cpu = cpu;
9739 }
9740 }
9741 break;
9742
9743 case OPTION_M4650:
9744 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9745 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9746 as_warn(_("A different -march= or -mtune= was already specified, "
9747 "is now -m4650"));
9748 mips_arch = CPU_R4650;
9749 mips_tune = CPU_R4650;
9750 break;
9751
9752 case OPTION_NO_M4650:
9753 break;
9754
9755 case OPTION_M4010:
9756 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9757 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9758 as_warn(_("A different -march= or -mtune= was already specified, "
9759 "is now -m4010"));
9760 mips_arch = CPU_R4010;
9761 mips_tune = CPU_R4010;
9762 break;
9763
9764 case OPTION_NO_M4010:
9765 break;
9766
9767 case OPTION_M4100:
9768 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9769 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9770 as_warn(_("A different -march= or -mtune= was already specified, "
9771 "is now -m4100"));
9772 mips_arch = CPU_VR4100;
9773 mips_tune = CPU_VR4100;
9774 break;
9775
9776 case OPTION_NO_M4100:
9777 break;
9778
9779 case OPTION_M3900:
9780 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9781 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9782 as_warn(_("A different -march= or -mtune= was already specified, "
9783 "is now -m3900"));
9784 mips_arch = CPU_R3900;
9785 mips_tune = CPU_R3900;
9786 break;
9787
9788 case OPTION_NO_M3900:
9789 break;
9790
9791 case OPTION_MIPS16:
9792 mips_opts.mips16 = 1;
9793 mips_no_prev_insn (false);
9794 break;
9795
9796 case OPTION_NO_MIPS16:
9797 mips_opts.mips16 = 0;
9798 mips_no_prev_insn (false);
9799 break;
9800
9801 case OPTION_MEMBEDDED_PIC:
9802 mips_pic = EMBEDDED_PIC;
9803 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9804 {
9805 as_bad (_("-G may not be used with embedded PIC code"));
9806 return 0;
9807 }
9808 g_switch_value = 0x7fffffff;
9809 break;
9810
9811 #ifdef OBJ_ELF
9812 /* When generating ELF code, we permit -KPIC and -call_shared to
9813 select SVR4_PIC, and -non_shared to select no PIC. This is
9814 intended to be compatible with Irix 5. */
9815 case OPTION_CALL_SHARED:
9816 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9817 {
9818 as_bad (_("-call_shared is supported only for ELF format"));
9819 return 0;
9820 }
9821 mips_pic = SVR4_PIC;
9822 if (g_switch_seen && g_switch_value != 0)
9823 {
9824 as_bad (_("-G may not be used with SVR4 PIC code"));
9825 return 0;
9826 }
9827 g_switch_value = 0;
9828 break;
9829
9830 case OPTION_NON_SHARED:
9831 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
9832 {
9833 as_bad (_("-non_shared is supported only for ELF format"));
9834 return 0;
9835 }
9836 mips_pic = NO_PIC;
9837 break;
9838
9839 /* The -xgot option tells the assembler to use 32 offsets when
9840 accessing the got in SVR4_PIC mode. It is for Irix
9841 compatibility. */
9842 case OPTION_XGOT:
9843 mips_big_got = 1;
9844 break;
9845 #endif /* OBJ_ELF */
9846
9847 case 'G':
9848 if (! USE_GLOBAL_POINTER_OPT)
9849 {
9850 as_bad (_("-G is not supported for this configuration"));
9851 return 0;
9852 }
9853 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
9854 {
9855 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
9856 return 0;
9857 }
9858 else
9859 g_switch_value = atoi (arg);
9860 g_switch_seen = 1;
9861 break;
9862
9863 #ifdef OBJ_ELF
9864 /* The -32 and -64 options tell the assembler to output the 32
9865 bit or the 64 bit MIPS ELF format. */
9866 case OPTION_32:
9867 mips_opts.abi = O32_ABI;
9868 break;
9869
9870 case OPTION_N32:
9871 mips_opts.abi = N32_ABI;
9872 break;
9873
9874 case OPTION_64:
9875 mips_opts.abi = N64_ABI;
9876 if (! support_64bit_objects())
9877 as_fatal (_("No compiled in support for 64 bit object file format"));
9878 break;
9879 #endif /* OBJ_ELF */
9880
9881 case OPTION_GP32:
9882 file_mips_gp32 = 1;
9883 if (mips_opts.abi != O32_ABI)
9884 mips_opts.abi = NO_ABI;
9885 break;
9886
9887 case OPTION_GP64:
9888 file_mips_gp32 = 0;
9889 if (mips_opts.abi == O32_ABI)
9890 mips_opts.abi = NO_ABI;
9891 break;
9892
9893 case OPTION_FP32:
9894 file_mips_fp32 = 1;
9895 if (mips_opts.abi != O32_ABI)
9896 mips_opts.abi = NO_ABI;
9897 break;
9898
9899 #ifdef OBJ_ELF
9900 case OPTION_MABI:
9901 if (strcmp (arg, "32") == 0)
9902 mips_opts.abi = O32_ABI;
9903 else if (strcmp (arg, "o64") == 0)
9904 mips_opts.abi = O64_ABI;
9905 else if (strcmp (arg, "n32") == 0)
9906 mips_opts.abi = N32_ABI;
9907 else if (strcmp (arg, "64") == 0)
9908 {
9909 mips_opts.abi = N64_ABI;
9910 if (! support_64bit_objects())
9911 as_fatal (_("No compiled in support for 64 bit object file "
9912 "format"));
9913 }
9914 else if (strcmp (arg, "eabi") == 0)
9915 mips_opts.abi = EABI_ABI;
9916 else
9917 mips_opts.abi = NO_ABI;
9918 break;
9919 #endif /* OBJ_ELF */
9920
9921 case OPTION_M7000_HILO_FIX:
9922 mips_7000_hilo_fix = true;
9923 break;
9924
9925 case OPTION_NO_M7000_HILO_FIX:
9926 mips_7000_hilo_fix = false;
9927 break;
9928
9929 default:
9930 return 0;
9931 }
9932
9933 return 1;
9934 }
9935
9936 static void
9937 show (stream, string, col_p, first_p)
9938 FILE *stream;
9939 char *string;
9940 int *col_p;
9941 int *first_p;
9942 {
9943 if (*first_p)
9944 {
9945 fprintf (stream, "%24s", "");
9946 *col_p = 24;
9947 }
9948 else
9949 {
9950 fprintf (stream, ", ");
9951 *col_p += 2;
9952 }
9953
9954 if (*col_p + strlen (string) > 72)
9955 {
9956 fprintf (stream, "\n%24s", "");
9957 *col_p = 24;
9958 }
9959
9960 fprintf (stream, "%s", string);
9961 *col_p += strlen (string);
9962
9963 *first_p = 0;
9964 }
9965
9966 void
9967 md_show_usage (stream)
9968 FILE *stream;
9969 {
9970 int column, first;
9971
9972 fprintf (stream, _("\
9973 MIPS options:\n\
9974 -membedded-pic generate embedded position independent code\n\
9975 -EB generate big endian output\n\
9976 -EL generate little endian output\n\
9977 -g, -g2 do not remove unneeded NOPs or swap branches\n\
9978 -G NUM allow referencing objects up to NUM bytes\n\
9979 implicitly with the gp register [default 8]\n"));
9980 fprintf (stream, _("\
9981 -mips1 generate MIPS ISA I instructions\n\
9982 -mips2 generate MIPS ISA II instructions\n\
9983 -mips3 generate MIPS ISA III instructions\n\
9984 -mips4 generate MIPS ISA IV instructions\n\
9985 -mips5 generate MIPS ISA V instructions\n\
9986 -mips32 generate MIPS32 ISA instructions\n\
9987 -mips64 generate MIPS64 ISA instructions\n\
9988 -march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
9989
9990 first = 1;
9991
9992 show (stream, "2000", &column, &first);
9993 show (stream, "3000", &column, &first);
9994 show (stream, "3900", &column, &first);
9995 show (stream, "4000", &column, &first);
9996 show (stream, "4010", &column, &first);
9997 show (stream, "4100", &column, &first);
9998 show (stream, "4111", &column, &first);
9999 show (stream, "4300", &column, &first);
10000 show (stream, "4400", &column, &first);
10001 show (stream, "4600", &column, &first);
10002 show (stream, "4650", &column, &first);
10003 show (stream, "5000", &column, &first);
10004 show (stream, "5200", &column, &first);
10005 show (stream, "5230", &column, &first);
10006 show (stream, "5231", &column, &first);
10007 show (stream, "5261", &column, &first);
10008 show (stream, "5721", &column, &first);
10009 show (stream, "6000", &column, &first);
10010 show (stream, "8000", &column, &first);
10011 show (stream, "10000", &column, &first);
10012 show (stream, "12000", &column, &first);
10013 show (stream, "sb1", &column, &first);
10014 fputc ('\n', stream);
10015
10016 fprintf (stream, _("\
10017 -mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
10018 -no-mCPU don't generate code specific to CPU.\n\
10019 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10020
10021 first = 1;
10022
10023 show (stream, "3900", &column, &first);
10024 show (stream, "4010", &column, &first);
10025 show (stream, "4100", &column, &first);
10026 show (stream, "4650", &column, &first);
10027 fputc ('\n', stream);
10028
10029 fprintf (stream, _("\
10030 -mips16 generate mips16 instructions\n\
10031 -no-mips16 do not generate mips16 instructions\n"));
10032 fprintf (stream, _("\
10033 -mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10034 -mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
10035 -O0 remove unneeded NOPs, do not swap branches\n\
10036 -O remove unneeded NOPs and swap branches\n\
10037 -n warn about NOPs generated from macros\n\
10038 --[no-]construct-floats [dis]allow floating point values to be constructed\n\
10039 --trap, --no-break trap exception on div by 0 and mult overflow\n\
10040 --break, --no-trap break exception on div by 0 and mult overflow\n"));
10041 #ifdef OBJ_ELF
10042 fprintf (stream, _("\
10043 -KPIC, -call_shared generate SVR4 position independent code\n\
10044 -non_shared do not generate position independent code\n\
10045 -xgot assume a 32 bit GOT\n\
10046 -32 create o32 ABI object file (default)\n\
10047 -n32 create n32 ABI object file\n\
10048 -64 create 64 ABI object file\n"));
10049 #endif
10050 }
10051 \f
10052 void
10053 mips_init_after_args ()
10054 {
10055 /* initialize opcodes */
10056 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
10057 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
10058 }
10059
10060 long
10061 md_pcrel_from (fixP)
10062 fixS *fixP;
10063 {
10064 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10065 && fixP->fx_addsy != (symbolS *) NULL
10066 && ! S_IS_DEFINED (fixP->fx_addsy))
10067 {
10068 /* This makes a branch to an undefined symbol be a branch to the
10069 current location. */
10070 if (mips_pic == EMBEDDED_PIC)
10071 return 4;
10072 else
10073 return 1;
10074 }
10075
10076 /* return the address of the delay slot */
10077 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10078 }
10079
10080 /* This is called before the symbol table is processed. In order to
10081 work with gcc when using mips-tfile, we must keep all local labels.
10082 However, in other cases, we want to discard them. If we were
10083 called with -g, but we didn't see any debugging information, it may
10084 mean that gcc is smuggling debugging information through to
10085 mips-tfile, in which case we must generate all local labels. */
10086
10087 void
10088 mips_frob_file_before_adjust ()
10089 {
10090 #ifndef NO_ECOFF_DEBUGGING
10091 if (ECOFF_DEBUGGING
10092 && mips_debug != 0
10093 && ! ecoff_debugging_seen)
10094 flag_keep_locals = 1;
10095 #endif
10096 }
10097
10098 /* Sort any unmatched HI16_S relocs so that they immediately precede
10099 the corresponding LO reloc. This is called before md_apply_fix3 and
10100 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10101 explicit use of the %hi modifier. */
10102
10103 void
10104 mips_frob_file ()
10105 {
10106 struct mips_hi_fixup *l;
10107
10108 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10109 {
10110 segment_info_type *seginfo;
10111 int pass;
10112
10113 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10114
10115 /* Check quickly whether the next fixup happens to be a matching
10116 %lo. */
10117 if (l->fixp->fx_next != NULL
10118 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10119 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10120 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10121 continue;
10122
10123 /* Look through the fixups for this segment for a matching %lo.
10124 When we find one, move the %hi just in front of it. We do
10125 this in two passes. In the first pass, we try to find a
10126 unique %lo. In the second pass, we permit multiple %hi
10127 relocs for a single %lo (this is a GNU extension). */
10128 seginfo = seg_info (l->seg);
10129 for (pass = 0; pass < 2; pass++)
10130 {
10131 fixS *f, *prev;
10132
10133 prev = NULL;
10134 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10135 {
10136 /* Check whether this is a %lo fixup which matches l->fixp. */
10137 if (f->fx_r_type == BFD_RELOC_LO16
10138 && f->fx_addsy == l->fixp->fx_addsy
10139 && f->fx_offset == l->fixp->fx_offset
10140 && (pass == 1
10141 || prev == NULL
10142 || prev->fx_r_type != BFD_RELOC_HI16_S
10143 || prev->fx_addsy != f->fx_addsy
10144 || prev->fx_offset != f->fx_offset))
10145 {
10146 fixS **pf;
10147
10148 /* Move l->fixp before f. */
10149 for (pf = &seginfo->fix_root;
10150 *pf != l->fixp;
10151 pf = &(*pf)->fx_next)
10152 assert (*pf != NULL);
10153
10154 *pf = l->fixp->fx_next;
10155
10156 l->fixp->fx_next = f;
10157 if (prev == NULL)
10158 seginfo->fix_root = l->fixp;
10159 else
10160 prev->fx_next = l->fixp;
10161
10162 break;
10163 }
10164
10165 prev = f;
10166 }
10167
10168 if (f != NULL)
10169 break;
10170
10171 #if 0 /* GCC code motion plus incomplete dead code elimination
10172 can leave a %hi without a %lo. */
10173 if (pass == 1)
10174 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10175 _("Unmatched %%hi reloc"));
10176 #endif
10177 }
10178 }
10179 }
10180
10181 /* When generating embedded PIC code we need to use a special
10182 relocation to represent the difference of two symbols in the .text
10183 section (switch tables use a difference of this sort). See
10184 include/coff/mips.h for details. This macro checks whether this
10185 fixup requires the special reloc. */
10186 #define SWITCH_TABLE(fixp) \
10187 ((fixp)->fx_r_type == BFD_RELOC_32 \
10188 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
10189 && (fixp)->fx_addsy != NULL \
10190 && (fixp)->fx_subsy != NULL \
10191 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10192 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10193
10194 /* When generating embedded PIC code we must keep all PC relative
10195 relocations, in case the linker has to relax a call. We also need
10196 to keep relocations for switch table entries.
10197
10198 We may have combined relocations without symbols in the N32/N64 ABI.
10199 We have to prevent gas from dropping them. */
10200
10201 int
10202 mips_force_relocation (fixp)
10203 fixS *fixp;
10204 {
10205 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10206 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10207 return 1;
10208
10209 if (HAVE_NEWABI
10210 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10211 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10212 || fixp->fx_r_type == BFD_RELOC_HI16_S
10213 || fixp->fx_r_type == BFD_RELOC_LO16))
10214 return 1;
10215
10216 return (mips_pic == EMBEDDED_PIC
10217 && (fixp->fx_pcrel
10218 || SWITCH_TABLE (fixp)
10219 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10220 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10221 }
10222
10223 #ifdef OBJ_ELF
10224 static int
10225 mips_need_elf_addend_fixup (fixP)
10226 fixS *fixP;
10227 {
10228 return (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16
10229 || ((S_IS_WEAK (fixP->fx_addsy)
10230 || S_IS_EXTERN (fixP->fx_addsy))
10231 && !S_IS_COMMON (fixP->fx_addsy))
10232 || (symbol_used_in_reloc_p (fixP->fx_addsy)
10233 && (((bfd_get_section_flags (stdoutput,
10234 S_GET_SEGMENT (fixP->fx_addsy))
10235 & SEC_LINK_ONCE) != 0)
10236 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10237 ".gnu.linkonce",
10238 sizeof (".gnu.linkonce") - 1))));
10239 }
10240 #endif
10241
10242 /* Apply a fixup to the object file. */
10243
10244 void
10245 md_apply_fix3 (fixP, valP, seg)
10246 fixS *fixP;
10247 valueT * valP;
10248 segT seg ATTRIBUTE_UNUSED;
10249 {
10250 unsigned char *buf;
10251 long insn;
10252 valueT value;
10253
10254 assert (fixP->fx_size == 4
10255 || fixP->fx_r_type == BFD_RELOC_16
10256 || fixP->fx_r_type == BFD_RELOC_32
10257 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10258 || fixP->fx_r_type == BFD_RELOC_HI16_S
10259 || fixP->fx_r_type == BFD_RELOC_LO16
10260 || fixP->fx_r_type == BFD_RELOC_GPREL16
10261 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
10262 || fixP->fx_r_type == BFD_RELOC_GPREL32
10263 || fixP->fx_r_type == BFD_RELOC_64
10264 || fixP->fx_r_type == BFD_RELOC_CTOR
10265 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10266 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10267 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10268 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10269 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10270 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
10271 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10272 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10273
10274 value = * valP;
10275
10276 /* If we aren't adjusting this fixup to be against the section
10277 symbol, we need to adjust the value. */
10278 #ifdef OBJ_ELF
10279 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
10280 {
10281 if (mips_need_elf_addend_fixup (fixP))
10282 {
10283 valueT symval = S_GET_VALUE (fixP->fx_addsy);
10284
10285 value -= symval;
10286 if (value != 0 && ! fixP->fx_pcrel)
10287 {
10288 /* In this case, the bfd_install_relocation routine will
10289 incorrectly add the symbol value back in. We just want
10290 the addend to appear in the object file. */
10291 value -= symval;
10292
10293 /* Make sure the addend is still non-zero. If it became zero
10294 after the last operation, set it to a spurious value and
10295 subtract the same value from the object file's contents. */
10296 if (value == 0)
10297 {
10298 value = 8;
10299
10300 /* The in-place addends for LO16 relocations are signed;
10301 leave the matching HI16 in-place addends as zero. */
10302 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10303 {
10304 reloc_howto_type *howto;
10305 bfd_vma contents, mask, field;
10306
10307 howto = bfd_reloc_type_lookup (stdoutput,
10308 fixP->fx_r_type);
10309
10310 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10311 + fixP->fx_where,
10312 fixP->fx_size * 8,
10313 target_big_endian);
10314
10315 /* MASK has bits set where the relocation should go.
10316 FIELD is -value, shifted into the appropriate place
10317 for this relocation. */
10318 mask = 1 << (howto->bitsize - 1);
10319 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10320 field = (-value >> howto->rightshift) << howto->bitpos;
10321
10322 bfd_put_bits ((field & mask) | (contents & ~mask),
10323 fixP->fx_frag->fr_literal + fixP->fx_where,
10324 fixP->fx_size * 8,
10325 target_big_endian);
10326 }
10327 }
10328 }
10329 }
10330
10331 /* This code was generated using trial and error and so is
10332 fragile and not trustworthy. If you change it, you should
10333 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10334 they still pass. */
10335 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10336 {
10337 value += fixP->fx_frag->fr_address + fixP->fx_where;
10338
10339 /* BFD's REL handling, for MIPS, is _very_ weird.
10340 This gives the right results, but it can't possibly
10341 be the way things are supposed to work. */
10342 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10343 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
10344 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10345 value += fixP->fx_frag->fr_address + fixP->fx_where;
10346 }
10347 }
10348 #endif
10349
10350 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
10351
10352 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10353 fixP->fx_done = 1;
10354
10355 switch (fixP->fx_r_type)
10356 {
10357 case BFD_RELOC_MIPS_JMP:
10358 case BFD_RELOC_MIPS_SHIFT5:
10359 case BFD_RELOC_MIPS_SHIFT6:
10360 case BFD_RELOC_MIPS_GOT_DISP:
10361 case BFD_RELOC_MIPS_GOT_PAGE:
10362 case BFD_RELOC_MIPS_GOT_OFST:
10363 case BFD_RELOC_MIPS_SUB:
10364 case BFD_RELOC_MIPS_INSERT_A:
10365 case BFD_RELOC_MIPS_INSERT_B:
10366 case BFD_RELOC_MIPS_DELETE:
10367 case BFD_RELOC_MIPS_HIGHEST:
10368 case BFD_RELOC_MIPS_HIGHER:
10369 case BFD_RELOC_MIPS_SCN_DISP:
10370 case BFD_RELOC_MIPS_REL16:
10371 case BFD_RELOC_MIPS_RELGOT:
10372 case BFD_RELOC_MIPS_JALR:
10373 case BFD_RELOC_HI16:
10374 case BFD_RELOC_HI16_S:
10375 case BFD_RELOC_GPREL16:
10376 case BFD_RELOC_MIPS_LITERAL:
10377 case BFD_RELOC_MIPS_CALL16:
10378 case BFD_RELOC_MIPS_GOT16:
10379 case BFD_RELOC_GPREL32:
10380 case BFD_RELOC_MIPS_GOT_HI16:
10381 case BFD_RELOC_MIPS_GOT_LO16:
10382 case BFD_RELOC_MIPS_CALL_HI16:
10383 case BFD_RELOC_MIPS_CALL_LO16:
10384 case BFD_RELOC_MIPS16_GPREL:
10385 if (fixP->fx_pcrel)
10386 as_bad_where (fixP->fx_file, fixP->fx_line,
10387 _("Invalid PC relative reloc"));
10388 /* Nothing needed to do. The value comes from the reloc entry */
10389 break;
10390
10391 case BFD_RELOC_MIPS16_JMP:
10392 /* We currently always generate a reloc against a symbol, which
10393 means that we don't want an addend even if the symbol is
10394 defined. */
10395 fixP->fx_addnumber = 0;
10396 break;
10397
10398 case BFD_RELOC_PCREL_HI16_S:
10399 /* The addend for this is tricky if it is internal, so we just
10400 do everything here rather than in bfd_install_relocation. */
10401 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10402 && !fixP->fx_done
10403 && value != 0)
10404 break;
10405 if (fixP->fx_addsy
10406 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10407 {
10408 /* For an external symbol adjust by the address to make it
10409 pcrel_offset. We use the address of the RELLO reloc
10410 which follows this one. */
10411 value += (fixP->fx_next->fx_frag->fr_address
10412 + fixP->fx_next->fx_where);
10413 }
10414 value = ((value + 0x8000) >> 16) & 0xffff;
10415 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10416 if (target_big_endian)
10417 buf += 2;
10418 md_number_to_chars (buf, value, 2);
10419 break;
10420
10421 case BFD_RELOC_PCREL_LO16:
10422 /* The addend for this is tricky if it is internal, so we just
10423 do everything here rather than in bfd_install_relocation. */
10424 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
10425 && !fixP->fx_done
10426 && value != 0)
10427 break;
10428 if (fixP->fx_addsy
10429 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
10430 value += fixP->fx_frag->fr_address + fixP->fx_where;
10431 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10432 if (target_big_endian)
10433 buf += 2;
10434 md_number_to_chars (buf, value, 2);
10435 break;
10436
10437 case BFD_RELOC_64:
10438 /* This is handled like BFD_RELOC_32, but we output a sign
10439 extended value if we are only 32 bits. */
10440 if (fixP->fx_done
10441 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10442 {
10443 if (8 <= sizeof (valueT))
10444 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10445 value, 8);
10446 else
10447 {
10448 long w1, w2;
10449 long hiv;
10450
10451 w1 = w2 = fixP->fx_where;
10452 if (target_big_endian)
10453 w1 += 4;
10454 else
10455 w2 += 4;
10456 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10457 if ((value & 0x80000000) != 0)
10458 hiv = 0xffffffff;
10459 else
10460 hiv = 0;
10461 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10462 }
10463 }
10464 break;
10465
10466 case BFD_RELOC_RVA:
10467 case BFD_RELOC_32:
10468 /* If we are deleting this reloc entry, we must fill in the
10469 value now. This can happen if we have a .word which is not
10470 resolved when it appears but is later defined. We also need
10471 to fill in the value if this is an embedded PIC switch table
10472 entry. */
10473 if (fixP->fx_done
10474 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10475 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10476 value, 4);
10477 break;
10478
10479 case BFD_RELOC_16:
10480 /* If we are deleting this reloc entry, we must fill in the
10481 value now. */
10482 assert (fixP->fx_size == 2);
10483 if (fixP->fx_done)
10484 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10485 value, 2);
10486 break;
10487
10488 case BFD_RELOC_LO16:
10489 /* When handling an embedded PIC switch statement, we can wind
10490 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10491 if (fixP->fx_done)
10492 {
10493 if (value + 0x8000 > 0xffff)
10494 as_bad_where (fixP->fx_file, fixP->fx_line,
10495 _("relocation overflow"));
10496 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10497 if (target_big_endian)
10498 buf += 2;
10499 md_number_to_chars (buf, value, 2);
10500 }
10501 break;
10502
10503 case BFD_RELOC_16_PCREL_S2:
10504 if ((value & 0x3) != 0)
10505 as_bad_where (fixP->fx_file, fixP->fx_line,
10506 _("Branch to odd address (%lx)"), (long) value);
10507
10508 /* Fall through. */
10509
10510 case BFD_RELOC_16_PCREL:
10511 /*
10512 * We need to save the bits in the instruction since fixup_segment()
10513 * might be deleting the relocation entry (i.e., a branch within
10514 * the current segment).
10515 */
10516 if (!fixP->fx_done && value != 0)
10517 break;
10518 /* If 'value' is zero, the remaining reloc code won't actually
10519 do the store, so it must be done here. This is probably
10520 a bug somewhere. */
10521 if (!fixP->fx_done)
10522 value -= fixP->fx_frag->fr_address + fixP->fx_where;
10523
10524 value = (offsetT) value >> 2;
10525
10526 /* update old instruction data */
10527 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10528 if (target_big_endian)
10529 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10530 else
10531 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10532
10533 if (value + 0x8000 <= 0xffff)
10534 insn |= value & 0xffff;
10535 else
10536 {
10537 /* The branch offset is too large. If this is an
10538 unconditional branch, and we are not generating PIC code,
10539 we can convert it to an absolute jump instruction. */
10540 if (mips_pic == NO_PIC
10541 && fixP->fx_done
10542 && fixP->fx_frag->fr_address >= text_section->vma
10543 && (fixP->fx_frag->fr_address
10544 < text_section->vma + text_section->_raw_size)
10545 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10546 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10547 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10548 {
10549 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10550 insn = 0x0c000000; /* jal */
10551 else
10552 insn = 0x08000000; /* j */
10553 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10554 fixP->fx_done = 0;
10555 fixP->fx_addsy = section_symbol (text_section);
10556 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10557 }
10558 else
10559 {
10560 /* FIXME. It would be possible in principle to handle
10561 conditional branches which overflow. They could be
10562 transformed into a branch around a jump. This would
10563 require setting up variant frags for each different
10564 branch type. The native MIPS assembler attempts to
10565 handle these cases, but it appears to do it
10566 incorrectly. */
10567 as_bad_where (fixP->fx_file, fixP->fx_line,
10568 _("Branch out of range"));
10569 }
10570 }
10571
10572 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10573 break;
10574
10575 case BFD_RELOC_VTABLE_INHERIT:
10576 fixP->fx_done = 0;
10577 if (fixP->fx_addsy
10578 && !S_IS_DEFINED (fixP->fx_addsy)
10579 && !S_IS_WEAK (fixP->fx_addsy))
10580 S_SET_WEAK (fixP->fx_addsy);
10581 break;
10582
10583 case BFD_RELOC_VTABLE_ENTRY:
10584 fixP->fx_done = 0;
10585 break;
10586
10587 default:
10588 internalError ();
10589 }
10590 }
10591
10592 #if 0
10593 void
10594 printInsn (oc)
10595 unsigned long oc;
10596 {
10597 const struct mips_opcode *p;
10598 int treg, sreg, dreg, shamt;
10599 short imm;
10600 const char *args;
10601 int i;
10602
10603 for (i = 0; i < NUMOPCODES; ++i)
10604 {
10605 p = &mips_opcodes[i];
10606 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10607 {
10608 printf ("%08lx %s\t", oc, p->name);
10609 treg = (oc >> 16) & 0x1f;
10610 sreg = (oc >> 21) & 0x1f;
10611 dreg = (oc >> 11) & 0x1f;
10612 shamt = (oc >> 6) & 0x1f;
10613 imm = oc;
10614 for (args = p->args;; ++args)
10615 {
10616 switch (*args)
10617 {
10618 case '\0':
10619 printf ("\n");
10620 break;
10621
10622 case ',':
10623 case '(':
10624 case ')':
10625 printf ("%c", *args);
10626 continue;
10627
10628 case 'r':
10629 assert (treg == sreg);
10630 printf ("$%d,$%d", treg, sreg);
10631 continue;
10632
10633 case 'd':
10634 case 'G':
10635 printf ("$%d", dreg);
10636 continue;
10637
10638 case 't':
10639 case 'E':
10640 printf ("$%d", treg);
10641 continue;
10642
10643 case 'k':
10644 printf ("0x%x", treg);
10645 continue;
10646
10647 case 'b':
10648 case 's':
10649 printf ("$%d", sreg);
10650 continue;
10651
10652 case 'a':
10653 printf ("0x%08lx", oc & 0x1ffffff);
10654 continue;
10655
10656 case 'i':
10657 case 'j':
10658 case 'o':
10659 case 'u':
10660 printf ("%d", imm);
10661 continue;
10662
10663 case '<':
10664 case '>':
10665 printf ("$%d", shamt);
10666 continue;
10667
10668 default:
10669 internalError ();
10670 }
10671 break;
10672 }
10673 return;
10674 }
10675 }
10676 printf (_("%08lx UNDEFINED\n"), oc);
10677 }
10678 #endif
10679
10680 static symbolS *
10681 get_symbol ()
10682 {
10683 int c;
10684 char *name;
10685 symbolS *p;
10686
10687 name = input_line_pointer;
10688 c = get_symbol_end ();
10689 p = (symbolS *) symbol_find_or_make (name);
10690 *input_line_pointer = c;
10691 return p;
10692 }
10693
10694 /* Align the current frag to a given power of two. The MIPS assembler
10695 also automatically adjusts any preceding label. */
10696
10697 static void
10698 mips_align (to, fill, label)
10699 int to;
10700 int fill;
10701 symbolS *label;
10702 {
10703 mips_emit_delays (false);
10704 frag_align (to, fill, 0);
10705 record_alignment (now_seg, to);
10706 if (label != NULL)
10707 {
10708 assert (S_GET_SEGMENT (label) == now_seg);
10709 symbol_set_frag (label, frag_now);
10710 S_SET_VALUE (label, (valueT) frag_now_fix ());
10711 }
10712 }
10713
10714 /* Align to a given power of two. .align 0 turns off the automatic
10715 alignment used by the data creating pseudo-ops. */
10716
10717 static void
10718 s_align (x)
10719 int x ATTRIBUTE_UNUSED;
10720 {
10721 register int temp;
10722 register long temp_fill;
10723 long max_alignment = 15;
10724
10725 /*
10726
10727 o Note that the assembler pulls down any immediately preceeding label
10728 to the aligned address.
10729 o It's not documented but auto alignment is reinstated by
10730 a .align pseudo instruction.
10731 o Note also that after auto alignment is turned off the mips assembler
10732 issues an error on attempt to assemble an improperly aligned data item.
10733 We don't.
10734
10735 */
10736
10737 temp = get_absolute_expression ();
10738 if (temp > max_alignment)
10739 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10740 else if (temp < 0)
10741 {
10742 as_warn (_("Alignment negative: 0 assumed."));
10743 temp = 0;
10744 }
10745 if (*input_line_pointer == ',')
10746 {
10747 input_line_pointer++;
10748 temp_fill = get_absolute_expression ();
10749 }
10750 else
10751 temp_fill = 0;
10752 if (temp)
10753 {
10754 auto_align = 1;
10755 mips_align (temp, (int) temp_fill,
10756 insn_labels != NULL ? insn_labels->label : NULL);
10757 }
10758 else
10759 {
10760 auto_align = 0;
10761 }
10762
10763 demand_empty_rest_of_line ();
10764 }
10765
10766 void
10767 mips_flush_pending_output ()
10768 {
10769 mips_emit_delays (false);
10770 mips_clear_insn_labels ();
10771 }
10772
10773 static void
10774 s_change_sec (sec)
10775 int sec;
10776 {
10777 segT seg;
10778
10779 /* When generating embedded PIC code, we only use the .text, .lit8,
10780 .sdata and .sbss sections. We change the .data and .rdata
10781 pseudo-ops to use .sdata. */
10782 if (mips_pic == EMBEDDED_PIC
10783 && (sec == 'd' || sec == 'r'))
10784 sec = 's';
10785
10786 #ifdef OBJ_ELF
10787 /* The ELF backend needs to know that we are changing sections, so
10788 that .previous works correctly. We could do something like check
10789 for an obj_section_change_hook macro, but that might be confusing
10790 as it would not be appropriate to use it in the section changing
10791 functions in read.c, since obj-elf.c intercepts those. FIXME:
10792 This should be cleaner, somehow. */
10793 obj_elf_section_change_hook ();
10794 #endif
10795
10796 mips_emit_delays (false);
10797 switch (sec)
10798 {
10799 case 't':
10800 s_text (0);
10801 break;
10802 case 'd':
10803 s_data (0);
10804 break;
10805 case 'b':
10806 subseg_set (bss_section, (subsegT) get_absolute_expression ());
10807 demand_empty_rest_of_line ();
10808 break;
10809
10810 case 'r':
10811 if (USE_GLOBAL_POINTER_OPT)
10812 {
10813 seg = subseg_new (RDATA_SECTION_NAME,
10814 (subsegT) get_absolute_expression ());
10815 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10816 {
10817 bfd_set_section_flags (stdoutput, seg,
10818 (SEC_ALLOC
10819 | SEC_LOAD
10820 | SEC_READONLY
10821 | SEC_RELOC
10822 | SEC_DATA));
10823 if (strcmp (TARGET_OS, "elf") != 0)
10824 record_alignment (seg, 4);
10825 }
10826 demand_empty_rest_of_line ();
10827 }
10828 else
10829 {
10830 as_bad (_("No read only data section in this object file format"));
10831 demand_empty_rest_of_line ();
10832 return;
10833 }
10834 break;
10835
10836 case 's':
10837 if (USE_GLOBAL_POINTER_OPT)
10838 {
10839 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
10840 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
10841 {
10842 bfd_set_section_flags (stdoutput, seg,
10843 SEC_ALLOC | SEC_LOAD | SEC_RELOC
10844 | SEC_DATA);
10845 if (strcmp (TARGET_OS, "elf") != 0)
10846 record_alignment (seg, 4);
10847 }
10848 demand_empty_rest_of_line ();
10849 break;
10850 }
10851 else
10852 {
10853 as_bad (_("Global pointers not supported; recompile -G 0"));
10854 demand_empty_rest_of_line ();
10855 return;
10856 }
10857 }
10858
10859 auto_align = 1;
10860 }
10861
10862 void
10863 mips_enable_auto_align ()
10864 {
10865 auto_align = 1;
10866 }
10867
10868 static void
10869 s_cons (log_size)
10870 int log_size;
10871 {
10872 symbolS *label;
10873
10874 label = insn_labels != NULL ? insn_labels->label : NULL;
10875 mips_emit_delays (false);
10876 if (log_size > 0 && auto_align)
10877 mips_align (log_size, 0, label);
10878 mips_clear_insn_labels ();
10879 cons (1 << log_size);
10880 }
10881
10882 static void
10883 s_float_cons (type)
10884 int type;
10885 {
10886 symbolS *label;
10887
10888 label = insn_labels != NULL ? insn_labels->label : NULL;
10889
10890 mips_emit_delays (false);
10891
10892 if (auto_align)
10893 {
10894 if (type == 'd')
10895 mips_align (3, 0, label);
10896 else
10897 mips_align (2, 0, label);
10898 }
10899
10900 mips_clear_insn_labels ();
10901
10902 float_cons (type);
10903 }
10904
10905 /* Handle .globl. We need to override it because on Irix 5 you are
10906 permitted to say
10907 .globl foo .text
10908 where foo is an undefined symbol, to mean that foo should be
10909 considered to be the address of a function. */
10910
10911 static void
10912 s_mips_globl (x)
10913 int x ATTRIBUTE_UNUSED;
10914 {
10915 char *name;
10916 int c;
10917 symbolS *symbolP;
10918 flagword flag;
10919
10920 name = input_line_pointer;
10921 c = get_symbol_end ();
10922 symbolP = symbol_find_or_make (name);
10923 *input_line_pointer = c;
10924 SKIP_WHITESPACE ();
10925
10926 /* On Irix 5, every global symbol that is not explicitly labelled as
10927 being a function is apparently labelled as being an object. */
10928 flag = BSF_OBJECT;
10929
10930 if (! is_end_of_line[(unsigned char) *input_line_pointer])
10931 {
10932 char *secname;
10933 asection *sec;
10934
10935 secname = input_line_pointer;
10936 c = get_symbol_end ();
10937 sec = bfd_get_section_by_name (stdoutput, secname);
10938 if (sec == NULL)
10939 as_bad (_("%s: no such section"), secname);
10940 *input_line_pointer = c;
10941
10942 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
10943 flag = BSF_FUNCTION;
10944 }
10945
10946 symbol_get_bfdsym (symbolP)->flags |= flag;
10947
10948 S_SET_EXTERNAL (symbolP);
10949 demand_empty_rest_of_line ();
10950 }
10951
10952 static void
10953 s_option (x)
10954 int x ATTRIBUTE_UNUSED;
10955 {
10956 char *opt;
10957 char c;
10958
10959 opt = input_line_pointer;
10960 c = get_symbol_end ();
10961
10962 if (*opt == 'O')
10963 {
10964 /* FIXME: What does this mean? */
10965 }
10966 else if (strncmp (opt, "pic", 3) == 0)
10967 {
10968 int i;
10969
10970 i = atoi (opt + 3);
10971 if (i == 0)
10972 mips_pic = NO_PIC;
10973 else if (i == 2)
10974 mips_pic = SVR4_PIC;
10975 else
10976 as_bad (_(".option pic%d not supported"), i);
10977
10978 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
10979 {
10980 if (g_switch_seen && g_switch_value != 0)
10981 as_warn (_("-G may not be used with SVR4 PIC code"));
10982 g_switch_value = 0;
10983 bfd_set_gp_size (stdoutput, 0);
10984 }
10985 }
10986 else
10987 as_warn (_("Unrecognized option \"%s\""), opt);
10988
10989 *input_line_pointer = c;
10990 demand_empty_rest_of_line ();
10991 }
10992
10993 /* This structure is used to hold a stack of .set values. */
10994
10995 struct mips_option_stack
10996 {
10997 struct mips_option_stack *next;
10998 struct mips_set_options options;
10999 };
11000
11001 static struct mips_option_stack *mips_opts_stack;
11002
11003 /* Handle the .set pseudo-op. */
11004
11005 static void
11006 s_mipsset (x)
11007 int x ATTRIBUTE_UNUSED;
11008 {
11009 char *name = input_line_pointer, ch;
11010
11011 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11012 input_line_pointer++;
11013 ch = *input_line_pointer;
11014 *input_line_pointer = '\0';
11015
11016 if (strcmp (name, "reorder") == 0)
11017 {
11018 if (mips_opts.noreorder && prev_nop_frag != NULL)
11019 {
11020 /* If we still have pending nops, we can discard them. The
11021 usual nop handling will insert any that are still
11022 needed. */
11023 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11024 * (mips_opts.mips16 ? 2 : 4));
11025 prev_nop_frag = NULL;
11026 }
11027 mips_opts.noreorder = 0;
11028 }
11029 else if (strcmp (name, "noreorder") == 0)
11030 {
11031 mips_emit_delays (true);
11032 mips_opts.noreorder = 1;
11033 mips_any_noreorder = 1;
11034 }
11035 else if (strcmp (name, "at") == 0)
11036 {
11037 mips_opts.noat = 0;
11038 }
11039 else if (strcmp (name, "noat") == 0)
11040 {
11041 mips_opts.noat = 1;
11042 }
11043 else if (strcmp (name, "macro") == 0)
11044 {
11045 mips_opts.warn_about_macros = 0;
11046 }
11047 else if (strcmp (name, "nomacro") == 0)
11048 {
11049 if (mips_opts.noreorder == 0)
11050 as_bad (_("`noreorder' must be set before `nomacro'"));
11051 mips_opts.warn_about_macros = 1;
11052 }
11053 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11054 {
11055 mips_opts.nomove = 0;
11056 }
11057 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11058 {
11059 mips_opts.nomove = 1;
11060 }
11061 else if (strcmp (name, "bopt") == 0)
11062 {
11063 mips_opts.nobopt = 0;
11064 }
11065 else if (strcmp (name, "nobopt") == 0)
11066 {
11067 mips_opts.nobopt = 1;
11068 }
11069 else if (strcmp (name, "mips16") == 0
11070 || strcmp (name, "MIPS-16") == 0)
11071 mips_opts.mips16 = 1;
11072 else if (strcmp (name, "nomips16") == 0
11073 || strcmp (name, "noMIPS-16") == 0)
11074 mips_opts.mips16 = 0;
11075 else if (strncmp (name, "mips", 4) == 0)
11076 {
11077 int isa;
11078
11079 /* Permit the user to change the ISA on the fly. Needless to
11080 say, misuse can cause serious problems. */
11081 isa = atoi (name + 4);
11082 switch (isa)
11083 {
11084 case 0:
11085 mips_opts.gp32 = file_mips_gp32;
11086 mips_opts.fp32 = file_mips_fp32;
11087 mips_opts.abi = file_mips_abi;
11088 break;
11089 case 1:
11090 case 2:
11091 case 32:
11092 mips_opts.gp32 = 1;
11093 mips_opts.fp32 = 1;
11094 break;
11095 case 3:
11096 case 4:
11097 case 5:
11098 case 64:
11099 /* Loosen ABI register width restriction. */
11100 if (mips_opts.abi == O32_ABI)
11101 mips_opts.abi = NO_ABI;
11102 mips_opts.gp32 = 0;
11103 mips_opts.fp32 = 0;
11104 break;
11105 default:
11106 as_bad (_("unknown ISA level %s"), name + 4);
11107 break;
11108 }
11109
11110 switch (isa)
11111 {
11112 case 0: mips_opts.isa = file_mips_isa; break;
11113 case 1: mips_opts.isa = ISA_MIPS1; break;
11114 case 2: mips_opts.isa = ISA_MIPS2; break;
11115 case 3: mips_opts.isa = ISA_MIPS3; break;
11116 case 4: mips_opts.isa = ISA_MIPS4; break;
11117 case 5: mips_opts.isa = ISA_MIPS5; break;
11118 case 32: mips_opts.isa = ISA_MIPS32; break;
11119 case 64: mips_opts.isa = ISA_MIPS64; break;
11120 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11121 }
11122 }
11123 else if (strcmp (name, "autoextend") == 0)
11124 mips_opts.noautoextend = 0;
11125 else if (strcmp (name, "noautoextend") == 0)
11126 mips_opts.noautoextend = 1;
11127 else if (strcmp (name, "push") == 0)
11128 {
11129 struct mips_option_stack *s;
11130
11131 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11132 s->next = mips_opts_stack;
11133 s->options = mips_opts;
11134 mips_opts_stack = s;
11135 }
11136 else if (strcmp (name, "pop") == 0)
11137 {
11138 struct mips_option_stack *s;
11139
11140 s = mips_opts_stack;
11141 if (s == NULL)
11142 as_bad (_(".set pop with no .set push"));
11143 else
11144 {
11145 /* If we're changing the reorder mode we need to handle
11146 delay slots correctly. */
11147 if (s->options.noreorder && ! mips_opts.noreorder)
11148 mips_emit_delays (true);
11149 else if (! s->options.noreorder && mips_opts.noreorder)
11150 {
11151 if (prev_nop_frag != NULL)
11152 {
11153 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11154 * (mips_opts.mips16 ? 2 : 4));
11155 prev_nop_frag = NULL;
11156 }
11157 }
11158
11159 mips_opts = s->options;
11160 mips_opts_stack = s->next;
11161 free (s);
11162 }
11163 }
11164 else
11165 {
11166 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11167 }
11168 *input_line_pointer = ch;
11169 demand_empty_rest_of_line ();
11170 }
11171
11172 /* Handle the .abicalls pseudo-op. I believe this is equivalent to
11173 .option pic2. It means to generate SVR4 PIC calls. */
11174
11175 static void
11176 s_abicalls (ignore)
11177 int ignore ATTRIBUTE_UNUSED;
11178 {
11179 mips_pic = SVR4_PIC;
11180 if (USE_GLOBAL_POINTER_OPT)
11181 {
11182 if (g_switch_seen && g_switch_value != 0)
11183 as_warn (_("-G may not be used with SVR4 PIC code"));
11184 g_switch_value = 0;
11185 }
11186 bfd_set_gp_size (stdoutput, 0);
11187 demand_empty_rest_of_line ();
11188 }
11189
11190 /* Handle the .cpload pseudo-op. This is used when generating SVR4
11191 PIC code. It sets the $gp register for the function based on the
11192 function address, which is in the register named in the argument.
11193 This uses a relocation against _gp_disp, which is handled specially
11194 by the linker. The result is:
11195 lui $gp,%hi(_gp_disp)
11196 addiu $gp,$gp,%lo(_gp_disp)
11197 addu $gp,$gp,.cpload argument
11198 The .cpload argument is normally $25 == $t9. */
11199
11200 static void
11201 s_cpload (ignore)
11202 int ignore ATTRIBUTE_UNUSED;
11203 {
11204 expressionS ex;
11205 int icnt = 0;
11206
11207 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11208 .cpload is ignored. */
11209 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11210 {
11211 s_ignore (0);
11212 return;
11213 }
11214
11215 /* .cpload should be in a .set noreorder section. */
11216 if (mips_opts.noreorder == 0)
11217 as_warn (_(".cpload not in noreorder section"));
11218
11219 ex.X_op = O_symbol;
11220 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11221 ex.X_op_symbol = NULL;
11222 ex.X_add_number = 0;
11223
11224 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11225 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11226
11227 macro_build_lui (NULL, &icnt, &ex, GP);
11228 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11229 (int) BFD_RELOC_LO16);
11230
11231 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11232 GP, GP, tc_get_register (0));
11233
11234 demand_empty_rest_of_line ();
11235 }
11236
11237 /* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11238 .cpsetup $reg1, offset|$reg2, label
11239
11240 If offset is given, this results in:
11241 sd $gp, offset($sp)
11242 lui $gp, %gp_rel(%neg(%hi(label)))
11243 daddiu $gp, $gp, %gp_rel(%neg(%lo(label)))
11244 addu $gp, $gp, $reg1
11245
11246 If $reg2 is given, this results in:
11247 daddu $reg2, $gp, $0
11248 lui $gp, %gp_rel(%neg(%hi(label)))
11249 daddiu $gp, $gp, %gp_rel(%neg(%lo(label)))
11250 addu $gp, $gp, $reg1
11251 */
11252 static void
11253 s_cpsetup (ignore)
11254 int ignore ATTRIBUTE_UNUSED;
11255 {
11256 expressionS ex_off;
11257 expressionS ex_sym;
11258 int reg1;
11259 int icnt = 0;
11260 char *sym;
11261
11262 /* If we are not generating SVR4 PIC code, .cpload is ignored.
11263 We also need NewABI support. */
11264 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11265 {
11266 s_ignore (0);
11267 return;
11268 }
11269
11270 reg1 = tc_get_register (0);
11271 SKIP_WHITESPACE ();
11272 if (*input_line_pointer != ',')
11273 {
11274 as_bad (_("missing argument separator ',' for .cpsetup"));
11275 return;
11276 }
11277 else
11278 input_line_pointer++;
11279 SKIP_WHITESPACE ();
11280 if (*input_line_pointer == '$')
11281 mips_cpreturn_register = tc_get_register (0);
11282 else
11283 mips_cpreturn_offset = get_absolute_expression ();
11284 SKIP_WHITESPACE ();
11285 if (*input_line_pointer != ',')
11286 {
11287 as_bad (_("missing argument separator ',' for .cpsetup"));
11288 return;
11289 }
11290 else
11291 input_line_pointer++;
11292 SKIP_WHITESPACE ();
11293 sym = input_line_pointer;
11294 while (ISALNUM (*input_line_pointer))
11295 input_line_pointer++;
11296 *input_line_pointer = 0;
11297
11298 ex_sym.X_op = O_symbol;
11299 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11300 ex_sym.X_op_symbol = NULL;
11301 ex_sym.X_add_number = 0;
11302
11303 if (mips_cpreturn_register == -1)
11304 {
11305 ex_off.X_op = O_constant;
11306 ex_off.X_add_symbol = NULL;
11307 ex_off.X_op_symbol = NULL;
11308 ex_off.X_add_number = mips_cpreturn_offset;
11309
11310 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11311 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11312 }
11313 else
11314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11315 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11316
11317 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11318 (int) BFD_RELOC_GPREL16);
11319 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11320 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11321 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11322 NULL, 0, 0, BFD_RELOC_HI16_S);
11323 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11324 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11325 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11326 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11327 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11328 NULL, 0, 0, BFD_RELOC_LO16);
11329 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11330 "d,v,t", mips_gp_register, mips_gp_register, reg1);
11331
11332 demand_empty_rest_of_line ();
11333 }
11334
11335 static void
11336 s_cplocal (ignore)
11337 int ignore ATTRIBUTE_UNUSED;
11338 {
11339 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11340 .cplocal is ignored. */
11341 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11342 {
11343 s_ignore (0);
11344 return;
11345 }
11346
11347 mips_gp_register = tc_get_register (0);
11348 }
11349
11350 /* Handle the .cprestore pseudo-op. This stores $gp into a given
11351 offset from $sp. The offset is remembered, and after making a PIC
11352 call $gp is restored from that location. */
11353
11354 static void
11355 s_cprestore (ignore)
11356 int ignore ATTRIBUTE_UNUSED;
11357 {
11358 expressionS ex;
11359 int icnt = 0;
11360
11361 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11362 .cprestore is ignored. */
11363 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11364 {
11365 s_ignore (0);
11366 return;
11367 }
11368
11369 mips_cprestore_offset = get_absolute_expression ();
11370
11371 ex.X_op = O_constant;
11372 ex.X_add_symbol = NULL;
11373 ex.X_op_symbol = NULL;
11374 ex.X_add_number = mips_cprestore_offset;
11375
11376 macro_build ((char *) NULL, &icnt, &ex,
11377 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11378 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11379
11380 demand_empty_rest_of_line ();
11381 }
11382
11383 /* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11384 was given in the preceeding .gpsetup, it results in:
11385 ld $gp, offset($sp)
11386
11387 If a register $reg2 was given there, it results in:
11388 daddiu $gp, $gp, $reg2
11389 */
11390 static void
11391 s_cpreturn (ignore)
11392 int ignore ATTRIBUTE_UNUSED;
11393 {
11394 expressionS ex;
11395 int icnt = 0;
11396
11397 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11398 We also need NewABI support. */
11399 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11400 {
11401 s_ignore (0);
11402 return;
11403 }
11404
11405 if (mips_cpreturn_register == -1)
11406 {
11407 ex.X_op = O_constant;
11408 ex.X_add_symbol = NULL;
11409 ex.X_op_symbol = NULL;
11410 ex.X_add_number = mips_cpreturn_offset;
11411
11412 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11413 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11414 }
11415 else
11416 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11417 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11418
11419 demand_empty_rest_of_line ();
11420 }
11421
11422 /* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11423 code. It sets the offset to use in gp_rel relocations. */
11424
11425 static void
11426 s_gpvalue (ignore)
11427 int ignore ATTRIBUTE_UNUSED;
11428 {
11429 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11430 We also need NewABI support. */
11431 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11432 {
11433 s_ignore (0);
11434 return;
11435 }
11436
11437 mips_cpreturn_offset = get_absolute_expression ();
11438
11439 demand_empty_rest_of_line ();
11440 }
11441
11442 /* Handle the .gpword pseudo-op. This is used when generating PIC
11443 code. It generates a 32 bit GP relative reloc. */
11444
11445 static void
11446 s_gpword (ignore)
11447 int ignore ATTRIBUTE_UNUSED;
11448 {
11449 symbolS *label;
11450 expressionS ex;
11451 char *p;
11452
11453 /* When not generating PIC code, this is treated as .word. */
11454 if (mips_pic != SVR4_PIC)
11455 {
11456 s_cons (2);
11457 return;
11458 }
11459
11460 label = insn_labels != NULL ? insn_labels->label : NULL;
11461 mips_emit_delays (true);
11462 if (auto_align)
11463 mips_align (2, 0, label);
11464 mips_clear_insn_labels ();
11465
11466 expression (&ex);
11467
11468 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11469 {
11470 as_bad (_("Unsupported use of .gpword"));
11471 ignore_rest_of_line ();
11472 }
11473
11474 p = frag_more (4);
11475 md_number_to_chars (p, (valueT) 0, 4);
11476 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
11477 BFD_RELOC_GPREL32);
11478
11479 demand_empty_rest_of_line ();
11480 }
11481
11482 /* Handle the .cpadd pseudo-op. This is used when dealing with switch
11483 tables in SVR4 PIC code. */
11484
11485 static void
11486 s_cpadd (ignore)
11487 int ignore ATTRIBUTE_UNUSED;
11488 {
11489 int icnt = 0;
11490 int reg;
11491
11492 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11493 code. */
11494 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
11495 {
11496 s_ignore (0);
11497 return;
11498 }
11499
11500 /* Add $gp to the register named as an argument. */
11501 reg = tc_get_register (0);
11502 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11503 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
11504 "d,v,t", reg, reg, GP);
11505
11506 demand_empty_rest_of_line ();
11507 }
11508
11509 /* Handle the .insn pseudo-op. This marks instruction labels in
11510 mips16 mode. This permits the linker to handle them specially,
11511 such as generating jalx instructions when needed. We also make
11512 them odd for the duration of the assembly, in order to generate the
11513 right sort of code. We will make them even in the adjust_symtab
11514 routine, while leaving them marked. This is convenient for the
11515 debugger and the disassembler. The linker knows to make them odd
11516 again. */
11517
11518 static void
11519 s_insn (ignore)
11520 int ignore ATTRIBUTE_UNUSED;
11521 {
11522 if (mips_opts.mips16)
11523 mips16_mark_labels ();
11524
11525 demand_empty_rest_of_line ();
11526 }
11527
11528 /* Handle a .stabn directive. We need these in order to mark a label
11529 as being a mips16 text label correctly. Sometimes the compiler
11530 will emit a label, followed by a .stabn, and then switch sections.
11531 If the label and .stabn are in mips16 mode, then the label is
11532 really a mips16 text label. */
11533
11534 static void
11535 s_mips_stab (type)
11536 int type;
11537 {
11538 if (type == 'n' && mips_opts.mips16)
11539 mips16_mark_labels ();
11540
11541 s_stab (type);
11542 }
11543
11544 /* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11545 */
11546
11547 static void
11548 s_mips_weakext (ignore)
11549 int ignore ATTRIBUTE_UNUSED;
11550 {
11551 char *name;
11552 int c;
11553 symbolS *symbolP;
11554 expressionS exp;
11555
11556 name = input_line_pointer;
11557 c = get_symbol_end ();
11558 symbolP = symbol_find_or_make (name);
11559 S_SET_WEAK (symbolP);
11560 *input_line_pointer = c;
11561
11562 SKIP_WHITESPACE ();
11563
11564 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11565 {
11566 if (S_IS_DEFINED (symbolP))
11567 {
11568 as_bad ("Ignoring attempt to redefine symbol `%s'.",
11569 S_GET_NAME (symbolP));
11570 ignore_rest_of_line ();
11571 return;
11572 }
11573
11574 if (*input_line_pointer == ',')
11575 {
11576 ++input_line_pointer;
11577 SKIP_WHITESPACE ();
11578 }
11579
11580 expression (&exp);
11581 if (exp.X_op != O_symbol)
11582 {
11583 as_bad ("bad .weakext directive");
11584 ignore_rest_of_line();
11585 return;
11586 }
11587 symbol_set_value_expression (symbolP, &exp);
11588 }
11589
11590 demand_empty_rest_of_line ();
11591 }
11592
11593 /* Parse a register string into a number. Called from the ECOFF code
11594 to parse .frame. The argument is non-zero if this is the frame
11595 register, so that we can record it in mips_frame_reg. */
11596
11597 int
11598 tc_get_register (frame)
11599 int frame;
11600 {
11601 int reg;
11602
11603 SKIP_WHITESPACE ();
11604 if (*input_line_pointer++ != '$')
11605 {
11606 as_warn (_("expected `$'"));
11607 reg = 0;
11608 }
11609 else if (ISDIGIT (*input_line_pointer))
11610 {
11611 reg = get_absolute_expression ();
11612 if (reg < 0 || reg >= 32)
11613 {
11614 as_warn (_("Bad register number"));
11615 reg = 0;
11616 }
11617 }
11618 else
11619 {
11620 if (strncmp (input_line_pointer, "fp", 2) == 0)
11621 reg = FP;
11622 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11623 reg = SP;
11624 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11625 reg = GP;
11626 else if (strncmp (input_line_pointer, "at", 2) == 0)
11627 reg = AT;
11628 else
11629 {
11630 as_warn (_("Unrecognized register name"));
11631 reg = 0;
11632 }
11633 input_line_pointer += 2;
11634 }
11635 if (frame)
11636 mips_frame_reg = reg != 0 ? reg : SP;
11637 return reg;
11638 }
11639
11640 valueT
11641 md_section_align (seg, addr)
11642 asection *seg;
11643 valueT addr;
11644 {
11645 int align = bfd_get_section_alignment (stdoutput, seg);
11646
11647 #ifdef OBJ_ELF
11648 /* We don't need to align ELF sections to the full alignment.
11649 However, Irix 5 may prefer that we align them at least to a 16
11650 byte boundary. We don't bother to align the sections if we are
11651 targeted for an embedded system. */
11652 if (strcmp (TARGET_OS, "elf") == 0)
11653 return addr;
11654 if (align > 4)
11655 align = 4;
11656 #endif
11657
11658 return ((addr + (1 << align) - 1) & (-1 << align));
11659 }
11660
11661 /* Utility routine, called from above as well. If called while the
11662 input file is still being read, it's only an approximation. (For
11663 example, a symbol may later become defined which appeared to be
11664 undefined earlier.) */
11665
11666 static int
11667 nopic_need_relax (sym, before_relaxing)
11668 symbolS *sym;
11669 int before_relaxing;
11670 {
11671 if (sym == 0)
11672 return 0;
11673
11674 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
11675 {
11676 const char *symname;
11677 int change;
11678
11679 /* Find out whether this symbol can be referenced off the GP
11680 register. It can be if it is smaller than the -G size or if
11681 it is in the .sdata or .sbss section. Certain symbols can
11682 not be referenced off the GP, although it appears as though
11683 they can. */
11684 symname = S_GET_NAME (sym);
11685 if (symname != (const char *) NULL
11686 && (strcmp (symname, "eprol") == 0
11687 || strcmp (symname, "etext") == 0
11688 || strcmp (symname, "_gp") == 0
11689 || strcmp (symname, "edata") == 0
11690 || strcmp (symname, "_fbss") == 0
11691 || strcmp (symname, "_fdata") == 0
11692 || strcmp (symname, "_ftext") == 0
11693 || strcmp (symname, "end") == 0
11694 || strcmp (symname, "_gp_disp") == 0))
11695 change = 1;
11696 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11697 && (0
11698 #ifndef NO_ECOFF_DEBUGGING
11699 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11700 && (symbol_get_obj (sym)->ecoff_extern_size
11701 <= g_switch_value))
11702 #endif
11703 /* We must defer this decision until after the whole
11704 file has been read, since there might be a .extern
11705 after the first use of this symbol. */
11706 || (before_relaxing
11707 #ifndef NO_ECOFF_DEBUGGING
11708 && symbol_get_obj (sym)->ecoff_extern_size == 0
11709 #endif
11710 && S_GET_VALUE (sym) == 0)
11711 || (S_GET_VALUE (sym) != 0
11712 && S_GET_VALUE (sym) <= g_switch_value)))
11713 change = 0;
11714 else
11715 {
11716 const char *segname;
11717
11718 segname = segment_name (S_GET_SEGMENT (sym));
11719 assert (strcmp (segname, ".lit8") != 0
11720 && strcmp (segname, ".lit4") != 0);
11721 change = (strcmp (segname, ".sdata") != 0
11722 && strcmp (segname, ".sbss") != 0
11723 && strncmp (segname, ".sdata.", 7) != 0
11724 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
11725 }
11726 return change;
11727 }
11728 else
11729 /* We are not optimizing for the GP register. */
11730 return 1;
11731 }
11732
11733 /* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11734 extended opcode. SEC is the section the frag is in. */
11735
11736 static int
11737 mips16_extended_frag (fragp, sec, stretch)
11738 fragS *fragp;
11739 asection *sec;
11740 long stretch;
11741 {
11742 int type;
11743 register const struct mips16_immed_operand *op;
11744 offsetT val;
11745 int mintiny, maxtiny;
11746 segT symsec;
11747 fragS *sym_frag;
11748
11749 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11750 return 0;
11751 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
11752 return 1;
11753
11754 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
11755 op = mips16_immed_operands;
11756 while (op->type != type)
11757 {
11758 ++op;
11759 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
11760 }
11761
11762 if (op->unsp)
11763 {
11764 if (type == '<' || type == '>' || type == '[' || type == ']')
11765 {
11766 mintiny = 1;
11767 maxtiny = 1 << op->nbits;
11768 }
11769 else
11770 {
11771 mintiny = 0;
11772 maxtiny = (1 << op->nbits) - 1;
11773 }
11774 }
11775 else
11776 {
11777 mintiny = - (1 << (op->nbits - 1));
11778 maxtiny = (1 << (op->nbits - 1)) - 1;
11779 }
11780
11781 sym_frag = symbol_get_frag (fragp->fr_symbol);
11782 val = S_GET_VALUE (fragp->fr_symbol);
11783 symsec = S_GET_SEGMENT (fragp->fr_symbol);
11784
11785 if (op->pcrel)
11786 {
11787 addressT addr;
11788
11789 /* We won't have the section when we are called from
11790 mips_relax_frag. However, we will always have been called
11791 from md_estimate_size_before_relax first. If this is a
11792 branch to a different section, we mark it as such. If SEC is
11793 NULL, and the frag is not marked, then it must be a branch to
11794 the same section. */
11795 if (sec == NULL)
11796 {
11797 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
11798 return 1;
11799 }
11800 else
11801 {
11802 /* Must have been called from md_estimate_size_before_relax. */
11803 if (symsec != sec)
11804 {
11805 fragp->fr_subtype =
11806 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11807
11808 /* FIXME: We should support this, and let the linker
11809 catch branches and loads that are out of range. */
11810 as_bad_where (fragp->fr_file, fragp->fr_line,
11811 _("unsupported PC relative reference to different section"));
11812
11813 return 1;
11814 }
11815 if (fragp != sym_frag && sym_frag->fr_address == 0)
11816 /* Assume non-extended on the first relaxation pass.
11817 The address we have calculated will be bogus if this is
11818 a forward branch to another frag, as the forward frag
11819 will have fr_address == 0. */
11820 return 0;
11821 }
11822
11823 /* In this case, we know for sure that the symbol fragment is in
11824 the same section. If the relax_marker of the symbol fragment
11825 differs from the relax_marker of this fragment, we have not
11826 yet adjusted the symbol fragment fr_address. We want to add
11827 in STRETCH in order to get a better estimate of the address.
11828 This particularly matters because of the shift bits. */
11829 if (stretch != 0
11830 && sym_frag->relax_marker != fragp->relax_marker)
11831 {
11832 fragS *f;
11833
11834 /* Adjust stretch for any alignment frag. Note that if have
11835 been expanding the earlier code, the symbol may be
11836 defined in what appears to be an earlier frag. FIXME:
11837 This doesn't handle the fr_subtype field, which specifies
11838 a maximum number of bytes to skip when doing an
11839 alignment. */
11840 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
11841 {
11842 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
11843 {
11844 if (stretch < 0)
11845 stretch = - ((- stretch)
11846 & ~ ((1 << (int) f->fr_offset) - 1));
11847 else
11848 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
11849 if (stretch == 0)
11850 break;
11851 }
11852 }
11853 if (f != NULL)
11854 val += stretch;
11855 }
11856
11857 addr = fragp->fr_address + fragp->fr_fix;
11858
11859 /* The base address rules are complicated. The base address of
11860 a branch is the following instruction. The base address of a
11861 PC relative load or add is the instruction itself, but if it
11862 is in a delay slot (in which case it can not be extended) use
11863 the address of the instruction whose delay slot it is in. */
11864 if (type == 'p' || type == 'q')
11865 {
11866 addr += 2;
11867
11868 /* If we are currently assuming that this frag should be
11869 extended, then, the current address is two bytes
11870 higher. */
11871 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
11872 addr += 2;
11873
11874 /* Ignore the low bit in the target, since it will be set
11875 for a text label. */
11876 if ((val & 1) != 0)
11877 --val;
11878 }
11879 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
11880 addr -= 4;
11881 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
11882 addr -= 2;
11883
11884 val -= addr & ~ ((1 << op->shift) - 1);
11885
11886 /* Branch offsets have an implicit 0 in the lowest bit. */
11887 if (type == 'p' || type == 'q')
11888 val /= 2;
11889
11890 /* If any of the shifted bits are set, we must use an extended
11891 opcode. If the address depends on the size of this
11892 instruction, this can lead to a loop, so we arrange to always
11893 use an extended opcode. We only check this when we are in
11894 the main relaxation loop, when SEC is NULL. */
11895 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
11896 {
11897 fragp->fr_subtype =
11898 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11899 return 1;
11900 }
11901
11902 /* If we are about to mark a frag as extended because the value
11903 is precisely maxtiny + 1, then there is a chance of an
11904 infinite loop as in the following code:
11905 la $4,foo
11906 .skip 1020
11907 .align 2
11908 foo:
11909 In this case when the la is extended, foo is 0x3fc bytes
11910 away, so the la can be shrunk, but then foo is 0x400 away, so
11911 the la must be extended. To avoid this loop, we mark the
11912 frag as extended if it was small, and is about to become
11913 extended with a value of maxtiny + 1. */
11914 if (val == ((maxtiny + 1) << op->shift)
11915 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
11916 && sec == NULL)
11917 {
11918 fragp->fr_subtype =
11919 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
11920 return 1;
11921 }
11922 }
11923 else if (symsec != absolute_section && sec != NULL)
11924 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
11925
11926 if ((val & ((1 << op->shift) - 1)) != 0
11927 || val < (mintiny << op->shift)
11928 || val > (maxtiny << op->shift))
11929 return 1;
11930 else
11931 return 0;
11932 }
11933
11934 /* Estimate the size of a frag before relaxing. Unless this is the
11935 mips16, we are not really relaxing here, and the final size is
11936 encoded in the subtype information. For the mips16, we have to
11937 decide whether we are using an extended opcode or not. */
11938
11939 int
11940 md_estimate_size_before_relax (fragp, segtype)
11941 fragS *fragp;
11942 asection *segtype;
11943 {
11944 int change = 0;
11945 boolean linkonce = false;
11946
11947 if (RELAX_MIPS16_P (fragp->fr_subtype))
11948 {
11949 if (mips16_extended_frag (fragp, segtype, 0))
11950 {
11951 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
11952 return 4;
11953 }
11954 else
11955 {
11956 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
11957 return 2;
11958 }
11959 }
11960
11961 if (mips_pic == NO_PIC)
11962 {
11963 change = nopic_need_relax (fragp->fr_symbol, 0);
11964 }
11965 else if (mips_pic == SVR4_PIC)
11966 {
11967 symbolS *sym;
11968 asection *symsec;
11969
11970 sym = fragp->fr_symbol;
11971
11972 /* Handle the case of a symbol equated to another symbol. */
11973 while (symbol_equated_reloc_p (sym))
11974 {
11975 symbolS *n;
11976
11977 /* It's possible to get a loop here in a badly written
11978 program. */
11979 n = symbol_get_value_expression (sym)->X_add_symbol;
11980 if (n == sym)
11981 break;
11982 sym = n;
11983 }
11984
11985 symsec = S_GET_SEGMENT (sym);
11986
11987 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
11988 if (symsec != segtype && ! S_IS_LOCAL (sym))
11989 {
11990 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
11991 != 0)
11992 linkonce = true;
11993
11994 /* The GNU toolchain uses an extension for ELF: a section
11995 beginning with the magic string .gnu.linkonce is a linkonce
11996 section. */
11997 if (strncmp (segment_name (symsec), ".gnu.linkonce",
11998 sizeof ".gnu.linkonce" - 1) == 0)
11999 linkonce = true;
12000 }
12001
12002 /* This must duplicate the test in adjust_reloc_syms. */
12003 change = (symsec != &bfd_und_section
12004 && symsec != &bfd_abs_section
12005 && ! bfd_is_com_section (symsec)
12006 && !linkonce
12007 #ifdef OBJ_ELF
12008 /* A global or weak symbol is treated as external. */
12009 && (OUTPUT_FLAVOR == bfd_target_elf_flavour
12010 && ! (S_IS_EXTERN (sym) || S_IS_WEAK (sym)))
12011 #endif
12012 );
12013 }
12014 else
12015 abort ();
12016
12017 if (change)
12018 {
12019 /* Record the offset to the first reloc in the fr_opcode field.
12020 This lets md_convert_frag and tc_gen_reloc know that the code
12021 must be expanded. */
12022 fragp->fr_opcode = (fragp->fr_literal
12023 + fragp->fr_fix
12024 - RELAX_OLD (fragp->fr_subtype)
12025 + RELAX_RELOC1 (fragp->fr_subtype));
12026 /* FIXME: This really needs as_warn_where. */
12027 if (RELAX_WARN (fragp->fr_subtype))
12028 as_warn (_("AT used after \".set noat\" or macro used after "
12029 "\".set nomacro\""));
12030
12031 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
12032 }
12033
12034 return 0;
12035 }
12036
12037 /* This is called to see whether a reloc against a defined symbol
12038 should be converted into a reloc against a section. Don't adjust
12039 MIPS16 jump relocations, so we don't have to worry about the format
12040 of the offset in the .o file. Don't adjust relocations against
12041 mips16 symbols, so that the linker can find them if it needs to set
12042 up a stub. */
12043
12044 int
12045 mips_fix_adjustable (fixp)
12046 fixS *fixp;
12047 {
12048 #ifdef OBJ_ELF
12049 /* Prevent all adjustments to global symbols. */
12050 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12051 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
12052 return 0;
12053 #endif
12054 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12055 return 0;
12056 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12057 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12058 return 0;
12059 if (fixp->fx_addsy == NULL)
12060 return 1;
12061 #ifdef OBJ_ELF
12062 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12063 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12064 && fixp->fx_subsy == NULL)
12065 return 0;
12066 #endif
12067 return 1;
12068 }
12069
12070 /* Translate internal representation of relocation info to BFD target
12071 format. */
12072
12073 arelent **
12074 tc_gen_reloc (section, fixp)
12075 asection *section ATTRIBUTE_UNUSED;
12076 fixS *fixp;
12077 {
12078 static arelent *retval[4];
12079 arelent *reloc;
12080 bfd_reloc_code_real_type code;
12081
12082 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12083 retval[1] = NULL;
12084
12085 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12086 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12087 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12088
12089 if (mips_pic == EMBEDDED_PIC
12090 && SWITCH_TABLE (fixp))
12091 {
12092 /* For a switch table entry we use a special reloc. The addend
12093 is actually the difference between the reloc address and the
12094 subtrahend. */
12095 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12096 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12097 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12098 fixp->fx_r_type = BFD_RELOC_GPREL32;
12099 }
12100 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12101 reloc->addend = fixp->fx_addnumber;
12102 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12103 {
12104 /* We use a special addend for an internal RELLO reloc. */
12105 if (symbol_section_p (fixp->fx_addsy))
12106 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12107 else
12108 reloc->addend = fixp->fx_addnumber + reloc->address;
12109 }
12110 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12111 {
12112 assert (fixp->fx_next != NULL
12113 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
12114 /* We use a special addend for an internal RELHI reloc. The
12115 reloc is relative to the RELLO; adjust the addend
12116 accordingly. */
12117 if (symbol_section_p (fixp->fx_addsy))
12118 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12119 + fixp->fx_next->fx_where
12120 - S_GET_VALUE (fixp->fx_subsy));
12121 else
12122 reloc->addend = (fixp->fx_addnumber
12123 + fixp->fx_next->fx_frag->fr_address
12124 + fixp->fx_next->fx_where);
12125 }
12126 else
12127 {
12128 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12129 /* A gruesome hack which is a result of the gruesome gas reloc
12130 handling. */
12131 reloc->addend = reloc->address;
12132 else
12133 reloc->addend = -reloc->address;
12134 }
12135
12136 /* If this is a variant frag, we may need to adjust the existing
12137 reloc and generate a new one. */
12138 if (fixp->fx_frag->fr_opcode != NULL
12139 && (fixp->fx_r_type == BFD_RELOC_GPREL16
12140 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12141 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12142 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12143 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12144 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12145 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12146 && ! HAVE_NEWABI)
12147 {
12148 arelent *reloc2;
12149
12150 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12151
12152 /* If this is not the last reloc in this frag, then we have two
12153 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12154 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12155 the second one handle all of them. */
12156 if (fixp->fx_next != NULL
12157 && fixp->fx_frag == fixp->fx_next->fx_frag)
12158 {
12159 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12160 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
12161 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12162 && (fixp->fx_next->fx_r_type
12163 == BFD_RELOC_MIPS_GOT_LO16))
12164 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12165 && (fixp->fx_next->fx_r_type
12166 == BFD_RELOC_MIPS_CALL_LO16)));
12167 retval[0] = NULL;
12168 return retval;
12169 }
12170
12171 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12172 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12173 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12174 retval[2] = NULL;
12175 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12176 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
12177 reloc2->address = (reloc->address
12178 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12179 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12180 reloc2->addend = fixp->fx_addnumber;
12181 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12182 assert (reloc2->howto != NULL);
12183
12184 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12185 {
12186 arelent *reloc3;
12187
12188 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12189 retval[3] = NULL;
12190 *reloc3 = *reloc2;
12191 reloc3->address += 4;
12192 }
12193
12194 if (mips_pic == NO_PIC)
12195 {
12196 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
12197 fixp->fx_r_type = BFD_RELOC_HI16_S;
12198 }
12199 else if (mips_pic == SVR4_PIC)
12200 {
12201 switch (fixp->fx_r_type)
12202 {
12203 default:
12204 abort ();
12205 case BFD_RELOC_MIPS_GOT16:
12206 break;
12207 case BFD_RELOC_MIPS_CALL16:
12208 case BFD_RELOC_MIPS_GOT_LO16:
12209 case BFD_RELOC_MIPS_CALL_LO16:
12210 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12211 break;
12212 }
12213 }
12214 else
12215 abort ();
12216 }
12217
12218 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12219 to be used in the relocation's section offset. */
12220 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12221 {
12222 reloc->address = reloc->addend;
12223 reloc->addend = 0;
12224 }
12225
12226 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12227 fixup_segment converted a non-PC relative reloc into a PC
12228 relative reloc. In such a case, we need to convert the reloc
12229 code. */
12230 code = fixp->fx_r_type;
12231 if (fixp->fx_pcrel)
12232 {
12233 switch (code)
12234 {
12235 case BFD_RELOC_8:
12236 code = BFD_RELOC_8_PCREL;
12237 break;
12238 case BFD_RELOC_16:
12239 code = BFD_RELOC_16_PCREL;
12240 break;
12241 case BFD_RELOC_32:
12242 code = BFD_RELOC_32_PCREL;
12243 break;
12244 case BFD_RELOC_64:
12245 code = BFD_RELOC_64_PCREL;
12246 break;
12247 case BFD_RELOC_8_PCREL:
12248 case BFD_RELOC_16_PCREL:
12249 case BFD_RELOC_32_PCREL:
12250 case BFD_RELOC_64_PCREL:
12251 case BFD_RELOC_16_PCREL_S2:
12252 case BFD_RELOC_PCREL_HI16_S:
12253 case BFD_RELOC_PCREL_LO16:
12254 break;
12255 default:
12256 as_bad_where (fixp->fx_file, fixp->fx_line,
12257 _("Cannot make %s relocation PC relative"),
12258 bfd_get_reloc_code_name (code));
12259 }
12260 }
12261
12262 #ifdef OBJ_ELF
12263 /* md_apply_fix3 has a double-subtraction hack to get
12264 bfd_install_relocation to behave nicely. GPREL relocations are
12265 handled correctly without this hack, so undo it here. We can't
12266 stop md_apply_fix3 from subtracting twice in the first place since
12267 the fake addend is required for variant frags above. */
12268 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
12269 && code == BFD_RELOC_MIPS_GPREL
12270 && reloc->addend != 0
12271 && mips_need_elf_addend_fixup (fixp))
12272 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12273 #endif
12274
12275 /* To support a PC relative reloc when generating embedded PIC code
12276 for ECOFF, we use a Cygnus extension. We check for that here to
12277 make sure that we don't let such a reloc escape normally. */
12278 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12279 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12280 && code == BFD_RELOC_16_PCREL_S2
12281 && mips_pic != EMBEDDED_PIC)
12282 reloc->howto = NULL;
12283 else
12284 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12285
12286 if (reloc->howto == NULL)
12287 {
12288 as_bad_where (fixp->fx_file, fixp->fx_line,
12289 _("Can not represent %s relocation in this object file format"),
12290 bfd_get_reloc_code_name (code));
12291 retval[0] = NULL;
12292 }
12293
12294 return retval;
12295 }
12296
12297 /* Relax a machine dependent frag. This returns the amount by which
12298 the current size of the frag should change. */
12299
12300 int
12301 mips_relax_frag (fragp, stretch)
12302 fragS *fragp;
12303 long stretch;
12304 {
12305 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12306 return 0;
12307
12308 if (mips16_extended_frag (fragp, NULL, stretch))
12309 {
12310 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12311 return 0;
12312 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12313 return 2;
12314 }
12315 else
12316 {
12317 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12318 return 0;
12319 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12320 return -2;
12321 }
12322
12323 return 0;
12324 }
12325
12326 /* Convert a machine dependent frag. */
12327
12328 void
12329 md_convert_frag (abfd, asec, fragp)
12330 bfd *abfd ATTRIBUTE_UNUSED;
12331 segT asec;
12332 fragS *fragp;
12333 {
12334 int old, new;
12335 char *fixptr;
12336
12337 if (RELAX_MIPS16_P (fragp->fr_subtype))
12338 {
12339 int type;
12340 register const struct mips16_immed_operand *op;
12341 boolean small, ext;
12342 offsetT val;
12343 bfd_byte *buf;
12344 unsigned long insn;
12345 boolean use_extend;
12346 unsigned short extend;
12347
12348 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12349 op = mips16_immed_operands;
12350 while (op->type != type)
12351 ++op;
12352
12353 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12354 {
12355 small = false;
12356 ext = true;
12357 }
12358 else
12359 {
12360 small = true;
12361 ext = false;
12362 }
12363
12364 resolve_symbol_value (fragp->fr_symbol);
12365 val = S_GET_VALUE (fragp->fr_symbol);
12366 if (op->pcrel)
12367 {
12368 addressT addr;
12369
12370 addr = fragp->fr_address + fragp->fr_fix;
12371
12372 /* The rules for the base address of a PC relative reloc are
12373 complicated; see mips16_extended_frag. */
12374 if (type == 'p' || type == 'q')
12375 {
12376 addr += 2;
12377 if (ext)
12378 addr += 2;
12379 /* Ignore the low bit in the target, since it will be
12380 set for a text label. */
12381 if ((val & 1) != 0)
12382 --val;
12383 }
12384 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12385 addr -= 4;
12386 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12387 addr -= 2;
12388
12389 addr &= ~ (addressT) ((1 << op->shift) - 1);
12390 val -= addr;
12391
12392 /* Make sure the section winds up with the alignment we have
12393 assumed. */
12394 if (op->shift > 0)
12395 record_alignment (asec, op->shift);
12396 }
12397
12398 if (ext
12399 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12400 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12401 as_warn_where (fragp->fr_file, fragp->fr_line,
12402 _("extended instruction in delay slot"));
12403
12404 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12405
12406 if (target_big_endian)
12407 insn = bfd_getb16 (buf);
12408 else
12409 insn = bfd_getl16 (buf);
12410
12411 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12412 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12413 small, ext, &insn, &use_extend, &extend);
12414
12415 if (use_extend)
12416 {
12417 md_number_to_chars (buf, 0xf000 | extend, 2);
12418 fragp->fr_fix += 2;
12419 buf += 2;
12420 }
12421
12422 md_number_to_chars (buf, insn, 2);
12423 fragp->fr_fix += 2;
12424 buf += 2;
12425 }
12426 else
12427 {
12428 if (fragp->fr_opcode == NULL)
12429 return;
12430
12431 old = RELAX_OLD (fragp->fr_subtype);
12432 new = RELAX_NEW (fragp->fr_subtype);
12433 fixptr = fragp->fr_literal + fragp->fr_fix;
12434
12435 if (new > 0)
12436 memcpy (fixptr - old, fixptr, new);
12437
12438 fragp->fr_fix += new - old;
12439 }
12440 }
12441
12442 #ifdef OBJ_ELF
12443
12444 /* This function is called after the relocs have been generated.
12445 We've been storing mips16 text labels as odd. Here we convert them
12446 back to even for the convenience of the debugger. */
12447
12448 void
12449 mips_frob_file_after_relocs ()
12450 {
12451 asymbol **syms;
12452 unsigned int count, i;
12453
12454 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12455 return;
12456
12457 syms = bfd_get_outsymbols (stdoutput);
12458 count = bfd_get_symcount (stdoutput);
12459 for (i = 0; i < count; i++, syms++)
12460 {
12461 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12462 && ((*syms)->value & 1) != 0)
12463 {
12464 (*syms)->value &= ~1;
12465 /* If the symbol has an odd size, it was probably computed
12466 incorrectly, so adjust that as well. */
12467 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12468 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12469 }
12470 }
12471 }
12472
12473 #endif
12474
12475 /* This function is called whenever a label is defined. It is used
12476 when handling branch delays; if a branch has a label, we assume we
12477 can not move it. */
12478
12479 void
12480 mips_define_label (sym)
12481 symbolS *sym;
12482 {
12483 struct insn_label_list *l;
12484
12485 if (free_insn_labels == NULL)
12486 l = (struct insn_label_list *) xmalloc (sizeof *l);
12487 else
12488 {
12489 l = free_insn_labels;
12490 free_insn_labels = l->next;
12491 }
12492
12493 l->label = sym;
12494 l->next = insn_labels;
12495 insn_labels = l;
12496 }
12497 \f
12498 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12499
12500 /* Some special processing for a MIPS ELF file. */
12501
12502 void
12503 mips_elf_final_processing ()
12504 {
12505 /* Write out the register information. */
12506 if (! HAVE_NEWABI)
12507 {
12508 Elf32_RegInfo s;
12509
12510 s.ri_gprmask = mips_gprmask;
12511 s.ri_cprmask[0] = mips_cprmask[0];
12512 s.ri_cprmask[1] = mips_cprmask[1];
12513 s.ri_cprmask[2] = mips_cprmask[2];
12514 s.ri_cprmask[3] = mips_cprmask[3];
12515 /* The gp_value field is set by the MIPS ELF backend. */
12516
12517 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12518 ((Elf32_External_RegInfo *)
12519 mips_regmask_frag));
12520 }
12521 else
12522 {
12523 Elf64_Internal_RegInfo s;
12524
12525 s.ri_gprmask = mips_gprmask;
12526 s.ri_pad = 0;
12527 s.ri_cprmask[0] = mips_cprmask[0];
12528 s.ri_cprmask[1] = mips_cprmask[1];
12529 s.ri_cprmask[2] = mips_cprmask[2];
12530 s.ri_cprmask[3] = mips_cprmask[3];
12531 /* The gp_value field is set by the MIPS ELF backend. */
12532
12533 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12534 ((Elf64_External_RegInfo *)
12535 mips_regmask_frag));
12536 }
12537
12538 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12539 sort of BFD interface for this. */
12540 if (mips_any_noreorder)
12541 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12542 if (mips_pic != NO_PIC)
12543 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12544
12545 /* Set the MIPS ELF ABI flags. */
12546 if (file_mips_abi == NO_ABI)
12547 ;
12548 else if (file_mips_abi == O32_ABI)
12549 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
12550 else if (file_mips_abi == O64_ABI)
12551 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
12552 else if (file_mips_abi == EABI_ABI)
12553 {
12554 if (mips_eabi64)
12555 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12556 else
12557 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12558 }
12559 else if (file_mips_abi == N32_ABI)
12560 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12561
12562 /* Nothing to do for "64". */
12563
12564 if (mips_32bitmode)
12565 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12566 }
12567
12568 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12569 \f
12570 typedef struct proc {
12571 symbolS *isym;
12572 unsigned long reg_mask;
12573 unsigned long reg_offset;
12574 unsigned long fpreg_mask;
12575 unsigned long fpreg_offset;
12576 unsigned long frame_offset;
12577 unsigned long frame_reg;
12578 unsigned long pc_reg;
12579 } procS;
12580
12581 static procS cur_proc;
12582 static procS *cur_proc_ptr;
12583 static int numprocs;
12584
12585 /* Fill in an rs_align_code fragment. */
12586
12587 void
12588 mips_handle_align (fragp)
12589 fragS *fragp;
12590 {
12591 if (fragp->fr_type != rs_align_code)
12592 return;
12593
12594 if (mips_opts.mips16)
12595 {
12596 static const unsigned char be_nop[] = { 0x65, 0x00 };
12597 static const unsigned char le_nop[] = { 0x00, 0x65 };
12598
12599 int bytes;
12600 char *p;
12601
12602 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12603 p = fragp->fr_literal + fragp->fr_fix;
12604
12605 if (bytes & 1)
12606 {
12607 *p++ = 0;
12608 fragp->fr_fix += 1;
12609 }
12610
12611 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12612 fragp->fr_var = 2;
12613 }
12614
12615 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
12616 }
12617
12618 static void
12619 md_obj_begin ()
12620 {
12621 }
12622
12623 static void
12624 md_obj_end ()
12625 {
12626 /* check for premature end, nesting errors, etc */
12627 if (cur_proc_ptr)
12628 as_warn (_("missing .end at end of assembly"));
12629 }
12630
12631 static long
12632 get_number ()
12633 {
12634 int negative = 0;
12635 long val = 0;
12636
12637 if (*input_line_pointer == '-')
12638 {
12639 ++input_line_pointer;
12640 negative = 1;
12641 }
12642 if (!ISDIGIT (*input_line_pointer))
12643 as_bad (_("Expected simple number."));
12644 if (input_line_pointer[0] == '0')
12645 {
12646 if (input_line_pointer[1] == 'x')
12647 {
12648 input_line_pointer += 2;
12649 while (ISXDIGIT (*input_line_pointer))
12650 {
12651 val <<= 4;
12652 val |= hex_value (*input_line_pointer++);
12653 }
12654 return negative ? -val : val;
12655 }
12656 else
12657 {
12658 ++input_line_pointer;
12659 while (ISDIGIT (*input_line_pointer))
12660 {
12661 val <<= 3;
12662 val |= *input_line_pointer++ - '0';
12663 }
12664 return negative ? -val : val;
12665 }
12666 }
12667 if (!ISDIGIT (*input_line_pointer))
12668 {
12669 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12670 *input_line_pointer, *input_line_pointer);
12671 as_warn (_("Invalid number"));
12672 return -1;
12673 }
12674 while (ISDIGIT (*input_line_pointer))
12675 {
12676 val *= 10;
12677 val += *input_line_pointer++ - '0';
12678 }
12679 return negative ? -val : val;
12680 }
12681
12682 /* The .file directive; just like the usual .file directive, but there
12683 is an initial number which is the ECOFF file index. */
12684
12685 static void
12686 s_file (x)
12687 int x ATTRIBUTE_UNUSED;
12688 {
12689 int line;
12690
12691 line = get_number ();
12692 s_app_file (0);
12693 }
12694
12695 /* The .end directive. */
12696
12697 static void
12698 s_mips_end (x)
12699 int x ATTRIBUTE_UNUSED;
12700 {
12701 symbolS *p;
12702 int maybe_text;
12703
12704 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12705 {
12706 p = get_symbol ();
12707 demand_empty_rest_of_line ();
12708 }
12709 else
12710 p = NULL;
12711
12712 #ifdef BFD_ASSEMBLER
12713 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12714 maybe_text = 1;
12715 else
12716 maybe_text = 0;
12717 #else
12718 if (now_seg != data_section && now_seg != bss_section)
12719 maybe_text = 1;
12720 else
12721 maybe_text = 0;
12722 #endif
12723
12724 if (!maybe_text)
12725 as_warn (_(".end not in text section"));
12726
12727 if (!cur_proc_ptr)
12728 {
12729 as_warn (_(".end directive without a preceding .ent directive."));
12730 demand_empty_rest_of_line ();
12731 return;
12732 }
12733
12734 if (p != NULL)
12735 {
12736 assert (S_GET_NAME (p));
12737 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
12738 as_warn (_(".end symbol does not match .ent symbol."));
12739 }
12740 else
12741 as_warn (_(".end directive missing or unknown symbol"));
12742
12743 #ifdef MIPS_STABS_ELF
12744 {
12745 segT saved_seg = now_seg;
12746 subsegT saved_subseg = now_subseg;
12747 valueT dot;
12748 expressionS exp;
12749 char *fragp;
12750
12751 dot = frag_now_fix ();
12752
12753 #ifdef md_flush_pending_output
12754 md_flush_pending_output ();
12755 #endif
12756
12757 assert (pdr_seg);
12758 subseg_set (pdr_seg, 0);
12759
12760 /* Write the symbol. */
12761 exp.X_op = O_symbol;
12762 exp.X_add_symbol = p;
12763 exp.X_add_number = 0;
12764 emit_expr (&exp, 4);
12765
12766 fragp = frag_more (7 * 4);
12767
12768 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
12769 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
12770 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
12771 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
12772 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
12773 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
12774 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
12775
12776 subseg_set (saved_seg, saved_subseg);
12777 }
12778 #endif
12779
12780 cur_proc_ptr = NULL;
12781 }
12782
12783 /* The .aent and .ent directives. */
12784
12785 static void
12786 s_mips_ent (aent)
12787 int aent;
12788 {
12789 int number = 0;
12790 symbolS *symbolP;
12791 int maybe_text;
12792
12793 symbolP = get_symbol ();
12794 if (*input_line_pointer == ',')
12795 input_line_pointer++;
12796 SKIP_WHITESPACE ();
12797 if (ISDIGIT (*input_line_pointer)
12798 || *input_line_pointer == '-')
12799 number = get_number ();
12800
12801 #ifdef BFD_ASSEMBLER
12802 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12803 maybe_text = 1;
12804 else
12805 maybe_text = 0;
12806 #else
12807 if (now_seg != data_section && now_seg != bss_section)
12808 maybe_text = 1;
12809 else
12810 maybe_text = 0;
12811 #endif
12812
12813 if (!maybe_text)
12814 as_warn (_(".ent or .aent not in text section."));
12815
12816 if (!aent && cur_proc_ptr)
12817 as_warn (_("missing .end"));
12818
12819 if (!aent)
12820 {
12821 cur_proc_ptr = &cur_proc;
12822 memset (cur_proc_ptr, '\0', sizeof (procS));
12823
12824 cur_proc_ptr->isym = symbolP;
12825
12826 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
12827
12828 numprocs++;
12829 }
12830
12831 demand_empty_rest_of_line ();
12832 }
12833
12834 /* The .frame directive. If the mdebug section is present (IRIX 5 native)
12835 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
12836 s_mips_frame is used so that we can set the PDR information correctly.
12837 We can't use the ecoff routines because they make reference to the ecoff
12838 symbol table (in the mdebug section). */
12839
12840 static void
12841 s_mips_frame (ignore)
12842 int ignore ATTRIBUTE_UNUSED;
12843 {
12844 #ifdef MIPS_STABS_ELF
12845
12846 long val;
12847
12848 if (cur_proc_ptr == (procS *) NULL)
12849 {
12850 as_warn (_(".frame outside of .ent"));
12851 demand_empty_rest_of_line ();
12852 return;
12853 }
12854
12855 cur_proc_ptr->frame_reg = tc_get_register (1);
12856
12857 SKIP_WHITESPACE ();
12858 if (*input_line_pointer++ != ','
12859 || get_absolute_expression_and_terminator (&val) != ',')
12860 {
12861 as_warn (_("Bad .frame directive"));
12862 --input_line_pointer;
12863 demand_empty_rest_of_line ();
12864 return;
12865 }
12866
12867 cur_proc_ptr->frame_offset = val;
12868 cur_proc_ptr->pc_reg = tc_get_register (0);
12869
12870 demand_empty_rest_of_line ();
12871 #else
12872 s_ignore (ignore);
12873 #endif /* MIPS_STABS_ELF */
12874 }
12875
12876 /* The .fmask and .mask directives. If the mdebug section is present
12877 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
12878 embedded targets, s_mips_mask is used so that we can set the PDR
12879 information correctly. We can't use the ecoff routines because they
12880 make reference to the ecoff symbol table (in the mdebug section). */
12881
12882 static void
12883 s_mips_mask (reg_type)
12884 char reg_type;
12885 {
12886 #ifdef MIPS_STABS_ELF
12887 long mask, off;
12888
12889 if (cur_proc_ptr == (procS *) NULL)
12890 {
12891 as_warn (_(".mask/.fmask outside of .ent"));
12892 demand_empty_rest_of_line ();
12893 return;
12894 }
12895
12896 if (get_absolute_expression_and_terminator (&mask) != ',')
12897 {
12898 as_warn (_("Bad .mask/.fmask directive"));
12899 --input_line_pointer;
12900 demand_empty_rest_of_line ();
12901 return;
12902 }
12903
12904 off = get_absolute_expression ();
12905
12906 if (reg_type == 'F')
12907 {
12908 cur_proc_ptr->fpreg_mask = mask;
12909 cur_proc_ptr->fpreg_offset = off;
12910 }
12911 else
12912 {
12913 cur_proc_ptr->reg_mask = mask;
12914 cur_proc_ptr->reg_offset = off;
12915 }
12916
12917 demand_empty_rest_of_line ();
12918 #else
12919 s_ignore (reg_type);
12920 #endif /* MIPS_STABS_ELF */
12921 }
12922
12923 /* The .loc directive. */
12924
12925 #if 0
12926 static void
12927 s_loc (x)
12928 int x;
12929 {
12930 symbolS *symbolP;
12931 int lineno;
12932 int addroff;
12933
12934 assert (now_seg == text_section);
12935
12936 lineno = get_number ();
12937 addroff = frag_now_fix ();
12938
12939 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
12940 S_SET_TYPE (symbolP, N_SLINE);
12941 S_SET_OTHER (symbolP, 0);
12942 S_SET_DESC (symbolP, lineno);
12943 symbolP->sy_segment = now_seg;
12944 }
12945 #endif
12946
12947 /* CPU name/ISA/number mapping table.
12948
12949 Entries are grouped by type. The first matching CPU or ISA entry
12950 gets chosen by CPU or ISA, so it should be the 'canonical' name
12951 for that type. Entries after that within the type are sorted
12952 alphabetically.
12953
12954 Case is ignored in comparison, so put the canonical entry in the
12955 appropriate case but everything else in lower case to ease eye pain. */
12956 static const struct mips_cpu_info mips_cpu_info_table[] =
12957 {
12958 /* MIPS1 ISA */
12959 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
12960 { "mips", 1, ISA_MIPS1, CPU_R3000, },
12961
12962 /* MIPS2 ISA */
12963 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
12964
12965 /* MIPS3 ISA */
12966 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
12967
12968 /* MIPS4 ISA */
12969 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
12970
12971 /* MIPS5 ISA */
12972 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
12973 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
12974
12975 /* MIPS32 ISA */
12976 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
12977 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
12978 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
12979 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
12980 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
12981 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
12982
12983 /* For historical reasons. */
12984 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
12985
12986 /* MIPS64 ISA */
12987 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
12988 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
12989 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
12990 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
12991
12992 /* R2000 CPU */
12993 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
12994 { "2000", 0, ISA_MIPS1, CPU_R2000, },
12995 { "2k", 0, ISA_MIPS1, CPU_R2000, },
12996 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
12997
12998 /* R3000 CPU */
12999 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13000 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13001 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13002 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13003
13004 /* TX3900 CPU */
13005 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13006 { "3900", 0, ISA_MIPS1, CPU_R3900, },
13007 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
13008
13009 /* R4000 CPU */
13010 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13011 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13012 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13013 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13014
13015 /* R4010 CPU */
13016 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13017 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13018
13019 /* R4400 CPU */
13020 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13021 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13022
13023 /* R4600 CPU */
13024 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13025 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13026 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13027 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13028
13029 /* R4650 CPU */
13030 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13031 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13032
13033 /* R6000 CPU */
13034 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13035 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13036 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13037 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13038
13039 /* R8000 CPU */
13040 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13041 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13042 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13043 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13044
13045 /* R10000 CPU */
13046 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13047 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13048 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13049 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13050
13051 /* R12000 CPU */
13052 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13053 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13054 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13055 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13056
13057 /* VR4100 CPU */
13058 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13059 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13060 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13061 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13062
13063 /* VR4111 CPU */
13064 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13065 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13066 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13067 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13068
13069 /* VR4300 CPU */
13070 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13071 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13072 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13073 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13074
13075 /* VR5000 CPU */
13076 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13077 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13078 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13079 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13080 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13081 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
13082 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
13083 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
13084 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
13085 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
13086 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
13087 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
13088 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
13089 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
13090 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
13091 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13092 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13093
13094 /* Broadcom SB-1 CPU */
13095 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
13096 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
13097 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13098 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13099
13100 /* End marker. */
13101 { NULL, 0, 0, 0, },
13102 };
13103
13104 static const struct mips_cpu_info *
13105 mips_cpu_info_from_name (name)
13106 const char *name;
13107 {
13108 int i;
13109
13110 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13111 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
13112 return (&mips_cpu_info_table[i]);
13113
13114 return NULL;
13115 }
13116
13117 static const struct mips_cpu_info *
13118 mips_cpu_info_from_isa (isa)
13119 int isa;
13120 {
13121 int i;
13122
13123 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13124 if (mips_cpu_info_table[i].is_isa
13125 && isa == mips_cpu_info_table[i].isa)
13126 return (&mips_cpu_info_table[i]);
13127
13128 return NULL;
13129 }
13130
13131 static const struct mips_cpu_info *
13132 mips_cpu_info_from_cpu (cpu)
13133 int cpu;
13134 {
13135 int i;
13136
13137 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13138 if (!mips_cpu_info_table[i].is_isa
13139 && cpu == mips_cpu_info_table[i].cpu)
13140 return (&mips_cpu_info_table[i]);
13141
13142 return NULL;
13143 }
This page took 0.394962 seconds and 5 git commands to generate.