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